]> git.sesse.net Git - ffmpeg/blob - libavformat/segment.c
lavf: split packets before muxing.
[ffmpeg] / libavformat / segment.c
1 /*
2  * Copyright (c) 2011, Luca Barbato
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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.
10  *
11  * Libav 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.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file generic segmenter
23  * M3U8 specification can be find here:
24  * @url{http://tools.ietf.org/id/draft-pantos-http-live-streaming-08.txt}
25  */
26
27 #include <float.h>
28
29 #include "avformat.h"
30 #include "internal.h"
31
32 #include "libavutil/log.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/avstring.h"
35 #include "libavutil/parseutils.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/timestamp.h"
38
39 typedef enum {
40     LIST_TYPE_UNDEFINED = -1,
41     LIST_TYPE_FLAT = 0,
42     LIST_TYPE_CSV,
43     LIST_TYPE_M3U8,
44     LIST_TYPE_EXT, ///< deprecated
45     LIST_TYPE_NB,
46 } ListType;
47
48
49 #define SEGMENT_LIST_FLAG_CACHE 1
50 #define SEGMENT_LIST_FLAG_LIVE  2
51
52 typedef struct {
53     const AVClass *class;  /**< Class for private options. */
54     int segment_idx;       ///< index of the segment file to write, starting from 0
55     int segment_idx_wrap;  ///< number after which the index wraps
56     int segment_count;     ///< number of segment files already written
57     AVOutputFormat *oformat;
58     AVFormatContext *avf;
59     char *format;          ///< format to use for output segment files
60     char *list;            ///< filename for the segment list file
61     int   list_flags;      ///< flags affecting list generation
62     int   list_size;       ///< number of entries for the segment list file
63     double list_max_segment_time; ///< max segment time in the current list
64     ListType list_type;    ///< set the list type
65     AVIOContext *list_pb;  ///< list file put-byte context
66     char *time_str;        ///< segment duration specification string
67     int64_t time;          ///< segment duration
68     char *times_str;       ///< segment times specification string
69     int64_t *times;        ///< list of segment interval specification
70     int nb_times;          ///< number of elments in the times array
71     char *time_delta_str;  ///< approximation value duration used for the segment times
72     int64_t time_delta;
73     int  individual_header_trailer; /**< Set by a private option. */
74     int  write_header_trailer; /**< Set by a private option. */
75
76     int reset_timestamps;  ///< reset timestamps at the begin of each segment
77     int has_video;
78     double start_time, end_time;
79     int64_t start_pts, start_dts;
80     int is_first_pkt;      ///< tells if it is the first packet in the segment
81 } SegmentContext;
82
83 static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
84 {
85     int needs_quoting = !!str[strcspn(str, "\",\n\r")];
86
87     if (needs_quoting)
88         avio_w8(ctx, '"');
89
90     for (; *str; str++) {
91         if (*str == '"')
92             avio_w8(ctx, '"');
93         avio_w8(ctx, *str);
94     }
95     if (needs_quoting)
96         avio_w8(ctx, '"');
97 }
98
99 static int segment_mux_init(AVFormatContext *s)
100 {
101     SegmentContext *seg = s->priv_data;
102     AVFormatContext *oc;
103     int i;
104
105     seg->avf = oc = avformat_alloc_context();
106     if (!oc)
107         return AVERROR(ENOMEM);
108
109     oc->oformat            = seg->oformat;
110     oc->interrupt_callback = s->interrupt_callback;
111
112     for (i = 0; i < s->nb_streams; i++) {
113         AVStream *st;
114         AVCodecContext *icodec, *ocodec;
115
116         if (!(st = avformat_new_stream(oc, NULL)))
117             return AVERROR(ENOMEM);
118         icodec = s->streams[i]->codec;
119         ocodec = st->codec;
120         avcodec_copy_context(ocodec, icodec);
121         if (!oc->oformat->codec_tag ||
122             av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == ocodec->codec_id ||
123             av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0) {
124             ocodec->codec_tag = icodec->codec_tag;
125         } else {
126             ocodec->codec_tag = 0;
127         }
128         st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
129     }
130
131     return 0;
132 }
133
134 static int set_segment_filename(AVFormatContext *s)
135 {
136     SegmentContext *seg = s->priv_data;
137     AVFormatContext *oc = seg->avf;
138
139     if (seg->segment_idx_wrap)
140         seg->segment_idx %= seg->segment_idx_wrap;
141     if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
142                               s->filename, seg->segment_idx) < 0) {
143         av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
144         return AVERROR(EINVAL);
145     }
146     return 0;
147 }
148
149 static int segment_start(AVFormatContext *s, int write_header)
150 {
151     SegmentContext *seg = s->priv_data;
152     AVFormatContext *oc = seg->avf;
153     int err = 0;
154
155     if (write_header) {
156         avformat_free_context(oc);
157         seg->avf = NULL;
158         if ((err = segment_mux_init(s)) < 0)
159             return err;
160         oc = seg->avf;
161     }
162
163     seg->segment_idx++;
164     if ((err = set_segment_filename(s)) < 0)
165         return err;
166     seg->segment_count++;
167
168     if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
169                           &s->interrupt_callback, NULL)) < 0)
170         return err;
171
172     if (oc->oformat->priv_class && oc->priv_data)
173         av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
174
175     if (write_header) {
176         if ((err = avformat_write_header(oc, NULL)) < 0)
177             return err;
178     }
179
180     seg->is_first_pkt = 1;
181     return 0;
182 }
183
184 static int segment_list_open(AVFormatContext *s)
185 {
186     SegmentContext *seg = s->priv_data;
187     int ret;
188
189     ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
190                      &s->interrupt_callback, NULL);
191     if (ret < 0)
192         return ret;
193     seg->list_max_segment_time = 0;
194
195     if (seg->list_type == LIST_TYPE_M3U8) {
196         avio_printf(seg->list_pb, "#EXTM3U\n");
197         avio_printf(seg->list_pb, "#EXT-X-VERSION:3\n");
198         avio_printf(seg->list_pb, "#EXT-X-MEDIA-SEQUENCE:%d\n", seg->segment_idx);
199         avio_printf(seg->list_pb, "#EXT-X-ALLOWCACHE:%d\n",
200                     !!(seg->list_flags & SEGMENT_LIST_FLAG_CACHE));
201         if (seg->list_flags & SEGMENT_LIST_FLAG_LIVE)
202             avio_printf(seg->list_pb,
203                         "#EXT-X-TARGETDURATION:%"PRId64"\n", seg->time / 1000000);
204     }
205
206     return ret;
207 }
208
209 static void segment_list_close(AVFormatContext *s)
210 {
211     SegmentContext *seg = s->priv_data;
212
213     if (seg->list_type == LIST_TYPE_M3U8) {
214         if (!(seg->list_flags & SEGMENT_LIST_FLAG_LIVE))
215             avio_printf(seg->list_pb, "#EXT-X-TARGETDURATION:%d\n",
216                         (int)ceil(seg->list_max_segment_time));
217         avio_printf(seg->list_pb, "#EXT-X-ENDLIST\n");
218     }
219
220     avio_close(seg->list_pb);
221 }
222
223 static int segment_end(AVFormatContext *s, int write_trailer)
224 {
225     SegmentContext *seg = s->priv_data;
226     AVFormatContext *oc = seg->avf;
227     int ret = 0;
228
229     av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
230     if (write_trailer)
231         ret = av_write_trailer(oc);
232
233     if (ret < 0)
234         av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
235                oc->filename);
236
237     if (seg->list) {
238         if (seg->list_size && !(seg->segment_count % seg->list_size)) {
239             segment_list_close(s);
240             if ((ret = segment_list_open(s)) < 0)
241                 goto end;
242         }
243
244         if (seg->list_type == LIST_TYPE_FLAT) {
245             avio_printf(seg->list_pb, "%s\n", oc->filename);
246         } else if (seg->list_type == LIST_TYPE_CSV || seg->list_type == LIST_TYPE_EXT) {
247             print_csv_escaped_str(seg->list_pb, oc->filename);
248             avio_printf(seg->list_pb, ",%f,%f\n", seg->start_time, seg->end_time);
249         } else if (seg->list_type == LIST_TYPE_M3U8) {
250             avio_printf(seg->list_pb, "#EXTINF:%f,\n%s\n",
251                         seg->end_time - seg->start_time, oc->filename);
252         }
253         seg->list_max_segment_time = FFMAX(seg->end_time - seg->start_time, seg->list_max_segment_time);
254         avio_flush(seg->list_pb);
255     }
256
257 end:
258     avio_close(oc->pb);
259
260     return ret;
261 }
262
263 static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
264                        const char *times_str)
265 {
266     char *p;
267     int i, ret = 0;
268     char *times_str1 = av_strdup(times_str);
269     char *saveptr = NULL;
270
271     if (!times_str1)
272         return AVERROR(ENOMEM);
273
274 #define FAIL(err) ret = err; goto end
275
276     *nb_times = 1;
277     for (p = times_str1; *p; p++)
278         if (*p == ',')
279             (*nb_times)++;
280
281     *times = av_malloc(sizeof(**times) * *nb_times);
282     if (!*times) {
283         av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array\n");
284         FAIL(AVERROR(ENOMEM));
285     }
286
287     p = times_str1;
288     for (i = 0; i < *nb_times; i++) {
289         int64_t t;
290         char *tstr = av_strtok(p, ",", &saveptr);
291         p = NULL;
292
293         if (!tstr || !tstr[0]) {
294             av_log(log_ctx, AV_LOG_ERROR, "Empty time specification in times list %s\n",
295                    times_str);
296             FAIL(AVERROR(EINVAL));
297         }
298
299         ret = av_parse_time(&t, tstr, 1);
300         if (ret < 0) {
301             av_log(log_ctx, AV_LOG_ERROR,
302                    "Invalid time duration specification '%s' in times list %s\n", tstr, times_str);
303             FAIL(AVERROR(EINVAL));
304         }
305         (*times)[i] = t;
306
307         /* check on monotonicity */
308         if (i && (*times)[i-1] > (*times)[i]) {
309             av_log(log_ctx, AV_LOG_ERROR,
310                    "Specified time %f is greater than the following time %f\n",
311                    (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000);
312             FAIL(AVERROR(EINVAL));
313         }
314     }
315
316 end:
317     av_free(times_str1);
318     return ret;
319 }
320
321 static int open_null_ctx(AVIOContext **ctx)
322 {
323     int buf_size = 32768;
324     uint8_t *buf = av_malloc(buf_size);
325     if (!buf)
326         return AVERROR(ENOMEM);
327     *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
328     if (!*ctx) {
329         av_free(buf);
330         return AVERROR(ENOMEM);
331     }
332     return 0;
333 }
334
335 static void close_null_ctx(AVIOContext *pb)
336 {
337     av_free(pb->buffer);
338     av_free(pb);
339 }
340
341 static int seg_write_header(AVFormatContext *s)
342 {
343     SegmentContext *seg = s->priv_data;
344     AVFormatContext *oc = NULL;
345     int ret, i;
346
347     seg->segment_count = 0;
348     if (!seg->write_header_trailer)
349         seg->individual_header_trailer = 0;
350
351     if (seg->time_str && seg->times_str) {
352         av_log(s, AV_LOG_ERROR,
353                "segment_time and segment_times options are mutually exclusive, select just one of them\n");
354         return AVERROR(EINVAL);
355     }
356
357     if ((seg->list_flags & SEGMENT_LIST_FLAG_LIVE) && seg->times_str) {
358         av_log(s, AV_LOG_ERROR,
359                "segment_flags +live and segment_times options are mutually exclusive:"
360                "specify -segment_time if you want a live-friendly list\n");
361         return AVERROR(EINVAL);
362     }
363
364     if (seg->times_str) {
365         if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
366             return ret;
367     } else {
368         /* set default value if not specified */
369         if (!seg->time_str)
370             seg->time_str = av_strdup("2");
371         if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
372             av_log(s, AV_LOG_ERROR,
373                    "Invalid time duration specification '%s' for segment_time option\n",
374                    seg->time_str);
375             return ret;
376         }
377     }
378
379     if (seg->time_delta_str) {
380         if ((ret = av_parse_time(&seg->time_delta, seg->time_delta_str, 1)) < 0) {
381             av_log(s, AV_LOG_ERROR,
382                    "Invalid time duration specification '%s' for delta option\n",
383                    seg->time_delta_str);
384             return ret;
385         }
386     }
387
388     if (seg->list) {
389         if (seg->list_type == LIST_TYPE_UNDEFINED) {
390             if      (av_match_ext(seg->list, "csv" )) seg->list_type = LIST_TYPE_CSV;
391             else if (av_match_ext(seg->list, "ext" )) seg->list_type = LIST_TYPE_EXT;
392             else if (av_match_ext(seg->list, "m3u8")) seg->list_type = LIST_TYPE_M3U8;
393             else                                      seg->list_type = LIST_TYPE_FLAT;
394         }
395         if ((ret = segment_list_open(s)) < 0)
396             goto fail;
397     }
398     if (seg->list_type == LIST_TYPE_EXT)
399         av_log(s, AV_LOG_WARNING, "'ext' list type option is deprecated in favor of 'csv'\n");
400
401     for (i = 0; i < s->nb_streams; i++)
402         seg->has_video +=
403             (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
404
405     if (seg->has_video > 1)
406         av_log(s, AV_LOG_WARNING,
407                "More than a single video stream present, "
408                "expect issues decoding it.\n");
409
410     seg->oformat = av_guess_format(seg->format, s->filename, NULL);
411
412     if (!seg->oformat) {
413         ret = AVERROR_MUXER_NOT_FOUND;
414         goto fail;
415     }
416     if (seg->oformat->flags & AVFMT_NOFILE) {
417         av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
418                seg->oformat->name);
419         ret = AVERROR(EINVAL);
420         goto fail;
421     }
422
423     if ((ret = segment_mux_init(s)) < 0)
424         goto fail;
425     oc = seg->avf;
426
427     if ((ret = set_segment_filename(s)) < 0)
428         goto fail;
429     seg->segment_count++;
430
431     if (seg->write_header_trailer) {
432         if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
433                               &s->interrupt_callback, NULL)) < 0)
434             goto fail;
435     } else {
436         if ((ret = open_null_ctx(&oc->pb)) < 0)
437             goto fail;
438     }
439
440     if ((ret = avformat_write_header(oc, NULL)) < 0) {
441         avio_close(oc->pb);
442         goto fail;
443     }
444     seg->is_first_pkt = 1;
445
446     if (oc->avoid_negative_ts > 0 && s->avoid_negative_ts < 0)
447         s->avoid_negative_ts = 1;
448
449     if (!seg->write_header_trailer) {
450         close_null_ctx(oc->pb);
451         if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
452                               &s->interrupt_callback, NULL)) < 0)
453             goto fail;
454     }
455
456 fail:
457     if (ret) {
458         if (seg->list)
459             segment_list_close(s);
460         if (seg->avf)
461             avformat_free_context(seg->avf);
462     }
463     return ret;
464 }
465
466 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
467 {
468     SegmentContext *seg = s->priv_data;
469     AVFormatContext *oc = seg->avf;
470     AVStream *st = s->streams[pkt->stream_index];
471     int64_t end_pts;
472     int ret;
473
474     if (seg->times) {
475         end_pts = seg->segment_count <= seg->nb_times ?
476             seg->times[seg->segment_count-1] : INT64_MAX;
477     } else {
478         end_pts = seg->time * seg->segment_count;
479     }
480
481     /* if the segment has video, start a new segment *only* with a key video frame */
482     if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
483         pkt->pts != AV_NOPTS_VALUE &&
484         av_compare_ts(pkt->pts, st->time_base,
485                       end_pts-seg->time_delta, AV_TIME_BASE_Q) >= 0 &&
486         pkt->flags & AV_PKT_FLAG_KEY) {
487         ret = segment_end(s, seg->individual_header_trailer);
488
489         if (!ret)
490             ret = segment_start(s, seg->individual_header_trailer);
491
492         if (ret)
493             goto fail;
494
495         oc = seg->avf;
496
497         seg->start_time = (double)pkt->pts * av_q2d(st->time_base);
498         seg->start_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);
499         seg->start_dts = pkt->dts != AV_NOPTS_VALUE ?
500             av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q) : seg->start_pts;
501     } else if (pkt->pts != AV_NOPTS_VALUE) {
502         seg->end_time = FFMAX(seg->end_time,
503                               (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
504     }
505
506     if (seg->is_first_pkt) {
507         av_log(s, AV_LOG_DEBUG, "segment:'%s' starts with packet stream:%d pts:%s pts_time:%s\n",
508                seg->avf->filename, pkt->stream_index,
509                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &st->time_base));
510         seg->is_first_pkt = 0;
511     }
512
513     if (seg->reset_timestamps) {
514         av_log(s, AV_LOG_DEBUG, "start_pts:%s pts:%s start_dts:%s dts:%s",
515                av_ts2timestr(seg->start_pts, &AV_TIME_BASE_Q), av_ts2timestr(pkt->pts, &st->time_base),
516                av_ts2timestr(seg->start_dts, &AV_TIME_BASE_Q), av_ts2timestr(pkt->dts, &st->time_base));
517
518         /* compute new timestamps */
519         if (pkt->pts != AV_NOPTS_VALUE)
520             pkt->pts -= av_rescale_q(seg->start_pts, AV_TIME_BASE_Q, st->time_base);
521         if (pkt->dts != AV_NOPTS_VALUE)
522             pkt->dts -= av_rescale_q(seg->start_dts, AV_TIME_BASE_Q, st->time_base);
523
524         av_log(s, AV_LOG_DEBUG, " -> pts:%s dts:%s\n",
525                av_ts2timestr(pkt->pts, &st->time_base), av_ts2timestr(pkt->dts, &st->time_base));
526     }
527
528     ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
529
530 fail:
531     if (ret < 0) {
532         if (seg->list)
533             avio_close(seg->list_pb);
534         avformat_free_context(oc);
535     }
536
537     return ret;
538 }
539
540 static int seg_write_trailer(struct AVFormatContext *s)
541 {
542     SegmentContext *seg = s->priv_data;
543     AVFormatContext *oc = seg->avf;
544     int ret;
545     if (!seg->write_header_trailer) {
546         if ((ret = segment_end(s, 0)) < 0)
547             goto fail;
548         open_null_ctx(&oc->pb);
549         ret = av_write_trailer(oc);
550         close_null_ctx(oc->pb);
551     } else {
552         ret = segment_end(s, 1);
553     }
554 fail:
555     if (seg->list)
556         segment_list_close(s);
557
558     av_opt_free(seg);
559     av_freep(&seg->times);
560
561     avformat_free_context(oc);
562     return ret;
563 }
564
565 #define OFFSET(x) offsetof(SegmentContext, x)
566 #define E AV_OPT_FLAG_ENCODING_PARAM
567 static const AVOption options[] = {
568     { "segment_format",    "set container format used for the segments", OFFSET(format),  AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
569     { "segment_list",      "set the segment list filename",              OFFSET(list),    AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
570
571     { "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"},
572     { "cache",             "allow list caching",                                    0, AV_OPT_TYPE_CONST, {.i64 = SEGMENT_LIST_FLAG_CACHE }, INT_MIN, INT_MAX,   E, "list_flags"},
573     { "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"},
574
575     { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT,  {.i64 = 0},     0, INT_MAX, E },
576
577     { "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" },
578     { "flat", "flat format",     0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, E, "list_type" },
579     { "csv",  "csv format",      0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_CSV  }, INT_MIN, INT_MAX, E, "list_type" },
580     { "ext",  "extended format", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_EXT  }, INT_MIN, INT_MAX, E, "list_type" },
581     { "m3u8", "M3U8 format",     0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
582     { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64=LIST_TYPE_M3U8 }, INT_MIN, INT_MAX, E, "list_type" },
583
584     { "segment_time",      "set segment duration",                       OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
585     { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta_str), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, E },
586     { "segment_times",     "set segment split time points",              OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL},  0, 0,       E },
587     { "segment_wrap",      "set number after which the index wraps",     OFFSET(segment_idx_wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
588     { "segment_start_number", "set the sequence number of the first segment", OFFSET(segment_idx), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
589
590     { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
591     { "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 },
592     { "reset_timestamps", "reset timestamps at the begin of each segment", OFFSET(reset_timestamps), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, E },
593     { NULL },
594 };
595
596 static const AVClass seg_class = {
597     .class_name = "segment muxer",
598     .item_name  = av_default_item_name,
599     .option     = options,
600     .version    = LIBAVUTIL_VERSION_INT,
601 };
602
603 AVOutputFormat ff_segment_muxer = {
604     .name           = "segment",
605     .long_name      = NULL_IF_CONFIG_SMALL("segment"),
606     .priv_data_size = sizeof(SegmentContext),
607     .flags          = AVFMT_NOFILE|AVFMT_GLOBALHEADER,
608     .write_header   = seg_write_header,
609     .write_packet   = seg_write_packet,
610     .write_trailer  = seg_write_trailer,
611     .priv_class     = &seg_class,
612 };
613
614 static const AVClass sseg_class = {
615     .class_name = "stream_segment muxer",
616     .item_name  = av_default_item_name,
617     .option     = options,
618     .version    = LIBAVUTIL_VERSION_INT,
619 };
620
621 AVOutputFormat ff_stream_segment_muxer = {
622     .name           = "stream_segment,ssegment",
623     .long_name      = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
624     .priv_data_size = sizeof(SegmentContext),
625     .flags          = AVFMT_NOFILE,
626     .write_header   = seg_write_header,
627     .write_packet   = seg_write_packet,
628     .write_trailer  = seg_write_trailer,
629     .priv_class     = &sseg_class,
630 };