Mercurial > libavcodec.hg
annotate mjpegdec.c @ 5376:83682d6837e4 libavcodec
revise comment for exponent decoding function
author | jbr |
---|---|
date | Thu, 19 Jul 2007 02:09:58 +0000 |
parents | 1a25f2f6316e |
children | 4cac2cfe2745 |
rev | line source |
---|---|
5020 | 1 /* |
2 * MJPEG decoder | |
3 * Copyright (c) 2000, 2001 Fabrice Bellard. | |
4 * Copyright (c) 2003 Alex Beregszaszi | |
5 * Copyright (c) 2003-2004 Michael Niedermayer | |
6 * | |
5214 | 7 * Support for external huffman table, various fixes (AVID workaround), |
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support | |
9 * by Alex Beregszaszi | |
10 * | |
5020 | 11 * This file is part of FFmpeg. |
12 * | |
13 * FFmpeg is free software; you can redistribute it and/or | |
14 * modify it under the terms of the GNU Lesser General Public | |
15 * License as published by the Free Software Foundation; either | |
16 * version 2.1 of the License, or (at your option) any later version. | |
17 * | |
18 * FFmpeg is distributed in the hope that it will be useful, | |
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
21 * Lesser General Public License for more details. | |
22 * | |
23 * You should have received a copy of the GNU Lesser General Public | |
24 * License along with FFmpeg; if not, write to the Free Software | |
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
26 */ | |
27 | |
28 /** | |
29 * @file mjpegdec.c | |
30 * MJPEG decoder. | |
31 */ | |
32 | |
33 //#define DEBUG | |
34 #include <assert.h> | |
35 | |
36 #include "avcodec.h" | |
37 #include "dsputil.h" | |
38 #include "mjpeg.h" | |
5041 | 39 #include "mjpegdec.h" |
5020 | 40 #include "jpeglsdec.h" |
41 | |
42 | |
43 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, | |
44 int nb_codes, int use_static, int is_ac) | |
45 { | |
46 uint8_t huff_size[256+16]; | |
47 uint16_t huff_code[256+16]; | |
48 | |
49 assert(nb_codes <= 256); | |
50 | |
51 memset(huff_size, 0, sizeof(huff_size)); | |
5021 | 52 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table); |
5020 | 53 |
54 if(is_ac){ | |
55 memmove(huff_size+16, huff_size, sizeof(uint8_t)*nb_codes); | |
56 memmove(huff_code+16, huff_code, sizeof(uint16_t)*nb_codes); | |
57 memset(huff_size, 0, sizeof(uint8_t)*16); | |
58 memset(huff_code, 0, sizeof(uint16_t)*16); | |
59 nb_codes += 16; | |
60 } | |
61 | |
62 return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static); | |
63 } | |
64 | |
5068
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
65 static void build_basic_mjpeg_vlc(MJpegDecodeContext * s) { |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
66 build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance, |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
67 ff_mjpeg_val_dc_luminance, 12, 0, 0); |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
68 build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance, |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
69 ff_mjpeg_val_dc_chrominance, 12, 0, 0); |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
70 build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance, |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
71 ff_mjpeg_val_ac_luminance, 251, 0, 1); |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
72 build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance, |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
73 ff_mjpeg_val_ac_chrominance, 251, 0, 1); |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
74 } |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
75 |
5041 | 76 int ff_mjpeg_decode_init(AVCodecContext *avctx) |
5020 | 77 { |
78 MJpegDecodeContext *s = avctx->priv_data; | |
79 | |
80 s->avctx = avctx; | |
81 dsputil_init(&s->dsp, avctx); | |
82 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct); | |
83 s->buffer_size = 0; | |
84 s->buffer = NULL; | |
85 s->start_code = -1; | |
86 s->first_picture = 1; | |
87 s->org_height = avctx->coded_height; | |
88 | |
5068
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
89 build_basic_mjpeg_vlc(s); |
5020 | 90 |
91 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF) | |
92 { | |
93 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n"); | |
94 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8); | |
5068
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
95 if (ff_mjpeg_decode_dht(s)) { |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
96 av_log(avctx, AV_LOG_ERROR, "mjpeg: error using external huffman table, switching back to internal\n"); |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
97 build_basic_mjpeg_vlc(s); |
93e369c55c31
fix mjpeg decoding with broken huffman table headers
ods15
parents:
5044
diff
changeset
|
98 } |
5020 | 99 } |
100 if (avctx->extradata_size > 9 && | |
101 AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) { | |
102 if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */ | |
103 s->interlace_polarity = 1; /* bottom field first */ | |
104 av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n"); | |
105 } | |
106 } | |
107 | |
108 return 0; | |
109 } | |
110 | |
111 | |
112 /* quantize tables */ | |
5044 | 113 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s) |
5020 | 114 { |
115 int len, index, i, j; | |
116 | |
117 len = get_bits(&s->gb, 16) - 2; | |
118 | |
119 while (len >= 65) { | |
120 /* only 8 bit precision handled */ | |
121 if (get_bits(&s->gb, 4) != 0) | |
122 { | |
123 av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n"); | |
124 return -1; | |
125 } | |
126 index = get_bits(&s->gb, 4); | |
127 if (index >= 4) | |
128 return -1; | |
129 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index); | |
130 /* read quant table */ | |
131 for(i=0;i<64;i++) { | |
132 j = s->scantable.permutated[i]; | |
133 s->quant_matrixes[index][j] = get_bits(&s->gb, 8); | |
134 } | |
135 | |
136 //XXX FIXME finetune, and perhaps add dc too | |
137 s->qscale[index]= FFMAX( | |
138 s->quant_matrixes[index][s->scantable.permutated[1]], | |
139 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1; | |
140 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]); | |
141 len -= 65; | |
142 } | |
143 | |
144 return 0; | |
145 } | |
146 | |
147 /* decode huffman tables and build VLC decoders */ | |
5044 | 148 int ff_mjpeg_decode_dht(MJpegDecodeContext *s) |
5020 | 149 { |
150 int len, index, i, class, n, v, code_max; | |
151 uint8_t bits_table[17]; | |
152 uint8_t val_table[256]; | |
153 | |
154 len = get_bits(&s->gb, 16) - 2; | |
155 | |
156 while (len > 0) { | |
157 if (len < 17) | |
158 return -1; | |
159 class = get_bits(&s->gb, 4); | |
160 if (class >= 2) | |
161 return -1; | |
162 index = get_bits(&s->gb, 4); | |
163 if (index >= 4) | |
164 return -1; | |
165 n = 0; | |
166 for(i=1;i<=16;i++) { | |
167 bits_table[i] = get_bits(&s->gb, 8); | |
168 n += bits_table[i]; | |
169 } | |
170 len -= 17; | |
171 if (len < n || n > 256) | |
172 return -1; | |
173 | |
174 code_max = 0; | |
175 for(i=0;i<n;i++) { | |
176 v = get_bits(&s->gb, 8); | |
177 if (v > code_max) | |
178 code_max = v; | |
179 val_table[i] = v; | |
180 } | |
181 len -= n; | |
182 | |
183 /* build VLC and flush previous vlc if present */ | |
184 free_vlc(&s->vlcs[class][index]); | |
185 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n", | |
186 class, index, code_max + 1); | |
187 if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){ | |
188 return -1; | |
189 } | |
190 } | |
191 return 0; | |
192 } | |
193 | |
5044 | 194 int ff_mjpeg_decode_sof(MJpegDecodeContext *s) |
5020 | 195 { |
196 int len, nb_components, i, width, height, pix_fmt_id; | |
197 | |
198 /* XXX: verify len field validity */ | |
199 len = get_bits(&s->gb, 16); | |
200 s->bits= get_bits(&s->gb, 8); | |
201 | |
202 if(s->pegasus_rct) s->bits=9; | |
203 if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly | |
204 | |
205 if (s->bits != 8 && !s->lossless){ | |
206 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n"); | |
207 return -1; | |
208 } | |
209 | |
210 height = get_bits(&s->gb, 16); | |
211 width = get_bits(&s->gb, 16); | |
212 | |
213 //HACK for odd_height.mov | |
214 if(s->interlaced && s->width == width && s->height == height + 1) | |
215 height= s->height; | |
216 | |
217 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height); | |
218 if(avcodec_check_dimensions(s->avctx, width, height)) | |
219 return -1; | |
220 | |
221 nb_components = get_bits(&s->gb, 8); | |
222 if (nb_components <= 0 || | |
223 nb_components > MAX_COMPONENTS) | |
224 return -1; | |
225 if (s->ls && !(s->bits <= 8 || nb_components == 1)){ | |
226 av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n"); | |
227 return -1; | |
228 } | |
229 s->nb_components = nb_components; | |
230 s->h_max = 1; | |
231 s->v_max = 1; | |
232 for(i=0;i<nb_components;i++) { | |
233 /* component id */ | |
234 s->component_id[i] = get_bits(&s->gb, 8) - 1; | |
235 s->h_count[i] = get_bits(&s->gb, 4); | |
236 s->v_count[i] = get_bits(&s->gb, 4); | |
237 /* compute hmax and vmax (only used in interleaved case) */ | |
238 if (s->h_count[i] > s->h_max) | |
239 s->h_max = s->h_count[i]; | |
240 if (s->v_count[i] > s->v_max) | |
241 s->v_max = s->v_count[i]; | |
242 s->quant_index[i] = get_bits(&s->gb, 8); | |
243 if (s->quant_index[i] >= 4) | |
244 return -1; | |
245 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i], | |
246 s->v_count[i], s->component_id[i], s->quant_index[i]); | |
247 } | |
248 | |
249 if(s->ls && (s->h_max > 1 || s->v_max > 1)) { | |
250 av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n"); | |
251 return -1; | |
252 } | |
253 | |
254 if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1; | |
255 | |
256 /* if different size, realloc/alloc picture */ | |
257 /* XXX: also check h_count and v_count */ | |
258 if (width != s->width || height != s->height) { | |
259 av_freep(&s->qscale_table); | |
260 | |
261 s->width = width; | |
262 s->height = height; | |
263 s->interlaced = 0; | |
264 | |
265 /* test interlaced mode */ | |
266 if (s->first_picture && | |
267 s->org_height != 0 && | |
268 s->height < ((s->org_height * 3) / 4)) { | |
269 s->interlaced = 1; | |
270 s->bottom_field = s->interlace_polarity; | |
271 s->picture.interlaced_frame = 1; | |
272 s->picture.top_field_first = !s->interlace_polarity; | |
273 height *= 2; | |
274 } | |
275 | |
276 avcodec_set_dimensions(s->avctx, width, height); | |
277 | |
278 s->qscale_table= av_mallocz((s->width+15)/16); | |
279 | |
280 s->first_picture = 0; | |
281 } | |
282 | |
283 if(s->interlaced && (s->bottom_field == !s->interlace_polarity)) | |
284 return 0; | |
285 | |
286 /* XXX: not complete test ! */ | |
287 pix_fmt_id = (s->h_count[0] << 20) | (s->v_count[0] << 16) | | |
288 (s->h_count[1] << 12) | (s->v_count[1] << 8) | | |
289 (s->h_count[2] << 4) | s->v_count[2]; | |
290 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id); | |
291 switch(pix_fmt_id){ | |
292 case 0x222222: | |
293 case 0x111111: | |
294 if(s->rgb){ | |
295 s->avctx->pix_fmt = PIX_FMT_RGB32; | |
296 }else if(s->nb_components==3) | |
297 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P; | |
298 else | |
299 s->avctx->pix_fmt = PIX_FMT_GRAY8; | |
300 break; | |
5363 | 301 case 0x121111: |
302 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV440P : PIX_FMT_YUVJ440P; | |
303 break; | |
5020 | 304 case 0x211111: |
305 case 0x221212: | |
306 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P; | |
307 break; | |
308 default: | |
309 case 0x221111: | |
310 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P; | |
311 break; | |
312 } | |
313 if(s->ls){ | |
314 if(s->nb_components > 1) | |
315 s->avctx->pix_fmt = PIX_FMT_RGB24; | |
316 else if(s->bits <= 8) | |
317 s->avctx->pix_fmt = PIX_FMT_GRAY8; | |
318 else | |
319 s->avctx->pix_fmt = PIX_FMT_GRAY16; | |
320 } | |
321 | |
322 if(s->picture.data[0]) | |
323 s->avctx->release_buffer(s->avctx, &s->picture); | |
324 | |
325 s->picture.reference= 0; | |
326 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){ | |
327 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
328 return -1; | |
329 } | |
330 s->picture.pict_type= I_TYPE; | |
331 s->picture.key_frame= 1; | |
332 | |
333 for(i=0; i<3; i++){ | |
334 s->linesize[i]= s->picture.linesize[i] << s->interlaced; | |
335 } | |
336 | |
337 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height); | |
338 | |
339 if (len != (8+(3*nb_components))) | |
340 { | |
341 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len); | |
342 } | |
343 | |
344 /* totally blank picture as progressive JPEG will only add details to it */ | |
345 if(s->progressive){ | |
346 memset(s->picture.data[0], 0, s->picture.linesize[0] * s->height); | |
347 memset(s->picture.data[1], 0, s->picture.linesize[1] * s->height >> (s->v_max - s->v_count[1])); | |
348 memset(s->picture.data[2], 0, s->picture.linesize[2] * s->height >> (s->v_max - s->v_count[2])); | |
349 } | |
350 return 0; | |
351 } | |
352 | |
353 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index) | |
354 { | |
355 int code; | |
356 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2); | |
357 if (code < 0) | |
358 { | |
359 av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index, | |
360 &s->vlcs[0][dc_index]); | |
361 return 0xffff; | |
362 } | |
363 | |
364 if(code) | |
365 return get_xbits(&s->gb, code); | |
366 else | |
367 return 0; | |
368 } | |
369 | |
370 /* decode block and dequantize */ | |
371 static int decode_block(MJpegDecodeContext *s, DCTELEM *block, | |
372 int component, int dc_index, int ac_index, int16_t *quant_matrix) | |
373 { | |
374 int code, i, j, level, val; | |
375 | |
376 /* DC coef */ | |
377 val = mjpeg_decode_dc(s, dc_index); | |
378 if (val == 0xffff) { | |
379 av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); | |
380 return -1; | |
381 } | |
382 val = val * quant_matrix[0] + s->last_dc[component]; | |
383 s->last_dc[component] = val; | |
384 block[0] = val; | |
385 /* AC coefs */ | |
386 i = 0; | |
387 {OPEN_READER(re, &s->gb) | |
388 for(;;) { | |
389 UPDATE_CACHE(re, &s->gb); | |
390 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) | |
391 | |
392 /* EOB */ | |
393 if (code == 0x10) | |
394 break; | |
395 i += ((unsigned)code) >> 4; | |
396 if(code != 0x100){ | |
397 code &= 0xf; | |
398 if(code > MIN_CACHE_BITS - 16){ | |
399 UPDATE_CACHE(re, &s->gb) | |
400 } | |
401 { | |
402 int cache=GET_CACHE(re,&s->gb); | |
403 int sign=(~cache)>>31; | |
404 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; | |
405 } | |
406 | |
407 LAST_SKIP_BITS(re, &s->gb, code) | |
408 | |
409 if (i >= 63) { | |
410 if(i == 63){ | |
411 j = s->scantable.permutated[63]; | |
412 block[j] = level * quant_matrix[j]; | |
413 break; | |
414 } | |
415 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); | |
416 return -1; | |
417 } | |
418 j = s->scantable.permutated[i]; | |
419 block[j] = level * quant_matrix[j]; | |
420 } | |
421 } | |
422 CLOSE_READER(re, &s->gb)} | |
423 | |
424 return 0; | |
425 } | |
426 | |
427 /* decode block and dequantize - progressive JPEG version */ | |
428 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, | |
429 int component, int dc_index, int ac_index, int16_t *quant_matrix, | |
430 int ss, int se, int Ah, int Al, int *EOBRUN) | |
431 { | |
432 int code, i, j, level, val, run; | |
433 | |
434 /* DC coef */ | |
435 if(!ss){ | |
436 val = mjpeg_decode_dc(s, dc_index); | |
437 if (val == 0xffff) { | |
438 av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); | |
439 return -1; | |
440 } | |
441 val = (val * quant_matrix[0] << Al) + s->last_dc[component]; | |
442 }else | |
443 val = 0; | |
444 s->last_dc[component] = val; | |
445 block[0] = val; | |
446 if(!se) return 0; | |
447 /* AC coefs */ | |
448 if(*EOBRUN){ | |
449 (*EOBRUN)--; | |
450 return 0; | |
451 } | |
452 {OPEN_READER(re, &s->gb) | |
453 for(i=ss;;i++) { | |
454 UPDATE_CACHE(re, &s->gb); | |
455 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) | |
456 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */ | |
457 code -= 16; | |
458 if(code & 0xF) { | |
459 i += ((unsigned) code) >> 4; | |
460 code &= 0xf; | |
461 if(code > MIN_CACHE_BITS - 16){ | |
462 UPDATE_CACHE(re, &s->gb) | |
463 } | |
464 { | |
465 int cache=GET_CACHE(re,&s->gb); | |
466 int sign=(~cache)>>31; | |
467 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; | |
468 } | |
469 | |
470 LAST_SKIP_BITS(re, &s->gb, code) | |
471 | |
472 if (i >= se) { | |
473 if(i == se){ | |
474 j = s->scantable.permutated[se]; | |
475 block[j] = level * quant_matrix[j] << Al; | |
476 break; | |
477 } | |
478 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); | |
479 return -1; | |
480 } | |
481 j = s->scantable.permutated[i]; | |
482 block[j] = level * quant_matrix[j] << Al; | |
483 }else{ | |
484 run = ((unsigned) code) >> 4; | |
485 if(run == 0xF){// ZRL - skip 15 coefficients | |
486 i += 15; | |
487 }else{ | |
488 val = run; | |
489 run = (1 << run); | |
490 UPDATE_CACHE(re, &s->gb); | |
491 run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1); | |
492 if(val) | |
493 LAST_SKIP_BITS(re, &s->gb, val); | |
494 *EOBRUN = run - 1; | |
495 break; | |
496 } | |
497 } | |
498 } | |
499 CLOSE_READER(re, &s->gb)} | |
500 | |
501 return 0; | |
502 } | |
503 | |
504 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){ | |
505 int i, mb_x, mb_y; | |
506 uint16_t buffer[32768][4]; | |
507 int left[3], top[3], topleft[3]; | |
508 const int linesize= s->linesize[0]; | |
509 const int mask= (1<<s->bits)-1; | |
510 | |
511 if((unsigned)s->mb_width > 32768) //dynamic alloc | |
512 return -1; | |
513 | |
514 for(i=0; i<3; i++){ | |
515 buffer[0][i]= 1 << (s->bits + point_transform - 1); | |
516 } | |
517 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { | |
518 const int modified_predictor= mb_y ? predictor : 1; | |
519 uint8_t *ptr = s->picture.data[0] + (linesize * mb_y); | |
520 | |
521 if (s->interlaced && s->bottom_field) | |
522 ptr += linesize >> 1; | |
523 | |
524 for(i=0; i<3; i++){ | |
525 top[i]= left[i]= topleft[i]= buffer[0][i]; | |
526 } | |
527 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
528 if (s->restart_interval && !s->restart_count) | |
529 s->restart_count = s->restart_interval; | |
530 | |
531 for(i=0;i<3;i++) { | |
532 int pred; | |
533 | |
534 topleft[i]= top[i]; | |
535 top[i]= buffer[mb_x][i]; | |
536 | |
537 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); | |
538 | |
539 left[i]= | |
540 buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform)); | |
541 } | |
542 | |
543 if (s->restart_interval && !--s->restart_count) { | |
544 align_get_bits(&s->gb); | |
545 skip_bits(&s->gb, 16); /* skip RSTn */ | |
546 } | |
547 } | |
548 | |
549 if(s->rct){ | |
550 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
551 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2); | |
552 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; | |
553 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; | |
554 } | |
555 }else if(s->pegasus_rct){ | |
556 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
557 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2); | |
558 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; | |
559 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; | |
560 } | |
561 }else{ | |
562 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
563 ptr[4*mb_x+0] = buffer[mb_x][0]; | |
564 ptr[4*mb_x+1] = buffer[mb_x][1]; | |
565 ptr[4*mb_x+2] = buffer[mb_x][2]; | |
566 } | |
567 } | |
568 } | |
569 return 0; | |
570 } | |
571 | |
572 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){ | |
573 int i, mb_x, mb_y; | |
574 const int nb_components=3; | |
575 | |
576 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { | |
577 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
578 if (s->restart_interval && !s->restart_count) | |
579 s->restart_count = s->restart_interval; | |
580 | |
581 if(mb_x==0 || mb_y==0 || s->interlaced){ | |
582 for(i=0;i<nb_components;i++) { | |
583 uint8_t *ptr; | |
584 int n, h, v, x, y, c, j, linesize; | |
585 n = s->nb_blocks[i]; | |
586 c = s->comp_index[i]; | |
587 h = s->h_scount[i]; | |
588 v = s->v_scount[i]; | |
589 x = 0; | |
590 y = 0; | |
591 linesize= s->linesize[c]; | |
592 | |
593 for(j=0; j<n; j++) { | |
594 int pred; | |
595 | |
596 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap | |
597 if(y==0 && mb_y==0){ | |
598 if(x==0 && mb_x==0){ | |
599 pred= 128 << point_transform; | |
600 }else{ | |
601 pred= ptr[-1]; | |
602 } | |
603 }else{ | |
604 if(x==0 && mb_x==0){ | |
605 pred= ptr[-linesize]; | |
606 }else{ | |
607 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); | |
608 } | |
609 } | |
610 | |
611 if (s->interlaced && s->bottom_field) | |
612 ptr += linesize >> 1; | |
613 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); | |
614 | |
615 if (++x == h) { | |
616 x = 0; | |
617 y++; | |
618 } | |
619 } | |
620 } | |
621 }else{ | |
622 for(i=0;i<nb_components;i++) { | |
623 uint8_t *ptr; | |
624 int n, h, v, x, y, c, j, linesize; | |
625 n = s->nb_blocks[i]; | |
626 c = s->comp_index[i]; | |
627 h = s->h_scount[i]; | |
628 v = s->v_scount[i]; | |
629 x = 0; | |
630 y = 0; | |
631 linesize= s->linesize[c]; | |
632 | |
633 for(j=0; j<n; j++) { | |
634 int pred; | |
635 | |
636 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap | |
637 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); | |
638 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); | |
639 if (++x == h) { | |
640 x = 0; | |
641 y++; | |
642 } | |
643 } | |
644 } | |
645 } | |
646 if (s->restart_interval && !--s->restart_count) { | |
647 align_get_bits(&s->gb); | |
648 skip_bits(&s->gb, 16); /* skip RSTn */ | |
649 } | |
650 } | |
651 } | |
652 return 0; | |
653 } | |
654 | |
655 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int ss, int se, int Ah, int Al){ | |
656 int i, mb_x, mb_y; | |
657 int EOBRUN = 0; | |
658 | |
659 if(Ah) return 0; /* TODO decode refinement planes too */ | |
660 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { | |
661 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
662 if (s->restart_interval && !s->restart_count) | |
663 s->restart_count = s->restart_interval; | |
664 | |
665 for(i=0;i<nb_components;i++) { | |
666 uint8_t *ptr; | |
667 int n, h, v, x, y, c, j; | |
668 n = s->nb_blocks[i]; | |
669 c = s->comp_index[i]; | |
670 h = s->h_scount[i]; | |
671 v = s->v_scount[i]; | |
672 x = 0; | |
673 y = 0; | |
674 for(j=0;j<n;j++) { | |
675 memset(s->block, 0, sizeof(s->block)); | |
676 if (!s->progressive && decode_block(s, s->block, i, | |
677 s->dc_index[i], s->ac_index[i], | |
678 s->quant_matrixes[ s->quant_index[c] ]) < 0) { | |
679 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); | |
680 return -1; | |
681 } | |
682 if (s->progressive && decode_block_progressive(s, s->block, i, | |
683 s->dc_index[i], s->ac_index[i], | |
684 s->quant_matrixes[ s->quant_index[c] ], ss, se, Ah, Al, &EOBRUN) < 0) { | |
685 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); | |
686 return -1; | |
687 } | |
688 // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x); | |
689 ptr = s->picture.data[c] + | |
690 (((s->linesize[c] * (v * mb_y + y) * 8) + | |
691 (h * mb_x + x) * 8) >> s->avctx->lowres); | |
692 if (s->interlaced && s->bottom_field) | |
693 ptr += s->linesize[c] >> 1; | |
694 //av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d %d %d %d %d \n", mb_x, mb_y, x, y, c, s->bottom_field, (v * mb_y + y) * 8, (h * mb_x + x) * 8); | |
695 if(!s->progressive) | |
696 s->dsp.idct_put(ptr, s->linesize[c], s->block); | |
697 else | |
698 s->dsp.idct_add(ptr, s->linesize[c], s->block); | |
699 if (++x == h) { | |
700 x = 0; | |
701 y++; | |
702 } | |
703 } | |
704 } | |
705 /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */ | |
706 if (s->restart_interval && (s->restart_interval < 1350) && | |
707 !--s->restart_count) { | |
708 align_get_bits(&s->gb); | |
709 skip_bits(&s->gb, 16); /* skip RSTn */ | |
710 for (i=0; i<nb_components; i++) /* reset dc */ | |
711 s->last_dc[i] = 1024; | |
712 } | |
713 } | |
714 } | |
715 return 0; | |
716 } | |
717 | |
5044 | 718 int ff_mjpeg_decode_sos(MJpegDecodeContext *s) |
5020 | 719 { |
720 int len, nb_components, i, h, v, predictor, point_transform; | |
721 int vmax, hmax, index, id; | |
722 const int block_size= s->lossless ? 1 : 8; | |
723 int ilv, prev_shift; | |
724 | |
725 /* XXX: verify len field validity */ | |
726 len = get_bits(&s->gb, 16); | |
727 nb_components = get_bits(&s->gb, 8); | |
728 if (len != 6+2*nb_components) | |
729 { | |
730 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len); | |
731 return -1; | |
732 } | |
733 vmax = 0; | |
734 hmax = 0; | |
735 for(i=0;i<nb_components;i++) { | |
736 id = get_bits(&s->gb, 8) - 1; | |
737 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id); | |
738 /* find component index */ | |
739 for(index=0;index<s->nb_components;index++) | |
740 if (id == s->component_id[index]) | |
741 break; | |
742 if (index == s->nb_components) | |
743 { | |
744 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index); | |
745 return -1; | |
746 } | |
747 | |
748 s->comp_index[i] = index; | |
749 | |
750 s->nb_blocks[i] = s->h_count[index] * s->v_count[index]; | |
751 s->h_scount[i] = s->h_count[index]; | |
752 s->v_scount[i] = s->v_count[index]; | |
753 | |
754 s->dc_index[i] = get_bits(&s->gb, 4); | |
755 s->ac_index[i] = get_bits(&s->gb, 4); | |
756 | |
757 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 || | |
758 s->dc_index[i] >= 4 || s->ac_index[i] >= 4) | |
759 goto out_of_range; | |
760 #if 0 //buggy | |
761 switch(s->start_code) | |
762 { | |
763 case SOF0: | |
764 if (dc_index[i] > 1 || ac_index[i] > 1) | |
765 goto out_of_range; | |
766 break; | |
767 case SOF1: | |
768 case SOF2: | |
769 if (dc_index[i] > 3 || ac_index[i] > 3) | |
770 goto out_of_range; | |
771 break; | |
772 case SOF3: | |
773 if (dc_index[i] > 3 || ac_index[i] != 0) | |
774 goto out_of_range; | |
775 break; | |
776 } | |
777 #endif | |
778 } | |
779 | |
780 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */ | |
781 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */ | |
782 prev_shift = get_bits(&s->gb, 4); /* Ah */ | |
783 point_transform= get_bits(&s->gb, 4); /* Al */ | |
784 | |
785 for(i=0;i<nb_components;i++) | |
786 s->last_dc[i] = 1024; | |
787 | |
788 if (nb_components > 1) { | |
789 /* interleaved stream */ | |
790 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size); | |
791 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size); | |
792 } else if(!s->ls) { /* skip this for JPEG-LS */ | |
793 h = s->h_max / s->h_scount[0]; | |
794 v = s->v_max / s->v_scount[0]; | |
795 s->mb_width = (s->width + h * block_size - 1) / (h * block_size); | |
796 s->mb_height = (s->height + v * block_size - 1) / (v * block_size); | |
797 s->nb_blocks[0] = 1; | |
798 s->h_scount[0] = 1; | |
799 s->v_scount[0] = 1; | |
800 } | |
801 | |
802 if(s->avctx->debug & FF_DEBUG_PICT_INFO) | |
803 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n", s->lossless ? "lossless" : "sequencial DCT", s->rgb ? "RGB" : "", | |
804 predictor, point_transform, ilv, s->bits, | |
805 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : "")); | |
806 | |
807 | |
808 /* mjpeg-b can have padding bytes between sos and image data, skip them */ | |
809 for (i = s->mjpb_skiptosod; i > 0; i--) | |
810 skip_bits(&s->gb, 8); | |
811 | |
812 if(s->lossless){ | |
5041 | 813 if(ENABLE_JPEGLS_DECODER && s->ls){ |
5020 | 814 // for(){ |
815 // reset_ls_coding_parameters(s, 0); | |
816 | |
817 ff_jpegls_decode_picture(s, predictor, point_transform, ilv); | |
818 }else{ | |
819 if(s->rgb){ | |
820 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0) | |
821 return -1; | |
822 }else{ | |
823 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0) | |
824 return -1; | |
825 } | |
826 } | |
827 }else{ | |
828 if(mjpeg_decode_scan(s, nb_components, predictor, ilv, prev_shift, point_transform) < 0) | |
829 return -1; | |
830 } | |
831 emms_c(); | |
832 return 0; | |
833 out_of_range: | |
834 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n"); | |
835 return -1; | |
836 } | |
837 | |
838 static int mjpeg_decode_dri(MJpegDecodeContext *s) | |
839 { | |
840 if (get_bits(&s->gb, 16) != 4) | |
841 return -1; | |
842 s->restart_interval = get_bits(&s->gb, 16); | |
843 s->restart_count = 0; | |
844 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval); | |
845 | |
846 return 0; | |
847 } | |
848 | |
849 static int mjpeg_decode_app(MJpegDecodeContext *s) | |
850 { | |
851 int len, id, i; | |
852 | |
853 len = get_bits(&s->gb, 16); | |
854 if (len < 5) | |
855 return -1; | |
856 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits) | |
857 return -1; | |
858 | |
859 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); | |
860 id = be2me_32(id); | |
861 len -= 6; | |
862 | |
863 if(s->avctx->debug & FF_DEBUG_STARTCODE){ | |
864 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id); | |
865 } | |
866 | |
867 /* buggy AVID, it puts EOI only at every 10th frame */ | |
868 /* also this fourcc is used by non-avid files too, it holds some | |
869 informations, but it's always present in AVID creates files */ | |
870 if (id == ff_get_fourcc("AVI1")) | |
871 { | |
872 /* structure: | |
873 4bytes AVI1 | |
874 1bytes polarity | |
875 1bytes always zero | |
876 4bytes field_size | |
877 4bytes field_size_less_padding | |
878 */ | |
879 s->buggy_avid = 1; | |
880 // if (s->first_picture) | |
881 // printf("mjpeg: workarounding buggy AVID\n"); | |
882 i = get_bits(&s->gb, 8); | |
883 if (i==2) s->bottom_field= 1; | |
884 else if(i==1) s->bottom_field= 0; | |
885 #if 0 | |
886 skip_bits(&s->gb, 8); | |
887 skip_bits(&s->gb, 32); | |
888 skip_bits(&s->gb, 32); | |
889 len -= 10; | |
890 #endif | |
891 // if (s->interlace_polarity) | |
892 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity); | |
893 goto out; | |
894 } | |
895 | |
896 // len -= 2; | |
897 | |
898 if (id == ff_get_fourcc("JFIF")) | |
899 { | |
900 int t_w, t_h, v1, v2; | |
901 skip_bits(&s->gb, 8); /* the trailing zero-byte */ | |
902 v1= get_bits(&s->gb, 8); | |
903 v2= get_bits(&s->gb, 8); | |
904 skip_bits(&s->gb, 8); | |
905 | |
906 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16); | |
907 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16); | |
908 | |
909 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
910 av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n", | |
911 v1, v2, | |
912 s->avctx->sample_aspect_ratio.num, | |
913 s->avctx->sample_aspect_ratio.den | |
914 ); | |
915 | |
916 t_w = get_bits(&s->gb, 8); | |
917 t_h = get_bits(&s->gb, 8); | |
918 if (t_w && t_h) | |
919 { | |
920 /* skip thumbnail */ | |
921 if (len-10-(t_w*t_h*3) > 0) | |
922 len -= t_w*t_h*3; | |
923 } | |
924 len -= 10; | |
925 goto out; | |
926 } | |
927 | |
928 if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e')) | |
929 { | |
930 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
931 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n"); | |
932 skip_bits(&s->gb, 16); /* version */ | |
933 skip_bits(&s->gb, 16); /* flags0 */ | |
934 skip_bits(&s->gb, 16); /* flags1 */ | |
935 skip_bits(&s->gb, 8); /* transform */ | |
936 len -= 7; | |
937 goto out; | |
938 } | |
939 | |
940 if (id == ff_get_fourcc("LJIF")){ | |
941 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
942 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n"); | |
943 skip_bits(&s->gb, 16); /* version ? */ | |
944 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
945 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
946 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
947 switch( get_bits(&s->gb, 8)){ | |
948 case 1: | |
949 s->rgb= 1; | |
950 s->pegasus_rct=0; | |
951 break; | |
952 case 2: | |
953 s->rgb= 1; | |
954 s->pegasus_rct=1; | |
955 break; | |
956 default: | |
957 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n"); | |
958 } | |
959 len -= 9; | |
960 goto out; | |
961 } | |
962 | |
963 /* Apple MJPEG-A */ | |
964 if ((s->start_code == APP1) && (len > (0x28 - 8))) | |
965 { | |
966 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); | |
967 id = be2me_32(id); | |
968 len -= 4; | |
969 if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */ | |
970 { | |
971 #if 0 | |
972 skip_bits(&s->gb, 32); /* field size */ | |
973 skip_bits(&s->gb, 32); /* pad field size */ | |
974 skip_bits(&s->gb, 32); /* next off */ | |
975 skip_bits(&s->gb, 32); /* quant off */ | |
976 skip_bits(&s->gb, 32); /* huff off */ | |
977 skip_bits(&s->gb, 32); /* image off */ | |
978 skip_bits(&s->gb, 32); /* scan off */ | |
979 skip_bits(&s->gb, 32); /* data off */ | |
980 #endif | |
981 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
982 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n"); | |
983 } | |
984 } | |
985 | |
986 out: | |
987 /* slow but needed for extreme adobe jpegs */ | |
988 if (len < 0) | |
989 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n"); | |
990 while(--len > 0) | |
991 skip_bits(&s->gb, 8); | |
992 | |
993 return 0; | |
994 } | |
995 | |
996 static int mjpeg_decode_com(MJpegDecodeContext *s) | |
997 { | |
998 int len = get_bits(&s->gb, 16); | |
999 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) { | |
1000 char *cbuf = av_malloc(len - 1); | |
1001 if (cbuf) { | |
1002 int i; | |
1003 for (i = 0; i < len - 2; i++) | |
1004 cbuf[i] = get_bits(&s->gb, 8); | |
1005 if (i > 0 && cbuf[i-1] == '\n') | |
1006 cbuf[i-1] = 0; | |
1007 else | |
1008 cbuf[i] = 0; | |
1009 | |
1010 if(s->avctx->debug & FF_DEBUG_PICT_INFO) | |
1011 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf); | |
1012 | |
1013 /* buggy avid, it puts EOI only at every 10th frame */ | |
1014 if (!strcmp(cbuf, "AVID")) | |
1015 { | |
1016 s->buggy_avid = 1; | |
1017 // if (s->first_picture) | |
1018 // printf("mjpeg: workarounding buggy AVID\n"); | |
1019 } | |
1020 else if(!strcmp(cbuf, "CS=ITU601")){ | |
1021 s->cs_itu601= 1; | |
1022 } | |
1023 | |
1024 av_free(cbuf); | |
1025 } | |
1026 } | |
1027 | |
1028 return 0; | |
1029 } | |
1030 | |
1031 #if 0 | |
1032 static int valid_marker_list[] = | |
1033 { | |
1034 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */ | |
1035 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1036 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1037 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1038 /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1039 /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1040 /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1041 /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1042 /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1043 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1044 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1045 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1046 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1047 /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1048 /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1049 /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1050 /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, | |
1051 } | |
1052 #endif | |
1053 | |
1054 /* return the 8 bit start code value and update the search | |
1055 state. Return -1 if no start code found */ | |
1056 static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end) | |
1057 { | |
1058 uint8_t *buf_ptr; | |
1059 unsigned int v, v2; | |
1060 int val; | |
1061 #ifdef DEBUG | |
1062 int skipped=0; | |
1063 #endif | |
1064 | |
1065 buf_ptr = *pbuf_ptr; | |
1066 while (buf_ptr < buf_end) { | |
1067 v = *buf_ptr++; | |
1068 v2 = *buf_ptr; | |
1069 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) { | |
1070 val = *buf_ptr++; | |
1071 goto found; | |
1072 } | |
1073 #ifdef DEBUG | |
1074 skipped++; | |
1075 #endif | |
1076 } | |
1077 val = -1; | |
1078 found: | |
1079 #ifdef DEBUG | |
1080 av_log(NULL, AV_LOG_VERBOSE, "find_marker skipped %d bytes\n", skipped); | |
1081 #endif | |
1082 *pbuf_ptr = buf_ptr; | |
1083 return val; | |
1084 } | |
1085 | |
5041 | 1086 int ff_mjpeg_decode_frame(AVCodecContext *avctx, |
5020 | 1087 void *data, int *data_size, |
1088 uint8_t *buf, int buf_size) | |
1089 { | |
1090 MJpegDecodeContext *s = avctx->priv_data; | |
1091 uint8_t *buf_end, *buf_ptr; | |
1092 int start_code; | |
1093 AVFrame *picture = data; | |
1094 | |
1095 buf_ptr = buf; | |
1096 buf_end = buf + buf_size; | |
1097 while (buf_ptr < buf_end) { | |
1098 /* find start next marker */ | |
1099 start_code = find_marker(&buf_ptr, buf_end); | |
1100 { | |
1101 /* EOF */ | |
1102 if (start_code < 0) { | |
1103 goto the_end; | |
1104 } else { | |
5153 | 1105 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr); |
5020 | 1106 |
1107 if ((buf_end - buf_ptr) > s->buffer_size) | |
1108 { | |
1109 av_free(s->buffer); | |
1110 s->buffer_size = buf_end-buf_ptr; | |
1111 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
1112 av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n", | |
1113 s->buffer_size); | |
1114 } | |
1115 | |
1116 /* unescape buffer of SOS, use special treatment for JPEG-LS */ | |
1117 if (start_code == SOS && !s->ls) | |
1118 { | |
1119 uint8_t *src = buf_ptr; | |
1120 uint8_t *dst = s->buffer; | |
1121 | |
1122 while (src<buf_end) | |
1123 { | |
1124 uint8_t x = *(src++); | |
1125 | |
1126 *(dst++) = x; | |
1127 if (avctx->codec_id != CODEC_ID_THP) | |
1128 { | |
1129 if (x == 0xff) { | |
1130 while (src < buf_end && x == 0xff) | |
1131 x = *(src++); | |
1132 | |
1133 if (x >= 0xd0 && x <= 0xd7) | |
1134 *(dst++) = x; | |
1135 else if (x) | |
1136 break; | |
1137 } | |
1138 } | |
1139 } | |
1140 init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8); | |
1141 | |
5153 | 1142 av_log(avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n", |
5020 | 1143 (buf_end - buf_ptr) - (dst - s->buffer)); |
1144 } | |
1145 else if(start_code == SOS && s->ls){ | |
1146 uint8_t *src = buf_ptr; | |
1147 uint8_t *dst = s->buffer; | |
1148 int bit_count = 0; | |
1149 int t = 0, b = 0; | |
1150 PutBitContext pb; | |
1151 | |
1152 s->cur_scan++; | |
1153 | |
1154 /* find marker */ | |
1155 while (src + t < buf_end){ | |
1156 uint8_t x = src[t++]; | |
1157 if (x == 0xff){ | |
1158 while((src + t < buf_end) && x == 0xff) | |
1159 x = src[t++]; | |
1160 if (x & 0x80) { | |
1161 t -= 2; | |
1162 break; | |
1163 } | |
1164 } | |
1165 } | |
1166 bit_count = t * 8; | |
1167 | |
1168 init_put_bits(&pb, dst, t); | |
1169 | |
1170 /* unescape bitstream */ | |
1171 while(b < t){ | |
1172 uint8_t x = src[b++]; | |
1173 put_bits(&pb, 8, x); | |
1174 if(x == 0xFF){ | |
1175 x = src[b++]; | |
1176 put_bits(&pb, 7, x); | |
1177 bit_count--; | |
1178 } | |
1179 } | |
1180 flush_put_bits(&pb); | |
1181 | |
1182 init_get_bits(&s->gb, dst, bit_count); | |
1183 } | |
1184 else | |
1185 init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8); | |
1186 | |
1187 s->start_code = start_code; | |
1188 if(s->avctx->debug & FF_DEBUG_STARTCODE){ | |
1189 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code); | |
1190 } | |
1191 | |
1192 /* process markers */ | |
1193 if (start_code >= 0xd0 && start_code <= 0xd7) { | |
1194 av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f); | |
1195 /* APP fields */ | |
1196 } else if (start_code >= APP0 && start_code <= APP15) { | |
1197 mjpeg_decode_app(s); | |
1198 /* Comment */ | |
1199 } else if (start_code == COM){ | |
1200 mjpeg_decode_com(s); | |
1201 } | |
1202 | |
1203 switch(start_code) { | |
1204 case SOI: | |
1205 s->restart_interval = 0; | |
1206 | |
1207 s->restart_count = 0; | |
1208 /* nothing to do on SOI */ | |
1209 break; | |
1210 case DQT: | |
5044 | 1211 ff_mjpeg_decode_dqt(s); |
5020 | 1212 break; |
1213 case DHT: | |
5044 | 1214 if(ff_mjpeg_decode_dht(s) < 0){ |
5020 | 1215 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n"); |
1216 return -1; | |
1217 } | |
1218 break; | |
1219 case SOF0: | |
1220 s->lossless=0; | |
1221 s->ls=0; | |
1222 s->progressive=0; | |
5044 | 1223 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1224 return -1; |
1225 break; | |
1226 case SOF2: | |
1227 s->lossless=0; | |
1228 s->ls=0; | |
1229 s->progressive=1; | |
5044 | 1230 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1231 return -1; |
1232 break; | |
1233 case SOF3: | |
1234 s->lossless=1; | |
1235 s->ls=0; | |
1236 s->progressive=0; | |
5044 | 1237 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1238 return -1; |
1239 break; | |
1240 case SOF48: | |
1241 s->lossless=1; | |
1242 s->ls=1; | |
1243 s->progressive=0; | |
5044 | 1244 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1245 return -1; |
1246 break; | |
1247 case LSE: | |
5041 | 1248 if (!ENABLE_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0) |
5020 | 1249 return -1; |
1250 break; | |
1251 case EOI: | |
1252 s->cur_scan = 0; | |
1253 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) | |
1254 break; | |
1255 eoi_parser: | |
1256 { | |
1257 if (s->interlaced) { | |
1258 s->bottom_field ^= 1; | |
1259 /* if not bottom field, do not output image yet */ | |
1260 if (s->bottom_field == !s->interlace_polarity) | |
1261 goto not_the_end; | |
1262 } | |
1263 *picture = s->picture; | |
1264 *data_size = sizeof(AVFrame); | |
1265 | |
1266 if(!s->lossless){ | |
1267 picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]); | |
1268 picture->qstride= 0; | |
1269 picture->qscale_table= s->qscale_table; | |
1270 memset(picture->qscale_table, picture->quality, (s->width+15)/16); | |
1271 if(avctx->debug & FF_DEBUG_QP) | |
1272 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality); | |
1273 picture->quality*= FF_QP2LAMBDA; | |
1274 } | |
1275 | |
1276 goto the_end; | |
1277 } | |
1278 break; | |
1279 case SOS: | |
5044 | 1280 ff_mjpeg_decode_sos(s); |
5020 | 1281 /* buggy avid puts EOI every 10-20th frame */ |
1282 /* if restart period is over process EOI */ | |
1283 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) | |
1284 goto eoi_parser; | |
1285 break; | |
1286 case DRI: | |
1287 mjpeg_decode_dri(s); | |
1288 break; | |
1289 case SOF1: | |
1290 case SOF5: | |
1291 case SOF6: | |
1292 case SOF7: | |
1293 case SOF9: | |
1294 case SOF10: | |
1295 case SOF11: | |
1296 case SOF13: | |
1297 case SOF14: | |
1298 case SOF15: | |
1299 case JPG: | |
1300 av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code); | |
1301 break; | |
1302 // default: | |
1303 // printf("mjpeg: unsupported marker (%x)\n", start_code); | |
1304 // break; | |
1305 } | |
1306 | |
1307 not_the_end: | |
1308 /* eof process start code */ | |
1309 buf_ptr += (get_bits_count(&s->gb)+7)/8; | |
1310 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n", | |
1311 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb)); | |
1312 } | |
1313 } | |
1314 } | |
1315 the_end: | |
5153 | 1316 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr); |
5020 | 1317 // return buf_end - buf_ptr; |
1318 return buf_ptr - buf; | |
1319 } | |
1320 | |
5041 | 1321 int ff_mjpeg_decode_end(AVCodecContext *avctx) |
5020 | 1322 { |
1323 MJpegDecodeContext *s = avctx->priv_data; | |
1324 int i, j; | |
1325 | |
1326 av_free(s->buffer); | |
1327 av_free(s->qscale_table); | |
1328 | |
1329 for(i=0;i<2;i++) { | |
1330 for(j=0;j<4;j++) | |
1331 free_vlc(&s->vlcs[i][j]); | |
1332 } | |
1333 return 0; | |
1334 } | |
1335 | |
1336 AVCodec mjpeg_decoder = { | |
1337 "mjpeg", | |
1338 CODEC_TYPE_VIDEO, | |
1339 CODEC_ID_MJPEG, | |
1340 sizeof(MJpegDecodeContext), | |
5041 | 1341 ff_mjpeg_decode_init, |
5020 | 1342 NULL, |
5041 | 1343 ff_mjpeg_decode_end, |
1344 ff_mjpeg_decode_frame, | |
5020 | 1345 CODEC_CAP_DR1, |
1346 NULL | |
1347 }; | |
1348 | |
1349 AVCodec thp_decoder = { | |
1350 "thp", | |
1351 CODEC_TYPE_VIDEO, | |
1352 CODEC_ID_THP, | |
1353 sizeof(MJpegDecodeContext), | |
5041 | 1354 ff_mjpeg_decode_init, |
5020 | 1355 NULL, |
5041 | 1356 ff_mjpeg_decode_end, |
1357 ff_mjpeg_decode_frame, | |
5020 | 1358 CODEC_CAP_DR1, |
1359 NULL | |
1360 }; |