]> git.sesse.net Git - ffmpeg/blob - libavformat/webm_chunk.c
Merge commit 'c0c4d7a0a556ec66e3068d36a883e84d1efb0690'
[ffmpeg] / libavformat / webm_chunk.c
1 /*
2  * Copyright (c) 2015, Vignesh Venkatasubramanian
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file WebM Chunk Muxer
23  * The chunk muxer enables writing WebM Live chunks where there is a header
24  * chunk, followed by data chunks where each Cluster is written out as a Chunk.
25  */
26
27 #include <float.h>
28 #include <time.h>
29
30 #include "avformat.h"
31 #include "avio.h"
32 #include "avio_internal.h"
33 #include "internal.h"
34
35 #include "libavutil/avassert.h"
36 #include "libavutil/log.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/avstring.h"
39 #include "libavutil/parseutils.h"
40 #include "libavutil/mathematics.h"
41 #include "libavutil/time.h"
42 #include "libavutil/time_internal.h"
43 #include "libavutil/timestamp.h"
44
45 #define MAX_FILENAME_SIZE 1024
46
47 typedef struct WebMChunkContext {
48     const AVClass *class;
49     int chunk_start_index;
50     char *header_filename;
51     int chunk_duration;
52     int chunk_index;
53     uint64_t duration_written;
54     int prev_pts;
55     AVOutputFormat *oformat;
56     AVFormatContext *avf;
57 } WebMChunkContext;
58
59 static int chunk_mux_init(AVFormatContext *s)
60 {
61     WebMChunkContext *wc = s->priv_data;
62     AVFormatContext *oc;
63     int ret;
64
65     ret = avformat_alloc_output_context2(&wc->avf, wc->oformat, NULL, NULL);
66     if (ret < 0)
67         return ret;
68     oc = wc->avf;
69
70     oc->interrupt_callback = s->interrupt_callback;
71     oc->max_delay          = s->max_delay;
72     av_dict_copy(&oc->metadata, s->metadata, 0);
73
74     *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
75     av_opt_set_defaults(oc->priv_data);
76     av_opt_set_int(oc->priv_data, "dash", 1, 0);
77     av_opt_set_int(oc->priv_data, "cluster_time_limit", wc->chunk_duration, 0);
78     av_opt_set_int(oc->priv_data, "live", 1, 0);
79
80     oc->streams = s->streams;
81     oc->nb_streams = s->nb_streams;
82
83     return 0;
84 }
85
86 static int get_chunk_filename(AVFormatContext *s, int is_header, char *filename)
87 {
88     WebMChunkContext *wc = s->priv_data;
89     AVFormatContext *oc = wc->avf;
90     if (!filename) {
91         return AVERROR(EINVAL);
92     }
93     if (is_header) {
94         if (!wc->header_filename) {
95             return AVERROR(EINVAL);
96         }
97         av_strlcpy(filename, wc->header_filename, strlen(wc->header_filename) + 1);
98     } else {
99         if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
100                                   s->filename, wc->chunk_index - 1) < 0) {
101             av_log(oc, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->filename);
102             return AVERROR(EINVAL);
103         }
104     }
105     return 0;
106 }
107
108 static int webm_chunk_write_header(AVFormatContext *s)
109 {
110     WebMChunkContext *wc = s->priv_data;
111     AVFormatContext *oc = NULL;
112     int ret;
113
114     // DASH Streams can only have either one track per file.
115     if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
116
117     wc->chunk_index = wc->chunk_start_index;
118     wc->oformat = av_guess_format("webm", s->filename, "video/webm");
119     if (!wc->oformat)
120         return AVERROR_MUXER_NOT_FOUND;
121
122     ret = chunk_mux_init(s);
123     if (ret < 0)
124         return ret;
125     oc = wc->avf;
126     ret = get_chunk_filename(s, 1, oc->filename);
127     if (ret < 0)
128         return ret;
129     ret = ffio_open_whitelist(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
130                               &s->interrupt_callback, NULL, s->protocol_whitelist);
131     if (ret < 0)
132         return ret;
133
134     oc->pb->seekable = 0;
135     ret = oc->oformat->write_header(oc);
136     if (ret < 0)
137         return ret;
138     avio_close(oc->pb);
139     return 0;
140 }
141
142 static int chunk_start(AVFormatContext *s)
143 {
144     WebMChunkContext *wc = s->priv_data;
145     AVFormatContext *oc = wc->avf;
146     int ret;
147
148     ret = avio_open_dyn_buf(&oc->pb);
149     if (ret < 0)
150         return ret;
151     wc->chunk_index++;
152     return 0;
153 }
154
155 static int chunk_end(AVFormatContext *s)
156 {
157     WebMChunkContext *wc = s->priv_data;
158     AVFormatContext *oc = wc->avf;
159     int ret;
160     int buffer_size;
161     uint8_t *buffer;
162     AVIOContext *pb;
163     char filename[MAX_FILENAME_SIZE];
164
165     if (wc->chunk_start_index == wc->chunk_index)
166         return 0;
167     // Flush the cluster in WebM muxer.
168     oc->oformat->write_packet(oc, NULL);
169     buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
170     ret = get_chunk_filename(s, 0, filename);
171     if (ret < 0)
172         goto fail;
173     ret = ffio_open_whitelist(&pb, filename, AVIO_FLAG_WRITE,
174                               &s->interrupt_callback, NULL, s->protocol_whitelist);
175     if (ret < 0)
176         goto fail;
177     avio_write(pb, buffer, buffer_size);
178     ret = avio_close(pb);
179     if (ret < 0)
180         goto fail;
181     oc->pb = NULL;
182 fail:
183     av_free(buffer);
184     return (ret < 0) ? ret : 0;
185 }
186
187 static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
188 {
189     WebMChunkContext *wc = s->priv_data;
190     AVFormatContext *oc = wc->avf;
191     AVStream *st = s->streams[pkt->stream_index];
192     int ret;
193
194     if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
195         wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
196                                              st->time_base,
197                                              (AVRational) {1, 1000});
198         wc->prev_pts = pkt->pts;
199     }
200
201     // For video, a new chunk is started only on key frames. For audio, a new
202     // chunk is started based on chunk_duration.
203     if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
204          (pkt->flags & AV_PKT_FLAG_KEY)) ||
205         (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
206          (pkt->pts == 0 || wc->duration_written >= wc->chunk_duration))) {
207         wc->duration_written = 0;
208         if ((ret = chunk_end(s)) < 0 || (ret = chunk_start(s)) < 0) {
209             goto fail;
210         }
211     }
212
213     ret = oc->oformat->write_packet(oc, pkt);
214     if (ret < 0)
215         goto fail;
216
217 fail:
218     if (ret < 0) {
219         oc->streams = NULL;
220         oc->nb_streams = 0;
221         avformat_free_context(oc);
222     }
223
224     return ret;
225 }
226
227 static int webm_chunk_write_trailer(AVFormatContext *s)
228 {
229     WebMChunkContext *wc = s->priv_data;
230     AVFormatContext *oc = wc->avf;
231     oc->oformat->write_trailer(oc);
232     chunk_end(s);
233     oc->streams = NULL;
234     oc->nb_streams = 0;
235     avformat_free_context(oc);
236     return 0;
237 }
238
239 #define OFFSET(x) offsetof(WebMChunkContext, x)
240 static const AVOption options[] = {
241     { "chunk_start_index",  "start index of the chunk", OFFSET(chunk_start_index), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
242     { "header", "filename of the header where the initialization data will be written", OFFSET(header_filename), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
243     { "audio_chunk_duration", "duration of each chunk in milliseconds", OFFSET(chunk_duration), AV_OPT_TYPE_INT, {.i64 = 5000}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
244     { NULL },
245 };
246
247 #if CONFIG_WEBM_CHUNK_MUXER
248 static const AVClass webm_chunk_class = {
249     .class_name = "WebM Chunk Muxer",
250     .item_name  = av_default_item_name,
251     .option     = options,
252     .version    = LIBAVUTIL_VERSION_INT,
253 };
254
255 AVOutputFormat ff_webm_chunk_muxer = {
256     .name           = "webm_chunk",
257     .long_name      = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
258     .mime_type      = "video/webm",
259     .extensions     = "chk",
260     .flags          = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
261                       AVFMT_TS_NONSTRICT,
262     .priv_data_size = sizeof(WebMChunkContext),
263     .write_header   = webm_chunk_write_header,
264     .write_packet   = webm_chunk_write_packet,
265     .write_trailer  = webm_chunk_write_trailer,
266     .priv_class     = &webm_chunk_class,
267 };
268 #endif