comparison mjpegdec.c @ 5020:eb0ad6423405 libavcodec

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