65 #define SEGMENT_LIST_FLAG_CACHE 1
66 #define SEGMENT_LIST_FLAG_LIVE 2
123 int needs_quoting = !!str[strcspn(str,
"\",\n\r")];
128 for (; *str; str++) {
187 struct tm *tm, tmpbuf;
270 double max_duration = 0;
283 avio_printf(seg->
list_pb,
"#EXT-X-TARGETDURATION:%"PRId64
"\n", (int64_t)ceil(max_duration));
314 "Error writing list entry '%s' in list file\n", list_entry->
filename);
349 memcpy(entry, &seg->
cur_entry,
sizeof(*entry));
387 static int parse_times(
void *log_ctx, int64_t **times,
int *nb_times,
388 const char *times_str)
393 char *saveptr =
NULL;
398 #define FAIL(err) ret = err; goto end
401 for (p = times_str1; *p; p++)
412 for (i = 0; i < *nb_times; i++) {
414 char *tstr =
av_strtok(p,
",", &saveptr);
417 if (!tstr || !tstr[0]) {
426 "Invalid time duration specification '%s' in times list %s\n", tstr, times_str);
432 if (i && (*times)[i-1] > (*times)[i]) {
434 "Specified time %f is greater than the following time %f\n",
435 (
float)((*times)[i])/1000000, (
float)((*times)[i-1])/1000000);
446 const char *frames_str)
450 char *frames_str1 =
av_strdup(frames_str);
451 char *saveptr =
NULL;
456 #define FAIL(err) ret = err; goto end
459 for (p = frames_str1; *p; p++)
470 for (i = 0; i < *nb_frames; i++) {
473 char *fstr =
av_strtok(p,
",", &saveptr);
481 f = strtol(fstr, &tailptr, 10);
482 if (*tailptr || f <= 0 || f >= INT_MAX) {
484 "Invalid argument '%s', must be a positive integer <= INT64_MAX\n",
491 if (i && (*frames)[i-1] > (*frames)[i]) {
493 "Specified frame %d is greater than the following frame %d\n",
494 (*frames)[i], (*frames)[i-1]);
506 int buf_size = 32768;
533 static const enum AVMediaType type_priority_list[] = {
543 type_index_map[i] = -1;
548 if ((
unsigned)type < AVMEDIA_TYPE_NB && type_index_map[type] == -1
551 type_index_map[
type] = i;
555 type = type_priority_list[i];
607 "segment_time, segment_times, and segment_frames options "
608 "are mutually exclusive, select just one of them\n");
624 "Invalid time duration specification '%s' for segment_time option\n",
697 "Some of the provided format options in '%s' are not recognized\n", seg->
format_options_str);
744 int64_t end_pts = INT64_MAX,
offset;
745 int start_frame = INT_MAX;
763 time_t sec = avgt / 1000000;
765 usecs = (int64_t)(ti.tm_hour * 3600 + ti.tm_min * 60 + ti.tm_sec) * 1000000 + (avgt % 1000000);
766 wrapped_val = usecs % seg->
time;
767 if (seg->
last_cut != usecs && wrapped_val < seg->last_val) {
777 av_dlog(s,
"packet stream:%d pts:%s pts_time:%s duration_time:%s is_key:%d frame:%d\n",
813 av_log(s,
AV_LOG_VERBOSE,
"segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
818 av_log(s,
AV_LOG_DEBUG,
"stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
891 #define OFFSET(x) offsetof(SegmentContext, x)
892 #define E AV_OPT_FLAG_ENCODING_PARAM
894 {
"reference_stream",
"set reference stream",
OFFSET(reference_stream_specifier),
AV_OPT_TYPE_STRING, {.str =
"auto"}, CHAR_MIN, CHAR_MAX,
E },
896 {
"segment_format_options",
"set list of options for the container format used for the segments",
OFFSET(format_options_str),
AV_OPT_TYPE_STRING, {.str =
NULL}, 0, 0,
E },
898 {
"segment_header_filename",
"write a single file containing the header",
OFFSET(header_filename),
AV_OPT_TYPE_STRING, {.str =
NULL}, 0, 0,
E },
904 {
"segment_list_size",
"set the maximum number of playlist entries",
OFFSET(list_size),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
914 {
"segment_atclocktime",
"set segment to be cut at clocktime",
OFFSET(use_clocktime),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
E},
916 {
"segment_time_delta",
"set approximation value used for the segment times",
OFFSET(time_delta),
AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, 0,
E },
919 {
"segment_wrap",
"set number after which the index wraps",
OFFSET(segment_idx_wrap),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
921 {
"segment_start_number",
"set the sequence number of the first segment",
OFFSET(segment_idx),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
922 {
"segment_wrap_number",
"set the number of wrap before the first segment",
OFFSET(segment_idx_wrap_nb),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
923 {
"strftime",
"set filename expansion with strftime at segment creation",
OFFSET(use_strftime),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1,
E },
924 {
"break_non_keyframes",
"allow breaking segments on non-keyframes",
OFFSET(break_non_keyframes),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
E },
926 {
"individual_header_trailer",
"write header/trailer to each segment",
OFFSET(individual_header_trailer),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
E },
927 {
"write_header_trailer",
"write a header to the first segment and a trailer to the last one",
OFFSET(write_header_trailer),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
E },
928 {
"reset_timestamps",
"reset timestamps at the begin of each segment",
OFFSET(reset_timestamps),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
E },
929 {
"initial_offset",
"set initial timestamp offset",
OFFSET(initial_offset),
AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX,
E },
948 .priv_class = &seg_class,
959 .
name =
"stream_segment,ssegment",
966 .priv_class = &sseg_class,
struct SegmentListEntry * next
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
char * header_filename
filename to write the output header to
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.
int av_escape(char **dst, const char *src, const char *special_chars, enum AVEscapeMode mode, int flags)
Escape string in src, and put the escaped string in an allocated string in *dst, which must be freed ...
#define LIBAVUTIL_VERSION_INT
char * entry_prefix
prefix to add to list entry filenames
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
AVDictionary * format_options
static int segment_start(AVFormatContext *s, int write_header)
int segment_idx_wrap
number after which the index wraps
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
#define AVIO_FLAG_WRITE
write-only
#define FF_ARRAY_ELEMS(a)
int segment_frame_count
number of reference frames in the segment
int avcodec_copy_context(AVCodecContext *dest, const AVCodecContext *src)
Copy the settings of the source AVCodecContext into the destination AVCodecContext.
static int parse_frames(void *log_ctx, int **frames, int *nb_frames, const char *frames_str)
static int seg_write_header(AVFormatContext *s)
static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
int list_flags
flags affecting list generation
static int segment_mux_init(AVFormatContext *s)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static void close_null_ctxp(AVIOContext **pb)
const char * av_basename(const char *path)
Thread safe basename.
Opaque data information usually continuous.
enum AVCodecID av_codec_get_id(const struct AVCodecTag *const *tags, unsigned int tag)
Get the AVCodecID for the given codec tag tag.
timestamp utils, mostly useful for debugging/logging purposes
static av_cold int end(AVCodecContext *avctx)
int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
Check if the stream st contained in s is matched by the stream specifier spec.
static int seg_write_trailer(struct AVFormatContext *s)
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
A list of all streams in the file.
static double av_q2d(AVRational a)
Convert rational to double.
#define AV_LOG_VERBOSE
Detailed information.
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
char * format
format to use for output segment files
static int select_reference_stream(AVFormatContext *s)
struct AVOutputFormat * oformat
The output container format.
int64_t initial_offset
initial timestamps offset, expressed in microseconds
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
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.
int individual_header_trailer
Set by a private option.
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#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. ...
simple assert() macros that are a bit more flexible than ISO C assert().
int64_t * times
list of segment interval specification
static const uint8_t offset[127][2]
char * reference_stream_specifier
reference stream specifier
static int set_segment_filename(AVFormatContext *s)
int reference_stream_index
int nb_times
number of elments in the times array
static int segment_list_open(AVFormatContext *s)
int flags
A combination of AV_PKT_FLAG values.
int64_t last_val
remember last time for wrap around detection
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.
Use auto-selected escaping mode.
AVIOContext * list_pb
list file put-byte context
AVCodecContext * codec
Codec context associated with this stream.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
char filename[1024]
input or output filename
static struct tm * localtime_r(const time_t *clock, struct tm *result)
static int write_trailer(AVFormatContext *s1)
static const AVClass sseg_class
int use_strftime
flag to expand filename with strftime
int reset_timestamps
reset timestamps at the begin of each segment
int use_clocktime
flag to cut segments at regular clock time
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...
Opaque data information usually sparse.
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Return in 'buf' the path with 'd' replaced by a number.
static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
int64_t av_gettime(void)
Get the current time in microseconds.
#define av_dlog(pctx,...)
av_dlog macros
char * list
filename for the segment list file
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
char * frames_str
segment frame numbers specification string
enum AVMediaType codec_type
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
char * av_strdup(const char *s)
Duplicate the string s.
AVIOContext * pb
I/O context.
void avio_w8(AVIOContext *s, int b)
main external API structure.
int64_t time
segment duration
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
int list_type
set the list type
AVOutputFormat ff_segment_muxer
int segment_idx
index of the segment file to write, starting from 0
Describe the class of an AVClass context structure.
int * frames
list of frame number specification
char * time_str
segment duration specification string
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.
SegmentListEntry * segment_list_entries
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
int nb_frames
number of elments in the frames array
int write_header_trailer
Set by a private option.
char * format_options_str
format options to use for output segment files
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok()...
static void segment_list_print_entry(AVIOContext *list_ioctx, ListType list_type, const SegmentListEntry *list_entry, void *log_ctx)
static const AVClass seg_class
void av_opt_free(void *obj)
Free all allocated objects in obj.
int disposition
AV_DISPOSITION_* bit field.
#define SEGMENT_LIST_FLAG_CACHE
int pts_wrap_bits
number of bits in pts (used for wrapping control)
static int parse_times(void *log_ctx, int64_t **times, int *nb_times, const char *times_str)
int segment_idx_wrap_nb
number of time the index has wraped
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
void * priv_data
Format private data.
static const AVOption options[]
int64_t last_cut
remember last cut
#define AV_ESCAPE_FLAG_WHITESPACE
Consider spaces special and escape them even in the middle of the string.
static void write_header(FFV1Context *f)
AVOutputFormat ff_stream_segment_muxer
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.
static int open_null_ctx(AVIOContext **ctx)
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
int segment_count
number of segment files already written
static void seg_free_context(SegmentContext *seg)
char * times_str
segment times specification string
#define av_malloc_array(a, b)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
SegmentListEntry cur_entry
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 list_size
number of entries for the segment list file
This structure stores compressed data.
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
int frame_count
total number of reference frames
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define SEGMENT_LIST_FLAG_LIVE
SegmentListEntry * segment_list_entries_end
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.
int avio_printf(AVIOContext *s, const char *fmt,...) av_printf_format(2