34 #include "config_components.h"
80 #define QUANT_BIAS_SHIFT 8
82 #define QMAT_SHIFT_MMX 16
108 uint16_t (*
qmat16)[2][64],
109 const uint16_t *quant_matrix,
110 int bias,
int qmin,
int qmax,
int intra)
121 else qscale2 =
qscale << 1;
128 for (
i = 0;
i < 64;
i++) {
129 const int j =
s->idsp.idct_permutation[
i];
130 int64_t den = (int64_t) qscale2 * quant_matrix[j];
140 for (
i = 0;
i < 64;
i++) {
141 const int j =
s->idsp.idct_permutation[
i];
142 int64_t den =
ff_aanscales[
i] * (int64_t) qscale2 * quant_matrix[j];
152 for (
i = 0;
i < 64;
i++) {
153 const int j =
s->idsp.idct_permutation[
i];
154 int64_t den = (int64_t) qscale2 * quant_matrix[j];
174 for (
i = intra;
i < 64;
i++) {
186 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
193 if (
s->q_scale_type == 1 && 0) {
195 int bestdiff=INT_MAX;
203 if (
diff < bestdiff) {
212 s->qscale =
av_clip(
s->qscale,
s->avctx->qmin,
s->vbv_ignore_qmax ? 31 :
s->avctx->qmax);
225 for (
i = 0;
i < 64;
i++) {
237 int8_t *
const qscale_table =
s->current_picture.qscale_table;
240 for (
i = 0;
i <
s->mb_num;
i++) {
241 unsigned int lam =
s->lambda_table[
s->mb_index2xy[
i]];
243 qscale_table[
s->mb_index2xy[
i]] =
av_clip(qp,
s->avctx->qmin,
251 #define COPY(a) dst->a= src->a
267 for (
int i = -16;
i < 16;
i++)
286 s->input_picture_number = 0;
287 s->picture_in_gop_number = 0;
296 if (CONFIG_H263_ENCODER)
298 if (!
s->dct_quantize)
302 s->fast_dct_quantize =
s->dct_quantize;
303 if (
s->avctx->trellis)
315 int mb_array_size, mv_table_size;
343 "keyframe interval too large!, reducing it from %d to %d\n",
355 "max b frames must be 0 or positive for mpegvideo based encoders\n");
366 s->rtp_mode = !!
s->rtp_payload_size;
370 if (
s->intra_dc_precision < 0) {
371 s->intra_dc_precision += 8;
372 }
else if (
s->intra_dc_precision >= 8)
373 s->intra_dc_precision -= 8;
375 if (
s->intra_dc_precision < 0) {
377 "intra dc precision must be positive, note some applications use"
378 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
388 if (
s->gop_size <= 1) {
442 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
459 "impossible bitrate constraints, this will fail\n");
469 if (!
s->fixed_qscale &&
475 if (nbt <= INT_MAX) {
488 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
489 "specified vbv buffer is too large for the given bitrate!\n");
501 "OBMC is only supported with simple mb decision\n");
516 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
569 if (
s->scenechange_threshold < 1000000000 &&
572 "closed gop with scene change detection are not supported yet, "
573 "set threshold to 1000000000\n");
581 "low delay forcing is only available for mpeg2, "
582 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
585 if (
s->max_b_frames != 0) {
587 "B-frames cannot be used with low delay\n");
592 if (
s->q_scale_type == 1) {
595 "non linear quant only supports qmax <= 28 currently\n");
608 "notice: b_frame_strategy only affects the first pass\n");
609 s->b_frame_strategy = 0;
623 s->inter_quant_bias = 0;
625 s->intra_quant_bias = 0;
640 "timebase %d/%d not supported by MPEG 4 standard, "
641 "the maximum admitted value for the timebase denominator "
649 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
656 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
660 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
674 if (!CONFIG_SPEEDHQ_ENCODER)
682 if (!CONFIG_H261_ENCODER)
693 if (!CONFIG_H263_ENCODER)
696 s->width,
s->height) == 8) {
698 "The specified picture size of %dx%d is not valid for "
699 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
700 "352x288, 704x576, and 1408x1152. "
701 "Try H.263+.\n",
s->width,
s->height);
713 s->modified_quant =
s->h263_aic;
715 s->unrestricted_mv =
s->obmc ||
s->loop_filter ||
s->umvplus;
725 s->unrestricted_mv = 1;
739 s->modified_quant = 1;
743 s->unrestricted_mv = 0;
748 s->unrestricted_mv = 1;
749 s->low_delay =
s->max_b_frames ? 0 : 1;
750 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
755 s->unrestricted_mv = 1;
756 s->msmpeg4_version = 2;
763 s->unrestricted_mv = 1;
764 s->msmpeg4_version = 3;
765 s->flipflop_rounding = 1;
772 s->unrestricted_mv = 1;
773 s->msmpeg4_version = 4;
774 s->flipflop_rounding = 1;
781 s->unrestricted_mv = 1;
782 s->msmpeg4_version = 5;
783 s->flipflop_rounding = 1;
795 s->progressive_frame =
800 if (
s->lmin >
s->lmax) {
829 mv_table_size = (
s->mb_height + 2) *
s->mb_stride + 1;
837 s->p_mv_table =
s->p_mv_table_base +
s->mb_stride + 1;
838 s->b_forw_mv_table =
s->b_forw_mv_table_base +
s->mb_stride + 1;
839 s->b_back_mv_table =
s->b_back_mv_table_base +
s->mb_stride + 1;
840 s->b_bidir_forw_mv_table =
s->b_bidir_forw_mv_table_base +
s->mb_stride + 1;
841 s->b_bidir_back_mv_table =
s->b_bidir_back_mv_table_base +
s->mb_stride + 1;
842 s->b_direct_mv_table =
s->b_direct_mv_table_base +
s->mb_stride + 1;
845 mb_array_size =
s->mb_stride *
s->mb_height;
855 #define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
860 if (!(tmp1 =
ALLOCZ_ARRAYS(
s->b_field_mv_table_base, 8, mv_table_size)) ||
861 !(tmp2 =
ALLOCZ_ARRAYS(
s->b_field_select_table[0][0], 2 * 4, mv_table_size)) ||
865 s->p_field_select_table[1] =
s->p_field_select_table[0] + 2 * mv_table_size;
866 tmp1 +=
s->mb_stride + 1;
868 for (
int i = 0;
i < 2;
i++) {
869 for (
int j = 0; j < 2; j++) {
870 for (
int k = 0; k < 2; k++) {
871 s->b_field_mv_table[
i][j][k] = tmp1;
872 tmp1 += mv_table_size;
874 s->b_field_select_table[
i][j] = tmp2;
875 tmp2 += 2 * mv_table_size;
880 if (
s->noise_reduction) {
888 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
889 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
891 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
892 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
894 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
895 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
898 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) &&
s->modified_quant)
901 if (
s->slice_context_count > 1) {
905 s->h263_slice_structured = 1;
908 s->quant_precision = 5;
915 if (CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263) {
917 if (CONFIG_MSMPEG4ENC &&
s->msmpeg4_version)
922 for (
i = 0;
i < 64;
i++) {
923 int j =
s->idsp.idct_permutation[
i];
936 s->chroma_intra_matrix[j] =
960 if (
s->b_frame_strategy == 2) {
961 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
963 if (!
s->tmp_frames[
i])
967 s->tmp_frames[
i]->width =
s->width >>
s->brd_scale;
968 s->tmp_frames[
i]->height =
s->height >>
s->brd_scale;
1006 av_freep(&
s->b_bidir_forw_mv_table_base);
1007 av_freep(&
s->b_bidir_back_mv_table_base);
1010 av_freep(&
s->b_field_select_table[0][0]);
1019 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
1020 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
1021 s->q_chroma_intra_matrix=
NULL;
1022 s->q_chroma_intra_matrix16=
NULL;
1037 #define IS_ENCODER 1
1045 for (
int i = 0;
i < 6;
i++) {
1046 for (
int j = 0; j < 64; j++) {
1048 block[
i][
s->idsp.idct_permutation[j]]);
1062 for (y = 0; y < 16; y++) {
1063 for (x = 0; x < 16; x++) {
1078 h =
s->height & ~15;
1080 for (y = 0; y <
h; y += 16) {
1081 for (x = 0; x <
w; x += 16) {
1088 acc += sae + 500 < sad;
1106 for (
int i = 0; pic->
f->
data[
i];
i++) {
1116 s->mb_stride,
s->mb_width,
s->mb_height,
s->b8_stride,
1117 &
s->linesize, &
s->uvlinesize);
1124 int i, display_picture_number = 0,
ret;
1125 int encoding_delay =
s->max_b_frames ?
s->max_b_frames
1126 : (
s->low_delay ? 0 : 1);
1127 int flush_offset = 1;
1132 display_picture_number =
s->input_picture_number++;
1136 int64_t last =
s->user_specified_pts;
1140 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1145 if (!
s->low_delay && display_picture_number == 1)
1146 s->dts_delta =
pts - last;
1148 s->user_specified_pts =
pts;
1151 s->user_specified_pts =
1152 pts =
s->user_specified_pts + 1;
1154 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1157 pts = display_picture_number;
1161 if (pic_arg->
linesize[0] !=
s->linesize ||
1162 pic_arg->
linesize[1] !=
s->uvlinesize ||
1165 if ((
s->width & 15) || (
s->height & 15))
1173 pic_arg->
linesize[1],
s->linesize,
s->uvlinesize);
1179 pic = &
s->picture[
i];
1196 for (
int i = 0;
i < 3;
i++) {
1197 ptrdiff_t src_stride = pic_arg->
linesize[
i];
1198 ptrdiff_t dst_stride =
i ?
s->uvlinesize :
s->linesize;
1199 int h_shift =
i ?
s->chroma_x_shift : 0;
1200 int v_shift =
i ?
s->chroma_y_shift : 0;
1201 int w =
s->width >> h_shift;
1202 int h =
s->height >> v_shift;
1203 const uint8_t *
src = pic_arg->
data[
i];
1204 uint8_t *dst = pic->
f->
data[
i];
1208 && !
s->progressive_sequence
1209 &&
FFALIGN(
s->height, 32) -
s->height > 16)
1212 if (!
s->avctx->rc_buffer_size)
1215 if (src_stride == dst_stride)
1216 memcpy(dst,
src, src_stride *
h - src_stride +
w);
1219 uint8_t *dst2 = dst;
1221 memcpy(dst2,
src,
w);
1226 if ((
s->width & 15) || (
s->height & (vpad-1))) {
1227 s->mpvencdsp.draw_edges(dst, dst_stride,
1242 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1243 if (
s->input_picture[flush_offset])
1246 if (flush_offset <= 1)
1249 encoding_delay = encoding_delay - flush_offset + 1;
1254 s->input_picture[
i - flush_offset] =
s->input_picture[
i];
1256 s->input_picture[encoding_delay] = pic;
1265 int64_t score64 = 0;
1267 for (plane = 0; plane < 3; plane++) {
1269 const int bw = plane ? 1 : 2;
1270 for (y = 0; y <
s->mb_height * bw; y++) {
1271 for (x = 0; x <
s->mb_width * bw; x++) {
1272 int off = p->
shared ? 0 : 16;
1273 const uint8_t *dptr = p->
f->
data[plane] + 8 * (x + y *
stride) + off;
1274 const uint8_t *rptr =
ref->f->data[plane] + 8 * (x + y *
stride);
1275 int v =
s->mecc.frame_skip_cmp[1](
s, dptr, rptr,
stride, 8);
1277 switch (
FFABS(
s->frame_skip_exp)) {
1278 case 0: score =
FFMAX(score, v);
break;
1279 case 1: score +=
FFABS(v);
break;
1280 case 2: score64 += v * (int64_t)v;
break;
1281 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1282 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1291 if (
s->frame_skip_exp < 0)
1292 score64 = pow(score64 / (
double)(
s->mb_width *
s->mb_height),
1293 -1.0/
s->frame_skip_exp);
1297 if (score64 < ((
s->frame_skip_factor * (int64_t)
s->lambda) >> 8))
1326 const int scale =
s->brd_scale;
1330 int64_t best_rd = INT64_MAX;
1331 int best_b_count = -1;
1346 b_lambda = p_lambda;
1350 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1351 const Picture *pre_input_ptr =
i ?
s->input_picture[
i - 1] :
1352 s->next_picture_ptr;
1354 if (pre_input_ptr) {
1355 const uint8_t *
data[4];
1358 if (!pre_input_ptr->
shared &&
i) {
1364 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[0],
1365 s->tmp_frames[
i]->linesize[0],
1369 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[1],
1370 s->tmp_frames[
i]->linesize[1],
1374 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[2],
1375 s->tmp_frames[
i]->linesize[2],
1382 for (j = 0; j <
s->max_b_frames + 1; j++) {
1386 if (!
s->input_picture[j])
1399 c->mb_decision =
s->avctx->mb_decision;
1400 c->me_cmp =
s->avctx->me_cmp;
1401 c->mb_cmp =
s->avctx->mb_cmp;
1402 c->me_sub_cmp =
s->avctx->me_sub_cmp;
1404 c->time_base =
s->avctx->time_base;
1405 c->max_b_frames =
s->max_b_frames;
1423 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1424 int is_p =
i % (j + 1) == j ||
i ==
s->max_b_frames;
1426 s->tmp_frames[
i + 1]->pict_type = is_p ?
1428 s->tmp_frames[
i + 1]->quality = is_p ? p_lambda : b_lambda;
1447 rd +=
c->error[0] +
c->error[1] +
c->error[2];
1465 return best_b_count;
1473 s->reordered_input_picture[
i - 1] =
s->reordered_input_picture[
i];
1477 if (!
s->reordered_input_picture[0] &&
s->input_picture[0]) {
1478 if (
s->frame_skip_threshold ||
s->frame_skip_factor) {
1479 if (
s->picture_in_gop_number <
s->gop_size &&
1480 s->next_picture_ptr &&
1492 !
s->next_picture_ptr ||
s->intra_only) {
1493 s->reordered_input_picture[0] =
s->input_picture[0];
1495 s->reordered_input_picture[0]->coded_picture_number =
1496 s->coded_picture_number++;
1501 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1502 int pict_num =
s->input_picture[0]->display_picture_number +
i;
1504 if (pict_num >=
s->rc_context.num_entries)
1506 if (!
s->input_picture[
i]) {
1511 s->input_picture[
i]->f->pict_type =
1512 s->rc_context.entry[pict_num].new_pict_type;
1516 if (
s->b_frame_strategy == 0) {
1517 b_frames =
s->max_b_frames;
1518 while (b_frames && !
s->input_picture[b_frames])
1520 }
else if (
s->b_frame_strategy == 1) {
1521 for (
i = 1;
i <
s->max_b_frames + 1;
i++) {
1522 if (
s->input_picture[
i] &&
1523 s->input_picture[
i]->b_frame_score == 0) {
1524 s->input_picture[
i]->b_frame_score =
1526 s->input_picture[
i ]->f->data[0],
1527 s->input_picture[
i - 1]->f->data[0],
1531 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1532 if (!
s->input_picture[
i] ||
1533 s->input_picture[
i]->b_frame_score - 1 >
1534 s->mb_num /
s->b_sensitivity)
1538 b_frames =
FFMAX(0,
i - 1);
1541 for (
i = 0;
i < b_frames + 1;
i++) {
1542 s->input_picture[
i]->b_frame_score = 0;
1544 }
else if (
s->b_frame_strategy == 2) {
1554 for (
i = b_frames - 1;
i >= 0;
i--) {
1555 int type =
s->input_picture[
i]->f->pict_type;
1560 b_frames ==
s->max_b_frames) {
1562 "warning, too many B-frames in a row\n");
1565 if (
s->picture_in_gop_number + b_frames >=
s->gop_size) {
1567 s->gop_size >
s->picture_in_gop_number) {
1568 b_frames =
s->gop_size -
s->picture_in_gop_number - 1;
1580 s->reordered_input_picture[0] =
s->input_picture[b_frames];
1583 s->reordered_input_picture[0]->coded_picture_number =
1584 s->coded_picture_number++;
1585 for (
i = 0;
i < b_frames;
i++) {
1586 s->reordered_input_picture[
i + 1] =
s->input_picture[
i];
1587 s->reordered_input_picture[
i + 1]->f->pict_type =
1589 s->reordered_input_picture[
i + 1]->coded_picture_number =
1590 s->coded_picture_number++;
1597 if (
s->reordered_input_picture[0]) {
1598 s->reordered_input_picture[0]->reference =
1599 s->reordered_input_picture[0]->f->pict_type !=
1603 s->reordered_input_picture[0]->f)))
1606 if (
s->reordered_input_picture[0]->shared ||
s->avctx->rc_buffer_size) {
1614 pic = &
s->picture[
i];
1616 pic->
reference =
s->reordered_input_picture[0]->reference;
1631 s->reordered_input_picture[0]->shared = 0;
1633 s->current_picture_ptr = pic;
1636 s->current_picture_ptr =
s->reordered_input_picture[0];
1637 for (
i = 0;
i < 4;
i++) {
1638 if (
s->new_picture->data[
i])
1642 s->picture_number =
s->current_picture_ptr->display_picture_number;
1653 if (
s->unrestricted_mv &&
1654 s->current_picture.reference &&
1656 int hshift =
s->chroma_x_shift;
1657 int vshift =
s->chroma_y_shift;
1658 s->mpvencdsp.draw_edges(
s->current_picture.f->data[0],
1659 s->current_picture.f->linesize[0],
1660 s->h_edge_pos,
s->v_edge_pos,
1663 s->mpvencdsp.draw_edges(
s->current_picture.f->data[1],
1664 s->current_picture.f->linesize[1],
1665 s->h_edge_pos >> hshift,
1666 s->v_edge_pos >> vshift,
1670 s->mpvencdsp.draw_edges(
s->current_picture.f->data[2],
1671 s->current_picture.f->linesize[2],
1672 s->h_edge_pos >> hshift,
1673 s->v_edge_pos >> vshift,
1681 s->last_pict_type =
s->pict_type;
1682 s->last_lambda_for [
s->pict_type] =
s->current_picture_ptr->f->quality;
1684 s->last_non_b_pict_type =
s->pict_type;
1691 for (intra = 0; intra < 2; intra++) {
1692 if (
s->dct_count[intra] > (1 << 16)) {
1693 for (
i = 0;
i < 64;
i++) {
1694 s->dct_error_sum[intra][
i] >>= 1;
1696 s->dct_count[intra] >>= 1;
1699 for (
i = 0;
i < 64;
i++) {
1700 s->dct_offset[intra][
i] = (
s->noise_reduction *
1701 s->dct_count[intra] +
1702 s->dct_error_sum[intra][
i] / 2) /
1703 (
s->dct_error_sum[intra][
i] + 1);
1714 s->last_picture_ptr !=
s->next_picture_ptr &&
1715 s->last_picture_ptr->f->buf[0]) {
1719 s->current_picture_ptr->f->pict_type =
s->pict_type;
1723 s->current_picture_ptr)) < 0)
1727 s->last_picture_ptr =
s->next_picture_ptr;
1728 s->next_picture_ptr =
s->current_picture_ptr;
1731 if (
s->last_picture_ptr) {
1733 if (
s->last_picture_ptr->f->buf[0] &&
1735 s->last_picture_ptr)) < 0)
1738 if (
s->next_picture_ptr) {
1740 if (
s->next_picture_ptr->f->buf[0] &&
1742 s->next_picture_ptr)) < 0)
1746 if (
s->dct_error_sum) {
1755 const AVFrame *pic_arg,
int *got_packet)
1758 int i, stuffing_count,
ret;
1759 int context_count =
s->slice_context_count;
1761 s->vbv_ignore_qmax = 0;
1763 s->picture_in_gop_number++;
1773 if (
s->new_picture->data[0]) {
1774 int growing_buffer = context_count == 1 && !
s->data_partitioning;
1775 size_t pkt_size = 10000 +
s->mb_width *
s->mb_height *
1788 s->mb_width*
s->mb_height*12);
1789 s->prev_mb_info =
s->last_mb_info =
s->mb_info_size = 0;
1792 for (
i = 0;
i < context_count;
i++) {
1793 int start_y =
s->thread_context[
i]->start_mb_y;
1795 int h =
s->mb_height;
1796 uint8_t *start =
pkt->
data + (size_t)(((int64_t)
pkt->
size) * start_y /
h);
1797 uint8_t *end =
pkt->
data + (size_t)(((int64_t)
pkt->
size) * end_y /
h);
1802 s->pict_type =
s->new_picture->pict_type;
1809 if (growing_buffer) {
1819 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
s->out_format ==
FMT_MJPEG)
1829 s->lambda <
s->lmax) {
1830 s->next_lambda =
FFMAX(
s->lambda + min_step,
s->lambda *
1831 (
s->qscale + 1) /
s->qscale);
1832 if (
s->adaptive_quant) {
1834 for (
i = 0;
i <
s->mb_height *
s->mb_stride;
i++)
1835 s->lambda_table[
i] =
1836 FFMAX(
s->lambda_table[
i] + min_step,
1837 s->lambda_table[
i] * (
s->qscale + 1) /
1843 if (
s->flipflop_rounding ||
1846 s->no_rounding ^= 1;
1849 s->time_base =
s->last_time_base;
1850 s->last_non_b_time =
s->time -
s->pp_time;
1852 for (
i = 0;
i < context_count;
i++) {
1856 s->vbv_ignore_qmax = 1;
1867 for (
i = 0;
i < 4;
i++) {
1877 s->misc_bits +
s->i_tex_bits +
1883 s->stuffing_bits = 8*stuffing_count;
1884 if (stuffing_count) {
1890 switch (
s->codec_id) {
1893 while (stuffing_count--) {
1900 stuffing_count -= 4;
1901 while (stuffing_count--) {
1907 s->stuffing_bits = 0;
1925 int minbits =
s->frame_bits - 8 *
1926 (
s->vbv_delay_pos - 1);
1927 double bits =
s->rc_context.buffer_index + minbits - inbits;
1928 uint8_t *
const vbv_delay_ptr =
s->pb.buf +
s->vbv_delay_pos;
1932 "Internal error, negative bits\n");
1944 vbv_delay_ptr[0] &= 0xF8;
1947 vbv_delay_ptr[2] &= 0x07;
1956 (uint8_t*)props, props_size);
1962 s->total_bits +=
s->frame_bits;
1964 pkt->
pts =
s->current_picture.f->pts;
1967 if (!
s->current_picture.coded_picture_number)
1992 if (!
s->picture[
i].reference)
2004 int n,
int threshold)
2006 static const char tab[64] = {
2007 3, 2, 2, 1, 1, 1, 1, 1,
2008 1, 1, 1, 1, 1, 1, 1, 1,
2009 1, 1, 1, 1, 1, 1, 1, 1,
2010 0, 0, 0, 0, 0, 0, 0, 0,
2011 0, 0, 0, 0, 0, 0, 0, 0,
2012 0, 0, 0, 0, 0, 0, 0, 0,
2013 0, 0, 0, 0, 0, 0, 0, 0,
2014 0, 0, 0, 0, 0, 0, 0, 0
2019 int16_t *
block =
s->block[n];
2020 const int last_index =
s->block_last_index[n];
2023 if (threshold < 0) {
2025 threshold = -threshold;
2030 if (last_index <= skip_dc - 1)
2033 for (
i = 0;
i <= last_index;
i++) {
2034 const int j =
s->intra_scantable.permutated[
i];
2037 if (skip_dc &&
i == 0)
2041 }
else if (
level > 1) {
2047 if (score >= threshold)
2049 for (
i = skip_dc;
i <= last_index;
i++) {
2050 const int j =
s->intra_scantable.permutated[
i];
2054 s->block_last_index[n] = 0;
2056 s->block_last_index[n] = -1;
2063 const int maxlevel =
s->max_qcoeff;
2064 const int minlevel =
s->min_qcoeff;
2072 for (;
i <= last_index;
i++) {
2073 const int j =
s->intra_scantable.permutated[
i];
2076 if (
level > maxlevel) {
2079 }
else if (
level < minlevel) {
2089 "warning, clipping %d dct coefficients to %d..%d\n",
2097 for (y = 0; y < 8; y++) {
2098 for (x = 0; x < 8; x++) {
2104 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2105 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2106 int v = ptr[x2 + y2 *
stride];
2118 int motion_x,
int motion_y,
2119 int mb_block_height,
2128 #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2129 (s)->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2131 int16_t orig[12][64];
2132 const int mb_x =
s->mb_x;
2133 const int mb_y =
s->mb_y;
2137 int uv_dct_offset =
s->uvlinesize * 8;
2138 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2139 ptrdiff_t wrap_y, wrap_c;
2141 for (
i = 0;
i < mb_block_count;
i++)
2142 skip_dct[
i] =
s->skipdct;
2144 if (
s->adaptive_quant) {
2145 const int last_qp =
s->qscale;
2146 const int mb_xy =
mb_x +
mb_y *
s->mb_stride;
2148 s->lambda =
s->lambda_table[mb_xy];
2152 s->qscale =
s->current_picture_ptr->qscale_table[mb_xy];
2153 s->dquant =
s->qscale - last_qp;
2174 wrap_y =
s->linesize;
2175 wrap_c =
s->uvlinesize;
2176 ptr_y =
s->new_picture->data[0] +
2178 ptr_cb =
s->new_picture->data[1] +
2179 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2180 ptr_cr =
s->new_picture->data[2] +
2181 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2184 uint8_t *ebuf =
s->sc.edge_emu_buffer + 38 * wrap_y;
2187 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2190 s->width,
s->height);
2192 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2194 mb_block_width, mb_block_height,
2195 mb_x * mb_block_width,
mb_y * mb_block_height,
2197 ptr_cb = ebuf + 16 * wrap_y;
2198 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2200 mb_block_width, mb_block_height,
2201 mb_x * mb_block_width,
mb_y * mb_block_height,
2203 ptr_cr = ebuf + 16 * wrap_y + 16;
2208 int progressive_score, interlaced_score;
2210 s->interlaced_dct = 0;
2211 progressive_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
NULL, wrap_y, 8) +
2212 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y * 8,
2213 NULL, wrap_y, 8) - 400;
2215 if (progressive_score > 0) {
2216 interlaced_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
2217 NULL, wrap_y * 2, 8) +
2218 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y,
2219 NULL, wrap_y * 2, 8);
2220 if (progressive_score > interlaced_score) {
2221 s->interlaced_dct = 1;
2224 uv_dct_offset = wrap_c;
2233 s->pdsp.get_pixels(
s->block[0], ptr_y, wrap_y);
2234 s->pdsp.get_pixels(
s->block[1], ptr_y + 8, wrap_y);
2235 s->pdsp.get_pixels(
s->block[2], ptr_y +
dct_offset, wrap_y);
2236 s->pdsp.get_pixels(
s->block[3], ptr_y +
dct_offset + 8, wrap_y);
2242 s->pdsp.get_pixels(
s->block[4], ptr_cb, wrap_c);
2243 s->pdsp.get_pixels(
s->block[5], ptr_cr, wrap_c);
2245 s->pdsp.get_pixels(
s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2246 s->pdsp.get_pixels(
s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2248 s->pdsp.get_pixels(
s->block[ 6], ptr_cb + 8, wrap_c);
2249 s->pdsp.get_pixels(
s->block[ 7], ptr_cr + 8, wrap_c);
2250 s->pdsp.get_pixels(
s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2251 s->pdsp.get_pixels(
s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2252 s->pdsp.get_pixels(
s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2253 s->pdsp.get_pixels(
s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2259 uint8_t *dest_y, *dest_cb, *dest_cr;
2261 dest_y =
s->dest[0];
2262 dest_cb =
s->dest[1];
2263 dest_cr =
s->dest[2];
2266 op_pix =
s->hdsp.put_pixels_tab;
2267 op_qpix =
s->qdsp.put_qpel_pixels_tab;
2269 op_pix =
s->hdsp.put_no_rnd_pixels_tab;
2270 op_qpix =
s->qdsp.put_no_rnd_qpel_pixels_tab;
2275 s->last_picture.f->data,
2277 op_pix =
s->hdsp.avg_pixels_tab;
2278 op_qpix =
s->qdsp.avg_qpel_pixels_tab;
2282 s->next_picture.f->data,
2287 int progressive_score, interlaced_score;
2289 s->interlaced_dct = 0;
2290 progressive_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2291 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y * 8,
2296 progressive_score -= 400;
2298 if (progressive_score > 0) {
2299 interlaced_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y,
2301 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y,
2305 if (progressive_score > interlaced_score) {
2306 s->interlaced_dct = 1;
2309 uv_dct_offset = wrap_c;
2317 s->pdsp.diff_pixels(
s->block[0], ptr_y, dest_y, wrap_y);
2318 s->pdsp.diff_pixels(
s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2321 s->pdsp.diff_pixels(
s->block[3], ptr_y +
dct_offset + 8,
2328 s->pdsp.diff_pixels(
s->block[4], ptr_cb, dest_cb, wrap_c);
2329 s->pdsp.diff_pixels(
s->block[5], ptr_cr, dest_cr, wrap_c);
2331 s->pdsp.diff_pixels(
s->block[6], ptr_cb + uv_dct_offset,
2332 dest_cb + uv_dct_offset, wrap_c);
2333 s->pdsp.diff_pixels(
s->block[7], ptr_cr + uv_dct_offset,
2334 dest_cr + uv_dct_offset, wrap_c);
2338 if (
s->mc_mb_var[
s->mb_stride *
mb_y +
mb_x] < 2 *
s->qscale *
s->qscale) {
2340 if (
s->mecc.sad[1](
NULL, ptr_y, dest_y, wrap_y, 8) < 20 *
s->qscale)
2342 if (
s->mecc.sad[1](
NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 *
s->qscale)
2345 wrap_y, 8) < 20 *
s->qscale)
2348 wrap_y, 8) < 20 *
s->qscale)
2350 if (
s->mecc.sad[1](
NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 *
s->qscale)
2352 if (
s->mecc.sad[1](
NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 *
s->qscale)
2355 if (
s->mecc.sad[1](
NULL, ptr_cb + uv_dct_offset,
2356 dest_cb + uv_dct_offset,
2357 wrap_c, 8) < 20 *
s->qscale)
2359 if (
s->mecc.sad[1](
NULL, ptr_cr + uv_dct_offset,
2360 dest_cr + uv_dct_offset,
2361 wrap_c, 8) < 20 *
s->qscale)
2367 if (
s->quantizer_noise_shaping) {
2388 memcpy(orig[0],
s->block[0],
sizeof(int16_t) * 64 * mb_block_count);
2394 for (
i = 0;
i < mb_block_count;
i++) {
2397 s->block_last_index[
i] =
s->dct_quantize(
s,
s->block[
i],
i,
s->qscale, &
overflow);
2406 s->block_last_index[
i] = -1;
2408 if (
s->quantizer_noise_shaping) {
2409 for (
i = 0;
i < mb_block_count;
i++) {
2411 s->block_last_index[
i] =
2413 orig[
i],
i,
s->qscale);
2418 if (
s->luma_elim_threshold && !
s->mb_intra)
2419 for (
i = 0;
i < 4;
i++)
2421 if (
s->chroma_elim_threshold && !
s->mb_intra)
2422 for (
i = 4;
i < mb_block_count;
i++)
2426 for (
i = 0;
i < mb_block_count;
i++) {
2427 if (
s->block_last_index[
i] == -1)
2428 s->coded_score[
i] = INT_MAX / 256;
2434 s->block_last_index[4] =
2435 s->block_last_index[5] = 0;
2437 s->block[5][0] = (1024 +
s->c_dc_scale / 2) /
s->c_dc_scale;
2439 for (
i=6;
i<12;
i++) {
2440 s->block_last_index[
i] = 0;
2441 s->block[
i][0] =
s->block[4][0];
2448 for (
i = 0;
i < mb_block_count;
i++) {
2450 if (
s->block_last_index[
i] > 0) {
2451 for (j = 63; j > 0; j--) {
2452 if (
s->block[
i][
s->intra_scantable.permutated[j]])
2455 s->block_last_index[
i] = j;
2461 switch(
s->codec_id){
2464 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2468 if (CONFIG_MPEG4_ENCODER)
2474 if (CONFIG_MSMPEG4ENC)
2478 if (CONFIG_WMV2_ENCODER)
2482 if (CONFIG_H261_ENCODER)
2490 if (CONFIG_H263_ENCODER)
2493 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
2500 if (CONFIG_SPEEDHQ_ENCODER)
2523 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2526 d->mb_skip_run=
s->mb_skip_run;
2528 d->last_dc[
i] =
s->last_dc[
i];
2531 d->mv_bits=
s->mv_bits;
2532 d->i_tex_bits=
s->i_tex_bits;
2533 d->p_tex_bits=
s->p_tex_bits;
2534 d->i_count=
s->i_count;
2535 d->skip_count=
s->skip_count;
2536 d->misc_bits=
s->misc_bits;
2540 d->qscale=
s->qscale;
2541 d->dquant=
s->dquant;
2543 d->esc3_level_length=
s->esc3_level_length;
2551 memcpy(
d->mv,
s->mv, 2*4*2*
sizeof(
int));
2552 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2555 d->mb_skip_run=
s->mb_skip_run;
2557 d->last_dc[
i] =
s->last_dc[
i];
2560 d->mv_bits=
s->mv_bits;
2561 d->i_tex_bits=
s->i_tex_bits;
2562 d->p_tex_bits=
s->p_tex_bits;
2563 d->i_count=
s->i_count;
2564 d->skip_count=
s->skip_count;
2565 d->misc_bits=
s->misc_bits;
2567 d->mb_intra=
s->mb_intra;
2568 d->mb_skipped=
s->mb_skipped;
2569 d->mv_type=
s->mv_type;
2570 d->mv_dir=
s->mv_dir;
2572 if(
s->data_partitioning){
2574 d->tex_pb=
s->tex_pb;
2578 d->block_last_index[
i]=
s->block_last_index[
i];
2579 d->interlaced_dct=
s->interlaced_dct;
2580 d->qscale=
s->qscale;
2582 d->esc3_level_length=
s->esc3_level_length;
2587 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2590 uint8_t *dest_backup[3];
2594 s->block=
s->blocks[*next_block];
2595 s->pb=
pb[*next_block];
2596 if(
s->data_partitioning){
2597 s->pb2 =
pb2 [*next_block];
2598 s->tex_pb=
tex_pb[*next_block];
2602 memcpy(dest_backup,
s->dest,
sizeof(
s->dest));
2603 s->dest[0] =
s->sc.rd_scratchpad;
2604 s->dest[1] =
s->sc.rd_scratchpad + 16*
s->linesize;
2605 s->dest[2] =
s->sc.rd_scratchpad + 16*
s->linesize + 8;
2612 if(
s->data_partitioning){
2620 score *=
s->lambda2;
2625 memcpy(
s->dest, dest_backup,
sizeof(
s->dest));
2643 else if(
w==8 &&
h==8)
2660 int chroma_mb_w =
w >>
s->chroma_x_shift;
2661 int chroma_mb_h =
h >>
s->chroma_y_shift;
2663 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
2664 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
2668 return s->mecc.nsse[0](
s,
s->new_picture->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
2669 s->dest[0],
s->linesize, 16) +
2670 s->mecc.nsse[1](
s,
s->new_picture->data[1] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2671 s->dest[1],
s->uvlinesize, chroma_mb_h) +
2672 s->mecc.nsse[1](
s,
s->new_picture->data[2] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2673 s->dest[2],
s->uvlinesize, chroma_mb_h);
2675 return s->mecc.sse[0](
NULL,
s->new_picture->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
2676 s->dest[0],
s->linesize, 16) +
2677 s->mecc.sse[1](
NULL,
s->new_picture->data[1] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2678 s->dest[1],
s->uvlinesize, chroma_mb_h) +
2679 s->mecc.sse[1](
NULL,
s->new_picture->data[2] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2680 s->dest[2],
s->uvlinesize, chroma_mb_h);
2683 return sse(
s,
s->new_picture->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
2684 s->dest[0],
w,
h,
s->linesize) +
2685 sse(
s,
s->new_picture->data[1] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2686 s->dest[1],
w >>
s->chroma_x_shift,
h >>
s->chroma_y_shift,
s->uvlinesize) +
2687 sse(
s,
s->new_picture->data[2] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2688 s->dest[2],
w >>
s->chroma_x_shift,
h >>
s->chroma_y_shift,
s->uvlinesize);
2696 s->me.dia_size=
s->avctx->pre_dia_size;
2697 s->first_slice_line=1;
2698 for(
s->mb_y=
s->end_mb_y-1;
s->mb_y >=
s->start_mb_y;
s->mb_y--) {
2699 for(
s->mb_x=
s->mb_width-1;
s->mb_x >=0 ;
s->mb_x--) {
2702 s->first_slice_line=0;
2713 s->me.dia_size=
s->avctx->dia_size;
2714 s->first_slice_line=1;
2715 for(
s->mb_y=
s->start_mb_y;
s->mb_y <
s->end_mb_y;
s->mb_y++) {
2718 for(
s->mb_x=0;
s->mb_x <
s->mb_width;
s->mb_x++) {
2719 s->block_index[0]+=2;
2720 s->block_index[1]+=2;
2721 s->block_index[2]+=2;
2722 s->block_index[3]+=2;
2730 s->first_slice_line=0;
2743 const uint8_t *pix =
s->new_picture->data[0] + (yy *
s->linesize) + xx;
2745 int sum =
s->mpvencdsp.pix_sum(pix,
s->linesize);
2747 varc = (
s->mpvencdsp.pix_norm1(pix,
s->linesize) -
2748 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2750 s->mb_var [
s->mb_stride *
mb_y +
mb_x] = varc;
2751 s->mb_mean[
s->mb_stride *
mb_y +
mb_x] = (sum+128)>>8;
2752 s->me.mb_var_sum_temp += varc;
2760 if(
s->partitioned_frame){
2765 }
else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2768 }
else if (CONFIG_SPEEDHQ_ENCODER &&
s->out_format ==
FMT_SPEEDHQ) {
2780 uint8_t *ptr =
s->mb_info_ptr +
s->mb_info_size - 12;
2782 int mba =
s->mb_x +
s->mb_width * (
s->mb_y %
s->gob_index);
2783 int gobn =
s->mb_y /
s->gob_index;
2785 if (CONFIG_H263_ENCODER)
2787 bytestream_put_le32(&ptr,
offset);
2788 bytestream_put_byte(&ptr,
s->qscale);
2789 bytestream_put_byte(&ptr, gobn);
2790 bytestream_put_le16(&ptr, mba);
2791 bytestream_put_byte(&ptr, pred_x);
2792 bytestream_put_byte(&ptr, pred_y);
2794 bytestream_put_byte(&ptr, 0);
2795 bytestream_put_byte(&ptr, 0);
2803 s->mb_info_size += 12;
2804 s->prev_mb_info =
s->last_mb_info;
2816 if (!
s->mb_info_size)
2817 s->mb_info_size += 12;
2824 &&
s->slice_context_count == 1
2825 &&
s->pb.buf ==
s->avctx->internal->byte_buffer) {
2826 int lastgob_pos =
s->ptr_lastgob -
s->pb.buf;
2828 uint8_t *new_buffer =
NULL;
2829 int new_buffer_size = 0;
2831 if ((
s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2839 s->avctx->internal->byte_buffer_size + size_increase);
2843 memcpy(new_buffer,
s->avctx->internal->byte_buffer,
s->avctx->internal->byte_buffer_size);
2844 av_free(
s->avctx->internal->byte_buffer);
2845 s->avctx->internal->byte_buffer = new_buffer;
2846 s->avctx->internal->byte_buffer_size = new_buffer_size;
2848 s->ptr_lastgob =
s->pb.buf + lastgob_pos;
2858 int chr_h= 16>>
s->chroma_y_shift;
2883 s->last_dc[
i] = 128 <<
s->intra_dc_precision;
2885 s->encoding_error[
i] = 0;
2888 s->last_dc[0] = 128*8/13;
2889 s->last_dc[1] = 128*8/14;
2890 s->last_dc[2] = 128*8/14;
2893 memset(
s->last_mv, 0,
sizeof(
s->last_mv));
2897 switch(
s->codec_id){
2901 if (CONFIG_H263_ENCODER)
2905 if(CONFIG_MPEG4_ENCODER &&
s->partitioned_frame)
2912 s->first_slice_line = 1;
2913 s->ptr_lastgob =
s->pb.buf;
2914 for (mb_y_order =
s->start_mb_y; mb_y_order < s->
end_mb_y; mb_y_order++) {
2918 if (first_in_slice && mb_y_order !=
s->start_mb_y)
2920 s->last_dc[0] =
s->last_dc[1] =
s->last_dc[2] = 1024 <<
s->intra_dc_precision;
2936 int size_increase =
s->avctx->internal->byte_buffer_size/4
2944 if(
s->data_partitioning){
2958 xy=
s->mb_y*
s->mb_stride +
s->mb_x;
2964 int current_packet_size, is_gob_start;
2967 - (
s->ptr_lastgob -
s->pb.buf);
2969 is_gob_start =
s->rtp_payload_size &&
2970 current_packet_size >=
s->rtp_payload_size &&
2973 if(
s->start_mb_y ==
mb_y &&
mb_y > 0 &&
mb_x==0) is_gob_start=1;
2975 switch(
s->codec_id){
2978 if(!
s->h263_slice_structured)
2979 if(
s->mb_x ||
s->mb_y%
s->gob_index) is_gob_start=0;
2982 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
2984 if(
s->mb_skip_run) is_gob_start=0;
2987 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
3003 if (
s->error_rate &&
s->resync_mb_x +
s->resync_mb_y > 0) {
3005 int d = 100 /
s->error_rate;
3007 current_packet_size=0;
3008 s->pb.buf_ptr=
s->ptr_lastgob;
3013 switch(
s->codec_id){
3015 if (CONFIG_MPEG4_ENCODER) {
3022 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3029 if (CONFIG_H263_ENCODER) {
3038 s->misc_bits+=
bits -
s->last_bits;
3042 s->ptr_lastgob += current_packet_size;
3043 s->first_slice_line=1;
3044 s->resync_mb_x=
mb_x;
3045 s->resync_mb_y=
mb_y;
3049 if( (
s->resync_mb_x ==
s->mb_x)
3050 &&
s->resync_mb_y+1 ==
s->mb_y){
3051 s->first_slice_line=0;
3061 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3067 if(
s->data_partitioning){
3068 backup_s.pb2=
s->pb2;
3069 backup_s.tex_pb=
s->tex_pb;
3076 s->mv[0][0][0] =
s->p_mv_table[xy][0];
3077 s->mv[0][0][1] =
s->p_mv_table[xy][1];
3079 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3086 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3087 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3088 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3091 &dmin, &next_block, 0, 0);
3100 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3107 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3108 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3111 &dmin, &next_block, 0, 0);
3117 s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3118 s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3120 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3126 s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3127 s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3129 &dmin, &next_block,
s->mv[1][0][0],
s->mv[1][0][1]);
3135 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3136 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3137 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3138 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3140 &dmin, &next_block, 0, 0);
3147 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3148 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3149 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3152 &dmin, &next_block, 0, 0);
3159 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3160 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3161 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3164 &dmin, &next_block, 0, 0);
3170 for(dir=0; dir<2; dir++){
3172 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3173 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3174 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3178 &dmin, &next_block, 0, 0);
3187 &dmin, &next_block, 0, 0);
3188 if(
s->h263_pred ||
s->h263_aic){
3190 s->mbintra_table[
mb_x +
mb_y*
s->mb_stride]=1;
3198 const int last_qp= backup_s.qscale;
3202 static const int dquant_tab[4]={-1,1,-2,2};
3203 int storecoefs =
s->mb_intra &&
s->dc_val[0];
3211 s->mv[0][0][0] = best_s.
mv[0][0][0];
3212 s->mv[0][0][1] = best_s.
mv[0][0][1];
3213 s->mv[1][0][0] = best_s.
mv[1][0][0];
3214 s->mv[1][0][1] = best_s.
mv[1][0][1];
3217 for(; qpi<4; qpi++){
3218 int dquant= dquant_tab[qpi];
3220 if(qp < s->
avctx->
qmin || qp >
s->avctx->qmax)
3225 dc[
i]=
s->dc_val[0][
s->block_index[
i] ];
3226 memcpy(ac[
i],
s->ac_val[0][
s->block_index[
i]],
sizeof(int16_t)*16);
3231 &dmin, &next_block,
s->mv[mvdir][0][0],
s->mv[mvdir][0][1]);
3235 s->dc_val[0][
s->block_index[
i] ]=
dc[
i];
3236 memcpy(
s->ac_val[0][
s->block_index[
i]], ac[
i],
sizeof(int16_t)*16);
3244 int mx=
s->b_direct_mv_table[xy][0];
3245 int my=
s->b_direct_mv_table[xy][1];
3247 backup_s.dquant = 0;
3252 &dmin, &next_block, mx, my);
3255 backup_s.dquant = 0;
3260 &dmin, &next_block, 0, 0);
3265 coded |=
s->block_last_index[
i];
3268 memcpy(
s->mv, best_s.
mv,
sizeof(
s->mv));
3290 &dmin, &next_block, mx, my);
3295 s->current_picture.qscale_table[xy] = best_s.
qscale;
3301 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3304 if(
s->data_partitioning){
3307 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3308 s->pb2= backup_s.pb2;
3312 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3313 s->tex_pb= backup_s.tex_pb;
3317 if (CONFIG_H263_ENCODER &&
3322 s->hdsp.put_pixels_tab[0][0](
s->dest[0],
s->sc.rd_scratchpad ,
s->linesize ,16);
3323 s->hdsp.put_pixels_tab[1][0](
s->dest[1],
s->sc.rd_scratchpad + 16*
s->linesize ,
s->uvlinesize, 8);
3324 s->hdsp.put_pixels_tab[1][0](
s->dest[2],
s->sc.rd_scratchpad + 16*
s->linesize + 8,
s->uvlinesize, 8);
3330 int motion_x = 0, motion_y = 0;
3338 motion_x=
s->mv[0][0][0] = 0;
3339 motion_y=
s->mv[0][0][1] = 0;
3344 motion_x=
s->mv[0][0][0] =
s->p_mv_table[xy][0];
3345 motion_y=
s->mv[0][0][1] =
s->p_mv_table[xy][1];
3352 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3353 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3354 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3362 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3363 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3367 if (CONFIG_MPEG4_ENCODER) {
3370 motion_x=
s->b_direct_mv_table[xy][0];
3371 motion_y=
s->b_direct_mv_table[xy][1];
3376 if (CONFIG_MPEG4_ENCODER) {
3385 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3386 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3387 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3388 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3393 motion_x=
s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3394 motion_y=
s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3399 motion_x=
s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3400 motion_y=
s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3407 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3408 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3409 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3417 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3418 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3419 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3426 for(dir=0; dir<2; dir++){
3428 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3429 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3430 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3441 s->last_mv_dir =
s->mv_dir;
3443 if (CONFIG_H263_ENCODER &&
3452 s->p_mv_table[xy][0]=0;
3453 s->p_mv_table[xy][1]=0;
3460 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
3461 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
3463 s->encoding_error[0] +=
sse(
3464 s,
s->new_picture->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
3465 s->dest[0],
w,
h,
s->linesize);
3466 s->encoding_error[1] +=
sse(
3467 s,
s->new_picture->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3468 s->dest[1],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3469 s->encoding_error[2] +=
sse(
3470 s,
s->new_picture->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3471 s->dest[2],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3474 if(CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
3477 ff_dlog(
s->avctx,
"MB %d %d bits\n",
3483 if (CONFIG_MSMPEG4ENC &&
s->msmpeg4_version &&
s->msmpeg4_version<4 &&
s->pict_type ==
AV_PICTURE_TYPE_I)
3491 #define MERGE(field) dst->field += src->field; src->field=0
3514 for(
i=0;
i<64;
i++){
3527 if (
s->next_lambda){
3528 s->current_picture_ptr->f->quality =
3529 s->current_picture.f->quality =
s->next_lambda;
3530 if(!dry_run)
s->next_lambda= 0;
3531 }
else if (!
s->fixed_qscale) {
3533 s->current_picture_ptr->f->quality =
3534 s->current_picture.f->quality =
quality;
3535 if (
s->current_picture.f->quality < 0)
3539 if(
s->adaptive_quant){
3540 switch(
s->codec_id){
3542 if (CONFIG_MPEG4_ENCODER)
3548 if (CONFIG_H263_ENCODER)
3555 s->lambda=
s->lambda_table[0];
3558 s->lambda =
s->current_picture.f->quality;
3566 s->time =
s->current_picture_ptr->f->pts *
s->avctx->time_base.num;
3569 s->pb_time=
s->pp_time - (
s->last_non_b_time -
s->time);
3572 s->pp_time=
s->time -
s->last_non_b_time;
3573 s->last_non_b_time=
s->time;
3582 int context_count =
s->slice_context_count;
3585 s->me.mb_var_sum_temp =
3586 s->me.mc_mb_var_sum_temp = 0;
3590 if (
s->out_format ==
FMT_MPEG1 || (
s->h263_pred && !
s->msmpeg4_version))
3595 s->me.scene_change_score=0;
3600 if(
s->msmpeg4_version >= 3)
s->no_rounding=1;
3601 else s->no_rounding=0;
3604 s->no_rounding ^= 1;
3613 s->lambda=
s->last_lambda_for[
s->pict_type];
3615 s->lambda=
s->last_lambda_for[
s->last_non_b_pict_type];
3620 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
3621 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
3622 s->q_chroma_intra_matrix =
s->q_intra_matrix;
3623 s->q_chroma_intra_matrix16 =
s->q_intra_matrix16;
3627 for(
i=1;
i<context_count;
i++){
3638 s->lambda = (
s->lambda *
s->me_penalty_compensation + 128) >> 8;
3639 s->lambda2 = (
s->lambda2 * (int64_t)
s->me_penalty_compensation + 128) >> 8;
3650 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3653 if(!
s->fixed_qscale){
3655 s->avctx->execute(
s->avctx,
mb_var_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3658 for(
i=1;
i<context_count;
i++){
3661 s->mc_mb_var_sum =
s->me.mc_mb_var_sum_temp;
3662 s->mb_var_sum =
s->me. mb_var_sum_temp;
3665 if (
s->me.scene_change_score >
s->scenechange_threshold &&
3668 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3670 if(
s->msmpeg4_version >= 3)
3672 ff_dlog(
s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3673 s->mb_var_sum,
s->mc_mb_var_sum);
3714 for(dir=0; dir<2; dir++){
3720 s->b_field_mv_table[dir][
i][j], dir ?
s->b_code :
s->f_code,
type, 1);
3731 if (
s->qscale < 3 &&
s->max_qcoeff <= 128 &&
3740 if (
s->avctx->intra_matrix) {
3742 luma_matrix =
s->avctx->intra_matrix;
3744 if (
s->avctx->chroma_intra_matrix)
3745 chroma_matrix =
s->avctx->chroma_intra_matrix;
3749 int j =
s->idsp.idct_permutation[
i];
3751 s->chroma_intra_matrix[j] =
av_clip_uint8((chroma_matrix[
i] *
s->qscale) >> 3);
3754 s->y_dc_scale_table=
3756 s->chroma_intra_matrix[0] =
3759 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3761 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3765 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};
3766 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};
3767 for (
int i = 1;
i < 64;
i++) {
3773 s->y_dc_scale_table = y;
3774 s->c_dc_scale_table =
c;
3775 s->intra_matrix[0] = 13;
3776 s->chroma_intra_matrix[0] = 14;
3778 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3780 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3793 s->current_picture_ptr->f->pict_type =
3794 s->current_picture.f->pict_type =
s->pict_type;
3797 s->picture_in_gop_number=0;
3799 s->mb_x =
s->mb_y = 0;
3801 switch(
s->out_format) {
3802 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
3808 if (CONFIG_SPEEDHQ_ENCODER)
3812 if (CONFIG_H261_ENCODER)
3818 else if (CONFIG_MSMPEG4ENC &&
s->msmpeg4_version)
3820 else if (CONFIG_MPEG4_ENCODER &&
s->h263_pred) {
3833 else if (CONFIG_H263_ENCODER)
3837 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3844 s->header_bits=
bits -
s->last_bits;
3846 for(
i=1;
i<context_count;
i++){
3849 s->avctx->execute(
s->avctx,
encode_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3850 for(
i=1;
i<context_count;
i++){
3851 if (
s->pb.buf_end ==
s->thread_context[
i]->pb.buf)
3860 const int intra=
s->mb_intra;
3863 s->dct_count[intra]++;
3865 for(
i=0;
i<64;
i++){
3870 s->dct_error_sum[intra][
i] +=
level;
3871 level -=
s->dct_offset[intra][
i];
3874 s->dct_error_sum[intra][
i] -=
level;
3875 level +=
s->dct_offset[intra][
i];
3884 int16_t *
block,
int n,
3888 const uint8_t *scantable;
3889 const uint8_t *perm_scantable;
3891 unsigned int threshold1, threshold2;
3903 int coeff_count[64];
3904 int qmul, qadd, start_i, last_non_zero,
i,
dc;
3905 const int esc_length=
s->ac_esc_length;
3907 uint8_t * last_length;
3913 if(
s->dct_error_sum)
3919 else mpeg2_qscale =
qscale << 1;
3923 scantable=
s->intra_scantable.scantable;
3924 perm_scantable=
s->intra_scantable.permutated;
3941 qmat = n < 4 ?
s->q_intra_matrix[
qscale] :
s->q_chroma_intra_matrix[
qscale];
3942 matrix = n < 4 ?
s->intra_matrix :
s->chroma_intra_matrix;
3946 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
3947 length =
s->intra_chroma_ac_vlc_length;
3948 last_length=
s->intra_chroma_ac_vlc_last_length;
3950 length =
s->intra_ac_vlc_length;
3951 last_length=
s->intra_ac_vlc_last_length;
3954 scantable=
s->inter_scantable.scantable;
3955 perm_scantable=
s->inter_scantable.permutated;
3958 qmat =
s->q_inter_matrix[
qscale];
3960 length =
s->inter_ac_vlc_length;
3961 last_length=
s->inter_ac_vlc_last_length;
3966 threshold2= (threshold1<<1);
3968 for(
i=63;
i>=start_i;
i--) {
3969 const int j = scantable[
i];
3972 if(((
unsigned)(
level+threshold1))>threshold2){
3978 for(
i=start_i;
i<=last_non_zero;
i++) {
3979 const int j = scantable[
i];
3984 if(((
unsigned)(
level+threshold1))>threshold2){
4007 if(last_non_zero < start_i){
4008 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4009 return last_non_zero;
4012 score_tab[start_i]= 0;
4013 survivor[0]= start_i;
4016 for(
i=start_i;
i<=last_non_zero;
i++){
4017 int level_index, j, zero_distortion;
4019 int best_score=256*256*256*120;
4023 zero_distortion= dct_coeff*dct_coeff;
4025 for(level_index=0; level_index < coeff_count[
i]; level_index++){
4034 unquant_coeff= alevel*qmul + qadd;
4036 j =
s->idsp.idct_permutation[scantable[
i]];
4037 unquant_coeff = alevel *
matrix[j] * 8;
4039 j =
s->idsp.idct_permutation[scantable[
i]];
4041 unquant_coeff = (
int)( alevel * mpeg2_qscale *
matrix[j]) >> 4;
4042 unquant_coeff = (unquant_coeff - 1) | 1;
4044 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int)
matrix[j])) >> 5;
4045 unquant_coeff = (unquant_coeff - 1) | 1;
4050 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4052 if((
level&(~127)) == 0){
4053 for(j=survivor_count-1; j>=0; j--){
4054 int run=
i - survivor[j];
4056 score += score_tab[
i-
run];
4058 if(score < best_score){
4061 level_tab[
i+1]=
level-64;
4066 for(j=survivor_count-1; j>=0; j--){
4067 int run=
i - survivor[j];
4069 score += score_tab[
i-
run];
4070 if(score < last_score){
4073 last_level=
level-64;
4079 distortion += esc_length*
lambda;
4080 for(j=survivor_count-1; j>=0; j--){
4081 int run=
i - survivor[j];
4082 int score= distortion + score_tab[
i-
run];
4084 if(score < best_score){
4087 level_tab[
i+1]=
level-64;
4092 for(j=survivor_count-1; j>=0; j--){
4093 int run=
i - survivor[j];
4094 int score= distortion + score_tab[
i-
run];
4095 if(score < last_score){
4098 last_level=
level-64;
4106 score_tab[
i+1]= best_score;
4109 if(last_non_zero <= 27){
4110 for(; survivor_count; survivor_count--){
4111 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4115 for(; survivor_count; survivor_count--){
4116 if(score_tab[ survivor[survivor_count-1] ] <= best_score +
lambda)
4121 survivor[ survivor_count++ ]=
i+1;
4125 last_score= 256*256*256*120;
4126 for(
i= survivor[0];
i<=last_non_zero + 1;
i++){
4127 int score= score_tab[
i];
4131 if(score < last_score){
4134 last_level= level_tab[
i];
4135 last_run= run_tab[
i];
4140 s->coded_score[n] = last_score;
4143 last_non_zero= last_i - 1;
4144 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4146 if(last_non_zero < start_i)
4147 return last_non_zero;
4149 if(last_non_zero == 0 && start_i == 0){
4151 int best_score=
dc *
dc;
4153 for(
i=0;
i<coeff_count[0];
i++){
4156 int unquant_coeff, score, distortion;
4159 unquant_coeff= (alevel*qmul + qadd)>>3;
4161 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int)
matrix[0])) >> 5;
4162 unquant_coeff = (unquant_coeff - 1) | 1;
4164 unquant_coeff = (unquant_coeff + 4) >> 3;
4165 unquant_coeff<<= 3 + 3;
4167 distortion= (unquant_coeff -
dc) * (unquant_coeff -
dc);
4170 else score= distortion + esc_length*
lambda;
4172 if(score < best_score){
4174 best_level=
level - 64;
4177 block[0]= best_level;
4178 s->coded_score[n] = best_score -
dc*
dc;
4179 if(best_level == 0)
return -1;
4180 else return last_non_zero;
4186 block[ perm_scantable[last_non_zero] ]= last_level;
4189 for(;
i>start_i;
i -= run_tab[
i] + 1){
4190 block[ perm_scantable[
i-1] ]= level_tab[
i];
4193 return last_non_zero;
4208 if(
i==0)
s*= sqrt(0.5);
4209 if(j==0)
s*= sqrt(0.5);
4222 const uint8_t *scantable;
4223 const uint8_t *perm_scantable;
<