diff options
Diffstat (limited to 'libavformat/dashenc.c')
-rw-r--r-- | libavformat/dashenc.c | 215 |
1 files changed, 150 insertions, 65 deletions
diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c index ce018602dd..1f31968ab9 100644 --- a/libavformat/dashenc.c +++ b/libavformat/dashenc.c @@ -2,20 +2,20 @@ * MPEG-DASH ISO BMFF segmenter * Copyright (c) 2014 Martin Storsjo * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * FFmpeg is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * Libav is distributed in the hope that it will be useful, + * FFmpeg is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -24,10 +24,12 @@ #include <unistd.h> #endif +#include "libavutil/avassert.h" #include "libavutil/avstring.h" #include "libavutil/intreadwrite.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" +#include "libavutil/rational.h" #include "libavutil/time_internal.h" #include "avc.h" @@ -94,6 +96,8 @@ typedef struct DASHContext { const char *single_file_name; const char *init_seg_name; const char *media_seg_name; + AVRational min_frame_rate, max_frame_rate; + int ambiguous_frame_rate; } DASHContext; static int dash_write(void *opaque, uint8_t *buf, int buf_size) @@ -130,7 +134,7 @@ static void set_codec_str(AVFormatContext *s, AVCodecParameters *par, tags[0] = ff_mp4_obj_type; oti = av_codec_get_tag(tags, par->codec_id); if (oti) - av_strlcatf(str, size, ".%02"SCNx32, oti); + av_strlcatf(str, size, ".%02x", oti); else return; @@ -440,9 +444,15 @@ static int write_manifest(AVFormatContext *s, int final) AVIOContext *out; char temp_filename[1024]; int ret, i; + const char *proto = avio_find_protocol_name(s->filename); + int use_rename = proto && !strcmp(proto, "file"); + static unsigned int warned_non_file = 0; AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0); - snprintf(temp_filename, sizeof(temp_filename), "%s.tmp", s->filename); + if (!use_rename && !warned_non_file++) + av_log(s, AV_LOG_ERROR, "Cannot use rename on non file protocol, this may lead to races and temporary partial files\n"); + + snprintf(temp_filename, sizeof(temp_filename), use_rename ? "%s.tmp" : "%s", s->filename); ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL); if (ret < 0) { av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename); @@ -502,13 +512,23 @@ static int write_manifest(AVFormatContext *s, int final) } if (c->has_video) { - avio_printf(out, "\t\t<AdaptationSet contentType=\"video\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">\n"); + avio_printf(out, "\t\t<AdaptationSet contentType=\"video\" segmentAlignment=\"true\" bitstreamSwitching=\"true\""); + if (c->max_frame_rate.num && !c->ambiguous_frame_rate) + avio_printf(out, " %s=\"%d/%d\"", (av_cmp_q(c->min_frame_rate, c->max_frame_rate) < 0) ? "maxFrameRate" : "frameRate", c->max_frame_rate.num, c->max_frame_rate.den); + avio_printf(out, ">\n"); + for (i = 0; i < s->nb_streams; i++) { AVStream *st = s->streams[i]; OutputStream *os = &c->streams[i]; + if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) continue; - avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"video/mp4\" codecs=\"%s\"%s width=\"%d\" height=\"%d\">\n", i, os->codec_str, os->bandwidth_str, st->codecpar->width, st->codecpar->height); + + avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"video/mp4\" codecs=\"%s\"%s width=\"%d\" height=\"%d\"", i, os->codec_str, os->bandwidth_str, st->codecpar->width, st->codecpar->height); + if (st->avg_frame_rate.num) + avio_printf(out, " frameRate=\"%d/%d\"", st->avg_frame_rate.num, st->avg_frame_rate.den); + avio_printf(out, ">\n"); + output_segment_list(&c->streams[i], out, c); avio_printf(out, "\t\t\t</Representation>\n"); } @@ -519,8 +539,10 @@ static int write_manifest(AVFormatContext *s, int final) for (i = 0; i < s->nb_streams; i++) { AVStream *st = s->streams[i]; OutputStream *os = &c->streams[i]; + if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) continue; + avio_printf(out, "\t\t\t<Representation id=\"%d\" mimeType=\"audio/mp4\" codecs=\"%s\"%s audioSamplingRate=\"%d\">\n", i, os->codec_str, os->bandwidth_str, st->codecpar->sample_rate); avio_printf(out, "\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n", st->codecpar->channels); output_segment_list(&c->streams[i], out, c); @@ -532,10 +554,38 @@ static int write_manifest(AVFormatContext *s, int final) avio_printf(out, "</MPD>\n"); avio_flush(out); ff_format_io_close(s, &out); - return ff_rename(temp_filename, s->filename); + + if (use_rename) + return avpriv_io_move(temp_filename, s->filename); + + return 0; } -static int dash_write_header(AVFormatContext *s) +static int set_bitrate(AVFormatContext *s) +{ + DASHContext *c = s->priv_data; + int i; + + for (i = 0; i < s->nb_streams; i++) { + OutputStream *os = &c->streams[i]; + + os->bit_rate = s->streams[i]->codecpar->bit_rate; + if (os->bit_rate) { + snprintf(os->bandwidth_str, sizeof(os->bandwidth_str), + " bandwidth=\"%d\"", os->bit_rate); + } else { + int level = s->strict_std_compliance >= FF_COMPLIANCE_STRICT ? + AV_LOG_ERROR : AV_LOG_WARNING; + av_log(s, level, "No bit rate set for stream %d\n", i); + if (s->strict_std_compliance >= FF_COMPLIANCE_STRICT) + return AVERROR(EINVAL); + } + } + + return 0; +} + +static int dash_init(AVFormatContext *s) { DASHContext *c = s->priv_data; int ret = 0, i; @@ -547,6 +597,7 @@ static int dash_write_header(AVFormatContext *s) c->single_file = 1; if (c->single_file) c->use_template = 0; + c->ambiguous_frame_rate = 0; av_strlcpy(c->dirname, s->filename, sizeof(c->dirname)); ptr = strrchr(c->dirname, '/'); @@ -563,16 +614,16 @@ static int dash_write_header(AVFormatContext *s) *ptr = '\0'; oformat = av_guess_format("mp4", NULL, NULL); - if (!oformat) { - ret = AVERROR_MUXER_NOT_FOUND; - goto fail; - } + if (!oformat) + return AVERROR_MUXER_NOT_FOUND; c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams); - if (!c->streams) { - ret = AVERROR(ENOMEM); - goto fail; - } + if (!c->streams) + return AVERROR(ENOMEM); + + ret = set_bitrate(s); + if (ret < 0) + return ret; for (i = 0; i < s->nb_streams; i++) { OutputStream *os = &c->streams[i]; @@ -581,25 +632,9 @@ static int dash_write_header(AVFormatContext *s) AVDictionary *opts = NULL; char filename[1024]; - os->bit_rate = s->streams[i]->codecpar->bit_rate; - if (os->bit_rate) { - snprintf(os->bandwidth_str, sizeof(os->bandwidth_str), - " bandwidth=\"%d\"", os->bit_rate); - } else { - int level = s->strict_std_compliance >= FF_COMPLIANCE_STRICT ? - AV_LOG_ERROR : AV_LOG_WARNING; - av_log(s, level, "No bit rate set for stream %d\n", i); - if (s->strict_std_compliance >= FF_COMPLIANCE_STRICT) { - ret = AVERROR(EINVAL); - goto fail; - } - } - ctx = avformat_alloc_context(); - if (!ctx) { - ret = AVERROR(ENOMEM); - goto fail; - } + if (!ctx) + return AVERROR(ENOMEM); os->ctx = ctx; ctx->oformat = oformat; ctx->interrupt_callback = s->interrupt_callback; @@ -607,20 +642,17 @@ static int dash_write_header(AVFormatContext *s) ctx->io_close = s->io_close; ctx->io_open = s->io_open; - if (!(st = avformat_new_stream(ctx, NULL))) { - ret = AVERROR(ENOMEM); - goto fail; - } + if (!(st = avformat_new_stream(ctx, NULL))) + return AVERROR(ENOMEM); avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar); st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio; st->time_base = s->streams[i]->time_base; ctx->avoid_negative_ts = s->avoid_negative_ts; + ctx->flags = s->flags; ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), AVIO_FLAG_WRITE, os, NULL, dash_write, NULL); - if (!ctx->pb) { - ret = AVERROR(ENOMEM); - goto fail; - } + if (!ctx->pb) + return AVERROR(ENOMEM); if (c->single_file) { if (c->single_file_name) @@ -633,13 +665,12 @@ static int dash_write_header(AVFormatContext *s) snprintf(filename, sizeof(filename), "%s%s", c->dirname, os->initfile); ret = s->io_open(s, &os->out, filename, AVIO_FLAG_WRITE, NULL); if (ret < 0) - goto fail; + return ret; os->init_start_pos = 0; av_dict_set(&opts, "movflags", "frag_custom+dash+delay_moov", 0); - if ((ret = avformat_write_header(ctx, &opts)) < 0) { - goto fail; - } + if ((ret = avformat_init_output(ctx, &opts)) < 0) + return ret; os->ctx_inited = 1; avio_flush(ctx->pb); av_dict_free(&opts); @@ -651,10 +682,20 @@ static int dash_write_header(AVFormatContext *s) // already before being handed to this muxer, so we don't have mismatches // between the MPD and the actual segments. s->avoid_negative_ts = ctx->avoid_negative_ts; - if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) + if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { + AVRational avg_frame_rate = s->streams[i]->avg_frame_rate; + if (avg_frame_rate.num > 0) { + if (av_cmp_q(avg_frame_rate, c->min_frame_rate) < 0) + c->min_frame_rate = avg_frame_rate; + if (av_cmp_q(c->max_frame_rate, avg_frame_rate) < 0) + c->max_frame_rate = avg_frame_rate; + } else { + c->ambiguous_frame_rate = 1; + } c->has_video = 1; - else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) + } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { c->has_audio = 1; + } set_codec_str(s, st->codecpar, os->codec_str, sizeof(os->codec_str)); os->first_pts = AV_NOPTS_VALUE; @@ -665,15 +706,25 @@ static int dash_write_header(AVFormatContext *s) if (!c->has_video && c->min_seg_duration <= 0) { av_log(s, AV_LOG_WARNING, "no video stream and no min seg duration set\n"); - ret = AVERROR(EINVAL); + return AVERROR(EINVAL); + } + return 0; +} + +static int dash_write_header(AVFormatContext *s) +{ + DASHContext *c = s->priv_data; + int i, ret; + for (i = 0; i < s->nb_streams; i++) { + OutputStream *os = &c->streams[i]; + if ((ret = avformat_write_header(os->ctx, NULL)) < 0) { + dash_free(s); + return ret; + } } ret = write_manifest(s, 0); if (!ret) av_log(s, AV_LOG_VERBOSE, "Manifest written to: %s\n", s->filename); - -fail: - if (ret) - dash_free(s); return ret; } @@ -771,6 +822,10 @@ static int dash_flush(AVFormatContext *s, int final, int stream) { DASHContext *c = s->priv_data; int i, ret = 0; + + const char *proto = avio_find_protocol_name(s->filename); + int use_rename = proto && !strcmp(proto, "file"); + int cur_flush_segment_index = 0; if (stream >= 0) cur_flush_segment_index = c->streams[stream].segment_index; @@ -808,7 +863,7 @@ static int dash_flush(AVFormatContext *s, int final, int stream) if (!c->single_file) { dash_fill_tmpl_params(filename, sizeof(filename), c->media_seg_name, i, os->segment_index, os->bit_rate, os->start_pts); snprintf(full_path, sizeof(full_path), "%s%s", c->dirname, filename); - snprintf(temp_path, sizeof(temp_path), "%s.tmp", full_path); + snprintf(temp_path, sizeof(temp_path), use_rename ? "%s.tmp" : "%s", full_path); ret = s->io_open(s, &os->out, temp_path, AVIO_FLAG_WRITE, NULL); if (ret < 0) break; @@ -826,9 +881,12 @@ static int dash_flush(AVFormatContext *s, int final, int stream) find_index_range(s, full_path, start_pos, &index_length); } else { ff_format_io_close(s, &os->out); - ret = ff_rename(temp_path, full_path); - if (ret < 0) - break; + + if (use_rename) { + ret = avpriv_io_move(temp_path, full_path); + if (ret < 0) + break; + } } add_segment(os, filename, os->start_pts, os->max_pts - os->start_pts, start_pos, range_length, index_length); av_log(s, AV_LOG_VERBOSE, "Representation %d media segment %d written to: %s\n", i, os->segment_index, full_path); @@ -932,13 +990,15 @@ static int dash_write_packet(AVFormatContext *s, AVPacket *pkt) else os->max_pts = FFMAX(os->max_pts, pkt->pts + pkt->duration); os->packets_written++; - return ff_write_chained(os->ctx, 0, pkt, s); + return ff_write_chained(os->ctx, 0, pkt, s, 0); } static int dash_write_trailer(AVFormatContext *s) { DASHContext *c = s->priv_data; + set_bitrate(s); + if (s->nb_streams > 0) { OutputStream *os = &c->streams[0]; // If no segments have been written so far, try to do a crude @@ -964,20 +1024,42 @@ static int dash_write_trailer(AVFormatContext *s) unlink(s->filename); } - dash_free(s); return 0; } +static int dash_check_bitstream(struct AVFormatContext *s, const AVPacket *avpkt) +{ + DASHContext *c = s->priv_data; + OutputStream *os = &c->streams[avpkt->stream_index]; + AVFormatContext *oc = os->ctx; + if (oc->oformat->check_bitstream) { + int ret; + AVPacket pkt = *avpkt; + pkt.stream_index = 0; + ret = oc->oformat->check_bitstream(oc, &pkt); + if (ret == 1) { + AVStream *st = s->streams[avpkt->stream_index]; + AVStream *ost = oc->streams[0]; + st->internal->bsfcs = ost->internal->bsfcs; + st->internal->nb_bsfcs = ost->internal->nb_bsfcs; + ost->internal->bsfcs = NULL; + ost->internal->nb_bsfcs = 0; + } + return ret; + } + return 1; +} + #define OFFSET(x) offsetof(DASHContext, x) #define E AV_OPT_FLAG_ENCODING_PARAM static const AVOption options[] = { { "window_size", "number of segments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E }, { "extra_window_size", "number of segments kept outside of the manifest before removing from disk", OFFSET(extra_window_size), AV_OPT_TYPE_INT, { .i64 = 5 }, 0, INT_MAX, E }, { "min_seg_duration", "minimum segment duration (in microseconds)", OFFSET(min_seg_duration), AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX, E }, - { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E }, - { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E }, - { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, E }, - { "single_file", "Store all segments in one file, accessed using byte ranges", OFFSET(single_file), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E }, + { "remove_at_exit", "remove all segments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, + { "use_template", "Use SegmentTemplate instead of SegmentList", OFFSET(use_template), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E }, + { "use_timeline", "Use SegmentTimeline in SegmentTemplate", OFFSET(use_timeline), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, E }, + { "single_file", "Store all segments in one file, accessed using byte ranges", OFFSET(single_file), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E }, { "single_file_name", "DASH-templated name to be used for baseURL. Implies storing all segments in one file, accessed using byte ranges", OFFSET(single_file_name), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E }, { "init_seg_name", "DASH-templated name to used for the initialization segment", OFFSET(init_seg_name), AV_OPT_TYPE_STRING, {.str = "init-stream$RepresentationID$.m4s"}, 0, 0, E }, { "media_seg_name", "DASH-templated name to used for the media segments", OFFSET(media_seg_name), AV_OPT_TYPE_STRING, {.str = "chunk-stream$RepresentationID$-$Number%05d$.m4s"}, 0, 0, E }, @@ -998,9 +1080,12 @@ AVOutputFormat ff_dash_muxer = { .audio_codec = AV_CODEC_ID_AAC, .video_codec = AV_CODEC_ID_H264, .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE | AVFMT_TS_NEGATIVE, + .init = dash_init, .write_header = dash_write_header, .write_packet = dash_write_packet, .write_trailer = dash_write_trailer, + .deinit = dash_free, .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 }, + .check_bitstream = dash_check_bitstream, .priv_class = &dash_class, }; |