Replace all CODEC_ID_* with AV_CODEC_ID_*
[libav.git] / libavformat / matroskaenc.c
1 /*
2 * Matroska muxer
3 * Copyright (c) 2007 David Conrad
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 "avformat.h"
23 #include "internal.h"
24 #include "riff.h"
25 #include "isom.h"
26 #include "matroska.h"
27 #include "avc.h"
28 #include "flacenc.h"
29 #include "avlanguage.h"
30 #include "libavutil/samplefmt.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/intfloat.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/random_seed.h"
35 #include "libavutil/lfg.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/avstring.h"
38 #include "libavcodec/xiph.h"
39 #include "libavcodec/mpeg4audio.h"
40
41 typedef struct ebml_master {
42 int64_t pos; ///< absolute offset in the file where the master's elements start
43 int sizebytes; ///< how many bytes were reserved for the size
44 } ebml_master;
45
46 typedef struct mkv_seekhead_entry {
47 unsigned int elementid;
48 uint64_t segmentpos;
49 } mkv_seekhead_entry;
50
51 typedef struct mkv_seekhead {
52 int64_t filepos;
53 int64_t segment_offset; ///< the file offset to the beginning of the segment
54 int reserved_size; ///< -1 if appending to file
55 int max_entries;
56 mkv_seekhead_entry *entries;
57 int num_entries;
58 } mkv_seekhead;
59
60 typedef struct {
61 uint64_t pts;
62 int tracknum;
63 int64_t cluster_pos; ///< file offset of the cluster containing the block
64 } mkv_cuepoint;
65
66 typedef struct {
67 int64_t segment_offset;
68 mkv_cuepoint *entries;
69 int num_entries;
70 } mkv_cues;
71
72 typedef struct {
73 int write_dts;
74 } mkv_track;
75
76 #define MODE_MATROSKAv2 0x01
77 #define MODE_WEBM 0x02
78
79 typedef struct MatroskaMuxContext {
80 int mode;
81 AVIOContext *dyn_bc;
82 ebml_master segment;
83 int64_t segment_offset;
84 ebml_master cluster;
85 int64_t cluster_pos; ///< file offset of the current cluster
86 int64_t cluster_pts;
87 int64_t duration_offset;
88 int64_t duration;
89 mkv_seekhead *main_seekhead;
90 mkv_cues *cues;
91 mkv_track *tracks;
92
93 unsigned int audio_buffer_size;
94 AVPacket cur_audio_pkt;
95
96 int have_attachments;
97 } MatroskaMuxContext;
98
99
100 /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit
101 * offset, 4 bytes for target EBML ID */
102 #define MAX_SEEKENTRY_SIZE 21
103
104 /** per-cuepoint-track - 3 1-byte EBML IDs, 3 1-byte EBML sizes, 2
105 * 8-byte uint max */
106 #define MAX_CUETRACKPOS_SIZE 22
107
108 /** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */
109 #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracks
110
111
112 static int ebml_id_size(unsigned int id)
113 {
114 return (av_log2(id+1)-1)/7+1;
115 }
116
117 static void put_ebml_id(AVIOContext *pb, unsigned int id)
118 {
119 int i = ebml_id_size(id);
120 while (i--)
121 avio_w8(pb, id >> (i*8));
122 }
123
124 /**
125 * Write an EBML size meaning "unknown size".
126 *
127 * @param bytes The number of bytes the size should occupy (maximum: 8).
128 */
129 static void put_ebml_size_unknown(AVIOContext *pb, int bytes)
130 {
131 assert(bytes <= 8);
132 avio_w8(pb, 0x1ff >> bytes);
133 while (--bytes)
134 avio_w8(pb, 0xff);
135 }
136
137 /**
138 * Calculate how many bytes are needed to represent a given number in EBML.
139 */
140 static int ebml_num_size(uint64_t num)
141 {
142 int bytes = 1;
143 while ((num+1) >> bytes*7) bytes++;
144 return bytes;
145 }
146
147 /**
148 * Write a number in EBML variable length format.
149 *
150 * @param bytes The number of bytes that need to be used to write the number.
151 * If zero, any number of bytes can be used.
152 */
153 static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes)
154 {
155 int i, needed_bytes = ebml_num_size(num);
156
157 // sizes larger than this are currently undefined in EBML
158 assert(num < (1ULL<<56)-1);
159
160 if (bytes == 0)
161 // don't care how many bytes are used, so use the min
162 bytes = needed_bytes;
163 // the bytes needed to write the given size would exceed the bytes
164 // that we need to use, so write unknown size. This shouldn't happen.
165 assert(bytes >= needed_bytes);
166
167 num |= 1ULL << bytes*7;
168 for (i = bytes - 1; i >= 0; i--)
169 avio_w8(pb, num >> i*8);
170 }
171
172 static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val)
173 {
174 int i, bytes = 1;
175 uint64_t tmp = val;
176 while (tmp>>=8) bytes++;
177
178 put_ebml_id(pb, elementid);
179 put_ebml_num(pb, bytes, 0);
180 for (i = bytes - 1; i >= 0; i--)
181 avio_w8(pb, val >> i*8);
182 }
183
184 static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val)
185 {
186 put_ebml_id(pb, elementid);
187 put_ebml_num(pb, 8, 0);
188 avio_wb64(pb, av_double2int(val));
189 }
190
191 static void put_ebml_binary(AVIOContext *pb, unsigned int elementid,
192 const void *buf, int size)
193 {
194 put_ebml_id(pb, elementid);
195 put_ebml_num(pb, size, 0);
196 avio_write(pb, buf, size);
197 }
198
199 static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str)
200 {
201 put_ebml_binary(pb, elementid, str, strlen(str));
202 }
203
204 /**
205 * Write a void element of a given size. Useful for reserving space in
206 * the file to be written to later.
207 *
208 * @param size The number of bytes to reserve, which must be at least 2.
209 */
210 static void put_ebml_void(AVIOContext *pb, uint64_t size)
211 {
212 int64_t currentpos = avio_tell(pb);
213
214 assert(size >= 2);
215
216 put_ebml_id(pb, EBML_ID_VOID);
217 // we need to subtract the length needed to store the size from the
218 // size we need to reserve so 2 cases, we use 8 bytes to store the
219 // size if possible, 1 byte otherwise
220 if (size < 10)
221 put_ebml_num(pb, size-1, 0);
222 else
223 put_ebml_num(pb, size-9, 8);
224 while(avio_tell(pb) < currentpos + size)
225 avio_w8(pb, 0);
226 }
227
228 static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize)
229 {
230 int bytes = expectedsize ? ebml_num_size(expectedsize) : 8;
231 put_ebml_id(pb, elementid);
232 put_ebml_size_unknown(pb, bytes);
233 return (ebml_master){ avio_tell(pb), bytes };
234 }
235
236 static void end_ebml_master(AVIOContext *pb, ebml_master master)
237 {
238 int64_t pos = avio_tell(pb);
239
240 if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0)
241 return;
242 put_ebml_num(pb, pos - master.pos, master.sizebytes);
243 avio_seek(pb, pos, SEEK_SET);
244 }
245
246 static void put_xiph_size(AVIOContext *pb, int size)
247 {
248 int i;
249 for (i = 0; i < size / 255; i++)
250 avio_w8(pb, 255);
251 avio_w8(pb, size % 255);
252 }
253
254 /**
255 * Initialize a mkv_seekhead element to be ready to index level 1 Matroska
256 * elements. If a maximum number of elements is specified, enough space
257 * will be reserved at the current file location to write a seek head of
258 * that size.
259 *
260 * @param segment_offset The absolute offset to the position in the file
261 * where the segment begins.
262 * @param numelements The maximum number of elements that will be indexed
263 * by this seek head, 0 if unlimited.
264 */
265 static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset, int numelements)
266 {
267 mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead));
268 if (new_seekhead == NULL)
269 return NULL;
270
271 new_seekhead->segment_offset = segment_offset;
272
273 if (numelements > 0) {
274 new_seekhead->filepos = avio_tell(pb);
275 // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID
276 // and size, and 3 bytes to guarantee that an EBML void element
277 // will fit afterwards
278 new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13;
279 new_seekhead->max_entries = numelements;
280 put_ebml_void(pb, new_seekhead->reserved_size);
281 }
282 return new_seekhead;
283 }
284
285 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos)
286 {
287 mkv_seekhead_entry *entries = seekhead->entries;
288
289 // don't store more elements than we reserved space for
290 if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries)
291 return -1;
292
293 entries = av_realloc(entries, (seekhead->num_entries + 1) * sizeof(mkv_seekhead_entry));
294 if (entries == NULL)
295 return AVERROR(ENOMEM);
296
297 entries[seekhead->num_entries ].elementid = elementid;
298 entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset;
299
300 seekhead->entries = entries;
301 return 0;
302 }
303
304 /**
305 * Write the seek head to the file and free it. If a maximum number of
306 * elements was specified to mkv_start_seekhead(), the seek head will
307 * be written at the location reserved for it. Otherwise, it is written
308 * at the current location in the file.
309 *
310 * @return The file offset where the seekhead was written,
311 * -1 if an error occurred.
312 */
313 static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead)
314 {
315 ebml_master metaseek, seekentry;
316 int64_t currentpos;
317 int i;
318
319 currentpos = avio_tell(pb);
320
321 if (seekhead->reserved_size > 0) {
322 if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) {
323 currentpos = -1;
324 goto fail;
325 }
326 }
327
328 metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size);
329 for (i = 0; i < seekhead->num_entries; i++) {
330 mkv_seekhead_entry *entry = &seekhead->entries[i];
331
332 seekentry = start_ebml_master(pb, MATROSKA_ID_SEEKENTRY, MAX_SEEKENTRY_SIZE);
333
334 put_ebml_id(pb, MATROSKA_ID_SEEKID);
335 put_ebml_num(pb, ebml_id_size(entry->elementid), 0);
336 put_ebml_id(pb, entry->elementid);
337
338 put_ebml_uint(pb, MATROSKA_ID_SEEKPOSITION, entry->segmentpos);
339 end_ebml_master(pb, seekentry);
340 }
341 end_ebml_master(pb, metaseek);
342
343 if (seekhead->reserved_size > 0) {
344 uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb);
345 put_ebml_void(pb, remaining);
346 avio_seek(pb, currentpos, SEEK_SET);
347
348 currentpos = seekhead->filepos;
349 }
350 fail:
351 av_free(seekhead->entries);
352 av_free(seekhead);
353
354 return currentpos;
355 }
356
357 static mkv_cues * mkv_start_cues(int64_t segment_offset)
358 {
359 mkv_cues *cues = av_mallocz(sizeof(mkv_cues));
360 if (cues == NULL)
361 return NULL;
362
363 cues->segment_offset = segment_offset;
364 return cues;
365 }
366
367 static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos)
368 {
369 mkv_cuepoint *entries = cues->entries;
370
371 entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint));
372 if (entries == NULL)
373 return AVERROR(ENOMEM);
374
375 if (ts < 0)
376 return 0;
377
378 entries[cues->num_entries ].pts = ts;
379 entries[cues->num_entries ].tracknum = stream + 1;
380 entries[cues->num_entries++].cluster_pos = cluster_pos - cues->segment_offset;
381
382 cues->entries = entries;
383 return 0;
384 }
385
386 static int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, int num_tracks)
387 {
388 ebml_master cues_element;
389 int64_t currentpos;
390 int i, j;
391
392 currentpos = avio_tell(pb);
393 cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0);
394
395 for (i = 0; i < cues->num_entries; i++) {
396 ebml_master cuepoint, track_positions;
397 mkv_cuepoint *entry = &cues->entries[i];
398 uint64_t pts = entry->pts;
399
400 cuepoint = start_ebml_master(pb, MATROSKA_ID_POINTENTRY, MAX_CUEPOINT_SIZE(num_tracks));
401 put_ebml_uint(pb, MATROSKA_ID_CUETIME, pts);
402
403 // put all the entries from different tracks that have the exact same
404 // timestamp into the same CuePoint
405 for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) {
406 track_positions = start_ebml_master(pb, MATROSKA_ID_CUETRACKPOSITION, MAX_CUETRACKPOS_SIZE);
407 put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum );
408 put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION, entry[j].cluster_pos);
409 end_ebml_master(pb, track_positions);
410 }
411 i += j - 1;
412 end_ebml_master(pb, cuepoint);
413 }
414 end_ebml_master(pb, cues_element);
415
416 return currentpos;
417 }
418
419 static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec)
420 {
421 uint8_t *header_start[3];
422 int header_len[3];
423 int first_header_size;
424 int j;
425
426 if (codec->codec_id == AV_CODEC_ID_VORBIS)
427 first_header_size = 30;
428 else
429 first_header_size = 42;
430
431 if (avpriv_split_xiph_headers(codec->extradata, codec->extradata_size,
432 first_header_size, header_start, header_len) < 0) {
433 av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n");
434 return -1;
435 }
436
437 avio_w8(pb, 2); // number packets - 1
438 for (j = 0; j < 2; j++) {
439 put_xiph_size(pb, header_len[j]);
440 }
441 for (j = 0; j < 3; j++)
442 avio_write(pb, header_start[j], header_len[j]);
443
444 return 0;
445 }
446
447 static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate)
448 {
449 MPEG4AudioConfig mp4ac;
450
451 if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata,
452 codec->extradata_size * 8, 1) < 0) {
453 av_log(s, AV_LOG_WARNING, "Error parsing AAC extradata, unable to determine samplerate.\n");
454 return;
455 }
456
457 *sample_rate = mp4ac.sample_rate;
458 *output_sample_rate = mp4ac.ext_sample_rate;
459 }
460
461 static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id)
462 {
463 AVIOContext *dyn_cp;
464 uint8_t *codecpriv;
465 int ret, codecpriv_size;
466
467 ret = avio_open_dyn_buf(&dyn_cp);
468 if(ret < 0)
469 return ret;
470
471 if (native_id) {
472 if (codec->codec_id == AV_CODEC_ID_VORBIS || codec->codec_id == AV_CODEC_ID_THEORA)
473 ret = put_xiph_codecpriv(s, dyn_cp, codec);
474 else if (codec->codec_id == AV_CODEC_ID_FLAC)
475 ret = ff_flac_write_header(dyn_cp, codec, 1);
476 else if (codec->codec_id == AV_CODEC_ID_H264)
477 ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size);
478 else if (codec->extradata_size)
479 avio_write(dyn_cp, codec->extradata, codec->extradata_size);
480 } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
481 if (qt_id) {
482 if (!codec->codec_tag)
483 codec->codec_tag = ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id);
484 if (codec->extradata_size)
485 avio_write(dyn_cp, codec->extradata, codec->extradata_size);
486 } else {
487 if (!codec->codec_tag)
488 codec->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id);
489 if (!codec->codec_tag) {
490 av_log(s, AV_LOG_ERROR, "No bmp codec ID found.\n");
491 ret = -1;
492 }
493
494 ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0);
495 }
496
497 } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) {
498 unsigned int tag;
499 tag = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id);
500 if (!tag) {
501 av_log(s, AV_LOG_ERROR, "No wav codec ID found.\n");
502 ret = -1;
503 }
504 if (!codec->codec_tag)
505 codec->codec_tag = tag;
506
507 ff_put_wav_header(dyn_cp, codec);
508 }
509
510 codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv);
511 if (codecpriv_size)
512 put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv, codecpriv_size);
513 av_free(codecpriv);
514 return ret;
515 }
516
517 static int mkv_write_tracks(AVFormatContext *s)
518 {
519 MatroskaMuxContext *mkv = s->priv_data;
520 AVIOContext *pb = s->pb;
521 ebml_master tracks;
522 int i, j, ret;
523
524 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TRACKS, avio_tell(pb));
525 if (ret < 0) return ret;
526
527 tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0);
528 for (i = 0; i < s->nb_streams; i++) {
529 AVStream *st = s->streams[i];
530 AVCodecContext *codec = st->codec;
531 ebml_master subinfo, track;
532 int native_id = 0;
533 int qt_id = 0;
534 int bit_depth = av_get_bits_per_sample(codec->codec_id);
535 int sample_rate = codec->sample_rate;
536 int output_sample_rate = 0;
537 AVDictionaryEntry *tag;
538
539 if (codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) {
540 mkv->have_attachments = 1;
541 continue;
542 }
543
544 if (!bit_depth)
545 bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3;
546
547 if (codec->codec_id == AV_CODEC_ID_AAC)
548 get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate);
549
550 track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0);
551 put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER , i + 1);
552 put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1);
553 put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet)
554
555 if ((tag = av_dict_get(st->metadata, "title", NULL, 0)))
556 put_ebml_string(pb, MATROSKA_ID_TRACKNAME, tag->value);
557 tag = av_dict_get(st->metadata, "language", NULL, 0);
558 put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und");
559
560 if (st->disposition)
561 put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGDEFAULT, !!(st->disposition & AV_DISPOSITION_DEFAULT));
562
563 // look for a codec ID string specific to mkv to use,
564 // if none are found, use AVI codes
565 for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) {
566 if (ff_mkv_codec_tags[j].id == codec->codec_id) {
567 put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str);
568 native_id = 1;
569 break;
570 }
571 }
572
573 if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 ||
574 codec->codec_id == AV_CODEC_ID_VORBIS)) {
575 av_log(s, AV_LOG_ERROR,
576 "Only VP8 video and Vorbis audio are supported for WebM.\n");
577 return AVERROR(EINVAL);
578 }
579
580 switch (codec->codec_type) {
581 case AVMEDIA_TYPE_VIDEO:
582 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO);
583 put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9);
584
585 if (!native_id &&
586 ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) &&
587 (!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id)
588 || codec->codec_id == AV_CODEC_ID_SVQ1
589 || codec->codec_id == AV_CODEC_ID_SVQ3
590 || codec->codec_id == AV_CODEC_ID_CINEPAK))
591 qt_id = 1;
592
593 if (qt_id)
594 put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME");
595 else if (!native_id) {
596 // if there is no mkv-specific codec ID, use VFW mode
597 put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
598 mkv->tracks[i].write_dts = 1;
599 }
600
601 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
602 // XXX: interlace flag?
603 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width);
604 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height);
605 if ((tag = av_dict_get(s->metadata, "stereo_mode", NULL, 0))) {
606 uint8_t stereo_fmt = atoi(tag->value);
607 int valid_fmt = 0;
608
609 switch (mkv->mode) {
610 case MODE_WEBM:
611 if (stereo_fmt <= MATROSKA_VIDEO_STEREOMODE_TYPE_TOP_BOTTOM
612 || stereo_fmt == MATROSKA_VIDEO_STEREOMODE_TYPE_RIGHT_LEFT)
613 valid_fmt = 1;
614 break;
615 case MODE_MATROSKAv2:
616 if (stereo_fmt <= MATROSKA_VIDEO_STEREOMODE_TYPE_BOTH_EYES_BLOCK_RL)
617 valid_fmt = 1;
618 break;
619 }
620
621 if (valid_fmt)
622 put_ebml_uint (pb, MATROSKA_ID_VIDEOSTEREOMODE, stereo_fmt);
623 }
624 if (st->sample_aspect_ratio.num) {
625 int d_width = codec->width*av_q2d(st->sample_aspect_ratio);
626 put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width);
627 put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height);
628 put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYUNIT, 3);
629 }
630 end_ebml_master(pb, subinfo);
631 break;
632
633 case AVMEDIA_TYPE_AUDIO:
634 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO);
635
636 if (!native_id)
637 // no mkv-specific ID, use ACM mode
638 put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM");
639
640 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0);
641 put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels);
642 put_ebml_float (pb, MATROSKA_ID_AUDIOSAMPLINGFREQ, sample_rate);
643 if (output_sample_rate)
644 put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate);
645 if (bit_depth)
646 put_ebml_uint(pb, MATROSKA_ID_AUDIOBITDEPTH, bit_depth);
647 end_ebml_master(pb, subinfo);
648 break;
649
650 case AVMEDIA_TYPE_SUBTITLE:
651 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_SUBTITLE);
652 if (!native_id) {
653 av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id);
654 return AVERROR(ENOSYS);
655 }
656 break;
657 default:
658 av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n");
659 break;
660 }
661 ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id);
662 if (ret < 0) return ret;
663
664 end_ebml_master(pb, track);
665
666 // ms precision is the de-facto standard timescale for mkv files
667 avpriv_set_pts_info(st, 64, 1, 1000);
668 }
669 end_ebml_master(pb, tracks);
670 return 0;
671 }
672
673 static int mkv_write_chapters(AVFormatContext *s)
674 {
675 MatroskaMuxContext *mkv = s->priv_data;
676 AVIOContext *pb = s->pb;
677 ebml_master chapters, editionentry;
678 AVRational scale = {1, 1E9};
679 int i, ret;
680
681 if (!s->nb_chapters)
682 return 0;
683
684 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CHAPTERS, avio_tell(pb));
685 if (ret < 0) return ret;
686
687 chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0);
688 editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0);
689 put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGDEFAULT, 1);
690 put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGHIDDEN , 0);
691 for (i = 0; i < s->nb_chapters; i++) {
692 ebml_master chapteratom, chapterdisplay;
693 AVChapter *c = s->chapters[i];
694 AVDictionaryEntry *t = NULL;
695
696 chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0);
697 put_ebml_uint(pb, MATROSKA_ID_CHAPTERUID, c->id);
698 put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMESTART,
699 av_rescale_q(c->start, c->time_base, scale));
700 put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMEEND,
701 av_rescale_q(c->end, c->time_base, scale));
702 put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGHIDDEN , 0);
703 put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGENABLED, 1);
704 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
705 chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0);
706 put_ebml_string(pb, MATROSKA_ID_CHAPSTRING, t->value);
707 put_ebml_string(pb, MATROSKA_ID_CHAPLANG , "und");
708 end_ebml_master(pb, chapterdisplay);
709 }
710 end_ebml_master(pb, chapteratom);
711 }
712 end_ebml_master(pb, editionentry);
713 end_ebml_master(pb, chapters);
714 return 0;
715 }
716
717 static void mkv_write_simpletag(AVIOContext *pb, AVDictionaryEntry *t)
718 {
719 uint8_t *key = av_strdup(t->key);
720 uint8_t *p = key;
721 const uint8_t *lang = NULL;
722 ebml_master tag;
723
724 if ((p = strrchr(p, '-')) &&
725 (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL)))
726 *p = 0;
727
728 p = key;
729 while (*p) {
730 if (*p == ' ')
731 *p = '_';
732 else if (*p >= 'a' && *p <= 'z')
733 *p -= 'a' - 'A';
734 p++;
735 }
736
737 tag = start_ebml_master(pb, MATROSKA_ID_SIMPLETAG, 0);
738 put_ebml_string(pb, MATROSKA_ID_TAGNAME, key);
739 if (lang)
740 put_ebml_string(pb, MATROSKA_ID_TAGLANG, lang);
741 put_ebml_string(pb, MATROSKA_ID_TAGSTRING, t->value);
742 end_ebml_master(pb, tag);
743
744 av_freep(&key);
745 }
746
747 static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid,
748 unsigned int uid, ebml_master *tags)
749 {
750 MatroskaMuxContext *mkv = s->priv_data;
751 ebml_master tag, targets;
752 AVDictionaryEntry *t = NULL;
753 int ret;
754
755 if (!tags->pos) {
756 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TAGS, avio_tell(s->pb));
757 if (ret < 0) return ret;
758
759 *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0);
760 }
761
762 tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0);
763 targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0);
764 if (elementid)
765 put_ebml_uint(s->pb, elementid, uid);
766 end_ebml_master(s->pb, targets);
767
768 while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
769 if (av_strcasecmp(t->key, "title"))
770 mkv_write_simpletag(s->pb, t);
771
772 end_ebml_master(s->pb, tag);
773 return 0;
774 }
775
776 static int mkv_write_tags(AVFormatContext *s)
777 {
778 ebml_master tags = {0};
779 int i, ret;
780
781 ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL);
782
783 if (av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
784 ret = mkv_write_tag(s, s->metadata, 0, 0, &tags);
785 if (ret < 0) return ret;
786 }
787
788 for (i = 0; i < s->nb_streams; i++) {
789 AVStream *st = s->streams[i];
790
791 if (!av_dict_get(st->metadata, "", 0, AV_DICT_IGNORE_SUFFIX))
792 continue;
793
794 ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags);
795 if (ret < 0) return ret;
796 }
797
798 for (i = 0; i < s->nb_chapters; i++) {
799 AVChapter *ch = s->chapters[i];
800
801 if (!av_dict_get(ch->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
802 continue;
803
804 ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id, &tags);
805 if (ret < 0) return ret;
806 }
807
808 if (tags.pos)
809 end_ebml_master(s->pb, tags);
810 return 0;
811 }
812
813 static int mkv_write_attachments(AVFormatContext *s)
814 {
815 MatroskaMuxContext *mkv = s->priv_data;
816 AVIOContext *pb = s->pb;
817 ebml_master attachments;
818 AVLFG c;
819 int i, ret;
820
821 if (!mkv->have_attachments)
822 return 0;
823
824 av_lfg_init(&c, av_get_random_seed());
825
826 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_ATTACHMENTS, avio_tell(pb));
827 if (ret < 0) return ret;
828
829 attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0);
830
831 for (i = 0; i < s->nb_streams; i++) {
832 AVStream *st = s->streams[i];
833 ebml_master attached_file;
834 AVDictionaryEntry *t;
835 const char *mimetype = NULL;
836
837 if (st->codec->codec_type != AVMEDIA_TYPE_ATTACHMENT)
838 continue;
839
840 attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0);
841
842 if (t = av_dict_get(st->metadata, "title", NULL, 0))
843 put_ebml_string(pb, MATROSKA_ID_FILEDESC, t->value);
844 if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) {
845 av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i);
846 return AVERROR(EINVAL);
847 }
848 put_ebml_string(pb, MATROSKA_ID_FILENAME, t->value);
849 if (t = av_dict_get(st->metadata, "mimetype", NULL, 0))
850 mimetype = t->value;
851 else if (st->codec->codec_id != AV_CODEC_ID_NONE ) {
852 int i;
853 for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++)
854 if (ff_mkv_mime_tags[i].id == st->codec->codec_id) {
855 mimetype = ff_mkv_mime_tags[i].str;
856 break;
857 }
858 }
859 if (!mimetype) {
860 av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and "
861 "it cannot be deduced from the codec id.\n", i);
862 return AVERROR(EINVAL);
863 }
864
865 put_ebml_string(pb, MATROSKA_ID_FILEMIMETYPE, mimetype);
866 put_ebml_binary(pb, MATROSKA_ID_FILEDATA, st->codec->extradata, st->codec->extradata_size);
867 put_ebml_uint(pb, MATROSKA_ID_FILEUID, av_lfg_get(&c));
868 end_ebml_master(pb, attached_file);
869 }
870 end_ebml_master(pb, attachments);
871
872 return 0;
873 }
874
875 static int mkv_write_header(AVFormatContext *s)
876 {
877 MatroskaMuxContext *mkv = s->priv_data;
878 AVIOContext *pb = s->pb;
879 ebml_master ebml_header, segment_info;
880 AVDictionaryEntry *tag;
881 int ret, i;
882
883 if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM;
884 else mkv->mode = MODE_MATROSKAv2;
885
886 mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks));
887 if (!mkv->tracks)
888 return AVERROR(ENOMEM);
889
890 ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0);
891 put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1);
892 put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1);
893 put_ebml_uint (pb, EBML_ID_EBMLMAXIDLENGTH , 4);
894 put_ebml_uint (pb, EBML_ID_EBMLMAXSIZELENGTH , 8);
895 put_ebml_string (pb, EBML_ID_DOCTYPE , s->oformat->name);
896 put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , 2);
897 put_ebml_uint (pb, EBML_ID_DOCTYPEREADVERSION , 2);
898 end_ebml_master(pb, ebml_header);
899
900 mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT, 0);
901 mkv->segment_offset = avio_tell(pb);
902
903 // we write 2 seek heads - one at the end of the file to point to each
904 // cluster, and one at the beginning to point to all other level one
905 // elements (including the seek head at the end of the file), which
906 // isn't more than 10 elements if we only write one of each other
907 // currently defined level 1 element
908 mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10);
909 if (!mkv->main_seekhead)
910 return AVERROR(ENOMEM);
911
912 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_INFO, avio_tell(pb));
913 if (ret < 0) return ret;
914
915 segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0);
916 put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000);
917 if ((tag = av_dict_get(s->metadata, "title", NULL, 0)))
918 put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value);
919 if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
920 uint32_t segment_uid[4];
921 AVLFG lfg;
922
923 av_lfg_init(&lfg, av_get_random_seed());
924
925 for (i = 0; i < 4; i++)
926 segment_uid[i] = av_lfg_get(&lfg);
927
928 put_ebml_string(pb, MATROSKA_ID_MUXINGAPP , LIBAVFORMAT_IDENT);
929 put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
930 put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16);
931 }
932
933 // reserve space for the duration
934 mkv->duration = 0;
935 mkv->duration_offset = avio_tell(pb);
936 put_ebml_void(pb, 11); // assumes double-precision float to be written
937 end_ebml_master(pb, segment_info);
938
939 ret = mkv_write_tracks(s);
940 if (ret < 0) return ret;
941
942 if (mkv->mode != MODE_WEBM) {
943 ret = mkv_write_chapters(s);
944 if (ret < 0) return ret;
945
946 ret = mkv_write_tags(s);
947 if (ret < 0) return ret;
948
949 ret = mkv_write_attachments(s);
950 if (ret < 0) return ret;
951 }
952
953 if (!s->pb->seekable)
954 mkv_write_seekhead(pb, mkv->main_seekhead);
955
956 mkv->cues = mkv_start_cues(mkv->segment_offset);
957 if (mkv->cues == NULL)
958 return AVERROR(ENOMEM);
959
960 av_init_packet(&mkv->cur_audio_pkt);
961 mkv->cur_audio_pkt.size = 0;
962 mkv->audio_buffer_size = 0;
963
964 avio_flush(pb);
965 return 0;
966 }
967
968 static int mkv_blockgroup_size(int pkt_size)
969 {
970 int size = pkt_size + 4;
971 size += ebml_num_size(size);
972 size += 2; // EBML ID for block and block duration
973 size += 8; // max size of block duration
974 size += ebml_num_size(size);
975 size += 1; // blockgroup EBML ID
976 return size;
977 }
978
979 static int ass_get_duration(const uint8_t *p)
980 {
981 int sh, sm, ss, sc, eh, em, es, ec;
982 uint64_t start, end;
983
984 if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
985 &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8)
986 return 0;
987 start = 3600000*sh + 60000*sm + 1000*ss + 10*sc;
988 end = 3600000*eh + 60000*em + 1000*es + 10*ec;
989 return end - start;
990 }
991
992 static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
993 {
994 MatroskaMuxContext *mkv = s->priv_data;
995 int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;
996 uint8_t *start, *end, *data = pkt->data;
997 ebml_master blockgroup;
998 char buffer[2048];
999
1000 while (data_size) {
1001 int duration = ass_get_duration(data);
1002 max_duration = FFMAX(duration, max_duration);
1003 end = memchr(data, '\n', data_size);
1004 size = line_size = end ? end-data+1 : data_size;
1005 size -= end ? (end[-1]=='\r')+1 : 0;
1006 start = data;
1007 for (i=0; i<3; i++, start++)
1008 if (!(start = memchr(start, ',', size-(start-data))))
1009 return max_duration;
1010 size -= start - data;
1011 sscanf(data, "Dialogue: %d,", &layer);
1012 i = snprintf(buffer, sizeof(buffer), "%"PRId64",%d,",
1013 s->streams[pkt->stream_index]->nb_frames, layer);
1014 size = FFMIN(i+size, sizeof(buffer));
1015 memcpy(buffer+i, start, size-i);
1016
1017 av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1018 "pts %" PRId64 ", duration %d\n",
1019 avio_tell(pb), size, pkt->pts, duration);
1020 blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(size));
1021 put_ebml_id(pb, MATROSKA_ID_BLOCK);
1022 put_ebml_num(pb, size+4, 0);
1023 avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1024 avio_wb16(pb, pkt->pts - mkv->cluster_pts);
1025 avio_w8(pb, 0);
1026 avio_write(pb, buffer, size);
1027 put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
1028 end_ebml_master(pb, blockgroup);
1029
1030 data += line_size;
1031 data_size -= line_size;
1032 }
1033
1034 return max_duration;
1035 }
1036
1037 static void mkv_write_block(AVFormatContext *s, AVIOContext *pb,
1038 unsigned int blockid, AVPacket *pkt, int flags)
1039 {
1040 MatroskaMuxContext *mkv = s->priv_data;
1041 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1042 uint8_t *data = NULL;
1043 int size = pkt->size;
1044 int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1045
1046 av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, "
1047 "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n",
1048 avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags);
1049 if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 &&
1050 (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1))
1051 ff_avc_parse_nal_units_buf(pkt->data, &data, &size);
1052 else
1053 data = pkt->data;
1054 put_ebml_id(pb, blockid);
1055 put_ebml_num(pb, size+4, 0);
1056 avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126
1057 avio_wb16(pb, ts - mkv->cluster_pts);
1058 avio_w8(pb, flags);
1059 avio_write(pb, data, size);
1060 if (data != pkt->data)
1061 av_free(data);
1062 }
1063
1064 static int srt_get_duration(uint8_t **buf)
1065 {
1066 int i, duration = 0;
1067
1068 for (i=0; i<2 && !duration; i++) {
1069 int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec;
1070 if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d",
1071 &s_hour, &s_min, &s_sec, &s_hsec,
1072 &e_hour, &e_min, &e_sec, &e_hsec) == 8) {
1073 s_min += 60*s_hour; e_min += 60*e_hour;
1074 s_sec += 60*s_min; e_sec += 60*e_min;
1075 s_hsec += 1000*s_sec; e_hsec += 1000*e_sec;
1076 duration = e_hsec - s_hsec;
1077 }
1078 *buf += strcspn(*buf, "\n") + 1;
1079 }
1080 return duration;
1081 }
1082
1083 static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt)
1084 {
1085 ebml_master blockgroup;
1086 AVPacket pkt2 = *pkt;
1087 int64_t duration = srt_get_duration(&pkt2.data);
1088 pkt2.size -= pkt2.data - pkt->data;
1089
1090 blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,
1091 mkv_blockgroup_size(pkt2.size));
1092 mkv_write_block(s, pb, MATROSKA_ID_BLOCK, &pkt2, 0);
1093 put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
1094 end_ebml_master(pb, blockgroup);
1095
1096 return duration;
1097 }
1098
1099 static void mkv_flush_dynbuf(AVFormatContext *s)
1100 {
1101 MatroskaMuxContext *mkv = s->priv_data;
1102 int bufsize;
1103 uint8_t *dyn_buf;
1104
1105 if (!mkv->dyn_bc)
1106 return;
1107
1108 bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf);
1109 avio_write(s->pb, dyn_buf, bufsize);
1110 av_free(dyn_buf);
1111 mkv->dyn_bc = NULL;
1112 }
1113
1114 static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
1115 {
1116 MatroskaMuxContext *mkv = s->priv_data;
1117 AVIOContext *pb = s->pb;
1118 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1119 int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1120 int duration = pkt->duration;
1121 int ret;
1122 int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1123
1124 if (ts == AV_NOPTS_VALUE) {
1125 av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");
1126 return AVERROR(EINVAL);
1127 }
1128
1129 if (!s->pb->seekable) {
1130 if (!mkv->dyn_bc)
1131 avio_open_dyn_buf(&mkv->dyn_bc);
1132 pb = mkv->dyn_bc;
1133 }
1134
1135 if (!mkv->cluster_pos) {
1136 mkv->cluster_pos = avio_tell(s->pb);
1137 mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER, 0);
1138 put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts));
1139 mkv->cluster_pts = FFMAX(0, ts);
1140 }
1141
1142 if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
1143 mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7);
1144 } else if (codec->codec_id == AV_CODEC_ID_SSA) {
1145 duration = mkv_write_ass_blocks(s, pb, pkt);
1146 } else if (codec->codec_id == AV_CODEC_ID_SRT) {
1147 duration = mkv_write_srt_blocks(s, pb, pkt);
1148 } else {
1149 ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(pkt->size));
1150 duration = pkt->convergence_duration;
1151 mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0);
1152 put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);
1153 end_ebml_master(pb, blockgroup);
1154 }
1155
1156 if (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe) {
1157 ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, ts, mkv->cluster_pos);
1158 if (ret < 0) return ret;
1159 }
1160
1161 mkv->duration = FFMAX(mkv->duration, ts + duration);
1162 return 0;
1163 }
1164
1165 static int mkv_copy_packet(MatroskaMuxContext *mkv, const AVPacket *pkt)
1166 {
1167 uint8_t *data = mkv->cur_audio_pkt.data;
1168 mkv->cur_audio_pkt = *pkt;
1169 mkv->cur_audio_pkt.data = av_fast_realloc(data, &mkv->audio_buffer_size, pkt->size);
1170 if (!mkv->cur_audio_pkt.data)
1171 return AVERROR(ENOMEM);
1172
1173 memcpy(mkv->cur_audio_pkt.data, pkt->data, pkt->size);
1174 mkv->cur_audio_pkt.size = pkt->size;
1175 return 0;
1176 }
1177
1178 static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
1179 {
1180 MatroskaMuxContext *mkv = s->priv_data;
1181 AVIOContext *pb = s->pb->seekable ? s->pb : mkv->dyn_bc;
1182 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
1183 int ret, keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY);
1184 int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;
1185 int cluster_size = avio_tell(pb) - (s->pb->seekable ? mkv->cluster_pos : 0);
1186
1187 // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or
1188 // after 4k and on a keyframe
1189 if (mkv->cluster_pos &&
1190 ((!s->pb->seekable && (cluster_size > 32*1024 || ts > mkv->cluster_pts + 1000))
1191 || cluster_size > 5*1024*1024 || ts > mkv->cluster_pts + 5000
1192 || (codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe && cluster_size > 4*1024))) {
1193 av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64
1194 " bytes, pts %" PRIu64 "\n", avio_tell(pb), ts);
1195 end_ebml_master(pb, mkv->cluster);
1196 mkv->cluster_pos = 0;
1197 if (mkv->dyn_bc)
1198 mkv_flush_dynbuf(s);
1199 }
1200
1201 // check if we have an audio packet cached
1202 if (mkv->cur_audio_pkt.size > 0) {
1203 ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1204 mkv->cur_audio_pkt.size = 0;
1205 if (ret < 0) {
1206 av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1207 return ret;
1208 }
1209 }
1210
1211 // buffer an audio packet to ensure the packet containing the video
1212 // keyframe's timecode is contained in the same cluster for WebM
1213 if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
1214 ret = mkv_copy_packet(mkv, pkt);
1215 else
1216 ret = mkv_write_packet_internal(s, pkt);
1217 return ret;
1218 }
1219
1220 static int mkv_write_trailer(AVFormatContext *s)
1221 {
1222 MatroskaMuxContext *mkv = s->priv_data;
1223 AVIOContext *pb = s->pb;
1224 int64_t currentpos, cuespos;
1225 int ret;
1226
1227 // check if we have an audio packet cached
1228 if (mkv->cur_audio_pkt.size > 0) {
1229 ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt);
1230 mkv->cur_audio_pkt.size = 0;
1231 if (ret < 0) {
1232 av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret);
1233 return ret;
1234 }
1235 }
1236
1237 if (mkv->dyn_bc) {
1238 end_ebml_master(mkv->dyn_bc, mkv->cluster);
1239 mkv_flush_dynbuf(s);
1240 } else if (mkv->cluster_pos) {
1241 end_ebml_master(pb, mkv->cluster);
1242 }
1243
1244 if (pb->seekable) {
1245 if (mkv->cues->num_entries) {
1246 cuespos = mkv_write_cues(pb, mkv->cues, s->nb_streams);
1247
1248 ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES, cuespos);
1249 if (ret < 0) return ret;
1250 }
1251
1252 mkv_write_seekhead(pb, mkv->main_seekhead);
1253
1254 // update the duration
1255 av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration);
1256 currentpos = avio_tell(pb);
1257 avio_seek(pb, mkv->duration_offset, SEEK_SET);
1258 put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration);
1259
1260 avio_seek(pb, currentpos, SEEK_SET);
1261 }
1262
1263 end_ebml_master(pb, mkv->segment);
1264 av_free(mkv->tracks);
1265 av_freep(&mkv->cues->entries);
1266 av_freep(&mkv->cues);
1267 av_destruct_packet(&mkv->cur_audio_pkt);
1268 avio_flush(pb);
1269 return 0;
1270 }
1271
1272 static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance)
1273 {
1274 int i;
1275 for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++)
1276 if (ff_mkv_codec_tags[i].id == codec_id)
1277 return 1;
1278
1279 if (std_compliance < FF_COMPLIANCE_NORMAL) { // mkv theoretically supports any
1280 enum AVMediaType type = avcodec_get_type(codec_id); // video/audio through VFW/ACM
1281 if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)
1282 return 1;
1283 }
1284
1285 return 0;
1286 }
1287
1288 #if CONFIG_MATROSKA_MUXER
1289 AVOutputFormat ff_matroska_muxer = {
1290 .name = "matroska",
1291 .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
1292 .mime_type = "video/x-matroska",
1293 .extensions = "mkv",
1294 .priv_data_size = sizeof(MatroskaMuxContext),
1295 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1296 AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
1297 .video_codec = CONFIG_LIBX264_ENCODER ?
1298 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
1299 .write_header = mkv_write_header,
1300 .write_packet = mkv_write_packet,
1301 .write_trailer = mkv_write_trailer,
1302 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
1303 AVFMT_TS_NONSTRICT,
1304 .codec_tag = (const AVCodecTag* const []){
1305 ff_codec_bmp_tags, ff_codec_wav_tags, 0
1306 },
1307 .subtitle_codec = AV_CODEC_ID_SSA,
1308 .query_codec = mkv_query_codec,
1309 };
1310 #endif
1311
1312 #if CONFIG_WEBM_MUXER
1313 AVOutputFormat ff_webm_muxer = {
1314 .name = "webm",
1315 .long_name = NULL_IF_CONFIG_SMALL("WebM"),
1316 .mime_type = "video/webm",
1317 .extensions = "webm",
1318 .priv_data_size = sizeof(MatroskaMuxContext),
1319 .audio_codec = AV_CODEC_ID_VORBIS,
1320 .video_codec = AV_CODEC_ID_VP8,
1321 .write_header = mkv_write_header,
1322 .write_packet = mkv_write_packet,
1323 .write_trailer = mkv_write_trailer,
1324 .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS |
1325 AVFMT_TS_NONSTRICT,
1326 };
1327 #endif
1328
1329 #if CONFIG_MATROSKA_AUDIO_MUXER
1330 AVOutputFormat ff_matroska_audio_muxer = {
1331 .name = "matroska",
1332 .long_name = NULL_IF_CONFIG_SMALL("Matroska"),
1333 .mime_type = "audio/x-matroska",
1334 .extensions = "mka",
1335 .priv_data_size = sizeof(MatroskaMuxContext),
1336 .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
1337 AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3,
1338 .video_codec = AV_CODEC_ID_NONE,
1339 .write_header = mkv_write_header,
1340 .write_packet = mkv_write_packet,
1341 .write_trailer = mkv_write_trailer,
1342 .flags = AVFMT_GLOBALHEADER | AVFMT_TS_NONSTRICT,
1343 .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
1344 };
1345 #endif