34 #include "config_components.h"
81 #define QUANT_BIAS_SHIFT 8
83 #define QMAT_SHIFT_MMX 16
109 uint16_t (*
qmat16)[2][64],
110 const uint16_t *quant_matrix,
111 int bias,
int qmin,
int qmax,
int intra)
122 else qscale2 =
qscale << 1;
129 for (
i = 0;
i < 64;
i++) {
130 const int j =
s->idsp.idct_permutation[
i];
131 int64_t den = (int64_t) qscale2 * quant_matrix[j];
141 for (
i = 0;
i < 64;
i++) {
142 const int j =
s->idsp.idct_permutation[
i];
143 int64_t den =
ff_aanscales[
i] * (int64_t) qscale2 * quant_matrix[j];
153 for (
i = 0;
i < 64;
i++) {
154 const int j =
s->idsp.idct_permutation[
i];
155 int64_t den = (int64_t) qscale2 * quant_matrix[j];
175 for (
i = intra;
i < 64;
i++) {
187 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
194 if (
s->q_scale_type == 1 && 0) {
196 int bestdiff=INT_MAX;
204 if (
diff < bestdiff) {
213 s->qscale =
av_clip(
s->qscale,
s->avctx->qmin,
s->vbv_ignore_qmax ? 31 :
s->avctx->qmax);
226 for (
i = 0;
i < 64;
i++) {
238 int8_t *
const qscale_table =
s->current_picture.qscale_table;
241 for (
i = 0;
i <
s->mb_num;
i++) {
242 unsigned int lam =
s->lambda_table[
s->mb_index2xy[
i]];
244 qscale_table[
s->mb_index2xy[
i]] =
av_clip(qp,
s->avctx->qmin,
252 #define COPY(a) dst->a= src->a
268 for (
int i = -16;
i < 16;
i++)
287 s->input_picture_number = 0;
288 s->picture_in_gop_number = 0;
297 if (CONFIG_H263_ENCODER)
299 if (!
s->dct_quantize)
303 s->fast_dct_quantize =
s->dct_quantize;
304 if (
s->avctx->trellis)
344 "keyframe interval too large!, reducing it from %d to %d\n",
356 "max b frames must be 0 or positive for mpegvideo based encoders\n");
367 s->rtp_mode = !!
s->rtp_payload_size;
371 if (
s->intra_dc_precision < 0) {
372 s->intra_dc_precision += 8;
373 }
else if (
s->intra_dc_precision >= 8)
374 s->intra_dc_precision -= 8;
376 if (
s->intra_dc_precision < 0) {
378 "intra dc precision must be positive, note some applications use"
379 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
389 if (
s->gop_size <= 1) {
443 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
460 "impossible bitrate constraints, this will fail\n");
470 if (!
s->fixed_qscale &&
476 if (nbt <= INT_MAX) {
489 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
490 "specified vbv buffer is too large for the given bitrate!\n");
502 "OBMC is only supported with simple mb decision\n");
517 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
570 if (
s->scenechange_threshold < 1000000000 &&
573 "closed gop with scene change detection are not supported yet, "
574 "set threshold to 1000000000\n");
582 "low delay forcing is only available for mpeg2, "
583 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
586 if (
s->max_b_frames != 0) {
588 "B-frames cannot be used with low delay\n");
593 if (
s->q_scale_type == 1) {
596 "non linear quant only supports qmax <= 28 currently\n");
609 "notice: b_frame_strategy only affects the first pass\n");
610 s->b_frame_strategy = 0;
624 s->inter_quant_bias = 0;
626 s->intra_quant_bias = 0;
641 "timebase %d/%d not supported by MPEG 4 standard, "
642 "the maximum admitted value for the timebase denominator "
653 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
658 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
661 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
675 if (!CONFIG_SPEEDHQ_ENCODER)
683 if (!CONFIG_H261_ENCODER)
687 "The specified picture size of %dx%d is not valid for the "
688 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
689 s->width,
s->height);
698 if (!CONFIG_H263_ENCODER)
701 s->width,
s->height) == 8) {
703 "The specified picture size of %dx%d is not valid for "
704 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
705 "352x288, 704x576, and 1408x1152. "
706 "Try H.263+.\n",
s->width,
s->height);
718 s->modified_quant =
s->h263_aic;
720 s->unrestricted_mv =
s->obmc ||
s->loop_filter ||
s->umvplus;
730 s->unrestricted_mv = 1;
744 s->modified_quant = 1;
748 s->unrestricted_mv = 0;
753 s->unrestricted_mv = 1;
754 s->low_delay =
s->max_b_frames ? 0 : 1;
755 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
760 s->unrestricted_mv = 1;
761 s->msmpeg4_version = 2;
768 s->unrestricted_mv = 1;
769 s->msmpeg4_version = 3;
770 s->flipflop_rounding = 1;
777 s->unrestricted_mv = 1;
778 s->msmpeg4_version = 4;
779 s->flipflop_rounding = 1;
786 s->unrestricted_mv = 1;
787 s->msmpeg4_version = 5;
788 s->flipflop_rounding = 1;
800 s->progressive_frame =
827 mb_array_size =
s->mb_stride *
s->mb_height;
833 if (
s->noise_reduction) {
840 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) &&
s->modified_quant)
843 if (
s->slice_context_count > 1) {
847 s->h263_slice_structured = 1;
850 s->quant_precision = 5;
853 ff_set_cmp(&
s->mecc,
s->mecc.frame_skip_cmp,
s->frame_skip_cmp);
855 if (CONFIG_H261_ENCODER &&
s->out_format ==
FMT_H261) {
857 }
else if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
860 }
else if (CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263) {
862 if (CONFIG_MSMPEG4ENC &&
s->msmpeg4_version)
867 for (
i = 0;
i < 64;
i++) {
868 int j =
s->idsp.idct_permutation[
i];
881 s->chroma_intra_matrix[j] =
905 if (
s->b_frame_strategy == 2) {
906 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
908 if (!
s->tmp_frames[
i])
912 s->tmp_frames[
i]->width =
s->width >>
s->brd_scale;
913 s->tmp_frames[
i]->height =
s->height >>
s->brd_scale;
948 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
949 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
950 s->q_chroma_intra_matrix=
NULL;
951 s->q_chroma_intra_matrix16=
NULL;
971 for (y = 0; y < 16; y++) {
972 for (x = 0; x < 16; x++) {
989 for (y = 0; y <
h; y += 16) {
990 for (x = 0; x <
w; x += 16) {
997 acc += sae + 500 < sad;
1006 s->chroma_x_shift,
s->chroma_y_shift,
s->out_format,
1007 s->mb_stride,
s->mb_width,
s->mb_height,
s->b8_stride,
1008 &
s->linesize, &
s->uvlinesize);
1015 int i, display_picture_number = 0,
ret;
1016 int encoding_delay =
s->max_b_frames ?
s->max_b_frames
1017 : (
s->low_delay ? 0 : 1);
1018 int flush_offset = 1;
1023 display_picture_number =
s->input_picture_number++;
1027 int64_t last =
s->user_specified_pts;
1031 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1036 if (!
s->low_delay && display_picture_number == 1)
1037 s->dts_delta =
pts - last;
1039 s->user_specified_pts =
pts;
1042 s->user_specified_pts =
1043 pts =
s->user_specified_pts + 1;
1045 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1048 pts = display_picture_number;
1052 if (!pic_arg->
buf[0] ||
1054 pic_arg->
linesize[1] !=
s->uvlinesize ||
1057 if ((
s->width & 15) || (
s->height & 15))
1065 pic_arg->
linesize[1],
s->linesize,
s->uvlinesize);
1071 pic = &
s->picture[
i];
1088 int h_chroma_shift, v_chroma_shift;
1093 for (
i = 0;
i < 3;
i++) {
1095 int dst_stride =
i ?
s->uvlinesize :
s->linesize;
1096 int h_shift =
i ? h_chroma_shift : 0;
1097 int v_shift =
i ? v_chroma_shift : 0;
1098 int w =
s->width >> h_shift;
1099 int h =
s->height >> v_shift;
1100 const uint8_t *
src = pic_arg->
data[
i];
1101 uint8_t *dst = pic->
f->
data[
i];
1105 && !
s->progressive_sequence
1106 &&
FFALIGN(
s->height, 32) -
s->height > 16)
1109 if (!
s->avctx->rc_buffer_size)
1112 if (src_stride == dst_stride)
1113 memcpy(dst,
src, src_stride *
h);
1116 uint8_t *dst2 = dst;
1118 memcpy(dst2,
src,
w);
1123 if ((
s->width & 15) || (
s->height & (vpad-1))) {
1124 s->mpvencdsp.draw_edges(dst, dst_stride,
1143 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1144 if (
s->input_picture[flush_offset])
1147 if (flush_offset <= 1)
1150 encoding_delay = encoding_delay - flush_offset + 1;
1155 s->input_picture[
i - flush_offset] =
s->input_picture[
i];
1157 s->input_picture[encoding_delay] = pic;
1166 int64_t score64 = 0;
1168 for (plane = 0; plane < 3; plane++) {
1170 const int bw = plane ? 1 : 2;
1171 for (y = 0; y <
s->mb_height * bw; y++) {
1172 for (x = 0; x <
s->mb_width * bw; x++) {
1173 int off = p->
shared ? 0 : 16;
1174 const uint8_t *dptr = p->
f->
data[plane] + 8 * (x + y *
stride) + off;
1175 const uint8_t *rptr =
ref->f->data[plane] + 8 * (x + y *
stride);
1176 int v =
s->mecc.frame_skip_cmp[1](
s, dptr, rptr,
stride, 8);
1178 switch (
FFABS(
s->frame_skip_exp)) {
1179 case 0: score =
FFMAX(score, v);
break;
1180 case 1: score +=
FFABS(v);
break;
1181 case 2: score64 += v * (int64_t)v;
break;
1182 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1183 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1192 if (
s->frame_skip_exp < 0)
1193 score64 = pow(score64 / (
double)(
s->mb_width *
s->mb_height),
1194 -1.0/
s->frame_skip_exp);
1198 if (score64 < ((
s->frame_skip_factor * (int64_t)
s->lambda) >> 8))
1227 const int scale =
s->brd_scale;
1231 int64_t best_rd = INT64_MAX;
1232 int best_b_count = -1;
1247 b_lambda = p_lambda;
1251 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1252 const Picture *pre_input_ptr =
i ?
s->input_picture[
i - 1] :
1253 s->next_picture_ptr;
1255 if (pre_input_ptr) {
1256 const uint8_t *
data[4];
1259 if (!pre_input_ptr->
shared &&
i) {
1265 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[0],
1266 s->tmp_frames[
i]->linesize[0],
1270 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[1],
1271 s->tmp_frames[
i]->linesize[1],
1275 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[2],
1276 s->tmp_frames[
i]->linesize[2],
1283 for (j = 0; j <
s->max_b_frames + 1; j++) {
1287 if (!
s->input_picture[j])
1300 c->mb_decision =
s->avctx->mb_decision;
1301 c->me_cmp =
s->avctx->me_cmp;
1302 c->mb_cmp =
s->avctx->mb_cmp;
1303 c->me_sub_cmp =
s->avctx->me_sub_cmp;
1305 c->time_base =
s->avctx->time_base;
1306 c->max_b_frames =
s->max_b_frames;
1324 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1325 int is_p =
i % (j + 1) == j ||
i ==
s->max_b_frames;
1327 s->tmp_frames[
i + 1]->pict_type = is_p ?
1329 s->tmp_frames[
i + 1]->quality = is_p ? p_lambda : b_lambda;
1348 rd +=
c->error[0] +
c->error[1] +
c->error[2];
1366 return best_b_count;
1374 s->reordered_input_picture[
i - 1] =
s->reordered_input_picture[
i];
1378 if (!
s->reordered_input_picture[0] &&
s->input_picture[0]) {
1379 if (
s->frame_skip_threshold ||
s->frame_skip_factor) {
1380 if (
s->picture_in_gop_number <
s->gop_size &&
1381 s->next_picture_ptr &&
1393 !
s->next_picture_ptr ||
s->intra_only) {
1394 s->reordered_input_picture[0] =
s->input_picture[0];
1396 s->reordered_input_picture[0]->f->coded_picture_number =
1397 s->coded_picture_number++;
1402 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1403 int pict_num =
s->input_picture[0]->f->display_picture_number +
i;
1405 if (pict_num >=
s->rc_context.num_entries)
1407 if (!
s->input_picture[
i]) {
1412 s->input_picture[
i]->f->pict_type =
1413 s->rc_context.entry[pict_num].new_pict_type;
1417 if (
s->b_frame_strategy == 0) {
1418 b_frames =
s->max_b_frames;
1419 while (b_frames && !
s->input_picture[b_frames])
1421 }
else if (
s->b_frame_strategy == 1) {
1422 for (
i = 1;
i <
s->max_b_frames + 1;
i++) {
1423 if (
s->input_picture[
i] &&
1424 s->input_picture[
i]->b_frame_score == 0) {
1425 s->input_picture[
i]->b_frame_score =
1427 s->input_picture[
i ]->f->data[0],
1428 s->input_picture[
i - 1]->f->data[0],
1432 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1433 if (!
s->input_picture[
i] ||
1434 s->input_picture[
i]->b_frame_score - 1 >
1435 s->mb_num /
s->b_sensitivity)
1439 b_frames =
FFMAX(0,
i - 1);
1442 for (
i = 0;
i < b_frames + 1;
i++) {
1443 s->input_picture[
i]->b_frame_score = 0;
1445 }
else if (
s->b_frame_strategy == 2) {
1453 for (
i = b_frames - 1;
i >= 0;
i--) {
1454 int type =
s->input_picture[
i]->f->pict_type;
1459 b_frames ==
s->max_b_frames) {
1461 "warning, too many B-frames in a row\n");
1464 if (
s->picture_in_gop_number + b_frames >=
s->gop_size) {
1466 s->gop_size >
s->picture_in_gop_number) {
1467 b_frames =
s->gop_size -
s->picture_in_gop_number - 1;
1479 s->reordered_input_picture[0] =
s->input_picture[b_frames];
1482 s->reordered_input_picture[0]->f->coded_picture_number =
1483 s->coded_picture_number++;
1484 for (
i = 0;
i < b_frames;
i++) {
1485 s->reordered_input_picture[
i + 1] =
s->input_picture[
i];
1486 s->reordered_input_picture[
i + 1]->f->pict_type =
1488 s->reordered_input_picture[
i + 1]->f->coded_picture_number =
1489 s->coded_picture_number++;
1496 if (
s->reordered_input_picture[0]) {
1497 s->reordered_input_picture[0]->reference =
1498 s->reordered_input_picture[0]->f->pict_type !=
1502 s->reordered_input_picture[0]->f)))
1505 if (
s->reordered_input_picture[0]->shared ||
s->avctx->rc_buffer_size) {
1513 pic = &
s->picture[
i];
1515 pic->
reference =
s->reordered_input_picture[0]->reference;
1526 s->reordered_input_picture[0]->shared = 0;
1528 s->current_picture_ptr = pic;
1531 s->current_picture_ptr =
s->reordered_input_picture[0];
1532 for (
i = 0;
i < 4;
i++) {
1533 if (
s->new_picture->data[
i])
1537 s->picture_number =
s->new_picture->display_picture_number;
1544 if (
s->unrestricted_mv &&
1545 s->current_picture.reference &&
1548 int hshift =
desc->log2_chroma_w;
1549 int vshift =
desc->log2_chroma_h;
1550 s->mpvencdsp.draw_edges(
s->current_picture.f->data[0],
1551 s->current_picture.f->linesize[0],
1552 s->h_edge_pos,
s->v_edge_pos,
1555 s->mpvencdsp.draw_edges(
s->current_picture.f->data[1],
1556 s->current_picture.f->linesize[1],
1557 s->h_edge_pos >> hshift,
1558 s->v_edge_pos >> vshift,
1562 s->mpvencdsp.draw_edges(
s->current_picture.f->data[2],
1563 s->current_picture.f->linesize[2],
1564 s->h_edge_pos >> hshift,
1565 s->v_edge_pos >> vshift,
1573 s->last_pict_type =
s->pict_type;
1574 s->last_lambda_for [
s->pict_type] =
s->current_picture_ptr->f->quality;
1576 s->last_non_b_pict_type =
s->pict_type;
1583 for (intra = 0; intra < 2; intra++) {
1584 if (
s->dct_count[intra] > (1 << 16)) {
1585 for (
i = 0;
i < 64;
i++) {
1586 s->dct_error_sum[intra][
i] >>= 1;
1588 s->dct_count[intra] >>= 1;
1591 for (
i = 0;
i < 64;
i++) {
1592 s->dct_offset[intra][
i] = (
s->noise_reduction *
1593 s->dct_count[intra] +
1594 s->dct_error_sum[intra][
i] / 2) /
1595 (
s->dct_error_sum[intra][
i] + 1);
1606 s->last_picture_ptr !=
s->next_picture_ptr &&
1607 s->last_picture_ptr->f->buf[0]) {
1611 s->current_picture_ptr->f->pict_type =
s->pict_type;
1616 s->current_picture_ptr)) < 0)
1620 s->last_picture_ptr =
s->next_picture_ptr;
1621 s->next_picture_ptr =
s->current_picture_ptr;
1624 if (
s->last_picture_ptr) {
1626 if (
s->last_picture_ptr->f->buf[0] &&
1628 s->last_picture_ptr)) < 0)
1631 if (
s->next_picture_ptr) {
1633 if (
s->next_picture_ptr->f->buf[0] &&
1635 s->next_picture_ptr)) < 0)
1641 for (
i = 0;
i < 4;
i++) {
1643 s->current_picture.f->data[
i] +=
1644 s->current_picture.f->linesize[
i];
1646 s->current_picture.f->linesize[
i] *= 2;
1647 s->last_picture.f->linesize[
i] *= 2;
1648 s->next_picture.f->linesize[
i] *= 2;
1653 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
1654 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
1656 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
1657 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
1659 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
1660 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
1663 if (
s->dct_error_sum) {
1672 const AVFrame *pic_arg,
int *got_packet)
1675 int i, stuffing_count,
ret;
1676 int context_count =
s->slice_context_count;
1678 s->vbv_ignore_qmax = 0;
1680 s->picture_in_gop_number++;
1690 if (
s->new_picture->data[0]) {
1691 int growing_buffer = context_count == 1 && !
s->data_partitioning;
1692 size_t pkt_size = 10000 +
s->mb_width *
s->mb_height *
1705 s->mb_width*
s->mb_height*12);
1706 s->prev_mb_info =
s->last_mb_info =
s->mb_info_size = 0;
1709 for (
i = 0;
i < context_count;
i++) {
1710 int start_y =
s->thread_context[
i]->start_mb_y;
1712 int h =
s->mb_height;
1713 uint8_t *start =
pkt->
data + (size_t)(((int64_t)
pkt->
size) * start_y /
h);
1714 uint8_t *end =
pkt->
data + (size_t)(((int64_t)
pkt->
size) * end_y /
h);
1719 s->pict_type =
s->new_picture->pict_type;
1726 if (growing_buffer) {
1736 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
s->out_format ==
FMT_MJPEG)
1746 s->lambda <
s->lmax) {
1747 s->next_lambda =
FFMAX(
s->lambda + min_step,
s->lambda *
1748 (
s->qscale + 1) /
s->qscale);
1749 if (
s->adaptive_quant) {
1751 for (
i = 0;
i <
s->mb_height *
s->mb_stride;
i++)
1752 s->lambda_table[
i] =
1753 FFMAX(
s->lambda_table[
i] + min_step,
1754 s->lambda_table[
i] * (
s->qscale + 1) /
1760 if (
s->flipflop_rounding ||
1763 s->no_rounding ^= 1;
1766 s->time_base =
s->last_time_base;
1767 s->last_non_b_time =
s->time -
s->pp_time;
1769 for (
i = 0;
i < context_count;
i++) {
1773 s->vbv_ignore_qmax = 1;
1784 for (
i = 0;
i < 4;
i++) {
1794 s->misc_bits +
s->i_tex_bits +
1800 s->stuffing_bits = 8*stuffing_count;
1801 if (stuffing_count) {
1807 switch (
s->codec_id) {
1810 while (stuffing_count--) {
1817 stuffing_count -= 4;
1818 while (stuffing_count--) {
1824 s->stuffing_bits = 0;
1842 int minbits =
s->frame_bits - 8 *
1843 (
s->vbv_delay_pos - 1);
1844 double bits =
s->rc_context.buffer_index + minbits - inbits;
1845 uint8_t *
const vbv_delay_ptr =
s->pb.buf +
s->vbv_delay_pos;
1849 "Internal error, negative bits\n");
1861 vbv_delay_ptr[0] &= 0xF8;
1864 vbv_delay_ptr[2] &= 0x07;
1873 (uint8_t*)props, props_size);
1879 s->total_bits +=
s->frame_bits;
1881 pkt->
pts =
s->current_picture.f->pts;
1883 if (!
s->current_picture.f->coded_picture_number)
1890 if (
s->current_picture.f->key_frame)
1900 if (!
s->picture[
i].reference)
1912 int n,
int threshold)
1914 static const char tab[64] = {
1915 3, 2, 2, 1, 1, 1, 1, 1,
1916 1, 1, 1, 1, 1, 1, 1, 1,
1917 1, 1, 1, 1, 1, 1, 1, 1,
1918 0, 0, 0, 0, 0, 0, 0, 0,
1919 0, 0, 0, 0, 0, 0, 0, 0,
1920 0, 0, 0, 0, 0, 0, 0, 0,
1921 0, 0, 0, 0, 0, 0, 0, 0,
1922 0, 0, 0, 0, 0, 0, 0, 0
1927 int16_t *
block =
s->block[n];
1928 const int last_index =
s->block_last_index[n];
1931 if (threshold < 0) {
1933 threshold = -threshold;
1938 if (last_index <= skip_dc - 1)
1941 for (
i = 0;
i <= last_index;
i++) {
1942 const int j =
s->intra_scantable.permutated[
i];
1945 if (skip_dc &&
i == 0)
1949 }
else if (
level > 1) {
1955 if (score >= threshold)
1957 for (
i = skip_dc;
i <= last_index;
i++) {
1958 const int j =
s->intra_scantable.permutated[
i];
1962 s->block_last_index[n] = 0;
1964 s->block_last_index[n] = -1;
1971 const int maxlevel =
s->max_qcoeff;
1972 const int minlevel =
s->min_qcoeff;
1980 for (;
i <= last_index;
i++) {
1981 const int j =
s->intra_scantable.permutated[
i];
1984 if (
level > maxlevel) {
1987 }
else if (
level < minlevel) {
1997 "warning, clipping %d dct coefficients to %d..%d\n",
2005 for (y = 0; y < 8; y++) {
2006 for (x = 0; x < 8; x++) {
2012 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2013 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2014 int v = ptr[x2 + y2 *
stride];
2026 int motion_x,
int motion_y,
2027 int mb_block_height,
2036 #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2037 (s)->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2039 int16_t orig[12][64];
2040 const int mb_x =
s->mb_x;
2041 const int mb_y =
s->mb_y;
2045 int uv_dct_offset =
s->uvlinesize * 8;
2046 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2047 ptrdiff_t wrap_y, wrap_c;
2049 for (
i = 0;
i < mb_block_count;
i++)
2050 skip_dct[
i] =
s->skipdct;
2052 if (
s->adaptive_quant) {
2053 const int last_qp =
s->qscale;
2054 const int mb_xy =
mb_x +
mb_y *
s->mb_stride;
2056 s->lambda =
s->lambda_table[mb_xy];
2060 s->qscale =
s->current_picture_ptr->qscale_table[mb_xy];
2061 s->dquant =
s->qscale - last_qp;
2082 wrap_y =
s->linesize;
2083 wrap_c =
s->uvlinesize;
2084 ptr_y =
s->new_picture->data[0] +
2086 ptr_cb =
s->new_picture->data[1] +
2087 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2088 ptr_cr =
s->new_picture->data[2] +
2089 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2092 uint8_t *ebuf =
s->sc.edge_emu_buffer + 38 * wrap_y;
2095 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2098 s->width,
s->height);
2100 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2102 mb_block_width, mb_block_height,
2103 mb_x * mb_block_width,
mb_y * mb_block_height,
2105 ptr_cb = ebuf + 16 * wrap_y;
2106 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2108 mb_block_width, mb_block_height,
2109 mb_x * mb_block_width,
mb_y * mb_block_height,
2111 ptr_cr = ebuf + 16 * wrap_y + 16;
2116 int progressive_score, interlaced_score;
2118 s->interlaced_dct = 0;
2119 progressive_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
NULL, wrap_y, 8) +
2120 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y * 8,
2121 NULL, wrap_y, 8) - 400;
2123 if (progressive_score > 0) {
2124 interlaced_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
2125 NULL, wrap_y * 2, 8) +
2126 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y,
2127 NULL, wrap_y * 2, 8);
2128 if (progressive_score > interlaced_score) {
2129 s->interlaced_dct = 1;
2132 uv_dct_offset = wrap_c;
2141 s->pdsp.get_pixels(
s->block[0], ptr_y, wrap_y);
2142 s->pdsp.get_pixels(
s->block[1], ptr_y + 8, wrap_y);
2143 s->pdsp.get_pixels(
s->block[2], ptr_y +
dct_offset, wrap_y);
2144 s->pdsp.get_pixels(
s->block[3], ptr_y +
dct_offset + 8, wrap_y);
2150 s->pdsp.get_pixels(
s->block[4], ptr_cb, wrap_c);
2151 s->pdsp.get_pixels(
s->block[5], ptr_cr, wrap_c);
2153 s->pdsp.get_pixels(
s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2154 s->pdsp.get_pixels(
s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2156 s->pdsp.get_pixels(
s->block[ 6], ptr_cb + 8, wrap_c);
2157 s->pdsp.get_pixels(
s->block[ 7], ptr_cr + 8, wrap_c);
2158 s->pdsp.get_pixels(
s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2159 s->pdsp.get_pixels(
s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2160 s->pdsp.get_pixels(
s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2161 s->pdsp.get_pixels(
s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2167 uint8_t *dest_y, *dest_cb, *dest_cr;
2169 dest_y =
s->dest[0];
2170 dest_cb =
s->dest[1];
2171 dest_cr =
s->dest[2];
2174 op_pix =
s->hdsp.put_pixels_tab;
2175 op_qpix =
s->qdsp.put_qpel_pixels_tab;
2177 op_pix =
s->hdsp.put_no_rnd_pixels_tab;
2178 op_qpix =
s->qdsp.put_no_rnd_qpel_pixels_tab;
2183 s->last_picture.f->data,
2185 op_pix =
s->hdsp.avg_pixels_tab;
2186 op_qpix =
s->qdsp.avg_qpel_pixels_tab;
2190 s->next_picture.f->data,
2195 int progressive_score, interlaced_score;
2197 s->interlaced_dct = 0;
2198 progressive_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2199 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y * 8,
2204 progressive_score -= 400;
2206 if (progressive_score > 0) {
2207 interlaced_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y,
2209 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y,
2213 if (progressive_score > interlaced_score) {
2214 s->interlaced_dct = 1;
2217 uv_dct_offset = wrap_c;
2225 s->pdsp.diff_pixels(
s->block[0], ptr_y, dest_y, wrap_y);
2226 s->pdsp.diff_pixels(
s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2229 s->pdsp.diff_pixels(
s->block[3], ptr_y +
dct_offset + 8,
2236 s->pdsp.diff_pixels(
s->block[4], ptr_cb, dest_cb, wrap_c);
2237 s->pdsp.diff_pixels(
s->block[5], ptr_cr, dest_cr, wrap_c);
2239 s->pdsp.diff_pixels(
s->block[6], ptr_cb + uv_dct_offset,
2240 dest_cb + uv_dct_offset, wrap_c);
2241 s->pdsp.diff_pixels(
s->block[7], ptr_cr + uv_dct_offset,
2242 dest_cr + uv_dct_offset, wrap_c);
2246 if (
s->mc_mb_var[
s->mb_stride *
mb_y +
mb_x] < 2 *
s->qscale *
s->qscale) {
2248 if (
s->mecc.sad[1](
NULL, ptr_y, dest_y, wrap_y, 8) < 20 *
s->qscale)
2250 if (
s->mecc.sad[1](
NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 *
s->qscale)
2253 wrap_y, 8) < 20 *
s->qscale)
2256 wrap_y, 8) < 20 *
s->qscale)
2258 if (
s->mecc.sad[1](
NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 *
s->qscale)
2260 if (
s->mecc.sad[1](
NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 *
s->qscale)
2263 if (
s->mecc.sad[1](
NULL, ptr_cb + uv_dct_offset,
2264 dest_cb + uv_dct_offset,
2265 wrap_c, 8) < 20 *
s->qscale)
2267 if (
s->mecc.sad[1](
NULL, ptr_cr + uv_dct_offset,
2268 dest_cr + uv_dct_offset,
2269 wrap_c, 8) < 20 *
s->qscale)
2275 if (
s->quantizer_noise_shaping) {
2296 memcpy(orig[0],
s->block[0],
sizeof(int16_t) * 64 * mb_block_count);
2302 for (
i = 0;
i < mb_block_count;
i++) {
2305 s->block_last_index[
i] =
s->dct_quantize(
s,
s->block[
i],
i,
s->qscale, &
overflow);
2314 s->block_last_index[
i] = -1;
2316 if (
s->quantizer_noise_shaping) {
2317 for (
i = 0;
i < mb_block_count;
i++) {
2319 s->block_last_index[
i] =
2321 orig[
i],
i,
s->qscale);
2326 if (
s->luma_elim_threshold && !
s->mb_intra)
2327 for (
i = 0;
i < 4;
i++)
2329 if (
s->chroma_elim_threshold && !
s->mb_intra)
2330 for (
i = 4;
i < mb_block_count;
i++)
2334 for (
i = 0;
i < mb_block_count;
i++) {
2335 if (
s->block_last_index[
i] == -1)
2336 s->coded_score[
i] = INT_MAX / 256;
2342 s->block_last_index[4] =
2343 s->block_last_index[5] = 0;
2345 s->block[5][0] = (1024 +
s->c_dc_scale / 2) /
s->c_dc_scale;
2347 for (
i=6;
i<12;
i++) {
2348 s->block_last_index[
i] = 0;
2349 s->block[
i][0] =
s->block[4][0];
2356 for (
i = 0;
i < mb_block_count;
i++) {
2358 if (
s->block_last_index[
i] > 0) {
2359 for (j = 63; j > 0; j--) {
2360 if (
s->block[
i][
s->intra_scantable.permutated[j]])
2363 s->block_last_index[
i] = j;
2369 switch(
s->codec_id){
2372 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2376 if (CONFIG_MPEG4_ENCODER)
2382 if (CONFIG_MSMPEG4ENC)
2386 if (CONFIG_WMV2_ENCODER)
2390 if (CONFIG_H261_ENCODER)
2398 if (CONFIG_H263_ENCODER)
2401 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
2408 if (CONFIG_SPEEDHQ_ENCODER)
2431 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2434 d->mb_skip_run=
s->mb_skip_run;
2436 d->last_dc[
i] =
s->last_dc[
i];
2439 d->mv_bits=
s->mv_bits;
2440 d->i_tex_bits=
s->i_tex_bits;
2441 d->p_tex_bits=
s->p_tex_bits;
2442 d->i_count=
s->i_count;
2443 d->skip_count=
s->skip_count;
2444 d->misc_bits=
s->misc_bits;
2448 d->qscale=
s->qscale;
2449 d->dquant=
s->dquant;
2451 d->esc3_level_length=
s->esc3_level_length;
2459 memcpy(
d->mv,
s->mv, 2*4*2*
sizeof(
int));
2460 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2463 d->mb_skip_run=
s->mb_skip_run;
2465 d->last_dc[
i] =
s->last_dc[
i];
2468 d->mv_bits=
s->mv_bits;
2469 d->i_tex_bits=
s->i_tex_bits;
2470 d->p_tex_bits=
s->p_tex_bits;
2471 d->i_count=
s->i_count;
2472 d->skip_count=
s->skip_count;
2473 d->misc_bits=
s->misc_bits;
2475 d->mb_intra=
s->mb_intra;
2476 d->mb_skipped=
s->mb_skipped;
2477 d->mv_type=
s->mv_type;
2478 d->mv_dir=
s->mv_dir;
2480 if(
s->data_partitioning){
2482 d->tex_pb=
s->tex_pb;
2486 d->block_last_index[
i]=
s->block_last_index[
i];
2487 d->interlaced_dct=
s->interlaced_dct;
2488 d->qscale=
s->qscale;
2490 d->esc3_level_length=
s->esc3_level_length;
2495 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2498 uint8_t *dest_backup[3];
2502 s->block=
s->blocks[*next_block];
2503 s->pb=
pb[*next_block];
2504 if(
s->data_partitioning){
2505 s->pb2 =
pb2 [*next_block];
2506 s->tex_pb=
tex_pb[*next_block];
2510 memcpy(dest_backup,
s->dest,
sizeof(
s->dest));
2511 s->dest[0] =
s->sc.rd_scratchpad;
2512 s->dest[1] =
s->sc.rd_scratchpad + 16*
s->linesize;
2513 s->dest[2] =
s->sc.rd_scratchpad + 16*
s->linesize + 8;
2520 if(
s->data_partitioning){
2528 score *=
s->lambda2;
2533 memcpy(
s->dest, dest_backup,
sizeof(
s->dest));
2551 else if(
w==8 &&
h==8)
2568 int chroma_mb_w =
w >>
s->chroma_x_shift;
2569 int chroma_mb_h =
h >>
s->chroma_y_shift;
2571 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
2572 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
2576 return s->mecc.nsse[0](
s,
s->new_picture->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
2577 s->dest[0],
s->linesize, 16) +
2578 s->mecc.nsse[1](
s,
s->new_picture->data[1] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2579 s->dest[1],
s->uvlinesize, chroma_mb_h) +
2580 s->mecc.nsse[1](
s,
s->new_picture->data[2] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2581 s->dest[2],
s->uvlinesize, chroma_mb_h);
2583 return s->mecc.sse[0](
NULL,
s->new_picture->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
2584 s->dest[0],
s->linesize, 16) +
2585 s->mecc.sse[1](
NULL,
s->new_picture->data[1] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2586 s->dest[1],
s->uvlinesize, chroma_mb_h) +
2587 s->mecc.sse[1](
NULL,
s->new_picture->data[2] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2588 s->dest[2],
s->uvlinesize, chroma_mb_h);
2591 return sse(
s,
s->new_picture->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
2592 s->dest[0],
w,
h,
s->linesize) +
2593 sse(
s,
s->new_picture->data[1] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2594 s->dest[1],
w >>
s->chroma_x_shift,
h >>
s->chroma_y_shift,
s->uvlinesize) +
2595 sse(
s,
s->new_picture->data[2] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2596 s->dest[2],
w >>
s->chroma_x_shift,
h >>
s->chroma_y_shift,
s->uvlinesize);
2604 s->me.dia_size=
s->avctx->pre_dia_size;
2605 s->first_slice_line=1;
2606 for(
s->mb_y=
s->end_mb_y-1;
s->mb_y >=
s->start_mb_y;
s->mb_y--) {
2607 for(
s->mb_x=
s->mb_width-1;
s->mb_x >=0 ;
s->mb_x--) {
2610 s->first_slice_line=0;
2621 s->me.dia_size=
s->avctx->dia_size;
2622 s->first_slice_line=1;
2623 for(
s->mb_y=
s->start_mb_y;
s->mb_y <
s->end_mb_y;
s->mb_y++) {
2626 for(
s->mb_x=0;
s->mb_x <
s->mb_width;
s->mb_x++) {
2627 s->block_index[0]+=2;
2628 s->block_index[1]+=2;
2629 s->block_index[2]+=2;
2630 s->block_index[3]+=2;
2638 s->first_slice_line=0;
2651 const uint8_t *pix =
s->new_picture->data[0] + (yy *
s->linesize) + xx;
2653 int sum =
s->mpvencdsp.pix_sum(pix,
s->linesize);
2655 varc = (
s->mpvencdsp.pix_norm1(pix,
s->linesize) -
2656 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2658 s->mb_var [
s->mb_stride *
mb_y +
mb_x] = varc;
2659 s->mb_mean[
s->mb_stride *
mb_y +
mb_x] = (sum+128)>>8;
2660 s->me.mb_var_sum_temp += varc;
2668 if(
s->partitioned_frame){
2673 }
else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2676 }
else if (CONFIG_SPEEDHQ_ENCODER &&
s->out_format ==
FMT_SPEEDHQ) {
2688 uint8_t *ptr =
s->mb_info_ptr +
s->mb_info_size - 12;
2690 int mba =
s->mb_x +
s->mb_width * (
s->mb_y %
s->gob_index);
2691 int gobn =
s->mb_y /
s->gob_index;
2693 if (CONFIG_H263_ENCODER)
2695 bytestream_put_le32(&ptr,
offset);
2696 bytestream_put_byte(&ptr,
s->qscale);
2697 bytestream_put_byte(&ptr, gobn);
2698 bytestream_put_le16(&ptr, mba);
2699 bytestream_put_byte(&ptr, pred_x);
2700 bytestream_put_byte(&ptr, pred_y);
2702 bytestream_put_byte(&ptr, 0);
2703 bytestream_put_byte(&ptr, 0);
2711 s->mb_info_size += 12;
2712 s->prev_mb_info =
s->last_mb_info;
2724 if (!
s->mb_info_size)
2725 s->mb_info_size += 12;
2732 &&
s->slice_context_count == 1
2733 &&
s->pb.buf ==
s->avctx->internal->byte_buffer) {
2734 int lastgob_pos =
s->ptr_lastgob -
s->pb.buf;
2736 uint8_t *new_buffer =
NULL;
2737 int new_buffer_size = 0;
2739 if ((
s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2747 s->avctx->internal->byte_buffer_size + size_increase);
2751 memcpy(new_buffer,
s->avctx->internal->byte_buffer,
s->avctx->internal->byte_buffer_size);
2752 av_free(
s->avctx->internal->byte_buffer);
2753 s->avctx->internal->byte_buffer = new_buffer;
2754 s->avctx->internal->byte_buffer_size = new_buffer_size;
2756 s->ptr_lastgob =
s->pb.buf + lastgob_pos;
2766 int chr_h= 16>>
s->chroma_y_shift;
2791 s->last_dc[
i] = 128 <<
s->intra_dc_precision;
2793 s->encoding_error[
i] = 0;
2796 s->last_dc[0] = 128*8/13;
2797 s->last_dc[1] = 128*8/14;
2798 s->last_dc[2] = 128*8/14;
2801 memset(
s->last_mv, 0,
sizeof(
s->last_mv));
2805 switch(
s->codec_id){
2809 if (CONFIG_H263_ENCODER)
2813 if(CONFIG_MPEG4_ENCODER &&
s->partitioned_frame)
2820 s->first_slice_line = 1;
2821 s->ptr_lastgob =
s->pb.buf;
2822 for (mb_y_order =
s->start_mb_y; mb_y_order < s->
end_mb_y; mb_y_order++) {
2826 if (first_in_slice && mb_y_order !=
s->start_mb_y)
2828 s->last_dc[0] =
s->last_dc[1] =
s->last_dc[2] = 1024 <<
s->intra_dc_precision;
2844 int size_increase =
s->avctx->internal->byte_buffer_size/4
2852 if(
s->data_partitioning){
2866 xy=
s->mb_y*
s->mb_stride +
s->mb_x;
2872 int current_packet_size, is_gob_start;
2875 - (
s->ptr_lastgob -
s->pb.buf);
2877 is_gob_start =
s->rtp_payload_size &&
2878 current_packet_size >=
s->rtp_payload_size &&
2881 if(
s->start_mb_y ==
mb_y &&
mb_y > 0 &&
mb_x==0) is_gob_start=1;
2883 switch(
s->codec_id){
2886 if(!
s->h263_slice_structured)
2887 if(
s->mb_x ||
s->mb_y%
s->gob_index) is_gob_start=0;
2890 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
2892 if(
s->mb_skip_run) is_gob_start=0;
2895 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
2911 if (
s->error_rate &&
s->resync_mb_x +
s->resync_mb_y > 0) {
2913 int d = 100 /
s->error_rate;
2915 current_packet_size=0;
2916 s->pb.buf_ptr=
s->ptr_lastgob;
2921 switch(
s->codec_id){
2923 if (CONFIG_MPEG4_ENCODER) {
2930 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2937 if (CONFIG_H263_ENCODER) {
2946 s->misc_bits+=
bits -
s->last_bits;
2950 s->ptr_lastgob += current_packet_size;
2951 s->first_slice_line=1;
2952 s->resync_mb_x=
mb_x;
2953 s->resync_mb_y=
mb_y;
2957 if( (
s->resync_mb_x ==
s->mb_x)
2958 &&
s->resync_mb_y+1 ==
s->mb_y){
2959 s->first_slice_line=0;
2969 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2975 if(
s->data_partitioning){
2976 backup_s.pb2=
s->pb2;
2977 backup_s.tex_pb=
s->tex_pb;
2984 s->mv[0][0][0] =
s->p_mv_table[xy][0];
2985 s->mv[0][0][1] =
s->p_mv_table[xy][1];
2987 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
2994 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
2995 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
2996 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
2999 &dmin, &next_block, 0, 0);
3008 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3015 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3016 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3019 &dmin, &next_block, 0, 0);
3025 s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3026 s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3028 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3034 s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3035 s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3037 &dmin, &next_block,
s->mv[1][0][0],
s->mv[1][0][1]);
3043 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3044 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3045 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3046 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3048 &dmin, &next_block, 0, 0);
3055 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3056 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3057 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3060 &dmin, &next_block, 0, 0);
3067 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3068 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3069 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3072 &dmin, &next_block, 0, 0);
3078 for(dir=0; dir<2; dir++){
3080 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3081 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3082 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3086 &dmin, &next_block, 0, 0);
3095 &dmin, &next_block, 0, 0);
3096 if(
s->h263_pred ||
s->h263_aic){
3098 s->mbintra_table[
mb_x +
mb_y*
s->mb_stride]=1;
3106 const int last_qp= backup_s.qscale;
3110 static const int dquant_tab[4]={-1,1,-2,2};
3111 int storecoefs =
s->mb_intra &&
s->dc_val[0];
3119 s->mv[0][0][0] = best_s.
mv[0][0][0];
3120 s->mv[0][0][1] = best_s.
mv[0][0][1];
3121 s->mv[1][0][0] = best_s.
mv[1][0][0];
3122 s->mv[1][0][1] = best_s.
mv[1][0][1];
3125 for(; qpi<4; qpi++){
3126 int dquant= dquant_tab[qpi];
3128 if(qp < s->
avctx->
qmin || qp >
s->avctx->qmax)
3133 dc[
i]=
s->dc_val[0][
s->block_index[
i] ];
3134 memcpy(ac[
i],
s->ac_val[0][
s->block_index[
i]],
sizeof(int16_t)*16);
3139 &dmin, &next_block,
s->mv[mvdir][0][0],
s->mv[mvdir][0][1]);
3143 s->dc_val[0][
s->block_index[
i] ]=
dc[
i];
3144 memcpy(
s->ac_val[0][
s->block_index[
i]], ac[
i],
sizeof(int16_t)*16);
3152 int mx=
s->b_direct_mv_table[xy][0];
3153 int my=
s->b_direct_mv_table[xy][1];
3155 backup_s.dquant = 0;
3160 &dmin, &next_block, mx, my);
3163 backup_s.dquant = 0;
3168 &dmin, &next_block, 0, 0);
3173 coded |=
s->block_last_index[
i];
3176 memcpy(
s->mv, best_s.
mv,
sizeof(
s->mv));
3198 &dmin, &next_block, mx, my);
3203 s->current_picture.qscale_table[xy] = best_s.
qscale;
3209 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3212 if(
s->data_partitioning){
3215 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3216 s->pb2= backup_s.pb2;
3220 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3221 s->tex_pb= backup_s.tex_pb;
3225 if (CONFIG_H263_ENCODER &&
3230 s->hdsp.put_pixels_tab[0][0](
s->dest[0],
s->sc.rd_scratchpad ,
s->linesize ,16);
3231 s->hdsp.put_pixels_tab[1][0](
s->dest[1],
s->sc.rd_scratchpad + 16*
s->linesize ,
s->uvlinesize, 8);
3232 s->hdsp.put_pixels_tab[1][0](
s->dest[2],
s->sc.rd_scratchpad + 16*
s->linesize + 8,
s->uvlinesize, 8);
3238 int motion_x = 0, motion_y = 0;
3246 motion_x=
s->mv[0][0][0] = 0;
3247 motion_y=
s->mv[0][0][1] = 0;
3252 motion_x=
s->mv[0][0][0] =
s->p_mv_table[xy][0];
3253 motion_y=
s->mv[0][0][1] =
s->p_mv_table[xy][1];
3260 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3261 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3262 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3270 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3271 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3275 if (CONFIG_MPEG4_ENCODER) {
3278 motion_x=
s->b_direct_mv_table[xy][0];
3279 motion_y=
s->b_direct_mv_table[xy][1];
3284 if (CONFIG_MPEG4_ENCODER) {
3293 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3294 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3295 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3296 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3301 motion_x=
s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3302 motion_y=
s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3307 motion_x=
s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3308 motion_y=
s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3315 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3316 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3317 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3325 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3326 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3327 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3334 for(dir=0; dir<2; dir++){
3336 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3337 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3338 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3349 s->last_mv_dir =
s->mv_dir;
3351 if (CONFIG_H263_ENCODER &&
3360 s->p_mv_table[xy][0]=0;
3361 s->p_mv_table[xy][1]=0;
3368 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
3369 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
3371 s->encoding_error[0] +=
sse(
3372 s,
s->new_picture->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
3373 s->dest[0],
w,
h,
s->linesize);
3374 s->encoding_error[1] +=
sse(
3375 s,
s->new_picture->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3376 s->dest[1],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3377 s->encoding_error[2] +=
sse(
3378 s,
s->new_picture->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3379 s->dest[2],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3382 if(CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
3385 ff_dlog(
s->avctx,
"MB %d %d bits\n",
3391 if (CONFIG_MSMPEG4ENC &&
s->msmpeg4_version &&
s->msmpeg4_version<4 &&
s->pict_type ==
AV_PICTURE_TYPE_I)
3399 #define MERGE(field) dst->field += src->field; src->field=0
3422 for(
i=0;
i<64;
i++){
3435 if (
s->next_lambda){
3436 s->current_picture_ptr->f->quality =
3437 s->current_picture.f->quality =
s->next_lambda;
3438 if(!dry_run)
s->next_lambda= 0;
3439 }
else if (!
s->fixed_qscale) {
3441 s->current_picture_ptr->f->quality =
3442 s->current_picture.f->quality =
quality;
3443 if (
s->current_picture.f->quality < 0)
3447 if(
s->adaptive_quant){
3448 switch(
s->codec_id){
3450 if (CONFIG_MPEG4_ENCODER)
3456 if (CONFIG_H263_ENCODER)
3463 s->lambda=
s->lambda_table[0];
3466 s->lambda =
s->current_picture.f->quality;
3474 s->time =
s->current_picture_ptr->f->pts *
s->avctx->time_base.num;
3477 s->pb_time=
s->pp_time - (
s->last_non_b_time -
s->time);
3480 s->pp_time=
s->time -
s->last_non_b_time;
3481 s->last_non_b_time=
s->time;
3490 int context_count =
s->slice_context_count;
3495 s->me.mb_var_sum_temp =
3496 s->me.mc_mb_var_sum_temp = 0;
3500 if (
s->out_format ==
FMT_MPEG1 || (
s->h263_pred && !
s->msmpeg4_version))
3505 s->me.scene_change_score=0;
3510 if(
s->msmpeg4_version >= 3)
s->no_rounding=1;
3511 else s->no_rounding=0;
3514 s->no_rounding ^= 1;
3523 s->lambda=
s->last_lambda_for[
s->pict_type];
3525 s->lambda=
s->last_lambda_for[
s->last_non_b_pict_type];
3530 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
3531 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
3532 s->q_chroma_intra_matrix =
s->q_intra_matrix;
3533 s->q_chroma_intra_matrix16 =
s->q_intra_matrix16;
3537 for(
i=1;
i<context_count;
i++){
3548 s->lambda = (
s->lambda *
s->me_penalty_compensation + 128) >> 8;
3549 s->lambda2 = (
s->lambda2 * (int64_t)
s->me_penalty_compensation + 128) >> 8;
3560 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3563 if(!
s->fixed_qscale){
3565 s->avctx->execute(
s->avctx,
mb_var_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3568 for(
i=1;
i<context_count;
i++){
3571 s->mc_mb_var_sum =
s->me.mc_mb_var_sum_temp;
3572 s->mb_var_sum =
s->me. mb_var_sum_temp;
3575 if (
s->me.scene_change_score >
s->scenechange_threshold &&
3578 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3580 if(
s->msmpeg4_version >= 3)
3582 ff_dlog(
s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3583 s->mb_var_sum,
s->mc_mb_var_sum);
3624 for(dir=0; dir<2; dir++){
3630 s->b_field_mv_table[dir][
i][j], dir ?
s->b_code :
s->f_code,
type, 1);
3641 if (
s->qscale < 3 &&
s->max_qcoeff <= 128 &&
3650 if (
s->avctx->intra_matrix) {
3652 luma_matrix =
s->avctx->intra_matrix;
3654 if (
s->avctx->chroma_intra_matrix)
3655 chroma_matrix =
s->avctx->chroma_intra_matrix;
3659 int j =
s->idsp.idct_permutation[
i];
3661 s->chroma_intra_matrix[j] =
av_clip_uint8((chroma_matrix[
i] *
s->qscale) >> 3);
3664 s->y_dc_scale_table=
3666 s->chroma_intra_matrix[0] =
3669 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3671 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3675 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};
3676 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};
3677 for (
int i = 1;
i < 64;
i++) {
3683 s->y_dc_scale_table = y;
3684 s->c_dc_scale_table =
c;
3685 s->intra_matrix[0] = 13;
3686 s->chroma_intra_matrix[0] = 14;
3688 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3690 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3694 s->y_dc_scale_table=
3699 s->current_picture_ptr->f->key_frame =
3701 s->current_picture_ptr->f->pict_type =
3702 s->current_picture.f->pict_type =
s->pict_type;
3704 if (
s->current_picture.f->key_frame)
3705 s->picture_in_gop_number=0;
3707 s->mb_x =
s->mb_y = 0;
3709 switch(
s->out_format) {
3710 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
3716 if (CONFIG_SPEEDHQ_ENCODER)
3720 if (CONFIG_H261_ENCODER)
3726 else if (CONFIG_MSMPEG4ENC &&
s->msmpeg4_version)
3728 else if (CONFIG_MPEG4_ENCODER &&
s->h263_pred) {
3741 else if (CONFIG_H263_ENCODER)
3745 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3752 s->header_bits=
bits -
s->last_bits;
3754 for(
i=1;
i<context_count;
i++){
3757 s->avctx->execute(
s->avctx,
encode_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3758 for(
i=1;
i<context_count;
i++){
3759 if (
s->pb.buf_end ==
s->thread_context[
i]->pb.buf)
3768 const int intra=
s->mb_intra;
3771 s->dct_count[intra]++;
3773 for(
i=0;
i<64;
i++){
3778 s->dct_error_sum[intra][
i] +=
level;
3779 level -=
s->dct_offset[intra][
i];
3782 s->dct_error_sum[intra][
i] -=
level;
3783 level +=
s->dct_offset[intra][
i];
3792 int16_t *
block,
int n,
3796 const uint8_t *scantable;
3797 const uint8_t *perm_scantable;
3799 unsigned int threshold1, threshold2;
3811 int coeff_count[64];
3812 int qmul, qadd, start_i, last_non_zero,
i,
dc;
3813 const int esc_length=
s->ac_esc_length;
3815 uint8_t * last_length;
3821 if(
s->dct_error_sum)
3827 else mpeg2_qscale =
qscale << 1;
3831 scantable=
s->intra_scantable.scantable;
3832 perm_scantable=
s->intra_scantable.permutated;
3849 qmat = n < 4 ?
s->q_intra_matrix[
qscale] :
s->q_chroma_intra_matrix[
qscale];
3850 matrix = n < 4 ?
s->intra_matrix :
s->chroma_intra_matrix;
3854 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
3855 length =
s->intra_chroma_ac_vlc_length;
3856 last_length=
s->intra_chroma_ac_vlc_last_length;
3858 length =
s->intra_ac_vlc_length;
3859 last_length=
s->intra_ac_vlc_last_length;
3862 scantable=
s->inter_scantable.scantable;
3863 perm_scantable=
s->inter_scantable.permutated;
3866 qmat =
s->q_inter_matrix[
qscale];
3868 length =
s->inter_ac_vlc_length;
3869 last_length=
s->inter_ac_vlc_last_length;
3874 threshold2= (threshold1<<1);
3876 for(
i=63;
i>=start_i;
i--) {
3877 const int j = scantable[
i];
3880 if(((
unsigned)(
level+threshold1))>threshold2){
3886 for(
i=start_i;
i<=last_non_zero;
i++) {
3887 const int j = scantable[
i];
3892 if(((
unsigned)(
level+threshold1))>threshold2){
3915 if(last_non_zero < start_i){
3916 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3917 return last_non_zero;
3920 score_tab[start_i]= 0;
3921 survivor[0]= start_i;
3924 for(
i=start_i;
i<=last_non_zero;
i++){
3925 int level_index, j, zero_distortion;
3927 int best_score=256*256*256*120;
3931 zero_distortion= dct_coeff*dct_coeff;
3933 for(level_index=0; level_index < coeff_count[
i]; level_index++){
3942 unquant_coeff= alevel*qmul + qadd;
3944 j =
s->idsp.idct_permutation[scantable[
i]];
3945 unquant_coeff = alevel *
matrix[j] * 8;
3947 j =
s->idsp.idct_permutation[scantable[
i]];
3949 unquant_coeff = (
int)( alevel * mpeg2_qscale *
matrix[j]) >> 4;
3950 unquant_coeff = (unquant_coeff - 1) | 1;
3952 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int)
matrix[j])) >> 5;
3953 unquant_coeff = (unquant_coeff - 1) | 1;
3958 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3960 if((
level&(~127)) == 0){
3961 for(j=survivor_count-1; j>=0; j--){
3962 int run=
i - survivor[j];
3964 score += score_tab[
i-
run];
3966 if(score < best_score){
3969 level_tab[
i+1]=
level-64;
3974 for(j=survivor_count-1; j>=0; j--){
3975 int run=
i - survivor[j];
3977 score += score_tab[
i-
run];
3978 if(score < last_score){
3981 last_level=
level-64;
3987 distortion += esc_length*
lambda;
3988 for(j=survivor_count-1; j>=0; j--){
3989 int run=
i - survivor[j];
3990 int score= distortion + score_tab[
i-
run];
3992 if(score < best_score){
3995 level_tab[
i+1]=
level-64;
4000 for(j=survivor_count-1; j>=0; j--){
4001 int run=
i - survivor[j];
4002 int score= distortion + score_tab[
i-
run];
4003 if(score < last_score){
4006 last_level=
level-64;
4014 score_tab[
i+1]= best_score;
4017 if(last_non_zero <= 27){
4018 for(; survivor_count; survivor_count--){
4019 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4023 for(; survivor_count; survivor_count--){
4024 if(score_tab[ survivor[survivor_count-1] ] <= best_score +
lambda)
4029 survivor[ survivor_count++ ]=
i+1;
4033 last_score= 256*256*256*120;
4034 for(
i= survivor[0];
i<=last_non_zero + 1;
i++){
4035 int score= score_tab[
i];
4039 if(score < last_score){
4042 last_level= level_tab[
i];
4043 last_run= run_tab[
i];
4048 s->coded_score[n] = last_score;
4051 last_non_zero= last_i - 1;
4052 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4054 if(last_non_zero < start_i)
4055 return last_non_zero;
4057 if(last_non_zero == 0 && start_i == 0){
4059 int best_score=
dc *
dc;
4061 for(
i=0;
i<coeff_count[0];
i++){
4064 int unquant_coeff, score, distortion;
4067 unquant_coeff= (alevel*qmul + qadd)>>3;
4069 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int)
matrix[0])) >> 5;
4070 unquant_coeff = (unquant_coeff - 1) | 1;
4072 unquant_coeff = (unquant_coeff + 4) >> 3;
4073 unquant_coeff<<= 3 + 3;
4075 distortion= (unquant_coeff -
dc) * (unquant_coeff -
dc);
4078 else score= distortion + esc_length*
lambda;
4080 if(score < best_score){
4082 best_level=
level - 64;
4085 block[0]= best_level;
4086 s->coded_score[n] = best_score -
dc*
dc;
4087 if(best_level == 0)
return -1;
4088 else return last_non_zero;
4094 block[ perm_scantable[last_non_zero] ]= last_level;
4097 for(;
i>start_i;
i -= run_tab[
i] + 1){
4098 block[ perm_scantable[
i-1] ]= level_tab[
i];
4101 return last_non_zero;
4116 if(
i==0)
s*= sqrt(0.5);
4117 if(j==0)
s*= sqrt(0.5);
4130 const uint8_t *scantable;
4131 const uint8_t *perm_scantable;
4137 int qmul, qadd, start_i, last_non_zero,
i,
dc;
4139 uint8_t * last_length;
4141 int rle_index,
run, q = 1, sum;
4143 if(
basis[0][0] == 0)
4149 scantable=
s->intra_scantable.scantable;
4150 perm_scantable=
s->intra_scantable.permutated;
4168 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
4169 length =
s->intra_chroma_ac_vlc_length;
4170 last_length=
s->intra_chroma_ac_vlc_last_length;
4172 length =
s->intra_ac_vlc_length;
4173 last_length=
s->intra_ac_vlc_last_length;
4176 scantable=
s->inter_scantable.scantable;
4177 perm_scantable=
s->inter_scantable.permutated;
4180 length =
s->inter_ac_vlc_length;
4181 last_length=
s->inter_ac_vlc_last_length;
4183 last_non_zero =
s->block_last_index[n];
4186 for(
i=0;
i<64;
i++){
4191 for(
i=0;
i<64;
i++){
4197 w= 15 + (48*qns*one +
w/2)/
w;
4210 for(
i=start_i;
i<=last_non_zero;
i++){
4211 int j= perm_scantable[
i];
4218 run_tab[rle_index++]=
run;
4228 int best_score =
s->mpvencdsp.try_8x8basis(rem,
weight,
basis[0], 0);
4231 int run2, best_unquant_change=0, analyze_gradient;
4232 analyze_gradient = last_non_zero > 2 ||
s->quantizer_noise_shaping >= 3;
4234 if(analyze_gradient){
4235 for(
i=0;
i<64;
i++){
4245 int change, old_coeff;