62 #define QUANT_BIAS_SHIFT 8
64 #define QMAT_SHIFT_MMX 16
82 uint16_t (*qmat16)[2][64],
83 const uint16_t *quant_matrix,
84 int bias,
int qmin,
int qmax,
int intra)
90 for (qscale = qmin; qscale <= qmax; qscale++) {
97 for (i = 0; i < 64; i++) {
106 (qscale * quant_matrix[j]));
109 for (i = 0; i < 64; i++) {
121 for (i = 0; i < 64; i++) {
129 (qscale * quant_matrix[j]));
133 (qscale * quant_matrix[j]);
135 if (qmat16[qscale][0][i] == 0 ||
136 qmat16[qscale][0][i] == 128 * 256)
137 qmat16[
qscale][0][i] = 128 * 256 - 1;
140 qmat16[qscale][0][i]);
144 for (i = intra; i < 64; i++) {
149 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
156 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
177 for (i = 0; i < 64; i++) {
192 for (i = 0; i < s->
mb_num; i++) {
203 #define COPY(a) dst->a= src->a
228 for (i = -16; i < 16; i++) {
242 if (CONFIG_H263_ENCODER)
259 int i,
ret, format_supported;
268 "only YUV420 and YUV422 are supported\n");
274 format_supported = 0;
283 format_supported = 1;
289 format_supported = 1;
291 if (!format_supported) {
325 "keyframe interval too large!, reducing it from %d to %d\n",
354 "intra dc precision must be positive, note some applications use"
355 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
415 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
421 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
438 "impossible bitrate constraints, this will fail\n");
463 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
464 "specified vbv buffer is too large for the given bitrate!\n");
476 "OBMC is only supported with simple mb decision\n");
494 "max b frames must be 0 or positive for mpegvideo based encoders\n");
504 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
512 (avctx->
width > 2048 ||
519 ((avctx->
width &3) ||
526 (avctx->
width > 4095 ||
533 (avctx->
width > 16383 ||
534 avctx->
height > 16383 )) {
535 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
570 "mpeg2 style quantization not supported by codec\n");
588 "closed gop with scene change detection are not supported yet, "
589 "set threshold to 1000000000\n");
596 "low delay forcing is only available for mpeg2\n");
601 "b frames cannot be used with low delay\n");
607 if (avctx->
qmax > 12) {
609 "non linear quant only supports qmax <= 12 currently\n");
621 "multi threaded encoding not supported by codec\n");
627 "automatic thread number detection not supported by codec, "
643 i = (INT_MAX / 2 + 128) >> 8;
652 "notice: b_frame_strategy only affects the first pass\n");
675 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
689 "timebase %d/%d not supported by MPEG 4 standard, "
690 "the maximum admitted value for the timebase denominator "
713 if (!CONFIG_MJPEG_ENCODER ||
720 if (!CONFIG_H261_ENCODER)
724 "The specified picture size of %dx%d is not valid for the "
725 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
734 if (!CONFIG_H263_ENCODER)
739 "The specified picture size of %dx%d is not valid for "
740 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
741 "352x288, 704x576, and 1408x1152. "
857 (
MAX_RUN + 1) * 2 *
sizeof(
int), fail);
874 2 * 64 *
sizeof(uint16_t), fail);
879 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
893 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
898 for (i = 0; i < 64; i++) {
932 #if FF_API_ERROR_RATE
939 #if FF_API_NORMALIZE_AQP
983 if (CONFIG_MJPEG_ENCODER &&
1018 for (y = 0; y < 16; y++) {
1019 for (x = 0; x < 16; x++) {
1020 acc +=
FFABS(src[x + y * stride] - ref);
1036 for (y = 0; y < h; y += 16) {
1037 for (x = 0; x < w; x += 16) {
1038 int offset = x + y * stride;
1042 int sae =
get_sae(src + offset, mean, stride);
1044 acc += sae + 500 < sad;
1055 int i, display_picture_number = 0,
ret;
1070 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1075 if (!s->
low_delay && display_picture_number == 1)
1084 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1087 pts = display_picture_number;
1093 if (!pic_arg->
buf[0])
1141 int h_chroma_shift, v_chroma_shift;
1146 for (i = 0; i < 3; i++) {
1147 int src_stride = pic_arg->
linesize[i];
1149 int h_shift = i ? h_chroma_shift : 0;
1150 int v_shift = i ? v_chroma_shift : 0;
1151 int w = s->
width >> h_shift;
1152 int h = s->
height >> v_shift;
1165 if (src_stride == dst_stride)
1166 memcpy(dst, src, src_stride * h);
1171 memcpy(dst2, src, w);
1207 int64_t score64 = 0;
1209 for (plane = 0; plane < 3; plane++) {
1211 const int bw = plane ? 1 : 2;
1212 for (y = 0; y < s->
mb_height * bw; y++) {
1213 for (x = 0; x < s->
mb_width * bw; x++) {
1214 int off = p->
shared ? 0 : 16;
1215 uint8_t *dptr = p->
f->
data[plane] + 8 * (x + y * stride) + off;
1216 uint8_t *rptr = ref->
f->
data[plane] + 8 * (x + y * stride);
1220 case 0: score =
FFMAX(score, v);
break;
1221 case 1: score +=
FFABS(v);
break;
1222 case 2: score64 += v * (int64_t)v;
break;
1223 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1224 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1247 int ret, got_output;
1264 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1265 int64_t best_rd = INT64_MAX;
1266 int best_b_count = -1;
1276 b_lambda = p_lambda;
1301 pre_input = *pre_input_ptr;
1302 memcpy(data, pre_input_ptr->
f->
data,
sizeof(data));
1304 if (!pre_input.
shared && i) {
1372 return best_b_count;
1445 b_frames =
FFMAX(0, i - 1);
1448 for (i = 0; i < b_frames + 1; i++) {
1460 for (i = b_frames - 1; i >= 0; i--) {
1468 "warning, too many b frames in a row\n");
1491 for (i = 0; i < b_frames; i++) {
1537 for (i = 0; i < 4; i++) {
1597 for (intra = 0; intra < 2; intra++) {
1599 for (i = 0; i < 64; i++) {
1605 for (i = 0; i < 64; i++) {
1656 for (i = 0; i < 4; i++) {
1687 const AVFrame *pic_arg,
int *got_packet)
1690 int i, stuffing_count,
ret;
1713 for (i = 0; i < context_count; i++) {
1775 for (i = 0; i < context_count; i++) {
1788 for (i = 0; i < 4; i++) {
1804 if (stuffing_count) {
1806 stuffing_count + 50) {
1814 while (stuffing_count--) {
1821 stuffing_count -= 4;
1822 while (stuffing_count--) {
1848 "Internal error, negative bits\n");
1856 vbv_delay =
FFMAX(vbv_delay, min_delay);
1896 *got_packet = !!pkt->
size;
1901 int n,
int threshold)
1903 static const char tab[64] = {
1904 3, 2, 2, 1, 1, 1, 1, 1,
1905 1, 1, 1, 1, 1, 1, 1, 1,
1906 1, 1, 1, 1, 1, 1, 1, 1,
1907 0, 0, 0, 0, 0, 0, 0, 0,
1908 0, 0, 0, 0, 0, 0, 0, 0,
1909 0, 0, 0, 0, 0, 0, 0, 0,
1910 0, 0, 0, 0, 0, 0, 0, 0,
1911 0, 0, 0, 0, 0, 0, 0, 0
1920 if (threshold < 0) {
1922 threshold = -threshold;
1927 if (last_index <= skip_dc - 1)
1930 for (i = 0; i <= last_index; i++) {
1934 if (skip_dc && i == 0)
1938 }
else if (level > 1) {
1944 if (score >= threshold)
1946 for (i = skip_dc; i <= last_index; i++) {
1969 for (; i <= last_index; i++) {
1971 int level = block[j];
1973 if (level > maxlevel) {
1976 }
else if (level < minlevel) {
1986 "warning, clipping %d dct coefficients to %d..%d\n",
1987 overflow, minlevel, maxlevel);
1994 for (y = 0; y < 8; y++) {
1995 for (x = 0; x < 8; x++) {
2001 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2002 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2003 int v = ptr[x2 + y2 * stride];
2009 weight[x + 8 *
y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2015 int motion_x,
int motion_y,
2016 int mb_block_height,
2021 int16_t orig[12][64];
2028 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2029 ptrdiff_t wrap_y, wrap_c;
2031 for (i = 0; i < mb_block_count; i++)
2035 const int last_qp = s->
qscale;
2036 const int mb_xy = mb_x + mb_y * s->
mb_stride;
2067 (mb_y * 16 * wrap_y) + mb_x * 16;
2069 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2071 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2079 16, 16, mb_x * 16, mb_y * 16,
2084 mb_block_width, mb_block_height,
2085 mb_x * mb_block_width, mb_y * mb_block_height,
2087 ptr_cb = ebuf + 16 * wrap_y;
2090 mb_block_width, mb_block_height,
2091 mb_x * mb_block_width, mb_y * mb_block_height,
2093 ptr_cr = ebuf + 16 * wrap_y + 16;
2098 int progressive_score, interlaced_score;
2101 progressive_score = s->
mecc.
ildct_cmp[4](
s, ptr_y, NULL, wrap_y, 8) +
2103 NULL, wrap_y, 8) - 400;
2105 if (progressive_score > 0) {
2107 NULL, wrap_y * 2, 8) +
2109 NULL, wrap_y * 2, 8);
2110 if (progressive_score > interlaced_score) {
2113 dct_offset = wrap_y;
2114 uv_dct_offset = wrap_c;
2149 uint8_t *dest_y, *dest_cb, *dest_cr;
2151 dest_y = s->
dest[0];
2152 dest_cb = s->
dest[1];
2153 dest_cr = s->
dest[2];
2177 int progressive_score, interlaced_score;
2180 progressive_score = s->
mecc.
ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2186 progressive_score -= 400;
2188 if (progressive_score > 0) {
2195 if (progressive_score > interlaced_score) {
2198 dct_offset = wrap_y;
2199 uv_dct_offset = wrap_c;
2210 dest_y + dct_offset, wrap_y);
2212 dest_y + dct_offset + 8, wrap_y);
2222 dest_cb + uv_dct_offset, wrap_c);
2224 dest_cr + uv_dct_offset, wrap_c);
2231 if (s->
mecc.
sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->
qscale)
2233 if (s->
mecc.
sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->
qscale)
2235 if (s->
mecc.
sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2236 wrap_y, 8) < 20 * s->
qscale)
2238 if (s->
mecc.
sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2239 wrap_y, 8) < 20 * s->
qscale)
2241 if (s->
mecc.
sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->
qscale)
2243 if (s->
mecc.
sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->
qscale)
2246 if (s->
mecc.
sad[1](NULL, ptr_cb + uv_dct_offset,
2247 dest_cb + uv_dct_offset,
2248 wrap_c, 8) < 20 * s->
qscale)
2250 if (s->
mecc.
sad[1](NULL, ptr_cr + uv_dct_offset,
2251 dest_cr + uv_dct_offset,
2252 wrap_c, 8) < 20 * s->
qscale)
2279 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2285 for (i = 0; i < mb_block_count; i++) {
2300 for (i = 0; i < mb_block_count; i++) {
2310 for (i = 0; i < 4; i++)
2313 for (i = 4; i < mb_block_count; i++)
2317 for (i = 0; i < mb_block_count; i++) {
2330 for (i=6; i<12; i++) {
2339 for (i = 0; i < mb_block_count; i++) {
2342 for (j = 63; j > 0; j--) {
2355 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2359 if (CONFIG_MPEG4_ENCODER)
2369 if (CONFIG_WMV2_ENCODER)
2373 if (CONFIG_H261_ENCODER)
2381 if (CONFIG_H263_ENCODER)
2386 if (CONFIG_MJPEG_ENCODER)
2432 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2470 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2478 s->
pb= pb[*next_block];
2480 s->
pb2 = pb2 [*next_block];
2481 s->
tex_pb= tex_pb[*next_block];
2485 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2508 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2525 return s->
mecc.
sse[0](NULL, src1, src2, stride, 16);
2526 else if(w==8 && h==8)
2527 return s->
mecc.
sse[1](NULL, src1, src2, stride, 8);
2531 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2616 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2624 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2659 if (CONFIG_H263_ENCODER)
2661 bytestream_put_le32(&ptr, offset);
2662 bytestream_put_byte(&ptr, s->
qscale);
2663 bytestream_put_byte(&ptr, gobn);
2664 bytestream_put_le16(&ptr, mba);
2665 bytestream_put_byte(&ptr, pred_x);
2666 bytestream_put_byte(&ptr, pred_y);
2668 bytestream_put_byte(&ptr, 0);
2669 bytestream_put_byte(&ptr, 0);
2745 if (CONFIG_H263_ENCODER)
2765 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2796 int current_packet_size, is_gob_start;
2802 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2814 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2819 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2839 current_packet_size=0;
2853 if (CONFIG_MPEG4_ENCODER) {
2860 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2867 if (CONFIG_H263_ENCODER)
2897 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2914 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2915 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2926 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2927 &dmin, &next_block, 0, 0);
2935 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2936 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2946 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2947 &dmin, &next_block, 0, 0);
2955 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2956 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2964 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2965 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
2975 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2976 &dmin, &next_block, 0, 0);
2987 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2988 &dmin, &next_block, 0, 0);
2999 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3000 &dmin, &next_block, 0, 0);
3006 for(dir=0; dir<2; dir++){
3013 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3014 &dmin, &next_block, 0, 0);
3022 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3023 &dmin, &next_block, 0, 0);
3034 const int last_qp= backup_s.
qscale;
3038 static const int dquant_tab[4]={-1,1,-2,2};
3047 s->
mv[0][0][0] = best_s.
mv[0][0][0];
3048 s->
mv[0][0][1] = best_s.
mv[0][0][1];
3049 s->
mv[1][0][0] = best_s.
mv[1][0][0];
3050 s->
mv[1][0][1] = best_s.
mv[1][0][1];
3053 for(; qpi<4; qpi++){
3054 int dquant= dquant_tab[qpi];
3066 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3067 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3087 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3088 &dmin, &next_block, mx, my);
3095 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3096 &dmin, &next_block, 0, 0);
3104 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3125 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3126 &dmin, &next_block, mx, my);
3153 if (CONFIG_H263_ENCODER &&
3166 int motion_x = 0, motion_y = 0;
3174 motion_x= s->
mv[0][0][0] = 0;
3175 motion_y= s->
mv[0][0][1] = 0;
3203 if (CONFIG_MPEG4_ENCODER) {
3212 if (CONFIG_MPEG4_ENCODER) {
3262 for(dir=0; dir<2; dir++){
3279 if (CONFIG_H263_ENCODER &&
3336 #define MERGE(field) dst->field += src->field; src->field=0
3363 for(i=0; i<64; i++){
3390 if (CONFIG_MPEG4_ENCODER)
3396 if (CONFIG_H263_ENCODER)
3477 for(i=1; i<context_count; i++){
3507 for(i=1; i<context_count; i++){
3520 av_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3564 for(dir=0; dir<2; dir++){
3613 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};
3614 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};
3645 if (CONFIG_MJPEG_ENCODER)
3650 if (CONFIG_H261_ENCODER)
3658 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred)
3666 else if (CONFIG_H263_ENCODER)
3670 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3679 for(i=1; i<context_count; i++){
3683 for(i=1; i<context_count; i++){
3696 for(i=0; i<64; i++){
3697 int level= block[i];
3703 if(level<0) level=0;
3707 if(level>0) level=0;
3716 int qscale,
int *overflow){
3721 unsigned int threshold1, threshold2;
3733 int coeff_count[64];
3734 int qmul, qadd, start_i, last_non_zero, i,
dc;
3745 qadd= ((qscale-1)|1)*8;
3762 block[0] = (block[0] + (q >> 1)) / q;
3780 threshold2= (threshold1<<1);
3782 for(i=63; i>=start_i; i--) {
3783 const int j = scantable[i];
3784 int level = block[j] * qmat[j];
3786 if(((
unsigned)(level+threshold1))>threshold2){
3792 for(i=start_i; i<=last_non_zero; i++) {
3793 const int j = scantable[i];
3794 int level = block[j] * qmat[j];
3798 if(((
unsigned)(level+threshold1))>threshold2){
3802 coeff[1][i]= level-1;
3806 coeff[0][i]= -
level;
3807 coeff[1][i]= -level+1;
3810 coeff_count[i]=
FFMIN(level, 2);
3814 coeff[0][i]= (level>>31)|1;
3821 if(last_non_zero < start_i){
3822 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3823 return last_non_zero;
3826 score_tab[start_i]= 0;
3827 survivor[0]= start_i;
3830 for(i=start_i; i<=last_non_zero; i++){
3831 int level_index, j, zero_distortion;
3832 int dct_coeff=
FFABS(block[ scantable[i] ]);
3833 int best_score=256*256*256*120;
3837 zero_distortion= dct_coeff*dct_coeff;
3839 for(level_index=0; level_index < coeff_count[i]; level_index++){
3841 int level= coeff[level_index][i];
3842 const int alevel=
FFABS(level);
3848 unquant_coeff= alevel*qmul + qadd;
3852 unquant_coeff = (int)( alevel * qscale * s->
intra_matrix[j]) >> 3;
3853 unquant_coeff = (unquant_coeff - 1) | 1;
3855 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[j])) >> 4;
3856 unquant_coeff = (unquant_coeff - 1) | 1;
3861 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3863 if((level&(~127)) == 0){
3864 for(j=survivor_count-1; j>=0; j--){
3865 int run= i - survivor[j];
3867 score += score_tab[i-
run];
3869 if(score < best_score){
3872 level_tab[i+1]= level-64;
3877 for(j=survivor_count-1; j>=0; j--){
3878 int run= i - survivor[j];
3880 score += score_tab[i-
run];
3881 if(score < last_score){
3884 last_level= level-64;
3890 distortion += esc_length*
lambda;
3891 for(j=survivor_count-1; j>=0; j--){
3892 int run= i - survivor[j];
3893 int score= distortion + score_tab[i-
run];
3895 if(score < best_score){
3898 level_tab[i+1]= level-64;
3903 for(j=survivor_count-1; j>=0; j--){
3904 int run= i - survivor[j];
3905 int score= distortion + score_tab[i-
run];
3906 if(score < last_score){
3909 last_level= level-64;
3917 score_tab[i+1]= best_score;
3920 if(last_non_zero <= 27){
3921 for(; survivor_count; survivor_count--){
3922 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3926 for(; survivor_count; survivor_count--){
3927 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3932 survivor[ survivor_count++ ]= i+1;
3936 last_score= 256*256*256*120;
3937 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3938 int score= score_tab[i];
3939 if(i) score += lambda*2;
3941 if(score < last_score){
3944 last_level= level_tab[i];
3945 last_run= run_tab[i];
3952 dc=
FFABS(block[0]);
3953 last_non_zero= last_i - 1;
3954 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3956 if(last_non_zero < start_i)
3957 return last_non_zero;
3959 if(last_non_zero == 0 && start_i == 0){
3961 int best_score= dc *
dc;
3963 for(i=0; i<coeff_count[0]; i++){
3964 int level= coeff[i][0];
3965 int alevel=
FFABS(level);
3966 int unquant_coeff, score, distortion;
3969 unquant_coeff= (alevel*qmul + qadd)>>3;
3971 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[0])) >> 4;
3972 unquant_coeff = (unquant_coeff - 1) | 1;
3974 unquant_coeff = (unquant_coeff + 4) >> 3;
3975 unquant_coeff<<= 3 + 3;
3977 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
3980 else score= distortion + esc_length*
lambda;
3982 if(score < best_score){
3984 best_level= level - 64;
3987 block[0]= best_level;
3989 if(best_level == 0)
return -1;
3990 else return last_non_zero;
3996 block[ perm_scantable[last_non_zero] ]= last_level;
3999 for(; i>start_i; i -= run_tab[i] + 1){
4000 block[ perm_scantable[i-1] ]= level_tab[i];
4003 return last_non_zero;
4018 int perm_index= perm[
index];
4019 if(i==0) s*= sqrt(0.5);
4020 if(j==0) s*= sqrt(0.5);
4021 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)));
4040 int qmul, qadd, start_i, last_non_zero, i,
dc;
4044 int rle_index,
run, q = 1, sum;
4047 static int after_last=0;
4048 static int to_zero=0;
4049 static int from_zero=0;
4052 static int messed_sign=0;
4055 if(basis[0][0] == 0)
4092 for(i=0; i<64; i++){
4099 for(i=0; i<64; i++){