FFmpeg  2.8.17
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 "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"
42 #include "libavutil/timestamp.h"
43 
44 #define MAX_FILENAME_SIZE 1024
45 
46 typedef struct WebMChunkContext {
47  const AVClass *class;
52  uint64_t duration_written;
53  int prev_pts;
57 
59 {
60  WebMChunkContext *wc = s->priv_data;
61  AVFormatContext *oc;
62  int ret;
63 
65  if (ret < 0)
66  return ret;
67  oc = wc->avf;
68 
70  oc->max_delay = s->max_delay;
71  av_dict_copy(&oc->metadata, s->metadata, 0);
72 
73  *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
75  av_opt_set_int(oc->priv_data, "dash", 1, 0);
76  av_opt_set_int(oc->priv_data, "cluster_time_limit", wc->chunk_duration, 0);
77  av_opt_set_int(oc->priv_data, "live", 1, 0);
78 
79  oc->streams = s->streams;
80  oc->nb_streams = s->nb_streams;
81 
82  return 0;
83 }
84 
85 static int get_chunk_filename(AVFormatContext *s, int is_header, char filename[MAX_FILENAME_SIZE])
86 {
87  WebMChunkContext *wc = s->priv_data;
88  AVFormatContext *oc = wc->avf;
89  if (!filename) {
90  return AVERROR(EINVAL);
91  }
92  if (is_header) {
93  int len;
94  if (!wc->header_filename) {
95  return AVERROR(EINVAL);
96  }
97  len = av_strlcpy(filename, wc->header_filename, MAX_FILENAME_SIZE);
98  if (len >= MAX_FILENAME_SIZE) {
99  av_log(oc, AV_LOG_ERROR, "Header filename too long\n");
100  return AVERROR(EINVAL);
101  }
102  } else {
103  if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
104  s->filename, wc->chunk_index - 1) < 0) {
105  av_log(oc, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->filename);
106  return AVERROR(EINVAL);
107  }
108  }
109  return 0;
110 }
111 
113 {
114  WebMChunkContext *wc = s->priv_data;
115  AVFormatContext *oc = NULL;
116  int ret;
117 
118  // DASH Streams can only have either one track per file.
119  if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
120 
121  wc->chunk_index = wc->chunk_start_index;
122  wc->oformat = av_guess_format("webm", s->filename, "video/webm");
123  if (!wc->oformat)
125 
126  ret = chunk_mux_init(s);
127  if (ret < 0)
128  return ret;
129  oc = wc->avf;
130  ret = get_chunk_filename(s, 1, oc->filename);
131  if (ret < 0)
132  return ret;
133  ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
134  &s->interrupt_callback, NULL);
135  if (ret < 0)
136  return ret;
137 
138  oc->pb->seekable = 0;
139  ret = oc->oformat->write_header(oc);
140  if (ret < 0)
141  return ret;
142  avio_close(oc->pb);
143  return 0;
144 }
145 
147 {
148  WebMChunkContext *wc = s->priv_data;
149  AVFormatContext *oc = wc->avf;
150  int ret;
151 
152  ret = avio_open_dyn_buf(&oc->pb);
153  if (ret < 0)
154  return ret;
155  wc->chunk_index++;
156  return 0;
157 }
158 
160 {
161  WebMChunkContext *wc = s->priv_data;
162  AVFormatContext *oc = wc->avf;
163  int ret;
164  int buffer_size;
165  uint8_t *buffer;
166  AVIOContext *pb;
167  char filename[MAX_FILENAME_SIZE];
168 
169  if (wc->chunk_start_index == wc->chunk_index)
170  return 0;
171  // Flush the cluster in WebM muxer.
172  oc->oformat->write_packet(oc, NULL);
173  buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
174  ret = get_chunk_filename(s, 0, filename);
175  if (ret < 0)
176  goto fail;
177  ret = avio_open2(&pb, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
178  if (ret < 0)
179  goto fail;
180  avio_write(pb, buffer, buffer_size);
181  ret = avio_close(pb);
182  if (ret < 0)
183  goto fail;
184  oc->pb = NULL;
185 fail:
186  av_free(buffer);
187  return (ret < 0) ? ret : 0;
188 }
189 
191 {
192  WebMChunkContext *wc = s->priv_data;
193  AVFormatContext *oc = wc->avf;
194  AVStream *st = s->streams[pkt->stream_index];
195  int ret;
196 
197  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
198  wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
199  st->time_base,
200  (AVRational) {1, 1000});
201  wc->prev_pts = pkt->pts;
202  }
203 
204  // For video, a new chunk is started only on key frames. For audio, a new
205  // chunk is started based on chunk_duration.
206  if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
207  (pkt->flags & AV_PKT_FLAG_KEY)) ||
209  (pkt->pts == 0 || wc->duration_written >= wc->chunk_duration))) {
210  wc->duration_written = 0;
211  if ((ret = chunk_end(s)) < 0 || (ret = chunk_start(s)) < 0) {
212  goto fail;
213  }
214  }
215 
216  ret = oc->oformat->write_packet(oc, pkt);
217  if (ret < 0)
218  goto fail;
219 
220 fail:
221  if (ret < 0) {
222  oc->streams = NULL;
223  oc->nb_streams = 0;
225  }
226 
227  return ret;
228 }
229 
231 {
232  WebMChunkContext *wc = s->priv_data;
233  AVFormatContext *oc = wc->avf;
234  oc->oformat->write_trailer(oc);
235  chunk_end(s);
236  oc->streams = NULL;
237  oc->nb_streams = 0;
239  return 0;
240 }
241 
242 #define OFFSET(x) offsetof(WebMChunkContext, x)
243 static const AVOption options[] = {
244  { "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 },
245  { "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 },
246  { "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 },
247  { NULL },
248 };
249 
250 #if CONFIG_WEBM_CHUNK_MUXER
251 static const AVClass webm_chunk_class = {
252  .class_name = "WebM Chunk Muxer",
253  .item_name = av_default_item_name,
254  .option = options,
255  .version = LIBAVUTIL_VERSION_INT,
256 };
257 
258 AVOutputFormat ff_webm_chunk_muxer = {
259  .name = "webm_chunk",
260  .long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
261  .mime_type = "video/webm",
262  .extensions = "chk",
265  .priv_data_size = sizeof(WebMChunkContext),
269  .priv_class = &webm_chunk_class,
270 };
271 #endif
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Buffered I/O operations.
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1535
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1150
AVOption.
Definition: opt.h:255
static int chunk_start(AVFormatContext *s)
Definition: webm_chunk.c:146
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1178
AVFormatContext * avf
Definition: webm_chunk.c:55
uint64_t duration_written
Definition: webm_chunk.c:52
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:486
int(* write_packet)(struct AVFormatContext *, AVPacket *pkt)
Write a packet.
Definition: avformat.h:576
static AVPacket pkt
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1138
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:495
Format I/O context.
Definition: avformat.h:1285
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
#define MAX_FILENAME_SIZE
Definition: webm_chunk.c:44
char * header_filename
Definition: webm_chunk.c:49
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1353
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:178
AVOutputFormat * oformat
Definition: webm_chunk.c:54
void av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.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:285
struct AVOutputFormat * oformat
The output container format.
Definition: avformat.h:1304
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1479
int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition: mux.c:148
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:147
static int get_chunk_filename(AVFormatContext *s, int is_header, char filename[MAX_FILENAME_SIZE])
Definition: webm_chunk.c:85
#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:1497
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
static const AVOption options[]
Definition: webm_chunk.c:243
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:178
int(* write_header)(struct AVFormatContext *)
Definition: avformat.h:568
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:945
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:491
static int chunk_mux_init(AVFormatContext *s)
Definition: webm_chunk.c:58
simple assert() macros that are a bit more flexible than ISO C assert().
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
#define fail()
Definition: checkasm.h:57
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1439
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:873
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1341
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:160
char filename[1024]
input or output filename
Definition: avformat.h:1361
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:483
const char * name
Definition: avformat.h:525
#define OFFSET(x)
Definition: webm_chunk.c:242
static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: webm_chunk.c:190
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:98
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:553
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Return in 'buf' the path with 'd' replaced by a number.
Definition: utils.c:3923
Stream structure.
Definition: avformat.h:854
enum AVMediaType codec_type
Definition: avcodec.h:1520
AVIOContext * pb
I/O context.
Definition: avformat.h:1327
static int webm_chunk_write_header(AVFormatContext *s)
Definition: webm_chunk.c:112
Describe the class of an AVClass context structure.
Definition: log.h:67
rational number numerator/denominator
Definition: rational.h:43
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:922
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:3698
misc parsing utilities
static int chunk_end(AVFormatContext *s)
Definition: webm_chunk.c:159
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:477
static int webm_chunk_write_trailer(AVFormatContext *s)
Definition: webm_chunk.c:230
#define av_free(p)
int len
void * priv_data
Format private data.
Definition: avformat.h:1313
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
#define AVERROR_MUXER_NOT_FOUND
Muxer not found.
Definition: error.h:60
int stream_index
Definition: avcodec.h:1435
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:896
This structure stores compressed data.
Definition: avcodec.h:1410
int(* write_trailer)(struct AVFormatContext *)
Definition: avformat.h:577
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1426
GLuint buffer
Definition: opengl_enc.c:102
#define AVFMT_NEEDNUMBER
Needs 'd' in filename.
Definition: avformat.h:478