68 #define QUANT_BIAS_SHIFT 8
70 #define QMAT_SHIFT_MMX 16
89 const uint16_t *quant_matrix,
90 int bias,
int qmin,
int qmax,
int intra)
96 for (qscale = qmin; qscale <= qmax; qscale++) {
103 for (i = 0; i < 64; i++) {
105 int64_t den = (int64_t) qscale * quant_matrix[j];
115 for (i = 0; i < 64; i++) {
117 int64_t den =
ff_aanscales[i] * (int64_t) qscale * quant_matrix[j];
127 for (i = 0; i < 64; i++) {
129 int64_t den = (int64_t) qscale * quant_matrix[j];
140 if (
qmat16[qscale][0][i] == 0 ||
141 qmat16[qscale][0][i] == 128 * 256)
149 for (i = intra; i < 64; i++) {
154 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
161 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
170 int bestdiff=INT_MAX;
173 1,2,3,4,5,6,7,8,9,10,11,12,14,16,18,20,24,26,28
178 if (non_linear_qscale[i] < s->
avctx->
qmin ||
181 if (diff < bestdiff) {
183 best = non_linear_qscale[i];
203 for (i = 0; i < 64; i++) {
218 for (i = 0; i < s->
mb_num; i++) {
229 #define COPY(a) dst->a= src->a
254 for (i = -16; i < 16; i++) {
268 if (CONFIG_H263_ENCODER)
285 int i, ret, format_supported;
294 "only YUV420 and YUV422 are supported\n");
300 format_supported = 0;
309 format_supported = 1;
315 format_supported = 1;
317 if (!format_supported) {
351 "keyframe interval too large!, reducing it from %d to %d\n",
378 "intra dc precision must be positive, note some applications use"
379 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
396 #if FF_API_MOTION_EST
450 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
456 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
473 "impossible bitrate constraints, this will fail\n");
498 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
499 "specified vbv buffer is too large for the given bitrate!\n");
511 "OBMC is only supported with simple mb decision\n");
529 "max b frames must be 0 or positive for mpegvideo based encoders\n");
539 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
547 (avctx->
width > 2048 ||
554 ((avctx->
width &3) ||
561 (avctx->
width > 4095 ||
568 (avctx->
width > 16383 ||
569 avctx->
height > 16383 )) {
570 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
605 "mpeg2 style quantization not supported by codec\n");
623 "closed gop with scene change detection are not supported yet, "
624 "set threshold to 1000000000\n");
631 "low delay forcing is only available for mpeg2\n");
636 "b frames cannot be used with low delay\n");
642 if (avctx->
qmax > 28) {
644 "non linear quant only supports qmax <= 28 currently\n");
656 "multi threaded encoding not supported by codec\n");
662 "automatic thread number detection not supported by codec, "
680 "notice: b_frame_strategy only affects the first pass\n");
703 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
707 #if FF_API_QUANT_BIAS
723 "timebase %d/%d not supported by MPEG 4 standard, "
724 "the maximum admitted value for the timebase denominator "
747 if (!CONFIG_MJPEG_ENCODER ||
754 if (!CONFIG_H261_ENCODER)
758 "The specified picture size of %dx%d is not valid for the "
759 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
769 if (!CONFIG_H263_ENCODER)
774 "The specified picture size of %dx%d is not valid for "
775 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
776 "352x288, 704x576, and 1408x1152. "
907 2 * 64 *
sizeof(uint16_t),
fail);
912 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
927 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
932 for (i = 0; i < 64; i++) {
967 #if FF_API_ERROR_RATE
974 #if FF_API_NORMALIZE_AQP
1044 if (CONFIG_MJPEG_ENCODER &&
1079 for (y = 0; y < 16; y++) {
1080 for (x = 0; x < 16; x++) {
1081 acc +=
FFABS(src[x + y * stride] - ref);
1097 for (y = 0; y <
h; y += 16) {
1098 for (x = 0; x < w; x += 16) {
1103 int sae =
get_sae(src + offset, mean, stride);
1105 acc += sae + 500 < sad;
1123 int i, display_picture_number = 0, ret;
1138 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1143 if (!s->
low_delay && display_picture_number == 1)
1152 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1155 pts = display_picture_number;
1161 if (!pic_arg->
buf[0] ||
1197 int h_chroma_shift, v_chroma_shift;
1202 for (i = 0; i < 3; i++) {
1203 int src_stride = pic_arg->
linesize[i];
1205 int h_shift = i ? h_chroma_shift : 0;
1206 int v_shift = i ? v_chroma_shift : 0;
1207 int w = s->
width >> h_shift;
1221 if (src_stride == dst_stride)
1222 memcpy(dst, src, src_stride * h);
1227 memcpy(dst2, src, w);
1263 int64_t score64 = 0;
1265 for (plane = 0; plane < 3; plane++) {
1267 const int bw = plane ? 1 : 2;
1268 for (y = 0; y < s->
mb_height * bw; y++) {
1269 for (x = 0; x < s->
mb_width * bw; x++) {
1270 int off = p->
shared ? 0 : 16;
1276 case 0: score =
FFMAX(score, v);
break;
1277 case 1: score +=
FFABS(v);
break;
1278 case 2: score64 += v * (int64_t)v;
break;
1279 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1280 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1303 int ret, got_output;
1320 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1321 int64_t best_rd = INT64_MAX;
1322 int best_b_count = -1;
1334 b_lambda = p_lambda;
1359 pre_input = *pre_input_ptr;
1360 memcpy(data, pre_input_ptr->
f->
data,
sizeof(data));
1362 if (!pre_input.
shared && i) {
1430 return best_b_count;
1503 b_frames =
FFMAX(0, i - 1);
1506 for (i = 0; i < b_frames + 1; i++) {
1518 for (i = b_frames - 1; i >= 0; i--) {
1526 "warning, too many b frames in a row\n");
1549 for (i = 0; i < b_frames; i++) {
1595 for (i = 0; i < 4; i++) {
1647 #if FF_API_CODED_FRAME
1658 for (intra = 0; intra < 2; intra++) {
1660 for (i = 0; i < 64; i++) {
1666 for (i = 0; i < 64; i++) {
1717 for (i = 0; i < 4; i++) {
1748 const AVFrame *pic_arg,
int *got_packet)
1751 int i, stuffing_count, ret;
1780 for (i = 0; i < context_count; i++) {
1797 if (growing_buffer) {
1848 for (i = 0; i < context_count; i++) {
1863 for (i = 0; i < 4; i++) {
1883 if (stuffing_count) {
1885 stuffing_count + 50) {
1893 while (stuffing_count--) {
1900 stuffing_count -= 4;
1901 while (stuffing_count--) {
1927 "Internal error, negative bits\n");
1935 vbv_delay =
FFMAX(vbv_delay, min_delay);
1975 *got_packet = !!pkt->
size;
1980 int n,
int threshold)
1982 static const char tab[64] = {
1983 3, 2, 2, 1, 1, 1, 1, 1,
1984 1, 1, 1, 1, 1, 1, 1, 1,
1985 1, 1, 1, 1, 1, 1, 1, 1,
1986 0, 0, 0, 0, 0, 0, 0, 0,
1987 0, 0, 0, 0, 0, 0, 0, 0,
1988 0, 0, 0, 0, 0, 0, 0, 0,
1989 0, 0, 0, 0, 0, 0, 0, 0,
1990 0, 0, 0, 0, 0, 0, 0, 0
1999 if (threshold < 0) {
2001 threshold = -threshold;
2006 if (last_index <= skip_dc - 1)
2009 for (i = 0; i <= last_index; i++) {
2013 if (skip_dc && i == 0)
2017 }
else if (level > 1) {
2023 if (score >= threshold)
2025 for (i = skip_dc; i <= last_index; i++) {
2048 for (; i <= last_index; i++) {
2050 int level = block[j];
2052 if (level > maxlevel) {
2055 }
else if (level < minlevel) {
2065 "warning, clipping %d dct coefficients to %d..%d\n",
2066 overflow, minlevel, maxlevel);
2073 for (y = 0; y < 8; y++) {
2074 for (x = 0; x < 8; x++) {
2080 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2081 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2082 int v = ptr[x2 + y2 *
stride];
2088 weight[x + 8 *
y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2094 int motion_x,
int motion_y,
2095 int mb_block_height,
2100 int16_t orig[12][64];
2107 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2108 ptrdiff_t wrap_y, wrap_c;
2110 for (i = 0; i < mb_block_count; i++)
2114 const int last_qp = s->
qscale;
2115 const int mb_xy = mb_x + mb_y * s->
mb_stride;
2146 (mb_y * 16 * wrap_y) + mb_x * 16;
2148 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2150 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2158 16, 16, mb_x * 16, mb_y * 16,
2163 mb_block_width, mb_block_height,
2164 mb_x * mb_block_width, mb_y * mb_block_height,
2166 ptr_cb = ebuf + 16 * wrap_y;
2169 mb_block_width, mb_block_height,
2170 mb_x * mb_block_width, mb_y * mb_block_height,
2172 ptr_cr = ebuf + 16 * wrap_y + 16;
2177 int progressive_score, interlaced_score;
2182 NULL, wrap_y, 8) - 400;
2184 if (progressive_score > 0) {
2186 NULL, wrap_y * 2, 8) +
2188 NULL, wrap_y * 2, 8);
2189 if (progressive_score > interlaced_score) {
2192 dct_offset = wrap_y;
2193 uv_dct_offset = wrap_c;
2228 uint8_t *dest_y, *dest_cb, *dest_cr;
2230 dest_y = s->
dest[0];
2231 dest_cb = s->
dest[1];
2232 dest_cr = s->
dest[2];
2256 int progressive_score, interlaced_score;
2259 progressive_score = s->
mecc.
ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2265 progressive_score -= 400;
2267 if (progressive_score > 0) {
2274 if (progressive_score > interlaced_score) {
2277 dct_offset = wrap_y;
2278 uv_dct_offset = wrap_c;
2289 dest_y + dct_offset, wrap_y);
2291 dest_y + dct_offset + 8, wrap_y);
2301 dest_cb + uv_dct_offset, wrap_c);
2303 dest_cr + uv_dct_offset, wrap_c);
2314 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset, dest_y + dct_offset,
2315 wrap_y, 8) < 20 * s->
qscale)
2317 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2318 wrap_y, 8) < 20 * s->
qscale)
2326 dest_cb + uv_dct_offset,
2327 wrap_c, 8) < 20 * s->
qscale)
2330 dest_cr + uv_dct_offset,
2331 wrap_c, 8) < 20 * s->
qscale)
2358 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2364 for (i = 0; i < mb_block_count; i++) {
2379 for (i = 0; i < mb_block_count; i++) {
2389 for (i = 0; i < 4; i++)
2392 for (i = 4; i < mb_block_count; i++)
2396 for (i = 0; i < mb_block_count; i++) {
2409 for (i=6; i<12; i++) {
2418 for (i = 0; i < mb_block_count; i++) {
2421 for (j = 63; j > 0; j--) {
2434 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2438 if (CONFIG_MPEG4_ENCODER)
2448 if (CONFIG_WMV2_ENCODER)
2452 if (CONFIG_H261_ENCODER)
2460 if (CONFIG_H263_ENCODER)
2465 if (CONFIG_MJPEG_ENCODER)
2511 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2549 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2557 s->
pb= pb[*next_block];
2559 s->
pb2 = pb2 [*next_block];
2560 s->
tex_pb= tex_pb[*next_block];
2564 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2587 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2605 else if(w==8 && h==8)
2695 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2703 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2738 if (CONFIG_H263_ENCODER)
2740 bytestream_put_le32(&ptr, offset);
2741 bytestream_put_byte(&ptr, s->
qscale);
2742 bytestream_put_byte(&ptr, gobn);
2743 bytestream_put_le16(&ptr, mba);
2744 bytestream_put_byte(&ptr, pred_x);
2745 bytestream_put_byte(&ptr, pred_y);
2747 bytestream_put_byte(&ptr, 0);
2748 bytestream_put_byte(&ptr, 0);
2783 int new_buffer_size = 0;
2853 if (CONFIG_H263_ENCODER)
2873 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2907 int current_packet_size, is_gob_start;
2913 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2922 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2927 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2947 current_packet_size=0;
2961 if (CONFIG_MPEG4_ENCODER) {
2968 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2975 if (CONFIG_H263_ENCODER)
3005 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3012 backup_s.pb2= s->
pb2;
3013 backup_s.tex_pb= s->
tex_pb;
3022 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
3023 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3034 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
3035 &dmin, &next_block, 0, 0);
3043 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3044 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3054 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3055 &dmin, &next_block, 0, 0);
3063 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3064 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3072 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3073 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
3083 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3084 &dmin, &next_block, 0, 0);
3095 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3096 &dmin, &next_block, 0, 0);
3107 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3108 &dmin, &next_block, 0, 0);
3114 for(dir=0; dir<2; dir++){
3121 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3122 &dmin, &next_block, 0, 0);
3130 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3131 &dmin, &next_block, 0, 0);
3142 const int last_qp= backup_s.qscale;
3146 static const int dquant_tab[4]={-1,1,-2,2};
3155 s->
mv[0][0][0] = best_s.
mv[0][0][0];
3156 s->
mv[0][0][1] = best_s.
mv[0][0][1];
3157 s->
mv[1][0][0] = best_s.
mv[1][0][0];
3158 s->
mv[1][0][1] = best_s.
mv[1][0][1];
3161 for(; qpi<4; qpi++){
3162 int dquant= dquant_tab[qpi];
3174 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3175 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3191 backup_s.dquant = 0;
3195 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3196 &dmin, &next_block, mx, my);
3199 backup_s.dquant = 0;
3203 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3204 &dmin, &next_block, 0, 0);
3212 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3233 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3234 &dmin, &next_block, mx, my);
3252 s->
pb2= backup_s.pb2;
3256 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3257 s->
tex_pb= backup_s.tex_pb;
3261 if (CONFIG_H263_ENCODER &&
3274 int motion_x = 0, motion_y = 0;
3282 motion_x= s->
mv[0][0][0] = 0;
3283 motion_y= s->
mv[0][0][1] = 0;
3311 if (CONFIG_MPEG4_ENCODER) {
3320 if (CONFIG_MPEG4_ENCODER) {
3370 for(dir=0; dir<2; dir++){
3387 if (CONFIG_H263_ENCODER &&
3444 #define MERGE(field) dst->field += src->field; src->field=0
3471 for(i=0; i<64; i++){
3498 if (CONFIG_MPEG4_ENCODER)
3504 if (CONFIG_H263_ENCODER)
3585 for(i=1; i<context_count; i++){
3615 for(i=1; i<context_count; i++){
3628 ff_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3672 for(dir=0; dir<2; dir++){
3723 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};
3724 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};
3755 if (CONFIG_MJPEG_ENCODER)
3760 if (CONFIG_H261_ENCODER)
3768 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred)
3779 else if (CONFIG_H263_ENCODER)
3783 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3792 for(i=1; i<context_count; i++){
3796 for(i=1; i<context_count; i++){
3811 for(i=0; i<64; i++){
3812 int level= block[i];
3818 if(level<0) level=0;
3822 if(level>0) level=0;
3831 int qscale,
int *overflow){
3833 const uint16_t *matrix;
3837 unsigned int threshold1, threshold2;
3849 int coeff_count[64];
3850 int qmul, qadd, start_i, last_non_zero, i,
dc;
3861 qadd= ((qscale-1)|1)*8;
3878 block[0] = (block[0] + (q >> 1)) / q;
3904 threshold2= (threshold1<<1);
3906 for(i=63; i>=start_i; i--) {
3907 const int j = scantable[i];
3908 int level = block[j] * qmat[j];
3910 if(((
unsigned)(level+threshold1))>threshold2){
3916 for(i=start_i; i<=last_non_zero; i++) {
3917 const int j = scantable[i];
3918 int level = block[j] * qmat[j];
3922 if(((
unsigned)(level+threshold1))>threshold2){
3926 coeff[1][i]= level-1;
3930 coeff[0][i]= -
level;
3931 coeff[1][i]= -level+1;
3934 coeff_count[i]=
FFMIN(level, 2);
3938 coeff[0][i]= (level>>31)|1;
3945 if(last_non_zero < start_i){
3946 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3947 return last_non_zero;
3950 score_tab[start_i]= 0;
3951 survivor[0]= start_i;
3954 for(i=start_i; i<=last_non_zero; i++){
3955 int level_index, j, zero_distortion;
3956 int dct_coeff=
FFABS(block[ scantable[i] ]);
3957 int best_score=256*256*256*120;
3961 zero_distortion= dct_coeff*dct_coeff;
3963 for(level_index=0; level_index < coeff_count[i]; level_index++){
3965 int level= coeff[level_index][i];
3966 const int alevel=
FFABS(level);
3972 unquant_coeff= alevel*qmul + qadd;
3975 unquant_coeff = alevel * matrix[j] * 8;
3979 unquant_coeff = (int)( alevel * qscale * matrix[j]) >> 3;
3980 unquant_coeff = (unquant_coeff - 1) | 1;
3982 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) matrix[j])) >> 4;
3983 unquant_coeff = (unquant_coeff - 1) | 1;
3988 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3990 if((level&(~127)) == 0){
3991 for(j=survivor_count-1; j>=0; j--){
3992 int run= i - survivor[j];
3994 score += score_tab[i-
run];
3996 if(score < best_score){
3999 level_tab[i+1]= level-64;
4004 for(j=survivor_count-1; j>=0; j--){
4005 int run= i - survivor[j];
4007 score += score_tab[i-
run];
4008 if(score < last_score){
4011 last_level= level-64;
4017 distortion += esc_length*
lambda;
4018 for(j=survivor_count-1; j>=0; j--){
4019 int run= i - survivor[j];
4020 int score= distortion + score_tab[i-
run];
4022 if(score < best_score){
4025 level_tab[i+1]= level-64;
4030 for(j=survivor_count-1; j>=0; j--){
4031 int run= i - survivor[j];
4032 int score= distortion + score_tab[i-
run];
4033 if(score < last_score){
4036 last_level= level-64;
4044 score_tab[i+1]= best_score;
4047 if(last_non_zero <= 27){
4048 for(; survivor_count; survivor_count--){
4049 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4053 for(; survivor_count; survivor_count--){
4054 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4059 survivor[ survivor_count++ ]= i+1;
4063 last_score= 256*256*256*120;
4064 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4065 int score= score_tab[i];
4066 if(i) score += lambda*2;
4068 if(score < last_score){
4071 last_level= level_tab[i];
4072 last_run= run_tab[i];
4079 dc=
FFABS(block[0]);
4080 last_non_zero= last_i - 1;
4081 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4083 if(last_non_zero < start_i)
4084 return last_non_zero;
4086 if(last_non_zero == 0 && start_i == 0){
4088 int best_score= dc *
dc;
4090 for(i=0; i<coeff_count[0]; i++){
4091 int level= coeff[i][0];
4092 int alevel=
FFABS(level);
4093 int unquant_coeff, score, distortion;
4096 unquant_coeff= (alevel*qmul + qadd)>>3;
4098 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) matrix[0])) >> 4;
4099 unquant_coeff = (unquant_coeff - 1) | 1;
4101 unquant_coeff = (unquant_coeff + 4) >> 3;
4102 unquant_coeff<<= 3 + 3;
4104 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
4107 else score= distortion + esc_length*
lambda;
4109 if(score < best_score){
4111 best_level= level - 64;
4114 block[0]= best_level;
4116 if(best_level == 0)
return -1;
4117 else return last_non_zero;
4123 block[ perm_scantable[last_non_zero] ]= last_level;
4126 for(; i>start_i; i -= run_tab[i] + 1){
4127 block[ perm_scantable[i-1] ]= level_tab[i];
4130 return last_non_zero;
4145 int perm_index= perm[
index];
4146 if(i==0) s*= sqrt(0.5);
4147 if(j==0) s*= sqrt(0.5);
4148 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)));
4167 int qmul, qadd, start_i, last_non_zero, i,
dc;
4171 int rle_index,
run, q = 1, sum;
4174 static int after_last=0;
4175 static int to_zero=0;
4176 static int from_zero=0;
4179 static int messed_sign=0;
4182 if(basis[0][0] == 0)
4224 for(i=0; i<64; i++){
4231 for(i=0; i<64; i++){
4236 w=
FFABS(weight[i]) + qns*one;
4237 w= 15 + (48*qns*one + w/2)/w;
4252 for(i=start_i; i<=last_non_zero; i++){
4253 int j= perm_scantable[i];
4254 const int level= block[j];
4258 if(level<0) coeff= qmul*level - qadd;
4259 else coeff= qmul*level + qadd;
4260 run_tab[rle_index++]=
run;
4269 if(last_non_zero>0){
4280 int run2, best_unquant_change=0, analyze_gradient;
4286 if(analyze_gradient){
4290 for(i=0; i<64; i++){
4306 const int level= block[0];
4307 int change, old_coeff;
4313 for(change=-1; change<=1; change+=2){
4314 int new_level= level + change;
4315 int score, new_coeff;
4317 new_coeff= q*new_level;
4318 if(new_coeff >= 2048 || new_coeff < 0)
4322 new_coeff - old_coeff);
4323 if(score<best_score){
4326 best_change= change;
4327 best_unquant_change= new_coeff - old_coeff;
4334 run2= run_tab[rle_index++];
4338 for(i=start_i; i<64; i++){
4339 int j= perm_scantable[i];
4340 const int level= block[j];
4341 int change, old_coeff;
4347 if(level<0) old_coeff= qmul*level - qadd;
4348 else old_coeff= qmul*level + qadd;
4349 run2= run_tab[rle_index++];
4356 for(change=-1; change<=1; change+=2){
4357 int new_level= level + change;
4358 int score, new_coeff, unquant_change;
4365 if(new_level<0) new_coeff= qmul*new_level - qadd;
4366 else new_coeff= qmul*new_level + qadd;
4367 if(new_coeff >= 2048 || new_coeff <= -2048)
4372 if(level < 63 && level > -63){
4373 if(i < last_non_zero)
4383 if(analyze_gradient){
4384 int g= d1[ scantable[i] ];
4385 if(g && (g^new_level) >= 0)
4389 if(i < last_non_zero){
4390 int next_i= i + run2 + 1;
4391 int next_level= block[ perm_scantable[next_i] ] + 64;
4393 if(next_level&(~127))
4396 if(next_i < last_non_zero)
4416 if(i < last_non_zero){
4417 int next_i= i + run2 + 1;
4418 int next_level= block[ perm_scantable[next_i] ] + 64;
4420 if(next_level&(~127))
4423 if(next_i < last_non_zero)
4442 unquant_change= new_coeff - old_coeff;
4443 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4447 if(score<best_score){
4450 best_change= change;
4451 best_unquant_change= unquant_change;
4455 prev_level= level + 64;
4456 if(prev_level&(~127))
4469 int j= perm_scantable[ best_coeff ];
4471 block[j] += best_change;
4473 if(best_coeff > last_non_zero){
4474 last_non_zero= best_coeff;
4482 if(block[j] - best_change){
4483 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
4495 for(; last_non_zero>=start_i; last_non_zero--){
4496 if(block[perm_scantable[last_non_zero]])
4502 if(256*256*256*64 % count == 0){
4503 av_log(s->
avctx,
AV_LOG_DEBUG,
"after_last:%d to_zero:%d from_zero:%d raise:%d lower:%d sign:%d xyp:%d/%d/%d\n", after_last, to_zero, from_zero,
raise, lower, messed_sign, s->
mb_x, s->
mb_y, s->
picture_number);
4508 for(i=start_i; i<=last_non_zero; i++){
4509 int j= perm_scantable[i];
4510 const int level= block[j];
4513 run_tab[rle_index++]=
run;
4526 if(last_non_zero>0){
4532 return last_non_zero;
4547 const uint8_t *scantable,
int last)
4558 for (i = 0; i <= last; i++) {
4559 const int j = scantable[i];
4564 for (i = 0; i <= last; i++) {
4565 const int j = scantable[i];
4566 const int perm_j = permutation[j];
4567 block[perm_j] = temp[j];
4573 int qscale,
int *overflow)
4575 int i, j,
level, last_non_zero, q, start_i;