comparison mpegvideo.c @ 344:9f6071a87e17 libavcodec

fixed msmpeg4 infinite loop if buggy stream rewrote quantizer fixed bias (+10% compression/quality for h263 like codecs) qscale=1 support mpeg1 intra frames looks far less blocky added codec_id field
author michaelni
date Sat, 27 Apr 2002 12:30:26 +0000
parents bf26081c373c
children e05b357a398a
comparison
equal deleted inserted replaced
343:9211a0c9466a 344:9f6071a87e17
36 static void dct_unquantize_mpeg2_c(MpegEncContext *s, 36 static void dct_unquantize_mpeg2_c(MpegEncContext *s,
37 DCTELEM *block, int n, int qscale); 37 DCTELEM *block, int n, int qscale);
38 static void dct_unquantize_h263_c(MpegEncContext *s, 38 static void dct_unquantize_h263_c(MpegEncContext *s,
39 DCTELEM *block, int n, int qscale); 39 DCTELEM *block, int n, int qscale);
40 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w); 40 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w);
41 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale); 41 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
42 42
43 int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale)= dct_quantize_c; 43 int (*dct_quantize)(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow)= dct_quantize_c;
44 void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c; 44 void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c;
45 45
46 #define EDGE_WIDTH 16 46 #define EDGE_WIDTH 16
47 47
48 /* enable all paranoid tests for rounding, overflows, etc... */ 48 /* enable all paranoid tests for rounding, overflows, etc... */
76 extern UINT8 zigzag_end[64]; 76 extern UINT8 zigzag_end[64];
77 77
78 /* default motion estimation */ 78 /* default motion estimation */
79 int motion_estimation_method = ME_EPZS; 79 int motion_estimation_method = ME_EPZS;
80 80
81 static void convert_matrix(int *qmat, UINT16 *qmat16, const UINT16 *quant_matrix, int qscale) 81 static void convert_matrix(int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64],
82 { 82 const UINT16 *quant_matrix, int bias)
83 int i; 83 {
84 84 int qscale;
85 if (av_fdct == jpeg_fdct_ifast) { 85
86 for(i=0;i<64;i++) { 86 for(qscale=1; qscale<32; qscale++){
87 /* 16 <= qscale * quant_matrix[i] <= 7905 */ 87 int i;
88 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ 88 if (av_fdct == jpeg_fdct_ifast) {
89 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */ 89 for(i=0;i<64;i++) {
90 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */ 90 const int j= block_permute_op(i);
91 91 /* 16 <= qscale * quant_matrix[i] <= 7905 */
92 qmat[block_permute_op(i)] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) / 92 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
93 (aanscales[i] * qscale * quant_matrix[block_permute_op(i)])); 93 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */
94 } 94 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */
95 } else { 95
96 for(i=0;i<64;i++) { 96 qmat[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
97 /* We can safely suppose that 16 <= quant_matrix[i] <= 255 97 (aanscales[i] * qscale * quant_matrix[j]));
98 So 16 <= qscale * quant_matrix[i] <= 7905 98 }
99 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905 99 } else {
100 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 100 for(i=0;i<64;i++) {
101 */ 101 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
102 qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]); 102 So 16 <= qscale * quant_matrix[i] <= 7905
103 qmat16[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]); 103 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905
104 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67
105 */
106 qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
107 qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]);
108
109 if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1;
110
111 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]);
112 }
104 } 113 }
105 } 114 }
106 } 115 }
107 116
108 /* init common structure for both encoder and decoder */ 117 /* init common structure for both encoder and decoder */
386 s->aspect_ratio_info= avctx->aspect_ratio_info; 395 s->aspect_ratio_info= avctx->aspect_ratio_info;
387 s->flags= avctx->flags; 396 s->flags= avctx->flags;
388 s->max_b_frames= avctx->max_b_frames; 397 s->max_b_frames= avctx->max_b_frames;
389 s->rc_strategy= avctx->rc_strategy; 398 s->rc_strategy= avctx->rc_strategy;
390 s->b_frame_strategy= avctx->b_frame_strategy; 399 s->b_frame_strategy= avctx->b_frame_strategy;
391 400 s->codec_id= avctx->codec->id;
401
392 if (s->gop_size <= 1) { 402 if (s->gop_size <= 1) {
393 s->intra_only = 1; 403 s->intra_only = 1;
394 s->gop_size = 12; 404 s->gop_size = 12;
395 } else { 405 } else {
396 s->intra_only = 0; 406 s->intra_only = 0;
521 if (MPV_common_init(s) < 0) 531 if (MPV_common_init(s) < 0)
522 return -1; 532 return -1;
523 533
524 /* init default q matrix */ 534 /* init default q matrix */
525 for(i=0;i<64;i++) { 535 for(i=0;i<64;i++) {
526 s->intra_matrix[i] = default_intra_matrix[i]; 536 if(s->out_format == FMT_H263)
527 s->non_intra_matrix[i] = default_non_intra_matrix[i]; 537 s->intra_matrix[i] = default_non_intra_matrix[i];
538 else
539 s->intra_matrix[i] = default_intra_matrix[i];
540
541 s->inter_matrix[i] = default_non_intra_matrix[i];
542 }
543
544 /* precompute matrix */
545 /* for mjpeg, we do include qscale in the matrix */
546 if (s->out_format != FMT_MJPEG) {
547 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias,
548 s->intra_matrix, s->intra_quant_bias);
549 convert_matrix(s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias,
550 s->inter_matrix, s->inter_quant_bias);
528 } 551 }
529 552
530 if(ff_rate_control_init(s) < 0) 553 if(ff_rate_control_init(s) < 0)
531 return -1; 554 return -1;
532 555
1305 } 1328 }
1306 the_end: 1329 the_end:
1307 emms_c(); //FIXME remove 1330 emms_c(); //FIXME remove
1308 } 1331 }
1309 1332
1333 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index)
1334 {
1335 int i;
1336 const int maxlevel= s->max_qcoeff;
1337 const int minlevel= s->min_qcoeff;
1338
1339 for(i=0; i<=last_index; i++){
1340 const int j = zigzag_direct[i];
1341 int level = block[j];
1342
1343 if (level>maxlevel) level=maxlevel;
1344 else if(level<minlevel) level=minlevel;
1345 block[j]= level;
1346 }
1347 }
1310 1348
1311 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) 1349 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
1312 { 1350 {
1313 const int mb_x= s->mb_x; 1351 const int mb_x= s->mb_x;
1314 const int mb_y= s->mb_y; 1352 const int mb_y= s->mb_y;
1405 } else { 1443 } else {
1406 /* default quantization values */ 1444 /* default quantization values */
1407 s->y_dc_scale = 8; 1445 s->y_dc_scale = 8;
1408 s->c_dc_scale = 8; 1446 s->c_dc_scale = 8;
1409 } 1447 }
1410 for(i=0;i<6;i++) { 1448 if(s->out_format==FMT_MJPEG){
1411 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale); 1449 for(i=0;i<6;i++) {
1450 int overflow;
1451 s->block_last_index[i] = dct_quantize(s, s->block[i], i, 8, &overflow);
1452 if(overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
1453 }
1454 }else{
1455 for(i=0;i<6;i++) {
1456 int overflow;
1457 s->block_last_index[i] = dct_quantize(s, s->block[i], i, s->qscale, &overflow);
1458 // FIXME we could decide to change to quantizer instead of clipping
1459 if(overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]);
1460 }
1412 } 1461 }
1413 1462
1414 /* huffman encode */ 1463 /* huffman encode */
1415 switch(s->out_format) { 1464 switch(s->out_format) {
1416 case FMT_MPEG1: 1465 case FMT_MPEG1:
1594 if(s->flags&CODEC_FLAG_PASS2) 1643 if(s->flags&CODEC_FLAG_PASS2)
1595 s->qscale = ff_rate_estimate_qscale_pass2(s); 1644 s->qscale = ff_rate_estimate_qscale_pass2(s);
1596 else if (!s->fixed_qscale) 1645 else if (!s->fixed_qscale)
1597 s->qscale = ff_rate_estimate_qscale(s); 1646 s->qscale = ff_rate_estimate_qscale(s);
1598 1647
1599
1600 /* precompute matrix */
1601 if (s->out_format == FMT_MJPEG) { 1648 if (s->out_format == FMT_MJPEG) {
1602 /* for mjpeg, we do include qscale in the matrix */ 1649 /* for mjpeg, we do include qscale in the matrix */
1603 s->intra_matrix[0] = default_intra_matrix[0]; 1650 s->intra_matrix[0] = default_intra_matrix[0];
1604 for(i=1;i<64;i++) 1651 for(i=1;i<64;i++)
1605 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3; 1652 s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
1606 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, 8); 1653 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16,
1607 } else { 1654 s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias);
1608 convert_matrix(s->q_intra_matrix, s->q_intra_matrix16, s->intra_matrix, s->qscale);
1609 convert_matrix(s->q_non_intra_matrix, s->q_non_intra_matrix16, s->non_intra_matrix, s->qscale);
1610 } 1655 }
1611 1656
1612 s->last_bits= get_bit_count(&s->pb); 1657 s->last_bits= get_bit_count(&s->pb);
1613 switch(s->out_format) { 1658 switch(s->out_format) {
1614 case FMT_MJPEG: 1659 case FMT_MJPEG:
1955 } 2000 }
1956 } 2001 }
1957 2002
1958 static int dct_quantize_c(MpegEncContext *s, 2003 static int dct_quantize_c(MpegEncContext *s,
1959 DCTELEM *block, int n, 2004 DCTELEM *block, int n,
1960 int qscale) 2005 int qscale, int *overflow)
1961 { 2006 {
1962 int i, j, level, last_non_zero, q; 2007 int i, j, level, last_non_zero, q;
1963 const int *qmat; 2008 const int *qmat;
1964 int minLevel, maxLevel; 2009 int bias;
1965 2010 int max=0;
1966 if(s->avctx!=NULL && s->avctx->codec->id==CODEC_ID_MPEG4){ 2011 unsigned int threshold1, threshold2;
1967 /* mpeg4 */
1968 minLevel= -2048;
1969 maxLevel= 2047;
1970 }else if(s->out_format==FMT_MPEG1){
1971 /* mpeg1 */
1972 minLevel= -255;
1973 maxLevel= 255;
1974 }else if(s->out_format==FMT_MJPEG){
1975 /* (m)jpeg */
1976 minLevel= -1023;
1977 maxLevel= 1023;
1978 }else{
1979 /* h263 / msmpeg4 */
1980 minLevel= -128;
1981 maxLevel= 127;
1982 }
1983 2012
1984 av_fdct (block); 2013 av_fdct (block);
1985 2014
1986 /* we need this permutation so that we correct the IDCT 2015 /* we need this permutation so that we correct the IDCT
1987 permutation. will be moved into DCT code */ 2016 permutation. will be moved into DCT code */
1996 2025
1997 /* note: block[0] is assumed to be positive */ 2026 /* note: block[0] is assumed to be positive */
1998 block[0] = (block[0] + (q >> 1)) / q; 2027 block[0] = (block[0] + (q >> 1)) / q;
1999 i = 1; 2028 i = 1;
2000 last_non_zero = 0; 2029 last_non_zero = 0;
2001 if (s->out_format == FMT_H263) { 2030 qmat = s->q_intra_matrix[qscale];
2002 qmat = s->q_non_intra_matrix; 2031 bias= s->intra_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
2003 } else {
2004 qmat = s->q_intra_matrix;
2005 }
2006 } else { 2032 } else {
2007 i = 0; 2033 i = 0;
2008 last_non_zero = -1; 2034 last_non_zero = -1;
2009 qmat = s->q_non_intra_matrix; 2035 qmat = s->q_inter_matrix[qscale];
2010 } 2036 bias= s->inter_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT);
2037 }
2038 threshold1= (1<<(QMAT_SHIFT - 3)) - bias - 1;
2039 threshold2= threshold1<<1;
2011 2040
2012 for(;i<64;i++) { 2041 for(;i<64;i++) {
2013 j = zigzag_direct[i]; 2042 j = zigzag_direct[i];
2014 level = block[j]; 2043 level = block[j];
2015 level = level * qmat[j]; 2044 level = level * qmat[j];
2016 #ifdef PARANOID 2045
2017 { 2046 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
2018 static int count = 0; 2047 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
2019 int level1, level2, qmat1; 2048 if(((unsigned)(level+threshold1))>threshold2){
2020 double val; 2049 if(level>0){
2021 if (qmat == s->q_non_intra_matrix) { 2050 level= (bias + level)>>(QMAT_SHIFT - 3);
2022 qmat1 = default_non_intra_matrix[j] * s->qscale; 2051 block[j]= level;
2023 } else { 2052 }else{
2024 qmat1 = default_intra_matrix[j] * s->qscale; 2053 level= (bias - level)>>(QMAT_SHIFT - 3);
2054 block[j]= -level;
2025 } 2055 }
2026 if (av_fdct != jpeg_fdct_ifast) 2056 max |=level;
2027 val = ((double)block[j] * 8.0) / (double)qmat1;
2028 else
2029 val = ((double)block[j] * 8.0 * 2048.0) /
2030 ((double)qmat1 * aanscales[j]);
2031 level1 = (int)val;
2032 level2 = level / (1 << (QMAT_SHIFT - 3));
2033 if (level1 != level2) {
2034 fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
2035 count, level2, level1, block[j], qmat1, qmat[j],
2036 val);
2037 count++;
2038 }
2039
2040 }
2041 #endif
2042 /* XXX: slight error for the low range. Test should be equivalent to
2043 (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
2044 (QMAT_SHIFT - 3)))
2045 */
2046 if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
2047 level) {
2048 level = level / (1 << (QMAT_SHIFT - 3));
2049 /* XXX: currently, this code is not optimal. the range should be:
2050 mpeg1: -255..255
2051 mpeg2: -2048..2047
2052 h263: -128..127
2053 mpeg4: -2048..2047
2054 */
2055 if (level > maxLevel)
2056 level = maxLevel;
2057 else if (level < minLevel)
2058 level = minLevel;
2059
2060 block[j] = level;
2061 last_non_zero = i; 2057 last_non_zero = i;
2062 } else { 2058 }else{
2063 block[j] = 0; 2059 block[j]=0;
2064 } 2060 }
2065 } 2061 }
2062 *overflow= s->max_qcoeff < max; //overflow might have happend
2063
2066 return last_non_zero; 2064 return last_non_zero;
2067 } 2065 }
2068 2066
2069 static void dct_unquantize_mpeg1_c(MpegEncContext *s, 2067 static void dct_unquantize_mpeg1_c(MpegEncContext *s,
2070 DCTELEM *block, int n, int qscale) 2068 DCTELEM *block, int n, int qscale)
2102 block[j] = level; 2100 block[j] = level;
2103 } 2101 }
2104 } 2102 }
2105 } else { 2103 } else {
2106 i = 0; 2104 i = 0;
2107 quant_matrix = s->non_intra_matrix; 2105 quant_matrix = s->inter_matrix;
2108 for(;i<nCoeffs;i++) { 2106 for(;i<nCoeffs;i++) {
2109 int j= zigzag_direct[i]; 2107 int j= zigzag_direct[i];
2110 level = block[j]; 2108 level = block[j];
2111 if (level) { 2109 if (level) {
2112 if (level < 0) { 2110 if (level < 0) {
2164 } 2162 }
2165 } 2163 }
2166 } else { 2164 } else {
2167 int sum=-1; 2165 int sum=-1;
2168 i = 0; 2166 i = 0;
2169 quant_matrix = s->non_intra_matrix; 2167 quant_matrix = s->inter_matrix;
2170 for(;i<nCoeffs;i++) { 2168 for(;i<nCoeffs;i++) {
2171 int j= zigzag_direct[i]; 2169 int j= zigzag_direct[i];
2172 level = block[j]; 2170 level = block[j];
2173 if (level) { 2171 if (level) {
2174 if (level < 0) { 2172 if (level < 0) {