73 #define QUANT_BIAS_SHIFT 8
75 #define QMAT_SHIFT_MMX 16
94 const uint16_t *quant_matrix,
95 int bias,
int qmin,
int qmax,
int intra)
106 else qscale2 =
qscale << 1;
113 for (
i = 0;
i < 64;
i++) {
114 const int j =
s->idsp.idct_permutation[
i];
115 int64_t den = (int64_t) qscale2 * quant_matrix[j];
125 for (
i = 0;
i < 64;
i++) {
126 const int j =
s->idsp.idct_permutation[
i];
127 int64_t den =
ff_aanscales[
i] * (int64_t) qscale2 * quant_matrix[j];
137 for (
i = 0;
i < 64;
i++) {
138 const int j =
s->idsp.idct_permutation[
i];
139 int64_t den = (int64_t) qscale2 * quant_matrix[j];
159 for (
i = intra;
i < 64;
i++) {
171 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
178 if (
s->q_scale_type == 1 && 0) {
180 int bestdiff=INT_MAX;
188 if (
diff < bestdiff) {
197 s->qscale =
av_clip(
s->qscale,
s->avctx->qmin,
s->vbv_ignore_qmax ? 31 :
s->avctx->qmax);
210 for (
i = 0;
i < 64;
i++) {
222 int8_t *
const qscale_table =
s->current_picture.qscale_table;
225 for (
i = 0;
i <
s->mb_num;
i++) {
226 unsigned int lam =
s->lambda_table[
s->mb_index2xy[
i]];
228 qscale_table[
s->mb_index2xy[
i]] =
av_clip(qp,
s->avctx->qmin,
236 #define COPY(a) dst->a= src->a
254 for (
int i = -16;
i < 16;
i++)
273 s->input_picture_number = 0;
274 s->picture_in_gop_number = 0;
282 if (CONFIG_H263_ENCODER)
284 if (!
s->dct_quantize)
288 s->fast_dct_quantize =
s->dct_quantize;
289 if (
s->avctx->trellis)
300 int i,
ret, format_supported;
309 "only YUV420 and YUV422 are supported\n");
315 format_supported = 0;
324 format_supported = 1;
330 format_supported = 1;
332 if (!format_supported) {
342 "only YUV420/YUV422/YUV444 are supported (no alpha support yet)\n");
371 #if FF_API_PRIVATE_OPT
388 "keyframe interval too large!, reducing it from %d to %d\n",
403 s->rtp_mode = !!
s->rtp_payload_size;
407 if (
s->intra_dc_precision < 0) {
408 s->intra_dc_precision += 8;
409 }
else if (
s->intra_dc_precision >= 8)
410 s->intra_dc_precision -= 8;
412 if (
s->intra_dc_precision < 0) {
414 "intra dc precision must be positive, note some applications use"
415 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
428 if (
s->gop_size <= 1) {
482 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
499 "impossible bitrate constraints, this will fail\n");
509 if (!
s->fixed_qscale &&
515 if (nbt <= INT_MAX) {
528 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
529 "specified vbv buffer is too large for the given bitrate!\n");
541 "OBMC is only supported with simple mb decision\n");
550 if (
s->max_b_frames &&
557 if (
s->max_b_frames < 0) {
559 "max b frames must be 0 or positive for mpegvideo based encoders\n");
569 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
631 #if FF_API_PRIVATE_OPT
642 "mpeg2 style quantization not supported by codec\n");
662 "QP RD is no longer compatible with MJPEG or AMV\n");
666 #if FF_API_PRIVATE_OPT
673 if (
s->scenechange_threshold < 1000000000 &&
676 "closed gop with scene change detection are not supported yet, "
677 "set threshold to 1000000000\n");
685 "low delay forcing is only available for mpeg2, "
686 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
689 if (
s->max_b_frames != 0) {
691 "B-frames cannot be used with low delay\n");
696 if (
s->q_scale_type == 1) {
699 "non linear quant only supports qmax <= 28 currently\n");
717 "multi threaded encoding not supported by codec\n");
723 "automatic thread number detection not supported by codec, "
733 #if FF_API_PRIVATE_OPT
744 "notice: b_frame_strategy only affects the first pass\n");
745 s->b_frame_strategy = 0;
759 s->inter_quant_bias = 0;
761 s->intra_quant_bias = 0;
776 "timebase %d/%d not supported by MPEG 4 standard, "
777 "the maximum admitted value for the timebase denominator "
788 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
793 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
800 if (!CONFIG_MJPEG_ENCODER)
810 if (!CONFIG_SPEEDHQ_ENCODER)
818 if (!CONFIG_H261_ENCODER)
822 "The specified picture size of %dx%d is not valid for the "
823 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
824 s->width,
s->height);
833 if (!CONFIG_H263_ENCODER)
836 s->width,
s->height) == 8) {
838 "The specified picture size of %dx%d is not valid for "
839 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
840 "352x288, 704x576, and 1408x1152. "
841 "Try H.263+.\n",
s->width,
s->height);
853 s->modified_quant =
s->h263_aic;
855 s->unrestricted_mv =
s->obmc ||
s->loop_filter ||
s->umvplus;
865 s->unrestricted_mv = 1;
879 s->modified_quant = 1;
883 s->unrestricted_mv = 0;
888 s->unrestricted_mv = 1;
889 s->low_delay =
s->max_b_frames ? 0 : 1;
890 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
895 s->unrestricted_mv = 1;
896 s->msmpeg4_version = 2;
903 s->unrestricted_mv = 1;
904 s->msmpeg4_version = 3;
905 s->flipflop_rounding = 1;
912 s->unrestricted_mv = 1;
913 s->msmpeg4_version = 4;
914 s->flipflop_rounding = 1;
921 s->unrestricted_mv = 1;
922 s->msmpeg4_version = 5;
923 s->flipflop_rounding = 1;
931 #if FF_API_PRIVATE_OPT
942 s->progressive_frame =
958 if (
s->msmpeg4_version) {
975 if (
s->noise_reduction) {
982 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) &&
s->modified_quant)
985 if (
s->slice_context_count > 1) {
989 s->h263_slice_structured = 1;
992 s->quant_precision = 5;
994 #if FF_API_PRIVATE_OPT
1008 ff_set_cmp(&
s->mecc,
s->mecc.frame_skip_cmp,
s->frame_skip_cmp);
1010 if (CONFIG_H261_ENCODER &&
s->out_format ==
FMT_H261)
1012 if (CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
1016 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
1021 for (
i = 0;
i < 64;
i++) {
1022 int j =
s->idsp.idct_permutation[
i];
1028 s->intra_matrix[j] =
1031 s->intra_matrix[j] =
1035 s->chroma_intra_matrix[j] =
1049 s->intra_matrix,
s->intra_quant_bias,
avctx->
qmin,
1052 s->inter_matrix,
s->inter_quant_bias,
avctx->
qmin,
1059 #if FF_API_PRIVATE_OPT
1069 if (
s->b_frame_strategy == 2) {
1070 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1072 if (!
s->tmp_frames[
i])
1076 s->tmp_frames[
i]->width =
s->width >>
s->brd_scale;
1077 s->tmp_frames[
i]->height =
s->height >>
s->brd_scale;
1104 if (CONFIG_MJPEG_ENCODER &&
1119 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
1120 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
1121 s->q_chroma_intra_matrix=
NULL;
1122 s->q_chroma_intra_matrix16=
NULL;
1139 for (y = 0; y < 16; y++) {
1140 for (x = 0; x < 16; x++) {
1155 h =
s->height & ~15;
1157 for (y = 0; y <
h; y += 16) {
1158 for (x = 0; x <
w; x += 16) {
1165 acc += sae + 500 < sad;
1174 s->chroma_x_shift,
s->chroma_y_shift,
s->out_format,
1175 s->mb_stride,
s->mb_width,
s->mb_height,
s->b8_stride,
1176 &
s->linesize, &
s->uvlinesize);
1183 int i, display_picture_number = 0,
ret;
1184 int encoding_delay =
s->max_b_frames ?
s->max_b_frames
1185 : (
s->low_delay ? 0 : 1);
1186 int flush_offset = 1;
1191 display_picture_number =
s->input_picture_number++;
1195 int64_t last =
s->user_specified_pts;
1199 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1204 if (!
s->low_delay && display_picture_number == 1)
1205 s->dts_delta =
pts - last;
1207 s->user_specified_pts =
pts;
1210 s->user_specified_pts =
1211 pts =
s->user_specified_pts + 1;
1213 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1216 pts = display_picture_number;
1220 if (!pic_arg->
buf[0] ||
1222 pic_arg->
linesize[1] !=
s->uvlinesize ||
1225 if ((
s->width & 15) || (
s->height & 15))
1233 pic_arg->
linesize[1],
s->linesize,
s->uvlinesize);
1239 pic = &
s->picture[
i];
1256 int h_chroma_shift, v_chroma_shift;
1261 for (
i = 0;
i < 3;
i++) {
1263 int dst_stride =
i ?
s->uvlinesize :
s->linesize;
1264 int h_shift =
i ? h_chroma_shift : 0;
1265 int v_shift =
i ? v_chroma_shift : 0;
1266 int w =
s->width >> h_shift;
1267 int h =
s->height >> v_shift;
1273 && !
s->progressive_sequence
1274 &&
FFALIGN(
s->height, 32) -
s->height > 16)
1277 if (!
s->avctx->rc_buffer_size)
1280 if (src_stride == dst_stride)
1281 memcpy(dst,
src, src_stride *
h);
1286 memcpy(dst2,
src,
w);
1291 if ((
s->width & 15) || (
s->height & (vpad-1))) {
1292 s->mpvencdsp.draw_edges(dst, dst_stride,
1311 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1312 if (
s->input_picture[flush_offset])
1315 if (flush_offset <= 1)
1318 encoding_delay = encoding_delay - flush_offset + 1;
1323 s->input_picture[
i - flush_offset] =
s->input_picture[
i];
1325 s->input_picture[encoding_delay] = (
Picture*) pic;
1334 int64_t score64 = 0;
1336 for (plane = 0; plane < 3; plane++) {
1338 const int bw = plane ? 1 : 2;
1339 for (y = 0; y <
s->mb_height * bw; y++) {
1340 for (x = 0; x <
s->mb_width * bw; x++) {
1341 int off = p->
shared ? 0 : 16;
1344 int v =
s->mecc.frame_skip_cmp[1](
s, dptr, rptr,
stride, 8);
1346 switch (
FFABS(
s->frame_skip_exp)) {
1347 case 0: score =
FFMAX(score, v);
break;
1348 case 1: score +=
FFABS(v);
break;
1349 case 2: score64 += v * (int64_t)v;
break;
1350 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1351 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1360 if (
s->frame_skip_exp < 0)
1361 score64 = pow(score64 / (
double)(
s->mb_width *
s->mb_height),
1362 -1.0/
s->frame_skip_exp);
1366 if (score64 < ((
s->frame_skip_factor * (int64_t)
s->lambda) >> 8))
1396 const int scale =
s->brd_scale;
1397 int width =
s->width >> scale;
1398 int height =
s->height >> scale;
1400 int64_t best_rd = INT64_MAX;
1401 int best_b_count = -1;
1416 b_lambda = p_lambda;
1420 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1421 Picture pre_input, *pre_input_ptr =
i ?
s->input_picture[
i - 1] :
1422 s->next_picture_ptr;
1425 if (pre_input_ptr && (!
i ||
s->input_picture[
i - 1])) {
1426 pre_input = *pre_input_ptr;
1435 s->mpvencdsp.shrink[scale](
s->tmp_frames[
i]->data[0],
1436 s->tmp_frames[
i]->linesize[0],
1440 s->mpvencdsp.shrink[scale](
s->tmp_frames[
i]->data[1],
1441 s->tmp_frames[
i]->linesize[1],
1445 s->mpvencdsp.shrink[scale](
s->tmp_frames[
i]->data[2],
1446 s->tmp_frames[
i]->linesize[2],
1453 for (j = 0; j <
s->max_b_frames + 1; j++) {
1457 if (!
s->input_picture[j])
1470 c->mb_decision =
s->avctx->mb_decision;
1471 c->me_cmp =
s->avctx->me_cmp;
1472 c->mb_cmp =
s->avctx->mb_cmp;
1473 c->me_sub_cmp =
s->avctx->me_sub_cmp;
1475 c->time_base =
s->avctx->time_base;
1476 c->max_b_frames =
s->max_b_frames;
1494 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1495 int is_p =
i % (j + 1) == j ||
i ==
s->max_b_frames;
1497 s->tmp_frames[
i + 1]->pict_type = is_p ?
1499 s->tmp_frames[
i + 1]->quality = is_p ? p_lambda : b_lambda;
1518 rd +=
c->error[0] +
c->error[1] +
c->error[2];
1536 return best_b_count;
1544 s->reordered_input_picture[
i - 1] =
s->reordered_input_picture[
i];
1548 if (!
s->reordered_input_picture[0] &&
s->input_picture[0]) {
1549 if (
s->frame_skip_threshold ||
s->frame_skip_factor) {
1550 if (
s->picture_in_gop_number <
s->gop_size &&
1551 s->next_picture_ptr &&
1563 !
s->next_picture_ptr ||
s->intra_only) {
1564 s->reordered_input_picture[0] =
s->input_picture[0];
1566 s->reordered_input_picture[0]->f->coded_picture_number =
1567 s->coded_picture_number++;
1572 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1573 int pict_num =
s->input_picture[0]->f->display_picture_number +
i;
1575 if (pict_num >=
s->rc_context.num_entries)
1577 if (!
s->input_picture[
i]) {
1582 s->input_picture[
i]->f->pict_type =
1583 s->rc_context.entry[pict_num].new_pict_type;
1587 if (
s->b_frame_strategy == 0) {
1588 b_frames =
s->max_b_frames;
1589 while (b_frames && !
s->input_picture[b_frames])
1591 }
else if (
s->b_frame_strategy == 1) {
1592 for (
i = 1;
i <
s->max_b_frames + 1;
i++) {
1593 if (
s->input_picture[
i] &&
1594 s->input_picture[
i]->b_frame_score == 0) {
1595 s->input_picture[
i]->b_frame_score =
1597 s->input_picture[
i ]->f->data[0],
1598 s->input_picture[
i - 1]->f->data[0],
1602 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1603 if (!
s->input_picture[
i] ||
1604 s->input_picture[
i]->b_frame_score - 1 >
1605 s->mb_num /
s->b_sensitivity)
1609 b_frames =
FFMAX(0,
i - 1);
1612 for (
i = 0;
i < b_frames + 1;
i++) {
1613 s->input_picture[
i]->b_frame_score = 0;
1615 }
else if (
s->b_frame_strategy == 2) {
1623 for (
i = b_frames - 1;
i >= 0;
i--) {
1624 int type =
s->input_picture[
i]->f->pict_type;
1629 b_frames ==
s->max_b_frames) {
1631 "warning, too many B-frames in a row\n");
1634 if (
s->picture_in_gop_number + b_frames >=
s->gop_size) {
1636 s->gop_size >
s->picture_in_gop_number) {
1637 b_frames =
s->gop_size -
s->picture_in_gop_number - 1;
1649 s->reordered_input_picture[0] =
s->input_picture[b_frames];
1652 s->reordered_input_picture[0]->f->coded_picture_number =
1653 s->coded_picture_number++;
1654 for (
i = 0;
i < b_frames;
i++) {
1655 s->reordered_input_picture[
i + 1] =
s->input_picture[
i];
1656 s->reordered_input_picture[
i + 1]->f->pict_type =
1658 s->reordered_input_picture[
i + 1]->f->coded_picture_number =
1659 s->coded_picture_number++;
1666 if (
s->reordered_input_picture[0]) {
1667 s->reordered_input_picture[0]->reference =
1668 s->reordered_input_picture[0]->f->pict_type !=
1674 if (
s->reordered_input_picture[0]->shared ||
s->avctx->rc_buffer_size) {
1682 pic = &
s->picture[
i];
1684 pic->
reference =
s->reordered_input_picture[0]->reference;
1695 s->reordered_input_picture[0]->shared = 0;
1697 s->current_picture_ptr = pic;
1700 s->current_picture_ptr =
s->reordered_input_picture[0];
1701 for (
i = 0;
i < 4;
i++) {
1702 if (
s->new_picture.f->data[
i])
1708 s->current_picture_ptr)) < 0)
1711 s->picture_number =
s->new_picture.f->display_picture_number;
1718 if (
s->unrestricted_mv &&
1719 s->current_picture.reference &&
1722 int hshift =
desc->log2_chroma_w;
1723 int vshift =
desc->log2_chroma_h;
1724 s->mpvencdsp.draw_edges(
s->current_picture.f->data[0],
1725 s->current_picture.f->linesize[0],
1726 s->h_edge_pos,
s->v_edge_pos,
1729 s->mpvencdsp.draw_edges(
s->current_picture.f->data[1],
1730 s->current_picture.f->linesize[1],
1731 s->h_edge_pos >> hshift,
1732 s->v_edge_pos >> vshift,
1736 s->mpvencdsp.draw_edges(
s->current_picture.f->data[2],
1737 s->current_picture.f->linesize[2],
1738 s->h_edge_pos >> hshift,
1739 s->v_edge_pos >> vshift,
1747 s->last_pict_type =
s->pict_type;
1748 s->last_lambda_for [
s->pict_type] =
s->current_picture_ptr->f->quality;
1750 s->last_non_b_pict_type =
s->pict_type;
1752 #if FF_API_CODED_FRAME
1758 #if FF_API_ERROR_FRAME
1760 memcpy(
s->current_picture.f->error,
s->current_picture.encoding_error,
1761 sizeof(
s->current_picture.encoding_error));
1770 for (intra = 0; intra < 2; intra++) {
1771 if (
s->dct_count[intra] > (1 << 16)) {
1772 for (
i = 0;
i < 64;
i++) {
1773 s->dct_error_sum[intra][
i] >>= 1;
1775 s->dct_count[intra] >>= 1;
1778 for (
i = 0;
i < 64;
i++) {
1779 s->dct_offset[intra][
i] = (
s->noise_reduction *
1780 s->dct_count[intra] +
1781 s->dct_error_sum[intra][
i] / 2) /
1782 (
s->dct_error_sum[intra][
i] + 1);
1793 s->last_picture_ptr !=
s->next_picture_ptr &&
1794 s->last_picture_ptr->f->buf[0]) {
1798 s->current_picture_ptr->f->pict_type =
s->pict_type;
1803 s->current_picture_ptr)) < 0)
1807 s->last_picture_ptr =
s->next_picture_ptr;
1809 s->next_picture_ptr =
s->current_picture_ptr;
1812 if (
s->last_picture_ptr) {
1814 if (
s->last_picture_ptr->f->buf[0] &&
1816 s->last_picture_ptr)) < 0)
1819 if (
s->next_picture_ptr) {
1821 if (
s->next_picture_ptr->f->buf[0] &&
1823 s->next_picture_ptr)) < 0)
1829 for (
i = 0;
i < 4;
i++) {
1831 s->current_picture.f->data[
i] +=
1832 s->current_picture.f->linesize[
i];
1834 s->current_picture.f->linesize[
i] *= 2;
1835 s->last_picture.f->linesize[
i] *= 2;
1836 s->next_picture.f->linesize[
i] *= 2;
1841 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
1842 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
1844 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
1845 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
1847 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
1848 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
1851 if (
s->dct_error_sum) {
1860 const AVFrame *pic_arg,
int *got_packet)
1863 int i, stuffing_count,
ret;
1864 int context_count =
s->slice_context_count;
1866 s->vbv_ignore_qmax = 0;
1868 s->picture_in_gop_number++;
1878 if (
s->new_picture.f->data[0]) {
1879 int growing_buffer = context_count == 1 && !
pkt->
data && !
s->data_partitioning;
1888 s->mb_width*
s->mb_height*12);
1889 s->prev_mb_info =
s->last_mb_info =
s->mb_info_size = 0;
1892 for (
i = 0;
i < context_count;
i++) {
1893 int start_y =
s->thread_context[
i]->start_mb_y;
1895 int h =
s->mb_height;
1902 s->pict_type =
s->new_picture.f->pict_type;
1909 if (growing_buffer) {
1917 #if FF_API_STAT_BITS
1933 if (CONFIG_MJPEG_ENCODER &&
s->out_format ==
FMT_MJPEG)
1943 s->lambda <
s->lmax) {
1944 s->next_lambda =
FFMAX(
s->lambda + min_step,
s->lambda *
1945 (
s->qscale + 1) /
s->qscale);
1946 if (
s->adaptive_quant) {
1948 for (
i = 0;
i <
s->mb_height *
s->mb_stride;
i++)
1949 s->lambda_table[
i] =
1950 FFMAX(
s->lambda_table[
i] + min_step,
1951 s->lambda_table[
i] * (
s->qscale + 1) /
1957 if (
s->flipflop_rounding ||
1960 s->no_rounding ^= 1;
1963 s->time_base =
s->last_time_base;
1964 s->last_non_b_time =
s->time -
s->pp_time;
1966 for (
i = 0;
i < context_count;
i++) {
1970 s->vbv_ignore_qmax = 1;
1981 for (
i = 0;
i < 4;
i++) {
1982 s->current_picture_ptr->encoding_error[
i] =
s->current_picture.encoding_error[
i];
1983 avctx->
error[
i] +=
s->current_picture_ptr->encoding_error[
i];
1986 s->current_picture_ptr->encoding_error,
1992 s->misc_bits +
s->i_tex_bits +
1998 s->stuffing_bits = 8*stuffing_count;
1999 if (stuffing_count) {
2001 stuffing_count + 50) {
2006 switch (
s->codec_id) {
2009 while (stuffing_count--) {
2016 stuffing_count -= 4;
2017 while (stuffing_count--) {
2023 s->stuffing_bits = 0;
2041 int minbits =
s->frame_bits - 8 *
2042 (
s->vbv_delay_ptr -
s->pb.buf - 1);
2043 double bits =
s->rc_context.buffer_index + minbits - inbits;
2047 "Internal error, negative bits\n");
2059 s->vbv_delay_ptr[0] &= 0xF8;
2062 s->vbv_delay_ptr[2] &= 0x07;
2077 #if FF_API_VBV_DELAY
2083 s->total_bits +=
s->frame_bits;
2084 #if FF_API_STAT_BITS
2091 pkt->
pts =
s->current_picture.f->pts;
2093 if (!
s->current_picture.f->coded_picture_number)
2100 if (
s->current_picture.f->key_frame)
2110 if (!
s->picture[
i].reference)
2122 int n,
int threshold)
2124 static const char tab[64] = {
2125 3, 2, 2, 1, 1, 1, 1, 1,
2126 1, 1, 1, 1, 1, 1, 1, 1,
2127 1, 1, 1, 1, 1, 1, 1, 1,
2128 0, 0, 0, 0, 0, 0, 0, 0,
2129 0, 0, 0, 0, 0, 0, 0, 0,
2130 0, 0, 0, 0, 0, 0, 0, 0,
2131 0, 0, 0, 0, 0, 0, 0, 0,
2132 0, 0, 0, 0, 0, 0, 0, 0
2137 int16_t *
block =
s->block[n];
2138 const int last_index =
s->block_last_index[n];
2141 if (threshold < 0) {
2143 threshold = -threshold;
2148 if (last_index <= skip_dc - 1)
2151 for (
i = 0;
i <= last_index;
i++) {
2152 const int j =
s->intra_scantable.permutated[
i];
2155 if (skip_dc &&
i == 0)
2159 }
else if (
level > 1) {
2165 if (score >= threshold)
2167 for (
i = skip_dc;
i <= last_index;
i++) {
2168 const int j =
s->intra_scantable.permutated[
i];
2172 s->block_last_index[n] = 0;
2174 s->block_last_index[n] = -1;
2181 const int maxlevel =
s->max_qcoeff;
2182 const int minlevel =
s->min_qcoeff;
2190 for (;
i <= last_index;
i++) {
2191 const int j =
s->intra_scantable.permutated[
i];
2194 if (
level > maxlevel) {
2197 }
else if (
level < minlevel) {
2207 "warning, clipping %d dct coefficients to %d..%d\n",
2215 for (y = 0; y < 8; y++) {
2216 for (x = 0; x < 8; x++) {
2222 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2223 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2224 int v = ptr[x2 + y2 *
stride];
2230 weight[x + 8 * y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2236 int motion_x,
int motion_y,
2237 int mb_block_height,
2242 int16_t orig[12][64];
2243 const int mb_x =
s->mb_x;
2244 const int mb_y =
s->mb_y;
2248 int uv_dct_offset =
s->uvlinesize * 8;
2249 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2250 ptrdiff_t wrap_y, wrap_c;
2252 for (
i = 0;
i < mb_block_count;
i++)
2253 skip_dct[
i] =
s->skipdct;
2255 if (
s->adaptive_quant) {
2256 const int last_qp =
s->qscale;
2257 const int mb_xy =
mb_x +
mb_y *
s->mb_stride;
2259 s->lambda =
s->lambda_table[mb_xy];
2263 s->qscale =
s->current_picture_ptr->qscale_table[mb_xy];
2264 s->dquant =
s->qscale - last_qp;
2285 wrap_y =
s->linesize;
2286 wrap_c =
s->uvlinesize;
2287 ptr_y =
s->new_picture.f->data[0] +
2289 ptr_cb =
s->new_picture.f->data[1] +
2290 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2291 ptr_cr =
s->new_picture.f->data[2] +
2292 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2295 uint8_t *ebuf =
s->sc.edge_emu_buffer + 38 * wrap_y;
2296 int cw = (
s->width +
s->chroma_x_shift) >>
s->chroma_x_shift;
2297 int ch = (
s->height +
s->chroma_y_shift) >>
s->chroma_y_shift;
2298 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2301 s->width,
s->height);
2303 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2305 mb_block_width, mb_block_height,
2306 mb_x * mb_block_width,
mb_y * mb_block_height,
2308 ptr_cb = ebuf + 16 * wrap_y;
2309 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2311 mb_block_width, mb_block_height,
2312 mb_x * mb_block_width,
mb_y * mb_block_height,
2314 ptr_cr = ebuf + 16 * wrap_y + 16;
2319 int progressive_score, interlaced_score;
2321 s->interlaced_dct = 0;
2322 progressive_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
NULL, wrap_y, 8) +
2323 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y * 8,
2324 NULL, wrap_y, 8) - 400;
2326 if (progressive_score > 0) {
2327 interlaced_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
2328 NULL, wrap_y * 2, 8) +
2329 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y,
2330 NULL, wrap_y * 2, 8);
2331 if (progressive_score > interlaced_score) {
2332 s->interlaced_dct = 1;
2335 uv_dct_offset = wrap_c;
2344 s->pdsp.get_pixels(
s->block[0], ptr_y, wrap_y);
2345 s->pdsp.get_pixels(
s->block[1], ptr_y + 8, wrap_y);
2346 s->pdsp.get_pixels(
s->block[2], ptr_y +
dct_offset, wrap_y);
2347 s->pdsp.get_pixels(
s->block[3], ptr_y +
dct_offset + 8, wrap_y);
2353 s->pdsp.get_pixels(
s->block[4], ptr_cb, wrap_c);
2354 s->pdsp.get_pixels(
s->block[5], ptr_cr, wrap_c);
2355 if (!
s->chroma_y_shift &&
s->chroma_x_shift) {
2356 s->pdsp.get_pixels(
s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2357 s->pdsp.get_pixels(
s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2358 }
else if (!
s->chroma_y_shift && !
s->chroma_x_shift) {
2359 s->pdsp.get_pixels(
s->block[ 6], ptr_cb + 8, wrap_c);
2360 s->pdsp.get_pixels(
s->block[ 7], ptr_cr + 8, wrap_c);
2361 s->pdsp.get_pixels(
s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2362 s->pdsp.get_pixels(
s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2363 s->pdsp.get_pixels(
s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2364 s->pdsp.get_pixels(
s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2370 uint8_t *dest_y, *dest_cb, *dest_cr;
2372 dest_y =
s->dest[0];
2373 dest_cb =
s->dest[1];
2374 dest_cr =
s->dest[2];
2377 op_pix =
s->hdsp.put_pixels_tab;
2378 op_qpix =
s->qdsp.put_qpel_pixels_tab;
2380 op_pix =
s->hdsp.put_no_rnd_pixels_tab;
2381 op_qpix =
s->qdsp.put_no_rnd_qpel_pixels_tab;
2386 s->last_picture.f->data,
2388 op_pix =
s->hdsp.avg_pixels_tab;
2389 op_qpix =
s->qdsp.avg_qpel_pixels_tab;
2393 s->next_picture.f->data,
2398 int progressive_score, interlaced_score;
2400 s->interlaced_dct = 0;
2401 progressive_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2402 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y * 8,
2407 progressive_score -= 400;
2409 if (progressive_score > 0) {
2410 interlaced_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y,
2412 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y,
2416 if (progressive_score > interlaced_score) {
2417 s->interlaced_dct = 1;
2420 uv_dct_offset = wrap_c;
2428 s->pdsp.diff_pixels(
s->block[0], ptr_y, dest_y, wrap_y);
2429 s->pdsp.diff_pixels(
s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2432 s->pdsp.diff_pixels(
s->block[3], ptr_y +
dct_offset + 8,
2439 s->pdsp.diff_pixels(
s->block[4], ptr_cb, dest_cb, wrap_c);
2440 s->pdsp.diff_pixels(
s->block[5], ptr_cr, dest_cr, wrap_c);
2441 if (!
s->chroma_y_shift) {
2442 s->pdsp.diff_pixels(
s->block[6], ptr_cb + uv_dct_offset,
2443 dest_cb + uv_dct_offset, wrap_c);
2444 s->pdsp.diff_pixels(
s->block[7], ptr_cr + uv_dct_offset,
2445 dest_cr + uv_dct_offset, wrap_c);
2449 if (
s->current_picture.mc_mb_var[
s->mb_stride *
mb_y +
mb_x] <
2450 2 *
s->qscale *
s->qscale) {
2452 if (
s->mecc.sad[1](
NULL, ptr_y, dest_y, wrap_y, 8) < 20 *
s->qscale)
2454 if (
s->mecc.sad[1](
NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 *
s->qscale)
2457 wrap_y, 8) < 20 *
s->qscale)
2460 wrap_y, 8) < 20 *
s->qscale)
2462 if (
s->mecc.sad[1](
NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 *
s->qscale)
2464 if (
s->mecc.sad[1](
NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 *
s->qscale)
2466 if (!
s->chroma_y_shift) {
2467 if (
s->mecc.sad[1](
NULL, ptr_cb + uv_dct_offset,
2468 dest_cb + uv_dct_offset,
2469 wrap_c, 8) < 20 *
s->qscale)
2471 if (
s->mecc.sad[1](
NULL, ptr_cr + uv_dct_offset,
2472 dest_cr + uv_dct_offset,
2473 wrap_c, 8) < 20 *
s->qscale)
2479 if (
s->quantizer_noise_shaping) {
2492 if (!
s->chroma_y_shift) {
2500 memcpy(orig[0],
s->block[0],
sizeof(int16_t) * 64 * mb_block_count);
2506 for (
i = 0;
i < mb_block_count;
i++) {
2509 s->block_last_index[
i] =
s->dct_quantize(
s,
s->block[
i],
i,
s->qscale, &
overflow);
2518 s->block_last_index[
i] = -1;
2520 if (
s->quantizer_noise_shaping) {
2521 for (
i = 0;
i < mb_block_count;
i++) {
2523 s->block_last_index[
i] =
2525 orig[
i],
i,
s->qscale);
2530 if (
s->luma_elim_threshold && !
s->mb_intra)
2531 for (
i = 0;
i < 4;
i++)
2533 if (
s->chroma_elim_threshold && !
s->mb_intra)
2534 for (
i = 4;
i < mb_block_count;
i++)
2538 for (
i = 0;
i < mb_block_count;
i++) {
2539 if (
s->block_last_index[
i] == -1)
2540 s->coded_score[
i] = INT_MAX / 256;
2546 s->block_last_index[4] =
2547 s->block_last_index[5] = 0;
2549 s->block[5][0] = (1024 +
s->c_dc_scale / 2) /
s->c_dc_scale;
2550 if (!
s->chroma_y_shift) {
2551 for (
i=6;
i<12;
i++) {
2552 s->block_last_index[
i] = 0;
2553 s->block[
i][0] =
s->block[4][0];
2560 for (
i = 0;
i < mb_block_count;
i++) {
2562 if (
s->block_last_index[
i] > 0) {
2563 for (j = 63; j > 0; j--) {
2564 if (
s->block[
i][
s->intra_scantable.permutated[j]])
2567 s->block_last_index[
i] = j;
2573 switch(
s->codec_id){
2576 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2580 if (CONFIG_MPEG4_ENCODER)
2590 if (CONFIG_WMV2_ENCODER)
2594 if (CONFIG_H261_ENCODER)
2602 if (CONFIG_H263_ENCODER)
2607 if (CONFIG_MJPEG_ENCODER)
2611 if (CONFIG_SPEEDHQ_ENCODER)
2629 memcpy(d->
last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2657 memcpy(d->
mv,
s->mv, 2*4*2*
sizeof(
int));
2658 memcpy(d->
last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2680 if(
s->data_partitioning){
2695 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2702 s->block=
s->blocks[*next_block];
2703 s->pb=
pb[*next_block];
2704 if(
s->data_partitioning){
2705 s->pb2 =
pb2 [*next_block];
2706 s->tex_pb=
tex_pb[*next_block];
2710 memcpy(dest_backup,
s->dest,
sizeof(
s->dest));
2711 s->dest[0] =
s->sc.rd_scratchpad;
2712 s->dest[1] =
s->sc.rd_scratchpad + 16*
s->linesize;
2713 s->dest[2] =
s->sc.rd_scratchpad + 16*
s->linesize + 8;
2720 if(
s->data_partitioning){
2728 score *=
s->lambda2;
2733 memcpy(
s->dest, dest_backup,
sizeof(
s->dest));
2751 else if(
w==8 &&
h==8)
2769 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
2770 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
2774 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) +
2775 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) +
2776 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);
2778 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) +
2779 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) +
2780 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);
2783 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)
2784 +
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)
2785 +
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);
2793 s->me.dia_size=
s->avctx->pre_dia_size;
2794 s->first_slice_line=1;
2795 for(
s->mb_y=
s->end_mb_y-1;
s->mb_y >=
s->start_mb_y;
s->mb_y--) {
2796 for(
s->mb_x=
s->mb_width-1;
s->mb_x >=0 ;
s->mb_x--) {
2799 s->first_slice_line=0;
2810 s->me.dia_size=
s->avctx->dia_size;
2811 s->first_slice_line=1;
2812 for(
s->mb_y=
s->start_mb_y;
s->mb_y <
s->end_mb_y;
s->mb_y++) {
2815 for(
s->mb_x=0;
s->mb_x <
s->mb_width;
s->mb_x++) {
2816 s->block_index[0]+=2;
2817 s->block_index[1]+=2;
2818 s->block_index[2]+=2;
2819 s->block_index[3]+=2;
2827 s->first_slice_line=0;
2840 uint8_t *pix =
s->new_picture.f->data[0] + (yy *
s->linesize) + xx;
2842 int sum =
s->mpvencdsp.pix_sum(pix,
s->linesize);
2844 varc = (
s->mpvencdsp.pix_norm1(pix,
s->linesize) -
2845 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2847 s->current_picture.mb_var [
s->mb_stride *
mb_y +
mb_x] = varc;
2848 s->current_picture.mb_mean[
s->mb_stride *
mb_y +
mb_x] = (sum+128)>>8;
2849 s->me.mb_var_sum_temp += varc;
2857 if(
s->partitioned_frame){
2862 }
else if(CONFIG_MJPEG_ENCODER &&
s->out_format ==
FMT_MJPEG){
2864 }
else if (CONFIG_SPEEDHQ_ENCODER &&
s->out_format ==
FMT_SPEEDHQ) {
2876 uint8_t *ptr =
s->mb_info_ptr +
s->mb_info_size - 12;
2878 int mba =
s->mb_x +
s->mb_width * (
s->mb_y %
s->gob_index);
2879 int gobn =
s->mb_y /
s->gob_index;
2881 if (CONFIG_H263_ENCODER)
2883 bytestream_put_le32(&ptr,
offset);
2884 bytestream_put_byte(&ptr,
s->qscale);
2885 bytestream_put_byte(&ptr, gobn);
2886 bytestream_put_le16(&ptr, mba);
2887 bytestream_put_byte(&ptr, pred_x);
2888 bytestream_put_byte(&ptr, pred_y);
2890 bytestream_put_byte(&ptr, 0);
2891 bytestream_put_byte(&ptr, 0);
2899 s->mb_info_size += 12;
2900 s->prev_mb_info =
s->last_mb_info;
2912 if (!
s->mb_info_size)
2913 s->mb_info_size += 12;
2920 &&
s->slice_context_count == 1
2921 &&
s->pb.buf ==
s->avctx->internal->byte_buffer) {
2922 int lastgob_pos =
s->ptr_lastgob -
s->pb.buf;
2923 int vbv_pos =
s->vbv_delay_ptr -
s->pb.buf;
2926 int new_buffer_size = 0;
2928 if ((
s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2936 s->avctx->internal->byte_buffer_size + size_increase);
2940 memcpy(new_buffer,
s->avctx->internal->byte_buffer,
s->avctx->internal->byte_buffer_size);
2941 av_free(
s->avctx->internal->byte_buffer);
2942 s->avctx->internal->byte_buffer = new_buffer;
2943 s->avctx->internal->byte_buffer_size = new_buffer_size;
2945 s->ptr_lastgob =
s->pb.buf + lastgob_pos;
2946 s->vbv_delay_ptr =
s->pb.buf + vbv_pos;
2956 int chr_h= 16>>
s->chroma_y_shift;
2983 s->last_dc[
i] = 128 <<
s->intra_dc_precision;
2985 s->current_picture.encoding_error[
i] = 0;
2988 s->last_dc[0] = 128*8/13;
2989 s->last_dc[1] = 128*8/14;
2990 s->last_dc[2] = 128*8/14;
2993 memset(
s->last_mv, 0,
sizeof(
s->last_mv));
2997 switch(
s->codec_id){
3001 if (CONFIG_H263_ENCODER)
3005 if(CONFIG_MPEG4_ENCODER &&
s->partitioned_frame)
3012 s->first_slice_line = 1;
3013 s->ptr_lastgob =
s->pb.buf;
3014 for (mb_y_order =
s->start_mb_y; mb_y_order < s->
end_mb_y; mb_y_order++) {
3018 if (first_in_slice && mb_y_order !=
s->start_mb_y)
3020 s->last_dc[0] =
s->last_dc[1] =
s->last_dc[2] = 1024 <<
s->intra_dc_precision;
3036 int size_increase =
s->avctx->internal->byte_buffer_size/4
3044 if(
s->data_partitioning){
3058 xy=
s->mb_y*
s->mb_stride +
s->mb_x;
3064 int current_packet_size, is_gob_start;
3066 current_packet_size= ((
put_bits_count(&
s->pb)+7)>>3) - (
s->ptr_lastgob -
s->pb.buf);
3068 is_gob_start =
s->rtp_payload_size &&
3069 current_packet_size >=
s->rtp_payload_size &&
3072 if(
s->start_mb_y ==
mb_y &&
mb_y > 0 &&
mb_x==0) is_gob_start=1;
3074 switch(
s->codec_id){
3077 if(!
s->h263_slice_structured)
3078 if(
s->mb_x ||
s->mb_y%
s->gob_index) is_gob_start=0;
3081 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
3083 if(
s->mb_skip_run) is_gob_start=0;
3086 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
3102 if (
s->error_rate &&
s->resync_mb_x +
s->resync_mb_y > 0) {
3104 int d = 100 /
s->error_rate;
3106 current_packet_size=0;
3107 s->pb.buf_ptr=
s->ptr_lastgob;
3112 #if FF_API_RTP_CALLBACK
3114 if (
s->avctx->rtp_callback){
3115 int number_mb = (
mb_y -
s->resync_mb_y)*
s->mb_width +
mb_x -
s->resync_mb_x;
3116 s->avctx->rtp_callback(
s->avctx,
s->ptr_lastgob, current_packet_size, number_mb);
3122 switch(
s->codec_id){
3124 if (CONFIG_MPEG4_ENCODER) {
3131 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3138 if (CONFIG_H263_ENCODER)
3145 s->misc_bits+=
bits -
s->last_bits;
3149 s->ptr_lastgob += current_packet_size;
3150 s->first_slice_line=1;
3151 s->resync_mb_x=
mb_x;
3152 s->resync_mb_y=
mb_y;
3156 if( (
s->resync_mb_x ==
s->mb_x)
3157 &&
s->resync_mb_y+1 ==
s->mb_y){
3158 s->first_slice_line=0;
3168 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3174 if(
s->data_partitioning){
3175 backup_s.pb2=
s->pb2;
3176 backup_s.tex_pb=
s->tex_pb;
3183 s->mv[0][0][0] =
s->p_mv_table[xy][0];
3184 s->mv[0][0][1] =
s->p_mv_table[xy][1];
3186 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3193 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3194 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3195 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3198 &dmin, &next_block, 0, 0);
3207 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3214 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3215 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3218 &dmin, &next_block, 0, 0);
3224 s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3225 s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3227 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3233 s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3234 s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3236 &dmin, &next_block,
s->mv[1][0][0],
s->mv[1][0][1]);
3242 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3243 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3244 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3245 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3247 &dmin, &next_block, 0, 0);
3254 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3255 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3256 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3259 &dmin, &next_block, 0, 0);
3266 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3267 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3268 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3271 &dmin, &next_block, 0, 0);
3277 for(dir=0; dir<2; dir++){
3279 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3280 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3281 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3285 &dmin, &next_block, 0, 0);
3294 &dmin, &next_block, 0, 0);
3295 if(
s->h263_pred ||
s->h263_aic){
3297 s->mbintra_table[
mb_x +
mb_y*
s->mb_stride]=1;
3305 const int last_qp= backup_s.qscale;
3309 static const int dquant_tab[4]={-1,1,-2,2};
3310 int storecoefs =
s->mb_intra &&
s->dc_val[0];
3318 s->mv[0][0][0] = best_s.
mv[0][0][0];
3319 s->mv[0][0][1] = best_s.
mv[0][0][1];
3320 s->mv[1][0][0] = best_s.
mv[1][0][0];
3321 s->mv[1][0][1] = best_s.
mv[1][0][1];
3324 for(; qpi<4; qpi++){
3325 int dquant= dquant_tab[qpi];
3327 if(qp < s->
avctx->
qmin || qp >
s->avctx->qmax)
3332 dc[
i]=
s->dc_val[0][
s->block_index[
i] ];
3333 memcpy(ac[
i],
s->ac_val[0][
s->block_index[
i]],
sizeof(int16_t)*16);
3338 &dmin, &next_block,
s->mv[mvdir][0][0],
s->mv[mvdir][0][1]);
3342 s->dc_val[0][
s->block_index[
i] ]=
dc[
i];
3343 memcpy(
s->ac_val[0][
s->block_index[
i]], ac[
i],
sizeof(int16_t)*16);
3351 int mx=
s->b_direct_mv_table[xy][0];
3352 int my=
s->b_direct_mv_table[xy][1];
3354 backup_s.dquant = 0;
3359 &dmin, &next_block, mx, my);
3362 backup_s.dquant = 0;
3367 &dmin, &next_block, 0, 0);
3372 coded |=
s->block_last_index[
i];
3375 memcpy(
s->mv, best_s.
mv,
sizeof(
s->mv));
3397 &dmin, &next_block, mx, my);
3402 s->current_picture.qscale_table[xy] = best_s.
qscale;
3408 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3411 if(
s->data_partitioning){
3414 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3415 s->pb2= backup_s.pb2;
3419 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3420 s->tex_pb= backup_s.tex_pb;
3424 if (CONFIG_H263_ENCODER &&
3429 s->hdsp.put_pixels_tab[0][0](
s->dest[0],
s->sc.rd_scratchpad ,
s->linesize ,16);
3430 s->hdsp.put_pixels_tab[1][0](
s->dest[1],
s->sc.rd_scratchpad + 16*
s->linesize ,
s->uvlinesize, 8);
3431 s->hdsp.put_pixels_tab[1][0](
s->dest[2],
s->sc.rd_scratchpad + 16*
s->linesize + 8,
s->uvlinesize, 8);
3437 int motion_x = 0, motion_y = 0;
3445 motion_x=
s->mv[0][0][0] = 0;
3446 motion_y=
s->mv[0][0][1] = 0;
3451 motion_x=
s->mv[0][0][0] =
s->p_mv_table[xy][0];
3452 motion_y=
s->mv[0][0][1] =
s->p_mv_table[xy][1];
3459 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3460 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3461 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3469 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3470 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3474 if (CONFIG_MPEG4_ENCODER) {
3477 motion_x=
s->b_direct_mv_table[xy][0];
3478 motion_y=
s->b_direct_mv_table[xy][1];
3483 if (CONFIG_MPEG4_ENCODER) {
3492 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3493 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3494 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3495 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3500 motion_x=
s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3501 motion_y=
s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3506 motion_x=
s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3507 motion_y=
s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3514 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3515 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3516 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3524 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3525 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3526 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3533 for(dir=0; dir<2; dir++){
3535 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3536 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3537 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3548 s->last_mv_dir =
s->mv_dir;
3550 if (CONFIG_H263_ENCODER &&
3559 s->p_mv_table[xy][0]=0;
3560 s->p_mv_table[xy][1]=0;
3567 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
3568 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
3570 s->current_picture.encoding_error[0] +=
sse(
3571 s,
s->new_picture.f->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
3572 s->dest[0],
w,
h,
s->linesize);
3573 s->current_picture.encoding_error[1] +=
sse(
3574 s,
s->new_picture.f->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3575 s->dest[1],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3576 s->current_picture.encoding_error[2] +=
sse(
3577 s,
s->new_picture.f->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3578 s->dest[2],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3581 if(CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
3584 ff_dlog(
s->avctx,
"MB %d %d bits\n",
3595 #if FF_API_RTP_CALLBACK
3598 if (
s->avctx->rtp_callback) {
3599 int number_mb = (
mb_y -
s->resync_mb_y)*
s->mb_width -
s->resync_mb_x;
3603 s->avctx->rtp_callback(
s->avctx,
s->ptr_lastgob, pdif, number_mb);
3611 #define MERGE(field) dst->field += src->field; src->field=0
3638 for(
i=0;
i<64;
i++){
3651 if (
s->next_lambda){
3652 s->current_picture_ptr->f->quality =
3653 s->current_picture.f->quality =
s->next_lambda;
3654 if(!dry_run)
s->next_lambda= 0;
3655 }
else if (!
s->fixed_qscale) {
3657 s->current_picture_ptr->f->quality =
3658 s->current_picture.f->quality =
quality;
3659 if (
s->current_picture.f->quality < 0)
3663 if(
s->adaptive_quant){
3664 switch(
s->codec_id){
3666 if (CONFIG_MPEG4_ENCODER)
3672 if (CONFIG_H263_ENCODER)
3679 s->lambda=
s->lambda_table[0];
3682 s->lambda =
s->current_picture.f->quality;
3690 s->time =
s->current_picture_ptr->f->pts *
s->avctx->time_base.num;
3693 s->pb_time=
s->pp_time - (
s->last_non_b_time -
s->time);
3696 s->pp_time=
s->time -
s->last_non_b_time;
3697 s->last_non_b_time=
s->time;
3706 int context_count =
s->slice_context_count;
3711 s->me.mb_var_sum_temp =
3712 s->me.mc_mb_var_sum_temp = 0;
3721 s->me.scene_change_score=0;
3726 if(
s->msmpeg4_version >= 3)
s->no_rounding=1;
3727 else s->no_rounding=0;
3730 s->no_rounding ^= 1;
3739 s->lambda=
s->last_lambda_for[
s->pict_type];
3741 s->lambda=
s->last_lambda_for[
s->last_non_b_pict_type];
3746 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
3747 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
3748 s->q_chroma_intra_matrix =
s->q_intra_matrix;
3749 s->q_chroma_intra_matrix16 =
s->q_intra_matrix16;
3753 for(
i=1;
i<context_count;
i++){
3764 s->lambda = (
s->lambda *
s->me_penalty_compensation + 128) >> 8;
3765 s->lambda2 = (
s->lambda2 * (int64_t)
s->me_penalty_compensation + 128) >> 8;
3776 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3779 if(!
s->fixed_qscale){
3781 s->avctx->execute(
s->avctx,
mb_var_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3784 for(
i=1;
i<context_count;
i++){
3787 s->current_picture.mc_mb_var_sum=
s->current_picture_ptr->mc_mb_var_sum=
s->me.mc_mb_var_sum_temp;
3788 s->current_picture. mb_var_sum=
s->current_picture_ptr-> mb_var_sum=
s->me. mb_var_sum_temp;
3791 if (
s->me.scene_change_score >
s->scenechange_threshold &&
3794 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3796 if(
s->msmpeg4_version >= 3)
3798 ff_dlog(
s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3799 s->current_picture.mb_var_sum,
s->current_picture.mc_mb_var_sum);
3842 for(dir=0; dir<2; dir++){
3848 s->b_field_mv_table[dir][
i][j], dir ?
s->b_code :
s->f_code,
type, 1);
3859 if (
s->qscale < 3 &&
s->max_qcoeff <= 128 &&
3868 if (
s->avctx->intra_matrix) {
3870 luma_matrix =
s->avctx->intra_matrix;
3872 if (
s->avctx->chroma_intra_matrix)
3873 chroma_matrix =
s->avctx->chroma_intra_matrix;
3877 int j =
s->idsp.idct_permutation[
i];
3879 s->chroma_intra_matrix[j] =
av_clip_uint8((chroma_matrix[
i] *
s->qscale) >> 3);
3882 s->y_dc_scale_table=
3884 s->chroma_intra_matrix[0] =
3887 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3889 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3893 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};
3894 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};
3901 s->y_dc_scale_table= y;
3902 s->c_dc_scale_table=
c;
3903 s->intra_matrix[0] = 13;
3904 s->chroma_intra_matrix[0] = 14;
3906 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3908 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3913 s->y_dc_scale_table=
3918 s->current_picture_ptr->f->key_frame =
3920 s->current_picture_ptr->f->pict_type =
3921 s->current_picture.f->pict_type =
s->pict_type;
3923 if (
s->current_picture.f->key_frame)
3924 s->picture_in_gop_number=0;
3926 s->mb_x =
s->mb_y = 0;
3928 switch(
s->out_format) {
3932 s->pred,
s->intra_matrix,
s->chroma_intra_matrix);
3935 if (CONFIG_SPEEDHQ_ENCODER)
3939 if (CONFIG_H261_ENCODER)
3947 else if (CONFIG_MPEG4_ENCODER &&
s->h263_pred) {
3960 else if (CONFIG_H263_ENCODER)
3964 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3971 s->header_bits=
bits -
s->last_bits;
3973 for(
i=1;
i<context_count;
i++){
3976 s->avctx->execute(
s->avctx,
encode_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3977 for(
i=1;
i<context_count;
i++){
3978 if (
s->pb.buf_end ==
s->thread_context[
i]->pb.buf)
3987 const int intra=
s->mb_intra;
3990 s->dct_count[intra]++;
3992 for(
i=0;
i<64;
i++){
3997 s->dct_error_sum[intra][
i] +=
level;
3998 level -=
s->dct_offset[intra][
i];
4001 s->dct_error_sum[intra][
i] -=
level;
4002 level +=
s->dct_offset[intra][
i];
4011 int16_t *
block,
int n,
4014 const uint16_t *matrix;
4016 const uint8_t *perm_scantable;
4018 unsigned int threshold1, threshold2;
4030 int coeff_count[64];
4031 int qmul, qadd, start_i, last_non_zero,
i,
dc;
4032 const int esc_length=
s->ac_esc_length;
4040 if(
s->dct_error_sum)
4046 else mpeg2_qscale =
qscale << 1;
4050 scantable=
s->intra_scantable.scantable;
4051 perm_scantable=
s->intra_scantable.permutated;
4068 qmat = n < 4 ?
s->q_intra_matrix[
qscale] :
s->q_chroma_intra_matrix[
qscale];
4069 matrix = n < 4 ?
s->intra_matrix :
s->chroma_intra_matrix;
4073 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
4074 length =
s->intra_chroma_ac_vlc_length;
4075 last_length=
s->intra_chroma_ac_vlc_last_length;
4077 length =
s->intra_ac_vlc_length;
4078 last_length=
s->intra_ac_vlc_last_length;
4081 scantable=
s->inter_scantable.scantable;
4082 perm_scantable=
s->inter_scantable.permutated;
4085 qmat =
s->q_inter_matrix[
qscale];
4086 matrix =
s->inter_matrix;
4087 length =
s->inter_ac_vlc_length;
4088 last_length=
s->inter_ac_vlc_last_length;
4093 threshold2= (threshold1<<1);
4095 for(
i=63;
i>=start_i;
i--) {
4096 const int j = scantable[
i];
4099 if(((
unsigned)(
level+threshold1))>threshold2){
4105 for(
i=start_i;
i<=last_non_zero;
i++) {
4106 const int j = scantable[
i];
4111 if(((
unsigned)(
level+threshold1))>threshold2){
4134 if(last_non_zero < start_i){
4135 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4136 return last_non_zero;
4139 score_tab[start_i]= 0;
4140 survivor[0]= start_i;
4143 for(
i=start_i;
i<=last_non_zero;
i++){
4144 int level_index, j, zero_distortion;
4146 int best_score=256*256*256*120;
4150 zero_distortion= dct_coeff*dct_coeff;
4152 for(level_index=0; level_index < coeff_count[
i]; level_index++){