Mercurial > libavcodec.hg
annotate mjpegdec.c @ 5317:6d81881f257f libavcodec
AC-3 decoder, soc revision 57, Aug 19 12:44:38 2006 UTC by cloud9
Optimization as suggested by Benjamin.
author | jbr |
---|---|
date | Sat, 14 Jul 2007 16:02:08 +0000 |
parents | 470601203f44 |
children | 1a25f2f6316e |
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; | |
301 case 0x211111: | |
302 case 0x221212: | |
303 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P; | |
304 break; | |
305 default: | |
306 case 0x221111: | |
307 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P; | |
308 break; | |
309 } | |
310 if(s->ls){ | |
311 if(s->nb_components > 1) | |
312 s->avctx->pix_fmt = PIX_FMT_RGB24; | |
313 else if(s->bits <= 8) | |
314 s->avctx->pix_fmt = PIX_FMT_GRAY8; | |
315 else | |
316 s->avctx->pix_fmt = PIX_FMT_GRAY16; | |
317 } | |
318 | |
319 if(s->picture.data[0]) | |
320 s->avctx->release_buffer(s->avctx, &s->picture); | |
321 | |
322 s->picture.reference= 0; | |
323 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){ | |
324 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); | |
325 return -1; | |
326 } | |
327 s->picture.pict_type= I_TYPE; | |
328 s->picture.key_frame= 1; | |
329 | |
330 for(i=0; i<3; i++){ | |
331 s->linesize[i]= s->picture.linesize[i] << s->interlaced; | |
332 } | |
333 | |
334 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height); | |
335 | |
336 if (len != (8+(3*nb_components))) | |
337 { | |
338 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len); | |
339 } | |
340 | |
341 /* totally blank picture as progressive JPEG will only add details to it */ | |
342 if(s->progressive){ | |
343 memset(s->picture.data[0], 0, s->picture.linesize[0] * s->height); | |
344 memset(s->picture.data[1], 0, s->picture.linesize[1] * s->height >> (s->v_max - s->v_count[1])); | |
345 memset(s->picture.data[2], 0, s->picture.linesize[2] * s->height >> (s->v_max - s->v_count[2])); | |
346 } | |
347 return 0; | |
348 } | |
349 | |
350 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index) | |
351 { | |
352 int code; | |
353 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2); | |
354 if (code < 0) | |
355 { | |
356 av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index, | |
357 &s->vlcs[0][dc_index]); | |
358 return 0xffff; | |
359 } | |
360 | |
361 if(code) | |
362 return get_xbits(&s->gb, code); | |
363 else | |
364 return 0; | |
365 } | |
366 | |
367 /* decode block and dequantize */ | |
368 static int decode_block(MJpegDecodeContext *s, DCTELEM *block, | |
369 int component, int dc_index, int ac_index, int16_t *quant_matrix) | |
370 { | |
371 int code, i, j, level, val; | |
372 | |
373 /* DC coef */ | |
374 val = mjpeg_decode_dc(s, dc_index); | |
375 if (val == 0xffff) { | |
376 av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); | |
377 return -1; | |
378 } | |
379 val = val * quant_matrix[0] + s->last_dc[component]; | |
380 s->last_dc[component] = val; | |
381 block[0] = val; | |
382 /* AC coefs */ | |
383 i = 0; | |
384 {OPEN_READER(re, &s->gb) | |
385 for(;;) { | |
386 UPDATE_CACHE(re, &s->gb); | |
387 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) | |
388 | |
389 /* EOB */ | |
390 if (code == 0x10) | |
391 break; | |
392 i += ((unsigned)code) >> 4; | |
393 if(code != 0x100){ | |
394 code &= 0xf; | |
395 if(code > MIN_CACHE_BITS - 16){ | |
396 UPDATE_CACHE(re, &s->gb) | |
397 } | |
398 { | |
399 int cache=GET_CACHE(re,&s->gb); | |
400 int sign=(~cache)>>31; | |
401 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; | |
402 } | |
403 | |
404 LAST_SKIP_BITS(re, &s->gb, code) | |
405 | |
406 if (i >= 63) { | |
407 if(i == 63){ | |
408 j = s->scantable.permutated[63]; | |
409 block[j] = level * quant_matrix[j]; | |
410 break; | |
411 } | |
412 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); | |
413 return -1; | |
414 } | |
415 j = s->scantable.permutated[i]; | |
416 block[j] = level * quant_matrix[j]; | |
417 } | |
418 } | |
419 CLOSE_READER(re, &s->gb)} | |
420 | |
421 return 0; | |
422 } | |
423 | |
424 /* decode block and dequantize - progressive JPEG version */ | |
425 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, | |
426 int component, int dc_index, int ac_index, int16_t *quant_matrix, | |
427 int ss, int se, int Ah, int Al, int *EOBRUN) | |
428 { | |
429 int code, i, j, level, val, run; | |
430 | |
431 /* DC coef */ | |
432 if(!ss){ | |
433 val = mjpeg_decode_dc(s, dc_index); | |
434 if (val == 0xffff) { | |
435 av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); | |
436 return -1; | |
437 } | |
438 val = (val * quant_matrix[0] << Al) + s->last_dc[component]; | |
439 }else | |
440 val = 0; | |
441 s->last_dc[component] = val; | |
442 block[0] = val; | |
443 if(!se) return 0; | |
444 /* AC coefs */ | |
445 if(*EOBRUN){ | |
446 (*EOBRUN)--; | |
447 return 0; | |
448 } | |
449 {OPEN_READER(re, &s->gb) | |
450 for(i=ss;;i++) { | |
451 UPDATE_CACHE(re, &s->gb); | |
452 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) | |
453 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */ | |
454 code -= 16; | |
455 if(code & 0xF) { | |
456 i += ((unsigned) code) >> 4; | |
457 code &= 0xf; | |
458 if(code > MIN_CACHE_BITS - 16){ | |
459 UPDATE_CACHE(re, &s->gb) | |
460 } | |
461 { | |
462 int cache=GET_CACHE(re,&s->gb); | |
463 int sign=(~cache)>>31; | |
464 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; | |
465 } | |
466 | |
467 LAST_SKIP_BITS(re, &s->gb, code) | |
468 | |
469 if (i >= se) { | |
470 if(i == se){ | |
471 j = s->scantable.permutated[se]; | |
472 block[j] = level * quant_matrix[j] << Al; | |
473 break; | |
474 } | |
475 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); | |
476 return -1; | |
477 } | |
478 j = s->scantable.permutated[i]; | |
479 block[j] = level * quant_matrix[j] << Al; | |
480 }else{ | |
481 run = ((unsigned) code) >> 4; | |
482 if(run == 0xF){// ZRL - skip 15 coefficients | |
483 i += 15; | |
484 }else{ | |
485 val = run; | |
486 run = (1 << run); | |
487 UPDATE_CACHE(re, &s->gb); | |
488 run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1); | |
489 if(val) | |
490 LAST_SKIP_BITS(re, &s->gb, val); | |
491 *EOBRUN = run - 1; | |
492 break; | |
493 } | |
494 } | |
495 } | |
496 CLOSE_READER(re, &s->gb)} | |
497 | |
498 return 0; | |
499 } | |
500 | |
501 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){ | |
502 int i, mb_x, mb_y; | |
503 uint16_t buffer[32768][4]; | |
504 int left[3], top[3], topleft[3]; | |
505 const int linesize= s->linesize[0]; | |
506 const int mask= (1<<s->bits)-1; | |
507 | |
508 if((unsigned)s->mb_width > 32768) //dynamic alloc | |
509 return -1; | |
510 | |
511 for(i=0; i<3; i++){ | |
512 buffer[0][i]= 1 << (s->bits + point_transform - 1); | |
513 } | |
514 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { | |
515 const int modified_predictor= mb_y ? predictor : 1; | |
516 uint8_t *ptr = s->picture.data[0] + (linesize * mb_y); | |
517 | |
518 if (s->interlaced && s->bottom_field) | |
519 ptr += linesize >> 1; | |
520 | |
521 for(i=0; i<3; i++){ | |
522 top[i]= left[i]= topleft[i]= buffer[0][i]; | |
523 } | |
524 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
525 if (s->restart_interval && !s->restart_count) | |
526 s->restart_count = s->restart_interval; | |
527 | |
528 for(i=0;i<3;i++) { | |
529 int pred; | |
530 | |
531 topleft[i]= top[i]; | |
532 top[i]= buffer[mb_x][i]; | |
533 | |
534 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); | |
535 | |
536 left[i]= | |
537 buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform)); | |
538 } | |
539 | |
540 if (s->restart_interval && !--s->restart_count) { | |
541 align_get_bits(&s->gb); | |
542 skip_bits(&s->gb, 16); /* skip RSTn */ | |
543 } | |
544 } | |
545 | |
546 if(s->rct){ | |
547 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
548 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2); | |
549 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; | |
550 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; | |
551 } | |
552 }else if(s->pegasus_rct){ | |
553 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
554 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2); | |
555 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; | |
556 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; | |
557 } | |
558 }else{ | |
559 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
560 ptr[4*mb_x+0] = buffer[mb_x][0]; | |
561 ptr[4*mb_x+1] = buffer[mb_x][1]; | |
562 ptr[4*mb_x+2] = buffer[mb_x][2]; | |
563 } | |
564 } | |
565 } | |
566 return 0; | |
567 } | |
568 | |
569 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){ | |
570 int i, mb_x, mb_y; | |
571 const int nb_components=3; | |
572 | |
573 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { | |
574 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
575 if (s->restart_interval && !s->restart_count) | |
576 s->restart_count = s->restart_interval; | |
577 | |
578 if(mb_x==0 || mb_y==0 || s->interlaced){ | |
579 for(i=0;i<nb_components;i++) { | |
580 uint8_t *ptr; | |
581 int n, h, v, x, y, c, j, linesize; | |
582 n = s->nb_blocks[i]; | |
583 c = s->comp_index[i]; | |
584 h = s->h_scount[i]; | |
585 v = s->v_scount[i]; | |
586 x = 0; | |
587 y = 0; | |
588 linesize= s->linesize[c]; | |
589 | |
590 for(j=0; j<n; j++) { | |
591 int pred; | |
592 | |
593 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap | |
594 if(y==0 && mb_y==0){ | |
595 if(x==0 && mb_x==0){ | |
596 pred= 128 << point_transform; | |
597 }else{ | |
598 pred= ptr[-1]; | |
599 } | |
600 }else{ | |
601 if(x==0 && mb_x==0){ | |
602 pred= ptr[-linesize]; | |
603 }else{ | |
604 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); | |
605 } | |
606 } | |
607 | |
608 if (s->interlaced && s->bottom_field) | |
609 ptr += linesize >> 1; | |
610 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); | |
611 | |
612 if (++x == h) { | |
613 x = 0; | |
614 y++; | |
615 } | |
616 } | |
617 } | |
618 }else{ | |
619 for(i=0;i<nb_components;i++) { | |
620 uint8_t *ptr; | |
621 int n, h, v, x, y, c, j, linesize; | |
622 n = s->nb_blocks[i]; | |
623 c = s->comp_index[i]; | |
624 h = s->h_scount[i]; | |
625 v = s->v_scount[i]; | |
626 x = 0; | |
627 y = 0; | |
628 linesize= s->linesize[c]; | |
629 | |
630 for(j=0; j<n; j++) { | |
631 int pred; | |
632 | |
633 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap | |
634 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); | |
635 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); | |
636 if (++x == h) { | |
637 x = 0; | |
638 y++; | |
639 } | |
640 } | |
641 } | |
642 } | |
643 if (s->restart_interval && !--s->restart_count) { | |
644 align_get_bits(&s->gb); | |
645 skip_bits(&s->gb, 16); /* skip RSTn */ | |
646 } | |
647 } | |
648 } | |
649 return 0; | |
650 } | |
651 | |
652 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int ss, int se, int Ah, int Al){ | |
653 int i, mb_x, mb_y; | |
654 int EOBRUN = 0; | |
655 | |
656 if(Ah) return 0; /* TODO decode refinement planes too */ | |
657 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { | |
658 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { | |
659 if (s->restart_interval && !s->restart_count) | |
660 s->restart_count = s->restart_interval; | |
661 | |
662 for(i=0;i<nb_components;i++) { | |
663 uint8_t *ptr; | |
664 int n, h, v, x, y, c, j; | |
665 n = s->nb_blocks[i]; | |
666 c = s->comp_index[i]; | |
667 h = s->h_scount[i]; | |
668 v = s->v_scount[i]; | |
669 x = 0; | |
670 y = 0; | |
671 for(j=0;j<n;j++) { | |
672 memset(s->block, 0, sizeof(s->block)); | |
673 if (!s->progressive && decode_block(s, s->block, i, | |
674 s->dc_index[i], s->ac_index[i], | |
675 s->quant_matrixes[ s->quant_index[c] ]) < 0) { | |
676 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); | |
677 return -1; | |
678 } | |
679 if (s->progressive && decode_block_progressive(s, s->block, i, | |
680 s->dc_index[i], s->ac_index[i], | |
681 s->quant_matrixes[ s->quant_index[c] ], ss, se, Ah, Al, &EOBRUN) < 0) { | |
682 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); | |
683 return -1; | |
684 } | |
685 // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x); | |
686 ptr = s->picture.data[c] + | |
687 (((s->linesize[c] * (v * mb_y + y) * 8) + | |
688 (h * mb_x + x) * 8) >> s->avctx->lowres); | |
689 if (s->interlaced && s->bottom_field) | |
690 ptr += s->linesize[c] >> 1; | |
691 //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); | |
692 if(!s->progressive) | |
693 s->dsp.idct_put(ptr, s->linesize[c], s->block); | |
694 else | |
695 s->dsp.idct_add(ptr, s->linesize[c], s->block); | |
696 if (++x == h) { | |
697 x = 0; | |
698 y++; | |
699 } | |
700 } | |
701 } | |
702 /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */ | |
703 if (s->restart_interval && (s->restart_interval < 1350) && | |
704 !--s->restart_count) { | |
705 align_get_bits(&s->gb); | |
706 skip_bits(&s->gb, 16); /* skip RSTn */ | |
707 for (i=0; i<nb_components; i++) /* reset dc */ | |
708 s->last_dc[i] = 1024; | |
709 } | |
710 } | |
711 } | |
712 return 0; | |
713 } | |
714 | |
5044 | 715 int ff_mjpeg_decode_sos(MJpegDecodeContext *s) |
5020 | 716 { |
717 int len, nb_components, i, h, v, predictor, point_transform; | |
718 int vmax, hmax, index, id; | |
719 const int block_size= s->lossless ? 1 : 8; | |
720 int ilv, prev_shift; | |
721 | |
722 /* XXX: verify len field validity */ | |
723 len = get_bits(&s->gb, 16); | |
724 nb_components = get_bits(&s->gb, 8); | |
725 if (len != 6+2*nb_components) | |
726 { | |
727 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len); | |
728 return -1; | |
729 } | |
730 vmax = 0; | |
731 hmax = 0; | |
732 for(i=0;i<nb_components;i++) { | |
733 id = get_bits(&s->gb, 8) - 1; | |
734 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id); | |
735 /* find component index */ | |
736 for(index=0;index<s->nb_components;index++) | |
737 if (id == s->component_id[index]) | |
738 break; | |
739 if (index == s->nb_components) | |
740 { | |
741 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index); | |
742 return -1; | |
743 } | |
744 | |
745 s->comp_index[i] = index; | |
746 | |
747 s->nb_blocks[i] = s->h_count[index] * s->v_count[index]; | |
748 s->h_scount[i] = s->h_count[index]; | |
749 s->v_scount[i] = s->v_count[index]; | |
750 | |
751 s->dc_index[i] = get_bits(&s->gb, 4); | |
752 s->ac_index[i] = get_bits(&s->gb, 4); | |
753 | |
754 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 || | |
755 s->dc_index[i] >= 4 || s->ac_index[i] >= 4) | |
756 goto out_of_range; | |
757 #if 0 //buggy | |
758 switch(s->start_code) | |
759 { | |
760 case SOF0: | |
761 if (dc_index[i] > 1 || ac_index[i] > 1) | |
762 goto out_of_range; | |
763 break; | |
764 case SOF1: | |
765 case SOF2: | |
766 if (dc_index[i] > 3 || ac_index[i] > 3) | |
767 goto out_of_range; | |
768 break; | |
769 case SOF3: | |
770 if (dc_index[i] > 3 || ac_index[i] != 0) | |
771 goto out_of_range; | |
772 break; | |
773 } | |
774 #endif | |
775 } | |
776 | |
777 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */ | |
778 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */ | |
779 prev_shift = get_bits(&s->gb, 4); /* Ah */ | |
780 point_transform= get_bits(&s->gb, 4); /* Al */ | |
781 | |
782 for(i=0;i<nb_components;i++) | |
783 s->last_dc[i] = 1024; | |
784 | |
785 if (nb_components > 1) { | |
786 /* interleaved stream */ | |
787 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size); | |
788 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size); | |
789 } else if(!s->ls) { /* skip this for JPEG-LS */ | |
790 h = s->h_max / s->h_scount[0]; | |
791 v = s->v_max / s->v_scount[0]; | |
792 s->mb_width = (s->width + h * block_size - 1) / (h * block_size); | |
793 s->mb_height = (s->height + v * block_size - 1) / (v * block_size); | |
794 s->nb_blocks[0] = 1; | |
795 s->h_scount[0] = 1; | |
796 s->v_scount[0] = 1; | |
797 } | |
798 | |
799 if(s->avctx->debug & FF_DEBUG_PICT_INFO) | |
800 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" : "", | |
801 predictor, point_transform, ilv, s->bits, | |
802 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : "")); | |
803 | |
804 | |
805 /* mjpeg-b can have padding bytes between sos and image data, skip them */ | |
806 for (i = s->mjpb_skiptosod; i > 0; i--) | |
807 skip_bits(&s->gb, 8); | |
808 | |
809 if(s->lossless){ | |
5041 | 810 if(ENABLE_JPEGLS_DECODER && s->ls){ |
5020 | 811 // for(){ |
812 // reset_ls_coding_parameters(s, 0); | |
813 | |
814 ff_jpegls_decode_picture(s, predictor, point_transform, ilv); | |
815 }else{ | |
816 if(s->rgb){ | |
817 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0) | |
818 return -1; | |
819 }else{ | |
820 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0) | |
821 return -1; | |
822 } | |
823 } | |
824 }else{ | |
825 if(mjpeg_decode_scan(s, nb_components, predictor, ilv, prev_shift, point_transform) < 0) | |
826 return -1; | |
827 } | |
828 emms_c(); | |
829 return 0; | |
830 out_of_range: | |
831 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n"); | |
832 return -1; | |
833 } | |
834 | |
835 static int mjpeg_decode_dri(MJpegDecodeContext *s) | |
836 { | |
837 if (get_bits(&s->gb, 16) != 4) | |
838 return -1; | |
839 s->restart_interval = get_bits(&s->gb, 16); | |
840 s->restart_count = 0; | |
841 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval); | |
842 | |
843 return 0; | |
844 } | |
845 | |
846 static int mjpeg_decode_app(MJpegDecodeContext *s) | |
847 { | |
848 int len, id, i; | |
849 | |
850 len = get_bits(&s->gb, 16); | |
851 if (len < 5) | |
852 return -1; | |
853 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits) | |
854 return -1; | |
855 | |
856 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); | |
857 id = be2me_32(id); | |
858 len -= 6; | |
859 | |
860 if(s->avctx->debug & FF_DEBUG_STARTCODE){ | |
861 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id); | |
862 } | |
863 | |
864 /* buggy AVID, it puts EOI only at every 10th frame */ | |
865 /* also this fourcc is used by non-avid files too, it holds some | |
866 informations, but it's always present in AVID creates files */ | |
867 if (id == ff_get_fourcc("AVI1")) | |
868 { | |
869 /* structure: | |
870 4bytes AVI1 | |
871 1bytes polarity | |
872 1bytes always zero | |
873 4bytes field_size | |
874 4bytes field_size_less_padding | |
875 */ | |
876 s->buggy_avid = 1; | |
877 // if (s->first_picture) | |
878 // printf("mjpeg: workarounding buggy AVID\n"); | |
879 i = get_bits(&s->gb, 8); | |
880 if (i==2) s->bottom_field= 1; | |
881 else if(i==1) s->bottom_field= 0; | |
882 #if 0 | |
883 skip_bits(&s->gb, 8); | |
884 skip_bits(&s->gb, 32); | |
885 skip_bits(&s->gb, 32); | |
886 len -= 10; | |
887 #endif | |
888 // if (s->interlace_polarity) | |
889 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity); | |
890 goto out; | |
891 } | |
892 | |
893 // len -= 2; | |
894 | |
895 if (id == ff_get_fourcc("JFIF")) | |
896 { | |
897 int t_w, t_h, v1, v2; | |
898 skip_bits(&s->gb, 8); /* the trailing zero-byte */ | |
899 v1= get_bits(&s->gb, 8); | |
900 v2= get_bits(&s->gb, 8); | |
901 skip_bits(&s->gb, 8); | |
902 | |
903 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16); | |
904 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16); | |
905 | |
906 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
907 av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n", | |
908 v1, v2, | |
909 s->avctx->sample_aspect_ratio.num, | |
910 s->avctx->sample_aspect_ratio.den | |
911 ); | |
912 | |
913 t_w = get_bits(&s->gb, 8); | |
914 t_h = get_bits(&s->gb, 8); | |
915 if (t_w && t_h) | |
916 { | |
917 /* skip thumbnail */ | |
918 if (len-10-(t_w*t_h*3) > 0) | |
919 len -= t_w*t_h*3; | |
920 } | |
921 len -= 10; | |
922 goto out; | |
923 } | |
924 | |
925 if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e')) | |
926 { | |
927 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
928 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n"); | |
929 skip_bits(&s->gb, 16); /* version */ | |
930 skip_bits(&s->gb, 16); /* flags0 */ | |
931 skip_bits(&s->gb, 16); /* flags1 */ | |
932 skip_bits(&s->gb, 8); /* transform */ | |
933 len -= 7; | |
934 goto out; | |
935 } | |
936 | |
937 if (id == ff_get_fourcc("LJIF")){ | |
938 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
939 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n"); | |
940 skip_bits(&s->gb, 16); /* version ? */ | |
941 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
942 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
943 skip_bits(&s->gb, 16); /* unknwon always 0? */ | |
944 switch( get_bits(&s->gb, 8)){ | |
945 case 1: | |
946 s->rgb= 1; | |
947 s->pegasus_rct=0; | |
948 break; | |
949 case 2: | |
950 s->rgb= 1; | |
951 s->pegasus_rct=1; | |
952 break; | |
953 default: | |
954 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n"); | |
955 } | |
956 len -= 9; | |
957 goto out; | |
958 } | |
959 | |
960 /* Apple MJPEG-A */ | |
961 if ((s->start_code == APP1) && (len > (0x28 - 8))) | |
962 { | |
963 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); | |
964 id = be2me_32(id); | |
965 len -= 4; | |
966 if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */ | |
967 { | |
968 #if 0 | |
969 skip_bits(&s->gb, 32); /* field size */ | |
970 skip_bits(&s->gb, 32); /* pad field size */ | |
971 skip_bits(&s->gb, 32); /* next off */ | |
972 skip_bits(&s->gb, 32); /* quant off */ | |
973 skip_bits(&s->gb, 32); /* huff off */ | |
974 skip_bits(&s->gb, 32); /* image off */ | |
975 skip_bits(&s->gb, 32); /* scan off */ | |
976 skip_bits(&s->gb, 32); /* data off */ | |
977 #endif | |
978 if (s->avctx->debug & FF_DEBUG_PICT_INFO) | |
979 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n"); | |
980 } | |
981 } | |
982 | |
983 out: | |
984 /* slow but needed for extreme adobe jpegs */ | |
985 if (len < 0) | |
986 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n"); | |
987 while(--len > 0) | |
988 skip_bits(&s->gb, 8); | |
989 | |
990 return 0; | |
991 } | |
992 | |
993 static int mjpeg_decode_com(MJpegDecodeContext *s) | |
994 { | |
995 int len = get_bits(&s->gb, 16); | |
996 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) { | |
997 char *cbuf = av_malloc(len - 1); | |
998 if (cbuf) { | |
999 int i; | |
1000 for (i = 0; i < len - 2; i++) | |
1001 cbuf[i] = get_bits(&s->gb, 8); | |
1002 if (i > 0 && cbuf[i-1] == '\n') | |
1003 cbuf[i-1] = 0; | |
1004 else | |
1005 cbuf[i] = 0; | |
1006 | |
1007 if(s->avctx->debug & FF_DEBUG_PICT_INFO) | |
1008 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf); | |
1009 | |
1010 /* buggy avid, it puts EOI only at every 10th frame */ | |
1011 if (!strcmp(cbuf, "AVID")) | |
1012 { | |
1013 s->buggy_avid = 1; | |
1014 // if (s->first_picture) | |
1015 // printf("mjpeg: workarounding buggy AVID\n"); | |
1016 } | |
1017 else if(!strcmp(cbuf, "CS=ITU601")){ | |
1018 s->cs_itu601= 1; | |
1019 } | |
1020 | |
1021 av_free(cbuf); | |
1022 } | |
1023 } | |
1024 | |
1025 return 0; | |
1026 } | |
1027 | |
1028 #if 0 | |
1029 static int valid_marker_list[] = | |
1030 { | |
1031 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */ | |
1032 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1033 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1034 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1035 /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1036 /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1037 /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1038 /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1039 /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1040 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1041 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1042 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1043 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
1044 /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1045 /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1046 /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
1047 /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, | |
1048 } | |
1049 #endif | |
1050 | |
1051 /* return the 8 bit start code value and update the search | |
1052 state. Return -1 if no start code found */ | |
1053 static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end) | |
1054 { | |
1055 uint8_t *buf_ptr; | |
1056 unsigned int v, v2; | |
1057 int val; | |
1058 #ifdef DEBUG | |
1059 int skipped=0; | |
1060 #endif | |
1061 | |
1062 buf_ptr = *pbuf_ptr; | |
1063 while (buf_ptr < buf_end) { | |
1064 v = *buf_ptr++; | |
1065 v2 = *buf_ptr; | |
1066 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) { | |
1067 val = *buf_ptr++; | |
1068 goto found; | |
1069 } | |
1070 #ifdef DEBUG | |
1071 skipped++; | |
1072 #endif | |
1073 } | |
1074 val = -1; | |
1075 found: | |
1076 #ifdef DEBUG | |
1077 av_log(NULL, AV_LOG_VERBOSE, "find_marker skipped %d bytes\n", skipped); | |
1078 #endif | |
1079 *pbuf_ptr = buf_ptr; | |
1080 return val; | |
1081 } | |
1082 | |
5041 | 1083 int ff_mjpeg_decode_frame(AVCodecContext *avctx, |
5020 | 1084 void *data, int *data_size, |
1085 uint8_t *buf, int buf_size) | |
1086 { | |
1087 MJpegDecodeContext *s = avctx->priv_data; | |
1088 uint8_t *buf_end, *buf_ptr; | |
1089 int start_code; | |
1090 AVFrame *picture = data; | |
1091 | |
1092 buf_ptr = buf; | |
1093 buf_end = buf + buf_size; | |
1094 while (buf_ptr < buf_end) { | |
1095 /* find start next marker */ | |
1096 start_code = find_marker(&buf_ptr, buf_end); | |
1097 { | |
1098 /* EOF */ | |
1099 if (start_code < 0) { | |
1100 goto the_end; | |
1101 } else { | |
5153 | 1102 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr); |
5020 | 1103 |
1104 if ((buf_end - buf_ptr) > s->buffer_size) | |
1105 { | |
1106 av_free(s->buffer); | |
1107 s->buffer_size = buf_end-buf_ptr; | |
1108 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE); | |
1109 av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n", | |
1110 s->buffer_size); | |
1111 } | |
1112 | |
1113 /* unescape buffer of SOS, use special treatment for JPEG-LS */ | |
1114 if (start_code == SOS && !s->ls) | |
1115 { | |
1116 uint8_t *src = buf_ptr; | |
1117 uint8_t *dst = s->buffer; | |
1118 | |
1119 while (src<buf_end) | |
1120 { | |
1121 uint8_t x = *(src++); | |
1122 | |
1123 *(dst++) = x; | |
1124 if (avctx->codec_id != CODEC_ID_THP) | |
1125 { | |
1126 if (x == 0xff) { | |
1127 while (src < buf_end && x == 0xff) | |
1128 x = *(src++); | |
1129 | |
1130 if (x >= 0xd0 && x <= 0xd7) | |
1131 *(dst++) = x; | |
1132 else if (x) | |
1133 break; | |
1134 } | |
1135 } | |
1136 } | |
1137 init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8); | |
1138 | |
5153 | 1139 av_log(avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n", |
5020 | 1140 (buf_end - buf_ptr) - (dst - s->buffer)); |
1141 } | |
1142 else if(start_code == SOS && s->ls){ | |
1143 uint8_t *src = buf_ptr; | |
1144 uint8_t *dst = s->buffer; | |
1145 int bit_count = 0; | |
1146 int t = 0, b = 0; | |
1147 PutBitContext pb; | |
1148 | |
1149 s->cur_scan++; | |
1150 | |
1151 /* find marker */ | |
1152 while (src + t < buf_end){ | |
1153 uint8_t x = src[t++]; | |
1154 if (x == 0xff){ | |
1155 while((src + t < buf_end) && x == 0xff) | |
1156 x = src[t++]; | |
1157 if (x & 0x80) { | |
1158 t -= 2; | |
1159 break; | |
1160 } | |
1161 } | |
1162 } | |
1163 bit_count = t * 8; | |
1164 | |
1165 init_put_bits(&pb, dst, t); | |
1166 | |
1167 /* unescape bitstream */ | |
1168 while(b < t){ | |
1169 uint8_t x = src[b++]; | |
1170 put_bits(&pb, 8, x); | |
1171 if(x == 0xFF){ | |
1172 x = src[b++]; | |
1173 put_bits(&pb, 7, x); | |
1174 bit_count--; | |
1175 } | |
1176 } | |
1177 flush_put_bits(&pb); | |
1178 | |
1179 init_get_bits(&s->gb, dst, bit_count); | |
1180 } | |
1181 else | |
1182 init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8); | |
1183 | |
1184 s->start_code = start_code; | |
1185 if(s->avctx->debug & FF_DEBUG_STARTCODE){ | |
1186 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code); | |
1187 } | |
1188 | |
1189 /* process markers */ | |
1190 if (start_code >= 0xd0 && start_code <= 0xd7) { | |
1191 av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f); | |
1192 /* APP fields */ | |
1193 } else if (start_code >= APP0 && start_code <= APP15) { | |
1194 mjpeg_decode_app(s); | |
1195 /* Comment */ | |
1196 } else if (start_code == COM){ | |
1197 mjpeg_decode_com(s); | |
1198 } | |
1199 | |
1200 switch(start_code) { | |
1201 case SOI: | |
1202 s->restart_interval = 0; | |
1203 | |
1204 s->restart_count = 0; | |
1205 /* nothing to do on SOI */ | |
1206 break; | |
1207 case DQT: | |
5044 | 1208 ff_mjpeg_decode_dqt(s); |
5020 | 1209 break; |
1210 case DHT: | |
5044 | 1211 if(ff_mjpeg_decode_dht(s) < 0){ |
5020 | 1212 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n"); |
1213 return -1; | |
1214 } | |
1215 break; | |
1216 case SOF0: | |
1217 s->lossless=0; | |
1218 s->ls=0; | |
1219 s->progressive=0; | |
5044 | 1220 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1221 return -1; |
1222 break; | |
1223 case SOF2: | |
1224 s->lossless=0; | |
1225 s->ls=0; | |
1226 s->progressive=1; | |
5044 | 1227 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1228 return -1; |
1229 break; | |
1230 case SOF3: | |
1231 s->lossless=1; | |
1232 s->ls=0; | |
1233 s->progressive=0; | |
5044 | 1234 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1235 return -1; |
1236 break; | |
1237 case SOF48: | |
1238 s->lossless=1; | |
1239 s->ls=1; | |
1240 s->progressive=0; | |
5044 | 1241 if (ff_mjpeg_decode_sof(s) < 0) |
5020 | 1242 return -1; |
1243 break; | |
1244 case LSE: | |
5041 | 1245 if (!ENABLE_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0) |
5020 | 1246 return -1; |
1247 break; | |
1248 case EOI: | |
1249 s->cur_scan = 0; | |
1250 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) | |
1251 break; | |
1252 eoi_parser: | |
1253 { | |
1254 if (s->interlaced) { | |
1255 s->bottom_field ^= 1; | |
1256 /* if not bottom field, do not output image yet */ | |
1257 if (s->bottom_field == !s->interlace_polarity) | |
1258 goto not_the_end; | |
1259 } | |
1260 *picture = s->picture; | |
1261 *data_size = sizeof(AVFrame); | |
1262 | |
1263 if(!s->lossless){ | |
1264 picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]); | |
1265 picture->qstride= 0; | |
1266 picture->qscale_table= s->qscale_table; | |
1267 memset(picture->qscale_table, picture->quality, (s->width+15)/16); | |
1268 if(avctx->debug & FF_DEBUG_QP) | |
1269 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality); | |
1270 picture->quality*= FF_QP2LAMBDA; | |
1271 } | |
1272 | |
1273 goto the_end; | |
1274 } | |
1275 break; | |
1276 case SOS: | |
5044 | 1277 ff_mjpeg_decode_sos(s); |
5020 | 1278 /* buggy avid puts EOI every 10-20th frame */ |
1279 /* if restart period is over process EOI */ | |
1280 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) | |
1281 goto eoi_parser; | |
1282 break; | |
1283 case DRI: | |
1284 mjpeg_decode_dri(s); | |
1285 break; | |
1286 case SOF1: | |
1287 case SOF5: | |
1288 case SOF6: | |
1289 case SOF7: | |
1290 case SOF9: | |
1291 case SOF10: | |
1292 case SOF11: | |
1293 case SOF13: | |
1294 case SOF14: | |
1295 case SOF15: | |
1296 case JPG: | |
1297 av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code); | |
1298 break; | |
1299 // default: | |
1300 // printf("mjpeg: unsupported marker (%x)\n", start_code); | |
1301 // break; | |
1302 } | |
1303 | |
1304 not_the_end: | |
1305 /* eof process start code */ | |
1306 buf_ptr += (get_bits_count(&s->gb)+7)/8; | |
1307 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n", | |
1308 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb)); | |
1309 } | |
1310 } | |
1311 } | |
1312 the_end: | |
5153 | 1313 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr); |
5020 | 1314 // return buf_end - buf_ptr; |
1315 return buf_ptr - buf; | |
1316 } | |
1317 | |
5041 | 1318 int ff_mjpeg_decode_end(AVCodecContext *avctx) |
5020 | 1319 { |
1320 MJpegDecodeContext *s = avctx->priv_data; | |
1321 int i, j; | |
1322 | |
1323 av_free(s->buffer); | |
1324 av_free(s->qscale_table); | |
1325 | |
1326 for(i=0;i<2;i++) { | |
1327 for(j=0;j<4;j++) | |
1328 free_vlc(&s->vlcs[i][j]); | |
1329 } | |
1330 return 0; | |
1331 } | |
1332 | |
1333 AVCodec mjpeg_decoder = { | |
1334 "mjpeg", | |
1335 CODEC_TYPE_VIDEO, | |
1336 CODEC_ID_MJPEG, | |
1337 sizeof(MJpegDecodeContext), | |
5041 | 1338 ff_mjpeg_decode_init, |
5020 | 1339 NULL, |
5041 | 1340 ff_mjpeg_decode_end, |
1341 ff_mjpeg_decode_frame, | |
5020 | 1342 CODEC_CAP_DR1, |
1343 NULL | |
1344 }; | |
1345 | |
1346 AVCodec thp_decoder = { | |
1347 "thp", | |
1348 CODEC_TYPE_VIDEO, | |
1349 CODEC_ID_THP, | |
1350 sizeof(MJpegDecodeContext), | |
5041 | 1351 ff_mjpeg_decode_init, |
5020 | 1352 NULL, |
5041 | 1353 ff_mjpeg_decode_end, |
1354 ff_mjpeg_decode_frame, | |
5020 | 1355 CODEC_CAP_DR1, |
1356 NULL | |
1357 }; |