History

avcodec/mpegvideo: Move MPVPicture arrays to MPVMainEncContext

Also avoid allocations while at it.

Andreas Rheinhardt committed 1 year ago

Commit bf4dedd1 parent 72bf3d34 files · +68 −72Browse files

Unified · Split

4 files
  1. libavcodec/mpeg4videoenc.c +6 −5
  2. libavcodec/mpegvideo.h +0 −2
  3. libavcodec/mpegvideo_enc.c +59 −65
  4. libavcodec/mpegvideoenc.h +3 −0
libavcodec/mpeg4videoenc.c+6−5
@@ -668,7 +668,7 @@ static void mpeg4_encode_mb(MpegEncContext *const s, int16_t block[][64], · 667 unmodified lines
668668 for (int i = 0; i < m->max_b_frames; i++) {
669669 const uint8_t *b_pic;
670670 int diff;
671− const MPVPicture *pic = s->reordered_input_picture[i + 1];
671+ const MPVPicture *pic = m->reordered_input_picture[i + 1];
672672
673673 if (!pic || pic->f->pict_type != AV_PICTURE_TYPE_B)
674674 break;
@@ -900,16 +900,17 @@ void ff_set_mpeg4_time(MpegEncContext *s) · 225 unmodified lines
900900 }
901901 }
902902
903−static void mpeg4_encode_gop_header(MpegEncContext *s)
903+static void mpeg4_encode_gop_header(MPVMainEncContext *const m)
904904 {
905+ MpegEncContext *const s = &m->s;
905906 int64_t hours, minutes, seconds;
906907 int64_t time;
907908
908909 put_bits32(&s->pb, GOP_STARTCODE);
909910
910911 time = s->cur_pic.ptr->f->pts;
911− if (s->reordered_input_picture[1])
912− time = FFMIN(time, s->reordered_input_picture[1]->f->pts);
912+ if (m->reordered_input_picture[1])
913+ time = FFMIN(time, m->reordered_input_picture[1]->f->pts);
913914 time = time * s->avctx->time_base.num;
914915 s->last_time_base = FFUDIV(time, s->avctx->time_base.den);
915916
@@ -1077,7 +1078,7 @@ static int mpeg4_encode_picture_header(MPVMainEncContext *const m) · 161 unmodified lines
10771078 if (s->avctx->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || s->picture_number == 0) // HACK, the reference sw is buggy
10781079 mpeg4_encode_vol_header(m4, 0, 0);
10791080 }
1080− mpeg4_encode_gop_header(s);
1081+ mpeg4_encode_gop_header(m);
10811082 }
10821083
10831084 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
libavcodec/mpegvideo.h+0−2
@@ -123,8 +123,6 @@ typedef struct MpegEncContext { · 122 unmodified lines
123123 ptrdiff_t linesize; ///< line size, in bytes, may be different from width
124124 ptrdiff_t uvlinesize; ///< line size, for chroma in bytes, may be different from width
125125 struct AVRefStructPool *picture_pool; ///< Pool for MPVPictures
126− MPVPicture **input_picture;///< next pictures on display order for encoding
127− MPVPicture **reordered_input_picture; ///< pointer to the next pictures in coded order for encoding
128126
129127 BufferPoolContext buffer_pools;
130128
libavcodec/mpegvideo_enc.c+59−65
@@ -915,8 +915,6 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 914 unmodified lines
915915 return ret;
916916
917917 if (!(avctx->stats_out = av_mallocz(256)) ||
918− !FF_ALLOCZ_TYPED_ARRAY(s->input_picture, MPVENC_MAX_B_FRAMES + 1) ||
919− !FF_ALLOCZ_TYPED_ARRAY(s->reordered_input_picture, MPVENC_MAX_B_FRAMES + 1) ||
920918 !(s->new_pic = av_frame_alloc()) ||
921919 !(s->picture_pool = ff_mpv_alloc_pic_pool(0)))
922920 return AVERROR(ENOMEM);
@@ -1049,11 +1047,9 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx) · 126 unmodified lines
10491047 ff_mpv_common_end(s);
10501048 av_refstruct_pool_uninit(&s->picture_pool);
10511049
1052− if (s->input_picture && s->reordered_input_picture) {
1053− for (int i = 0; i < MPVENC_MAX_B_FRAMES + 1; i++) {
1054− av_refstruct_unref(&s->input_picture[i]);
1055− av_refstruct_unref(&s->reordered_input_picture[i]);
1056− }
1050+ for (int i = 0; i < MPVENC_MAX_B_FRAMES + 1; i++) {
1051+ av_refstruct_unref(&m->input_picture[i]);
1052+ av_refstruct_unref(&m->reordered_input_picture[i]);
10571053 }
10581054 for (int i = 0; i < FF_ARRAY_ELEMS(m->tmp_frames); i++)
10591055 av_frame_free(&m->tmp_frames[i]);
@@ -1077,8 +1073,6 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx) · 17 unmodified lines
10771073
10781074 av_freep(&s->q_intra_matrix);
10791075 av_freep(&s->q_intra_matrix16);
1080− av_freep(&s->input_picture);
1081− av_freep(&s->reordered_input_picture);
10821076 av_freep(&s->dct_offset);
10831077 av_freep(&s->mb_var);
10841078 av_freep(&s->mc_mb_var);
@@ -1260,7 +1254,7 @@ static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg · 175 unmodified lines
12601254 int flush_offset = 1;
12611255 int direct = 1;
12621256
1263− av_assert1(!s->input_picture[0]);
1257+ av_assert1(!m->input_picture[0]);
12641258
12651259 if (pic_arg) {
12661260 pts = pic_arg->pts;
@@ -1363,14 +1357,14 @@ static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg · 96 unmodified lines
13631357
13641358 pic->display_picture_number = display_picture_number;
13651359 pic->f->pts = pts; // we set this here to avoid modifying pic_arg
1366− } else if (!s->reordered_input_picture[1]) {
1360+ } else if (!m->reordered_input_picture[1]) {
13671361 /* Flushing: When the above check is true, the encoder is about to run
13681362 * out of frames to encode. Check if there are input_pictures left;
1369− * if so, ensure s->input_picture[0] contains the first picture.
1363+ * if so, ensure m->input_picture[0] contains the first picture.
13701364 * A flush_offset != 1 will only happen if we did not receive enough
13711365 * input frames. */
13721366 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1373− if (s->input_picture[flush_offset])
1367+ if (m->input_picture[flush_offset])
13741368 break;
13751369
13761370 encoding_delay -= flush_offset - 1;
@@ -1378,11 +1372,11 @@ static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg · 1 unmodified line
13781372
13791373 /* shift buffer entries */
13801374 for (int i = flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
1381− s->input_picture[i - flush_offset] = s->input_picture[i];
1375+ m->input_picture[i - flush_offset] = m->input_picture[i];
13821376 for (int i = MPVENC_MAX_B_FRAMES + 1 - flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
1383− s->input_picture[i] = NULL;
1377+ m->input_picture[i] = NULL;
13841378
1385− s->input_picture[encoding_delay] = pic;
1379+ m->input_picture[encoding_delay] = pic;
13861380
13871381 return 0;
13881382 fail:
@@ -1482,7 +1476,7 @@ static int estimate_best_b_count(MPVMainEncContext *const m) · 93 unmodified lines
14821476 FF_LAMBDA_SHIFT;
14831477
14841478 for (int i = 0; i < m->max_b_frames + 2; i++) {
1485− const MPVPicture *pre_input_ptr = i ? s->input_picture[i - 1] :
1479+ const MPVPicture *pre_input_ptr = i ? m->input_picture[i - 1] :
14861480 s->next_pic.ptr;
14871481
14881482 if (pre_input_ptr) {
@@ -1517,7 +1511,7 @@ static int estimate_best_b_count(MPVMainEncContext *const m) · 28 unmodified lines
15171511 AVCodecContext *c;
15181512 int64_t rd = 0;
15191513
1520− if (!s->input_picture[j])
1514+ if (!m->input_picture[j])
15211515 break;
15221516
15231517 c = avcodec_alloc_context3(NULL);
@@ -1612,16 +1606,16 @@ static int set_bframe_chain_length(MPVMainEncContext *const m) · 88 unmodified lines
16121606 MpegEncContext *const s = &m->s;
16131607
16141608 /* Either nothing to do or can't do anything */
1615− if (s->reordered_input_picture[0] || !s->input_picture[0])
1609+ if (m->reordered_input_picture[0] || !m->input_picture[0])
16161610 return 0;
16171611
16181612 /* set next picture type & ordering */
16191613 if (m->frame_skip_threshold || m->frame_skip_factor) {
16201614 if (m->picture_in_gop_number < m->gop_size &&
16211615 s->next_pic.ptr &&
1622− skip_check(m, s->input_picture[0], s->next_pic.ptr)) {
1616+ skip_check(m, m->input_picture[0], s->next_pic.ptr)) {
16231617 // FIXME check that the gop check above is +-1 correct
1624− av_refstruct_unref(&s->input_picture[0]);
1618+ av_refstruct_unref(&m->input_picture[0]);
16251619
16261620 ff_vbv_update(m, 0);
16271621
@@ -1631,48 +1625,48 @@ static int set_bframe_chain_length(MPVMainEncContext *const m) · 3 unmodified lines
16311625
16321626 if (/* m->picture_in_gop_number >= m->gop_size || */
16331627 !s->next_pic.ptr || m->intra_only) {
1634− s->reordered_input_picture[0] = s->input_picture[0];
1635− s->input_picture[0] = NULL;
1636− s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1637− s->reordered_input_picture[0]->coded_picture_number =
1628+ m->reordered_input_picture[0] = m->input_picture[0];
1629+ m->input_picture[0] = NULL;
1630+ m->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1631+ m->reordered_input_picture[0]->coded_picture_number =
16381632 m->coded_picture_number++;
16391633 } else {
16401634 int b_frames = 0;
16411635
16421636 if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
16431637 for (int i = 0; i < m->max_b_frames + 1; i++) {
1644− int pict_num = s->input_picture[0]->display_picture_number + i;
1638+ int pict_num = m->input_picture[0]->display_picture_number + i;
16451639
16461640 if (pict_num >= m->rc_context.num_entries)
16471641 break;
1648− if (!s->input_picture[i]) {
1642+ if (!m->input_picture[i]) {
16491643 m->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
16501644 break;
16511645 }
16521646
1653− s->input_picture[i]->f->pict_type =
1647+ m->input_picture[i]->f->pict_type =
16541648 m->rc_context.entry[pict_num].new_pict_type;
16551649 }
16561650 }
16571651
16581652 if (m->b_frame_strategy == 0) {
16591653 b_frames = m->max_b_frames;
1660− while (b_frames && !s->input_picture[b_frames])
1654+ while (b_frames && !m->input_picture[b_frames])
16611655 b_frames--;
16621656 } else if (m->b_frame_strategy == 1) {
16631657 for (int i = 1; i < m->max_b_frames + 1; i++) {
1664− if (s->input_picture[i] &&
1665− s->input_picture[i]->b_frame_score == 0) {
1666− s->input_picture[i]->b_frame_score =
1658+ if (m->input_picture[i] &&
1659+ m->input_picture[i]->b_frame_score == 0) {
1660+ m->input_picture[i]->b_frame_score =
16671661 get_intra_count(s,
1668− s->input_picture[i ]->f->data[0],
1669− s->input_picture[i - 1]->f->data[0],
1662+ m->input_picture[i ]->f->data[0],
1663+ m->input_picture[i - 1]->f->data[0],
16701664 s->linesize) + 1;
16711665 }
16721666 }
16731667 for (int i = 0; i < m->max_b_frames + 1; i++) {
1674− if (!s->input_picture[i] ||
1675− s->input_picture[i]->b_frame_score - 1 >
1668+ if (!m->input_picture[i] ||
1669+ m->input_picture[i]->b_frame_score - 1 >
16761670 s->mb_num / m->b_sensitivity) {
16771671 b_frames = FFMAX(0, i - 1);
16781672 break;
@@ -1681,11 +1675,11 @@ static int set_bframe_chain_length(MPVMainEncContext *const m) · 2 unmodified lines
16811675
16821676 /* reset scores */
16831677 for (int i = 0; i < b_frames + 1; i++)
1684− s->input_picture[i]->b_frame_score = 0;
1678+ m->input_picture[i]->b_frame_score = 0;
16851679 } else if (m->b_frame_strategy == 2) {
16861680 b_frames = estimate_best_b_count(m);
16871681 if (b_frames < 0) {
1688− av_refstruct_unref(&s->input_picture[0]);
1682+ av_refstruct_unref(&m->input_picture[0]);
16891683 return b_frames;
16901684 }
16911685 }
@@ -1693,11 +1687,11 @@ static int set_bframe_chain_length(MPVMainEncContext *const m) · 1 unmodified line
16931687 emms_c();
16941688
16951689 for (int i = b_frames - 1; i >= 0; i--) {
1696− int type = s->input_picture[i]->f->pict_type;
1690+ int type = m->input_picture[i]->f->pict_type;
16971691 if (type && type != AV_PICTURE_TYPE_B)
16981692 b_frames = i;
16991693 }
1700− if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1694+ if (m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
17011695 b_frames == m->max_b_frames) {
17021696 av_log(s->avctx, AV_LOG_ERROR,
17031697 "warning, too many B-frames in a row\n");
@@ -1710,26 +1704,26 @@ static int set_bframe_chain_length(MPVMainEncContext *const m) · 6 unmodified lines
17101704 } else {
17111705 if (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
17121706 b_frames = 0;
1713− s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1707+ m->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
17141708 }
17151709 }
17161710
17171711 if ((s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
1718− s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1712+ m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
17191713 b_frames--;
17201714
1721− s->reordered_input_picture[0] = s->input_picture[b_frames];
1722− s->input_picture[b_frames] = NULL;
1723− if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1724− s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1725− s->reordered_input_picture[0]->coded_picture_number =
1715+ m->reordered_input_picture[0] = m->input_picture[b_frames];
1716+ m->input_picture[b_frames] = NULL;
1717+ if (m->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1718+ m->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1719+ m->reordered_input_picture[0]->coded_picture_number =
17261720 m->coded_picture_number++;
17271721 for (int i = 0; i < b_frames; i++) {
1728− s->reordered_input_picture[i + 1] = s->input_picture[i];
1729− s->input_picture[i] = NULL;
1730− s->reordered_input_picture[i + 1]->f->pict_type =
1722+ m->reordered_input_picture[i + 1] = m->input_picture[i];
1723+ m->input_picture[i] = NULL;
1724+ m->reordered_input_picture[i + 1]->f->pict_type =
17311725 AV_PICTURE_TYPE_B;
1732− s->reordered_input_picture[i + 1]->coded_picture_number =
1726+ m->reordered_input_picture[i + 1]->coded_picture_number =
17331727 m->coded_picture_number++;
17341728 }
17351729 }
@@ -1742,34 +1736,34 @@ static int select_input_picture(MPVMainEncContext *const m) · 6 unmodified lines
17421736 MpegEncContext *const s = &m->s;
17431737 int ret;
17441738
1745− av_assert1(!s->reordered_input_picture[0]);
1739+ av_assert1(!m->reordered_input_picture[0]);
17461740
17471741 for (int i = 1; i <= MPVENC_MAX_B_FRAMES; i++)
1748− s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1749− s->reordered_input_picture[MPVENC_MAX_B_FRAMES] = NULL;
1742+ m->reordered_input_picture[i - 1] = m->reordered_input_picture[i];
1743+ m->reordered_input_picture[MPVENC_MAX_B_FRAMES] = NULL;
17501744
17511745 ret = set_bframe_chain_length(m);
1752− av_assert1(!s->input_picture[0]);
1746+ av_assert1(!m->input_picture[0]);
17531747 if (ret < 0)
17541748 return ret;
17551749
17561750 av_frame_unref(s->new_pic);
17571751
1758− if (s->reordered_input_picture[0]) {
1759− s->reordered_input_picture[0]->reference =
1760− s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_B;
1752+ if (m->reordered_input_picture[0]) {
1753+ m->reordered_input_picture[0]->reference =
1754+ m->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_B;
17611755
1762− if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1756+ if (m->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
17631757 // input is a shared pix, so we can't modify it -> allocate a new
17641758 // one & ensure that the shared one is reuseable
1765− av_frame_move_ref(s->new_pic, s->reordered_input_picture[0]->f);
1759+ av_frame_move_ref(s->new_pic, m->reordered_input_picture[0]->f);
17661760
1767− ret = prepare_picture(s, s->reordered_input_picture[0]->f, s->new_pic);
1761+ ret = prepare_picture(s, m->reordered_input_picture[0]->f, s->new_pic);
17681762 if (ret < 0)
17691763 goto fail;
17701764 } else {
17711765 // input is not a shared pix -> reuse buffer for current_pix
1772− ret = av_frame_ref(s->new_pic, s->reordered_input_picture[0]->f);
1766+ ret = av_frame_ref(s->new_pic, m->reordered_input_picture[0]->f);
17731767 if (ret < 0)
17741768 goto fail;
17751769 for (int i = 0; i < MPV_MAX_PLANES; i++) {
@@ -1777,8 +1771,8 @@ static int select_input_picture(MPVMainEncContext *const m) · 1 unmodified line
17771771 s->new_pic->data[i] += INPLACE_OFFSET;
17781772 }
17791773 }
1780− s->cur_pic.ptr = s->reordered_input_picture[0];
1781− s->reordered_input_picture[0] = NULL;
1774+ s->cur_pic.ptr = m->reordered_input_picture[0];
1775+ m->reordered_input_picture[0] = NULL;
17821776 av_assert1(s->mb_width == s->buffer_pools.alloc_mb_width);
17831777 av_assert1(s->mb_height == s->buffer_pools.alloc_mb_height);
17841778 av_assert1(s->mb_stride == s->buffer_pools.alloc_mb_stride);
@@ -1793,7 +1787,7 @@ static int select_input_picture(MPVMainEncContext *const m) · 8 unmodified lines
17931787 }
17941788 return 0;
17951789 fail:
1796− av_refstruct_unref(&s->reordered_input_picture[0]);
1790+ av_refstruct_unref(&m->reordered_input_picture[0]);
17971791 return ret;
17981792 }
17991793
libavcodec/mpegvideoenc.h+3−0
@@ -47,6 +47,9 @@ typedef struct MPVMainEncContext { · 46 unmodified lines
4747 int input_picture_number; ///< used to set pic->display_picture_number
4848 int coded_picture_number; ///< used to set pic->coded_picture_number
4949
50+ MPVPicture *input_picture[MPVENC_MAX_B_FRAMES + 1]; ///< next pictures in display order
51+ MPVPicture *reordered_input_picture[MPVENC_MAX_B_FRAMES + 1]; ///< next pictures in coded order
52+
5053 int64_t user_specified_pts; ///< last non-zero pts from user-supplied AVFrame
5154 /**
5255 * pts difference between the first and second input frame, used for

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