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