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/avassert.h"
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"
41 const AVClass *class; /**< Class for private options. */
44 char *format; ///< format to use for output segment files
45 char *list; ///< filename for the segment list file
46 int list_size; ///< number of entries for the segment list file
47 ListType list_type; ///< set the list type
48 AVIOContext *list_pb; ///< list file put-byte context
49 int wrap; ///< number after which the index wraps
50 char *time_str; ///< segment duration specification string
51 int64_t time; ///< segment duration
52 char *times_str; ///< segment times specification string
53 int64_t *times; ///< list of segment interval specification
54 int nb_times; ///< number of elments in the times array
55 char *time_delta_str; ///< approximation value duration used for the segment times
58 double start_time, end_time;
61 static int segment_start(AVFormatContext *s)
63 SegmentContext *seg = s->priv_data;
64 AVFormatContext *oc = seg->avf;
68 seg->number %= seg->wrap;
70 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
71 s->filename, seg->number++) < 0) {
72 av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
73 return AVERROR(EINVAL);
76 if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
77 &s->interrupt_callback, NULL)) < 0)
80 if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
81 oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
84 return AVERROR(ENOMEM);
86 if (oc->oformat->priv_class) {
87 *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
88 av_opt_set_defaults(oc->priv_data);
92 if ((err = oc->oformat->write_header(oc)) < 0) {
99 av_log(oc, AV_LOG_ERROR, "Failure occurred when starting segment '%s'\n",
102 av_freep(&oc->priv_data);
107 static int segment_end(AVFormatContext *s)
109 SegmentContext *seg = s->priv_data;
110 AVFormatContext *oc = seg->avf;
113 if (oc->oformat->write_trailer)
114 ret = oc->oformat->write_trailer(oc);
117 av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
121 if (seg->list_size && !(seg->number % seg->list_size)) {
122 avio_close(seg->list_pb);
123 if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
124 &s->interrupt_callback, NULL)) < 0)
128 if (seg->list_type == LIST_TYPE_FLAT) {
129 avio_printf(seg->list_pb, "%s\n", oc->filename);
130 } else if (seg->list_type == LIST_TYPE_EXT) {
131 avio_printf(seg->list_pb, "%s,%f,%f\n", oc->filename, seg->start_time, seg->end_time);
133 avio_flush(seg->list_pb);
138 if (oc->oformat->priv_class)
139 av_opt_free(oc->priv_data);
140 av_freep(&oc->priv_data);
145 static int parse_times(void *log_ctx, int64_t **times, int *nb_times,
146 const char *times_str)
150 char *times_str1 = av_strdup(times_str);
151 char *saveptr = NULL;
154 return AVERROR(ENOMEM);
156 #define FAIL(err) ret = err; goto end
159 for (p = times_str1; *p; p++)
163 *times = av_malloc(sizeof(**times) * *nb_times);
165 av_log(log_ctx, AV_LOG_ERROR, "Could not allocate forced times array\n");
166 FAIL(AVERROR(ENOMEM));
170 for (i = 0; i < *nb_times; i++) {
172 char *tstr = av_strtok(p, ",", &saveptr);
176 ret = av_parse_time(&t, tstr, 1);
178 av_log(log_ctx, AV_LOG_ERROR,
179 "Invalid time duration specification in %s\n", p);
180 FAIL(AVERROR(EINVAL));
184 /* check on monotonicity */
185 if (i && (*times)[i-1] > (*times)[i]) {
186 av_log(log_ctx, AV_LOG_ERROR,
187 "Specified time %f is greater than the following time %f\n",
188 (float)((*times)[i])/1000000, (float)((*times)[i-1])/1000000);
189 FAIL(AVERROR(EINVAL));
198 static int seg_write_header(AVFormatContext *s)
200 SegmentContext *seg = s->priv_data;
206 if (seg->time_str && seg->times_str) {
207 av_log(s, AV_LOG_ERROR,
208 "segment_time and segment_times options are mutually exclusive, select just one of them\n");
209 return AVERROR(EINVAL);
212 if (seg->times_str) {
213 if ((ret = parse_times(s, &seg->times, &seg->nb_times, seg->times_str)) < 0)
216 /* set default value if not specified */
218 seg->time_str = av_strdup("2");
219 if ((ret = av_parse_time(&seg->time, seg->time_str, 1)) < 0) {
220 av_log(s, AV_LOG_ERROR,
221 "Invalid time duration specification '%s' for segment_time option\n",
227 if (seg->time_delta_str) {
228 if ((ret = av_parse_time(&seg->time_delta, seg->time_delta_str, 1)) < 0) {
229 av_log(s, AV_LOG_ERROR,
230 "Invalid time duration specification '%s' for delta option\n",
231 seg->time_delta_str);
236 oc = avformat_alloc_context();
239 return AVERROR(ENOMEM);
242 if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
243 &s->interrupt_callback, NULL)) < 0)
246 for (i = 0; i< s->nb_streams; i++)
248 (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
250 if (seg->has_video > 1)
251 av_log(s, AV_LOG_WARNING,
252 "More than a single video stream present, "
253 "expect issues decoding it.\n");
255 oc->oformat = av_guess_format(seg->format, s->filename, NULL);
258 ret = AVERROR_MUXER_NOT_FOUND;
261 if (oc->oformat->flags & AVFMT_NOFILE) {
262 av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
264 ret = AVERROR(EINVAL);
270 oc->streams = s->streams;
271 oc->nb_streams = s->nb_streams;
273 if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
274 s->filename, seg->number++) < 0) {
275 ret = AVERROR(EINVAL);
279 if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
280 &s->interrupt_callback, NULL)) < 0)
283 if ((ret = avformat_write_header(oc, NULL)) < 0) {
293 avformat_free_context(oc);
296 avio_close(seg->list_pb);
301 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
303 SegmentContext *seg = s->priv_data;
304 AVFormatContext *oc = seg->avf;
305 AVStream *st = oc->streams[pkt->stream_index];
310 end_pts = seg->number <= seg->nb_times ? seg->times[seg->number-1] : INT64_MAX;
312 end_pts = seg->time * seg->number;
315 /* if the segment has video, start a new segment *only* with a key video frame */
316 if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
317 av_compare_ts(pkt->pts, st->time_base,
318 end_pts-seg->time_delta, AV_TIME_BASE_Q) >= 0 &&
319 pkt->flags & AV_PKT_FLAG_KEY) {
321 av_log(s, AV_LOG_DEBUG, "Next segment starts with packet stream:%d pts:%"PRId64" pts_time:%f\n",
322 pkt->stream_index, pkt->pts, pkt->pts * av_q2d(st->time_base));
324 if ((ret = segment_end(s)) < 0 || (ret = segment_start(s)) < 0)
326 seg->start_time = (double)pkt->pts * av_q2d(st->time_base);
327 } else if (pkt->pts != AV_NOPTS_VALUE) {
328 seg->end_time = FFMAX(seg->end_time,
329 (double)(pkt->pts + pkt->duration) * av_q2d(st->time_base));
332 ret = oc->oformat->write_packet(oc, pkt);
339 avio_close(seg->list_pb);
340 avformat_free_context(oc);
346 static int seg_write_trailer(struct AVFormatContext *s)
348 SegmentContext *seg = s->priv_data;
349 AVFormatContext *oc = seg->avf;
350 int ret = segment_end(s);
352 avio_close(seg->list_pb);
355 av_freep(&seg->times);
359 avformat_free_context(oc);
363 #define OFFSET(x) offsetof(SegmentContext, x)
364 #define E AV_OPT_FLAG_ENCODING_PARAM
365 static const AVOption options[] = {
366 { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
367 { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
368 { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
369 { "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" },
370 { "flat", "flat format", 0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_FLAT }, INT_MIN, INT_MAX, 0, "list_type" },
371 { "ext", "extended format", 0, AV_OPT_TYPE_CONST, {.dbl=LIST_TYPE_EXT }, INT_MIN, INT_MAX, 0, "list_type" },
372 { "segment_time", "set segment duration", OFFSET(time_str),AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
373 { "segment_time_delta","set approximation value used for the segment times", OFFSET(time_delta_str), AV_OPT_TYPE_STRING, {.str = "0"}, 0, 0, E },
374 { "segment_times", "set segment split time points", OFFSET(times_str),AV_OPT_TYPE_STRING,{.str = NULL}, 0, 0, E },
375 { "segment_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
379 static const AVClass seg_class = {
380 .class_name = "segment muxer",
381 .item_name = av_default_item_name,
383 .version = LIBAVUTIL_VERSION_INT,
386 AVOutputFormat ff_segment_muxer = {
388 .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
389 .priv_data_size = sizeof(SegmentContext),
390 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
391 .write_header = seg_write_header,
392 .write_packet = seg_write_packet,
393 .write_trailer = seg_write_trailer,
394 .priv_class = &seg_class,
397 static const AVClass sseg_class = {
398 .class_name = "stream_segment muxer",
399 .item_name = av_default_item_name,
401 .version = LIBAVUTIL_VERSION_INT,
404 AVOutputFormat ff_stream_segment_muxer = {
405 .name = "stream_segment,ssegment",
406 .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
407 .priv_data_size = sizeof(SegmentContext),
408 .flags = AVFMT_NOFILE,
409 .write_header = seg_write_header,
410 .write_packet = seg_write_packet,
411 .write_trailer = seg_write_trailer,
412 .priv_class = &sseg_class,