73 uint16_t (*qmat16)[2][64],
74 const uint16_t *quant_matrix,
75 int bias,
int qmin,
int qmax,
int intra)
80 for (qscale = qmin; qscale <= qmax; qscale++) {
85 for (i = 0; i < 64; i++) {
94 (qscale * quant_matrix[j]));
97 for (i = 0; i < 64; i++) {
109 for (i = 0; i < 64; i++) {
117 (qscale * quant_matrix[j]));
121 (qscale * quant_matrix[j]);
123 if (qmat16[qscale][0][i] == 0 ||
124 qmat16[qscale][0][i] == 128 * 256)
125 qmat16[
qscale][0][i] = 128 * 256 - 1;
128 qmat16[qscale][0][i]);
132 for (i = intra; i < 64; i++) {
137 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
144 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
165 for (i = 0; i < 64; i++) {
180 for (i = 0; i < s->
mb_num; i++) {
191 #define COPY(a) dst->a= src->a
216 for (i = -16; i < 16; i++) {
230 if (CONFIG_H263_ENCODER)
256 "only YUV420 and YUV422 are supported\n");
302 "keyframe interval too large!, reducing it from %d to %d\n",
374 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
381 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
398 "impossible bitrate constraints, this will fail\n");
423 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
424 "specified vbv buffer is too large for the given bitrate!\n");
436 "OBMC is only supported with simple mb decision\n");
454 "max b frames must be 0 or positive for mpegvideo based encoders\n");
464 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
472 (avctx->
width > 2048 ||
479 ((avctx->
width &3) ||
486 (avctx->
width > 4095 ||
493 (avctx->
width > 16383 ||
494 avctx->
height > 16383 )) {
495 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
530 "mpeg2 style quantization not supported by codec\n");
548 "closed gop with scene change detection are not supported yet, "
549 "set threshold to 1000000000\n");
556 "low delay forcing is only available for mpeg2\n");
561 "b frames cannot be used with low delay\n");
567 if (avctx->
qmax > 12) {
569 "non linear quant only supports qmax <= 12 currently\n");
581 "multi threaded encoding not supported by codec\n");
587 "automatic thread number detection not supported by codec, "
603 i = (INT_MAX / 2 + 128) >> 8;
612 "notice: b_frame_strategy only affects the first pass\n");
635 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 "
673 if (!CONFIG_MJPEG_ENCODER ||
680 if (!CONFIG_H261_ENCODER)
684 "The specified picture size of %dx%d is not valid for the "
685 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
694 if (!CONFIG_H263_ENCODER)
699 "The specified picture size of %dx%d is not valid for "
700 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
701 "352x288, 704x576, and 1408x1152. "
810 (
MAX_RUN + 1) * 2 *
sizeof(
int), fail);
827 2 * 64 *
sizeof(uint16_t), fail);
832 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
846 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
851 for (i = 0; i < 64; i++) {
885 #if FF_API_ERROR_RATE
922 if (CONFIG_MJPEG_ENCODER &&
957 for (y = 0; y < 16; y++) {
958 for (x = 0; x < 16; x++) {
959 acc +=
FFABS(src[x + y * stride] - ref);
975 for (y = 0; y < h; y += 16) {
976 for (x = 0; x < w; x += 16) {
977 int offset = x + y * stride;
980 int mean = (s->
dsp.
pix_sum(src + offset, stride) + 128) >> 8;
981 int sae =
get_sae(src + offset, mean, stride);
983 acc += sae + 500 < sad;
994 int i, display_picture_number = 0,
ret;
1009 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1014 if (!s->
low_delay && display_picture_number == 1)
1023 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1026 pts = display_picture_number;
1032 if (!pic_arg->
buf[0])
1080 int h_chroma_shift, v_chroma_shift;
1085 for (i = 0; i < 3; i++) {
1086 int src_stride = pic_arg->
linesize[i];
1088 int h_shift = i ? h_chroma_shift : 0;
1089 int v_shift = i ? v_chroma_shift : 0;
1090 int w = s->
width >> h_shift;
1091 int h = s->
height >> v_shift;
1096 h = ((s->
height + 15)/16*16) >> v_shift;
1102 if (src_stride == dst_stride)
1103 memcpy(dst, src, src_stride * h);
1108 memcpy(dst2, src, w);
1144 int64_t score64 = 0;
1146 for (plane = 0; plane < 3; plane++) {
1148 const int bw = plane ? 1 : 2;
1149 for (y = 0; y < s->
mb_height * bw; y++) {
1150 for (x = 0; x < s->
mb_width * bw; x++) {
1152 uint8_t *dptr = p->
f.
data[plane] + 8 * (x + y * stride) + off;
1153 uint8_t *rptr = ref->
f.
data[plane] + 8 * (x + y * stride);
1157 case 0: score =
FFMAX(score, v);
break;
1158 case 1: score +=
FFABS(v);
break;
1159 case 2: score64 += v * (int64_t)v;
break;
1160 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1161 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1184 int ret, got_output;
1201 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1202 int64_t best_rd = INT64_MAX;
1203 int best_b_count = -1;
1213 b_lambda = p_lambda;
1238 pre_input = *pre_input_ptr;
1240 if (!pre_input.
shared && i) {
1302 return best_b_count;
1375 b_frames =
FFMAX(0, i - 1);
1378 for (i = 0; i < b_frames + 1; i++) {
1390 for (i = b_frames - 1; i >= 0; i--) {
1398 "warning, too many b frames in a row\n");
1421 for (i = 0; i < b_frames; i++) {
1467 for (i = 0; i < 4; i++) {
1520 for (intra = 0; intra < 2; intra++) {
1522 for (i = 0; i < 64; i++) {
1528 for (i = 0; i < 64; i++) {
1579 for (i = 0; i < 4; i++) {
1610 const AVFrame *pic_arg,
int *got_packet)
1613 int i, stuffing_count,
ret;
1636 for (i = 0; i < context_count; i++) {
1698 for (i = 0; i < context_count; i++) {
1711 for (i = 0; i < 4; i++) {
1725 if (stuffing_count) {
1727 stuffing_count + 50) {
1735 while (stuffing_count--) {
1742 stuffing_count -= 4;
1743 while (stuffing_count--) {
1769 "Internal error, negative bits\n");
1777 vbv_delay =
FFMAX(vbv_delay, min_delay);
1817 *got_packet = !!pkt->
size;
1822 int n,
int threshold)
1824 static const char tab[64] = {
1825 3, 2, 2, 1, 1, 1, 1, 1,
1826 1, 1, 1, 1, 1, 1, 1, 1,
1827 1, 1, 1, 1, 1, 1, 1, 1,
1828 0, 0, 0, 0, 0, 0, 0, 0,
1829 0, 0, 0, 0, 0, 0, 0, 0,
1830 0, 0, 0, 0, 0, 0, 0, 0,
1831 0, 0, 0, 0, 0, 0, 0, 0,
1832 0, 0, 0, 0, 0, 0, 0, 0
1841 if (threshold < 0) {
1843 threshold = -threshold;
1848 if (last_index <= skip_dc - 1)
1851 for (i = 0; i <= last_index; i++) {
1855 if (skip_dc && i == 0)
1859 }
else if (level > 1) {
1865 if (score >= threshold)
1867 for (i = skip_dc; i <= last_index; i++) {
1890 for (; i <= last_index; i++) {
1892 int level = block[j];
1894 if (level > maxlevel) {
1897 }
else if (level < minlevel) {
1907 "warning, clipping %d dct coefficients to %d..%d\n",
1908 overflow, minlevel, maxlevel);
1915 for (y = 0; y < 8; y++) {
1916 for (x = 0; x < 8; x++) {
1922 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
1923 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
1924 int v = ptr[x2 + y2 * stride];
1930 weight[x + 8 *
y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
1936 int motion_x,
int motion_y,
1937 int mb_block_height,
1942 int16_t orig[12][64];
1949 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1950 ptrdiff_t wrap_y, wrap_c;
1952 for (i = 0; i < mb_block_count; i++)
1956 const int last_qp = s->
qscale;
1957 const int mb_xy = mb_x + mb_y * s->
mb_stride;
1988 (mb_y * 16 * wrap_y) + mb_x * 16;
1990 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1992 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2000 16, 16, mb_x * 16, mb_y * 16,
2005 mb_block_width, mb_block_height,
2006 mb_x * mb_block_width, mb_y * mb_block_height,
2008 ptr_cb = ebuf + 18 * wrap_y;
2011 mb_block_width, mb_block_height,
2012 mb_x * mb_block_width, mb_y * mb_block_height,
2014 ptr_cr = ebuf + 18 * wrap_y + 16;
2019 int progressive_score, interlaced_score;
2025 NULL, wrap_y, 8) - 400;
2027 if (progressive_score > 0) {
2029 NULL, wrap_y * 2, 8) +
2031 NULL, wrap_y * 2, 8);
2032 if (progressive_score > interlaced_score) {
2035 dct_offset = wrap_y;
2036 uv_dct_offset = wrap_c;
2071 uint8_t *dest_y, *dest_cb, *dest_cr;
2073 dest_y = s->
dest[0];
2074 dest_cb = s->
dest[1];
2075 dest_cr = s->
dest[2];
2099 int progressive_score, interlaced_score;
2106 ptr_y + wrap_y * 8, wrap_y,
2110 progressive_score -= 400;
2112 if (progressive_score > 0) {
2120 if (progressive_score > interlaced_score) {
2123 dct_offset = wrap_y;
2124 uv_dct_offset = wrap_c;
2135 dest_y + dct_offset, wrap_y);
2137 dest_y + dct_offset + 8, wrap_y);
2147 dest_cb + uv_dct_offset, wrap_c);
2149 dest_cr + uv_dct_offset, wrap_c);
2156 if (s->
dsp.
sad[1](NULL, ptr_y , dest_y,
2157 wrap_y, 8) < 20 * s->
qscale)
2159 if (s->
dsp.
sad[1](NULL, ptr_y + 8,
2160 dest_y + 8, wrap_y, 8) < 20 * s->
qscale)
2162 if (s->
dsp.
sad[1](NULL, ptr_y + dct_offset,
2163 dest_y + dct_offset, wrap_y, 8) < 20 * s->
qscale)
2165 if (s->
dsp.
sad[1](NULL, ptr_y + dct_offset + 8,
2166 dest_y + dct_offset + 8,
2167 wrap_y, 8) < 20 * s->
qscale)
2169 if (s->
dsp.
sad[1](NULL, ptr_cb, dest_cb,
2170 wrap_c, 8) < 20 * s->
qscale)
2172 if (s->
dsp.
sad[1](NULL, ptr_cr, dest_cr,
2173 wrap_c, 8) < 20 * s->
qscale)
2176 if (s->
dsp.
sad[1](NULL, ptr_cb + uv_dct_offset,
2177 dest_cb + uv_dct_offset,
2178 wrap_c, 8) < 20 * s->
qscale)
2180 if (s->
dsp.
sad[1](NULL, ptr_cr + uv_dct_offset,
2181 dest_cr + uv_dct_offset,
2182 wrap_c, 8) < 20 * s->
qscale)
2209 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2215 for (i = 0; i < mb_block_count; i++) {
2230 for (i = 0; i < mb_block_count; i++) {
2240 for (i = 0; i < 4; i++)
2243 for (i = 4; i < mb_block_count; i++)
2247 for (i = 0; i < mb_block_count; i++) {
2260 for (i=6; i<12; i++) {
2269 for (i = 0; i < mb_block_count; i++) {
2272 for (j = 63; j > 0; j--) {
2285 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2289 if (CONFIG_MPEG4_ENCODER)
2299 if (CONFIG_WMV2_ENCODER)
2303 if (CONFIG_H261_ENCODER)
2311 if (CONFIG_H263_ENCODER)
2316 if (CONFIG_MJPEG_ENCODER)
2362 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2400 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2408 s->
pb= pb[*next_block];
2410 s->
pb2 = pb2 [*next_block];
2411 s->
tex_pb= tex_pb[*next_block];
2415 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2438 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2455 return s->
dsp.
sse[0](NULL, src1, src2, stride, 16);
2456 else if(w==8 && h==8)
2457 return s->
dsp.
sse[1](NULL, src1, src2, stride, 8);
2461 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2546 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2588 if (CONFIG_H263_ENCODER)
2590 bytestream_put_le32(&ptr, offset);
2591 bytestream_put_byte(&ptr, s->
qscale);
2592 bytestream_put_byte(&ptr, gobn);
2593 bytestream_put_le16(&ptr, mba);
2594 bytestream_put_byte(&ptr, pred_x);
2595 bytestream_put_byte(&ptr, pred_y);
2597 bytestream_put_byte(&ptr, 0);
2598 bytestream_put_byte(&ptr, 0);
2674 if (CONFIG_H263_ENCODER)
2694 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2725 int current_packet_size, is_gob_start;
2731 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2740 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2745 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2765 current_packet_size=0;
2779 if (CONFIG_MPEG4_ENCODER) {
2786 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2793 if (CONFIG_H263_ENCODER)
2823 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2840 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2841 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2852 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2853 &dmin, &next_block, 0, 0);
2861 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2862 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2872 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2873 &dmin, &next_block, 0, 0);
2881 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2882 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2890 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2891 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
2901 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2902 &dmin, &next_block, 0, 0);
2913 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2914 &dmin, &next_block, 0, 0);
2925 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2926 &dmin, &next_block, 0, 0);
2932 for(dir=0; dir<2; dir++){
2939 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2940 &dmin, &next_block, 0, 0);
2948 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2949 &dmin, &next_block, 0, 0);
2960 const int last_qp= backup_s.
qscale;
2964 static const int dquant_tab[4]={-1,1,-2,2};
2973 s->
mv[0][0][0] = best_s.
mv[0][0][0];
2974 s->
mv[0][0][1] = best_s.
mv[0][0][1];
2975 s->
mv[1][0][0] = best_s.
mv[1][0][0];
2976 s->
mv[1][0][1] = best_s.
mv[1][0][1];
2979 for(; qpi<4; qpi++){
2980 int dquant= dquant_tab[qpi];
2992 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2993 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3013 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3014 &dmin, &next_block, mx, my);
3021 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3022 &dmin, &next_block, 0, 0);
3030 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3051 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3052 &dmin, &next_block, mx, my);
3079 if (CONFIG_H263_ENCODER &&
3092 int motion_x = 0, motion_y = 0;
3100 motion_x= s->
mv[0][0][0] = 0;
3101 motion_y= s->
mv[0][0][1] = 0;
3129 if (CONFIG_MPEG4_ENCODER) {
3138 if (CONFIG_MPEG4_ENCODER) {
3188 for(dir=0; dir<2; dir++){
3205 if (CONFIG_H263_ENCODER &&
3262 #define MERGE(field) dst->field += src->field; src->field=0
3289 for(i=0; i<64; i++){
3316 if (CONFIG_MPEG4_ENCODER)
3322 if (CONFIG_H263_ENCODER)
3403 for(i=1; i<context_count; i++){
3433 for(i=1; i<context_count; i++){
3446 av_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3490 for(dir=0; dir<2; dir++){
3539 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};
3540 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};
3571 if (CONFIG_MJPEG_ENCODER)
3576 if (CONFIG_H261_ENCODER)
3584 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred)
3592 else if (CONFIG_H263_ENCODER)
3596 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3605 for(i=1; i<context_count; i++){
3609 for(i=1; i<context_count; i++){
3622 for(i=0; i<64; i++){
3623 int level= block[i];
3629 if(level<0) level=0;
3633 if(level>0) level=0;
3642 int qscale,
int *overflow){
3647 unsigned int threshold1, threshold2;
3659 int coeff_count[64];
3660 int qmul, qadd, start_i, last_non_zero, i,
dc;
3671 qadd= ((qscale-1)|1)*8;
3688 block[0] = (block[0] + (q >> 1)) / q;
3706 threshold2= (threshold1<<1);
3708 for(i=63; i>=start_i; i--) {
3709 const int j = scantable[i];
3710 int level = block[j] * qmat[j];
3712 if(((
unsigned)(level+threshold1))>threshold2){
3718 for(i=start_i; i<=last_non_zero; i++) {
3719 const int j = scantable[i];
3720 int level = block[j] * qmat[j];
3724 if(((
unsigned)(level+threshold1))>threshold2){
3728 coeff[1][i]= level-1;
3732 coeff[0][i]= -
level;
3733 coeff[1][i]= -level+1;
3736 coeff_count[i]=
FFMIN(level, 2);
3740 coeff[0][i]= (level>>31)|1;
3747 if(last_non_zero < start_i){
3748 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3749 return last_non_zero;
3752 score_tab[start_i]= 0;
3753 survivor[0]= start_i;
3756 for(i=start_i; i<=last_non_zero; i++){
3757 int level_index, j, zero_distortion;
3758 int dct_coeff=
FFABS(block[ scantable[i] ]);
3759 int best_score=256*256*256*120;
3763 zero_distortion= dct_coeff*dct_coeff;
3765 for(level_index=0; level_index < coeff_count[i]; level_index++){
3767 int level= coeff[level_index][i];
3768 const int alevel=
FFABS(level);
3774 unquant_coeff= alevel*qmul + qadd;
3778 unquant_coeff = (int)( alevel * qscale * s->
intra_matrix[j]) >> 3;
3779 unquant_coeff = (unquant_coeff - 1) | 1;
3781 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[j])) >> 4;
3782 unquant_coeff = (unquant_coeff - 1) | 1;
3787 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3789 if((level&(~127)) == 0){
3790 for(j=survivor_count-1; j>=0; j--){
3791 int run= i - survivor[j];
3793 score += score_tab[i-
run];
3795 if(score < best_score){
3798 level_tab[i+1]= level-64;
3803 for(j=survivor_count-1; j>=0; j--){
3804 int run= i - survivor[j];
3806 score += score_tab[i-
run];
3807 if(score < last_score){
3810 last_level= level-64;
3816 distortion += esc_length*
lambda;
3817 for(j=survivor_count-1; j>=0; j--){
3818 int run= i - survivor[j];
3819 int score= distortion + score_tab[i-
run];
3821 if(score < best_score){
3824 level_tab[i+1]= level-64;
3829 for(j=survivor_count-1; j>=0; j--){
3830 int run= i - survivor[j];
3831 int score= distortion + score_tab[i-
run];
3832 if(score < last_score){
3835 last_level= level-64;
3843 score_tab[i+1]= best_score;
3846 if(last_non_zero <= 27){
3847 for(; survivor_count; survivor_count--){
3848 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3852 for(; survivor_count; survivor_count--){
3853 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3858 survivor[ survivor_count++ ]= i+1;
3862 last_score= 256*256*256*120;
3863 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3864 int score= score_tab[i];
3865 if(i) score += lambda*2;
3867 if(score < last_score){
3870 last_level= level_tab[i];
3871 last_run= run_tab[i];
3878 dc=
FFABS(block[0]);
3879 last_non_zero= last_i - 1;
3880 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3882 if(last_non_zero < start_i)
3883 return last_non_zero;
3885 if(last_non_zero == 0 && start_i == 0){
3887 int best_score= dc *
dc;
3889 for(i=0; i<coeff_count[0]; i++){
3890 int level= coeff[i][0];
3891 int alevel=
FFABS(level);
3892 int unquant_coeff, score, distortion;
3895 unquant_coeff= (alevel*qmul + qadd)>>3;
3897 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[0])) >> 4;
3898 unquant_coeff = (unquant_coeff - 1) | 1;
3900 unquant_coeff = (unquant_coeff + 4) >> 3;
3901 unquant_coeff<<= 3 + 3;
3903 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
3906 else score= distortion + esc_length*
lambda;
3908 if(score < best_score){
3910 best_level= level - 64;
3913 block[0]= best_level;
3915 if(best_level == 0)
return -1;
3916 else return last_non_zero;
3922 block[ perm_scantable[last_non_zero] ]= last_level;
3925 for(; i>start_i; i -= run_tab[i] + 1){
3926 block[ perm_scantable[i-1] ]= level_tab[i];
3929 return last_non_zero;
3944 int perm_index= perm[
index];
3945 if(i==0) s*= sqrt(0.5);
3946 if(j==0) s*= sqrt(0.5);
3947 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)));
3966 int qmul, qadd, start_i, last_non_zero, i,
dc;
3970 int rle_index,
run, q = 1, sum;
3973 static int after_last=0;
3974 static int to_zero=0;
3975 static int from_zero=0;
3978 static int messed_sign=0;
3981 if(basis[0][0] == 0)
4018 for(i=0; i<64; i++){
4025 for(i=0; i<64; i++){
4030 w=
FFABS(weight[i]) + qns*one;
4031 w= 15 + (48*qns*one + w/2)/w;
4046 for(i=start_i; i<=last_non_zero; i++){
4047 int j= perm_scantable[i];
4048 const int level= block[j];
4052 if(level<0) coeff= qmul*level - qadd;
4053 else coeff= qmul*level + qadd;
4054 run_tab[rle_index++]=
run;
4063 if(last_non_zero>0){
4074 int run2, best_unquant_change=0, analyze_gradient;
4080 if(analyze_gradient){
4084 for(i=0; i<64; i++){
4100 const int level= block[0];
4101 int change, old_coeff;
4107 for(change=-1; change<=1; change+=2){
4108 int new_level= level + change;
4109 int score, new_coeff;
4111 new_coeff= q*new_level;
4112 if(new_coeff >= 2048 || new_coeff < 0)
4115 score= s->
dsp.
try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
4116 if(score<best_score){
4119 best_change= change;
4120 best_unquant_change= new_coeff - old_coeff;
4127 run2= run_tab[rle_index++];
4131 for(i=start_i; i<64; i++){
4132 int j= perm_scantable[i];
4133 const int level= block[j];
4134 int change, old_coeff;
4140 if(level<0) old_coeff= qmul*level - qadd;
4141 else old_coeff= qmul*level + qadd;
4142 run2= run_tab[rle_index++];
4149 for(change=-1; change<=1; change+=2){
4150 int new_level= level + change;
4151 int score, new_coeff, unquant_change;
4158 if(new_level<0) new_coeff= qmul*new_level - qadd;
4159 else new_coeff= qmul*new_level + qadd;
4160 if(new_coeff >= 2048 || new_coeff <= -2048)
4165 if(level < 63 && level > -63){
4166 if(i < last_non_zero)
4176 if(analyze_gradient){
4177 int g= d1[ scantable[i] ];
4178 if(g && (g^new_level) >= 0)
4182 if(i < last_non_zero){
4183 int next_i= i + run2 + 1;
4184 int next_level= block[ perm_scantable[next_i] ] + 64;
4186 if(next_level&(~127))
4189 if(next_i < last_non_zero)
4209 if(i < last_non_zero){
4210 int next_i= i + run2 + 1;
4211 int next_level= block[ perm_scantable[next_i] ] + 64;
4213 if(next_level&(~127))
4216 if(next_i < last_non_zero)
4235 unquant_change= new_coeff - old_coeff;
4236 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4239 if(score<best_score){
4242 best_change= change;
4243 best_unquant_change= unquant_change;
4247 prev_level= level + 64;
4248 if(prev_level&(~127))
4261 int j= perm_scantable[ best_coeff ];
4263 block[j] += best_change;
4265 if(best_coeff > last_non_zero){
4266 last_non_zero= best_coeff;
4274 if(block[j] - best_change){
4275 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
4287 for(; last_non_zero>=start_i; last_non_zero--){
4288 if(block[perm_scantable[last_non_zero]])
4294 if(256*256*256*64 % count == 0){
4295 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);
4300 for(i=start_i; i<=last_non_zero; i++){
4301 int j= perm_scantable[i];
4302 const int level= block[j];
4305 run_tab[rle_index++]=
run;
4318 if(last_non_zero>0){
4324 return last_non_zero;
4329 int qscale,
int *overflow)
4331 int i, j,
level, last_non_zero, q, start_i;
4336 unsigned int threshold1, threshold2;
4355 block[0] = (block[0] + (q >> 1)) / q;
4367 threshold2= (threshold1<<1);
4368 for(i=63;i>=start_i;i--) {
4370 level = block[j] * qmat[j];
4372 if(((
unsigned)(level+threshold1))>threshold2){
4379 for(i=start_i; i<=last_non_zero; i++) {
4381 level = block[j] * qmat[j];
4385 if(((
unsigned)(level+threshold1))>threshold2){
4404 return last_non_zero;
4407 #define OFFSET(x) offsetof(MpegEncContext, x)
4408 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4412 {
"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 },
4469 .
name =
"msmpeg4v2",
4478 .priv_class = &msmpeg4v2_class,
4493 .priv_class = &msmpeg4v3_class,
4508 .priv_class = &wmv1_class,