3 * Copyright (c) 2011, Luca Barbato
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/log.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/parseutils.h"
31 #include "libavutil/mathematics.h"
34 const AVClass *class; /**< Class for private options. */
36 AVOutputFormat *oformat;
38 char *format; /**< Set by a private option. */
39 char *list; /**< Set by a private option. */
40 int list_type; /**< Set by a private option. */
41 float time; /**< Set by a private option. */
42 int size; /**< Set by a private option. */
43 int wrap; /**< Set by a private option. */
44 int individual_header_trailer; /**< Set by a private option. */
45 int write_header_trailer; /**< Set by a private option. */
47 int64_t recording_time;
57 static int segment_mux_init(AVFormatContext *s)
59 SegmentContext *seg = s->priv_data;
63 seg->avf = oc = avformat_alloc_context();
65 return AVERROR(ENOMEM);
67 oc->oformat = seg->oformat;
68 oc->interrupt_callback = s->interrupt_callback;
70 for (i = 0; i < s->nb_streams; i++) {
72 if (!(st = avformat_new_stream(oc, NULL)))
73 return AVERROR(ENOMEM);
74 avcodec_copy_context(st->codec, s->streams[i]->codec);
75 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
81 static int segment_hls_window(AVFormatContext *s, int last)
83 SegmentContext *seg = s->priv_data;
87 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
88 &s->interrupt_callback, NULL)) < 0)
91 avio_printf(seg->pb, "#EXTM3U\n");
92 avio_printf(seg->pb, "#EXT-X-VERSION:3\n");
93 avio_printf(seg->pb, "#EXT-X-TARGETDURATION:%d\n", (int)seg->time);
94 avio_printf(seg->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
95 FFMAX(0, seg->number - seg->size));
97 for (i = FFMAX(0, seg->number - seg->size);
98 i < seg->number; i++) {
99 avio_printf(seg->pb, "#EXTINF:%d,\n", (int)seg->time);
100 av_get_frame_filename(buf, sizeof(buf), s->filename, i);
101 avio_printf(seg->pb, "%s\n", buf);
105 avio_printf(seg->pb, "#EXT-X-ENDLIST\n");
107 avio_closep(&seg->pb);
111 static int segment_start(AVFormatContext *s, int write_header)
113 SegmentContext *c = s->priv_data;
114 AVFormatContext *oc = c->avf;
118 avformat_free_context(oc);
120 if ((err = segment_mux_init(s)) < 0)
126 c->number %= c->wrap;
128 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
129 s->filename, c->number++) < 0)
130 return AVERROR(EINVAL);
132 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
133 &s->interrupt_callback, NULL)) < 0)
136 if (oc->oformat->priv_class && oc->priv_data)
137 av_opt_set(oc->priv_data, "resend_headers", "1", 0); /* mpegts specific */
140 if ((err = avformat_write_header(oc, NULL)) < 0)
147 static int segment_end(AVFormatContext *oc, int write_trailer)
151 av_write_frame(oc, NULL); /* Flush any buffered data (fragmented mp4) */
153 av_write_trailer(oc);
159 static int open_null_ctx(AVIOContext **ctx)
161 int buf_size = 32768;
162 uint8_t *buf = av_malloc(buf_size);
164 return AVERROR(ENOMEM);
165 *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
168 return AVERROR(ENOMEM);
173 static void close_null_ctx(AVIOContext *pb)
179 static int seg_write_header(AVFormatContext *s)
181 SegmentContext *seg = s->priv_data;
182 AVFormatContext *oc = NULL;
186 seg->offset_time = 0;
187 seg->recording_time = seg->time * 1000000;
188 if (!seg->write_header_trailer)
189 seg->individual_header_trailer = 0;
191 if (seg->list && seg->list_type != LIST_HLS)
192 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
193 &s->interrupt_callback, NULL)) < 0)
196 for (i = 0; i < s->nb_streams; i++)
198 (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
200 if (seg->has_video > 1)
201 av_log(s, AV_LOG_WARNING,
202 "More than a single video stream present, "
203 "expect issues decoding it.\n");
205 seg->oformat = av_guess_format(seg->format, s->filename, NULL);
208 ret = AVERROR_MUXER_NOT_FOUND;
211 if (seg->oformat->flags & AVFMT_NOFILE) {
212 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
214 ret = AVERROR(EINVAL);
218 if ((ret = segment_mux_init(s)) < 0)
222 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
223 s->filename, seg->number++) < 0) {
224 ret = AVERROR(EINVAL);
228 if (seg->write_header_trailer) {
229 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
230 &s->interrupt_callback, NULL)) < 0)
233 if ((ret = open_null_ctx(&oc->pb)) < 0)
237 if ((ret = avformat_write_header(oc, NULL)) < 0) {
242 if (!seg->write_header_trailer) {
243 close_null_ctx(oc->pb);
244 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
245 &s->interrupt_callback, NULL)) < 0)
250 if (seg->list_type == LIST_HLS) {
251 if ((ret = segment_hls_window(s, 0)) < 0)
254 avio_printf(seg->pb, "%s\n", oc->filename);
264 avformat_free_context(seg->avf);
269 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
271 SegmentContext *seg = s->priv_data;
272 AVFormatContext *oc = seg->avf;
273 AVStream *st = s->streams[pkt->stream_index];
274 int64_t end_pts = seg->recording_time * seg->number;
275 int ret, can_split = 1;
277 if (seg->has_video) {
278 can_split = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
279 pkt->flags & AV_PKT_FLAG_KEY;
282 if (can_split && av_compare_ts(pkt->pts, st->time_base, end_pts,
283 AV_TIME_BASE_Q) >= 0) {
284 av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
285 pkt->stream_index, pkt->pts);
287 ret = segment_end(oc, seg->individual_header_trailer);
290 ret = segment_start(s, seg->individual_header_trailer);
298 if (seg->list_type == LIST_HLS) {
299 if ((ret = segment_hls_window(s, 0)) < 0)
302 avio_printf(seg->pb, "%s\n", oc->filename);
304 if (seg->size && !(seg->number % seg->size)) {
305 avio_closep(&seg->pb);
306 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
307 &s->interrupt_callback, NULL)) < 0)
314 ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
320 avformat_free_context(oc);
326 static int seg_write_trailer(struct AVFormatContext *s)
328 SegmentContext *seg = s->priv_data;
329 AVFormatContext *oc = seg->avf;
331 if (!seg->write_header_trailer) {
332 if ((ret = segment_end(oc, 0)) < 0)
334 open_null_ctx(&oc->pb);
335 ret = av_write_trailer(oc);
336 close_null_ctx(oc->pb);
338 ret = segment_end(oc, 1);
344 if (seg->list && seg->list_type == LIST_HLS) {
345 if ((ret = segment_hls_window(s, 1) < 0))
351 avformat_free_context(oc);
355 #define OFFSET(x) offsetof(SegmentContext, x)
356 #define E AV_OPT_FLAG_ENCODING_PARAM
357 static const AVOption options[] = {
358 { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
359 { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
360 { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
361 { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E },
362 { "segment_list_type", "segment list format", OFFSET(list_type), AV_OPT_TYPE_INT, {.i64 = LIST_FLAT}, 0, 2, E, "list_type" },
363 { "flat", "plain list (default)", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_FLAT}, 0, 0, E, "list_type" },
364 { "hls", "Apple HTTP Live Streaming compatible", 0, AV_OPT_TYPE_CONST, {.i64 = LIST_HLS}, 0, 0, E, "list_type" },
365 { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
366 { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
367 { "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 },
371 static const AVClass seg_class = {
372 .class_name = "segment muxer",
373 .item_name = av_default_item_name,
375 .version = LIBAVUTIL_VERSION_INT,
379 AVOutputFormat ff_segment_muxer = {
381 .long_name = NULL_IF_CONFIG_SMALL("segment"),
382 .priv_data_size = sizeof(SegmentContext),
383 .flags = AVFMT_NOFILE,
384 .write_header = seg_write_header,
385 .write_packet = seg_write_packet,
386 .write_trailer = seg_write_trailer,
387 .priv_class = &seg_class,