FFmpeg
webm_chunk.c
Go to the documentation of this file.
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"
43 #include "libavutil/timestamp.h"
44 
45 #define MAX_FILENAME_SIZE 1024
46 
47 typedef struct WebMChunkContext {
48  const AVClass *class;
52  char *http_method;
53  uint64_t duration_written;
54  int64_t prev_pts;
57 
59 {
60  WebMChunkContext *wc = s->priv_data;
61  ff_const59 AVOutputFormat *oformat;
62  AVFormatContext *oc;
63  AVStream *st, *ost = s->streams[0];
64  AVDictionary *dict = NULL;
65  int ret;
66 
67  // DASH Streams can only have one track per file.
68  if (s->nb_streams != 1)
69  return AVERROR(EINVAL);
70 
71  if (!wc->header_filename) {
72  av_log(s, AV_LOG_ERROR, "No header filename provided\n");
73  return AVERROR(EINVAL);
74  }
75 
77 
78  oformat = av_guess_format("webm", s->url, "video/webm");
79  if (!oformat)
81 
82  ret = avformat_alloc_output_context2(&wc->avf, oformat, NULL, NULL);
83  if (ret < 0)
84  return ret;
85  oc = wc->avf;
86 
88  wc->header_filename = NULL;
89 
91  oc->max_delay = s->max_delay;
95 
96  oc->flush_packets = 0;
97 
98  if ((ret = av_dict_copy(&oc->metadata, s->metadata, 0)) < 0)
99  return ret;
100 
101  if (!(st = avformat_new_stream(oc, NULL)))
102  return AVERROR(ENOMEM);
103 
104  if ((ret = avcodec_parameters_copy(st->codecpar, ost->codecpar)) < 0 ||
105  (ret = av_dict_copy(&st->metadata, ost->metadata, 0)) < 0)
106  return ret;
107 
109  st->disposition = ost->disposition;
111  ost->time_base.den);
112 
113  if ((ret = av_dict_set_int(&dict, "dash", 1, 0)) < 0 ||
114  (ret = av_dict_set_int(&dict, "cluster_time_limit",
115  wc->chunk_duration, 0)) < 0 ||
116  (ret = av_dict_set_int(&dict, "live", 1, 0)) < 0)
117  goto fail;
118 
119  ret = avformat_init_output(oc, &dict);
120 fail:
121  av_dict_free(&dict);
122  if (ret < 0)
123  return ret;
124 
125  // Copy the timing info back to the original stream
126  // so that the timestamps of the packets are directly usable
128  st->time_base.den);
129 
130  // This ensures that the timestamps will already be properly shifted
131  // when the packets arrive here, so we don't need to shift again.
135  oc->avoid_negative_ts = 0;
136 
137  return 0;
138 }
139 
141 {
142  WebMChunkContext *wc = s->priv_data;
143  if (!filename) {
144  return AVERROR(EINVAL);
145  }
146  if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
147  s->url, wc->chunk_index - 1) < 0) {
148  av_log(s, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->url);
149  return AVERROR(EINVAL);
150  }
151  return 0;
152 }
153 
155 {
156  WebMChunkContext *wc = s->priv_data;
157  AVFormatContext *oc = wc->avf;
158  int ret;
160 
161  if (wc->http_method)
162  if ((ret = av_dict_set(&options, "method", wc->http_method, 0)) < 0)
163  return ret;
164  ret = s->io_open(s, &oc->pb, oc->url, AVIO_FLAG_WRITE, &options);
165  av_dict_free(&options);
166  if (ret < 0)
167  return ret;
168 
169  oc->pb->seekable = 0;
170  ret = avformat_write_header(oc, NULL);
171  ff_format_io_close(s, &oc->pb);
172  if (ret < 0)
173  return ret;
174  return 0;
175 }
176 
178 {
179  WebMChunkContext *wc = s->priv_data;
180  AVFormatContext *oc = wc->avf;
181  int ret;
182 
183  ret = avio_open_dyn_buf(&oc->pb);
184  if (ret < 0)
185  return ret;
186  wc->chunk_index++;
187  return 0;
188 }
189 
190 static int chunk_end(AVFormatContext *s, int flush)
191 {
192  WebMChunkContext *wc = s->priv_data;
193  AVFormatContext *oc = wc->avf;
194  int ret;
195  int buffer_size;
196  uint8_t *buffer;
197  AVIOContext *pb;
198  char filename[MAX_FILENAME_SIZE];
200 
201  if (!oc->pb)
202  return 0;
203 
204  if (flush)
205  // Flush the cluster in WebM muxer.
206  av_write_frame(oc, NULL);
207  buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
208  oc->pb = NULL;
209  ret = get_chunk_filename(s, filename);
210  if (ret < 0)
211  goto fail;
212  if (wc->http_method)
213  if ((ret = av_dict_set(&options, "method", wc->http_method, 0)) < 0)
214  goto fail;
215  ret = s->io_open(s, &pb, filename, AVIO_FLAG_WRITE, &options);
216  av_dict_free(&options);
217  if (ret < 0)
218  goto fail;
219  avio_write(pb, buffer, buffer_size);
220  ff_format_io_close(s, &pb);
221 fail:
222  av_free(buffer);
223  return (ret < 0) ? ret : 0;
224 }
225 
227 {
228  WebMChunkContext *wc = s->priv_data;
229  AVFormatContext *oc = wc->avf;
230  AVStream *st = s->streams[pkt->stream_index];
231  int ret;
232 
233  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
234  if (wc->prev_pts != AV_NOPTS_VALUE)
235  wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
236  st->time_base,
237  (AVRational) {1, 1000});
238  wc->prev_pts = pkt->pts;
239  }
240 
241  // For video, a new chunk is started only on key frames. For audio, a new
242  // chunk is started based on chunk_duration. Also, a new chunk is started
243  // unconditionally if there is no currently open chunk.
244  if (!oc->pb || (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
245  (pkt->flags & AV_PKT_FLAG_KEY)) ||
247  wc->duration_written >= wc->chunk_duration)) {
248  wc->duration_written = 0;
249  if ((ret = chunk_end(s, 1)) < 0 || (ret = chunk_start(s)) < 0) {
250  return ret;
251  }
252  }
253 
254  // We only have one stream, so use the non-interleaving av_write_frame.
255  return av_write_frame(oc, pkt);
256 }
257 
259 {
260  WebMChunkContext *wc = s->priv_data;
261  AVFormatContext *oc = wc->avf;
262  int ret;
263 
264  if (!oc->pb) {
265  ret = chunk_start(s);
266  if (ret < 0)
267  return ret;
268  }
269  ret = av_write_trailer(oc);
270  if (ret < 0)
271  return ret;
272  return chunk_end(s, 0);
273 }
274 
276 {
277  WebMChunkContext *wc = s->priv_data;
278 
279  if (!wc->avf)
280  return;
281 
282  ffio_free_dyn_buf(&wc->avf->pb);
284  wc->avf = NULL;
285 }
286 
287 #define OFFSET(x) offsetof(WebMChunkContext, x)
288 static const AVOption options[] = {
289  { "chunk_start_index", "start index of the chunk", OFFSET(chunk_index), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
290  { "header", "filename of the header where the initialization data will be written", OFFSET(header_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
291  { "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 },
292  { "method", "set the HTTP method", OFFSET(http_method), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
293  { NULL },
294 };
295 
296 #if CONFIG_WEBM_CHUNK_MUXER
297 static const AVClass webm_chunk_class = {
298  .class_name = "WebM Chunk Muxer",
299  .item_name = av_default_item_name,
300  .option = options,
301  .version = LIBAVUTIL_VERSION_INT,
302 };
303 
305  .name = "webm_chunk",
306  .long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
307  .mime_type = "video/webm",
308  .extensions = "chk",
311  .priv_data_size = sizeof(WebMChunkContext),
316  .deinit = webm_chunk_deinit,
317  .priv_class = &webm_chunk_class,
318 };
319 #endif
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
Definition: ffmpeg.c:698
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition: avformat.h:1935
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
Buffered I/O operations.
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1631
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1421
AVOption.
Definition: opt.h:246
static void flush(AVCodecContext *avctx)
int flush_packets
Flush the I/O context after each packet.
Definition: avformat.h:1768
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:883
static int chunk_start(AVFormatContext *s)
Definition: webm_chunk.c:177
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4943
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVFormatContext * avf
Definition: webm_chunk.c:55
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:938
int num
Numerator.
Definition: rational.h:59
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1806
uint64_t duration_written
Definition: webm_chunk.c:53
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:675
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
int avoid_negative_ts_use_pts
Definition: internal.h:119
static AVPacket pkt
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1376
int strict_std_compliance
Allow non-standard and experimental extension.
Definition: avformat.h:1661
char * http_method
Definition: webm_chunk.c:52
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:472
Format I/O context.
Definition: avformat.h:1353
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
uint8_t
AVOptions.
timestamp utils, mostly useful for debugging/logging purposes
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:5697
#define MAX_FILENAME_SIZE
Definition: webm_chunk.c:45
char * header_filename
Definition: webm_chunk.c:49
static void webm_chunk_deinit(AVFormatContext *s)
Definition: webm_chunk.c:275
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4524
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1421
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1484
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:213
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:276
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1565
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define ff_const59
The ff_const59 define is not part of the public API and will be removed without further warning...
Definition: avformat.h:544
#define AVFMT_FLAG_FLUSH_PACKETS
Flush the AVIOContext every packet.
Definition: avformat.h:1494
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2051
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1593
static const AVOption options[]
Definition: webm_chunk.c:288
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
char * url
input or output URL.
Definition: avformat.h:1449
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4032
simple assert() macros that are a bit more flexible than ISO C assert().
int avformat_alloc_output_context2(AVFormatContext **ctx, ff_const59 AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:148
#define fail()
Definition: checkasm.h:122
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1539
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1409
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition: mux.c:516
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:461
const char * name
Definition: avformat.h:500
#define OFFSET(x)
Definition: webm_chunk.c:287
#define s(width, name)
Definition: cbs_vp9.c:257
int avoid_negative_ts
Avoid negative timestamps during muxing.
Definition: avformat.h:1684
static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: webm_chunk.c:226
void ff_format_set_url(AVFormatContext *s, char *url)
Set AVFormatContext url field to the provided pointer.
Definition: utils.c:5857
AVDictionary * metadata
Definition: avformat.h:940
av_warn_unused_result int avformat_init_output(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and initialize the codec, but do not write the header.
Definition: mux.c:496
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Definition: utils.c:4792
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1451
Stream structure.
Definition: avformat.h:876
AVIOContext * pb
I/O context.
Definition: avformat.h:1395
static int webm_chunk_write_header(AVFormatContext *s)
Definition: webm_chunk.c:154
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
Describe the class of an AVClass context structure.
Definition: log.h:67
AVOutputFormat ff_webm_chunk_muxer
ff_const59 AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: format.c:51
Rational number (pair of numerator and denominator).
Definition: rational.h:58
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4453
misc parsing utilities
static AVStream * ost
static int get_chunk_filename(AVFormatContext *s, char filename[MAX_FILENAME_SIZE])
Definition: webm_chunk.c:140
Main libavformat public API header.
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:458
static int webm_chunk_write_trailer(AVFormatContext *s)
Definition: webm_chunk.c:258
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set that converts the value to a string and stores it...
Definition: dict.c:147
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:929
int pts_wrap_bits
number of bits in pts (used for wrapping control)
Definition: avformat.h:1068
int den
Denominator.
Definition: rational.h:60
#define av_free(p)
void * priv_data
Format private data.
Definition: avformat.h:1381
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
static int chunk_end(AVFormatContext *s, int flush)
Definition: webm_chunk.c:190
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition: mux.c:1245
int64_t prev_pts
Definition: webm_chunk.c:54
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
Definition: error.h:60
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
int stream_index
Definition: avcodec.h:1535
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:905
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
static int webm_chunk_init(AVFormatContext *s)
Definition: webm_chunk.c:58
This structure stores compressed data.
Definition: avcodec.h:1510
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1526
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
GLuint buffer
Definition: opengl_enc.c:101
#define AVFMT_NEEDNUMBER
Needs &#39;d&#39; in filename.
Definition: avformat.h:459