146 if (!strcmp(str,
"mp4a") || !strcmp(str,
"mp4v")) {
155 if (tag ==
MKTAG(
'm',
'p',
'4',
'a')) {
162 }
else if (tag ==
MKTAG(
'm',
'p',
'4',
'v')) {
166 }
else if (!strcmp(str,
"avc1")) {
172 if (extradata[0] != 1) {
184 if (extradata_size >= 4)
186 extradata[1], extradata[2], extradata[3]);
216 int ret, range_length;
234 for (i = 0; i < c->
nb_as; i++)
260 int i, start_index = 0, start_number = 1;
268 avio_printf(out,
"\t\t\t\t<SegmentTemplate timescale=\"%d\" ", timescale);
279 if (i == start_index || seg->
time != cur_time) {
280 cur_time = seg->
time;
292 cur_time += (1 + repeat) * seg->
duration;
294 avio_printf(out,
"\t\t\t\t\t</SegmentTimeline>\n");
314 avio_printf(out,
"\t\t\t\t\t<SegmentURL media=\"%s\" />\n", seg->
file);
321 int outlen = strlen(str)*3/2 + 6;
326 for (; *str; str++) {
327 if (pos + 6 > outlen) {
329 outlen = 2 * outlen + 6;
338 memcpy(&out[pos],
"&", 5);
340 }
else if (*str ==
'<') {
341 memcpy(&out[pos],
"<", 4);
343 }
else if (*str ==
'>') {
344 memcpy(&out[pos],
">", 4);
346 }
else if (*str ==
'\'') {
347 memcpy(&out[pos],
"'", 6);
349 }
else if (*str ==
'\"') {
350 memcpy(&out[pos],
""", 6);
364 int minutes = seconds / 60;
365 int hours = minutes / 60;
371 if (hours || minutes)
378 time_t t = time(
NULL);
379 struct tm *ptm, tmbuf;
382 if (!strftime(buf, size,
"%Y-%m-%dT%H:%M:%SZ", ptm))
394 avio_printf(out,
"\t\t<AdaptationSet id=\"%s\" contentType=\"%s\" segmentAlignment=\"true\" bitstreamSwitching=\"true\"",
405 avio_printf(out,
"\t\t\t<Role schemeIdUri=\"urn:mpeg:dash:role:2011\" value=\"%s\"/>\n", role->
value);
410 if (os->
as_idx - 1 != as_index)
415 avio_printf(out,
"\t\t\t<Representation id=\"%d\" mimeType=\"video/%s\" codecs=\"%s\"%s width=\"%d\" height=\"%d\"",
421 avio_printf(out,
"\t\t\t<Representation id=\"%d\" mimeType=\"audio/%s\" codecs=\"%s\"%s audioSamplingRate=\"%d\">\n",
423 avio_printf(out,
"\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n",
445 memset(*as, 0,
sizeof(**as));
446 (*as)->media_type =
type;
458 av_log(s,
AV_LOG_ERROR,
"Codec type of stream %d doesn't match AdaptationSet's media type\n", i);
473 enum { new_set, parse_id, parsing_streams }
state;
508 state = parsing_streams;
509 }
else if (
state == parsing_streams) {
511 char idx_str[8], *end_str;
513 n = strcspn(p,
" ,");
514 snprintf(idx_str,
sizeof(idx_str),
"%.*s", n, p);
532 i = strtol(idx_str, &end_str, 10);
533 if (idx_str == end_str || i < 0 || i >= s->
nb_streams) {
572 char temp_filename[1024];
575 int use_rename = proto && !strcmp(proto,
"file");
576 static unsigned int warned_non_file = 0;
579 if (!use_rename && !warned_non_file++)
580 av_log(s,
AV_LOG_ERROR,
"Cannot use rename on non file protocol, this may lead to races and temporary partial files\n");
582 snprintf(temp_filename,
sizeof(temp_filename), use_rename ?
"%s.tmp" :
"%s", s->
filename);
588 avio_printf(out,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
589 avio_printf(out,
"<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
590 "\txmlns=\"urn:mpeg:dash:schema:mpd:2011\"\n"
591 "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
592 "\txsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\"\n"
593 "\tprofiles=\"urn:mpeg:dash:profile:isoff-live:2011\"\n"
594 "\ttype=\"%s\"\n",
final ?
"static" :
"dynamic");
596 avio_printf(out,
"\tmediaPresentationDuration=\"");
604 avio_printf(out,
"\tminimumUpdatePeriod=\"PT%"PRId64
"S\"\n", update_period);
613 avio_printf(out,
"\tpublishTime=\"%s\"\n", now_str);
626 avio_printf(out,
"\t\t<Title>%s</Title>\n", escaped);
641 avio_printf(out,
"\t<Period id=\"0\" start=\"PT0.0S\">\n");
644 for (i = 0; i < c->
nb_as; i++) {
670 snprintf(valuestr,
sizeof(valuestr),
"%"PRId64, value);
689 ptr = strrchr(c->
dirname,
'/');
691 av_strlcpy(basename, &ptr[1],
sizeof(basename));
698 ptr = strrchr(basename,
'.');
724 av_log(s, level,
"No bit rate set for stream %d\n", i);
783 av_dict_set(&opts,
"movflags",
"frag_custom+dash+delay_moov", 0);
786 dict_set_int(&opts,
"cluster_size_limit", 5 * 1024 * 1024, 0);
794 av_log(s,
AV_LOG_VERBOSE,
"Representation %d init segment will be written to: %s\n", i, filename);
810 if (avg_frame_rate.
num > 0) {
854 int64_t start_pos, int64_t range_length,
855 int64_t index_length)
897 int64_t pos,
int *index_length)
906 if (
avio_seek(pb, pos, SEEK_SET) != pos) {
916 *index_length =
AV_RB32(&buf[0]);
948 int use_rename = proto && !strcmp(proto,
"file");
950 int cur_flush_segment_index = 0;
956 char filename[1024] =
"", full_path[1024], temp_path[1024];
957 int range_length, index_length = 0;
965 if (stream >= 0 && i != stream) {
981 snprintf(temp_path,
sizeof(temp_path), use_rename ?
"%s.tmp" :
"%s", full_path);
1014 " bandwidth=\"%d\"", os->
bit_rate);
1020 os->
pos += range_length;
1031 for (j = 0; j <
remove; j++) {
1032 char filename[1024];
1097 "Segment durations differ too much, enable use_timeline "
1098 "and use_template, or keep a stricter keyframe interval\n");
1142 char filename[1024];
1178 #define OFFSET(x) offsetof(DASHContext, x)
1179 #define E AV_OPT_FLAG_ENCODING_PARAM
1181 {
"adaptation_sets",
"Adaptation sets. Syntax: id=0,streams=0,1,2 id=1,streams=3,4 and so on",
OFFSET(adaptation_sets),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
AV_OPT_FLAG_ENCODING_PARAM },
1182 {
"window_size",
"number of segments kept in the manifest",
OFFSET(window_size),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
E },
1183 {
"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 },
1184 {
"min_seg_duration",
"minimum segment duration (in microseconds)",
OFFSET(min_seg_duration),
AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX,
E },
1185 {
"remove_at_exit",
"remove all segments when finished",
OFFSET(remove_at_exit),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
E },
1186 {
"use_template",
"Use SegmentTemplate instead of SegmentList",
OFFSET(use_template),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
E },
1187 {
"use_timeline",
"Use SegmentTimeline in SegmentTemplate",
OFFSET(use_timeline),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
E },
1188 {
"single_file",
"Store all segments in one file, accessed using byte ranges",
OFFSET(single_file),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
E },
1189 {
"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 },
1190 {
"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 },
1191 {
"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 },
1192 {
"utc_timing_url",
"URL of the page that will return the UTC timestamp in ISO format",
OFFSET(utc_timing_url),
AV_OPT_TYPE_STRING, { 0 }, 0, 0,
E },
1216 .priv_class = &dash_class,
AVRational max_frame_rate
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
static int dict_copy_entry(AVDictionary **dst, const AVDictionary *src, const char *key)
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
static int dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
static int parse_adaptation_sets(AVFormatContext *s)
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
static const AVOption options[]
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
static av_cold int init(AVCodecContext *avctx)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
#define AVIO_FLAG_READ
read-only
#define AVIO_FLAG_WRITE
write-only
Convenience header that includes libavutil's core.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
static void output_segment_list(OutputStream *os, AVIOContext *out, DASHContext *c)
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
int strict_std_compliance
Allow non-standard and experimental extension.
static int dash_init(AVFormatContext *s)
This struct describes the properties of an encoded stream.
int avpriv_io_move(const char *url_src, const char *url_dst)
Move or rename a resource.
static struct codec_string codecs[]
static int write_adaptation_set(AVFormatContext *s, AVIOContext *out, int as_index)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static int64_t start_time
miscellaneous OS support macros and functions.
const AVCodecTag ff_codec_movvideo_tags[]
static int add_adaptation_set(AVFormatContext *s, AdaptationSet **as, enum AVMediaType type)
static av_cold int end(AVCodecContext *avctx)
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
AVStream ** streams
A list of all streams in the file.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
int flags
Flags modifying the (de)muxer behaviour.
#define AV_LOG_VERBOSE
Detailed information.
AVRational min_frame_rate
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
uint64_t availability_start_time
static void format_date_now(char *buf, int size)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
struct AVOutputFormat * oformat
The output container format.
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
static void write_time(AVIOContext *out, int64_t time)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
static int write_manifest(AVFormatContext *s, int final)
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVDictionary * metadata
Metadata that applies to the whole file.
static int dash_write_packet(AVFormatContext *s, AVPacket *pkt)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
enum AVMediaType codec_type
General type of the encoded data.
simple assert() macros that are a bit more flexible than ISO C assert().
static int dash_check_bitstream(struct AVFormatContext *s, const AVPacket *avpkt)
static int flush_init_segment(AVFormatContext *s, OutputStream *os)
AVRational avg_frame_rate
Average framerate.
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
const AVCodecTag ff_mp4_obj_type[]
static struct tm * gmtime_r(const time_t *clock, struct tm *result)
int flags
A combination of AV_PKT_FLAG values.
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
int extradata_size
Size of the extradata content in bytes.
AVOutputFormat ff_dash_muxer
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
AVBSFContext ** bsfcs
bitstream filters to run on stream
const char * media_seg_name
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
char filename[1024]
input or output filename
#define AV_TIME_BASE
Internal time base represented as integer.
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
GLsizei GLboolean const GLfloat * value
static int write_trailer(AVFormatContext *s1)
enum AVMediaType media_type
int avoid_negative_ts
Avoid negative timestamps during muxing.
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Usually treated as AVMEDIA_TYPE_DATA.
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
void ff_dash_fill_tmpl_params(char *dst, size_t buffer_size, const char *template, int rep_id, int number, int bit_rate, int64_t time)
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
AVStreamInternal * internal
An opaque field for libavformat internal usage.
const AVCodecTag ff_codec_movaudio_tags[]
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
AVIOContext * pb
I/O context.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
static void set_codec_str(AVFormatContext *s, AVCodecParameters *par, char *str, int size)
Describe the class of an AVClass context structure.
const char * init_seg_name
Rational number (pair of numerator and denominator).
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
static int adaptation_set_add_stream(AVFormatContext *s, int as_idx, int i)
static void find_index_range(AVFormatContext *s, const char *full_path, int64_t pos, int *index_length)
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
static int flush_dynbuf(OutputStream *os, int *range_length)
int sample_rate
Audio only.
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Utilties for rational number calculation.
const char * single_file_name
static void write_styp(AVIOContext *pb)
static int dash_write_header(AVFormatContext *s)
void * priv_data
Format private data.
const char * utc_timing_url
static int dash_write_trailer(AVFormatContext *s)
static void write_header(FFV1Context *f)
static void dash_free(AVFormatContext *s)
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
void avio_wb32(AVIOContext *s, unsigned int val)
static int update_stream_extradata(AVFormatContext *s, OutputStream *os, AVCodecParameters *par)
unbuffered private I/O API
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
AVCodecParameters * codecpar
static char * xmlescape(const char *str)
static int dash_flush(AVFormatContext *s, int final, int stream)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
#define MKTAG(a, b, c, d)
unsigned int av_codec_get_tag(const struct AVCodecTag *const *tags, enum AVCodecID id)
Get the codec tag for the given codec id id.
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
This structure stores compressed data.
void(* io_close)(struct AVFormatContext *s, AVIOContext *pb)
A callback for closing the streams opened with AVFormatContext.io_open().
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
static const AVClass dash_class
#define AV_NOPTS_VALUE
Undefined timestamp value.
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2
static int add_segment(OutputStream *os, const char *file, int64_t time, int duration, int64_t start_pos, int64_t range_length, int64_t index_length)