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