Debian Codegraph @

camlibs/ax203/README.ax203-compression

raw
1 History of the reverse engineering of the ax203 compression
2 -----------------------------------------------------------
3
4 There are 4 (known) different compression algorithms:
5
6 YUV This algorithm uses a simple YUV format, with the UV members
7 subsampled at a 2x2 resolution. It uses 5 bits for the Y
8 values and 6 bits for the U and V values, cramming 4 pixels
9 into 4 bytes. This is used with firmware v3.3.x (usb-id
10 1908:1315) and v3.4.x frames (usb-id 1908:1320)
11
12 YUV-DELTA This version uses a less simple YUV format, it still
13 subsamples UV at a 2x2 resolution. It then further uses a
14 delta compression to store 4 values into 2 bytes. Data is
15 stored in to 4x4 pixels block, Using 6 x 2 bytes, 4 x 4 Y
16 pixels + 4 U and 4 V pixels. This is used with firmware v3.3.x
17 (usb-id 1908:1315) and v3.4.x frames (usb-id 1908:1320)
18
19 JPEG (ax3003) Regular JPEG
20
21 JPEG (ax206) Uses a JPEG derived format reverse engineering this one
22 was a challenge, see below for the full story. This is used with
23 ax206 based firmware v3.5.x frames (usb-id 1908:0102)
24
25 After some failed attempts by me (Hans de Goede), I asked help
26 from Bertrik Sikken with reverse engineering the JPEG compression.
27 He went the route of disassembling the windows binaries for uploading
28 pictures, and wrote an algorithm description of the decompression on
29 the picframe wiki:
30 http://picframe.spritesserver.nl/wiki/index.php/ImageEncodingAx206
31
32 A copy of this page is included below.
33
34 I used this description to write decompression code. For the decompression
35 code I started with tinyjpeg, which with I'm familiar from my libv4l
36 work. The ax203 camlib contains a modified tinyjpeg copy which was
37 modified to work with ax203's JPEG flavor. Tinyjpeg is:
38
39 /*
40 * Small jpeg decoder library
41 *
42 * Copyright (c) 2006, Luc Saillard <[email protected]>
43 * All rights reserved.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions are met:
46 *
47 * - Redistributions of source code must retain the above copyright notice,
48 * this list of conditions and the following disclaimer.
49 *
50 * - Redistributions in binary form must reproduce the above copyright notice,
51 * this list of conditions and the following disclaimer in the documentation
52 * and/or other materials provided with the distribution.
53 *
54 * - Neither the name of the author nor the names of its contributors may be
55 * used to endorse or promote products derived from this software without
56 * specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
59 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
62 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
69 *
70 */
71
72 Note that tinyjpeg includes jidctflt.c from libjpeg, see below for
73 libjpeg's copyright info.
74
75 Once I had working decompression code I needed to write compression code,
76 as tinyjpeg does not do compression I turned to libjpeg. I did not want
77 to include a modified libjpeg and luckily with some tricks it was not needed
78 to include libjpeg code other then jpeg_memsrcdest.c, which is not part of
79 the standard libjpeg distribution. This code is:
80
81 /*
82 * jpeg_memsrcdest.c and jidctflt.c
83 *
84 * Copyright (C) 1994-1998, Thomas G. Lane.
85 * This file is part of the Independent JPEG Group's software.
86 *
87 * The authors make NO WARRANTY or representation, either express or implied,
88 * with respect to this software, its quality, accuracy, merchantability, or
89 * fitness for a particular purpose. This software is provided "AS IS", and you,
90 * its user, assume the entire risk as to its quality and accuracy.
91 *
92 * This software is copyright (C) 1991-1998, Thomas G. Lane.
93 * All Rights Reserved except as specified below.
94 *
95 * Permission is hereby granted to use, copy, modify, and distribute this
96 * software (or portions thereof) for any purpose, without fee, subject to these
97 * conditions:
98 * (1) If any part of the source code for this software is distributed, then this
99 * README file must be included, with this copyright and no-warranty notice
100 * unaltered; and any additions, deletions, or changes to the original files
101 * must be clearly indicated in accompanying documentation.
102 * (2) If only executable code is distributed, then the accompanying
103 * documentation must state that "this software is based in part on the work of
104 * the Independent JPEG Group".
105 * (3) Permission for use of this software is granted only if the user accepts
106 * full responsibility for any undesirable consequences; the authors accept
107 * NO LIABILITY for damages of any kind.
108 *
109 * These conditions apply to any software derived from or based on the IJG code,
110 * not just to the unmodified library. If you use our work, you ought to
111 * acknowledge us.
112 *
113 * Permission is NOT granted for the use of any IJG author's name or company name
114 * in advertising or publicity relating to this software or products derived from
115 * it. This software may be referred to only as "the Independent JPEG Group's
116 * software".
117 *
118 * We specifically permit and encourage the use of this software as the basis of
119 * commercial products, provided that all warranty or liability claims are
120 * assumed by the product vendor.
121 */
122
123 Getting the JPEG compression working was not the end of the story, after this
124 I could create .dpf files which the windows software would read. But the files
125 had a "mystery" block in them between the 16 byte header and the quantisation
126 tables, and the picframe does not grok the files without the mystery block
127 containing the proper data. It turns out this mystery block contains an 8 byte
128 info block per MCU which makes it possible to start decoding with a random
129 MCU, rather then needing to do the decode linearly. This way the picture frame
130 can do transition effects. The info in each MCU info block consists of
131 last DC vals for the 3 components and the offset in the huffman bitstream
132 where the data for this block starts. For more info so the dump of the wiki
133 page below.
134
135
136 Below is a dump of:
137 http://picframe.spritesserver.nl/wiki/index.php/ImageEncodingAx206
138 Made on 30 March 2010.
139
140 Contents
141
142 • 1 Compressed image format
143 □ 1.1 Introduction
144 □ 1.2 Image format
145 ☆ 1.2.1 Header
146 ☆ 1.2.2 MCU info block
147 ☆ 1.2.3 DQT table
148 ☆ 1.2.4 DHT table
149 ☆ 1.2.5 Entropy coded data
150
151 Compressed image format
152
153 This page describes the currently known information about the compression of
154 image files used in pictureframes using the ax206 chip.
155
156 Introduction
157
158 The ax206 chip stores images in a JPEG-like format, but with several
159 modifications and/or parts missing. Images also contain some kind of thumbnail
160 image.
161
162 To understand the information on this page, it is recommended to have a basic
163 understanding of JPEG compression.
164
165 Image format
166
167 The image file seems to contain the following parts:
168
169 • a 16-byte header
170 • a small, low-res version of the image in a kind of raw format (color space
171 is unknown, but likely YCbCr)
172 • a JPEG DQT table (but without the DQT marker) containing two JPEG
173 quantisation tables, one for luma and one for chroma
174 • a JPEG DHT table (but without the DHT marker) containing four huffman
175 tables (combinations of luma/chroma, DC/AC)
176 • the entropy coded JPEG data
177
178 Although the encoding is very much like JPEG, there are no JPEG chunk markers
179 at all.
180
181 Header
182
183 The image header is contained in the first 16 bytes.
184
185 Some of the fields in the header can be related to values from the JPEG
186 specification.
187
188 The table below shows all known fields:
189
190 ┌───────┬─────────┬───────────────────────────────────────────────────────────┐
191 │ │ Related │ │
192 │ Byte │ JPEG │ Meaning │
193 │ │ field │ │
194 ├───────┼─────────┼───────────────────────────────────────────────────────────┤
195 │0x00/ │SOF: X │Image width, encoded as big-endian │
196 │0x01 │ │ │
197 ├───────┼─────────┼───────────────────────────────────────────────────────────┤
198 │0x02/ │SOF: Y │Image height, encoded as big-endian │
199 │0x03 │ │ │
200 ├───────┼─────────┼───────────────────────────────────────────────────────────┤
201 │ │SOF: Hi/ │"Resolution" byte, 0 for images without sub-sampling of the│
202 │0x04 │Vi │color components, 3 for images that use sub-sampling of the│
203 │ │ │color components │
204 ├───────┼─────────┼───────────────────────────────────────────────────────────┤
205 │0x05/ │ │Three numbers, each indicating which quantisation table (0 │
206 │0x06/ │DQT: Tq │or 1) should be used for which component (Y, Cb, Cr) │
207 │0x07 │ │ │
208 ├───────┼─────────┼───────────────────────────────────────────────────────────┤
209 │0x08/ │ │Three numbers, each indicating which DC huffman table (0 or│
210 │0x09/ │SOS: Tdj │1) should be used for which component (Y, Cb, Cr) │
211 │0x0A │ │ │
212 ├───────┼─────────┼───────────────────────────────────────────────────────────┤
213 │0x08/ │ │Three numbers, each indicating which AC huffman table (0 or│
214 │0x09/ │SOS: Taj │1) should be used for which component (Y, Cb, Cr) │
215 │0x0A │ │ │
216 ├───────┼─────────┼───────────────────────────────────────────────────────────┤
217 │0x0E/ │Unknown │Unknown │
218 │0x0F │ │ │
219 └───────┴─────────┴───────────────────────────────────────────────────────────┘
220
221 MCU info block
222
223 Immediately after the header there is an MCU info block, this block contains 8
224 bytes per MCU which makes it possible to start decoding with a random MCU,
225 rather then needing to do the decode linearly. This way the picture frame can
226 do transition effects. The size of this block depends on the number of MCU's
227 and thus on the "resolution" byte 0x04 from the header. For a resolution value
228 of 0x00, the size (in bytes) is width * height / 8. For a resolution value of
229 0x03, the size is width * height / 32.
230
231 The info in each MCU info block consists of last DC vals for the 3 components
232 and the location in the huffman bitstream where the data for this block starts.
233 The way the location in the huffman bitstream is coded is, erm, rather
234 interesting. The contents of this field of the per MCU info is an offset to add
235 to the start of the MCU info block. An example to make things more clear. Lets
236 say the huffman compressed data for the 4th MCU starts at location 3066
237 (decimal), the information block for the 4th MCU starts at offset 16 + 3 * 8 =
238 40 (decimal). So the contents of the field coding were the huffman compressed
239 data for the 4th MCU starts is 3066 - 40 = 3026. So say that the 5th MCU starts
240 5 bytes later (the 4th one compressed well), then the field coding were the
241 huffman compressed data starts would contain 3071 - 48 = 3023. Yes this is a
242 bit weird, but it is how it is.
243
244 ┌─────────┬───────────────────────────────────────────────────────────────────┐
245 │ Offset │ Meaning │
246 ├─────────┼───────────────────────────────────────────────────────────────────┤
247 │0x00-0x01│Little Endian 16 bit word storing last DC val for the Y channel │
248 ├─────────┼───────────────────────────────────────────────────────────────────┤
249 │0x02-0x03│Little Endian 16 bit word storing last DC val for the Cb channel │
250 ├─────────┼───────────────────────────────────────────────────────────────────┤
251 │0x04-0x05│Little Endian 16 bit word storing last DC val for the Cr channel │
252 ├─────────┼───────────────────────────────────────────────────────────────────┤
253 │ │Little Endian 16 bit word storing the offset into the image, from │
254 │0x06-0x07│the start of this info block! Where the huffman data for this MCU │
255 │ │starts │
256 └─────────┴───────────────────────────────────────────────────────────────────┘
257
258 DQT table
259
260 This table is basically a standard JPEG DQT table, but it seems to be missing
261 its 0xFFDB marker at the start.
262
263 DHT table
264
265 This table is basically a standard JPEG DHT table, but it seems to be missing
266 its 0xFFC4 marker at the start.
267
268 Entropy coded data
269
270 This seems to be standard huffman encoded coefficient data, with the following
271 exceptions:
272
273 • there are no stuffing bytes after 0xFF bytes
274 • after each JPEG MCU (minimum coded unit), the next MCU seems to start at
275 the next byte-aligned position in the stream
276 • there is no end-of-image JPEG marker
277
278 The order of components (Y, Cb, Cr) coded in the stream depends on the
279 resolution byte from the header.
280
281 • For resolution value 0x00, they seem to be interleaved in the following
282 order: chroma1, chroma2, luma.
283 • For resolution value 0x03, they seem to be interleaved in the following
284 order: chroma1, chroma2, luma, luma, luma, luma. The two chroma blocks are
285 sub-sampled by a factor 2 in this case (and therefore encode for a 16x16
286 pixel area), the luma blocks are not sub-sampled (so that's why there are 4
287 blocks of 8x8 pixels to cover the same 16x16 pixel area).
288
289 Retrieved from "http://picframe.spritesserver.nl/wiki/index.php/
290 ImageEncodingAx206"
291
292 Powered by MediaWiki
293 GNU Free Documentation License 1.2
294
295 • This page was last modified 14:09, 30 March 2010.
296 • This page has been accessed 29 times.
297 • Content is available under GNU Free Documentation License 1.2.