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