Libav
|
00001 /* 00002 * MJPEG decoder 00003 * Copyright (c) 2000, 2001 Fabrice Bellard 00004 * Copyright (c) 2003 Alex Beregszaszi 00005 * Copyright (c) 2003-2004 Michael Niedermayer 00006 * 00007 * Support for external huffman table, various fixes (AVID workaround), 00008 * aspecting, new decode_frame mechanism and apple mjpeg-b support 00009 * by Alex Beregszaszi 00010 * 00011 * This file is part of FFmpeg. 00012 * 00013 * FFmpeg is free software; you can redistribute it and/or 00014 * modify it under the terms of the GNU Lesser General Public 00015 * License as published by the Free Software Foundation; either 00016 * version 2.1 of the License, or (at your option) any later version. 00017 * 00018 * FFmpeg is distributed in the hope that it will be useful, 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00021 * Lesser General Public License for more details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public 00024 * License along with FFmpeg; if not, write to the Free Software 00025 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00026 */ 00027 00033 //#define DEBUG 00034 #include <assert.h> 00035 00036 #include "avcodec.h" 00037 #include "dsputil.h" 00038 #include "mjpeg.h" 00039 #include "mjpegdec.h" 00040 #include "jpeglsdec.h" 00041 00042 00043 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table, 00044 int nb_codes, int use_static, int is_ac) 00045 { 00046 uint8_t huff_size[256+16]; 00047 uint16_t huff_code[256+16]; 00048 00049 assert(nb_codes <= 256); 00050 00051 memset(huff_size, 0, sizeof(huff_size)); 00052 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table); 00053 00054 if(is_ac){ 00055 memmove(huff_size+16, huff_size, sizeof(uint8_t)*nb_codes); 00056 memmove(huff_code+16, huff_code, sizeof(uint16_t)*nb_codes); 00057 memset(huff_size, 0, sizeof(uint8_t)*16); 00058 memset(huff_code, 0, sizeof(uint16_t)*16); 00059 nb_codes += 16; 00060 } 00061 00062 return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_static); 00063 } 00064 00065 static void build_basic_mjpeg_vlc(MJpegDecodeContext * s) { 00066 build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance, 00067 ff_mjpeg_val_dc, 12, 0, 0); 00068 build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance, 00069 ff_mjpeg_val_dc, 12, 0, 0); 00070 build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance, 00071 ff_mjpeg_val_ac_luminance, 251, 0, 1); 00072 build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance, 00073 ff_mjpeg_val_ac_chrominance, 251, 0, 1); 00074 } 00075 00076 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx) 00077 { 00078 MJpegDecodeContext *s = avctx->priv_data; 00079 00080 s->avctx = avctx; 00081 dsputil_init(&s->dsp, avctx); 00082 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct); 00083 s->buffer_size = 0; 00084 s->buffer = NULL; 00085 s->start_code = -1; 00086 s->first_picture = 1; 00087 s->org_height = avctx->coded_height; 00088 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER; 00089 00090 build_basic_mjpeg_vlc(s); 00091 00092 if (avctx->flags & CODEC_FLAG_EXTERN_HUFF) 00093 { 00094 av_log(avctx, AV_LOG_INFO, "mjpeg: using external huffman table\n"); 00095 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8); 00096 if (ff_mjpeg_decode_dht(s)) { 00097 av_log(avctx, AV_LOG_ERROR, "mjpeg: error using external huffman table, switching back to internal\n"); 00098 build_basic_mjpeg_vlc(s); 00099 } 00100 } 00101 if (avctx->extradata_size > 9 && 00102 AV_RL32(avctx->extradata + 4) == MKTAG('f','i','e','l')) { 00103 if (avctx->extradata[9] == 6) { /* quicktime icefloe 019 */ 00104 s->interlace_polarity = 1; /* bottom field first */ 00105 av_log(avctx, AV_LOG_DEBUG, "mjpeg bottom field first\n"); 00106 } 00107 } 00108 if (avctx->codec->id == CODEC_ID_AMV) 00109 s->flipped = 1; 00110 00111 return 0; 00112 } 00113 00114 00115 /* quantize tables */ 00116 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s) 00117 { 00118 int len, index, i, j; 00119 00120 len = get_bits(&s->gb, 16) - 2; 00121 00122 while (len >= 65) { 00123 /* only 8 bit precision handled */ 00124 if (get_bits(&s->gb, 4) != 0) 00125 { 00126 av_log(s->avctx, AV_LOG_ERROR, "dqt: 16bit precision\n"); 00127 return -1; 00128 } 00129 index = get_bits(&s->gb, 4); 00130 if (index >= 4) 00131 return -1; 00132 av_log(s->avctx, AV_LOG_DEBUG, "index=%d\n", index); 00133 /* read quant table */ 00134 for(i=0;i<64;i++) { 00135 j = s->scantable.permutated[i]; 00136 s->quant_matrixes[index][j] = get_bits(&s->gb, 8); 00137 } 00138 00139 //XXX FIXME finetune, and perhaps add dc too 00140 s->qscale[index]= FFMAX( 00141 s->quant_matrixes[index][s->scantable.permutated[1]], 00142 s->quant_matrixes[index][s->scantable.permutated[8]]) >> 1; 00143 av_log(s->avctx, AV_LOG_DEBUG, "qscale[%d]: %d\n", index, s->qscale[index]); 00144 len -= 65; 00145 } 00146 00147 return 0; 00148 } 00149 00150 /* decode huffman tables and build VLC decoders */ 00151 int ff_mjpeg_decode_dht(MJpegDecodeContext *s) 00152 { 00153 int len, index, i, class, n, v, code_max; 00154 uint8_t bits_table[17]; 00155 uint8_t val_table[256]; 00156 00157 len = get_bits(&s->gb, 16) - 2; 00158 00159 while (len > 0) { 00160 if (len < 17) 00161 return -1; 00162 class = get_bits(&s->gb, 4); 00163 if (class >= 2) 00164 return -1; 00165 index = get_bits(&s->gb, 4); 00166 if (index >= 4) 00167 return -1; 00168 n = 0; 00169 for(i=1;i<=16;i++) { 00170 bits_table[i] = get_bits(&s->gb, 8); 00171 n += bits_table[i]; 00172 } 00173 len -= 17; 00174 if (len < n || n > 256) 00175 return -1; 00176 00177 code_max = 0; 00178 for(i=0;i<n;i++) { 00179 v = get_bits(&s->gb, 8); 00180 if (v > code_max) 00181 code_max = v; 00182 val_table[i] = v; 00183 } 00184 len -= n; 00185 00186 /* build VLC and flush previous vlc if present */ 00187 free_vlc(&s->vlcs[class][index]); 00188 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n", 00189 class, index, code_max + 1); 00190 if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1, 0, class > 0) < 0){ 00191 return -1; 00192 } 00193 } 00194 return 0; 00195 } 00196 00197 int ff_mjpeg_decode_sof(MJpegDecodeContext *s) 00198 { 00199 int len, nb_components, i, width, height, pix_fmt_id; 00200 00201 /* XXX: verify len field validity */ 00202 len = get_bits(&s->gb, 16); 00203 s->bits= get_bits(&s->gb, 8); 00204 00205 if(s->pegasus_rct) s->bits=9; 00206 if(s->bits==9 && !s->pegasus_rct) s->rct=1; //FIXME ugly 00207 00208 if (s->bits != 8 && !s->lossless){ 00209 av_log(s->avctx, AV_LOG_ERROR, "only 8 bits/component accepted\n"); 00210 return -1; 00211 } 00212 00213 height = get_bits(&s->gb, 16); 00214 width = get_bits(&s->gb, 16); 00215 00216 //HACK for odd_height.mov 00217 if(s->interlaced && s->width == width && s->height == height + 1) 00218 height= s->height; 00219 00220 av_log(s->avctx, AV_LOG_DEBUG, "sof0: picture: %dx%d\n", width, height); 00221 if(avcodec_check_dimensions(s->avctx, width, height)) 00222 return -1; 00223 00224 nb_components = get_bits(&s->gb, 8); 00225 if (nb_components <= 0 || 00226 nb_components > MAX_COMPONENTS) 00227 return -1; 00228 if (s->ls && !(s->bits <= 8 || nb_components == 1)){ 00229 av_log(s->avctx, AV_LOG_ERROR, "only <= 8 bits/component or 16-bit gray accepted for JPEG-LS\n"); 00230 return -1; 00231 } 00232 s->nb_components = nb_components; 00233 s->h_max = 1; 00234 s->v_max = 1; 00235 for(i=0;i<nb_components;i++) { 00236 /* component id */ 00237 s->component_id[i] = get_bits(&s->gb, 8) - 1; 00238 s->h_count[i] = get_bits(&s->gb, 4); 00239 s->v_count[i] = get_bits(&s->gb, 4); 00240 /* compute hmax and vmax (only used in interleaved case) */ 00241 if (s->h_count[i] > s->h_max) 00242 s->h_max = s->h_count[i]; 00243 if (s->v_count[i] > s->v_max) 00244 s->v_max = s->v_count[i]; 00245 s->quant_index[i] = get_bits(&s->gb, 8); 00246 if (s->quant_index[i] >= 4) 00247 return -1; 00248 av_log(s->avctx, AV_LOG_DEBUG, "component %d %d:%d id: %d quant:%d\n", i, s->h_count[i], 00249 s->v_count[i], s->component_id[i], s->quant_index[i]); 00250 } 00251 00252 if(s->ls && (s->h_max > 1 || s->v_max > 1)) { 00253 av_log(s->avctx, AV_LOG_ERROR, "Subsampling in JPEG-LS is not supported.\n"); 00254 return -1; 00255 } 00256 00257 if(s->v_max==1 && s->h_max==1 && s->lossless==1) s->rgb=1; 00258 00259 /* if different size, realloc/alloc picture */ 00260 /* XXX: also check h_count and v_count */ 00261 if (width != s->width || height != s->height) { 00262 av_freep(&s->qscale_table); 00263 00264 s->width = width; 00265 s->height = height; 00266 s->interlaced = 0; 00267 00268 /* test interlaced mode */ 00269 if (s->first_picture && 00270 s->org_height != 0 && 00271 s->height < ((s->org_height * 3) / 4)) { 00272 s->interlaced = 1; 00273 s->bottom_field = s->interlace_polarity; 00274 s->picture.interlaced_frame = 1; 00275 s->picture.top_field_first = !s->interlace_polarity; 00276 height *= 2; 00277 } 00278 00279 avcodec_set_dimensions(s->avctx, width, height); 00280 00281 s->qscale_table= av_mallocz((s->width+15)/16); 00282 00283 s->first_picture = 0; 00284 } 00285 00286 if(s->interlaced && (s->bottom_field == !s->interlace_polarity)) 00287 return 0; 00288 00289 /* XXX: not complete test ! */ 00290 pix_fmt_id = (s->h_count[0] << 28) | (s->v_count[0] << 24) | 00291 (s->h_count[1] << 20) | (s->v_count[1] << 16) | 00292 (s->h_count[2] << 12) | (s->v_count[2] << 8) | 00293 (s->h_count[3] << 4) | s->v_count[3]; 00294 av_log(s->avctx, AV_LOG_DEBUG, "pix fmt id %x\n", pix_fmt_id); 00295 //NOTE we do not allocate pictures large enough for the possible padding of h/v_count being 4 00296 if(!(pix_fmt_id & 0xD0D0D0D0)) 00297 pix_fmt_id-= (pix_fmt_id & 0xF0F0F0F0)>>1; 00298 if(!(pix_fmt_id & 0x0D0D0D0D)) 00299 pix_fmt_id-= (pix_fmt_id & 0x0F0F0F0F)>>1; 00300 00301 switch(pix_fmt_id){ 00302 case 0x11111100: 00303 if(s->rgb){ 00304 s->avctx->pix_fmt = PIX_FMT_BGRA; 00305 }else 00306 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV444P : PIX_FMT_YUVJ444P; 00307 assert(s->nb_components==3); 00308 break; 00309 case 0x11000000: 00310 s->avctx->pix_fmt = PIX_FMT_GRAY8; 00311 break; 00312 case 0x12111100: 00313 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV440P : PIX_FMT_YUVJ440P; 00314 break; 00315 case 0x21111100: 00316 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV422P : PIX_FMT_YUVJ422P; 00317 break; 00318 case 0x22111100: 00319 s->avctx->pix_fmt = s->cs_itu601 ? PIX_FMT_YUV420P : PIX_FMT_YUVJ420P; 00320 break; 00321 default: 00322 av_log(s->avctx, AV_LOG_ERROR, "Unhandled pixel format 0x%x\n", pix_fmt_id); 00323 return -1; 00324 } 00325 if(s->ls){ 00326 if(s->nb_components > 1) 00327 s->avctx->pix_fmt = PIX_FMT_RGB24; 00328 else if(s->bits <= 8) 00329 s->avctx->pix_fmt = PIX_FMT_GRAY8; 00330 else 00331 s->avctx->pix_fmt = PIX_FMT_GRAY16; 00332 } 00333 00334 if(s->picture.data[0]) 00335 s->avctx->release_buffer(s->avctx, &s->picture); 00336 00337 s->picture.reference= 0; 00338 if(s->avctx->get_buffer(s->avctx, &s->picture) < 0){ 00339 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 00340 return -1; 00341 } 00342 s->picture.pict_type= FF_I_TYPE; 00343 s->picture.key_frame= 1; 00344 s->got_picture = 1; 00345 00346 for(i=0; i<3; i++){ 00347 s->linesize[i]= s->picture.linesize[i] << s->interlaced; 00348 } 00349 00350 // printf("%d %d %d %d %d %d\n", s->width, s->height, s->linesize[0], s->linesize[1], s->interlaced, s->avctx->height); 00351 00352 if (len != (8+(3*nb_components))) 00353 { 00354 av_log(s->avctx, AV_LOG_DEBUG, "decode_sof0: error, len(%d) mismatch\n", len); 00355 } 00356 00357 /* totally blank picture as progressive JPEG will only add details to it */ 00358 if(s->progressive){ 00359 int bw = (width + s->h_max*8-1) / (s->h_max*8); 00360 int bh = (height + s->v_max*8-1) / (s->v_max*8); 00361 for(i=0; i<s->nb_components; i++) { 00362 int size = bw * bh * s->h_count[i] * s->v_count[i]; 00363 av_freep(&s->blocks[i]); 00364 av_freep(&s->last_nnz[i]); 00365 s->blocks[i] = av_malloc(size * sizeof(**s->blocks)); 00366 s->last_nnz[i] = av_mallocz(size * sizeof(**s->last_nnz)); 00367 s->block_stride[i] = bw * s->h_count[i]; 00368 } 00369 memset(s->coefs_finished, 0, sizeof(s->coefs_finished)); 00370 } 00371 return 0; 00372 } 00373 00374 static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index) 00375 { 00376 int code; 00377 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2); 00378 if (code < 0) 00379 { 00380 av_log(s->avctx, AV_LOG_WARNING, "mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index, 00381 &s->vlcs[0][dc_index]); 00382 return 0xffff; 00383 } 00384 00385 if(code) 00386 return get_xbits(&s->gb, code); 00387 else 00388 return 0; 00389 } 00390 00391 /* decode block and dequantize */ 00392 static int decode_block(MJpegDecodeContext *s, DCTELEM *block, 00393 int component, int dc_index, int ac_index, int16_t *quant_matrix) 00394 { 00395 int code, i, j, level, val; 00396 00397 /* DC coef */ 00398 val = mjpeg_decode_dc(s, dc_index); 00399 if (val == 0xffff) { 00400 av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); 00401 return -1; 00402 } 00403 val = val * quant_matrix[0] + s->last_dc[component]; 00404 s->last_dc[component] = val; 00405 block[0] = val; 00406 /* AC coefs */ 00407 i = 0; 00408 {OPEN_READER(re, &s->gb) 00409 for(;;) { 00410 UPDATE_CACHE(re, &s->gb); 00411 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) 00412 00413 /* EOB */ 00414 if (code == 0x10) 00415 break; 00416 i += ((unsigned)code) >> 4; 00417 if(code != 0x100){ 00418 code &= 0xf; 00419 if(code > MIN_CACHE_BITS - 16){ 00420 UPDATE_CACHE(re, &s->gb) 00421 } 00422 { 00423 int cache=GET_CACHE(re,&s->gb); 00424 int sign=(~cache)>>31; 00425 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; 00426 } 00427 00428 LAST_SKIP_BITS(re, &s->gb, code) 00429 00430 if (i >= 63) { 00431 if(i == 63){ 00432 j = s->scantable.permutated[63]; 00433 block[j] = level * quant_matrix[j]; 00434 break; 00435 } 00436 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); 00437 return -1; 00438 } 00439 j = s->scantable.permutated[i]; 00440 block[j] = level * quant_matrix[j]; 00441 } 00442 } 00443 CLOSE_READER(re, &s->gb)} 00444 00445 return 0; 00446 } 00447 00448 static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int component, 00449 int dc_index, int16_t *quant_matrix, int Al) 00450 { 00451 int val; 00452 s->dsp.clear_block(block); 00453 val = mjpeg_decode_dc(s, dc_index); 00454 if (val == 0xffff) { 00455 av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); 00456 return -1; 00457 } 00458 val = (val * quant_matrix[0] << Al) + s->last_dc[component]; 00459 s->last_dc[component] = val; 00460 block[0] = val; 00461 return 0; 00462 } 00463 00464 /* decode block and dequantize - progressive JPEG version */ 00465 static int decode_block_progressive(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz, 00466 int ac_index, int16_t *quant_matrix, 00467 int ss, int se, int Al, int *EOBRUN) 00468 { 00469 int code, i, j, level, val, run; 00470 00471 if(*EOBRUN){ 00472 (*EOBRUN)--; 00473 return 0; 00474 } 00475 {OPEN_READER(re, &s->gb) 00476 for(i=ss;;i++) { 00477 UPDATE_CACHE(re, &s->gb); 00478 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) 00479 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */ 00480 code -= 16; 00481 if(code & 0xF) { 00482 i += ((unsigned) code) >> 4; 00483 code &= 0xf; 00484 if(code > MIN_CACHE_BITS - 16){ 00485 UPDATE_CACHE(re, &s->gb) 00486 } 00487 { 00488 int cache=GET_CACHE(re,&s->gb); 00489 int sign=(~cache)>>31; 00490 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; 00491 } 00492 00493 LAST_SKIP_BITS(re, &s->gb, code) 00494 00495 if (i >= se) { 00496 if(i == se){ 00497 j = s->scantable.permutated[se]; 00498 block[j] = level * quant_matrix[j] << Al; 00499 break; 00500 } 00501 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); 00502 return -1; 00503 } 00504 j = s->scantable.permutated[i]; 00505 block[j] = level * quant_matrix[j] << Al; 00506 }else{ 00507 run = ((unsigned) code) >> 4; 00508 if(run == 0xF){// ZRL - skip 15 coefficients 00509 i += 15; 00510 }else{ 00511 val = run; 00512 run = (1 << run); 00513 UPDATE_CACHE(re, &s->gb); 00514 run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1); 00515 if(val) 00516 LAST_SKIP_BITS(re, &s->gb, val); 00517 *EOBRUN = run - 1; 00518 break; 00519 } 00520 } 00521 } 00522 CLOSE_READER(re, &s->gb)} 00523 if(i > *last_nnz) 00524 *last_nnz = i; 00525 return 0; 00526 } 00527 00528 #define REFINE_BIT(j) {\ 00529 UPDATE_CACHE(re, &s->gb);\ 00530 sign = block[j]>>15;\ 00531 block[j] += SHOW_UBITS(re, &s->gb, 1) * ((quant_matrix[j]^sign)-sign) << Al;\ 00532 LAST_SKIP_BITS(re, &s->gb, 1);\ 00533 } 00534 00535 #define ZERO_RUN \ 00536 for(;;i++) {\ 00537 if(i > last) {\ 00538 i += run;\ 00539 if(i > se) {\ 00540 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i);\ 00541 return -1;\ 00542 }\ 00543 break;\ 00544 }\ 00545 j = s->scantable.permutated[i];\ 00546 if(block[j])\ 00547 REFINE_BIT(j)\ 00548 else if(run-- == 0)\ 00549 break;\ 00550 } 00551 00552 /* decode block and dequantize - progressive JPEG refinement pass */ 00553 static int decode_block_refinement(MJpegDecodeContext *s, DCTELEM *block, uint8_t *last_nnz, 00554 int ac_index, int16_t *quant_matrix, 00555 int ss, int se, int Al, int *EOBRUN) 00556 { 00557 int code, i=ss, j, sign, val, run; 00558 int last = FFMIN(se, *last_nnz); 00559 00560 OPEN_READER(re, &s->gb); 00561 if(*EOBRUN) 00562 (*EOBRUN)--; 00563 else { 00564 for(;;i++) { 00565 UPDATE_CACHE(re, &s->gb); 00566 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) 00567 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset 16 by default */ 00568 code -= 16; 00569 if(code & 0xF) { 00570 run = ((unsigned) code) >> 4; 00571 UPDATE_CACHE(re, &s->gb); 00572 val = SHOW_UBITS(re, &s->gb, 1); 00573 LAST_SKIP_BITS(re, &s->gb, 1); 00574 ZERO_RUN; 00575 j = s->scantable.permutated[i]; 00576 val--; 00577 block[j] = ((quant_matrix[j]^val)-val) << Al; 00578 if(i == se) { 00579 if(i > *last_nnz) 00580 *last_nnz = i; 00581 CLOSE_READER(re, &s->gb) 00582 return 0; 00583 } 00584 }else{ 00585 run = ((unsigned) code) >> 4; 00586 if(run == 0xF){ 00587 ZERO_RUN; 00588 }else{ 00589 val = run; 00590 run = (1 << run); 00591 if(val) { 00592 UPDATE_CACHE(re, &s->gb); 00593 run += SHOW_UBITS(re, &s->gb, val); 00594 LAST_SKIP_BITS(re, &s->gb, val); 00595 } 00596 *EOBRUN = run - 1; 00597 break; 00598 } 00599 } 00600 } 00601 00602 if(i > *last_nnz) 00603 *last_nnz = i; 00604 } 00605 00606 for(;i<=last;i++) { 00607 j = s->scantable.permutated[i]; 00608 if(block[j]) 00609 REFINE_BIT(j) 00610 } 00611 CLOSE_READER(re, &s->gb); 00612 00613 return 0; 00614 } 00615 #undef REFINE_BIT 00616 #undef ZERO_RUN 00617 00618 static int ljpeg_decode_rgb_scan(MJpegDecodeContext *s, int predictor, int point_transform){ 00619 int i, mb_x, mb_y; 00620 uint16_t (*buffer)[4]; 00621 int left[3], top[3], topleft[3]; 00622 const int linesize= s->linesize[0]; 00623 const int mask= (1<<s->bits)-1; 00624 00625 av_fast_malloc(&s->ljpeg_buffer, &s->ljpeg_buffer_size, (unsigned)s->mb_width * 4 * sizeof(s->ljpeg_buffer[0][0])); 00626 buffer= s->ljpeg_buffer; 00627 00628 for(i=0; i<3; i++){ 00629 buffer[0][i]= 1 << (s->bits + point_transform - 1); 00630 } 00631 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { 00632 const int modified_predictor= mb_y ? predictor : 1; 00633 uint8_t *ptr = s->picture.data[0] + (linesize * mb_y); 00634 00635 if (s->interlaced && s->bottom_field) 00636 ptr += linesize >> 1; 00637 00638 for(i=0; i<3; i++){ 00639 top[i]= left[i]= topleft[i]= buffer[0][i]; 00640 } 00641 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { 00642 if (s->restart_interval && !s->restart_count) 00643 s->restart_count = s->restart_interval; 00644 00645 for(i=0;i<3;i++) { 00646 int pred; 00647 00648 topleft[i]= top[i]; 00649 top[i]= buffer[mb_x][i]; 00650 00651 PREDICT(pred, topleft[i], top[i], left[i], modified_predictor); 00652 00653 left[i]= 00654 buffer[mb_x][i]= mask & (pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform)); 00655 } 00656 00657 if (s->restart_interval && !--s->restart_count) { 00658 align_get_bits(&s->gb); 00659 skip_bits(&s->gb, 16); /* skip RSTn */ 00660 } 00661 } 00662 00663 if(s->rct){ 00664 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { 00665 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2] - 0x200)>>2); 00666 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; 00667 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; 00668 } 00669 }else if(s->pegasus_rct){ 00670 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { 00671 ptr[4*mb_x+1] = buffer[mb_x][0] - ((buffer[mb_x][1] + buffer[mb_x][2])>>2); 00672 ptr[4*mb_x+0] = buffer[mb_x][1] + ptr[4*mb_x+1]; 00673 ptr[4*mb_x+2] = buffer[mb_x][2] + ptr[4*mb_x+1]; 00674 } 00675 }else{ 00676 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { 00677 ptr[4*mb_x+0] = buffer[mb_x][2]; 00678 ptr[4*mb_x+1] = buffer[mb_x][1]; 00679 ptr[4*mb_x+2] = buffer[mb_x][0]; 00680 } 00681 } 00682 } 00683 return 0; 00684 } 00685 00686 static int ljpeg_decode_yuv_scan(MJpegDecodeContext *s, int predictor, int point_transform){ 00687 int i, mb_x, mb_y; 00688 const int nb_components=3; 00689 00690 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { 00691 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { 00692 if (s->restart_interval && !s->restart_count) 00693 s->restart_count = s->restart_interval; 00694 00695 if(mb_x==0 || mb_y==0 || s->interlaced){ 00696 for(i=0;i<nb_components;i++) { 00697 uint8_t *ptr; 00698 int n, h, v, x, y, c, j, linesize; 00699 n = s->nb_blocks[i]; 00700 c = s->comp_index[i]; 00701 h = s->h_scount[i]; 00702 v = s->v_scount[i]; 00703 x = 0; 00704 y = 0; 00705 linesize= s->linesize[c]; 00706 00707 for(j=0; j<n; j++) { 00708 int pred; 00709 00710 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap 00711 if(y==0 && mb_y==0){ 00712 if(x==0 && mb_x==0){ 00713 pred= 128 << point_transform; 00714 }else{ 00715 pred= ptr[-1]; 00716 } 00717 }else{ 00718 if(x==0 && mb_x==0){ 00719 pred= ptr[-linesize]; 00720 }else{ 00721 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); 00722 } 00723 } 00724 00725 if (s->interlaced && s->bottom_field) 00726 ptr += linesize >> 1; 00727 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); 00728 00729 if (++x == h) { 00730 x = 0; 00731 y++; 00732 } 00733 } 00734 } 00735 }else{ 00736 for(i=0;i<nb_components;i++) { 00737 uint8_t *ptr; 00738 int n, h, v, x, y, c, j, linesize; 00739 n = s->nb_blocks[i]; 00740 c = s->comp_index[i]; 00741 h = s->h_scount[i]; 00742 v = s->v_scount[i]; 00743 x = 0; 00744 y = 0; 00745 linesize= s->linesize[c]; 00746 00747 for(j=0; j<n; j++) { 00748 int pred; 00749 00750 ptr = s->picture.data[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap 00751 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor); 00752 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform); 00753 if (++x == h) { 00754 x = 0; 00755 y++; 00756 } 00757 } 00758 } 00759 } 00760 if (s->restart_interval && !--s->restart_count) { 00761 align_get_bits(&s->gb); 00762 skip_bits(&s->gb, 16); /* skip RSTn */ 00763 } 00764 } 00765 } 00766 return 0; 00767 } 00768 00769 static int mjpeg_decode_scan(MJpegDecodeContext *s, int nb_components, int Ah, int Al){ 00770 int i, mb_x, mb_y; 00771 uint8_t* data[MAX_COMPONENTS]; 00772 int linesize[MAX_COMPONENTS]; 00773 00774 if(s->flipped && s->avctx->flags & CODEC_FLAG_EMU_EDGE) { 00775 av_log(s->avctx, AV_LOG_ERROR, "Can not flip image with CODEC_FLAG_EMU_EDGE set!\n"); 00776 s->flipped = 0; 00777 } 00778 for(i=0; i < nb_components; i++) { 00779 int c = s->comp_index[i]; 00780 data[c] = s->picture.data[c]; 00781 linesize[c]=s->linesize[c]; 00782 s->coefs_finished[c] |= 1; 00783 if(s->flipped) { 00784 //picture should be flipped upside-down for this codec 00785 data[c] += (linesize[c] * (s->v_scount[i] * (8 * s->mb_height -((s->height/s->v_max)&7)) - 1 )); 00786 linesize[c] *= -1; 00787 } 00788 } 00789 00790 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { 00791 for(mb_x = 0; mb_x < s->mb_width; mb_x++) { 00792 if (s->restart_interval && !s->restart_count) 00793 s->restart_count = s->restart_interval; 00794 00795 for(i=0;i<nb_components;i++) { 00796 uint8_t *ptr; 00797 int n, h, v, x, y, c, j; 00798 n = s->nb_blocks[i]; 00799 c = s->comp_index[i]; 00800 h = s->h_scount[i]; 00801 v = s->v_scount[i]; 00802 x = 0; 00803 y = 0; 00804 for(j=0;j<n;j++) { 00805 ptr = data[c] + 00806 (((linesize[c] * (v * mb_y + y) * 8) + 00807 (h * mb_x + x) * 8) >> s->avctx->lowres); 00808 if(s->interlaced && s->bottom_field) 00809 ptr += linesize[c] >> 1; 00810 if(!s->progressive) { 00811 s->dsp.clear_block(s->block); 00812 if(decode_block(s, s->block, i, 00813 s->dc_index[i], s->ac_index[i], 00814 s->quant_matrixes[ s->quant_index[c] ]) < 0) { 00815 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); 00816 return -1; 00817 } 00818 s->dsp.idct_put(ptr, linesize[c], s->block); 00819 } else { 00820 int block_idx = s->block_stride[c] * (v * mb_y + y) + (h * mb_x + x); 00821 DCTELEM *block = s->blocks[c][block_idx]; 00822 if(Ah) 00823 block[0] += get_bits1(&s->gb) * s->quant_matrixes[ s->quant_index[c] ][0] << Al; 00824 else if(decode_dc_progressive(s, block, i, s->dc_index[i], s->quant_matrixes[ s->quant_index[c] ], Al) < 0) { 00825 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); 00826 return -1; 00827 } 00828 } 00829 // av_log(s->avctx, AV_LOG_DEBUG, "mb: %d %d processed\n", mb_y, mb_x); 00830 //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); 00831 if (++x == h) { 00832 x = 0; 00833 y++; 00834 } 00835 } 00836 } 00837 00838 if (s->restart_interval && !--s->restart_count) { 00839 align_get_bits(&s->gb); 00840 skip_bits(&s->gb, 16); /* skip RSTn */ 00841 for (i=0; i<nb_components; i++) /* reset dc */ 00842 s->last_dc[i] = 1024; 00843 } 00844 } 00845 } 00846 return 0; 00847 } 00848 00849 static int mjpeg_decode_scan_progressive_ac(MJpegDecodeContext *s, int ss, int se, int Ah, int Al){ 00850 int mb_x, mb_y; 00851 int EOBRUN = 0; 00852 int c = s->comp_index[0]; 00853 uint8_t* data = s->picture.data[c]; 00854 int linesize = s->linesize[c]; 00855 int last_scan = 0; 00856 int16_t *quant_matrix = s->quant_matrixes[ s->quant_index[c] ]; 00857 00858 if(!Al) { 00859 s->coefs_finished[c] |= (1LL<<(se+1))-(1LL<<ss); 00860 last_scan = !~s->coefs_finished[c]; 00861 } 00862 00863 if(s->interlaced && s->bottom_field) 00864 data += linesize >> 1; 00865 00866 for(mb_y = 0; mb_y < s->mb_height; mb_y++) { 00867 uint8_t *ptr = data + (mb_y*linesize*8 >> s->avctx->lowres); 00868 int block_idx = mb_y * s->block_stride[c]; 00869 DCTELEM (*block)[64] = &s->blocks[c][block_idx]; 00870 uint8_t *last_nnz = &s->last_nnz[c][block_idx]; 00871 for(mb_x = 0; mb_x < s->mb_width; mb_x++, block++, last_nnz++) { 00872 int ret; 00873 if(Ah) 00874 ret = decode_block_refinement(s, *block, last_nnz, s->ac_index[0], 00875 quant_matrix, ss, se, Al, &EOBRUN); 00876 else 00877 ret = decode_block_progressive(s, *block, last_nnz, s->ac_index[0], 00878 quant_matrix, ss, se, Al, &EOBRUN); 00879 if(ret < 0) { 00880 av_log(s->avctx, AV_LOG_ERROR, "error y=%d x=%d\n", mb_y, mb_x); 00881 return -1; 00882 } 00883 if(last_scan) { 00884 s->dsp.idct_put(ptr, linesize, *block); 00885 ptr += 8 >> s->avctx->lowres; 00886 } 00887 } 00888 } 00889 return 0; 00890 } 00891 00892 int ff_mjpeg_decode_sos(MJpegDecodeContext *s) 00893 { 00894 int len, nb_components, i, h, v, predictor, point_transform; 00895 int index, id; 00896 const int block_size= s->lossless ? 1 : 8; 00897 int ilv, prev_shift; 00898 00899 /* XXX: verify len field validity */ 00900 len = get_bits(&s->gb, 16); 00901 nb_components = get_bits(&s->gb, 8); 00902 if (nb_components == 0 || nb_components > MAX_COMPONENTS){ 00903 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: nb_components (%d) unsupported\n", nb_components); 00904 return -1; 00905 } 00906 if (len != 6+2*nb_components) 00907 { 00908 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: invalid len (%d)\n", len); 00909 return -1; 00910 } 00911 for(i=0;i<nb_components;i++) { 00912 id = get_bits(&s->gb, 8) - 1; 00913 av_log(s->avctx, AV_LOG_DEBUG, "component: %d\n", id); 00914 /* find component index */ 00915 for(index=0;index<s->nb_components;index++) 00916 if (id == s->component_id[index]) 00917 break; 00918 if (index == s->nb_components) 00919 { 00920 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: index(%d) out of components\n", index); 00921 return -1; 00922 } 00923 /* Metasoft MJPEG codec has Cb and Cr swapped */ 00924 if (s->avctx->codec_tag == MKTAG('M', 'T', 'S', 'J') 00925 && nb_components == 3 && s->nb_components == 3 && i) 00926 index = 3 - i; 00927 00928 s->comp_index[i] = index; 00929 00930 s->nb_blocks[i] = s->h_count[index] * s->v_count[index]; 00931 s->h_scount[i] = s->h_count[index]; 00932 s->v_scount[i] = s->v_count[index]; 00933 00934 s->dc_index[i] = get_bits(&s->gb, 4); 00935 s->ac_index[i] = get_bits(&s->gb, 4); 00936 00937 if (s->dc_index[i] < 0 || s->ac_index[i] < 0 || 00938 s->dc_index[i] >= 4 || s->ac_index[i] >= 4) 00939 goto out_of_range; 00940 if (!s->vlcs[0][s->dc_index[i]].table || !s->vlcs[1][s->ac_index[i]].table) 00941 goto out_of_range; 00942 } 00943 00944 predictor= get_bits(&s->gb, 8); /* JPEG Ss / lossless JPEG predictor /JPEG-LS NEAR */ 00945 ilv= get_bits(&s->gb, 8); /* JPEG Se / JPEG-LS ILV */ 00946 prev_shift = get_bits(&s->gb, 4); /* Ah */ 00947 point_transform= get_bits(&s->gb, 4); /* Al */ 00948 00949 for(i=0;i<nb_components;i++) 00950 s->last_dc[i] = 1024; 00951 00952 if (nb_components > 1) { 00953 /* interleaved stream */ 00954 s->mb_width = (s->width + s->h_max * block_size - 1) / (s->h_max * block_size); 00955 s->mb_height = (s->height + s->v_max * block_size - 1) / (s->v_max * block_size); 00956 } else if(!s->ls) { /* skip this for JPEG-LS */ 00957 h = s->h_max / s->h_scount[0]; 00958 v = s->v_max / s->v_scount[0]; 00959 s->mb_width = (s->width + h * block_size - 1) / (h * block_size); 00960 s->mb_height = (s->height + v * block_size - 1) / (v * block_size); 00961 s->nb_blocks[0] = 1; 00962 s->h_scount[0] = 1; 00963 s->v_scount[0] = 1; 00964 } 00965 00966 if(s->avctx->debug & FF_DEBUG_PICT_INFO) 00967 av_log(s->avctx, AV_LOG_DEBUG, "%s %s p:%d >>:%d ilv:%d bits:%d %s\n", s->lossless ? "lossless" : "sequential DCT", s->rgb ? "RGB" : "", 00968 predictor, point_transform, ilv, s->bits, 00969 s->pegasus_rct ? "PRCT" : (s->rct ? "RCT" : "")); 00970 00971 00972 /* mjpeg-b can have padding bytes between sos and image data, skip them */ 00973 for (i = s->mjpb_skiptosod; i > 0; i--) 00974 skip_bits(&s->gb, 8); 00975 00976 if(s->lossless){ 00977 if(CONFIG_JPEGLS_DECODER && s->ls){ 00978 // for(){ 00979 // reset_ls_coding_parameters(s, 0); 00980 00981 if(ff_jpegls_decode_picture(s, predictor, point_transform, ilv) < 0) 00982 return -1; 00983 }else{ 00984 if(s->rgb){ 00985 if(ljpeg_decode_rgb_scan(s, predictor, point_transform) < 0) 00986 return -1; 00987 }else{ 00988 if(ljpeg_decode_yuv_scan(s, predictor, point_transform) < 0) 00989 return -1; 00990 } 00991 } 00992 }else{ 00993 if(s->progressive && predictor) { 00994 if(mjpeg_decode_scan_progressive_ac(s, predictor, ilv, prev_shift, point_transform) < 0) 00995 return -1; 00996 } else { 00997 if(mjpeg_decode_scan(s, nb_components, prev_shift, point_transform) < 0) 00998 return -1; 00999 } 01000 } 01001 emms_c(); 01002 return 0; 01003 out_of_range: 01004 av_log(s->avctx, AV_LOG_ERROR, "decode_sos: ac/dc index out of range\n"); 01005 return -1; 01006 } 01007 01008 static int mjpeg_decode_dri(MJpegDecodeContext *s) 01009 { 01010 if (get_bits(&s->gb, 16) != 4) 01011 return -1; 01012 s->restart_interval = get_bits(&s->gb, 16); 01013 s->restart_count = 0; 01014 av_log(s->avctx, AV_LOG_DEBUG, "restart interval: %d\n", s->restart_interval); 01015 01016 return 0; 01017 } 01018 01019 static int mjpeg_decode_app(MJpegDecodeContext *s) 01020 { 01021 int len, id, i; 01022 01023 len = get_bits(&s->gb, 16); 01024 if (len < 5) 01025 return -1; 01026 if(8*len + get_bits_count(&s->gb) > s->gb.size_in_bits) 01027 return -1; 01028 01029 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); 01030 id = be2me_32(id); 01031 len -= 6; 01032 01033 if(s->avctx->debug & FF_DEBUG_STARTCODE){ 01034 av_log(s->avctx, AV_LOG_DEBUG, "APPx %8X\n", id); 01035 } 01036 01037 /* buggy AVID, it puts EOI only at every 10th frame */ 01038 /* also this fourcc is used by non-avid files too, it holds some 01039 informations, but it's always present in AVID creates files */ 01040 if (id == AV_RL32("AVI1")) 01041 { 01042 /* structure: 01043 4bytes AVI1 01044 1bytes polarity 01045 1bytes always zero 01046 4bytes field_size 01047 4bytes field_size_less_padding 01048 */ 01049 s->buggy_avid = 1; 01050 // if (s->first_picture) 01051 // printf("mjpeg: workarounding buggy AVID\n"); 01052 i = get_bits(&s->gb, 8); 01053 if (i==2) s->bottom_field= 1; 01054 else if(i==1) s->bottom_field= 0; 01055 #if 0 01056 skip_bits(&s->gb, 8); 01057 skip_bits(&s->gb, 32); 01058 skip_bits(&s->gb, 32); 01059 len -= 10; 01060 #endif 01061 // if (s->interlace_polarity) 01062 // printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity); 01063 goto out; 01064 } 01065 01066 // len -= 2; 01067 01068 if (id == AV_RL32("JFIF")) 01069 { 01070 int t_w, t_h, v1, v2; 01071 skip_bits(&s->gb, 8); /* the trailing zero-byte */ 01072 v1= get_bits(&s->gb, 8); 01073 v2= get_bits(&s->gb, 8); 01074 skip_bits(&s->gb, 8); 01075 01076 s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 16); 01077 s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 16); 01078 01079 if (s->avctx->debug & FF_DEBUG_PICT_INFO) 01080 av_log(s->avctx, AV_LOG_INFO, "mjpeg: JFIF header found (version: %x.%x) SAR=%d/%d\n", 01081 v1, v2, 01082 s->avctx->sample_aspect_ratio.num, 01083 s->avctx->sample_aspect_ratio.den 01084 ); 01085 01086 t_w = get_bits(&s->gb, 8); 01087 t_h = get_bits(&s->gb, 8); 01088 if (t_w && t_h) 01089 { 01090 /* skip thumbnail */ 01091 if (len-10-(t_w*t_h*3) > 0) 01092 len -= t_w*t_h*3; 01093 } 01094 len -= 10; 01095 goto out; 01096 } 01097 01098 if (id == AV_RL32("Adob") && (get_bits(&s->gb, 8) == 'e')) 01099 { 01100 if (s->avctx->debug & FF_DEBUG_PICT_INFO) 01101 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Adobe header found\n"); 01102 skip_bits(&s->gb, 16); /* version */ 01103 skip_bits(&s->gb, 16); /* flags0 */ 01104 skip_bits(&s->gb, 16); /* flags1 */ 01105 skip_bits(&s->gb, 8); /* transform */ 01106 len -= 7; 01107 goto out; 01108 } 01109 01110 if (id == AV_RL32("LJIF")){ 01111 if (s->avctx->debug & FF_DEBUG_PICT_INFO) 01112 av_log(s->avctx, AV_LOG_INFO, "Pegasus lossless jpeg header found\n"); 01113 skip_bits(&s->gb, 16); /* version ? */ 01114 skip_bits(&s->gb, 16); /* unknwon always 0? */ 01115 skip_bits(&s->gb, 16); /* unknwon always 0? */ 01116 skip_bits(&s->gb, 16); /* unknwon always 0? */ 01117 switch( get_bits(&s->gb, 8)){ 01118 case 1: 01119 s->rgb= 1; 01120 s->pegasus_rct=0; 01121 break; 01122 case 2: 01123 s->rgb= 1; 01124 s->pegasus_rct=1; 01125 break; 01126 default: 01127 av_log(s->avctx, AV_LOG_ERROR, "unknown colorspace\n"); 01128 } 01129 len -= 9; 01130 goto out; 01131 } 01132 01133 /* Apple MJPEG-A */ 01134 if ((s->start_code == APP1) && (len > (0x28 - 8))) 01135 { 01136 id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); 01137 id = be2me_32(id); 01138 len -= 4; 01139 if (id == AV_RL32("mjpg")) /* Apple MJPEG-A */ 01140 { 01141 #if 0 01142 skip_bits(&s->gb, 32); /* field size */ 01143 skip_bits(&s->gb, 32); /* pad field size */ 01144 skip_bits(&s->gb, 32); /* next off */ 01145 skip_bits(&s->gb, 32); /* quant off */ 01146 skip_bits(&s->gb, 32); /* huff off */ 01147 skip_bits(&s->gb, 32); /* image off */ 01148 skip_bits(&s->gb, 32); /* scan off */ 01149 skip_bits(&s->gb, 32); /* data off */ 01150 #endif 01151 if (s->avctx->debug & FF_DEBUG_PICT_INFO) 01152 av_log(s->avctx, AV_LOG_INFO, "mjpeg: Apple MJPEG-A header found\n"); 01153 } 01154 } 01155 01156 out: 01157 /* slow but needed for extreme adobe jpegs */ 01158 if (len < 0) 01159 av_log(s->avctx, AV_LOG_ERROR, "mjpeg: error, decode_app parser read over the end\n"); 01160 while(--len > 0) 01161 skip_bits(&s->gb, 8); 01162 01163 return 0; 01164 } 01165 01166 static int mjpeg_decode_com(MJpegDecodeContext *s) 01167 { 01168 int len = get_bits(&s->gb, 16); 01169 if (len >= 2 && 8*len - 16 + get_bits_count(&s->gb) <= s->gb.size_in_bits) { 01170 char *cbuf = av_malloc(len - 1); 01171 if (cbuf) { 01172 int i; 01173 for (i = 0; i < len - 2; i++) 01174 cbuf[i] = get_bits(&s->gb, 8); 01175 if (i > 0 && cbuf[i-1] == '\n') 01176 cbuf[i-1] = 0; 01177 else 01178 cbuf[i] = 0; 01179 01180 if(s->avctx->debug & FF_DEBUG_PICT_INFO) 01181 av_log(s->avctx, AV_LOG_INFO, "mjpeg comment: '%s'\n", cbuf); 01182 01183 /* buggy avid, it puts EOI only at every 10th frame */ 01184 if (!strcmp(cbuf, "AVID")) 01185 { 01186 s->buggy_avid = 1; 01187 // if (s->first_picture) 01188 // printf("mjpeg: workarounding buggy AVID\n"); 01189 } 01190 else if(!strcmp(cbuf, "CS=ITU601")){ 01191 s->cs_itu601= 1; 01192 } 01193 else if((len > 20 && !strncmp(cbuf, "Intel(R) JPEG Library", 21)) || 01194 (len > 19 && !strncmp(cbuf, "Metasoft MJPEG Codec", 20))){ 01195 s->flipped = 1; 01196 } 01197 01198 av_free(cbuf); 01199 } 01200 } 01201 01202 return 0; 01203 } 01204 01205 #if 0 01206 static int valid_marker_list[] = 01207 { 01208 /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f */ 01209 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01210 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01211 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01212 /* 3 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01213 /* 4 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01214 /* 5 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01215 /* 6 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01216 /* 7 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01217 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01218 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01219 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01220 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 01221 /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 01222 /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 01223 /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 01224 /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 01225 } 01226 #endif 01227 01228 /* return the 8 bit start code value and update the search 01229 state. Return -1 if no start code found */ 01230 static int find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end) 01231 { 01232 const uint8_t *buf_ptr; 01233 unsigned int v, v2; 01234 int val; 01235 #ifdef DEBUG 01236 int skipped=0; 01237 #endif 01238 01239 buf_ptr = *pbuf_ptr; 01240 while (buf_ptr < buf_end) { 01241 v = *buf_ptr++; 01242 v2 = *buf_ptr; 01243 if ((v == 0xff) && (v2 >= 0xc0) && (v2 <= 0xfe) && buf_ptr < buf_end) { 01244 val = *buf_ptr++; 01245 goto found; 01246 } 01247 #ifdef DEBUG 01248 skipped++; 01249 #endif 01250 } 01251 val = -1; 01252 found: 01253 dprintf(NULL, "find_marker skipped %d bytes\n", skipped); 01254 *pbuf_ptr = buf_ptr; 01255 return val; 01256 } 01257 01258 int ff_mjpeg_decode_frame(AVCodecContext *avctx, 01259 void *data, int *data_size, 01260 AVPacket *avpkt) 01261 { 01262 const uint8_t *buf = avpkt->data; 01263 int buf_size = avpkt->size; 01264 MJpegDecodeContext *s = avctx->priv_data; 01265 const uint8_t *buf_end, *buf_ptr; 01266 int start_code; 01267 AVFrame *picture = data; 01268 01269 s->got_picture = 0; // picture from previous image can not be reused 01270 buf_ptr = buf; 01271 buf_end = buf + buf_size; 01272 while (buf_ptr < buf_end) { 01273 /* find start next marker */ 01274 start_code = find_marker(&buf_ptr, buf_end); 01275 { 01276 /* EOF */ 01277 if (start_code < 0) { 01278 goto the_end; 01279 } else { 01280 av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%td\n", start_code, buf_end - buf_ptr); 01281 01282 if ((buf_end - buf_ptr) > s->buffer_size) 01283 { 01284 av_free(s->buffer); 01285 s->buffer_size = buf_end-buf_ptr; 01286 s->buffer = av_malloc(s->buffer_size + FF_INPUT_BUFFER_PADDING_SIZE); 01287 av_log(avctx, AV_LOG_DEBUG, "buffer too small, expanding to %d bytes\n", 01288 s->buffer_size); 01289 } 01290 01291 /* unescape buffer of SOS, use special treatment for JPEG-LS */ 01292 if (start_code == SOS && !s->ls) 01293 { 01294 const uint8_t *src = buf_ptr; 01295 uint8_t *dst = s->buffer; 01296 01297 while (src<buf_end) 01298 { 01299 uint8_t x = *(src++); 01300 01301 *(dst++) = x; 01302 if (avctx->codec_id != CODEC_ID_THP) 01303 { 01304 if (x == 0xff) { 01305 while (src < buf_end && x == 0xff) 01306 x = *(src++); 01307 01308 if (x >= 0xd0 && x <= 0xd7) 01309 *(dst++) = x; 01310 else if (x) 01311 break; 01312 } 01313 } 01314 } 01315 init_get_bits(&s->gb, s->buffer, (dst - s->buffer)*8); 01316 01317 av_log(avctx, AV_LOG_DEBUG, "escaping removed %td bytes\n", 01318 (buf_end - buf_ptr) - (dst - s->buffer)); 01319 } 01320 else if(start_code == SOS && s->ls){ 01321 const uint8_t *src = buf_ptr; 01322 uint8_t *dst = s->buffer; 01323 int bit_count = 0; 01324 int t = 0, b = 0; 01325 PutBitContext pb; 01326 01327 s->cur_scan++; 01328 01329 /* find marker */ 01330 while (src + t < buf_end){ 01331 uint8_t x = src[t++]; 01332 if (x == 0xff){ 01333 while((src + t < buf_end) && x == 0xff) 01334 x = src[t++]; 01335 if (x & 0x80) { 01336 t -= 2; 01337 break; 01338 } 01339 } 01340 } 01341 bit_count = t * 8; 01342 01343 init_put_bits(&pb, dst, t); 01344 01345 /* unescape bitstream */ 01346 while(b < t){ 01347 uint8_t x = src[b++]; 01348 put_bits(&pb, 8, x); 01349 if(x == 0xFF){ 01350 x = src[b++]; 01351 put_bits(&pb, 7, x); 01352 bit_count--; 01353 } 01354 } 01355 flush_put_bits(&pb); 01356 01357 init_get_bits(&s->gb, dst, bit_count); 01358 } 01359 else 01360 init_get_bits(&s->gb, buf_ptr, (buf_end - buf_ptr)*8); 01361 01362 s->start_code = start_code; 01363 if(s->avctx->debug & FF_DEBUG_STARTCODE){ 01364 av_log(avctx, AV_LOG_DEBUG, "startcode: %X\n", start_code); 01365 } 01366 01367 /* process markers */ 01368 if (start_code >= 0xd0 && start_code <= 0xd7) { 01369 av_log(avctx, AV_LOG_DEBUG, "restart marker: %d\n", start_code&0x0f); 01370 /* APP fields */ 01371 } else if (start_code >= APP0 && start_code <= APP15) { 01372 mjpeg_decode_app(s); 01373 /* Comment */ 01374 } else if (start_code == COM){ 01375 mjpeg_decode_com(s); 01376 } 01377 01378 switch(start_code) { 01379 case SOI: 01380 s->restart_interval = 0; 01381 01382 s->restart_count = 0; 01383 /* nothing to do on SOI */ 01384 break; 01385 case DQT: 01386 ff_mjpeg_decode_dqt(s); 01387 break; 01388 case DHT: 01389 if(ff_mjpeg_decode_dht(s) < 0){ 01390 av_log(avctx, AV_LOG_ERROR, "huffman table decode error\n"); 01391 return -1; 01392 } 01393 break; 01394 case SOF0: 01395 case SOF1: 01396 s->lossless=0; 01397 s->ls=0; 01398 s->progressive=0; 01399 if (ff_mjpeg_decode_sof(s) < 0) 01400 return -1; 01401 break; 01402 case SOF2: 01403 s->lossless=0; 01404 s->ls=0; 01405 s->progressive=1; 01406 if (ff_mjpeg_decode_sof(s) < 0) 01407 return -1; 01408 break; 01409 case SOF3: 01410 s->lossless=1; 01411 s->ls=0; 01412 s->progressive=0; 01413 if (ff_mjpeg_decode_sof(s) < 0) 01414 return -1; 01415 break; 01416 case SOF48: 01417 s->lossless=1; 01418 s->ls=1; 01419 s->progressive=0; 01420 if (ff_mjpeg_decode_sof(s) < 0) 01421 return -1; 01422 break; 01423 case LSE: 01424 if (!CONFIG_JPEGLS_DECODER || ff_jpegls_decode_lse(s) < 0) 01425 return -1; 01426 break; 01427 case EOI: 01428 s->cur_scan = 0; 01429 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) 01430 break; 01431 eoi_parser: 01432 if (!s->got_picture) { 01433 av_log(avctx, AV_LOG_WARNING, "Found EOI before any SOF, ignoring\n"); 01434 break; 01435 } 01436 { 01437 if (s->interlaced) { 01438 s->bottom_field ^= 1; 01439 /* if not bottom field, do not output image yet */ 01440 if (s->bottom_field == !s->interlace_polarity) 01441 goto not_the_end; 01442 } 01443 *picture = s->picture; 01444 *data_size = sizeof(AVFrame); 01445 01446 if(!s->lossless){ 01447 picture->quality= FFMAX3(s->qscale[0], s->qscale[1], s->qscale[2]); 01448 picture->qstride= 0; 01449 picture->qscale_table= s->qscale_table; 01450 memset(picture->qscale_table, picture->quality, (s->width+15)/16); 01451 if(avctx->debug & FF_DEBUG_QP) 01452 av_log(avctx, AV_LOG_DEBUG, "QP: %d\n", picture->quality); 01453 picture->quality*= FF_QP2LAMBDA; 01454 } 01455 01456 goto the_end; 01457 } 01458 break; 01459 case SOS: 01460 if (!s->got_picture) { 01461 av_log(avctx, AV_LOG_WARNING, "Can not process SOS before SOF, skipping\n"); 01462 break; 01463 } 01464 ff_mjpeg_decode_sos(s); 01465 /* buggy avid puts EOI every 10-20th frame */ 01466 /* if restart period is over process EOI */ 01467 if ((s->buggy_avid && !s->interlaced) || s->restart_interval) 01468 goto eoi_parser; 01469 break; 01470 case DRI: 01471 mjpeg_decode_dri(s); 01472 break; 01473 case SOF5: 01474 case SOF6: 01475 case SOF7: 01476 case SOF9: 01477 case SOF10: 01478 case SOF11: 01479 case SOF13: 01480 case SOF14: 01481 case SOF15: 01482 case JPG: 01483 av_log(avctx, AV_LOG_ERROR, "mjpeg: unsupported coding type (%x)\n", start_code); 01484 break; 01485 // default: 01486 // printf("mjpeg: unsupported marker (%x)\n", start_code); 01487 // break; 01488 } 01489 01490 not_the_end: 01491 /* eof process start code */ 01492 buf_ptr += (get_bits_count(&s->gb)+7)/8; 01493 av_log(avctx, AV_LOG_DEBUG, "marker parser used %d bytes (%d bits)\n", 01494 (get_bits_count(&s->gb)+7)/8, get_bits_count(&s->gb)); 01495 } 01496 } 01497 } 01498 if (s->got_picture) { 01499 av_log(avctx, AV_LOG_WARNING, "EOI missing, emulating\n"); 01500 goto eoi_parser; 01501 } 01502 av_log(avctx, AV_LOG_FATAL, "No JPEG data found in image\n"); 01503 return -1; 01504 the_end: 01505 av_log(avctx, AV_LOG_DEBUG, "mjpeg decode frame unused %td bytes\n", buf_end - buf_ptr); 01506 // return buf_end - buf_ptr; 01507 return buf_ptr - buf; 01508 } 01509 01510 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx) 01511 { 01512 MJpegDecodeContext *s = avctx->priv_data; 01513 int i, j; 01514 01515 if (s->picture.data[0]) 01516 avctx->release_buffer(avctx, &s->picture); 01517 01518 av_free(s->buffer); 01519 av_free(s->qscale_table); 01520 av_freep(&s->ljpeg_buffer); 01521 s->ljpeg_buffer_size=0; 01522 01523 for(i=0;i<2;i++) { 01524 for(j=0;j<4;j++) 01525 free_vlc(&s->vlcs[i][j]); 01526 } 01527 for(i=0; i<MAX_COMPONENTS; i++) { 01528 av_freep(&s->blocks[i]); 01529 av_freep(&s->last_nnz[i]); 01530 } 01531 return 0; 01532 } 01533 01534 AVCodec mjpeg_decoder = { 01535 "mjpeg", 01536 AVMEDIA_TYPE_VIDEO, 01537 CODEC_ID_MJPEG, 01538 sizeof(MJpegDecodeContext), 01539 ff_mjpeg_decode_init, 01540 NULL, 01541 ff_mjpeg_decode_end, 01542 ff_mjpeg_decode_frame, 01543 CODEC_CAP_DR1, 01544 NULL, 01545 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"), 01546 }; 01547 01548 AVCodec thp_decoder = { 01549 "thp", 01550 AVMEDIA_TYPE_VIDEO, 01551 CODEC_ID_THP, 01552 sizeof(MJpegDecodeContext), 01553 ff_mjpeg_decode_init, 01554 NULL, 01555 ff_mjpeg_decode_end, 01556 ff_mjpeg_decode_frame, 01557 CODEC_CAP_DR1, 01558 NULL, 01559 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"), 01560 };