]> git.sesse.net Git - ffmpeg/blob - libavformat/segment.c
lavf/segment: rename SegmentContext.pb to list_pb
[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;          ///< format to use for output segment files
38     char *list;            ///< filename for the segment list file
39     int   list_size;       ///< number of entries for the segment list file
40     AVIOContext *list_pb;  ///< list file put-byte context
41     float time;            ///< segment duration
42     int  wrap;             ///< number after which the index wraps
43     int64_t recording_time;
44     int has_video;
45 } SegmentContext;
46
47 static int segment_start(AVFormatContext *s)
48 {
49     SegmentContext *seg = s->priv_data;
50     AVFormatContext *oc = seg->avf;
51     int err = 0;
52
53     if (seg->wrap)
54         seg->number %= seg->wrap;
55
56     if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
57                               s->filename, seg->number++) < 0) {
58         av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
59         return AVERROR(EINVAL);
60     }
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     av_log(oc, AV_LOG_ERROR, "Failure occurred when starting segment '%s'\n",
86            oc->filename);
87     avio_close(oc->pb);
88     av_freep(&oc->priv_data);
89
90     return err;
91 }
92
93 static int segment_end(AVFormatContext *s)
94 {
95     SegmentContext *seg = s->priv_data;
96     AVFormatContext *oc = seg->avf;
97     int ret = 0;
98
99     if (oc->oformat->write_trailer)
100         ret = oc->oformat->write_trailer(oc);
101
102     if (ret < 0)
103         av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
104                oc->filename);
105
106     if (seg->list) {
107         if (seg->list_size && !(seg->number % seg->list_size)) {
108             avio_close(seg->list_pb);
109             if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
110                                   &s->interrupt_callback, NULL)) < 0)
111                 goto end;
112         }
113         avio_printf(seg->list_pb, "%s\n", oc->filename);
114         avio_flush(seg->list_pb);
115     }
116
117 end:
118     avio_close(oc->pb);
119     if (oc->oformat->priv_class)
120         av_opt_free(oc->priv_data);
121     av_freep(&oc->priv_data);
122
123     return ret;
124 }
125
126 static int seg_write_header(AVFormatContext *s)
127 {
128     SegmentContext *seg = s->priv_data;
129     AVFormatContext *oc;
130     int ret, i;
131
132     seg->number = 0;
133     seg->recording_time = seg->time * 1000000;
134
135     oc = avformat_alloc_context();
136
137     if (!oc)
138         return AVERROR(ENOMEM);
139
140     if (seg->list)
141         if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
142                               &s->interrupt_callback, NULL)) < 0)
143             goto fail;
144
145     for (i = 0; i< s->nb_streams; i++)
146         seg->has_video +=
147             (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
148
149     if (seg->has_video > 1)
150         av_log(s, AV_LOG_WARNING,
151                "More than a single video stream present, "
152                "expect issues decoding it.\n");
153
154     oc->oformat = av_guess_format(seg->format, s->filename, NULL);
155
156     if (!oc->oformat) {
157         ret = AVERROR_MUXER_NOT_FOUND;
158         goto fail;
159     }
160     if (oc->oformat->flags & AVFMT_NOFILE) {
161         av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
162                oc->oformat->name);
163         ret = AVERROR(EINVAL);
164         goto fail;
165     }
166
167     seg->avf = oc;
168
169     oc->streams = s->streams;
170     oc->nb_streams = s->nb_streams;
171
172     if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
173                               s->filename, seg->number++) < 0) {
174         ret = AVERROR(EINVAL);
175         goto fail;
176     }
177
178     if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
179                           &s->interrupt_callback, NULL)) < 0)
180         goto fail;
181
182     if ((ret = avformat_write_header(oc, NULL)) < 0) {
183         avio_close(oc->pb);
184         goto fail;
185     }
186
187 fail:
188     if (ret) {
189         if (oc) {
190             oc->streams = NULL;
191             oc->nb_streams = 0;
192             avformat_free_context(oc);
193         }
194         if (seg->list)
195             avio_close(seg->list_pb);
196     }
197     return ret;
198 }
199
200 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
201 {
202     SegmentContext *seg = s->priv_data;
203     AVFormatContext *oc = seg->avf;
204     AVStream *st = oc->streams[pkt->stream_index];
205     int64_t end_pts = seg->recording_time * seg->number;
206     int ret;
207
208     /* if the segment has video, start a new segment *only* with a key video frame */
209     if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
210         av_compare_ts(pkt->pts, st->time_base,
211                       end_pts, AV_TIME_BASE_Q) >= 0 &&
212         pkt->flags & AV_PKT_FLAG_KEY) {
213
214         av_log(s, AV_LOG_DEBUG, "Next segment starts with packet stream:%d pts:%"PRId64" pts_time:%f\n",
215                pkt->stream_index, pkt->pts, pkt->pts * av_q2d(st->time_base));
216
217         if ((ret = segment_end(s)) < 0 || (ret = segment_start(s)) < 0)
218             goto fail;
219     }
220
221     ret = oc->oformat->write_packet(oc, pkt);
222
223 fail:
224     if (ret < 0) {
225         oc->streams = NULL;
226         oc->nb_streams = 0;
227         if (seg->list)
228             avio_close(seg->list_pb);
229         avformat_free_context(oc);
230     }
231
232     return ret;
233 }
234
235 static int seg_write_trailer(struct AVFormatContext *s)
236 {
237     SegmentContext *seg = s->priv_data;
238     AVFormatContext *oc = seg->avf;
239     int ret = segment_end(s);
240     if (seg->list)
241         avio_close(seg->list_pb);
242     oc->streams = NULL;
243     oc->nb_streams = 0;
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",    "set container format used for the segments", OFFSET(format),  AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
252     { "segment_time",      "set segment length in seconds",              OFFSET(time),    AV_OPT_TYPE_FLOAT,  {.dbl = 2},     0, FLT_MAX, E },
253     { "segment_list",      "set the segment list filename",              OFFSET(list),    AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
254     { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT,  {.dbl = 5},     0, INT_MAX, E },
255     { "segment_wrap",      "set number after which the index wraps",     OFFSET(wrap),    AV_OPT_TYPE_INT,    {.dbl = 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 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 };
276
277 static const AVClass sseg_class = {
278     .class_name = "stream_segment muxer",
279     .item_name  = av_default_item_name,
280     .option     = options,
281     .version    = LIBAVUTIL_VERSION_INT,
282 };
283
284 AVOutputFormat ff_stream_segment_muxer = {
285     .name           = "stream_segment,ssegment",
286     .long_name      = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
287     .priv_data_size = sizeof(SegmentContext),
288     .flags          = AVFMT_NOFILE,
289     .write_header   = seg_write_header,
290     .write_packet   = seg_write_packet,
291     .write_trailer  = seg_write_trailer,
292     .priv_class     = &sseg_class,
293 };