70 #define QUANT_BIAS_SHIFT 8
72 #define QMAT_SHIFT_MMX 16
91 const uint16_t *quant_matrix,
92 int bias,
int qmin,
int qmax,
int intra)
98 for (qscale = qmin; qscale <= qmax; qscale++) {
103 else qscale2 = qscale << 1;
110 for (i = 0; i < 64; i++) {
112 int64_t den = (int64_t) qscale2 * quant_matrix[j];
122 for (i = 0; i < 64; i++) {
124 int64_t den =
ff_aanscales[i] * (int64_t) qscale2 * quant_matrix[j];
134 for (i = 0; i < 64; i++) {
136 int64_t den = (int64_t) qscale2 * quant_matrix[j];
147 if (
qmat16[qscale][0][i] == 0 ||
148 qmat16[qscale][0][i] == 128 * 256)
156 for (i = intra; i < 64; i++) {
161 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
168 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
177 int bestdiff=INT_MAX;
185 if (diff < bestdiff) {
207 for (i = 0; i < 64; i++) {
222 for (i = 0; i < s->
mb_num; i++) {
233 #define COPY(a) dst->a= src->a
258 for (i = -16; i < 16; i++) {
272 if (CONFIG_H263_ENCODER)
290 int i, ret, format_supported;
299 "only YUV420 and YUV422 are supported\n");
305 format_supported = 0;
314 format_supported = 1;
320 format_supported = 1;
322 if (!format_supported) {
352 #if FF_API_PRIVATE_OPT
369 "keyframe interval too large!, reducing it from %d to %d\n",
395 "intra dc precision must be positive, note some applications use"
396 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
413 #if FF_API_MOTION_EST
467 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
473 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
490 "impossible bitrate constraints, this will fail\n");
515 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
516 "specified vbv buffer is too large for the given bitrate!\n");
528 "OBMC is only supported with simple mb decision\n");
546 "max b frames must be 0 or positive for mpegvideo based encoders\n");
556 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
564 (avctx->
width > 2048 ||
571 ((avctx->
width &3) ||
578 (avctx->
width > 4095 ||
585 (avctx->
width > 16383 ||
586 avctx->
height > 16383 )) {
587 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
618 #if FF_API_PRIVATE_OPT
629 "mpeg2 style quantization not supported by codec\n");
644 #if FF_API_PRIVATE_OPT
654 "closed gop with scene change detection are not supported yet, "
655 "set threshold to 1000000000\n");
662 "low delay forcing is only available for mpeg2\n");
667 "B-frames cannot be used with low delay\n");
673 if (avctx->
qmax > 28) {
675 "non linear quant only supports qmax <= 28 currently\n");
693 "multi threaded encoding not supported by codec\n");
699 "automatic thread number detection not supported by codec, "
709 #if FF_API_PRIVATE_OPT
720 "notice: b_frame_strategy only affects the first pass\n");
743 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
747 #if FF_API_QUANT_BIAS
761 "timebase %d/%d not supported by MPEG 4 standard, "
762 "the maximum admitted value for the timebase denominator "
785 if (!CONFIG_MJPEG_ENCODER ||
792 if (!CONFIG_H261_ENCODER)
796 "The specified picture size of %dx%d is not valid for the "
797 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
807 if (!CONFIG_H263_ENCODER)
812 "The specified picture size of %dx%d is not valid for "
813 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
814 "352x288, 704x576, and 1408x1152. "
905 #if FF_API_PRIVATE_OPT
953 2 * 64 *
sizeof(uint16_t),
fail);
958 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
970 #if FF_API_PRIVATE_OPT
993 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
998 for (i = 0; i < 64; i++) {
1033 #if FF_API_ERROR_RATE
1040 #if FF_API_NORMALIZE_AQP
1080 #if FF_API_PRIVATE_OPT
1128 if (CONFIG_MJPEG_ENCODER &&
1163 for (y = 0; y < 16; y++) {
1164 for (x = 0; x < 16; x++) {
1165 acc +=
FFABS(src[x + y * stride] - ref);
1181 for (y = 0; y <
h; y += 16) {
1182 for (x = 0; x < w; x += 16) {
1187 int sae =
get_sae(src + offset, mean, stride);
1189 acc += sae + 500 < sad;
1207 int i, display_picture_number = 0, ret;
1210 int flush_offset = 1;
1223 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1228 if (!s->
low_delay && display_picture_number == 1)
1237 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1240 pts = display_picture_number;
1244 if (!pic_arg->
buf[0] ||
1280 int h_chroma_shift, v_chroma_shift;
1285 for (i = 0; i < 3; i++) {
1286 int src_stride = pic_arg->
linesize[i];
1288 int h_shift = i ? h_chroma_shift : 0;
1289 int v_shift = i ? v_chroma_shift : 0;
1290 int w = s->
width >> h_shift;
1304 if (src_stride == dst_stride)
1305 memcpy(dst, src, src_stride * h);
1310 memcpy(dst2, src, w);
1335 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1339 if (flush_offset <= 1)
1342 encoding_delay = encoding_delay - flush_offset + 1;
1358 int64_t score64 = 0;
1360 for (plane = 0; plane < 3; plane++) {
1362 const int bw = plane ? 1 : 2;
1363 for (y = 0; y < s->
mb_height * bw; y++) {
1364 for (x = 0; x < s->
mb_width * bw; x++) {
1365 int off = p->
shared ? 0 : 16;
1371 case 0: score =
FFMAX(score, v);
break;
1372 case 1: score +=
FFABS(v);
break;
1373 case 2: score64 += v * (int64_t)v;
break;
1374 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1375 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1398 int ret, got_output;
1416 int64_t best_rd = INT64_MAX;
1417 int best_b_count = -1;
1429 b_lambda = p_lambda;
1454 pre_input = *pre_input_ptr;
1455 memcpy(data, pre_input_ptr->
f->
data,
sizeof(data));
1457 if (!pre_input.
shared && i) {
1524 return best_b_count;
1597 b_frames =
FFMAX(0, i - 1);
1600 for (i = 0; i < b_frames + 1; i++) {
1609 for (i = b_frames - 1; i >= 0; i--) {
1617 "warning, too many B-frames in a row\n");
1640 for (i = 0; i < b_frames; i++) {
1687 for (i = 0; i < 4; i++) {
1737 #if FF_API_CODED_FRAME
1743 #if FF_API_ERROR_FRAME
1755 for (intra = 0; intra < 2; intra++) {
1757 for (i = 0; i < 64; i++) {
1763 for (i = 0; i < 64; i++) {
1814 for (i = 0; i < 4; i++) {
1845 const AVFrame *pic_arg,
int *got_packet)
1848 int i, stuffing_count, ret;
1877 for (i = 0; i < context_count; i++) {
1894 if (growing_buffer) {
1902 #if FF_API_STAT_BITS
1951 for (i = 0; i < context_count; i++) {
1966 for (i = 0; i < 4; i++) {
1984 if (stuffing_count) {
1986 stuffing_count + 50) {
1994 while (stuffing_count--) {
2001 stuffing_count -= 4;
2002 while (stuffing_count--) {
2031 "Internal error, negative bits\n");
2039 vbv_delay =
FFMAX(vbv_delay, min_delay);
2061 #if FF_API_VBV_DELAY
2068 #if FF_API_STAT_BITS
2101 *got_packet = !!pkt->
size;
2106 int n,
int threshold)
2108 static const char tab[64] = {
2109 3, 2, 2, 1, 1, 1, 1, 1,
2110 1, 1, 1, 1, 1, 1, 1, 1,
2111 1, 1, 1, 1, 1, 1, 1, 1,
2112 0, 0, 0, 0, 0, 0, 0, 0,
2113 0, 0, 0, 0, 0, 0, 0, 0,
2114 0, 0, 0, 0, 0, 0, 0, 0,
2115 0, 0, 0, 0, 0, 0, 0, 0,
2116 0, 0, 0, 0, 0, 0, 0, 0
2125 if (threshold < 0) {
2127 threshold = -threshold;
2132 if (last_index <= skip_dc - 1)
2135 for (i = 0; i <= last_index; i++) {
2139 if (skip_dc && i == 0)
2143 }
else if (level > 1) {
2149 if (score >= threshold)
2151 for (i = skip_dc; i <= last_index; i++) {
2174 for (; i <= last_index; i++) {
2176 int level = block[j];
2178 if (level > maxlevel) {
2181 }
else if (level < minlevel) {
2191 "warning, clipping %d dct coefficients to %d..%d\n",
2192 overflow, minlevel, maxlevel);
2199 for (y = 0; y < 8; y++) {
2200 for (x = 0; x < 8; x++) {
2206 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2207 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2208 int v = ptr[x2 + y2 *
stride];
2214 weight[x + 8 * y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2220 int motion_x,
int motion_y,
2221 int mb_block_height,
2226 int16_t orig[12][64];
2233 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2234 ptrdiff_t wrap_y, wrap_c;
2236 for (i = 0; i < mb_block_count; i++)
2240 const int last_qp = s->
qscale;
2241 const int mb_xy = mb_x + mb_y * s->
mb_stride;
2272 (mb_y * 16 * wrap_y) + mb_x * 16;
2274 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2276 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2284 16, 16, mb_x * 16, mb_y * 16,
2289 mb_block_width, mb_block_height,
2290 mb_x * mb_block_width, mb_y * mb_block_height,
2292 ptr_cb = ebuf + 16 * wrap_y;
2295 mb_block_width, mb_block_height,
2296 mb_x * mb_block_width, mb_y * mb_block_height,
2298 ptr_cr = ebuf + 16 * wrap_y + 16;
2303 int progressive_score, interlaced_score;
2308 NULL, wrap_y, 8) - 400;
2310 if (progressive_score > 0) {
2312 NULL, wrap_y * 2, 8) +
2314 NULL, wrap_y * 2, 8);
2315 if (progressive_score > interlaced_score) {
2318 dct_offset = wrap_y;
2319 uv_dct_offset = wrap_c;
2354 uint8_t *dest_y, *dest_cb, *dest_cr;
2356 dest_y = s->
dest[0];
2357 dest_cb = s->
dest[1];
2358 dest_cr = s->
dest[2];
2382 int progressive_score, interlaced_score;
2385 progressive_score = s->
mecc.
ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2391 progressive_score -= 400;
2393 if (progressive_score > 0) {
2400 if (progressive_score > interlaced_score) {
2403 dct_offset = wrap_y;
2404 uv_dct_offset = wrap_c;
2415 dest_y + dct_offset, wrap_y);
2417 dest_y + dct_offset + 8, wrap_y);
2427 dest_cb + uv_dct_offset, wrap_c);
2429 dest_cr + uv_dct_offset, wrap_c);
2440 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset, dest_y + dct_offset,
2441 wrap_y, 8) < 20 * s->
qscale)
2443 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2444 wrap_y, 8) < 20 * s->
qscale)
2452 dest_cb + uv_dct_offset,
2453 wrap_c, 8) < 20 * s->
qscale)
2456 dest_cr + uv_dct_offset,
2457 wrap_c, 8) < 20 * s->
qscale)
2484 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2490 for (i = 0; i < mb_block_count; i++) {
2505 for (i = 0; i < mb_block_count; i++) {
2515 for (i = 0; i < 4; i++)
2518 for (i = 4; i < mb_block_count; i++)
2522 for (i = 0; i < mb_block_count; i++) {
2535 for (i=6; i<12; i++) {
2544 for (i = 0; i < mb_block_count; i++) {
2547 for (j = 63; j > 0; j--) {
2560 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2564 if (CONFIG_MPEG4_ENCODER)
2574 if (CONFIG_WMV2_ENCODER)
2578 if (CONFIG_H261_ENCODER)
2586 if (CONFIG_H263_ENCODER)
2591 if (CONFIG_MJPEG_ENCODER)
2637 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2675 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2683 s->
pb= pb[*next_block];
2685 s->
pb2 = pb2 [*next_block];
2686 s->
tex_pb= tex_pb[*next_block];
2690 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2713 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2731 else if(w==8 && h==8)
2821 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2829 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2864 if (CONFIG_H263_ENCODER)
2866 bytestream_put_le32(&ptr, offset);
2867 bytestream_put_byte(&ptr, s->
qscale);
2868 bytestream_put_byte(&ptr, gobn);
2869 bytestream_put_le16(&ptr, mba);
2870 bytestream_put_byte(&ptr, pred_x);
2871 bytestream_put_byte(&ptr, pred_y);
2873 bytestream_put_byte(&ptr, 0);
2874 bytestream_put_byte(&ptr, 0);
2909 int new_buffer_size = 0;
2986 if (CONFIG_H263_ENCODER)
3006 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
3040 int current_packet_size, is_gob_start;
3048 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
3057 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
3062 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
3082 current_packet_size=0;
3088 #if FF_API_RTP_CALLBACK
3100 if (CONFIG_MPEG4_ENCODER) {
3107 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3114 if (CONFIG_H263_ENCODER)
3144 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3151 backup_s.pb2= s->
pb2;
3152 backup_s.tex_pb= s->
tex_pb;
3161 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
3162 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3173 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
3174 &dmin, &next_block, 0, 0);
3182 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3183 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3193 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3194 &dmin, &next_block, 0, 0);
3202 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3203 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3211 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3212 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
3222 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3223 &dmin, &next_block, 0, 0);
3234 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3235 &dmin, &next_block, 0, 0);
3246 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3247 &dmin, &next_block, 0, 0);
3253 for(dir=0; dir<2; dir++){
3260 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3261 &dmin, &next_block, 0, 0);
3269 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3270 &dmin, &next_block, 0, 0);
3281 const int last_qp= backup_s.qscale;
3285 static const int dquant_tab[4]={-1,1,-2,2};
3294 s->
mv[0][0][0] = best_s.
mv[0][0][0];
3295 s->
mv[0][0][1] = best_s.
mv[0][0][1];
3296 s->
mv[1][0][0] = best_s.
mv[1][0][0];
3297 s->
mv[1][0][1] = best_s.
mv[1][0][1];
3300 for(; qpi<4; qpi++){
3301 int dquant= dquant_tab[qpi];
3313 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3314 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3330 backup_s.dquant = 0;
3334 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3335 &dmin, &next_block, mx, my);
3338 backup_s.dquant = 0;
3342 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3343 &dmin, &next_block, 0, 0);
3351 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3372 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3373 &dmin, &next_block, mx, my);
3391 s->
pb2= backup_s.pb2;
3395 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3396 s->
tex_pb= backup_s.tex_pb;
3400 if (CONFIG_H263_ENCODER &&
3413 int motion_x = 0, motion_y = 0;
3421 motion_x= s->
mv[0][0][0] = 0;
3422 motion_y= s->
mv[0][0][1] = 0;
3450 if (CONFIG_MPEG4_ENCODER) {
3459 if (CONFIG_MPEG4_ENCODER) {
3509 for(dir=0; dir<2; dir++){
3526 if (CONFIG_H263_ENCODER &&
3571 #if FF_API_RTP_CALLBACK
3587 #define MERGE(field) dst->field += src->field; src->field=0
3614 for(i=0; i<64; i++){
3641 if (CONFIG_MPEG4_ENCODER)
3647 if (CONFIG_H263_ENCODER)
3728 for(i=1; i<context_count; i++){
3759 for(i=1; i<context_count; i++){
3773 ff_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3817 for(dir=0; dir<2; dir++){
3868 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};
3869 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};
3900 if (CONFIG_MJPEG_ENCODER)
3905 if (CONFIG_H261_ENCODER)
3913 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred) {
3926 else if (CONFIG_H263_ENCODER)
3930 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3939 for(i=1; i<context_count; i++){
3943 for(i=1; i<context_count; i++){
3958 for(i=0; i<64; i++){
3959 int level= block[i];
3965 if(level<0) level=0;
3969 if(level>0) level=0;
3978 int qscale,
int *overflow){
3980 const uint16_t *matrix;
3984 unsigned int threshold1, threshold2;
3996 int coeff_count[64];
3997 int qmul, qadd, start_i, last_non_zero, i,
dc;
4009 qadd= ((qscale-1)|1)*8;
4012 else mpeg2_qscale = qscale << 1;
4029 block[0] = (block[0] + (q >> 1)) / q;
4055 threshold2= (threshold1<<1);
4057 for(i=63; i>=start_i; i--) {
4058 const int j = scantable[i];
4059 int level = block[j] * qmat[j];
4061 if(((
unsigned)(level+threshold1))>threshold2){
4067 for(i=start_i; i<=last_non_zero; i++) {
4068 const int j = scantable[i];
4069 int level = block[j] * qmat[j];
4073 if(((
unsigned)(level+threshold1))>threshold2){
4077 coeff[1][i]= level-1;
4081 coeff[0][i]= -
level;
4082 coeff[1][i]= -level+1;
4085 coeff_count[i]=
FFMIN(level, 2);
4089 coeff[0][i]= (level>>31)|1;
4096 if(last_non_zero < start_i){
4097 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4098 return last_non_zero;
4101 score_tab[start_i]= 0;
4102 survivor[0]= start_i;
4105 for(i=start_i; i<=last_non_zero; i++){
4106 int level_index, j, zero_distortion;
4107 int dct_coeff=
FFABS(block[ scantable[i] ]);
4108 int best_score=256*256*256*120;
4112 zero_distortion= dct_coeff*dct_coeff;
4114 for(level_index=0; level_index < coeff_count[i]; level_index++){
4116 int level= coeff[level_index][i];
4117 const int alevel=
FFABS(level);
4123 unquant_coeff= alevel*qmul + qadd;
4126 unquant_coeff = alevel * matrix[j] * 8;
4130 unquant_coeff = (int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4131 unquant_coeff = (unquant_coeff - 1) | 1;
4133 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
4134 unquant_coeff = (unquant_coeff - 1) | 1;
4139 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4141 if((level&(~127)) == 0){
4142 for(j=survivor_count-1; j>=0; j--){
4143 int run= i - survivor[j];
4145 score += score_tab[i-
run];
4147 if(score < best_score){
4150 level_tab[i+1]= level-64;
4155 for(j=survivor_count-1; j>=0; j--){
4156 int run= i - survivor[j];
4158 score += score_tab[i-
run];
4159 if(score < last_score){
4162 last_level= level-64;
4168 distortion += esc_length*
lambda;
4169 for(j=survivor_count-1; j>=0; j--){
4170 int run= i - survivor[j];
4171 int score= distortion + score_tab[i-
run];
4173 if(score < best_score){
4176 level_tab[i+1]= level-64;
4181 for(j=survivor_count-1; j>=0; j--){
4182 int run= i - survivor[j];
4183 int score= distortion + score_tab[i-
run];
4184 if(score < last_score){
4187 last_level= level-64;
4195 score_tab[i+1]= best_score;
4198 if(last_non_zero <= 27){
4199 for(; survivor_count; survivor_count--){
4200 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4204 for(; survivor_count; survivor_count--){
4205 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4210 survivor[ survivor_count++ ]= i+1;
4214 last_score= 256*256*256*120;
4215 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4216 int score= score_tab[i];
4218 score += lambda * 2;
4220 if(score < last_score){
4223 last_level= level_tab[i];
4224 last_run= run_tab[i];
4231 dc=
FFABS(block[0]);
4232 last_non_zero= last_i - 1;
4233 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4235 if(last_non_zero < start_i)
4236 return last_non_zero;
4238 if(last_non_zero == 0 && start_i == 0){
4240 int best_score= dc *
dc;
4242 for(i=0; i<coeff_count[0]; i++){
4243 int level= coeff[i][0];
4244 int alevel=
FFABS(level);
4245 int unquant_coeff, score, distortion;
4248 unquant_coeff= (alevel*qmul + qadd)>>3;
4250 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
4251 unquant_coeff = (unquant_coeff - 1) | 1;
4253 unquant_coeff = (unquant_coeff + 4) >> 3;
4254 unquant_coeff<<= 3 + 3;
4256 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
4259 else score= distortion + esc_length*
lambda;
4261 if(score < best_score){
4263 best_level= level - 64;
4266 block[0]= best_level;
4268 if(best_level == 0)
return -1;
4269 else return last_non_zero;
4275 block[ perm_scantable[last_non_zero] ]= last_level;
4278 for(; i>start_i; i -= run_tab[i] + 1){
4279 block[ perm_scantable[i-1] ]= level_tab[i];
4282 return last_non_zero;
4297 int perm_index= perm[
index];
4298 if(i==0) s*= sqrt(0.5);
4299 if(j==0) s*= sqrt(0.5);
4300 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)));
4319 int qmul, qadd, start_i, last_non_zero, i,
dc;
4323 int rle_index,
run, q = 1, sum;
4326 static int after_last=0;
4327 static int to_zero=0;
4328 static int from_zero=0;
4331 static int messed_sign=0;
4334 if(basis[0][0] == 0)
4376 for(i=0; i<64; i++){
4383 for(i=0; i<64; i++){
4388 w=
FFABS(weight[i]) + qns*one;
4389 w= 15 + (48*qns*one + w/2)/w;
4404 for(i=start_i; i<=last_non_zero; i++){
4405 int j= perm_scantable[i];
4406 const int level= block[j];
4410 if(level<0) coeff= qmul*level - qadd;
4411 else coeff= qmul*level + qadd;
4412 run_tab[rle_index++]=
run;
4421 if(last_non_zero>0){
4432 int run2, best_unquant_change=0, analyze_gradient;
4438 if(analyze_gradient){
4442 for(i=0; i<64; i++){
4458 const int level= block[0];
4459 int change, old_coeff;
4465 for(change=-1; change<=1; change+=2){
4466 int new_level= level + change;
4467 int score, new_coeff;
4469 new_coeff= q*new_level;
4470 if(new_coeff >= 2048 || new_coeff < 0)
4474 new_coeff - old_coeff);
4475 if(score<best_score){
4478 best_change= change;
4479 best_unquant_change= new_coeff - old_coeff;
4486 run2= run_tab[rle_index++];
4490 for(i=start_i; i<64; i++){
4491 int j= perm_scantable[i];
4492 const int level= block[j];
4493 int change, old_coeff;
4499 if(level<0) old_coeff= qmul*level - qadd;
4500 else old_coeff= qmul*level + qadd;
4501 run2= run_tab[rle_index++];
4508 for(change=-1; change<=1; change+=2){
4509 int new_level= level + change;
4510 int score, new_coeff, unquant_change;
4517 if(new_level<0) new_coeff= qmul*new_level - qadd;
4518 else new_coeff= qmul*new_level + qadd;
4519 if(new_coeff >= 2048 || new_coeff <= -2048)
4524 if(level < 63 && level > -63){
4525 if(i < last_non_zero)
4535 if(analyze_gradient){
4536 int g= d1[ scantable[i] ];
4537 if(g && (g^new_level) >= 0)
4541 if(i < last_non_zero){
4542 int next_i= i + run2 + 1;
4543 int next_level= block[ perm_scantable[next_i] ] + 64;
4545 if(next_level&(~127))
4548 if(next_i < last_non_zero)