73 uint16_t (*qmat16)[2][64],
74 const uint16_t *quant_matrix,
75 int bias,
int qmin,
int qmax,
int intra)
80 for (qscale = qmin; qscale <= qmax; qscale++) {
85 for (i = 0; i < 64; i++) {
94 (qscale * quant_matrix[j]));
97 for (i = 0; i < 64; i++) {
109 for (i = 0; i < 64; i++) {
117 (qscale * quant_matrix[j]));
121 (qscale * quant_matrix[j]);
123 if (qmat16[qscale][0][i] == 0 ||
124 qmat16[qscale][0][i] == 128 * 256)
125 qmat16[
qscale][0][i] = 128 * 256 - 1;
128 qmat16[qscale][0][i]);
132 for (i = intra; i < 64; i++) {
137 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
144 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
165 for (i = 0; i < 64; i++) {
180 for (i = 0; i < s->
mb_num; i++) {
211 "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
217 for (i = 0; i < 2; i++) {
225 2 * stride * height *
sizeof(int16_t));
238 #define COPY(a) dst->a= src->a
263 for (i = -16; i < 16; i++) {
290 int chroma_h_shift, chroma_v_shift;
299 "only YUV420 and YUV422 are supported\n");
360 "keyframe interval too large!, reducing it from %d to %d\n",
370 #if FF_API_MPV_GLOBAL_OPTS
371 if (avctx->luma_elim_threshold)
373 if (avctx->chroma_elim_threshold)
395 #if FF_API_MPV_GLOBAL_OPTS
396 if (s->
flags & CODEC_FLAG_QP_RD)
438 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
445 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
462 "impossible bitrate constraints, this will fail\n");
476 "bitrate tolerance too small for bitrate\n");
487 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
488 "specified vbv buffer is too large for the given bitrate!\n");
500 "OBMC is only supported with simple mb decision\n");
523 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
531 (avctx->
width > 2048 ||
538 ((avctx->
width &3) ||
545 (avctx->
width > 4095 ||
552 (avctx->
width > 16383 ||
553 avctx->
height > 16383 )) {
554 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
588 "mpeg2 style quantization not supported by codec\n");
592 #if FF_API_MPV_GLOBAL_OPTS
593 if (s->
flags & CODEC_FLAG_CBP_RD)
611 "closed gop with scene change detection are not supported yet, "
612 "set threshold to 1000000000\n");
619 "low delay forcing is only available for mpeg2\n");
624 "b frames cannot be used with low delay\n");
630 if (avctx->
qmax > 12) {
632 "non linear quant only supports qmax <= 12 currently\n");
644 "multi threaded encoding not supported by codec\n");
650 "automatic thread number detection not supported by codec, "
666 i = (INT_MAX / 2 + 128) >> 8;
680 "notice: b_frame_strategy only affects the first pass\n");
714 "timebase %d/%d not supported by MPEG 4 standard, "
715 "the maximum admitted value for the timebase denominator "
722 #if FF_API_MPV_GLOBAL_OPTS
723 if (avctx->
flags2 & CODEC_FLAG2_SKIP_RD)
725 if (avctx->
flags2 & CODEC_FLAG2_STRICT_GOP)
727 if (avctx->quantizer_noise_shaping)
766 if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
773 if (!CONFIG_H261_ENCODER)
777 "The specified picture size of %dx%d is not valid for the "
778 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
787 if (!CONFIG_H263_ENCODER)
792 "The specified picture size of %dx%d is not valid for "
793 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
794 "352x288, 704x576, and 1408x1152. "
900 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
914 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
919 for (i = 0; i < 64; i++) {
963 if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
977 for (y = 0; y < 16; y++) {
978 for (x = 0; x < 16; x++) {
979 acc +=
FFABS(src[x + y * stride] - ref);
995 for (y = 0; y < h; y += 16) {
996 for (x = 0; x < w; x += 16) {
1000 int mean = (s->
dsp.
pix_sum(src + offset, stride) + 128) >> 8;
1001 int sae =
get_sae(src + offset, mean, stride);
1003 acc += sae + 500 < sad;
1014 int i, display_picture_number = 0;
1030 "Error, Invalid timestamp=%"PRId64
", "
1035 if (!s->
low_delay && display_picture_number == 1)
1044 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1047 pts = display_picture_number;
1073 for (i = 0; i < 4; i++) {
1097 int h_chroma_shift, v_chroma_shift;
1102 for (i = 0; i < 3; i++) {
1103 int src_stride = pic_arg->
linesize[i];
1105 int h_shift = i ? h_chroma_shift : 0;
1106 int v_shift = i ? v_chroma_shift : 0;
1107 int w = s->
width >> h_shift;
1108 int h = s->
height >> v_shift;
1113 h = ((s->
height + 15)/16*16) >> v_shift;
1119 if (src_stride == dst_stride)
1120 memcpy(dst, src, src_stride * h);
1125 memcpy(dst2, src, w);
1158 int64_t score64 = 0;
1160 for (plane = 0; plane < 3; plane++) {
1162 const int bw = plane ? 1 : 2;
1163 for (y = 0; y < s->
mb_height * bw; y++) {
1164 for (x = 0; x < s->
mb_width * bw; x++) {
1171 case 0: score =
FFMAX(score, v);
break;
1172 case 1: score +=
FFABS(v);
break;
1173 case 2: score += v *
v;
break;
1174 case 3: score64 +=
FFABS(v * v * (int64_t)v);
break;
1175 case 4: score64 += v * v * (int64_t)(v * v);
break;
1194 int ret, got_output;
1212 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1213 int64_t best_rd = INT64_MAX;
1214 int best_b_count = -1;
1224 b_lambda = p_lambda;
1252 input[i].
data[1] = input[i].
data[0] + ysize;
1253 input[i].
data[2] = input[i].
data[1] + csize;
1259 pre_input = *pre_input_ptr;
1299 input[i + 1].
quality = is_p ? p_lambda : b_lambda;
1327 return best_b_count;
1354 for (i = 0; i < 4; i++)
1410 b_frames =
FFMAX(0, i - 1);
1413 for (i = 0; i < b_frames + 1; i++) {
1425 for (i = b_frames - 1; i >= 0; i--) {
1433 "warning, too many b frames in a row\n");
1456 for (i = 0; i < b_frames; i++) {
1493 for (i = 0; i < 4; i++)
1510 for (i = 0; i < 4; i++) {
1524 AVFrame *pic_arg,
int *got_packet)
1527 int i, stuffing_count, ret;
1550 for (i = 0; i < context_count; i++) {
1554 uint8_t *start = pkt->
data + (size_t)(((int64_t) pkt->
size) * start_y / h);
1611 for (i = 0; i < context_count; i++) {
1624 for (i = 0; i < 4; i++) {
1638 if (stuffing_count) {
1640 stuffing_count + 50) {
1648 while (stuffing_count--) {
1655 stuffing_count -= 4;
1656 while (stuffing_count--) {
1682 "Internal error, negative bits\n");
1690 vbv_delay =
FFMAX(vbv_delay, min_delay);
1723 *got_packet = !!pkt->
size;
1728 int n,
int threshold)
1730 static const char tab[64] = {
1731 3, 2, 2, 1, 1, 1, 1, 1,
1732 1, 1, 1, 1, 1, 1, 1, 1,
1733 1, 1, 1, 1, 1, 1, 1, 1,
1734 0, 0, 0, 0, 0, 0, 0, 0,
1735 0, 0, 0, 0, 0, 0, 0, 0,
1736 0, 0, 0, 0, 0, 0, 0, 0,
1737 0, 0, 0, 0, 0, 0, 0, 0,
1738 0, 0, 0, 0, 0, 0, 0, 0
1747 if (threshold < 0) {
1749 threshold = -threshold;
1754 if (last_index <= skip_dc - 1)
1757 for (i = 0; i <= last_index; i++) {
1761 if (skip_dc && i == 0)
1765 }
else if (level > 1) {
1771 if (score >= threshold)
1773 for (i = skip_dc; i <= last_index; i++) {
1796 for (; i <= last_index; i++) {
1798 int level = block[j];
1800 if (level > maxlevel) {
1803 }
else if (level < minlevel) {
1813 "warning, clipping %d dct coefficients to %d..%d\n",
1814 overflow, minlevel, maxlevel);
1821 for (y = 0; y < 8; y++) {
1822 for (x = 0; x < 8; x++) {
1828 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
1829 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
1830 int v = ptr[x2 + y2 *
stride];
1836 weight[x + 8 *
y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
1842 int motion_x,
int motion_y,
1843 int mb_block_height,
1848 int16_t orig[12][64];
1855 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1858 for (i = 0; i < mb_block_count; i++)
1862 const int last_qp = s->
qscale;
1863 const int mb_xy = mb_x + mb_y * s->
mb_stride;
1894 (mb_y * 16 * wrap_y) + mb_x * 16;
1896 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1898 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1908 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1910 ptr_cb = ebuf + 18 * wrap_y;
1912 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1914 ptr_cr = ebuf + 18 * wrap_y + 16;
1919 int progressive_score, interlaced_score;
1925 NULL, wrap_y, 8) - 400;
1927 if (progressive_score > 0) {
1929 NULL, wrap_y * 2, 8) +
1931 NULL, wrap_y * 2, 8);
1932 if (progressive_score > interlaced_score) {
1935 dct_offset = wrap_y;
1936 uv_dct_offset = wrap_c;
1971 uint8_t *dest_y, *dest_cb, *dest_cr;
1973 dest_y = s->
dest[0];
1974 dest_cb = s->
dest[1];
1975 dest_cr = s->
dest[2];
1999 int progressive_score, interlaced_score;
2006 ptr_y + wrap_y * 8, wrap_y,
2010 progressive_score -= 400;
2012 if (progressive_score > 0) {
2020 if (progressive_score > interlaced_score) {
2023 dct_offset = wrap_y;
2024 uv_dct_offset = wrap_c;
2035 dest_y + dct_offset, wrap_y);
2037 dest_y + dct_offset + 8, wrap_y);
2047 dest_cb + uv_dct_offset, wrap_c);
2049 dest_cr + uv_dct_offset, wrap_c);
2057 wrap_y, 8) < 20 * s->
qscale)
2060 dest_y + 8, wrap_y, 8) < 20 * s->
qscale)
2063 dest_y + dct_offset, wrap_y, 8) < 20 * s->
qscale)
2065 if (s->
dsp.
sad[1](
NULL, ptr_y + dct_offset + 8,
2066 dest_y + dct_offset + 8,
2067 wrap_y, 8) < 20 * s->
qscale)
2070 wrap_c, 8) < 20 * s->
qscale)
2073 wrap_c, 8) < 20 * s->
qscale)
2076 if (s->
dsp.
sad[1](
NULL, ptr_cb + uv_dct_offset,
2077 dest_cb + uv_dct_offset,
2078 wrap_c, 8) < 20 * s->
qscale)
2080 if (s->
dsp.
sad[1](
NULL, ptr_cr + uv_dct_offset,
2081 dest_cr + uv_dct_offset,
2082 wrap_c, 8) < 20 * s->
qscale)
2109 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2115 for (i = 0; i < mb_block_count; i++) {
2130 for (i = 0; i < mb_block_count; i++) {
2140 for (i = 0; i < 4; i++)
2143 for (i = 4; i < mb_block_count; i++)
2147 for (i = 0; i < mb_block_count; i++) {
2160 for (i=6; i<12; i++) {
2169 for (i = 0; i < mb_block_count; i++) {
2172 for (j = 63; j > 0; j--) {
2185 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2189 if (CONFIG_MPEG4_ENCODER)
2199 if (CONFIG_WMV2_ENCODER)
2203 if (CONFIG_H261_ENCODER)
2211 if (CONFIG_H263_ENCODER)
2216 if (CONFIG_MJPEG_ENCODER)
2262 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2300 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2308 s->
pb= pb[*next_block];
2310 s->
pb2 = pb2 [*next_block];
2311 s->
tex_pb= tex_pb[*next_block];
2315 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2338 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2356 else if(w==8 && h==8)
2446 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2488 if (CONFIG_H263_ENCODER)
2490 bytestream_put_le32(&ptr, offset);
2491 bytestream_put_byte(&ptr, s->
qscale);
2492 bytestream_put_byte(&ptr, gobn);
2493 bytestream_put_le16(&ptr, mba);
2494 bytestream_put_byte(&ptr, pred_x);
2495 bytestream_put_byte(&ptr, pred_y);
2497 bytestream_put_byte(&ptr, 0);
2498 bytestream_put_byte(&ptr, 0);
2574 if (CONFIG_H263_ENCODER)
2594 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2625 int current_packet_size, is_gob_start;
2631 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2640 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2645 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2664 current_packet_size=0;
2678 if (CONFIG_MPEG4_ENCODER) {
2685 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2692 if (CONFIG_H263_ENCODER)
2722 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2739 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2740 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2751 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2752 &dmin, &next_block, 0, 0);
2760 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2761 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2771 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2772 &dmin, &next_block, 0, 0);
2780 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2781 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2789 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2790 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
2800 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2801 &dmin, &next_block, 0, 0);
2812 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2813 &dmin, &next_block, 0, 0);
2824 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2825 &dmin, &next_block, 0, 0);
2831 for(dir=0; dir<2; dir++){
2838 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2839 &dmin, &next_block, 0, 0);
2847 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2848 &dmin, &next_block, 0, 0);
2859 const int last_qp= backup_s.
qscale;
2863 static const int dquant_tab[4]={-1,1,-2,2};
2872 s->
mv[0][0][0] = best_s.
mv[0][0][0];
2873 s->
mv[0][0][1] = best_s.
mv[0][0][1];
2874 s->
mv[1][0][0] = best_s.
mv[1][0][0];
2875 s->
mv[1][0][1] = best_s.
mv[1][0][1];
2878 for(; qpi<4; qpi++){
2879 int dquant= dquant_tab[qpi];
2891 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2892 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
2912 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2913 &dmin, &next_block, mx, my);
2920 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2921 &dmin, &next_block, 0, 0);
2929 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
2950 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2951 &dmin, &next_block, mx, my);
2978 if (CONFIG_H263_ENCODER &&
2991 int motion_x = 0, motion_y = 0;
2999 motion_x= s->
mv[0][0][0] = 0;
3000 motion_y= s->
mv[0][0][1] = 0;
3028 if (CONFIG_MPEG4_ENCODER) {
3037 if (CONFIG_MPEG4_ENCODER) {
3087 for(dir=0; dir<2; dir++){
3104 if (CONFIG_H263_ENCODER &&
3161 #define MERGE(field) dst->field += src->field; src->field=0
3188 for(i=0; i<64; i++){
3215 if (CONFIG_MPEG4_ENCODER)
3221 if (CONFIG_H263_ENCODER)
3302 for(i=1; i<context_count; i++){
3332 for(i=1; i<context_count; i++){
3345 av_dlog(s,
"Scene change detected, encoding as I Frame %d %d\n",
3389 for(dir=0; dir<2; dir++){
3424 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};
3425 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};
3456 if (CONFIG_MJPEG_ENCODER)
3460 if (CONFIG_H261_ENCODER)
3468 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred)
3476 else if (CONFIG_H263_ENCODER)
3480 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3491 for(i=1; i<context_count; i++){
3495 for(i=1; i<context_count; i++){
3508 for(i=0; i<64; i++){
3509 int level= block[i];
3515 if(level<0) level=0;
3519 if(level>0) level=0;
3527 int16_t *
block,
int n,
3528 int qscale,
int *overflow){
3533 unsigned int threshold1, threshold2;
3545 int coeff_count[64];
3546 int qmul, qadd, start_i, last_non_zero, i,
dc;
3557 qadd= ((qscale-1)|1)*8;
3574 block[0] = (block[0] + (q >> 1)) / q;
3592 threshold2= (threshold1<<1);
3594 for(i=63; i>=start_i; i--) {
3595 const int j = scantable[i];
3596 int level = block[j] * qmat[j];
3598 if(((
unsigned)(level+threshold1))>threshold2){
3604 for(i=start_i; i<=last_non_zero; i++) {
3605 const int j = scantable[i];
3606 int level = block[j] * qmat[j];
3610 if(((
unsigned)(level+threshold1))>threshold2){
3614 coeff[1][i]= level-1;
3618 coeff[0][i]= -
level;
3619 coeff[1][i]= -level+1;
3622 coeff_count[i]=
FFMIN(level, 2);
3626 coeff[0][i]= (level>>31)|1;
3633 if(last_non_zero < start_i){
3634 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3635 return last_non_zero;
3638 score_tab[start_i]= 0;
3639 survivor[0]= start_i;
3642 for(i=start_i; i<=last_non_zero; i++){
3643 int level_index, j, zero_distortion;
3644 int dct_coeff=
FFABS(block[ scantable[i] ]);
3645 int best_score=256*256*256*120;
3649 zero_distortion= dct_coeff*dct_coeff;
3651 for(level_index=0; level_index < coeff_count[i]; level_index++){
3653 int level= coeff[level_index][i];
3654 const int alevel=
FFABS(level);
3660 unquant_coeff= alevel*qmul + qadd;
3664 unquant_coeff = (int)( alevel * qscale * s->
intra_matrix[j]) >> 3;
3665 unquant_coeff = (unquant_coeff - 1) | 1;
3667 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[j])) >> 4;
3668 unquant_coeff = (unquant_coeff - 1) | 1;
3673 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3675 if((level&(~127)) == 0){
3676 for(j=survivor_count-1; j>=0; j--){
3677 int run= i - survivor[j];
3679 score += score_tab[i-
run];
3681 if(score < best_score){
3684 level_tab[i+1]= level-64;
3689 for(j=survivor_count-1; j>=0; j--){
3690 int run= i - survivor[j];
3692 score += score_tab[i-
run];
3693 if(score < last_score){
3696 last_level= level-64;
3702 distortion += esc_length*
lambda;
3703 for(j=survivor_count-1; j>=0; j--){
3704 int run= i - survivor[j];
3705 int score= distortion + score_tab[i-
run];
3707 if(score < best_score){
3710 level_tab[i+1]= level-64;
3715 for(j=survivor_count-1; j>=0; j--){
3716 int run= i - survivor[j];
3717 int score= distortion + score_tab[i-
run];
3718 if(score < last_score){
3721 last_level= level-64;
3729 score_tab[i+1]= best_score;
3732 if(last_non_zero <= 27){
3733 for(; survivor_count; survivor_count--){
3734 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3738 for(; survivor_count; survivor_count--){
3739 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3744 survivor[ survivor_count++ ]= i+1;
3748 last_score= 256*256*256*120;
3749 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3750 int score= score_tab[i];
3751 if(i) score += lambda*2;
3753 if(score < last_score){
3756 last_level= level_tab[i];
3757 last_run= run_tab[i];
3764 dc=
FFABS(block[0]);
3765 last_non_zero= last_i - 1;
3766 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3768 if(last_non_zero < start_i)
3769 return last_non_zero;
3771 if(last_non_zero == 0 && start_i == 0){
3773 int best_score= dc *
dc;
3775 for(i=0; i<coeff_count[0]; i++){
3776 int level= coeff[i][0];
3777 int alevel=
FFABS(level);
3778 int unquant_coeff, score, distortion;
3781 unquant_coeff= (alevel*qmul + qadd)>>3;
3783 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[0])) >> 4;
3784 unquant_coeff = (unquant_coeff - 1) | 1;
3786 unquant_coeff = (unquant_coeff + 4) >> 3;
3787 unquant_coeff<<= 3 + 3;
3789 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
3792 else score= distortion + esc_length*
lambda;
3794 if(score < best_score){
3796 best_level= level - 64;
3799 block[0]= best_level;
3801 if(best_level == 0)
return -1;
3802 else return last_non_zero;
3808 block[ perm_scantable[last_non_zero] ]= last_level;
3811 for(; i>start_i; i -= run_tab[i] + 1){
3812 block[ perm_scantable[i-1] ]= level_tab[i];
3815 return last_non_zero;
3830 int perm_index= perm[
index];
3831 if(i==0) s*= sqrt(0.5);
3832 if(j==0) s*= sqrt(0.5);
3833 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)));
3852 int qmul, qadd, start_i, last_non_zero, i,
dc;
3856 int rle_index,
run, q = 1, sum;
3859 static int after_last=0;
3860 static int to_zero=0;
3861 static int from_zero=0;
3864 static int messed_sign=0;
3867 if(basis[0][0] == 0)
3904 for(i=0; i<64; i++){
3911 for(i=0; i<64; i++){
3916 w=
FFABS(weight[i]) + qns*one;
3917 w= 15 + (48*qns*one + w/2)/w;
3932 for(i=start_i; i<=last_non_zero; i++){
3933 int j= perm_scantable[i];
3934 const int level= block[j];
3938 if(level<0) coeff= qmul*level - qadd;
3939 else coeff= qmul*level + qadd;
3940 run_tab[rle_index++]=
run;
3949 if(last_non_zero>0){
3960 int run2, best_unquant_change=0, analyze_gradient;
3966 if(analyze_gradient){
3970 for(i=0; i<64; i++){
3986 const int level= block[0];
3987 int change, old_coeff;
3993 for(change=-1; change<=1; change+=2){
3994 int new_level= level + change;
3995 int score, new_coeff;
3997 new_coeff= q*new_level;
3998 if(new_coeff >= 2048 || new_coeff < 0)
4001 score= s->
dsp.
try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
4002 if(score<best_score){
4005 best_change= change;
4006 best_unquant_change= new_coeff - old_coeff;
4013 run2= run_tab[rle_index++];
4017 for(i=start_i; i<64; i++){
4018 int j= perm_scantable[i];
4019 const int level= block[j];
4020 int change, old_coeff;
4026 if(level<0) old_coeff= qmul*level - qadd;
4027 else old_coeff= qmul*level + qadd;
4028 run2= run_tab[rle_index++];
4035 for(change=-1; change<=1; change+=2){
4036 int new_level= level + change;
4037 int score, new_coeff, unquant_change;
4044 if(new_level<0) new_coeff= qmul*new_level - qadd;
4045 else new_coeff= qmul*new_level + qadd;
4046 if(new_coeff >= 2048 || new_coeff <= -2048)
4051 if(level < 63 && level > -63){
4052 if(i < last_non_zero)
4062 if(analyze_gradient){
4063 int g= d1[ scantable[i] ];
4064 if(g && (g^new_level) >= 0)
4068 if(i < last_non_zero){
4069 int next_i= i + run2 + 1;
4070 int next_level= block[ perm_scantable[next_i] ] + 64;
4072 if(next_level&(~127))
4075 if(next_i < last_non_zero)
4095 if(i < last_non_zero){
4096 int next_i= i + run2 + 1;
4097 int next_level= block[ perm_scantable[next_i] ] + 64;
4099 if(next_level&(~127))
4102 if(next_i < last_non_zero)
4121 unquant_change= new_coeff - old_coeff;
4122 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4125 if(score<best_score){
4128 best_change= change;
4129 best_unquant_change= unquant_change;
4133 prev_level= level + 64;
4134 if(prev_level&(~127))
4147 int j= perm_scantable[ best_coeff ];
4149 block[j] += best_change;
4151 if(best_coeff > last_non_zero){
4152 last_non_zero= best_coeff;
4160 if(block[j] - best_change){
4161 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
4173 for(; last_non_zero>=start_i; last_non_zero--){
4174 if(block[perm_scantable[last_non_zero]])
4180 if(256*256*256*64 % count == 0){
4181 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);
4186 for(i=start_i; i<=last_non_zero; i++){
4187 int j= perm_scantable[i];
4188 const int level= block[j];
4191 run_tab[rle_index++]=
run;
4204 if(last_non_zero>0){
4210 return last_non_zero;
4214 int16_t *
block,
int n,
4215 int qscale,
int *overflow)
4217 int i, j,
level, last_non_zero, q, start_i;
4222 unsigned int threshold1, threshold2;
4241 block[0] = (block[0] + (q >> 1)) / q;
4253 threshold2= (threshold1<<1);
4254 for(i=63;i>=start_i;i--) {
4256 level = block[j] * qmat[j];
4258 if(((
unsigned)(level+threshold1))>threshold2){
4265 for(i=start_i; i<=last_non_zero; i++) {
4267 level = block[j] * qmat[j];
4271 if(((
unsigned)(level+threshold1))>threshold2){
4290 return last_non_zero;
4293 #define OFFSET(x) offsetof(MpegEncContext, x)
4294 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4298 {
"mb_info",
"emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size",
OFFSET(
mb_info),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
VE },
4355 .
name =
"msmpeg4v2",
4364 .priv_class = &msmpeg4v2_class,
4379 .priv_class = &msmpeg4v3_class,
4394 .priv_class = &wmv1_class,