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