34 #include "libavutil/avassert.h"
35 #include "libavutil/log.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/parseutils.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/timestamp.h"
61 #define SEGMENT_LIST_FLAG_CACHE 1
62 #define SEGMENT_LIST_FLAG_LIVE 2
107 int needs_quoting = !!str[strcspn(str,
"\",\n\r")];
112 for (; *str; str++) {
219 double max_duration = 0;
229 avio_printf(seg->
list_pb,
"#EXT-X-TARGETDURATION:%"PRId64
"\n", (int64_t)ceil(max_duration));
260 "Error writing list entry '%s' in list file\n", list_entry->
filename);
295 memcpy(entry, &seg->
cur_entry,
sizeof(*entry));
332 static int parse_times(
void *log_ctx, int64_t **times,
int *nb_times,
333 const char *times_str)
338 char *saveptr = NULL;
343 #define FAIL(err) ret = err; goto end
346 for (p = times_str1; *p; p++)
350 *times =
av_malloc(
sizeof(**times) * *nb_times);
357 for (i = 0; i < *nb_times; i++) {
359 char *tstr =
av_strtok(p,
",", &saveptr);
362 if (!tstr || !tstr[0]) {
371 "Invalid time duration specification '%s' in times list %s\n", tstr, times_str);
377 if (i && (*times)[i-1] > (*times)[i]) {
379 "Specified time %f is greater than the following time %f\n",
380 (
float)((*times)[i])/1000000, (
float)((*times)[i-1])/1000000);
391 const char *frames_str)
395 char *frames_str1 =
av_strdup(frames_str);
396 char *saveptr = NULL;
401 #define FAIL(err) ret = err; goto end
404 for (p = frames_str1; *p; p++)
408 *frames =
av_malloc(
sizeof(**frames) * *nb_frames);
415 for (i = 0; i < *nb_frames; i++) {
418 char *fstr =
av_strtok(p,
",", &saveptr);
426 f = strtol(fstr, &tailptr, 10);
427 if (*tailptr || f <= 0 || f >= INT_MAX) {
429 "Invalid argument '%s', must be a positive integer <= INT64_MAX\n",
436 if (i && (*frames)[i-1] > (*frames)[i]) {
438 "Specified frame %d is greater than the following frame %d\n",
439 (*frames)[i], (*frames)[i-1]);
451 int buf_size = 32768;
478 static const enum AVMediaType type_priority_list[] = {
488 type_index_map[i] = -1;
493 if ((
unsigned)type < AVMEDIA_TYPE_NB && type_index_map[type] == -1
496 type_index_map[
type] = i;
500 type = type_priority_list[i];
538 "segment_time, segment_times, and segment_frames options "
539 "are mutually exclusive, select just one of them\n");
555 "Invalid time duration specification '%s' for segment_time option\n",
641 int64_t end_pts = INT64_MAX,
offset;
642 int start_frame = INT_MAX;
655 av_dlog(s,
"packet stream:%d pts:%s pts_time:%s is_key:%d frame:%d\n",
683 av_log(s,
AV_LOG_DEBUG,
"segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
689 av_log(s,
AV_LOG_DEBUG,
"stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
757 #define OFFSET(x) offsetof(SegmentContext, x)
758 #define E AV_OPT_FLAG_ENCODING_PARAM
760 {
"reference_stream",
"set reference stream",
OFFSET(reference_stream_specifier),
AV_OPT_TYPE_STRING, {.str =
"auto"}, CHAR_MIN, CHAR_MAX,
E },
761 {
"segment_format",
"set container format used for the segments",
OFFSET(format),
AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0,
E },
768 {
"segment_list_size",
"set the maximum number of playlist entries",
OFFSET(list_size),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
779 {
"segment_time_delta",
"set approximation value used for the segment times",
OFFSET(time_delta),
AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, 0,
E },
782 {
"segment_wrap",
"set number after which the index wraps",
OFFSET(segment_idx_wrap),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
783 {
"segment_start_number",
"set the sequence number of the first segment",
OFFSET(segment_idx),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX,
E },
785 {
"individual_header_trailer",
"write header/trailer to each segment",
OFFSET(individual_header_trailer),
AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1,
E },
786 {
"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 },
787 {
"reset_timestamps",
"reset timestamps at the begin of each segment",
OFFSET(reset_timestamps),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
E },
788 {
"initial_offset",
"set initial timestamp offset",
OFFSET(initial_offset),
AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX,
E },
807 .priv_class = &seg_class,
818 .
name =
"stream_segment,ssegment",
825 .priv_class = &sseg_class,
struct SegmentListEntry * next
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
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 av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define LIBAVUTIL_VERSION_INT
char * av_strdup(const char *s) av_malloc_attrib
Duplicate the string s.
static int segment_start(AVFormatContext *s, int write_header)
int segment_idx_wrap
number after which the index wraps
AVCodecContext * codec
Codec context associated with this stream.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
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 void close_null_ctx(AVIOContext *pb)
static int segment_mux_init(AVFormatContext *s)
#define AVERROR_MUXER_NOT_FOUND
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
unsigned char * buffer
Start of the buffer.
const char * av_default_item_name(void *ctx)
Return the context name.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
enum AVCodecID av_codec_get_id(const struct AVCodecTag *const *tags, unsigned int tag)
Get the AVCodecID for the given codec tag tag.
static const uint8_t offset[511][2]
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)
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.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
const OptionDef options[]
char * format
format to use for output segment files
static int select_reference_stream(AVFormatContext *s)
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) av_const
Rescale a 64-bit integer by 2 rational numbers.
void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
int individual_header_trailer
Set by a private option.
void * priv_data
Format private data.
char filename[1024]
input or output filename
#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.
int64_t * times
list of segment interval specification
char * reference_stream_specifier
reference stream specifier
static int set_segment_filename(AVFormatContext *s)
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.
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.
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
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.
unsigned int nb_streams
A list of all streams in the file.
#define FF_ARRAY_ELEMS(a)
static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
Opaque data information usually continuous.
static int write_trailer(AVFormatContext *s1)
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static const AVClass sseg_class
int reset_timestamps
reset timestamps at the begin of each segment
int avoid_negative_ts
Avoid negative timestamps during muxing.
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)
char * list
filename for the segment list file
char * frames_str
segment frame numbers specification string
enum AVMediaType codec_type
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
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').
AVIOContext * pb
I/O context.
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 av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
int is_first_pkt
tells if it is the first packet in the segment
SegmentListEntry * segment_list_entries
int nb_frames
number of elments in the frames array
int write_header_trailer
Set by a private option.
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 string and binary options in obj.
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()...
int disposition
AV_DISPOSITION_* bit field.
#define SEGMENT_LIST_FLAG_CACHE
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
static int parse_times(void *log_ctx, int64_t **times, int *nb_times, const char *times_str)
struct AVOutputFormat * oformat
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...
#define AV_ESCAPE_FLAG_WHITESPACE
Consider spaces special and escape them even in the middle of the string.
Opaque data information usually sparse.
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)
int segment_count
number of segment files already written
#define av_assert0(cond)
assert() equivalent, that is always enabled.
char * times_str
segment times specification string
ListType list_type
set the list type
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.
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#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.
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...