04249dd97d0675cbef614213b890827aa1b5c645
[libav.git] / libavcodec / mjpegdec.c
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 * 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 *
11 * This file is part of Libav.
12 *
13 * Libav 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 * Libav 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 Libav; 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
30 * MJPEG decoder.
31 */
32
33 // #define DEBUG
34 #include <assert.h>
35
36 #include "libavutil/imgutils.h"
37 #include "libavutil/opt.h"
38 #include "avcodec.h"
39 #include "dsputil.h"
40 #include "internal.h"
41 #include "mjpeg.h"
42 #include "mjpegdec.h"
43 #include "jpeglsdec.h"
44
45
46 static int build_vlc(VLC *vlc, const uint8_t *bits_table,
47 const uint8_t *val_table, int nb_codes,
48 int use_static, int is_ac)
49 {
50 uint8_t huff_size[256] = { 0 };
51 uint16_t huff_code[256];
52 uint16_t huff_sym[256];
53 int i;
54
55 assert(nb_codes <= 256);
56
57 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
58
59 for (i = 0; i < 256; i++)
60 huff_sym[i] = i + 16 * is_ac;
61
62 if (is_ac)
63 huff_sym[0] = 16 * 256;
64
65 return ff_init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1,
66 huff_code, 2, 2, huff_sym, 2, 2, use_static);
67 }
68
69 static void build_basic_mjpeg_vlc(MJpegDecodeContext *s)
70 {
71 build_vlc(&s->vlcs[0][0], avpriv_mjpeg_bits_dc_luminance,
72 avpriv_mjpeg_val_dc, 12, 0, 0);
73 build_vlc(&s->vlcs[0][1], avpriv_mjpeg_bits_dc_chrominance,
74 avpriv_mjpeg_val_dc, 12, 0, 0);
75 build_vlc(&s->vlcs[1][0], avpriv_mjpeg_bits_ac_luminance,
76 avpriv_mjpeg_val_ac_luminance, 251, 0, 1);
77 build_vlc(&s->vlcs[1][1], avpriv_mjpeg_bits_ac_chrominance,
78 avpriv_mjpeg_val_ac_chrominance, 251, 0, 1);
79 build_vlc(&s->vlcs[2][0], avpriv_mjpeg_bits_ac_luminance,
80 avpriv_mjpeg_val_ac_luminance, 251, 0, 0);
81 build_vlc(&s->vlcs[2][1], avpriv_mjpeg_bits_ac_chrominance,
82 avpriv_mjpeg_val_ac_chrominance, 251, 0, 0);
83 }
84
85 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
86 {
87 MJpegDecodeContext *s = avctx->priv_data;
88
89 if (!s->picture_ptr)
90 s->picture_ptr = &s->picture;
91
92 s->avctx = avctx;
93 ff_dsputil_init(&s->dsp, avctx);
94 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
95 s->buffer_size = 0;
96 s->buffer = NULL;
97 s->start_code = -1;
98 s->first_picture = 1;
99 s->org_height = avctx->coded_height;
100 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
101
102 build_basic_mjpeg_vlc(s);
103
104 if (s->extern_huff) {
105 int ret;
106 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
107 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size * 8);
108 if ((ret = ff_mjpeg_decode_dht(s))) {
109 av_log(avctx, AV_LOG_ERROR,
110 "mjpeg: error using external huffman table\n");
111 return ret;
112 }
113 }
114 if (avctx->field_order == AV_FIELD_BB) { /* quicktime icefloe 019 */
115 s->interlace_polarity = 1; /* bottom field first */
116 av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
117 }
118 if (avctx->codec->id == AV_CODEC_ID_AMV)
119 s->flipped = 1;
120
121 return 0;
122 }
123
124
125 /* quantize tables */
126 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
127 {
128 int len, index, i, j;
129
130 len = get_bits(&s->gb, 16) - 2;
131
132 while (len >= 65) {
133 /* only 8 bit precision handled */
134 if (get_bits(&s->gb, 4) != 0) {
135 av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
136 return -1;
137 }
138 index = get_bits(&s->gb, 4);
139 if (index >= 4)
140 return -1;
141 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
142 /* read quant table */
143 for (i = 0; i < 64; i++) {
144 j = s->scantable.permutated[i];
145 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
146 }
147
148 // XXX FIXME finetune, and perhaps add dc too
149 s->qscale[index] = FFMAX(s->quant_matrixes[index][s->scantable.permutated[1]],
150 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
151 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n",
152 index, s->qscale[index]);
153 len -= 65;
154 }
155 return 0;
156 }
157
158 /* decode huffman tables and build VLC decoders */
159 int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
160 {
161 int len, index, i, class, n, v, code_max;
162 uint8_t bits_table[17];
163 uint8_t val_table[256];
164 int ret = 0;
165
166 len = get_bits(&s->gb, 16) - 2;
167
168 while (len > 0) {
169 if (len < 17)
170 return AVERROR_INVALIDDATA;
171 class = get_bits(&s->gb, 4);
172 if (class >= 2)
173 return AVERROR_INVALIDDATA;
174 index = get_bits(&s->gb, 4);
175 if (index >= 4)
176 return AVERROR_INVALIDDATA;
177 n = 0;
178 for (i = 1; i <= 16; i++) {
179 bits_table[i] = get_bits(&s->gb, 8);
180 n += bits_table[i];
181 }
182 len -= 17;
183 if (len < n || n > 256)
184 return AVERROR_INVALIDDATA;
185
186 code_max = 0;
187 for (i = 0; i < n; i++) {
188 v = get_bits(&s->gb, 8);
189 if (v > code_max)
190 code_max = v;
191 val_table[i] = v;
192 }
193 len -= n;
194
195 /* build VLC and flush previous vlc if present */
196 ff_free_vlc(&s->vlcs[class][index]);
197 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
198 class, index, code_max + 1);
199 if ((ret = build_vlc(&s->vlcs[class][index], bits_table, val_table,
200 code_max + 1, 0, class > 0)) < 0)
201 return ret;
202
203 if (class > 0) {
204 ff_free_vlc(&s->vlcs[2][index]);
205 if ((ret = build_vlc(&s->vlcs[2][index], bits_table, val_table,
206 code_max + 1, 0, 0)) < 0)
207 return ret;
208 }
209 }
210 return 0;
211 }
212
213 int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
214 {
215 int len, nb_components, i, width, height, pix_fmt_id;
216
217 /* XXX: verify len field validity */
218 len = get_bits(&s->gb, 16);
219 s->bits = get_bits(&s->gb, 8);
220
221 if (s->pegasus_rct)
222 s->bits = 9;
223 if (s->bits == 9 && !s->pegasus_rct)
224 s->rct = 1; // FIXME ugly
225
226 if (s->bits != 8 && !s->lossless) {
227 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
228 return -1;
229 }
230
231 height = get_bits(&s->gb, 16);
232 width = get_bits(&s->gb, 16);
233
234 // HACK for odd_height.mov
235 if (s->interlaced && s->width == width && s->height == height + 1)
236 height= s->height;
237
238 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
239 if (av_image_check_size(width, height, 0, s->avctx))
240 return AVERROR_INVALIDDATA;
241
242 nb_components = get_bits(&s->gb, 8);
243 if (nb_components <= 0 ||
244 nb_components > MAX_COMPONENTS)
245 return -1;
246 if (s->ls && !(s->bits <= 8 || nb_components == 1)) {
247 av_log_missing_feature(s->avctx,
248 "For JPEG-LS anything except <= 8 bits/component"
249 " or 16-bit gray", 0);
250 return AVERROR_PATCHWELCOME;
251 }
252 s->nb_components = nb_components;
253 s->h_max = 1;
254 s->v_max = 1;
255 for (i = 0; i < nb_components; i++) {
256 /* component id */
257 s->component_id[i] = get_bits(&s->gb, 8) - 1;
258 s->h_count[i] = get_bits(&s->gb, 4);
259 s->v_count[i] = get_bits(&s->gb, 4);
260 /* compute hmax and vmax (only used in interleaved case) */
261 if (s->h_count[i] > s->h_max)
262 s->h_max = s->h_count[i];
263 if (s->v_count[i] > s->v_max)
264 s->v_max = s->v_count[i];
265 s->quant_index[i] = get_bits(&s->gb, 8);
266 if (s->quant_index[i] >= 4)
267 return AVERROR_INVALIDDATA;
268 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n",
269 i, s->h_count[i], s->v_count[i],
270 s->component_id[i], s->quant_index[i]);
271 }
272
273 if (s->ls && (s->h_max > 1 || s->v_max > 1)) {
274 av_log_missing_feature(s->avctx, "Subsampling in JPEG-LS", 0);
275 return AVERROR_PATCHWELCOME;
276 }
277
278 if (s->v_max == 1 && s->h_max == 1 && s->lossless == 1)
279 s->rgb = 1;
280
281 /* if different size, realloc/alloc picture */
282 /* XXX: also check h_count and v_count */
283 if (width != s->width || height != s->height) {
284 av_freep(&s->qscale_table);
285
286 s->width = width;
287 s->height = height;
288 s->interlaced = 0;
289
290 /* test interlaced mode */
291 if (s->first_picture &&
292 s->org_height != 0 &&
293 s->height < ((s->org_height * 3) / 4)) {
294 s->interlaced = 1;
295 s->bottom_field = s->interlace_polarity;
296 s->picture_ptr->interlaced_frame = 1;
297 s->picture_ptr->top_field_first = !s->interlace_polarity;
298 height *= 2;
299 }
300
301 avcodec_set_dimensions(s->avctx, width, height);
302
303 s->qscale_table = av_mallocz((s->width + 15) / 16);
304 s->first_picture = 0;
305 }
306
307 if (!(s->interlaced && (s->bottom_field == !s->interlace_polarity))) {
308 /* XXX: not complete test ! */
309 pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) |
310 (s->h_count[1] << 20) | (s->v_count[1] << 16) |
311 (s->h_count[2] << 12) | (s->v_count[2] << 8) |
312 (s->h_count[3] << 4) | s->v_count[3];
313 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
314 /* NOTE we do not allocate pictures large enough for the possible
315 * padding of h/v_count being 4 */
316 if (!(pix_fmt_id & 0xD0D0D0D0))
317 pix_fmt_id -= (pix_fmt_id & 0xF0F0F0F0) >> 1;
318 if (!(pix_fmt_id & 0x0D0D0D0D))
319 pix_fmt_id -= (pix_fmt_id & 0x0F0F0F0F) >> 1;
320
321 switch (pix_fmt_id) {
322 case 0x11111100:
323 if (s->rgb)
324 s->avctx->pix_fmt = AV_PIX_FMT_BGRA;
325 else
326 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV444P : AV_PIX_FMT_YUVJ444P;
327 assert(s->nb_components == 3);
328 break;
329 case 0x11000000:
330 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
331 break;
332 case 0x12111100:
333 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV440P : AV_PIX_FMT_YUVJ440P;
334 break;
335 case 0x21111100:
336 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV422P : AV_PIX_FMT_YUVJ422P;
337 break;
338 case 0x22111100:
339 s->avctx->pix_fmt = s->cs_itu601 ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUVJ420P;
340 break;
341 default:
342 av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id);
343 return AVERROR_PATCHWELCOME;
344 }
345 if (s->ls) {
346 if (s->nb_components > 1)
347 s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
348 else if (s->bits <= 8)
349 s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
350 else
351 s->avctx->pix_fmt = AV_PIX_FMT_GRAY16;
352 }
353
354 if (s->picture_ptr->data[0])
355 s->avctx->release_buffer(s->avctx, s->picture_ptr);
356
357 if (ff_get_buffer(s->avctx, s->picture_ptr) < 0) {
358 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
359 return -1;
360 }
361 s->picture_ptr->pict_type = AV_PICTURE_TYPE_I;
362 s->picture_ptr->key_frame = 1;
363 s->got_picture = 1;
364
365 for (i = 0; i < 3; i++)
366 s->linesize[i] = s->picture_ptr->linesize[i] << s->interlaced;
367
368 av_dlog(s->avctx, "%d %d %d %d %d %d\n",
369 s->width, s->height, s->linesize[0], s->linesize[1],
370 s->interlaced, s->avctx->height);
371
372 if (len != (8 + (3 * nb_components)))
373 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
374 }
375
376 /* totally blank picture as progressive JPEG will only add details to it */
377 if (s->progressive) {
378 int bw = (width + s->h_max * 8 - 1) / (s->h_max * 8);
379 int bh = (height + s->v_max * 8 - 1) / (s->v_max * 8);
380 for (i = 0; i < s->nb_components; i++) {
381 int size = bw * bh * s->h_count[i] * s->v_count[i];
382 av_freep(&s->blocks[i]);
383 av_freep(&s->last_nnz[i]);
384 s->blocks[i] = av_malloc(size * sizeof(**s->blocks));
385 s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz));
386 s->block_stride[i] = bw * s->h_count[i];
387 }
388 memset(s->coefs_finished, 0, sizeof(s->coefs_finished));
389 }
390 return 0;
391 }
392
393 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
394 {
395 int code;
396 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
397 if (code < 0) {
398 av_log(s->avctx, AV_LOG_WARNING,
399 "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n",
400 0, dc_index, &s->vlcs[0][dc_index]);
401 return 0xffff;
402 }
403
404 if (code)
405 return get_xbits(&s->gb, code);
406 else
407 return 0;
408 }
409
410 /* decode block and dequantize */
411 static int decode_block(MJpegDecodeContext *s, DCTELEM *block, int component,
412 int dc_index, int ac_index, int16_t *quant_matrix)
413 {
414 int code, i, j, level, val;
415
416 /* DC coef */
417 val = mjpeg_decode_dc(s, dc_index);
418 if (val == 0xffff) {
419 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
420 return AVERROR_INVALIDDATA;
421 }
422 val = val * quant_matrix[0] + s->last_dc[component];
423 s->last_dc[component] = val;
424 block[0] = val;
425 /* AC coefs */
426 i = 0;
427 {OPEN_READER(re, &s->gb);
428 do {
429 UPDATE_CACHE(re, &s->gb);
430 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2);
431
432 i += ((unsigned)code) >> 4;
433 code &= 0xf;
434 if (code) {
435 if (code > MIN_CACHE_BITS - 16)
436 UPDATE_CACHE(re, &s->gb);
437
438 {
439 int cache = GET_CACHE(re, &s->gb);
440 int sign = (~cache) >> 31;
441 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
442 }
443
444 LAST_SKIP_BITS(re, &s->gb, code);
445
446 if (i > 63) {
447 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
448 return AVERROR_INVALIDDATA;
449 }
450 j = s->scantable.permutated[i];
451 block[j] = level * quant_matrix[j];
452 }
453 } while (i < 63);
454 CLOSE_READER(re, &s->gb);}
455
456 return 0;
457 }
458
459 static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block,
460 int component, int dc_index,
461 int16_t *quant_matrix, int Al)
462 {
463 int val;
464 s->dsp.clear_block(block);
465 val = mjpeg_decode_dc(s, dc_index);
466 if (val == 0xffff) {
467 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
468 return AVERROR_INVALIDDATA;
469 }
470 val = (val * quant_matrix[0] << Al) + s->last_dc[component];
471 s->last_dc[component] = val;
472 block[0] = val;
473 return 0;
474 }
475
476 /* decode block and dequantize - progressive JPEG version */
477 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block,
478 uint8_t *last_nnz, int ac_index,
479 int16_t *quant_matrix,
480 int ss, int se, int Al, int *EOBRUN)
481 {
482 int code, i, j, level, val, run;
483
484 if (*EOBRUN) {
485 (*EOBRUN)--;
486 return 0;
487 }
488
489 {
490 OPEN_READER(re, &s->gb);
491 for (i = ss; ; i++) {
492 UPDATE_CACHE(re, &s->gb);
493 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
494
495 run = ((unsigned) code) >> 4;
496 code &= 0xF;
497 if (code) {
498 i += run;
499 if (code > MIN_CACHE_BITS - 16)
500 UPDATE_CACHE(re, &s->gb);
501
502 {
503 int cache = GET_CACHE(re, &s->gb);
504 int sign = (~cache) >> 31;
505 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
506 }
507
508 LAST_SKIP_BITS(re, &s->gb, code);
509
510 if (i >= se) {
511 if (i == se) {
512 j = s->scantable.permutated[se];
513 block[j] = level * quant_matrix[j] << Al;
514 break;
515 }
516 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
517 return AVERROR_INVALIDDATA;
518 }
519 j = s->scantable.permutated[i];
520 block[j] = level * quant_matrix[j] << Al;
521 } else {
522 if (run == 0xF) {// ZRL - skip 15 coefficients
523 i += 15;
524 if (i >= se) {
525 av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i);
526 return AVERROR_INVALIDDATA;
527 }
528 } else {
529 val = (1 << run);
530 if (run) {
531 UPDATE_CACHE(re, &s->gb);
532 val += NEG_USR32(GET_CACHE(re, &s->gb), run);
533 LAST_SKIP_BITS(re, &s->gb, run);
534 }
535 *EOBRUN = val - 1;
536 break;
537 }
538 }
539 }
540 CLOSE_READER(re, &s->gb);
541 }
542
543 if (i > *last_nnz)
544 *last_nnz = i;
545
546 return 0;
547 }
548
549 #define REFINE_BIT(j) { \
550 UPDATE_CACHE(re, &s->gb); \
551 sign = block[j] >> 15; \
552 block[j] += SHOW_UBITS(re, &s->gb, 1) * \
553 ((quant_matrix[j] ^ sign) - sign) << Al; \
554 LAST_SKIP_BITS(re, &s->gb, 1); \
555 }
556
557 #define ZERO_RUN \
558 for (; ; i++) { \
559 if (i > last) { \
560 i += run; \
561 if (i > se) { \
562 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); \
563 return -1; \
564 } \
565 break; \
566 } \
567 j = s->scantable.permutated[i]; \
568 if (block[j]) \
569 REFINE_BIT(j) \
570 else if (run-- == 0) \
571 break; \
572 }
573
574 /* decode block and dequantize - progressive JPEG refinement pass */
575 static int decode_block_refinement(MJpegDecodeContext *s, DCTELEM *block,
576 uint8_t *last_nnz,
577 int ac_index, int16_t *quant_matrix,
578 int ss, int se, int Al, int *EOBRUN)
579 {
580 int code, i = ss, j, sign, val, run;
581 int last = FFMIN(se, *last_nnz);
582
583 OPEN_READER(re, &s->gb);
584 if (*EOBRUN) {
585 (*EOBRUN)--;
586 } else {
587 for (; ; i++) {
588 UPDATE_CACHE(re, &s->gb);
589 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2);
590
591 if (code & 0xF) {
592 run = ((unsigned) code) >> 4;
593 UPDATE_CACHE(re, &s->gb);
594 val = SHOW_UBITS(re, &s->gb, 1);
595 LAST_SKIP_BITS(re, &s->gb, 1);
596 ZERO_RUN;
597 j = s->scantable.permutated[i];
598 val--;
599 block[j] = ((quant_matrix[j]^val) - val) << Al;
600 if (i == se) {
601 if (i > *last_nnz)
602 *last_nnz = i;
603 CLOSE_READER(re, &s->gb);
604 return 0;
605 }
606 } else {
607 run = ((unsigned) code) >> 4;
608 if (run == 0xF) {
609 ZERO_RUN;
610 } else {
611 val = run;
612 run = (1 << run);
613 if (val) {
614 UPDATE_CACHE(re, &s->gb);
615 run += SHOW_UBITS(re, &s->gb, val);
616 LAST_SKIP_BITS(re, &s->gb, val);
617 }
618 *EOBRUN = run - 1;
619 break;
620 }
621 }
622 }
623
624 if (i > *last_nnz)
625 *last_nnz = i;
626 }
627
628 for (; i <= last; i++) {
629 j = s->scantable.permutated[i];
630 if (block[j])
631 REFINE_BIT(j)
632 }
633 CLOSE_READER(re, &s->gb);
634
635 return 0;
636 }
637 #undef REFINE_BIT
638 #undef ZERO_RUN
639
640 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor,
641 int point_transform)
642 {
643 int i, mb_x, mb_y;
644 uint16_t (*buffer)[4];
645 int left[3], top[3], topleft[3];
646 const int linesize = s->linesize[0];
647 const int mask = (1 << s->bits) - 1;
648
649 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size,
650 (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0]));
651 buffer = s->ljpeg_buffer;
652
653 for (i = 0; i < 3; i++)
654 buffer[0][i] = 1 << (s->bits + point_transform - 1);
655
656 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
657 const int modified_predictor = mb_y ? predictor : 1;
658 uint8_t *ptr = s->picture_ptr->data[0] + (linesize * mb_y);
659
660 if (s->interlaced && s->bottom_field)
661 ptr += linesize >> 1;
662
663 for (i = 0; i < 3; i++)
664 top[i] = left[i] = topleft[i] = buffer[0][i];
665
666 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
667 if (s->restart_interval && !s->restart_count)
668 s->restart_count = s->restart_interval;
669
670 for (i = 0; i < 3; i++) {
671 int pred;
672
673 topleft[i] = top[i];
674 top[i] = buffer[mb_x][i];
675
676 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
677
678 left[i] = buffer[mb_x][i] =
679 mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
680 }
681
682 if (s->restart_interval && !--s->restart_count) {
683 align_get_bits(&s->gb);
684 skip_bits(&s->gb, 16); /* skip RSTn */
685 }
686 }
687
688 if (s->rct) {
689 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
690 ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200) >> 2);
691 ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1];
692 ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1];
693 }
694 } else if (s->pegasus_rct) {
695 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
696 ptr[4 * mb_x + 1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2]) >> 2);
697 ptr[4 * mb_x + 0] = buffer[mb_x][1] + ptr[4 * mb_x + 1];
698 ptr[4 * mb_x + 2] = buffer[mb_x][2] + ptr[4 * mb_x + 1];
699 }
700 } else {
701 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
702 ptr[4 * mb_x + 0] = buffer[mb_x][2];
703 ptr[4 * mb_x + 1] = buffer[mb_x][1];
704 ptr[4 * mb_x + 2] = buffer[mb_x][0];
705 }
706 }
707 }
708 return 0;
709 }
710
711 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor,
712 int point_transform)
713 {
714 int i, mb_x, mb_y;
715 const int nb_components = 3;
716
717 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
718 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
719 if (s->restart_interval && !s->restart_count)
720 s->restart_count = s->restart_interval;
721
722 if (mb_x == 0 || mb_y == 0 || s->interlaced) {
723 for (i = 0; i < nb_components; i++) {
724 uint8_t *ptr;
725 int n, h, v, x, y, c, j, linesize;
726 n = s->nb_blocks[i];
727 c = s->comp_index[i];
728 h = s->h_scount[i];
729 v = s->v_scount[i];
730 x = 0;
731 y = 0;
732 linesize = s->linesize[c];
733
734 for (j = 0; j < n; j++) {
735 int pred;
736 // FIXME optimize this crap
737 ptr = s->picture_ptr->data[c] +
738 (linesize * (v * mb_y + y)) +
739 (h * mb_x + x);
740 if (y == 0 && mb_y == 0) {
741 if (x == 0 && mb_x == 0)
742 pred = 128 << point_transform;
743 else
744 pred = ptr[-1];
745 } else {
746 if (x == 0 && mb_x == 0)
747 pred = ptr[-linesize];
748 else
749 PREDICT(pred, ptr[-linesize - 1],
750 ptr[-linesize], ptr[-1], predictor);
751 }
752
753 if (s->interlaced && s->bottom_field)
754 ptr += linesize >> 1;
755 *ptr = pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
756
757 if (++x == h) {
758 x = 0;
759 y++;
760 }
761 }
762 }
763 } else {
764 for (i = 0; i < nb_components; i++) {
765 uint8_t *ptr;
766 int n, h, v, x, y, c, j, linesize;
767 n = s->nb_blocks[i];
768 c = s->comp_index[i];
769 h = s->h_scount[i];
770 v = s->v_scount[i];
771 x = 0;
772 y = 0;
773 linesize = s->linesize[c];
774
775 for (j = 0; j < n; j++) {
776 int pred;
777
778 // FIXME optimize this crap
779 ptr = s->picture_ptr->data[c] +
780 (linesize * (v * mb_y + y)) +
781 (h * mb_x + x);
782 PREDICT(pred, ptr[-linesize - 1],
783 ptr[-linesize], ptr[-1], predictor);
784 *ptr = pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
785 if (++x == h) {
786 x = 0;
787 y++;
788 }
789 }
790 }
791 }
792 if (s->restart_interval && !--s->restart_count) {
793 align_get_bits(&s->gb);
794 skip_bits(&s->gb, 16); /* skip RSTn */
795 }
796 }
797 }
798 return 0;
799 }
800
801 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah,
802 int Al, const uint8_t *mb_bitmask,
803 const AVFrame *reference)
804 {
805 int i, mb_x, mb_y;
806 uint8_t *data[MAX_COMPONENTS];
807 const uint8_t *reference_data[MAX_COMPONENTS];
808 int linesize[MAX_COMPONENTS];
809 GetBitContext mb_bitmask_gb;
810
811 if (mb_bitmask)
812 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
813
814 if (s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) {
815 av_log(s->avctx, AV_LOG_ERROR,
816 "Can not flip image with CODEC_FLAG_EMU_EDGE set!\n");
817 s->flipped = 0;
818 }
819
820 for (i = 0; i < nb_components; i++) {
821 int c = s->comp_index[i];
822 data[c] = s->picture_ptr->data[c];
823 reference_data[c] = reference ? reference->data[c] : NULL;
824 linesize[c] = s->linesize[c];
825 s->coefs_finished[c] |= 1;
826 if (s->flipped) {
827 // picture should be flipped upside-down for this codec
828 int offset = (linesize[c] * (s->v_scount[i] *
829 (8 * s->mb_height - ((s->height / s->v_max) & 7)) - 1));
830 data[c] += offset;
831 reference_data[c] += offset;
832 linesize[c] *= -1;
833 }
834 }
835
836 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
837 for (mb_x = 0; mb_x < s->mb_width; mb_x++) {
838 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
839
840 if (s->restart_interval && !s->restart_count)
841 s->restart_count = s->restart_interval;
842
843 if (get_bits_left(&s->gb) < 0) {
844 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n",
845 -get_bits_left(&s->gb));
846 return AVERROR_INVALIDDATA;
847 }
848 for (i = 0; i < nb_components; i++) {
849 uint8_t *ptr;
850 int n, h, v, x, y, c, j;
851 int block_offset;
852 n = s->nb_blocks[i];
853 c = s->comp_index[i];
854 h = s->h_scount[i];
855 v = s->v_scount[i];
856 x = 0;
857 y = 0;
858 for (j = 0; j < n; j++) {
859 block_offset = ((linesize[c] * (v * mb_y + y) * 8) +
860 (h * mb_x + x) * 8);
861
862 if (s->interlaced && s->bottom_field)
863 block_offset += linesize[c] >> 1;
864 ptr = data[c] + block_offset;
865 if (!s->progressive) {
866 if (copy_mb)
867 copy_block8(ptr, reference_data[c] + block_offset,
868 linesize[c], linesize[c], 8);
869 else {
870 s->dsp.clear_block(s->block);
871 if (decode_block(s, s->block, i,
872 s->dc_index[i], s->ac_index[i],
873 s->quant_matrixes[s->quant_index[c]]) < 0) {
874 av_log(s->avctx, AV_LOG_ERROR,
875 "error y=%d x=%d\n", mb_y, mb_x);
876 return AVERROR_INVALIDDATA;
877 }
878 s->dsp.idct_put(ptr, linesize[c], s->block);
879 }
880 } else {
881 int block_idx = s->block_stride[c] * (v * mb_y + y) +
882 (h * mb_x + x);
883 DCTELEM *block = s->blocks[c][block_idx];
884 if (Ah)
885 block[0] += get_bits1(&s->gb) *
886 s->quant_matrixes[s->quant_index[c]][0] << Al;
887 else if (decode_dc_progressive(s, block, i, s->dc_index[i],
888 s->quant_matrixes[s->quant_index[c]],
889 Al) < 0) {
890 av_log(s->avctx, AV_LOG_ERROR,
891 "error y=%d x=%d\n", mb_y, mb_x);
892 return AVERROR_INVALIDDATA;
893 }
894 }
895 av_dlog(s->avctx, "mb: %d %d processed\n", mb_y, mb_x);
896 av_dlog(s->avctx, "%d %d %d %d %d %d %d %d \n",
897 mb_x, mb_y, x, y, c, s->bottom_field,
898 (v * mb_y + y) * 8, (h * mb_x + x) * 8);
899 if (++x == h) {
900 x = 0;
901 y++;
902 }
903 }
904 }
905
906 if (s->restart_interval) {
907 s->restart_count--;
908 i = 8 + ((-get_bits_count(&s->gb)) & 7);
909 /* skip RSTn */
910 if (show_bits(&s->gb, i) == (1 << i) - 1) {
911 int pos = get_bits_count(&s->gb);
912 align_get_bits(&s->gb);
913 while (get_bits_left(&s->gb) >= 8 && show_bits(&s->gb, 8) == 0xFF)
914 skip_bits(&s->gb, 8);
915 if ((get_bits(&s->gb, 8) & 0xF8) == 0xD0) {
916 for (i = 0; i < nb_components; i++) /* reset dc */
917 s->last_dc[i] = 1024;
918 } else
919 skip_bits_long(&s->gb, pos - get_bits_count(&s->gb));
920 }
921 }
922 }
923 }
924 return 0;
925 }
926
927 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss,
928 int se, int Ah, int Al,
929 const uint8_t *mb_bitmask,
930 const AVFrame *reference)
931 {
932 int mb_x, mb_y;
933 int EOBRUN = 0;
934 int c = s->comp_index[0];
935 uint8_t *data = s->picture_ptr->data[c];
936 const uint8_t *reference_data = reference ? reference->data[c] : NULL;
937 int linesize = s->linesize[c];
938 int last_scan = 0;
939 int16_t *quant_matrix = s->quant_matrixes[s->quant_index[c]];
940 GetBitContext mb_bitmask_gb;
941
942 if (mb_bitmask)
943 init_get_bits(&mb_bitmask_gb, mb_bitmask, s->mb_width * s->mb_height);
944
945 if (!Al) {
946 s->coefs_finished[c] |= (1LL << (se + 1)) - (1LL << ss);
947 last_scan = !~s->coefs_finished[c];
948 }
949
950 if (s->interlaced && s->bottom_field) {
951 int offset = linesize >> 1;
952 data += offset;
953 reference_data += offset;
954 }
955
956 for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
957 int block_offset = mb_y * linesize * 8;
958 uint8_t *ptr = data + block_offset;
959 int block_idx = mb_y * s->block_stride[c];
960 DCTELEM (*block)[64] = &s->blocks[c][block_idx];
961 uint8_t *last_nnz = &s->last_nnz[c][block_idx];
962 for (mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) {
963 const int copy_mb = mb_bitmask && !get_bits1(&mb_bitmask_gb);
964
965 if (!copy_mb) {
966 int ret;
967 if (Ah)
968 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0],
969 quant_matrix, ss, se, Al, &EOBRUN);
970 else
971 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0],
972 quant_matrix, ss, se, Al, &EOBRUN);
973 if (ret < 0) {
974 av_log(s->avctx, AV_LOG_ERROR,
975 "error y=%d x=%d\n", mb_y, mb_x);
976 return AVERROR_INVALIDDATA;
977 }
978 }
979
980 if (last_scan) {
981 if (copy_mb) {
982 copy_block8(ptr, reference_data + block_offset,
983 linesize, linesize, 8);
984 } else {
985 s->dsp.idct_put(ptr, linesize, *block);
986 ptr += 8;
987 }
988 }
989 }
990 }
991 return 0;
992 }
993
994 int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask,
995 const AVFrame *reference)
996 {
997 int len, nb_components, i, h, v, predictor, point_transform;
998 int index, id, ret;
999 const int block_size = s->lossless ? 1 : 8;
1000 int ilv, prev_shift;
1001
1002 /* XXX: verify len field validity */
1003 len = get_bits(&s->gb, 16);
1004 nb_components = get_bits(&s->gb, 8);
1005 if (nb_components == 0 || nb_components > MAX_COMPONENTS) {
1006 av_log(s->avctx, AV_LOG_ERROR,
1007 "decode_sos: nb_components (%d) unsupported\n", nb_components);
1008 return AVERROR_PATCHWELCOME;
1009 }
1010 if (len != 6 + 2 * nb_components) {
1011 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
1012 return AVERROR_INVALIDDATA;
1013 }
1014 for (i = 0; i < nb_components; i++) {
1015 id = get_bits(&s->gb, 8) - 1;
1016 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
1017 /* find component index */
1018 for (index = 0; index < s->nb_components; index++)
1019 if (id == s->component_id[index])
1020 break;
1021 if (index == s->nb_components) {
1022 av_log(s->avctx, AV_LOG_ERROR,
1023 "decode_sos: index(%d) out of components\n", index);
1024 return AVERROR_INVALIDDATA;
1025 }
1026 /* Metasoft MJPEG codec has Cb and Cr swapped */
1027 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J')
1028 && nb_components == 3 && s->nb_components == 3 && i)
1029 index = 3 - i;
1030
1031 s->comp_index[i] = index;
1032
1033 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
1034 s->h_scount[i] = s->h_count[index];
1035 s->v_scount[i] = s->v_count[index];
1036
1037 s->dc_index[i] = get_bits(&s->gb, 4);
1038 s->ac_index[i] = get_bits(&s->gb, 4);
1039
1040 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
1041 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
1042 goto out_of_range;
1043 if (!s->vlcs[0][s->dc_index[i]].table ||
1044 !s->vlcs[1][s->ac_index[i]].table)
1045 goto out_of_range;
1046 }
1047
1048 predictor = get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
1049 ilv = get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
1050 prev_shift = get_bits(&s->gb, 4); /* Ah */
1051 point_transform = get_bits(&s->gb, 4); /* Al */
1052
1053 if (nb_components > 1) {
1054 /* interleaved stream */
1055 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
1056 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
1057 } else if (!s->ls) { /* skip this for JPEG-LS */
1058 h = s->h_max / s->h_scount[0];
1059 v = s->v_max / s->v_scount[0];
1060 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
1061 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
1062 s->nb_blocks[0] = 1;
1063 s->h_scount[0] = 1;
1064 s->v_scount[0] = 1;
1065 }
1066
1067 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1068 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n",
1069 s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "",
1070 predictor, point_transform, ilv, s->bits,
1071 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
1072
1073
1074 /* mjpeg-b can have padding bytes between sos and image data, skip them */
1075 for (i = s->mjpb_skiptosod; i > 0; i--)
1076 skip_bits(&s->gb, 8);
1077
1078 next_field:
1079 for (i = 0; i < nb_components; i++)
1080 s->last_dc[i] = 1024;
1081
1082 if (s->lossless) {
1083 if (CONFIG_JPEGLS_DECODER && s->ls) {
1084 // for () {
1085 // reset_ls_coding_parameters(s, 0);
1086
1087 if ((ret = ff_jpegls_decode_picture(s, predictor,
1088 point_transform, ilv)) < 0)
1089 return ret;
1090 } else {
1091 if (s->rgb) {
1092 if ((ret = ljpeg_decode_rgb_scan(s, predictor,
1093 point_transform)) < 0)
1094 return ret;
1095 } else {
1096 if ((ret = ljpeg_decode_yuv_scan(s, predictor,
1097 point_transform)) < 0)
1098 return ret;
1099 }
1100 }
1101 } else {
1102 if (s->progressive && predictor) {
1103 if ((ret = mjpeg_decode_scan_progressive_ac(s, predictor,
1104 ilv, prev_shift,
1105 point_transform,
1106 mb_bitmask,
1107 reference)) < 0)
1108 return ret;
1109 } else {
1110 if ((ret = mjpeg_decode_scan(s, nb_components,
1111 prev_shift, point_transform,
1112 mb_bitmask, reference)) < 0)
1113 return ret;
1114 }
1115 }
1116
1117 if (s->interlaced &&
1118 get_bits_left(&s->gb) > 32 &&
1119 show_bits(&s->gb, 8) == 0xFF) {
1120 GetBitContext bak = s->gb;
1121 align_get_bits(&bak);
1122 if (show_bits(&bak, 16) == 0xFFD1) {
1123 av_dlog(s->avctx, "AVRn interlaced picture marker found\n");
1124 s->gb = bak;
1125 skip_bits(&s->gb, 16);
1126 s->bottom_field ^= 1;
1127
1128 goto next_field;
1129 }
1130 }
1131
1132 emms_c();
1133 return 0;
1134 out_of_range:
1135 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
1136 return AVERROR_INVALIDDATA;
1137 }
1138
1139 static int mjpeg_decode_dri(MJpegDecodeContext *s)
1140 {
1141 if (get_bits(&s->gb, 16) != 4)
1142 return AVERROR_INVALIDDATA;
1143 s->restart_interval = get_bits(&s->gb, 16);
1144 s->restart_count = 0;
1145 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n",
1146 s->restart_interval);
1147
1148 return 0;
1149 }
1150
1151 static int mjpeg_decode_app(MJpegDecodeContext *s)
1152 {
1153 int len, id, i;
1154
1155 len = get_bits(&s->gb, 16);
1156 if (len < 5)
1157 return AVERROR_INVALIDDATA;
1158 if (8 * len > get_bits_left(&s->gb))
1159 return AVERROR_INVALIDDATA;
1160
1161 id = get_bits_long(&s->gb, 32);
1162 id = av_be2ne32(id);
1163 len -= 6;
1164
1165 if (s->avctx->debug & FF_DEBUG_STARTCODE)
1166 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
1167
1168 /* Buggy AVID, it puts EOI only at every 10th frame. */
1169 /* Also, this fourcc is used by non-avid files too, it holds some
1170 information, but it's always present in AVID-created files. */
1171 if (id == AV_RL32("AVI1")) {
1172 /* structure:
1173 4bytes AVI1
1174 1bytes polarity
1175 1bytes always zero
1176 4bytes field_size
1177 4bytes field_size_less_padding
1178 */
1179 s->buggy_avid = 1;
1180 i = get_bits(&s->gb, 8);
1181 if (i == 2)
1182 s->bottom_field = 1;
1183 else if (i == 1)
1184 s->bottom_field = 0;
1185 #if 0
1186 skip_bits(&s->gb, 8);
1187 skip_bits(&s->gb, 32);
1188 skip_bits(&s->gb, 32);
1189 len -= 10;
1190 #endif
1191 goto out;
1192 }
1193
1194 // len -= 2;
1195
1196 if (id == AV_RL32("JFIF")) {
1197 int t_w, t_h, v1, v2;
1198 skip_bits(&s->gb, 8); /* the trailing zero-byte */
1199 v1 = get_bits(&s->gb, 8);
1200 v2 = get_bits(&s->gb, 8);
1201 skip_bits(&s->gb, 8);
1202
1203 s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 16);
1204 s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 16);
1205
1206 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1207 av_log(s->avctx, AV_LOG_INFO,
1208 "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
1209 v1, v2,
1210 s->avctx->sample_aspect_ratio.num,
1211 s->avctx->sample_aspect_ratio.den);
1212
1213 t_w = get_bits(&s->gb, 8);
1214 t_h = get_bits(&s->gb, 8);
1215 if (t_w && t_h) {
1216 /* skip thumbnail */
1217 if (len -10 - (t_w * t_h * 3) > 0)
1218 len -= t_w * t_h * 3;
1219 }
1220 len -= 10;
1221 goto out;
1222 }
1223
1224 if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e')) {
1225 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1226 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
1227 skip_bits(&s->gb, 16); /* version */
1228 skip_bits(&s->gb, 16); /* flags0 */
1229 skip_bits(&s->gb, 16); /* flags1 */
1230 skip_bits(&s->gb, 8); /* transform */
1231 len -= 7;
1232 goto out;
1233 }
1234
1235 if (id == AV_RL32("LJIF")) {
1236 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1237 av_log(s->avctx, AV_LOG_INFO,
1238 "Pegasus lossless jpeg header found\n");
1239 skip_bits(&s->gb, 16); /* version ? */
1240 skip_bits(&s->gb, 16); /* unknwon always 0? */
1241 skip_bits(&s->gb, 16); /* unknwon always 0? */
1242 skip_bits(&s->gb, 16); /* unknwon always 0? */
1243 switch (get_bits(&s->gb, 8)) {
1244 case 1:
1245 s->rgb = 1;
1246 s->pegasus_rct = 0;
1247 break;
1248 case 2:
1249 s->rgb = 1;
1250 s->pegasus_rct = 1;
1251 break;
1252 default:
1253 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
1254 }
1255 len -= 9;
1256 goto out;
1257 }
1258
1259 /* Apple MJPEG-A */
1260 if ((s->start_code == APP1) && (len > (0x28 - 8))) {
1261 id = get_bits_long(&s->gb, 32);
1262 id = av_be2ne32(id);
1263 len -= 4;
1264 /* Apple MJPEG-A */
1265 if (id == AV_RL32("mjpg")) {
1266 #if 0
1267 skip_bits(&s->gb, 32); /* field size */
1268 skip_bits(&s->gb, 32); /* pad field size */
1269 skip_bits(&s->gb, 32); /* next off */
1270 skip_bits(&s->gb, 32); /* quant off */
1271 skip_bits(&s->gb, 32); /* huff off */
1272 skip_bits(&s->gb, 32); /* image off */
1273 skip_bits(&s->gb, 32); /* scan off */
1274 skip_bits(&s->gb, 32); /* data off */
1275 #endif
1276 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1277 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
1278 }
1279 }
1280
1281 out:
1282 /* slow but needed for extreme adobe jpegs */
1283 if (len < 0)
1284 av_log(s->avctx, AV_LOG_ERROR,
1285 "mjpeg: error, decode_app parser read over the end\n");
1286 while (--len > 0)
1287 skip_bits(&s->gb, 8);
1288
1289 return 0;
1290 }
1291
1292 static int mjpeg_decode_com(MJpegDecodeContext *s)
1293 {
1294 int len = get_bits(&s->gb, 16);
1295 if (len >= 2 && 8 * len - 16 <= get_bits_left(&s->gb)) {
1296 char *cbuf = av_malloc(len - 1);
1297 if (cbuf) {
1298 int i;
1299 for (i = 0; i < len - 2; i++)
1300 cbuf[i] = get_bits(&s->gb, 8);
1301 if (i > 0 && cbuf[i - 1] == '\n')
1302 cbuf[i - 1] = 0;
1303 else
1304 cbuf[i] = 0;
1305
1306 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1307 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
1308
1309 /* buggy avid, it puts EOI only at every 10th frame */
1310 if (!strcmp(cbuf, "AVID")) {
1311 s->buggy_avid = 1;
1312 } else if (!strcmp(cbuf, "CS=ITU601"))
1313 s->cs_itu601 = 1;
1314 else if ((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) ||
1315 (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20)))
1316 s->flipped = 1;
1317
1318 av_free(cbuf);
1319 }
1320 }
1321
1322 return 0;
1323 }
1324
1325 /* return the 8 bit start code value and update the search
1326 state. Return -1 if no start code found */
1327 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
1328 {
1329 const uint8_t *buf_ptr;
1330 unsigned int v, v2;
1331 int val;
1332 #ifdef DEBUG
1333 int skipped = 0;
1334 #endif
1335
1336 buf_ptr = *pbuf_ptr;
1337 while (buf_ptr < buf_end) {
1338 v = *buf_ptr++;
1339 v2 = *buf_ptr;
1340 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1341 val = *buf_ptr++;
1342 goto found;
1343 }
1344 #ifdef DEBUG
1345 skipped++;
1346 #endif
1347 }
1348 val = -1;
1349 found:
1350 av_dlog(NULL, "find_marker skipped %d bytes\n", skipped);
1351 *pbuf_ptr = buf_ptr;
1352 return val;
1353 }
1354
1355 int ff_mjpeg_find_marker(MJpegDecodeContext *s,
1356 const uint8_t **buf_ptr, const uint8_t *buf_end,
1357 const uint8_t **unescaped_buf_ptr,
1358 int *unescaped_buf_size)
1359 {
1360 int start_code;
1361 start_code = find_marker(buf_ptr, buf_end);
1362
1363 av_fast_padded_malloc(&s->buffer, &s->buffer_size, buf_end - *buf_ptr);
1364 if (!s->buffer)
1365 return AVERROR(ENOMEM);
1366
1367 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1368 if (start_code == SOS && !s->ls) {
1369 const uint8_t *src = *buf_ptr;
1370 uint8_t *dst = s->buffer;
1371
1372 while (src < buf_end) {
1373 uint8_t x = *(src++);
1374
1375 *(dst++) = x;
1376 if (s->avctx->codec_id != AV_CODEC_ID_THP) {
1377 if (x == 0xff) {
1378 while (src < buf_end && x == 0xff)
1379 x = *(src++);
1380
1381 if (x >= 0xd0 && x <= 0xd7)
1382 *(dst++) = x;
1383 else if (x)
1384 break;
1385 }
1386 }
1387 }
1388 *unescaped_buf_ptr = s->buffer;
1389 *unescaped_buf_size = dst - s->buffer;
1390
1391 av_log(s->avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n",
1392 (buf_end - *buf_ptr) - (dst - s->buffer));
1393 } else if (start_code == SOS && s->ls) {
1394 const uint8_t *src = *buf_ptr;
1395 uint8_t *dst = s->buffer;
1396 int bit_count = 0;
1397 int t = 0, b = 0;
1398 PutBitContext pb;
1399
1400 s->cur_scan++;
1401
1402 /* find marker */
1403 while (src + t < buf_end) {
1404 uint8_t x = src[t++];
1405 if (x == 0xff) {
1406 while ((src + t < buf_end) && x == 0xff)
1407 x = src[t++];
1408 if (x & 0x80) {
1409 t -= 2;
1410 break;
1411 }
1412 }
1413 }
1414 bit_count = t * 8;
1415 init_put_bits(&pb, dst, t);
1416
1417 /* unescape bitstream */
1418 while (b < t) {
1419 uint8_t x = src[b++];
1420 put_bits(&pb, 8, x);
1421 if (x == 0xFF) {
1422 x = src[b++];
1423 put_bits(&pb, 7, x);
1424 bit_count--;
1425 }
1426 }
1427 flush_put_bits(&pb);
1428
1429 *unescaped_buf_ptr = dst;
1430 *unescaped_buf_size = (bit_count + 7) >> 3;
1431 } else {
1432 *unescaped_buf_ptr = *buf_ptr;
1433 *unescaped_buf_size = buf_end - *buf_ptr;
1434 }
1435
1436 return start_code;
1437 }
1438
1439 int ff_mjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
1440 AVPacket *avpkt)
1441 {
1442 const uint8_t *buf = avpkt->data;
1443 int buf_size = avpkt->size;
1444 MJpegDecodeContext *s = avctx->priv_data;
1445 const uint8_t *buf_end, *buf_ptr;
1446 const uint8_t *unescaped_buf_ptr;
1447 int unescaped_buf_size;
1448 int start_code;
1449 int ret = 0;
1450 AVFrame *picture = data;
1451
1452 s->got_picture = 0; // picture from previous image can not be reused
1453 buf_ptr = buf;
1454 buf_end = buf + buf_size;
1455 while (buf_ptr < buf_end) {
1456 /* find start next marker */
1457 start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
1458 &unescaped_buf_ptr,
1459 &unescaped_buf_size);
1460 /* EOF */
1461 if (start_code < 0) {
1462 goto the_end;
1463 } else if (unescaped_buf_size > (1U<<29)) {
1464 av_log(avctx, AV_LOG_ERROR, "MJPEG packet 0x%x too big (0x%x/0x%x), corrupt data?\n",
1465 start_code, unescaped_buf_size, buf_size);
1466 return AVERROR_INVALIDDATA;
1467 } else {
1468 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n",
1469 start_code, buf_end - buf_ptr);
1470
1471 init_get_bits(&s->gb, unescaped_buf_ptr, unescaped_buf_size * 8);
1472
1473 s->start_code = start_code;
1474 if (s->avctx->debug & FF_DEBUG_STARTCODE)
1475 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1476
1477 /* process markers */
1478 if (start_code >= 0xd0 && start_code <= 0xd7)
1479 av_log(avctx, AV_LOG_DEBUG,
1480 "restart marker: %d\n", start_code & 0x0f);
1481 /* APP fields */
1482 else if (start_code >= APP0 && start_code <= APP15)
1483 mjpeg_decode_app(s);
1484 /* Comment */
1485 else if (start_code == COM)
1486 mjpeg_decode_com(s);
1487
1488 switch (start_code) {
1489 case SOI:
1490 s->restart_interval = 0;
1491 s->restart_count = 0;
1492 /* nothing to do on SOI */
1493 break;
1494 case DQT:
1495 ff_mjpeg_decode_dqt(s);
1496 break;
1497 case DHT:
1498 if ((ret = ff_mjpeg_decode_dht(s)) < 0) {
1499 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1500 return ret;
1501 }
1502 break;
1503 case SOF0:
1504 case SOF1:
1505 s->lossless = 0;
1506 s->ls = 0;
1507 s->progressive = 0;
1508 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1509 return ret;
1510 break;
1511 case SOF2:
1512 s->lossless = 0;
1513 s->ls = 0;
1514 s->progressive = 1;
1515 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1516 return ret;
1517 break;
1518 case SOF3:
1519 s->lossless = 1;
1520 s->ls = 0;
1521 s->progressive = 0;
1522 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1523 return ret;
1524 break;
1525 case SOF48:
1526 s->lossless = 1;
1527 s->ls = 1;
1528 s->progressive = 0;
1529 if ((ret = ff_mjpeg_decode_sof(s)) < 0)
1530 return ret;
1531 break;
1532 case LSE:
1533 if (!CONFIG_JPEGLS_DECODER ||
1534 (ret = ff_jpegls_decode_lse(s)) < 0)
1535 return ret;
1536 break;
1537 case EOI:
1538 s->cur_scan = 0;
1539 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1540 break;
1541 eoi_parser:
1542 if (!s->got_picture) {
1543 av_log(avctx, AV_LOG_WARNING,
1544 "Found EOI before any SOF, ignoring\n");
1545 break;
1546 }
1547 if (s->interlaced) {
1548 s->bottom_field ^= 1;
1549 /* if not bottom field, do not output image yet */
1550 if (s->bottom_field == !s->interlace_polarity)
1551 goto not_the_end;
1552 }
1553 *picture = *s->picture_ptr;
1554 *data_size = sizeof(AVFrame);
1555
1556 if (!s->lossless) {
1557 picture->quality = FFMAX3(s->qscale[0],
1558 s->qscale[1],
1559 s->qscale[2]);
1560 picture->qstride = 0;
1561 picture->qscale_table = s->qscale_table;
1562 memset(picture->qscale_table, picture->quality,
1563 (s->width + 15) / 16);
1564 if (avctx->debug & FF_DEBUG_QP)
1565 av_log(avctx, AV_LOG_DEBUG,
1566 "QP: %d\n", picture->quality);
1567 picture->quality *= FF_QP2LAMBDA;
1568 }
1569
1570 goto the_end;
1571 case SOS:
1572 if (!s->got_picture) {
1573 av_log(avctx, AV_LOG_WARNING,
1574 "Can not process SOS before SOF, skipping\n");
1575 break;
1576 }
1577 if ((ret = ff_mjpeg_decode_sos(s, NULL, NULL)) < 0 &&
1578 (avctx->err_recognition & AV_EF_EXPLODE))
1579 return ret;
1580 /* buggy avid puts EOI every 10-20th frame */
1581 /* if restart period is over process EOI */
1582 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1583 goto eoi_parser;
1584 break;
1585 case DRI:
1586 mjpeg_decode_dri(s);
1587 break;
1588 case SOF5:
1589 case SOF6:
1590 case SOF7:
1591 case SOF9:
1592 case SOF10:
1593 case SOF11:
1594 case SOF13:
1595 case SOF14:
1596 case SOF15:
1597 case JPG:
1598 av_log(avctx, AV_LOG_ERROR,
1599 "mjpeg: unsupported coding type (%x)\n", start_code);
1600 break;
1601 }
1602
1603 not_the_end:
1604 /* eof process start code */
1605 buf_ptr += (get_bits_count(&s->gb) + 7) / 8;
1606 av_log(avctx, AV_LOG_DEBUG,
1607 "marker parser used %d bytes (%d bits)\n",
1608 (get_bits_count(&s->gb) + 7) / 8, get_bits_count(&s->gb));
1609 }
1610 }
1611 if (s->got_picture) {
1612 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n");
1613 goto eoi_parser;
1614 }
1615 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n");
1616 return AVERROR_INVALIDDATA;
1617 the_end:
1618 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n",
1619 buf_end - buf_ptr);
1620 // return buf_end - buf_ptr;
1621 return buf_ptr - buf;
1622 }
1623
1624 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
1625 {
1626 MJpegDecodeContext *s = avctx->priv_data;
1627 int i, j;
1628
1629 if (s->picture_ptr && s->picture_ptr->data[0])
1630 avctx->release_buffer(avctx, s->picture_ptr);
1631
1632 av_free(s->buffer);
1633 av_free(s->qscale_table);
1634 av_freep(&s->ljpeg_buffer);
1635 s->ljpeg_buffer_size = 0;
1636
1637 for (i = 0; i < 3; i++) {
1638 for (j = 0; j < 4; j++)
1639 ff_free_vlc(&s->vlcs[i][j]);
1640 }
1641 for (i = 0; i < MAX_COMPONENTS; i++) {
1642 av_freep(&s->blocks[i]);
1643 av_freep(&s->last_nnz[i]);
1644 }
1645 return 0;
1646 }
1647
1648 #define OFFSET(x) offsetof(MJpegDecodeContext, x)
1649 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
1650 static const AVOption options[] = {
1651 { "extern_huff", "Use external huffman table.",
1652 OFFSET(extern_huff), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
1653 { NULL },
1654 };
1655
1656 static const AVClass mjpegdec_class = {
1657 .class_name = "MJPEG decoder",
1658 .item_name = av_default_item_name,
1659 .option = options,
1660 .version = LIBAVUTIL_VERSION_INT,
1661 };
1662
1663 AVCodec ff_mjpeg_decoder = {
1664 .name = "mjpeg",
1665 .type = AVMEDIA_TYPE_VIDEO,
1666 .id = AV_CODEC_ID_MJPEG,
1667 .priv_data_size = sizeof(MJpegDecodeContext),
1668 .init = ff_mjpeg_decode_init,
1669 .close = ff_mjpeg_decode_end,
1670 .decode = ff_mjpeg_decode_frame,
1671 .capabilities = CODEC_CAP_DR1,
1672 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
1673 .priv_class = &mjpegdec_class,
1674 };
1675
1676 AVCodec ff_thp_decoder = {
1677 .name = "thp",
1678 .type = AVMEDIA_TYPE_VIDEO,
1679 .id = AV_CODEC_ID_THP,
1680 .priv_data_size = sizeof(MJpegDecodeContext),
1681 .init = ff_mjpeg_decode_init,
1682 .close = ff_mjpeg_decode_end,
1683 .decode = ff_mjpeg_decode_frame,
1684 .capabilities = CODEC_CAP_DR1,
1685 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"),
1686 };