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