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");
541 "mpeg2 style quantization not supported by codec\n");
559 "closed gop with scene change detection are not supported yet, "
560 "set threshold to 1000000000\n");
567 "low delay forcing is only available for mpeg2\n");
572 "b frames cannot be used with low delay\n");
578 if (avctx->
qmax > 12) {
580 "non linear quant only supports qmax <= 12 currently\n");
592 "multi threaded encoding not supported by codec\n");
598 "automatic thread number detection not supported by codec, "
614 i = (INT_MAX / 2 + 128) >> 8;
623 "notice: b_frame_strategy only affects the first pass\n");
657 "timebase %d/%d not supported by MPEG 4 standard, "
658 "the maximum admitted value for the timebase denominator "
700 if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
707 if (!CONFIG_H261_ENCODER)
711 "The specified picture size of %dx%d is not valid for the "
712 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
721 if (!CONFIG_H263_ENCODER)
726 "The specified picture size of %dx%d is not valid for "
727 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
728 "352x288, 704x576, and 1408x1152. "
834 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->
modified_quant)
848 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
853 for (i = 0; i < 64; i++) {
897 if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
911 for (y = 0; y < 16; y++) {
912 for (x = 0; x < 16; x++) {
913 acc +=
FFABS(src[x + y * stride] - ref);
929 for (y = 0; y < h; y += 16) {
930 for (x = 0; x < w; x += 16) {
934 int mean = (s->
dsp.
pix_sum(src + offset, stride) + 128) >> 8;
935 int sae =
get_sae(src + offset, mean, stride);
937 acc += sae + 500 < sad;
948 int i, display_picture_number = 0,
ret;
963 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
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);
1645 *got_packet = !!pkt->
size;
1650 int n,
int threshold)
1652 static const char tab[64] = {
1653 3, 2, 2, 1, 1, 1, 1, 1,
1654 1, 1, 1, 1, 1, 1, 1, 1,
1655 1, 1, 1, 1, 1, 1, 1, 1,
1656 0, 0, 0, 0, 0, 0, 0, 0,
1657 0, 0, 0, 0, 0, 0, 0, 0,
1658 0, 0, 0, 0, 0, 0, 0, 0,
1659 0, 0, 0, 0, 0, 0, 0, 0,
1660 0, 0, 0, 0, 0, 0, 0, 0
1669 if (threshold < 0) {
1671 threshold = -threshold;
1676 if (last_index <= skip_dc - 1)
1679 for (i = 0; i <= last_index; i++) {
1683 if (skip_dc && i == 0)
1687 }
else if (level > 1) {
1693 if (score >= threshold)
1695 for (i = skip_dc; i <= last_index; i++) {
1718 for (; i <= last_index; i++) {
1720 int level = block[j];
1722 if (level > maxlevel) {
1725 }
else if (level < minlevel) {
1735 "warning, clipping %d dct coefficients to %d..%d\n",
1736 overflow, minlevel, maxlevel);
1743 for (y = 0; y < 8; y++) {
1744 for (x = 0; x < 8; x++) {
1750 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
1751 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
1752 int v = ptr[x2 + y2 *
stride];
1758 weight[x + 8 *
y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
1764 int motion_x,
int motion_y,
1765 int mb_block_height,
1770 int16_t orig[12][64];
1777 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1780 for (i = 0; i < mb_block_count; i++)
1784 const int last_qp = s->
qscale;
1785 const int mb_xy = mb_x + mb_y * s->
mb_stride;
1816 (mb_y * 16 * wrap_y) + mb_x * 16;
1818 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1820 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1830 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1832 ptr_cb = ebuf + 18 * wrap_y;
1834 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1836 ptr_cr = ebuf + 18 * wrap_y + 16;
1841 int progressive_score, interlaced_score;
1847 NULL, wrap_y, 8) - 400;
1849 if (progressive_score > 0) {
1851 NULL, wrap_y * 2, 8) +
1853 NULL, wrap_y * 2, 8);
1854 if (progressive_score > interlaced_score) {
1857 dct_offset = wrap_y;
1858 uv_dct_offset = wrap_c;
1893 uint8_t *dest_y, *dest_cb, *dest_cr;
1895 dest_y = s->
dest[0];
1896 dest_cb = s->
dest[1];
1897 dest_cr = s->
dest[2];
1921 int progressive_score, interlaced_score;
1928 ptr_y + wrap_y * 8, wrap_y,
1932 progressive_score -= 400;
1934 if (progressive_score > 0) {
1942 if (progressive_score > interlaced_score) {
1945 dct_offset = wrap_y;
1946 uv_dct_offset = wrap_c;
1957 dest_y + dct_offset, wrap_y);
1959 dest_y + dct_offset + 8, wrap_y);
1969 dest_cb + uv_dct_offset, wrap_c);
1971 dest_cr + uv_dct_offset, wrap_c);
1978 if (s->
dsp.
sad[1](NULL, ptr_y , dest_y,
1979 wrap_y, 8) < 20 * s->
qscale)
1981 if (s->
dsp.
sad[1](NULL, ptr_y + 8,
1982 dest_y + 8, wrap_y, 8) < 20 * s->
qscale)
1984 if (s->
dsp.
sad[1](NULL, ptr_y + dct_offset,
1985 dest_y + dct_offset, wrap_y, 8) < 20 * s->
qscale)
1987 if (s->
dsp.
sad[1](NULL, ptr_y + dct_offset + 8,
1988 dest_y + dct_offset + 8,
1989 wrap_y, 8) < 20 * s->
qscale)
1991 if (s->
dsp.
sad[1](NULL, ptr_cb, dest_cb,
1992 wrap_c, 8) < 20 * s->
qscale)
1994 if (s->
dsp.
sad[1](NULL, ptr_cr, dest_cr,
1995 wrap_c, 8) < 20 * s->
qscale)
1998 if (s->
dsp.
sad[1](NULL, ptr_cb + uv_dct_offset,
1999 dest_cb + uv_dct_offset,
2000 wrap_c, 8) < 20 * s->
qscale)
2002 if (s->
dsp.
sad[1](NULL, ptr_cr + uv_dct_offset,
2003 dest_cr + uv_dct_offset,
2004 wrap_c, 8) < 20 * s->
qscale)
2031 memcpy(orig[0], s->
block[0],
sizeof(int16_t) * 64 * mb_block_count);
2037 for (i = 0; i < mb_block_count; i++) {
2052 for (i = 0; i < mb_block_count; i++) {
2062 for (i = 0; i < 4; i++)
2065 for (i = 4; i < mb_block_count; i++)
2069 for (i = 0; i < mb_block_count; i++) {
2082 for (i=6; i<12; i++) {
2091 for (i = 0; i < mb_block_count; i++) {
2094 for (j = 63; j > 0; j--) {
2107 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2111 if (CONFIG_MPEG4_ENCODER)
2121 if (CONFIG_WMV2_ENCODER)
2125 if (CONFIG_H261_ENCODER)
2133 if (CONFIG_H263_ENCODER)
2138 if (CONFIG_MJPEG_ENCODER)
2184 memcpy(d->
mv, s->
mv, 2*4*2*
sizeof(
int));
2222 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2230 s->
pb= pb[*next_block];
2232 s->
pb2 = pb2 [*next_block];
2233 s->
tex_pb= tex_pb[*next_block];
2237 memcpy(dest_backup, s->
dest,
sizeof(s->
dest));
2260 memcpy(s->
dest, dest_backup,
sizeof(s->
dest));
2278 else if(w==8 && h==8)
2368 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2410 if (CONFIG_H263_ENCODER)
2412 bytestream_put_le32(&ptr, offset);
2413 bytestream_put_byte(&ptr, s->
qscale);
2414 bytestream_put_byte(&ptr, gobn);
2415 bytestream_put_le16(&ptr, mba);
2416 bytestream_put_byte(&ptr, pred_x);
2417 bytestream_put_byte(&ptr, pred_y);
2419 bytestream_put_byte(&ptr, 0);
2420 bytestream_put_byte(&ptr, 0);
2496 if (CONFIG_H263_ENCODER)
2516 for(mb_x=0; mb_x < s->
mb_width; mb_x++) {
2547 int current_packet_size, is_gob_start;
2553 if(s->
start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2562 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2567 if(s->
mb_x==0 && s->
mb_y!=0) is_gob_start=1;
2586 current_packet_size=0;
2600 if (CONFIG_MPEG4_ENCODER) {
2607 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2614 if (CONFIG_H263_ENCODER)
2644 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2661 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2662 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2673 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2674 &dmin, &next_block, 0, 0);
2682 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
2683 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2693 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
2694 &dmin, &next_block, 0, 0);
2702 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
2703 &dmin, &next_block, s->
mv[0][0][0], s->
mv[0][0][1]);
2711 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
2712 &dmin, &next_block, s->
mv[1][0][0], s->
mv[1][0][1]);
2722 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
2723 &dmin, &next_block, 0, 0);
2734 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
2735 &dmin, &next_block, 0, 0);
2746 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
2747 &dmin, &next_block, 0, 0);
2753 for(dir=0; dir<2; dir++){
2760 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
2761 &dmin, &next_block, 0, 0);
2769 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
2770 &dmin, &next_block, 0, 0);
2781 const int last_qp= backup_s.
qscale;
2785 static const int dquant_tab[4]={-1,1,-2,2};
2794 s->
mv[0][0][0] = best_s.
mv[0][0][0];
2795 s->
mv[0][0][1] = best_s.
mv[0][0][1];
2796 s->
mv[1][0][0] = best_s.
mv[1][0][0];
2797 s->
mv[1][0][1] = best_s.
mv[1][0][1];
2800 for(; qpi<4; qpi++){
2801 int dquant= dquant_tab[qpi];
2813 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2814 &dmin, &next_block, s->
mv[mvdir][0][0], s->
mv[mvdir][0][1]);
2834 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2835 &dmin, &next_block, mx, my);
2842 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2843 &dmin, &next_block, 0, 0);
2851 memcpy(s->
mv, best_s.
mv,
sizeof(s->
mv));
2872 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER , pb, pb2, tex_pb,
2873 &dmin, &next_block, mx, my);
2900 if (CONFIG_H263_ENCODER &&
2913 int motion_x = 0, motion_y = 0;
2921 motion_x= s->
mv[0][0][0] = 0;
2922 motion_y= s->
mv[0][0][1] = 0;
2950 if (CONFIG_MPEG4_ENCODER) {
2959 if (CONFIG_MPEG4_ENCODER) {
3009 for(dir=0; dir<2; dir++){
3026 if (CONFIG_H263_ENCODER &&
3083 #define MERGE(field) dst->field += src->field; src->field=0
3110 for(i=0; i<64; i++){
3137 if (CONFIG_MPEG4_ENCODER)
3143 if (CONFIG_H263_ENCODER)
3224 for(i=1; i<context_count; i++){
3254 for(i=1; i<context_count; i++){
3267 av_dlog(s,
"Scene change detected, encoding as I Frame %d %d\n",
3311 for(dir=0; dir<2; dir++){
3346 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};
3347 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};
3378 if (CONFIG_MJPEG_ENCODER)
3382 if (CONFIG_H261_ENCODER)
3390 else if (CONFIG_MPEG4_ENCODER && s->
h263_pred)
3398 else if (CONFIG_H263_ENCODER)
3402 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3411 for(i=1; i<context_count; i++){
3415 for(i=1; i<context_count; i++){
3428 for(i=0; i<64; i++){
3429 int level= block[i];
3435 if(level<0) level=0;
3439 if(level>0) level=0;
3448 int qscale,
int *overflow){
3453 unsigned int threshold1, threshold2;
3465 int coeff_count[64];
3466 int qmul, qadd, start_i, last_non_zero, i,
dc;
3477 qadd= ((qscale-1)|1)*8;
3494 block[0] = (block[0] + (q >> 1)) / q;
3512 threshold2= (threshold1<<1);
3514 for(i=63; i>=start_i; i--) {
3515 const int j = scantable[i];
3516 int level = block[j] * qmat[j];
3518 if(((
unsigned)(level+threshold1))>threshold2){
3524 for(i=start_i; i<=last_non_zero; i++) {
3525 const int j = scantable[i];
3526 int level = block[j] * qmat[j];
3530 if(((
unsigned)(level+threshold1))>threshold2){
3534 coeff[1][i]= level-1;
3538 coeff[0][i]= -
level;
3539 coeff[1][i]= -level+1;
3542 coeff_count[i]=
FFMIN(level, 2);
3546 coeff[0][i]= (level>>31)|1;
3553 if(last_non_zero < start_i){
3554 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3555 return last_non_zero;
3558 score_tab[start_i]= 0;
3559 survivor[0]= start_i;
3562 for(i=start_i; i<=last_non_zero; i++){
3563 int level_index, j, zero_distortion;
3564 int dct_coeff=
FFABS(block[ scantable[i] ]);
3565 int best_score=256*256*256*120;
3569 zero_distortion= dct_coeff*dct_coeff;
3571 for(level_index=0; level_index < coeff_count[i]; level_index++){
3573 int level= coeff[level_index][i];
3574 const int alevel=
FFABS(level);
3580 unquant_coeff= alevel*qmul + qadd;
3584 unquant_coeff = (int)( alevel * qscale * s->
intra_matrix[j]) >> 3;
3585 unquant_coeff = (unquant_coeff - 1) | 1;
3587 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[j])) >> 4;
3588 unquant_coeff = (unquant_coeff - 1) | 1;
3593 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3595 if((level&(~127)) == 0){
3596 for(j=survivor_count-1; j>=0; j--){
3597 int run= i - survivor[j];
3599 score += score_tab[i-
run];
3601 if(score < best_score){
3604 level_tab[i+1]= level-64;
3609 for(j=survivor_count-1; j>=0; j--){
3610 int run= i - survivor[j];
3612 score += score_tab[i-
run];
3613 if(score < last_score){
3616 last_level= level-64;
3622 distortion += esc_length*
lambda;
3623 for(j=survivor_count-1; j>=0; j--){
3624 int run= i - survivor[j];
3625 int score= distortion + score_tab[i-
run];
3627 if(score < best_score){
3630 level_tab[i+1]= level-64;
3635 for(j=survivor_count-1; j>=0; j--){
3636 int run= i - survivor[j];
3637 int score= distortion + score_tab[i-
run];
3638 if(score < last_score){
3641 last_level= level-64;
3649 score_tab[i+1]= best_score;
3652 if(last_non_zero <= 27){
3653 for(; survivor_count; survivor_count--){
3654 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
3658 for(; survivor_count; survivor_count--){
3659 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
3664 survivor[ survivor_count++ ]= i+1;
3668 last_score= 256*256*256*120;
3669 for(i= survivor[0]; i<=last_non_zero + 1; i++){
3670 int score= score_tab[i];
3671 if(i) score += lambda*2;
3673 if(score < last_score){
3676 last_level= level_tab[i];
3677 last_run= run_tab[i];
3684 dc=
FFABS(block[0]);
3685 last_non_zero= last_i - 1;
3686 memset(block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3688 if(last_non_zero < start_i)
3689 return last_non_zero;
3691 if(last_non_zero == 0 && start_i == 0){
3693 int best_score= dc *
dc;
3695 for(i=0; i<coeff_count[0]; i++){
3696 int level= coeff[i][0];
3697 int alevel=
FFABS(level);
3698 int unquant_coeff, score, distortion;
3701 unquant_coeff= (alevel*qmul + qadd)>>3;
3703 unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->
inter_matrix[0])) >> 4;
3704 unquant_coeff = (unquant_coeff - 1) | 1;
3706 unquant_coeff = (unquant_coeff + 4) >> 3;
3707 unquant_coeff<<= 3 + 3;
3709 distortion= (unquant_coeff -
dc) * (unquant_coeff - dc);
3712 else score= distortion + esc_length*
lambda;
3714 if(score < best_score){
3716 best_level= level - 64;
3719 block[0]= best_level;
3721 if(best_level == 0)
return -1;
3722 else return last_non_zero;
3728 block[ perm_scantable[last_non_zero] ]= last_level;
3731 for(; i>start_i; i -= run_tab[i] + 1){
3732 block[ perm_scantable[i-1] ]= level_tab[i];
3735 return last_non_zero;
3750 int perm_index= perm[
index];
3751 if(i==0) s*= sqrt(0.5);
3752 if(j==0) s*= sqrt(0.5);
3753 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)));
3772 int qmul, qadd, start_i, last_non_zero, i,
dc;
3776 int rle_index,
run, q = 1, sum;
3779 static int after_last=0;
3780 static int to_zero=0;
3781 static int from_zero=0;
3784 static int messed_sign=0;
3787 if(basis[0][0] == 0)
3824 for(i=0; i<64; i++){
3831 for(i=0; i<64; i++){
3836 w=
FFABS(weight[i]) + qns*one;
3837 w= 15 + (48*qns*one + w/2)/w;
3852 for(i=start_i; i<=last_non_zero; i++){
3853 int j= perm_scantable[i];
3854 const int level= block[j];
3858 if(level<0) coeff= qmul*level - qadd;
3859 else coeff= qmul*level + qadd;
3860 run_tab[rle_index++]=
run;
3869 if(last_non_zero>0){
3880 int run2, best_unquant_change=0, analyze_gradient;
3886 if(analyze_gradient){
3890 for(i=0; i<64; i++){
3906 const int level= block[0];
3907 int change, old_coeff;
3913 for(change=-1; change<=1; change+=2){
3914 int new_level= level + change;
3915 int score, new_coeff;
3917 new_coeff= q*new_level;
3918 if(new_coeff >= 2048 || new_coeff < 0)
3921 score= s->
dsp.
try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
3922 if(score<best_score){
3925 best_change= change;
3926 best_unquant_change= new_coeff - old_coeff;
3933 run2= run_tab[rle_index++];
3937 for(i=start_i; i<64; i++){
3938 int j= perm_scantable[i];
3939 const int level= block[j];
3940 int change, old_coeff;
3946 if(level<0) old_coeff= qmul*level - qadd;
3947 else old_coeff= qmul*level + qadd;
3948 run2= run_tab[rle_index++];
3955 for(change=-1; change<=1; change+=2){
3956 int new_level= level + change;
3957 int score, new_coeff, unquant_change;
3964 if(new_level<0) new_coeff= qmul*new_level - qadd;
3965 else new_coeff= qmul*new_level + qadd;
3966 if(new_coeff >= 2048 || new_coeff <= -2048)
3971 if(level < 63 && level > -63){
3972 if(i < last_non_zero)
3982 if(analyze_gradient){
3983 int g= d1[ scantable[i] ];
3984 if(g && (g^new_level) >= 0)
3988 if(i < last_non_zero){
3989 int next_i= i + run2 + 1;
3990 int next_level= block[ perm_scantable[next_i] ] + 64;
3992 if(next_level&(~127))
3995 if(next_i < last_non_zero)
4015 if(i < last_non_zero){
4016 int next_i= i + run2 + 1;
4017 int next_level= block[ perm_scantable[next_i] ] + 64;
4019 if(next_level&(~127))
4022 if(next_i < last_non_zero)
4041 unquant_change= new_coeff - old_coeff;
4042 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4045 if(score<best_score){
4048 best_change= change;
4049 best_unquant_change= unquant_change;
4053 prev_level= level + 64;
4054 if(prev_level&(~127))
4067 int j= perm_scantable[ best_coeff ];
4069 block[j] += best_change;
4071 if(best_coeff > last_non_zero){
4072 last_non_zero= best_coeff;
4080 if(block[j] - best_change){
4081 if(
FFABS(block[j]) >
FFABS(block[j] - best_change)){
4093 for(; last_non_zero>=start_i; last_non_zero--){
4094 if(block[perm_scantable[last_non_zero]])
4100 if(256*256*256*64 % count == 0){
4101 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);
4106 for(i=start_i; i<=last_non_zero; i++){
4107 int j= perm_scantable[i];
4108 const int level= block[j];
4111 run_tab[rle_index++]=
run;
4124 if(last_non_zero>0){
4130 return last_non_zero;
4135 int qscale,
int *overflow)
4137 int i, j,
level, last_non_zero, q, start_i;
4142 unsigned int threshold1, threshold2;
4161 block[0] = (block[0] + (q >> 1)) / q;
4173 threshold2= (threshold1<<1);
4174 for(i=63;i>=start_i;i--) {
4176 level = block[j] * qmat[j];
4178 if(((
unsigned)(level+threshold1))>threshold2){
4185 for(i=start_i; i<=last_non_zero; i++) {
4187 level = block[j] * qmat[j];
4191 if(((
unsigned)(level+threshold1))>threshold2){
4210 return last_non_zero;
4213 #define OFFSET(x) offsetof(MpegEncContext, x)
4214 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4217 {
"structured_slices",
"Write slice start position at every GOB header instead of just GOB number.",
OFFSET(
h263_slice_structured),