70 uint16_t (*qmat16)[2][64],
71 const uint16_t *quant_matrix,
72 int bias,
int qmin,
int qmax,
int intra)
77 for (qscale = qmin; qscale <= qmax; qscale++) {
82 for (i = 0; i < 64; i++) {
91 (qscale * quant_matrix[j]));
94 for (i = 0; i < 64; i++) {
106 for (i = 0; i < 64; i++) {
114 (qscale * quant_matrix[j]));
118 (qscale * quant_matrix[j]);
120 if (qmat16[qscale][0][i] == 0 ||
121 qmat16[qscale][0][i] == 128 * 256)
122 qmat16[
qscale][0][i] = 128 * 256 - 1;
125 qmat16[qscale][0][i]);
129 for (i = intra; i < 64; i++) {
134 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
141 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
162 for (i = 0; i < 64; i++) {
177 for (i = 0; i < s->
mb_num; i++) {
188 #define COPY(a) dst->a= src->a
213 for (i = -16; i < 16; i++) {
240 int chroma_h_shift, chroma_v_shift;
249 "only YUV420 and YUV422 are supported\n");
310 "keyframe interval too large!, reducing it from %d to %d\n",
377 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
384 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
401 "impossible bitrate constraints, this will fail\n");
415 "bitrate tolerance too small for bitrate\n");
426 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
427 "specified vbv buffer is too large for the given bitrate!\n");
439 "OBMC is only supported with simple mb decision\n");
462 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
470 (avctx->
width > 2048 ||
477 ((avctx->
width &3) ||
484 (avctx->
width > 4095 ||
491 (avctx->
width > 16383 ||
492 avctx->
height > 16383 )) {
493 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
528 "mpeg2 style quantization not supported by codec\n");
546 "closed gop with scene change detection are not supported yet, "
547 "set threshold to 1000000000\n");
554 "low delay forcing is only available for mpeg2\n");
559 "b frames cannot be used with low delay\n");
565 if (avctx->
qmax > 12) {
567 "non linear quant only supports qmax <= 12 currently\n");
579 "multi threaded encoding not supported by codec\n");
585 "automatic thread number detection not supported by codec, "
601 i = (INT_MAX / 2 + 128) >> 8;
610 "notice: b_frame_strategy only affects the first pass\n");
633 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
649 "timebase %d/%d not supported by MPEG 4 standard, "
650 "the maximum admitted value for the timebase denominator "
692 if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
699 if (!CONFIG_H261_ENCODER)
703 "The specified picture size of %dx%d is not valid for the "
704 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
713 if (!CONFIG_H263_ENCODER)
718 "The specified picture size of %dx%d is not valid for "
719 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
720 "352x288, 704x576, and 1408x1152. "
826 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
840 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
845 for (i = 0; i < 64; i++) {
906 if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
923 for (y = 0; y < 16; y++) {
924 for (x = 0; x < 16; x++) {
925 acc +=
FFABS(src[x + y * stride] - ref);
941 for (y = 0; y < h; y += 16) {
942 for (x = 0; x < w; x += 16) {
946 int mean = (s->
dsp.
pix_sum(src + offset, stride) + 128) >> 8;
947 int sae =
get_sae(src + offset, mean, stride);
949 acc += sae + 500 < sad;
960 int i, display_picture_number = 0,
ret;
975 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
980 if (!s->
low_delay && display_picture_number == 1)
989 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
992 pts = display_picture_number;
998 if (!pic_arg->
buf[0])
1040 int h_chroma_shift, v_chroma_shift;
1045 for (i = 0; i < 3; i++) {
1046 int src_stride = pic_arg->
linesize[i];
1048 int h_shift = i ? h_chroma_shift : 0;
1049 int v_shift = i ? v_chroma_shift : 0;
1050 int w = s->
width >> h_shift;
1051 int h = s->
height >> v_shift;
1056 h = ((s->
height + 15)/16*16) >> v_shift;
1062 if (src_stride == dst_stride)
1063 memcpy(dst, src, src_stride * h);
1068 memcpy(dst2, src, w);
1104 int64_t score64 = 0;
1106 for (plane = 0; plane < 3; plane++) {
1108 const int bw = plane ? 1 : 2;
1109 for (y = 0; y < s->
mb_height * bw; y++) {
1110 for (x = 0; x < s->
mb_width * bw; x++) {
1117 case 0: score =
FFMAX(score, v);
break;
1118 case 1: score +=
FFABS(v);
break;
1119 case 2: score += v *
v;
break;
1120 case 3: score64 +=
FFABS(v * v * (int64_t)v);
break;
1121 case 4: score64 += v * v * (int64_t)(v * v);
break;
1140 int ret, got_output;
1157 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1158 int64_t best_rd = INT64_MAX;
1159 int best_b_count = -1;
1169 b_lambda = p_lambda;
1194 pre_input = *pre_input_ptr;
1196 if (!pre_input.
shared && i) {
1258 return best_b_count;
1331 b_frames =
FFMAX(0, i - 1);
1334 for (i = 0; i < b_frames + 1; i++) {
1346 for (i = b_frames - 1; i >= 0; i--) {
1354 "warning, too many b frames in a row\n");
1377 for (i = 0; i < b_frames; i++) {
1423 for (i = 0; i < 4; i++) {
1440 AVFrame *pic_arg,
int *got_packet)
1443 int i, stuffing_count,
ret;
1466 for (i = 0; i < context_count; i++) {
1527 for (i = 0; i < context_count; i++) {
1540 for (i = 0; i < 4; i++) {
1554 if (stuffing_count) {
1556 stuffing_count + 50) {
1564 while (stuffing_count--) {
1571 stuffing_count -= 4;
1572 while (stuffing_count--) {
1598 "Internal error, negative bits\n");
1606 vbv_delay =
FFMAX(vbv_delay, min_delay);
1646 *got_packet = !!pkt->
size;
1651 int n,
int threshold)
1653 static const char tab[64] = {
1654 3, 2, 2, 1, 1, 1, 1, 1,
1655 1, 1, 1, 1, 1, 1, 1, 1,
1656 1, 1, 1, 1, 1, 1, 1, 1,
1657 0, 0, 0, 0, 0, 0, 0, 0,
1658 0, 0, 0, 0, 0, 0, 0, 0,
1659 0, 0, 0, 0, 0, 0, 0, 0,
1660 0, 0, 0, 0, 0, 0, 0, 0,
1661 0, 0, 0, 0, 0, 0, 0, 0
1670 if (threshold < 0) {
1672 threshold = -threshold;
1677 if (last_index <= skip_dc - 1)
1680 for (i = 0; i <= last_index; i++) {
1684 if (skip_dc && i == 0)
1688 }
else if (level > 1) {
1694 if (score >= threshold)
1696 for (i = skip_dc; i <= last_index; i++) {
1719 for (; i <= last_index; i++) {
1721 int level = block[j];
1723 if (level > maxlevel) {
1726 }
else if (level < minlevel) {
1736 "warning, clipping %d dct coefficients to %d..%d\n",
1737 overflow, minlevel, maxlevel);
1744 for (y = 0; y < 8; y++) {
1745 for (x = 0; x < 8; x++) {
1751 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
1752 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
1753 int v = ptr[x2 + y2 *
stride];
1759 weight[x + 8 *
y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
1765 int motion_x,
int motion_y,
1766 int mb_block_height,
1771 int16_t orig[12][64];
1778 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1779 ptrdiff_t wrap_y, wrap_c;
1781 for (i = 0; i < mb_block_count; i++)
1785 const int last_qp = s->
qscale;
1786 const int mb_xy = mb_x + mb_y * s->
mb_stride;
1817 (mb_y * 16 * wrap_y) + mb_x * 16;
1819 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1821 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1831 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1833 ptr_cb = ebuf + 18 * wrap_y;
1835 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1837 ptr_cr = ebuf + 18 * wrap_y + 16;
1842 int progressive_score, interlaced_score;
1848 NULL, wrap_y, 8) - 400;
1850 if (progressive_score > 0) {
1852 NULL, wrap_y * 2, 8) +
1854 NULL, wrap_y * 2, 8);
1855 if (progressive_score > interlaced_score) {
1858 dct_offset = wrap_y;
1859 uv_dct_offset = wrap_c;
1894 uint8_t *dest_y, *dest_cb, *dest_cr;
1896 dest_y = s->
dest[0];
1897 dest_cb = s->
dest[1];
1898 dest_cr = s->
dest[2];
1922 int progressive_score, interlaced_score;
1929 ptr_y + wrap_y * 8, wrap_y,
1933 progressive_score -= 400;
1935 if (progressive_score > 0) {
1943 if (progressive_score > interlaced_score) {
1946 dct_offset = wrap_y;
1947 uv_dct_offset = wrap_c;
1958 dest_y + dct_offset, wrap_y);
1960 dest_y + dct_offset + 8, wrap_y);
1970 dest_cb + uv_dct_offset, wrap_c);
1972 dest_cr + uv_dct_offset, wrap_c);
1979 if (s->
dsp.
sad[1](NULL, ptr_y , dest_y,
1980 wrap_y, 8) < 20 * s->
qscale)
1982 if (s->
dsp.
sad[1](NULL, ptr_y + 8,
1983 dest_y + 8, wrap_y, 8) < 20 * s->
qscale)
1985 if (s->
dsp.
sad[1](NULL, ptr_y + dct_offset,
1986 dest_y + dct_offset, wrap_y, 8) < 20 * s->
qscale)
1988 if (s->
dsp.
sad[1](NULL, ptr_y + dct_offset + 8,
1989 dest_y + dct_offset + 8,
1990 wrap_y, 8) < 20 * s->
qscale)
1992 if (s->
dsp.
sad[1](NULL, ptr_cb, dest_cb,
1993 wrap_c, 8) < 20 * s->
qscale)
1995 if (s->
dsp.
sad[1](NULL, ptr_cr, dest_cr,
1996 wrap_c, 8) < 20 * s->
qscale)
1999 if (s->
dsp.
sad[1](NULL, ptr_cb + uv_dct_offset,
2000 dest_cb + uv_dct_offset,
2001 wrap_c, 8) < 20 * s->
qscale)
2003 if (s->
dsp.
sad[1](NULL, ptr_cr + uv_dct_offset,
2004 dest_cr + uv_dct_offset,
2005 wrap_c, 8) < 20 * s->
qscale)
2032 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2038 for (i = 0; i < mb_block_count; i++) {
2053 for (i = 0; i < mb_block_count; i++) {
2063 for (i = 0; i < 4; i++)
2066 for (i = 4; i < mb_block_count; i++)
2070 for (i = 0; i < mb_block_count; i++) {
2083 for (i=6; i<12; i++) {
2092 for (i = 0; i < mb_block_count; i++) {
2095 for (j = 63; j > 0; j--) {
2108 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2112 if (CONFIG_MPEG4_ENCODER)
2122 if (CONFIG_WMV2_ENCODER)
2126 if (CONFIG_H261_ENCODER)
2134 if (CONFIG_H263_ENCODER)
2139 if (CONFIG_MJPEG_ENCODER)
2185 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2223 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2231 s->
pb= pb[*next_block];
2233 s->
pb2 = pb2 [*next_block];
2234 s->
tex_pb= tex_pb[*next_block];
2238 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2261 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2279 else if(w==8 && h==8)
2369 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2411 if (CONFIG_H263_ENCODER)
2413 bytestream_put_le32(&ptr, offset);
2414 bytestream_put_byte(&ptr, s->
qscale);
2415 bytestream_put_byte(&ptr, gobn);
2416 bytestream_put_le16(&ptr, mba);
2417 bytestream_put_byte(&ptr, pred_x);
2418 bytestream_put_byte(&ptr, pred_y);
2420 bytestream_put_byte(&ptr, 0);
2421 bytestream_put_byte(&ptr, 0);
2497 if (CONFIG_H263_ENCODER)
2517 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2548 int current_packet_size, is_gob_start;
2554 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2563 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2568 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2587 current_packet_size=0;
2601 if (CONFIG_MPEG4_ENCODER) {
2608 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2615 if (CONFIG_H263_ENCODER)
2645 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2662 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2663 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2674 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2675 &dmin, &next_block, 0, 0);
2683 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2684 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2694 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2695 &dmin, &next_block, 0, 0);
2703 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2704 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2712 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2713 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
2723 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2724 &dmin, &next_block, 0, 0);
2735 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2736 &dmin, &next_block, 0, 0);
2747 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2748 &dmin, &next_block, 0, 0);
2754 for(dir=0; dir<2; dir++){
2761 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2762 &dmin, &next_block, 0, 0);
2770 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2771 &dmin, &next_block, 0, 0);
2782 const int last_qp= backup_s.
qscale;
2786 static const int dquant_tab[4]={-1,1,-2,2};
2795 s->
mv[0][0][0] = best_s.
mv[0][0][0];
2796 s->
mv[0][0][1] = best_s.
mv[0][0][1];
2797 s->
mv[1][0][0] = best_s.
mv[1][0][0];
2798 s->
mv[1][0][1] = best_s.
mv[1][0][1];
2801 for(; qpi<4; qpi++){
2802 int dquant= dquant_tab[qpi];
2814 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2815 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
2835 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2836 &dmin, &next_block, mx, my);
2843 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2844 &dmin, &next_block, 0, 0);
2852 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
2873 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2874 &dmin, &next_block, mx, my);
2901 if (CONFIG_H263_ENCODER &&
2914 int motion_x = 0, motion_y = 0;
2922 motion_x= s->
mv[0][0][0] = 0;
2923 motion_y= s->
mv[0][0][1] = 0;
2951 if (CONFIG_MPEG4_ENCODER) {
2960 if (CONFIG_MPEG4_ENCODER) {
3010 for(dir=0; dir<2; dir++){
3027 if (CONFIG_H263_ENCODER &&
3084 #define MERGE(field) dst->field += src->field; src->field=0
3111 for(i=0; i<64; i++){
3138 if (CONFIG_MPEG4_ENCODER)
3144 if (CONFIG_H263_ENCODER)
3225 for(i=1; i<context_count; i++){
3255 for(i=1; i<context_count; i++){
3268 av_dlog(s,
"Scene change detected, encoding as I Frame %d %d\n",
3312 for(dir=0; dir<2; dir++){
3347 static const uint8_t y[32]={13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13};
3348 static const uint8_t c[32]={14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14};
3379 if (CONFIG_MJPEG_ENCODER)
3383 if (CONFIG_H261_ENCODER)
3391 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred)
3399 else if (CONFIG_H263_ENCODER)
3403 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3412 for(i=1; i<context_count; i++){
3416 for(i=1; i<context_count; i++){
3429 for(i=0; i<64; i++){
3430 int level= block[i];
3436 if(level<0) level=0;
3440 if(level>0) level=0;
3449 int qscale,
int *overflow){
3454 unsigned int threshold1, threshold2;
3466 int coeff_count[64];
3467 int qmul, qadd, start_i, last_non_zero, i,
dc;
3478 qadd= ((qscale-1)|1)*8;
3495 block[0] = (block[0] + (q >> 1)) / q;
3513 threshold2= (threshold1<<1);
3515 for(i=63; i>=start_i; i--) {
3516 const int j = scantable[i];
3517 int level = block[j] * qmat[j];
3519 if(((
unsigned)(level+threshold1))>threshold2){
3525 for(i=start_i; i<=last_non_zero; i++) {
3526 const int j = scantable[i];
3527 int level = block[j] * qmat[j];
3531 if(((
unsigned)(level+threshold1))>threshold2){
3535 coeff[1][i]= level-1;
3539 coeff[0][i]= -
level;
3540 coeff[1][i]= -level+1;
3543 coeff_count[i]=
FFMIN(level, 2);
3547 coeff[0][i]= (level>>31)|1;
3554 if(last_non_zero < start_i){
3555 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3556 return last_non_zero;
3559 score_tab[start_i]= 0;
3560 survivor[0]= start_i;
3563 for(i=start_i; i<=last_non_zero; i++){
3564 int level_index, j, zero_distortion;
3565 int dct_coeff=
FFABS(block[ scantable[i] ]);
3566 int best_score=256*256*256*120;
3570 zero_distortion= dct_coeff*dct_coeff;
3572 for(level_index=0; level_index < coeff_count[i]; level_index++){
3574 int level= coeff[level_index][i];
3575 const int alevel=
FFABS(level);
3581 unquant_coeff= alevel*qmul + qadd;
3585 unquant_coeff = (int)( alevel * qscale * s->
intra_matrix[j]) >> 3;
3586 unquant_coeff = (unquant_coeff - 1) | 1;
3588 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[j])) >> 4;
3589 unquant_coeff = (unquant_coeff - 1) | 1;
3594 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3596 if((level&(~127)) == 0){
3597 for(j=survivor_count-1; j>=0; j--){
3598 int run= i - survivor[j];
3600 score += score_tab[i-
run];
3602 if(score < best_score){
3605 level_tab[i+1]= level-64;
3610 for(j=survivor_count-1; j>=0; j--){
3611 int run= i - survivor[j];
3613 score += score_tab[i-
run];
3614 if(score < last_score){
3617 last_level= level-64;
3623 distortion += esc_length*
lambda;
3624 for(j=survivor_count-1; j>=0; j--){
3625 int run= i - survivor[j];
3626 int score= distortion + score_tab[i-
run];
3628 if(score < best_score){
3631 level_tab[i+1]= level-64;
3636 for(j=survivor_count-1; j>=0; j--){
3637 int run= i - survivor[j];
3638 int score= distortion + score_tab[i-
run];
3639 if(score < last_score){
3642 last_level= level-64;
3650 score_tab[i+1]= best_score;
3653 if(last_non_zero <= 27){
3654 for(; survivor_count; survivor_count--){
3655 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3659 for(; survivor_count; survivor_count--){
3660 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3665 survivor[ survivor_count++ ]= i+1;
3669 last_score= 256*256*256*120;
3670 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3671 int score= score_tab[i];
3672 if(i) score += lambda*2;
3674 if(score < last_score){
3677 last_level= level_tab[i];
3678 last_run= run_tab[i];
3685 dc=
FFABS(block[0]);
3686 last_non_zero= last_i - 1;
3687 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3689 if(last_non_zero < start_i)
3690 return last_non_zero;
3692 if(last_non_zero == 0 && start_i == 0){
3694 int best_score= dc *
dc;
3696 for(i=0; i<coeff_count[0]; i++){
3697 int level= coeff[i][0];
3698 int alevel=
FFABS(level);
3699 int unquant_coeff, score, distortion;
3702 unquant_coeff= (alevel*qmul + qadd)>>3;
3704 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[0])) >> 4;
3705 unquant_coeff = (unquant_coeff - 1) | 1;
3707 unquant_coeff = (unquant_coeff + 4) >> 3;
3708 unquant_coeff<<= 3 + 3;
3710 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
3713 else score= distortion + esc_length*
lambda;
3715 if(score < best_score){
3717 best_level= level - 64;
3720 block[0]= best_level;
3722 if(best_level == 0)
return -1;
3723 else return last_non_zero;
3729 block[ perm_scantable[last_non_zero] ]= last_level;
3732 for(; i>start_i; i -= run_tab[i] + 1){
3733 block[ perm_scantable[i-1] ]= level_tab[i];
3736 return last_non_zero;
3751 int perm_index= perm[
index];
3752 if(i==0) s*= sqrt(0.5);
3753 if(j==0) s*= sqrt(0.5);
3754 basis[perm_index][8*x +
y]=
lrintf(s * cos((
M_PI/8.0)*i*(x+0.5)) * cos((
M_PI/8.0)*j*(y+0.5)));
3773 int qmul, qadd, start_i, last_non_zero, i,
dc;
3777 int rle_index,
run, q = 1, sum;
3780 static int after_last=0;
3781 static int to_zero=0;
3782 static int from_zero=0;
3785 static int messed_sign=0;
3788 if(basis[0][0] == 0)
3825 for(i=0; i<64; i++){
3832 for(i=0; i<64; i++){
3837 w=
FFABS(weight[i]) + qns*one;
3838 w= 15 + (48*qns*one + w/2)/w;
3853 for(i=start_i; i<=last_non_zero; i++){
3854 int j= perm_scantable[i];
3855 const int level= block[j];
3859 if(level<0) coeff= qmul*level - qadd;
3860 else coeff= qmul*level + qadd;
3861 run_tab[rle_index++]=
run;
3870 if(last_non_zero>0){
3881 int run2, best_unquant_change=0, analyze_gradient;
3887 if(analyze_gradient){
3891 for(i=0; i<64; i++){
3907 const int level= block[0];
3908 int change, old_coeff;
3914 for(change=-1; change<=1; change+=2){
3915 int new_level= level + change;
3916 int score, new_coeff;
3918 new_coeff= q*new_level;
3919 if(new_coeff >= 2048 || new_coeff < 0)
3922 score= s->
dsp.
try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
3923 if(score<best_score){
3926 best_change= change;
3927 best_unquant_change= new_coeff - old_coeff;
3934 run2= run_tab[rle_index++];
3938 for(i=start_i; i<64; i++){
3939 int j= perm_scantable[i];
3940 const int level= block[j];
3941 int change, old_coeff;
3947 if(level<0) old_coeff= qmul*level - qadd;
3948 else old_coeff= qmul*level + qadd;
3949 run2= run_tab[rle_index++];
3956 for(change=-1; change<=1; change+=2){
3957 int new_level= level + change;
3958 int score, new_coeff, unquant_change;
3965 if(new_level<0) new_coeff= qmul*new_level - qadd;
3966 else new_coeff= qmul*new_level + qadd;
3967 if(new_coeff >= 2048 || new_coeff <= -2048)
3972 if(level < 63 && level > -63){
3973 if(i < last_non_zero)
3983 if(analyze_gradient){
3984 int g= d1[ scantable[i] ];
3985 if(g && (g^new_level) >= 0)
3989 if(i < last_non_zero){
3990 int next_i= i + run2 + 1;
3991 int next_level= block[ perm_scantable[next_i] ] + 64;
3993 if(next_level&(~127))
3996 if(next_i < last_non_zero)
4016 if(i < last_non_zero){
4017 int next_i= i + run2 + 1;
4018 int next_level= block[ perm_scantable[next_i] ] + 64;
4020 if(next_level&(~127))
4023 if(next_i < last_non_zero)
4042 unquant_change= new_coeff - old_coeff;
4043 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4046 if(score<best_score){
4049 best_change= change;
4050 best_unquant_change= unquant_change;
4054 prev_level= level + 64;
4055 if(prev_level&(~127))
4068 int j= perm_scantable[ best_coeff ];
4070 block[j] += best_change;
4072 if(best_coeff > last_non_zero){
4073 last_non_zero= best_coeff;
4081 if(block[j] - best_change){
4082 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
4094 for(; last_non_zero>=start_i; last_non_zero--){
4095 if(block[perm_scantable[last_non_zero]])
4101 if(256*256*256*64 % count == 0){
4102 av_log(s->
avctx,
AV_LOG_DEBUG,
"after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero,
raise, lower, messed_sign, s->
mb_x, s->
mb_y, s->
picture_number);
4107 for(i=start_i; i<=last_non_zero; i++){
4108 int j= perm_scantable[i];
4109 const int level= block[j];
4112 run_tab[rle_index++]=
run;
4125 if(last_non_zero>0){
4131 return last_non_zero;
4136 int qscale,
int *overflow)
4138 int i, j,
level, last_non_zero, q, start_i;
4143 unsigned int threshold1, threshold2;
4162 block[0] = (block[0] + (q >> 1)) / q;
4174 threshold2= (threshold1<<1);
4175 for(i=63;i>=start_i;i--) {
4177 level = block[j] * qmat[j];
4179 if(((
unsigned)(level+threshold1))>threshold2){
4186 for(i=start_i; i<=last_non_zero; i++) {
4188 level = block[j] * qmat[j];
4192 if(((
unsigned)(level+threshold1))>threshold2){
4211 return last_non_zero;
4214 #define OFFSET(x) offsetof(MpegEncContext, x)
4215 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4219 {
"mb_info",
"emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size",
OFFSET(
mb_info),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
VE },
4276 .
name =
"msmpeg4v2",
4285 .priv_class = &msmpeg4v2_class,
4300 .priv_class = &msmpeg4v3_class,
4315 .priv_class = &wmv1_class,