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++) {
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) {
325 "keyframe interval too large!, reducing it from %d to %d\n",
354 "intra dc precision must be positive, note some applications use"
355 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
422 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
428 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
445 "impossible bitrate constraints, this will fail\n");
470 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
471 "specified vbv buffer is too large for the given bitrate!\n");
483 "OBMC is only supported with simple mb decision\n");
501 "max b frames must be 0 or positive for mpegvideo based encoders\n");
511 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
519 (avctx->
width > 2048 ||
526 ((avctx->
width &3) ||
533 (avctx->
width > 4095 ||
540 (avctx->
width > 16383 ||
541 avctx->
height > 16383 )) {
542 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
577 "mpeg2 style quantization not supported by codec\n");
595 "closed gop with scene change detection are not supported yet, "
596 "set threshold to 1000000000\n");
603 "low delay forcing is only available for mpeg2\n");
608 "b frames cannot be used with low delay\n");
614 if (avctx->
qmax > 12) {
616 "non linear quant only supports qmax <= 12 currently\n");
628 "multi threaded encoding not supported by codec\n");
634 "automatic thread number detection not supported by codec, "
652 "notice: b_frame_strategy only affects the first pass\n");
675 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
689 "timebase %d/%d not supported by MPEG 4 standard, "
690 "the maximum admitted value for the timebase denominator "
724 "The specified picture size of %dx%d is not valid for the "
725 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
740 "The specified picture size of %dx%d is not valid for "
741 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
742 "352x288, 704x576, and 1408x1152. "
858 (
MAX_RUN + 1) * 2 *
sizeof(
int), fail);
875 2 * 64 *
sizeof(uint16_t), fail);
900 for (i = 0; i < 64; i++) {
935 #if FF_API_ERROR_RATE
942 #if FF_API_NORMALIZE_AQP
1047 for (y = 0; y < 16; y++) {
1048 for (x = 0; x < 16; x++) {
1049 acc +=
FFABS(src[x + y * stride] - ref);
1065 for (y = 0; y < h; y += 16) {
1066 for (x = 0; x < w; x += 16) {
1067 int offset = x + y * stride;
1071 int sae =
get_sae(src + offset, mean, stride);
1073 acc += sae + 500 < sad;
1084 int i, display_picture_number = 0,
ret;
1099 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1104 if (!s->
low_delay && display_picture_number == 1)
1113 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1116 pts = display_picture_number;
1122 if (!pic_arg->
buf[0] ||
1160 int h_chroma_shift, v_chroma_shift;
1165 for (i = 0; i < 3; i++) {
1166 int src_stride = pic_arg->
linesize[i];
1168 int h_shift = i ? h_chroma_shift : 0;
1169 int v_shift = i ? v_chroma_shift : 0;
1170 int w = s->
width >> h_shift;
1171 int h = s->
height >> v_shift;
1184 if (src_stride == dst_stride)
1185 memcpy(dst, src, src_stride * h);
1190 memcpy(dst2, src, w);
1226 int64_t score64 = 0;
1228 for (plane = 0; plane < 3; plane++) {
1230 const int bw = plane ? 1 : 2;
1231 for (y = 0; y < s->
mb_height * bw; y++) {
1232 for (x = 0; x < s->
mb_width * bw; x++) {
1233 int off = p->
shared ? 0 : 16;
1234 uint8_t *dptr = p->
f->
data[plane] + 8 * (x + y * stride) + off;
1235 uint8_t *rptr = ref->
f->
data[plane] + 8 * (x + y * stride);
1239 case 0: score =
FFMAX(score, v);
break;
1240 case 1: score +=
FFABS(v);
break;
1241 case 2: score64 += v * (int64_t)v;
break;
1242 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1243 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1266 int ret, got_output;
1283 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1284 int64_t best_rd = INT64_MAX;
1285 int best_b_count = -1;
1295 b_lambda = p_lambda;
1320 pre_input = *pre_input_ptr;
1321 memcpy(data, pre_input_ptr->
f->
data,
sizeof(data));
1323 if (!pre_input.
shared && i) {
1391 return best_b_count;
1464 b_frames =
FFMAX(0, i - 1);
1467 for (i = 0; i < b_frames + 1; i++) {
1479 for (i = b_frames - 1; i >= 0; i--) {
1487 "warning, too many b frames in a row\n");
1510 for (i = 0; i < b_frames; i++) {
1556 for (i = 0; i < 4; i++) {
1616 for (intra = 0; intra < 2; intra++) {
1618 for (i = 0; i < 64; i++) {
1624 for (i = 0; i < 64; i++) {
1675 for (i = 0; i < 4; i++) {
1706 const AVFrame *pic_arg,
int *got_packet)
1709 int i, stuffing_count,
ret;
1736 for (i = 0; i < context_count; i++) {
1753 if (growing_buffer) {
1804 for (i = 0; i < context_count; i++) {
1818 for (i = 0; i < 4; i++) {
1834 if (stuffing_count) {
1836 stuffing_count + 50) {
1844 while (stuffing_count--) {
1851 stuffing_count -= 4;
1852 while (stuffing_count--) {
1878 "Internal error, negative bits\n");
1886 vbv_delay =
FFMAX(vbv_delay, min_delay);
1926 *got_packet = !!pkt->
size;
1931 int n,
int threshold)
1933 static const char tab[64] = {
1934 3, 2, 2, 1, 1, 1, 1, 1,
1935 1, 1, 1, 1, 1, 1, 1, 1,
1936 1, 1, 1, 1, 1, 1, 1, 1,
1937 0, 0, 0, 0, 0, 0, 0, 0,
1938 0, 0, 0, 0, 0, 0, 0, 0,
1939 0, 0, 0, 0, 0, 0, 0, 0,
1940 0, 0, 0, 0, 0, 0, 0, 0,
1941 0, 0, 0, 0, 0, 0, 0, 0
1950 if (threshold < 0) {
1952 threshold = -threshold;
1957 if (last_index <= skip_dc - 1)
1960 for (i = 0; i <= last_index; i++) {
1964 if (skip_dc && i == 0)
1968 }
else if (level > 1) {
1974 if (score >= threshold)
1976 for (i = skip_dc; i <= last_index; i++) {
1999 for (; i <= last_index; i++) {
2001 int level = block[j];
2003 if (level > maxlevel) {
2006 }
else if (level < minlevel) {
2016 "warning, clipping %d dct coefficients to %d..%d\n",
2017 overflow, minlevel, maxlevel);
2024 for (y = 0; y < 8; y++) {
2025 for (x = 0; x < 8; x++) {
2031 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2032 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2033 int v = ptr[x2 + y2 * stride];
2039 weight[x + 8 *
y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2045 int motion_x,
int motion_y,
2046 int mb_block_height,
2051 int16_t orig[12][64];
2058 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2059 ptrdiff_t wrap_y, wrap_c;
2061 for (i = 0; i < mb_block_count; i++)
2065 const int last_qp = s->
qscale;
2066 const int mb_xy = mb_x + mb_y * s->
mb_stride;
2097 (mb_y * 16 * wrap_y) + mb_x * 16;
2099 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2101 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2109 16, 16, mb_x * 16, mb_y * 16,
2114 mb_block_width, mb_block_height,
2115 mb_x * mb_block_width, mb_y * mb_block_height,
2117 ptr_cb = ebuf + 16 * wrap_y;
2120 mb_block_width, mb_block_height,
2121 mb_x * mb_block_width, mb_y * mb_block_height,
2123 ptr_cr = ebuf + 16 * wrap_y + 16;
2128 int progressive_score, interlaced_score;
2133 NULL, wrap_y, 8) - 400;
2135 if (progressive_score > 0) {
2137 NULL, wrap_y * 2, 8) +
2139 NULL, wrap_y * 2, 8);
2140 if (progressive_score > interlaced_score) {
2143 dct_offset = wrap_y;
2144 uv_dct_offset = wrap_c;
2179 uint8_t *dest_y, *dest_cb, *dest_cr;
2181 dest_y = s->
dest[0];
2182 dest_cb = s->
dest[1];
2183 dest_cr = s->
dest[2];
2207 int progressive_score, interlaced_score;
2210 progressive_score = s->
mecc.
ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2216 progressive_score -= 400;
2218 if (progressive_score > 0) {
2225 if (progressive_score > interlaced_score) {
2228 dct_offset = wrap_y;
2229 uv_dct_offset = wrap_c;
2240 dest_y + dct_offset, wrap_y);
2242 dest_y + dct_offset + 8, wrap_y);
2252 dest_cb + uv_dct_offset, wrap_c);
2254 dest_cr + uv_dct_offset, wrap_c);
2265 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset, dest_y + dct_offset,
2266 wrap_y, 8) < 20 * s->
qscale)
2268 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2269 wrap_y, 8) < 20 * s->
qscale)
2277 dest_cb + uv_dct_offset,
2278 wrap_c, 8) < 20 * s->
qscale)
2281 dest_cr + uv_dct_offset,
2282 wrap_c, 8) < 20 * s->
qscale)
2309 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2315 for (i = 0; i < mb_block_count; i++) {
2330 for (i = 0; i < mb_block_count; i++) {
2340 for (i = 0; i < 4; i++)
2343 for (i = 4; i < mb_block_count; i++)
2347 for (i = 0; i < mb_block_count; i++) {
2360 for (i=6; i<12; i++) {
2369 for (i = 0; i < mb_block_count; i++) {
2372 for (j = 63; j > 0; j--) {
2462 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2500 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2508 s->
pb= pb[*next_block];
2510 s->
pb2 = pb2 [*next_block];
2511 s->
tex_pb= tex_pb[*next_block];
2515 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2538 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2555 return s->
mecc.
sse[0](
NULL, src1, src2, stride, 16);
2556 else if(w==8 && h==8)
2557 return s->
mecc.
sse[1](
NULL, src1, src2, stride, 8);
2561 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2646 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2654 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2691 bytestream_put_le32(&ptr, offset);
2692 bytestream_put_byte(&ptr, s->
qscale);
2693 bytestream_put_byte(&ptr, gobn);
2694 bytestream_put_le16(&ptr, mba);
2695 bytestream_put_byte(&ptr, pred_x);
2696 bytestream_put_byte(&ptr, pred_y);
2698 bytestream_put_byte(&ptr, 0);
2699 bytestream_put_byte(&ptr, 0);
2734 int new_buffer_size = 0;
2829 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2863 int current_packet_size, is_gob_start;
2869 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2878 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2883 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2903 current_packet_size=0;
2961 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2968 backup_s.pb2= s->
pb2;
2969 backup_s.tex_pb= s->
tex_pb;
2978 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2979 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2990 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2991 &dmin, &next_block, 0, 0);
2999 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3000 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3010 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3011 &dmin, &next_block, 0, 0);
3019 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3020 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3028 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3029 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
3039 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3040 &dmin, &next_block, 0, 0);
3051 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3052 &dmin, &next_block, 0, 0);
3063 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3064 &dmin, &next_block, 0, 0);
3070 for(dir=0; dir<2; dir++){
3077 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3078 &dmin, &next_block, 0, 0);
3086 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3087 &dmin, &next_block, 0, 0);
3098 const int last_qp= backup_s.qscale;
3102 static const int dquant_tab[4]={-1,1,-2,2};
3111 s->
mv[0][0][0] = best_s.
mv[0][0][0];
3112 s->
mv[0][0][1] = best_s.
mv[0][0][1];
3113 s->
mv[1][0][0] = best_s.
mv[1][0][0];
3114 s->
mv[1][0][1] = best_s.
mv[1][0][1];
3117 for(; qpi<4; qpi++){
3118 int dquant= dquant_tab[qpi];
3130 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3131 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3147 backup_s.dquant = 0;
3151 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3152 &dmin, &next_block, mx, my);
3155 backup_s.dquant = 0;
3159 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3160 &dmin, &next_block, 0, 0);
3168 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3189 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3190 &dmin, &next_block, mx, my);
3208 s->
pb2= backup_s.pb2;
3212 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3213 s->
tex_pb= backup_s.tex_pb;
3230 int motion_x = 0, motion_y = 0;
3238 motion_x= s->
mv[0][0][0] = 0;
3239 motion_y= s->
mv[0][0][1] = 0;
3326 for(dir=0; dir<2; dir++){
3400 #define MERGE(field) dst->field += src->field; src->field=0
3427 for(i=0; i<64; i++){
3541 for(i=1; i<context_count; i++){
3571 for(i=1; i<context_count; i++){
3584 av_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3628 for(dir=0; dir<2; dir++){
3677 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};
3678 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};
3748 for(i=1; i<context_count; i++){
3752 for(i=1; i<context_count; i++){
3767 for(i=0; i<64; i++){
3768 int level= block[i];
3774 if(level<0) level=0;
3778 if(level>0) level=0;
3787 int qscale,
int *overflow){
3789 const uint16_t *matrix;
3793 unsigned int threshold1, threshold2;
3805 int coeff_count[64];
3806 int qmul, qadd, start_i, last_non_zero, i,
dc;
3817 qadd= ((qscale-1)|1)*8;
3834 block[0] = (block[0] + (q >> 1)) / q;
3860 threshold2= (threshold1<<1);
3862 for(i=63; i>=start_i; i--) {
3863 const int j = scantable[i];
3864 int level = block[j] * qmat[j];
3866 if(((
unsigned)(level+threshold1))>threshold2){
3872 for(i=start_i; i<=last_non_zero; i++) {
3873 const int j = scantable[i];
3874 int level = block[j] * qmat[j];
3878 if(((
unsigned)(level+threshold1))>threshold2){
3882 coeff[1][i]= level-1;
3886 coeff[0][i]= -
level;
3887 coeff[1][i]= -level+1;
3890 coeff_count[i]=
FFMIN(level, 2);
3894 coeff[0][i]= (level>>31)|1;
3901 if(last_non_zero < start_i){
3902 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3903 return last_non_zero;
3906 score_tab[start_i]= 0;
3907 survivor[0]= start_i;
3910 for(i=start_i; i<=last_non_zero; i++){
3911 int level_index, j, zero_distortion;
3912 int dct_coeff=
FFABS(block[ scantable[i] ]);
3913 int best_score=256*256*256*120;
3917 zero_distortion= dct_coeff*dct_coeff;
3919 for(level_index=0; level_index < coeff_count[i]; level_index++){
3921 int level= coeff[level_index][i];
3922 const int alevel=
FFABS(level);
3928 unquant_coeff= alevel*qmul + qadd;
3931 unquant_coeff = alevel * matrix[j] * 8;
3935 unquant_coeff = (int)( alevel * qscale * matrix[j]) >> 3;
3936 unquant_coeff = (unquant_coeff - 1) | 1;
3938 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) matrix[j])) >> 4;
3939 unquant_coeff = (unquant_coeff - 1) | 1;
3944 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3946 if((level&(~127)) == 0){
3947 for(j=survivor_count-1; j>=0; j--){
3948 int run= i - survivor[j];
3950 score += score_tab[i-
run];
3952 if(score < best_score){
3955 level_tab[i+1]= level-64;
3960 for(j=survivor_count-1; j>=0; j--){
3961 int run= i - survivor[j];
3963 score += score_tab[i-
run];
3964 if(score < last_score){
3967 last_level= level-64;
3973 distortion += esc_length*
lambda;
3974 for(j=survivor_count-1; j>=0; j--){
3975 int run= i - survivor[j];
3976 int score= distortion + score_tab[i-
run];
3978 if(score < best_score){
3981 level_tab[i+1]= level-64;
3986 for(j=survivor_count-1; j>=0; j--){
3987 int run= i - survivor[j];
3988 int score= distortion + score_tab[i-
run];
3989 if(score < last_score){
3992 last_level= level-64;
4000 score_tab[i+1]= best_score;
4003 if(last_non_zero <= 27){
4004 for(; survivor_count; survivor_count--){
4005 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4009 for(; survivor_count; survivor_count--){
4010 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4015 survivor[ survivor_count++ ]= i+1;
4019 last_score= 256*256*256*120;
4020 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4021 int score= score_tab[i];
4022 if(i) score += lambda*2;
4024 if(score < last_score){
4027 last_level= level_tab[i];
4028 last_run= run_tab[i];
4035 dc=
FFABS(block[0]);
4036 last_non_zero= last_i - 1;
4037 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4039 if(last_non_zero < start_i)
4040 return last_non_zero;
4042 if(last_non_zero == 0 && start_i == 0){
4044 int best_score= dc *
dc;
4046 for(i=0; i<coeff_count[0]; i++){
4047 int level= coeff[i][0];
4048 int alevel=
FFABS(level);
4049 int unquant_coeff, score, distortion;
4052 unquant_coeff= (alevel*qmul + qadd)>>3;
4054 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) matrix[0])) >> 4;
4055 unquant_coeff = (unquant_coeff - 1) | 1;
4057 unquant_coeff = (unquant_coeff + 4) >> 3;
4058 unquant_coeff<<= 3 + 3;
4060 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
4063 else score= distortion + esc_length*
lambda;
4065 if(score < best_score){
4067 best_level= level - 64;
4070 block[0]= best_level;
4072 if(best_level == 0)
return -1;
4073 else return last_non_zero;
4079 block[ perm_scantable[last_non_zero] ]= last_level;
4082 for(; i>start_i; i -= run_tab[i] + 1){
4083 block[ perm_scantable[i-1] ]= level_tab[i];
4086 return last_non_zero;
4101 int perm_index= perm[
index];
4102 if(i==0) s*= sqrt(0.5);
4103 if(j==0) s*= sqrt(0.5);
4104 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)));
4123 int qmul, qadd, start_i, last_non_zero, i,
dc;
4127 int rle_index,
run, q = 1, sum;
4130 static int after_last=0;
4131 static int to_zero=0;
4132 static int from_zero=0;
4135 static int messed_sign=0;
4138 if(basis[0][0] == 0)
4180 for(i=0; i<64; i++){
4187 for(i=0; i<64; i++){
4192 w=
FFABS(weight[i]) + qns*one;
4193 w= 15 + (48*qns*one + w/2)/w;
4208 for(i=start_i; i<=last_non_zero; i++){
4209 int j= perm_scantable[i];
4210 const int level= block[j];
4214 if(level<0) coeff= qmul*level - qadd;
4215 else coeff= qmul*level + qadd;
4216 run_tab[rle_index++]=
run;
4225 if(last_non_zero>0){
4236 int run2, best_unquant_change=0, analyze_gradient;
4242 if(analyze_gradient){
4246 for(i=0; i<64; i++){
4262 const int level= block[0];
4263 int change, old_coeff;
4269 for(change=-1; change<=1; change+=2){
4270 int new_level= level + change;
4271 int score, new_coeff;
4273 new_coeff= q*new_level;
4274 if(new_coeff >= 2048 || new_coeff < 0)
4278 new_coeff - old_coeff);
4279 if(score<best_score){
4282 best_change= change;
4283 best_unquant_change= new_coeff - old_coeff;
4290 run2= run_tab[rle_index++];
4294 for(i=start_i; i<64; i++){
4295 int j= perm_scantable[i];
4296 const int level= block[j];
4297 int change, old_coeff;
4303 if(level<0) old_coeff= qmul*level - qadd;
4304 else old_coeff= qmul*level + qadd;
4305 run2= run_tab[rle_index++];
4312 for(change=-1; change<=1; change+=2){
4313 int new_level= level + change;
4314 int score, new_coeff, unquant_change;
4321 if(new_level<0) new_coeff= qmul*new_level - qadd;
4322 else new_coeff= qmul*new_level + qadd;
4323 if(new_coeff >= 2048 || new_coeff <= -2048)
4328 if(level < 63 && level > -63){
4329 if(i < last_non_zero)
4339 if(analyze_gradient){
4340 int g= d1[ scantable[i] ];
4341 if(g && (g^new_level) >= 0)
4345 if(i < last_non_zero){
4346 int next_i= i + run2 + 1;
4347 int next_level= block[ perm_scantable[next_i] ] + 64;
4349 if(next_level&(~127))
4352 if(next_i < last_non_zero)
4372 if(i < last_non_zero){
4373 int next_i= i + run2 + 1;
4374 int next_level= block[ perm_scantable[next_i] ] + 64;
4376 if(next_level&(~127))
4379 if(next_i < last_non_zero)
4398 unquant_change= new_coeff - old_coeff;
4399 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4403 if(score<best_score){
4406 best_change= change;
4407 best_unquant_change= unquant_change;
4411 prev_level= level + 64;
4412 if(prev_level&(~127))
4425 int j= perm_scantable[ best_coeff ];
4427 block[j] += best_change;
4429 if(best_coeff > last_non_zero){
4430 last_non_zero= best_coeff;
4438 if(block[j] - best_change){
4439 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
4451 for(; last_non_zero>=start_i; last_non_zero--){
4452 if(block[perm_scantable[last_non_zero]])
4458 if(256*256*256*64 % count == 0){
4459 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);
4464 for(i=start_i; i<=last_non_zero; i++){
4465 int j= perm_scantable[i];
4466 const int level= block[j];
4469 run_tab[rle_index++]=
run;
4482 if(last_non_zero>0){
4488 return last_non_zero;
4493 int qscale,
int *overflow)
4495 int i, j,
level, last_non_zero, q, start_i;
4500 unsigned int threshold1, threshold2;
4519 block[0] = (block[0] + (q >> 1)) / q;
4531 threshold2= (threshold1<<1);
4532 for(i=63;i>=start_i;i--) {
4534 level = block[j] * qmat[j];
4536 if(((
unsigned)(level+threshold1))>threshold2){
4543 for(i=start_i; i<=last_non_zero; i++) {
4545 level = block[j] * qmat[j];
4549 if(((
unsigned)(level+threshold1))>threshold2){
4567 scantable, last_non_zero);
4569 return last_non_zero;
4572 #define OFFSET(x) offsetof(MpegEncContext, x)
4573 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4577 {
"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 },
4634 .
name =
"msmpeg4v2",
4643 .priv_class = &msmpeg4v2_class,
4658 .priv_class = &msmpeg4v3_class,
4673 .priv_class = &wmv1_class,