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_count; ///< number of segment files already written
69 AVOutputFormat *oformat;
71 char *format; ///< format to use for output segment files
72 char *list; ///< filename for the segment list file
73 int list_flags; ///< flags affecting list generation
74 int list_size; ///< number of entries for the segment list file
75 char *list_entry_prefix; ///< prefix to add to list entry filenames
76 ListType list_type; ///< set the list type
77 AVIOContext *list_pb; ///< list file put-byte context
78 char *time_str; ///< segment duration specification string
79 int64_t time; ///< segment duration
81 char *times_str; ///< segment times specification string
82 int64_t *times; ///< list of segment interval specification
83 int nb_times; ///< number of elments in the times array
85 char *frames_str; ///< segment frame numbers specification string
86 int *frames; ///< list of frame number specification
87 int nb_frames; ///< number of elments in the frames array
91 int individual_header_trailer; /**< Set by a private option. */
92 int write_header_trailer; /**< Set by a private option. */
94 int reset_timestamps; ///< reset timestamps at the begin of each segment
95 int64_t initial_offset; ///< initial timestamps offset, expressed in microseconds
96 char *reference_stream_specifier; ///< reference stream specifier
97 int reference_stream_index;
99 SegmentListEntry cur_entry;
100 SegmentListEntry *segment_list_entries;
101 SegmentListEntry *segment_list_entries_end;
103 int is_first_pkt; ///< tells if it is the first packet in the segment
106 static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
108 int needs_quoting = !!str[strcspn(str, "\",\n\r")];
113 for (; *str; str++) {
122 static int segment_mux_init(AVFormatContext *s)
124 SegmentContext *seg = s->priv_data;
128 seg->avf = oc = avformat_alloc_context();
130 return AVERROR(ENOMEM);
132 oc->oformat = seg->oformat;
133 oc->interrupt_callback = s->interrupt_callback;
134 av_dict_copy(&oc->metadata, s->metadata, 0);
136 for (i = 0; i < s->nb_streams; i++) {
138 AVCodecContext *icodec, *ocodec;
140 if (!(st = avformat_new_stream(oc, NULL)))
141 return AVERROR(ENOMEM);
142 icodec = s->streams[i]->codec;
144 avcodec_copy_context(ocodec, icodec);
145 if (!oc->oformat->codec_tag ||
146 av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == ocodec->codec_id ||
147 av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0) {
148 ocodec->codec_tag = icodec->codec_tag;
150 ocodec->codec_tag = 0;
152 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
158 static int set_segment_filename(AVFormatContext *s)
160 SegmentContext *seg = s->priv_data;
161 AVFormatContext *oc = seg->avf;
164 if (seg->segment_idx_wrap)
165 seg->segment_idx %= seg->segment_idx_wrap;
166 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
167 s->filename, seg->segment_idx) < 0) {
168 av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
169 return AVERROR(EINVAL);
172 /* copy modified name in list entry */
173 size = strlen(av_basename(oc->filename)) + 1;
174 if (seg->list_entry_prefix)
175 size += strlen(seg->list_entry_prefix);
177 seg->cur_entry.filename = av_mallocz(size);
178 if (!seg->cur_entry.filename)
179 return AVERROR(ENOMEM);
180 snprintf(seg->cur_entry.filename, size, "%s%s",
181 seg->list_entry_prefix ? seg->list_entry_prefix : "",
182 av_basename(oc->filename));
187 static int segment_start(AVFormatContext *s, int write_header)
189 SegmentContext *seg = s->priv_data;
190 AVFormatContext *oc = seg->avf;
194 avformat_free_context(oc);
196 if ((err = segment_mux_init(s)) < 0)
202 if ((err = set_segment_filename(s)) < 0)
205 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
206 &s->interrupt_callback, NULL)) < 0) {
207 av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
211 if (oc->oformat->priv_class && oc->priv_data)
212 av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
215 if ((err = avformat_write_header(oc, NULL)) < 0)
219 seg->is_first_pkt = 1;
223 static int segment_list_open(AVFormatContext *s)
225 SegmentContext *seg = s->priv_data;
228 ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
229 &s->interrupt_callback, NULL);
231 av_log(s, AV_LOG_ERROR, "Failed to open segment list '%s'\n", seg->list);
235 if (seg->list_type == LIST_TYPE_M3U8 && seg->segment_list_entries) {
236 SegmentListEntry *entry;
237 double max_duration = 0;
239 avio_printf(seg->list_pb, "#EXTM3U\n");
240 avio_printf(seg->list_pb, "#EXT-X-VERSION:3\n");
241 avio_printf(seg->list_pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->segment_list_entries->index);
242 avio_printf(seg->list_pb, "#EXT-X-ALLOW-CACHE:%s\n",
243 seg->list_flags & SEGMENT_LIST_FLAG_CACHE ? "YES" : "NO");
245 for (entry = seg->segment_list_entries; entry; entry = entry->next)
246 max_duration = FFMAX(max_duration, entry->end_time - entry->start_time);
247 avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%"PRId64"\n", (int64_t)ceil(max_duration));
248 } else if (seg->list_type == LIST_TYPE_FFCONCAT) {
249 avio_printf(seg->list_pb, "ffconcat version 1.0\n");
255 static void segment_list_print_entry(AVIOContext *list_ioctx,
257 const SegmentListEntry *list_entry,
262 avio_printf(list_ioctx, "%s\n", list_entry->filename);
266 print_csv_escaped_str(list_ioctx, list_entry->filename);
267 avio_printf(list_ioctx, ",%f,%f\n", list_entry->start_time, list_entry->end_time);
270 avio_printf(list_ioctx, "#EXTINF:%f,\n%s\n",
271 list_entry->end_time - list_entry->start_time, list_entry->filename);
273 case LIST_TYPE_FFCONCAT:
276 if (av_escape(&buf, list_entry->filename, NULL, AV_ESCAPE_MODE_AUTO, AV_ESCAPE_FLAG_WHITESPACE) < 0) {
277 av_log(log_ctx, AV_LOG_WARNING,
278 "Error writing list entry '%s' in list file\n", list_entry->filename);
281 avio_printf(list_ioctx, "file %s\n", buf);
286 av_assert0(!"Invalid list type");
290 static int segment_end(AVFormatContext *s, int write_trailer, int is_last)
292 SegmentContext *seg = s->priv_data;
293 AVFormatContext *oc = seg->avf;
296 av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
298 ret = av_write_trailer(oc);
301 av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
305 if (seg->list_size || seg->list_type == LIST_TYPE_M3U8) {
306 SegmentListEntry *entry = av_mallocz(sizeof(*entry));
308 ret = AVERROR(ENOMEM);
312 /* append new element */
313 memcpy(entry, &seg->cur_entry, sizeof(*entry));
314 if (!seg->segment_list_entries)
315 seg->segment_list_entries = seg->segment_list_entries_end = entry;
317 seg->segment_list_entries_end->next = entry;
318 seg->segment_list_entries_end = entry;
320 /* drop first item */
321 if (seg->list_size && seg->segment_count > seg->list_size) {
322 entry = seg->segment_list_entries;
323 seg->segment_list_entries = seg->segment_list_entries->next;
324 av_free(entry->filename);
328 avio_close(seg->list_pb);
329 if ((ret = segment_list_open(s)) < 0)
331 for (entry = seg->segment_list_entries; entry; entry = entry->next)
332 segment_list_print_entry(seg->list_pb, seg->list_type, entry, s);
333 if (seg->list_type == LIST_TYPE_M3U8 && is_last)
334 avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
336 segment_list_print_entry(seg->list_pb, seg->list_type, &seg->cur_entry, s);
338 avio_flush(seg->list_pb);
341 av_log(s, AV_LOG_VERBOSE, "segment:'%s' count:%d ended\n",
342 seg->avf->filename, seg->segment_count);
343 seg->segment_count++;
351 static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
352 const char *times_str)
356 char *times_str1 = av_strdup(times_str);
357 char *saveptr = NULL;
360 return AVERROR(ENOMEM);
362 #define FAIL(err) ret = err; goto end
365 for (p = times_str1; *p; p++)
369 *times = av_malloc(sizeof(**times) * *nb_times);
371 av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array\n");
372 FAIL(AVERROR(ENOMEM));
376 for (i = 0; i < *nb_times; i++) {
378 char *tstr = av_strtok(p, ",", &saveptr);
381 if (!tstr || !tstr[0]) {
382 av_log(log_ctx, AV_LOG_ERROR, "Empty time specification in times list %s\n",
384 FAIL(AVERROR(EINVAL));
387 ret = av_parse_time(&t, tstr, 1);
389 av_log(log_ctx, AV_LOG_ERROR,
390 "Invalid time duration specification '%s' in times list %s\n", tstr, times_str);
391 FAIL(AVERROR(EINVAL));
395 /* check on monotonicity */
396 if (i && (*times)[i-1] > (*times)[i]) {
397 av_log(log_ctx, AV_LOG_ERROR,
398 "Specified time %f is greater than the following time %f\n",
399 (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000);
400 FAIL(AVERROR(EINVAL));
409 static int parse_frames(void *log_ctx, int **frames, int *nb_frames,
410 const char *frames_str)
414 char *frames_str1 = av_strdup(frames_str);
415 char *saveptr = NULL;
418 return AVERROR(ENOMEM);
420 #define FAIL(err) ret = err; goto end
423 for (p = frames_str1; *p; p++)
427 *frames = av_malloc(sizeof(**frames) * *nb_frames);
429 av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced frames array\n");
430 FAIL(AVERROR(ENOMEM));
434 for (i = 0; i < *nb_frames; i++) {
437 char *fstr = av_strtok(p, ",", &saveptr);
441 av_log(log_ctx, AV_LOG_ERROR, "Empty frame specification in frame list %s\n",
443 FAIL(AVERROR(EINVAL));
445 f = strtol(fstr, &tailptr, 10);
446 if (*tailptr || f <= 0 || f >= INT_MAX) {
447 av_log(log_ctx, AV_LOG_ERROR,
448 "Invalid argument '%s', must be a positive integer <= INT64_MAX\n",
450 FAIL(AVERROR(EINVAL));
454 /* check on monotonicity */
455 if (i && (*frames)[i-1] > (*frames)[i]) {
456 av_log(log_ctx, AV_LOG_ERROR,
457 "Specified frame %d is greater than the following frame %d\n",
458 (*frames)[i], (*frames)[i-1]);
459 FAIL(AVERROR(EINVAL));
464 av_free(frames_str1);
468 static int open_null_ctx(AVIOContext **ctx)
470 int buf_size = 32768;
471 uint8_t *buf = av_malloc(buf_size);
473 return AVERROR(ENOMEM);
474 *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
477 return AVERROR(ENOMEM);
482 static void close_null_ctx(AVIOContext *pb)
488 static int select_reference_stream(AVFormatContext *s)
490 SegmentContext *seg = s->priv_data;
493 seg->reference_stream_index = -1;
494 if (!strcmp(seg->reference_stream_specifier, "auto")) {
495 /* select first index of type with highest priority */
496 int type_index_map[AVMEDIA_TYPE_NB];
497 static const enum AVMediaType type_priority_list[] = {
500 AVMEDIA_TYPE_SUBTITLE,
502 AVMEDIA_TYPE_ATTACHMENT
504 enum AVMediaType type;
506 for (i = 0; i < AVMEDIA_TYPE_NB; i++)
507 type_index_map[i] = -1;
509 /* select first index for each type */
510 for (i = 0; i < s->nb_streams; i++) {
511 type = s->streams[i]->codec->codec_type;
512 if ((unsigned)type < AVMEDIA_TYPE_NB && type_index_map[type] == -1
513 /* ignore attached pictures/cover art streams */
514 && !(s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC))
515 type_index_map[type] = i;
518 for (i = 0; i < FF_ARRAY_ELEMS(type_priority_list); i++) {
519 type = type_priority_list[i];
520 if ((seg->reference_stream_index = type_index_map[type]) >= 0)
524 for (i = 0; i < s->nb_streams; i++) {
525 ret = avformat_match_stream_specifier(s, s->streams[i],
526 seg->reference_stream_specifier);
530 seg->reference_stream_index = i;
536 if (seg->reference_stream_index < 0) {
537 av_log(s, AV_LOG_ERROR, "Could not select stream matching identifier '%s'\n",
538 seg->reference_stream_specifier);
539 return AVERROR(EINVAL);
545 static int seg_write_header(AVFormatContext *s)
547 SegmentContext *seg = s->priv_data;
548 AVFormatContext *oc = NULL;
551 seg->segment_count = 0;
552 if (!seg->write_header_trailer)
553 seg->individual_header_trailer = 0;
555 if (!!seg->time_str + !!seg->times_str + !!seg->frames_str > 1) {
556 av_log(s, AV_LOG_ERROR,
557 "segment_time, segment_times, and segment_frames options "
558 "are mutually exclusive, select just one of them\n");
559 return AVERROR(EINVAL);
562 if (seg->times_str) {
563 if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
565 } else if (seg->frames_str) {
566 if ((ret = parse_frames(s, &seg->frames, &seg->nb_frames, seg->frames_str)) < 0)
569 /* set default value if not specified */
571 seg->time_str = av_strdup("2");
572 if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
573 av_log(s, AV_LOG_ERROR,
574 "Invalid time duration specification '%s' for segment_time option\n",
581 if (seg->list_type == LIST_TYPE_UNDEFINED) {
582 if (av_match_ext(seg->list, "csv" )) seg->list_type = LIST_TYPE_CSV;
583 else if (av_match_ext(seg->list, "ext" )) seg->list_type = LIST_TYPE_EXT;
584 else if (av_match_ext(seg->list, "m3u8")) seg->list_type = LIST_TYPE_M3U8;
585 else if (av_match_ext(seg->list, "ffcat,ffconcat")) seg->list_type = LIST_TYPE_FFCONCAT;
586 else seg->list_type = LIST_TYPE_FLAT;
588 if ((ret = segment_list_open(s)) < 0)
591 if (seg->list_type == LIST_TYPE_EXT)
592 av_log(s, AV_LOG_WARNING, "'ext' list type option is deprecated in favor of 'csv'\n");
594 if ((ret = select_reference_stream(s)) < 0)
596 av_log(s, AV_LOG_VERBOSE, "Selected stream id:%d type:%s\n",
597 seg->reference_stream_index,
598 av_get_media_type_string(s->streams[seg->reference_stream_index]->codec->codec_type));
600 seg->oformat = av_guess_format(seg->format, s->filename, NULL);
603 ret = AVERROR_MUXER_NOT_FOUND;
606 if (seg->oformat->flags & AVFMT_NOFILE) {
607 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
609 ret = AVERROR(EINVAL);
613 if ((ret = segment_mux_init(s)) < 0)
617 if ((ret = set_segment_filename(s)) < 0)
620 if (seg->write_header_trailer) {
621 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
622 &s->interrupt_callback, NULL)) < 0) {
623 av_log(s, AV_LOG_ERROR, "Failed to open segment '%s'\n", oc->filename);
627 if ((ret = open_null_ctx(&oc->pb)) < 0)
631 if ((ret = avformat_write_header(oc, NULL)) < 0) {
635 seg->is_first_pkt = 1;
637 if (oc->avoid_negative_ts > 0 && s->avoid_negative_ts < 0)
638 s->avoid_negative_ts = 1;
640 if (!seg->write_header_trailer) {
641 close_null_ctx(oc->pb);
642 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
643 &s->interrupt_callback, NULL)) < 0)
650 avio_close(seg->list_pb);
652 avformat_free_context(seg->avf);
657 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
659 SegmentContext *seg = s->priv_data;
660 AVFormatContext *oc = seg->avf;
661 AVStream *st = s->streams[pkt->stream_index];
662 int64_t end_pts = INT64_MAX, offset;
663 int start_frame = INT_MAX;
667 end_pts = seg->segment_count < seg->nb_times ?
668 seg->times[seg->segment_count] : INT64_MAX;
669 } else if (seg->frames) {
670 start_frame = seg->segment_count <= seg->nb_frames ?
671 seg->frames[seg->segment_count] : INT_MAX;
673 end_pts = seg->time * (seg->segment_count+1);
676 av_dlog(s, "packet stream:%d pts:%s pts_time:%s is_key:%d frame:%d\n",
677 pkt->stream_index, av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
678 pkt->flags & AV_PKT_FLAG_KEY,
679 pkt->stream_index == seg->reference_stream_index ? seg->frame_count : -1);
681 if (pkt->stream_index == seg->reference_stream_index &&
682 pkt->flags & AV_PKT_FLAG_KEY &&
683 (seg->frame_count >= start_frame ||
684 (pkt->pts != AV_NOPTS_VALUE &&
685 av_compare_ts(pkt->pts, st->time_base,
686 end_pts-seg->time_delta, AV_TIME_BASE_Q) >= 0))) {
687 if ((ret = segment_end(s, seg->individual_header_trailer, 0)) < 0)
690 if ((ret = segment_start(s, seg->individual_header_trailer)) < 0)
695 seg->cur_entry.index = seg->segment_idx;
696 seg->cur_entry.start_time = (double)pkt->pts * av_q2d(st->time_base);
697 seg->cur_entry.start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
698 } else if (pkt->pts != AV_NOPTS_VALUE) {
699 seg->cur_entry.end_time =
700 FFMAX(seg->cur_entry.end_time, (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
703 if (seg->is_first_pkt) {
704 av_log(s, AV_LOG_DEBUG, "segment:'%s' starts with packet stream:%d pts:%s pts_time:%s frame:%d\n",
705 seg->avf->filename, pkt->stream_index,
706 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base), seg->frame_count);
707 seg->is_first_pkt = 0;
710 av_log(s, AV_LOG_DEBUG, "stream:%d start_pts_time:%s pts:%s pts_time:%s dts:%s dts_time:%s",
712 av_ts2timestr(seg->cur_entry.start_pts, &AV_TIME_BASE_Q),
713 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
714 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
716 /* compute new timestamps */
717 offset = av_rescale_q(seg->initial_offset - (seg->reset_timestamps ? seg->cur_entry.start_pts : 0),
718 AV_TIME_BASE_Q, st->time_base);
719 if (pkt->pts != AV_NOPTS_VALUE)
721 if (pkt->dts != AV_NOPTS_VALUE)
724 av_log(s, AV_LOG_DEBUG, " -> pts:%s pts_time:%s dts:%s dts_time:%s\n",
725 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base),
726 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &st->time_base));
728 ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
731 if (pkt->stream_index == seg->reference_stream_index)
736 avio_close(seg->list_pb);
737 avformat_free_context(oc);
743 static int seg_write_trailer(struct AVFormatContext *s)
745 SegmentContext *seg = s->priv_data;
746 AVFormatContext *oc = seg->avf;
747 SegmentListEntry *cur, *next;
750 if (!seg->write_header_trailer) {
751 if ((ret = segment_end(s, 0, 1)) < 0)
753 open_null_ctx(&oc->pb);
754 ret = av_write_trailer(oc);
755 close_null_ctx(oc->pb);
757 ret = segment_end(s, 1, 1);
761 avio_close(seg->list_pb);
764 av_freep(&seg->times);
765 av_freep(&seg->frames);
767 cur = seg->segment_list_entries;
770 av_free(cur->filename);
775 avformat_free_context(oc);
779 #define OFFSET(x) offsetof(SegmentContext, x)
780 #define E AV_OPT_FLAG_ENCODING_PARAM
781 static const AVOption options[] = {
782 { "reference_stream", "set reference stream", OFFSET(reference_stream_specifier), AV_OPT_TYPE_STRING, {.str = "auto"}, CHAR_MIN, CHAR_MAX, E },
783 { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
784 { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
786 { "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"},
787 { "cache", "allow list caching", 0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_CACHE }, INT_MIN, INT_MAX, E, "list_flags"},
788 { "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"},
790 { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
791 { "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 },
793 { "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" },
794 { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, E, "list_type" },
795 { "csv", "csv format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_CSV }, INT_MIN, INT_MAX, E, "list_type" },
796 { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_EXT }, INT_MIN, INT_MAX, E, "list_type" },
797 { "ffconcat", "ffconcat format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FFCONCAT }, INT_MIN, INT_MAX, E, "list_type" },
798 { "m3u8", "M3U8 format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
799 { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
801 { "segment_time", "set segment duration", OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
802 { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta), AV_OPT_TYPE_DURATION, {.i64 = 0}, 0, 0, E },
803 { "segment_times", "set segment split time points", OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
804 { "segment_frames", "set segment split frame numbers", OFFSET(frames_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
805 { "segment_wrap", "set number after which the index wraps", OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
806 { "segment_start_number", "set the sequence number of the first segment", OFFSET(segment_idx), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
808 { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
809 { "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 },
810 { "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, E },
811 { "initial_offset", "set initial timestamp offset", OFFSET(initial_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX, E },
815 static const AVClass seg_class = {
816 .class_name = "segment muxer",
817 .item_name = av_default_item_name,
819 .version = LIBAVUTIL_VERSION_INT,
822 AVOutputFormat ff_segment_muxer = {
824 .long_name = NULL_IF_CONFIG_SMALL("segment"),
825 .priv_data_size = sizeof(SegmentContext),
826 .flags = AVFMT_NOFILE|AVFMT_GLOBALHEADER,
827 .write_header = seg_write_header,
828 .write_packet = seg_write_packet,
829 .write_trailer = seg_write_trailer,
830 .priv_class = &seg_class,
833 static const AVClass sseg_class = {
834 .class_name = "stream_segment muxer",
835 .item_name = av_default_item_name,
837 .version = LIBAVUTIL_VERSION_INT,
840 AVOutputFormat ff_stream_segment_muxer = {
841 .name = "stream_segment,ssegment",
842 .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
843 .priv_data_size = sizeof(SegmentContext),
844 .flags = AVFMT_NOFILE,
845 .write_header = seg_write_header,
846 .write_packet = seg_write_packet,
847 .write_trailer = seg_write_trailer,
848 .priv_class = &sseg_class,