3 * Copyright (c) 2000, 2001 Fabrice Bellard.
4 * Copyright (c) 2003 Alex Beregszaszi
5 * Copyright (c) 2003-2004 Michael Niedermayer
7 * Support for external huffman table, various fixes (AVID workaround),
8 * aspecting, new decode_frame mechanism and apple mjpeg-b support
11 * This file is part of FFmpeg.
13 * FFmpeg is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * FFmpeg is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with FFmpeg; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
40 #include "jpeglsdec.h"
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
)
46 uint8_t huff_size
[256+16];
47 uint16_t huff_code
[256+16];
49 assert(nb_codes
<= 256);
51 memset(huff_size
, 0, sizeof(huff_size
));
52 ff_mjpeg_build_huffman_codes(huff_size
, huff_code
, bits_table
, val_table
);
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);
62 return init_vlc(vlc
, 9, nb_codes
, huff_size
, 1, 1, huff_code
, 2, 2, use_static
);
65 static void build_basic_mjpeg_vlc(MJpegDecodeContext
* s
) {
66 build_vlc(&s
->vlcs
[0][0], ff_mjpeg_bits_dc_luminance
,
67 ff_mjpeg_val_dc_luminance
, 12, 0, 0);
68 build_vlc(&s
->vlcs
[0][1], ff_mjpeg_bits_dc_chrominance
,
69 ff_mjpeg_val_dc_chrominance
, 12, 0, 0);
70 build_vlc(&s
->vlcs
[1][0], ff_mjpeg_bits_ac_luminance
,
71 ff_mjpeg_val_ac_luminance
, 251, 0, 1);
72 build_vlc(&s
->vlcs
[1][1], ff_mjpeg_bits_ac_chrominance
,
73 ff_mjpeg_val_ac_chrominance
, 251, 0, 1);
76 av_cold
int ff_mjpeg_decode_init(AVCodecContext
*avctx
)
78 MJpegDecodeContext
*s
= avctx
->priv_data
;
81 dsputil_init(&s
->dsp
, avctx
);
82 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->scantable
, ff_zigzag_direct
);
87 s
->org_height
= avctx
->coded_height
;
89 build_basic_mjpeg_vlc(s
);
91 if (avctx
->flags
& CODEC_FLAG_EXTERN_HUFF
)
93 av_log(avctx
, AV_LOG_INFO
, "mjpeg: using external huffman table\n");
94 init_get_bits(&s
->gb
, avctx
->extradata
, avctx
->extradata_size
*8);
95 if (ff_mjpeg_decode_dht(s
)) {
96 av_log(avctx
, AV_LOG_ERROR
, "mjpeg: error using external huffman table, switching back to internal\n");
97 build_basic_mjpeg_vlc(s
);
100 if (avctx
->extradata_size
> 9 &&
101 AV_RL32(avctx
->extradata
+ 4) == MKTAG('f','i','e','l')) {
102 if (avctx
->extradata
[9] == 6) { /* quicktime icefloe 019 */
103 s
->interlace_polarity
= 1; /* bottom field first */
104 av_log(avctx
, AV_LOG_DEBUG
, "mjpeg bottom field first\n");
112 /* quantize tables */
113 int ff_mjpeg_decode_dqt(MJpegDecodeContext
*s
)
115 int len
, index
, i
, j
;
117 len
= get_bits(&s
->gb
, 16) - 2;
120 /* only 8 bit precision handled */
121 if (get_bits(&s
->gb
, 4) != 0)
123 av_log(s
->avctx
, AV_LOG_ERROR
, "dqt: 16bit precision\n");
126 index
= get_bits(&s
->gb
, 4);
129 av_log(s
->avctx
, AV_LOG_DEBUG
, "index=%d\n", index
);
130 /* read quant table */
132 j
= s
->scantable
.permutated
[i
];
133 s
->quant_matrixes
[index
][j
] = get_bits(&s
->gb
, 8);
136 //XXX FIXME finetune, and perhaps add dc too
137 s
->qscale
[index
]= FFMAX(
138 s
->quant_matrixes
[index
][s
->scantable
.permutated
[1]],
139 s
->quant_matrixes
[index
][s
->scantable
.permutated
[8]]) >> 1;
140 av_log(s
->avctx
, AV_LOG_DEBUG
, "qscale[%d]: %d\n", index
, s
->qscale
[index
]);
147 /* decode huffman tables and build VLC decoders */
148 int ff_mjpeg_decode_dht(MJpegDecodeContext
*s
)
150 int len
, index
, i
, class, n
, v
, code_max
;
151 uint8_t bits_table
[17];
152 uint8_t val_table
[256];
154 len
= get_bits(&s
->gb
, 16) - 2;
159 class = get_bits(&s
->gb
, 4);
162 index
= get_bits(&s
->gb
, 4);
167 bits_table
[i
] = get_bits(&s
->gb
, 8);
171 if (len
< n
|| n
> 256)
176 v
= get_bits(&s
->gb
, 8);
183 /* build VLC and flush previous vlc if present */
184 free_vlc(&s
->vlcs
[class][index
]);
185 av_log(s
->avctx
, AV_LOG_DEBUG
, "class=%d index=%d nb_codes=%d\n",
186 class, index
, code_max
+ 1);
187 if(build_vlc(&s
->vlcs
[class][index
], bits_table
, val_table
, code_max
+ 1, 0, class > 0) < 0){
194 int ff_mjpeg_decode_sof(MJpegDecodeContext
*s
)
196 int len
, nb_components
, i
, width
, height
, pix_fmt_id
;
198 /* XXX: verify len field validity */
199 len
= get_bits(&s
->gb
, 16);
200 s
->bits
= get_bits(&s
->gb
, 8);
202 if(s
->pegasus_rct
) s
->bits
=9;
203 if(s
->bits
==9 && !s
->pegasus_rct
) s
->rct
=1; //FIXME ugly
205 if (s
->bits
!= 8 && !s
->lossless
){
206 av_log(s
->avctx
, AV_LOG_ERROR
, "only 8 bits/component accepted\n");
210 height
= get_bits(&s
->gb
, 16);
211 width
= get_bits(&s
->gb
, 16);
213 //HACK for odd_height.mov
214 if(s
->interlaced
&& s
->width
== width
&& s
->height
== height
+ 1)
217 av_log(s
->avctx
, AV_LOG_DEBUG
, "sof0: picture: %dx%d\n", width
, height
);
218 if(avcodec_check_dimensions(s
->avctx
, width
, height
))
221 nb_components
= get_bits(&s
->gb
, 8);
222 if (nb_components
<= 0 ||
223 nb_components
> MAX_COMPONENTS
)
225 if (s
->ls
&& !(s
->bits
<= 8 || nb_components
== 1)){
226 av_log(s
->avctx
, AV_LOG_ERROR
, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n");
229 s
->nb_components
= nb_components
;
232 for(i
=0;i
<nb_components
;i
++) {
234 s
->component_id
[i
] = get_bits(&s
->gb
, 8) - 1;
235 s
->h_count
[i
] = get_bits(&s
->gb
, 4);
236 s
->v_count
[i
] = get_bits(&s
->gb
, 4);
237 /* compute hmax and vmax (only used in interleaved case) */
238 if (s
->h_count
[i
] > s
->h_max
)
239 s
->h_max
= s
->h_count
[i
];
240 if (s
->v_count
[i
] > s
->v_max
)
241 s
->v_max
= s
->v_count
[i
];
242 s
->quant_index
[i
] = get_bits(&s
->gb
, 8);
243 if (s
->quant_index
[i
] >= 4)
245 av_log(s
->avctx
, AV_LOG_DEBUG
, "component %d %d:%d id: %d quant:%d\n", i
, s
->h_count
[i
],
246 s
->v_count
[i
], s
->component_id
[i
], s
->quant_index
[i
]);
249 if(s
->ls
&& (s
->h_max
> 1 || s
->v_max
> 1)) {
250 av_log(s
->avctx
, AV_LOG_ERROR
, "Subsampling in JPEG-LS is not supported.\n");
254 if(s
->v_max
==1 && s
->h_max
==1 && s
->lossless
==1) s
->rgb
=1;
256 /* if different size, realloc/alloc picture */
257 /* XXX: also check h_count and v_count */
258 if (width
!= s
->width
|| height
!= s
->height
) {
259 av_freep(&s
->qscale_table
);
265 /* test interlaced mode */
266 if (s
->first_picture
&&
267 s
->org_height
!= 0 &&
268 s
->height
< ((s
->org_height
* 3) / 4)) {
270 s
->bottom_field
= s
->interlace_polarity
;
271 s
->picture
.interlaced_frame
= 1;
272 s
->picture
.top_field_first
= !s
->interlace_polarity
;
276 avcodec_set_dimensions(s
->avctx
, width
, height
);
278 s
->qscale_table
= av_mallocz((s
->width
+15)/16);
280 s
->first_picture
= 0;
283 if(s
->interlaced
&& (s
->bottom_field
== !s
->interlace_polarity
))
286 /* XXX: not complete test ! */
287 pix_fmt_id
= (s
->h_count
[0] << 20) | (s
->v_count
[0] << 16) |
288 (s
->h_count
[1] << 12) | (s
->v_count
[1] << 8) |
289 (s
->h_count
[2] << 4) | s
->v_count
[2];
290 av_log(s
->avctx
, AV_LOG_DEBUG
, "pix fmt id %x\n", pix_fmt_id
);
295 s
->avctx
->pix_fmt
= PIX_FMT_RGB32
;
296 }else if(s
->nb_components
==3)
297 s
->avctx
->pix_fmt
= s
->cs_itu601
? PIX_FMT_YUV444P
: PIX_FMT_YUVJ444P
;
299 s
->avctx
->pix_fmt
= PIX_FMT_GRAY8
;
302 s
->avctx
->pix_fmt
= PIX_FMT_GRAY8
;
305 s
->avctx
->pix_fmt
= s
->cs_itu601
? PIX_FMT_YUV440P
: PIX_FMT_YUVJ440P
;
309 s
->avctx
->pix_fmt
= s
->cs_itu601
? PIX_FMT_YUV422P
: PIX_FMT_YUVJ422P
;
312 s
->avctx
->pix_fmt
= s
->cs_itu601
? PIX_FMT_YUV420P
: PIX_FMT_YUVJ420P
;
315 av_log(s
->avctx
, AV_LOG_ERROR
, "Unhandled pixel format 0x%x\n", pix_fmt_id
);
319 if(s
->nb_components
> 1)
320 s
->avctx
->pix_fmt
= PIX_FMT_RGB24
;
321 else if(s
->bits
<= 8)
322 s
->avctx
->pix_fmt
= PIX_FMT_GRAY8
;
324 s
->avctx
->pix_fmt
= PIX_FMT_GRAY16
;
327 if(s
->picture
.data
[0])
328 s
->avctx
->release_buffer(s
->avctx
, &s
->picture
);
330 s
->picture
.reference
= 0;
331 if(s
->avctx
->get_buffer(s
->avctx
, &s
->picture
) < 0){
332 av_log(s
->avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
335 s
->picture
.pict_type
= FF_I_TYPE
;
336 s
->picture
.key_frame
= 1;
339 s
->linesize
[i
]= s
->picture
.linesize
[i
] << s
->interlaced
;
342 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height);
344 if (len
!= (8+(3*nb_components
)))
346 av_log(s
->avctx
, AV_LOG_DEBUG
, "decode_sof0: error, len(%d) mismatch\n", len
);
349 /* totally blank picture as progressive JPEG will only add details to it */
351 memset(s
->picture
.data
[0], 0, s
->picture
.linesize
[0] * s
->height
);
352 memset(s
->picture
.data
[1], 0, s
->picture
.linesize
[1] * s
->height
>> (s
->v_max
- s
->v_count
[1]));
353 memset(s
->picture
.data
[2], 0, s
->picture
.linesize
[2] * s
->height
>> (s
->v_max
- s
->v_count
[2]));
358 static inline int mjpeg_decode_dc(MJpegDecodeContext
*s
, int dc_index
)
361 code
= get_vlc2(&s
->gb
, s
->vlcs
[0][dc_index
].table
, 9, 2);
364 av_log(s
->avctx
, AV_LOG_WARNING
, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index
,
365 &s
->vlcs
[0][dc_index
]);
370 return get_xbits(&s
->gb
, code
);
375 /* decode block and dequantize */
376 static int decode_block(MJpegDecodeContext
*s
, DCTELEM
*block
,
377 int component
, int dc_index
, int ac_index
, int16_t *quant_matrix
)
379 int code
, i
, j
, level
, val
;
382 val
= mjpeg_decode_dc(s
, dc_index
);
384 av_log(s
->avctx
, AV_LOG_ERROR
, "error dc\n");
387 val
= val
* quant_matrix
[0] + s
->last_dc
[component
];
388 s
->last_dc
[component
] = val
;
392 {OPEN_READER(re
, &s
->gb
)
394 UPDATE_CACHE(re
, &s
->gb
);
395 GET_VLC(code
, re
, &s
->gb
, s
->vlcs
[1][ac_index
].table
, 9, 2)
400 i
+= ((unsigned)code
) >> 4;
403 if(code
> MIN_CACHE_BITS
- 16){
404 UPDATE_CACHE(re
, &s
->gb
)
407 int cache
=GET_CACHE(re
,&s
->gb
);
408 int sign
=(~cache
)>>31;
409 level
= (NEG_USR32(sign
^ cache
,code
) ^ sign
) - sign
;
412 LAST_SKIP_BITS(re
, &s
->gb
, code
)
416 j
= s
->scantable
.permutated
[63];
417 block
[j
] = level
* quant_matrix
[j
];
420 av_log(s
->avctx
, AV_LOG_ERROR
, "error count: %d\n", i
);
423 j
= s
->scantable
.permutated
[i
];
424 block
[j
] = level
* quant_matrix
[j
];
427 CLOSE_READER(re
, &s
->gb
)}
432 /* decode block and dequantize - progressive JPEG version */
433 static int decode_block_progressive(MJpegDecodeContext
*s
, DCTELEM
*block
,
434 int component
, int dc_index
, int ac_index
, int16_t *quant_matrix
,
435 int ss
, int se
, int Ah
, int Al
, int *EOBRUN
)
437 int code
, i
, j
, level
, val
, run
;
441 val
= mjpeg_decode_dc(s
, dc_index
);
443 av_log(s
->avctx
, AV_LOG_ERROR
, "error dc\n");
446 val
= (val
* quant_matrix
[0] << Al
) + s
->last_dc
[component
];
449 s
->last_dc
[component
] = val
;
457 {OPEN_READER(re
, &s
->gb
)
459 UPDATE_CACHE(re
, &s
->gb
);
460 GET_VLC(code
, re
, &s
->gb
, s
->vlcs
[1][ac_index
].table
, 9, 2)
461 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */
464 i
+= ((unsigned) code
) >> 4;
466 if(code
> MIN_CACHE_BITS
- 16){
467 UPDATE_CACHE(re
, &s
->gb
)
470 int cache
=GET_CACHE(re
,&s
->gb
);
471 int sign
=(~cache
)>>31;
472 level
= (NEG_USR32(sign
^ cache
,code
) ^ sign
) - sign
;
475 LAST_SKIP_BITS(re
, &s
->gb
, code
)
479 j
= s
->scantable
.permutated
[se
];
480 block
[j
] = level
* quant_matrix
[j
] << Al
;
483 av_log(s
->avctx
, AV_LOG_ERROR
, "error count: %d\n", i
);
486 j
= s
->scantable
.permutated
[i
];
487 block
[j
] = level
* quant_matrix
[j
] << Al
;
489 run
= ((unsigned) code
) >> 4;
490 if(run
== 0xF){// ZRL - skip 15 coefficients
495 UPDATE_CACHE(re
, &s
->gb
);
496 run
+= (GET_CACHE(re
, &s
->gb
) >> (32 - val
)) & (run
- 1);
498 LAST_SKIP_BITS(re
, &s
->gb
, val
);
504 CLOSE_READER(re
, &s
->gb
)}
509 static int ljpeg_decode_rgb_scan(MJpegDecodeContext
*s
, int predictor
, int point_transform
){
511 uint16_t buffer
[32768][4];
512 int left
[3], top
[3], topleft
[3];
513 const int linesize
= s
->linesize
[0];
514 const int mask
= (1<<s
->bits
)-1;
516 if((unsigned)s
->mb_width
> 32768) //dynamic alloc
520 buffer
[0][i
]= 1 << (s
->bits
+ point_transform
- 1);
522 for(mb_y
= 0; mb_y
< s
->mb_height
; mb_y
++) {
523 const int modified_predictor
= mb_y
? predictor
: 1;
524 uint8_t *ptr
= s
->picture
.data
[0] + (linesize
* mb_y
);
526 if (s
->interlaced
&& s
->bottom_field
)
527 ptr
+= linesize
>> 1;
530 top
[i
]= left
[i
]= topleft
[i
]= buffer
[0][i
];
532 for(mb_x
= 0; mb_x
< s
->mb_width
; mb_x
++) {
533 if (s
->restart_interval
&& !s
->restart_count
)
534 s
->restart_count
= s
->restart_interval
;
540 top
[i
]= buffer
[mb_x
][i
];
542 PREDICT(pred
, topleft
[i
], top
[i
], left
[i
], modified_predictor
);
545 buffer
[mb_x
][i
]= mask
& (pred
+ (mjpeg_decode_dc(s
, s
->dc_index
[i
]) << point_transform
));
548 if (s
->restart_interval
&& !--s
->restart_count
) {
549 align_get_bits(&s
->gb
);
550 skip_bits(&s
->gb
, 16); /* skip RSTn */
555 for(mb_x
= 0; mb_x
< s
->mb_width
; mb_x
++) {
556 ptr
[4*mb_x
+1] = buffer
[mb_x
][0] - ((buffer
[mb_x
][1] + buffer
[mb_x
][2] - 0x200)>>2);
557 ptr
[4*mb_x
+0] = buffer
[mb_x
][1] + ptr
[4*mb_x
+1];
558 ptr
[4*mb_x
+2] = buffer
[mb_x
][2] + ptr
[4*mb_x
+1];
560 }else if(s
->pegasus_rct
){
561 for(mb_x
= 0; mb_x
< s
->mb_width
; mb_x
++) {
562 ptr
[4*mb_x
+1] = buffer
[mb_x
][0] - ((buffer
[mb_x
][1] + buffer
[mb_x
][2])>>2);
563 ptr
[4*mb_x
+0] = buffer
[mb_x
][1] + ptr
[4*mb_x
+1];
564 ptr
[4*mb_x
+2] = buffer
[mb_x
][2] + ptr
[4*mb_x
+1];
567 for(mb_x
= 0; mb_x
< s
->mb_width
; mb_x
++) {
568 ptr
[4*mb_x
+0] = buffer
[mb_x
][0];
569 ptr
[4*mb_x
+1] = buffer
[mb_x
][1];
570 ptr
[4*mb_x
+2] = buffer
[mb_x
][2];
577 static int ljpeg_decode_yuv_scan(MJpegDecodeContext
*s
, int predictor
, int point_transform
){
579 const int nb_components
=3;
581 for(mb_y
= 0; mb_y
< s
->mb_height
; mb_y
++) {
582 for(mb_x
= 0; mb_x
< s
->mb_width
; mb_x
++) {
583 if (s
->restart_interval
&& !s
->restart_count
)
584 s
->restart_count
= s
->restart_interval
;
586 if(mb_x
==0 || mb_y
==0 || s
->interlaced
){
587 for(i
=0;i
<nb_components
;i
++) {
589 int n
, h
, v
, x
, y
, c
, j
, linesize
;
591 c
= s
->comp_index
[i
];
596 linesize
= s
->linesize
[c
];
601 ptr
= s
->picture
.data
[c
] + (linesize
* (v
* mb_y
+ y
)) + (h
* mb_x
+ x
); //FIXME optimize this crap
604 pred
= 128 << point_transform
;
610 pred
= ptr
[-linesize
];
612 PREDICT(pred
, ptr
[-linesize
-1], ptr
[-linesize
], ptr
[-1], predictor
);
616 if (s
->interlaced
&& s
->bottom_field
)
617 ptr
+= linesize
>> 1;
618 *ptr
= pred
+ (mjpeg_decode_dc(s
, s
->dc_index
[i
]) << point_transform
);
627 for(i
=0;i
<nb_components
;i
++) {
629 int n
, h
, v
, x
, y
, c
, j
, linesize
;
631 c
= s
->comp_index
[i
];
636 linesize
= s
->linesize
[c
];
641 ptr
= s
->picture
.data
[c
] + (linesize
* (v
* mb_y
+ y
)) + (h
* mb_x
+ x
); //FIXME optimize this crap
642 PREDICT(pred
, ptr
[-linesize
-1], ptr
[-linesize
], ptr
[-1], predictor
);
643 *ptr
= pred
+ (mjpeg_decode_dc(s
, s
->dc_index
[i
]) << point_transform
);
651 if (s
->restart_interval
&& !--s
->restart_count
) {
652 align_get_bits(&s
->gb
);
653 skip_bits(&s
->gb
, 16); /* skip RSTn */
660 static int mjpeg_decode_scan(MJpegDecodeContext
*s
, int nb_components
, int ss
, int se
, int Ah
, int Al
){
663 uint8_t* data
[MAX_COMPONENTS
];
664 int linesize
[MAX_COMPONENTS
];
666 if(Ah
) return 0; /* TODO decode refinement planes too */
668 for(i
=0; i
< nb_components
; i
++) {
669 int c
= s
->comp_index
[i
];
670 data
[c
] = s
->picture
.data
[c
];
671 linesize
[c
]=s
->linesize
[c
];
672 if(s
->avctx
->codec
->id
==CODEC_ID_AMV
) {
673 //picture should be flipped upside-down for this codec
674 assert(!(s
->avctx
->flags
& CODEC_FLAG_EMU_EDGE
));
675 data
[c
] += (linesize
[c
] * (s
->v_scount
[i
] * (8 * s
->mb_height
-((s
->height
/s
->v_max
)&7)) - 1 ));
680 for(mb_y
= 0; mb_y
< s
->mb_height
; mb_y
++) {
681 for(mb_x
= 0; mb_x
< s
->mb_width
; mb_x
++) {
682 if (s
->restart_interval
&& !s
->restart_count
)
683 s
->restart_count
= s
->restart_interval
;
685 for(i
=0;i
<nb_components
;i
++) {
687 int n
, h
, v
, x
, y
, c
, j
;
689 c
= s
->comp_index
[i
];
695 memset(s
->block
, 0, sizeof(s
->block
));
696 if (!s
->progressive
&& decode_block(s
, s
->block
, i
,
697 s
->dc_index
[i
], s
->ac_index
[i
],
698 s
->quant_matrixes
[ s
->quant_index
[c
] ]) < 0) {
699 av_log(s
->avctx
, AV_LOG_ERROR
, "error y=%d x=%d\n", mb_y
, mb_x
);
702 if (s
->progressive
&& decode_block_progressive(s
, s
->block
, i
,
703 s
->dc_index
[i
], s
->ac_index
[i
],
704 s
->quant_matrixes
[ s
->quant_index
[c
] ], ss
, se
, Ah
, Al
, &EOBRUN
) < 0) {
705 av_log(s
->avctx
, AV_LOG_ERROR
, "error y=%d x=%d\n", mb_y
, mb_x
);
708 // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x);
710 (((linesize
[c
] * (v
* mb_y
+ y
) * 8) +
711 (h
* mb_x
+ x
) * 8) >> s
->avctx
->lowres
);
712 if (s
->interlaced
&& s
->bottom_field
)
713 ptr
+= linesize
[c
] >> 1;
714 //av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d %d %d %d %d \n", mb_x, mb_y, x, y, c, s->bottom_field, (v * mb_y + y) * 8, (h * mb_x + x) * 8);
716 s
->dsp
.idct_put(ptr
, linesize
[c
], s
->block
);
718 s
->dsp
.idct_add(ptr
, linesize
[c
], s
->block
);
725 /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */
726 if (s
->restart_interval
&& (s
->restart_interval
< 1350) &&
727 !--s
->restart_count
) {
728 align_get_bits(&s
->gb
);
729 skip_bits(&s
->gb
, 16); /* skip RSTn */
730 for (i
=0; i
<nb_components
; i
++) /* reset dc */
731 s
->last_dc
[i
] = 1024;
738 int ff_mjpeg_decode_sos(MJpegDecodeContext
*s
)
740 int len
, nb_components
, i
, h
, v
, predictor
, point_transform
;
741 int vmax
, hmax
, index
, id
;
742 const int block_size
= s
->lossless
? 1 : 8;
745 /* XXX: verify len field validity */
746 len
= get_bits(&s
->gb
, 16);
747 nb_components
= get_bits(&s
->gb
, 8);
748 if (len
!= 6+2*nb_components
)
750 av_log(s
->avctx
, AV_LOG_ERROR
, "decode_sos: invalid len (%d)\n", len
);
755 for(i
=0;i
<nb_components
;i
++) {
756 id
= get_bits(&s
->gb
, 8) - 1;
757 av_log(s
->avctx
, AV_LOG_DEBUG
, "component: %d\n", id
);
758 /* find component index */
759 for(index
=0;index
<s
->nb_components
;index
++)
760 if (id
== s
->component_id
[index
])
762 if (index
== s
->nb_components
)
764 av_log(s
->avctx
, AV_LOG_ERROR
, "decode_sos: index(%d) out of components\n", index
);
768 s
->comp_index
[i
] = index
;
770 s
->nb_blocks
[i
] = s
->h_count
[index
] * s
->v_count
[index
];
771 s
->h_scount
[i
] = s
->h_count
[index
];
772 s
->v_scount
[i
] = s
->v_count
[index
];
774 s
->dc_index
[i
] = get_bits(&s
->gb
, 4);
775 s
->ac_index
[i
] = get_bits(&s
->gb
, 4);
777 if (s
->dc_index
[i
] < 0 || s
->ac_index
[i
] < 0 ||
778 s
->dc_index
[i
] >= 4 || s
->ac_index
[i
] >= 4)
781 switch(s
->start_code
)
784 if (dc_index
[i
] > 1 || ac_index
[i
] > 1)
789 if (dc_index
[i
] > 3 || ac_index
[i
] > 3)
793 if (dc_index
[i
] > 3 || ac_index
[i
] != 0)
800 predictor
= get_bits(&s
->gb
, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */
801 ilv
= get_bits(&s
->gb
, 8); /* JPEG Se / JPEG-LS ILV */
802 prev_shift
= get_bits(&s
->gb
, 4); /* Ah */
803 point_transform
= get_bits(&s
->gb
, 4); /* Al */
805 for(i
=0;i
<nb_components
;i
++)
806 s
->last_dc
[i
] = 1024;
808 if (nb_components
> 1) {
809 /* interleaved stream */
810 s
->mb_width
= (s
->width
+ s
->h_max
* block_size
- 1) / (s
->h_max
* block_size
);
811 s
->mb_height
= (s
->height
+ s
->v_max
* block_size
- 1) / (s
->v_max
* block_size
);
812 } else if(!s
->ls
) { /* skip this for JPEG-LS */
813 h
= s
->h_max
/ s
->h_scount
[0];
814 v
= s
->v_max
/ s
->v_scount
[0];
815 s
->mb_width
= (s
->width
+ h
* block_size
- 1) / (h
* block_size
);
816 s
->mb_height
= (s
->height
+ v
* block_size
- 1) / (v
* block_size
);
822 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
823 av_log(s
->avctx
, AV_LOG_DEBUG
, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n", s
->lossless
? "lossless" : "sequencial DCT", s
->rgb
? "RGB" : "",
824 predictor
, point_transform
, ilv
, s
->bits
,
825 s
->pegasus_rct
? "PRCT" : (s
->rct
? "RCT" : ""));
828 /* mjpeg-b can have padding bytes between sos and image data, skip them */
829 for (i
= s
->mjpb_skiptosod
; i
> 0; i
--)
830 skip_bits(&s
->gb
, 8);
833 if(ENABLE_JPEGLS_DECODER
&& s
->ls
){
835 // reset_ls_coding_parameters(s, 0);
837 ff_jpegls_decode_picture(s
, predictor
, point_transform
, ilv
);
840 if(ljpeg_decode_rgb_scan(s
, predictor
, point_transform
) < 0)
843 if(ljpeg_decode_yuv_scan(s
, predictor
, point_transform
) < 0)
848 if(mjpeg_decode_scan(s
, nb_components
, predictor
, ilv
, prev_shift
, point_transform
) < 0)
854 av_log(s
->avctx
, AV_LOG_ERROR
, "decode_sos: ac/dc index out of range\n");
858 static int mjpeg_decode_dri(MJpegDecodeContext
*s
)
860 if (get_bits(&s
->gb
, 16) != 4)
862 s
->restart_interval
= get_bits(&s
->gb
, 16);
863 s
->restart_count
= 0;
864 av_log(s
->avctx
, AV_LOG_DEBUG
, "restart interval: %d\n", s
->restart_interval
);
869 static int mjpeg_decode_app(MJpegDecodeContext
*s
)
873 len
= get_bits(&s
->gb
, 16);
876 if(8*len
+ get_bits_count(&s
->gb
) > s
->gb
.size_in_bits
)
879 id
= (get_bits(&s
->gb
, 16) << 16) | get_bits(&s
->gb
, 16);
883 if(s
->avctx
->debug
& FF_DEBUG_STARTCODE
){
884 av_log(s
->avctx
, AV_LOG_DEBUG
, "APPx %8X\n", id
);
887 /* buggy AVID, it puts EOI only at every 10th frame */
888 /* also this fourcc is used by non-avid files too, it holds some
889 informations, but it's always present in AVID creates files */
890 if (id
== ff_get_fourcc("AVI1"))
897 4bytes field_size_less_padding
900 // if (s->first_picture)
901 // printf("mjpeg: workarounding buggy AVID\n");
902 i
= get_bits(&s
->gb
, 8);
903 if (i
==2) s
->bottom_field
= 1;
904 else if(i
==1) s
->bottom_field
= 0;
906 skip_bits(&s
->gb
, 8);
907 skip_bits(&s
->gb
, 32);
908 skip_bits(&s
->gb
, 32);
911 // if (s->interlace_polarity)
912 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity);
918 if (id
== ff_get_fourcc("JFIF"))
920 int t_w
, t_h
, v1
, v2
;
921 skip_bits(&s
->gb
, 8); /* the trailing zero-byte */
922 v1
= get_bits(&s
->gb
, 8);
923 v2
= get_bits(&s
->gb
, 8);
924 skip_bits(&s
->gb
, 8);
926 s
->avctx
->sample_aspect_ratio
.num
= get_bits(&s
->gb
, 16);
927 s
->avctx
->sample_aspect_ratio
.den
= get_bits(&s
->gb
, 16);
929 if (s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
930 av_log(s
->avctx
, AV_LOG_INFO
, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n",
932 s
->avctx
->sample_aspect_ratio
.num
,
933 s
->avctx
->sample_aspect_ratio
.den
936 t_w
= get_bits(&s
->gb
, 8);
937 t_h
= get_bits(&s
->gb
, 8);
941 if (len
-10-(t_w
*t_h
*3) > 0)
948 if (id
== ff_get_fourcc("Adob") && (get_bits(&s
->gb
, 8) == 'e'))
950 if (s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
951 av_log(s
->avctx
, AV_LOG_INFO
, "mjpeg: Adobe header found\n");
952 skip_bits(&s
->gb
, 16); /* version */
953 skip_bits(&s
->gb
, 16); /* flags0 */
954 skip_bits(&s
->gb
, 16); /* flags1 */
955 skip_bits(&s
->gb
, 8); /* transform */
960 if (id
== ff_get_fourcc("LJIF")){
961 if (s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
962 av_log(s
->avctx
, AV_LOG_INFO
, "Pegasus lossless jpeg header found\n");
963 skip_bits(&s
->gb
, 16); /* version ? */
964 skip_bits(&s
->gb
, 16); /* unknwon always 0? */
965 skip_bits(&s
->gb
, 16); /* unknwon always 0? */
966 skip_bits(&s
->gb
, 16); /* unknwon always 0? */
967 switch( get_bits(&s
->gb
, 8)){
977 av_log(s
->avctx
, AV_LOG_ERROR
, "unknown colorspace\n");
984 if ((s
->start_code
== APP1
) && (len
> (0x28 - 8)))
986 id
= (get_bits(&s
->gb
, 16) << 16) | get_bits(&s
->gb
, 16);
989 if (id
== ff_get_fourcc("mjpg")) /* Apple MJPEG-A */
992 skip_bits(&s
->gb
, 32); /* field size */
993 skip_bits(&s
->gb
, 32); /* pad field size */
994 skip_bits(&s
->gb
, 32); /* next off */
995 skip_bits(&s
->gb
, 32); /* quant off */
996 skip_bits(&s
->gb
, 32); /* huff off */
997 skip_bits(&s
->gb
, 32); /* image off */
998 skip_bits(&s
->gb
, 32); /* scan off */
999 skip_bits(&s
->gb
, 32); /* data off */
1001 if (s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
1002 av_log(s
->avctx
, AV_LOG_INFO
, "mjpeg: Apple MJPEG-A header found\n");
1007 /* slow but needed for extreme adobe jpegs */
1009 av_log(s
->avctx
, AV_LOG_ERROR
, "mjpeg: error, decode_app parser read over the end\n");
1011 skip_bits(&s
->gb
, 8);
1016 static int mjpeg_decode_com(MJpegDecodeContext
*s
)
1018 int len
= get_bits(&s
->gb
, 16);
1019 if (len
>= 2 && 8*len
- 16 + get_bits_count(&s
->gb
) <= s
->gb
.size_in_bits
) {
1020 char *cbuf
= av_malloc(len
- 1);
1023 for (i
= 0; i
< len
- 2; i
++)
1024 cbuf
[i
] = get_bits(&s
->gb
, 8);
1025 if (i
> 0 && cbuf
[i
-1] == '\n')
1030 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
1031 av_log(s
->avctx
, AV_LOG_INFO
, "mjpeg comment: '%s'\n", cbuf
);
1033 /* buggy avid, it puts EOI only at every 10th frame */
1034 if (!strcmp(cbuf
, "AVID"))
1037 // if (s->first_picture)
1038 // printf("mjpeg: workarounding buggy AVID\n");
1040 else if(!strcmp(cbuf
, "CS=ITU601")){
1052 static int valid_marker_list
[] =
1054 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */
1055 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1056 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1057 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1058 /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1059 /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1060 /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1061 /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1062 /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1063 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1064 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1065 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1066 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1067 /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1068 /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1069 /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1070 /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1074 /* return the 8 bit start code value and update the search
1075 state. Return -1 if no start code found */
1076 static int find_marker(const uint8_t **pbuf_ptr
, const uint8_t *buf_end
)
1078 const uint8_t *buf_ptr
;
1085 buf_ptr
= *pbuf_ptr
;
1086 while (buf_ptr
< buf_end
) {
1089 if ((v
== 0xff) && (v2
>= 0xc0) && (v2
<= 0xfe) && buf_ptr
< buf_end
) {
1100 av_log(NULL
, AV_LOG_VERBOSE
, "find_marker skipped %d bytes\n", skipped
);
1102 *pbuf_ptr
= buf_ptr
;
1106 int ff_mjpeg_decode_frame(AVCodecContext
*avctx
,
1107 void *data
, int *data_size
,
1108 const uint8_t *buf
, int buf_size
)
1110 MJpegDecodeContext
*s
= avctx
->priv_data
;
1111 const uint8_t *buf_end
, *buf_ptr
;
1113 AVFrame
*picture
= data
;
1116 buf_end
= buf
+ buf_size
;
1117 while (buf_ptr
< buf_end
) {
1118 /* find start next marker */
1119 start_code
= find_marker(&buf_ptr
, buf_end
);
1122 if (start_code
< 0) {
1125 av_log(avctx
, AV_LOG_DEBUG
, "marker=%x avail_size_in_buf=%td\n", start_code
, buf_end
- buf_ptr
);
1127 if ((buf_end
- buf_ptr
) > s
->buffer_size
)
1130 s
->buffer_size
= buf_end
-buf_ptr
;
1131 s
->buffer
= av_malloc(s
->buffer_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
1132 av_log(avctx
, AV_LOG_DEBUG
, "buffer too small, expanding to %d bytes\n",
1136 /* unescape buffer of SOS, use special treatment for JPEG-LS */
1137 if (start_code
== SOS
&& !s
->ls
)
1139 const uint8_t *src
= buf_ptr
;
1140 uint8_t *dst
= s
->buffer
;
1144 uint8_t x
= *(src
++);
1147 if (avctx
->codec_id
!= CODEC_ID_THP
)
1150 while (src
< buf_end
&& x
== 0xff)
1153 if (x
>= 0xd0 && x
<= 0xd7)
1160 init_get_bits(&s
->gb
, s
->buffer
, (dst
- s
->buffer
)*8);
1162 av_log(avctx
, AV_LOG_DEBUG
, "escaping removed %td bytes\n",
1163 (buf_end
- buf_ptr
) - (dst
- s
->buffer
));
1165 else if(start_code
== SOS
&& s
->ls
){
1166 const uint8_t *src
= buf_ptr
;
1167 uint8_t *dst
= s
->buffer
;
1175 while (src
+ t
< buf_end
){
1176 uint8_t x
= src
[t
++];
1178 while((src
+ t
< buf_end
) && x
== 0xff)
1188 init_put_bits(&pb
, dst
, t
);
1190 /* unescape bitstream */
1192 uint8_t x
= src
[b
++];
1193 put_bits(&pb
, 8, x
);
1196 put_bits(&pb
, 7, x
);
1200 flush_put_bits(&pb
);
1202 init_get_bits(&s
->gb
, dst
, bit_count
);
1205 init_get_bits(&s
->gb
, buf_ptr
, (buf_end
- buf_ptr
)*8);
1207 s
->start_code
= start_code
;
1208 if(s
->avctx
->debug
& FF_DEBUG_STARTCODE
){
1209 av_log(avctx
, AV_LOG_DEBUG
, "startcode: %X\n", start_code
);
1212 /* process markers */
1213 if (start_code
>= 0xd0 && start_code
<= 0xd7) {
1214 av_log(avctx
, AV_LOG_DEBUG
, "restart marker: %d\n", start_code
&0x0f);
1216 } else if (start_code
>= APP0
&& start_code
<= APP15
) {
1217 mjpeg_decode_app(s
);
1219 } else if (start_code
== COM
){
1220 mjpeg_decode_com(s
);
1223 switch(start_code
) {
1225 s
->restart_interval
= 0;
1227 s
->restart_count
= 0;
1228 /* nothing to do on SOI */
1231 ff_mjpeg_decode_dqt(s
);
1234 if(ff_mjpeg_decode_dht(s
) < 0){
1235 av_log(avctx
, AV_LOG_ERROR
, "huffman table decode error\n");
1243 if (ff_mjpeg_decode_sof(s
) < 0)
1250 if (ff_mjpeg_decode_sof(s
) < 0)
1257 if (ff_mjpeg_decode_sof(s
) < 0)
1264 if (ff_mjpeg_decode_sof(s
) < 0)
1268 if (!ENABLE_JPEGLS_DECODER
|| ff_jpegls_decode_lse(s
) < 0)
1273 if ((s
->buggy_avid
&& !s
->interlaced
) || s
->restart_interval
)
1277 if (s
->interlaced
) {
1278 s
->bottom_field
^= 1;
1279 /* if not bottom field, do not output image yet */
1280 if (s
->bottom_field
== !s
->interlace_polarity
)
1283 *picture
= s
->picture
;
1284 *data_size
= sizeof(AVFrame
);
1287 picture
->quality
= FFMAX3(s
->qscale
[0], s
->qscale
[1], s
->qscale
[2]);
1288 picture
->qstride
= 0;
1289 picture
->qscale_table
= s
->qscale_table
;
1290 memset(picture
->qscale_table
, picture
->quality
, (s
->width
+15)/16);
1291 if(avctx
->debug
& FF_DEBUG_QP
)
1292 av_log(avctx
, AV_LOG_DEBUG
, "QP: %d\n", picture
->quality
);
1293 picture
->quality
*= FF_QP2LAMBDA
;
1300 ff_mjpeg_decode_sos(s
);
1301 /* buggy avid puts EOI every 10-20th frame */
1302 /* if restart period is over process EOI */
1303 if ((s
->buggy_avid
&& !s
->interlaced
) || s
->restart_interval
)
1307 mjpeg_decode_dri(s
);
1320 av_log(avctx
, AV_LOG_ERROR
, "mjpeg: unsupported coding type (%x)\n", start_code
);
1323 // printf("mjpeg: unsupported marker (%x)\n", start_code);
1328 /* eof process start code */
1329 buf_ptr
+= (get_bits_count(&s
->gb
)+7)/8;
1330 av_log(avctx
, AV_LOG_DEBUG
, "marker parser used %d bytes (%d bits)\n",
1331 (get_bits_count(&s
->gb
)+7)/8, get_bits_count(&s
->gb
));
1336 av_log(avctx
, AV_LOG_DEBUG
, "mjpeg decode frame unused %td bytes\n", buf_end
- buf_ptr
);
1337 // return buf_end - buf_ptr;
1338 return buf_ptr
- buf
;
1341 av_cold
int ff_mjpeg_decode_end(AVCodecContext
*avctx
)
1343 MJpegDecodeContext
*s
= avctx
->priv_data
;
1347 av_free(s
->qscale_table
);
1351 free_vlc(&s
->vlcs
[i
][j
]);
1356 AVCodec mjpeg_decoder
= {
1360 sizeof(MJpegDecodeContext
),
1361 ff_mjpeg_decode_init
,
1363 ff_mjpeg_decode_end
,
1364 ff_mjpeg_decode_frame
,
1367 .long_name
= "MJPEG (Motion JPEG)",
1370 AVCodec thp_decoder
= {
1374 sizeof(MJpegDecodeContext
),
1375 ff_mjpeg_decode_init
,
1377 ff_mjpeg_decode_end
,
1378 ff_mjpeg_decode_frame
,
1381 .long_name
= "Nintendo Gamecube THP video",