| OLD | NEW |
| 1 /* | 1 /* |
| 2 * MJPEG decoder | 2 * MJPEG decoder |
| 3 * Copyright (c) 2000, 2001 Fabrice Bellard | 3 * Copyright (c) 2000, 2001 Fabrice Bellard |
| 4 * Copyright (c) 2003 Alex Beregszaszi | 4 * Copyright (c) 2003 Alex Beregszaszi |
| 5 * Copyright (c) 2003-2004 Michael Niedermayer | 5 * Copyright (c) 2003-2004 Michael Niedermayer |
| 6 * | 6 * |
| 7 * Support for external huffman table, various fixes (AVID workaround), | 7 * Support for external huffman table, various fixes (AVID workaround), |
| 8 * aspecting, new decode_frame mechanism and apple mjpeg-b support | 8 * aspecting, new decode_frame mechanism and apple mjpeg-b support |
| 9 * by Alex Beregszaszi | 9 * by Alex Beregszaszi |
| 10 * | 10 * |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 #include "avcodec.h" | 37 #include "avcodec.h" |
| 38 #include "dsputil.h" | 38 #include "dsputil.h" |
| 39 #include "mjpeg.h" | 39 #include "mjpeg.h" |
| 40 #include "mjpegdec.h" | 40 #include "mjpegdec.h" |
| 41 #include "jpeglsdec.h" | 41 #include "jpeglsdec.h" |
| 42 | 42 |
| 43 | 43 |
| 44 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_tab
le, | 44 static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_tab
le, |
| 45 int nb_codes, int use_static, int is_ac) | 45 int nb_codes, int use_static, int is_ac) |
| 46 { | 46 { |
| 47 uint8_t huff_size[256+16]; | 47 uint8_t huff_size[256]; |
| 48 uint16_t huff_code[256+16]; | 48 uint16_t huff_code[256]; |
| 49 uint16_t huff_sym[256]; |
| 50 int i; |
| 49 | 51 |
| 50 assert(nb_codes <= 256); | 52 assert(nb_codes <= 256); |
| 51 | 53 |
| 52 memset(huff_size, 0, sizeof(huff_size)); | 54 memset(huff_size, 0, sizeof(huff_size)); |
| 53 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table); | 55 ff_mjpeg_build_huffman_codes(huff_size, huff_code, bits_table, val_table); |
| 54 | 56 |
| 55 if(is_ac){ | 57 for(i=0; i<256; i++) |
| 56 memmove(huff_size+16, huff_size, sizeof(uint8_t)*nb_codes); | 58 huff_sym[i]= i + 16*is_ac; |
| 57 memmove(huff_code+16, huff_code, sizeof(uint16_t)*nb_codes); | |
| 58 memset(huff_size, 0, sizeof(uint8_t)*16); | |
| 59 memset(huff_code, 0, sizeof(uint16_t)*16); | |
| 60 nb_codes += 16; | |
| 61 } | |
| 62 | 59 |
| 63 return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, use_stat
ic); | 60 if(is_ac) huff_sym[0]= 16*256; |
| 61 |
| 62 return init_vlc_sparse(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2, h
uff_sym, 2, 2, use_static); |
| 64 } | 63 } |
| 65 | 64 |
| 66 static void build_basic_mjpeg_vlc(MJpegDecodeContext * s) { | 65 static void build_basic_mjpeg_vlc(MJpegDecodeContext * s) { |
| 67 build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance, | 66 build_vlc(&s->vlcs[0][0], ff_mjpeg_bits_dc_luminance, |
| 68 ff_mjpeg_val_dc, 12, 0, 0); | 67 ff_mjpeg_val_dc, 12, 0, 0); |
| 69 build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance, | 68 build_vlc(&s->vlcs[0][1], ff_mjpeg_bits_dc_chrominance, |
| 70 ff_mjpeg_val_dc, 12, 0, 0); | 69 ff_mjpeg_val_dc, 12, 0, 0); |
| 71 build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance, | 70 build_vlc(&s->vlcs[1][0], ff_mjpeg_bits_ac_luminance, |
| 72 ff_mjpeg_val_ac_luminance, 251, 0, 1); | 71 ff_mjpeg_val_ac_luminance, 251, 0, 1); |
| 73 build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance, | 72 build_vlc(&s->vlcs[1][1], ff_mjpeg_bits_ac_chrominance, |
| 74 ff_mjpeg_val_ac_chrominance, 251, 0, 1); | 73 ff_mjpeg_val_ac_chrominance, 251, 0, 1); |
| 74 build_vlc(&s->vlcs[2][0], ff_mjpeg_bits_ac_luminance, |
| 75 ff_mjpeg_val_ac_luminance, 251, 0, 0); |
| 76 build_vlc(&s->vlcs[2][1], ff_mjpeg_bits_ac_chrominance, |
| 77 ff_mjpeg_val_ac_chrominance, 251, 0, 0); |
| 75 } | 78 } |
| 76 | 79 |
| 77 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx) | 80 av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx) |
| 78 { | 81 { |
| 79 MJpegDecodeContext *s = avctx->priv_data; | 82 MJpegDecodeContext *s = avctx->priv_data; |
| 80 | 83 |
| 81 s->avctx = avctx; | 84 s->avctx = avctx; |
| 82 dsputil_init(&s->dsp, avctx); | 85 dsputil_init(&s->dsp, avctx); |
| 83 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct); | 86 ff_init_scantable(s->dsp.idct_permutation, &s->scantable, ff_zigzag_direct); |
| 84 s->buffer_size = 0; | 87 s->buffer_size = 0; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 } | 187 } |
| 185 len -= n; | 188 len -= n; |
| 186 | 189 |
| 187 /* build VLC and flush previous vlc if present */ | 190 /* build VLC and flush previous vlc if present */ |
| 188 free_vlc(&s->vlcs[class][index]); | 191 free_vlc(&s->vlcs[class][index]); |
| 189 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n", | 192 av_log(s->avctx, AV_LOG_DEBUG, "class=%d index=%d nb_codes=%d\n", |
| 190 class, index, code_max + 1); | 193 class, index, code_max + 1); |
| 191 if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1
, 0, class > 0) < 0){ | 194 if(build_vlc(&s->vlcs[class][index], bits_table, val_table, code_max + 1
, 0, class > 0) < 0){ |
| 192 return -1; | 195 return -1; |
| 193 } | 196 } |
| 197 |
| 198 if(class>0){ |
| 199 free_vlc(&s->vlcs[2][index]); |
| 200 if(build_vlc(&s->vlcs[2][index], bits_table, val_table, code_max + 1
, 0, 0) < 0){ |
| 201 return -1; |
| 202 } |
| 203 } |
| 194 } | 204 } |
| 195 return 0; | 205 return 0; |
| 196 } | 206 } |
| 197 | 207 |
| 198 int ff_mjpeg_decode_sof(MJpegDecodeContext *s) | 208 int ff_mjpeg_decode_sof(MJpegDecodeContext *s) |
| 199 { | 209 { |
| 200 int len, nb_components, i, width, height, pix_fmt_id; | 210 int len, nb_components, i, width, height, pix_fmt_id; |
| 201 | 211 |
| 202 /* XXX: verify len field validity */ | 212 /* XXX: verify len field validity */ |
| 203 len = get_bits(&s->gb, 16); | 213 len = get_bits(&s->gb, 16); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 if (val == 0xffff) { | 410 if (val == 0xffff) { |
| 401 av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); | 411 av_log(s->avctx, AV_LOG_ERROR, "error dc\n"); |
| 402 return -1; | 412 return -1; |
| 403 } | 413 } |
| 404 val = val * quant_matrix[0] + s->last_dc[component]; | 414 val = val * quant_matrix[0] + s->last_dc[component]; |
| 405 s->last_dc[component] = val; | 415 s->last_dc[component] = val; |
| 406 block[0] = val; | 416 block[0] = val; |
| 407 /* AC coefs */ | 417 /* AC coefs */ |
| 408 i = 0; | 418 i = 0; |
| 409 {OPEN_READER(re, &s->gb) | 419 {OPEN_READER(re, &s->gb) |
| 410 for(;;) { | 420 do { |
| 411 UPDATE_CACHE(re, &s->gb); | 421 UPDATE_CACHE(re, &s->gb); |
| 412 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) | 422 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) |
| 413 | 423 |
| 414 /* EOB */ | |
| 415 if (code == 0x10) | |
| 416 break; | |
| 417 i += ((unsigned)code) >> 4; | 424 i += ((unsigned)code) >> 4; |
| 418 if(code != 0x100){ | |
| 419 code &= 0xf; | 425 code &= 0xf; |
| 426 if(code){ |
| 420 if(code > MIN_CACHE_BITS - 16){ | 427 if(code > MIN_CACHE_BITS - 16){ |
| 421 UPDATE_CACHE(re, &s->gb) | 428 UPDATE_CACHE(re, &s->gb) |
| 422 } | 429 } |
| 423 { | 430 { |
| 424 int cache=GET_CACHE(re,&s->gb); | 431 int cache=GET_CACHE(re,&s->gb); |
| 425 int sign=(~cache)>>31; | 432 int sign=(~cache)>>31; |
| 426 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; | 433 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; |
| 427 } | 434 } |
| 428 | 435 |
| 429 LAST_SKIP_BITS(re, &s->gb, code) | 436 LAST_SKIP_BITS(re, &s->gb, code) |
| 430 | 437 |
| 431 if (i >= 63) { | 438 if (i > 63) { |
| 432 if(i == 63){ | |
| 433 j = s->scantable.permutated[63]; | |
| 434 block[j] = level * quant_matrix[j]; | |
| 435 break; | |
| 436 } | |
| 437 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); | 439 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); |
| 438 return -1; | 440 return -1; |
| 439 } | 441 } |
| 440 j = s->scantable.permutated[i]; | 442 j = s->scantable.permutated[i]; |
| 441 block[j] = level * quant_matrix[j]; | 443 block[j] = level * quant_matrix[j]; |
| 442 } | 444 } |
| 443 } | 445 }while(i<63); |
| 444 CLOSE_READER(re, &s->gb)} | 446 CLOSE_READER(re, &s->gb)} |
| 445 | 447 |
| 446 return 0; | 448 return 0; |
| 447 } | 449 } |
| 448 | 450 |
| 449 static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int comp
onent, | 451 static int decode_dc_progressive(MJpegDecodeContext *s, DCTELEM *block, int comp
onent, |
| 450 int dc_index, int16_t *quant_matrix, int Al) | 452 int dc_index, int16_t *quant_matrix, int Al) |
| 451 { | 453 { |
| 452 int val; | 454 int val; |
| 453 s->dsp.clear_block(block); | 455 s->dsp.clear_block(block); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 469 { | 471 { |
| 470 int code, i, j, level, val, run; | 472 int code, i, j, level, val, run; |
| 471 | 473 |
| 472 if(*EOBRUN){ | 474 if(*EOBRUN){ |
| 473 (*EOBRUN)--; | 475 (*EOBRUN)--; |
| 474 return 0; | 476 return 0; |
| 475 } | 477 } |
| 476 {OPEN_READER(re, &s->gb) | 478 {OPEN_READER(re, &s->gb) |
| 477 for(i=ss;;i++) { | 479 for(i=ss;;i++) { |
| 478 UPDATE_CACHE(re, &s->gb); | 480 UPDATE_CACHE(re, &s->gb); |
| 479 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) | 481 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2) |
| 480 /* Progressive JPEG use AC coeffs from zero and this decoder sets offset
16 by default */ | 482 |
| 481 code -= 16; | 483 run = ((unsigned) code) >> 4; |
| 482 if(code & 0xF) { | 484 code &= 0xF; |
| 483 i += ((unsigned) code) >> 4; | 485 if(code) { |
| 484 code &= 0xf; | 486 i += run; |
| 485 if(code > MIN_CACHE_BITS - 16){ | 487 if(code > MIN_CACHE_BITS - 16){ |
| 486 UPDATE_CACHE(re, &s->gb) | 488 UPDATE_CACHE(re, &s->gb) |
| 487 } | 489 } |
| 488 { | 490 { |
| 489 int cache=GET_CACHE(re,&s->gb); | 491 int cache=GET_CACHE(re,&s->gb); |
| 490 int sign=(~cache)>>31; | 492 int sign=(~cache)>>31; |
| 491 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; | 493 level = (NEG_USR32(sign ^ cache,code) ^ sign) - sign; |
| 492 } | 494 } |
| 493 | 495 |
| 494 LAST_SKIP_BITS(re, &s->gb, code) | 496 LAST_SKIP_BITS(re, &s->gb, code) |
| 495 | 497 |
| 496 if (i >= se) { | 498 if (i >= se) { |
| 497 if(i == se){ | 499 if(i == se){ |
| 498 j = s->scantable.permutated[se]; | 500 j = s->scantable.permutated[se]; |
| 499 block[j] = level * quant_matrix[j] << Al; | 501 block[j] = level * quant_matrix[j] << Al; |
| 500 break; | 502 break; |
| 501 } | 503 } |
| 502 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); | 504 av_log(s->avctx, AV_LOG_ERROR, "error count: %d\n", i); |
| 503 return -1; | 505 return -1; |
| 504 } | 506 } |
| 505 j = s->scantable.permutated[i]; | 507 j = s->scantable.permutated[i]; |
| 506 block[j] = level * quant_matrix[j] << Al; | 508 block[j] = level * quant_matrix[j] << Al; |
| 507 }else{ | 509 }else{ |
| 508 run = ((unsigned) code) >> 4; | |
| 509 if(run == 0xF){// ZRL - skip 15 coefficients | 510 if(run == 0xF){// ZRL - skip 15 coefficients |
| 510 i += 15; | 511 i += 15; |
| 512 if (i >= se) { |
| 513 av_log(s->avctx, AV_LOG_ERROR, "ZRL overflow: %d\n", i); |
| 514 return -1; |
| 515 } |
| 511 }else{ | 516 }else{ |
| 512 val = run; | 517 val = (1 << run); |
| 513 run = (1 << run); | 518 if(run){ |
| 514 UPDATE_CACHE(re, &s->gb); | 519 UPDATE_CACHE(re, &s->gb); |
| 515 run += (GET_CACHE(re, &s->gb) >> (32 - val)) & (run - 1); | 520 val += NEG_USR32(GET_CACHE(re, &s->gb), run); |
| 516 if(val) | 521 LAST_SKIP_BITS(re, &s->gb, run); |
| 517 LAST_SKIP_BITS(re, &s->gb, val); | 522 } |
| 518 *EOBRUN = run - 1; | 523 *EOBRUN = val - 1; |
| 519 break; | 524 break; |
| 520 } | 525 } |
| 521 } | 526 } |
| 522 } | 527 } |
| 523 CLOSE_READER(re, &s->gb)} | 528 CLOSE_READER(re, &s->gb)} |
| 524 if(i > *last_nnz) | 529 if(i > *last_nnz) |
| 525 *last_nnz = i; | 530 *last_nnz = i; |
| 526 return 0; | 531 return 0; |
| 527 } | 532 } |
| 528 | 533 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 557 { | 562 { |
| 558 int code, i=ss, j, sign, val, run; | 563 int code, i=ss, j, sign, val, run; |
| 559 int last = FFMIN(se, *last_nnz); | 564 int last = FFMIN(se, *last_nnz); |
| 560 | 565 |
| 561 OPEN_READER(re, &s->gb); | 566 OPEN_READER(re, &s->gb); |
| 562 if(*EOBRUN) | 567 if(*EOBRUN) |
| 563 (*EOBRUN)--; | 568 (*EOBRUN)--; |
| 564 else { | 569 else { |
| 565 for(;;i++) { | 570 for(;;i++) { |
| 566 UPDATE_CACHE(re, &s->gb); | 571 UPDATE_CACHE(re, &s->gb); |
| 567 GET_VLC(code, re, &s->gb, s->vlcs[1][ac_index].table, 9, 2) | 572 GET_VLC(code, re, &s->gb, s->vlcs[2][ac_index].table, 9, 2) |
| 568 /* Progressive JPEG use AC coeffs from zero and this decoder sets of
fset 16 by default */ | 573 |
| 569 code -= 16; | |
| 570 if(code & 0xF) { | 574 if(code & 0xF) { |
| 571 run = ((unsigned) code) >> 4; | 575 run = ((unsigned) code) >> 4; |
| 572 UPDATE_CACHE(re, &s->gb); | 576 UPDATE_CACHE(re, &s->gb); |
| 573 val = SHOW_UBITS(re, &s->gb, 1); | 577 val = SHOW_UBITS(re, &s->gb, 1); |
| 574 LAST_SKIP_BITS(re, &s->gb, 1); | 578 LAST_SKIP_BITS(re, &s->gb, 1); |
| 575 ZERO_RUN; | 579 ZERO_RUN; |
| 576 j = s->scantable.permutated[i]; | 580 j = s->scantable.permutated[i]; |
| 577 val--; | 581 val--; |
| 578 block[j] = ((quant_matrix[j]^val)-val) << Al; | 582 block[j] = ((quant_matrix[j]^val)-val) << Al; |
| 579 if(i == se) { | 583 if(i == se) { |
| (...skipping 934 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1514 int i, j; | 1518 int i, j; |
| 1515 | 1519 |
| 1516 if (s->picture.data[0]) | 1520 if (s->picture.data[0]) |
| 1517 avctx->release_buffer(avctx, &s->picture); | 1521 avctx->release_buffer(avctx, &s->picture); |
| 1518 | 1522 |
| 1519 av_free(s->buffer); | 1523 av_free(s->buffer); |
| 1520 av_free(s->qscale_table); | 1524 av_free(s->qscale_table); |
| 1521 av_freep(&s->ljpeg_buffer); | 1525 av_freep(&s->ljpeg_buffer); |
| 1522 s->ljpeg_buffer_size=0; | 1526 s->ljpeg_buffer_size=0; |
| 1523 | 1527 |
| 1524 for(i=0;i<2;i++) { | 1528 for(i=0;i<3;i++) { |
| 1525 for(j=0;j<4;j++) | 1529 for(j=0;j<4;j++) |
| 1526 free_vlc(&s->vlcs[i][j]); | 1530 free_vlc(&s->vlcs[i][j]); |
| 1527 } | 1531 } |
| 1528 for(i=0; i<MAX_COMPONENTS; i++) { | 1532 for(i=0; i<MAX_COMPONENTS; i++) { |
| 1529 av_freep(&s->blocks[i]); | 1533 av_freep(&s->blocks[i]); |
| 1530 av_freep(&s->last_nnz[i]); | 1534 av_freep(&s->last_nnz[i]); |
| 1531 } | 1535 } |
| 1532 return 0; | 1536 return 0; |
| 1533 } | 1537 } |
| 1534 | 1538 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1554 sizeof(MJpegDecodeContext), | 1558 sizeof(MJpegDecodeContext), |
| 1555 ff_mjpeg_decode_init, | 1559 ff_mjpeg_decode_init, |
| 1556 NULL, | 1560 NULL, |
| 1557 ff_mjpeg_decode_end, | 1561 ff_mjpeg_decode_end, |
| 1558 ff_mjpeg_decode_frame, | 1562 ff_mjpeg_decode_frame, |
| 1559 CODEC_CAP_DR1, | 1563 CODEC_CAP_DR1, |
| 1560 NULL, | 1564 NULL, |
| 1561 .max_lowres = 3, | 1565 .max_lowres = 3, |
| 1562 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"), | 1566 .long_name = NULL_IF_CONFIG_SMALL("Nintendo Gamecube THP video"), |
| 1563 }; | 1567 }; |
| OLD | NEW |