71 uint16_t (*qmat16)[2][64],
72 const uint16_t *quant_matrix,
73 int bias,
int qmin,
int qmax,
int intra)
78 for (qscale = qmin; qscale <= qmax; qscale++) {
83 for (i = 0; i < 64; i++) {
92 (qscale * quant_matrix[j]));
95 for (i = 0; i < 64; i++) {
107 for (i = 0; i < 64; i++) {
115 (qscale * quant_matrix[j]));
119 (qscale * quant_matrix[j]);
121 if (qmat16[qscale][0][i] == 0 ||
122 qmat16[qscale][0][i] == 128 * 256)
123 qmat16[
qscale][0][i] = 128 * 256 - 1;
126 qmat16[qscale][0][i]);
130 for (i = intra; i < 64; i++) {
135 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
142 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
163 for (i = 0; i < 64; i++) {
178 for (i = 0; i < s->
mb_num; i++) {
210 "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
216 for (i = 0; i < 2; i++) {
224 2 * stride * height *
sizeof(int16_t));
237 #define COPY(a) dst->a= src->a
262 for (i = -16; i < 16; i++) {
289 int chroma_h_shift, chroma_v_shift;
298 "only YUV420 and YUV422 are supported\n");
359 "keyframe interval too large!, reducing it from %d to %d\n",
369 #if FF_API_MPV_GLOBAL_OPTS
370 if (avctx->luma_elim_threshold)
372 if (avctx->chroma_elim_threshold)
394 #if FF_API_MPV_GLOBAL_OPTS
395 if (s->
flags & CODEC_FLAG_QP_RD)
437 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
444 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
461 "impossible bitrate constraints, this will fail\n");
475 "bitrate tolerance too small for bitrate\n");
486 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
487 "specified vbv buffer is too large for the given bitrate!\n");
499 "OBMC is only supported with simple mb decision\n");
522 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
530 (avctx->
width > 2048 ||
537 ((avctx->
width &3) ||
544 (avctx->
width > 4095 ||
551 (avctx->
width > 16383 ||
552 avctx->
height > 16383 )) {
553 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
587 "mpeg2 style quantization not supported by codec\n");
591 #if FF_API_MPV_GLOBAL_OPTS
592 if (s->
flags & CODEC_FLAG_CBP_RD)
610 "closed gop with scene change detection are not supported yet, "
611 "set threshold to 1000000000\n");
618 "low delay forcing is only available for mpeg2\n");
623 "b frames cannot be used with low delay\n");
629 if (avctx->
qmax > 12) {
631 "non linear quant only supports qmax <= 12 currently\n");
643 "multi threaded encoding not supported by codec\n");
649 "automatic thread number detection not supported by codec, "
665 i = (INT_MAX / 2 + 128) >> 8;
679 "notice: b_frame_strategy only affects the first pass\n");
713 "timebase %d/%d not supported by MPEG 4 standard, "
714 "the maximum admitted value for the timebase denominator "
721 #if FF_API_MPV_GLOBAL_OPTS
722 if (avctx->
flags2 & CODEC_FLAG2_SKIP_RD)
724 if (avctx->
flags2 & CODEC_FLAG2_STRICT_GOP)
726 if (avctx->quantizer_noise_shaping)
765 if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
772 if (!CONFIG_H261_ENCODER)
776 "The specified picture size of %dx%d is not valid for the "
777 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
786 if (!CONFIG_H263_ENCODER)
791 "The specified picture size of %dx%d is not valid for "
792 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
793 "352x288, 704x576, and 1408x1152. "
899 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
913 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
918 for (i = 0; i < 64; i++) {
962 if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
976 for (y = 0; y < 16; y++) {
977 for (x = 0; x < 16; x++) {
978 acc +=
FFABS(src[x + y * stride] - ref);
994 for (y = 0; y < h; y += 16) {
995 for (x = 0; x < w; x += 16) {
999 int mean = (s->
dsp.
pix_sum(src + offset, stride) + 128) >> 8;
1000 int sae =
get_sae(src + offset, mean, stride);
1002 acc += sae + 500 < sad;
1029 "Error, Invalid timestamp=%"PRId64
", "
1043 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1072 for (i = 0; i < 4; i++) {
1096 int h_chroma_shift, v_chroma_shift;
1099 for (i = 0; i < 3; i++) {
1100 int src_stride = pic_arg->
linesize[i];
1102 int h_shift = i ? h_chroma_shift : 0;
1103 int v_shift = i ? v_chroma_shift : 0;
1104 int w = s->
width >> h_shift;
1105 int h = s->
height >> v_shift;
1110 h= ((s->
height+15)/16*16)>>v_shift;
1116 if (src_stride == dst_stride)
1117 memcpy(dst, src, src_stride * h);
1120 memcpy(dst, src, w);
1145 int64_t score64 = 0;
1147 for (plane = 0; plane < 3; plane++) {
1149 const int bw = plane ? 1 : 2;
1150 for (y = 0; y < s->
mb_height * bw; y++) {
1151 for (x = 0; x < s->
mb_width * bw; x++) {
1158 case 0: score =
FFMAX(score, v);
break;
1159 case 1: score +=
FFABS(v);
break;
1160 case 2: score += v * v;
break;
1161 case 3: score64 +=
FFABS(v * v * (int64_t)v);
break;
1162 case 4: score64 += v * v * (int64_t)(v * v);
break;
1181 int ret, got_output;
1199 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1200 int64_t best_rd = INT64_MAX;
1201 int best_b_count = -1;
1211 b_lambda = p_lambda;
1239 input[i].
data[1] = input[i].
data[0] + ysize;
1240 input[i].
data[2] = input[i].
data[1] + csize;
1246 pre_input = *pre_input_ptr;
1286 input[i + 1].
quality = is_p ? p_lambda : b_lambda;
1314 return best_b_count;
1341 for (i = 0; i < 4; i++)
1397 b_frames =
FFMAX(0, i - 1);
1400 for (i = 0; i < b_frames + 1; i++) {
1412 for (i = b_frames - 1; i >= 0; i--) {
1420 "warning, too many b frames in a row\n");
1443 for (i = 0; i < b_frames; i++) {
1480 for (i = 0; i < 4; i++)
1497 for (i = 0; i < 4; i++) {
1511 AVFrame *pic_arg,
int *got_packet)
1514 int i, stuffing_count, ret;
1537 for (i = 0; i < context_count; i++) {
1541 uint8_t *start = pkt->
data + (size_t)(((int64_t) pkt->
size) * start_y / h);
1598 for (i = 0; i < context_count; i++) {
1611 for (i = 0; i < 4; i++) {
1625 if (stuffing_count) {
1627 stuffing_count + 50) {
1635 while (stuffing_count--) {
1642 stuffing_count -= 4;
1643 while (stuffing_count--) {
1669 "Internal error, negative bits\n");
1677 vbv_delay =
FFMAX(vbv_delay, min_delay);
1710 *got_packet = !!pkt->
size;
1715 int n,
int threshold)
1717 static const char tab[64] = {
1718 3, 2, 2, 1, 1, 1, 1, 1,
1719 1, 1, 1, 1, 1, 1, 1, 1,
1720 1, 1, 1, 1, 1, 1, 1, 1,
1721 0, 0, 0, 0, 0, 0, 0, 0,
1722 0, 0, 0, 0, 0, 0, 0, 0,
1723 0, 0, 0, 0, 0, 0, 0, 0,
1724 0, 0, 0, 0, 0, 0, 0, 0,
1725 0, 0, 0, 0, 0, 0, 0, 0
1734 if (threshold < 0) {
1736 threshold = -threshold;
1741 if (last_index <= skip_dc - 1)
1744 for (i = 0; i <= last_index; i++) {
1748 if (skip_dc && i == 0)
1752 }
else if (level > 1) {
1758 if (score >= threshold)
1760 for (i = skip_dc; i <= last_index; i++) {
1783 for (; i <= last_index; i++) {
1785 int level = block[j];
1787 if (level > maxlevel) {
1790 }
else if (level < minlevel) {
1800 "warning, clipping %d dct coefficients to %d..%d\n",
1801 overflow, minlevel, maxlevel);
1808 for (y = 0; y < 8; y++) {
1809 for (x = 0; x < 8; x++) {
1815 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
1816 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
1817 int v = ptr[x2 + y2 *
stride];
1823 weight[x + 8 * y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
1829 int motion_x,
int motion_y,
1830 int mb_block_height,
1842 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1845 for (i = 0; i < mb_block_count; i++)
1849 const int last_qp = s->
qscale;
1850 const int mb_xy = mb_x + mb_y * s->
mb_stride;
1881 (mb_y * 16 * wrap_y) + mb_x * 16;
1883 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1885 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1895 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1897 ptr_cb = ebuf + 18 * wrap_y;
1899 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1901 ptr_cr = ebuf + 18 * wrap_y + 16;
1906 int progressive_score, interlaced_score;
1912 NULL, wrap_y, 8) - 400;
1914 if (progressive_score > 0) {
1916 NULL, wrap_y * 2, 8) +
1918 NULL, wrap_y * 2, 8);
1919 if (progressive_score > interlaced_score) {
1922 dct_offset = wrap_y;
1923 uv_dct_offset = wrap_c;
1958 uint8_t *dest_y, *dest_cb, *dest_cr;
1960 dest_y = s->
dest[0];
1961 dest_cb = s->
dest[1];
1962 dest_cr = s->
dest[2];
1986 int progressive_score, interlaced_score;
1993 ptr_y + wrap_y * 8, wrap_y,
1997 progressive_score -= 400;
1999 if (progressive_score > 0) {
2007 if (progressive_score > interlaced_score) {
2010 dct_offset = wrap_y;
2011 uv_dct_offset = wrap_c;
2022 dest_y + dct_offset, wrap_y);
2024 dest_y + dct_offset + 8, wrap_y);
2034 dest_cb + uv_dct_offset, wrap_c);
2036 dest_cr + uv_dct_offset, wrap_c);
2044 wrap_y, 8) < 20 * s->
qscale)
2047 dest_y + 8, wrap_y, 8) < 20 * s->
qscale)
2050 dest_y + dct_offset, wrap_y, 8) < 20 * s->
qscale)
2052 if (s->
dsp.
sad[1](
NULL, ptr_y + dct_offset + 8,
2053 dest_y + dct_offset + 8,
2054 wrap_y, 8) < 20 * s->
qscale)
2057 wrap_c, 8) < 20 * s->
qscale)
2060 wrap_c, 8) < 20 * s->
qscale)
2063 if (s->
dsp.
sad[1](
NULL, ptr_cb + uv_dct_offset,
2064 dest_cb + uv_dct_offset,
2065 wrap_c, 8) < 20 * s->
qscale)
2067 if (s->
dsp.
sad[1](
NULL, ptr_cr + uv_dct_offset,
2068 dest_cr + uv_dct_offset,
2069 wrap_c, 8) < 20 * s->
qscale)
2096 memcpy(orig[0], s->
block[0],
sizeof(
DCTELEM) * 64 * mb_block_count);
2102 for (i = 0; i < mb_block_count; i++) {
2117 for (i = 0; i < mb_block_count; i++) {
2127 for (i = 0; i < 4; i++)
2130 for (i = 4; i < mb_block_count; i++)
2134 for (i = 0; i < mb_block_count; i++) {
2150 for (i = 0; i < mb_block_count; i++) {
2153 for (j = 63; j > 0; j--) {
2166 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2170 if (CONFIG_MPEG4_ENCODER)
2180 if (CONFIG_WMV2_ENCODER)
2184 if (CONFIG_H261_ENCODER)
2192 if (CONFIG_H263_ENCODER)
2197 if (CONFIG_MJPEG_ENCODER)
2243 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2281 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2289 s->
pb= pb[*next_block];
2291 s->
pb2 = pb2 [*next_block];
2292 s->
tex_pb= tex_pb[*next_block];
2296 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2319 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2337 else if(w==8 && h==8)
2427 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2469 if (CONFIG_H263_ENCODER)
2471 bytestream_put_le32(&ptr, offset);
2472 bytestream_put_byte(&ptr, s->
qscale);
2473 bytestream_put_byte(&ptr, gobn);
2474 bytestream_put_le16(&ptr, mba);
2475 bytestream_put_byte(&ptr, pred_x);
2476 bytestream_put_byte(&ptr, pred_y);
2478 bytestream_put_byte(&ptr, 0);
2479 bytestream_put_byte(&ptr, 0);
2555 if (CONFIG_H263_ENCODER)
2575 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2606 int current_packet_size, is_gob_start;
2612 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2621 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2626 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2645 current_packet_size=0;
2659 if (CONFIG_MPEG4_ENCODER) {
2666 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2673 if (CONFIG_H263_ENCODER)
2703 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2720 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2721 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2732 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2733 &dmin, &next_block, 0, 0);
2741 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2742 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2752 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2753 &dmin, &next_block, 0, 0);
2761 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2762 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2770 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2771 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
2781 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2782 &dmin, &next_block, 0, 0);
2793 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2794 &dmin, &next_block, 0, 0);
2805 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2806 &dmin, &next_block, 0, 0);
2812 for(dir=0; dir<2; dir++){
2819 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2820 &dmin, &next_block, 0, 0);
2828 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2829 &dmin, &next_block, 0, 0);
2840 const int last_qp= backup_s.
qscale;
2844 static const int dquant_tab[4]={-1,1,-2,2};
2852 s->
mv[0][0][0] = best_s.
mv[0][0][0];
2853 s->
mv[0][0][1] = best_s.
mv[0][0][1];
2854 s->
mv[1][0][0] = best_s.
mv[1][0][0];
2855 s->
mv[1][0][1] = best_s.
mv[1][0][1];
2858 for(; qpi<4; qpi++){
2859 int dquant= dquant_tab[qpi];
2871 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2872 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
2892 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2893 &dmin, &next_block, mx, my);
2900 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2901 &dmin, &next_block, 0, 0);
2909 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
2930 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2931 &dmin, &next_block, mx, my);
2958 if (CONFIG_H263_ENCODER &&
2971 int motion_x = 0, motion_y = 0;
2979 motion_x= s->
mv[0][0][0] = 0;
2980 motion_y= s->
mv[0][0][1] = 0;
3008 if (CONFIG_MPEG4_ENCODER) {
3017 if (CONFIG_MPEG4_ENCODER) {
3067 for(dir=0; dir<2; dir++){
3084 if (CONFIG_H263_ENCODER &&
3141 #define MERGE(field) dst->field += src->field; src->field=0
3168 for(i=0; i<64; i++){
3195 if (CONFIG_MPEG4_ENCODER)
3201 if (CONFIG_H263_ENCODER)
3282 for(i=1; i<context_count; i++){
3312 for(i=1; i<context_count; i++){
3325 av_dlog(s,
"Scene change detected, encoding as I Frame %d %d\n",
3369 for(dir=0; dir<2; dir++){
3404 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};
3405 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};
3436 if (CONFIG_MJPEG_ENCODER)
3440 if (CONFIG_H261_ENCODER)
3448 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred)
3456 else if (CONFIG_H263_ENCODER)
3460 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3471 for(i=1; i<context_count; i++){
3475 for(i=1; i<context_count; i++){
3488 for(i=0; i<64; i++){
3489 int level= block[i];
3495 if(level<0) level=0;
3499 if(level>0) level=0;
3508 int qscale,
int *overflow){
3513 unsigned int threshold1, threshold2;
3525 int coeff_count[64];
3526 int qmul, qadd, start_i, last_non_zero, i, dc;
3537 qadd= ((qscale-1)|1)*8;
3554 block[0] = (block[0] + (q >> 1)) / q;
3572 threshold2= (threshold1<<1);
3574 for(i=63; i>=start_i; i--) {
3575 const int j = scantable[i];
3576 int level = block[j] * qmat[j];
3578 if(((
unsigned)(level+threshold1))>threshold2){
3584 for(i=start_i; i<=last_non_zero; i++) {
3585 const int j = scantable[i];
3586 int level = block[j] * qmat[j];
3590 if(((
unsigned)(level+threshold1))>threshold2){
3594 coeff[1][i]= level-1;
3598 coeff[0][i]= -
level;
3599 coeff[1][i]= -level+1;
3602 coeff_count[i]=
FFMIN(level, 2);
3606 coeff[0][i]= (level>>31)|1;
3613 if(last_non_zero < start_i){
3614 memset(block + start_i, 0, (64-start_i)*
sizeof(
DCTELEM));
3615 return last_non_zero;
3618 score_tab[start_i]= 0;
3619 survivor[0]= start_i;
3622 for(i=start_i; i<=last_non_zero; i++){
3623 int level_index, j, zero_distortion;
3624 int dct_coeff=
FFABS(block[ scantable[i] ]);
3625 int best_score=256*256*256*120;
3629 zero_distortion= dct_coeff*dct_coeff;
3631 for(level_index=0; level_index < coeff_count[i]; level_index++){
3633 int level= coeff[level_index][i];
3634 const int alevel=
FFABS(level);
3640 unquant_coeff= alevel*qmul + qadd;
3644 unquant_coeff = (int)( alevel * qscale * s->
intra_matrix[j]) >> 3;
3645 unquant_coeff = (unquant_coeff - 1) | 1;
3647 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[j])) >> 4;
3648 unquant_coeff = (unquant_coeff - 1) | 1;
3653 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3655 if((level&(~127)) == 0){
3656 for(j=survivor_count-1; j>=0; j--){
3657 int run= i - survivor[j];
3659 score += score_tab[i-
run];
3661 if(score < best_score){
3664 level_tab[i+1]= level-64;
3669 for(j=survivor_count-1; j>=0; j--){
3670 int run= i - survivor[j];
3672 score += score_tab[i-
run];
3673 if(score < last_score){
3676 last_level= level-64;
3682 distortion += esc_length*
lambda;
3683 for(j=survivor_count-1; j>=0; j--){
3684 int run= i - survivor[j];
3685 int score= distortion + score_tab[i-
run];
3687 if(score < best_score){
3690 level_tab[i+1]= level-64;
3695 for(j=survivor_count-1; j>=0; j--){
3696 int run= i - survivor[j];
3697 int score= distortion + score_tab[i-
run];
3698 if(score < last_score){
3701 last_level= level-64;
3709 score_tab[i+1]= best_score;
3712 if(last_non_zero <= 27){
3713 for(; survivor_count; survivor_count--){
3714 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3718 for(; survivor_count; survivor_count--){
3719 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3724 survivor[ survivor_count++ ]= i+1;
3728 last_score= 256*256*256*120;
3729 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3730 int score= score_tab[i];
3731 if(i) score += lambda*2;
3733 if(score < last_score){
3736 last_level= level_tab[i];
3737 last_run= run_tab[i];
3744 dc=
FFABS(block[0]);
3745 last_non_zero= last_i - 1;
3746 memset(block + start_i, 0, (64-start_i)*
sizeof(
DCTELEM));
3748 if(last_non_zero < start_i)
3749 return last_non_zero;
3751 if(last_non_zero == 0 && start_i == 0){
3753 int best_score= dc * dc;
3755 for(i=0; i<coeff_count[0]; i++){
3756 int level= coeff[i][0];
3757 int alevel=
FFABS(level);
3758 int unquant_coeff, score, distortion;
3761 unquant_coeff= (alevel*qmul + qadd)>>3;
3763 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[0])) >> 4;
3764 unquant_coeff = (unquant_coeff - 1) | 1;
3766 unquant_coeff = (unquant_coeff + 4) >> 3;
3767 unquant_coeff<<= 3 + 3;
3769 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3772 else score= distortion + esc_length*
lambda;
3774 if(score < best_score){
3776 best_level= level - 64;
3779 block[0]= best_level;
3781 if(best_level == 0)
return -1;
3782 else return last_non_zero;
3788 block[ perm_scantable[last_non_zero] ]= last_level;
3791 for(; i>start_i; i -= run_tab[i] + 1){
3792 block[ perm_scantable[i-1] ]= level_tab[i];
3795 return last_non_zero;
3810 int perm_index= perm[
index];
3811 if(i==0) s*= sqrt(0.5);
3812 if(j==0) s*= sqrt(0.5);
3813 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)));
3832 int qmul, qadd, start_i, last_non_zero, i, dc;
3836 int rle_index,
run, q = 1, sum;
3839 static int after_last=0;
3840 static int to_zero=0;
3841 static int from_zero=0;
3844 static int messed_sign=0;
3847 if(basis[0][0] == 0)
3884 for(i=0; i<64; i++){
3891 for(i=0; i<64; i++){
3896 w=
FFABS(weight[i]) + qns*one;
3897 w= 15 + (48*qns*one + w/2)/w;
3912 for(i=start_i; i<=last_non_zero; i++){
3913 int j= perm_scantable[i];
3914 const int level= block[j];
3918 if(level<0) coeff= qmul*level - qadd;
3919 else coeff= qmul*level + qadd;
3920 run_tab[rle_index++]=
run;
3929 if(last_non_zero>0){
3940 int run2, best_unquant_change=0, analyze_gradient;
3946 if(analyze_gradient){
3950 for(i=0; i<64; i++){
3966 const int level= block[0];
3967 int change, old_coeff;
3973 for(change=-1; change<=1; change+=2){
3974 int new_level= level + change;
3975 int score, new_coeff;
3977 new_coeff= q*new_level;
3978 if(new_coeff >= 2048 || new_coeff < 0)
3981 score= s->
dsp.
try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
3982 if(score<best_score){
3985 best_change= change;
3986 best_unquant_change= new_coeff - old_coeff;
3993 run2= run_tab[rle_index++];
3997 for(i=start_i; i<64; i++){
3998 int j= perm_scantable[i];
3999 const int level= block[j];
4000 int change, old_coeff;
4006 if(level<0) old_coeff= qmul*level - qadd;
4007 else old_coeff= qmul*level + qadd;
4008 run2= run_tab[rle_index++];
4015 for(change=-1; change<=1; change+=2){
4016 int new_level= level + change;
4017 int score, new_coeff, unquant_change;
4024 if(new_level<0) new_coeff= qmul*new_level - qadd;
4025 else new_coeff= qmul*new_level + qadd;
4026 if(new_coeff >= 2048 || new_coeff <= -2048)
4031 if(level < 63 && level > -63){
4032 if(i < last_non_zero)
4042 if(analyze_gradient){
4043 int g= d1[ scantable[i] ];
4044 if(g && (g^new_level) >= 0)
4048 if(i < last_non_zero){
4049 int next_i= i + run2 + 1;
4050 int next_level= block[ perm_scantable[next_i] ] + 64;
4052 if(next_level&(~127))
4055 if(next_i < last_non_zero)
4075 if(i < last_non_zero){
4076 int next_i= i + run2 + 1;
4077 int next_level= block[ perm_scantable[next_i] ] + 64;
4079 if(next_level&(~127))
4082 if(next_i < last_non_zero)
4101 unquant_change= new_coeff - old_coeff;
4102 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4105 if(score<best_score){
4108 best_change= change;
4109 best_unquant_change= unquant_change;
4113 prev_level= level + 64;
4114 if(prev_level&(~127))
4127 int j= perm_scantable[ best_coeff ];
4129 block[j] += best_change;
4131 if(best_coeff > last_non_zero){
4132 last_non_zero= best_coeff;
4140 if(block[j] - best_change){
4141 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
4153 for(; last_non_zero>=start_i; last_non_zero--){
4154 if(block[perm_scantable[last_non_zero]])
4160 if(256*256*256*64 % count == 0){
4161 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);
4166 for(i=start_i; i<=last_non_zero; i++){
4167 int j= perm_scantable[i];
4168 const int level= block[j];
4171 run_tab[rle_index++]=
run;
4184 if(last_non_zero>0){
4190 return last_non_zero;
4195 int qscale,
int *overflow)
4197 int i, j,
level, last_non_zero, q, start_i;
4202 unsigned int threshold1, threshold2;
4221 block[0] = (block[0] + (q >> 1)) / q;
4233 threshold2= (threshold1<<1);
4234 for(i=63;i>=start_i;i--) {
4236 level = block[j] * qmat[j];
4238 if(((
unsigned)(level+threshold1))>threshold2){
4245 for(i=start_i; i<=last_non_zero; i++) {
4247 level = block[j] * qmat[j];
4251 if(((
unsigned)(level+threshold1))>threshold2){
4270 return last_non_zero;
4273 #define OFFSET(x) offsetof(MpegEncContext, x)
4274 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4278 {
"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 },
4335 .
name =
"msmpeg4v2",
4344 .priv_class = &msmpeg4v2_class,
4359 .priv_class = &msmpeg4v3_class,
4374 .priv_class = &wmv1_class,