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. */
37 char *format; /**< Set by a private option. */
38 char *list; /**< Set by a private option. */
39 float time; /**< Set by a private option. */
40 int size; /**< Set by a private option. */
41 int wrap; /**< Set by a private option. */
43 int64_t recording_time;
48 static int segment_start(AVFormatContext *s)
50 SegmentContext *c = s->priv_data;
51 AVFormatContext *oc = c->avf;
57 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
58 s->filename, c->number++) < 0)
59 return AVERROR(EINVAL);
61 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
62 &s->interrupt_callback, NULL)) < 0)
65 if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
66 oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
69 return AVERROR(ENOMEM);
71 if (oc->oformat->priv_class) {
72 *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
73 av_opt_set_defaults(oc->priv_data);
77 if ((err = oc->oformat->write_header(oc)) < 0) {
85 av_freep(&oc->priv_data);
90 static int segment_end(AVFormatContext *oc)
94 if (oc->oformat->write_trailer)
95 ret = oc->oformat->write_trailer(oc);
98 if (oc->oformat->priv_class)
99 av_opt_free(oc->priv_data);
100 av_freep(&oc->priv_data);
105 static int seg_write_header(AVFormatContext *s)
107 SegmentContext *seg = s->priv_data;
112 seg->offset_time = 0;
113 seg->recording_time = seg->time * 1000000;
115 oc = avformat_alloc_context();
118 return AVERROR(ENOMEM);
121 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
122 &s->interrupt_callback, NULL)) < 0)
125 for (i = 0; i< s->nb_streams; i++)
127 (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
129 if (seg->has_video > 1)
130 av_log(s, AV_LOG_WARNING,
131 "More than a single video stream present, "
132 "expect issues decoding it.\n");
134 oc->oformat = av_guess_format(seg->format, s->filename, NULL);
137 ret = AVERROR_MUXER_NOT_FOUND;
140 if (oc->oformat->flags & AVFMT_NOFILE) {
141 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
143 ret = AVERROR(EINVAL);
149 oc->streams = s->streams;
150 oc->nb_streams = s->nb_streams;
152 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
153 s->filename, seg->number++) < 0) {
154 ret = AVERROR(EINVAL);
158 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
159 &s->interrupt_callback, NULL)) < 0)
162 if ((ret = avformat_write_header(oc, NULL)) < 0) {
168 avio_printf(seg->pb, "%s\n", oc->filename);
178 avformat_free_context(oc);
183 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
185 SegmentContext *seg = s->priv_data;
186 AVFormatContext *oc = seg->avf;
187 AVStream *st = oc->streams[pkt->stream_index];
188 int64_t end_pts = seg->recording_time * seg->number;
191 if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
192 av_compare_ts(pkt->pts, st->time_base,
193 end_pts, AV_TIME_BASE_Q) >= 0 &&
194 pkt->flags & AV_PKT_FLAG_KEY) {
196 av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
197 pkt->stream_index, pkt->pts);
199 ret = segment_end(oc);
202 ret = segment_start(s);
208 avio_printf(seg->pb, "%s\n", oc->filename);
210 if (seg->size && !(seg->number % seg->size)) {
212 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
213 &s->interrupt_callback, NULL)) < 0)
219 ret = oc->oformat->write_packet(oc, pkt);
227 avformat_free_context(oc);
233 static int seg_write_trailer(struct AVFormatContext *s)
235 SegmentContext *seg = s->priv_data;
236 AVFormatContext *oc = seg->avf;
237 int ret = segment_end(oc);
242 avformat_free_context(oc);
246 #define OFFSET(x) offsetof(SegmentContext, x)
247 #define E AV_OPT_FLAG_ENCODING_PARAM
248 static const AVOption options[] = {
249 { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
250 { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
251 { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
252 { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
253 { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
257 static const AVClass seg_class = {
258 .class_name = "segment muxer",
259 .item_name = av_default_item_name,
261 .version = LIBAVUTIL_VERSION_INT,
265 AVOutputFormat ff_segment_muxer = {
267 .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
268 .priv_data_size = sizeof(SegmentContext),
269 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
270 .write_header = seg_write_header,
271 .write_packet = seg_write_packet,
272 .write_trailer = seg_write_trailer,
273 .priv_class = &seg_class,