History

avformat/dashenc: allow splitting fragments following P-Frame reordering

James Almer committed 7 years ago

Commit ea5a9101 parent ceb236c1 file · +67 −2Browse files

Unified · Split

libavformat/dashenc.c+67−2
@@ -62,6 +62,7 @@ enum { · 61 unmodified lines
6262 FRAG_TYPE_NONE = 0,
6363 FRAG_TYPE_EVERY_FRAME,
6464 FRAG_TYPE_DURATION,
65+ FRAG_TYPE_PFRAMES,
6566 FRAG_TYPE_NB
6667 };
6768
@@ -91,6 +92,8 @@ typedef struct OutputStream { · 23 unmodified lines
9192 AVFormatContext *ctx;
9293 int ctx_inited, as_idx;
9394 AVIOContext *out;
95+ AVCodecParserContext *parser;
96+ AVCodecContext *parser_avctx;
9497 int packets_written;
9598 char initfile[1024];
9699 int64_t init_start_pos, pos;
@@ -102,6 +105,7 @@ typedef struct OutputStream { · 5 unmodified lines
102105 Segment **segments;
103106 int64_t first_pts, start_pts, max_pts;
104107 int64_t last_dts, last_pts;
108+ int last_flags;
105109 int bit_rate;
106110 SegmentType segment_type; /* segment type selected for this particular stream */
107111 const char *format_name;
@@ -117,6 +121,7 @@ typedef struct OutputStream { · 9 unmodified lines
117121 char temp_path[1024];
118122 double availability_time_offset;
119123 int total_pkt_size;
124+ int64_t total_pkt_duration;
120125 int muxer_overhead;
121126 int frag_type;
122127 } OutputStream;
@@ -606,6 +611,8 @@ static void dash_free(AVFormatContext *s) · 483 unmodified lines
606611 }
607612 ff_format_io_close(s, &os->out);
608613 avformat_free_context(os->ctx);
614+ avcodec_free_context(&os->parser_avctx);
615+ av_parser_close(os->parser);
609616 for (j = 0; j < os->nb_segments; j++)
610617 av_free(os->segments[j]);
611618 av_free(os->segments);
@@ -933,6 +940,8 @@ static int parse_adaptation_sets(AVFormatContext *s) · 321 unmodified lines
933940
934941 if (!strcmp(type_str, "duration"))
935942 as->frag_type = FRAG_TYPE_DURATION;
943+ else if (!strcmp(type_str, "pframes"))
944+ as->frag_type = FRAG_TYPE_PFRAMES;
936945 else if (!strcmp(type_str, "every_frame"))
937946 as->frag_type = FRAG_TYPE_EVERY_FRAME;
938947 else if (!strcmp(type_str, "none"))
@@ -1349,6 +1358,18 @@ static int dash_init(AVFormatContext *s) · 410 unmodified lines
13491358 ctx->avoid_negative_ts = s->avoid_negative_ts;
13501359 ctx->flags = s->flags;
13511360
1361+ os->parser = av_parser_init(st->codecpar->codec_id);
1362+ if (os->parser) {
1363+ os->parser_avctx = avcodec_alloc_context3(NULL);
1364+ if (!os->parser_avctx)
1365+ return AVERROR(ENOMEM);
1366+ ret = avcodec_parameters_to_context(os->parser_avctx, st->codecpar);
1367+ if (ret < 0)
1368+ return ret;
1369+ // We only want to parse frame headers
1370+ os->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
1371+ }
1372+
13521373 if (c->single_file) {
13531374 if (os->single_file_name)
13541375 ff_dash_fill_tmpl_params(os->initfile, sizeof(os->initfile), os->single_file_name, i, 0, os->bit_rate, 0);
@@ -1391,6 +1412,11 @@ static int dash_init(AVFormatContext *s) · 36 unmodified lines
13911412 av_log(s, AV_LOG_ERROR, "Fragment duration %"PRId64" is longer than Segment duration %"PRId64"\n", os->frag_duration, os->seg_duration);
13921413 return AVERROR(EINVAL);
13931414 }
1415+ if (os->frag_type == FRAG_TYPE_PFRAMES && (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO || !os->parser)) {
1416+ if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && !os->parser)
1417+ av_log(s, AV_LOG_WARNING, "frag_type set to P-Frame reordering, but no parser found for stream %d\n", i);
1418+ os->frag_type = c->streaming ? FRAG_TYPE_EVERY_FRAME : FRAG_TYPE_NONE;
1419+ }
13941420
13951421 if (os->segment_type == SEGMENT_TYPE_MP4) {
13961422 if (c->streaming)
@@ -1458,6 +1484,8 @@ static int dash_init(AVFormatContext *s) · 61 unmodified lines
14581484 av_log(s, AV_LOG_WARNING, "no video stream and no seg duration set\n");
14591485 return AVERROR(EINVAL);
14601486 }
1487+ if (!c->has_video && c->frag_type == FRAG_TYPE_PFRAMES)
1488+ av_log(s, AV_LOG_WARNING, "no video stream and P-frame fragmentation set\n");
14611489
14621490 c->nr_of_streams_flushed = 0;
14631491
@@ -1730,6 +1758,7 @@ static int dash_flush(AVFormatContext *s, int final, int stream) · 266 unmodified lines
17301758 av_rescale_q(os->max_pts - os->start_pts,
17311759 st->time_base, AV_TIME_BASE_Q);
17321760 os->total_pkt_size = 0;
1761+ os->total_pkt_duration = 0;
17331762
17341763 if (!os->bit_rate) {
17351764 // calculate average bitrate of first segment
@@ -1831,6 +1860,9 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt) · 95 unmodified lines
18311860 sizeof(c->availability_start_time));
18321861 }
18331862
1863+ if (!os->packets_written)
1864+ os->availability_time_offset = 0;
1865+
18341866 if (!os->availability_time_offset &&
18351867 ((os->frag_type == FRAG_TYPE_DURATION && os->seg_duration != os->frag_duration) ||
18361868 (os->frag_type == FRAG_TYPE_EVERY_FRAME && pkt->duration))) {
@@ -1895,11 +1927,43 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt) · 58 unmodified lines
18951927 os->max_pts = pkt->pts + pkt->duration;
18961928 else
18971929 os->max_pts = FFMAX(os->max_pts, pkt->pts + pkt->duration);
1898− os->packets_written++;
1899− os->total_pkt_size += pkt->size;
1930+
1931+ if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1932+ os->frag_type == FRAG_TYPE_PFRAMES &&
1933+ os->packets_written) {
1934+ uint8_t *data;
1935+ int size;
1936+
1937+ av_assert0(os->parser);
1938+ av_parser_parse2(os->parser, os->parser_avctx,
1939+ &data, &size, pkt->data, pkt->size,
1940+ pkt->pts, pkt->dts, pkt->pos);
1941+
1942+ if ((os->parser->pict_type == AV_PICTURE_TYPE_P &&
1943+ st->codecpar->video_delay &&
1944+ !(os->last_flags & AV_PKT_FLAG_KEY)) ||
1945+ pkt->flags & AV_PKT_FLAG_KEY) {
1946+ ret = av_write_frame(os->ctx, NULL);
1947+ if (ret < 0)
1948+ return ret;
1949+
1950+ if (!os->availability_time_offset) {
1951+ int64_t frag_duration = av_rescale_q(os->total_pkt_duration, st->time_base,
1952+ AV_TIME_BASE_Q);
1953+ os->availability_time_offset = ((double) os->seg_duration -
1954+ frag_duration) / AV_TIME_BASE;
1955+ }
1956+ }
1957+ }
1958+
19001959 if ((ret = ff_write_chained(os->ctx, 0, pkt, s, 0)) < 0)
19011960 return ret;
19021961
1962+ os->packets_written++;
1963+ os->total_pkt_size += pkt->size;
1964+ os->total_pkt_duration += pkt->duration;
1965+ os->last_flags = pkt->flags;
1966+
19031967 if (!os->init_range_length)
19041968 flush_init_segment(s, os);
19051969
@@ -2026,6 +2090,7 @@ static const AVOption options[] = { · 120 unmodified lines
20262090 { "none", "one fragment per segment", 0, AV_OPT_TYPE_CONST, {.i64 = FRAG_TYPE_NONE }, 0, UINT_MAX, E, "frag_type"},
20272091 { "every_frame", "fragment at every frame", 0, AV_OPT_TYPE_CONST, {.i64 = FRAG_TYPE_EVERY_FRAME }, 0, UINT_MAX, E, "frag_type"},
20282092 { "duration", "fragment at specific time intervals", 0, AV_OPT_TYPE_CONST, {.i64 = FRAG_TYPE_DURATION }, 0, UINT_MAX, E, "frag_type"},
2093+ { "pframes", "fragment at keyframes and following P-Frame reordering (Video only, experimental)", 0, AV_OPT_TYPE_CONST, {.i64 = FRAG_TYPE_PFRAMES }, 0, UINT_MAX, E, "frag_type"},
20292094 { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
20302095 { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },
20312096 { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E },

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