77 uint16_t (*qmat16)[2][64],
78 const uint16_t *quant_matrix,
79 int bias,
int qmin,
int qmax,
int intra)
85 for (qscale = qmin; qscale <= qmax; qscale++) {
90 for (i = 0; i < 64; i++) {
99 (qscale * quant_matrix[j]));
102 for (i = 0; i < 64; i++) {
114 for (i = 0; i < 64; i++) {
122 (qscale * quant_matrix[j]));
126 (qscale * quant_matrix[j]);
128 if (qmat16[qscale][0][i] == 0 ||
129 qmat16[qscale][0][i] == 128 * 256)
130 qmat16[
qscale][0][i] = 128 * 256 - 1;
133 qmat16[qscale][0][i]);
137 for (i = intra; i < 64; i++) {
142 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
149 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
170 for (i = 0; i < 64; i++) {
185 for (i = 0; i < s->
mb_num; i++) {
196 #define COPY(a) dst->a= src->a
221 for (i = -16; i < 16; i++) {
235 if (CONFIG_H263_ENCODER)
252 int i,
ret, format_supported;
261 "only YUV420 and YUV422 are supported\n");
267 format_supported = 0;
276 format_supported = 1;
282 format_supported = 1;
284 if (!format_supported) {
318 "keyframe interval too large!, reducing it from %d to %d\n",
347 "intra dc precision must be positive, note some applications use"
348 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
408 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
414 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
431 "impossible bitrate constraints, this will fail\n");
456 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
457 "specified vbv buffer is too large for the given bitrate!\n");
469 "OBMC is only supported with simple mb decision\n");
487 "max b frames must be 0 or positive for mpegvideo based encoders\n");
497 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
505 (avctx->
width > 2048 ||
512 ((avctx->
width &3) ||
519 (avctx->
width > 4095 ||
526 (avctx->
width > 16383 ||
527 avctx->
height > 16383 )) {
528 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
563 "mpeg2 style quantization not supported by codec\n");
581 "closed gop with scene change detection are not supported yet, "
582 "set threshold to 1000000000\n");
589 "low delay forcing is only available for mpeg2\n");
594 "b frames cannot be used with low delay\n");
600 if (avctx->
qmax > 12) {
602 "non linear quant only supports qmax <= 12 currently\n");
614 "multi threaded encoding not supported by codec\n");
620 "automatic thread number detection not supported by codec, "
636 i = (INT_MAX / 2 + 128) >> 8;
645 "notice: b_frame_strategy only affects the first pass\n");
668 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
682 "timebase %d/%d not supported by MPEG 4 standard, "
683 "the maximum admitted value for the timebase denominator "
706 if (!CONFIG_MJPEG_ENCODER ||
713 if (!CONFIG_H261_ENCODER)
717 "The specified picture size of %dx%d is not valid for the "
718 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
727 if (!CONFIG_H263_ENCODER)
732 "The specified picture size of %dx%d is not valid for "
733 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
734 "352x288, 704x576, and 1408x1152. "
848 (
MAX_RUN + 1) * 2 *
sizeof(
int), fail);
865 2 * 64 *
sizeof(uint16_t), fail);
870 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
884 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
889 for (i = 0; i < 64; i++) {
923 #if FF_API_ERROR_RATE
930 #if FF_API_NORMALIZE_AQP
974 if (CONFIG_MJPEG_ENCODER &&
1009 for (y = 0; y < 16; y++) {
1010 for (x = 0; x < 16; x++) {
1011 acc +=
FFABS(src[x + y * stride] - ref);
1027 for (y = 0; y < h; y += 16) {
1028 for (x = 0; x < w; x += 16) {
1029 int offset = x + y * stride;
1033 int sae =
get_sae(src + offset, mean, stride);
1035 acc += sae + 500 < sad;
1046 int i, display_picture_number = 0,
ret;
1061 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1066 if (!s->
low_delay && display_picture_number == 1)
1075 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1078 pts = display_picture_number;
1084 if (!pic_arg->
buf[0])
1132 int h_chroma_shift, v_chroma_shift;
1137 for (i = 0; i < 3; i++) {
1138 int src_stride = pic_arg->
linesize[i];
1140 int h_shift = i ? h_chroma_shift : 0;
1141 int v_shift = i ? v_chroma_shift : 0;
1142 int w = s->
width >> h_shift;
1143 int h = s->
height >> v_shift;
1156 if (src_stride == dst_stride)
1157 memcpy(dst, src, src_stride * h);
1162 memcpy(dst2, src, w);
1198 int64_t score64 = 0;
1200 for (plane = 0; plane < 3; plane++) {
1202 const int bw = plane ? 1 : 2;
1203 for (y = 0; y < s->
mb_height * bw; y++) {
1204 for (x = 0; x < s->
mb_width * bw; x++) {
1205 int off = p->
shared ? 0 : 16;
1206 uint8_t *dptr = p->
f->
data[plane] + 8 * (x + y * stride) + off;
1207 uint8_t *rptr = ref->
f->
data[plane] + 8 * (x + y * stride);
1211 case 0: score =
FFMAX(score, v);
break;
1212 case 1: score +=
FFABS(v);
break;
1213 case 2: score64 += v * (int64_t)v;
break;
1214 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1215 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1238 int ret, got_output;
1255 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1256 int64_t best_rd = INT64_MAX;
1257 int best_b_count = -1;
1267 b_lambda = p_lambda;
1292 pre_input = *pre_input_ptr;
1293 memcpy(data, pre_input_ptr->
f->
data,
sizeof(data));
1295 if (!pre_input.
shared && i) {
1363 return best_b_count;
1436 b_frames =
FFMAX(0, i - 1);
1439 for (i = 0; i < b_frames + 1; i++) {
1451 for (i = b_frames - 1; i >= 0; i--) {
1459 "warning, too many b frames in a row\n");
1482 for (i = 0; i < b_frames; i++) {
1528 for (i = 0; i < 4; i++) {
1588 for (intra = 0; intra < 2; intra++) {
1590 for (i = 0; i < 64; i++) {
1596 for (i = 0; i < 64; i++) {
1647 for (i = 0; i < 4; i++) {
1678 const AVFrame *pic_arg,
int *got_packet)
1681 int i, stuffing_count,
ret;
1704 for (i = 0; i < context_count; i++) {
1766 for (i = 0; i < context_count; i++) {
1779 for (i = 0; i < 4; i++) {
1795 if (stuffing_count) {
1797 stuffing_count + 50) {
1805 while (stuffing_count--) {
1812 stuffing_count -= 4;
1813 while (stuffing_count--) {
1839 "Internal error, negative bits\n");
1847 vbv_delay =
FFMAX(vbv_delay, min_delay);
1887 *got_packet = !!pkt->
size;
1892 int n,
int threshold)
1894 static const char tab[64] = {
1895 3, 2, 2, 1, 1, 1, 1, 1,
1896 1, 1, 1, 1, 1, 1, 1, 1,
1897 1, 1, 1, 1, 1, 1, 1, 1,
1898 0, 0, 0, 0, 0, 0, 0, 0,
1899 0, 0, 0, 0, 0, 0, 0, 0,
1900 0, 0, 0, 0, 0, 0, 0, 0,
1901 0, 0, 0, 0, 0, 0, 0, 0,
1902 0, 0, 0, 0, 0, 0, 0, 0
1911 if (threshold < 0) {
1913 threshold = -threshold;
1918 if (last_index <= skip_dc - 1)
1921 for (i = 0; i <= last_index; i++) {
1925 if (skip_dc && i == 0)
1929 }
else if (level > 1) {
1935 if (score >= threshold)
1937 for (i = skip_dc; i <= last_index; i++) {
1960 for (; i <= last_index; i++) {
1962 int level = block[j];
1964 if (level > maxlevel) {
1967 }
else if (level < minlevel) {
1977 "warning, clipping %d dct coefficients to %d..%d\n",
1978 overflow, minlevel, maxlevel);
1985 for (y = 0; y < 8; y++) {
1986 for (x = 0; x < 8; x++) {
1992 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
1993 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
1994 int v = ptr[x2 + y2 * stride];
2000 weight[x + 8 *
y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2006 int motion_x,
int motion_y,
2007 int mb_block_height,
2012 int16_t orig[12][64];
2019 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2020 ptrdiff_t wrap_y, wrap_c;
2022 for (i = 0; i < mb_block_count; i++)
2026 const int last_qp = s->
qscale;
2027 const int mb_xy = mb_x + mb_y * s->
mb_stride;
2058 (mb_y * 16 * wrap_y) + mb_x * 16;
2060 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2062 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2070 16, 16, mb_x * 16, mb_y * 16,
2075 mb_block_width, mb_block_height,
2076 mb_x * mb_block_width, mb_y * mb_block_height,
2078 ptr_cb = ebuf + 16 * wrap_y;
2081 mb_block_width, mb_block_height,
2082 mb_x * mb_block_width, mb_y * mb_block_height,
2084 ptr_cr = ebuf + 16 * wrap_y + 16;
2089 int progressive_score, interlaced_score;
2095 NULL, wrap_y, 8) - 400;
2097 if (progressive_score > 0) {
2099 NULL, wrap_y * 2, 8) +
2101 NULL, wrap_y * 2, 8);
2102 if (progressive_score > interlaced_score) {
2105 dct_offset = wrap_y;
2106 uv_dct_offset = wrap_c;
2141 uint8_t *dest_y, *dest_cb, *dest_cr;
2143 dest_y = s->
dest[0];
2144 dest_cb = s->
dest[1];
2145 dest_cr = s->
dest[2];
2169 int progressive_score, interlaced_score;
2176 ptr_y + wrap_y * 8, wrap_y,
2180 progressive_score -= 400;
2182 if (progressive_score > 0) {
2190 if (progressive_score > interlaced_score) {
2193 dct_offset = wrap_y;
2194 uv_dct_offset = wrap_c;
2205 dest_y + dct_offset, wrap_y);
2207 dest_y + dct_offset + 8, wrap_y);
2217 dest_cb + uv_dct_offset, wrap_c);
2219 dest_cr + uv_dct_offset, wrap_c);
2226 if (s->
dsp.
sad[1](NULL, ptr_y , dest_y,
2227 wrap_y, 8) < 20 * s->
qscale)
2229 if (s->
dsp.
sad[1](NULL, ptr_y + 8,
2230 dest_y + 8, wrap_y, 8) < 20 * s->
qscale)
2232 if (s->
dsp.
sad[1](NULL, ptr_y + dct_offset,
2233 dest_y + dct_offset, wrap_y, 8) < 20 * s->
qscale)
2235 if (s->
dsp.
sad[1](NULL, ptr_y + dct_offset + 8,
2236 dest_y + dct_offset + 8,
2237 wrap_y, 8) < 20 * s->
qscale)
2239 if (s->
dsp.
sad[1](NULL, ptr_cb, dest_cb,
2240 wrap_c, 8) < 20 * s->
qscale)
2242 if (s->
dsp.
sad[1](NULL, ptr_cr, dest_cr,
2243 wrap_c, 8) < 20 * s->
qscale)
2246 if (s->
dsp.
sad[1](NULL, ptr_cb + uv_dct_offset,
2247 dest_cb + uv_dct_offset,
2248 wrap_c, 8) < 20 * s->
qscale)
2250 if (s->
dsp.
sad[1](NULL, ptr_cr + uv_dct_offset,
2251 dest_cr + uv_dct_offset,
2252 wrap_c, 8) < 20 * s->
qscale)
2279 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2285 for (i = 0; i < mb_block_count; i++) {
2300 for (i = 0; i < mb_block_count; i++) {
2310 for (i = 0; i < 4; i++)
2313 for (i = 4; i < mb_block_count; i++)
2317 for (i = 0; i < mb_block_count; i++) {
2330 for (i=6; i<12; i++) {
2339 for (i = 0; i < mb_block_count; i++) {
2342 for (j = 63; j > 0; j--) {
2355 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2359 if (CONFIG_MPEG4_ENCODER)
2369 if (CONFIG_WMV2_ENCODER)
2373 if (CONFIG_H261_ENCODER)
2381 if (CONFIG_H263_ENCODER)
2386 if (CONFIG_MJPEG_ENCODER)
2432 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2470 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2478 s->
pb= pb[*next_block];
2480 s->
pb2 = pb2 [*next_block];
2481 s->
tex_pb= tex_pb[*next_block];
2485 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2508 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2525 return s->
dsp.
sse[0](NULL, src1, src2, stride, 16);
2526 else if(w==8 && h==8)
2527 return s->
dsp.
sse[1](NULL, src1, src2, stride, 8);
2531 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2616 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2624 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2659 if (CONFIG_H263_ENCODER)
2661 bytestream_put_le32(&ptr, offset);
2662 bytestream_put_byte(&ptr, s->
qscale);
2663 bytestream_put_byte(&ptr, gobn);
2664 bytestream_put_le16(&ptr, mba);
2665 bytestream_put_byte(&ptr, pred_x);
2666 bytestream_put_byte(&ptr, pred_y);
2668 bytestream_put_byte(&ptr, 0);
2669 bytestream_put_byte(&ptr, 0);
2745 if (CONFIG_H263_ENCODER)
2765 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2796 int current_packet_size, is_gob_start;
2802 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2811 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2816 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2836 current_packet_size=0;
2850 if (CONFIG_MPEG4_ENCODER) {
2857 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2864 if (CONFIG_H263_ENCODER)
2894 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2911 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2912 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2923 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2924 &dmin, &next_block, 0, 0);
2932 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2933 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2943 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2944 &dmin, &next_block, 0, 0);
2952 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2953 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2961 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2962 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
2972 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2973 &dmin, &next_block, 0, 0);
2984 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2985 &dmin, &next_block, 0, 0);
2996 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2997 &dmin, &next_block, 0, 0);
3003 for(dir=0; dir<2; dir++){
3010 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3011 &dmin, &next_block, 0, 0);
3019 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3020 &dmin, &next_block, 0, 0);
3031 const int last_qp= backup_s.
qscale;
3035 static const int dquant_tab[4]={-1,1,-2,2};
3044 s->
mv[0][0][0] = best_s.
mv[0][0][0];
3045 s->
mv[0][0][1] = best_s.
mv[0][0][1];
3046 s->
mv[1][0][0] = best_s.
mv[1][0][0];
3047 s->
mv[1][0][1] = best_s.
mv[1][0][1];
3050 for(; qpi<4; qpi++){
3051 int dquant= dquant_tab[qpi];
3063 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3064 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3084 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3085 &dmin, &next_block, mx, my);
3092 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3093 &dmin, &next_block, 0, 0);
3101 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3122 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3123 &dmin, &next_block, mx, my);
3150 if (CONFIG_H263_ENCODER &&
3163 int motion_x = 0, motion_y = 0;
3171 motion_x= s->
mv[0][0][0] = 0;
3172 motion_y= s->
mv[0][0][1] = 0;
3200 if (CONFIG_MPEG4_ENCODER) {
3209 if (CONFIG_MPEG4_ENCODER) {
3259 for(dir=0; dir<2; dir++){
3276 if (CONFIG_H263_ENCODER &&
3333 #define MERGE(field) dst->field += src->field; src->field=0
3360 for(i=0; i<64; i++){
3387 if (CONFIG_MPEG4_ENCODER)
3393 if (CONFIG_H263_ENCODER)
3474 for(i=1; i<context_count; i++){
3504 for(i=1; i<context_count; i++){
3517 av_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3561 for(dir=0; dir<2; dir++){
3610 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};
3611 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};
3642 if (CONFIG_MJPEG_ENCODER)
3647 if (CONFIG_H261_ENCODER)
3655 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred)
3663 else if (CONFIG_H263_ENCODER)
3667 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3676 for(i=1; i<context_count; i++){
3680 for(i=1; i<context_count; i++){
3693 for(i=0; i<64; i++){
3694 int level= block[i];
3700 if(level<0) level=0;
3704 if(level>0) level=0;
3713 int qscale,
int *overflow){
3718 unsigned int threshold1, threshold2;
3730 int coeff_count[64];
3731 int qmul, qadd, start_i, last_non_zero, i,
dc;
3742 qadd= ((qscale-1)|1)*8;
3759 block[0] = (block[0] + (q >> 1)) / q;
3777 threshold2= (threshold1<<1);
3779 for(i=63; i>=start_i; i--) {
3780 const int j = scantable[i];
3781 int level = block[j] * qmat[j];
3783 if(((
unsigned)(level+threshold1))>threshold2){
3789 for(i=start_i; i<=last_non_zero; i++) {
3790 const int j = scantable[i];
3791 int level = block[j] * qmat[j];
3795 if(((
unsigned)(level+threshold1))>threshold2){
3799 coeff[1][i]= level-1;
3803 coeff[0][i]= -
level;
3804 coeff[1][i]= -level+1;
3807 coeff_count[i]=
FFMIN(level, 2);
3811 coeff[0][i]= (level>>31)|1;
3818 if(last_non_zero < start_i){
3819 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3820 return last_non_zero;
3823 score_tab[start_i]= 0;
3824 survivor[0]= start_i;
3827 for(i=start_i; i<=last_non_zero; i++){
3828 int level_index, j, zero_distortion;
3829 int dct_coeff=
FFABS(block[ scantable[i] ]);
3830 int best_score=256*256*256*120;
3834 zero_distortion= dct_coeff*dct_coeff;
3836 for(level_index=0; level_index < coeff_count[i]; level_index++){
3838 int level= coeff[level_index][i];
3839 const int alevel=
FFABS(level);
3845 unquant_coeff= alevel*qmul + qadd;
3849 unquant_coeff = (int)( alevel * qscale * s->
intra_matrix[j]) >> 3;
3850 unquant_coeff = (unquant_coeff - 1) | 1;
3852 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[j])) >> 4;
3853 unquant_coeff = (unquant_coeff - 1) | 1;
3858 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3860 if((level&(~127)) == 0){
3861 for(j=survivor_count-1; j>=0; j--){
3862 int run= i - survivor[j];
3864 score += score_tab[i-
run];
3866 if(score < best_score){
3869 level_tab[i+1]= level-64;
3874 for(j=survivor_count-1; j>=0; j--){
3875 int run= i - survivor[j];
3877 score += score_tab[i-
run];
3878 if(score < last_score){
3881 last_level= level-64;
3887 distortion += esc_length*
lambda;
3888 for(j=survivor_count-1; j>=0; j--){
3889 int run= i - survivor[j];
3890 int score= distortion + score_tab[i-
run];
3892 if(score < best_score){
3895 level_tab[i+1]= level-64;
3900 for(j=survivor_count-1; j>=0; j--){
3901 int run= i - survivor[j];
3902 int score= distortion + score_tab[i-
run];
3903 if(score < last_score){
3906 last_level= level-64;
3914 score_tab[i+1]= best_score;
3917 if(last_non_zero <= 27){
3918 for(; survivor_count; survivor_count--){
3919 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3923 for(; survivor_count; survivor_count--){
3924 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3929 survivor[ survivor_count++ ]= i+1;
3933 last_score= 256*256*256*120;
3934 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3935 int score= score_tab[i];
3936 if(i) score += lambda*2;
3938 if(score < last_score){
3941 last_level= level_tab[i];
3942 last_run= run_tab[i];
3949 dc=
FFABS(block[0]);
3950 last_non_zero= last_i - 1;
3951 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3953 if(last_non_zero < start_i)
3954 return last_non_zero;
3956 if(last_non_zero == 0 && start_i == 0){
3958 int best_score= dc *
dc;
3960 for(i=0; i<coeff_count[0]; i++){
3961 int level= coeff[i][0];
3962 int alevel=
FFABS(level);
3963 int unquant_coeff, score, distortion;
3966 unquant_coeff= (alevel*qmul + qadd)>>3;
3968 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[0])) >> 4;
3969 unquant_coeff = (unquant_coeff - 1) | 1;
3971 unquant_coeff = (unquant_coeff + 4) >> 3;
3972 unquant_coeff<<= 3 + 3;
3974 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
3977 else score= distortion + esc_length*
lambda;
3979 if(score < best_score){
3981 best_level= level - 64;
3984 block[0]= best_level;
3986 if(best_level == 0)
return -1;
3987 else return last_non_zero;
3993 block[ perm_scantable[last_non_zero] ]= last_level;
3996 for(; i>start_i; i -= run_tab[i] + 1){
3997 block[ perm_scantable[i-1] ]= level_tab[i];
4000 return last_non_zero;
4015 int perm_index= perm[
index];
4016 if(i==0) s*= sqrt(0.5);
4017 if(j==0) s*= sqrt(0.5);
4018 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)));
4037 int qmul, qadd, start_i, last_non_zero, i,
dc;
4041 int rle_index,
run, q = 1, sum;
4044 static int after_last=0;
4045 static int to_zero=0;
4046 static int from_zero=0;
4049 static int messed_sign=0;
4052 if(basis[0][0] == 0)
4089 for(i=0; i<64; i++){
4096 for(i=0; i<64; i++){
4101 w=
FFABS(weight[i]) + qns*one;
4102 w= 15 + (48*qns*one + w/2)/w;
4117 for(i=start_i; i<=last_non_zero; i++){
4118 int j= perm_scantable[i];
4119 const int level= block[j];
4123 if(level<0) coeff= qmul*level - qadd;
4124 else coeff= qmul*level + qadd;
4125 run_tab[rle_index++]=
run;
4134 if(last_non_zero>0){
4145 int run2, best_unquant_change=0, analyze_gradient;
4151 if(analyze_gradient){
4155 for(i=0; i<64; i++){
4171 const int level= block[0];
4172 int change, old_coeff;
4178 for(change=-1; change<=1; change+=2){
4179 int new_level= level + change;
4180 int score, new_coeff;
4182 new_coeff= q*new_level;
4183 if(new_coeff >= 2048 || new_coeff < 0)
4187 new_coeff - old_coeff);
4188 if(score<best_score){
4191 best_change= change;
4192 best_unquant_change= new_coeff - old_coeff;
4199 run2= run_tab[rle_index++];
4203 for(i=start_i; i<64; i++){
4204 int j= perm_scantable[i];
4205 const int level= block[j];
4206 int change, old_coeff;
4212 if(level<0) old_coeff= qmul*level - qadd;
4213 else old_coeff= qmul*level + qadd;
4214 run2= run_tab[rle_index++];
4221 for(change=-1; change<=1; change+=2){
4222 int new_level= level + change;
4223 int score, new_coeff, unquant_change;
4230 if(new_level<0) new_coeff= qmul*new_level - qadd;
4231 else new_coeff= qmul*new_level + qadd;
4232 if(new_coeff >= 2048 || new_coeff <= -2048)
4237 if(level < 63 && level > -63){
4238 if(i < last_non_zero)
4248 if(analyze_gradient){
4249 int g= d1[ scantable[i] ];
4250 if(g && (g^new_level) >= 0)
4254 if(i < last_non_zero){
4255 int next_i= i + run2 + 1;
4256 int next_level= block[ perm_scantable[next_i] ] + 64;
4258 if(next_level&(~127))
4261 if(next_i < last_non_zero)
4281 if(i < last_non_zero){
4282 int next_i= i + run2 + 1;
4283 int next_level= block[ perm_scantable[next_i] ] + 64;
4285 if(next_level&(~127))
4288 if(next_i < last_non_zero)
4307 unquant_change= new_coeff - old_coeff;
4308 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4312 if(score<best_score){
4315 best_change= change;
4316 best_unquant_change= unquant_change;
4320 prev_level= level + 64;
4321 if(prev_level&(~127))
4334 int j= perm_scantable[ best_coeff ];
4336 block[j] += best_change;
4338 if(best_coeff > last_non_zero){
4339 last_non_zero= best_coeff;
4347 if(block[j] - best_change){
4348 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
4360 for(; last_non_zero>=start_i; last_non_zero--){
4361 if(block[perm_scantable[last_non_zero]])
4367 if(256*256*256*64 % count == 0){
4368 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);
4373 for(i=start_i; i<=last_non_zero; i++){
4374 int j= perm_scantable[i];
4375 const int level= block[j];
4378 run_tab[rle_index++]=
run;
4391 if(last_non_zero>0){
4397 return last_non_zero;
4402 int qscale,
int *overflow)
4404 int i, j,
level, last_non_zero, q, start_i;
4409 unsigned int threshold1, threshold2;
4428 block[0] = (block[0] + (q >> 1)) / q;
4440 threshold2= (threshold1<<1);
4441 for(i=63;i>=start_i;i--) {
4443 level = block[j] * qmat[j];
4445 if(((
unsigned)(level+threshold1))>threshold2){
4452 for(i=start_i; i<=last_non_zero; i++) {
4454 level = block[j] * qmat[j];
4458 if(((
unsigned)(level+threshold1))>threshold2){
4476 scantable, last_non_zero);
4478 return last_non_zero;
4481 #define OFFSET(x) offsetof(MpegEncContext, x)
4482 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4486 {
"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 },
4543 .
name =
"msmpeg4v2",
4552 .priv_class = &msmpeg4v2_class,
4567 .priv_class = &msmpeg4v3_class,
4582 .priv_class = &wmv1_class,