History

avcodec/mpegvideo: Move sequence-level properties to MPVMainEncContext

Andreas Rheinhardt committed 1 year ago

Commit b7106981 parent d9dea706 files · +71 −68Browse files

Unified · Split

6 files
  1. libavcodec/mpeg12enc.c +8 −6
  2. libavcodec/mpeg12enc.h +2 −2
  3. libavcodec/mpegvideo.h +0 −15
  4. libavcodec/mpegvideo_enc.c +43 −43
  5. libavcodec/mpegvideoenc.h +16 −0
  6. libavcodec/ratecontrol.c +2 −2
libavcodec/mpeg12enc.c+8−6
@@ -144,9 +144,9 @@ static void put_header(MpegEncContext *s, uint32_t header) · 143 unmodified lines
144144 }
145145
146146 /* put sequence header if needed */
147−static void mpeg1_encode_sequence_header(MpegEncContext *s)
147+static void mpeg1_encode_sequence_header(MPEG12EncContext *mpeg12)
148148 {
149− MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)s;
149+ MpegEncContext *const s = &mpeg12->mpeg.s;
150150 unsigned int vbv_buffer_size, fps, v;
151151 int constraint_parameter_flag;
152152 AVRational framerate = ff_mpeg12_frame_rate_tab[mpeg12->frame_rate_index];
@@ -299,7 +299,7 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s) · 146 unmodified lines
299299 put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
300300 put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
301301 put_bits(&s->pb, 1, !!(s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) ||
302− s->intra_only || !mpeg12->gop_picture_number);
302+ mpeg12->mpeg.intra_only || !mpeg12->gop_picture_number);
303303 put_bits(&s->pb, 1, 0); // broken link
304304 }
305305
@@ -333,11 +333,13 @@ void ff_mpeg1_encode_slice_header(MpegEncContext *s) · 27 unmodified lines
333333 put_bits(&s->pb, 1, 0);
334334 }
335335
336−void ff_mpeg1_encode_picture_header(MpegEncContext *s)
336+void ff_mpeg1_encode_picture_header(MPVMainEncContext *const m)
337337 {
338− MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)s;
338+ MPEG12EncContext *const mpeg12 = (MPEG12EncContext*)m;
339+ MpegEncContext *const s = &m->s;
339340 const AVFrameSideData *side_data;
340− mpeg1_encode_sequence_header(s);
341+
342+ mpeg1_encode_sequence_header(mpeg12);
341343
342344 /* MPEG-1 picture header */
343345 put_header(s, PICTURE_START_CODE);
libavcodec/mpeg12enc.h+2−2
@@ -24,10 +24,10 @@ · 23 unmodified lines
2424
2525 #include <stdint.h>
2626
27−#include "mpegvideo.h"
27+#include "mpegvideoenc.h"
2828 #include "mpegvideodata.h"
2929
30−void ff_mpeg1_encode_picture_header(MpegEncContext *s);
30+void ff_mpeg1_encode_picture_header(MPVMainEncContext *m);
3131 void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64],
3232 int motion_x, int motion_y);
3333 void ff_mpeg1_encode_slice_header(MpegEncContext *s);
libavcodec/mpegvideo.h+0−15
@@ -91,8 +91,6 @@ typedef struct MpegEncContext { · 90 unmodified lines
9191 void *private_ctx;
9292 /* the following parameters must be initialized before encoding */
9393 int width, height;///< picture size. must be a multiple of 16
94− int gop_size;
95− int intra_only; ///< if true, only intra pictures are generated
9694 int64_t bit_rate; ///< wanted bit rate
9795 enum OutputFormat out_format; ///< output format
9896 int h263_pred; ///< use MPEG-4/H.263 ac/dc predictions
@@ -114,10 +112,7 @@ typedef struct MpegEncContext { · 15 unmodified lines
114112
115113 /* sequence parameters */
116114 int context_initialized;
117− int input_picture_number; ///< used to set pic->display_picture_number, should not be used for/by anything else
118− int coded_picture_number; ///< used to set pic->coded_picture_number, should not be used for/by anything else
119115 int picture_number; //FIXME remove, unclear definition
120− int picture_in_gop_number; ///< 0-> first pic in gop, ...
121116 int mb_width, mb_height; ///< number of MBs horizontally & vertically
122117 int mb_stride; ///< mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11
123118 int b8_stride; ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
@@ -131,16 +126,6 @@ typedef struct MpegEncContext { · 7 unmodified lines
131126
132127 BufferPoolContext buffer_pools;
133128
134− int64_t user_specified_pts; ///< last non-zero pts from AVFrame which was passed into avcodec_send_frame()
135− /**
136− * pts difference between the first and second input frame, used for
137− * calculating dts of the first frame when there's a delay */
138− int64_t dts_delta;
139− /**
140− * reordered pts to be used as dts for the next output frame when there's
141− * a delay */
142− int64_t reordered_pts;
143−
144129 /** bit output */
145130 PutBitContext pb;
146131
libavcodec/mpegvideo_enc.c+43−43
@@ -291,9 +291,6 @@ static av_cold void mpv_encode_defaults(MpegEncContext *s) · 290 unmodified lines
291291 s->y_dc_scale_table =
292292 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
293293 }
294−
295− s->input_picture_number = 0;
296− s->picture_in_gop_number = 0;
297294 }
298295
299296 av_cold void ff_dct_encode_init(MpegEncContext *s)
@@ -354,9 +351,10 @@ static av_cold int me_cmp_init(MPVMainEncContext *const m, AVCodecContext *avctx · 54 unmodified lines
354351 }
355352
356353 #define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
357−static av_cold int init_matrices(MpegEncContext *s, AVCodecContext *avctx)
354+static av_cold int init_matrices(MPVMainEncContext *const m, AVCodecContext *avctx)
358355 {
359− const int nb_matrices = 1 + (s->out_format == FMT_MJPEG) + !s->intra_only;
356+ MpegEncContext *const s = &m->s;
357+ const int nb_matrices = 1 + (s->out_format == FMT_MJPEG) + !m->intra_only;
360358 const uint16_t *intra_matrix, *inter_matrix;
361359 int ret;
362360
@@ -368,14 +366,14 @@ static av_cold int init_matrices(MpegEncContext *s, AVCodecContext *avctx) · 5 unmodified lines
368366 s->q_chroma_intra_matrix = s->q_intra_matrix + 32;
369367 s->q_chroma_intra_matrix16 = s->q_intra_matrix16 + 32;
370368 // No need to set q_inter_matrix
371− av_assert1(s->intra_only);
369+ av_assert1(m->intra_only);
372370 // intra_matrix, chroma_intra_matrix will be set later for MJPEG.
373371 return 0;
374372 } else {
375373 s->q_chroma_intra_matrix = s->q_intra_matrix;
376374 s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
377375 }
378− if (!s->intra_only) {
376+ if (!m->intra_only) {
379377 s->q_inter_matrix = s->q_intra_matrix + 32;
380378 s->q_inter_matrix16 = s->q_intra_matrix16 + 32;
381379 }
@@ -460,7 +458,7 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 78 unmodified lines
460458 avctx->gop_size, 600);
461459 avctx->gop_size = 600;
462460 }
463− s->gop_size = avctx->gop_size;
461+ m->gop_size = avctx->gop_size;
464462 s->avctx = avctx;
465463 if (avctx->max_b_frames > MPVENC_MAX_B_FRAMES) {
466464 av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
@@ -499,13 +497,13 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 32 unmodified lines
499497 av_log(avctx, AV_LOG_ERROR, "intra dc precision too large\n");
500498 return AVERROR(EINVAL);
501499 }
502− s->user_specified_pts = AV_NOPTS_VALUE;
500+ m->user_specified_pts = AV_NOPTS_VALUE;
503501
504− if (s->gop_size <= 1) {
505− s->intra_only = 1;
506− s->gop_size = 12;
502+ if (m->gop_size <= 1) {
503+ m->intra_only = 1;
504+ m->gop_size = 12;
507505 } else {
508− s->intra_only = 0;
506+ m->intra_only = 0;
509507 }
510508
511509 /* Fixed QSCALE */
@@ -753,14 +751,14 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 241 unmodified lines
753751 case AV_CODEC_ID_MJPEG:
754752 case AV_CODEC_ID_AMV:
755753 s->out_format = FMT_MJPEG;
756− s->intra_only = 1; /* force intra only for jpeg */
754+ m->intra_only = 1; /* force intra only for jpeg */
757755 avctx->delay = 0;
758756 s->low_delay = 1;
759757 break;
760758 #endif
761759 case AV_CODEC_ID_SPEEDHQ:
762760 s->out_format = FMT_SPEEDHQ;
763− s->intra_only = 1; /* force intra only for SHQ */
761+ m->intra_only = 1; /* force intra only for SHQ */
764762 avctx->delay = 0;
765763 s->low_delay = 1;
766764 break;
@@ -885,7 +883,7 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 118 unmodified lines
885883 s->frame_reconstruction_bitfield = (1 << AV_PICTURE_TYPE_I) |
886884 (1 << AV_PICTURE_TYPE_P) |
887885 (1 << AV_PICTURE_TYPE_B);
888− } else if (!s->intra_only) {
886+ } else if (!m->intra_only) {
889887 s->frame_reconstruction_bitfield = (1 << AV_PICTURE_TYPE_I) |
890888 (1 << AV_PICTURE_TYPE_P);
891889 } else {
@@ -916,7 +914,7 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 24 unmodified lines
916914 !(s->picture_pool = ff_mpv_alloc_pic_pool(0)))
917915 return AVERROR(ENOMEM);
918916
919− ret = init_matrices(s, avctx);
917+ ret = init_matrices(m, avctx);
920918 if (ret < 0)
921919 return ret;
922920
@@ -1259,11 +1257,11 @@ static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg · 336 unmodified lines
12591257
12601258 if (pic_arg) {
12611259 pts = pic_arg->pts;
1262− display_picture_number = s->input_picture_number++;
1260+ display_picture_number = m->input_picture_number++;
12631261
12641262 if (pts != AV_NOPTS_VALUE) {
1265− if (s->user_specified_pts != AV_NOPTS_VALUE) {
1266− int64_t last = s->user_specified_pts;
1263+ if (m->user_specified_pts != AV_NOPTS_VALUE) {
1264+ int64_t last = m->user_specified_pts;
12671265
12681266 if (pts <= last) {
12691267 av_log(s->avctx, AV_LOG_ERROR,
@@ -1273,13 +1271,13 @@ static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg · 3 unmodified lines
12731271 }
12741272
12751273 if (!s->low_delay && display_picture_number == 1)
1276− s->dts_delta = pts - last;
1274+ m->dts_delta = pts - last;
12771275 }
1278− s->user_specified_pts = pts;
1276+ m->user_specified_pts = pts;
12791277 } else {
1280− if (s->user_specified_pts != AV_NOPTS_VALUE) {
1281− s->user_specified_pts =
1282− pts = s->user_specified_pts + 1;
1278+ if (m->user_specified_pts != AV_NOPTS_VALUE) {
1279+ m->user_specified_pts =
1280+ pts = m->user_specified_pts + 1;
12831281 av_log(s->avctx, AV_LOG_INFO,
12841282 "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
12851283 pts);
@@ -1612,7 +1610,7 @@ static int set_bframe_chain_length(MPVMainEncContext *const m) · 326 unmodified lines
16121610
16131611 /* set next picture type & ordering */
16141612 if (m->frame_skip_threshold || m->frame_skip_factor) {
1615− if (s->picture_in_gop_number < s->gop_size &&
1613+ if (m->picture_in_gop_number < m->gop_size &&
16161614 s->next_pic.ptr &&
16171615 skip_check(m, s->input_picture[0], s->next_pic.ptr)) {
16181616 // FIXME check that the gop check above is +-1 correct
@@ -1624,13 +1622,13 @@ static int set_bframe_chain_length(MPVMainEncContext *const m) · 5 unmodified lines
16241622 }
16251623 }
16261624
1627− if (/*s->picture_in_gop_number >= s->gop_size ||*/
1628− !s->next_pic.ptr || s->intra_only) {
1625+ if (/* m->picture_in_gop_number >= m->gop_size || */
1626+ !s->next_pic.ptr || m->intra_only) {
16291627 s->reordered_input_picture[0] = s->input_picture[0];
16301628 s->input_picture[0] = NULL;
16311629 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
16321630 s->reordered_input_picture[0]->coded_picture_number =
1633− s->coded_picture_number++;
1631+ m->coded_picture_number++;
16341632 } else {
16351633 int b_frames = 0;
16361634
@@ -1700,10 +1698,10 @@ static int set_bframe_chain_length(MPVMainEncContext *const m) · 63 unmodified lines
17001698 "warning, too many B-frames in a row\n");
17011699 }
17021700
1703− if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1701+ if (m->picture_in_gop_number + b_frames >= m->gop_size) {
17041702 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1705− s->gop_size > s->picture_in_gop_number) {
1706− b_frames = s->gop_size - s->picture_in_gop_number - 1;
1703+ m->gop_size > m->picture_in_gop_number) {
1704+ b_frames = m->gop_size - m->picture_in_gop_number - 1;
17071705 } else {
17081706 if (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
17091707 b_frames = 0;
@@ -1720,14 +1718,14 @@ static int set_bframe_chain_length(MPVMainEncContext *const m) · 10 unmodified lines
17201718 if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
17211719 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
17221720 s->reordered_input_picture[0]->coded_picture_number =
1723− s->coded_picture_number++;
1721+ m->coded_picture_number++;
17241722 for (int i = 0; i < b_frames; i++) {
17251723 s->reordered_input_picture[i + 1] = s->input_picture[i];
17261724 s->input_picture[i] = NULL;
17271725 s->reordered_input_picture[i + 1]->f->pict_type =
17281726 AV_PICTURE_TYPE_B;
17291727 s->reordered_input_picture[i + 1]->coded_picture_number =
1730− s->coded_picture_number++;
1728+ m->coded_picture_number++;
17311729 }
17321730 }
17331731
@@ -1794,11 +1792,13 @@ fail: · 60 unmodified lines
17941792 return ret;
17951793 }
17961794
1797−static void frame_end(MpegEncContext *s)
1795+static void frame_end(MPVMainEncContext *const m)
17981796 {
1797+ MpegEncContext *const s = &m->s;
1798+
17991799 if (s->unrestricted_mv &&
18001800 s->cur_pic.reference &&
1801− !s->intra_only) {
1801+ !m->intra_only) {
18021802 int hshift = s->chroma_x_shift;
18031803 int vshift = s->chroma_y_shift;
18041804 s->mpvencdsp.draw_edges(s->cur_pic.data[0],
@@ -1878,7 +1878,7 @@ int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, · 73 unmodified lines
18781878
18791879 s->vbv_ignore_qmax = 0;
18801880
1881− s->picture_in_gop_number++;
1881+ m->picture_in_gop_number++;
18821882
18831883 ret = load_input_picture(m, pic_arg);
18841884 if (ret < 0)
@@ -1923,7 +1923,7 @@ vbv_retry: · 38 unmodified lines
19231923 if (ret < 0)
19241924 return -1;
19251925
1926− frame_end(s);
1926+ frame_end(m);
19271927
19281928 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) && s->out_format == FMT_MJPEG)
19291929 ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
@@ -2066,10 +2066,10 @@ vbv_retry: · 136 unmodified lines
20662066 pkt->duration = s->cur_pic.ptr->f->duration;
20672067 if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
20682068 if (!s->cur_pic.ptr->coded_picture_number)
2069− pkt->dts = pkt->pts - s->dts_delta;
2069+ pkt->dts = pkt->pts - m->dts_delta;
20702070 else
2071− pkt->dts = s->reordered_pts;
2072− s->reordered_pts = pkt->pts;
2071+ pkt->dts = m->reordered_pts;
2072+ m->reordered_pts = pkt->pts;
20732073 } else
20742074 pkt->dts = pkt->pts;
20752075
@@ -3882,7 +3882,7 @@ static int encode_picture(MPVMainEncContext *const m, const AVPacket *pkt) · 1806 unmodified lines
38823882 s->cur_pic.ptr->f->pict_type = s->pict_type;
38833883
38843884 if (s->cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)
3885− s->picture_in_gop_number=0;
3885+ m->picture_in_gop_number = 0;
38863886
38873887 s->mb_x = s->mb_y = 0;
38883888 s->last_bits= put_bits_count(&s->pb);
@@ -3925,7 +3925,7 @@ static int encode_picture(MPVMainEncContext *const m, const AVPacket *pkt) · 36 unmodified lines
39253925 break;
39263926 case FMT_MPEG1:
39273927 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3928− ff_mpeg1_encode_picture_header(s);
3928+ ff_mpeg1_encode_picture_header(m);
39293929 break;
39303930 default:
39313931 av_assert0(0);
libavcodec/mpegvideoenc.h+16−0
@@ -39,6 +39,22 @@ · 38 unmodified lines
3939 typedef struct MPVMainEncContext {
4040 MpegEncContext s; ///< The main slicecontext
4141
42+ int intra_only; ///< if true, only intra pictures are generated
43+ int gop_size;
44+ int picture_in_gop_number; ///< 0-> first pic in gop, ...
45+ int input_picture_number; ///< used to set pic->display_picture_number
46+ int coded_picture_number; ///< used to set pic->coded_picture_number
47+
48+ int64_t user_specified_pts; ///< last non-zero pts from user-supplied AVFrame
49+ /**
50+ * pts difference between the first and second input frame, used for
51+ * calculating dts of the first frame when there's a delay */
52+ int64_t dts_delta;
53+ /**
54+ * reordered pts to be used as dts for the next output frame when there's
55+ * a delay */
56+ int64_t reordered_pts;
57+
4258 /// temporary frames used by b_frame_strategy = 2
4359 AVFrame *tmp_frames[MPVENC_MAX_B_FRAMES + 2];
4460 int b_frame_strategy;
libavcodec/ratecontrol.c+2−2
@@ -660,7 +660,7 @@ av_cold int ff_rate_control_init(MPVMainEncContext *const m) · 659 unmodified lines
660660 double bits = rcc->initial_cplx * (i / 10000.0 + 1.0) * s->mb_num;
661661 RateControlEntry rce;
662662
663− if (i % ((s->gop_size + 3) / 4) == 0)
663+ if (i % ((m->gop_size + 3) / 4) == 0)
664664 rce.pict_type = AV_PICTURE_TYPE_I;
665665 else if (i % (s->max_b_frames + 1))
666666 rce.pict_type = AV_PICTURE_TYPE_B;
@@ -1033,7 +1033,7 @@ float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run) · 366 unmodified lines
10331033 av_assert0(q > 0.0);
10341034
10351035 // FIXME type dependent blur like in 2-pass
1036− if (pict_type == AV_PICTURE_TYPE_P || s->intra_only) {
1036+ if (pict_type == AV_PICTURE_TYPE_P || m->intra_only) {
10371037 rcc->short_term_qsum *= a->qblur;
10381038 rcc->short_term_qcount *= a->qblur;
10391039

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t