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
28 #include "libavutil/log.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/parseutils.h"
32 #include "libavutil/mathematics.h"
35 const AVClass *class; /**< Class for private options. */
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. */
44 int64_t recording_time;
49 static int segment_start(AVFormatContext *s)
51 SegmentContext *c = s->priv_data;
52 AVFormatContext *oc = c->avf;
58 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
59 s->filename, c->number++) < 0)
60 return AVERROR(EINVAL);
62 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
63 &s->interrupt_callback, NULL)) < 0)
66 if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
67 oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
70 return AVERROR(ENOMEM);
72 if (oc->oformat->priv_class) {
73 *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
74 av_opt_set_defaults(oc->priv_data);
78 if ((err = oc->oformat->write_header(oc)) < 0) {
86 av_freep(&oc->priv_data);
91 static int segment_end(AVFormatContext *oc)
95 if (oc->oformat->write_trailer)
96 ret = oc->oformat->write_trailer(oc);
99 if (oc->oformat->priv_class)
100 av_opt_free(oc->priv_data);
101 av_freep(&oc->priv_data);
106 static int seg_write_header(AVFormatContext *s)
108 SegmentContext *seg = s->priv_data;
113 seg->offset_time = 0;
114 seg->recording_time = seg->time * 1000000;
116 oc = avformat_alloc_context();
119 return AVERROR(ENOMEM);
122 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
123 &s->interrupt_callback, NULL)) < 0)
126 for (i = 0; i< s->nb_streams; i++)
128 (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
130 if (seg->has_video > 1)
131 av_log(s, AV_LOG_WARNING,
132 "More than a single video stream present, "
133 "expect issues decoding it.\n");
135 oc->oformat = av_guess_format(seg->format, s->filename, NULL);
138 ret = AVERROR_MUXER_NOT_FOUND;
141 if (oc->oformat->flags & AVFMT_NOFILE) {
142 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
144 ret = AVERROR(EINVAL);
150 oc->streams = s->streams;
151 oc->nb_streams = s->nb_streams;
153 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
154 s->filename, seg->number++) < 0) {
155 ret = AVERROR(EINVAL);
159 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
160 &s->interrupt_callback, NULL)) < 0)
163 if ((ret = avformat_write_header(oc, NULL)) < 0) {
169 avio_printf(seg->pb, "%s\n", oc->filename);
178 avformat_free_context(oc);
186 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
188 SegmentContext *seg = s->priv_data;
189 AVFormatContext *oc = seg->avf;
190 AVStream *st = oc->streams[pkt->stream_index];
191 int64_t end_pts = seg->recording_time * seg->number;
194 if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
195 av_compare_ts(pkt->pts, st->time_base,
196 end_pts, AV_TIME_BASE_Q) >= 0 &&
197 pkt->flags & AV_PKT_FLAG_KEY) {
199 av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
200 pkt->stream_index, pkt->pts);
202 ret = segment_end(oc);
205 ret = segment_start(s);
211 avio_printf(seg->pb, "%s\n", oc->filename);
213 if (seg->size && !(seg->number % seg->size)) {
215 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
216 &s->interrupt_callback, NULL)) < 0)
222 ret = oc->oformat->write_packet(oc, pkt);
230 avformat_free_context(oc);
236 static int seg_write_trailer(struct AVFormatContext *s)
238 SegmentContext *seg = s->priv_data;
239 AVFormatContext *oc = seg->avf;
240 int ret = segment_end(oc);
245 avformat_free_context(oc);
249 #define OFFSET(x) offsetof(SegmentContext, x)
250 #define E AV_OPT_FLAG_ENCODING_PARAM
251 static const AVOption options[] = {
252 { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
253 { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
254 { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
255 { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
256 { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
260 static const AVClass seg_class = {
261 .class_name = "segment muxer",
262 .item_name = av_default_item_name,
264 .version = LIBAVUTIL_VERSION_INT,
268 AVOutputFormat ff_segment_muxer = {
270 .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
271 .priv_data_size = sizeof(SegmentContext),
272 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
273 .write_header = seg_write_header,
274 .write_packet = seg_write_packet,
275 .write_trailer = seg_write_trailer,
276 .priv_class = &seg_class,