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");
459 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
465 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
482 "impossible bitrate constraints, this will fail\n");
507 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
508 "specified vbv buffer is too large for the given bitrate!\n");
520 "OBMC is only supported with simple mb decision\n");
538 "max b frames must be 0 or positive for mpegvideo based encoders\n");
548 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
556 (avctx->
width > 2048 ||
563 ((avctx->
width &3) ||
570 (avctx->
width > 4095 ||
577 (avctx->
width > 16383 ||
578 avctx->
height > 16383 )) {
579 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
610 #if FF_API_PRIVATE_OPT
621 "mpeg2 style quantization not supported by codec\n");
641 "QP RD is no longer compatible with MJPEG or AMV\n");
645 #if FF_API_PRIVATE_OPT
655 "closed gop with scene change detection are not supported yet, "
656 "set threshold to 1000000000\n");
664 "low delay forcing is only available for mpeg2, "
665 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
670 "B-frames cannot be used with low delay\n");
676 if (avctx->
qmax > 28) {
678 "non linear quant only supports qmax <= 28 currently\n");
696 "multi threaded encoding not supported by codec\n");
702 "automatic thread number detection not supported by codec, "
712 #if FF_API_PRIVATE_OPT
723 "notice: b_frame_strategy only affects the first pass\n");
746 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
755 "timebase %d/%d not supported by MPEG 4 standard, "
756 "the maximum admitted value for the timebase denominator "
779 if (!CONFIG_MJPEG_ENCODER ||
786 if (!CONFIG_H261_ENCODER)
790 "The specified picture size of %dx%d is not valid for the "
791 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
801 if (!CONFIG_H263_ENCODER)
806 "The specified picture size of %dx%d is not valid for "
807 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
808 "352x288, 704x576, and 1408x1152. "
899 #if FF_API_PRIVATE_OPT
947 2 * 64 *
sizeof(uint16_t),
fail);
952 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
964 #if FF_API_PRIVATE_OPT
987 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
992 for (i = 0; i < 64; i++) {
1027 #if FF_API_PRIVATE_OPT
1075 if (CONFIG_MJPEG_ENCODER &&
1110 for (y = 0; y < 16; y++) {
1111 for (x = 0; x < 16; x++) {
1112 acc +=
FFABS(src[x + y * stride] - ref);
1128 for (y = 0; y <
h; y += 16) {
1129 for (x = 0; x <
w; x += 16) {
1134 int sae =
get_sae(src + offset, mean, stride);
1136 acc += sae + 500 < sad;
1154 int i, display_picture_number = 0, ret;
1157 int flush_offset = 1;
1170 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1175 if (!s->
low_delay && display_picture_number == 1)
1184 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1187 pts = display_picture_number;
1191 if (!pic_arg->
buf[0] ||
1227 int h_chroma_shift, v_chroma_shift;
1232 for (i = 0; i < 3; i++) {
1233 int src_stride = pic_arg->
linesize[i];
1235 int h_shift = i ? h_chroma_shift : 0;
1236 int v_shift = i ? v_chroma_shift : 0;
1237 int w = s->
width >> h_shift;
1251 if (src_stride == dst_stride)
1252 memcpy(dst, src, src_stride * h);
1257 memcpy(dst2, src, w);
1282 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1286 if (flush_offset <= 1)
1289 encoding_delay = encoding_delay - flush_offset + 1;
1305 int64_t score64 = 0;
1307 for (plane = 0; plane < 3; plane++) {
1309 const int bw = plane ? 1 : 2;
1310 for (y = 0; y < s->
mb_height * bw; y++) {
1311 for (x = 0; x < s->
mb_width * bw; x++) {
1312 int off = p->
shared ? 0 : 16;
1318 case 0: score =
FFMAX(score, v);
break;
1319 case 1: score +=
FFABS(v);
break;
1320 case 2: score64 += v * (int64_t)v;
break;
1321 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1322 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1373 int64_t best_rd = INT64_MAX;
1374 int best_b_count = -1;
1385 b_lambda = p_lambda;
1395 pre_input = *pre_input_ptr;
1396 memcpy(data, pre_input_ptr->
f->
data,
sizeof(data));
1398 if (!pre_input.
shared && i) {
1413 width >> 1, height >> 1);
1418 width >> 1, height >> 1);
1497 return best_b_count;
1570 b_frames =
FFMAX(0, i - 1);
1573 for (i = 0; i < b_frames + 1; i++) {
1584 for (i = b_frames - 1; i >= 0; i--) {
1592 "warning, too many B-frames in a row\n");
1615 for (i = 0; i < b_frames; i++) {
1662 for (i = 0; i < 4; i++) {
1712 #if FF_API_CODED_FRAME
1718 #if FF_API_ERROR_FRAME
1730 for (intra = 0; intra < 2; intra++) {
1732 for (i = 0; i < 64; i++) {
1738 for (i = 0; i < 64; i++) {
1789 for (i = 0; i < 4; i++) {
1820 const AVFrame *pic_arg,
int *got_packet)
1823 int i, stuffing_count, ret;
1852 for (i = 0; i < context_count; i++) {
1869 if (growing_buffer) {
1877 #if FF_API_STAT_BITS
1926 for (i = 0; i < context_count; i++) {
1941 for (i = 0; i < 4; i++) {
1959 if (stuffing_count) {
1961 stuffing_count + 50) {
1969 while (stuffing_count--) {
1976 stuffing_count -= 4;
1977 while (stuffing_count--) {
2006 "Internal error, negative bits\n");
2014 vbv_delay =
FFMAX(vbv_delay, min_delay);
2036 #if FF_API_VBV_DELAY
2043 #if FF_API_STAT_BITS
2076 *got_packet = !!pkt->
size;
2081 int n,
int threshold)
2083 static const char tab[64] = {
2084 3, 2, 2, 1, 1, 1, 1, 1,
2085 1, 1, 1, 1, 1, 1, 1, 1,
2086 1, 1, 1, 1, 1, 1, 1, 1,
2087 0, 0, 0, 0, 0, 0, 0, 0,
2088 0, 0, 0, 0, 0, 0, 0, 0,
2089 0, 0, 0, 0, 0, 0, 0, 0,
2090 0, 0, 0, 0, 0, 0, 0, 0,
2091 0, 0, 0, 0, 0, 0, 0, 0
2100 if (threshold < 0) {
2102 threshold = -threshold;
2107 if (last_index <= skip_dc - 1)
2110 for (i = 0; i <= last_index; i++) {
2114 if (skip_dc && i == 0)
2118 }
else if (level > 1) {
2124 if (score >= threshold)
2126 for (i = skip_dc; i <= last_index; i++) {
2149 for (; i <= last_index; i++) {
2151 int level = block[j];
2153 if (level > maxlevel) {
2156 }
else if (level < minlevel) {
2166 "warning, clipping %d dct coefficients to %d..%d\n",
2167 overflow, minlevel, maxlevel);
2174 for (y = 0; y < 8; y++) {
2175 for (x = 0; x < 8; x++) {
2181 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2182 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2183 int v = ptr[x2 + y2 *
stride];
2189 weight[x + 8 * y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2195 int motion_x,
int motion_y,
2196 int mb_block_height,
2201 int16_t orig[12][64];
2208 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2209 ptrdiff_t wrap_y, wrap_c;
2211 for (i = 0; i < mb_block_count; i++)
2215 const int last_qp = s->
qscale;
2216 const int mb_xy = mb_x + mb_y * s->
mb_stride;
2247 (mb_y * 16 * wrap_y) + mb_x * 16;
2249 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2251 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2259 16, 16, mb_x * 16, mb_y * 16,
2264 mb_block_width, mb_block_height,
2265 mb_x * mb_block_width, mb_y * mb_block_height,
2267 ptr_cb = ebuf + 16 * wrap_y;
2270 mb_block_width, mb_block_height,
2271 mb_x * mb_block_width, mb_y * mb_block_height,
2273 ptr_cr = ebuf + 16 * wrap_y + 16;
2278 int progressive_score, interlaced_score;
2283 NULL, wrap_y, 8) - 400;
2285 if (progressive_score > 0) {
2287 NULL, wrap_y * 2, 8) +
2289 NULL, wrap_y * 2, 8);
2290 if (progressive_score > interlaced_score) {
2293 dct_offset = wrap_y;
2294 uv_dct_offset = wrap_c;
2329 uint8_t *dest_y, *dest_cb, *dest_cr;
2331 dest_y = s->
dest[0];
2332 dest_cb = s->
dest[1];
2333 dest_cr = s->
dest[2];
2357 int progressive_score, interlaced_score;
2360 progressive_score = s->
mecc.
ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2366 progressive_score -= 400;
2368 if (progressive_score > 0) {
2375 if (progressive_score > interlaced_score) {
2378 dct_offset = wrap_y;
2379 uv_dct_offset = wrap_c;
2390 dest_y + dct_offset, wrap_y);
2392 dest_y + dct_offset + 8, wrap_y);
2402 dest_cb + uv_dct_offset, wrap_c);
2404 dest_cr + uv_dct_offset, wrap_c);
2415 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset, dest_y + dct_offset,
2416 wrap_y, 8) < 20 * s->
qscale)
2418 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2419 wrap_y, 8) < 20 * s->
qscale)
2427 dest_cb + uv_dct_offset,
2428 wrap_c, 8) < 20 * s->
qscale)
2431 dest_cr + uv_dct_offset,
2432 wrap_c, 8) < 20 * s->
qscale)
2459 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2465 for (i = 0; i < mb_block_count; i++) {
2480 for (i = 0; i < mb_block_count; i++) {
2490 for (i = 0; i < 4; i++)
2493 for (i = 4; i < mb_block_count; i++)
2497 for (i = 0; i < mb_block_count; i++) {
2510 for (i=6; i<12; i++) {
2519 for (i = 0; i < mb_block_count; i++) {
2522 for (j = 63; j > 0; j--) {
2535 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2539 if (CONFIG_MPEG4_ENCODER)
2549 if (CONFIG_WMV2_ENCODER)
2553 if (CONFIG_H261_ENCODER)
2561 if (CONFIG_H263_ENCODER)
2566 if (CONFIG_MJPEG_ENCODER)
2612 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2650 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2658 s->
pb= pb[*next_block];
2660 s->
pb2 = pb2 [*next_block];
2661 s->
tex_pb= tex_pb[*next_block];
2665 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2688 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2706 else if(w==8 && h==8)
2796 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2804 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2839 if (CONFIG_H263_ENCODER)
2841 bytestream_put_le32(&ptr, offset);
2842 bytestream_put_byte(&ptr, s->
qscale);
2843 bytestream_put_byte(&ptr, gobn);
2844 bytestream_put_le16(&ptr, mba);
2845 bytestream_put_byte(&ptr, pred_x);
2846 bytestream_put_byte(&ptr, pred_y);
2848 bytestream_put_byte(&ptr, 0);
2849 bytestream_put_byte(&ptr, 0);
2884 int new_buffer_size = 0;
2961 if (CONFIG_H263_ENCODER)
2981 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
3015 int current_packet_size, is_gob_start;
3023 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
3032 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
3037 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
3057 current_packet_size=0;
3063 #if FF_API_RTP_CALLBACK
3075 if (CONFIG_MPEG4_ENCODER) {
3082 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3089 if (CONFIG_H263_ENCODER)
3119 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3126 backup_s.pb2= s->
pb2;
3127 backup_s.tex_pb= s->
tex_pb;
3136 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
3137 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3148 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
3149 &dmin, &next_block, 0, 0);
3157 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3158 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3168 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3169 &dmin, &next_block, 0, 0);
3177 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3178 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3186 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3187 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
3197 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3198 &dmin, &next_block, 0, 0);
3209 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3210 &dmin, &next_block, 0, 0);
3221 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3222 &dmin, &next_block, 0, 0);
3228 for(dir=0; dir<2; dir++){
3235 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3236 &dmin, &next_block, 0, 0);
3244 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3245 &dmin, &next_block, 0, 0);
3256 const int last_qp= backup_s.qscale;
3260 static const int dquant_tab[4]={-1,1,-2,2};
3269 s->
mv[0][0][0] = best_s.
mv[0][0][0];
3270 s->
mv[0][0][1] = best_s.
mv[0][0][1];
3271 s->
mv[1][0][0] = best_s.
mv[1][0][0];
3272 s->
mv[1][0][1] = best_s.
mv[1][0][1];
3275 for(; qpi<4; qpi++){
3276 int dquant= dquant_tab[qpi];
3288 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3289 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3305 backup_s.dquant = 0;
3309 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3310 &dmin, &next_block, mx, my);
3313 backup_s.dquant = 0;
3317 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3318 &dmin, &next_block, 0, 0);
3326 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3347 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3348 &dmin, &next_block, mx, my);
3366 s->
pb2= backup_s.pb2;
3370 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3371 s->
tex_pb= backup_s.tex_pb;
3375 if (CONFIG_H263_ENCODER &&
3388 int motion_x = 0, motion_y = 0;
3396 motion_x= s->
mv[0][0][0] = 0;
3397 motion_y= s->
mv[0][0][1] = 0;
3425 if (CONFIG_MPEG4_ENCODER) {
3434 if (CONFIG_MPEG4_ENCODER) {
3484 for(dir=0; dir<2; dir++){
3501 if (CONFIG_H263_ENCODER &&
3546 #if FF_API_RTP_CALLBACK
3562 #define MERGE(field) dst->field += src->field; src->field=0
3589 for(i=0; i<64; i++){
3617 if (CONFIG_MPEG4_ENCODER)
3623 if (CONFIG_H263_ENCODER)
3704 for(i=1; i<context_count; i++){
3735 for(i=1; i<context_count; i++){
3749 ff_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3793 for(dir=0; dir<2; dir++){
3844 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};
3845 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};
3881 if (CONFIG_H261_ENCODER)
3889 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred) {
3902 else if (CONFIG_H263_ENCODER)
3906 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3915 for(i=1; i<context_count; i++){
3919 for(i=1; i<context_count; i++){
3934 for(i=0; i<64; i++){
3935 int level= block[i];
3941 if(level<0) level=0;
3945 if(level>0) level=0;
3954 int qscale,
int *overflow){
3956 const uint16_t *matrix;
3958 const uint8_t *perm_scantable;
3960 unsigned int threshold1, threshold2;
3972 int coeff_count[64];
3973 int qmul, qadd, start_i, last_non_zero, i,
dc;
3985 qadd= ((qscale-1)|1)*8;
3988 else mpeg2_qscale = qscale << 1;
4007 block[0] = (block[0] + (q >> 1)) / q;
4035 threshold2= (threshold1<<1);
4037 for(i=63; i>=start_i; i--) {
4038 const int j = scantable[i];
4039 int level = block[j] * qmat[j];
4041 if(((
unsigned)(level+threshold1))>threshold2){
4047 for(i=start_i; i<=last_non_zero; i++) {
4048 const int j = scantable[i];
4049 int level = block[j] * qmat[j];
4053 if(((
unsigned)(level+threshold1))>threshold2){
4057 coeff[1][i]= level-1;
4061 coeff[0][i]= -
level;
4062 coeff[1][i]= -level+1;
4065 coeff_count[i]=
FFMIN(level, 2);
4069 coeff[0][i]= (level>>31)|1;
4076 if(last_non_zero < start_i){
4077 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4078 return last_non_zero;
4081 score_tab[start_i]= 0;
4082 survivor[0]= start_i;
4085 for(i=start_i; i<=last_non_zero; i++){
4086 int level_index, j, zero_distortion;
4087 int dct_coeff=
FFABS(block[ scantable[i] ]);
4088 int best_score=256*256*256*120;
4092 zero_distortion= dct_coeff*dct_coeff;
4094 for(level_index=0; level_index < coeff_count[i]; level_index++){
4096 int level= coeff[level_index][i];
4097 const int alevel=
FFABS(level);
4103 unquant_coeff= alevel*qmul + qadd;
4106 unquant_coeff = alevel * matrix[j] * 8;
4110 unquant_coeff = (
int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4111 unquant_coeff = (unquant_coeff - 1) | 1;
4113 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
4114 unquant_coeff = (unquant_coeff - 1) | 1;
4119 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4121 if((level&(~127)) == 0){
4122 for(j=survivor_count-1; j>=0; j--){
4123 int run= i - survivor[j];
4125 score += score_tab[i-
run];
4127 if(score < best_score){
4130 level_tab[i+1]= level-64;
4135 for(j=survivor_count-1; j>=0; j--){
4136 int run= i - survivor[j];
4138 score += score_tab[i-
run];
4139 if(score < last_score){
4142 last_level= level-64;
4148 distortion += esc_length*
lambda;
4149 for(j=survivor_count-1; j>=0; j--){
4150 int run= i - survivor[j];
4151 int score= distortion + score_tab[i-
run];
4153 if(score < best_score){
4156 level_tab[i+1]= level-64;
4161 for(j=survivor_count-1; j>=0; j--){
4162 int run= i - survivor[j];
4163 int score= distortion + score_tab[i-
run];
4164 if(score < last_score){
4167 last_level= level-64;
4175 score_tab[i+1]= best_score;
4178 if(last_non_zero <= 27){
4179 for(; survivor_count; survivor_count--){
4180 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4184 for(; survivor_count; survivor_count--){
4185 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4190 survivor[ survivor_count++ ]= i+1;
4194 last_score= 256*256*256*120;
4195 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4196 int score= score_tab[i];
4198 score += lambda * 2;
4200 if(score < last_score){
4203 last_level= level_tab[i];
4204 last_run= run_tab[i];
4211 dc=
FFABS(block[0]);
4212 last_non_zero= last_i - 1;
4213 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4215 if(last_non_zero < start_i)
4216 return last_non_zero;
4218 if(last_non_zero == 0 && start_i == 0){
4220 int best_score= dc *
dc;
4222 for(i=0; i<coeff_count[0]; i++){
4223 int level= coeff[i][0];
4224 int alevel=
FFABS(level);
4225 int unquant_coeff, score, distortion;
4228 unquant_coeff= (alevel*qmul + qadd)>>3;
4230 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
4231 unquant_coeff = (unquant_coeff - 1) | 1;
4233 unquant_coeff = (unquant_coeff + 4) >> 3;
4234 unquant_coeff<<= 3 + 3;
4236 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
4239 else score= distortion + esc_length*
lambda;
4241 if(score < best_score){
4243 best_level= level - 64;
4246 block[0]= best_level;
4248 if(best_level == 0)
return -1;
4249 else return last_non_zero;
4255 block[ perm_scantable[last_non_zero] ]= last_level;
4258 for(; i>start_i; i -= run_tab[i] + 1){
4259 block[ perm_scantable[i-1] ]= level_tab[i];
4262 return last_non_zero;
4277 int perm_index= perm[
index];
4278 if(i==0) s*= sqrt(0.5);
4279 if(j==0) s*= sqrt(0.5);
4280 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)));
4293 const uint8_t *perm_scantable;
4299 int qmul, qadd, start_i, last_non_zero, i,
dc;
4303 int rle_index,
run, q = 1, sum;
4306 static int after_last=0;
4307 static int to_zero=0;
4308 static int from_zero=0;
4311 static int messed_sign=0;
4314 if(basis[0][0] == 0)
4360 for(i=0; i<64; i++){
4367 for(i=0; i<64; i++){
4372 w=
FFABS(weight[i]) + qns*one;
4373 w= 15 + (48*qns*one + w/2)/w;
4388 for(i=start_i; i<=last_non_zero; i++){
4389 int j= perm_scantable[i];
4390 const int level= block[j];
4394 if(level<0) coeff= qmul*level - qadd;
4395 else coeff= qmul*level + qadd;
4396 run_tab[rle_index++]=
run;
4405 if(last_non_zero>0){
4416 int run2, best_unquant_change=0, analyze_gradient;
4422 if(analyze_gradient){
4426 for(i=0; i<64; i++){
4442 const int level= block[0];
4443 int change, old_coeff;
4449 for(change=-1; change<=1; change+=2){
4450 int new_level= level + change;
4451 int score, new_coeff;
4453 new_coeff= q*new_level;
4454 if(new_coeff >= 2048 || new_coeff < 0)
4458 new_coeff - old_coeff);
4459 if(score<best_score){
4462 best_change= change;
4463 best_unquant_change= new_coeff - old_coeff;
4470 run2= run_tab[rle_index++];
4474 for(i=start_i; i<64; i++){
4475 int j= perm_scantable[i];
4476 const int level= block[j];
4477 int change, old_coeff;
4483 if(level<0) old_coeff= qmul*level - qadd;
4484 else old_coeff= qmul*level + qadd;
4485 run2= run_tab[rle_index++];
4492 for(change=-1; change<=1; change+=2){
4493 int new_level= level + change;
4494 int score, new_coeff, unquant_change;
4501 if(new_level<0) new_coeff= qmul*new_level - qadd;
4502 else new_coeff= qmul*new_level + qadd;
4503 if(new_coeff >= 2048 || new_coeff <= -2048)
4508 if(level < 63 && level > -63){
4509 if(i < last_non_zero)
4519 if(analyze_gradient){
4520 int g= d1[ scantable[i] ];
4521 if(g && (g^new_level) >= 0)
4525 if(i < last_non_zero){
4526 int next_i= i + run2 + 1;
4527 int next_level= block[ perm_scantable[next_i] ] + 64;
4529 if(next_level&(~127))
4532 if(next_i < last_non_zero)
4552 if(i < last_non_zero){
4553 int next_i= i + run2 + 1;
4554 int next_level= block[ perm_scantable[next_i] ] + 64;
4556 if(next_level&(~127))
4559 if(next_i < last_non_zero)