216
218
s->me.mv_penalty = default_mv_penalty;
217
219
s->fcode_tab = default_fcode_tab;
221
s->input_picture_number = 0;
222
s->picture_in_gop_number = 0;
220
225
av_cold int ff_dct_encode_init(MpegEncContext *s) {
222
227
ff_dct_encode_init_x86(s);
229
ff_h263dsp_init(&s->h263dsp);
224
230
if (!s->dct_quantize)
225
231
s->dct_quantize = ff_dct_quantize_c;
226
232
if (!s->denoise_dct)
253
case AV_CODEC_ID_LJPEG:
254
if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
255
avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
256
avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
257
avctx->pix_fmt != AV_PIX_FMT_BGR0 &&
258
avctx->pix_fmt != AV_PIX_FMT_BGRA &&
259
avctx->pix_fmt != AV_PIX_FMT_BGR24 &&
260
((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
261
avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
262
avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
263
avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
264
av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
268
258
case AV_CODEC_ID_MJPEG:
269
259
case AV_CODEC_ID_AMV:
270
260
if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
315
305
s->avctx = avctx;
316
306
s->flags = avctx->flags;
317
307
s->flags2 = avctx->flags2;
308
if (avctx->max_b_frames > MAX_B_FRAMES) {
309
av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
310
"is %d.\n", MAX_B_FRAMES);
311
avctx->max_b_frames = MAX_B_FRAMES;
318
313
s->max_b_frames = avctx->max_b_frames;
319
314
s->codec_id = avctx->codec->id;
320
315
s->strict_std_compliance = avctx->strict_std_compliance;
452
447
av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
450
if (s->max_b_frames < 0) {
451
av_log(avctx, AV_LOG_ERROR,
452
"max b frames must be 0 or positive for mpegvideo based encoders\n");
456
456
if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
457
457
s->codec_id == AV_CODEC_ID_H263 ||
642
642
av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
644
avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
646
644
if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
647
645
s->avctx->time_base.den > (1 << 16) - 1) {
648
646
av_log(avctx, AV_LOG_ERROR,
666
664
avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
669
case AV_CODEC_ID_LJPEG:
670
667
case AV_CODEC_ID_MJPEG:
671
668
case AV_CODEC_ID_AMV:
672
669
s->out_format = FMT_MJPEG;
673
670
s->intra_only = 1; /* force intra only for jpeg */
674
if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
675
(avctx->pix_fmt == AV_PIX_FMT_BGR0
676
|| s->avctx->pix_fmt == AV_PIX_FMT_BGRA
677
|| s->avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
678
s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
679
s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
680
s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
681
} else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
682
s->mjpeg_vsample[0] = s->mjpeg_vsample[1] = s->mjpeg_vsample[2] = 2;
683
s->mjpeg_hsample[0] = s->mjpeg_hsample[1] = s->mjpeg_hsample[2] = 1;
685
s->mjpeg_vsample[0] = 2;
686
s->mjpeg_vsample[1] = 2 >> chroma_v_shift;
687
s->mjpeg_vsample[2] = 2 >> chroma_v_shift;
688
s->mjpeg_hsample[0] = 2;
689
s->mjpeg_hsample[1] = 2 >> chroma_h_shift;
690
s->mjpeg_hsample[2] = 2 >> chroma_h_shift;
692
if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
671
if (!CONFIG_MJPEG_ENCODER ||
693
672
ff_mjpeg_encode_init(s) < 0)
695
674
avctx->delay = 0;
821
800
if (ff_MPV_common_init(s) < 0)
803
s->avctx->coded_frame = &s->current_picture.f;
805
if (s->msmpeg4_version) {
806
FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
807
2 * 2 * (MAX_LEVEL + 1) *
808
(MAX_RUN + 1) * 2 * sizeof(int), fail);
810
FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
812
FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix, 64 * 32 * sizeof(int), fail);
813
FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix, 64 * 32 * sizeof(int), fail);
814
FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix, 64 * 32 * sizeof(int), fail);
815
FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
816
FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
817
FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
818
FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
819
MAX_PICTURE_COUNT * sizeof(Picture *), fail);
820
FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
821
MAX_PICTURE_COUNT * sizeof(Picture *), fail);
823
if (s->avctx->noise_reduction) {
824
FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
825
2 * 64 * sizeof(uint16_t), fail);
824
828
ff_dct_encode_init(s);
826
830
if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
876
880
if (ff_rate_control_init(s) < 0)
883
#if FF_API_ERROR_RATE
884
FF_DISABLE_DEPRECATION_WARNINGS
885
if (avctx->error_rate)
886
s->error_rate = avctx->error_rate;
887
FF_ENABLE_DEPRECATION_WARNINGS;
890
if (avctx->b_frame_strategy == 2) {
891
for (i = 0; i < s->max_b_frames + 2; i++) {
892
s->tmp_frames[i] = av_frame_alloc();
893
if (!s->tmp_frames[i])
894
return AVERROR(ENOMEM);
896
s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
897
s->tmp_frames[i]->width = s->width >> avctx->brd_scale;
898
s->tmp_frames[i]->height = s->height >> avctx->brd_scale;
900
ret = av_frame_get_buffer(s->tmp_frames[i], 32);
908
ff_MPV_encode_end(avctx);
909
return AVERROR_UNKNOWN;
882
912
av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
884
914
MpegEncContext *s = avctx->priv_data;
886
917
ff_rate_control_uninit(s);
888
919
ff_MPV_common_end(s);
889
if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
920
if (CONFIG_MJPEG_ENCODER &&
890
921
s->out_format == FMT_MJPEG)
891
922
ff_mjpeg_encode_close(s);
893
924
av_freep(&avctx->extradata);
926
for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
927
av_frame_free(&s->tmp_frames[i]);
929
ff_free_picture_tables(&s->new_picture);
930
ff_mpeg_unref_picture(s, &s->new_picture);
932
av_freep(&s->avctx->stats_out);
933
av_freep(&s->ac_stats);
935
if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
936
if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
937
s->q_chroma_intra_matrix= NULL;
938
s->q_chroma_intra_matrix16= NULL;
939
av_freep(&s->q_intra_matrix);
940
av_freep(&s->q_inter_matrix);
941
av_freep(&s->q_intra_matrix16);
942
av_freep(&s->q_inter_matrix16);
943
av_freep(&s->input_picture);
944
av_freep(&s->reordered_input_picture);
945
av_freep(&s->dct_offset);
984
1036
if (pic_arg->linesize[2] != s->uvlinesize)
987
av_dlog(s->avctx, "%d %d %d %d\n", pic_arg->linesize[0],
988
pic_arg->linesize[1], (int) s->linesize, (int) s->uvlinesize);
1039
av_dlog(s->avctx, "%d %d %td %td\n", pic_arg->linesize[0],
1040
pic_arg->linesize[1], s->linesize, s->uvlinesize);
991
1043
i = ff_find_unused_picture(s, 1);
1093
1145
uint8_t *rptr = ref->f.data[plane] + 8 * (x + y * stride);
1094
1146
int v = s->dsp.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1096
switch (s->avctx->frame_skip_exp) {
1148
switch (FFABS(s->avctx->frame_skip_exp)) {
1097
1149
case 0: score = FFMAX(score, v); break;
1098
1150
case 1: score += FFABS(v); break;
1099
case 2: score += v * v; break;
1100
case 3: score64 += FFABS(v * v * (int64_t)v); break;
1101
case 4: score64 += v * v * (int64_t)(v * v); break;
1151
case 2: score64 += v * (int64_t)v; break;
1152
case 3: score64 += FFABS(v * (int64_t)v * v); break;
1153
case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v); break;
1108
1161
score64 = score;
1162
if (s->avctx->frame_skip_exp < 0)
1163
score64 = pow(score64 / (double)(s->mb_width * s->mb_height),
1164
-1.0/s->avctx->frame_skip_exp);
1110
1166
if (score64 < s->avctx->frame_skip_threshold)
1134
1190
AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
1135
1191
AVCodecContext *c = avcodec_alloc_context3(NULL);
1136
AVFrame input[FF_MAX_B_FRAMES + 2];
1137
1192
const int scale = s->avctx->brd_scale;
1138
1193
int i, j, out_size, p_lambda, b_lambda, lambda2;
1139
1194
int64_t best_rd = INT64_MAX;
1170
1225
for (i = 0; i < s->max_b_frames + 2; i++) {
1171
int ysize = c->width * c->height;
1172
int csize = (c->width / 2) * (c->height / 2);
1173
1226
Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1174
1227
s->next_picture_ptr;
1176
avcodec_get_frame_defaults(&input[i]);
1177
input[i].data[0] = av_malloc(ysize + 2 * csize);
1178
input[i].data[1] = input[i].data[0] + ysize;
1179
input[i].data[2] = input[i].data[1] + csize;
1180
input[i].linesize[0] = c->width;
1181
input[i].linesize[1] =
1182
input[i].linesize[2] = c->width / 2;
1184
1229
if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1185
1230
pre_input = *pre_input_ptr;
1190
1235
pre_input.f.data[2] += INPLACE_OFFSET;
1193
s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],
1238
s->dsp.shrink[scale](s->tmp_frames[i]->data[0], s->tmp_frames[i]->linesize[0],
1194
1239
pre_input.f.data[0], pre_input.f.linesize[0],
1195
1240
c->width, c->height);
1196
s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],
1241
s->dsp.shrink[scale](s->tmp_frames[i]->data[1], s->tmp_frames[i]->linesize[1],
1197
1242
pre_input.f.data[1], pre_input.f.linesize[1],
1198
1243
c->width >> 1, c->height >> 1);
1199
s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],
1244
s->dsp.shrink[scale](s->tmp_frames[i]->data[2], s->tmp_frames[i]->linesize[2],
1200
1245
pre_input.f.data[2], pre_input.f.linesize[2],
1201
1246
c->width >> 1, c->height >> 1);
1211
1256
c->error[0] = c->error[1] = c->error[2] = 0;
1213
input[0].pict_type = AV_PICTURE_TYPE_I;
1214
input[0].quality = 1 * FF_QP2LAMBDA;
1258
s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1259
s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1216
out_size = encode_frame(c, &input[0]);
1261
out_size = encode_frame(c, s->tmp_frames[0]);
1218
1263
//rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1220
1265
for (i = 0; i < s->max_b_frames + 1; i++) {
1221
1266
int is_p = i % (j + 1) == j || i == s->max_b_frames;
1223
input[i + 1].pict_type = is_p ?
1268
s->tmp_frames[i + 1]->pict_type = is_p ?
1224
1269
AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1225
input[i + 1].quality = is_p ? p_lambda : b_lambda;
1270
s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1227
out_size = encode_frame(c, &input[i + 1]);
1272
out_size = encode_frame(c, s->tmp_frames[i + 1]);
1229
1274
rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1264
1305
/* set next picture type & ordering */
1265
1306
if (s->reordered_input_picture[0] == NULL && s->input_picture[0]) {
1307
if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
1308
if (s->picture_in_gop_number < s->gop_size &&
1309
s->next_picture_ptr &&
1310
skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1311
// FIXME check that te gop check above is +-1 correct
1312
av_frame_unref(&s->input_picture[0]->f);
1314
ff_vbv_update(s, 0);
1266
1320
if (/*s->picture_in_gop_number >= s->gop_size ||*/
1267
1321
s->next_picture_ptr == NULL || s->intra_only) {
1268
1322
s->reordered_input_picture[0] = s->input_picture[0];
1275
if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
1276
if (s->picture_in_gop_number < s->gop_size &&
1277
skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1278
// FIXME check that te gop check above is +-1 correct
1279
av_frame_unref(&s->input_picture[0]->f);
1282
ff_vbv_update(s, 0);
1288
1329
if (s->flags & CODEC_FLAG_PASS2) {
1289
1330
for (i = 0; i < s->max_b_frames + 1; i++) {
1290
1331
int pict_num = s->input_picture[0]->f.display_picture_number + i;
1475
static void frame_end(MpegEncContext *s)
1477
if (s->unrestricted_mv &&
1478
s->current_picture.reference &&
1480
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1481
int hshift = desc->log2_chroma_w;
1482
int vshift = desc->log2_chroma_h;
1483
s->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
1484
s->h_edge_pos, s->v_edge_pos,
1485
EDGE_WIDTH, EDGE_WIDTH,
1486
EDGE_TOP | EDGE_BOTTOM);
1487
s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
1488
s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1489
EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1490
EDGE_TOP | EDGE_BOTTOM);
1491
s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
1492
s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1493
EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1494
EDGE_TOP | EDGE_BOTTOM);
1499
s->last_pict_type = s->pict_type;
1500
s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
1501
if (s->pict_type!= AV_PICTURE_TYPE_B)
1502
s->last_non_b_pict_type = s->pict_type;
1504
s->avctx->coded_frame = &s->current_picture_ptr->f;
1434
1508
int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1435
1509
AVFrame *pic_arg, int *got_packet)
1486
1560
avctx->p_count = s->mb_num - s->i_count - s->skip_count;
1487
1561
avctx->skip_count = s->skip_count;
1489
ff_MPV_frame_end(s);
1491
1565
if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1492
ff_mjpeg_encode_picture_trailer(s);
1566
ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
1494
1568
if (avctx->rc_buffer_size) {
1495
1569
RateControlContext *rcc = &s->rc_context;
1815
1889
ptr_cr = s->new_picture.f.data[2] +
1816
1890
(mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1818
if((mb_x*16+16 > s->width || mb_y*16+16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
1892
if((mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
1819
1893
uint8_t *ebuf = s->edge_emu_buffer + 32;
1820
1894
int cw = (s->width + s->chroma_x_shift) >> s->chroma_x_shift;
1821
1895
int ch = (s->height + s->chroma_y_shift) >> s->chroma_y_shift;
1822
s->vdsp.emulated_edge_mc(ebuf, wrap_y, ptr_y, wrap_y, 16, 16, mb_x * 16,
1823
mb_y * 16, s->width, s->height);
1896
s->vdsp.emulated_edge_mc(ebuf, ptr_y,
1898
16, 16, mb_x * 16, mb_y * 16,
1899
s->width, s->height);
1825
s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, wrap_c, ptr_cb, wrap_c, mb_block_width,
1826
mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1901
s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb,
1903
mb_block_width, mb_block_height,
1904
mb_x * mb_block_width, mb_y * mb_block_height,
1828
1906
ptr_cb = ebuf + 18 * wrap_y;
1829
s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 16, wrap_c, ptr_cr, wrap_c, mb_block_width,
1830
mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
1907
s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 16, ptr_cr,
1909
mb_block_width, mb_block_height,
1910
mb_x * mb_block_width, mb_y * mb_block_height,
1832
1912
ptr_cr = ebuf + 18 * wrap_y + 16;
2575
2656
av_assert2((put_bits_count(&s->pb)&7) == 0);
2576
2657
current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2578
if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
2659
if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
2579
2660
int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2580
int d= 100 / s->avctx->error_rate;
2661
int d = 100 / s->error_rate;
2581
2662
if(r % d == 0){
2582
2663
current_packet_size=0;
2583
2664
s->pb.buf_ptr= s->ptr_lastgob;
3604
if(s->out_format == FMT_H263){
3686
if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
3605
3687
for(j=survivor_count-1; j>=0; j--){
3606
3688
int run= i - survivor[j];
3607
3689
int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3693
3775
int alevel= FFABS(level);
3694
3776
int unquant_coeff, score, distortion;
3696
if(s->out_format == FMT_H263){
3778
if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
3697
3779
unquant_coeff= (alevel*qmul + qadd)>>3;
3699
3781
unquant_coeff = ((( alevel << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;