2 * Copyright (c) 2011, Luca Barbato
4 * This file is part of Libav.
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.
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.
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
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}
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"
40 LIST_TYPE_UNDEFINED = -1,
44 LIST_TYPE_EXT, ///< deprecated
49 #define SEGMENT_LIST_FLAG_CACHE 1
50 #define SEGMENT_LIST_FLAG_LIVE 2
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;
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
73 int individual_header_trailer; /**< Set by a private option. */
74 int write_header_trailer; /**< Set by a private option. */
76 int reset_timestamps; ///< reset timestamps at the begin of each segment
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
83 static void print_csv_escaped_str(AVIOContext *ctx, const char *str)
85 int needs_quoting = !!str[strcspn(str, "\",\n\r")];
99 static int segment_mux_init(AVFormatContext *s)
101 SegmentContext *seg = s->priv_data;
105 seg->avf = oc = avformat_alloc_context();
107 return AVERROR(ENOMEM);
109 oc->oformat = seg->oformat;
110 oc->interrupt_callback = s->interrupt_callback;
112 for (i = 0; i < s->nb_streams; i++) {
114 AVCodecContext *icodec, *ocodec;
116 if (!(st = avformat_new_stream(oc, NULL)))
117 return AVERROR(ENOMEM);
118 icodec = s->streams[i]->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;
126 ocodec->codec_tag = 0;
128 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
134 static int set_segment_filename(AVFormatContext *s)
136 SegmentContext *seg = s->priv_data;
137 AVFormatContext *oc = seg->avf;
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);
149 static int segment_start(AVFormatContext *s, int write_header)
151 SegmentContext *seg = s->priv_data;
152 AVFormatContext *oc = seg->avf;
156 avformat_free_context(oc);
158 if ((err = segment_mux_init(s)) < 0)
164 if ((err = set_segment_filename(s)) < 0)
166 seg->segment_count++;
168 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
169 &s->interrupt_callback, NULL)) < 0)
172 if (oc->oformat->priv_class && oc->priv_data)
173 av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
176 if ((err = avformat_write_header(oc, NULL)) < 0)
180 seg->is_first_pkt = 1;
184 static int segment_list_open(AVFormatContext *s)
186 SegmentContext *seg = s->priv_data;
189 ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
190 &s->interrupt_callback, NULL);
193 seg->list_max_segment_time = 0;
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);
209 static void segment_list_close(AVFormatContext *s)
211 SegmentContext *seg = s->priv_data;
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");
220 avio_close(seg->list_pb);
223 static int segment_end(AVFormatContext *s, int write_trailer)
225 SegmentContext *seg = s->priv_data;
226 AVFormatContext *oc = seg->avf;
229 av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
231 ret = av_write_trailer(oc);
234 av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
238 if (seg->list_size && !(seg->segment_count % seg->list_size)) {
239 segment_list_close(s);
240 if ((ret = segment_list_open(s)) < 0)
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);
253 seg->list_max_segment_time = FFMAX(seg->end_time - seg->start_time, seg->list_max_segment_time);
254 avio_flush(seg->list_pb);
263 static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
264 const char *times_str)
268 char *times_str1 = av_strdup(times_str);
269 char *saveptr = NULL;
272 return AVERROR(ENOMEM);
274 #define FAIL(err) ret = err; goto end
277 for (p = times_str1; *p; p++)
281 *times = av_malloc(sizeof(**times) * *nb_times);
283 av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array\n");
284 FAIL(AVERROR(ENOMEM));
288 for (i = 0; i < *nb_times; i++) {
290 char *tstr = av_strtok(p, ",", &saveptr);
293 if (!tstr || !tstr[0]) {
294 av_log(log_ctx, AV_LOG_ERROR, "Empty time specification in times list %s\n",
296 FAIL(AVERROR(EINVAL));
299 ret = av_parse_time(&t, tstr, 1);
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));
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));
321 static int open_null_ctx(AVIOContext **ctx)
323 int buf_size = 32768;
324 uint8_t *buf = av_malloc(buf_size);
326 return AVERROR(ENOMEM);
327 *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
330 return AVERROR(ENOMEM);
335 static void close_null_ctx(AVIOContext *pb)
341 static int seg_write_header(AVFormatContext *s)
343 SegmentContext *seg = s->priv_data;
344 AVFormatContext *oc = NULL;
347 seg->segment_count = 0;
348 if (!seg->write_header_trailer)
349 seg->individual_header_trailer = 0;
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);
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);
364 if (seg->times_str) {
365 if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
368 /* set default value if not specified */
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",
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);
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;
395 if ((ret = segment_list_open(s)) < 0)
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");
401 for (i = 0; i < s->nb_streams; i++)
403 (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
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");
410 seg->oformat = av_guess_format(seg->format, s->filename, NULL);
413 ret = AVERROR_MUXER_NOT_FOUND;
416 if (seg->oformat->flags & AVFMT_NOFILE) {
417 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
419 ret = AVERROR(EINVAL);
423 if ((ret = segment_mux_init(s)) < 0)
427 if ((ret = set_segment_filename(s)) < 0)
429 seg->segment_count++;
431 if (seg->write_header_trailer) {
432 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
433 &s->interrupt_callback, NULL)) < 0)
436 if ((ret = open_null_ctx(&oc->pb)) < 0)
440 if ((ret = avformat_write_header(oc, NULL)) < 0) {
444 seg->is_first_pkt = 1;
446 if (oc->avoid_negative_ts > 0 && s->avoid_negative_ts < 0)
447 s->avoid_negative_ts = 1;
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)
459 segment_list_close(s);
461 avformat_free_context(seg->avf);
466 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
468 SegmentContext *seg = s->priv_data;
469 AVFormatContext *oc = seg->avf;
470 AVStream *st = s->streams[pkt->stream_index];
475 end_pts = seg->segment_count <= seg->nb_times ?
476 seg->times[seg->segment_count-1] : INT64_MAX;
478 end_pts = seg->time * seg->segment_count;
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);
490 ret = segment_start(s, seg->individual_header_trailer);
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));
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;
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));
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);
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));
528 ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
533 avio_close(seg->list_pb);
534 avformat_free_context(oc);
540 static int seg_write_trailer(struct AVFormatContext *s)
542 SegmentContext *seg = s->priv_data;
543 AVFormatContext *oc = seg->avf;
545 if (!seg->write_header_trailer) {
546 if ((ret = segment_end(s, 0)) < 0)
548 open_null_ctx(&oc->pb);
549 ret = av_write_trailer(oc);
550 close_null_ctx(oc->pb);
552 ret = segment_end(s, 1);
556 segment_list_close(s);
559 av_freep(&seg->times);
561 avformat_free_context(oc);
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 },
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"},
575 { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
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" },
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 },
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 },
596 static const AVClass seg_class = {
597 .class_name = "segment muxer",
598 .item_name = av_default_item_name,
600 .version = LIBAVUTIL_VERSION_INT,
603 AVOutputFormat ff_segment_muxer = {
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,
614 static const AVClass sseg_class = {
615 .class_name = "stream_segment muxer",
616 .item_name = av_default_item_name,
618 .version = LIBAVUTIL_VERSION_INT,
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,