2 * Copyright (c) 2011, Luca Barbato
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * @file generic segmenter
23 * M3U8 specification can be find here:
24 * @url{http://tools.ietf.org/id/draft-pantos-http-live-streaming}
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"
42 typedef struct SegmentListEntry {
44 double start_time, end_time;
48 struct SegmentListEntry *next;
52 LIST_TYPE_UNDEFINED = -1,
56 LIST_TYPE_EXT, ///< deprecated
61 #define SEGMENT_LIST_FLAG_CACHE 1
62 #define SEGMENT_LIST_FLAG_LIVE 2
65 const AVClass *class; /**< Class for private options. */
66 int segment_idx; ///< index of the segment file to write, starting from 0
67 int segment_idx_wrap; ///< number after which the index wraps
68 int segment_idx_wrap_nb; ///< number of time the index has wraped
69 int segment_count; ///< number of segment files already written
70 AVOutputFormat *oformat;
72 char *format; ///< format to use for output segment files
73 char *list; ///< filename for the segment list file
74 int list_flags; ///< flags affecting list generation
75 int list_size; ///< number of entries for the segment list file
76 char *list_entry_prefix; ///< prefix to add to list entry filenames
77 ListType list_type; ///< set the list type
78 AVIOContext *list_pb; ///< list file put-byte context
79 char *time_str; ///< segment duration specification string
80 int64_t time; ///< segment duration
82 char *times_str; ///< segment times specification string
83 int64_t *times; ///< list of segment interval specification
84 int nb_times; ///< number of elments in the times array
86 char *frames_str; ///< segment frame numbers specification string
87 int *frames; ///< list of frame number specification
88 int nb_frames; ///< number of elments in the frames array
92 int individual_header_trailer; /**< Set by a private option. */
93 int write_header_trailer; /**< Set by a private option. */
95 int reset_timestamps; ///< reset timestamps at the begin of each segment
96 int64_t initial_offset; ///< initial timestamps offset, expressed in microseconds
97 char *reference_stream_specifier; ///< reference stream specifier
98 int reference_stream_index;
100 SegmentListEntry cur_entry;
101 SegmentListEntry *segment_list_entries;
102 SegmentListEntry *segment_list_entries_end;
104 int is_first_pkt; ///< tells if it is the first packet in the segment
107 static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
109 int needs_quoting = !!str[strcspn(str, "\",\n\r")];
114 for (; *str; str++) {
123 static int segment_mux_init(AVFormatContext *s)
125 SegmentContext *seg = s->priv_data;
129 seg->avf = oc = avformat_alloc_context();
131 return AVERROR(ENOMEM);
133 oc->oformat = seg->oformat;
134 oc->interrupt_callback = s->interrupt_callback;
135 av_dict_copy(&oc->metadata, s->metadata, 0);
137 for (i = 0; i < s->nb_streams; i++) {
139 AVCodecContext *icodec, *ocodec;
141 if (!(st = avformat_new_stream(oc, NULL)))
142 return AVERROR(ENOMEM);
143 icodec = s->streams[i]->codec;
145 avcodec_copy_context(ocodec, icodec);
146 if (!oc->oformat->codec_tag ||
147 av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == ocodec->codec_id ||
148 av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0) {
149 ocodec->codec_tag = icodec->codec_tag;
151 ocodec->codec_tag = 0;
153 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
159 static int set_segment_filename(AVFormatContext *s)
161 SegmentContext *seg = s->priv_data;
162 AVFormatContext *oc = seg->avf;
165 if (seg->segment_idx_wrap)
166 seg->segment_idx %= seg->segment_idx_wrap;
167 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
168 s->filename, seg->segment_idx) < 0) {
169 av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
170 return AVERROR(EINVAL);
173 /* copy modified name in list entry */
174 size = strlen(av_basename(oc->filename)) + 1;
175 if (seg->list_entry_prefix)
176 size += strlen(seg->list_entry_prefix);
178 seg->cur_entry.filename = av_mallocz(size);
179 if (!seg->cur_entry.filename)
180 return AVERROR(ENOMEM);
181 snprintf(seg->cur_entry.filename, size, "%s%s",
182 seg->list_entry_prefix ? seg->list_entry_prefix : "",
183 av_basename(oc->filename));
188 static int segment_start(AVFormatContext *s, int write_header)
190 SegmentContext *seg = s->priv_data;
191 AVFormatContext *oc = seg->avf;
195 avformat_free_context(oc);
197 if ((err = segment_mux_init(s)) < 0)
203 if ((seg->segment_idx_wrap) && (seg->segment_idx%seg->segment_idx_wrap == 0))
204 seg->segment_idx_wrap_nb++;
206 if ((err = set_segment_filename(s)) < 0)
209 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
210 &s->interrupt_callback, NULL)) < 0) {
211 av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
215 if (oc->oformat->priv_class && oc->priv_data)
216 av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
219 if ((err = avformat_write_header(oc, NULL)) < 0)
223 seg->is_first_pkt = 1;
227 static int segment_list_open(AVFormatContext *s)
229 SegmentContext *seg = s->priv_data;
232 ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
233 &s->interrupt_callback, NULL);
235 av_log(s, AV_LOG_ERROR, "Failed to open segment list '%s'\n", seg->list);
239 if (seg->list_type == LIST_TYPE_M3U8 && seg->segment_list_entries) {
240 SegmentListEntry *entry;
241 double max_duration = 0;
243 avio_printf(seg->list_pb, "#EXTM3U\n");
244 avio_printf(seg->list_pb, "#EXT-X-VERSION:3\n");
245 avio_printf(seg->list_pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->segment_list_entries->index);
246 avio_printf(seg->list_pb, "#EXT-X-ALLOW-CACHE:%s\n",
247 seg->list_flags & SEGMENT_LIST_FLAG_CACHE ? "YES" : "NO");
249 for (entry = seg->segment_list_entries; entry; entry = entry->next)
250 max_duration = FFMAX(max_duration, entry->end_time - entry->start_time);
251 avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%"PRId64"\n", (int64_t)ceil(max_duration));
252 } else if (seg->list_type == LIST_TYPE_FFCONCAT) {
253 avio_printf(seg->list_pb, "ffconcat version 1.0\n");
259 static void segment_list_print_entry(AVIOContext *list_ioctx,
261 const SegmentListEntry *list_entry,
266 avio_printf(list_ioctx, "%s\n", list_entry->filename);
270 print_csv_escaped_str(list_ioctx, list_entry->filename);
271 avio_printf(list_ioctx, ",%f,%f\n", list_entry->start_time, list_entry->end_time);
274 avio_printf(list_ioctx, "#EXTINF:%f,\n%s\n",
275 list_entry->end_time - list_entry->start_time, list_entry->filename);
277 case LIST_TYPE_FFCONCAT:
280 if (av_escape(&buf, list_entry->filename, NULL, AV_ESCAPE_MODE_AUTO, AV_ESCAPE_FLAG_WHITESPACE) < 0) {
281 av_log(log_ctx, AV_LOG_WARNING,
282 "Error writing list entry '%s' in list file\n", list_entry->filename);
285 avio_printf(list_ioctx, "file %s\n", buf);
290 av_assert0(!"Invalid list type");
294 static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
296 SegmentContext *seg = s->priv_data;
297 AVFormatContext *oc = seg->avf;
300 av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
302 ret = av_write_trailer(oc);
305 av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
309 if (seg->list_size || seg->list_type == LIST_TYPE_M3U8) {
310 SegmentListEntry *entry = av_mallocz(sizeof(*entry));
312 ret = AVERROR(ENOMEM);
316 /* append new element */
317 memcpy(entry, &seg->cur_entry, sizeof(*entry));
318 if (!seg->segment_list_entries)
319 seg->segment_list_entries = seg->segment_list_entries_end = entry;
321 seg->segment_list_entries_end->next = entry;
322 seg->segment_list_entries_end = entry;
324 /* drop first item */
325 if (seg->list_size && seg->segment_count > seg->list_size) {
326 entry = seg->segment_list_entries;
327 seg->segment_list_entries = seg->segment_list_entries->next;
328 av_free(entry->filename);
332 avio_close(seg->list_pb);
333 if ((ret = segment_list_open(s)) < 0)
335 for (entry = seg->segment_list_entries; entry; entry = entry->next)
336 segment_list_print_entry(seg->list_pb, seg->list_type, entry, s);
337 if (seg->list_type == LIST_TYPE_M3U8 && is_last)
338 avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
340 segment_list_print_entry(seg->list_pb, seg->list_type, &seg->cur_entry, s);
342 avio_flush(seg->list_pb);
345 av_log(s, AV_LOG_VERBOSE, "segment:'%s' count:%d ended\n",
346 seg->avf->filename, seg->segment_count);
347 seg->segment_count++;
355 static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
356 const char *times_str)
360 char *times_str1 = av_strdup(times_str);
361 char *saveptr = NULL;
364 return AVERROR(ENOMEM);
366 #define FAIL(err) ret = err; goto end
369 for (p = times_str1; *p; p++)
373 *times = av_malloc(sizeof(**times) * *nb_times);
375 av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array\n");
376 FAIL(AVERROR(ENOMEM));
380 for (i = 0; i < *nb_times; i++) {
382 char *tstr = av_strtok(p, ",", &saveptr);
385 if (!tstr || !tstr[0]) {
386 av_log(log_ctx, AV_LOG_ERROR, "Empty time specification in times list %s\n",
388 FAIL(AVERROR(EINVAL));
391 ret = av_parse_time(&t, tstr, 1);
393 av_log(log_ctx, AV_LOG_ERROR,
394 "Invalid time duration specification '%s' in times list %s\n", tstr, times_str);
395 FAIL(AVERROR(EINVAL));
399 /* check on monotonicity */
400 if (i && (*times)[i-1] > (*times)[i]) {
401 av_log(log_ctx, AV_LOG_ERROR,
402 "Specified time %f is greater than the following time %f\n",
403 (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000);
404 FAIL(AVERROR(EINVAL));
413 static int parse_frames(void *log_ctx, int **frames, int *nb_frames,
414 const char *frames_str)
418 char *frames_str1 = av_strdup(frames_str);
419 char *saveptr = NULL;
422 return AVERROR(ENOMEM);
424 #define FAIL(err) ret = err; goto end
427 for (p = frames_str1; *p; p++)
431 *frames = av_malloc(sizeof(**frames) * *nb_frames);
433 av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced frames array\n");
434 FAIL(AVERROR(ENOMEM));
438 for (i = 0; i < *nb_frames; i++) {
441 char *fstr = av_strtok(p, ",", &saveptr);
445 av_log(log_ctx, AV_LOG_ERROR, "Empty frame specification in frame list %s\n",
447 FAIL(AVERROR(EINVAL));
449 f = strtol(fstr, &tailptr, 10);
450 if (*tailptr || f <= 0 || f >= INT_MAX) {
451 av_log(log_ctx, AV_LOG_ERROR,
452 "Invalid argument '%s', must be a positive integer <= INT64_MAX\n",
454 FAIL(AVERROR(EINVAL));
458 /* check on monotonicity */
459 if (i && (*frames)[i-1] > (*frames)[i]) {
460 av_log(log_ctx, AV_LOG_ERROR,
461 "Specified frame %d is greater than the following frame %d\n",
462 (*frames)[i], (*frames)[i-1]);
463 FAIL(AVERROR(EINVAL));
468 av_free(frames_str1);
472 static int open_null_ctx(AVIOContext **ctx)
474 int buf_size = 32768;
475 uint8_t *buf = av_malloc(buf_size);
477 return AVERROR(ENOMEM);
478 *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
481 return AVERROR(ENOMEM);
486 static void close_null_ctx(AVIOContext *pb)
492 static int select_reference_stream(AVFormatContext *s)
494 SegmentContext *seg = s->priv_data;
497 seg->reference_stream_index = -1;
498 if (!strcmp(seg->reference_stream_specifier, "auto")) {
499 /* select first index of type with highest priority */
500 int type_index_map[AVMEDIA_TYPE_NB];
501 static const enum AVMediaType type_priority_list[] = {
504 AVMEDIA_TYPE_SUBTITLE,
506 AVMEDIA_TYPE_ATTACHMENT
508 enum AVMediaType type;
510 for (i = 0; i < AVMEDIA_TYPE_NB; i++)
511 type_index_map[i] = -1;
513 /* select first index for each type */
514 for (i = 0; i < s->nb_streams; i++) {
515 type = s->streams[i]->codec->codec_type;
516 if ((unsigned)type < AVMEDIA_TYPE_NB && type_index_map[type] == -1
517 /* ignore attached pictures/cover art streams */
518 && !(s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC))
519 type_index_map[type] = i;
522 for (i = 0; i < FF_ARRAY_ELEMS(type_priority_list); i++) {
523 type = type_priority_list[i];
524 if ((seg->reference_stream_index = type_index_map[type]) >= 0)
528 for (i = 0; i < s->nb_streams; i++) {
529 ret = avformat_match_stream_specifier(s, s->streams[i],
530 seg->reference_stream_specifier);
534 seg->reference_stream_index = i;
540 if (seg->reference_stream_index < 0) {
541 av_log(s, AV_LOG_ERROR, "Could not select stream matching identifier '%s'\n",
542 seg->reference_stream_specifier);
543 return AVERROR(EINVAL);
549 static int seg_write_header(AVFormatContext *s)
551 SegmentContext *seg = s->priv_data;
552 AVFormatContext *oc = NULL;
555 seg->segment_count = 0;
556 if (!seg->write_header_trailer)
557 seg->individual_header_trailer = 0;
559 if (!!seg->time_str + !!seg->times_str + !!seg->frames_str > 1) {
560 av_log(s, AV_LOG_ERROR,
561 "segment_time, segment_times, and segment_frames options "
562 "are mutually exclusive, select just one of them\n");
563 return AVERROR(EINVAL);
566 if (seg->times_str) {
567 if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
569 } else if (seg->frames_str) {
570 if ((ret = parse_frames(s, &seg->frames, &seg->nb_frames, seg->frames_str)) < 0)
573 /* set default value if not specified */
575 seg->time_str = av_strdup("2");
576 if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
577 av_log(s, AV_LOG_ERROR,
578 "Invalid time duration specification '%s' for segment_time option\n",
585 if (seg->list_type == LIST_TYPE_UNDEFINED) {
586 if (av_match_ext(seg->list, "csv" )) seg->list_type = LIST_TYPE_CSV;
587 else if (av_match_ext(seg->list, "ext" )) seg->list_type = LIST_TYPE_EXT;
588 else if (av_match_ext(seg->list, "m3u8")) seg->list_type = LIST_TYPE_M3U8;
589 else if (av_match_ext(seg->list, "ffcat,ffconcat")) seg->list_type = LIST_TYPE_FFCONCAT;
590 else seg->list_type = LIST_TYPE_FLAT;
592 if ((ret = segment_list_open(s)) < 0)
595 if (seg->list_type == LIST_TYPE_EXT)
596 av_log(s, AV_LOG_WARNING, "'ext' list type option is deprecated in favor of 'csv'\n");
598 if ((ret = select_reference_stream(s)) < 0)
600 av_log(s, AV_LOG_VERBOSE, "Selected stream id:%d type:%s\n",
601 seg->reference_stream_index,
602 av_get_media_type_string(s->streams[seg->reference_stream_index]->codec->codec_type));
604 seg->oformat = av_guess_format(seg->format, s->filename, NULL);
607 ret = AVERROR_MUXER_NOT_FOUND;
610 if (seg->oformat->flags & AVFMT_NOFILE) {
611 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
613 ret = AVERROR(EINVAL);
617 if ((ret = segment_mux_init(s)) < 0)
621 if ((ret = set_segment_filename(s)) < 0)
624 if (seg->write_header_trailer) {
625 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
626 &s->interrupt_callback, NULL)) < 0) {
627 av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
631 if ((ret = open_null_ctx(&oc->pb)) < 0)
635 if ((ret = avformat_write_header(oc, NULL)) < 0) {
639 seg->is_first_pkt = 1;
641 if (oc->avoid_negative_ts > 0 && s->avoid_negative_ts < 0)
642 s->avoid_negative_ts = 1;
644 if (!seg->write_header_trailer) {
645 close_null_ctx(oc->pb);
646 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
647 &s->interrupt_callback, NULL)) < 0)
654 avio_close(seg->list_pb);
656 avformat_free_context(seg->avf);
661 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
663 SegmentContext *seg = s->priv_data;
664 AVStream *st = s->streams[pkt->stream_index];
665 int64_t end_pts = INT64_MAX, offset;
666 int start_frame = INT_MAX;
670 end_pts = seg->segment_count < seg->nb_times ?
671 seg->times[seg->segment_count] : INT64_MAX;
672 } else if (seg->frames) {
673 start_frame = seg->segment_count <= seg->nb_frames ?
674 seg->frames[seg->segment_count] : INT_MAX;
676 end_pts = seg->time * (seg->segment_count+1);
679 av_dlog(s, "packet stream:%d pts:%s pts_time:%s is_key:%d frame:%d\n",
680 pkt->stream_index, av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
681 pkt->flags & AV_PKT_FLAG_KEY,
682 pkt->stream_index == seg->reference_stream_index ? seg->frame_count : -1);
684 if (pkt->stream_index == seg->reference_stream_index &&
685 pkt->flags & AV_PKT_FLAG_KEY &&
686 (seg->frame_count >= start_frame ||
687 (pkt->pts != AV_NOPTS_VALUE &&
688 av_compare_ts(pkt->pts, st->time_base,
689 end_pts-seg->time_delta, AV_TIME_BASE_Q) >= 0))) {
690 if ((ret = segment_end(s, seg->individual_header_trailer, 0)) < 0)
693 if ((ret = segment_start(s, seg->individual_header_trailer)) < 0)
696 seg->cur_entry.index = seg->segment_idx + seg->segment_idx_wrap*seg->segment_idx_wrap_nb;
697 seg->cur_entry.start_time = (double)pkt->pts * av_q2d(st->time_base);
698 seg->cur_entry.start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
699 } else if (pkt->pts != AV_NOPTS_VALUE) {
700 seg->cur_entry.end_time =
701 FFMAX(seg->cur_entry.end_time, (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
704 if (seg->is_first_pkt) {
705 av_log(s, AV_LOG_VERBOSE, "segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
706 seg->avf->filename, pkt->stream_index,
707 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base), seg->frame_count);
708 seg->is_first_pkt = 0;
711 av_log(s, AV_LOG_DEBUG, "stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
713 av_ts2timestr(seg->cur_entry.start_pts, &AV_TIME_BASE_Q),
714 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
715 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
717 /* compute new timestamps */
718 offset = av_rescale_q(seg->initial_offset - (seg->reset_timestamps ? seg->cur_entry.start_pts : 0),
719 AV_TIME_BASE_Q, st->time_base);
720 if (pkt->pts != AV_NOPTS_VALUE)
722 if (pkt->dts != AV_NOPTS_VALUE)
725 av_log(s, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
726 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
727 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
729 ret = ff_write_chained(seg->avf, pkt->stream_index, pkt, s);
732 if (pkt->stream_index == seg->reference_stream_index)
738 static int seg_write_trailer(struct AVFormatContext *s)
740 SegmentContext *seg = s->priv_data;
741 AVFormatContext *oc = seg->avf;
742 SegmentListEntry *cur, *next;
745 if (!seg->write_header_trailer) {
746 if ((ret = segment_end(s, 0, 1)) < 0)
748 open_null_ctx(&oc->pb);
749 ret = av_write_trailer(oc);
750 close_null_ctx(oc->pb);
752 ret = segment_end(s, 1, 1);
756 avio_close(seg->list_pb);
759 av_freep(&seg->times);
760 av_freep(&seg->frames);
762 cur = seg->segment_list_entries;
765 av_free(cur->filename);
770 avformat_free_context(oc);
774 #define OFFSET(x) offsetof(SegmentContext, x)
775 #define E AV_OPT_FLAG_ENCODING_PARAM
776 static const AVOption options[] = {
777 { "reference_stream", "set reference stream", OFFSET(reference_stream_specifier), AV_OPT_TYPE_STRING, {.str = "auto"}, CHAR_MIN, CHAR_MAX, E },
778 { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
779 { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
781 { "segment_list_flags","set flags affecting segment list generation", OFFSET(list_flags), AV_OPT_TYPE_FLAGS, {.i64 = SEGMENT_LIST_FLAG_CACHE }, 0, UINT_MAX, E, "list_flags"},
782 { "cache", "allow list caching", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_CACHE }, INT_MIN, INT_MAX, E, "list_flags"},
783 { "live", "enable live-friendly list generation (useful for HLS)", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_LIVE }, INT_MIN, INT_MAX, E, "list_flags"},
785 { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
786 { "segment_list_entry_prefix", "set prefix to prepend to each list entry filename", OFFSET(list_entry_prefix), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
788 { "segment_list_type", "set the segment list type", OFFSET(list_type), AV_OPT_TYPE_INT, {.i64 = LIST_TYPE_UNDEFINED}, -1, LIST_TYPE_NB-1, E, "list_type" },
789 { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, E, "list_type" },
790 { "csv", "csv format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_CSV }, INT_MIN, INT_MAX, E, "list_type" },
791 { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_EXT }, INT_MIN, INT_MAX, E, "list_type" },
792 { "ffconcat", "ffconcat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FFCONCAT }, INT_MIN, INT_MAX, E, "list_type" },
793 { "m3u8", "M3U8 format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
794 { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
796 { "segment_time", "set segment duration", OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
797 { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, 0, E },
798 { "segment_times", "set segment split time points", OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
799 { "segment_frames", "set segment split frame numbers", OFFSET(frames_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
800 { "segment_wrap", "set number after which the index wraps", OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
801 { "segment_start_number", "set the sequence number of the first segment", OFFSET(segment_idx), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
802 { "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 },
804 { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
805 { "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 },
806 { "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, E },
807 { "initial_offset", "set initial timestamp offset", OFFSET(initial_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX, E },
811 static const AVClass seg_class = {
812 .class_name = "segment muxer",
813 .item_name = av_default_item_name,
815 .version = LIBAVUTIL_VERSION_INT,
818 AVOutputFormat ff_segment_muxer = {
820 .long_name = NULL_IF_CONFIG_SMALL("segment"),
821 .priv_data_size = sizeof(SegmentContext),
822 .flags = AVFMT_NOFILE|AVFMT_GLOBALHEADER,
823 .write_header = seg_write_header,
824 .write_packet = seg_write_packet,
825 .write_trailer = seg_write_trailer,
826 .priv_class = &seg_class,
829 static const AVClass sseg_class = {
830 .class_name = "stream_segment muxer",
831 .item_name = av_default_item_name,
833 .version = LIBAVUTIL_VERSION_INT,
836 AVOutputFormat ff_stream_segment_muxer = {
837 .name = "stream_segment,ssegment",
838 .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
839 .priv_data_size = sizeof(SegmentContext),
840 .flags = AVFMT_NOFILE,
841 .write_header = seg_write_header,
842 .write_packet = seg_write_packet,
843 .write_trailer = seg_write_trailer,
844 .priv_class = &sseg_class,