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 float time; /**< Set by a private option. */
41 int size; /**< Set by a private option. */
42 int wrap; /**< Set by a private option. */
43 int individual_header_trailer; /**< Set by a private option. */
44 int write_header_trailer; /**< Set by a private option. */
46 int64_t recording_time;
51 static int segment_mux_init(AVFormatContext *s)
53 SegmentContext *seg = s->priv_data;
57 seg->avf = oc = avformat_alloc_context();
59 return AVERROR(ENOMEM);
61 oc->oformat = seg->oformat;
62 oc->interrupt_callback = s->interrupt_callback;
64 for (i = 0; i < s->nb_streams; i++) {
66 if (!(st = avformat_new_stream(oc, NULL)))
67 return AVERROR(ENOMEM);
68 avcodec_copy_context(st->codec, s->streams[i]->codec);
69 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
75 static int segment_start(AVFormatContext *s, int write_header)
77 SegmentContext *c = s->priv_data;
78 AVFormatContext *oc = c->avf;
82 avformat_free_context(oc);
84 if ((err = segment_mux_init(s)) < 0)
92 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
93 s->filename, c->number++) < 0)
94 return AVERROR(EINVAL);
96 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
97 &s->interrupt_callback, NULL)) < 0)
100 if (oc->oformat->priv_class && oc->priv_data)
101 av_opt_set(oc->priv_data, "resend_headers", "1", 0);
104 if ((err = avformat_write_header(oc, NULL)) < 0)
111 static int segment_end(AVFormatContext *oc, int write_trailer)
115 av_write_frame(oc, NULL); /* Flush any buffered data */
117 av_write_trailer(oc);
123 static int open_null_ctx(AVIOContext **ctx)
125 int buf_size = 32768;
126 uint8_t *buf = av_malloc(buf_size);
128 return AVERROR(ENOMEM);
129 *ctx = avio_alloc_context(buf, buf_size, AVIO_FLAG_WRITE, NULL, NULL, NULL, NULL);
132 return AVERROR(ENOMEM);
137 static void close_null_ctx(AVIOContext *pb)
143 static int seg_write_header(AVFormatContext *s)
145 SegmentContext *seg = s->priv_data;
146 AVFormatContext *oc = NULL;
150 seg->offset_time = 0;
151 seg->recording_time = seg->time * 1000000;
152 if (!seg->write_header_trailer)
153 seg->individual_header_trailer = 0;
156 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
157 &s->interrupt_callback, NULL)) < 0)
160 for (i = 0; i < s->nb_streams; i++)
162 (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
164 if (seg->has_video > 1)
165 av_log(s, AV_LOG_WARNING,
166 "More than a single video stream present, "
167 "expect issues decoding it.\n");
169 seg->oformat = av_guess_format(seg->format, s->filename, NULL);
172 ret = AVERROR_MUXER_NOT_FOUND;
175 if (seg->oformat->flags & AVFMT_NOFILE) {
176 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
178 ret = AVERROR(EINVAL);
182 if ((ret = segment_mux_init(s)) < 0)
186 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
187 s->filename, seg->number++) < 0) {
188 ret = AVERROR(EINVAL);
192 if (seg->write_header_trailer) {
193 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
194 &s->interrupt_callback, NULL)) < 0)
197 if ((ret = open_null_ctx(&oc->pb)) < 0)
201 if ((ret = avformat_write_header(oc, NULL)) < 0) {
206 if (!seg->write_header_trailer) {
207 close_null_ctx(oc->pb);
208 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
209 &s->interrupt_callback, NULL)) < 0)
214 avio_printf(seg->pb, "%s\n", oc->filename);
223 avformat_free_context(seg->avf);
228 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
230 SegmentContext *seg = s->priv_data;
231 AVFormatContext *oc = seg->avf;
232 AVStream *st = s->streams[pkt->stream_index];
233 int64_t end_pts = seg->recording_time * seg->number;
236 if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
237 av_compare_ts(pkt->pts, st->time_base,
238 end_pts, AV_TIME_BASE_Q) >= 0 &&
239 pkt->flags & AV_PKT_FLAG_KEY) {
241 av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
242 pkt->stream_index, pkt->pts);
244 ret = segment_end(oc, seg->individual_header_trailer);
247 ret = segment_start(s, seg->individual_header_trailer);
255 avio_printf(seg->pb, "%s\n", oc->filename);
257 if (seg->size && !(seg->number % seg->size)) {
259 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
260 &s->interrupt_callback, NULL)) < 0)
266 ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
272 avformat_free_context(oc);
278 static int seg_write_trailer(struct AVFormatContext *s)
280 SegmentContext *seg = s->priv_data;
281 AVFormatContext *oc = seg->avf;
283 if (!seg->write_header_trailer) {
284 ret = segment_end(oc, 0);
285 open_null_ctx(&oc->pb);
286 av_write_trailer(oc);
287 close_null_ctx(oc->pb);
289 ret = segment_end(oc, 1);
293 avformat_free_context(oc);
297 #define OFFSET(x) offsetof(SegmentContext, x)
298 #define E AV_OPT_FLAG_ENCODING_PARAM
299 static const AVOption options[] = {
300 { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
301 { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
302 { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
303 { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E },
304 { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
305 { "individual_header_trailer", "write header/trailer to each segment", OFFSET(individual_header_trailer), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, E },
306 { "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 },
310 static const AVClass seg_class = {
311 .class_name = "segment muxer",
312 .item_name = av_default_item_name,
314 .version = LIBAVUTIL_VERSION_INT,
318 AVOutputFormat ff_segment_muxer = {
320 .long_name = NULL_IF_CONFIG_SMALL("segment"),
321 .priv_data_size = sizeof(SegmentContext),
322 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
323 .write_header = seg_write_header,
324 .write_packet = seg_write_packet,
325 .write_trailer = seg_write_trailer,
326 .priv_class = &seg_class,