Mercurial > libavcodec.hg
annotate mjpeg.c @ 345:e05b357a398a libavcodec
export mbskip_table for direct rendering
add hurry_up support
author | michaelni |
---|---|
date | Sat, 27 Apr 2002 13:12:57 +0000 |
parents | 9f6071a87e17 |
children | 34f6c77ff01a |
rev | line source |
---|---|
0 | 1 /* |
23 | 2 * MJPEG encoder and decoder |
3 * Copyright (c) 2000, 2001 Gerard Lantau. | |
0 | 4 * |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 */ | |
76 | 19 //#define DEBUG |
0 | 20 #include "avcodec.h" |
21 #include "dsputil.h" | |
22 #include "mpegvideo.h" | |
23 | |
24 typedef struct MJpegContext { | |
25 UINT8 huff_size_dc_luminance[12]; | |
26 UINT16 huff_code_dc_luminance[12]; | |
27 UINT8 huff_size_dc_chrominance[12]; | |
28 UINT16 huff_code_dc_chrominance[12]; | |
29 | |
30 UINT8 huff_size_ac_luminance[256]; | |
31 UINT16 huff_code_ac_luminance[256]; | |
32 UINT8 huff_size_ac_chrominance[256]; | |
33 UINT16 huff_code_ac_chrominance[256]; | |
34 } MJpegContext; | |
35 | |
36 #define SOF0 0xc0 | |
37 #define SOI 0xd8 | |
38 #define EOI 0xd9 | |
39 #define DQT 0xdb | |
40 #define DHT 0xc4 | |
41 #define SOS 0xda | |
42 | |
43 #if 0 | |
44 /* These are the sample quantization tables given in JPEG spec section K.1. | |
45 * The spec says that the values given produce "good" quality, and | |
46 * when divided by 2, "very good" quality. | |
47 */ | |
48 static const unsigned char std_luminance_quant_tbl[64] = { | |
49 16, 11, 10, 16, 24, 40, 51, 61, | |
50 12, 12, 14, 19, 26, 58, 60, 55, | |
51 14, 13, 16, 24, 40, 57, 69, 56, | |
52 14, 17, 22, 29, 51, 87, 80, 62, | |
53 18, 22, 37, 56, 68, 109, 103, 77, | |
54 24, 35, 55, 64, 81, 104, 113, 92, | |
55 49, 64, 78, 87, 103, 121, 120, 101, | |
56 72, 92, 95, 98, 112, 100, 103, 99 | |
57 }; | |
58 static const unsigned char std_chrominance_quant_tbl[64] = { | |
59 17, 18, 24, 47, 99, 99, 99, 99, | |
60 18, 21, 26, 66, 99, 99, 99, 99, | |
61 24, 26, 56, 99, 99, 99, 99, 99, | |
62 47, 66, 99, 99, 99, 99, 99, 99, | |
63 99, 99, 99, 99, 99, 99, 99, 99, | |
64 99, 99, 99, 99, 99, 99, 99, 99, | |
65 99, 99, 99, 99, 99, 99, 99, 99, | |
66 99, 99, 99, 99, 99, 99, 99, 99 | |
67 }; | |
68 #endif | |
69 | |
70 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */ | |
71 /* IMPORTANT: these are only valid for 8-bit data precision! */ | |
72 static const UINT8 bits_dc_luminance[17] = | |
73 { /* 0-base */ 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }; | |
74 static const UINT8 val_dc_luminance[] = | |
75 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; | |
76 | |
77 static const UINT8 bits_dc_chrominance[17] = | |
78 { /* 0-base */ 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; | |
79 static const UINT8 val_dc_chrominance[] = | |
80 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; | |
81 | |
82 static const UINT8 bits_ac_luminance[17] = | |
83 { /* 0-base */ 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }; | |
84 static const UINT8 val_ac_luminance[] = | |
85 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, | |
86 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, | |
87 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, | |
88 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, | |
89 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, | |
90 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, | |
91 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, | |
92 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, | |
93 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, | |
94 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, | |
95 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, | |
96 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, | |
97 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, | |
98 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, | |
99 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, | |
100 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, | |
101 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, | |
102 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, | |
103 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, | |
104 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, | |
105 0xf9, 0xfa | |
106 }; | |
107 | |
108 static const UINT8 bits_ac_chrominance[17] = | |
109 { /* 0-base */ 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }; | |
110 | |
111 static const UINT8 val_ac_chrominance[] = | |
112 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, | |
113 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, | |
114 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, | |
115 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, | |
116 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, | |
117 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, | |
118 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, | |
119 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, | |
120 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, | |
121 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, | |
122 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, | |
123 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, | |
124 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, | |
125 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, | |
126 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, | |
127 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, | |
128 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, | |
129 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, | |
130 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, | |
131 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, | |
132 0xf9, 0xfa | |
133 }; | |
134 | |
135 | |
136 /* isn't this function nicer than the one in the libjpeg ? */ | |
137 static void build_huffman_codes(UINT8 *huff_size, UINT16 *huff_code, | |
138 const UINT8 *bits_table, const UINT8 *val_table) | |
139 { | |
140 int i, j, k,nb, code, sym; | |
141 | |
142 code = 0; | |
143 k = 0; | |
144 for(i=1;i<=16;i++) { | |
145 nb = bits_table[i]; | |
146 for(j=0;j<nb;j++) { | |
147 sym = val_table[k++]; | |
148 huff_size[sym] = i; | |
149 huff_code[sym] = code; | |
150 code++; | |
151 } | |
152 code <<= 1; | |
153 } | |
154 } | |
155 | |
156 int mjpeg_init(MpegEncContext *s) | |
157 { | |
158 MJpegContext *m; | |
159 | |
160 m = malloc(sizeof(MJpegContext)); | |
161 if (!m) | |
162 return -1; | |
344 | 163 |
164 s->min_qcoeff=-1023; | |
165 s->max_qcoeff= 1023; | |
0 | 166 |
167 /* build all the huffman tables */ | |
168 build_huffman_codes(m->huff_size_dc_luminance, | |
169 m->huff_code_dc_luminance, | |
170 bits_dc_luminance, | |
171 val_dc_luminance); | |
172 build_huffman_codes(m->huff_size_dc_chrominance, | |
173 m->huff_code_dc_chrominance, | |
174 bits_dc_chrominance, | |
175 val_dc_chrominance); | |
176 build_huffman_codes(m->huff_size_ac_luminance, | |
177 m->huff_code_ac_luminance, | |
178 bits_ac_luminance, | |
179 val_ac_luminance); | |
180 build_huffman_codes(m->huff_size_ac_chrominance, | |
181 m->huff_code_ac_chrominance, | |
182 bits_ac_chrominance, | |
183 val_ac_chrominance); | |
184 | |
185 s->mjpeg_ctx = m; | |
186 return 0; | |
187 } | |
188 | |
189 void mjpeg_close(MpegEncContext *s) | |
190 { | |
191 free(s->mjpeg_ctx); | |
192 } | |
193 | |
194 static inline void put_marker(PutBitContext *p, int code) | |
195 { | |
196 put_bits(p, 8, 0xff); | |
197 put_bits(p, 8, code); | |
198 } | |
199 | |
200 /* table_class: 0 = DC coef, 1 = AC coefs */ | |
201 static int put_huffman_table(MpegEncContext *s, int table_class, int table_id, | |
202 const UINT8 *bits_table, const UINT8 *value_table) | |
203 { | |
204 PutBitContext *p = &s->pb; | |
205 int n, i; | |
206 | |
207 put_bits(p, 4, table_class); | |
208 put_bits(p, 4, table_id); | |
209 | |
210 n = 0; | |
211 for(i=1;i<=16;i++) { | |
212 n += bits_table[i]; | |
213 put_bits(p, 8, bits_table[i]); | |
214 } | |
215 | |
216 for(i=0;i<n;i++) | |
217 put_bits(p, 8, value_table[i]); | |
218 | |
219 return n + 17; | |
220 } | |
221 | |
222 static void jpeg_table_header(MpegEncContext *s) | |
223 { | |
224 PutBitContext *p = &s->pb; | |
37 | 225 int i, j, size; |
0 | 226 UINT8 *ptr; |
227 | |
228 /* quant matrixes */ | |
229 put_marker(p, DQT); | |
230 put_bits(p, 16, 2 + 1 * (1 + 64)); | |
231 put_bits(p, 4, 0); /* 8 bit precision */ | |
232 put_bits(p, 4, 0); /* table 0 */ | |
233 for(i=0;i<64;i++) { | |
37 | 234 j = zigzag_direct[i]; |
235 put_bits(p, 8, s->intra_matrix[j]); | |
0 | 236 } |
237 #if 0 | |
238 put_bits(p, 4, 0); /* 8 bit precision */ | |
239 put_bits(p, 4, 1); /* table 1 */ | |
240 for(i=0;i<64;i++) { | |
37 | 241 j = zigzag_direct[i]; |
242 put_bits(p, 8, s->chroma_intra_matrix[j]); | |
0 | 243 } |
244 #endif | |
245 | |
246 /* huffman table */ | |
247 put_marker(p, DHT); | |
248 flush_put_bits(p); | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
229
diff
changeset
|
249 ptr = pbBufPtr(p); |
0 | 250 put_bits(p, 16, 0); /* patched later */ |
251 size = 2; | |
252 size += put_huffman_table(s, 0, 0, bits_dc_luminance, val_dc_luminance); | |
253 size += put_huffman_table(s, 0, 1, bits_dc_chrominance, val_dc_chrominance); | |
254 | |
255 size += put_huffman_table(s, 1, 0, bits_ac_luminance, val_ac_luminance); | |
256 size += put_huffman_table(s, 1, 1, bits_ac_chrominance, val_ac_chrominance); | |
257 ptr[0] = size >> 8; | |
258 ptr[1] = size; | |
259 } | |
260 | |
261 void mjpeg_picture_header(MpegEncContext *s) | |
262 { | |
263 put_marker(&s->pb, SOI); | |
264 | |
229 | 265 if (s->mjpeg_write_tables) jpeg_table_header(s); |
0 | 266 |
267 put_marker(&s->pb, SOF0); | |
268 | |
269 put_bits(&s->pb, 16, 17); | |
270 put_bits(&s->pb, 8, 8); /* 8 bits/component */ | |
271 put_bits(&s->pb, 16, s->height); | |
272 put_bits(&s->pb, 16, s->width); | |
273 put_bits(&s->pb, 8, 3); /* 3 components */ | |
274 | |
275 /* Y component */ | |
276 put_bits(&s->pb, 8, 1); /* component number */ | |
229 | 277 put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */ |
278 put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */ | |
0 | 279 put_bits(&s->pb, 8, 0); /* select matrix */ |
280 | |
281 /* Cb component */ | |
282 put_bits(&s->pb, 8, 2); /* component number */ | |
229 | 283 put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */ |
284 put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */ | |
0 | 285 put_bits(&s->pb, 8, 0); /* select matrix */ |
286 | |
287 /* Cr component */ | |
288 put_bits(&s->pb, 8, 3); /* component number */ | |
229 | 289 put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */ |
290 put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */ | |
0 | 291 put_bits(&s->pb, 8, 0); /* select matrix */ |
292 | |
293 /* scan header */ | |
294 put_marker(&s->pb, SOS); | |
295 put_bits(&s->pb, 16, 12); /* length */ | |
296 put_bits(&s->pb, 8, 3); /* 3 components */ | |
297 | |
298 /* Y component */ | |
299 put_bits(&s->pb, 8, 1); /* index */ | |
300 put_bits(&s->pb, 4, 0); /* DC huffman table index */ | |
301 put_bits(&s->pb, 4, 0); /* AC huffman table index */ | |
302 | |
303 /* Cb component */ | |
304 put_bits(&s->pb, 8, 2); /* index */ | |
305 put_bits(&s->pb, 4, 1); /* DC huffman table index */ | |
306 put_bits(&s->pb, 4, 1); /* AC huffman table index */ | |
307 | |
308 /* Cr component */ | |
309 put_bits(&s->pb, 8, 3); /* index */ | |
310 put_bits(&s->pb, 4, 1); /* DC huffman table index */ | |
311 put_bits(&s->pb, 4, 1); /* AC huffman table index */ | |
312 | |
313 put_bits(&s->pb, 8, 0); /* Ss (not used) */ | |
314 put_bits(&s->pb, 8, 63); /* Se (not used) */ | |
315 put_bits(&s->pb, 8, 0); /* (not used) */ | |
316 } | |
317 | |
318 void mjpeg_picture_trailer(MpegEncContext *s) | |
319 { | |
320 jflush_put_bits(&s->pb); | |
321 put_marker(&s->pb, EOI); | |
322 } | |
323 | |
324 static inline void encode_dc(MpegEncContext *s, int val, | |
325 UINT8 *huff_size, UINT16 *huff_code) | |
326 { | |
327 int mant, nbits; | |
328 | |
329 if (val == 0) { | |
330 jput_bits(&s->pb, huff_size[0], huff_code[0]); | |
331 } else { | |
332 mant = val; | |
333 if (val < 0) { | |
334 val = -val; | |
335 mant--; | |
336 } | |
337 | |
338 /* compute the log (XXX: optimize) */ | |
339 nbits = 0; | |
340 while (val != 0) { | |
341 val = val >> 1; | |
342 nbits++; | |
343 } | |
344 | |
345 jput_bits(&s->pb, huff_size[nbits], huff_code[nbits]); | |
346 | |
347 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1)); | |
348 } | |
349 } | |
350 | |
351 static void encode_block(MpegEncContext *s, DCTELEM *block, int n) | |
352 { | |
353 int mant, nbits, code, i, j; | |
354 int component, dc, run, last_index, val; | |
355 MJpegContext *m = s->mjpeg_ctx; | |
356 UINT8 *huff_size_ac; | |
357 UINT16 *huff_code_ac; | |
358 | |
359 /* DC coef */ | |
360 component = (n <= 3 ? 0 : n - 4 + 1); | |
361 dc = block[0]; /* overflow is impossible */ | |
362 val = dc - s->last_dc[component]; | |
363 if (n < 4) { | |
364 encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance); | |
365 huff_size_ac = m->huff_size_ac_luminance; | |
366 huff_code_ac = m->huff_code_ac_luminance; | |
367 } else { | |
368 encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance); | |
369 huff_size_ac = m->huff_size_ac_chrominance; | |
370 huff_code_ac = m->huff_code_ac_chrominance; | |
371 } | |
372 s->last_dc[component] = dc; | |
373 | |
374 /* AC coefs */ | |
375 | |
376 run = 0; | |
377 last_index = s->block_last_index[n]; | |
378 for(i=1;i<=last_index;i++) { | |
379 j = zigzag_direct[i]; | |
380 val = block[j]; | |
381 if (val == 0) { | |
382 run++; | |
383 } else { | |
384 while (run >= 16) { | |
385 jput_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]); | |
386 run -= 16; | |
387 } | |
388 mant = val; | |
389 if (val < 0) { | |
390 val = -val; | |
391 mant--; | |
392 } | |
393 | |
394 /* compute the log (XXX: optimize) */ | |
395 nbits = 0; | |
396 while (val != 0) { | |
397 val = val >> 1; | |
398 nbits++; | |
399 } | |
400 code = (run << 4) | nbits; | |
401 | |
402 jput_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]); | |
403 | |
404 jput_bits(&s->pb, nbits, mant & ((1 << nbits) - 1)); | |
405 run = 0; | |
406 } | |
407 } | |
408 | |
409 /* output EOB only if not already 64 values */ | |
410 if (last_index < 63 || run != 0) | |
411 jput_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]); | |
412 } | |
413 | |
414 void mjpeg_encode_mb(MpegEncContext *s, | |
415 DCTELEM block[6][64]) | |
416 { | |
417 int i; | |
418 for(i=0;i<6;i++) { | |
419 encode_block(s, block[i], i); | |
420 } | |
421 } | |
23 | 422 |
423 /******************************************/ | |
424 /* decoding */ | |
425 | |
426 /* compressed picture size */ | |
427 #define PICTURE_BUFFER_SIZE 100000 | |
428 | |
429 #define MAX_COMPONENTS 4 | |
430 | |
431 typedef struct MJpegDecodeContext { | |
432 GetBitContext gb; | |
433 UINT32 header_state; | |
434 int start_code; /* current start code */ | |
435 UINT8 *buf_ptr; | |
436 int buffer_size; | |
437 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ | |
438 INT16 quant_matrixes[4][64]; | |
439 VLC vlcs[2][4]; | |
53 | 440 |
441 int org_width, org_height; /* size given at codec init */ | |
442 int first_picture; /* true if decoding first picture */ | |
443 int interlaced; /* true if interlaced */ | |
444 int bottom_field; /* true if bottom field */ | |
445 | |
23 | 446 int width, height; |
26 | 447 int nb_components; |
448 int component_id[MAX_COMPONENTS]; | |
23 | 449 int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */ |
450 int v_count[MAX_COMPONENTS]; | |
451 int h_max, v_max; /* maximum h and v counts */ | |
452 int quant_index[4]; /* quant table index for each component */ | |
453 int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */ | |
454 UINT8 *current_picture[MAX_COMPONENTS]; /* picture structure */ | |
455 int linesize[MAX_COMPONENTS]; | |
456 DCTELEM block[64] __align8; | |
457 UINT8 buffer[PICTURE_BUFFER_SIZE]; | |
458 } MJpegDecodeContext; | |
459 | |
29 | 460 static void build_vlc(VLC *vlc, const UINT8 *bits_table, const UINT8 *val_table, |
461 int nb_codes) | |
462 { | |
463 UINT8 huff_size[256]; | |
464 UINT16 huff_code[256]; | |
465 | |
466 memset(huff_size, 0, sizeof(huff_size)); | |
467 build_huffman_codes(huff_size, huff_code, bits_table, val_table); | |
468 | |
469 init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2); | |
470 } | |
471 | |
23 | 472 static int mjpeg_decode_init(AVCodecContext *avctx) |
473 { | |
474 MJpegDecodeContext *s = avctx->priv_data; | |
475 | |
476 s->header_state = 0; | |
477 s->mpeg_enc_ctx_allocated = 0; | |
478 s->buffer_size = PICTURE_BUFFER_SIZE - 1; /* minus 1 to take into | |
479 account FF 00 case */ | |
480 s->start_code = -1; | |
481 s->buf_ptr = s->buffer; | |
53 | 482 s->first_picture = 1; |
483 s->org_width = avctx->width; | |
484 s->org_height = avctx->height; | |
29 | 485 |
486 build_vlc(&s->vlcs[0][0], bits_dc_luminance, val_dc_luminance, 12); | |
487 build_vlc(&s->vlcs[0][1], bits_dc_chrominance, val_dc_chrominance, 12); | |
488 build_vlc(&s->vlcs[1][0], bits_ac_luminance, val_ac_luminance, 251); | |
489 build_vlc(&s->vlcs[1][1], bits_ac_chrominance, val_ac_chrominance, 251); | |
23 | 490 return 0; |
491 } | |
492 | |
493 /* quantize tables */ | |
494 static int mjpeg_decode_dqt(MJpegDecodeContext *s, | |
495 UINT8 *buf, int buf_size) | |
496 { | |
37 | 497 int len, index, i, j; |
23 | 498 init_get_bits(&s->gb, buf, buf_size); |
499 | |
500 len = get_bits(&s->gb, 16); | |
501 len -= 2; | |
502 | |
503 while (len >= 65) { | |
504 /* only 8 bit precision handled */ | |
505 if (get_bits(&s->gb, 4) != 0) | |
506 return -1; | |
507 index = get_bits(&s->gb, 4); | |
508 if (index >= 4) | |
509 return -1; | |
510 dprintf("index=%d\n", index); | |
511 /* read quant table */ | |
37 | 512 for(i=0;i<64;i++) { |
513 j = zigzag_direct[i]; | |
514 s->quant_matrixes[index][j] = get_bits(&s->gb, 8); | |
515 } | |
23 | 516 len -= 65; |
517 } | |
518 return 0; | |
519 } | |
520 | |
521 /* decode huffman tables and build VLC decoders */ | |
522 static int mjpeg_decode_dht(MJpegDecodeContext *s, | |
523 UINT8 *buf, int buf_size) | |
524 { | |
525 int len, index, i, class, n, v, code_max; | |
526 UINT8 bits_table[17]; | |
527 UINT8 val_table[256]; | |
528 | |
529 init_get_bits(&s->gb, buf, buf_size); | |
530 | |
531 len = get_bits(&s->gb, 16); | |
532 len -= 2; | |
533 | |
534 while (len > 0) { | |
535 if (len < 17) | |
536 return -1; | |
537 class = get_bits(&s->gb, 4); | |
538 if (class >= 2) | |
539 return -1; | |
540 index = get_bits(&s->gb, 4); | |
541 if (index >= 4) | |
542 return -1; | |
543 n = 0; | |
544 for(i=1;i<=16;i++) { | |
545 bits_table[i] = get_bits(&s->gb, 8); | |
546 n += bits_table[i]; | |
547 } | |
548 len -= 17; | |
549 if (len < n || n > 256) | |
550 return -1; | |
551 | |
552 code_max = 0; | |
553 for(i=0;i<n;i++) { | |
554 v = get_bits(&s->gb, 8); | |
555 if (v > code_max) | |
556 code_max = v; | |
557 val_table[i] = v; | |
558 } | |
559 len -= n; | |
560 | |
561 /* build VLC and flush previous vlc if present */ | |
562 free_vlc(&s->vlcs[class][index]); | |
563 dprintf("class=%d index=%d nb_codes=%d\n", | |
564 class, index, code_max + 1); | |
29 | 565 build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1); |
23 | 566 } |
567 return 0; | |
568 } | |
569 | |
570 static int mjpeg_decode_sof0(MJpegDecodeContext *s, | |
571 UINT8 *buf, int buf_size) | |
572 { | |
26 | 573 int len, nb_components, i, width, height; |
23 | 574 |
575 init_get_bits(&s->gb, buf, buf_size); | |
576 | |
577 /* XXX: verify len field validity */ | |
578 len = get_bits(&s->gb, 16); | |
579 /* only 8 bits/component accepted */ | |
580 if (get_bits(&s->gb, 8) != 8) | |
581 return -1; | |
582 height = get_bits(&s->gb, 16); | |
583 width = get_bits(&s->gb, 16); | |
584 | |
585 nb_components = get_bits(&s->gb, 8); | |
586 if (nb_components <= 0 || | |
587 nb_components > MAX_COMPONENTS) | |
588 return -1; | |
26 | 589 s->nb_components = nb_components; |
23 | 590 s->h_max = 1; |
591 s->v_max = 1; | |
592 for(i=0;i<nb_components;i++) { | |
593 /* component id */ | |
26 | 594 s->component_id[i] = get_bits(&s->gb, 8) - 1; |
23 | 595 s->h_count[i] = get_bits(&s->gb, 4); |
596 s->v_count[i] = get_bits(&s->gb, 4); | |
597 /* compute hmax and vmax (only used in interleaved case) */ | |
598 if (s->h_count[i] > s->h_max) | |
599 s->h_max = s->h_count[i]; | |
600 if (s->v_count[i] > s->v_max) | |
601 s->v_max = s->v_count[i]; | |
602 s->quant_index[i] = get_bits(&s->gb, 8); | |
603 if (s->quant_index[i] >= 4) | |
604 return -1; | |
605 dprintf("component %d %d:%d\n", i, s->h_count[i], s->v_count[i]); | |
606 } | |
607 | |
608 /* if different size, realloc/alloc picture */ | |
609 /* XXX: also check h_count and v_count */ | |
610 if (width != s->width || height != s->height) { | |
611 for(i=0;i<MAX_COMPONENTS;i++) { | |
612 free(s->current_picture[i]); | |
613 s->current_picture[i] = NULL; | |
614 } | |
615 s->width = width; | |
616 s->height = height; | |
53 | 617 /* test interlaced mode */ |
618 if (s->first_picture && | |
619 s->org_height != 0 && | |
620 s->height < ((s->org_height * 3) / 4)) { | |
621 s->interlaced = 1; | |
622 s->bottom_field = 0; | |
623 } | |
624 | |
23 | 625 for(i=0;i<nb_components;i++) { |
226 | 626 int w, h; |
627 w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max); | |
628 h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max); | |
629 w = w * 8 * s->h_count[i]; | |
630 h = h * 8 * s->v_count[i]; | |
53 | 631 if (s->interlaced) |
632 w *= 2; | |
23 | 633 s->linesize[i] = w; |
634 /* memory test is done in mjpeg_decode_sos() */ | |
635 s->current_picture[i] = av_mallocz(w * h); | |
636 } | |
53 | 637 s->first_picture = 0; |
23 | 638 } |
53 | 639 |
23 | 640 return 0; |
641 } | |
642 | |
643 static inline int decode_dc(MJpegDecodeContext *s, int dc_index) | |
644 { | |
645 VLC *dc_vlc; | |
646 int code, diff; | |
647 | |
648 dc_vlc = &s->vlcs[0][dc_index]; | |
649 code = get_vlc(&s->gb, dc_vlc); | |
650 if (code < 0) | |
651 return 0xffff; | |
652 if (code == 0) { | |
653 diff = 0; | |
654 } else { | |
655 diff = get_bits(&s->gb, code); | |
656 if ((diff & (1 << (code - 1))) == 0) | |
657 diff = (-1 << code) | (diff + 1); | |
658 } | |
659 return diff; | |
660 } | |
661 | |
662 /* decode block and dequantize */ | |
663 static int decode_block(MJpegDecodeContext *s, DCTELEM *block, | |
664 int component, int dc_index, int ac_index, int quant_index) | |
665 { | |
666 int nbits, code, i, j, level; | |
667 int run, val; | |
668 VLC *ac_vlc; | |
669 INT16 *quant_matrix; | |
670 | |
671 quant_matrix = s->quant_matrixes[quant_index]; | |
672 /* DC coef */ | |
673 val = decode_dc(s, dc_index); | |
674 if (val == 0xffff) { | |
675 dprintf("error dc\n"); | |
676 return -1; | |
677 } | |
678 val = val * quant_matrix[0] + s->last_dc[component]; | |
679 s->last_dc[component] = val; | |
680 block[0] = val; | |
681 /* AC coefs */ | |
682 ac_vlc = &s->vlcs[1][ac_index]; | |
683 i = 1; | |
684 for(;;) { | |
685 code = get_vlc(&s->gb, ac_vlc); | |
686 if (code < 0) { | |
687 dprintf("error ac\n"); | |
688 return -1; | |
689 } | |
690 /* EOB */ | |
691 if (code == 0) | |
692 break; | |
693 if (code == 0xf0) { | |
694 i += 16; | |
695 } else { | |
696 run = code >> 4; | |
697 nbits = code & 0xf; | |
698 level = get_bits(&s->gb, nbits); | |
699 if ((level & (1 << (nbits - 1))) == 0) | |
700 level = (-1 << nbits) | (level + 1); | |
701 i += run; | |
702 if (i >= 64) { | |
703 dprintf("error count: %d\n", i); | |
704 return -1; | |
705 } | |
706 j = zigzag_direct[i]; | |
707 block[j] = level * quant_matrix[j]; | |
708 i++; | |
28
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
709 if (i >= 64) |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
710 break; |
23 | 711 } |
712 } | |
713 return 0; | |
714 } | |
715 | |
716 static int mjpeg_decode_sos(MJpegDecodeContext *s, | |
717 UINT8 *buf, int buf_size) | |
718 { | |
47
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
719 int len, nb_components, i, j, n, h, v, ret; |
26 | 720 int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id; |
23 | 721 int comp_index[4]; |
722 int dc_index[4]; | |
723 int ac_index[4]; | |
724 int nb_blocks[4]; | |
725 int h_count[4]; | |
726 int v_count[4]; | |
727 | |
728 init_get_bits(&s->gb, buf, buf_size); | |
729 /* XXX: verify len field validity */ | |
730 len = get_bits(&s->gb, 16); | |
731 nb_components = get_bits(&s->gb, 8); | |
732 /* XXX: only interleaved scan accepted */ | |
733 if (nb_components != 3) | |
734 return -1; | |
735 vmax = 0; | |
736 hmax = 0; | |
737 for(i=0;i<nb_components;i++) { | |
26 | 738 id = get_bits(&s->gb, 8) - 1; |
739 /* find component index */ | |
740 for(index=0;index<s->nb_components;index++) | |
741 if (id == s->component_id[index]) | |
742 break; | |
743 if (index == s->nb_components) | |
23 | 744 return -1; |
26 | 745 |
23 | 746 comp_index[i] = index; |
747 nb_blocks[i] = s->h_count[index] * s->v_count[index]; | |
748 h_count[i] = s->h_count[index]; | |
749 v_count[i] = s->v_count[index]; | |
28
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
750 |
23 | 751 dc_index[i] = get_bits(&s->gb, 4); |
752 if (dc_index[i] >= 4) | |
753 return -1; | |
754 ac_index[i] = get_bits(&s->gb, 4); | |
755 if (ac_index[i] >= 4) | |
756 return -1; | |
757 } | |
758 get_bits(&s->gb, 8); /* Ss */ | |
759 get_bits(&s->gb, 8); /* Se */ | |
760 get_bits(&s->gb, 8); /* not used */ | |
761 | |
762 for(i=0;i<nb_components;i++) | |
763 s->last_dc[i] = 1024; | |
764 | |
765 if (nb_components > 1) { | |
766 /* interleaved stream */ | |
767 mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8); | |
768 mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8); | |
769 } else { | |
770 h = s->h_max / s->h_count[comp_index[0]]; | |
771 v = s->v_max / s->v_count[comp_index[0]]; | |
772 mb_width = (s->width + h * 8 - 1) / (h * 8); | |
773 mb_height = (s->height + v * 8 - 1) / (v * 8); | |
774 nb_blocks[0] = 1; | |
775 h_count[0] = 1; | |
776 v_count[0] = 1; | |
777 } | |
778 | |
779 for(mb_y = 0; mb_y < mb_height; mb_y++) { | |
780 for(mb_x = 0; mb_x < mb_width; mb_x++) { | |
781 for(i=0;i<nb_components;i++) { | |
782 UINT8 *ptr; | |
783 int x, y, c; | |
784 n = nb_blocks[i]; | |
785 c = comp_index[i]; | |
786 h = h_count[i]; | |
787 v = v_count[i]; | |
788 x = 0; | |
789 y = 0; | |
790 for(j=0;j<n;j++) { | |
791 memset(s->block, 0, sizeof(s->block)); | |
792 if (decode_block(s, s->block, i, | |
793 dc_index[i], ac_index[i], | |
794 s->quant_index[c]) < 0) { | |
795 dprintf("error %d %d\n", mb_y, mb_x); | |
47
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
796 ret = -1; |
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
797 goto the_end; |
23 | 798 } |
799 ff_idct (s->block); | |
800 ptr = s->current_picture[c] + | |
801 (s->linesize[c] * (v * mb_y + y) * 8) + | |
802 (h * mb_x + x) * 8; | |
53 | 803 if (s->interlaced && s->bottom_field) |
804 ptr += s->linesize[c] >> 1; | |
23 | 805 put_pixels_clamped(s->block, ptr, s->linesize[c]); |
806 if (++x == h) { | |
807 x = 0; | |
808 y++; | |
809 } | |
810 } | |
811 } | |
812 } | |
813 } | |
47
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
814 ret = 0; |
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
815 the_end: |
44
92d51f683931
added forgotten emms() - fix various segmentation faults when using mjpeg
glantau
parents:
37
diff
changeset
|
816 emms_c(); |
47
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
817 return ret; |
23 | 818 } |
819 | |
820 /* return the 8 bit start code value and update the search | |
821 state. Return -1 if no start code found */ | |
822 static int find_marker(UINT8 **pbuf_ptr, UINT8 *buf_end, | |
823 UINT32 *header_state) | |
824 { | |
825 UINT8 *buf_ptr; | |
826 unsigned int state, v; | |
827 int val; | |
828 | |
829 state = *header_state; | |
830 buf_ptr = *pbuf_ptr; | |
831 if (state) { | |
832 /* get marker */ | |
833 found: | |
834 if (buf_ptr < buf_end) { | |
835 val = *buf_ptr++; | |
836 state = 0; | |
837 } else { | |
838 val = -1; | |
839 } | |
840 } else { | |
841 while (buf_ptr < buf_end) { | |
842 v = *buf_ptr++; | |
843 if (v == 0xff) { | |
844 state = 1; | |
845 goto found; | |
846 } | |
847 } | |
848 val = -1; | |
849 } | |
850 *pbuf_ptr = buf_ptr; | |
851 *header_state = state; | |
852 return val; | |
853 } | |
854 | |
855 static int mjpeg_decode_frame(AVCodecContext *avctx, | |
856 void *data, int *data_size, | |
857 UINT8 *buf, int buf_size) | |
858 { | |
859 MJpegDecodeContext *s = avctx->priv_data; | |
860 UINT8 *buf_end, *buf_ptr, *buf_start; | |
861 int len, code, start_code, input_size, i; | |
862 AVPicture *picture = data; | |
863 | |
68 | 864 *data_size = 0; |
865 | |
23 | 866 /* no supplementary picture */ |
68 | 867 if (buf_size == 0) |
23 | 868 return 0; |
869 | |
870 buf_ptr = buf; | |
871 buf_end = buf + buf_size; | |
872 while (buf_ptr < buf_end) { | |
873 buf_start = buf_ptr; | |
874 /* find start next marker */ | |
875 code = find_marker(&buf_ptr, buf_end, &s->header_state); | |
876 /* copy to buffer */ | |
877 len = buf_ptr - buf_start; | |
878 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) { | |
879 /* data too big : flush */ | |
880 s->buf_ptr = s->buffer; | |
881 if (code > 0) | |
882 s->start_code = code; | |
883 } else { | |
884 memcpy(s->buf_ptr, buf_start, len); | |
885 s->buf_ptr += len; | |
886 /* if we got FF 00, we copy FF to the stream to unescape FF 00 */ | |
887 if (code == 0) { | |
888 s->buf_ptr--; | |
889 } else if (code > 0) { | |
890 /* prepare data for next start code */ | |
891 input_size = s->buf_ptr - s->buffer; | |
892 start_code = s->start_code; | |
893 s->buf_ptr = s->buffer; | |
894 s->start_code = code; | |
44
92d51f683931
added forgotten emms() - fix various segmentation faults when using mjpeg
glantau
parents:
37
diff
changeset
|
895 dprintf("marker=%x\n", start_code); |
23 | 896 switch(start_code) { |
897 case SOI: | |
898 /* nothing to do on SOI */ | |
899 break; | |
900 case DQT: | |
901 mjpeg_decode_dqt(s, s->buffer, input_size); | |
902 break; | |
903 case DHT: | |
904 mjpeg_decode_dht(s, s->buffer, input_size); | |
905 break; | |
906 case SOF0: | |
907 mjpeg_decode_sof0(s, s->buffer, input_size); | |
908 break; | |
909 case SOS: | |
910 mjpeg_decode_sos(s, s->buffer, input_size); | |
911 if (s->start_code == EOI) { | |
53 | 912 int l; |
913 if (s->interlaced) { | |
914 s->bottom_field ^= 1; | |
915 /* if not bottom field, do not output image yet */ | |
916 if (s->bottom_field) | |
917 goto the_end; | |
918 } | |
23 | 919 for(i=0;i<3;i++) { |
920 picture->data[i] = s->current_picture[i]; | |
53 | 921 l = s->linesize[i]; |
922 if (s->interlaced) | |
923 l >>= 1; | |
924 picture->linesize[i] = l; | |
23 | 925 } |
926 *data_size = sizeof(AVPicture); | |
927 avctx->height = s->height; | |
53 | 928 if (s->interlaced) |
929 avctx->height *= 2; | |
23 | 930 avctx->width = s->width; |
28
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
931 /* XXX: not complete test ! */ |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
932 switch((s->h_count[0] << 4) | s->v_count[0]) { |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
933 case 0x11: |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
934 avctx->pix_fmt = PIX_FMT_YUV444P; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
935 break; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
936 case 0x21: |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
937 avctx->pix_fmt = PIX_FMT_YUV422P; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
938 break; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
939 default: |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
940 case 0x22: |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
941 avctx->pix_fmt = PIX_FMT_YUV420P; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
942 break; |
b611fafddf9e
added 422P and 444P support - fixed block parsing error
glantau
parents:
26
diff
changeset
|
943 } |
47
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
944 /* dummy quality */ |
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
945 /* XXX: infer it with matrix */ |
bd0dd8d0b759
return dummy quality to avoid bug in -sameq case - forgot emms in error case
glantau
parents:
44
diff
changeset
|
946 avctx->quality = 3; |
23 | 947 goto the_end; |
948 } | |
949 break; | |
950 } | |
951 } | |
952 } | |
953 } | |
954 the_end: | |
955 return buf_ptr - buf; | |
956 } | |
957 | |
958 static int mjpeg_decode_end(AVCodecContext *avctx) | |
959 { | |
960 MJpegDecodeContext *s = avctx->priv_data; | |
961 int i, j; | |
962 | |
963 for(i=0;i<MAX_COMPONENTS;i++) | |
964 free(s->current_picture[i]); | |
965 for(i=0;i<2;i++) { | |
966 for(j=0;j<4;j++) | |
967 free_vlc(&s->vlcs[i][j]); | |
968 } | |
969 return 0; | |
970 } | |
971 | |
972 AVCodec mjpeg_decoder = { | |
973 "mjpeg", | |
974 CODEC_TYPE_VIDEO, | |
975 CODEC_ID_MJPEG, | |
976 sizeof(MJpegDecodeContext), | |
977 mjpeg_decode_init, | |
978 NULL, | |
979 mjpeg_decode_end, | |
980 mjpeg_decode_frame, | |
981 }; |