64 #define QUANT_BIAS_SHIFT 8
66 #define QMAT_SHIFT_MMX 16
85 const uint16_t *quant_matrix,
86 int bias,
int qmin,
int qmax,
int intra)
92 for (qscale = qmin; qscale <= qmax; qscale++) {
99 for (i = 0; i < 64; i++) {
101 int64_t den = (int64_t) qscale * quant_matrix[j];
111 for (i = 0; i < 64; i++) {
113 int64_t den =
ff_aanscales[i] * (int64_t) qscale * quant_matrix[j];
123 for (i = 0; i < 64; i++) {
125 int64_t den = (int64_t) qscale * quant_matrix[j];
136 if (
qmat16[qscale][0][i] == 0 ||
137 qmat16[qscale][0][i] == 128 * 256)
145 for (i = intra; i < 64; i++) {
150 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
157 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
178 for (i = 0; i < 64; i++) {
193 for (i = 0; i < s->
mb_num; i++) {
204 #define COPY(a) dst->a= src->a
229 for (i = -16; i < 16; i++) {
243 if (CONFIG_H263_ENCODER)
260 int i,
ret, format_supported;
269 "only YUV420 and YUV422 are supported\n");
275 format_supported = 0;
284 format_supported = 1;
290 format_supported = 1;
292 if (!format_supported) {
326 "keyframe interval too large!, reducing it from %d to %d\n",
353 "intra dc precision must be positive, note some applications use"
354 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
421 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
427 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
444 "impossible bitrate constraints, this will fail\n");
469 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
470 "specified vbv buffer is too large for the given bitrate!\n");
482 "OBMC is only supported with simple mb decision\n");
500 "max b frames must be 0 or positive for mpegvideo based encoders\n");
510 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
518 (avctx->
width > 2048 ||
525 ((avctx->
width &3) ||
532 (avctx->
width > 4095 ||
539 (avctx->
width > 16383 ||
540 avctx->
height > 16383 )) {
541 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
576 "mpeg2 style quantization not supported by codec\n");
594 "closed gop with scene change detection are not supported yet, "
595 "set threshold to 1000000000\n");
602 "low delay forcing is only available for mpeg2\n");
607 "b frames cannot be used with low delay\n");
613 if (avctx->
qmax > 12) {
615 "non linear quant only supports qmax <= 12 currently\n");
627 "multi threaded encoding not supported by codec\n");
633 "automatic thread number detection not supported by codec, "
651 "notice: b_frame_strategy only affects the first pass\n");
674 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
688 "timebase %d/%d not supported by MPEG 4 standard, "
689 "the maximum admitted value for the timebase denominator "
712 if (!CONFIG_MJPEG_ENCODER ||
719 if (!CONFIG_H261_ENCODER)
723 "The specified picture size of %dx%d is not valid for the "
724 "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)
894 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
899 for (i = 0; i < 64; i++) {
934 #if FF_API_ERROR_RATE
941 #if FF_API_NORMALIZE_AQP
1011 if (CONFIG_MJPEG_ENCODER &&
1046 for (y = 0; y < 16; y++) {
1047 for (x = 0; x < 16; x++) {
1048 acc +=
FFABS(src[x + y * stride] - ref);
1064 for (y = 0; y <
h; y += 16) {
1065 for (x = 0; x < w; x += 16) {
1070 int sae =
get_sae(src + offset, mean, stride);
1072 acc += sae + 500 < sad;
1090 int i, display_picture_number = 0,
ret;
1105 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1110 if (!s->
low_delay && display_picture_number == 1)
1119 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1122 pts = display_picture_number;
1128 if (!pic_arg->
buf[0] ||
1164 int h_chroma_shift, v_chroma_shift;
1169 for (i = 0; i < 3; i++) {
1170 int src_stride = pic_arg->
linesize[i];
1172 int h_shift = i ? h_chroma_shift : 0;
1173 int v_shift = i ? v_chroma_shift : 0;
1174 int w = s->
width >> h_shift;
1188 if (src_stride == dst_stride)
1189 memcpy(dst, src, src_stride * h);
1194 memcpy(dst2, src, w);
1230 int64_t score64 = 0;
1232 for (plane = 0; plane < 3; plane++) {
1234 const int bw = plane ? 1 : 2;
1235 for (y = 0; y < s->
mb_height * bw; y++) {
1236 for (x = 0; x < s->
mb_width * bw; x++) {
1237 int off = p->
shared ? 0 : 16;
1243 case 0: score =
FFMAX(score, v);
break;
1244 case 1: score +=
FFABS(v);
break;
1245 case 2: score64 += v * (int64_t)v;
break;
1246 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1247 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1270 int ret, got_output;
1287 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1288 int64_t best_rd = INT64_MAX;
1289 int best_b_count = -1;
1301 b_lambda = p_lambda;
1326 pre_input = *pre_input_ptr;
1327 memcpy(data, pre_input_ptr->
f->
data,
sizeof(data));
1329 if (!pre_input.
shared && i) {
1397 return best_b_count;
1470 b_frames =
FFMAX(0, i - 1);
1473 for (i = 0; i < b_frames + 1; i++) {
1485 for (i = b_frames - 1; i >= 0; i--) {
1493 "warning, too many b frames in a row\n");
1516 for (i = 0; i < b_frames; i++) {
1562 for (i = 0; i < 4; i++) {
1622 for (intra = 0; intra < 2; intra++) {
1624 for (i = 0; i < 64; i++) {
1630 for (i = 0; i < 64; i++) {
1681 for (i = 0; i < 4; i++) {
1712 const AVFrame *pic_arg,
int *got_packet)
1715 int i, stuffing_count,
ret;
1742 for (i = 0; i < context_count; i++) {
1759 if (growing_buffer) {
1810 for (i = 0; i < context_count; i++) {
1824 for (i = 0; i < 4; i++) {
1840 if (stuffing_count) {
1842 stuffing_count + 50) {
1850 while (stuffing_count--) {
1857 stuffing_count -= 4;
1858 while (stuffing_count--) {
1884 "Internal error, negative bits\n");
1892 vbv_delay =
FFMAX(vbv_delay, min_delay);
1932 *got_packet = !!pkt->
size;
1937 int n,
int threshold)
1939 static const char tab[64] = {
1940 3, 2, 2, 1, 1, 1, 1, 1,
1941 1, 1, 1, 1, 1, 1, 1, 1,
1942 1, 1, 1, 1, 1, 1, 1, 1,
1943 0, 0, 0, 0, 0, 0, 0, 0,
1944 0, 0, 0, 0, 0, 0, 0, 0,
1945 0, 0, 0, 0, 0, 0, 0, 0,
1946 0, 0, 0, 0, 0, 0, 0, 0,
1947 0, 0, 0, 0, 0, 0, 0, 0
1956 if (threshold < 0) {
1958 threshold = -threshold;
1963 if (last_index <= skip_dc - 1)
1966 for (i = 0; i <= last_index; i++) {
1970 if (skip_dc && i == 0)
1974 }
else if (level > 1) {
1980 if (score >= threshold)
1982 for (i = skip_dc; i <= last_index; i++) {
2005 for (; i <= last_index; i++) {
2007 int level = block[j];
2009 if (level > maxlevel) {
2012 }
else if (level < minlevel) {
2022 "warning, clipping %d dct coefficients to %d..%d\n",
2023 overflow, minlevel, maxlevel);
2030 for (y = 0; y < 8; y++) {
2031 for (x = 0; x < 8; x++) {
2037 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2038 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2039 int v = ptr[x2 + y2 *
stride];
2045 weight[x + 8 *
y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2051 int motion_x,
int motion_y,
2052 int mb_block_height,
2057 int16_t orig[12][64];
2064 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2065 ptrdiff_t wrap_y, wrap_c;
2067 for (i = 0; i < mb_block_count; i++)
2071 const int last_qp = s->
qscale;
2072 const int mb_xy = mb_x + mb_y * s->
mb_stride;
2103 (mb_y * 16 * wrap_y) + mb_x * 16;
2105 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2107 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2115 16, 16, mb_x * 16, mb_y * 16,
2120 mb_block_width, mb_block_height,
2121 mb_x * mb_block_width, mb_y * mb_block_height,
2123 ptr_cb = ebuf + 16 * wrap_y;
2126 mb_block_width, mb_block_height,
2127 mb_x * mb_block_width, mb_y * mb_block_height,
2129 ptr_cr = ebuf + 16 * wrap_y + 16;
2134 int progressive_score, interlaced_score;
2139 NULL, wrap_y, 8) - 400;
2141 if (progressive_score > 0) {
2143 NULL, wrap_y * 2, 8) +
2145 NULL, wrap_y * 2, 8);
2146 if (progressive_score > interlaced_score) {
2149 dct_offset = wrap_y;
2150 uv_dct_offset = wrap_c;
2185 uint8_t *dest_y, *dest_cb, *dest_cr;
2187 dest_y = s->
dest[0];
2188 dest_cb = s->
dest[1];
2189 dest_cr = s->
dest[2];
2213 int progressive_score, interlaced_score;
2216 progressive_score = s->
mecc.
ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2222 progressive_score -= 400;
2224 if (progressive_score > 0) {
2231 if (progressive_score > interlaced_score) {
2234 dct_offset = wrap_y;
2235 uv_dct_offset = wrap_c;
2246 dest_y + dct_offset, wrap_y);
2248 dest_y + dct_offset + 8, wrap_y);
2258 dest_cb + uv_dct_offset, wrap_c);
2260 dest_cr + uv_dct_offset, wrap_c);
2271 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset, dest_y + dct_offset,
2272 wrap_y, 8) < 20 * s->
qscale)
2274 if (s->
mecc.
sad[1](
NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2275 wrap_y, 8) < 20 * s->
qscale)
2283 dest_cb + uv_dct_offset,
2284 wrap_c, 8) < 20 * s->
qscale)
2287 dest_cr + uv_dct_offset,
2288 wrap_c, 8) < 20 * s->
qscale)
2315 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2321 for (i = 0; i < mb_block_count; i++) {
2336 for (i = 0; i < mb_block_count; i++) {
2346 for (i = 0; i < 4; i++)
2349 for (i = 4; i < mb_block_count; i++)
2353 for (i = 0; i < mb_block_count; i++) {
2366 for (i=6; i<12; i++) {
2375 for (i = 0; i < mb_block_count; i++) {
2378 for (j = 63; j > 0; j--) {
2391 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2395 if (CONFIG_MPEG4_ENCODER)
2405 if (CONFIG_WMV2_ENCODER)
2409 if (CONFIG_H261_ENCODER)
2417 if (CONFIG_H263_ENCODER)
2422 if (CONFIG_MJPEG_ENCODER)
2468 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2506 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2514 s->
pb= pb[*next_block];
2516 s->
pb2 = pb2 [*next_block];
2517 s->
tex_pb= tex_pb[*next_block];
2521 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2544 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2562 else if(w==8 && h==8)
2652 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2660 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2695 if (CONFIG_H263_ENCODER)
2697 bytestream_put_le32(&ptr, offset);
2698 bytestream_put_byte(&ptr, s->
qscale);
2699 bytestream_put_byte(&ptr, gobn);
2700 bytestream_put_le16(&ptr, mba);
2701 bytestream_put_byte(&ptr, pred_x);
2702 bytestream_put_byte(&ptr, pred_y);
2704 bytestream_put_byte(&ptr, 0);
2705 bytestream_put_byte(&ptr, 0);
2740 int new_buffer_size = 0;
2810 if (CONFIG_H263_ENCODER)
2830 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2864 int current_packet_size, is_gob_start;
2870 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2879 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2884 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2904 current_packet_size=0;
2918 if (CONFIG_MPEG4_ENCODER) {
2925 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2932 if (CONFIG_H263_ENCODER)
2962 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2969 backup_s.pb2= s->
pb2;
2970 backup_s.tex_pb= s->
tex_pb;
2979 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2980 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2991 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2992 &dmin, &next_block, 0, 0);
3000 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3001 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3011 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3012 &dmin, &next_block, 0, 0);
3020 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3021 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
3029 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3030 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
3040 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3041 &dmin, &next_block, 0, 0);
3052 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3053 &dmin, &next_block, 0, 0);
3064 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3065 &dmin, &next_block, 0, 0);
3071 for(dir=0; dir<2; dir++){
3078 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3079 &dmin, &next_block, 0, 0);
3087 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3088 &dmin, &next_block, 0, 0);
3099 const int last_qp= backup_s.qscale;
3103 static const int dquant_tab[4]={-1,1,-2,2};
3112 s->
mv[0][0][0] = best_s.
mv[0][0][0];
3113 s->
mv[0][0][1] = best_s.
mv[0][0][1];
3114 s->
mv[1][0][0] = best_s.
mv[1][0][0];
3115 s->
mv[1][0][1] = best_s.
mv[1][0][1];
3118 for(; qpi<4; qpi++){
3119 int dquant= dquant_tab[qpi];
3131 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3132 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
3148 backup_s.dquant = 0;
3152 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3153 &dmin, &next_block, mx, my);
3156 backup_s.dquant = 0;
3160 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3161 &dmin, &next_block, 0, 0);
3169 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
3190 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
3191 &dmin, &next_block, mx, my);
3209 s->
pb2= backup_s.pb2;
3213 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3214 s->
tex_pb= backup_s.tex_pb;
3218 if (CONFIG_H263_ENCODER &&
3231 int motion_x = 0, motion_y = 0;
3239 motion_x= s->
mv[0][0][0] = 0;
3240 motion_y= s->
mv[0][0][1] = 0;
3268 if (CONFIG_MPEG4_ENCODER) {
3277 if (CONFIG_MPEG4_ENCODER) {
3327 for(dir=0; dir<2; dir++){
3344 if (CONFIG_H263_ENCODER &&
3401 #define MERGE(field) dst->field += src->field; src->field=0
3428 for(i=0; i<64; i++){
3455 if (CONFIG_MPEG4_ENCODER)
3461 if (CONFIG_H263_ENCODER)
3542 for(i=1; i<context_count; i++){
3572 for(i=1; i<context_count; i++){
3585 ff_dlog(s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3629 for(dir=0; dir<2; dir++){
3680 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};
3681 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};
3712 if (CONFIG_MJPEG_ENCODER)
3717 if (CONFIG_H261_ENCODER)
3725 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred)
3736 else if (CONFIG_H263_ENCODER)
3740 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3749 for(i=1; i<context_count; i++){
3753 for(i=1; i<context_count; i++){
3768 for(i=0; i<64; i++){
3769 int level= block[i];
3775 if(level<0) level=0;
3779 if(level>0) level=0;
3788 int qscale,
int *overflow){
3790 const uint16_t *matrix;
3794 unsigned int threshold1, threshold2;
3806 int coeff_count[64];
3807 int qmul, qadd, start_i, last_non_zero, i,
dc;
3818 qadd= ((qscale-1)|1)*8;
3835 block[0] = (block[0] + (q >> 1)) / q;
3861 threshold2= (threshold1<<1);
3863 for(i=63; i>=start_i; i--) {
3864 const int j = scantable[i];
3865 int level = block[j] * qmat[j];
3867 if(((
unsigned)(level+threshold1))>threshold2){
3873 for(i=start_i; i<=last_non_zero; i++) {
3874 const int j = scantable[i];
3875 int level = block[j] * qmat[j];
3879 if(((
unsigned)(level+threshold1))>threshold2){
3883 coeff[1][i]= level-1;
3887 coeff[0][i]= -
level;
3888 coeff[1][i]= -level+1;
3891 coeff_count[i]=
FFMIN(level, 2);
3895 coeff[0][i]= (level>>31)|1;
3902 if(last_non_zero < start_i){
3903 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3904 return last_non_zero;
3907 score_tab[start_i]= 0;
3908 survivor[0]= start_i;
3911 for(i=start_i; i<=last_non_zero; i++){
3912 int level_index, j, zero_distortion;
3913 int dct_coeff=
FFABS(block[ scantable[i] ]);
3914 int best_score=256*256*256*120;
3918 zero_distortion= dct_coeff*dct_coeff;
3920 for(level_index=0; level_index < coeff_count[i]; level_index++){
3922 int level= coeff[level_index][i];
3923 const int alevel=
FFABS(level);
3929 unquant_coeff= alevel*qmul + qadd;
3932 unquant_coeff = alevel * matrix[j] * 8;
3936 unquant_coeff = (int)( alevel * qscale * matrix[j]) >> 3;
3937 unquant_coeff = (unquant_coeff - 1) | 1;
3939 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) matrix[j])) >> 4;
3940 unquant_coeff = (unquant_coeff - 1) | 1;
3945 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3947 if((level&(~127)) == 0){
3948 for(j=survivor_count-1; j>=0; j--){
3949 int run= i - survivor[j];
3951 score += score_tab[i-
run];
3953 if(score < best_score){
3956 level_tab[i+1]= level-64;
3961 for(j=survivor_count-1; j>=0; j--){
3962 int run= i - survivor[j];
3964 score += score_tab[i-
run];
3965 if(score < last_score){
3968 last_level= level-64;
3974 distortion += esc_length*
lambda;
3975 for(j=survivor_count-1; j>=0; j--){
3976 int run= i - survivor[j];
3977 int score= distortion + score_tab[i-
run];
3979 if(score < best_score){
3982 level_tab[i+1]= level-64;
3987 for(j=survivor_count-1; j>=0; j--){
3988 int run= i - survivor[j];
3989 int score= distortion + score_tab[i-
run];
3990 if(score < last_score){
3993 last_level= level-64;
4001 score_tab[i+1]= best_score;
4004 if(last_non_zero <= 27){
4005 for(; survivor_count; survivor_count--){
4006 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4010 for(; survivor_count; survivor_count--){
4011 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4016 survivor[ survivor_count++ ]= i+1;
4020 last_score= 256*256*256*120;
4021 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4022 int score= score_tab[i];
4023 if(i) score += lambda*2;
4025 if(score < last_score){
4028 last_level= level_tab[i];
4029 last_run= run_tab[i];
4036 dc=
FFABS(block[0]);
4037 last_non_zero= last_i - 1;
4038 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4040 if(last_non_zero < start_i)
4041 return last_non_zero;
4043 if(last_non_zero == 0 && start_i == 0){
4045 int best_score= dc *
dc;
4047 for(i=0; i<coeff_count[0]; i++){
4048 int level= coeff[i][0];
4049 int alevel=
FFABS(level);
4050 int unquant_coeff, score, distortion;
4053 unquant_coeff= (alevel*qmul + qadd)>>3;
4055 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) matrix[0])) >> 4;
4056 unquant_coeff = (unquant_coeff - 1) | 1;
4058 unquant_coeff = (unquant_coeff + 4) >> 3;
4059 unquant_coeff<<= 3 + 3;
4061 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
4064 else score= distortion + esc_length*
lambda;
4066 if(score < best_score){
4068 best_level= level - 64;
4071 block[0]= best_level;
4073 if(best_level == 0)
return -1;
4074 else return last_non_zero;
4080 block[ perm_scantable[last_non_zero] ]= last_level;
4083 for(; i>start_i; i -= run_tab[i] + 1){
4084 block[ perm_scantable[i-1] ]= level_tab[i];
4087 return last_non_zero;
4102 int perm_index= perm[
index];
4103 if(i==0) s*= sqrt(0.5);
4104 if(j==0) s*= sqrt(0.5);
4105 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)));
4124 int qmul, qadd, start_i, last_non_zero, i,
dc;
4128 int rle_index,
run, q = 1, sum;
4131 static int after_last=0;
4132 static int to_zero=0;
4133 static int from_zero=0;
4136 static int messed_sign=0;
4139 if(basis[0][0] == 0)
4181 for(i=0; i<64; i++){
4188 for(i=0; i<64; i++){
4193 w=
FFABS(weight[i]) + qns*one;
4194 w= 15 + (48*qns*one + w/2)/w;
4209 for(i=start_i; i<=last_non_zero; i++){
4210 int j= perm_scantable[i];
4211 const int level= block[j];
4215 if(level<0) coeff= qmul*level - qadd;
4216 else coeff= qmul*level + qadd;
4217 run_tab[rle_index++]=
run;
4226 if(last_non_zero>0){
4237 int run2, best_unquant_change=0, analyze_gradient;
4243 if(analyze_gradient){
4247 for(i=0; i<64; i++){
4263 const int level= block[0];
4264 int change, old_coeff;
4270 for(change=-1; change<=1; change+=2){
4271 int new_level= level + change;
4272 int score, new_coeff;
4274 new_coeff= q*new_level;
4275 if(new_coeff >= 2048 || new_coeff < 0)
4279 new_coeff - old_coeff);
4280 if(score<best_score){
4283 best_change= change;
4284 best_unquant_change= new_coeff - old_coeff;
4291 run2= run_tab[rle_index++];
4295 for(i=start_i; i<64; i++){
4296 int j= perm_scantable[i];
4297 const int level= block[j];
4298 int change, old_coeff;
4304 if(level<0) old_coeff= qmul*level - qadd;
4305 else old_coeff= qmul*level + qadd;
4306 run2= run_tab[rle_index++];
4313 for(change=-1; change<=1; change+=2){
4314 int new_level= level + change;
4315 int score, new_coeff, unquant_change;
4322 if(new_level<0) new_coeff= qmul*new_level - qadd;
4323 else new_coeff= qmul*new_level + qadd;
4324 if(new_coeff >= 2048 || new_coeff <= -2048)
4329 if(level < 63 && level > -63){
4330 if(i < last_non_zero)
4340 if(analyze_gradient){
4341 int g= d1[ scantable[i] ];
4342 if(g && (g^new_level) >= 0)
4346 if(i < last_non_zero){
4347 int next_i= i + run2 + 1;
4348 int next_level= block[ perm_scantable[next_i] ] + 64;
4350 if(next_level&(~127))
4353 if(next_i < last_non_zero)
4373 if(i < last_non_zero){
4374 int next_i= i + run2 + 1;
4375 int next_level= block[ perm_scantable[next_i] ] + 64;
4377 if(next_level&(~127))
4380 if(next_i < last_non_zero)
4399 unquant_change= new_coeff - old_coeff;
4400 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4404 if(score<best_score){
4407 best_change= change;
4408 best_unquant_change= unquant_change;
4412 prev_level= level + 64;
4413 if(prev_level&(~127))
4426 int j= perm_scantable[ best_coeff ];
4428 block[j] += best_change;
4430 if(best_coeff > last_non_zero){
4431 last_non_zero= best_coeff;
4439 if(block[j] - best_change){
4440 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
4452 for(; last_non_zero>=start_i; last_non_zero--){
4453 if(block[perm_scantable[last_non_zero]])
4459 if(256*256*256*64 % count == 0){
4460 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);
4465 for(i=start_i; i<=last_non_zero; i++){
4466 int j= perm_scantable[i];
4467 const int level= block[j];
4470 run_tab[rle_index++]=
run;
4483 if(last_non_zero>0){
4489 return last_non_zero;
4494 int qscale,
int *overflow)
4496 int i, j,
level, last_non_zero, q, start_i;
4501 unsigned int threshold1, threshold2;
4520 block[0] = (block[0] + (q >> 1)) / q;
4532 threshold2= (threshold1<<1);
4533 for(i=63;i>=start_i;i--) {
4535 level = block[j] * qmat[j];
4537 if(((
unsigned)(level+threshold1))>threshold2){
4544 for(i=start_i; i<=last_non_zero; i++) {
4546 level = block[j] * qmat[j];
4550 if(((
unsigned)(level+threshold1))>threshold2){
4568 scantable, last_non_zero);
4570 return last_non_zero;
4573 #define OFFSET(x) offsetof(MpegEncContext, x)
4574 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4576 {
"obmc",
"use overlapped block motion compensation.",