]> git.sesse.net Git - ffmpeg/blob - libavformat/segment.c
lavf/segment: add some debugging logs
[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     av_log(oc, AV_LOG_ERROR, "Failure occurred when starting segment '%s'\n",
85            oc->filename);
86     avio_close(oc->pb);
87     av_freep(&oc->priv_data);
88
89     return err;
90 }
91
92 static int segment_end(AVFormatContext *oc)
93 {
94     int ret = 0;
95
96     if (oc->oformat->write_trailer)
97         ret = oc->oformat->write_trailer(oc);
98
99     if (ret < 0)
100         av_log(oc, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
101                oc->filename);
102
103     avio_close(oc->pb);
104     if (oc->oformat->priv_class)
105         av_opt_free(oc->priv_data);
106     av_freep(&oc->priv_data);
107
108     return ret;
109 }
110
111 static int seg_write_header(AVFormatContext *s)
112 {
113     SegmentContext *seg = s->priv_data;
114     AVFormatContext *oc;
115     int ret, i;
116
117     seg->number = 0;
118     seg->offset_time = 0;
119     seg->recording_time = seg->time * 1000000;
120
121     oc = avformat_alloc_context();
122
123     if (!oc)
124         return AVERROR(ENOMEM);
125
126     if (seg->list)
127         if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
128                               &s->interrupt_callback, NULL)) < 0)
129             goto fail;
130
131     for (i = 0; i< s->nb_streams; i++)
132         seg->has_video +=
133             (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
134
135     if (seg->has_video > 1)
136         av_log(s, AV_LOG_WARNING,
137                "More than a single video stream present, "
138                "expect issues decoding it.\n");
139
140     oc->oformat = av_guess_format(seg->format, s->filename, NULL);
141
142     if (!oc->oformat) {
143         ret = AVERROR_MUXER_NOT_FOUND;
144         goto fail;
145     }
146     if (oc->oformat->flags & AVFMT_NOFILE) {
147         av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
148                oc->oformat->name);
149         ret = AVERROR(EINVAL);
150         goto fail;
151     }
152
153     seg->avf = oc;
154
155     oc->streams = s->streams;
156     oc->nb_streams = s->nb_streams;
157
158     if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
159                               s->filename, seg->number++) < 0) {
160         ret = AVERROR(EINVAL);
161         goto fail;
162     }
163
164     if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
165                           &s->interrupt_callback, NULL)) < 0)
166         goto fail;
167
168     if ((ret = avformat_write_header(oc, NULL)) < 0) {
169         avio_close(oc->pb);
170         goto fail;
171     }
172
173     if (seg->list) {
174         avio_printf(seg->pb, "%s\n", oc->filename);
175         avio_flush(seg->pb);
176     }
177
178 fail:
179     if (ret) {
180         if (oc) {
181             oc->streams = NULL;
182             oc->nb_streams = 0;
183             avformat_free_context(oc);
184         }
185         if (seg->list)
186             avio_close(seg->pb);
187     }
188     return ret;
189 }
190
191 static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
192 {
193     SegmentContext *seg = s->priv_data;
194     AVFormatContext *oc = seg->avf;
195     AVStream *st = oc->streams[pkt->stream_index];
196     int64_t end_pts = seg->recording_time * seg->number;
197     int ret;
198
199     if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
200         av_compare_ts(pkt->pts, st->time_base,
201                       end_pts, AV_TIME_BASE_Q) >= 0 &&
202         pkt->flags & AV_PKT_FLAG_KEY) {
203
204         av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
205                pkt->stream_index, pkt->pts);
206
207         ret = segment_end(oc);
208
209         if (!ret)
210             ret = segment_start(s);
211
212         if (ret)
213             goto fail;
214
215         if (seg->list) {
216             avio_printf(seg->pb, "%s\n", oc->filename);
217             avio_flush(seg->pb);
218             if (seg->size && !(seg->number % seg->size)) {
219                 avio_close(seg->pb);
220                 if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
221                                       &s->interrupt_callback, NULL)) < 0)
222                     goto fail;
223             }
224         }
225     }
226
227     ret = oc->oformat->write_packet(oc, pkt);
228
229 fail:
230     if (ret < 0) {
231         oc->streams = NULL;
232         oc->nb_streams = 0;
233         if (seg->list)
234             avio_close(seg->pb);
235         avformat_free_context(oc);
236     }
237
238     return ret;
239 }
240
241 static int seg_write_trailer(struct AVFormatContext *s)
242 {
243     SegmentContext *seg = s->priv_data;
244     AVFormatContext *oc = seg->avf;
245     int ret = segment_end(oc);
246     if (seg->list)
247         avio_close(seg->pb);
248     oc->streams = NULL;
249     oc->nb_streams = 0;
250     avformat_free_context(oc);
251     return ret;
252 }
253
254 #define OFFSET(x) offsetof(SegmentContext, x)
255 #define E AV_OPT_FLAG_ENCODING_PARAM
256 static const AVOption options[] = {
257     { "segment_format",    "container format used for the segments",  OFFSET(format),  AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
258     { "segment_time",      "segment length in seconds",               OFFSET(time),    AV_OPT_TYPE_FLOAT,  {.dbl = 2},     0, FLT_MAX, E },
259     { "segment_list",      "output the segment list",                 OFFSET(list),    AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       E },
260     { "segment_list_size", "maximum number of playlist entries",      OFFSET(size),    AV_OPT_TYPE_INT,    {.dbl = 5},     0, INT_MAX, E },
261     { "segment_wrap",      "number after which the index wraps",      OFFSET(wrap),    AV_OPT_TYPE_INT,    {.dbl = 0},     0, INT_MAX, E },
262     { NULL },
263 };
264
265 static const AVClass seg_class = {
266     .class_name = "segment muxer",
267     .item_name  = av_default_item_name,
268     .option     = options,
269     .version    = LIBAVUTIL_VERSION_INT,
270 };
271
272
273 AVOutputFormat ff_segment_muxer = {
274     .name           = "segment",
275     .long_name      = NULL_IF_CONFIG_SMALL("segment muxer"),
276     .priv_data_size = sizeof(SegmentContext),
277     .flags          = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
278     .write_header   = seg_write_header,
279     .write_packet   = seg_write_packet,
280     .write_trailer  = seg_write_trailer,
281     .priv_class     = &seg_class,
282 };