History

avcodec/mpegvideoenc: Move tmp bframes to MPVMainEncContext

Also move b_frame_strategy and b_sensitivity and brd_scale; they are all only used by an encoder's main thread when b_frame_strategy == 2.

Andreas Rheinhardt committed 4 years ago

Commit bb0a72b1 parent f507f263 files · +73 −64Browse files

Showing the changes to libavcodec (3 of 3 files). Show all

Unified · Split

3 files
  1. libavcodec/mpegvideo.h +0 −8
  2. libavcodec/mpegvideo_enc.c +61 −53
  3. libavcodec/mpegvideoenc.h +12 −3
libavcodec/mpegvideo.h+0−8
@@ -48,8 +48,6 @@ · 47 unmodified lines
4848
4949 #define MAX_THREADS 32
5050
51−#define MAX_B_FRAMES 16
52−
5351 /**
5452 * Scantable.
5553 */
@@ -446,7 +444,6 @@ typedef struct MpegEncContext { · 390 unmodified lines
446444 int top_field_first;
447445 int concealment_motion_vectors;
448446 int q_scale_type;
449− int brd_scale;
450447 int intra_vlc_format;
451448 int alternate_scan;
452449 int repeat_first_field;
@@ -518,11 +515,6 @@ typedef struct MpegEncContext { · 65 unmodified lines
518515
519516 int error_rate;
520517
521− /* temporary frames used by b_frame_strategy = 2 */
522− AVFrame *tmp_frames[MAX_B_FRAMES + 2];
523− int b_frame_strategy;
524− int b_sensitivity;
525−
526518 /* frame skip options for encoding */
527519 int frame_skip_threshold;
528520 int frame_skip_factor;
libavcodec/mpegvideo_enc.c+61−53
@@ -419,7 +419,8 @@ static av_cold int init_matrices(MpegEncContext *s, AVCodecContext *avctx) · 418 unmodified lines
419419 /* init video encoder */
420420 av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
421421 {
422− MpegEncContext *s = avctx->priv_data;
422+ MPVMainEncContext *const m = avctx->priv_data;
423+ MpegEncContext *const s = &m->s;
423424 AVCPBProperties *cpb_props;
424425 int i, ret;
425426 int mb_array_size, mv_table_size;
@@ -456,10 +457,10 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 30 unmodified lines
456457 }
457458 s->gop_size = avctx->gop_size;
458459 s->avctx = avctx;
459− if (avctx->max_b_frames > MAX_B_FRAMES) {
460+ if (avctx->max_b_frames > MPVENC_MAX_B_FRAMES) {
460461 av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
461− "is %d.\n", MAX_B_FRAMES);
462− avctx->max_b_frames = MAX_B_FRAMES;
462+ "is " AV_STRINGIFY(MPVENC_MAX_B_FRAMES) ".\n");
463+ avctx->max_b_frames = MPVENC_MAX_B_FRAMES;
463464 } else if (avctx->max_b_frames < 0) {
464465 av_log(avctx, AV_LOG_ERROR,
465466 "max b frames must be 0 or positive for mpegvideo based encoders\n");
@@ -700,10 +701,10 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 234 unmodified lines
700701 return AVERROR(EINVAL);
701702 }
702703
703− if (s->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
704+ if (m->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
704705 av_log(avctx, AV_LOG_INFO,
705706 "notice: b_frame_strategy only affects the first pass\n");
706− s->b_frame_strategy = 0;
707+ m->b_frame_strategy = 0;
707708 }
708709
709710 i = av_gcd(avctx->time_base.den, avctx->time_base.num);
@@ -914,8 +915,8 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 204 unmodified lines
914915 return ret;
915916
916917 if (!(avctx->stats_out = av_mallocz(256)) ||
917− !FF_ALLOCZ_TYPED_ARRAY(s->input_picture, MAX_B_FRAMES + 1) ||
918− !FF_ALLOCZ_TYPED_ARRAY(s->reordered_input_picture, MAX_B_FRAMES + 1) ||
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) ||
919920 !(s->new_pic = av_frame_alloc()) ||
920921 !(s->picture_pool = ff_mpv_alloc_pic_pool(0)))
921922 return AVERROR(ENOMEM);
@@ -1010,17 +1011,17 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 88 unmodified lines
10101011 if ((ret = ff_rate_control_init(s)) < 0)
10111012 return ret;
10121013
1013− if (s->b_frame_strategy == 2) {
1014+ if (m->b_frame_strategy == 2) {
10141015 for (i = 0; i < s->max_b_frames + 2; i++) {
1015− s->tmp_frames[i] = av_frame_alloc();
1016− if (!s->tmp_frames[i])
1016+ m->tmp_frames[i] = av_frame_alloc();
1017+ if (!m->tmp_frames[i])
10171018 return AVERROR(ENOMEM);
10181019
1019− s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
1020− s->tmp_frames[i]->width = s->width >> s->brd_scale;
1021− s->tmp_frames[i]->height = s->height >> s->brd_scale;
1020+ m->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
1021+ m->tmp_frames[i]->width = s->width >> m->brd_scale;
1022+ m->tmp_frames[i]->height = s->height >> m->brd_scale;
10221023
1023− ret = av_frame_get_buffer(s->tmp_frames[i], 0);
1024+ ret = av_frame_get_buffer(m->tmp_frames[i], 0);
10241025 if (ret < 0)
10251026 return ret;
10261027 }
@@ -1039,8 +1040,8 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 12 unmodified lines
10391040
10401041 av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
10411042 {
1042− MpegEncContext *s = avctx->priv_data;
1043− int i;
1043+ MPVMainEncContext *const m = avctx->priv_data;
1044+ MpegEncContext *const s = &m->s;
10441045
10451046 ff_rate_control_uninit(&s->rc_context);
10461047
@@ -1048,13 +1049,13 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx) · 1 unmodified line
10481049 av_refstruct_pool_uninit(&s->picture_pool);
10491050
10501051 if (s->input_picture && s->reordered_input_picture) {
1051− for (int i = 0; i < MAX_B_FRAMES + 1; i++) {
1052+ for (int i = 0; i < MPVENC_MAX_B_FRAMES + 1; i++) {
10521053 av_refstruct_unref(&s->input_picture[i]);
10531054 av_refstruct_unref(&s->reordered_input_picture[i]);
10541055 }
10551056 }
1056− for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
1057− av_frame_free(&s->tmp_frames[i]);
1057+ for (int i = 0; i < FF_ARRAY_ELEMS(m->tmp_frames); i++)
1058+ av_frame_free(&m->tmp_frames[i]);
10581059
10591060 av_frame_free(&s->new_pic);
10601061
@@ -1249,8 +1250,9 @@ static int prepare_picture(MpegEncContext *s, AVFrame *f, const AVFrame *props_f · 188 unmodified lines
12491250 return 0;
12501251 }
12511252
1252−static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
1253+static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg)
12531254 {
1255+ MpegEncContext *const s = &m->s;
12541256 MPVPicture *pic = NULL;
12551257 int64_t pts;
12561258 int display_picture_number = 0, ret;
@@ -1376,9 +1378,9 @@ static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg) · 119 unmodified lines
13761378 }
13771379
13781380 /* shift buffer entries */
1379− for (int i = flush_offset; i <= MAX_B_FRAMES; i++)
1381+ for (int i = flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
13801382 s->input_picture[i - flush_offset] = s->input_picture[i];
1381− for (int i = MAX_B_FRAMES + 1 - flush_offset; i <= MAX_B_FRAMES; i++)
1383+ for (int i = MPVENC_MAX_B_FRAMES + 1 - flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
13821384 s->input_picture[i] = NULL;
13831385
13841386 s->input_picture[encoding_delay] = pic;
@@ -1451,10 +1453,11 @@ static int encode_frame(AVCodecContext *c, const AVFrame *frame, AVPacket *pkt) · 66 unmodified lines
14511453 return size;
14521454 }
14531455
1454−static int estimate_best_b_count(MpegEncContext *s)
1456+static int estimate_best_b_count(MPVMainEncContext *const m)
14551457 {
1458+ MpegEncContext *const s = &m->s;
14561459 AVPacket *pkt;
1457− const int scale = s->brd_scale;
1460+ const int scale = m->brd_scale;
14581461 int width = s->width >> scale;
14591462 int height = s->height >> scale;
14601463 int i, j, out_size, p_lambda, b_lambda, lambda2;
@@ -1491,18 +1494,18 @@ static int estimate_best_b_count(MpegEncContext *s) · 30 unmodified lines
14911494 data[2] += INPLACE_OFFSET;
14921495 }
14931496
1494− s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
1495− s->tmp_frames[i]->linesize[0],
1497+ s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[0],
1498+ m->tmp_frames[i]->linesize[0],
14961499 data[0],
14971500 pre_input_ptr->f->linesize[0],
14981501 width, height);
1499− s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
1500− s->tmp_frames[i]->linesize[1],
1502+ s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[1],
1503+ m->tmp_frames[i]->linesize[1],
15011504 data[1],
15021505 pre_input_ptr->f->linesize[1],
15031506 width >> 1, height >> 1);
1504− s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
1505− s->tmp_frames[i]->linesize[2],
1507+ s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[2],
1508+ m->tmp_frames[i]->linesize[2],
15061509 data[2],
15071510 pre_input_ptr->f->linesize[2],
15081511 width >> 1, height >> 1);
@@ -1539,10 +1542,10 @@ static int estimate_best_b_count(MpegEncContext *s) · 30 unmodified lines
15391542 goto fail;
15401543
15411544
1542− s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1543− s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1545+ m->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1546+ m->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
15441547
1545− out_size = encode_frame(c, s->tmp_frames[0], pkt);
1548+ out_size = encode_frame(c, m->tmp_frames[0], pkt);
15461549 if (out_size < 0) {
15471550 ret = out_size;
15481551 goto fail;
@@ -1553,11 +1556,11 @@ static int estimate_best_b_count(MpegEncContext *s) · 4 unmodified lines
15531556 for (i = 0; i < s->max_b_frames + 1; i++) {
15541557 int is_p = i % (j + 1) == j || i == s->max_b_frames;
15551558
1556− s->tmp_frames[i + 1]->pict_type = is_p ?
1559+ m->tmp_frames[i + 1]->pict_type = is_p ?
15571560 AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1558− s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1561+ m->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
15591562
1560− out_size = encode_frame(c, s->tmp_frames[i + 1], pkt);
1563+ out_size = encode_frame(c, m->tmp_frames[i + 1], pkt);
15611564 if (out_size < 0) {
15621565 ret = out_size;
15631566 goto fail;
@@ -1603,8 +1606,10 @@ fail: · 39 unmodified lines
16031606 * input_picture[0] is always NULL when exiting this function, even on error;
16041607 * reordered_input_picture[0] is always NULL when exiting this function on error.
16051608 */
1606−static int set_bframe_chain_length(MpegEncContext *s)
1609+static int set_bframe_chain_length(MPVMainEncContext *const m)
16071610 {
1611+ MpegEncContext *const s = &m->s;
1612+
16081613 /* Either nothing to do or can't do anything */
16091614 if (s->reordered_input_picture[0] || !s->input_picture[0])
16101615 return 0;
@@ -1649,11 +1654,11 @@ static int set_bframe_chain_length(MpegEncContext *s) · 38 unmodified lines
16491654 }
16501655 }
16511656
1652− if (s->b_frame_strategy == 0) {
1657+ if (m->b_frame_strategy == 0) {
16531658 b_frames = s->max_b_frames;
16541659 while (b_frames && !s->input_picture[b_frames])
16551660 b_frames--;
1656− } else if (s->b_frame_strategy == 1) {
1661+ } else if (m->b_frame_strategy == 1) {
16571662 int i;
16581663 for (i = 1; i < s->max_b_frames + 1; i++) {
16591664 if (s->input_picture[i] &&
@@ -1668,7 +1673,7 @@ static int set_bframe_chain_length(MpegEncContext *s) · 8 unmodified lines
16681673 for (i = 0; i < s->max_b_frames + 1; i++) {
16691674 if (!s->input_picture[i] ||
16701675 s->input_picture[i]->b_frame_score - 1 >
1671− s->mb_num / s->b_sensitivity)
1676+ s->mb_num / m->b_sensitivity)
16721677 break;
16731678 }
16741679
@@ -1678,8 +1683,8 @@ static int set_bframe_chain_length(MpegEncContext *s) · 3 unmodified lines
16781683 for (i = 0; i < b_frames + 1; i++) {
16791684 s->input_picture[i]->b_frame_score = 0;
16801685 }
1681− } else if (s->b_frame_strategy == 2) {
1682− b_frames = estimate_best_b_count(s);
1686+ } else if (m->b_frame_strategy == 2) {
1687+ b_frames = estimate_best_b_count(m);
16831688 if (b_frames < 0) {
16841689 av_refstruct_unref(&s->input_picture[0]);
16851690 return b_frames;
@@ -1733,17 +1738,18 @@ static int set_bframe_chain_length(MpegEncContext *s) · 47 unmodified lines
17331738 return 0;
17341739 }
17351740
1736−static int select_input_picture(MpegEncContext *s)
1741+static int select_input_picture(MPVMainEncContext *const m)
17371742 {
1743+ MpegEncContext *const s = &m->s;
17381744 int ret;
17391745
17401746 av_assert1(!s->reordered_input_picture[0]);
17411747
1742− for (int i = 1; i <= MAX_B_FRAMES; i++)
1748+ for (int i = 1; i <= MPVENC_MAX_B_FRAMES; i++)
17431749 s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1744− s->reordered_input_picture[MAX_B_FRAMES] = NULL;
1750+ s->reordered_input_picture[MPVENC_MAX_B_FRAMES] = NULL;
17451751
1746− ret = set_bframe_chain_length(s);
1752+ ret = set_bframe_chain_length(m);
17471753 av_assert1(!s->input_picture[0]);
17481754 if (ret < 0)
17491755 return ret;
@@ -1867,7 +1873,8 @@ static void frame_start(MpegEncContext *s) · 117 unmodified lines
18671873 int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
18681874 const AVFrame *pic_arg, int *got_packet)
18691875 {
1870− MpegEncContext *s = avctx->priv_data;
1876+ MPVMainEncContext *const m = avctx->priv_data;
1877+ MpegEncContext *const s = &m->s;
18711878 int stuffing_count, ret;
18721879 int context_count = s->slice_context_count;
18731880
@@ -1877,12 +1884,13 @@ int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, · 3 unmodified lines
18771884
18781885 s->picture_in_gop_number++;
18791886
1880− if (load_input_picture(s, pic_arg) < 0)
1881− return -1;
1887+ ret = load_input_picture(m, pic_arg);
1888+ if (ret < 0)
1889+ return ret;
18821890
1883− if (select_input_picture(s) < 0) {
1884− return -1;
1885− }
1891+ ret = select_input_picture(m);
1892+ if (ret < 0)
1893+ return ret;
18861894
18871895 /* output? */
18881896 if (s->new_pic->data[0]) {
libavcodec/mpegvideoenc.h+12−3
@@ -33,8 +33,16 @@ · 32 unmodified lines
3333 #include "libavutil/opt.h"
3434 #include "mpegvideo.h"
3535
36+#define MPVENC_MAX_B_FRAMES 16
37+
3638 typedef struct MPVMainEncContext {
3739 MpegEncContext s; ///< The main slicecontext
40+
41+ /// temporary frames used by b_frame_strategy = 2
42+ AVFrame *tmp_frames[MPVENC_MAX_B_FRAMES + 2];
43+ int b_frame_strategy;
44+ int b_sensitivity;
45+ int brd_scale;
3846 } MPVMainEncContext;
3947
4048 #define MAX_FCODE 7
@@ -85,6 +93,7 @@ typedef struct MPVMainEncContext { · 44 unmodified lines
8593 { "msad", "Sum of absolute differences, median predicted", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_MEDIAN_SAD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }
8694
8795 #define FF_MPV_OFFSET(x) offsetof(MpegEncContext, x)
96+#define FF_MPV_MAIN_OFFSET(x) offsetof(MPVMainEncContext, x)
8897 #define FF_RC_OFFSET(x) offsetof(MpegEncContext, rc_context.x)
8998 #define FF_MPV_OPT_FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
9099 #define FF_MPV_COMMON_OPTS \
@@ -126,9 +135,9 @@ FF_MPV_OPT_CMP_FUNC, \ · 35 unmodified lines
126135 {"ps", "RTP payload size in bytes", FF_MPV_OFFSET(rtp_payload_size), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
127136
128137 #define FF_MPV_COMMON_BFRAME_OPTS \
129−{"b_strategy", "Strategy to choose between I/P/B-frames", FF_MPV_OFFSET(b_frame_strategy), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, FF_MPV_OPT_FLAGS }, \
130−{"b_sensitivity", "Adjust sensitivity of b_frame_strategy 1", FF_MPV_OFFSET(b_sensitivity), AV_OPT_TYPE_INT, {.i64 = 40 }, 1, INT_MAX, FF_MPV_OPT_FLAGS }, \
131−{"brd_scale", "Downscale frames for dynamic B-frame decision", FF_MPV_OFFSET(brd_scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 3, FF_MPV_OPT_FLAGS },
138+{"b_strategy", "Strategy to choose between I/P/B-frames", FF_MPV_MAIN_OFFSET(b_frame_strategy), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, FF_MPV_OPT_FLAGS }, \
139+{"b_sensitivity", "Adjust sensitivity of b_frame_strategy 1", FF_MPV_MAIN_OFFSET(b_sensitivity), AV_OPT_TYPE_INT, {.i64 = 40 }, 1, INT_MAX, FF_MPV_OPT_FLAGS }, \
140+{"brd_scale", "Downscale frames for dynamic B-frame decision", FF_MPV_MAIN_OFFSET(brd_scale), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 3, FF_MPV_OPT_FLAGS },
132141
133142 #define FF_MPV_COMMON_MOTION_EST_OPTS \
134143 {"motion_est", "motion estimation algorithm", FF_MPV_OFFSET(motion_est), AV_OPT_TYPE_INT, {.i64 = FF_ME_EPZS }, FF_ME_ZERO, FF_ME_XONE, FF_MPV_OPT_FLAGS, .unit = "motion_est" }, \

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