3 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
7 * This file is part of FFmpeg.
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.
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.
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
23 * Support for external huffman table, various fixes (AVID workaround),
24 * aspecting, new decode_frame mechanism and apple mjpeg-b support
39 #include "jpeglsdec.h"
42 static int mjpeg_decode_dht(MJpegDecodeContext *s);
44 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,
45 int nb_codes, int use_static, int is_ac)
47 uint8_t huff_size[256+16];
48 uint16_t huff_code[256+16];
50 assert(nb_codes <= 256);
52 memset(huff_size, 0, sizeof(huff_size));
53 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table);
56 memmove(huff_size+16, huff_size, sizeof(uint8_t)*nb_codes);
57 memmove(huff_code+16, huff_code, sizeof(uint16_t)*nb_codes);
58 memset(huff_size, 0, sizeof(uint8_t)*16);
59 memset(huff_code, 0, sizeof(uint16_t)*16);
63 return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static);
66 static int mjpeg_decode_init(AVCodecContext *avctx)
68 MJpegDecodeContext *s = avctx->priv_data;
71 dsputil_init(&s->dsp, avctx);
72 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct);
77 s->org_height = avctx->coded_height;
79 build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance,
80 ff_mjpeg_val_dc_luminance, 12, 0, 0);
81 build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance,
82 ff_mjpeg_val_dc_chrominance, 12, 0, 0);
83 build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance,
84 ff_mjpeg_val_ac_luminance, 251, 0, 1);
85 build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance,
86 ff_mjpeg_val_ac_chrominance, 251, 0, 1);
88 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF)
90 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n");
91 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
93 /* should check for error - but dunno */
95 if (avctx->extradata_size > 9 &&
96 AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) {
97 if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */
98 s->interlace_polarity = 1; /* bottom field first */
99 av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n");
107 /* quantize tables */
108 static int mjpeg_decode_dqt(MJpegDecodeContext *s)
110 int len, index, i, j;
112 len = get_bits(&s->gb, 16) - 2;
115 /* only 8 bit precision handled */
116 if (get_bits(&s->gb, 4) != 0)
118 av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n");
121 index = get_bits(&s->gb, 4);
124 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index);
125 /* read quant table */
127 j = s->scantable.permutated[i];
128 s->quant_matrixes[index][j] = get_bits(&s->gb, 8);
131 //XXX FIXME finetune, and perhaps add dc too
132 s->qscale[index]= FFMAX(
133 s->quant_matrixes[index][s->scantable.permutated[1]],
134 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1;
135 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]);
142 /* decode huffman tables and build VLC decoders */
143 static int mjpeg_decode_dht(MJpegDecodeContext *s)
145 int len, index, i, class, n, v, code_max;
146 uint8_t bits_table[17];
147 uint8_t val_table[256];
149 len = get_bits(&s->gb, 16) - 2;
154 class = get_bits(&s->gb, 4);
157 index = get_bits(&s->gb, 4);
162 bits_table[i] = get_bits(&s->gb, 8);
166 if (len < n || n > 256)
171 v = get_bits(&s->gb, 8);
178 /* build VLC and flush previous vlc if present */
179 free_vlc(&s->vlcs[class][index]);
180 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n",
181 class, index, code_max + 1);
182 if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){
189 static int mjpeg_decode_sof(MJpegDecodeContext *s)
191 int len, nb_components, i, width, height, pix_fmt_id;
193 /* XXX: verify len field validity */
194 len = get_bits(&s->gb, 16);
195 s->bits= get_bits(&s->gb, 8);
197 if(s->pegasus_rct) s->bits=9;
198 if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly
200 if (s->bits != 8 && !s->lossless){
201 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n");
205 height = get_bits(&s->gb, 16);
206 width = get_bits(&s->gb, 16);
208 //HACK for odd_height.mov
209 if(s->interlaced && s->width == width && s->height == height + 1)
212 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height);
213 if(avcodec_check_dimensions(s->avctx, width, height))
216 nb_components = get_bits(&s->gb, 8);
217 if (nb_components <= 0 ||
218 nb_components > MAX_COMPONENTS)
220 if (s->ls && !(s->bits <= 8 || nb_components == 1)){
221 av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n");
224 s->nb_components = nb_components;
227 for(i=0;i<nb_components;i++) {
229 s->component_id[i] = get_bits(&s->gb, 8) - 1;
230 s->h_count[i] = get_bits(&s->gb, 4);
231 s->v_count[i] = get_bits(&s->gb, 4);
232 /* compute hmax and vmax (only used in interleaved case) */
233 if (s->h_count[i] > s->h_max)
234 s->h_max = s->h_count[i];
235 if (s->v_count[i] > s->v_max)
236 s->v_max = s->v_count[i];
237 s->quant_index[i] = get_bits(&s->gb, 8);
238 if (s->quant_index[i] >= 4)
240 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i],
241 s->v_count[i], s->component_id[i], s->quant_index[i]);
244 if(s->ls && (s->h_max > 1 || s->v_max > 1)) {
245 av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n");
249 if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1;
251 /* if different size, realloc/alloc picture */
252 /* XXX: also check h_count and v_count */
253 if (width != s->width || height != s->height) {
254 av_freep(&s->qscale_table);
260 /* test interlaced mode */
261 if (s->first_picture &&
262 s->org_height != 0 &&
263 s->height < ((s->org_height * 3) / 4)) {
265 s->bottom_field = s->interlace_polarity;
266 s->picture.interlaced_frame = 1;
267 s->picture.top_field_first = !s->interlace_polarity;
271 avcodec_set_dimensions(s->avctx, width, height);
273 s->qscale_table= av_mallocz((s->width+15)/16);
275 s->first_picture = 0;
278 if(s->interlaced && (s->bottom_field == !s->interlace_polarity))
281 /* XXX: not complete test ! */
282 pix_fmt_id = (s->h_count[0] << 20) | (s->v_count[0] << 16) |
283 (s->h_count[1] << 12) | (s->v_count[1] << 8) |
284 (s->h_count[2] << 4) | s->v_count[2];
285 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id);
290 s->avctx->pix_fmt = PIX_FMT_RGB32;
291 }else if(s->nb_components==3)
292 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P;
294 s->avctx->pix_fmt = PIX_FMT_GRAY8;
298 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P;
302 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P;
306 if(s->nb_components > 1)
307 s->avctx->pix_fmt = PIX_FMT_RGB24;
308 else if(s->bits <= 8)
309 s->avctx->pix_fmt = PIX_FMT_GRAY8;
311 s->avctx->pix_fmt = PIX_FMT_GRAY16;
314 if(s->picture.data[0])
315 s->avctx->release_buffer(s->avctx, &s->picture);
317 s->picture.reference= 0;
318 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){
319 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
322 s->picture.pict_type= I_TYPE;
323 s->picture.key_frame= 1;
326 s->linesize[i]= s->picture.linesize[i] << s->interlaced;
329 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
331 if (len != (8+(3*nb_components)))
333 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len);
336 /* totally blank picture as progressive JPEG will only add details to it */
338 memset(s->picture.data[0], 0, s->picture.linesize[0] * s->height);
339 memset(s->picture.data[1], 0, s->picture.linesize[1] * s->height >> (s->v_max - s->v_count[1]));
340 memset(s->picture.data[2], 0, s->picture.linesize[2] * s->height >> (s->v_max - s->v_count[2]));
345 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index)
348 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);
351 av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index,
352 &s->vlcs[0][dc_index]);
357 return get_xbits(&s->gb, code);
362 /* decode block and dequantize */
363 static int decode_block(MJpegDecodeContext *s, DCTELEM *block,
364 int component, int dc_index, int ac_index, int16_t *quant_matrix)
366 int code, i, j, level, val;
369 val = mjpeg_decode_dc(s, dc_index);
371 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
374 val = val * quant_matrix[0] + s->last_dc[component];
375 s->last_dc[component] = val;
379 {OPEN_READER(re, &s->gb)
381 UPDATE_CACHE(re, &s->gb);
382 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
387 i += ((unsigned)code) >> 4;
390 if(code > MIN_CACHE_BITS - 16){
391 UPDATE_CACHE(re, &s->gb)
394 int cache=GET_CACHE(re,&s->gb);
395 int sign=(~cache)>>31;
396 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
399 LAST_SKIP_BITS(re, &s->gb, code)
403 j = s->scantable.permutated[63];
404 block[j] = level * quant_matrix[j];
407 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
410 j = s->scantable.permutated[i];
411 block[j] = level * quant_matrix[j];
414 CLOSE_READER(re, &s->gb)}
419 /* decode block and dequantize - progressive JPEG version */
420 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block,
421 int component, int dc_index, int ac_index, int16_t *quant_matrix,
422 int ss, int se, int Ah, int Al, int *EOBRUN)
424 int code, i, j, level, val, run;
428 val = mjpeg_decode_dc(s, dc_index);
430 av_log(s->avctx, AV_LOG_ERROR, "error dc\n");
433 val = (val * quant_matrix[0] << Al) + s->last_dc[component];
436 s->last_dc[component] = val;
444 {OPEN_READER(re, &s->gb)
446 UPDATE_CACHE(re, &s->gb);
447 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2)
448 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */
451 i += ((unsigned) code) >> 4;
453 if(code > MIN_CACHE_BITS - 16){
454 UPDATE_CACHE(re, &s->gb)
457 int cache=GET_CACHE(re,&s->gb);
458 int sign=(~cache)>>31;
459 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign;
462 LAST_SKIP_BITS(re, &s->gb, code)
466 j = s->scantable.permutated[se];
467 block[j] = level * quant_matrix[j] << Al;
470 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);
473 j = s->scantable.permutated[i];
474 block[j] = level * quant_matrix[j] << Al;
476 run = ((unsigned) code) >> 4;
477 if(run == 0xF){// ZRL - skip 15 coefficients
482 UPDATE_CACHE(re, &s->gb);
483 run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1);
485 LAST_SKIP_BITS(re, &s->gb, val);
491 CLOSE_READER(re, &s->gb)}
496 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){
498 uint16_t buffer[32768][4];
499 int left[3], top[3], topleft[3];
500 const int linesize= s->linesize[0];
501 const int mask= (1<<s->bits)-1;
503 if((unsigned)s->mb_width > 32768) //dynamic alloc
507 buffer[0][i]= 1 << (s->bits + point_transform - 1);
509 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
510 const int modified_predictor= mb_y ? predictor : 1;
511 uint8_t *ptr = s->picture.data[0] + (linesize * mb_y);
513 if (s->interlaced && s->bottom_field)
514 ptr += linesize >> 1;
517 top[i]= left[i]= topleft[i]= buffer[0][i];
519 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
520 if (s->restart_interval && !s->restart_count)
521 s->restart_count = s->restart_interval;
527 top[i]= buffer[mb_x][i];
529 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);
532 buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform));
535 if (s->restart_interval && !--s->restart_count) {
536 align_get_bits(&s->gb);
537 skip_bits(&s->gb, 16); /* skip RSTn */
542 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
543 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2);
544 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
545 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
547 }else if(s->pegasus_rct){
548 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
549 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2);
550 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1];
551 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1];
554 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
555 ptr[4*mb_x+0] = buffer[mb_x][0];
556 ptr[4*mb_x+1] = buffer[mb_x][1];
557 ptr[4*mb_x+2] = buffer[mb_x][2];
564 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){
566 const int nb_components=3;
568 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
569 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
570 if (s->restart_interval && !s->restart_count)
571 s->restart_count = s->restart_interval;
573 if(mb_x==0 || mb_y==0 || s->interlaced){
574 for(i=0;i<nb_components;i++) {
576 int n, h, v, x, y, c, j, linesize;
578 c = s->comp_index[i];
583 linesize= s->linesize[c];
588 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
591 pred= 128 << point_transform;
597 pred= ptr[-linesize];
599 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
603 if (s->interlaced && s->bottom_field)
604 ptr += linesize >> 1;
605 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
614 for(i=0;i<nb_components;i++) {
616 int n, h, v, x, y, c, j, linesize;
618 c = s->comp_index[i];
623 linesize= s->linesize[c];
628 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
629 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
630 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
638 if (s->restart_interval && !--s->restart_count) {
639 align_get_bits(&s->gb);
640 skip_bits(&s->gb, 16); /* skip RSTn */
647 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int ss, int se, int Ah, int Al){
651 if(Ah) return 0; /* TODO decode refinement planes too */
652 for(mb_y = 0; mb_y < s->mb_height; mb_y++) {
653 for(mb_x = 0; mb_x < s->mb_width; mb_x++) {
654 if (s->restart_interval && !s->restart_count)
655 s->restart_count = s->restart_interval;
657 for(i=0;i<nb_components;i++) {
659 int n, h, v, x, y, c, j;
661 c = s->comp_index[i];
667 memset(s->block, 0, sizeof(s->block));
668 if (!s->progressive && decode_block(s, s->block, i,
669 s->dc_index[i], s->ac_index[i],
670 s->quant_matrixes[ s->quant_index[c] ]) < 0) {
671 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
674 if (s->progressive && decode_block_progressive(s, s->block, i,
675 s->dc_index[i], s->ac_index[i],
676 s->quant_matrixes[ s->quant_index[c] ], ss, se, Ah, Al, &EOBRUN) < 0) {
677 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x);
680 // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x);
681 ptr = s->picture.data[c] +
682 (((s->linesize[c] * (v * mb_y + y) * 8) +
683 (h * mb_x + x) * 8) >> s->avctx->lowres);
684 if (s->interlaced && s->bottom_field)
685 ptr += s->linesize[c] >> 1;
686 //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);
688 s->dsp.idct_put(ptr, s->linesize[c], s->block);
690 s->dsp.idct_add(ptr, s->linesize[c], s->block);
697 /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */
698 if (s->restart_interval && (s->restart_interval < 1350) &&
699 !--s->restart_count) {
700 align_get_bits(&s->gb);
701 skip_bits(&s->gb, 16); /* skip RSTn */
702 for (i=0; i<nb_components; i++) /* reset dc */
703 s->last_dc[i] = 1024;
710 static int mjpeg_decode_sos(MJpegDecodeContext *s)
712 int len, nb_components, i, h, v, predictor, point_transform;
713 int vmax, hmax, index, id;
714 const int block_size= s->lossless ? 1 : 8;
717 /* XXX: verify len field validity */
718 len = get_bits(&s->gb, 16);
719 nb_components = get_bits(&s->gb, 8);
720 if (len != 6+2*nb_components)
722 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len);
727 for(i=0;i<nb_components;i++) {
728 id = get_bits(&s->gb, 8) - 1;
729 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id);
730 /* find component index */
731 for(index=0;index<s->nb_components;index++)
732 if (id == s->component_id[index])
734 if (index == s->nb_components)
736 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index);
740 s->comp_index[i] = index;
742 s->nb_blocks[i] = s->h_count[index] * s->v_count[index];
743 s->h_scount[i] = s->h_count[index];
744 s->v_scount[i] = s->v_count[index];
746 s->dc_index[i] = get_bits(&s->gb, 4);
747 s->ac_index[i] = get_bits(&s->gb, 4);
749 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 ||
750 s->dc_index[i] >= 4 || s->ac_index[i] >= 4)
753 switch(s->start_code)
756 if (dc_index[i] > 1 || ac_index[i] > 1)
761 if (dc_index[i] > 3 || ac_index[i] > 3)
765 if (dc_index[i] > 3 || ac_index[i] != 0)
772 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
773 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */
774 prev_shift = get_bits(&s->gb, 4); /* Ah */
775 point_transform= get_bits(&s->gb, 4); /* Al */
777 for(i=0;i<nb_components;i++)
778 s->last_dc[i] = 1024;
780 if (nb_components > 1) {
781 /* interleaved stream */
782 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size);
783 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size);
784 } else if(!s->ls) { /* skip this for JPEG-LS */
785 h = s->h_max / s->h_scount[0];
786 v = s->v_max / s->v_scount[0];
787 s->mb_width = (s->width + h * block_size - 1) / (h * block_size);
788 s->mb_height = (s->height + v * block_size - 1) / (v * block_size);
794 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
795 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" : "",
796 predictor, point_transform, ilv, s->bits,
797 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : ""));
800 /* mjpeg-b can have padding bytes between sos and image data, skip them */
801 for (i = s->mjpb_skiptosod; i > 0; i--)
802 skip_bits(&s->gb, 8);
807 // reset_ls_coding_parameters(s, 0);
809 ff_jpegls_decode_picture(s, predictor, point_transform, ilv);
812 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0)
815 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0)
820 if(mjpeg_decode_scan(s, nb_components, predictor, ilv, prev_shift, point_transform) < 0)
826 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n");
830 static int mjpeg_decode_dri(MJpegDecodeContext *s)
832 if (get_bits(&s->gb, 16) != 4)
834 s->restart_interval = get_bits(&s->gb, 16);
835 s->restart_count = 0;
836 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval);
841 static int mjpeg_decode_app(MJpegDecodeContext *s)
845 len = get_bits(&s->gb, 16);
848 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits)
851 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
855 if(s->avctx->debug & FF_DEBUG_STARTCODE){
856 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id);
859 /* buggy AVID, it puts EOI only at every 10th frame */
860 /* also this fourcc is used by non-avid files too, it holds some
861 informations, but it's always present in AVID creates files */
862 if (id == ff_get_fourcc("AVI1"))
869 4bytes field_size_less_padding
872 // if (s->first_picture)
873 // printf("mjpeg: workarounding buggy AVID\n");
874 i = get_bits(&s->gb, 8);
875 if (i==2) s->bottom_field= 1;
876 else if(i==1) s->bottom_field= 0;
878 skip_bits(&s->gb, 8);
879 skip_bits(&s->gb, 32);
880 skip_bits(&s->gb, 32);
883 // if (s->interlace_polarity)
884 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
890 if (id == ff_get_fourcc("JFIF"))
892 int t_w, t_h, v1, v2;
893 skip_bits(&s->gb, 8); /* the trailing zero-byte */
894 v1= get_bits(&s->gb, 8);
895 v2= get_bits(&s->gb, 8);
896 skip_bits(&s->gb, 8);
898 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16);
899 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16);
901 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
902 av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
904 s->avctx->sample_aspect_ratio.num,
905 s->avctx->sample_aspect_ratio.den
908 t_w = get_bits(&s->gb, 8);
909 t_h = get_bits(&s->gb, 8);
913 if (len-10-(t_w*t_h*3) > 0)
920 if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e'))
922 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
923 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n");
924 skip_bits(&s->gb, 16); /* version */
925 skip_bits(&s->gb, 16); /* flags0 */
926 skip_bits(&s->gb, 16); /* flags1 */
927 skip_bits(&s->gb, 8); /* transform */
932 if (id == ff_get_fourcc("LJIF")){
933 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
934 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n");
935 skip_bits(&s->gb, 16); /* version ? */
936 skip_bits(&s->gb, 16); /* unknwon always 0? */
937 skip_bits(&s->gb, 16); /* unknwon always 0? */
938 skip_bits(&s->gb, 16); /* unknwon always 0? */
939 switch( get_bits(&s->gb, 8)){
949 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n");
956 if ((s->start_code == APP1) && (len > (0x28 - 8)))
958 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16);
961 if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */
964 skip_bits(&s->gb, 32); /* field size */
965 skip_bits(&s->gb, 32); /* pad field size */
966 skip_bits(&s->gb, 32); /* next off */
967 skip_bits(&s->gb, 32); /* quant off */
968 skip_bits(&s->gb, 32); /* huff off */
969 skip_bits(&s->gb, 32); /* image off */
970 skip_bits(&s->gb, 32); /* scan off */
971 skip_bits(&s->gb, 32); /* data off */
973 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
974 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n");
979 /* slow but needed for extreme adobe jpegs */
981 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n");
983 skip_bits(&s->gb, 8);
988 static int mjpeg_decode_com(MJpegDecodeContext *s)
990 int len = get_bits(&s->gb, 16);
991 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) {
992 char *cbuf = av_malloc(len - 1);
995 for (i = 0; i < len - 2; i++)
996 cbuf[i] = get_bits(&s->gb, 8);
997 if (i > 0 && cbuf[i-1] == '\n')
1002 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1003 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf);
1005 /* buggy avid, it puts EOI only at every 10th frame */
1006 if (!strcmp(cbuf, "AVID"))
1009 // if (s->first_picture)
1010 // printf("mjpeg: workarounding buggy AVID\n");
1012 else if(!strcmp(cbuf, "CS=ITU601")){
1024 static int valid_marker_list[] =
1026 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
1027 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1028 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1029 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1030 /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1031 /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1032 /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1033 /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1034 /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1035 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1036 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1037 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1038 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1039 /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1040 /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1041 /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1042 /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1046 /* return the 8 bit start code value and update the search
1047 state. Return -1 if no start code found */
1048 static int find_marker(uint8_t **pbuf_ptr, uint8_t *buf_end)
1057 buf_ptr = *pbuf_ptr;
1058 while (buf_ptr < buf_end) {
1061 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) {
1072 av_log(NULL, AV_LOG_VERBOSE, "find_marker skipped %d bytes\n", skipped);
1074 *pbuf_ptr = buf_ptr;
1078 static int mjpeg_decode_frame(AVCodecContext *avctx,
1079 void *data, int *data_size,
1080 uint8_t *buf, int buf_size)
1082 MJpegDecodeContext *s = avctx->priv_data;
1083 uint8_t *buf_end, *buf_ptr;
1085 AVFrame *picture = data;
1088 buf_end = buf + buf_size;
1089 while (buf_ptr < buf_end) {
1090 /* find start next marker */
1091 start_code = find_marker(&buf_ptr, buf_end);
1094 if (start_code < 0) {
1097 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%d\n", start_code, buf_end - buf_ptr);
1099 if ((buf_end - buf_ptr) > s->buffer_size)
1102 s->buffer_size = buf_end-buf_ptr;
1103 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE);
1104 av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n",
1108 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1109 if (start_code == SOS && !s->ls)
1111 uint8_t *src = buf_ptr;
1112 uint8_t *dst = s->buffer;
1116 uint8_t x = *(src++);
1119 if (avctx->codec_id != CODEC_ID_THP)
1122 while (src < buf_end && x == 0xff)
1125 if (x >= 0xd0 && x <= 0xd7)
1132 init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8);
1134 av_log(avctx, AV_LOG_DEBUG, "escaping removed %d bytes\n",
1135 (buf_end - buf_ptr) - (dst - s->buffer));
1137 else if(start_code == SOS && s->ls){
1138 uint8_t *src = buf_ptr;
1139 uint8_t *dst = s->buffer;
1147 while (src + t < buf_end){
1148 uint8_t x = src[t++];
1150 while((src + t < buf_end) && x == 0xff)
1160 init_put_bits(&pb, dst, t);
1162 /* unescape bitstream */
1164 uint8_t x = src[b++];
1165 put_bits(&pb, 8, x);
1168 put_bits(&pb, 7, x);
1172 flush_put_bits(&pb);
1174 init_get_bits(&s->gb, dst, bit_count);
1177 init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8);
1179 s->start_code = start_code;
1180 if(s->avctx->debug & FF_DEBUG_STARTCODE){
1181 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code);
1184 /* process markers */
1185 if (start_code >= 0xd0 && start_code <= 0xd7) {
1186 av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f);
1188 } else if (start_code >= APP0 && start_code <= APP15) {
1189 mjpeg_decode_app(s);
1191 } else if (start_code == COM){
1192 mjpeg_decode_com(s);
1195 switch(start_code) {
1197 s->restart_interval = 0;
1199 s->restart_count = 0;
1200 /* nothing to do on SOI */
1203 mjpeg_decode_dqt(s);
1206 if(mjpeg_decode_dht(s) < 0){
1207 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n");
1215 if (mjpeg_decode_sof(s) < 0)
1222 if (mjpeg_decode_sof(s) < 0)
1229 if (mjpeg_decode_sof(s) < 0)
1236 if (mjpeg_decode_sof(s) < 0)
1240 if (ff_jpegls_decode_lse(s) < 0)
1245 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1249 if (s->interlaced) {
1250 s->bottom_field ^= 1;
1251 /* if not bottom field, do not output image yet */
1252 if (s->bottom_field == !s->interlace_polarity)
1255 *picture = s->picture;
1256 *data_size = sizeof(AVFrame);
1259 picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]);
1260 picture->qstride= 0;
1261 picture->qscale_table= s->qscale_table;
1262 memset(picture->qscale_table, picture->quality, (s->width+15)/16);
1263 if(avctx->debug & FF_DEBUG_QP)
1264 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
1265 picture->quality*= FF_QP2LAMBDA;
1272 mjpeg_decode_sos(s);
1273 /* buggy avid puts EOI every 10-20th frame */
1274 /* if restart period is over process EOI */
1275 if ((s->buggy_avid && !s->interlaced) || s->restart_interval)
1279 mjpeg_decode_dri(s);
1292 av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code);
1295 // printf("mjpeg: unsupported marker (%x)\n", start_code);
1300 /* eof process start code */
1301 buf_ptr += (get_bits_count(&s->gb)+7)/8;
1302 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n",
1303 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb));
1308 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %d bytes\n", buf_end - buf_ptr);
1309 // return buf_end - buf_ptr;
1310 return buf_ptr - buf;
1313 static int mjpegb_decode_frame(AVCodecContext *avctx,
1314 void *data, int *data_size,
1315 uint8_t *buf, int buf_size)
1317 MJpegDecodeContext *s = avctx->priv_data;
1318 uint8_t *buf_end, *buf_ptr;
1319 AVFrame *picture = data;
1320 GetBitContext hgb; /* for the header */
1321 uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
1322 uint32_t field_size, sod_offs;
1325 buf_end = buf + buf_size;
1328 /* reset on every SOI */
1329 s->restart_interval = 0;
1330 s->restart_count = 0;
1331 s->mjpb_skiptosod = 0;
1333 init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
1335 skip_bits(&hgb, 32); /* reserved zeros */
1337 if (get_bits_long(&hgb, 32) != MKBETAG('m','j','p','g'))
1339 av_log(avctx, AV_LOG_WARNING, "not mjpeg-b (bad fourcc)\n");
1343 field_size = get_bits_long(&hgb, 32); /* field size */
1344 av_log(avctx, AV_LOG_DEBUG, "field size: 0x%x\n", field_size);
1345 skip_bits(&hgb, 32); /* padded field size */
1346 second_field_offs = get_bits_long(&hgb, 32);
1347 av_log(avctx, AV_LOG_DEBUG, "second field offs: 0x%x\n", second_field_offs);
1349 dqt_offs = get_bits_long(&hgb, 32);
1350 av_log(avctx, AV_LOG_DEBUG, "dqt offs: 0x%x\n", dqt_offs);
1353 init_get_bits(&s->gb, buf+dqt_offs, (buf_end - (buf+dqt_offs))*8);
1354 s->start_code = DQT;
1355 mjpeg_decode_dqt(s);
1358 dht_offs = get_bits_long(&hgb, 32);
1359 av_log(avctx, AV_LOG_DEBUG, "dht offs: 0x%x\n", dht_offs);
1362 init_get_bits(&s->gb, buf+dht_offs, (buf_end - (buf+dht_offs))*8);
1363 s->start_code = DHT;
1364 mjpeg_decode_dht(s);
1367 sof_offs = get_bits_long(&hgb, 32);
1368 av_log(avctx, AV_LOG_DEBUG, "sof offs: 0x%x\n", sof_offs);
1371 init_get_bits(&s->gb, buf+sof_offs, (buf_end - (buf+sof_offs))*8);
1372 s->start_code = SOF0;
1373 if (mjpeg_decode_sof(s) < 0)
1377 sos_offs = get_bits_long(&hgb, 32);
1378 av_log(avctx, AV_LOG_DEBUG, "sos offs: 0x%x\n", sos_offs);
1379 sod_offs = get_bits_long(&hgb, 32);
1380 av_log(avctx, AV_LOG_DEBUG, "sod offs: 0x%x\n", sod_offs);
1383 // init_get_bits(&s->gb, buf+sos_offs, (buf_end - (buf+sos_offs))*8);
1384 init_get_bits(&s->gb, buf+sos_offs, field_size*8);
1385 s->mjpb_skiptosod = (sod_offs - sos_offs - show_bits(&s->gb, 16));
1386 s->start_code = SOS;
1387 mjpeg_decode_sos(s);
1390 if (s->interlaced) {
1391 s->bottom_field ^= 1;
1392 /* if not bottom field, do not output image yet */
1393 if (s->bottom_field && second_field_offs)
1395 buf_ptr = buf + second_field_offs;
1396 second_field_offs = 0;
1401 //XXX FIXME factorize, this looks very similar to the EOI code
1403 *picture= s->picture;
1404 *data_size = sizeof(AVFrame);
1407 picture->quality= FFMAX(FFMAX(s->qscale[0], s->qscale[1]), s->qscale[2]);
1408 picture->qstride= 0;
1409 picture->qscale_table= s->qscale_table;
1410 memset(picture->qscale_table, picture->quality, (s->width+15)/16);
1411 if(avctx->debug & FF_DEBUG_QP)
1412 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality);
1413 picture->quality*= FF_QP2LAMBDA;
1416 return buf_ptr - buf;
1421 static int sp5x_decode_frame(AVCodecContext *avctx,
1422 void *data, int *data_size,
1423 uint8_t *buf, int buf_size)
1426 MJpegDecodeContext *s = avctx->priv_data;
1428 const int qscale = 5;
1429 uint8_t *buf_ptr, *buf_end, *recoded;
1432 if (!avctx->width || !avctx->height)
1436 buf_end = buf + buf_size;
1439 recoded = av_mallocz(buf_size + 1024);
1444 recoded[j++] = 0xFF;
1445 recoded[j++] = 0xD8;
1447 memcpy(recoded+j, &sp5x_data_dqt[0], sizeof(sp5x_data_dqt));
1448 memcpy(recoded+j+5, &sp5x_quant_table[qscale * 2], 64);
1449 memcpy(recoded+j+70, &sp5x_quant_table[(qscale * 2) + 1], 64);
1450 j += sizeof(sp5x_data_dqt);
1452 memcpy(recoded+j, &sp5x_data_dht[0], sizeof(sp5x_data_dht));
1453 j += sizeof(sp5x_data_dht);
1455 memcpy(recoded+j, &sp5x_data_sof[0], sizeof(sp5x_data_sof));
1456 recoded[j+5] = (avctx->coded_height >> 8) & 0xFF;
1457 recoded[j+6] = avctx->coded_height & 0xFF;
1458 recoded[j+7] = (avctx->coded_width >> 8) & 0xFF;
1459 recoded[j+8] = avctx->coded_width & 0xFF;
1460 j += sizeof(sp5x_data_sof);
1462 memcpy(recoded+j, &sp5x_data_sos[0], sizeof(sp5x_data_sos));
1463 j += sizeof(sp5x_data_sos);
1465 for (i = 14; i < buf_size && j < buf_size+1024-2; i++)
1467 recoded[j++] = buf[i];
1473 recoded[j++] = 0xFF;
1474 recoded[j++] = 0xD9;
1476 i = mjpeg_decode_frame(avctx, data, data_size, recoded, j);
1483 s->width = avctx->coded_width;
1484 s->height = avctx->coded_height;
1485 s->nb_components = 3;
1486 s->component_id[0] = 0;
1489 s->quant_index[0] = 0;
1490 s->component_id[1] = 1;
1493 s->quant_index[1] = 1;
1494 s->component_id[2] = 2;
1497 s->quant_index[2] = 1;
1501 s->qscale_table = av_mallocz((s->width+15)/16);
1502 avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420;
1505 s->picture.reference = 0;
1506 if (avctx->get_buffer(avctx, &s->picture) < 0)
1508 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1512 s->picture.pict_type = I_TYPE;
1513 s->picture.key_frame = 1;
1515 for (i = 0; i < 3; i++)
1516 s->linesize[i] = s->picture.linesize[i] << s->interlaced;
1519 for (i = 0; i < 64; i++)
1521 j = s->scantable.permutated[i];
1522 s->quant_matrixes[0][j] = sp5x_quant_table[(qscale * 2) + i];
1524 s->qscale[0] = FFMAX(
1525 s->quant_matrixes[0][s->scantable.permutated[1]],
1526 s->quant_matrixes[0][s->scantable.permutated[8]]) >> 1;
1528 for (i = 0; i < 64; i++)
1530 j = s->scantable.permutated[i];
1531 s->quant_matrixes[1][j] = sp5x_quant_table[(qscale * 2) + 1 + i];
1533 s->qscale[1] = FFMAX(
1534 s->quant_matrixes[1][s->scantable.permutated[1]],
1535 s->quant_matrixes[1][s->scantable.permutated[8]]) >> 1;
1540 s->comp_index[0] = 0;
1541 s->nb_blocks[0] = s->h_count[0] * s->v_count[0];
1542 s->h_scount[0] = s->h_count[0];
1543 s->v_scount[0] = s->v_count[0];
1547 s->comp_index[1] = 1;
1548 s->nb_blocks[1] = s->h_count[1] * s->v_count[1];
1549 s->h_scount[1] = s->h_count[1];
1550 s->v_scount[1] = s->v_count[1];
1554 s->comp_index[2] = 2;
1555 s->nb_blocks[2] = s->h_count[2] * s->v_count[2];
1556 s->h_scount[2] = s->h_count[2];
1557 s->v_scount[2] = s->v_count[2];
1561 for (i = 0; i < 3; i++)
1562 s->last_dc[i] = 1024;
1564 s->mb_width = (s->width * s->h_max * 8 -1) / (s->h_max * 8);
1565 s->mb_height = (s->height * s->v_max * 8 -1) / (s->v_max * 8);
1567 init_get_bits(&s->gb, buf+14, (buf_size-14)*8);
1569 return mjpeg_decode_scan(s);
1575 static int mjpeg_decode_end(AVCodecContext *avctx)
1577 MJpegDecodeContext *s = avctx->priv_data;
1581 av_free(s->qscale_table);
1585 free_vlc(&s->vlcs[i][j]);
1590 AVCodec mjpeg_decoder = {
1594 sizeof(MJpegDecodeContext),
1603 AVCodec thp_decoder = {
1607 sizeof(MJpegDecodeContext),
1616 AVCodec mjpegb_decoder = {
1620 sizeof(MJpegDecodeContext),
1624 mjpegb_decode_frame,
1629 AVCodec sp5x_decoder = {
1633 sizeof(MJpegDecodeContext),