34 #include "config_components.h"
81 #define QUANT_BIAS_SHIFT 8
83 #define QMAT_SHIFT_MMX 16
97 #if FF_API_MPEGVIDEO_OPTS
114 uint16_t (*
qmat16)[2][64],
115 const uint16_t *quant_matrix,
116 int bias,
int qmin,
int qmax,
int intra)
127 else qscale2 =
qscale << 1;
134 for (
i = 0;
i < 64;
i++) {
135 const int j =
s->idsp.idct_permutation[
i];
136 int64_t den = (int64_t) qscale2 * quant_matrix[j];
146 for (
i = 0;
i < 64;
i++) {
147 const int j =
s->idsp.idct_permutation[
i];
148 int64_t den =
ff_aanscales[
i] * (int64_t) qscale2 * quant_matrix[j];
158 for (
i = 0;
i < 64;
i++) {
159 const int j =
s->idsp.idct_permutation[
i];
160 int64_t den = (int64_t) qscale2 * quant_matrix[j];
180 for (
i = intra;
i < 64;
i++) {
192 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
199 if (
s->q_scale_type == 1 && 0) {
201 int bestdiff=INT_MAX;
209 if (
diff < bestdiff) {
218 s->qscale =
av_clip(
s->qscale,
s->avctx->qmin,
s->vbv_ignore_qmax ? 31 :
s->avctx->qmax);
231 for (
i = 0;
i < 64;
i++) {
243 int8_t *
const qscale_table =
s->current_picture.qscale_table;
246 for (
i = 0;
i <
s->mb_num;
i++) {
247 unsigned int lam =
s->lambda_table[
s->mb_index2xy[
i]];
249 qscale_table[
s->mb_index2xy[
i]] =
av_clip(qp,
s->avctx->qmin,
257 #define COPY(a) dst->a= src->a
273 for (
int i = -16;
i < 16;
i++)
292 s->input_picture_number = 0;
293 s->picture_in_gop_number = 0;
301 if (CONFIG_H263_ENCODER)
303 if (!
s->dct_quantize)
307 s->fast_dct_quantize =
s->dct_quantize;
308 if (
s->avctx->trellis)
347 "keyframe interval too large!, reducing it from %d to %d\n",
359 "max b frames must be 0 or positive for mpegvideo based encoders\n");
370 s->rtp_mode = !!
s->rtp_payload_size;
374 if (
s->intra_dc_precision < 0) {
375 s->intra_dc_precision += 8;
376 }
else if (
s->intra_dc_precision >= 8)
377 s->intra_dc_precision -= 8;
379 if (
s->intra_dc_precision < 0) {
381 "intra dc precision must be positive, note some applications use"
382 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
392 if (
s->gop_size <= 1) {
446 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
463 "impossible bitrate constraints, this will fail\n");
473 if (!
s->fixed_qscale &&
479 if (nbt <= INT_MAX) {
492 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
493 "specified vbv buffer is too large for the given bitrate!\n");
505 "OBMC is only supported with simple mb decision\n");
520 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
573 if (
s->scenechange_threshold < 1000000000 &&
576 "closed gop with scene change detection are not supported yet, "
577 "set threshold to 1000000000\n");
585 "low delay forcing is only available for mpeg2, "
586 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
589 if (
s->max_b_frames != 0) {
591 "B-frames cannot be used with low delay\n");
596 if (
s->q_scale_type == 1) {
599 "non linear quant only supports qmax <= 28 currently\n");
612 "notice: b_frame_strategy only affects the first pass\n");
613 s->b_frame_strategy = 0;
627 s->inter_quant_bias = 0;
629 s->intra_quant_bias = 0;
644 "timebase %d/%d not supported by MPEG 4 standard, "
645 "the maximum admitted value for the timebase denominator "
656 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
661 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
664 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
678 if (!CONFIG_SPEEDHQ_ENCODER)
686 if (!CONFIG_H261_ENCODER)
690 "The specified picture size of %dx%d is not valid for the "
691 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
692 s->width,
s->height);
701 if (!CONFIG_H263_ENCODER)
704 s->width,
s->height) == 8) {
706 "The specified picture size of %dx%d is not valid for "
707 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
708 "352x288, 704x576, and 1408x1152. "
709 "Try H.263+.\n",
s->width,
s->height);
721 s->modified_quant =
s->h263_aic;
723 s->unrestricted_mv =
s->obmc ||
s->loop_filter ||
s->umvplus;
733 s->unrestricted_mv = 1;
747 s->modified_quant = 1;
751 s->unrestricted_mv = 0;
756 s->unrestricted_mv = 1;
757 s->low_delay =
s->max_b_frames ? 0 : 1;
758 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
763 s->unrestricted_mv = 1;
764 s->msmpeg4_version = 2;
771 s->unrestricted_mv = 1;
772 s->msmpeg4_version = 3;
773 s->flipflop_rounding = 1;
780 s->unrestricted_mv = 1;
781 s->msmpeg4_version = 4;
782 s->flipflop_rounding = 1;
789 s->unrestricted_mv = 1;
790 s->msmpeg4_version = 5;
791 s->flipflop_rounding = 1;
803 s->progressive_frame =
830 if (
s->noise_reduction) {
837 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) &&
s->modified_quant)
840 if (
s->slice_context_count > 1) {
844 s->h263_slice_structured = 1;
847 s->quant_precision = 5;
850 ff_set_cmp(&
s->mecc,
s->mecc.frame_skip_cmp,
s->frame_skip_cmp);
852 if (CONFIG_H261_ENCODER &&
s->out_format ==
FMT_H261) {
854 }
else if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
857 }
else if (CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263) {
864 for (
i = 0;
i < 64;
i++) {
865 int j =
s->idsp.idct_permutation[
i];
878 s->chroma_intra_matrix[j] =
902 if (
s->b_frame_strategy == 2) {
903 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
905 if (!
s->tmp_frames[
i])
909 s->tmp_frames[
i]->width =
s->width >>
s->brd_scale;
910 s->tmp_frames[
i]->height =
s->height >>
s->brd_scale;
945 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
946 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
947 s->q_chroma_intra_matrix=
NULL;
948 s->q_chroma_intra_matrix16=
NULL;
965 for (y = 0; y < 16; y++) {
966 for (x = 0; x < 16; x++) {
983 for (y = 0; y <
h; y += 16) {
984 for (x = 0; x <
w; x += 16) {
991 acc += sae + 500 < sad;
1000 s->chroma_x_shift,
s->chroma_y_shift,
s->out_format,
1001 s->mb_stride,
s->mb_width,
s->mb_height,
s->b8_stride,
1002 &
s->linesize, &
s->uvlinesize);
1009 int i, display_picture_number = 0,
ret;
1010 int encoding_delay =
s->max_b_frames ?
s->max_b_frames
1011 : (
s->low_delay ? 0 : 1);
1012 int flush_offset = 1;
1017 display_picture_number =
s->input_picture_number++;
1021 int64_t last =
s->user_specified_pts;
1025 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1030 if (!
s->low_delay && display_picture_number == 1)
1031 s->dts_delta =
pts - last;
1033 s->user_specified_pts =
pts;
1036 s->user_specified_pts =
1037 pts =
s->user_specified_pts + 1;
1039 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1042 pts = display_picture_number;
1046 if (!pic_arg->
buf[0] ||
1048 pic_arg->
linesize[1] !=
s->uvlinesize ||
1051 if ((
s->width & 15) || (
s->height & 15))
1059 pic_arg->
linesize[1],
s->linesize,
s->uvlinesize);
1065 pic = &
s->picture[
i];
1082 int h_chroma_shift, v_chroma_shift;
1087 for (
i = 0;
i < 3;
i++) {
1089 int dst_stride =
i ?
s->uvlinesize :
s->linesize;
1090 int h_shift =
i ? h_chroma_shift : 0;
1091 int v_shift =
i ? v_chroma_shift : 0;
1092 int w =
s->width >> h_shift;
1093 int h =
s->height >> v_shift;
1095 uint8_t *dst = pic->
f->
data[
i];
1099 && !
s->progressive_sequence
1100 &&
FFALIGN(
s->height, 32) -
s->height > 16)
1103 if (!
s->avctx->rc_buffer_size)
1106 if (src_stride == dst_stride)
1107 memcpy(dst,
src, src_stride *
h);
1110 uint8_t *dst2 = dst;
1112 memcpy(dst2,
src,
w);
1117 if ((
s->width & 15) || (
s->height & (vpad-1))) {
1118 s->mpvencdsp.draw_edges(dst, dst_stride,
1137 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1138 if (
s->input_picture[flush_offset])
1141 if (flush_offset <= 1)
1144 encoding_delay = encoding_delay - flush_offset + 1;
1149 s->input_picture[
i - flush_offset] =
s->input_picture[
i];
1151 s->input_picture[encoding_delay] = (
Picture*) pic;
1160 int64_t score64 = 0;
1162 for (plane = 0; plane < 3; plane++) {
1164 const int bw = plane ? 1 : 2;
1165 for (y = 0; y <
s->mb_height * bw; y++) {
1166 for (x = 0; x <
s->mb_width * bw; x++) {
1167 int off = p->
shared ? 0 : 16;
1168 uint8_t *dptr = p->
f->
data[plane] + 8 * (x + y *
stride) + off;
1169 uint8_t *rptr =
ref->f->data[plane] + 8 * (x + y *
stride);
1170 int v =
s->mecc.frame_skip_cmp[1](
s, dptr, rptr,
stride, 8);
1172 switch (
FFABS(
s->frame_skip_exp)) {
1173 case 0: score =
FFMAX(score, v);
break;
1174 case 1: score +=
FFABS(v);
break;
1175 case 2: score64 += v * (int64_t)v;
break;
1176 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1177 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1186 if (
s->frame_skip_exp < 0)
1187 score64 = pow(score64 / (
double)(
s->mb_width *
s->mb_height),
1188 -1.0/
s->frame_skip_exp);
1192 if (score64 < ((
s->frame_skip_factor * (int64_t)
s->lambda) >> 8))
1221 const int scale =
s->brd_scale;
1225 int64_t best_rd = INT64_MAX;
1226 int best_b_count = -1;
1241 b_lambda = p_lambda;
1245 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1246 Picture pre_input, *pre_input_ptr =
i ?
s->input_picture[
i - 1] :
1247 s->next_picture_ptr;
1250 if (pre_input_ptr && (!
i ||
s->input_picture[
i - 1])) {
1251 pre_input = *pre_input_ptr;
1260 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[0],
1261 s->tmp_frames[
i]->linesize[0],
1265 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[1],
1266 s->tmp_frames[
i]->linesize[1],
1270 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[2],
1271 s->tmp_frames[
i]->linesize[2],
1278 for (j = 0; j <
s->max_b_frames + 1; j++) {
1282 if (!
s->input_picture[j])
1295 c->mb_decision =
s->avctx->mb_decision;
1296 c->me_cmp =
s->avctx->me_cmp;
1297 c->mb_cmp =
s->avctx->mb_cmp;
1298 c->me_sub_cmp =
s->avctx->me_sub_cmp;
1300 c->time_base =
s->avctx->time_base;
1301 c->max_b_frames =
s->max_b_frames;
1319 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1320 int is_p =
i % (j + 1) == j ||
i ==
s->max_b_frames;
1322 s->tmp_frames[
i + 1]->pict_type = is_p ?
1324 s->tmp_frames[
i + 1]->quality = is_p ? p_lambda : b_lambda;
1343 rd +=
c->error[0] +
c->error[1] +
c->error[2];
1361 return best_b_count;
1369 s->reordered_input_picture[
i - 1] =
s->reordered_input_picture[
i];
1373 if (!
s->reordered_input_picture[0] &&
s->input_picture[0]) {
1374 if (
s->frame_skip_threshold ||
s->frame_skip_factor) {
1375 if (
s->picture_in_gop_number <
s->gop_size &&
1376 s->next_picture_ptr &&
1388 !
s->next_picture_ptr ||
s->intra_only) {
1389 s->reordered_input_picture[0] =
s->input_picture[0];
1391 s->reordered_input_picture[0]->f->coded_picture_number =
1392 s->coded_picture_number++;
1397 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1398 int pict_num =
s->input_picture[0]->f->display_picture_number +
i;
1400 if (pict_num >=
s->rc_context.num_entries)
1402 if (!
s->input_picture[
i]) {
1407 s->input_picture[
i]->f->pict_type =
1408 s->rc_context.entry[pict_num].new_pict_type;
1412 if (
s->b_frame_strategy == 0) {
1413 b_frames =
s->max_b_frames;
1414 while (b_frames && !
s->input_picture[b_frames])
1416 }
else if (
s->b_frame_strategy == 1) {
1417 for (
i = 1;
i <
s->max_b_frames + 1;
i++) {
1418 if (
s->input_picture[
i] &&
1419 s->input_picture[
i]->b_frame_score == 0) {
1420 s->input_picture[
i]->b_frame_score =
1422 s->input_picture[
i ]->f->data[0],
1423 s->input_picture[
i - 1]->f->data[0],
1427 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1428 if (!
s->input_picture[
i] ||
1429 s->input_picture[
i]->b_frame_score - 1 >
1430 s->mb_num /
s->b_sensitivity)
1434 b_frames =
FFMAX(0,
i - 1);
1437 for (
i = 0;
i < b_frames + 1;
i++) {
1438 s->input_picture[
i]->b_frame_score = 0;
1440 }
else if (
s->b_frame_strategy == 2) {
1448 for (
i = b_frames - 1;
i >= 0;
i--) {
1449 int type =
s->input_picture[
i]->f->pict_type;
1454 b_frames ==
s->max_b_frames) {
1456 "warning, too many B-frames in a row\n");
1459 if (
s->picture_in_gop_number + b_frames >=
s->gop_size) {
1461 s->gop_size >
s->picture_in_gop_number) {
1462 b_frames =
s->gop_size -
s->picture_in_gop_number - 1;
1474 s->reordered_input_picture[0] =
s->input_picture[b_frames];
1477 s->reordered_input_picture[0]->f->coded_picture_number =
1478 s->coded_picture_number++;
1479 for (
i = 0;
i < b_frames;
i++) {
1480 s->reordered_input_picture[
i + 1] =
s->input_picture[
i];
1481 s->reordered_input_picture[
i + 1]->f->pict_type =
1483 s->reordered_input_picture[
i + 1]->f->coded_picture_number =
1484 s->coded_picture_number++;
1491 if (
s->reordered_input_picture[0]) {
1492 s->reordered_input_picture[0]->reference =
1493 s->reordered_input_picture[0]->f->pict_type !=
1497 s->reordered_input_picture[0]->f)))
1500 if (
s->reordered_input_picture[0]->shared ||
s->avctx->rc_buffer_size) {
1508 pic = &
s->picture[
i];
1510 pic->
reference =
s->reordered_input_picture[0]->reference;
1521 s->reordered_input_picture[0]->shared = 0;
1523 s->current_picture_ptr = pic;
1526 s->current_picture_ptr =
s->reordered_input_picture[0];
1527 for (
i = 0;
i < 4;
i++) {
1528 if (
s->new_picture->data[
i])
1532 s->picture_number =
s->new_picture->display_picture_number;
1539 if (
s->unrestricted_mv &&
1540 s->current_picture.reference &&
1543 int hshift =
desc->log2_chroma_w;
1544 int vshift =
desc->log2_chroma_h;
1545 s->mpvencdsp.draw_edges(
s->current_picture.f->data[0],
1546 s->current_picture.f->linesize[0],
1547 s->h_edge_pos,
s->v_edge_pos,
1550 s->mpvencdsp.draw_edges(
s->current_picture.f->data[1],
1551 s->current_picture.f->linesize[1],
1552 s->h_edge_pos >> hshift,
1553 s->v_edge_pos >> vshift,
1557 s->mpvencdsp.draw_edges(
s->current_picture.f->data[2],
1558 s->current_picture.f->linesize[2],
1559 s->h_edge_pos >> hshift,
1560 s->v_edge_pos >> vshift,
1568 s->last_pict_type =
s->pict_type;
1569 s->last_lambda_for [
s->pict_type] =
s->current_picture_ptr->f->quality;
1571 s->last_non_b_pict_type =
s->pict_type;
1578 for (intra = 0; intra < 2; intra++) {
1579 if (
s->dct_count[intra] > (1 << 16)) {
1580 for (
i = 0;
i < 64;
i++) {
1581 s->dct_error_sum[intra][
i] >>= 1;
1583 s->dct_count[intra] >>= 1;
1586 for (
i = 0;
i < 64;
i++) {
1587 s->dct_offset[intra][
i] = (
s->noise_reduction *
1588 s->dct_count[intra] +
1589 s->dct_error_sum[intra][
i] / 2) /
1590 (
s->dct_error_sum[intra][
i] + 1);
1601 s->last_picture_ptr !=
s->next_picture_ptr &&
1602 s->last_picture_ptr->f->buf[0]) {
1606 s->current_picture_ptr->f->pict_type =
s->pict_type;
1611 s->current_picture_ptr)) < 0)
1615 s->last_picture_ptr =
s->next_picture_ptr;
1616 s->next_picture_ptr =
s->current_picture_ptr;
1619 if (
s->last_picture_ptr) {
1621 if (
s->last_picture_ptr->f->buf[0] &&
1623 s->last_picture_ptr)) < 0)
1626 if (
s->next_picture_ptr) {
1628 if (
s->next_picture_ptr->f->buf[0] &&
1630 s->next_picture_ptr)) < 0)
1636 for (
i = 0;
i < 4;
i++) {
1638 s->current_picture.f->data[
i] +=
1639 s->current_picture.f->linesize[
i];
1641 s->current_picture.f->linesize[
i] *= 2;
1642 s->last_picture.f->linesize[
i] *= 2;
1643 s->next_picture.f->linesize[
i] *= 2;
1648 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
1649 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
1651 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
1652 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
1654 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
1655 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
1658 if (
s->dct_error_sum) {
1667 const AVFrame *pic_arg,
int *got_packet)
1670 int i, stuffing_count,
ret;
1671 int context_count =
s->slice_context_count;
1673 s->vbv_ignore_qmax = 0;
1675 s->picture_in_gop_number++;
1685 if (
s->new_picture->data[0]) {
1686 int growing_buffer = context_count == 1 && !
s->data_partitioning;
1687 size_t pkt_size = 10000 +
s->mb_width *
s->mb_height *
1700 s->mb_width*
s->mb_height*12);
1701 s->prev_mb_info =
s->last_mb_info =
s->mb_info_size = 0;
1704 for (
i = 0;
i < context_count;
i++) {
1705 int start_y =
s->thread_context[
i]->start_mb_y;
1707 int h =
s->mb_height;
1708 uint8_t *start =
pkt->
data + (size_t)(((int64_t)
pkt->
size) * start_y /
h);
1709 uint8_t *end =
pkt->
data + (size_t)(((int64_t)
pkt->
size) * end_y /
h);
1714 s->pict_type =
s->new_picture->pict_type;
1721 if (growing_buffer) {
1731 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
s->out_format ==
FMT_MJPEG)
1741 s->lambda <
s->lmax) {
1742 s->next_lambda =
FFMAX(
s->lambda + min_step,
s->lambda *
1743 (
s->qscale + 1) /
s->qscale);
1744 if (
s->adaptive_quant) {
1746 for (
i = 0;
i <
s->mb_height *
s->mb_stride;
i++)
1747 s->lambda_table[
i] =
1748 FFMAX(
s->lambda_table[
i] + min_step,
1749 s->lambda_table[
i] * (
s->qscale + 1) /
1755 if (
s->flipflop_rounding ||
1758 s->no_rounding ^= 1;
1761 s->time_base =
s->last_time_base;
1762 s->last_non_b_time =
s->time -
s->pp_time;
1764 for (
i = 0;
i < context_count;
i++) {
1768 s->vbv_ignore_qmax = 1;
1779 for (
i = 0;
i < 4;
i++) {
1780 s->current_picture_ptr->encoding_error[
i] =
s->current_picture.encoding_error[
i];
1781 avctx->
error[
i] +=
s->current_picture_ptr->encoding_error[
i];
1784 s->current_picture_ptr->encoding_error,
1790 s->misc_bits +
s->i_tex_bits +
1796 s->stuffing_bits = 8*stuffing_count;
1797 if (stuffing_count) {
1803 switch (
s->codec_id) {
1806 while (stuffing_count--) {
1813 stuffing_count -= 4;
1814 while (stuffing_count--) {
1820 s->stuffing_bits = 0;
1838 int minbits =
s->frame_bits - 8 *
1839 (
s->vbv_delay_pos - 1);
1840 double bits =
s->rc_context.buffer_index + minbits - inbits;
1841 uint8_t *
const vbv_delay_ptr =
s->pb.buf +
s->vbv_delay_pos;
1845 "Internal error, negative bits\n");
1857 vbv_delay_ptr[0] &= 0xF8;
1860 vbv_delay_ptr[2] &= 0x07;
1869 (uint8_t*)props, props_size);
1875 s->total_bits +=
s->frame_bits;
1877 pkt->
pts =
s->current_picture.f->pts;
1879 if (!
s->current_picture.f->coded_picture_number)
1886 if (
s->current_picture.f->key_frame)
1896 if (!
s->picture[
i].reference)
1908 int n,
int threshold)
1910 static const char tab[64] = {
1911 3, 2, 2, 1, 1, 1, 1, 1,
1912 1, 1, 1, 1, 1, 1, 1, 1,
1913 1, 1, 1, 1, 1, 1, 1, 1,
1914 0, 0, 0, 0, 0, 0, 0, 0,
1915 0, 0, 0, 0, 0, 0, 0, 0,
1916 0, 0, 0, 0, 0, 0, 0, 0,
1917 0, 0, 0, 0, 0, 0, 0, 0,
1918 0, 0, 0, 0, 0, 0, 0, 0
1923 int16_t *
block =
s->block[n];
1924 const int last_index =
s->block_last_index[n];
1927 if (threshold < 0) {
1929 threshold = -threshold;
1934 if (last_index <= skip_dc - 1)
1937 for (
i = 0;
i <= last_index;
i++) {
1938 const int j =
s->intra_scantable.permutated[
i];
1941 if (skip_dc &&
i == 0)
1945 }
else if (
level > 1) {
1951 if (score >= threshold)
1953 for (
i = skip_dc;
i <= last_index;
i++) {
1954 const int j =
s->intra_scantable.permutated[
i];
1958 s->block_last_index[n] = 0;
1960 s->block_last_index[n] = -1;
1967 const int maxlevel =
s->max_qcoeff;
1968 const int minlevel =
s->min_qcoeff;
1976 for (;
i <= last_index;
i++) {
1977 const int j =
s->intra_scantable.permutated[
i];
1980 if (
level > maxlevel) {
1983 }
else if (
level < minlevel) {
1993 "warning, clipping %d dct coefficients to %d..%d\n",
2001 for (y = 0; y < 8; y++) {
2002 for (x = 0; x < 8; x++) {
2008 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2009 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2010 int v = ptr[x2 + y2 *
stride];
2022 int motion_x,
int motion_y,
2023 int mb_block_height,
2032 #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2033 (s)->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2035 int16_t orig[12][64];
2036 const int mb_x =
s->mb_x;
2037 const int mb_y =
s->mb_y;
2041 int uv_dct_offset =
s->uvlinesize * 8;
2042 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2043 ptrdiff_t wrap_y, wrap_c;
2045 for (
i = 0;
i < mb_block_count;
i++)
2046 skip_dct[
i] =
s->skipdct;
2048 if (
s->adaptive_quant) {
2049 const int last_qp =
s->qscale;
2050 const int mb_xy =
mb_x +
mb_y *
s->mb_stride;
2052 s->lambda =
s->lambda_table[mb_xy];
2056 s->qscale =
s->current_picture_ptr->qscale_table[mb_xy];
2057 s->dquant =
s->qscale - last_qp;
2078 wrap_y =
s->linesize;
2079 wrap_c =
s->uvlinesize;
2080 ptr_y =
s->new_picture->data[0] +
2082 ptr_cb =
s->new_picture->data[1] +
2083 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2084 ptr_cr =
s->new_picture->data[2] +
2085 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2088 uint8_t *ebuf =
s->sc.edge_emu_buffer + 38 * wrap_y;
2091 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2094 s->width,
s->height);
2096 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2098 mb_block_width, mb_block_height,
2099 mb_x * mb_block_width,
mb_y * mb_block_height,
2101 ptr_cb = ebuf + 16 * wrap_y;
2102 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2104 mb_block_width, mb_block_height,
2105 mb_x * mb_block_width,
mb_y * mb_block_height,
2107 ptr_cr = ebuf + 16 * wrap_y + 16;
2112 int progressive_score, interlaced_score;
2114 s->interlaced_dct = 0;
2115 progressive_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
NULL, wrap_y, 8) +
2116 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y * 8,
2117 NULL, wrap_y, 8) - 400;
2119 if (progressive_score > 0) {
2120 interlaced_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
2121 NULL, wrap_y * 2, 8) +
2122 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y,
2123 NULL, wrap_y * 2, 8);
2124 if (progressive_score > interlaced_score) {
2125 s->interlaced_dct = 1;
2128 uv_dct_offset = wrap_c;
2137 s->pdsp.get_pixels(
s->block[0], ptr_y, wrap_y);
2138 s->pdsp.get_pixels(
s->block[1], ptr_y + 8, wrap_y);
2139 s->pdsp.get_pixels(
s->block[2], ptr_y +
dct_offset, wrap_y);
2140 s->pdsp.get_pixels(
s->block[3], ptr_y +
dct_offset + 8, wrap_y);
2146 s->pdsp.get_pixels(
s->block[4], ptr_cb, wrap_c);
2147 s->pdsp.get_pixels(
s->block[5], ptr_cr, wrap_c);
2149 s->pdsp.get_pixels(
s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2150 s->pdsp.get_pixels(
s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2152 s->pdsp.get_pixels(
s->block[ 6], ptr_cb + 8, wrap_c);
2153 s->pdsp.get_pixels(
s->block[ 7], ptr_cr + 8, wrap_c);
2154 s->pdsp.get_pixels(
s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2155 s->pdsp.get_pixels(
s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2156 s->pdsp.get_pixels(
s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2157 s->pdsp.get_pixels(
s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2163 uint8_t *dest_y, *dest_cb, *dest_cr;
2165 dest_y =
s->dest[0];
2166 dest_cb =
s->dest[1];
2167 dest_cr =
s->dest[2];
2170 op_pix =
s->hdsp.put_pixels_tab;
2171 op_qpix =
s->qdsp.put_qpel_pixels_tab;
2173 op_pix =
s->hdsp.put_no_rnd_pixels_tab;
2174 op_qpix =
s->qdsp.put_no_rnd_qpel_pixels_tab;
2179 s->last_picture.f->data,
2181 op_pix =
s->hdsp.avg_pixels_tab;
2182 op_qpix =
s->qdsp.avg_qpel_pixels_tab;
2186 s->next_picture.f->data,
2191 int progressive_score, interlaced_score;
2193 s->interlaced_dct = 0;
2194 progressive_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2195 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y * 8,
2200 progressive_score -= 400;
2202 if (progressive_score > 0) {
2203 interlaced_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y,
2205 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y,
2209 if (progressive_score > interlaced_score) {
2210 s->interlaced_dct = 1;
2213 uv_dct_offset = wrap_c;
2221 s->pdsp.diff_pixels(
s->block[0], ptr_y, dest_y, wrap_y);
2222 s->pdsp.diff_pixels(
s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2225 s->pdsp.diff_pixels(
s->block[3], ptr_y +
dct_offset + 8,
2232 s->pdsp.diff_pixels(
s->block[4], ptr_cb, dest_cb, wrap_c);
2233 s->pdsp.diff_pixels(
s->block[5], ptr_cr, dest_cr, wrap_c);
2235 s->pdsp.diff_pixels(
s->block[6], ptr_cb + uv_dct_offset,
2236 dest_cb + uv_dct_offset, wrap_c);
2237 s->pdsp.diff_pixels(
s->block[7], ptr_cr + uv_dct_offset,
2238 dest_cr + uv_dct_offset, wrap_c);
2242 if (
s->current_picture.mc_mb_var[
s->mb_stride *
mb_y +
mb_x] <
2243 2 *
s->qscale *
s->qscale) {
2245 if (
s->mecc.sad[1](
NULL, ptr_y, dest_y, wrap_y, 8) < 20 *
s->qscale)
2247 if (
s->mecc.sad[1](
NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 *
s->qscale)
2250 wrap_y, 8) < 20 *
s->qscale)
2253 wrap_y, 8) < 20 *
s->qscale)
2255 if (
s->mecc.sad[1](
NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 *
s->qscale)
2257 if (
s->mecc.sad[1](
NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 *
s->qscale)
2260 if (
s->mecc.sad[1](
NULL, ptr_cb + uv_dct_offset,
2261 dest_cb + uv_dct_offset,
2262 wrap_c, 8) < 20 *
s->qscale)
2264 if (
s->mecc.sad[1](
NULL, ptr_cr + uv_dct_offset,
2265 dest_cr + uv_dct_offset,
2266 wrap_c, 8) < 20 *
s->qscale)
2272 if (
s->quantizer_noise_shaping) {
2293 memcpy(orig[0],
s->block[0],
sizeof(int16_t) * 64 * mb_block_count);
2299 for (
i = 0;
i < mb_block_count;
i++) {
2302 s->block_last_index[
i] =
s->dct_quantize(
s,
s->block[
i],
i,
s->qscale, &
overflow);
2311 s->block_last_index[
i] = -1;
2313 if (
s->quantizer_noise_shaping) {
2314 for (
i = 0;
i < mb_block_count;
i++) {
2316 s->block_last_index[
i] =
2318 orig[
i],
i,
s->qscale);
2323 if (
s->luma_elim_threshold && !
s->mb_intra)
2324 for (
i = 0;
i < 4;
i++)
2326 if (
s->chroma_elim_threshold && !
s->mb_intra)
2327 for (
i = 4;
i < mb_block_count;
i++)
2331 for (
i = 0;
i < mb_block_count;
i++) {
2332 if (
s->block_last_index[
i] == -1)
2333 s->coded_score[
i] = INT_MAX / 256;
2339 s->block_last_index[4] =
2340 s->block_last_index[5] = 0;
2342 s->block[5][0] = (1024 +
s->c_dc_scale / 2) /
s->c_dc_scale;
2344 for (
i=6;
i<12;
i++) {
2345 s->block_last_index[
i] = 0;
2346 s->block[
i][0] =
s->block[4][0];
2353 for (
i = 0;
i < mb_block_count;
i++) {
2355 if (
s->block_last_index[
i] > 0) {
2356 for (j = 63; j > 0; j--) {
2357 if (
s->block[
i][
s->intra_scantable.permutated[j]])
2360 s->block_last_index[
i] = j;
2366 switch(
s->codec_id){
2369 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2373 if (CONFIG_MPEG4_ENCODER)
2383 if (CONFIG_WMV2_ENCODER)
2387 if (CONFIG_H261_ENCODER)
2395 if (CONFIG_H263_ENCODER)
2398 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
2405 if (CONFIG_SPEEDHQ_ENCODER)
2428 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2431 d->mb_skip_run=
s->mb_skip_run;
2433 d->last_dc[
i] =
s->last_dc[
i];
2436 d->mv_bits=
s->mv_bits;
2437 d->i_tex_bits=
s->i_tex_bits;
2438 d->p_tex_bits=
s->p_tex_bits;
2439 d->i_count=
s->i_count;
2440 d->skip_count=
s->skip_count;
2441 d->misc_bits=
s->misc_bits;
2445 d->qscale=
s->qscale;
2446 d->dquant=
s->dquant;
2448 d->esc3_level_length=
s->esc3_level_length;
2456 memcpy(
d->mv,
s->mv, 2*4*2*
sizeof(
int));
2457 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2460 d->mb_skip_run=
s->mb_skip_run;
2462 d->last_dc[
i] =
s->last_dc[
i];
2465 d->mv_bits=
s->mv_bits;
2466 d->i_tex_bits=
s->i_tex_bits;
2467 d->p_tex_bits=
s->p_tex_bits;
2468 d->i_count=
s->i_count;
2469 d->skip_count=
s->skip_count;
2470 d->misc_bits=
s->misc_bits;
2472 d->mb_intra=
s->mb_intra;
2473 d->mb_skipped=
s->mb_skipped;
2474 d->mv_type=
s->mv_type;
2475 d->mv_dir=
s->mv_dir;
2477 if(
s->data_partitioning){
2479 d->tex_pb=
s->tex_pb;
2483 d->block_last_index[
i]=
s->block_last_index[
i];
2484 d->interlaced_dct=
s->interlaced_dct;
2485 d->qscale=
s->qscale;
2487 d->esc3_level_length=
s->esc3_level_length;
2492 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2495 uint8_t *dest_backup[3];
2499 s->block=
s->blocks[*next_block];
2500 s->pb=
pb[*next_block];
2501 if(
s->data_partitioning){
2502 s->pb2 =
pb2 [*next_block];
2503 s->tex_pb=
tex_pb[*next_block];
2507 memcpy(dest_backup,
s->dest,
sizeof(
s->dest));
2508 s->dest[0] =
s->sc.rd_scratchpad;
2509 s->dest[1] =
s->sc.rd_scratchpad + 16*
s->linesize;
2510 s->dest[2] =
s->sc.rd_scratchpad + 16*
s->linesize + 8;
2517 if(
s->data_partitioning){
2525 score *=
s->lambda2;
2530 memcpy(
s->dest, dest_backup,
sizeof(
s->dest));
2548 else if(
w==8 &&
h==8)
2566 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
2567 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
2571 return s->mecc.nsse[0](
s,
s->new_picture->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
s->dest[0],
s->linesize, 16) +
2572 s->mecc.nsse[1](
s,
s->new_picture->data[1] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[1],
s->uvlinesize, 8) +
2573 s->mecc.nsse[1](
s,
s->new_picture->data[2] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[2],
s->uvlinesize, 8);
2575 return s->mecc.sse[0](
NULL,
s->new_picture->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
s->dest[0],
s->linesize, 16) +
2576 s->mecc.sse[1](
NULL,
s->new_picture->data[1] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[1],
s->uvlinesize, 8) +
2577 s->mecc.sse[1](
NULL,
s->new_picture->data[2] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[2],
s->uvlinesize, 8);
2580 return sse(
s,
s->new_picture->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
s->dest[0],
w,
h,
s->linesize)
2581 +
sse(
s,
s->new_picture->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*8,
s->dest[1],
w>>1,
h>>1,
s->uvlinesize)
2582 +
sse(
s,
s->new_picture->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*8,
s->dest[2],
w>>1,
h>>1,
s->uvlinesize);
2590 s->me.dia_size=
s->avctx->pre_dia_size;
2591 s->first_slice_line=1;
2592 for(
s->mb_y=
s->end_mb_y-1;
s->mb_y >=
s->start_mb_y;
s->mb_y--) {
2593 for(
s->mb_x=
s->mb_width-1;
s->mb_x >=0 ;
s->mb_x--) {
2596 s->first_slice_line=0;
2607 s->me.dia_size=
s->avctx->dia_size;
2608 s->first_slice_line=1;
2609 for(
s->mb_y=
s->start_mb_y;
s->mb_y <
s->end_mb_y;
s->mb_y++) {
2612 for(
s->mb_x=0;
s->mb_x <
s->mb_width;
s->mb_x++) {
2613 s->block_index[0]+=2;
2614 s->block_index[1]+=2;
2615 s->block_index[2]+=2;
2616 s->block_index[3]+=2;
2624 s->first_slice_line=0;
2637 uint8_t *pix =
s->new_picture->data[0] + (yy *
s->linesize) + xx;
2639 int sum =
s->mpvencdsp.pix_sum(pix,
s->linesize);
2641 varc = (
s->mpvencdsp.pix_norm1(pix,
s->linesize) -
2642 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2644 s->current_picture.mb_var [
s->mb_stride *
mb_y +
mb_x] = varc;
2645 s->current_picture.mb_mean[
s->mb_stride *
mb_y +
mb_x] = (sum+128)>>8;
2646 s->me.mb_var_sum_temp += varc;
2654 if(
s->partitioned_frame){
2659 }
else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2662 }
else if (CONFIG_SPEEDHQ_ENCODER &&
s->out_format ==
FMT_SPEEDHQ) {
2674 uint8_t *ptr =
s->mb_info_ptr +
s->mb_info_size - 12;
2676 int mba =
s->mb_x +
s->mb_width * (
s->mb_y %
s->gob_index);
2677 int gobn =
s->mb_y /
s->gob_index;
2679 if (CONFIG_H263_ENCODER)
2681 bytestream_put_le32(&ptr,
offset);
2682 bytestream_put_byte(&ptr,
s->qscale);
2683 bytestream_put_byte(&ptr, gobn);
2684 bytestream_put_le16(&ptr, mba);
2685 bytestream_put_byte(&ptr, pred_x);
2686 bytestream_put_byte(&ptr, pred_y);
2688 bytestream_put_byte(&ptr, 0);
2689 bytestream_put_byte(&ptr, 0);
2697 s->mb_info_size += 12;
2698 s->prev_mb_info =
s->last_mb_info;
2710 if (!
s->mb_info_size)
2711 s->mb_info_size += 12;
2718 &&
s->slice_context_count == 1
2719 &&
s->pb.buf ==
s->avctx->internal->byte_buffer) {
2720 int lastgob_pos =
s->ptr_lastgob -
s->pb.buf;
2722 uint8_t *new_buffer =
NULL;
2723 int new_buffer_size = 0;
2725 if ((
s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2733 s->avctx->internal->byte_buffer_size + size_increase);
2737 memcpy(new_buffer,
s->avctx->internal->byte_buffer,
s->avctx->internal->byte_buffer_size);
2738 av_free(
s->avctx->internal->byte_buffer);
2739 s->avctx->internal->byte_buffer = new_buffer;
2740 s->avctx->internal->byte_buffer_size = new_buffer_size;
2742 s->ptr_lastgob =
s->pb.buf + lastgob_pos;
2752 int chr_h= 16>>
s->chroma_y_shift;
2777 s->last_dc[
i] = 128 <<
s->intra_dc_precision;
2779 s->current_picture.encoding_error[
i] = 0;
2782 s->last_dc[0] = 128*8/13;
2783 s->last_dc[1] = 128*8/14;
2784 s->last_dc[2] = 128*8/14;
2787 memset(
s->last_mv, 0,
sizeof(
s->last_mv));
2791 switch(
s->codec_id){
2795 if (CONFIG_H263_ENCODER)
2799 if(CONFIG_MPEG4_ENCODER &&
s->partitioned_frame)
2806 s->first_slice_line = 1;
2807 s->ptr_lastgob =
s->pb.buf;
2808 for (mb_y_order =
s->start_mb_y; mb_y_order < s->
end_mb_y; mb_y_order++) {
2812 if (first_in_slice && mb_y_order !=
s->start_mb_y)
2814 s->last_dc[0] =
s->last_dc[1] =
s->last_dc[2] = 1024 <<
s->intra_dc_precision;
2830 int size_increase =
s->avctx->internal->byte_buffer_size/4
2838 if(
s->data_partitioning){
2852 xy=
s->mb_y*
s->mb_stride +
s->mb_x;
2858 int current_packet_size, is_gob_start;
2861 - (
s->ptr_lastgob -
s->pb.buf);
2863 is_gob_start =
s->rtp_payload_size &&
2864 current_packet_size >=
s->rtp_payload_size &&
2867 if(
s->start_mb_y ==
mb_y &&
mb_y > 0 &&
mb_x==0) is_gob_start=1;
2869 switch(
s->codec_id){
2872 if(!
s->h263_slice_structured)
2873 if(
s->mb_x ||
s->mb_y%
s->gob_index) is_gob_start=0;
2876 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
2878 if(
s->mb_skip_run) is_gob_start=0;
2881 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
2897 if (
s->error_rate &&
s->resync_mb_x +
s->resync_mb_y > 0) {
2899 int d = 100 /
s->error_rate;
2901 current_packet_size=0;
2902 s->pb.buf_ptr=
s->ptr_lastgob;
2907 switch(
s->codec_id){
2909 if (CONFIG_MPEG4_ENCODER) {
2916 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2923 if (CONFIG_H263_ENCODER) {
2932 s->misc_bits+=
bits -
s->last_bits;
2936 s->ptr_lastgob += current_packet_size;
2937 s->first_slice_line=1;
2938 s->resync_mb_x=
mb_x;
2939 s->resync_mb_y=
mb_y;
2943 if( (
s->resync_mb_x ==
s->mb_x)
2944 &&
s->resync_mb_y+1 ==
s->mb_y){
2945 s->first_slice_line=0;
2955 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2961 if(
s->data_partitioning){
2962 backup_s.pb2=
s->pb2;
2963 backup_s.tex_pb=
s->tex_pb;
2970 s->mv[0][0][0] =
s->p_mv_table[xy][0];
2971 s->mv[0][0][1] =
s->p_mv_table[xy][1];
2973 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
2980 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
2981 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
2982 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
2985 &dmin, &next_block, 0, 0);
2994 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3001 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3002 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3005 &dmin, &next_block, 0, 0);
3011 s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3012 s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3014 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3020 s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3021 s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3023 &dmin, &next_block,
s->mv[1][0][0],
s->mv[1][0][1]);
3029 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3030 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3031 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3032 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3034 &dmin, &next_block, 0, 0);
3041 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3042 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3043 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3046 &dmin, &next_block, 0, 0);
3053 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3054 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3055 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3058 &dmin, &next_block, 0, 0);
3064 for(dir=0; dir<2; dir++){
3066 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3067 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3068 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3072 &dmin, &next_block, 0, 0);
3081 &dmin, &next_block, 0, 0);
3082 if(
s->h263_pred ||
s->h263_aic){
3084 s->mbintra_table[
mb_x +
mb_y*
s->mb_stride]=1;
3092 const int last_qp= backup_s.qscale;
3096 static const int dquant_tab[4]={-1,1,-2,2};
3097 int storecoefs =
s->mb_intra &&
s->dc_val[0];
3105 s->mv[0][0][0] = best_s.
mv[0][0][0];
3106 s->mv[0][0][1] = best_s.
mv[0][0][1];
3107 s->mv[1][0][0] = best_s.
mv[1][0][0];
3108 s->mv[1][0][1] = best_s.
mv[1][0][1];
3111 for(; qpi<4; qpi++){
3112 int dquant= dquant_tab[qpi];
3114 if(qp < s->
avctx->
qmin || qp >
s->avctx->qmax)
3119 dc[
i]=
s->dc_val[0][
s->block_index[
i] ];
3120 memcpy(ac[
i],
s->ac_val[0][
s->block_index[
i]],
sizeof(int16_t)*16);
3125 &dmin, &next_block,
s->mv[mvdir][0][0],
s->mv[mvdir][0][1]);
3129 s->dc_val[0][
s->block_index[
i] ]=
dc[
i];
3130 memcpy(
s->ac_val[0][
s->block_index[
i]], ac[
i],
sizeof(int16_t)*16);
3138 int mx=
s->b_direct_mv_table[xy][0];
3139 int my=
s->b_direct_mv_table[xy][1];
3141 backup_s.dquant = 0;
3146 &dmin, &next_block, mx, my);
3149 backup_s.dquant = 0;
3154 &dmin, &next_block, 0, 0);
3159 coded |=
s->block_last_index[
i];
3162 memcpy(
s->mv, best_s.
mv,
sizeof(
s->mv));
3184 &dmin, &next_block, mx, my);
3189 s->current_picture.qscale_table[xy] = best_s.
qscale;
3195 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3198 if(
s->data_partitioning){
3201 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3202 s->pb2= backup_s.pb2;
3206 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3207 s->tex_pb= backup_s.tex_pb;
3211 if (CONFIG_H263_ENCODER &&
3216 s->hdsp.put_pixels_tab[0][0](
s->dest[0],
s->sc.rd_scratchpad ,
s->linesize ,16);
3217 s->hdsp.put_pixels_tab[1][0](
s->dest[1],
s->sc.rd_scratchpad + 16*
s->linesize ,
s->uvlinesize, 8);
3218 s->hdsp.put_pixels_tab[1][0](
s->dest[2],
s->sc.rd_scratchpad + 16*
s->linesize + 8,
s->uvlinesize, 8);
3224 int motion_x = 0, motion_y = 0;
3232 motion_x=
s->mv[0][0][0] = 0;
3233 motion_y=
s->mv[0][0][1] = 0;
3238 motion_x=
s->mv[0][0][0] =
s->p_mv_table[xy][0];
3239 motion_y=
s->mv[0][0][1] =
s->p_mv_table[xy][1];
3246 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3247 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3248 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3256 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3257 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3261 if (CONFIG_MPEG4_ENCODER) {
3264 motion_x=
s->b_direct_mv_table[xy][0];
3265 motion_y=
s->b_direct_mv_table[xy][1];
3270 if (CONFIG_MPEG4_ENCODER) {
3279 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3280 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3281 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3282 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3287 motion_x=
s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3288 motion_y=
s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3293 motion_x=
s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3294 motion_y=
s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3301 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3302 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3303 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3311 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3312 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3313 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3320 for(dir=0; dir<2; dir++){
3322 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3323 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3324 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3335 s->last_mv_dir =
s->mv_dir;
3337 if (CONFIG_H263_ENCODER &&
3346 s->p_mv_table[xy][0]=0;
3347 s->p_mv_table[xy][1]=0;
3354 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
3355 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
3357 s->current_picture.encoding_error[0] +=
sse(
3358 s,
s->new_picture->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
3359 s->dest[0],
w,
h,
s->linesize);
3360 s->current_picture.encoding_error[1] +=
sse(
3361 s,
s->new_picture->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3362 s->dest[1],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3363 s->current_picture.encoding_error[2] +=
sse(
3364 s,
s->new_picture->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3365 s->dest[2],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3368 if(CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
3371 ff_dlog(
s->avctx,
"MB %d %d bits\n",
3385 #define MERGE(field) dst->field += src->field; src->field=0
3408 for(
i=0;
i<64;
i++){
3421 if (
s->next_lambda){
3422 s->current_picture_ptr->f->quality =
3423 s->current_picture.f->quality =
s->next_lambda;
3424 if(!dry_run)
s->next_lambda= 0;
3425 }
else if (!
s->fixed_qscale) {
3427 s->current_picture_ptr->f->quality =
3428 s->current_picture.f->quality =
quality;
3429 if (
s->current_picture.f->quality < 0)
3433 if(
s->adaptive_quant){
3434 switch(
s->codec_id){
3436 if (CONFIG_MPEG4_ENCODER)
3442 if (CONFIG_H263_ENCODER)
3449 s->lambda=
s->lambda_table[0];
3452 s->lambda =
s->current_picture.f->quality;
3460 s->time =
s->current_picture_ptr->f->pts *
s->avctx->time_base.num;
3463 s->pb_time=
s->pp_time - (
s->last_non_b_time -
s->time);
3466 s->pp_time=
s->time -
s->last_non_b_time;
3467 s->last_non_b_time=
s->time;
3476 int context_count =
s->slice_context_count;
3481 s->me.mb_var_sum_temp =
3482 s->me.mc_mb_var_sum_temp = 0;
3486 if (
s->out_format ==
FMT_MPEG1 || (
s->h263_pred && !
s->msmpeg4_version))
3491 s->me.scene_change_score=0;
3496 if(
s->msmpeg4_version >= 3)
s->no_rounding=1;
3497 else s->no_rounding=0;
3500 s->no_rounding ^= 1;
3509 s->lambda=
s->last_lambda_for[
s->pict_type];
3511 s->lambda=
s->last_lambda_for[
s->last_non_b_pict_type];
3516 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
3517 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
3518 s->q_chroma_intra_matrix =
s->q_intra_matrix;
3519 s->q_chroma_intra_matrix16 =
s->q_intra_matrix16;
3523 for(
i=1;
i<context_count;
i++){
3534 s->lambda = (
s->lambda *
s->me_penalty_compensation + 128) >> 8;
3535 s->lambda2 = (
s->lambda2 * (int64_t)
s->me_penalty_compensation + 128) >> 8;
3546 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3549 if(!
s->fixed_qscale){
3551 s->avctx->execute(
s->avctx,
mb_var_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3554 for(
i=1;
i<context_count;
i++){
3557 s->current_picture.mc_mb_var_sum=
s->current_picture_ptr->mc_mb_var_sum=
s->me.mc_mb_var_sum_temp;
3558 s->current_picture. mb_var_sum=
s->current_picture_ptr-> mb_var_sum=
s->me. mb_var_sum_temp;
3561 if (
s->me.scene_change_score >
s->scenechange_threshold &&
3564 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3566 if(
s->msmpeg4_version >= 3)
3568 ff_dlog(
s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3569 s->current_picture.mb_var_sum,
s->current_picture.mc_mb_var_sum);
3610 for(dir=0; dir<2; dir++){
3616 s->b_field_mv_table[dir][
i][j], dir ?
s->b_code :
s->f_code,
type, 1);
3627 if (
s->qscale < 3 &&
s->max_qcoeff <= 128 &&
3636 if (
s->avctx->intra_matrix) {
3638 luma_matrix =
s->avctx->intra_matrix;
3640 if (
s->avctx->chroma_intra_matrix)
3641 chroma_matrix =
s->avctx->chroma_intra_matrix;
3645 int j =
s->idsp.idct_permutation[
i];
3647 s->chroma_intra_matrix[j] =
av_clip_uint8((chroma_matrix[
i] *
s->qscale) >> 3);
3650 s->y_dc_scale_table=
3652 s->chroma_intra_matrix[0] =
3655 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3657 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3661 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};
3662 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};
3663 for (
int i = 1;
i < 64;
i++) {
3669 s->y_dc_scale_table = y;
3670 s->c_dc_scale_table =
c;
3671 s->intra_matrix[0] = 13;
3672 s->chroma_intra_matrix[0] = 14;
3674 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3676 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3680 s->y_dc_scale_table=
3685 s->current_picture_ptr->f->key_frame =
3687 s->current_picture_ptr->f->pict_type =
3688 s->current_picture.f->pict_type =
s->pict_type;
3690 if (
s->current_picture.f->key_frame)
3691 s->picture_in_gop_number=0;
3693 s->mb_x =
s->mb_y = 0;
3695 switch(
s->out_format) {
3696 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
3702 if (CONFIG_SPEEDHQ_ENCODER)
3706 if (CONFIG_H261_ENCODER)
3714 else if (CONFIG_MPEG4_ENCODER &&
s->h263_pred) {
3727 else if (CONFIG_H263_ENCODER)
3731 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3738 s->header_bits=
bits -
s->last_bits;
3740 for(
i=1;
i<context_count;
i++){
3743 s->avctx->execute(
s->avctx,
encode_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3744 for(
i=1;
i<context_count;
i++){
3745 if (
s->pb.buf_end ==
s->thread_context[
i]->pb.buf)
3754 const int intra=
s->mb_intra;
3757 s->dct_count[intra]++;
3759 for(
i=0;
i<64;
i++){
3764 s->dct_error_sum[intra][
i] +=
level;
3765 level -=
s->dct_offset[intra][
i];
3768 s->dct_error_sum[intra][
i] -=
level;
3769 level +=
s->dct_offset[intra][
i];
3778 int16_t *
block,
int n,
3782 const uint8_t *scantable;
3783 const uint8_t *perm_scantable;
3785 unsigned int threshold1, threshold2;
3797 int coeff_count[64];
3798 int qmul, qadd, start_i, last_non_zero,
i,
dc;
3799 const int esc_length=
s->ac_esc_length;
3801 uint8_t * last_length;
3807 if(
s->dct_error_sum)
3813 else mpeg2_qscale =
qscale << 1;
3817 scantable=
s->intra_scantable.scantable;
3818 perm_scantable=
s->intra_scantable.permutated;
3835 qmat = n < 4 ?
s->q_intra_matrix[
qscale] :
s->q_chroma_intra_matrix[
qscale];
3836 matrix = n < 4 ?
s->intra_matrix :
s->chroma_intra_matrix;
3840 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
3841 length =
s->intra_chroma_ac_vlc_length;
3842 last_length=
s->intra_chroma_ac_vlc_last_length;
3844 length =
s->intra_ac_vlc_length;
3845 last_length=
s->intra_ac_vlc_last_length;
3848 scantable=
s->inter_scantable.scantable;
3849 perm_scantable=
s->inter_scantable.permutated;
3852 qmat =
s->q_inter_matrix[
qscale];
3854 length =
s->inter_ac_vlc_length;
3855 last_length=
s->inter_ac_vlc_last_length;
3860 threshold2= (threshold1<<1);
3862 for(
i=63;
i>=start_i;
i--) {
3863 const int j = scantable[
i];
3866 if(((
unsigned)(
level+threshold1))>threshold2){
3872 for(
i=start_i;
i<=last_non_zero;
i++) {
3873 const int j = scantable[
i];
3878 if(((
unsigned)(
level+threshold1))>threshold2){
3901 if(last_non_zero < start_i){
3902 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3903 return last_non_zero;
3906 score_tab[start_i]= 0;
3907 survivor[0]= start_i;
3910 for(
i=start_i;
i<=last_non_zero;
i++){
3911 int level_index, j, zero_distortion;
3913 int best_score=256*256*256*120;
3917 zero_distortion= dct_coeff*dct_coeff;
3919 for(level_index=0; level_index < coeff_count[
i]; level_index++){
3928 unquant_coeff= alevel*qmul + qadd;
3930 j =
s->idsp.idct_permutation[scantable[
i]];
3931 unquant_coeff = alevel *
matrix[j] * 8;
3933 j =
s->idsp.idct_permutation[scantable[
i]];
3935 unquant_coeff = (
int)( alevel * mpeg2_qscale *
matrix[j]) >> 4;
3936 unquant_coeff = (unquant_coeff - 1) | 1;
3938 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int)
matrix[j])) >> 5;
3939 unquant_coeff = (unquant_coeff - 1) | 1;
3944 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3946 if((
level&(~127)) == 0){
3947 for(j=survivor_count-1; j>=0; j--){
3948 int run=
i - survivor[j];
3950 score += score_tab[
i-
run];
3952 if(score < best_score){
3955 level_tab[
i+1]=
level-64;
3960 for(j=survivor_count-1; j>=0; j--){
3961 int run=
i - survivor[j];
3963 score += score_tab[
i-
run];
3964 if(score < last_score){
3967 last_level=
level-64;
3973 distortion += esc_length*
lambda;
3974 for(j=survivor_count-1; j>=0; j--){
3975 int run=
i - survivor[j];
3976 int score= distortion + score_tab[
i-
run];
3978 if(score < best_score){
3981 level_tab[
i+1]=
level-64;
3986 for(j=survivor_count-1; j>=0; j--){
3987 int run=
i - survivor[j];
3988 int score= distortion + score_tab[
i-
run];
3989 if(score < last_score){
3992 last_level=
level-64;
4000 score_tab[
i+1]= best_score;
4003 if(last_non_zero <= 27){
4004 for(; survivor_count; survivor_count--){
4005 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4009 for(; survivor_count; survivor_count--){
4010 if(score_tab[ survivor[survivor_count-1] ] <= best_score +
lambda)
4015 survivor[ survivor_count++ ]=
i+1;
4019 last_score= 256*256*256*120;
4020 for(
i= survivor[0];
i<=last_non_zero + 1;
i++){
4021 int score= score_tab[
i];
4025 if(score < last_score){
4028 last_level= level_tab[
i];
4029 last_run= run_tab[
i];
4034 s->coded_score[n] = last_score;
4037 last_non_zero= last_i - 1;
4038 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4040 if(last_non_zero < start_i)
4041 return last_non_zero;
4043 if(last_non_zero == 0 && start_i == 0){
4045 int best_score=
dc *
dc;
4047 for(
i=0;
i<coeff_count[0];
i++){
4050 int unquant_coeff, score, distortion;
4053 unquant_coeff= (alevel*qmul + qadd)>>3;
4055 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int)
matrix[0])) >> 5;
4056 unquant_coeff = (unquant_coeff - 1) | 1;
4058 unquant_coeff = (unquant_coeff + 4) >> 3;
4059 unquant_coeff<<= 3 + 3;
4061 distortion= (unquant_coeff -
dc) * (unquant_coeff -
dc);
4064 else score= distortion + esc_length*
lambda;
4066 if(score < best_score){
4068 best_level=
level - 64;
4071 block[0]= best_level;
4072 s->coded_score[n] = best_score -
dc*
dc;
4073 if(best_level == 0)
return -1;
4074 else return last_non_zero;
4080 block[ perm_scantable[last_non_zero] ]= last_level;
4083 for(;
i>start_i;
i -= run_tab[
i] + 1){
4084 block[ perm_scantable[
i-1] ]= level_tab[
i];
4087 return last_non_zero;
4102 if(
i==0)
s*= sqrt(0.5);
4103 if(j==0)
s*= sqrt(0.5);
4116 const uint8_t *scantable;
4117 const uint8_t *perm_scantable;
4123 int qmul, qadd, start_i, last_non_zero,
i,
dc;
4125 uint8_t * last_length;
4127 int rle_index,
run, q = 1, sum;
4129 if(
basis[0][0] == 0)
4135 scantable=
s->intra_scantable.scantable;
4136 perm_scantable=
s->intra_scantable.permutated;
4154 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
4155 length =
s->intra_chroma_ac_vlc_length;
4156 last_length=
s->intra_chroma_ac_vlc_last_length;
4158 length =
s->intra_ac_vlc_length;
4159 last_length=
s->intra_ac_vlc_last_length;
4162 scantable=
s->inter_scantable.scantable;
4163 perm_scantable=
s->inter_scantable.permutated;
4166 length =
s->inter_ac_vlc_length;
4167 last_length=
s->inter_ac_vlc_last_length;
4169 last_non_zero =
s->block_last_index[n];
4172 for(
i=0;
i<64;
i++){
4177 for(
i=0;
i<64;
i++){
4183 w= 15 + (48*qns*one +
w/2)/
w;
4196 for(
i=start_i;
i<=last_non_zero;
i++){
4197 int j= perm_scantable[
i];
4204 run_tab[rle_index++]=
run;
4214 int best_score =
s->mpvencdsp.try_8x8basis(rem,
weight,
basis[0], 0);
4217 int run2, best_unquant_change=0, analyze_gradient;
4218 analyze_gradient = last_non_zero > 2 ||
s->quantizer_noise_shaping >= 3;
4220 if(analyze_gradient){
4221 for(
i=0;
i<64;
i++){
4231 int change, old_coeff;
4237 for(change=-1; change<=1; change+=2){
4238 int new_level=
level + change;
4239 int score, new_coeff;
4241 new_coeff= q*new_level;
4242 if(new_coeff >= 2048 || new_coeff < 0)
4245 score =
s->mpvencdsp.try_8x8basis(rem,
weight,
basis[0],
4246 new_coeff - old_coeff);
4247 if(score<best_score){