128 if (!strcmp(str,
"mp4a") || !strcmp(str,
"mp4v")) {
137 if (tag ==
MKTAG(
'm',
'p',
'4',
'a')) {
144 }
else if (tag ==
MKTAG(
'm',
'p',
'4',
'v')) {
148 }
else if (!strcmp(str,
"avc1")) {
154 if (extradata[0] != 1) {
166 if (extradata_size >= 4)
168 extradata[1], extradata[2], extradata[3]);
198 int i, start_index = 0, start_number = 1;
206 avio_printf(out,
"\t\t\t\t<SegmentTemplate timescale=\"%d\" ", timescale);
217 if (i == start_index || seg->
time != cur_time) {
218 cur_time = seg->
time;
230 cur_time += (1 + repeat) * seg->
duration;
232 avio_printf(out,
"\t\t\t\t\t</SegmentTimeline>\n");
252 avio_printf(out,
"\t\t\t\t\t<SegmentURL media=\"%s\" />\n", seg->
file);
259 size_t format_tag_size,
const char **ptr) {
260 const char *next_ptr;
266 }
else if (
av_strstart(identifier,
"$RepresentationID$", &next_ptr)) {
270 av_strlcpy(format_tag,
"%d", format_tag_size);
275 else if (
av_strstart(identifier,
"$Bandwidth", &next_ptr))
277 else if (
av_strstart(identifier,
"$Time", &next_ptr))
286 if (next_ptr[0] ==
'$') {
287 snprintf(format_tag, format_tag_size,
"%%%s", number_format);
290 const char *width_ptr;
296 snprintf(format_tag, format_tag_size,
"%s%c%s",
"%0", width_ptr[0], number_format);
300 "closing '$' character or a format-string like '%%0[width]d', "
301 "where width must be a single digit\n", next_ptr);
311 const char *
template,
int rep_id,
312 int number,
int bit_rate,
315 const char *t_cur =
template;
316 while (dst_pos < buffer_size - 1 && *t_cur) {
320 const char *t_next = strchr(t_cur,
'$');
322 int num_copy_bytes =
FFMIN(t_next - t_cur, buffer_size - dst_pos - 1);
323 av_strlcpy(&dst[dst_pos], t_cur, num_copy_bytes + 1);
325 dst_pos += num_copy_bytes;
328 av_strlcpy(&dst[dst_pos], t_cur, buffer_size - dst_pos);
332 if (dst_pos >= buffer_size - 1 || !*t_cur)
343 n =
snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, rep_id);
346 n =
snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, number);
349 n =
snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, bit_rate);
352 n =
snprintf(&dst[dst_pos], buffer_size - dst_pos, format_tag, time);
366 dst_pos +=
FFMIN(n, buffer_size - dst_pos - 1);
372 int outlen = strlen(str)*3/2 + 6;
377 for (; *str; str++) {
378 if (pos + 6 > outlen) {
380 outlen = 2 * outlen + 6;
389 memcpy(&out[pos],
"&", 5);
391 }
else if (*str ==
'<') {
392 memcpy(&out[pos],
"<", 4);
394 }
else if (*str ==
'>') {
395 memcpy(&out[pos],
">", 4);
397 }
else if (*str ==
'\'') {
398 memcpy(&out[pos],
"'", 6);
400 }
else if (*str ==
'\"') {
401 memcpy(&out[pos],
""", 6);
415 int minutes = seconds / 60;
416 int hours = minutes / 60;
422 if (hours || minutes)
429 time_t t = time(
NULL);
430 struct tm *ptm, tmbuf;
433 if (!strftime(buf, size,
"%Y-%m-%dT%H:%M:%S", ptm))
442 char temp_filename[1024];
452 avio_printf(out,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
453 avio_printf(out,
"<MPD xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
454 "\txmlns=\"urn:mpeg:dash:schema:mpd:2011\"\n"
455 "\txmlns:xlink=\"http://www.w3.org/1999/xlink\"\n"
456 "\txsi:schemaLocation=\"urn:mpeg:DASH:schema:MPD:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd\"\n"
457 "\tprofiles=\"urn:mpeg:dash:profile:isoff-live:2011\"\n"
458 "\ttype=\"%s\"\n",
final ?
"static" :
"dynamic");
460 avio_printf(out,
"\tmediaPresentationDuration=\"");
468 avio_printf(out,
"\tminimumUpdatePeriod=\"PT%"PRId64
"S\"\n", update_period);
477 avio_printf(out,
"\tpublishTime=\"%s\"\n", now_str);
490 avio_printf(out,
"\t\t<Title>%s</Title>\n", escaped);
506 avio_printf(out,
"\t\t<AdaptationSet contentType=\"video\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">\n");
521 avio_printf(out,
"\t\t<AdaptationSet contentType=\"audio\" segmentAlignment=\"true\" bitstreamSwitching=\"true\">\n");
530 avio_printf(out,
"\t\t\t\t<AudioChannelConfiguration schemeIdUri=\"urn:mpeg:dash:23003:3:audio_channel_configuration:2011\" value=\"%d\" />\n", st->
codec->
channels);
557 ptr = strrchr(c->
dirname,
'/');
559 av_strlcpy(basename, &ptr[1],
sizeof(basename));
566 ptr = strrchr(basename,
'.');
598 av_log(s, level,
"No bit rate set for stream %d\n", i);
643 av_dict_set(&opts,
"movflags",
"frag_custom+dash+delay_moov", 0);
651 av_log(s,
AV_LOG_VERBOSE,
"Representation %d init segment will be written to: %s\n", i, filename);
686 int64_t start_pos, int64_t range_length,
687 int64_t index_length)
729 int64_t pos,
int *index_length)
748 *index_length =
AV_RB32(&buf[0]);
778 int cur_flush_segment_index = 0;
784 char filename[1024] =
"", full_path[1024], temp_path[1024];
786 int range_length, index_length = 0;
794 if (stream >= 0 && i != stream) {
817 snprintf(temp_path,
sizeof(temp_path),
"%s.tmp", full_path);
836 ret =
ff_rename(temp_path, full_path, s);
852 for (j = 0; j <
remove; j++) {
919 "Segment durations differ too much, enable use_timeline "
920 "and use_template, or keep a stricter keyframe interval\n");
978 #define OFFSET(x) offsetof(DASHContext, x)
979 #define E AV_OPT_FLAG_ENCODING_PARAM
981 {
"window_size",
"number of segments kept in the manifest",
OFFSET(window_size),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
E },
982 {
"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 },
983 {
"min_seg_duration",
"minimum segment duration (in microseconds)",
OFFSET(min_seg_duration),
AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX,
E },
984 {
"remove_at_exit",
"remove all segments when finished",
OFFSET(remove_at_exit),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
E },
985 {
"use_template",
"Use SegmentTemplate instead of SegmentList",
OFFSET(use_template),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
E },
986 {
"use_timeline",
"Use SegmentTimeline in SegmentTemplate",
OFFSET(use_timeline),
AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1,
E },
987 {
"single_file",
"Store all segments in one file, accessed using byte ranges",
OFFSET(single_file),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
E },
988 {
"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 },
989 {
"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 },
990 {
"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 },
int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
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 const AVOption options[]
int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
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
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
Write size bytes from buf to the resource accessed by h.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
#define AVIO_FLAG_READ
read-only
#define AVIO_FLAG_WRITE
write-only
char availability_start_time[100]
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 avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
int strict_std_compliance
Allow non-standard and experimental extension.
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[]
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
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 av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
#define AV_LOG_VERBOSE
Detailed information.
static void set_codec_str(AVFormatContext *s, AVCodecContext *codec, char *str, int size)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
static void format_date_now(char *buf, int size)
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
struct AVOutputFormat * oformat
The output container format.
#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)
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
#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. ...
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
int rc_max_rate
maximum bitrate
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 2 timestamps each in its own timebases.
AVOutputFormat ff_dash_muxer
AVCodecContext * codec
Codec context associated with this stream.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
const char * media_seg_name
int bit_rate
the average bitrate
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
char filename[1024]
input or output filename
static DASHTmplId dash_read_tmpl_id(const char *identifier, char *format_tag, size_t format_tag_size, const char **ptr)
#define AV_TIME_BASE
Internal time base represented as integer.
int width
picture width / height.
static int write_trailer(AVFormatContext *s1)
static int update_stream_extradata(AVFormatContext *s, OutputStream *os, AVCodecContext *codec)
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...
static int dash_write(void *opaque, uint8_t *buf, int buf_size)
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
enum AVMediaType codec_type
const AVCodecTag ff_codec_movaudio_tags[]
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
int sample_rate
samples per second
AVIOContext * pb
I/O context.
main external API structure.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Describe the class of an AVClass context structure.
const char * init_seg_name
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
static void find_index_range(AVFormatContext *s, const char *full_path, int64_t pos, int *index_length)
int ffurl_close(URLContext *h)
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h...
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
int ffurl_open(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create an URLContext for accessing to the resource indicated by url, and open it. ...
const char * single_file_name
static void write_styp(AVIOContext *pb)
static int dash_write_header(AVFormatContext *s)
int channels
number of audio channels
#define FF_COMPLIANCE_STRICT
Strictly conform to all the things in the spec no matter what consequences.
void * priv_data
Format private data.
static int dash_write_trailer(AVFormatContext *s)
static void write_header(FFV1Context *f)
static void dash_free(AVFormatContext *s)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
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)
unbuffered private I/O API
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
static void dash_fill_tmpl_params(char *dst, size_t buffer_size, const char *template, int rep_id, int number, int bit_rate, int64_t time)
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.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
This structure stores compressed data.
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
int ffurl_read(URLContext *h, unsigned char *buf, int size)
Read up to size bytes from the resource accessed by h, and store the read bytes in buf...
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
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)