71 #define QUANT_BIAS_SHIFT 8
73 #define QMAT_SHIFT_MMX 16
92 const uint16_t *quant_matrix,
93 int bias,
int qmin,
int qmax,
int intra)
99 for (qscale = qmin; qscale <= qmax; qscale++) {
104 else qscale2 = qscale << 1;
111 for (i = 0; i < 64; i++) {
113 int64_t den = (int64_t) qscale2 * quant_matrix[j];
123 for (i = 0; i < 64; i++) {
125 int64_t den =
ff_aanscales[i] * (int64_t) qscale2 * quant_matrix[j];
135 for (i = 0; i < 64; i++) {
137 int64_t den = (int64_t) qscale2 * quant_matrix[j];
148 if (
qmat16[qscale][0][i] == 0 ||
149 qmat16[qscale][0][i] == 128 * 256)
157 for (i = intra; i < 64; i++) {
162 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
169 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
178 int bestdiff=INT_MAX;
186 if (diff < bestdiff) {
208 for (i = 0; i < 64; i++) {
223 for (i = 0; i < s->
mb_num; i++) {
234 #define COPY(a) dst->a= src->a
259 for (i = -16; i < 16; i++) {
274 if (CONFIG_H263_ENCODER)
292 int i, ret, format_supported;
301 "only YUV420 and YUV422 are supported\n");
307 format_supported = 0;
316 format_supported = 1;
322 format_supported = 1;
324 if (!format_supported) {
354 #if FF_API_PRIVATE_OPT
371 "keyframe interval too large!, reducing it from %d to %d\n",
397 "intra dc precision must be positive, note some applications use"
398 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
418 #if FF_API_MOTION_EST
472 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
478 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
495 "impossible bitrate constraints, this will fail\n");
520 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
521 "specified vbv buffer is too large for the given bitrate!\n");
533 "OBMC is only supported with simple mb decision\n");
551 "max b frames must be 0 or positive for mpegvideo based encoders\n");
561 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
569 (avctx->
width > 2048 ||
576 ((avctx->
width &3) ||
583 (avctx->
width > 4095 ||
590 (avctx->
width > 16383 ||
591 avctx->
height > 16383 )) {
592 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
623 #if FF_API_PRIVATE_OPT
634 "mpeg2 style quantization not supported by codec\n");
654 "QP RD is no longer compatible with MJPEG or AMV\n");
658 #if FF_API_PRIVATE_OPT
668 "closed gop with scene change detection are not supported yet, "
669 "set threshold to 1000000000\n");
676 "low delay forcing is only available for mpeg2\n");
681 "B-frames cannot be used with low delay\n");
687 if (avctx->
qmax > 28) {
689 "non linear quant only supports qmax <= 28 currently\n");
707 "multi threaded encoding not supported by codec\n");
713 "automatic thread number detection not supported by codec, "
723 #if FF_API_PRIVATE_OPT
734 "notice: b_frame_strategy only affects the first pass\n");
757 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
761 #if FF_API_QUANT_BIAS
775 "timebase %d/%d not supported by MPEG 4 standard, "
776 "the maximum admitted value for the timebase denominator "
799 if (!CONFIG_MJPEG_ENCODER ||
806 if (!CONFIG_H261_ENCODER)
810 "The specified picture size of %dx%d is not valid for the "
811 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
821 if (!CONFIG_H263_ENCODER)
826 "The specified picture size of %dx%d is not valid for "
827 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
828 "352x288, 704x576, and 1408x1152. "
919 #if FF_API_PRIVATE_OPT
967 2 * 64 *
sizeof(uint16_t),
fail);
972 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
984 #if FF_API_PRIVATE_OPT
1007 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
1012 for (i = 0; i < 64; i++) {
1044 #if FF_API_RC_STRATEGY
1054 #if FF_API_RC_STRATEGY
1064 "Xvid ratecontrol requires libavcodec compiled with Xvid support.\n");
1070 #if FF_API_ERROR_RATE
1077 #if FF_API_NORMALIZE_AQP
1117 #if FF_API_PRIVATE_OPT
1169 if (CONFIG_MJPEG_ENCODER &&
1204 for (y = 0; y < 16; y++) {
1205 for (x = 0; x < 16; x++) {
1206 acc +=
FFABS(src[x + y * stride] - ref);
1222 for (y = 0; y <
h; y += 16) {
1223 for (x = 0; x < w; x += 16) {
1228 int sae =
get_sae(src + offset, mean, stride);
1230 acc += sae + 500 < sad;
1248 int i, display_picture_number = 0, ret;
1251 int flush_offset = 1;
1264 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1269 if (!s->
low_delay && display_picture_number == 1)
1278 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1281 pts = display_picture_number;
1285 if (!pic_arg->
buf[0] ||
1321 int h_chroma_shift, v_chroma_shift;
1326 for (i = 0; i < 3; i++) {
1327 int src_stride = pic_arg->
linesize[i];
1329 int h_shift = i ? h_chroma_shift : 0;
1330 int v_shift = i ? v_chroma_shift : 0;
1331 int w = s->
width >> h_shift;
1345 if (src_stride == dst_stride)
1346 memcpy(dst, src, src_stride * h);
1351 memcpy(dst2, src, w);
1376 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1380 if (flush_offset <= 1)
1383 encoding_delay = encoding_delay - flush_offset + 1;
1399 int64_t score64 = 0;
1401 for (plane = 0; plane < 3; plane++) {
1403 const int bw = plane ? 1 : 2;
1404 for (y = 0; y < s->
mb_height * bw; y++) {
1405 for (x = 0; x < s->
mb_width * bw; x++) {
1406 int off = p->
shared ? 0 : 16;
1412 case 0: score =
FFMAX(score, v);
break;
1413 case 1: score +=
FFABS(v);
break;
1414 case 2: score64 += v * (int64_t)v;
break;
1415 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1416 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1467 int64_t best_rd = INT64_MAX;
1468 int best_b_count = -1;
1479 b_lambda = p_lambda;
1489 pre_input = *pre_input_ptr;
1490 memcpy(data, pre_input_ptr->
f->
data,
sizeof(data));
1492 if (!pre_input.
shared && i) {
1507 width >> 1, height >> 1);
1512 width >> 1, height >> 1);
1591 return best_b_count;
1664 b_frames =
FFMAX(0, i - 1);
1667 for (i = 0; i < b_frames + 1; i++) {
1678 for (i = b_frames - 1; i >= 0; i--) {
1686 "warning, too many B-frames in a row\n");
1709 for (i = 0; i < b_frames; i++) {
1756 for (i = 0; i < 4; i++) {
1806 #if FF_API_CODED_FRAME
1812 #if FF_API_ERROR_FRAME
1824 for (intra = 0; intra < 2; intra++) {
1826 for (i = 0; i < 64; i++) {
1832 for (i = 0; i < 64; i++) {
1883 for (i = 0; i < 4; i++) {
1914 const AVFrame *pic_arg,
int *got_packet)
1917 int i, stuffing_count, ret;
1946 for (i = 0; i < context_count; i++) {
1963 if (growing_buffer) {
1971 #if FF_API_STAT_BITS
2020 for (i = 0; i < context_count; i++) {
2035 for (i = 0; i < 4; i++) {
2053 if (stuffing_count) {
2055 stuffing_count + 50) {
2063 while (stuffing_count--) {
2070 stuffing_count -= 4;
2071 while (stuffing_count--) {
2100 "Internal error, negative bits\n");
2108 vbv_delay =
FFMAX(vbv_delay, min_delay);
2130 #if FF_API_VBV_DELAY
2137 #if FF_API_STAT_BITS
2170 *got_packet = !!pkt->
size;
2175 int n,
int threshold)
2177 static const char tab[64] = {
2178 3, 2, 2, 1, 1, 1, 1, 1,
2179 1, 1, 1, 1, 1, 1, 1, 1,
2180 1, 1, 1, 1, 1, 1, 1, 1,
2181 0, 0, 0, 0, 0, 0, 0, 0,
2182 0, 0, 0, 0, 0, 0, 0, 0,
2183 0, 0, 0, 0, 0, 0, 0, 0,
2184 0, 0, 0, 0, 0, 0, 0, 0,
2185 0, 0, 0, 0, 0, 0, 0, 0
2194 if (threshold < 0) {
2196 threshold = -threshold;
2201 if (last_index <= skip_dc - 1)
2204 for (i = 0; i <= last_index; i++) {
2208 if (skip_dc && i == 0)
2212 }
else if (level > 1) {
2218 if (score >= threshold)
2220 for (i = skip_dc; i <= last_index; i++) {
2243 for (; i <= last_index; i++) {
2245 int level = block[j];
2247 if (level > maxlevel) {
2250 }
else if (level < minlevel) {
2260 "warning, clipping %d dct coefficients to %d..%d\n",
2261 overflow, minlevel, maxlevel);
2268 for (y = 0; y < 8; y++) {
2269 for (x = 0; x < 8; x++) {
2275 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2276 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2277 int v = ptr[x2 + y2 *
stride];
2283 weight[x + 8 * y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2289 int motion_x,
int motion_y,
2290 int mb_block_height,
2295 int16_t orig[12][64];
2302 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2303 ptrdiff_t wrap_y, wrap_c;
2305 for (i = 0; i < mb_block_count; i++)
2309 const int last_qp = s->
qscale;
2310 const int mb_xy = mb_x + mb_y * s->
mb_stride;
2341 (mb_y * 16 * wrap_y) + mb_x * 16;
2343 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2345 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2353 16, 16, mb_x * 16, mb_y * 16,
2358 mb_block_width, mb_block_height,
2359 mb_x * mb_block_width, mb_y * mb_block_height,
2361 ptr_cb = ebuf + 16 * wrap_y;
2364 mb_block_width, mb_block_height,
2365 mb_x * mb_block_width, mb_y * mb_block_height,
2367 ptr_cr = ebuf + 16 * wrap_y + 16;
2372 int progressive_score, interlaced_score;
2377 NULL, wrap_y, 8) - 400;
2379 if (progressive_score > 0) {
2381 NULL, wrap_y * 2, 8) +
2383 NULL, wrap_y * 2, 8);
2384 if (progressive_score > interlaced_score) {
2387 dct_offset = wrap_y;
2388 uv_dct_offset = wrap_c;
2423 uint8_t *dest_y, *dest_cb, *dest_cr;
2425 dest_y = s->
dest[0];
2426 dest_cb = s->
dest[1];
2427 dest_cr = s->
dest[2];
2451 int progressive_score, interlaced_score;
2454 progressive_score = s->
mecc.
ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2460 progressive_score -= 400;
2462 if (progressive_score > 0) {
2469 if (progressive_score > interlaced_score) {
2472 dct_offset = wrap_y;
2473 uv_dct_offset = wrap_c;
2484 dest_y + dct_offset, wrap_y);
2486 dest_y + dct_offset + 8, wrap_y);
2496 dest_cb + uv_dct_offset, wrap_c);
2498 dest_cr + uv_dct_offset, wrap_c);
2509 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset, dest_y + dct_offset,
2510 wrap_y, 8) < 20 * s->
qscale)
2512 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2513 wrap_y, 8) < 20 * s->
qscale)
2521 dest_cb + uv_dct_offset,
2522 wrap_c, 8) < 20 * s->
qscale)
2525 dest_cr + uv_dct_offset,
2526 wrap_c, 8) < 20 * s->
qscale)
2553 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2559 for (i = 0; i < mb_block_count; i++) {
2574 for (i = 0; i < mb_block_count; i++) {
2584 for (i = 0; i < 4; i++)
2587 for (i = 4; i < mb_block_count; i++)
2591 for (i = 0; i < mb_block_count; i++) {
2604 for (i=6; i<12; i++) {
2613 for (i = 0; i < mb_block_count; i++) {
2616 for (j = 63; j > 0; j--) {
2629 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2633 if (CONFIG_MPEG4_ENCODER)
2643 if (CONFIG_WMV2_ENCODER)
2647 if (CONFIG_H261_ENCODER)
2655 if (CONFIG_H263_ENCODER)
2660 if (CONFIG_MJPEG_ENCODER)
2706 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2744 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2752 s->
pb= pb[*next_block];
2754 s->
pb2 = pb2 [*next_block];
2755 s->
tex_pb= tex_pb[*next_block];
2759 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2782 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2800 else if(w==8 && h==8)
2890 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2898 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2933 if (CONFIG_H263_ENCODER)
2935 bytestream_put_le32(&ptr, offset);
2936 bytestream_put_byte(&ptr, s->
qscale);
2937 bytestream_put_byte(&ptr, gobn);
2938 bytestream_put_le16(&ptr, mba);
2939 bytestream_put_byte(&ptr, pred_x);
2940 bytestream_put_byte(&ptr, pred_y);
2942 bytestream_put_byte(&ptr, 0);
2943 bytestream_put_byte(&ptr, 0);
2978 int new_buffer_size = 0;
3055 if (CONFIG_H263_ENCODER)
3075 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
3109 int current_packet_size, is_gob_start;
3117 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
3126 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
3131 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
3151 current_packet_size=0;
3157 #if FF_API_RTP_CALLBACK
3169 if (CONFIG_MPEG4_ENCODER) {
3176 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3183 if (CONFIG_H263_ENCODER)
3213 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3220 backup_s.pb2= s->
pb2;
3221 backup_s.tex_pb= s->
tex_pb;
3230 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
3231 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3242 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
3243 &dmin, &next_block, 0, 0);
3251 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3252 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3262 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3263 &dmin, &next_block, 0, 0);
3271 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3272 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3280 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3281 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
3291 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3292 &dmin, &next_block, 0, 0);
3303 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3304 &dmin, &next_block, 0, 0);
3315 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3316 &dmin, &next_block, 0, 0);
3322 for(dir=0; dir<2; dir++){
3329 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3330 &dmin, &next_block, 0, 0);
3338 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3339 &dmin, &next_block, 0, 0);
3350 const int last_qp= backup_s.qscale;
3354 static const int dquant_tab[4]={-1,1,-2,2};
3363 s->
mv[0][0][0] = best_s.
mv[0][0][0];
3364 s->
mv[0][0][1] = best_s.
mv[0][0][1];
3365 s->
mv[1][0][0] = best_s.
mv[1][0][0];
3366 s->
mv[1][0][1] = best_s.
mv[1][0][1];
3369 for(; qpi<4; qpi++){
3370 int dquant= dquant_tab[qpi];
3382 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3383 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3399 backup_s.dquant = 0;
3403 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3404 &dmin, &next_block, mx, my);
3407 backup_s.dquant = 0;
3411 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3412 &dmin, &next_block, 0, 0);
3420 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3441 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3442 &dmin, &next_block, mx, my);
3460 s->
pb2= backup_s.pb2;
3464 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3465 s->
tex_pb= backup_s.tex_pb;
3469 if (CONFIG_H263_ENCODER &&
3482 int motion_x = 0, motion_y = 0;
3490 motion_x= s->
mv[0][0][0] = 0;
3491 motion_y= s->
mv[0][0][1] = 0;
3519 if (CONFIG_MPEG4_ENCODER) {
3528 if (CONFIG_MPEG4_ENCODER) {
3578 for(dir=0; dir<2; dir++){
3595 if (CONFIG_H263_ENCODER &&
3640 #if FF_API_RTP_CALLBACK
3656 #define MERGE(field) dst->field += src->field; src->field=0
3683 for(i=0; i<64; i++){
3717 if (CONFIG_MPEG4_ENCODER)
3723 if (CONFIG_H263_ENCODER)
3804 for(i=1; i<context_count; i++){
3835 for(i=1; i<context_count; i++){
3849 ff_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3893 for(dir=0; dir<2; dir++){
3944 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};
3945 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};
3981 if (CONFIG_H261_ENCODER)
3989 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred) {
4002 else if (CONFIG_H263_ENCODER)
4006 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
4015 for(i=1; i<context_count; i++){
4019 for(i=1; i<context_count; i++){
4034 for(i=0; i<64; i++){
4035 int level= block[i];
4041 if(level<0) level=0;
4045 if(level>0) level=0;
4054 int qscale,
int *overflow){
4056 const uint16_t *matrix;
4060 unsigned int threshold1, threshold2;
4072 int coeff_count[64];
4073 int qmul, qadd, start_i, last_non_zero, i,
dc;
4085 qadd= ((qscale-1)|1)*8;
4088 else mpeg2_qscale = qscale << 1;
4105 block[0] = (block[0] + (q >> 1)) / q;
4131 threshold2= (threshold1<<1);
4133 for(i=63; i>=start_i; i--) {
4134 const int j = scantable[i];
4135 int level = block[j] * qmat[j];
4137 if(((
unsigned)(level+threshold1))>threshold2){
4143 for(i=start_i; i<=last_non_zero; i++) {
4144 const int j = scantable[i];
4145 int level = block[j] * qmat[j];
4149 if(((
unsigned)(level+threshold1))>threshold2){
4153 coeff[1][i]= level-1;
4157 coeff[0][i]= -
level;
4158 coeff[1][i]= -level+1;
4161 coeff_count[i]=
FFMIN(level, 2);
4165 coeff[0][i]= (level>>31)|1;
4172 if(last_non_zero < start_i){
4173 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4174 return last_non_zero;
4177 score_tab[start_i]= 0;
4178 survivor[0]= start_i;
4181 for(i=start_i; i<=last_non_zero; i++){
4182 int level_index, j, zero_distortion;
4183 int dct_coeff=
FFABS(block[ scantable[i] ]);
4184 int best_score=256*256*256*120;
4188 zero_distortion= dct_coeff*dct_coeff;
4190 for(level_index=0; level_index < coeff_count[i]; level_index++){
4192 int level= coeff[level_index][i];
4193 const int alevel=
FFABS(level);
4199 unquant_coeff= alevel*qmul + qadd;
4202 unquant_coeff = alevel * matrix[j] * 8;
4206 unquant_coeff = (
int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4207 unquant_coeff = (unquant_coeff - 1) | 1;
4209 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
4210 unquant_coeff = (unquant_coeff - 1) | 1;
4215 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4217 if((level&(~127)) == 0){
4218 for(j=survivor_count-1; j>=0; j--){
4219 int run= i - survivor[j];
4221 score += score_tab[i-
run];
4223 if(score < best_score){
4226 level_tab[i+1]= level-64;
4231 for(j=survivor_count-1; j>=0; j--){
4232 int run= i - survivor[j];
4234 score += score_tab[i-
run];
4235 if(score < last_score){
4238 last_level= level-64;
4244 distortion += esc_length*
lambda;
4245 for(j=survivor_count-1; j>=0; j--){
4246 int run= i - survivor[j];
4247 int score= distortion + score_tab[i-
run];
4249 if(score < best_score){
4252 level_tab[i+1]= level-64;
4257 for(j=survivor_count-1; j>=0; j--){
4258 int run= i - survivor[j];
4259 int score= distortion + score_tab[i-
run];
4260 if(score < last_score){
4263 last_level= level-64;
4271 score_tab[i+1]= best_score;
4274 if(last_non_zero <= 27){
4275 for(; survivor_count; survivor_count--){
4276 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4280 for(; survivor_count; survivor_count--){
4281 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4286 survivor[ survivor_count++ ]= i+1;
4290 last_score= 256*256*256*120;
4291 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4292 int score= score_tab[i];
4294 score += lambda * 2;
4296 if(score < last_score){
4299 last_level= level_tab[i];
4300 last_run= run_tab[i];
4307 dc=
FFABS(block[0]);
4308 last_non_zero= last_i - 1;
4309 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4311 if(last_non_zero < start_i)
4312 return last_non_zero;
4314 if(last_non_zero == 0 && start_i == 0){
4316 int best_score= dc *
dc;
4318 for(i=0; i<coeff_count[0]; i++){
4319 int level= coeff[i][0];
4320 int alevel=
FFABS(level);
4321 int unquant_coeff, score, distortion;
4324 unquant_coeff= (alevel*qmul + qadd)>>3;
4326 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
4327 unquant_coeff = (unquant_coeff - 1) | 1;
4329 unquant_coeff = (unquant_coeff + 4) >> 3;
4330 unquant_coeff<<= 3 + 3;
4332 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
4335 else score= distortion + esc_length*
lambda;
4337 if(score < best_score){
4339 best_level= level - 64;
4342 block[0]= best_level;
4344 if(best_level == 0)
return -1;
4345 else return last_non_zero;
4351 block[ perm_scantable[last_non_zero] ]= last_level;
4354 for(; i>start_i; i -= run_tab[i] + 1){
4355 block[ perm_scantable[i-1] ]= level_tab[i];
4358 return last_non_zero;
4373 int perm_index= perm[
index];
4374 if(i==0) s*= sqrt(0.5);
4375 if(j==0) s*= sqrt(0.5);
4376 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)));
4395 int qmul, qadd, start_i, last_non_zero, i,
dc;
4399 int rle_index,
run, q = 1, sum;
4402 static int after_last=0;
4403 static int to_zero=0;
4404 static int from_zero=0;
4407 static int messed_sign=0;
4410 if(basis[0][0] == 0)
4452 for(i=0; i<64; i++){
4459 for(i=0; i<64; i++){
4464 w=
FFABS(weight[i]) + qns*one;
4465 w= 15 + (48*qns*one + w/2)/w;
4480 for(i=start_i; i<=last_non_zero; i++){
4481 int j= perm_scantable[i];
4482 const int level= block[j];
4486 if(level<0) coeff= qmul*level - qadd;
4487 else coeff= qmul*level + qadd;
4488 run_tab[rle_index++]=
run;
4497 if(last_non_zero>0){
4508 int run2, best_unquant_change=0, analyze_gradient;
4514 if(analyze_gradient){
4518 for(i=0; i<64; i++){