66 uint16_t (*qmat16)[2][64],
67 const uint16_t *quant_matrix,
68 int bias,
int qmin,
int qmax,
int intra)
73 for (qscale = qmin; qscale <= qmax; qscale++) {
81 for (i = 0; i < 64; i++) {
90 (qscale * quant_matrix[j]));
97 for (i = 0; i < 64; i++) {
110 for (i = 0; i < 64; i++) {
118 (qscale * quant_matrix[j]));
122 (qscale * quant_matrix[j]);
124 if (qmat16[qscale][0][i] == 0 ||
125 qmat16[qscale][0][i] == 128 * 256)
126 qmat16[
qscale][0][i] = 128 * 256 - 1;
129 qmat16[qscale][0][i]);
133 for (i = intra; i < 64; i++) {
142 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
149 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
170 for (i = 0; i < 64; i++) {
185 for (i = 0; i < s->
mb_num; i++) {
217 "AVFrame.motion_subsample_log2 doesn't match! (%d!=%d)\n",
223 for (i = 0; i < 2; i++) {
231 2 * stride * height *
sizeof(int16_t));
244 #define COPY(a) dst->a= src->a
269 for (i = -16; i < 16; i++) {
281 int chroma_h_shift, chroma_v_shift;
290 "only YUV420 and YUV422 are supported\n");
342 "Warning keyframe interval too large! reducing it ...\n");
354 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
355 if (avctx->
flags & CODEC_FLAG_PART)
386 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
390 s->
obmc = !!(s->
flags & CODEC_FLAG_OBMC);
395 "a vbv buffer size is needed, "
396 "for encoding with a maximum bitrate\n");
402 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
419 "impossible bitrate constraints, this will fail\n");
433 "bitrate tolerance too small for bitrate\n");
444 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
445 "specified vbv buffer is too large for the given bitrate!\n");
457 "OBMC is only supported with simple mb decision\n");
461 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
474 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
477 "data partitioning not supported by codec\n");
496 "Invalid pixel aspect ratio %i/%i, limit is 255/255\n",
503 | CODEC_FLAG_ALT_SCAN
514 "mpeg2 style quantization not supported by codec\n");
532 "closed gop with scene change detection are not supported yet, "
533 "set threshold to 1000000000\n");
537 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
538 if ((s->
flags2 & CODEC_FLAG2_INTRA_VLC) &&
541 "intra vlc table not supported by codec\n");
549 "low delay forcing is only available for mpeg2\n");
554 "b frames cannot be used with low delay\n");
560 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
563 "non linear quant is only available for mpeg2\n");
567 if (avctx->
qmax > 12) {
569 "non linear quant only supports qmax <= 12 currently\n");
580 || !(s->
flags & CODEC_FLAG_H263P_SLICE_STRUCT)
584 "multi threaded encoding not supported by codec\n");
590 "automatic thread number detection not supported by codec,"
603 i = (INT_MAX / 2 + 128) >> 8;
617 "notice: b_frame_strategy only affects the first pass\n");
651 "timebase %d/%d not supported by MPEG 4 standard, "
652 "the maximum admitted value for the timebase denominator "
699 "The specified picture size of %dx%d is not valid for the "
700 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
714 "The specified picture size of %dx%d is not valid for "
715 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
716 "352x288, 704x576, and 1408x1152."
728 #if FF_API_MPEGVIDEO_GLOBAL_OPTS
729 if (avctx->
flags & CODEC_FLAG_H263P_UMV)
731 if (avctx->
flags & CODEC_FLAG_H263P_AIV)
733 if (avctx->
flags & CODEC_FLAG_H263P_SLICE_STRUCT)
855 for (i = 0; i < 64; i++) {
913 for (y = 0; y < 16; y++) {
914 for (x = 0; x < 16; x++) {
915 acc +=
FFABS(src[x + y * stride] - ref);
931 for (y = 0; y < h; y += 16) {
932 for (x = 0; x < w; x += 16) {
933 int offset = x + y *
stride;
936 int mean = (s->
dsp.
pix_sum(src + offset, stride) + 128) >> 8;
937 int sae =
get_sae(src + offset, mean, stride);
939 acc += sae + 500 < sad;
965 "Error, Invalid timestamp=%"PRId64
", "
976 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1005 for (i = 0; i < 4; i++) {
1029 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;
1040 uint8_t *src = pic_arg->
data[i];
1041 uint8_t *dst = pic->
data[i];
1046 if (src_stride == dst_stride)
1047 memcpy(dst, src, src_stride * h);
1050 memcpy(dst, src, w);
1075 int64_t score64 = 0;
1077 for (plane = 0; plane < 3; plane++) {
1079 const int bw = plane ? 1 : 2;
1080 for (y = 0; y < s->
mb_height * bw; y++) {
1081 for (x = 0; x < s->
mb_width * bw; x++) {
1083 uint8_t *dptr = p->
f.
data[plane] + 8 * (x + y *
stride) + off;
1084 uint8_t *rptr = ref->
f.
data[plane] + 8 * (x + y *
stride);
1088 case 0: score =
FFMAX(score, v);
break;
1089 case 1: score +=
FFABS(v);
break;
1090 case 2: score += v *
v;
break;
1091 case 3: score64 +=
FFABS(v * v * (int64_t)v);
break;
1092 case 4: score64 += v * v * (int64_t)(v * v);
break;
1114 int i, j, out_size, p_lambda, b_lambda,
lambda2;
1116 uint8_t *outbuf =
av_malloc(outbuf_size);
1118 int best_b_count = -1;
1120 assert(scale >= 0 && scale <= 3);
1128 b_lambda = p_lambda;
1156 input[i].
data[1] = input[i].
data[0] + ysize;
1157 input[i].
data[2] = input[i].
data[1] + csize;
1163 pre_input = *pre_input_ptr;
1194 outbuf_size, &input[0]);
1202 input[i + 1].
quality = is_p ? p_lambda : b_lambda;
1230 return best_b_count;
1261 for (i = 0; i < 4; i++)
1317 b_frames =
FFMAX(0, i - 1);
1320 for (i = 0; i < b_frames + 1; i++) {
1335 for (i = b_frames - 1; i >= 0; i--) {
1343 "warning, too many b frames in a row\n");
1366 for (i = 0; i < b_frames; i++) {
1403 for (i = 0; i < 4; i++)
1420 for (i = 0; i < 4; i++) {
1435 unsigned char *buf,
int buf_size,
void *
data)
1439 int i, stuffing_count;
1442 for (i = 0; i < context_count; i++) {
1446 uint8_t *start = buf + (size_t)(((int64_t) buf_size) * start_y / h);
1447 uint8_t *end = buf + (size_t)(((int64_t) buf_size) * end_y / h);
1516 for (i = 0; i < context_count; i++) {
1529 for (i = 0; i < 4; i++) {
1542 if (stuffing_count) {
1544 stuffing_count + 50) {
1552 while (stuffing_count--) {
1559 stuffing_count -= 4;
1560 while (stuffing_count--) {
1577 int vbv_delay, min_delay;
1586 "Internal error, negative bits\n");
1594 vbv_delay =
FFMAX(vbv_delay, min_delay);
1596 assert(vbv_delay < 0xFFFF);
1617 int n,
int threshold)
1619 static const char tab[64] = {
1620 3, 2, 2, 1, 1, 1, 1, 1,
1621 1, 1, 1, 1, 1, 1, 1, 1,
1622 1, 1, 1, 1, 1, 1, 1, 1,
1623 0, 0, 0, 0, 0, 0, 0, 0,
1624 0, 0, 0, 0, 0, 0, 0, 0,
1625 0, 0, 0, 0, 0, 0, 0, 0,
1626 0, 0, 0, 0, 0, 0, 0, 0,
1627 0, 0, 0, 0, 0, 0, 0, 0
1636 if (threshold < 0) {
1638 threshold = -threshold;
1643 if (last_index <= skip_dc - 1)
1646 for (i = 0; i <= last_index; i++) {
1650 if (skip_dc && i == 0)
1654 }
else if (level > 1) {
1660 if (score >= threshold)
1662 for (i = skip_dc; i <= last_index; i++) {
1685 for (; i <= last_index; i++) {
1687 int level = block[j];
1689 if (level > maxlevel) {
1692 }
else if (level < minlevel) {
1702 "warning, clipping %d dct coefficients to %d..%d\n",
1703 overflow, minlevel, maxlevel);
1710 for (y = 0; y < 8; y++) {
1711 for (x = 0; x < 8; x++) {
1717 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
1718 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
1719 int v = ptr[x2 + y2 *
stride];
1725 weight[x + 8 * y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
1731 int motion_x,
int motion_y,
1732 int mb_block_height,
1735 int16_t weight[8][64];
1742 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1745 for (i = 0; i < mb_block_count; i++)
1749 const int last_qp = s->
qscale;
1750 const int mb_xy = mb_x + mb_y * s->
mb_stride;
1781 (mb_y * 16 * wrap_y) + mb_x * 16;
1783 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1785 (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1787 if (mb_x * 16 + 16 > s->
width || mb_y * 16 + 16 > s->
height) {
1793 mb_block_height, mb_x * 8, mb_y * 8,
1795 ptr_cb = ebuf + 18 * wrap_y;
1797 mb_block_height, mb_x * 8, mb_y * 8,
1799 ptr_cr = ebuf + 18 * wrap_y + 8;
1804 int progressive_score, interlaced_score;
1810 NULL, wrap_y, 8) - 400;
1812 if (progressive_score > 0) {
1814 NULL, wrap_y * 2, 8) +
1816 NULL, wrap_y * 2, 8);
1817 if (progressive_score > interlaced_score) {
1820 dct_offset = wrap_y;
1841 ptr_cb + (dct_offset >> 1), wrap_c);
1843 ptr_cr + (dct_offset >> 1), wrap_c);
1849 uint8_t *dest_y, *dest_cb, *dest_cr;
1851 dest_y = s->
dest[0];
1852 dest_cb = s->
dest[1];
1853 dest_cr = s->
dest[2];
1875 int progressive_score, interlaced_score;
1882 ptr_y + wrap_y * 8, wrap_y,
1886 progressive_score -= 400;
1888 if (progressive_score > 0) {
1896 if (progressive_score > interlaced_score) {
1899 dct_offset = wrap_y;
1910 dest_y + dct_offset, wrap_y);
1912 dest_y + dct_offset + 8, wrap_y);
1922 dest_cb + (dct_offset >> 1), wrap_c);
1924 dest_cr + (dct_offset >> 1), wrap_c);
1932 wrap_y, 8) < 20 * s->
qscale)
1935 dest_y + 8, wrap_y, 8) < 20 * s->
qscale)
1938 dest_y + dct_offset, wrap_y, 8) < 20 * s->
qscale)
1940 if (s->
dsp.
sad[1](
NULL, ptr_y + dct_offset + 8,
1941 dest_y + dct_offset + 8,
1942 wrap_y, 8) < 20 * s->
qscale)
1945 wrap_c, 8) < 20 * s->
qscale)
1948 wrap_c, 8) < 20 * s->
qscale)
1951 if (s->
dsp.
sad[1](
NULL, ptr_cb + (dct_offset >> 1),
1952 dest_cb + (dct_offset >> 1),
1953 wrap_c, 8) < 20 * s->
qscale)
1955 if (s->
dsp.
sad[1](
NULL, ptr_cr + (dct_offset >> 1),
1956 dest_cr + (dct_offset >> 1),
1957 wrap_c, 8) < 20 * s->
qscale)
1984 memcpy(orig[0], s->
block[0],
sizeof(
DCTELEM) * 64 * mb_block_count);
1990 for (i = 0; i < mb_block_count; i++) {
2005 for (i = 0; i < mb_block_count; i++) {
2015 for (i = 0; i < 4; i++)
2018 for (i = 4; i < mb_block_count; i++)
2022 for (i = 0; i < mb_block_count; i++) {
2038 for (i = 0; i < mb_block_count; i++) {
2041 for (j = 63; j > 0; j--) {
2129 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2167 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2170 uint8_t *dest_backup[3];
2175 s->
pb= pb[*next_block];
2177 s->
pb2 = pb2 [*next_block];
2178 s->
tex_pb= tex_pb[*next_block];
2182 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2205 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2223 else if(w==8 && h==8)
2313 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2415 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2446 int current_packet_size, is_gob_start;
2452 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2461 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2483 current_packet_size=0;
2538 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2555 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2556 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2567 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2568 &dmin, &next_block, 0, 0);
2576 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2577 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2587 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2588 &dmin, &next_block, 0, 0);
2596 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2597 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2605 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2606 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
2616 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2617 &dmin, &next_block, 0, 0);
2628 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2629 &dmin, &next_block, 0, 0);
2640 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2641 &dmin, &next_block, 0, 0);
2647 for(dir=0; dir<2; dir++){
2654 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2655 &dmin, &next_block, 0, 0);
2663 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2664 &dmin, &next_block, 0, 0);
2675 const int last_qp= backup_s.
qscale;
2679 static const int dquant_tab[4]={-1,1,-2,2};
2681 assert(backup_s.
dquant == 0);
2687 s->
mv[0][0][0] = best_s.
mv[0][0][0];
2688 s->
mv[0][0][1] = best_s.
mv[0][0][1];
2689 s->
mv[1][0][0] = best_s.
mv[1][0][0];
2690 s->
mv[1][0][1] = best_s.
mv[1][0][1];
2693 for(; qpi<4; qpi++){
2694 int dquant= dquant_tab[qpi];
2706 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2707 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
2727 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2728 &dmin, &next_block, mx, my);
2735 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2736 &dmin, &next_block, 0, 0);
2744 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
2765 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2766 &dmin, &next_block, mx, my);
2806 int motion_x = 0, motion_y = 0;
2814 motion_x= s->
mv[0][0][0] = 0;
2815 motion_y= s->
mv[0][0][1] = 0;
2903 for(dir=0; dir<2; dir++){
2976 #define MERGE(field) dst->field += src->field; src->field=0
3003 for(i=0; i<64; i++){
3111 for(i=1; i<context_count; i++){
3139 for(i=1; i<context_count; i++){
3193 for(dir=0; dir<2; dir++){
3275 for(i=1; i<context_count; i++){
3279 for(i=1; i<context_count; i++){
3292 for(i=0; i<64; i++){
3293 int level= block[i];
3299 if(level<0) level=0;
3303 if(level>0) level=0;
3312 int qscale,
int *overflow){
3317 unsigned int threshold1, threshold2;
3329 int coeff_count[64];
3330 int qmul, qadd, start_i, last_non_zero, i, dc;
3333 uint8_t * last_length;
3341 qadd= ((qscale-1)|1)*8;
3358 block[0] = (block[0] + (q >> 1)) / q;
3376 threshold2= (threshold1<<1);
3378 for(i=63; i>=start_i; i--) {
3379 const int j = scantable[i];
3380 int level = block[j] * qmat[j];
3382 if(((
unsigned)(level+threshold1))>threshold2){
3388 for(i=start_i; i<=last_non_zero; i++) {
3389 const int j = scantable[i];
3390 int level = block[j] * qmat[j];
3394 if(((
unsigned)(level+threshold1))>threshold2){
3398 coeff[1][i]= level-1;
3402 coeff[0][i]= -
level;
3403 coeff[1][i]= -level+1;
3406 coeff_count[i]=
FFMIN(level, 2);
3407 assert(coeff_count[i]);
3410 coeff[0][i]= (level>>31)|1;
3417 if(last_non_zero < start_i){
3418 memset(block + start_i, 0, (64-start_i)*
sizeof(
DCTELEM));
3419 return last_non_zero;
3422 score_tab[start_i]= 0;
3423 survivor[0]= start_i;
3426 for(i=start_i; i<=last_non_zero; i++){
3427 int level_index, j, zero_distortion;
3428 int dct_coeff=
FFABS(block[ scantable[i] ]);
3429 int best_score=256*256*256*120;
3437 zero_distortion= dct_coeff*dct_coeff;
3439 for(level_index=0; level_index < coeff_count[i]; level_index++){
3441 int level= coeff[level_index][i];
3442 const int alevel=
FFABS(level);
3448 unquant_coeff= alevel*qmul + qadd;
3452 unquant_coeff = (int)( alevel * qscale * s->
intra_matrix[j]) >> 3;
3453 unquant_coeff = (unquant_coeff - 1) | 1;
3455 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[j])) >> 4;
3456 unquant_coeff = (unquant_coeff - 1) | 1;
3461 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3463 if((level&(~127)) == 0){
3464 for(j=survivor_count-1; j>=0; j--){
3465 int run= i - survivor[j];
3467 score += score_tab[i-
run];
3469 if(score < best_score){
3472 level_tab[i+1]= level-64;
3477 for(j=survivor_count-1; j>=0; j--){
3478 int run= i - survivor[j];
3480 score += score_tab[i-
run];
3481 if(score < last_score){
3484 last_level= level-64;
3490 distortion += esc_length*
lambda;
3491 for(j=survivor_count-1; j>=0; j--){
3492 int run= i - survivor[j];
3493 int score= distortion + score_tab[i-
run];
3495 if(score < best_score){
3498 level_tab[i+1]= level-64;
3503 for(j=survivor_count-1; j>=0; j--){
3504 int run= i - survivor[j];
3505 int score= distortion + score_tab[i-
run];
3506 if(score < last_score){
3509 last_level= level-64;
3517 score_tab[i+1]= best_score;
3520 if(last_non_zero <= 27){
3521 for(; survivor_count; survivor_count--){
3522 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3526 for(; survivor_count; survivor_count--){
3527 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3532 survivor[ survivor_count++ ]= i+1;
3536 last_score= 256*256*256*120;
3537 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3538 int score= score_tab[i];
3539 if(i) score += lambda*2;
3541 if(score < last_score){
3544 last_level= level_tab[i];
3545 last_run= run_tab[i];
3552 dc=
FFABS(block[0]);
3553 last_non_zero= last_i - 1;
3554 memset(block + start_i, 0, (64-start_i)*
sizeof(
DCTELEM));
3556 if(last_non_zero < start_i)
3557 return last_non_zero;
3559 if(last_non_zero == 0 && start_i == 0){
3561 int best_score= dc * dc;
3563 for(i=0; i<coeff_count[0]; i++){
3564 int level= coeff[i][0];
3565 int alevel=
FFABS(level);
3566 int unquant_coeff, score, distortion;
3569 unquant_coeff= (alevel*qmul + qadd)>>3;
3571 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[0])) >> 4;
3572 unquant_coeff = (unquant_coeff - 1) | 1;
3574 unquant_coeff = (unquant_coeff + 4) >> 3;
3575 unquant_coeff<<= 3 + 3;
3577 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
3580 else score= distortion + esc_length*
lambda;
3582 if(score < best_score){
3584 best_level= level - 64;
3587 block[0]= best_level;
3589 if(best_level == 0)
return -1;
3590 else return last_non_zero;
3596 block[ perm_scantable[last_non_zero] ]= last_level;
3599 for(; i>start_i; i -= run_tab[i] + 1){
3600 block[ perm_scantable[i-1] ]= level_tab[i];
3603 return last_non_zero;
3618 int perm_index= perm[
index];
3619 if(i==0) s*= sqrt(0.5);
3620 if(j==0) s*= sqrt(0.5);
3621 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)));
3640 int qmul, qadd, start_i, last_non_zero, i, dc;
3642 uint8_t * last_length;
3644 int rle_index,
run, q = 1, sum;
3647 static int after_last=0;
3648 static int to_zero=0;
3649 static int from_zero=0;
3652 static int messed_sign=0;
3655 if(basis[0][0] == 0)
3692 for(i=0; i<64; i++){
3699 for(i=0; i<64; i++){
3704 w=
FFABS(weight[i]) + qns*one;
3705 w= 15 + (48*qns*one + w/2)/w;
3720 for(i=start_i; i<=last_non_zero; i++){
3721 int j= perm_scantable[i];
3722 const int level= block[j];
3726 if(level<0) coeff= qmul*level - qadd;
3727 else coeff= qmul*level + qadd;
3728 run_tab[rle_index++]=
run;
3737 if(last_non_zero>0){
3748 int run2, best_unquant_change=0, analyze_gradient;
3754 if(analyze_gradient){
3758 for(i=0; i<64; i++){
3774 const int level= block[0];
3775 int change, old_coeff;
3781 for(change=-1; change<=1; change+=2){
3782 int new_level= level + change;
3783 int score, new_coeff;
3785 new_coeff= q*new_level;
3786 if(new_coeff >= 2048 || new_coeff < 0)
3789 score= s->
dsp.
try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
3790 if(score<best_score){
3793 best_change= change;
3794 best_unquant_change= new_coeff - old_coeff;
3801 run2= run_tab[rle_index++];
3805 for(i=start_i; i<64; i++){
3806 int j= perm_scantable[i];
3807 const int level= block[j];
3808 int change, old_coeff;
3814 if(level<0) old_coeff= qmul*level - qadd;
3815 else old_coeff= qmul*level + qadd;
3816 run2= run_tab[rle_index++];
3820 assert(run2>=0 || i >= last_non_zero );
3823 for(change=-1; change<=1; change+=2){
3824 int new_level= level + change;
3825 int score, new_coeff, unquant_change;
3832 if(new_level<0) new_coeff= qmul*new_level - qadd;
3833 else new_coeff= qmul*new_level + qadd;
3834 if(new_coeff >= 2048 || new_coeff <= -2048)
3839 if(level < 63 && level > -63){
3840 if(i < last_non_zero)
3848 assert(
FFABS(new_level)==1);
3850 if(analyze_gradient){
3851 int g= d1[ scantable[i] ];
3852 if(g && (g^new_level) >= 0)
3856 if(i < last_non_zero){
3857 int next_i= i + run2 + 1;
3858 int next_level= block[ perm_scantable[next_i] ] + 64;
3860 if(next_level&(~127))
3863 if(next_i < last_non_zero)
3881 assert(
FFABS(level)==1);
3883 if(i < last_non_zero){
3884 int next_i= i + run2 + 1;
3885 int next_level= block[ perm_scantable[next_i] ] + 64;
3887 if(next_level&(~127))
3890 if(next_i < last_non_zero)
3909 unquant_change= new_coeff - old_coeff;
3910 assert((score < 100*lambda && score > -100*lambda) || lambda==0);
3913 if(score<best_score){
3916 best_change= change;
3917 best_unquant_change= unquant_change;
3921 prev_level= level + 64;
3922 if(prev_level&(~127))
3935 int j= perm_scantable[ best_coeff ];
3937 block[j] += best_change;
3939 if(best_coeff > last_non_zero){
3940 last_non_zero= best_coeff;
3948 if(block[j] - best_change){
3949 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
3961 for(; last_non_zero>=start_i; last_non_zero--){
3962 if(block[perm_scantable[last_non_zero]])
3968 if(256*256*256*64 % count == 0){
3969 printf(
"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);
3974 for(i=start_i; i<=last_non_zero; i++){
3975 int j= perm_scantable[i];
3976 const int level= block[j];
3979 run_tab[rle_index++]=
run;
3992 if(last_non_zero>0){
3998 return last_non_zero;
4003 int qscale,
int *overflow)
4005 int i, j,
level, last_non_zero, q, start_i;
4010 unsigned int threshold1, threshold2;
4029 block[0] = (block[0] + (q >> 1)) / q;
4041 threshold2= (threshold1<<1);
4042 for(i=63;i>=start_i;i--) {
4044 level = block[j] * qmat[j];
4046 if(((
unsigned)(level+threshold1))>threshold2){
4053 for(i=start_i; i<=last_non_zero; i++) {
4055 level = block[j] * qmat[j];
4059 if(((
unsigned)(level+threshold1))>threshold2){
4078 return last_non_zero;
4081 #define OFFSET(x) offsetof(MpegEncContext, x)
4082 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4138 .
name =
"msmpeg4v2",