]> git.sesse.net Git - ffmpeg/blob - libavformat/webm_chunk.c
tests/tiny_psnr: Make the search range extend both sides from the specified shift...
[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 "internal.h"
33
34 #include "libavutil/avassert.h"
35 #include "libavutil/log.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/parseutils.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/time.h"
41 #include "libavutil/time_internal.h"
42 #include "libavutil/timestamp.h"
43
44 typedef struct WebMChunkContext {
45     const AVClass *class;
46     int chunk_start_index;
47     char *header_filename;
48     int chunk_duration;
49     int chunk_count;
50     int chunk_index;
51     uint64_t duration_written;
52     int prev_pts;
53     AVOutputFormat *oformat;
54     AVFormatContext *avf;
55 } WebMChunkContext;
56
57 static int chunk_mux_init(AVFormatContext *s)
58 {
59     WebMChunkContext *wc = s->priv_data;
60     AVFormatContext *oc;
61     int ret;
62
63     ret = avformat_alloc_output_context2(&wc->avf, wc->oformat, NULL, NULL);
64     if (ret < 0)
65         return ret;
66     oc = wc->avf;
67
68     oc->interrupt_callback = s->interrupt_callback;
69     oc->max_delay          = s->max_delay;
70     av_dict_copy(&oc->metadata, s->metadata, 0);
71
72     oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
73     if (!oc->priv_data) {
74         avio_close(oc->pb);
75         return AVERROR(ENOMEM);
76     }
77     *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
78     av_opt_set_defaults(oc->priv_data);
79     av_opt_set_int(oc->priv_data, "dash", 1, 0);
80     av_opt_set_int(oc->priv_data, "cluster_time_limit", wc->chunk_duration, 0);
81     av_opt_set_int(oc->priv_data, "live", 1, 0);
82
83     oc->streams = s->streams;
84     oc->nb_streams = s->nb_streams;
85
86     return 0;
87 }
88
89 static int set_chunk_filename(AVFormatContext *s, int is_header)
90 {
91     WebMChunkContext *wc = s->priv_data;
92     AVFormatContext *oc = wc->avf;
93     if (is_header) {
94         if (!wc->header_filename) {
95             return AVERROR(EINVAL);
96         }
97         av_strlcpy(oc->filename, wc->header_filename, strlen(wc->header_filename) + 1);
98     } else {
99         if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
100                                   s->filename, wc->chunk_index) < 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_count = 0;
118     wc->chunk_index = wc->chunk_start_index;
119     wc->oformat = av_guess_format("webm", s->filename, "video/webm");
120     if (!wc->oformat)
121         return AVERROR_MUXER_NOT_FOUND;
122
123     ret = chunk_mux_init(s);
124     if (ret < 0)
125         return ret;
126     oc = wc->avf;
127     ret = set_chunk_filename(s, 1);
128     if (ret< 0)
129         return ret;
130     ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
131                      &s->interrupt_callback, NULL);
132     if (ret < 0)
133         return ret;
134
135     oc->pb->seekable = 0;
136     ret = oc->oformat->write_header(oc);
137     if (ret < 0)
138         return ret;
139     avio_close(oc->pb);
140     return 0;
141 }
142
143 static int chunk_start(AVFormatContext *s)
144 {
145     WebMChunkContext *wc = s->priv_data;
146     AVFormatContext *oc = wc->avf;
147     int ret;
148
149     ret = set_chunk_filename(s, 0);
150     if (ret < 0)
151         return ret;
152     wc->chunk_index++;
153     ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
154                      &s->interrupt_callback, NULL);
155     if (ret < 0)
156         return ret;
157     oc->pb->seekable = 0;
158     return 0;
159 }
160
161 static int chunk_end(AVFormatContext *s)
162 {
163     WebMChunkContext *wc = s->priv_data;
164     AVFormatContext *oc = wc->avf;
165     int ret;
166
167     if (wc->chunk_start_index == wc->chunk_index)
168         return 0;
169     // Flush the cluster in WebM muxer.
170     oc->oformat->write_packet(oc, NULL);
171     ret = avio_close(oc->pb);
172     if (ret < 0)
173         return ret;
174     wc->chunk_count++;
175     return 0;
176 }
177
178 static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
179 {
180     WebMChunkContext *wc = s->priv_data;
181     AVFormatContext *oc = wc->avf;
182     AVStream *st = s->streams[pkt->stream_index];
183     int ret;
184
185     if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
186         wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
187                                              st->time_base,
188                                              (AVRational) {1, 1000});
189         wc->prev_pts = pkt->pts;
190     }
191
192     // For video, a new chunk is started only on key frames. For audio, a new
193     // chunk is started based on chunk_duration.
194     if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
195          (pkt->flags & AV_PKT_FLAG_KEY)) ||
196         (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
197          (pkt->pts == 0 || wc->duration_written >= wc->chunk_duration))) {
198         wc->duration_written = 0;
199         if ((ret = chunk_end(s)) < 0 || (ret = chunk_start(s)) < 0) {
200             goto fail;
201         }
202     }
203
204     if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->pts == 0) {
205         goto fail;
206     }
207     ret = oc->oformat->write_packet(oc, pkt);
208     if (ret < 0)
209         goto fail;
210
211 fail:
212     if (ret < 0) {
213         oc->streams = NULL;
214         oc->nb_streams = 0;
215         avformat_free_context(oc);
216     }
217
218     return ret;
219 }
220
221 static int webm_chunk_write_trailer(AVFormatContext *s)
222 {
223     WebMChunkContext *wc = s->priv_data;
224     AVFormatContext *oc = wc->avf;
225     oc->oformat->write_trailer(oc);
226     chunk_end(s);
227     oc->streams = NULL;
228     oc->nb_streams = 0;
229     avformat_free_context(oc);
230     return 0;
231 }
232
233 #define OFFSET(x) offsetof(WebMChunkContext, x)
234 static const AVOption options[] = {
235     { "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 },
236     { "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 },
237     { "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 },
238     { NULL },
239 };
240
241 #if CONFIG_WEBM_CHUNK_MUXER
242 static const AVClass webm_chunk_class = {
243     .class_name = "WebM Chunk Muxer",
244     .item_name  = av_default_item_name,
245     .option     = options,
246     .version    = LIBAVUTIL_VERSION_INT,
247 };
248
249 AVOutputFormat ff_webm_chunk_muxer = {
250     .name           = "webm_chunk",
251     .long_name      = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
252     .mime_type      = "video/webm",
253     .extensions     = "chk",
254     .flags          = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
255                       AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH,
256     .priv_data_size = sizeof(WebMChunkContext),
257     .write_header   = webm_chunk_write_header,
258     .write_packet   = webm_chunk_write_packet,
259     .write_trailer  = webm_chunk_write_trailer,
260     .priv_class     = &webm_chunk_class,
261 };
262 #endif