62 #define QUANT_BIAS_SHIFT 8
64 #define QMAT_SHIFT_MMX 16
82 uint16_t (*qmat16)[2][64],
83 const uint16_t *quant_matrix,
84 int bias,
int qmin,
int qmax,
int intra)
90 for (qscale = qmin; qscale <= qmax; qscale++) {
97 for (i = 0; i < 64; i++) {
99 int64_t den = (int64_t) qscale * quant_matrix[j];
109 for (i = 0; i < 64; i++) {
111 int64_t den =
ff_aanscales[i] * (int64_t) qscale * quant_matrix[j];
121 for (i = 0; i < 64; i++) {
123 int64_t den = (int64_t) qscale * quant_matrix[j];
134 if (qmat16[qscale][0][i] == 0 ||
135 qmat16[qscale][0][i] == 128 * 256)
136 qmat16[
qscale][0][i] = 128 * 256 - 1;
139 qmat16[qscale][0][i]);
143 for (i = intra; i < 64; i++) {
148 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
155 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
176 for (i = 0; i < 64; i++) {
191 for (i = 0; i < s->
mb_num; i++) {
202 #define COPY(a) dst->a= src->a
227 for (i = -16; i < 16; i++) {
241 if (CONFIG_H263_ENCODER)
258 int i,
ret, format_supported;
267 "only YUV420 and YUV422 are supported\n");
273 format_supported = 0;
282 format_supported = 1;
288 format_supported = 1;
290 if (!format_supported) {
324 "keyframe interval too large!, reducing it from %d to %d\n",
353 "intra dc precision must be positive, note some applications use"
354 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
421 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
427 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
444 "impossible bitrate constraints, this will fail\n");
469 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
470 "specified vbv buffer is too large for the given bitrate!\n");
482 "OBMC is only supported with simple mb decision\n");
500 "max b frames must be 0 or positive for mpegvideo based encoders\n");
510 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
518 (avctx->
width > 2048 ||
525 ((avctx->
width &3) ||
532 (avctx->
width > 4095 ||
539 (avctx->
width > 16383 ||
540 avctx->
height > 16383 )) {
541 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
576 "mpeg2 style quantization not supported by codec\n");
594 "closed gop with scene change detection are not supported yet, "
595 "set threshold to 1000000000\n");
602 "low delay forcing is only available for mpeg2\n");
607 "b frames cannot be used with low delay\n");
613 if (avctx->
qmax > 12) {
615 "non linear quant only supports qmax <= 12 currently\n");
627 "multi threaded encoding not supported by codec\n");
633 "automatic thread number detection not supported by codec, "
651 "notice: b_frame_strategy only affects the first pass\n");
674 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
688 "timebase %d/%d not supported by MPEG 4 standard, "
689 "the maximum admitted value for the timebase denominator "
712 if (!CONFIG_MJPEG_ENCODER ||
719 if (!CONFIG_H261_ENCODER)
723 "The specified picture size of %dx%d is not valid for the "
724 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
733 if (!CONFIG_H263_ENCODER)
738 "The specified picture size of %dx%d is not valid for "
739 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
740 "352x288, 704x576, and 1408x1152. "
856 (
MAX_RUN + 1) * 2 *
sizeof(
int), fail);
873 2 * 64 *
sizeof(uint16_t), fail);
878 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
892 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
897 for (i = 0; i < 64; i++) {
931 #if FF_API_ERROR_RATE
938 #if FF_API_NORMALIZE_AQP
1008 if (CONFIG_MJPEG_ENCODER &&
1043 for (y = 0; y < 16; y++) {
1044 for (x = 0; x < 16; x++) {
1045 acc +=
FFABS(src[x + y * stride] - ref);
1061 for (y = 0; y < h; y += 16) {
1062 for (x = 0; x < w; x += 16) {
1063 int offset = x + y * stride;
1067 int sae =
get_sae(src + offset, mean, stride);
1069 acc += sae + 500 < sad;
1080 int i, display_picture_number = 0,
ret;
1095 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1100 if (!s->
low_delay && display_picture_number == 1)
1109 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1112 pts = display_picture_number;
1118 if (!pic_arg->
buf[0] ||
1156 int h_chroma_shift, v_chroma_shift;
1161 for (i = 0; i < 3; i++) {
1162 int src_stride = pic_arg->
linesize[i];
1164 int h_shift = i ? h_chroma_shift : 0;
1165 int v_shift = i ? v_chroma_shift : 0;
1166 int w = s->
width >> h_shift;
1167 int h = s->
height >> v_shift;
1180 if (src_stride == dst_stride)
1181 memcpy(dst, src, src_stride * h);
1186 memcpy(dst2, src, w);
1222 int64_t score64 = 0;
1224 for (plane = 0; plane < 3; plane++) {
1226 const int bw = plane ? 1 : 2;
1227 for (y = 0; y < s->
mb_height * bw; y++) {
1228 for (x = 0; x < s->
mb_width * bw; x++) {
1229 int off = p->
shared ? 0 : 16;
1230 uint8_t *dptr = p->
f->
data[plane] + 8 * (x + y * stride) + off;
1231 uint8_t *rptr = ref->
f->
data[plane] + 8 * (x + y * stride);
1235 case 0: score =
FFMAX(score, v);
break;
1236 case 1: score +=
FFABS(v);
break;
1237 case 2: score64 += v * (int64_t)v;
break;
1238 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1239 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1262 int ret, got_output;
1279 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1280 int64_t best_rd = INT64_MAX;
1281 int best_b_count = -1;
1291 b_lambda = p_lambda;
1316 pre_input = *pre_input_ptr;
1317 memcpy(data, pre_input_ptr->
f->
data,
sizeof(data));
1319 if (!pre_input.
shared && i) {
1387 return best_b_count;
1460 b_frames =
FFMAX(0, i - 1);
1463 for (i = 0; i < b_frames + 1; i++) {
1475 for (i = b_frames - 1; i >= 0; i--) {
1483 "warning, too many b frames in a row\n");
1506 for (i = 0; i < b_frames; i++) {
1552 for (i = 0; i < 4; i++) {
1612 for (intra = 0; intra < 2; intra++) {
1614 for (i = 0; i < 64; i++) {
1620 for (i = 0; i < 64; i++) {
1671 for (i = 0; i < 4; i++) {
1702 const AVFrame *pic_arg,
int *got_packet)
1705 int i, stuffing_count,
ret;
1732 for (i = 0; i < context_count; i++) {
1749 if (growing_buffer) {
1800 for (i = 0; i < context_count; i++) {
1814 for (i = 0; i < 4; i++) {
1830 if (stuffing_count) {
1832 stuffing_count + 50) {
1840 while (stuffing_count--) {
1847 stuffing_count -= 4;
1848 while (stuffing_count--) {
1874 "Internal error, negative bits\n");
1882 vbv_delay =
FFMAX(vbv_delay, min_delay);
1922 *got_packet = !!pkt->
size;
1927 int n,
int threshold)
1929 static const char tab[64] = {
1930 3, 2, 2, 1, 1, 1, 1, 1,
1931 1, 1, 1, 1, 1, 1, 1, 1,
1932 1, 1, 1, 1, 1, 1, 1, 1,
1933 0, 0, 0, 0, 0, 0, 0, 0,
1934 0, 0, 0, 0, 0, 0, 0, 0,
1935 0, 0, 0, 0, 0, 0, 0, 0,
1936 0, 0, 0, 0, 0, 0, 0, 0,
1937 0, 0, 0, 0, 0, 0, 0, 0
1946 if (threshold < 0) {
1948 threshold = -threshold;
1953 if (last_index <= skip_dc - 1)
1956 for (i = 0; i <= last_index; i++) {
1960 if (skip_dc && i == 0)
1964 }
else if (level > 1) {
1970 if (score >= threshold)
1972 for (i = skip_dc; i <= last_index; i++) {
1995 for (; i <= last_index; i++) {
1997 int level = block[j];
1999 if (level > maxlevel) {
2002 }
else if (level < minlevel) {
2012 "warning, clipping %d dct coefficients to %d..%d\n",
2013 overflow, minlevel, maxlevel);
2020 for (y = 0; y < 8; y++) {
2021 for (x = 0; x < 8; x++) {
2027 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2028 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2029 int v = ptr[x2 + y2 * stride];
2035 weight[x + 8 *
y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2041 int motion_x,
int motion_y,
2042 int mb_block_height,
2047 int16_t orig[12][64];
2054 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2055 ptrdiff_t wrap_y, wrap_c;
2057 for (i = 0; i < mb_block_count; i++)
2061 const int last_qp = s->
qscale;
2062 const int mb_xy = mb_x + mb_y * s->
mb_stride;
2093 (mb_y * 16 * wrap_y) + mb_x * 16;
2095 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2097 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2105 16, 16, mb_x * 16, mb_y * 16,
2110 mb_block_width, mb_block_height,
2111 mb_x * mb_block_width, mb_y * mb_block_height,
2113 ptr_cb = ebuf + 16 * wrap_y;
2116 mb_block_width, mb_block_height,
2117 mb_x * mb_block_width, mb_y * mb_block_height,
2119 ptr_cr = ebuf + 16 * wrap_y + 16;
2124 int progressive_score, interlaced_score;
2127 progressive_score = s->
mecc.
ildct_cmp[4](
s, ptr_y, NULL, wrap_y, 8) +
2129 NULL, wrap_y, 8) - 400;
2131 if (progressive_score > 0) {
2133 NULL, wrap_y * 2, 8) +
2135 NULL, wrap_y * 2, 8);
2136 if (progressive_score > interlaced_score) {
2139 dct_offset = wrap_y;
2140 uv_dct_offset = wrap_c;
2175 uint8_t *dest_y, *dest_cb, *dest_cr;
2177 dest_y = s->
dest[0];
2178 dest_cb = s->
dest[1];
2179 dest_cr = s->
dest[2];
2203 int progressive_score, interlaced_score;
2206 progressive_score = s->
mecc.
ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2212 progressive_score -= 400;
2214 if (progressive_score > 0) {
2221 if (progressive_score > interlaced_score) {
2224 dct_offset = wrap_y;
2225 uv_dct_offset = wrap_c;
2236 dest_y + dct_offset, wrap_y);
2238 dest_y + dct_offset + 8, wrap_y);
2248 dest_cb + uv_dct_offset, wrap_c);
2250 dest_cr + uv_dct_offset, wrap_c);
2257 if (s->
mecc.
sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->
qscale)
2259 if (s->
mecc.
sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->
qscale)
2261 if (s->
mecc.
sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2262 wrap_y, 8) < 20 * s->
qscale)
2264 if (s->
mecc.
sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2265 wrap_y, 8) < 20 * s->
qscale)
2267 if (s->
mecc.
sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->
qscale)
2269 if (s->
mecc.
sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->
qscale)
2272 if (s->
mecc.
sad[1](NULL, ptr_cb + uv_dct_offset,
2273 dest_cb + uv_dct_offset,
2274 wrap_c, 8) < 20 * s->
qscale)
2276 if (s->
mecc.
sad[1](NULL, ptr_cr + uv_dct_offset,
2277 dest_cr + uv_dct_offset,
2278 wrap_c, 8) < 20 * s->
qscale)
2305 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2311 for (i = 0; i < mb_block_count; i++) {
2326 for (i = 0; i < mb_block_count; i++) {
2336 for (i = 0; i < 4; i++)
2339 for (i = 4; i < mb_block_count; i++)
2343 for (i = 0; i < mb_block_count; i++) {
2356 for (i=6; i<12; i++) {
2365 for (i = 0; i < mb_block_count; i++) {
2368 for (j = 63; j > 0; j--) {
2381 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2385 if (CONFIG_MPEG4_ENCODER)
2395 if (CONFIG_WMV2_ENCODER)
2399 if (CONFIG_H261_ENCODER)
2407 if (CONFIG_H263_ENCODER)
2412 if (CONFIG_MJPEG_ENCODER)
2458 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2496 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2504 s->
pb= pb[*next_block];
2506 s->
pb2 = pb2 [*next_block];
2507 s->
tex_pb= tex_pb[*next_block];
2511 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2534 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2551 return s->
mecc.
sse[0](NULL, src1, src2, stride, 16);
2552 else if(w==8 && h==8)
2553 return s->
mecc.
sse[1](NULL, src1, src2, stride, 8);
2557 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2642 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2650 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2685 if (CONFIG_H263_ENCODER)
2687 bytestream_put_le32(&ptr, offset);
2688 bytestream_put_byte(&ptr, s->
qscale);
2689 bytestream_put_byte(&ptr, gobn);
2690 bytestream_put_le16(&ptr, mba);
2691 bytestream_put_byte(&ptr, pred_x);
2692 bytestream_put_byte(&ptr, pred_y);
2694 bytestream_put_byte(&ptr, 0);
2695 bytestream_put_byte(&ptr, 0);
2771 if (CONFIG_H263_ENCODER)
2791 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2808 int new_buffer_size = 0;
2845 int current_packet_size, is_gob_start;
2851 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2863 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2868 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2888 current_packet_size=0;
2902 if (CONFIG_MPEG4_ENCODER) {
2909 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2916 if (CONFIG_H263_ENCODER)
2946 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2963 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2964 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2975 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2976 &dmin, &next_block, 0, 0);
2984 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2985 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2995 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2996 &dmin, &next_block, 0, 0);
3004 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3005 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3013 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3014 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
3024 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3025 &dmin, &next_block, 0, 0);
3036 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3037 &dmin, &next_block, 0, 0);
3048 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3049 &dmin, &next_block, 0, 0);
3055 for(dir=0; dir<2; dir++){
3062 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3063 &dmin, &next_block, 0, 0);
3071 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3072 &dmin, &next_block, 0, 0);
3083 const int last_qp= backup_s.
qscale;
3087 static const int dquant_tab[4]={-1,1,-2,2};
3096 s->
mv[0][0][0] = best_s.
mv[0][0][0];
3097 s->
mv[0][0][1] = best_s.
mv[0][0][1];
3098 s->
mv[1][0][0] = best_s.
mv[1][0][0];
3099 s->
mv[1][0][1] = best_s.
mv[1][0][1];
3102 for(; qpi<4; qpi++){
3103 int dquant= dquant_tab[qpi];
3115 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3116 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3136 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3137 &dmin, &next_block, mx, my);
3144 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3145 &dmin, &next_block, 0, 0);
3153 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3174 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3175 &dmin, &next_block, mx, my);
3202 if (CONFIG_H263_ENCODER &&
3215 int motion_x = 0, motion_y = 0;
3223 motion_x= s->
mv[0][0][0] = 0;
3224 motion_y= s->
mv[0][0][1] = 0;
3252 if (CONFIG_MPEG4_ENCODER) {
3261 if (CONFIG_MPEG4_ENCODER) {
3311 for(dir=0; dir<2; dir++){
3328 if (CONFIG_H263_ENCODER &&
3385 #define MERGE(field) dst->field += src->field; src->field=0
3412 for(i=0; i<64; i++){
3439 if (CONFIG_MPEG4_ENCODER)
3445 if (CONFIG_H263_ENCODER)
3526 for(i=1; i<context_count; i++){
3556 for(i=1; i<context_count; i++){
3569 av_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3613 for(dir=0; dir<2; dir++){
3662 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};
3663 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};
3694 if (CONFIG_MJPEG_ENCODER)
3699 if (CONFIG_H261_ENCODER)
3707 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred)
3715 else if (CONFIG_H263_ENCODER)
3719 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3728 for(i=1; i<context_count; i++){
3732 for(i=1; i<context_count; i++){
3745 for(i=0; i<64; i++){
3746 int level= block[i];
3752 if(level<0) level=0;
3756 if(level>0) level=0;
3765 int qscale,
int *overflow){
3770 unsigned int threshold1, threshold2;
3782 int coeff_count[64];
3783 int qmul, qadd, start_i, last_non_zero, i,
dc;
3794 qadd= ((qscale-1)|1)*8;
3811 block[0] = (block[0] + (q >> 1)) / q;
3829 threshold2= (threshold1<<1);
3831 for(i=63; i>=start_i; i--) {
3832 const int j = scantable[i];
3833 int level = block[j] * qmat[j];
3835 if(((
unsigned)(level+threshold1))>threshold2){
3841 for(i=start_i; i<=last_non_zero; i++) {
3842 const int j = scantable[i];
3843 int level = block[j] * qmat[j];
3847 if(((
unsigned)(level+threshold1))>threshold2){
3851 coeff[1][i]= level-1;
3855 coeff[0][i]= -
level;
3856 coeff[1][i]= -level+1;
3859 coeff_count[i]=
FFMIN(level, 2);
3863 coeff[0][i]= (level>>31)|1;
3870 if(last_non_zero < start_i){
3871 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3872 return last_non_zero;
3875 score_tab[start_i]= 0;
3876 survivor[0]= start_i;
3879 for(i=start_i; i<=last_non_zero; i++){
3880 int level_index, j, zero_distortion;
3881 int dct_coeff=
FFABS(block[ scantable[i] ]);
3882 int best_score=256*256*256*120;
3886 zero_distortion= dct_coeff*dct_coeff;
3888 for(level_index=0; level_index < coeff_count[i]; level_index++){
3890 int level= coeff[level_index][i];
3891 const int alevel=
FFABS(level);
3897 unquant_coeff= alevel*qmul + qadd;
3901 unquant_coeff = (int)( alevel * qscale * s->
intra_matrix[j]) >> 3;
3902 unquant_coeff = (unquant_coeff - 1) | 1;
3904 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[j])) >> 4;
3905 unquant_coeff = (unquant_coeff - 1) | 1;
3910 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3912 if((level&(~127)) == 0){
3913 for(j=survivor_count-1; j>=0; j--){
3914 int run= i - survivor[j];
3916 score += score_tab[i-
run];
3918 if(score < best_score){
3921 level_tab[i+1]= level-64;
3926 for(j=survivor_count-1; j>=0; j--){
3927 int run= i - survivor[j];
3929 score += score_tab[i-
run];
3930 if(score < last_score){
3933 last_level= level-64;
3939 distortion += esc_length*
lambda;
3940 for(j=survivor_count-1; j>=0; j--){
3941 int run= i - survivor[j];
3942 int score= distortion + score_tab[i-
run];
3944 if(score < best_score){
3947 level_tab[i+1]= level-64;
3952 for(j=survivor_count-1; j>=0; j--){
3953 int run= i - survivor[j];
3954 int score= distortion + score_tab[i-
run];
3955 if(score < last_score){
3958 last_level= level-64;
3966 score_tab[i+1]= best_score;
3969 if(last_non_zero <= 27){
3970 for(; survivor_count; survivor_count--){
3971 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3975 for(; survivor_count; survivor_count--){
3976 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3981 survivor[ survivor_count++ ]= i+1;
3985 last_score= 256*256*256*120;
3986 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3987 int score= score_tab[i];
3988 if(i) score += lambda*2;
3990 if(score < last_score){
3993 last_level= level_tab[i];
3994 last_run= run_tab[i];
4001 dc=
FFABS(block[0]);
4002 last_non_zero= last_i - 1;
4003 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4005 if(last_non_zero < start_i)
4006 return last_non_zero;
4008 if(last_non_zero == 0 && start_i == 0){
4010 int best_score= dc *
dc;
4012 for(i=0; i<coeff_count[0]; i++){
4013 int level= coeff[i][0];
4014 int alevel=
FFABS(level);
4015 int unquant_coeff, score, distortion;
4018 unquant_coeff= (alevel*qmul + qadd)>>3;
4020 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[0])) >> 4;
4021 unquant_coeff = (unquant_coeff - 1) | 1;
4023 unquant_coeff = (unquant_coeff + 4) >> 3;
4024 unquant_coeff<<= 3 + 3;
4026 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
4029 else score= distortion + esc_length*
lambda;
4031 if(score < best_score){
4033 best_level= level - 64;
4036 block[0]= best_level;
4038 if(best_level == 0)
return -1;
4039 else return last_non_zero;
4045 block[ perm_scantable[last_non_zero] ]= last_level;
4048 for(; i>start_i; i -= run_tab[i] + 1){
4049 block[ perm_scantable[i-1] ]= level_tab[i];
4052 return last_non_zero;
4067 int perm_index= perm[
index];
4068 if(i==0) s*= sqrt(0.5);
4069 if(j==0) s*= sqrt(0.5);
4070 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)));
4089 int qmul, qadd, start_i, last_non_zero, i,
dc;
4093 int rle_index,
run, q = 1, sum;
4096 static int after_last=0;
4097 static int to_zero=0;
4098 static int from_zero=0;
4101 static int messed_sign=0;
4104 if(basis[0][0] == 0)
4141 for(i=0; i<64; i++){
4148 for(i=0; i<64; i++){
4153 w=
FFABS(weight[i]) + qns*one;
4154 w= 15 + (48*qns*one + w/2)/w;
4169 for(i=start_i; i<=last_non_zero; i++){
4170 int j= perm_scantable[i];
4171 const int level= block[j];
4175 if(level<0) coeff= qmul*level - qadd;
4176 else coeff= qmul*level + qadd;
4177 run_tab[rle_index++]=
run;
4186 if(last_non_zero>0){
4197 int run2, best_unquant_change=0, analyze_gradient;
4203 if(analyze_gradient){
4207 for(i=0; i<64; i++){
4223 const int level= block[0];
4224 int change, old_coeff;
4230 for(change=-1; change<=1; change+=2){
4231 int new_level= level + change;
4232 int score, new_coeff;
4234 new_coeff= q*new_level;
4235 if(new_coeff >= 2048 || new_coeff < 0)
4239 new_coeff - old_coeff);
4240 if(score<best_score){
4243 best_change= change;
4244 best_unquant_change= new_coeff - old_coeff;
4251 run2= run_tab[rle_index++];
4255 for(i=start_i; i<64; i++){
4256 int j= perm_scantable[i];
4257 const int level= block[j];
4258 int change, old_coeff;
4264 if(level<0) old_coeff= qmul*level - qadd;
4265 else old_coeff= qmul*level + qadd;
4266 run2= run_tab[rle_index++];
4273 for(change=-1; change<=1; change+=2){
4274 int new_level= level + change;
4275 int score, new_coeff, unquant_change;
4282 if(new_level<0) new_coeff= qmul*new_level - qadd;
4283 else new_coeff= qmul*new_level + qadd;
4284 if(new_coeff >= 2048 || new_coeff <= -2048)
4289 if(level < 63 && level > -63){
4290 if(i < last_non_zero)
4300 if(analyze_gradient){
4301 int g= d1[ scantable[i] ];
4302 if(g && (g^new_level) >= 0)
4306 if(i < last_non_zero){
4307 int next_i= i + run2 + 1;
4308 int next_level= block[ perm_scantable[next_i] ] + 64;
4310 if(next_level&(~127))
4313 if(next_i < last_non_zero)
4333 if(i < last_non_zero){
4334 int next_i= i + run2 + 1;
4335 int next_level= block[ perm_scantable[next_i] ] + 64;
4337 if(next_level&(~127))
4340 if(next_i < last_non_zero)
4359 unquant_change= new_coeff - old_coeff;
4360 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4364 if(score<best_score){
4367 best_change= change;
4368 best_unquant_change= unquant_change;
4372 prev_level= level + 64;
4373 if(prev_level&(~127))
4386 int j= perm_scantable[ best_coeff ];
4388 block[j] += best_change;
4390 if(best_coeff > last_non_zero){
4391 last_non_zero= best_coeff;
4399 if(block[j] - best_change){
4400 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
4412 for(; last_non_zero>=start_i; last_non_zero--){
4413 if(block[perm_scantable[last_non_zero]])
4419 if(256*256*256*64 % count == 0){
4420 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);
4425 for(i=start_i; i<=last_non_zero; i++){
4426 int j= perm_scantable[i];
4427 const int level= block[j];
4430 run_tab[rle_index++]=
run;
4443 if(last_non_zero>0){
4449 return last_non_zero;
4454 int qscale,
int *overflow)
4456 int i, j,
level, last_non_zero, q, start_i;
4461 unsigned int threshold1, threshold2;
4480 block[0] = (block[0] + (q >> 1)) / q;
4492 threshold2= (threshold1<<1);
4493 for(i=63;i>=start_i;i--) {
4495 level = block[j] * qmat[j];
4497 if(((
unsigned)(level+threshold1))>threshold2){
4504 for(i=start_i; i<=last_non_zero; i++) {
4506 level = block[j] * qmat[j];
4510 if(((
unsigned)(level+threshold1))>threshold2){
4528 scantable, last_non_zero);
4530 return last_non_zero;
4533 #define OFFSET(x) offsetof(MpegEncContext, x)
4534 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4538 {
"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 },
4595 .
name =
"msmpeg4v2",
4604 .priv_class = &msmpeg4v2_class,
4619 .priv_class = &msmpeg4v3_class,
4634 .priv_class = &wmv1_class,