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
22 #include "libavutil/avstring.h"
23 #include "libavutil/log.h"
24 #include "libavutil/opt.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/parseutils.h"
33 const AVClass *class; /**< Class for private options. */
36 char *format; /**< Set by a private option. */
37 char *pattern; /**< Set by a private option. */
38 char *path; /**< Set by a private option. */
39 float time; /**< Set by a private option. */
41 int64_t recording_time;
44 #if CONFIG_SEGMENT_MUXER
46 static int segment_header(SegmentContext *s)
48 AVFormatContext *oc = s->avf;
51 av_strlcpy(oc->filename, s->path, sizeof(oc->filename));
53 av_strlcatf(oc->filename, sizeof(oc->filename),
54 s->pattern, s->number++);
56 if ((err = avio_open(&oc->pb, oc->filename, AVIO_FLAG_WRITE)) < 0) {
60 if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
61 oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
64 return AVERROR(ENOMEM);
68 if ((err = oc->oformat->write_header(oc)) < 0) {
70 av_freep(&oc->priv_data);
76 static int segment_trailer(AVFormatContext *oc)
80 if(oc->oformat->write_trailer)
81 ret = oc->oformat->write_trailer(oc);
84 av_freep(&oc->priv_data);
89 static int seg_write_header(AVFormatContext *s)
91 SegmentContext *seg = s->priv_data;
96 seg->recording_time = seg->time*1000000;
101 seg->path = av_strdup(s->filename);
102 t = strrchr(seg->path, '.');
106 oc = avformat_alloc_context();
109 return AVERROR(ENOMEM);
112 oc->oformat = av_guess_format(seg->format, NULL, NULL);
115 avformat_free_context(oc);
116 return AVERROR_MUXER_NOT_FOUND;
121 oc->streams = s->streams;
122 oc->nb_streams = s->nb_streams;
124 av_strlcpy(oc->filename, seg->path, sizeof(oc->filename));
126 av_strlcatf(oc->filename, sizeof(oc->filename),
127 seg->pattern, seg->number++);
129 if ((ret = avio_open(&oc->pb, oc->filename, AVIO_FLAG_WRITE)) < 0) {
130 avformat_free_context(oc);
134 if ((ret = avformat_write_header(oc, NULL)) < 0) {
139 avformat_free_context(oc);
141 avio_printf(s->pb, "%s\n", oc->filename);
147 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
149 SegmentContext *seg = s->priv_data;
150 AVFormatContext *oc = seg->avf;
151 AVStream *st = oc->streams[pkt->stream_index];
154 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
155 av_compare_ts(pkt->pts, st->time_base,
156 seg->recording_time*seg->number,
157 (AVRational){1, 1000000}) >= 0 &&
158 pkt->flags & AV_PKT_FLAG_KEY) {
159 av_log(s, AV_LOG_INFO, "I'd reset at %d %"PRId64"\n",
160 pkt->stream_index, pkt->pts);
162 ret = segment_trailer(oc);
164 ret = segment_header(seg);
167 avformat_free_context(oc);
170 avio_printf(s->pb, "%s\n", oc->filename);
174 ret = oc->oformat->write_packet(oc, pkt);
179 static int seg_write_trailer(struct AVFormatContext *s)
181 SegmentContext *seg = s->priv_data;
182 AVFormatContext *oc = seg->avf;
184 return segment_trailer(oc);
187 #endif /* CONFIG_SEGMENT_MUXER */
189 #define OFFSET(x) offsetof(SegmentContext, x)
190 #define E AV_OPT_FLAG_ENCODING_PARAM
191 static const AVOption options[] = {
192 { "container_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = "nut"}, 0, 0, E },
193 { "segment_time", "segment lenght in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
194 { "segment_pattern", "pattern to use in segment files", OFFSET(pattern),AV_OPT_TYPE_STRING, {.str = "%03d"}, 0, 0, E },
195 { "segment_basename", "basename to use in segment files", OFFSET(path ),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
199 static const AVClass seg_class = {
200 .class_name = "segment muxer",
201 .item_name = av_default_item_name,
203 .version = LIBAVUTIL_VERSION_INT,
207 #if CONFIG_IMAGE2_DEMUXER
208 AVInputFormat ff_image2_demuxer = {
210 .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
211 .priv_data_size = sizeof(VideoData),
212 .read_probe = read_probe,
213 .read_header = read_header,
214 .read_packet = read_packet,
215 .flags = AVFMT_NOFILE,
216 .priv_class = &img2_class,
222 #if CONFIG_SEGMENT_MUXER
223 AVOutputFormat ff_segment_muxer = {
225 .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
226 .extensions = "m3u8",
227 .priv_data_size = sizeof(SegmentContext),
228 .flags = AVFMT_GLOBALHEADER,
229 .write_header = seg_write_header,
230 .write_packet = seg_write_packet,
231 .write_trailer = seg_write_trailer,
232 .priv_class = &seg_class,