73 #define QUANT_BIAS_SHIFT 8 75 #define QMAT_SHIFT_MMX 16 94 const uint16_t *quant_matrix,
95 int bias,
int qmin,
int qmax,
int intra)
101 for (qscale = qmin; qscale <= qmax; qscale++) {
106 else qscale2 = qscale << 1;
113 for (i = 0; i < 64; i++) {
115 int64_t den = (int64_t) qscale2 * quant_matrix[j];
125 for (i = 0; i < 64; i++) {
127 int64_t den =
ff_aanscales[
i] * (int64_t) qscale2 * quant_matrix[j];
137 for (i = 0; i < 64; i++) {
139 int64_t den = (int64_t) qscale2 * quant_matrix[j];
150 if (
qmat16[qscale][0][i] == 0 ||
151 qmat16[qscale][0][i] == 128 * 256)
159 for (i = intra; i < 64; i++) {
164 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
171 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
180 int bestdiff=INT_MAX;
188 if (diff < bestdiff) {
210 for (i = 0; i < 64; i++) {
225 for (i = 0; i < s->
mb_num; i++) {
236 #define COPY(a) dst->a= src->a 261 for (i = -16; i < 16; i++) {
276 if (CONFIG_H263_ENCODER)
294 int i,
ret, format_supported;
303 "only YUV420 and YUV422 are supported\n");
309 format_supported = 0;
318 format_supported = 1;
324 format_supported = 1;
326 if (!format_supported) {
336 "only YUV420/YUV422/YUV444 are supported (no alpha support yet)\n");
365 #if FF_API_PRIVATE_OPT 382 "keyframe interval too large!, reducing it from %d to %d\n",
408 "intra dc precision must be positive, note some applications use" 409 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
470 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
476 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
493 "impossible bitrate constraints, this will fail\n");
518 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the " 519 "specified vbv buffer is too large for the given bitrate!\n");
531 "OBMC is only supported with simple mb decision\n");
549 "max b frames must be 0 or positive for mpegvideo based encoders\n");
559 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
567 (avctx->
width > 2048 ||
574 ((avctx->
width &3) ||
581 (avctx->
width > 4095 ||
588 (avctx->
width > 16383 ||
589 avctx->
height > 16383 )) {
590 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
621 #if FF_API_PRIVATE_OPT 632 "mpeg2 style quantization not supported by codec\n");
652 "QP RD is no longer compatible with MJPEG or AMV\n");
656 #if FF_API_PRIVATE_OPT 666 "closed gop with scene change detection are not supported yet, " 667 "set threshold to 1000000000\n");
675 "low delay forcing is only available for mpeg2, " 676 "set strict_std_compliance to 'unofficial' or lower in order to allow it\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");
766 "timebase %d/%d not supported by MPEG 4 standard, " 767 "the maximum admitted value for the timebase denominator " 790 if (!CONFIG_MJPEG_ENCODER)
800 if (!CONFIG_SPEEDHQ_ENCODER)
808 if (!CONFIG_H261_ENCODER)
812 "The specified picture size of %dx%d is not valid for the " 813 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
823 if (!CONFIG_H263_ENCODER)
828 "The specified picture size of %dx%d is not valid for " 829 "the H.263 codec.\nValid sizes are 128x96, 176x144, " 830 "352x288, 704x576, and 1408x1152. " 921 #if FF_API_PRIVATE_OPT 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++) {
1050 #if FF_API_PRIVATE_OPT 1095 if (CONFIG_MJPEG_ENCODER &&
1130 for (y = 0; y < 16; y++) {
1131 for (x = 0; x < 16; x++) {
1132 acc +=
FFABS(src[x + y * stride] - ref);
1148 for (y = 0; y <
h; y += 16) {
1149 for (x = 0; x <
w; x += 16) {
1154 int sae =
get_sae(src + offset, mean, stride);
1156 acc += sae + 500 < sad;
1174 int i, display_picture_number = 0,
ret;
1177 int flush_offset = 1;
1190 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1195 if (!s->
low_delay && display_picture_number == 1)
1204 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1207 pts = display_picture_number;
1211 if (!pic_arg->
buf[0] ||
1247 int h_chroma_shift, v_chroma_shift;
1252 for (i = 0; i < 3; i++) {
1255 int h_shift = i ? h_chroma_shift : 0;
1256 int v_shift = i ? v_chroma_shift : 0;
1257 int w = s->
width >> h_shift;
1271 if (src_stride == dst_stride)
1272 memcpy(dst, src, src_stride * h);
1277 memcpy(dst2, src, w);
1302 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1306 if (flush_offset <= 1)
1309 encoding_delay = encoding_delay - flush_offset + 1;
1325 int64_t score64 = 0;
1327 for (plane = 0; plane < 3; plane++) {
1329 const int bw = plane ? 1 : 2;
1330 for (y = 0; y < s->
mb_height * bw; y++) {
1331 for (x = 0; x < s->
mb_width * bw; x++) {
1332 int off = p->
shared ? 0 : 16;
1338 case 0: score =
FFMAX(score, v);
break;
1339 case 1: score +=
FFABS(v);
break;
1340 case 2: score64 += v * (int64_t)v;
break;
1341 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1342 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1393 int64_t best_rd = INT64_MAX;
1394 int best_b_count = -1;
1405 b_lambda = p_lambda;
1415 pre_input = *pre_input_ptr;
1416 memcpy(data, pre_input_ptr->
f->
data,
sizeof(data));
1418 if (!pre_input.
shared && i) {
1433 width >> 1, height >> 1);
1438 width >> 1, height >> 1);
1517 return best_b_count;
1590 b_frames =
FFMAX(0, i - 1);
1593 for (i = 0; i < b_frames + 1; i++) {
1604 for (i = b_frames - 1; i >= 0; i--) {
1612 "warning, too many B-frames in a row\n");
1635 for (i = 0; i < b_frames; i++) {
1682 for (i = 0; i < 4; i++) {
1732 #if FF_API_CODED_FRAME 1738 #if FF_API_ERROR_FRAME 1750 for (intra = 0; intra < 2; intra++) {
1752 for (i = 0; i < 64; i++) {
1758 for (i = 0; i < 64; i++) {
1809 for (i = 0; i < 4; i++) {
1840 const AVFrame *pic_arg,
int *got_packet)
1843 int i, stuffing_count,
ret;
1872 for (i = 0; i < context_count; i++) {
1876 uint8_t *start = pkt->
data + (size_t)(((int64_t) pkt->
size) * start_y /
h);
1889 if (growing_buffer) {
1897 #if FF_API_STAT_BITS 1946 for (i = 0; i < context_count; i++) {
1961 for (i = 0; i < 4; i++) {
1979 if (stuffing_count) {
1981 stuffing_count + 50) {
1989 while (stuffing_count--) {
1996 stuffing_count -= 4;
1997 while (stuffing_count--) {
2026 "Internal error, negative bits\n");
2031 min_delay = (minbits * 90000LL + avctx->
rc_max_rate - 1) /
2034 vbv_delay =
FFMAX(vbv_delay, min_delay);
2056 #if FF_API_VBV_DELAY 2063 #if FF_API_STAT_BITS 2096 *got_packet = !!pkt->
size;
2101 int n,
int threshold)
2103 static const char tab[64] = {
2104 3, 2, 2, 1, 1, 1, 1, 1,
2105 1, 1, 1, 1, 1, 1, 1, 1,
2106 1, 1, 1, 1, 1, 1, 1, 1,
2107 0, 0, 0, 0, 0, 0, 0, 0,
2108 0, 0, 0, 0, 0, 0, 0, 0,
2109 0, 0, 0, 0, 0, 0, 0, 0,
2110 0, 0, 0, 0, 0, 0, 0, 0,
2111 0, 0, 0, 0, 0, 0, 0, 0
2120 if (threshold < 0) {
2122 threshold = -threshold;
2127 if (last_index <= skip_dc - 1)
2130 for (i = 0; i <= last_index; i++) {
2134 if (skip_dc && i == 0)
2138 }
else if (level > 1) {
2144 if (score >= threshold)
2146 for (i = skip_dc; i <= last_index; i++) {
2169 for (; i <= last_index; i++) {
2171 int level = block[j];
2173 if (level > maxlevel) {
2176 }
else if (level < minlevel) {
2186 "warning, clipping %d dct coefficients to %d..%d\n",
2187 overflow, minlevel, maxlevel);
2194 for (y = 0; y < 8; y++) {
2195 for (x = 0; x < 8; x++) {
2201 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2202 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2203 int v = ptr[x2 + y2 *
stride];
2209 weight[x + 8 * y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2215 int motion_x,
int motion_y,
2216 int mb_block_height,
2221 int16_t orig[12][64];
2228 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2229 ptrdiff_t wrap_y, wrap_c;
2231 for (i = 0; i < mb_block_count; i++)
2235 const int last_qp = s->
qscale;
2236 const int mb_xy = mb_x + mb_y * s->
mb_stride;
2267 (mb_y * 16 * wrap_y) + mb_x * 16;
2269 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2271 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2279 16, 16, mb_x * 16, mb_y * 16,
2284 mb_block_width, mb_block_height,
2285 mb_x * mb_block_width, mb_y * mb_block_height,
2287 ptr_cb = ebuf + 16 * wrap_y;
2290 mb_block_width, mb_block_height,
2291 mb_x * mb_block_width, mb_y * mb_block_height,
2293 ptr_cr = ebuf + 16 * wrap_y + 16;
2298 int progressive_score, interlaced_score;
2303 NULL, wrap_y, 8) - 400;
2305 if (progressive_score > 0) {
2307 NULL, wrap_y * 2, 8) +
2309 NULL, wrap_y * 2, 8);
2310 if (progressive_score > interlaced_score) {
2313 dct_offset = wrap_y;
2314 uv_dct_offset = wrap_c;
2349 uint8_t *dest_y, *dest_cb, *dest_cr;
2351 dest_y = s->
dest[0];
2352 dest_cb = s->
dest[1];
2353 dest_cr = s->
dest[2];
2377 int progressive_score, interlaced_score;
2380 progressive_score = s->
mecc.
ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2386 progressive_score -= 400;
2388 if (progressive_score > 0) {
2395 if (progressive_score > interlaced_score) {
2398 dct_offset = wrap_y;
2399 uv_dct_offset = wrap_c;
2410 dest_y + dct_offset, wrap_y);
2412 dest_y + dct_offset + 8, wrap_y);
2422 dest_cb + uv_dct_offset, wrap_c);
2424 dest_cr + uv_dct_offset, wrap_c);
2435 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset, dest_y + dct_offset,
2436 wrap_y, 8) < 20 * s->
qscale)
2438 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2439 wrap_y, 8) < 20 * s->
qscale)
2447 dest_cb + uv_dct_offset,
2448 wrap_c, 8) < 20 * s->
qscale)
2451 dest_cr + uv_dct_offset,
2452 wrap_c, 8) < 20 * s->
qscale)
2479 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2485 for (i = 0; i < mb_block_count; i++) {
2500 for (i = 0; i < mb_block_count; i++) {
2510 for (i = 0; i < 4; i++)
2513 for (i = 4; i < mb_block_count; i++)
2517 for (i = 0; i < mb_block_count; i++) {
2530 for (i=6; i<12; i++) {
2539 for (i = 0; i < mb_block_count; i++) {
2542 for (j = 63; j > 0; j--) {
2555 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2559 if (CONFIG_MPEG4_ENCODER)
2569 if (CONFIG_WMV2_ENCODER)
2573 if (CONFIG_H261_ENCODER)
2581 if (CONFIG_H263_ENCODER)
2586 if (CONFIG_MJPEG_ENCODER)
2590 if (CONFIG_SPEEDHQ_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;
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)
3003 if (first_in_slice && mb_y_order != s->
start_mb_y)
3015 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
3049 int current_packet_size, is_gob_start;
3057 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
3066 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
3071 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
3091 current_packet_size=0;
3097 #if FF_API_RTP_CALLBACK 3109 if (CONFIG_MPEG4_ENCODER) {
3116 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3123 if (CONFIG_H263_ENCODER)
3153 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3160 backup_s.pb2= s->
pb2;
3161 backup_s.tex_pb= s->
tex_pb;
3170 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
3171 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3182 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
3183 &dmin, &next_block, 0, 0);
3191 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3192 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3202 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3203 &dmin, &next_block, 0, 0);
3211 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3212 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3220 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3221 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
3231 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3232 &dmin, &next_block, 0, 0);
3243 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3244 &dmin, &next_block, 0, 0);
3255 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3256 &dmin, &next_block, 0, 0);
3262 for(dir=0; dir<2; dir++){
3269 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3270 &dmin, &next_block, 0, 0);
3278 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3279 &dmin, &next_block, 0, 0);
3290 const int last_qp= backup_s.qscale;
3294 static const int dquant_tab[4]={-1,1,-2,2};
3303 s->
mv[0][0][0] = best_s.
mv[0][0][0];
3304 s->
mv[0][0][1] = best_s.
mv[0][0][1];
3305 s->
mv[1][0][0] = best_s.
mv[1][0][0];
3306 s->
mv[1][0][1] = best_s.
mv[1][0][1];
3309 for(; qpi<4; qpi++){
3310 int dquant= dquant_tab[qpi];
3322 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3323 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3339 backup_s.dquant = 0;
3343 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3344 &dmin, &next_block, mx, my);
3347 backup_s.dquant = 0;
3351 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3352 &dmin, &next_block, 0, 0);
3360 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3381 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3382 &dmin, &next_block, mx, my);
3393 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3399 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3400 s->
pb2= backup_s.pb2;
3404 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3405 s->
tex_pb= backup_s.tex_pb;
3409 if (CONFIG_H263_ENCODER &&
3422 int motion_x = 0, motion_y = 0;
3430 motion_x= s->
mv[0][0][0] = 0;
3431 motion_y= s->
mv[0][0][1] = 0;
3459 if (CONFIG_MPEG4_ENCODER) {
3468 if (CONFIG_MPEG4_ENCODER) {
3518 for(dir=0; dir<2; dir++){
3535 if (CONFIG_H263_ENCODER &&
3580 #if FF_API_RTP_CALLBACK 3596 #define MERGE(field) dst->field += src->field; src->field=0 3623 for(i=0; i<64; i++){
3651 if (CONFIG_MPEG4_ENCODER)
3657 if (CONFIG_H263_ENCODER)
3738 for(i=1; i<context_count; i++){
3769 for(i=1; i<context_count; i++){
3783 ff_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3827 for(dir=0; dir<2; dir++){
3878 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};
3879 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};
3920 if (CONFIG_SPEEDHQ_ENCODER)
3924 if (CONFIG_H261_ENCODER)
3932 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred) {
3945 else if (CONFIG_H263_ENCODER)
3949 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3958 for(i=1; i<context_count; i++){
3962 for(i=1; i<context_count; i++){
3977 for(i=0; i<64; i++){
3984 if(level<0) level=0;
3988 if(level>0) level=0;
3996 int16_t *
block,
int n,
3999 const uint16_t *matrix;
4001 const uint8_t *perm_scantable;
4003 unsigned int threshold1, threshold2;
4015 int coeff_count[64];
4016 int qmul, qadd, start_i, last_non_zero,
i,
dc;
4028 qadd= ((qscale-1)|1)*8;
4031 else mpeg2_qscale = qscale << 1;
4050 block[0] = (block[0] + (q >> 1)) / q;
4078 threshold2= (threshold1<<1);
4080 for(i=63; i>=start_i; i--) {
4081 const int j = scantable[
i];
4082 int level = block[j] * qmat[j];
4084 if(((
unsigned)(level+threshold1))>threshold2){
4090 for(i=start_i; i<=last_non_zero; i++) {
4091 const int j = scantable[
i];
4092 int level = block[j] * qmat[j];
4096 if(((
unsigned)(level+threshold1))>threshold2){
4100 coeff[1][
i]= level-1;
4105 coeff[1][
i]= -level+1;
4108 coeff_count[
i]=
FFMIN(level, 2);
4112 coeff[0][
i]= (level>>31)|1;
4119 if(last_non_zero < start_i){
4120 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4121 return last_non_zero;
4124 score_tab[start_i]= 0;
4125 survivor[0]= start_i;
4128 for(i=start_i; i<=last_non_zero; i++){
4129 int level_index, j, zero_distortion;
4130 int dct_coeff=
FFABS(block[ scantable[i] ]);
4131 int best_score=256*256*256*120;
4135 zero_distortion= dct_coeff*dct_coeff;
4137 for(level_index=0; level_index < coeff_count[
i]; level_index++){
4139 int level= coeff[level_index][
i];
4140 const int alevel=
FFABS(level);
4146 unquant_coeff= alevel*qmul + qadd;
4149 unquant_coeff = alevel * matrix[j] * 8;
4153 unquant_coeff = (
int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4154 unquant_coeff = (unquant_coeff - 1) | 1;
4156 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
4157 unquant_coeff = (unquant_coeff - 1) | 1;
4162 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4164 if((level&(~127)) == 0){
4165 for(j=survivor_count-1; j>=0; j--){
4166 int run= i - survivor[j];
4168 score += 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];
4181 score += score_tab[i-
run];
4182 if(score < last_score){
4185 last_level= level-64;
4191 distortion += esc_length*
lambda;
4192 for(j=survivor_count-1; j>=0; j--){
4193 int run= i - survivor[j];
4194 int score= distortion + score_tab[i-
run];
4196 if(score < best_score){
4199 level_tab[i+1]= level-64;
4204 for(j=survivor_count-1; j>=0; j--){
4205 int run= i - survivor[j];
4206 int score= distortion + score_tab[i-
run];
4207 if(score < last_score){
4210 last_level= level-64;
4218 score_tab[i+1]= best_score;
4221 if(last_non_zero <= 27){
4222 for(; survivor_count; survivor_count--){
4223 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4227 for(; survivor_count; survivor_count--){
4228 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4233 survivor[ survivor_count++ ]= i+1;
4237 last_score= 256*256*256*120;
4238 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4239 int score= score_tab[
i];
4241 score += lambda * 2;
4243 if(score < last_score){
4246 last_level= level_tab[
i];
4247 last_run= run_tab[
i];
4254 dc=
FFABS(block[0]);
4255 last_non_zero= last_i - 1;
4256 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4258 if(last_non_zero < start_i)
4259 return last_non_zero;
4261 if(last_non_zero == 0 && start_i == 0){
4263 int best_score= dc *
dc;
4265 for(i=0; i<coeff_count[0]; i++){
4267 int alevel=
FFABS(level);
4268 int unquant_coeff, score, distortion;
4271 unquant_coeff= (alevel*qmul + qadd)>>3;
4273 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
4274 unquant_coeff = (unquant_coeff - 1) | 1;
4276 unquant_coeff = (unquant_coeff + 4) >> 3;
4277 unquant_coeff<<= 3 + 3;
4279 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
4282 else score= distortion + esc_length*
lambda;
4284 if(score < best_score){
4286 best_level= level - 64;
4289 block[0]= best_level;
4291 if(best_level == 0)
return -1;
4292 else return last_non_zero;
4298 block[ perm_scantable[last_non_zero] ]= last_level;
4301 for(; i>start_i; i -= run_tab[
i] + 1){
4302 block[ perm_scantable[i-1] ]= level_tab[
i];
4305 return last_non_zero;
4319 int perm_index= perm[
index];
4320 if(i==0) s*= sqrt(0.5);
4321 if(j==0) s*= sqrt(0.5);
4322 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)));
4335 const uint8_t *perm_scantable;
4341 int qmul, qadd, start_i, last_non_zero,
i,
dc;
4345 int rle_index,
run, q = 1, sum;
4347 if(basis[0][0] == 0)
4390 for(i=0; i<64; i++){
4395 for(i=0; i<64; i++){
4400 w=
FFABS(weight[i]) + qns*one;
4401 w= 15 + (48*qns*one + w/2)/w;
4414 for(i=start_i; i<=last_non_zero; i++){
4415 int j= perm_scantable[
i];
4416 const int level= block[j];
4420 if(level<0) coeff= qmul*level - qadd;
4421 else coeff= qmul*level + qadd;
4422 run_tab[rle_index++]=
run;
4435 int run2, best_unquant_change=0, analyze_gradient;
4438 if(analyze_gradient){
4439 for(i=0; i<64; i++){
4448 const int level= block[0];
4449 int change, old_coeff;
4455 for(change=-1; change<=1; change+=2){
4456 int new_level= level + change;
4457 int score, new_coeff;
4459 new_coeff= q*new_level;
4460 if(new_coeff >= 2048 || new_coeff < 0)
4464 new_coeff - old_coeff);
4465 if(score<best_score){
4468 best_change= change;
4469 best_unquant_change= new_coeff - old_coeff;
4476 run2= run_tab[rle_index++];
4480 for(i=start_i; i<64; i++){
4481 int j= perm_scantable[
i];
4482 const int level= block[j];
4483 int change, old_coeff;
4489 if(level<0) old_coeff= qmul*level - qadd;
4490 else old_coeff= qmul*level + qadd;
4491 run2= run_tab[rle_index++];