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);
1334 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1338 if (flush_offset <= 1)
1341 encoding_delay = encoding_delay - flush_offset + 1;
1357 int64_t score64 = 0;
1359 for (plane = 0; plane < 3; plane++) {
1361 const int bw = plane ? 1 : 2;
1362 for (y = 0; y < s->
mb_height * bw; y++) {
1363 for (x = 0; x < s->
mb_width * bw; x++) {
1364 int off = p->
shared ? 0 : 16;
1370 case 0: score =
FFMAX(score, v);
break;
1371 case 1: score +=
FFABS(v);
break;
1372 case 2: score64 += v * (int64_t)v;
break;
1373 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1374 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1397 int ret, got_output;
1415 int64_t best_rd = INT64_MAX;
1416 int best_b_count = -1;
1428 b_lambda = p_lambda;
1453 pre_input = *pre_input_ptr;
1454 memcpy(data, pre_input_ptr->
f->
data,
sizeof(data));
1456 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
1742 #if FF_API_ERROR_FRAME
1754 for (intra = 0; intra < 2; intra++) {
1756 for (i = 0; i < 64; i++) {
1762 for (i = 0; i < 64; i++) {
1813 for (i = 0; i < 4; i++) {
1844 const AVFrame *pic_arg,
int *got_packet)
1847 int i, stuffing_count, ret;
1876 for (i = 0; i < context_count; i++) {
1893 if (growing_buffer) {
1901 #if FF_API_STAT_BITS
1950 for (i = 0; i < context_count; i++) {
1965 for (i = 0; i < 4; i++) {
1983 if (stuffing_count) {
1985 stuffing_count + 50) {
1993 while (stuffing_count--) {
2000 stuffing_count -= 4;
2001 while (stuffing_count--) {
2030 "Internal error, negative bits\n");
2038 vbv_delay =
FFMAX(vbv_delay, min_delay);
2060 #if FF_API_VBV_DELAY
2067 #if FF_API_STAT_BITS
2100 *got_packet = !!pkt->
size;
2105 int n,
int threshold)
2107 static const char tab[64] = {
2108 3, 2, 2, 1, 1, 1, 1, 1,
2109 1, 1, 1, 1, 1, 1, 1, 1,
2110 1, 1, 1, 1, 1, 1, 1, 1,
2111 0, 0, 0, 0, 0, 0, 0, 0,
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
2124 if (threshold < 0) {
2126 threshold = -threshold;
2131 if (last_index <= skip_dc - 1)
2134 for (i = 0; i <= last_index; i++) {
2138 if (skip_dc && i == 0)
2142 }
else if (level > 1) {
2148 if (score >= threshold)
2150 for (i = skip_dc; i <= last_index; i++) {
2173 for (; i <= last_index; i++) {
2175 int level = block[j];
2177 if (level > maxlevel) {
2180 }
else if (level < minlevel) {
2190 "warning, clipping %d dct coefficients to %d..%d\n",
2191 overflow, minlevel, maxlevel);
2198 for (y = 0; y < 8; y++) {
2199 for (x = 0; x < 8; x++) {
2205 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2206 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2207 int v = ptr[x2 + y2 *
stride];
2213 weight[x + 8 * y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2219 int motion_x,
int motion_y,
2220 int mb_block_height,
2225 int16_t orig[12][64];
2232 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2233 ptrdiff_t wrap_y, wrap_c;
2235 for (i = 0; i < mb_block_count; i++)
2239 const int last_qp = s->
qscale;
2240 const int mb_xy = mb_x + mb_y * s->
mb_stride;
2271 (mb_y * 16 * wrap_y) + mb_x * 16;
2273 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2275 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2283 16, 16, mb_x * 16, mb_y * 16,
2288 mb_block_width, mb_block_height,
2289 mb_x * mb_block_width, mb_y * mb_block_height,
2291 ptr_cb = ebuf + 16 * wrap_y;
2294 mb_block_width, mb_block_height,
2295 mb_x * mb_block_width, mb_y * mb_block_height,
2297 ptr_cr = ebuf + 16 * wrap_y + 16;
2302 int progressive_score, interlaced_score;
2307 NULL, wrap_y, 8) - 400;
2309 if (progressive_score > 0) {
2311 NULL, wrap_y * 2, 8) +
2313 NULL, wrap_y * 2, 8);
2314 if (progressive_score > interlaced_score) {
2317 dct_offset = wrap_y;
2318 uv_dct_offset = wrap_c;
2353 uint8_t *dest_y, *dest_cb, *dest_cr;
2355 dest_y = s->
dest[0];
2356 dest_cb = s->
dest[1];
2357 dest_cr = s->
dest[2];
2381 int progressive_score, interlaced_score;
2384 progressive_score = s->
mecc.
ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2390 progressive_score -= 400;
2392 if (progressive_score > 0) {
2399 if (progressive_score > interlaced_score) {
2402 dct_offset = wrap_y;
2403 uv_dct_offset = wrap_c;
2414 dest_y + dct_offset, wrap_y);
2416 dest_y + dct_offset + 8, wrap_y);
2426 dest_cb + uv_dct_offset, wrap_c);
2428 dest_cr + uv_dct_offset, wrap_c);
2439 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset, dest_y + dct_offset,
2440 wrap_y, 8) < 20 * s->
qscale)
2442 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2443 wrap_y, 8) < 20 * s->
qscale)
2451 dest_cb + uv_dct_offset,
2452 wrap_c, 8) < 20 * s->
qscale)
2455 dest_cr + uv_dct_offset,
2456 wrap_c, 8) < 20 * s->
qscale)
2483 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2489 for (i = 0; i < mb_block_count; i++) {
2504 for (i = 0; i < mb_block_count; i++) {
2514 for (i = 0; i < 4; i++)
2517 for (i = 4; i < mb_block_count; i++)
2521 for (i = 0; i < mb_block_count; i++) {
2534 for (i=6; i<12; i++) {
2543 for (i = 0; i < mb_block_count; i++) {
2546 for (j = 63; j > 0; j--) {
2559 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2563 if (CONFIG_MPEG4_ENCODER)
2573 if (CONFIG_WMV2_ENCODER)
2577 if (CONFIG_H261_ENCODER)
2585 if (CONFIG_H263_ENCODER)
2590 if (CONFIG_MJPEG_ENCODER)
2636 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2674 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2682 s->
pb= pb[*next_block];
2684 s->
pb2 = pb2 [*next_block];
2685 s->
tex_pb= tex_pb[*next_block];
2689 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2712 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2730 else if(w==8 && h==8)
2820 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2828 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2863 if (CONFIG_H263_ENCODER)
2865 bytestream_put_le32(&ptr, offset);
2866 bytestream_put_byte(&ptr, s->
qscale);
2867 bytestream_put_byte(&ptr, gobn);
2868 bytestream_put_le16(&ptr, mba);
2869 bytestream_put_byte(&ptr, pred_x);
2870 bytestream_put_byte(&ptr, pred_y);
2872 bytestream_put_byte(&ptr, 0);
2873 bytestream_put_byte(&ptr, 0);
2908 int new_buffer_size = 0;
2983 if (CONFIG_H263_ENCODER)
3003 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
3037 int current_packet_size, is_gob_start;
3045 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
3054 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
3059 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
3079 current_packet_size=0;
3085 #if FF_API_RTP_CALLBACK
3097 if (CONFIG_MPEG4_ENCODER) {
3104 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3111 if (CONFIG_H263_ENCODER)
3141 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3148 backup_s.pb2= s->
pb2;
3149 backup_s.tex_pb= s->
tex_pb;
3158 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
3159 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3170 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
3171 &dmin, &next_block, 0, 0);
3179 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3180 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3190 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3191 &dmin, &next_block, 0, 0);
3199 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3200 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3208 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3209 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
3219 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3220 &dmin, &next_block, 0, 0);
3231 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3232 &dmin, &next_block, 0, 0);
3243 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3244 &dmin, &next_block, 0, 0);
3250 for(dir=0; dir<2; dir++){
3257 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3258 &dmin, &next_block, 0, 0);
3266 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3267 &dmin, &next_block, 0, 0);
3278 const int last_qp= backup_s.qscale;
3282 static const int dquant_tab[4]={-1,1,-2,2};
3291 s->
mv[0][0][0] = best_s.
mv[0][0][0];
3292 s->
mv[0][0][1] = best_s.
mv[0][0][1];
3293 s->
mv[1][0][0] = best_s.
mv[1][0][0];
3294 s->
mv[1][0][1] = best_s.
mv[1][0][1];
3297 for(; qpi<4; qpi++){
3298 int dquant= dquant_tab[qpi];
3310 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3311 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3327 backup_s.dquant = 0;
3331 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3332 &dmin, &next_block, mx, my);
3335 backup_s.dquant = 0;
3339 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3340 &dmin, &next_block, 0, 0);
3348 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3369 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3370 &dmin, &next_block, mx, my);
3388 s->
pb2= backup_s.pb2;
3392 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3393 s->
tex_pb= backup_s.tex_pb;
3397 if (CONFIG_H263_ENCODER &&
3410 int motion_x = 0, motion_y = 0;
3418 motion_x= s->
mv[0][0][0] = 0;
3419 motion_y= s->
mv[0][0][1] = 0;
3447 if (CONFIG_MPEG4_ENCODER) {
3456 if (CONFIG_MPEG4_ENCODER) {
3506 for(dir=0; dir<2; dir++){
3523 if (CONFIG_H263_ENCODER &&
3568 #if FF_API_RTP_CALLBACK
3584 #define MERGE(field) dst->field += src->field; src->field=0
3611 for(i=0; i<64; i++){
3638 if (CONFIG_MPEG4_ENCODER)
3644 if (CONFIG_H263_ENCODER)
3725 for(i=1; i<context_count; i++){
3756 for(i=1; i<context_count; i++){
3770 ff_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3814 for(dir=0; dir<2; dir++){
3865 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};
3866 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};
3897 if (CONFIG_MJPEG_ENCODER)
3902 if (CONFIG_H261_ENCODER)
3910 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred) {
3923 else if (CONFIG_H263_ENCODER)
3927 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3936 for(i=1; i<context_count; i++){
3940 for(i=1; i<context_count; i++){
3955 for(i=0; i<64; i++){
3956 int level= block[i];
3962 if(level<0) level=0;
3966 if(level>0) level=0;
3975 int qscale,
int *overflow){
3977 const uint16_t *matrix;
3981 unsigned int threshold1, threshold2;
3993 int coeff_count[64];
3994 int qmul, qadd, start_i, last_non_zero, i,
dc;
4006 qadd= ((qscale-1)|1)*8;
4009 else mpeg2_qscale = qscale << 1;
4026 block[0] = (block[0] + (q >> 1)) / q;
4052 threshold2= (threshold1<<1);
4054 for(i=63; i>=start_i; i--) {
4055 const int j = scantable[i];
4056 int level = block[j] * qmat[j];
4058 if(((
unsigned)(level+threshold1))>threshold2){
4064 for(i=start_i; i<=last_non_zero; i++) {
4065 const int j = scantable[i];
4066 int level = block[j] * qmat[j];
4070 if(((
unsigned)(level+threshold1))>threshold2){
4074 coeff[1][i]= level-1;
4078 coeff[0][i]= -
level;
4079 coeff[1][i]= -level+1;
4082 coeff_count[i]=
FFMIN(level, 2);
4086 coeff[0][i]= (level>>31)|1;
4093 if(last_non_zero < start_i){
4094 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4095 return last_non_zero;
4098 score_tab[start_i]= 0;
4099 survivor[0]= start_i;
4102 for(i=start_i; i<=last_non_zero; i++){
4103 int level_index, j, zero_distortion;
4104 int dct_coeff=
FFABS(block[ scantable[i] ]);
4105 int best_score=256*256*256*120;
4109 zero_distortion= dct_coeff*dct_coeff;
4111 for(level_index=0; level_index < coeff_count[i]; level_index++){
4113 int level= coeff[level_index][i];
4114 const int alevel=
FFABS(level);
4120 unquant_coeff= alevel*qmul + qadd;
4123 unquant_coeff = alevel * matrix[j] * 8;
4127 unquant_coeff = (int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4128 unquant_coeff = (unquant_coeff - 1) | 1;
4130 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
4131 unquant_coeff = (unquant_coeff - 1) | 1;
4136 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4138 if((level&(~127)) == 0){
4139 for(j=survivor_count-1; j>=0; j--){
4140 int run= i - survivor[j];
4142 score += score_tab[i-
run];
4144 if(score < best_score){
4147 level_tab[i+1]= level-64;
4152 for(j=survivor_count-1; j>=0; j--){
4153 int run= i - survivor[j];
4155 score += score_tab[i-
run];
4156 if(score < last_score){
4159 last_level= level-64;
4165 distortion += esc_length*
lambda;
4166 for(j=survivor_count-1; j>=0; j--){
4167 int run= i - survivor[j];
4168 int score= distortion + score_tab[i-
run];
4170 if(score < best_score){
4173 level_tab[i+1]= level-64;
4178 for(j=survivor_count-1; j>=0; j--){
4179 int run= i - survivor[j];
4180 int score= distortion + score_tab[i-
run];
4181 if(score < last_score){
4184 last_level= level-64;
4192 score_tab[i+1]= best_score;
4195 if(last_non_zero <= 27){
4196 for(; survivor_count; survivor_count--){
4197 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4201 for(; survivor_count; survivor_count--){
4202 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4207 survivor[ survivor_count++ ]= i+1;
4211 last_score= 256*256*256*120;
4212 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4213 int score= score_tab[i];
4214 if(i) score += lambda*2;
4216 if(score < last_score){
4219 last_level= level_tab[i];
4220 last_run= run_tab[i];
4227 dc=
FFABS(block[0]);
4228 last_non_zero= last_i - 1;
4229 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4231 if(last_non_zero < start_i)
4232 return last_non_zero;
4234 if(last_non_zero == 0 && start_i == 0){
4236 int best_score= dc *
dc;
4238 for(i=0; i<coeff_count[0]; i++){
4239 int level= coeff[i][0];
4240 int alevel=
FFABS(level);
4241 int unquant_coeff, score, distortion;
4244 unquant_coeff= (alevel*qmul + qadd)>>3;
4246 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
4247 unquant_coeff = (unquant_coeff - 1) | 1;
4249 unquant_coeff = (unquant_coeff + 4) >> 3;
4250 unquant_coeff<<= 3 + 3;
4252 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
4255 else score= distortion + esc_length*
lambda;
4257 if(score < best_score){
4259 best_level= level - 64;
4262 block[0]= best_level;
4264 if(best_level == 0)
return -1;
4265 else return last_non_zero;
4271 block[ perm_scantable[last_non_zero] ]= last_level;
4274 for(; i>start_i; i -= run_tab[i] + 1){
4275 block[ perm_scantable[i-1] ]= level_tab[i];
4278 return last_non_zero;
4293 int perm_index= perm[
index];
4294 if(i==0) s*= sqrt(0.5);
4295 if(j==0) s*= sqrt(0.5);
4296 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)));
4315 int qmul, qadd, start_i, last_non_zero, i,
dc;
4319 int rle_index,
run, q = 1, sum;
4322 static int after_last=0;
4323 static int to_zero=0;
4324 static int from_zero=0;
4327 static int messed_sign=0;
4330 if(basis[0][0] == 0)
4372 for(i=0; i<64; i++){
4379 for(i=0; i<64; i++){
4384 w=
FFABS(weight[i]) + qns*one;
4385 w= 15 + (48*qns*one + w/2)/w;
4400 for(i=start_i; i<=last_non_zero; i++){
4401 int j= perm_scantable[i];
4402 const int level= block[j];
4406 if(level<0) coeff= qmul*level - qadd;
4407 else coeff= qmul*level + qadd;
4408 run_tab[rle_index++]=
run;
4417 if(last_non_zero>0){
4428 int run2, best_unquant_change=0, analyze_gradient;
4434 if(analyze_gradient){
4438 for(i=0; i<64; i++){
4454 const int level= block[0];
4455 int change, old_coeff;
4461 for(change=-1; change<=1; change+=2){
4462 int new_level= level + change;
4463 int score, new_coeff;
4465 new_coeff= q*new_level;
4466 if(new_coeff >= 2048 || new_coeff < 0)
4470 new_coeff - old_coeff);
4471 if(score<best_score){
4474 best_change= change;
4475 best_unquant_change= new_coeff - old_coeff;
4482 run2= run_tab[rle_index++];
4486 for(i=start_i; i<64; i++){
4487 int j= perm_scantable[i];
4488 const int level= block[j];
4489 int change, old_coeff;
4495 if(level<0) old_coeff= qmul*level - qadd;
4496 else old_coeff= qmul*level + qadd;
4497 run2= run_tab[rle_index++];
4504 for(change=-1; change<=1; change+=2){
4505 int new_level= level + change;
4506 int score, new_coeff, unquant_change;
4513 if(new_level<0) new_coeff= qmul*new_level - qadd;
4514 else new_coeff= qmul*new_level + qadd;
4515 if(new_coeff >= 2048 || new_coeff <= -2048)
4520 if(level < 63 && level > -63){
4521 if(i < last_non_zero)
4531 if(analyze_gradient){
4532 int g= d1[ scantable[i] ];
4533 if(g && (g^new_level) >= 0)
4537 if(i < last_non_zero){
4538 int next_i= i + run2 + 1;
4539 int next_level= block[ perm_scantable[next_i] ] + 64;
4541 if(next_level&(~127))
4544 if(next_i < last_non_zero)