70 uint16_t (*qmat16)[2][64],
71 const uint16_t *quant_matrix,
72 int bias,
int qmin,
int qmax,
int intra)
77 for (qscale = qmin; qscale <= qmax; qscale++) {
82 for (i = 0; i < 64; i++) {
91 (qscale * quant_matrix[j]));
94 for (i = 0; i < 64; i++) {
106 for (i = 0; i < 64; i++) {
114 (qscale * quant_matrix[j]));
118 (qscale * quant_matrix[j]);
120 if (qmat16[qscale][0][i] == 0 ||
121 qmat16[qscale][0][i] == 128 * 256)
122 qmat16[
qscale][0][i] = 128 * 256 - 1;
125 qmat16[qscale][0][i]);
129 for (i = intra; i < 64; i++) {
134 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
141 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
162 for (i = 0; i < 64; i++) {
177 for (i = 0; i < s->
mb_num; i++) {
201 #define COPY(a) dst->a= src->a
226 for (i = -16; i < 16; i++) {
253 int chroma_h_shift, chroma_v_shift;
262 "only YUV420 and YUV422 are supported\n");
323 "keyframe interval too large!, reducing it from %d to %d\n",
390 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
397 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
414 "impossible bitrate constraints, this will fail\n");
428 "bitrate tolerance too small for bitrate\n");
439 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
440 "specified vbv buffer is too large for the given bitrate!\n");
452 "OBMC is only supported with simple mb decision\n");
475 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
483 (avctx->
width > 2048 ||
490 ((avctx->
width &3) ||
497 (avctx->
width > 4095 ||
504 (avctx->
width > 16383 ||
505 avctx->
height > 16383 )) {
506 av_log(avctx,
AV_LOG_ERROR,
"MPEG-2 does not support resolutions above 16383x16383\n");
540 "mpeg2 style quantization not supported by codec\n");
558 "closed gop with scene change detection are not supported yet, "
559 "set threshold to 1000000000\n");
566 "low delay forcing is only available for mpeg2\n");
571 "b frames cannot be used with low delay\n");
577 if (avctx->
qmax > 12) {
579 "non linear quant only supports qmax <= 12 currently\n");
591 "multi threaded encoding not supported by codec\n");
597 "automatic thread number detection not supported by codec, "
613 i = (INT_MAX / 2 + 128) >> 8;
622 "notice: b_frame_strategy only affects the first pass\n");
656 "timebase %d/%d not supported by MPEG 4 standard, "
657 "the maximum admitted value for the timebase denominator "
699 if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
706 if (!CONFIG_H261_ENCODER)
710 "The specified picture size of %dx%d is not valid for the "
711 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
720 if (!CONFIG_H263_ENCODER)
725 "The specified picture size of %dx%d is not valid for "
726 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
727 "352x288, 704x576, and 1408x1152. "
833 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
847 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
852 for (i = 0; i < 64; i++) {
896 if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
910 for (y = 0; y < 16; y++) {
911 for (x = 0; x < 16; x++) {
912 acc +=
FFABS(src[x + y * stride] - ref);
928 for (y = 0; y < h; y += 16) {
929 for (x = 0; x < w; x += 16) {
933 int mean = (s->
dsp.
pix_sum(src + offset, stride) + 128) >> 8;
934 int sae =
get_sae(src + offset, mean, stride);
936 acc += sae + 500 < sad;
947 int i, display_picture_number = 0,
ret;
963 "Error, Invalid timestamp=%"PRId64
", "
968 if (!s->
low_delay && display_picture_number == 1)
977 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
980 pts = display_picture_number;
986 if (!pic_arg->
buf[0])
1028 int h_chroma_shift, v_chroma_shift;
1033 for (i = 0; i < 3; i++) {
1034 int src_stride = pic_arg->
linesize[i];
1036 int h_shift = i ? h_chroma_shift : 0;
1037 int v_shift = i ? v_chroma_shift : 0;
1038 int w = s->
width >> h_shift;
1039 int h = s->
height >> v_shift;
1044 h = ((s->
height + 15)/16*16) >> v_shift;
1050 if (src_stride == dst_stride)
1051 memcpy(dst, src, src_stride * h);
1056 memcpy(dst2, src, w);
1089 int64_t score64 = 0;
1091 for (plane = 0; plane < 3; plane++) {
1093 const int bw = plane ? 1 : 2;
1094 for (y = 0; y < s->
mb_height * bw; y++) {
1095 for (x = 0; x < s->
mb_width * bw; x++) {
1102 case 0: score =
FFMAX(score, v);
break;
1103 case 1: score +=
FFABS(v);
break;
1104 case 2: score += v *
v;
break;
1105 case 3: score64 +=
FFABS(v * v * (int64_t)v);
break;
1106 case 4: score64 += v * v * (int64_t)(v * v);
break;
1125 int ret, got_output;
1143 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1144 int64_t best_rd = INT64_MAX;
1145 int best_b_count = -1;
1155 b_lambda = p_lambda;
1183 input[i].
data[1] = input[i].
data[0] + ysize;
1184 input[i].
data[2] = input[i].
data[1] + csize;
1190 pre_input = *pre_input_ptr;
1192 if (!pre_input.
shared && i) {
1230 input[i + 1].
quality = is_p ? p_lambda : b_lambda;
1258 return best_b_count;
1331 b_frames =
FFMAX(0, i - 1);
1334 for (i = 0; i < b_frames + 1; i++) {
1346 for (i = b_frames - 1; i >= 0; i--) {
1354 "warning, too many b frames in a row\n");
1377 for (i = 0; i < b_frames; i++) {
1422 for (i = 0; i < 4; i++) {
1439 AVFrame *pic_arg,
int *got_packet)
1442 int i, stuffing_count,
ret;
1465 for (i = 0; i < context_count; i++) {
1526 for (i = 0; i < context_count; i++) {
1539 for (i = 0; i < 4; i++) {
1553 if (stuffing_count) {
1555 stuffing_count + 50) {
1563 while (stuffing_count--) {
1570 stuffing_count -= 4;
1571 while (stuffing_count--) {
1597 "Internal error, negative bits\n");
1605 vbv_delay =
FFMAX(vbv_delay, min_delay);
1638 *got_packet = !!pkt->
size;
1643 int n,
int threshold)
1645 static const char tab[64] = {
1646 3, 2, 2, 1, 1, 1, 1, 1,
1647 1, 1, 1, 1, 1, 1, 1, 1,
1648 1, 1, 1, 1, 1, 1, 1, 1,
1649 0, 0, 0, 0, 0, 0, 0, 0,
1650 0, 0, 0, 0, 0, 0, 0, 0,
1651 0, 0, 0, 0, 0, 0, 0, 0,
1652 0, 0, 0, 0, 0, 0, 0, 0,
1653 0, 0, 0, 0, 0, 0, 0, 0
1662 if (threshold < 0) {
1664 threshold = -threshold;
1669 if (last_index <= skip_dc - 1)
1672 for (i = 0; i <= last_index; i++) {
1676 if (skip_dc && i == 0)
1680 }
else if (level > 1) {
1686 if (score >= threshold)
1688 for (i = skip_dc; i <= last_index; i++) {
1711 for (; i <= last_index; i++) {
1713 int level = block[j];
1715 if (level > maxlevel) {
1718 }
else if (level < minlevel) {
1728 "warning, clipping %d dct coefficients to %d..%d\n",
1729 overflow, minlevel, maxlevel);
1736 for (y = 0; y < 8; y++) {
1737 for (x = 0; x < 8; x++) {
1743 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
1744 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
1745 int v = ptr[x2 + y2 *
stride];
1751 weight[x + 8 *
y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
1757 int motion_x,
int motion_y,
1758 int mb_block_height,
1763 int16_t orig[12][64];
1770 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1773 for (i = 0; i < mb_block_count; i++)
1777 const int last_qp = s->
qscale;
1778 const int mb_xy = mb_x + mb_y * s->
mb_stride;
1809 (mb_y * 16 * wrap_y) + mb_x * 16;
1811 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1813 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1823 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1825 ptr_cb = ebuf + 18 * wrap_y;
1827 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1829 ptr_cr = ebuf + 18 * wrap_y + 16;
1834 int progressive_score, interlaced_score;
1840 NULL, wrap_y, 8) - 400;
1842 if (progressive_score > 0) {
1844 NULL, wrap_y * 2, 8) +
1846 NULL, wrap_y * 2, 8);
1847 if (progressive_score > interlaced_score) {
1850 dct_offset = wrap_y;
1851 uv_dct_offset = wrap_c;
1886 uint8_t *dest_y, *dest_cb, *dest_cr;
1888 dest_y = s->
dest[0];
1889 dest_cb = s->
dest[1];
1890 dest_cr = s->
dest[2];
1914 int progressive_score, interlaced_score;
1921 ptr_y + wrap_y * 8, wrap_y,
1925 progressive_score -= 400;
1927 if (progressive_score > 0) {
1935 if (progressive_score > interlaced_score) {
1938 dct_offset = wrap_y;
1939 uv_dct_offset = wrap_c;
1950 dest_y + dct_offset, wrap_y);
1952 dest_y + dct_offset + 8, wrap_y);
1962 dest_cb + uv_dct_offset, wrap_c);
1964 dest_cr + uv_dct_offset, wrap_c);
1972 wrap_y, 8) < 20 * s->
qscale)
1975 dest_y + 8, wrap_y, 8) < 20 * s->
qscale)
1978 dest_y + dct_offset, wrap_y, 8) < 20 * s->
qscale)
1980 if (s->
dsp.
sad[1](
NULL, ptr_y + dct_offset + 8,
1981 dest_y + dct_offset + 8,
1982 wrap_y, 8) < 20 * s->
qscale)
1985 wrap_c, 8) < 20 * s->
qscale)
1988 wrap_c, 8) < 20 * s->
qscale)
1991 if (s->
dsp.
sad[1](
NULL, ptr_cb + uv_dct_offset,
1992 dest_cb + uv_dct_offset,
1993 wrap_c, 8) < 20 * s->
qscale)
1995 if (s->
dsp.
sad[1](
NULL, ptr_cr + uv_dct_offset,
1996 dest_cr + uv_dct_offset,
1997 wrap_c, 8) < 20 * s->
qscale)
2024 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2030 for (i = 0; i < mb_block_count; i++) {
2045 for (i = 0; i < mb_block_count; i++) {
2055 for (i = 0; i < 4; i++)
2058 for (i = 4; i < mb_block_count; i++)
2062 for (i = 0; i < mb_block_count; i++) {
2075 for (i=6; i<12; i++) {
2084 for (i = 0; i < mb_block_count; i++) {
2087 for (j = 63; j > 0; j--) {
2100 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2104 if (CONFIG_MPEG4_ENCODER)
2114 if (CONFIG_WMV2_ENCODER)
2118 if (CONFIG_H261_ENCODER)
2126 if (CONFIG_H263_ENCODER)
2131 if (CONFIG_MJPEG_ENCODER)
2177 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2215 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2223 s->
pb= pb[*next_block];
2225 s->
pb2 = pb2 [*next_block];
2226 s->
tex_pb= tex_pb[*next_block];
2230 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2253 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2271 else if(w==8 && h==8)
2361 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2403 if (CONFIG_H263_ENCODER)
2405 bytestream_put_le32(&ptr, offset);
2406 bytestream_put_byte(&ptr, s->
qscale);
2407 bytestream_put_byte(&ptr, gobn);
2408 bytestream_put_le16(&ptr, mba);
2409 bytestream_put_byte(&ptr, pred_x);
2410 bytestream_put_byte(&ptr, pred_y);
2412 bytestream_put_byte(&ptr, 0);
2413 bytestream_put_byte(&ptr, 0);
2489 if (CONFIG_H263_ENCODER)
2509 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2540 int current_packet_size, is_gob_start;
2546 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2555 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2560 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2579 current_packet_size=0;
2593 if (CONFIG_MPEG4_ENCODER) {
2600 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2607 if (CONFIG_H263_ENCODER)
2637 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2654 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2655 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2666 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2667 &dmin, &next_block, 0, 0);
2675 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2676 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2686 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2687 &dmin, &next_block, 0, 0);
2695 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2696 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2704 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2705 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
2715 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2716 &dmin, &next_block, 0, 0);
2727 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2728 &dmin, &next_block, 0, 0);
2739 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2740 &dmin, &next_block, 0, 0);
2746 for(dir=0; dir<2; dir++){
2753 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2754 &dmin, &next_block, 0, 0);
2762 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2763 &dmin, &next_block, 0, 0);
2774 const int last_qp= backup_s.
qscale;
2778 static const int dquant_tab[4]={-1,1,-2,2};
2787 s->
mv[0][0][0] = best_s.
mv[0][0][0];
2788 s->
mv[0][0][1] = best_s.
mv[0][0][1];
2789 s->
mv[1][0][0] = best_s.
mv[1][0][0];
2790 s->
mv[1][0][1] = best_s.
mv[1][0][1];
2793 for(; qpi<4; qpi++){
2794 int dquant= dquant_tab[qpi];
2806 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2807 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
2827 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2828 &dmin, &next_block, mx, my);
2835 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2836 &dmin, &next_block, 0, 0);
2844 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
2865 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2866 &dmin, &next_block, mx, my);
2893 if (CONFIG_H263_ENCODER &&
2906 int motion_x = 0, motion_y = 0;
2914 motion_x= s->
mv[0][0][0] = 0;
2915 motion_y= s->
mv[0][0][1] = 0;
2943 if (CONFIG_MPEG4_ENCODER) {
2952 if (CONFIG_MPEG4_ENCODER) {
3002 for(dir=0; dir<2; dir++){
3019 if (CONFIG_H263_ENCODER &&
3076 #define MERGE(field) dst->field += src->field; src->field=0
3103 for(i=0; i<64; i++){
3130 if (CONFIG_MPEG4_ENCODER)
3136 if (CONFIG_H263_ENCODER)
3217 for(i=1; i<context_count; i++){
3247 for(i=1; i<context_count; i++){
3260 av_dlog(s,
"Scene change detected, encoding as I Frame %d %d\n",
3304 for(dir=0; dir<2; dir++){
3339 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};
3340 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};
3371 if (CONFIG_MJPEG_ENCODER)
3375 if (CONFIG_H261_ENCODER)
3383 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred)
3391 else if (CONFIG_H263_ENCODER)
3395 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3404 for(i=1; i<context_count; i++){
3408 for(i=1; i<context_count; i++){
3421 for(i=0; i<64; i++){
3422 int level= block[i];
3428 if(level<0) level=0;
3432 if(level>0) level=0;
3441 int qscale,
int *overflow){
3446 unsigned int threshold1, threshold2;
3458 int coeff_count[64];
3459 int qmul, qadd, start_i, last_non_zero, i,
dc;
3470 qadd= ((qscale-1)|1)*8;
3487 block[0] = (block[0] + (q >> 1)) / q;
3505 threshold2= (threshold1<<1);
3507 for(i=63; i>=start_i; i--) {
3508 const int j = scantable[i];
3509 int level = block[j] * qmat[j];
3511 if(((
unsigned)(level+threshold1))>threshold2){
3517 for(i=start_i; i<=last_non_zero; i++) {
3518 const int j = scantable[i];
3519 int level = block[j] * qmat[j];
3523 if(((
unsigned)(level+threshold1))>threshold2){
3527 coeff[1][i]= level-1;
3531 coeff[0][i]= -
level;
3532 coeff[1][i]= -level+1;
3535 coeff_count[i]=
FFMIN(level, 2);
3539 coeff[0][i]= (level>>31)|1;
3546 if(last_non_zero < start_i){
3547 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3548 return last_non_zero;
3551 score_tab[start_i]= 0;
3552 survivor[0]= start_i;
3555 for(i=start_i; i<=last_non_zero; i++){
3556 int level_index, j, zero_distortion;
3557 int dct_coeff=
FFABS(block[ scantable[i] ]);
3558 int best_score=256*256*256*120;
3562 zero_distortion= dct_coeff*dct_coeff;
3564 for(level_index=0; level_index < coeff_count[i]; level_index++){
3566 int level= coeff[level_index][i];
3567 const int alevel=
FFABS(level);
3573 unquant_coeff= alevel*qmul + qadd;
3577 unquant_coeff = (int)( alevel * qscale * s->
intra_matrix[j]) >> 3;
3578 unquant_coeff = (unquant_coeff - 1) | 1;
3580 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[j])) >> 4;
3581 unquant_coeff = (unquant_coeff - 1) | 1;
3586 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3588 if((level&(~127)) == 0){
3589 for(j=survivor_count-1; j>=0; j--){
3590 int run= i - survivor[j];
3592 score += score_tab[i-
run];
3594 if(score < best_score){
3597 level_tab[i+1]= level-64;
3602 for(j=survivor_count-1; j>=0; j--){
3603 int run= i - survivor[j];
3605 score += score_tab[i-
run];
3606 if(score < last_score){
3609 last_level= level-64;
3615 distortion += esc_length*
lambda;
3616 for(j=survivor_count-1; j>=0; j--){
3617 int run= i - survivor[j];
3618 int score= distortion + score_tab[i-
run];
3620 if(score < best_score){
3623 level_tab[i+1]= level-64;
3628 for(j=survivor_count-1; j>=0; j--){
3629 int run= i - survivor[j];
3630 int score= distortion + score_tab[i-
run];
3631 if(score < last_score){
3634 last_level= level-64;
3642 score_tab[i+1]= best_score;
3645 if(last_non_zero <= 27){
3646 for(; survivor_count; survivor_count--){
3647 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3651 for(; survivor_count; survivor_count--){
3652 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3657 survivor[ survivor_count++ ]= i+1;
3661 last_score= 256*256*256*120;
3662 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3663 int score= score_tab[i];
3664 if(i) score += lambda*2;
3666 if(score < last_score){
3669 last_level= level_tab[i];
3670 last_run= run_tab[i];
3677 dc=
FFABS(block[0]);
3678 last_non_zero= last_i - 1;
3679 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3681 if(last_non_zero < start_i)
3682 return last_non_zero;
3684 if(last_non_zero == 0 && start_i == 0){
3686 int best_score= dc *
dc;
3688 for(i=0; i<coeff_count[0]; i++){
3689 int level= coeff[i][0];
3690 int alevel=
FFABS(level);
3691 int unquant_coeff, score, distortion;
3694 unquant_coeff= (alevel*qmul + qadd)>>3;
3696 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[0])) >> 4;
3697 unquant_coeff = (unquant_coeff - 1) | 1;
3699 unquant_coeff = (unquant_coeff + 4) >> 3;
3700 unquant_coeff<<= 3 + 3;
3702 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
3705 else score= distortion + esc_length*
lambda;
3707 if(score < best_score){
3709 best_level= level - 64;
3712 block[0]= best_level;
3714 if(best_level == 0)
return -1;
3715 else return last_non_zero;
3721 block[ perm_scantable[last_non_zero] ]= last_level;
3724 for(; i>start_i; i -= run_tab[i] + 1){
3725 block[ perm_scantable[i-1] ]= level_tab[i];
3728 return last_non_zero;
3743 int perm_index= perm[
index];
3744 if(i==0) s*= sqrt(0.5);
3745 if(j==0) s*= sqrt(0.5);
3746 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)));
3765 int qmul, qadd, start_i, last_non_zero, i,
dc;
3769 int rle_index,
run, q = 1, sum;
3772 static int after_last=0;
3773 static int to_zero=0;
3774 static int from_zero=0;
3777 static int messed_sign=0;
3780 if(basis[0][0] == 0)
3817 for(i=0; i<64; i++){
3824 for(i=0; i<64; i++){
3829 w=
FFABS(weight[i]) + qns*one;
3830 w= 15 + (48*qns*one + w/2)/w;
3845 for(i=start_i; i<=last_non_zero; i++){
3846 int j= perm_scantable[i];
3847 const int level= block[j];
3851 if(level<0) coeff= qmul*level - qadd;
3852 else coeff= qmul*level + qadd;
3853 run_tab[rle_index++]=
run;
3862 if(last_non_zero>0){
3873 int run2, best_unquant_change=0, analyze_gradient;
3879 if(analyze_gradient){
3883 for(i=0; i<64; i++){
3899 const int level= block[0];
3900 int change, old_coeff;
3906 for(change=-1; change<=1; change+=2){
3907 int new_level= level + change;
3908 int score, new_coeff;
3910 new_coeff= q*new_level;
3911 if(new_coeff >= 2048 || new_coeff < 0)
3914 score= s->
dsp.
try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
3915 if(score<best_score){
3918 best_change= change;
3919 best_unquant_change= new_coeff - old_coeff;
3926 run2= run_tab[rle_index++];
3930 for(i=start_i; i<64; i++){
3931 int j= perm_scantable[i];
3932 const int level= block[j];
3933 int change, old_coeff;
3939 if(level<0) old_coeff= qmul*level - qadd;
3940 else old_coeff= qmul*level + qadd;
3941 run2= run_tab[rle_index++];
3948 for(change=-1; change<=1; change+=2){
3949 int new_level= level + change;
3950 int score, new_coeff, unquant_change;
3957 if(new_level<0) new_coeff= qmul*new_level - qadd;
3958 else new_coeff= qmul*new_level + qadd;
3959 if(new_coeff >= 2048 || new_coeff <= -2048)
3964 if(level < 63 && level > -63){
3965 if(i < last_non_zero)
3975 if(analyze_gradient){
3976 int g= d1[ scantable[i] ];
3977 if(g && (g^new_level) >= 0)
3981 if(i < last_non_zero){
3982 int next_i= i + run2 + 1;
3983 int next_level= block[ perm_scantable[next_i] ] + 64;
3985 if(next_level&(~127))
3988 if(next_i < last_non_zero)
4008 if(i < last_non_zero){
4009 int next_i= i + run2 + 1;
4010 int next_level= block[ perm_scantable[next_i] ] + 64;
4012 if(next_level&(~127))
4015 if(next_i < last_non_zero)
4034 unquant_change= new_coeff - old_coeff;
4035 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4038 if(score<best_score){
4041 best_change= change;
4042 best_unquant_change= unquant_change;
4046 prev_level= level + 64;
4047 if(prev_level&(~127))
4060 int j= perm_scantable[ best_coeff ];
4062 block[j] += best_change;
4064 if(best_coeff > last_non_zero){
4065 last_non_zero= best_coeff;
4073 if(block[j] - best_change){
4074 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
4086 for(; last_non_zero>=start_i; last_non_zero--){
4087 if(block[perm_scantable[last_non_zero]])
4093 if(256*256*256*64 % count == 0){
4094 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);
4099 for(i=start_i; i<=last_non_zero; i++){
4100 int j= perm_scantable[i];
4101 const int level= block[j];
4104 run_tab[rle_index++]=
run;
4117 if(last_non_zero>0){
4123 return last_non_zero;
4128 int qscale,
int *overflow)
4130 int i, j,
level, last_non_zero, q, start_i;
4135 unsigned int threshold1, threshold2;
4154 block[0] = (block[0] + (q >> 1)) / q;
4166 threshold2= (threshold1<<1);
4167 for(i=63;i>=start_i;i--) {
4169 level = block[j] * qmat[j];
4171 if(((
unsigned)(level+threshold1))>threshold2){
4178 for(i=start_i; i<=last_non_zero; i++) {
4180 level = block[j] * qmat[j];
4184 if(((
unsigned)(level+threshold1))>threshold2){
4203 return last_non_zero;