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"
40 const AVClass *class; /**< Class for private options. */
43 char *format; ///< format to use for output segment files
44 char *list; ///< filename for the segment list file
45 int list_size; ///< number of entries for the segment list file
46 ListType list_type; ///< set the list type
47 AVIOContext *list_pb; ///< list file put-byte context
48 float time; ///< segment duration
49 int wrap; ///< number after which the index wraps
50 int64_t recording_time;
52 double start_time, end_time;
55 static int segment_start(AVFormatContext *s)
57 SegmentContext *seg = s->priv_data;
58 AVFormatContext *oc = seg->avf;
62 seg->number %= seg->wrap;
64 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
65 s->filename, seg->number++) < 0) {
66 av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
67 return AVERROR(EINVAL);
70 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
71 &s->interrupt_callback, NULL)) < 0)
74 if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
75 oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
78 return AVERROR(ENOMEM);
80 if (oc->oformat->priv_class) {
81 *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
82 av_opt_set_defaults(oc->priv_data);
86 if ((err = oc->oformat->write_header(oc)) < 0) {
93 av_log(oc, AV_LOG_ERROR, "Failure occurred when starting segment '%s'\n",
96 av_freep(&oc->priv_data);
101 static int segment_end(AVFormatContext *s)
103 SegmentContext *seg = s->priv_data;
104 AVFormatContext *oc = seg->avf;
107 if (oc->oformat->write_trailer)
108 ret = oc->oformat->write_trailer(oc);
111 av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
115 if (seg->list_size && !(seg->number % seg->list_size)) {
116 avio_close(seg->list_pb);
117 if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
118 &s->interrupt_callback, NULL)) < 0)
122 if (seg->list_type == LIST_TYPE_FLAT) {
123 avio_printf(seg->list_pb, "%s\n", oc->filename);
124 } else if (seg->list_type == LIST_TYPE_EXT) {
125 avio_printf(seg->list_pb, "%s,%f,%f\n", oc->filename, seg->start_time, seg->end_time);
127 avio_flush(seg->list_pb);
132 if (oc->oformat->priv_class)
133 av_opt_free(oc->priv_data);
134 av_freep(&oc->priv_data);
139 static int seg_write_header(AVFormatContext *s)
141 SegmentContext *seg = s->priv_data;
146 seg->recording_time = seg->time * 1000000;
148 oc = avformat_alloc_context();
151 return AVERROR(ENOMEM);
154 if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
155 &s->interrupt_callback, NULL)) < 0)
158 for (i = 0; i< s->nb_streams; i++)
160 (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
162 if (seg->has_video > 1)
163 av_log(s, AV_LOG_WARNING,
164 "More than a single video stream present, "
165 "expect issues decoding it.\n");
167 oc->oformat = av_guess_format(seg->format, s->filename, NULL);
170 ret = AVERROR_MUXER_NOT_FOUND;
173 if (oc->oformat->flags & AVFMT_NOFILE) {
174 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
176 ret = AVERROR(EINVAL);
182 oc->streams = s->streams;
183 oc->nb_streams = s->nb_streams;
185 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
186 s->filename, seg->number++) < 0) {
187 ret = AVERROR(EINVAL);
191 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
192 &s->interrupt_callback, NULL)) < 0)
195 if ((ret = avformat_write_header(oc, NULL)) < 0) {
205 avformat_free_context(oc);
208 avio_close(seg->list_pb);
213 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
215 SegmentContext *seg = s->priv_data;
216 AVFormatContext *oc = seg->avf;
217 AVStream *st = oc->streams[pkt->stream_index];
218 int64_t end_pts = seg->recording_time * seg->number;
221 /* if the segment has video, start a new segment *only* with a key video frame */
222 if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
223 av_compare_ts(pkt->pts, st->time_base,
224 end_pts, AV_TIME_BASE_Q) >= 0 &&
225 pkt->flags & AV_PKT_FLAG_KEY) {
227 av_log(s, AV_LOG_DEBUG, "Next segment starts with packet stream:%d pts:%"PRId64" pts_time:%f\n",
228 pkt->stream_index, pkt->pts, pkt->pts * av_q2d(st->time_base));
230 if ((ret = segment_end(s)) < 0 || (ret = segment_start(s)) < 0)
232 seg->start_time = (double)pkt->pts * av_q2d(st->time_base);
233 } else if (pkt->pts != AV_NOPTS_VALUE) {
234 seg->end_time = FFMAX(seg->end_time,
235 (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
238 ret = oc->oformat->write_packet(oc, pkt);
245 avio_close(seg->list_pb);
246 avformat_free_context(oc);
252 static int seg_write_trailer(struct AVFormatContext *s)
254 SegmentContext *seg = s->priv_data;
255 AVFormatContext *oc = seg->avf;
256 int ret = segment_end(s);
258 avio_close(seg->list_pb);
261 avformat_free_context(oc);
265 #define OFFSET(x) offsetof(SegmentContext, x)
266 #define E AV_OPT_FLAG_ENCODING_PARAM
267 static const AVOption options[] = {
268 { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
269 { "segment_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
270 { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
271 { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
272 { "segment_list_type", "set the segment list type", OFFSET(list_type), AV_OPT_TYPE_INT, {.dbl = LIST_TYPE_FLAT}, 0, LIST_TYPE_NB-1, E, "list_type" },
273 { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, 0, "list_type" },
274 { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_EXT }, INT_MIN, INT_MAX, 0, "list_type" },
275 { "segment_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
279 static const AVClass seg_class = {
280 .class_name = "segment muxer",
281 .item_name = av_default_item_name,
283 .version = LIBAVUTIL_VERSION_INT,
286 AVOutputFormat ff_segment_muxer = {
288 .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
289 .priv_data_size = sizeof(SegmentContext),
290 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
291 .write_header = seg_write_header,
292 .write_packet = seg_write_packet,
293 .write_trailer = seg_write_trailer,
294 .priv_class = &seg_class,
297 static const AVClass sseg_class = {
298 .class_name = "stream_segment muxer",
299 .item_name = av_default_item_name,
301 .version = LIBAVUTIL_VERSION_INT,
304 AVOutputFormat ff_stream_segment_muxer = {
305 .name = "stream_segment,ssegment",
306 .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
307 .priv_data_size = sizeof(SegmentContext),
308 .flags = AVFMT_NOFILE,
309 .write_header = seg_write_header,
310 .write_packet = seg_write_packet,
311 .write_trailer = seg_write_trailer,
312 .priv_class = &sseg_class,