71 #define QUANT_BIAS_SHIFT 8
73 #define QMAT_SHIFT_MMX 16
92 const uint16_t *quant_matrix,
93 int bias,
int qmin,
int qmax,
int intra)
104 else qscale2 =
qscale << 1;
111 for (
i = 0;
i < 64;
i++) {
112 const int j =
s->idsp.idct_permutation[
i];
113 int64_t den = (int64_t) qscale2 * quant_matrix[j];
123 for (
i = 0;
i < 64;
i++) {
124 const int j =
s->idsp.idct_permutation[
i];
125 int64_t den =
ff_aanscales[
i] * (int64_t) qscale2 * quant_matrix[j];
135 for (
i = 0;
i < 64;
i++) {
136 const int j =
s->idsp.idct_permutation[
i];
137 int64_t den = (int64_t) qscale2 * quant_matrix[j];
157 for (
i = intra;
i < 64;
i++) {
169 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
176 if (
s->q_scale_type == 1 && 0) {
178 int bestdiff=INT_MAX;
186 if (
diff < bestdiff) {
195 s->qscale = av_clip(
s->qscale,
s->avctx->qmin,
s->vbv_ignore_qmax ? 31 :
s->avctx->qmax);
208 for (
i = 0;
i < 64;
i++) {
220 int8_t *
const qscale_table =
s->current_picture.qscale_table;
223 for (
i = 0;
i <
s->mb_num;
i++) {
224 unsigned int lam =
s->lambda_table[
s->mb_index2xy[
i]];
226 qscale_table[
s->mb_index2xy[
i]] = av_clip(qp,
s->avctx->qmin,
234 #define COPY(a) dst->a= src->a
259 for (
i = -16;
i < 16;
i++) {
265 s->input_picture_number = 0;
266 s->picture_in_gop_number = 0;
274 if (CONFIG_H263_ENCODER)
276 if (!
s->dct_quantize)
280 s->fast_dct_quantize =
s->dct_quantize;
281 if (
s->avctx->trellis)
292 int i,
ret, format_supported;
301 "only YUV420 and YUV422 are supported\n");
307 format_supported = 0;
316 format_supported = 1;
322 format_supported = 1;
324 if (!format_supported) {
354 #if FF_API_PRIVATE_OPT
371 "keyframe interval too large!, reducing it from %d to %d\n",
386 s->rtp_mode = !!
s->rtp_payload_size;
390 if (
s->intra_dc_precision < 0) {
391 s->intra_dc_precision += 8;
392 }
else if (
s->intra_dc_precision >= 8)
393 s->intra_dc_precision -= 8;
395 if (
s->intra_dc_precision < 0) {
397 "intra dc precision must be positive, note some applications use"
398 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
411 if (
s->gop_size <= 1) {
421 s->adaptive_quant = (
s->avctx->lumi_masking ||
422 s->avctx->dark_masking ||
423 s->avctx->temporal_cplx_masking ||
424 s->avctx->spatial_cplx_masking ||
425 s->avctx->p_masking ||
465 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
482 "impossible bitrate constraints, this will fail\n");
492 if (!
s->fixed_qscale &&
498 if (nbt <= INT_MAX) {
504 if (
s->avctx->rc_max_rate &&
505 s->avctx->rc_min_rate ==
s->avctx->rc_max_rate &&
509 s->avctx->rc_max_rate * 0xFFFFLL) {
511 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
512 "specified vbv buffer is too large for the given bitrate!\n");
524 "OBMC is only supported with simple mb decision\n");
533 if (
s->max_b_frames &&
540 if (
s->max_b_frames < 0) {
542 "max b frames must be 0 or positive for mpegvideo based encoders\n");
552 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
614 #if FF_API_PRIVATE_OPT
625 "mpeg2 style quantization not supported by codec\n");
645 "QP RD is no longer compatible with MJPEG or AMV\n");
649 #if FF_API_PRIVATE_OPT
656 if (
s->scenechange_threshold < 1000000000 &&
659 "closed gop with scene change detection are not supported yet, "
660 "set threshold to 1000000000\n");
668 "low delay forcing is only available for mpeg2, "
669 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
672 if (
s->max_b_frames != 0) {
674 "B-frames cannot be used with low delay\n");
679 if (
s->q_scale_type == 1) {
682 "non linear quant only supports qmax <= 28 currently\n");
693 if (
s->avctx->thread_count > 1 &&
700 "multi threaded encoding not supported by codec\n");
704 if (
s->avctx->thread_count < 1) {
706 "automatic thread number detection not supported by codec, "
716 #if FF_API_PRIVATE_OPT
727 "notice: b_frame_strategy only affects the first pass\n");
728 s->b_frame_strategy = 0;
742 s->inter_quant_bias = 0;
744 s->intra_quant_bias = 0;
757 s->avctx->time_base.den > (1 << 16) - 1) {
759 "timebase %d/%d not supported by MPEG 4 standard, "
760 "the maximum admitted value for the timebase denominator "
761 "is %d\n",
s->avctx->time_base.num,
s->avctx->time_base.den,
765 s->time_increment_bits =
av_log2(
s->avctx->time_base.den - 1) + 1;
771 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
776 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
783 if (!CONFIG_MJPEG_ENCODER ||
790 if (!CONFIG_H261_ENCODER)
794 "The specified picture size of %dx%d is not valid for the "
795 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
796 s->width,
s->height);
805 if (!CONFIG_H263_ENCODER)
808 s->width,
s->height) == 8) {
810 "The specified picture size of %dx%d is not valid for "
811 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
812 "352x288, 704x576, and 1408x1152. "
813 "Try H.263+.\n",
s->width,
s->height);
825 s->modified_quant =
s->h263_aic;
827 s->unrestricted_mv =
s->obmc ||
s->loop_filter ||
s->umvplus;
837 s->unrestricted_mv = 1;
851 s->modified_quant = 1;
855 s->unrestricted_mv = 0;
860 s->unrestricted_mv = 1;
861 s->low_delay =
s->max_b_frames ? 0 : 1;
862 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
867 s->unrestricted_mv = 1;
868 s->msmpeg4_version = 2;
875 s->unrestricted_mv = 1;
876 s->msmpeg4_version = 3;
877 s->flipflop_rounding = 1;
884 s->unrestricted_mv = 1;
885 s->msmpeg4_version = 4;
886 s->flipflop_rounding = 1;
893 s->unrestricted_mv = 1;
894 s->msmpeg4_version = 5;
895 s->flipflop_rounding = 1;
903 #if FF_API_PRIVATE_OPT
914 s->progressive_frame =
930 if (
s->msmpeg4_version) {
949 if (
s->noise_reduction) {
951 2 * 64 *
sizeof(uint16_t),
fail);
956 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) &&
s->modified_quant)
959 if (
s->slice_context_count > 1) {
963 s->h263_slice_structured = 1;
966 s->quant_precision = 5;
968 #if FF_API_PRIVATE_OPT
981 ff_set_cmp(&
s->mecc,
s->mecc.ildct_cmp,
s->avctx->ildct_cmp);
982 ff_set_cmp(&
s->mecc,
s->mecc.frame_skip_cmp,
s->frame_skip_cmp);
984 if (CONFIG_H261_ENCODER &&
s->out_format ==
FMT_H261)
986 if (CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
991 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
996 for (
i = 0;
i < 64;
i++) {
997 int j =
s->idsp.idct_permutation[
i];
1003 s->intra_matrix[j] =
1007 s->chroma_intra_matrix[j] =
1011 if (
s->avctx->intra_matrix)
1012 s->intra_matrix[j] =
s->avctx->intra_matrix[
i];
1013 if (
s->avctx->inter_matrix)
1014 s->inter_matrix[j] =
s->avctx->inter_matrix[
i];
1021 s->intra_matrix,
s->intra_quant_bias,
avctx->
qmin,
1024 s->inter_matrix,
s->inter_quant_bias,
avctx->
qmin,
1031 #if FF_API_PRIVATE_OPT
1041 if (
s->b_frame_strategy == 2) {
1042 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1044 if (!
s->tmp_frames[
i])
1048 s->tmp_frames[
i]->width =
s->width >>
s->brd_scale;
1049 s->tmp_frames[
i]->height =
s->height >>
s->brd_scale;
1079 if (CONFIG_MJPEG_ENCODER &&
1094 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
1095 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
1096 s->q_chroma_intra_matrix=
NULL;
1097 s->q_chroma_intra_matrix16=
NULL;
1114 for (y = 0; y < 16; y++) {
1115 for (x = 0; x < 16; x++) {
1130 h =
s->height & ~15;
1132 for (y = 0; y <
h; y += 16) {
1133 for (x = 0; x <
w; x += 16) {
1140 acc += sae + 500 < sad;
1149 s->chroma_x_shift,
s->chroma_y_shift,
s->out_format,
1150 s->mb_stride,
s->mb_width,
s->mb_height,
s->b8_stride,
1151 &
s->linesize, &
s->uvlinesize);
1158 int i, display_picture_number = 0,
ret;
1159 int encoding_delay =
s->max_b_frames ?
s->max_b_frames
1160 : (
s->low_delay ? 0 : 1);
1161 int flush_offset = 1;
1166 display_picture_number =
s->input_picture_number++;
1170 int64_t last =
s->user_specified_pts;
1174 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1179 if (!
s->low_delay && display_picture_number == 1)
1180 s->dts_delta =
pts - last;
1182 s->user_specified_pts =
pts;
1185 s->user_specified_pts =
1186 pts =
s->user_specified_pts + 1;
1188 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1191 pts = display_picture_number;
1195 if (!pic_arg->
buf[0] ||
1197 pic_arg->
linesize[1] !=
s->uvlinesize ||
1200 if ((
s->width & 15) || (
s->height & 15))
1208 pic_arg->
linesize[1],
s->linesize,
s->uvlinesize);
1214 pic = &
s->picture[
i];
1231 int h_chroma_shift, v_chroma_shift;
1236 for (
i = 0;
i < 3;
i++) {
1238 int dst_stride =
i ?
s->uvlinesize :
s->linesize;
1239 int h_shift =
i ? h_chroma_shift : 0;
1240 int v_shift =
i ? v_chroma_shift : 0;
1241 int w =
s->width >> h_shift;
1242 int h =
s->height >> v_shift;
1248 && !
s->progressive_sequence
1249 &&
FFALIGN(
s->height, 32) -
s->height > 16)
1252 if (!
s->avctx->rc_buffer_size)
1255 if (src_stride == dst_stride)
1256 memcpy(dst,
src, src_stride *
h);
1261 memcpy(dst2,
src,
w);
1266 if ((
s->width & 15) || (
s->height & (vpad-1))) {
1267 s->mpvencdsp.draw_edges(dst, dst_stride,
1286 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1287 if (
s->input_picture[flush_offset])
1290 if (flush_offset <= 1)
1293 encoding_delay = encoding_delay - flush_offset + 1;
1298 s->input_picture[
i - flush_offset] =
s->input_picture[
i];
1300 s->input_picture[encoding_delay] = (
Picture*) pic;
1309 int64_t score64 = 0;
1313 const int bw =
plane ? 1 : 2;
1314 for (y = 0; y <
s->mb_height * bw; y++) {
1315 for (x = 0; x <
s->mb_width * bw; x++) {
1316 int off = p->
shared ? 0 : 16;
1319 int v =
s->mecc.frame_skip_cmp[1](
s, dptr, rptr,
stride, 8);
1321 switch (
FFABS(
s->frame_skip_exp)) {
1322 case 0: score =
FFMAX(score, v);
break;
1323 case 1: score +=
FFABS(v);
break;
1324 case 2: score64 += v * (int64_t)v;
break;
1325 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1326 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1335 if (
s->frame_skip_exp < 0)
1336 score64 = pow(score64 / (
double)(
s->mb_width *
s->mb_height),
1337 -1.0/
s->frame_skip_exp);
1341 if (score64 < ((
s->frame_skip_factor * (int64_t)
s->lambda) >> 8))
1373 const int scale =
s->brd_scale;
1374 int width =
s->width >> scale;
1375 int height =
s->height >> scale;
1377 int64_t best_rd = INT64_MAX;
1378 int best_b_count = -1;
1389 b_lambda = p_lambda;
1393 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1394 Picture pre_input, *pre_input_ptr =
i ?
s->input_picture[
i - 1] :
1395 s->next_picture_ptr;
1398 if (pre_input_ptr && (!
i ||
s->input_picture[
i - 1])) {
1399 pre_input = *pre_input_ptr;
1408 s->mpvencdsp.shrink[scale](
s->tmp_frames[
i]->data[0],
1409 s->tmp_frames[
i]->linesize[0],
1413 s->mpvencdsp.shrink[scale](
s->tmp_frames[
i]->data[1],
1414 s->tmp_frames[
i]->linesize[1],
1418 s->mpvencdsp.shrink[scale](
s->tmp_frames[
i]->data[2],
1419 s->tmp_frames[
i]->linesize[2],
1426 for (j = 0; j <
s->max_b_frames + 1; j++) {
1430 if (!
s->input_picture[j])
1441 c->mb_decision =
s->avctx->mb_decision;
1442 c->me_cmp =
s->avctx->me_cmp;
1443 c->mb_cmp =
s->avctx->mb_cmp;
1444 c->me_sub_cmp =
s->avctx->me_sub_cmp;
1446 c->time_base =
s->avctx->time_base;
1447 c->max_b_frames =
s->max_b_frames;
1464 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1465 int is_p =
i % (j + 1) == j ||
i ==
s->max_b_frames;
1467 s->tmp_frames[
i + 1]->pict_type = is_p ?
1469 s->tmp_frames[
i + 1]->quality = is_p ? p_lambda : b_lambda;
1488 rd +=
c->error[0] +
c->error[1] +
c->error[2];
1501 return best_b_count;
1509 s->reordered_input_picture[
i - 1] =
s->reordered_input_picture[
i];
1513 if (!
s->reordered_input_picture[0] &&
s->input_picture[0]) {
1514 if (
s->frame_skip_threshold ||
s->frame_skip_factor) {
1515 if (
s->picture_in_gop_number <
s->gop_size &&
1516 s->next_picture_ptr &&
1528 !
s->next_picture_ptr ||
s->intra_only) {
1529 s->reordered_input_picture[0] =
s->input_picture[0];
1531 s->reordered_input_picture[0]->f->coded_picture_number =
1532 s->coded_picture_number++;
1537 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1538 int pict_num =
s->input_picture[0]->f->display_picture_number +
i;
1540 if (pict_num >=
s->rc_context.num_entries)
1542 if (!
s->input_picture[
i]) {
1547 s->input_picture[
i]->f->pict_type =
1548 s->rc_context.entry[pict_num].new_pict_type;
1552 if (
s->b_frame_strategy == 0) {
1553 b_frames =
s->max_b_frames;
1554 while (b_frames && !
s->input_picture[b_frames])
1556 }
else if (
s->b_frame_strategy == 1) {
1557 for (
i = 1;
i <
s->max_b_frames + 1;
i++) {
1558 if (
s->input_picture[
i] &&
1559 s->input_picture[
i]->b_frame_score == 0) {
1560 s->input_picture[
i]->b_frame_score =
1562 s->input_picture[
i ]->f->data[0],
1563 s->input_picture[
i - 1]->f->data[0],
1567 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1568 if (!
s->input_picture[
i] ||
1569 s->input_picture[
i]->b_frame_score - 1 >
1570 s->mb_num /
s->b_sensitivity)
1574 b_frames =
FFMAX(0,
i - 1);
1577 for (
i = 0;
i < b_frames + 1;
i++) {
1578 s->input_picture[
i]->b_frame_score = 0;
1580 }
else if (
s->b_frame_strategy == 2) {
1588 for (
i = b_frames - 1;
i >= 0;
i--) {
1589 int type =
s->input_picture[
i]->f->pict_type;
1594 b_frames ==
s->max_b_frames) {
1596 "warning, too many B-frames in a row\n");
1599 if (
s->picture_in_gop_number + b_frames >=
s->gop_size) {
1601 s->gop_size >
s->picture_in_gop_number) {
1602 b_frames =
s->gop_size -
s->picture_in_gop_number - 1;
1614 s->reordered_input_picture[0] =
s->input_picture[b_frames];
1617 s->reordered_input_picture[0]->f->coded_picture_number =
1618 s->coded_picture_number++;
1619 for (
i = 0;
i < b_frames;
i++) {
1620 s->reordered_input_picture[
i + 1] =
s->input_picture[
i];
1621 s->reordered_input_picture[
i + 1]->f->pict_type =
1623 s->reordered_input_picture[
i + 1]->f->coded_picture_number =
1624 s->coded_picture_number++;
1631 if (
s->reordered_input_picture[0]) {
1632 s->reordered_input_picture[0]->reference =
1633 s->reordered_input_picture[0]->f->pict_type !=
1639 if (
s->reordered_input_picture[0]->shared ||
s->avctx->rc_buffer_size) {
1647 pic = &
s->picture[
i];
1649 pic->
reference =
s->reordered_input_picture[0]->reference;
1660 s->reordered_input_picture[0]->shared = 0;
1662 s->current_picture_ptr = pic;
1665 s->current_picture_ptr =
s->reordered_input_picture[0];
1666 for (
i = 0;
i < 4;
i++) {
1672 s->current_picture_ptr)) < 0)
1675 s->picture_number =
s->new_picture.f->display_picture_number;
1682 if (
s->unrestricted_mv &&
1683 s->current_picture.reference &&
1686 int hshift =
desc->log2_chroma_w;
1687 int vshift =
desc->log2_chroma_h;
1688 s->mpvencdsp.draw_edges(
s->current_picture.f->data[0],
1689 s->current_picture.f->linesize[0],
1690 s->h_edge_pos,
s->v_edge_pos,
1693 s->mpvencdsp.draw_edges(
s->current_picture.f->data[1],
1694 s->current_picture.f->linesize[1],
1695 s->h_edge_pos >> hshift,
1696 s->v_edge_pos >> vshift,
1700 s->mpvencdsp.draw_edges(
s->current_picture.f->data[2],
1701 s->current_picture.f->linesize[2],
1702 s->h_edge_pos >> hshift,
1703 s->v_edge_pos >> vshift,
1711 s->last_pict_type =
s->pict_type;
1712 s->last_lambda_for [
s->pict_type] =
s->current_picture_ptr->f->quality;
1714 s->last_non_b_pict_type =
s->pict_type;
1716 #if FF_API_CODED_FRAME
1722 #if FF_API_ERROR_FRAME
1724 memcpy(
s->current_picture.f->error,
s->current_picture.encoding_error,
1725 sizeof(
s->current_picture.encoding_error));
1734 for (intra = 0; intra < 2; intra++) {
1735 if (
s->dct_count[intra] > (1 << 16)) {
1736 for (
i = 0;
i < 64;
i++) {
1737 s->dct_error_sum[intra][
i] >>= 1;
1739 s->dct_count[intra] >>= 1;
1742 for (
i = 0;
i < 64;
i++) {
1743 s->dct_offset[intra][
i] = (
s->noise_reduction *
1744 s->dct_count[intra] +
1745 s->dct_error_sum[intra][
i] / 2) /
1746 (
s->dct_error_sum[intra][
i] + 1);
1757 s->last_picture_ptr !=
s->next_picture_ptr &&
1758 s->last_picture_ptr->f->buf[0]) {
1762 s->current_picture_ptr->f->pict_type =
s->pict_type;
1767 s->current_picture_ptr)) < 0)
1771 s->last_picture_ptr =
s->next_picture_ptr;
1773 s->next_picture_ptr =
s->current_picture_ptr;
1776 if (
s->last_picture_ptr) {
1778 if (
s->last_picture_ptr->f->buf[0] &&
1780 s->last_picture_ptr)) < 0)
1783 if (
s->next_picture_ptr) {
1785 if (
s->next_picture_ptr->f->buf[0] &&
1787 s->next_picture_ptr)) < 0)
1793 for (
i = 0;
i < 4;
i++) {
1795 s->current_picture.f->data[
i] +=
1796 s->current_picture.f->linesize[
i];
1798 s->current_picture.f->linesize[
i] *= 2;
1799 s->last_picture.f->linesize[
i] *= 2;
1800 s->next_picture.f->linesize[
i] *= 2;
1805 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
1806 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
1808 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
1809 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
1811 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
1812 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
1815 if (
s->dct_error_sum) {
1824 const AVFrame *pic_arg,
int *got_packet)
1827 int i, stuffing_count,
ret;
1828 int context_count =
s->slice_context_count;
1830 s->vbv_ignore_qmax = 0;
1832 s->picture_in_gop_number++;
1842 if (
s->new_picture.f->data[0]) {
1843 int growing_buffer = context_count == 1 && !
pkt->
data && !
s->data_partitioning;
1852 s->mb_width*
s->mb_height*12);
1853 s->prev_mb_info =
s->last_mb_info =
s->mb_info_size = 0;
1856 for (
i = 0;
i < context_count;
i++) {
1857 int start_y =
s->thread_context[
i]->start_mb_y;
1859 int h =
s->mb_height;
1866 s->pict_type =
s->new_picture.f->pict_type;
1873 if (growing_buffer) {
1881 #if FF_API_STAT_BITS
1897 if (CONFIG_MJPEG_ENCODER &&
s->out_format ==
FMT_MJPEG)
1907 s->lambda <
s->lmax) {
1908 s->next_lambda =
FFMAX(
s->lambda + min_step,
s->lambda *
1909 (
s->qscale + 1) /
s->qscale);
1910 if (
s->adaptive_quant) {
1912 for (
i = 0;
i <
s->mb_height *
s->mb_stride;
i++)
1913 s->lambda_table[
i] =
1914 FFMAX(
s->lambda_table[
i] + min_step,
1915 s->lambda_table[
i] * (
s->qscale + 1) /
1921 if (
s->flipflop_rounding ||
1924 s->no_rounding ^= 1;
1927 s->time_base =
s->last_time_base;
1928 s->last_non_b_time =
s->time -
s->pp_time;
1930 for (
i = 0;
i < context_count;
i++) {
1934 s->vbv_ignore_qmax = 1;
1945 for (
i = 0;
i < 4;
i++) {
1946 s->current_picture_ptr->encoding_error[
i] =
s->current_picture.encoding_error[
i];
1947 avctx->
error[
i] +=
s->current_picture_ptr->encoding_error[
i];
1950 s->current_picture_ptr->encoding_error,
1956 s->misc_bits +
s->i_tex_bits +
1962 s->stuffing_bits = 8*stuffing_count;
1963 if (stuffing_count) {
1965 stuffing_count + 50) {
1970 switch (
s->codec_id) {
1973 while (stuffing_count--) {
1980 stuffing_count -= 4;
1981 while (stuffing_count--) {
1993 if (
s->avctx->rc_max_rate &&
1994 s->avctx->rc_min_rate ==
s->avctx->rc_max_rate &&
1997 s->avctx->rc_max_rate * 0xFFFFLL) {
2002 double inbits =
s->avctx->rc_max_rate *
2004 int minbits =
s->frame_bits - 8 *
2005 (
s->vbv_delay_ptr -
s->pb.buf - 1);
2006 double bits =
s->rc_context.buffer_index + minbits - inbits;
2010 "Internal error, negative bits\n");
2015 min_delay = (minbits * 90000LL +
s->avctx->rc_max_rate - 1) /
2016 s->avctx->rc_max_rate;
2022 s->vbv_delay_ptr[0] &= 0xF8;
2025 s->vbv_delay_ptr[2] &= 0x07;
2040 #if FF_API_VBV_DELAY
2046 s->total_bits +=
s->frame_bits;
2047 #if FF_API_STAT_BITS
2054 pkt->
pts =
s->current_picture.f->pts;
2056 if (!
s->current_picture.f->coded_picture_number)
2063 if (
s->current_picture.f->key_frame)
2073 if (!
s->picture[
i].reference)
2085 int n,
int threshold)
2087 static const char tab[64] = {
2088 3, 2, 2, 1, 1, 1, 1, 1,
2089 1, 1, 1, 1, 1, 1, 1, 1,
2090 1, 1, 1, 1, 1, 1, 1, 1,
2091 0, 0, 0, 0, 0, 0, 0, 0,
2092 0, 0, 0, 0, 0, 0, 0, 0,
2093 0, 0, 0, 0, 0, 0, 0, 0,
2094 0, 0, 0, 0, 0, 0, 0, 0,
2095 0, 0, 0, 0, 0, 0, 0, 0
2101 const int last_index =
s->block_last_index[
n];
2104 if (threshold < 0) {
2106 threshold = -threshold;
2111 if (last_index <= skip_dc - 1)
2114 for (
i = 0;
i <= last_index;
i++) {
2115 const int j =
s->intra_scantable.permutated[
i];
2118 if (skip_dc &&
i == 0)
2122 }
else if (
level > 1) {
2128 if (score >= threshold)
2130 for (
i = skip_dc;
i <= last_index;
i++) {
2131 const int j =
s->intra_scantable.permutated[
i];
2135 s->block_last_index[
n] = 0;
2137 s->block_last_index[
n] = -1;
2144 const int maxlevel =
s->max_qcoeff;
2145 const int minlevel =
s->min_qcoeff;
2153 for (;
i <= last_index;
i++) {
2154 const int j =
s->intra_scantable.permutated[
i];
2157 if (
level > maxlevel) {
2160 }
else if (
level < minlevel) {
2170 "warning, clipping %d dct coefficients to %d..%d\n",
2178 for (y = 0; y < 8; y++) {
2179 for (x = 0; x < 8; x++) {
2185 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2186 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2187 int v = ptr[x2 + y2 *
stride];
2199 int motion_x,
int motion_y,
2200 int mb_block_height,
2205 int16_t orig[12][64];
2206 const int mb_x =
s->mb_x;
2207 const int mb_y =
s->mb_y;
2211 int uv_dct_offset =
s->uvlinesize * 8;
2212 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2213 ptrdiff_t wrap_y, wrap_c;
2215 for (
i = 0;
i < mb_block_count;
i++)
2216 skip_dct[
i] =
s->skipdct;
2218 if (
s->adaptive_quant) {
2219 const int last_qp =
s->qscale;
2220 const int mb_xy =
mb_x +
mb_y *
s->mb_stride;
2222 s->lambda =
s->lambda_table[mb_xy];
2226 s->qscale =
s->current_picture_ptr->qscale_table[mb_xy];
2227 s->dquant =
s->qscale - last_qp;
2230 s->dquant = av_clip(
s->dquant, -2, 2);
2248 wrap_y =
s->linesize;
2249 wrap_c =
s->uvlinesize;
2250 ptr_y =
s->new_picture.f->data[0] +
2252 ptr_cb =
s->new_picture.f->data[1] +
2253 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2254 ptr_cr =
s->new_picture.f->data[2] +
2255 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2258 uint8_t *ebuf =
s->sc.edge_emu_buffer + 38 * wrap_y;
2259 int cw = (
s->width +
s->chroma_x_shift) >>
s->chroma_x_shift;
2260 int ch = (
s->height +
s->chroma_y_shift) >>
s->chroma_y_shift;
2261 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2264 s->width,
s->height);
2266 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2268 mb_block_width, mb_block_height,
2269 mb_x * mb_block_width,
mb_y * mb_block_height,
2271 ptr_cb = ebuf + 16 * wrap_y;
2272 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2274 mb_block_width, mb_block_height,
2275 mb_x * mb_block_width,
mb_y * mb_block_height,
2277 ptr_cr = ebuf + 16 * wrap_y + 16;
2282 int progressive_score, interlaced_score;
2284 s->interlaced_dct = 0;
2285 progressive_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
NULL, wrap_y, 8) +
2286 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y * 8,
2287 NULL, wrap_y, 8) - 400;
2289 if (progressive_score > 0) {
2290 interlaced_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
2291 NULL, wrap_y * 2, 8) +
2292 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y,
2293 NULL, wrap_y * 2, 8);
2294 if (progressive_score > interlaced_score) {
2295 s->interlaced_dct = 1;
2298 uv_dct_offset = wrap_c;
2307 s->pdsp.get_pixels(
s->block[0], ptr_y, wrap_y);
2308 s->pdsp.get_pixels(
s->block[1], ptr_y + 8, wrap_y);
2309 s->pdsp.get_pixels(
s->block[2], ptr_y +
dct_offset, wrap_y);
2310 s->pdsp.get_pixels(
s->block[3], ptr_y +
dct_offset + 8, wrap_y);
2316 s->pdsp.get_pixels(
s->block[4], ptr_cb, wrap_c);
2317 s->pdsp.get_pixels(
s->block[5], ptr_cr, wrap_c);
2318 if (!
s->chroma_y_shift &&
s->chroma_x_shift) {
2319 s->pdsp.get_pixels(
s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2320 s->pdsp.get_pixels(
s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2321 }
else if (!
s->chroma_y_shift && !
s->chroma_x_shift) {
2322 s->pdsp.get_pixels(
s->block[ 6], ptr_cb + 8, wrap_c);
2323 s->pdsp.get_pixels(
s->block[ 7], ptr_cr + 8, wrap_c);
2324 s->pdsp.get_pixels(
s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2325 s->pdsp.get_pixels(
s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2326 s->pdsp.get_pixels(
s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2327 s->pdsp.get_pixels(
s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2333 uint8_t *dest_y, *dest_cb, *dest_cr;
2335 dest_y =
s->dest[0];
2336 dest_cb =
s->dest[1];
2337 dest_cr =
s->dest[2];
2340 op_pix =
s->hdsp.put_pixels_tab;
2341 op_qpix =
s->qdsp.put_qpel_pixels_tab;
2343 op_pix =
s->hdsp.put_no_rnd_pixels_tab;
2344 op_qpix =
s->qdsp.put_no_rnd_qpel_pixels_tab;
2349 s->last_picture.f->data,
2351 op_pix =
s->hdsp.avg_pixels_tab;
2352 op_qpix =
s->qdsp.avg_qpel_pixels_tab;
2356 s->next_picture.f->data,
2361 int progressive_score, interlaced_score;
2363 s->interlaced_dct = 0;
2364 progressive_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2365 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y * 8,
2370 progressive_score -= 400;
2372 if (progressive_score > 0) {
2373 interlaced_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y,
2375 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y,
2379 if (progressive_score > interlaced_score) {
2380 s->interlaced_dct = 1;
2383 uv_dct_offset = wrap_c;
2391 s->pdsp.diff_pixels(
s->block[0], ptr_y, dest_y, wrap_y);
2392 s->pdsp.diff_pixels(
s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2395 s->pdsp.diff_pixels(
s->block[3], ptr_y +
dct_offset + 8,
2402 s->pdsp.diff_pixels(
s->block[4], ptr_cb, dest_cb, wrap_c);
2403 s->pdsp.diff_pixels(
s->block[5], ptr_cr, dest_cr, wrap_c);
2404 if (!
s->chroma_y_shift) {
2405 s->pdsp.diff_pixels(
s->block[6], ptr_cb + uv_dct_offset,
2406 dest_cb + uv_dct_offset, wrap_c);
2407 s->pdsp.diff_pixels(
s->block[7], ptr_cr + uv_dct_offset,
2408 dest_cr + uv_dct_offset, wrap_c);
2412 if (
s->current_picture.mc_mb_var[
s->mb_stride *
mb_y +
mb_x] <
2413 2 *
s->qscale *
s->qscale) {
2415 if (
s->mecc.sad[1](
NULL, ptr_y, dest_y, wrap_y, 8) < 20 *
s->qscale)
2417 if (
s->mecc.sad[1](
NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 *
s->qscale)
2420 wrap_y, 8) < 20 *
s->qscale)
2423 wrap_y, 8) < 20 *
s->qscale)
2425 if (
s->mecc.sad[1](
NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 *
s->qscale)
2427 if (
s->mecc.sad[1](
NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 *
s->qscale)
2429 if (!
s->chroma_y_shift) {
2430 if (
s->mecc.sad[1](
NULL, ptr_cb + uv_dct_offset,
2431 dest_cb + uv_dct_offset,
2432 wrap_c, 8) < 20 *
s->qscale)
2434 if (
s->mecc.sad[1](
NULL, ptr_cr + uv_dct_offset,
2435 dest_cr + uv_dct_offset,
2436 wrap_c, 8) < 20 *
s->qscale)
2442 if (
s->quantizer_noise_shaping) {
2455 if (!
s->chroma_y_shift) {
2463 memcpy(orig[0],
s->block[0],
sizeof(int16_t) * 64 * mb_block_count);
2469 for (
i = 0;
i < mb_block_count;
i++) {
2472 s->block_last_index[
i] =
s->dct_quantize(
s,
s->block[
i],
i,
s->qscale, &
overflow);
2481 s->block_last_index[
i] = -1;
2483 if (
s->quantizer_noise_shaping) {
2484 for (
i = 0;
i < mb_block_count;
i++) {
2486 s->block_last_index[
i] =
2488 orig[
i],
i,
s->qscale);
2493 if (
s->luma_elim_threshold && !
s->mb_intra)
2494 for (
i = 0;
i < 4;
i++)
2496 if (
s->chroma_elim_threshold && !
s->mb_intra)
2497 for (
i = 4;
i < mb_block_count;
i++)
2501 for (
i = 0;
i < mb_block_count;
i++) {
2502 if (
s->block_last_index[
i] == -1)
2503 s->coded_score[
i] = INT_MAX / 256;
2509 s->block_last_index[4] =
2510 s->block_last_index[5] = 0;
2512 s->block[5][0] = (1024 +
s->c_dc_scale / 2) /
s->c_dc_scale;
2513 if (!
s->chroma_y_shift) {
2514 for (
i=6;
i<12;
i++) {
2515 s->block_last_index[
i] = 0;
2516 s->block[
i][0] =
s->block[4][0];
2523 for (
i = 0;
i < mb_block_count;
i++) {
2525 if (
s->block_last_index[
i] > 0) {
2526 for (j = 63; j > 0; j--) {
2527 if (
s->block[
i][
s->intra_scantable.permutated[j]])
2530 s->block_last_index[
i] = j;
2536 switch(
s->codec_id){
2539 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2543 if (CONFIG_MPEG4_ENCODER)
2553 if (CONFIG_WMV2_ENCODER)
2557 if (CONFIG_H261_ENCODER)
2565 if (CONFIG_H263_ENCODER)
2570 if (CONFIG_MJPEG_ENCODER)
2588 memcpy(d->
last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2616 memcpy(d->
mv,
s->mv, 2*4*2*
sizeof(
int));
2617 memcpy(d->
last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2639 if(
s->data_partitioning){
2654 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2661 s->block=
s->blocks[*next_block];
2662 s->pb=
pb[*next_block];
2663 if(
s->data_partitioning){
2664 s->pb2 =
pb2 [*next_block];
2665 s->tex_pb=
tex_pb[*next_block];
2669 memcpy(dest_backup,
s->dest,
sizeof(
s->dest));
2670 s->dest[0] =
s->sc.rd_scratchpad;
2671 s->dest[1] =
s->sc.rd_scratchpad + 16*
s->linesize;
2672 s->dest[2] =
s->sc.rd_scratchpad + 16*
s->linesize + 8;
2679 if(
s->data_partitioning){
2687 score *=
s->lambda2;
2692 memcpy(
s->dest, dest_backup,
sizeof(
s->dest));
2710 else if(
w==8 &&
h==8)
2728 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
2729 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
2733 return s->mecc.nsse[0](
s,
s->new_picture.f->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
s->dest[0],
s->linesize, 16) +
2734 s->mecc.nsse[1](
s,
s->new_picture.f->data[1] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[1],
s->uvlinesize, 8) +
2735 s->mecc.nsse[1](
s,
s->new_picture.f->data[2] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[2],
s->uvlinesize, 8);
2737 return s->mecc.sse[0](
NULL,
s->new_picture.f->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
s->dest[0],
s->linesize, 16) +
2738 s->mecc.sse[1](
NULL,
s->new_picture.f->data[1] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[1],
s->uvlinesize, 8) +
2739 s->mecc.sse[1](
NULL,
s->new_picture.f->data[2] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[2],
s->uvlinesize, 8);
2742 return sse(
s,
s->new_picture.f->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
s->dest[0],
w,
h,
s->linesize)
2743 +
sse(
s,
s->new_picture.f->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*8,
s->dest[1],
w>>1,
h>>1,
s->uvlinesize)
2744 +
sse(
s,
s->new_picture.f->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*8,
s->dest[2],
w>>1,
h>>1,
s->uvlinesize);
2752 s->me.dia_size=
s->avctx->pre_dia_size;
2753 s->first_slice_line=1;
2754 for(
s->mb_y=
s->end_mb_y-1;
s->mb_y >=
s->start_mb_y;
s->mb_y--) {
2755 for(
s->mb_x=
s->mb_width-1;
s->mb_x >=0 ;
s->mb_x--) {
2758 s->first_slice_line=0;
2771 s->me.dia_size=
s->avctx->dia_size;
2772 s->first_slice_line=1;
2773 for(
s->mb_y=
s->start_mb_y;
s->mb_y <
s->end_mb_y;
s->mb_y++) {
2776 for(
s->mb_x=0;
s->mb_x <
s->mb_width;
s->mb_x++) {
2777 s->block_index[0]+=2;
2778 s->block_index[1]+=2;
2779 s->block_index[2]+=2;
2780 s->block_index[3]+=2;
2788 s->first_slice_line=0;
2803 uint8_t *pix =
s->new_picture.f->data[0] + (yy *
s->linesize) + xx;
2805 int sum =
s->mpvencdsp.pix_sum(pix,
s->linesize);
2807 varc = (
s->mpvencdsp.pix_norm1(pix,
s->linesize) -
2808 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2810 s->current_picture.mb_var [
s->mb_stride *
mb_y +
mb_x] = varc;
2811 s->current_picture.mb_mean[
s->mb_stride *
mb_y +
mb_x] = (sum+128)>>8;
2812 s->me.mb_var_sum_temp += varc;
2820 if(
s->partitioned_frame){
2825 }
else if(CONFIG_MJPEG_ENCODER &&
s->out_format ==
FMT_MJPEG){
2838 uint8_t *ptr =
s->mb_info_ptr +
s->mb_info_size - 12;
2840 int mba =
s->mb_x +
s->mb_width * (
s->mb_y %
s->gob_index);
2841 int gobn =
s->mb_y /
s->gob_index;
2843 if (CONFIG_H263_ENCODER)
2845 bytestream_put_le32(&ptr,
offset);
2846 bytestream_put_byte(&ptr,
s->qscale);
2847 bytestream_put_byte(&ptr, gobn);
2848 bytestream_put_le16(&ptr, mba);
2849 bytestream_put_byte(&ptr, pred_x);
2850 bytestream_put_byte(&ptr, pred_y);
2852 bytestream_put_byte(&ptr, 0);
2853 bytestream_put_byte(&ptr, 0);
2861 s->mb_info_size += 12;
2862 s->prev_mb_info =
s->last_mb_info;
2874 if (!
s->mb_info_size)
2875 s->mb_info_size += 12;
2882 &&
s->slice_context_count == 1
2883 &&
s->pb.buf ==
s->avctx->internal->byte_buffer) {
2884 int lastgob_pos =
s->ptr_lastgob -
s->pb.buf;
2885 int vbv_pos =
s->vbv_delay_ptr -
s->pb.buf;
2888 int new_buffer_size = 0;
2890 if ((
s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2898 s->avctx->internal->byte_buffer_size + size_increase);
2902 memcpy(new_buffer,
s->avctx->internal->byte_buffer,
s->avctx->internal->byte_buffer_size);
2903 av_free(
s->avctx->internal->byte_buffer);
2904 s->avctx->internal->byte_buffer = new_buffer;
2905 s->avctx->internal->byte_buffer_size = new_buffer_size;
2907 s->ptr_lastgob =
s->pb.buf + lastgob_pos;
2908 s->vbv_delay_ptr =
s->pb.buf + vbv_pos;
2918 int chr_h= 16>>
s->chroma_y_shift;
2947 s->last_dc[
i] = 128 <<
s->intra_dc_precision;
2949 s->current_picture.encoding_error[
i] = 0;
2952 s->last_dc[0] = 128*8/13;
2953 s->last_dc[1] = 128*8/14;
2954 s->last_dc[2] = 128*8/14;
2957 memset(
s->last_mv, 0,
sizeof(
s->last_mv));
2961 switch(
s->codec_id){
2965 if (CONFIG_H263_ENCODER)
2969 if(CONFIG_MPEG4_ENCODER &&
s->partitioned_frame)
2976 s->first_slice_line = 1;
2977 s->ptr_lastgob =
s->pb.buf;
2991 int size_increase =
s->avctx->internal->byte_buffer_size/4
2999 if(
s->data_partitioning){
3013 xy=
s->mb_y*
s->mb_stride +
s->mb_x;
3019 int current_packet_size, is_gob_start;
3021 current_packet_size= ((
put_bits_count(&
s->pb)+7)>>3) - (
s->ptr_lastgob -
s->pb.buf);
3023 is_gob_start =
s->rtp_payload_size &&
3024 current_packet_size >=
s->rtp_payload_size &&
3027 if(
s->start_mb_y ==
mb_y &&
mb_y > 0 &&
mb_x==0) is_gob_start=1;
3029 switch(
s->codec_id){
3032 if(!
s->h263_slice_structured)
3033 if(
s->mb_x ||
s->mb_y%
s->gob_index) is_gob_start=0;
3036 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
3038 if(
s->mb_skip_run) is_gob_start=0;
3041 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
3057 if (
s->error_rate &&
s->resync_mb_x +
s->resync_mb_y > 0) {
3059 int d = 100 /
s->error_rate;
3061 current_packet_size=0;
3062 s->pb.buf_ptr=
s->ptr_lastgob;
3067 #if FF_API_RTP_CALLBACK
3069 if (
s->avctx->rtp_callback){
3070 int number_mb = (
mb_y -
s->resync_mb_y)*
s->mb_width +
mb_x -
s->resync_mb_x;
3071 s->avctx->rtp_callback(
s->avctx,
s->ptr_lastgob, current_packet_size, number_mb);
3077 switch(
s->codec_id){
3079 if (CONFIG_MPEG4_ENCODER) {
3086 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3093 if (CONFIG_H263_ENCODER)
3100 s->misc_bits+=
bits -
s->last_bits;
3104 s->ptr_lastgob += current_packet_size;
3105 s->first_slice_line=1;
3106 s->resync_mb_x=
mb_x;
3107 s->resync_mb_y=
mb_y;
3111 if( (
s->resync_mb_x ==
s->mb_x)
3112 &&
s->resync_mb_y+1 ==
s->mb_y){
3113 s->first_slice_line=0;
3123 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3129 if(
s->data_partitioning){
3130 backup_s.pb2=
s->pb2;
3131 backup_s.tex_pb=
s->tex_pb;
3138 s->mv[0][0][0] =
s->p_mv_table[xy][0];
3139 s->mv[0][0][1] =
s->p_mv_table[xy][1];
3141 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3148 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3149 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3150 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3153 &dmin, &next_block, 0, 0);
3162 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3169 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3170 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3173 &dmin, &next_block, 0, 0);
3179 s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3180 s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3182 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3188 s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3189 s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3191 &dmin, &next_block,
s->mv[1][0][0],
s->mv[1][0][1]);
3197 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3198 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3199 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3200 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3202 &dmin, &next_block, 0, 0);
3209 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3210 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3211 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3214 &dmin, &next_block, 0, 0);
3221 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3222 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3223 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3226 &dmin, &next_block, 0, 0);
3232 for(dir=0; dir<2; dir++){
3234 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3235 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3236 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3240 &dmin, &next_block, 0, 0);
3249 &dmin, &next_block, 0, 0);
3250 if(
s->h263_pred ||
s->h263_aic){
3252 s->mbintra_table[
mb_x +
mb_y*
s->mb_stride]=1;
3260 const int last_qp= backup_s.qscale;
3264 static const int dquant_tab[4]={-1,1,-2,2};
3265 int storecoefs =
s->mb_intra &&
s->dc_val[0];
3273 s->mv[0][0][0] = best_s.
mv[0][0][0];
3274 s->mv[0][0][1] = best_s.
mv[0][0][1];
3275 s->mv[1][0][0] = best_s.
mv[1][0][0];
3276 s->mv[1][0][1] = best_s.
mv[1][0][1];
3279 for(; qpi<4; qpi++){
3280 int dquant= dquant_tab[qpi];
3282 if(qp < s->
avctx->
qmin || qp >
s->avctx->qmax)
3287 dc[
i]=
s->dc_val[0][
s->block_index[
i] ];
3288 memcpy(ac[
i],
s->ac_val[0][
s->block_index[
i]],
sizeof(int16_t)*16);
3293 &dmin, &next_block,
s->mv[mvdir][0][0],
s->mv[mvdir][0][1]);
3297 s->dc_val[0][
s->block_index[
i] ]=
dc[
i];
3298 memcpy(
s->ac_val[0][
s->block_index[
i]], ac[
i],
sizeof(int16_t)*16);
3306 int mx=
s->b_direct_mv_table[xy][0];
3307 int my=
s->b_direct_mv_table[xy][1];
3309 backup_s.dquant = 0;
3314 &dmin, &next_block, mx, my);
3317 backup_s.dquant = 0;
3322 &dmin, &next_block, 0, 0);
3327 coded |=
s->block_last_index[
i];
3330 memcpy(
s->mv, best_s.
mv,
sizeof(
s->mv));
3352 &dmin, &next_block, mx, my);
3357 s->current_picture.qscale_table[xy] = best_s.
qscale;
3366 if(
s->data_partitioning){
3370 s->pb2= backup_s.pb2;
3374 avpriv_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3375 s->tex_pb= backup_s.tex_pb;
3379 if (CONFIG_H263_ENCODER &&
3384 s->hdsp.put_pixels_tab[0][0](
s->dest[0],
s->sc.rd_scratchpad ,
s->linesize ,16);
3385 s->hdsp.put_pixels_tab[1][0](
s->dest[1],
s->sc.rd_scratchpad + 16*
s->linesize ,
s->uvlinesize, 8);
3386 s->hdsp.put_pixels_tab[1][0](
s->dest[2],
s->sc.rd_scratchpad + 16*
s->linesize + 8,
s->uvlinesize, 8);
3392 int motion_x = 0, motion_y = 0;
3400 motion_x=
s->mv[0][0][0] = 0;
3401 motion_y=
s->mv[0][0][1] = 0;
3406 motion_x=
s->mv[0][0][0] =
s->p_mv_table[xy][0];
3407 motion_y=
s->mv[0][0][1] =
s->p_mv_table[xy][1];
3414 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3415 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3416 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3424 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3425 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3429 if (CONFIG_MPEG4_ENCODER) {
3432 motion_x=
s->b_direct_mv_table[xy][0];
3433 motion_y=
s->b_direct_mv_table[xy][1];
3438 if (CONFIG_MPEG4_ENCODER) {
3447 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3448 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3449 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3450 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3455 motion_x=
s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3456 motion_y=
s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3461 motion_x=
s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3462 motion_y=
s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3469 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3470 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3471 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3479 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3480 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3481 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3488 for(dir=0; dir<2; dir++){
3490 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3491 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3492 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3503 s->last_mv_dir =
s->mv_dir;
3505 if (CONFIG_H263_ENCODER &&
3514 s->p_mv_table[xy][0]=0;
3515 s->p_mv_table[xy][1]=0;
3522 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
3523 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
3525 s->current_picture.encoding_error[0] +=
sse(
3526 s,
s->new_picture.f->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
3527 s->dest[0],
w,
h,
s->linesize);
3528 s->current_picture.encoding_error[1] +=
sse(
3529 s,
s->new_picture.f->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3530 s->dest[1],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3531 s->current_picture.encoding_error[2] +=
sse(
3532 s,
s->new_picture.f->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3533 s->dest[2],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3536 if(CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
3539 ff_dlog(
s->avctx,
"MB %d %d bits\n",
3550 #if FF_API_RTP_CALLBACK
3553 if (
s->avctx->rtp_callback) {
3554 int number_mb = (
mb_y -
s->resync_mb_y)*
s->mb_width -
s->resync_mb_x;
3558 s->avctx->rtp_callback(
s->avctx,
s->ptr_lastgob, pdif, number_mb);
3566 #define MERGE(field) dst->field += src->field; src->field=0
3593 for(
i=0;
i<64;
i++){
3606 if (
s->next_lambda){
3607 s->current_picture_ptr->f->quality =
3608 s->current_picture.f->quality =
s->next_lambda;
3609 if(!dry_run)
s->next_lambda= 0;
3610 }
else if (!
s->fixed_qscale) {
3612 s->current_picture_ptr->f->quality =
3613 s->current_picture.f->quality =
quality;
3614 if (
s->current_picture.f->quality < 0)
3618 if(
s->adaptive_quant){
3619 switch(
s->codec_id){
3621 if (CONFIG_MPEG4_ENCODER)
3627 if (CONFIG_H263_ENCODER)
3634 s->lambda=
s->lambda_table[0];
3637 s->lambda =
s->current_picture.f->quality;
3645 s->time =
s->current_picture_ptr->f->pts *
s->avctx->time_base.num;
3648 s->pb_time=
s->pp_time - (
s->last_non_b_time -
s->time);
3651 s->pp_time=
s->time -
s->last_non_b_time;
3652 s->last_non_b_time=
s->time;
3661 int context_count =
s->slice_context_count;
3666 s->me.mb_var_sum_temp =
3667 s->me.mc_mb_var_sum_temp = 0;
3676 s->me.scene_change_score=0;
3681 if(
s->msmpeg4_version >= 3)
s->no_rounding=1;
3682 else s->no_rounding=0;
3685 s->no_rounding ^= 1;
3694 s->lambda=
s->last_lambda_for[
s->pict_type];
3696 s->lambda=
s->last_lambda_for[
s->last_non_b_pict_type];
3701 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
3702 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
3703 s->q_chroma_intra_matrix =
s->q_intra_matrix;
3704 s->q_chroma_intra_matrix16 =
s->q_intra_matrix16;
3708 for(
i=1;
i<context_count;
i++){
3719 s->lambda = (
s->lambda *
s->me_penalty_compensation + 128) >> 8;
3720 s->lambda2 = (
s->lambda2 * (int64_t)
s->me_penalty_compensation + 128) >> 8;
3731 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3734 if(!
s->fixed_qscale){
3736 s->avctx->execute(
s->avctx,
mb_var_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3739 for(
i=1;
i<context_count;
i++){
3742 s->current_picture.mc_mb_var_sum=
s->current_picture_ptr->mc_mb_var_sum=
s->me.mc_mb_var_sum_temp;
3743 s->current_picture. mb_var_sum=
s->current_picture_ptr-> mb_var_sum=
s->me. mb_var_sum_temp;
3746 if (
s->me.scene_change_score >
s->scenechange_threshold &&
3749 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3751 if(
s->msmpeg4_version >= 3)
3753 ff_dlog(
s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3754 s->current_picture.mb_var_sum,
s->current_picture.mc_mb_var_sum);
3797 for(dir=0; dir<2; dir++){
3803 s->b_field_mv_table[dir][
i][j], dir ?
s->b_code :
s->f_code,
type, 1);
3814 if (
s->qscale < 3 &&
s->max_qcoeff <= 128 &&
3823 if (
s->avctx->intra_matrix) {
3825 luma_matrix =
s->avctx->intra_matrix;
3827 if (
s->avctx->chroma_intra_matrix)
3828 chroma_matrix =
s->avctx->chroma_intra_matrix;
3832 int j =
s->idsp.idct_permutation[
i];
3834 s->chroma_intra_matrix[j] = av_clip_uint8((chroma_matrix[
i] *
s->qscale) >> 3);
3835 s->
intra_matrix[j] = av_clip_uint8(( luma_matrix[
i] *
s->qscale) >> 3);
3837 s->y_dc_scale_table=
3839 s->chroma_intra_matrix[0] =
3842 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3844 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3848 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};
3849 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};
3856 s->y_dc_scale_table= y;
3857 s->c_dc_scale_table=
c;
3858 s->intra_matrix[0] = 13;
3859 s->chroma_intra_matrix[0] = 14;
3861 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3863 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3868 s->current_picture_ptr->f->key_frame =
3870 s->current_picture_ptr->f->pict_type =
3871 s->current_picture.f->pict_type =
s->pict_type;
3873 if (
s->current_picture.f->key_frame)
3874 s->picture_in_gop_number=0;
3876 s->mb_x =
s->mb_y = 0;
3878 switch(
s->out_format) {
3882 s->pred,
s->intra_matrix,
s->chroma_intra_matrix);
3885 if (CONFIG_H261_ENCODER)
3893 else if (CONFIG_MPEG4_ENCODER &&
s->h263_pred) {
3906 else if (CONFIG_H263_ENCODER)
3910 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3917 s->header_bits=
bits -
s->last_bits;
3919 for(
i=1;
i<context_count;
i++){
3922 s->avctx->execute(
s->avctx,
encode_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3923 for(
i=1;
i<context_count;
i++){
3924 if (
s->pb.buf_end ==
s->thread_context[
i]->pb.buf)
3933 const int intra=
s->mb_intra;
3936 s->dct_count[intra]++;
3938 for(
i=0;
i<64;
i++){
3943 s->dct_error_sum[intra][
i] +=
level;
3944 level -=
s->dct_offset[intra][
i];
3947 s->dct_error_sum[intra][
i] -=
level;
3948 level +=
s->dct_offset[intra][
i];
3960 const uint16_t *matrix;
3962 const uint8_t *perm_scantable;
3964 unsigned int threshold1, threshold2;
3976 int coeff_count[64];
3977 int qmul, qadd, start_i, last_non_zero,
i,
dc;
3978 const int esc_length=
s->ac_esc_length;
3986 if(
s->dct_error_sum)
3992 else mpeg2_qscale =
qscale << 1;
3996 scantable=
s->intra_scantable.scantable;
3997 perm_scantable=
s->intra_scantable.permutated;
4014 qmat =
n < 4 ?
s->q_intra_matrix[
qscale] :
s->q_chroma_intra_matrix[
qscale];
4015 matrix =
n < 4 ?
s->intra_matrix :
s->chroma_intra_matrix;
4019 if (
n > 3 &&
s->intra_chroma_ac_vlc_length) {
4020 length =
s->intra_chroma_ac_vlc_length;
4021 last_length=
s->intra_chroma_ac_vlc_last_length;
4023 length =
s->intra_ac_vlc_length;
4024 last_length=
s->intra_ac_vlc_last_length;
4027 scantable=
s->inter_scantable.scantable;
4028 perm_scantable=
s->inter_scantable.permutated;
4031 qmat =
s->q_inter_matrix[
qscale];
4032 matrix =
s->inter_matrix;
4033 length =
s->inter_ac_vlc_length;
4034 last_length=
s->inter_ac_vlc_last_length;
4039 threshold2= (threshold1<<1);
4041 for(
i=63;
i>=start_i;
i--) {
4042 const int j = scantable[
i];
4045 if(((
unsigned)(
level+threshold1))>threshold2){
4051 for(
i=start_i;
i<=last_non_zero;
i++) {
4052 const int j = scantable[
i];
4057 if(((
unsigned)(
level+threshold1))>threshold2){
4080 if(last_non_zero < start_i){
4081 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4082 return last_non_zero;
4085 score_tab[start_i]= 0;
4086 survivor[0]= start_i;
4089 for(
i=start_i;
i<=last_non_zero;
i++){
4090 int level_index, j, zero_distortion;
4092 int best_score=256*256*256*120;
4096 zero_distortion= dct_coeff*dct_coeff;
4098 for(level_index=0; level_index < coeff_count[
i]; level_index++){
4107 unquant_coeff= alevel*qmul + qadd;
4109 j =
s->idsp.idct_permutation[scantable[
i]];
4110 unquant_coeff = alevel * matrix[j] * 8;
4112 j =
s->idsp.idct_permutation[scantable[
i]];
4114 unquant_coeff = (
int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4115 unquant_coeff = (unquant_coeff - 1) | 1;
4117 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int) matrix[j])) >> 5;
4118 unquant_coeff = (unquant_coeff - 1) | 1;
4123 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4125 if((
level&(~127)) == 0){
4126 for(j=survivor_count-1; j>=0; j--){
4127 int run=
i - survivor[j];
4129 score += score_tab[
i-
run];
4131 if(score < best_score){
4134 level_tab[
i+1]=
level-64;
4139 for(j=survivor_count-1; j>=0; j--){
4140 int run=
i - survivor[j];
4142 score += score_tab[
i-
run];
4143 if(score < last_score){
4146 last_level=
level-64;
4152 distortion += esc_length*
lambda;
4153 for(j=survivor_count-1; j>=0; j--){
4154 int run=
i - survivor[j];
4155 int score= distortion + score_tab[
i-
run];
4157 if(score < best_score){
4160 level_tab[
i+1]=
level-64;
4165 for(j=survivor_count-1; j>=0; j--){
4166 int run=
i - survivor[j];
4167 int score= distortion + score_tab[
i-
run];
4168 if(score < last_score){
4171 last_level=
level-64;
4179 score_tab[
i+1]= best_score;
4182 if(last_non_zero <= 27){