]> git.sesse.net Git - ffmpeg/blob - libavformat/segment.c
segment: Properly create new AVStreams for the chained muxer
[ffmpeg] / libavformat / segment.c
1 /*
2  * Generic segmenter
3  * Copyright (c) 2011, Luca Barbato
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <float.h>
23
24 #include "avformat.h"
25 #include "internal.h"
26
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"
32
33 typedef struct {
34     const AVClass *class;  /**< Class for private options. */
35     int number;
36     AVFormatContext *avf;
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. */
42     int64_t offset_time;
43     int64_t recording_time;
44     int has_video;
45     AVIOContext *pb;
46 } SegmentContext;
47
48 static int segment_start(AVFormatContext *s)
49 {
50     SegmentContext *c = s->priv_data;
51     AVFormatContext *oc = c->avf;
52     int err = 0;
53
54     if (c->wrap)
55         c->number %= c->wrap;
56
57     if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
58                               s->filename, c->number++) < 0)
59         return AVERROR(EINVAL);
60
61     if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
62                           &s->interrupt_callback, NULL)) < 0)
63         return err;
64
65     if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
66         oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
67         if (!oc->priv_data) {
68             avio_close(oc->pb);
69             return AVERROR(ENOMEM);
70         }
71         if (oc->oformat->priv_class) {
72             *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
73             av_opt_set_defaults(oc->priv_data);
74         }
75     }
76
77     if ((err = oc->oformat->write_header(oc)) < 0) {
78         goto fail;
79     }
80
81     return 0;
82
83 fail:
84     avio_close(oc->pb);
85     av_freep(&oc->priv_data);
86
87     return err;
88 }
89
90 static int segment_end(AVFormatContext *oc)
91 {
92     int ret = 0;
93
94     if (oc->oformat->write_trailer)
95         ret = oc->oformat->write_trailer(oc);
96
97     avio_close(oc->pb);
98     if (oc->oformat->priv_class)
99         av_opt_free(oc->priv_data);
100     av_freep(&oc->priv_data);
101
102     return ret;
103 }
104
105 static int seg_write_header(AVFormatContext *s)
106 {
107     SegmentContext *seg = s->priv_data;
108     AVFormatContext *oc;
109     int ret, i;
110
111     seg->number = 0;
112     seg->offset_time = 0;
113     seg->recording_time = seg->time * 1000000;
114
115     oc = avformat_alloc_context();
116
117     if (!oc)
118         return AVERROR(ENOMEM);
119
120     if (seg->list)
121         if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
122                               &s->interrupt_callback, NULL)) < 0)
123             goto fail;
124
125     for (i = 0; i < s->nb_streams; i++)
126         seg->has_video +=
127             (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
128
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");
133
134     oc->oformat = av_guess_format(seg->format, s->filename, NULL);
135
136     if (!oc->oformat) {
137         ret = AVERROR_MUXER_NOT_FOUND;
138         goto fail;
139     }
140     if (oc->oformat->flags & AVFMT_NOFILE) {
141         av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
142                oc->oformat->name);
143         ret = AVERROR(EINVAL);
144         goto fail;
145     }
146
147     oc->interrupt_callback = s->interrupt_callback;
148     seg->avf = oc;
149
150     for (i = 0; i < s->nb_streams; i++) {
151         AVStream *st;
152         if (!(st = avformat_new_stream(oc, NULL))) {
153             ret = AVERROR(ENOMEM);
154             goto fail;
155         }
156         avcodec_copy_context(st->codec, s->streams[i]->codec);
157         st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
158     }
159
160     if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
161                               s->filename, seg->number++) < 0) {
162         ret = AVERROR(EINVAL);
163         goto fail;
164     }
165
166     if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
167                           &s->interrupt_callback, NULL)) < 0)
168         goto fail;
169
170     if ((ret = avformat_write_header(oc, NULL)) < 0) {
171         avio_close(oc->pb);
172         goto fail;
173     }
174
175     if (seg->list) {
176         avio_printf(seg->pb, "%s\n", oc->filename);
177         avio_flush(seg->pb);
178     }
179
180 fail:
181     if (ret) {
182         if (seg->list)
183             avio_close(seg->pb);
184         avformat_free_context(oc);
185     }
186     return ret;
187 }
188
189 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
190 {
191     SegmentContext *seg = s->priv_data;
192     AVFormatContext *oc = seg->avf;
193     AVStream *st = s->streams[pkt->stream_index];
194     int64_t end_pts = seg->recording_time * seg->number;
195     int ret;
196
197     if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
198         av_compare_ts(pkt->pts, st->time_base,
199                       end_pts, AV_TIME_BASE_Q) >= 0 &&
200         pkt->flags & AV_PKT_FLAG_KEY) {
201
202         av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
203                pkt->stream_index, pkt->pts);
204
205         ret = segment_end(oc);
206
207         if (!ret)
208             ret = segment_start(s);
209
210         if (ret)
211             goto fail;
212
213         if (seg->list) {
214             avio_printf(seg->pb, "%s\n", oc->filename);
215             avio_flush(seg->pb);
216             if (seg->size && !(seg->number % seg->size)) {
217                 avio_close(seg->pb);
218                 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
219                                       &s->interrupt_callback, NULL)) < 0)
220                     goto fail;
221             }
222         }
223     }
224
225     ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
226
227 fail:
228     if (ret < 0) {
229         if (seg->list)
230             avio_close(seg->pb);
231         avformat_free_context(oc);
232     }
233
234     return ret;
235 }
236
237 static int seg_write_trailer(struct AVFormatContext *s)
238 {
239     SegmentContext *seg = s->priv_data;
240     AVFormatContext *oc = seg->avf;
241     int ret = segment_end(oc);
242     if (seg->list)
243         avio_close(seg->pb);
244     avformat_free_context(oc);
245     return ret;
246 }
247
248 #define OFFSET(x) offsetof(SegmentContext, x)
249 #define E AV_OPT_FLAG_ENCODING_PARAM
250 static const AVOption options[] = {
251     { "segment_format",    "container format used for the segments",  OFFSET(format),  AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
252     { "segment_time",      "segment length in seconds",               OFFSET(time),    AV_OPT_TYPE_FLOAT,  {.dbl = 2},     0, FLT_MAX, E },
253     { "segment_list",      "output the segment list",                 OFFSET(list),    AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
254     { "segment_list_size", "maximum number of playlist entries",      OFFSET(size),    AV_OPT_TYPE_INT,    {.i64 = 5},     0, INT_MAX, E },
255     { "segment_wrap",      "number after which the index wraps",      OFFSET(wrap),    AV_OPT_TYPE_INT,    {.i64 = 0},     0, INT_MAX, E },
256     { NULL },
257 };
258
259 static const AVClass seg_class = {
260     .class_name = "segment muxer",
261     .item_name  = av_default_item_name,
262     .option     = options,
263     .version    = LIBAVUTIL_VERSION_INT,
264 };
265
266
267 AVOutputFormat ff_segment_muxer = {
268     .name           = "segment",
269     .long_name      = NULL_IF_CONFIG_SMALL("segment"),
270     .priv_data_size = sizeof(SegmentContext),
271     .flags          = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
272     .write_header   = seg_write_header,
273     .write_packet   = seg_write_packet,
274     .write_trailer  = seg_write_trailer,
275     .priv_class     = &seg_class,
276 };