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