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