Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4349 | Serge | 1 | /* |
2 | * Matroska muxer |
||
3 | * Copyright (c) 2007 David Conrad |
||
4 | * |
||
5 | * This file is part of FFmpeg. |
||
6 | * |
||
7 | * FFmpeg 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 | * FFmpeg 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 FFmpeg; if not, write to the Free Software |
||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||
20 | */ |
||
21 | |||
22 | #include "avc.h" |
||
23 | #include "avformat.h" |
||
24 | #include "avio_internal.h" |
||
25 | #include "avlanguage.h" |
||
26 | #include "flacenc.h" |
||
27 | #include "internal.h" |
||
28 | #include "isom.h" |
||
29 | #include "matroska.h" |
||
30 | #include "riff.h" |
||
31 | #include "subtitles.h" |
||
32 | #include "wv.h" |
||
33 | |||
34 | #include "libavutil/avstring.h" |
||
35 | #include "libavutil/dict.h" |
||
36 | #include "libavutil/intfloat.h" |
||
37 | #include "libavutil/intreadwrite.h" |
||
38 | #include "libavutil/lfg.h" |
||
39 | #include "libavutil/mathematics.h" |
||
40 | #include "libavutil/opt.h" |
||
41 | #include "libavutil/random_seed.h" |
||
42 | #include "libavutil/samplefmt.h" |
||
43 | #include "libavutil/sha.h" |
||
44 | |||
45 | #include "libavcodec/xiph.h" |
||
46 | #include "libavcodec/mpeg4audio.h" |
||
47 | #include "libavcodec/internal.h" |
||
48 | |||
49 | typedef struct ebml_master { |
||
50 | int64_t pos; ///< absolute offset in the file where the master's elements start |
||
51 | int sizebytes; ///< how many bytes were reserved for the size |
||
52 | } ebml_master; |
||
53 | |||
54 | typedef struct mkv_seekhead_entry { |
||
55 | unsigned int elementid; |
||
56 | uint64_t segmentpos; |
||
57 | } mkv_seekhead_entry; |
||
58 | |||
59 | typedef struct mkv_seekhead { |
||
60 | int64_t filepos; |
||
61 | int64_t segment_offset; ///< the file offset to the beginning of the segment |
||
62 | int reserved_size; ///< -1 if appending to file |
||
63 | int max_entries; |
||
64 | mkv_seekhead_entry *entries; |
||
65 | int num_entries; |
||
66 | } mkv_seekhead; |
||
67 | |||
68 | typedef struct { |
||
69 | uint64_t pts; |
||
70 | int tracknum; |
||
71 | int64_t cluster_pos; ///< file offset of the cluster containing the block |
||
72 | int64_t relative_pos; ///< relative offset from the position of the cluster containing the block |
||
73 | int64_t duration; ///< duration of the block according to time base |
||
74 | } mkv_cuepoint; |
||
75 | |||
76 | typedef struct { |
||
77 | int64_t segment_offset; |
||
78 | mkv_cuepoint *entries; |
||
79 | int num_entries; |
||
80 | } mkv_cues; |
||
81 | |||
82 | typedef struct { |
||
83 | int write_dts; |
||
84 | int has_cue; |
||
85 | } mkv_track; |
||
86 | |||
87 | #define MODE_MATROSKAv2 0x01 |
||
88 | #define MODE_WEBM 0x02 |
||
89 | |||
90 | typedef struct MatroskaMuxContext { |
||
91 | const AVClass *class; |
||
92 | int mode; |
||
93 | AVIOContext *dyn_bc; |
||
94 | ebml_master segment; |
||
95 | int64_t segment_offset; |
||
96 | ebml_master cluster; |
||
97 | int64_t cluster_pos; ///< file offset of the current cluster |
||
98 | int64_t cluster_pts; |
||
99 | int64_t duration_offset; |
||
100 | int64_t duration; |
||
101 | mkv_seekhead *main_seekhead; |
||
102 | mkv_cues *cues; |
||
103 | mkv_track *tracks; |
||
104 | |||
105 | AVPacket cur_audio_pkt; |
||
106 | |||
107 | int have_attachments; |
||
108 | |||
109 | int reserve_cues_space; |
||
110 | int cluster_size_limit; |
||
111 | int64_t cues_pos; |
||
112 | int64_t cluster_time_limit; |
||
113 | |||
114 | uint32_t chapter_id_offset; |
||
115 | int wrote_chapters; |
||
116 | } MatroskaMuxContext; |
||
117 | |||
118 | |||
119 | /** 2 bytes * 3 for EBML IDs, 3 1-byte EBML lengths, 8 bytes for 64 bit |
||
120 | * offset, 4 bytes for target EBML ID */ |
||
121 | #define MAX_SEEKENTRY_SIZE 21 |
||
122 | |||
123 | /** per-cuepoint-track - 5 1-byte EBML IDs, 5 1-byte EBML sizes, 4 |
||
124 | * 8-byte uint max */ |
||
125 | #define MAX_CUETRACKPOS_SIZE 42 |
||
126 | |||
127 | /** per-cuepoint - 2 1-byte EBML IDs, 2 1-byte EBML sizes, 8-byte uint max */ |
||
128 | #define MAX_CUEPOINT_SIZE(num_tracks) 12 + MAX_CUETRACKPOS_SIZE*num_tracks |
||
129 | |||
130 | /** Seek preroll value for opus */ |
||
131 | #define OPUS_SEEK_PREROLL 80000000 |
||
132 | |||
133 | |||
134 | static int ebml_id_size(unsigned int id) |
||
135 | { |
||
136 | return (av_log2(id+1)-1)/7+1; |
||
137 | } |
||
138 | |||
139 | static void put_ebml_id(AVIOContext *pb, unsigned int id) |
||
140 | { |
||
141 | int i = ebml_id_size(id); |
||
142 | while (i--) |
||
143 | avio_w8(pb, (uint8_t)(id >> (i*8))); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Write an EBML size meaning "unknown size". |
||
148 | * |
||
149 | * @param bytes The number of bytes the size should occupy (maximum: 8). |
||
150 | */ |
||
151 | static void put_ebml_size_unknown(AVIOContext *pb, int bytes) |
||
152 | { |
||
153 | av_assert0(bytes <= 8); |
||
154 | avio_w8(pb, 0x1ff >> bytes); |
||
155 | ffio_fill(pb, 0xff, bytes - 1); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Calculate how many bytes are needed to represent a given number in EBML. |
||
160 | */ |
||
161 | static int ebml_num_size(uint64_t num) |
||
162 | { |
||
163 | int bytes = 1; |
||
164 | while ((num+1) >> bytes*7) bytes++; |
||
165 | return bytes; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Write a number in EBML variable length format. |
||
170 | * |
||
171 | * @param bytes The number of bytes that need to be used to write the number. |
||
172 | * If zero, any number of bytes can be used. |
||
173 | */ |
||
174 | static void put_ebml_num(AVIOContext *pb, uint64_t num, int bytes) |
||
175 | { |
||
176 | int i, needed_bytes = ebml_num_size(num); |
||
177 | |||
178 | // sizes larger than this are currently undefined in EBML |
||
179 | av_assert0(num < (1ULL<<56)-1); |
||
180 | |||
181 | if (bytes == 0) |
||
182 | // don't care how many bytes are used, so use the min |
||
183 | bytes = needed_bytes; |
||
184 | // the bytes needed to write the given size would exceed the bytes |
||
185 | // that we need to use, so write unknown size. This shouldn't happen. |
||
186 | av_assert0(bytes >= needed_bytes); |
||
187 | |||
188 | num |= 1ULL << bytes*7; |
||
189 | for (i = bytes - 1; i >= 0; i--) |
||
190 | avio_w8(pb, (uint8_t)(num >> i*8)); |
||
191 | } |
||
192 | |||
193 | static void put_ebml_uint(AVIOContext *pb, unsigned int elementid, uint64_t val) |
||
194 | { |
||
195 | int i, bytes = 1; |
||
196 | uint64_t tmp = val; |
||
197 | while (tmp>>=8) bytes++; |
||
198 | |||
199 | put_ebml_id(pb, elementid); |
||
200 | put_ebml_num(pb, bytes, 0); |
||
201 | for (i = bytes - 1; i >= 0; i--) |
||
202 | avio_w8(pb, (uint8_t)(val >> i*8)); |
||
203 | } |
||
204 | |||
205 | static void put_ebml_float(AVIOContext *pb, unsigned int elementid, double val) |
||
206 | { |
||
207 | put_ebml_id(pb, elementid); |
||
208 | put_ebml_num(pb, 8, 0); |
||
209 | avio_wb64(pb, av_double2int(val)); |
||
210 | } |
||
211 | |||
212 | static void put_ebml_binary(AVIOContext *pb, unsigned int elementid, |
||
213 | const void *buf, int size) |
||
214 | { |
||
215 | put_ebml_id(pb, elementid); |
||
216 | put_ebml_num(pb, size, 0); |
||
217 | avio_write(pb, buf, size); |
||
218 | } |
||
219 | |||
220 | static void put_ebml_string(AVIOContext *pb, unsigned int elementid, const char *str) |
||
221 | { |
||
222 | put_ebml_binary(pb, elementid, str, strlen(str)); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Write a void element of a given size. Useful for reserving space in |
||
227 | * the file to be written to later. |
||
228 | * |
||
229 | * @param size The number of bytes to reserve, which must be at least 2. |
||
230 | */ |
||
231 | static void put_ebml_void(AVIOContext *pb, uint64_t size) |
||
232 | { |
||
233 | int64_t currentpos = avio_tell(pb); |
||
234 | |||
235 | av_assert0(size >= 2); |
||
236 | |||
237 | put_ebml_id(pb, EBML_ID_VOID); |
||
238 | // we need to subtract the length needed to store the size from the |
||
239 | // size we need to reserve so 2 cases, we use 8 bytes to store the |
||
240 | // size if possible, 1 byte otherwise |
||
241 | if (size < 10) |
||
242 | put_ebml_num(pb, size-1, 0); |
||
243 | else |
||
244 | put_ebml_num(pb, size-9, 8); |
||
245 | ffio_fill(pb, 0, currentpos + size - avio_tell(pb)); |
||
246 | } |
||
247 | |||
248 | static ebml_master start_ebml_master(AVIOContext *pb, unsigned int elementid, uint64_t expectedsize) |
||
249 | { |
||
250 | int bytes = expectedsize ? ebml_num_size(expectedsize) : 8; |
||
251 | put_ebml_id(pb, elementid); |
||
252 | put_ebml_size_unknown(pb, bytes); |
||
253 | return (ebml_master){ avio_tell(pb), bytes }; |
||
254 | } |
||
255 | |||
256 | static void end_ebml_master(AVIOContext *pb, ebml_master master) |
||
257 | { |
||
258 | int64_t pos = avio_tell(pb); |
||
259 | |||
260 | if (avio_seek(pb, master.pos - master.sizebytes, SEEK_SET) < 0) |
||
261 | return; |
||
262 | put_ebml_num(pb, pos - master.pos, master.sizebytes); |
||
263 | avio_seek(pb, pos, SEEK_SET); |
||
264 | } |
||
265 | |||
266 | static void put_xiph_size(AVIOContext *pb, int size) |
||
267 | { |
||
268 | ffio_fill(pb, 255, size / 255); |
||
269 | avio_w8(pb, size % 255); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Initialize a mkv_seekhead element to be ready to index level 1 Matroska |
||
274 | * elements. If a maximum number of elements is specified, enough space |
||
275 | * will be reserved at the current file location to write a seek head of |
||
276 | * that size. |
||
277 | * |
||
278 | * @param segment_offset The absolute offset to the position in the file |
||
279 | * where the segment begins. |
||
280 | * @param numelements The maximum number of elements that will be indexed |
||
281 | * by this seek head, 0 if unlimited. |
||
282 | */ |
||
283 | static mkv_seekhead * mkv_start_seekhead(AVIOContext *pb, int64_t segment_offset, int numelements) |
||
284 | { |
||
285 | mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead)); |
||
286 | if (new_seekhead == NULL) |
||
287 | return NULL; |
||
288 | |||
289 | new_seekhead->segment_offset = segment_offset; |
||
290 | |||
291 | if (numelements > 0) { |
||
292 | new_seekhead->filepos = avio_tell(pb); |
||
293 | // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID |
||
294 | // and size, and 3 bytes to guarantee that an EBML void element |
||
295 | // will fit afterwards |
||
296 | new_seekhead->reserved_size = numelements * MAX_SEEKENTRY_SIZE + 13; |
||
297 | new_seekhead->max_entries = numelements; |
||
298 | put_ebml_void(pb, new_seekhead->reserved_size); |
||
299 | } |
||
300 | return new_seekhead; |
||
301 | } |
||
302 | |||
303 | static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos) |
||
304 | { |
||
305 | mkv_seekhead_entry *entries = seekhead->entries; |
||
306 | |||
307 | // don't store more elements than we reserved space for |
||
308 | if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries) |
||
309 | return -1; |
||
310 | |||
311 | entries = av_realloc_array(entries, seekhead->num_entries + 1, sizeof(mkv_seekhead_entry)); |
||
312 | if (entries == NULL) |
||
313 | return AVERROR(ENOMEM); |
||
314 | seekhead->entries = entries; |
||
315 | |||
316 | seekhead->entries[seekhead->num_entries].elementid = elementid; |
||
317 | seekhead->entries[seekhead->num_entries++].segmentpos = filepos - seekhead->segment_offset; |
||
318 | |||
319 | return 0; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Write the seek head to the file and free it. If a maximum number of |
||
324 | * elements was specified to mkv_start_seekhead(), the seek head will |
||
325 | * be written at the location reserved for it. Otherwise, it is written |
||
326 | * at the current location in the file. |
||
327 | * |
||
328 | * @return The file offset where the seekhead was written, |
||
329 | * -1 if an error occurred. |
||
330 | */ |
||
331 | static int64_t mkv_write_seekhead(AVIOContext *pb, mkv_seekhead *seekhead) |
||
332 | { |
||
333 | ebml_master metaseek, seekentry; |
||
334 | int64_t currentpos; |
||
335 | int i; |
||
336 | |||
337 | currentpos = avio_tell(pb); |
||
338 | |||
339 | if (seekhead->reserved_size > 0) { |
||
340 | if (avio_seek(pb, seekhead->filepos, SEEK_SET) < 0) { |
||
341 | currentpos = -1; |
||
342 | goto fail; |
||
343 | } |
||
344 | } |
||
345 | |||
346 | metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD, seekhead->reserved_size); |
||
347 | for (i = 0; i < seekhead->num_entries; i++) { |
||
348 | mkv_seekhead_entry *entry = &seekhead->entries[i]; |
||
349 | |||
350 | seekentry = start_ebml_master(pb, MATROSKA_ID_SEEKENTRY, MAX_SEEKENTRY_SIZE); |
||
351 | |||
352 | put_ebml_id(pb, MATROSKA_ID_SEEKID); |
||
353 | put_ebml_num(pb, ebml_id_size(entry->elementid), 0); |
||
354 | put_ebml_id(pb, entry->elementid); |
||
355 | |||
356 | put_ebml_uint(pb, MATROSKA_ID_SEEKPOSITION, entry->segmentpos); |
||
357 | end_ebml_master(pb, seekentry); |
||
358 | } |
||
359 | end_ebml_master(pb, metaseek); |
||
360 | |||
361 | if (seekhead->reserved_size > 0) { |
||
362 | uint64_t remaining = seekhead->filepos + seekhead->reserved_size - avio_tell(pb); |
||
363 | put_ebml_void(pb, remaining); |
||
364 | avio_seek(pb, currentpos, SEEK_SET); |
||
365 | |||
366 | currentpos = seekhead->filepos; |
||
367 | } |
||
368 | fail: |
||
369 | av_freep(&seekhead->entries); |
||
370 | av_free(seekhead); |
||
371 | |||
372 | return currentpos; |
||
373 | } |
||
374 | |||
375 | static mkv_cues * mkv_start_cues(int64_t segment_offset) |
||
376 | { |
||
377 | mkv_cues *cues = av_mallocz(sizeof(mkv_cues)); |
||
378 | if (cues == NULL) |
||
379 | return NULL; |
||
380 | |||
381 | cues->segment_offset = segment_offset; |
||
382 | return cues; |
||
383 | } |
||
384 | |||
385 | static int mkv_add_cuepoint(mkv_cues *cues, int stream, int64_t ts, int64_t cluster_pos, int64_t relative_pos, |
||
386 | int64_t duration) |
||
387 | { |
||
388 | mkv_cuepoint *entries = cues->entries; |
||
389 | |||
390 | if (ts < 0) |
||
391 | return 0; |
||
392 | |||
393 | entries = av_realloc_array(entries, cues->num_entries + 1, sizeof(mkv_cuepoint)); |
||
394 | if (entries == NULL) |
||
395 | return AVERROR(ENOMEM); |
||
396 | cues->entries = entries; |
||
397 | |||
398 | cues->entries[cues->num_entries].pts = ts; |
||
399 | cues->entries[cues->num_entries].tracknum = stream + 1; |
||
400 | cues->entries[cues->num_entries].cluster_pos = cluster_pos - cues->segment_offset; |
||
401 | cues->entries[cues->num_entries].relative_pos = relative_pos; |
||
402 | cues->entries[cues->num_entries++].duration = duration; |
||
403 | |||
404 | return 0; |
||
405 | } |
||
406 | |||
407 | static int64_t mkv_write_cues(AVIOContext *pb, mkv_cues *cues, mkv_track *tracks, int num_tracks) |
||
408 | { |
||
409 | ebml_master cues_element; |
||
410 | int64_t currentpos; |
||
411 | int i, j; |
||
412 | |||
413 | currentpos = avio_tell(pb); |
||
414 | cues_element = start_ebml_master(pb, MATROSKA_ID_CUES, 0); |
||
415 | |||
416 | for (i = 0; i < cues->num_entries; i++) { |
||
417 | ebml_master cuepoint, track_positions; |
||
418 | mkv_cuepoint *entry = &cues->entries[i]; |
||
419 | uint64_t pts = entry->pts; |
||
420 | |||
421 | cuepoint = start_ebml_master(pb, MATROSKA_ID_POINTENTRY, MAX_CUEPOINT_SIZE(num_tracks)); |
||
422 | put_ebml_uint(pb, MATROSKA_ID_CUETIME, pts); |
||
423 | |||
424 | // put all the entries from different tracks that have the exact same |
||
425 | // timestamp into the same CuePoint |
||
426 | for (j = 0; j < num_tracks; j++) |
||
427 | tracks[j].has_cue = 0; |
||
428 | for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) { |
||
429 | int tracknum = entry[j].tracknum - 1; |
||
430 | av_assert0(tracknum>=0 && tracknum |
||
431 | if (tracks[tracknum].has_cue) |
||
432 | continue; |
||
433 | tracks[tracknum].has_cue = 1; |
||
434 | track_positions = start_ebml_master(pb, MATROSKA_ID_CUETRACKPOSITION, MAX_CUETRACKPOS_SIZE); |
||
435 | put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum ); |
||
436 | put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION , entry[j].cluster_pos); |
||
437 | put_ebml_uint(pb, MATROSKA_ID_CUERELATIVEPOSITION, entry[j].relative_pos); |
||
438 | if (entry[j].duration != -1) |
||
439 | put_ebml_uint(pb, MATROSKA_ID_CUEDURATION , entry[j].duration); |
||
440 | end_ebml_master(pb, track_positions); |
||
441 | } |
||
442 | i += j - 1; |
||
443 | end_ebml_master(pb, cuepoint); |
||
444 | } |
||
445 | end_ebml_master(pb, cues_element); |
||
446 | |||
447 | return currentpos; |
||
448 | } |
||
449 | |||
450 | static int put_xiph_codecpriv(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec) |
||
451 | { |
||
452 | uint8_t *header_start[3]; |
||
453 | int header_len[3]; |
||
454 | int first_header_size; |
||
455 | int j; |
||
456 | |||
457 | if (codec->codec_id == AV_CODEC_ID_VORBIS) |
||
458 | first_header_size = 30; |
||
459 | else |
||
460 | first_header_size = 42; |
||
461 | |||
462 | if (avpriv_split_xiph_headers(codec->extradata, codec->extradata_size, |
||
463 | first_header_size, header_start, header_len) < 0) { |
||
464 | av_log(s, AV_LOG_ERROR, "Extradata corrupt.\n"); |
||
465 | return -1; |
||
466 | } |
||
467 | |||
468 | avio_w8(pb, 2); // number packets - 1 |
||
469 | for (j = 0; j < 2; j++) { |
||
470 | put_xiph_size(pb, header_len[j]); |
||
471 | } |
||
472 | for (j = 0; j < 3; j++) |
||
473 | avio_write(pb, header_start[j], header_len[j]); |
||
474 | |||
475 | return 0; |
||
476 | } |
||
477 | |||
478 | static int put_wv_codecpriv(AVIOContext *pb, AVCodecContext *codec) |
||
479 | { |
||
480 | if (codec->extradata && codec->extradata_size == 2) |
||
481 | avio_write(pb, codec->extradata, 2); |
||
482 | else |
||
483 | avio_wl16(pb, 0x403); // fallback to the version mentioned in matroska specs |
||
484 | return 0; |
||
485 | } |
||
486 | |||
487 | static void get_aac_sample_rates(AVFormatContext *s, AVCodecContext *codec, int *sample_rate, int *output_sample_rate) |
||
488 | { |
||
489 | MPEG4AudioConfig mp4ac; |
||
490 | |||
491 | if (avpriv_mpeg4audio_get_config(&mp4ac, codec->extradata, |
||
492 | codec->extradata_size * 8, 1) < 0) { |
||
493 | av_log(s, AV_LOG_WARNING, "Error parsing AAC extradata, unable to determine samplerate.\n"); |
||
494 | return; |
||
495 | } |
||
496 | |||
497 | *sample_rate = mp4ac.sample_rate; |
||
498 | *output_sample_rate = mp4ac.ext_sample_rate; |
||
499 | } |
||
500 | |||
501 | static int mkv_write_codecprivate(AVFormatContext *s, AVIOContext *pb, AVCodecContext *codec, int native_id, int qt_id) |
||
502 | { |
||
503 | AVIOContext *dyn_cp; |
||
504 | uint8_t *codecpriv; |
||
505 | int ret, codecpriv_size; |
||
506 | |||
507 | ret = avio_open_dyn_buf(&dyn_cp); |
||
508 | if(ret < 0) |
||
509 | return ret; |
||
510 | |||
511 | if (native_id) { |
||
512 | if (codec->codec_id == AV_CODEC_ID_VORBIS || codec->codec_id == AV_CODEC_ID_THEORA) |
||
513 | ret = put_xiph_codecpriv(s, dyn_cp, codec); |
||
514 | else if (codec->codec_id == AV_CODEC_ID_FLAC) |
||
515 | ret = ff_flac_write_header(dyn_cp, codec, 1); |
||
516 | else if (codec->codec_id == AV_CODEC_ID_WAVPACK) |
||
517 | ret = put_wv_codecpriv(dyn_cp, codec); |
||
518 | else if (codec->codec_id == AV_CODEC_ID_H264) |
||
519 | ret = ff_isom_write_avcc(dyn_cp, codec->extradata, codec->extradata_size); |
||
520 | else if (codec->codec_id == AV_CODEC_ID_ALAC) { |
||
521 | if (codec->extradata_size < 36) { |
||
522 | av_log(s, AV_LOG_ERROR, |
||
523 | "Invalid extradata found, ALAC expects a 36-byte " |
||
524 | "QuickTime atom."); |
||
525 | ret = AVERROR_INVALIDDATA; |
||
526 | } else |
||
527 | avio_write(dyn_cp, codec->extradata + 12, |
||
528 | codec->extradata_size - 12); |
||
529 | } |
||
530 | else if (codec->extradata_size && codec->codec_id != AV_CODEC_ID_TTA) |
||
531 | avio_write(dyn_cp, codec->extradata, codec->extradata_size); |
||
532 | } else if (codec->codec_type == AVMEDIA_TYPE_VIDEO) { |
||
533 | if (qt_id) { |
||
534 | if (!codec->codec_tag) |
||
535 | codec->codec_tag = ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id); |
||
536 | if (codec->extradata_size) |
||
537 | avio_write(dyn_cp, codec->extradata, codec->extradata_size); |
||
538 | } else { |
||
539 | if (!codec->codec_tag) |
||
540 | codec->codec_tag = ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id); |
||
541 | if (!codec->codec_tag) { |
||
542 | av_log(s, AV_LOG_ERROR, "No bmp codec tag found for codec %s\n", |
||
543 | avcodec_get_name(codec->codec_id)); |
||
544 | ret = AVERROR(EINVAL); |
||
545 | } |
||
546 | |||
547 | ff_put_bmp_header(dyn_cp, codec, ff_codec_bmp_tags, 0); |
||
548 | } |
||
549 | |||
550 | } else if (codec->codec_type == AVMEDIA_TYPE_AUDIO) { |
||
551 | unsigned int tag; |
||
552 | tag = ff_codec_get_tag(ff_codec_wav_tags, codec->codec_id); |
||
553 | if (!tag) { |
||
554 | av_log(s, AV_LOG_ERROR, "No wav codec tag found for codec %s\n", |
||
555 | avcodec_get_name(codec->codec_id)); |
||
556 | ret = AVERROR(EINVAL); |
||
557 | } |
||
558 | if (!codec->codec_tag) |
||
559 | codec->codec_tag = tag; |
||
560 | |||
561 | ff_put_wav_header(dyn_cp, codec); |
||
562 | } |
||
563 | |||
564 | codecpriv_size = avio_close_dyn_buf(dyn_cp, &codecpriv); |
||
565 | if (codecpriv_size) |
||
566 | put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codecpriv, codecpriv_size); |
||
567 | av_free(codecpriv); |
||
568 | return ret; |
||
569 | } |
||
570 | |||
571 | static int mkv_write_tracks(AVFormatContext *s) |
||
572 | { |
||
573 | MatroskaMuxContext *mkv = s->priv_data; |
||
574 | AVIOContext *pb = s->pb; |
||
575 | ebml_master tracks; |
||
576 | int i, j, ret, default_stream_exists = 0; |
||
577 | |||
578 | ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TRACKS, avio_tell(pb)); |
||
579 | if (ret < 0) return ret; |
||
580 | |||
581 | tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS, 0); |
||
582 | for (i = 0; i < s->nb_streams; i++) { |
||
583 | AVStream *st = s->streams[i]; |
||
584 | default_stream_exists |= st->disposition & AV_DISPOSITION_DEFAULT; |
||
585 | } |
||
586 | for (i = 0; i < s->nb_streams; i++) { |
||
587 | AVStream *st = s->streams[i]; |
||
588 | AVCodecContext *codec = st->codec; |
||
589 | ebml_master subinfo, track; |
||
590 | int native_id = 0; |
||
591 | int qt_id = 0; |
||
592 | int bit_depth = av_get_bits_per_sample(codec->codec_id); |
||
593 | int sample_rate = codec->sample_rate; |
||
594 | int output_sample_rate = 0; |
||
595 | AVDictionaryEntry *tag; |
||
596 | |||
597 | if (codec->codec_type == AVMEDIA_TYPE_ATTACHMENT) { |
||
598 | mkv->have_attachments = 1; |
||
599 | continue; |
||
600 | } |
||
601 | |||
602 | if (!bit_depth) |
||
603 | bit_depth = av_get_bytes_per_sample(codec->sample_fmt) << 3; |
||
604 | if (!bit_depth) |
||
605 | bit_depth = codec->bits_per_coded_sample; |
||
606 | |||
607 | if (codec->codec_id == AV_CODEC_ID_AAC) |
||
608 | get_aac_sample_rates(s, codec, &sample_rate, &output_sample_rate); |
||
609 | |||
610 | track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY, 0); |
||
611 | put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER , i + 1); |
||
612 | put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1); |
||
613 | put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet) |
||
614 | |||
615 | if ((tag = av_dict_get(st->metadata, "title", NULL, 0))) |
||
616 | put_ebml_string(pb, MATROSKA_ID_TRACKNAME, tag->value); |
||
617 | tag = av_dict_get(st->metadata, "language", NULL, 0); |
||
618 | if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) { |
||
619 | put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag ? tag->value:"und"); |
||
620 | } else if (tag && tag->value) { |
||
621 | put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, tag->value); |
||
622 | } |
||
623 | |||
624 | // The default value for TRACKFLAGDEFAULT is 1, so add element |
||
625 | // if we need to clear it. |
||
626 | if (default_stream_exists && !(st->disposition & AV_DISPOSITION_DEFAULT)) |
||
627 | put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGDEFAULT, !!(st->disposition & AV_DISPOSITION_DEFAULT)); |
||
628 | |||
629 | if (st->disposition & AV_DISPOSITION_FORCED) |
||
630 | put_ebml_uint(pb, MATROSKA_ID_TRACKFLAGFORCED, 1); |
||
631 | |||
632 | if (mkv->mode == MODE_WEBM && codec->codec_id == AV_CODEC_ID_WEBVTT) { |
||
633 | const char *codec_id; |
||
634 | if (st->disposition & AV_DISPOSITION_CAPTIONS) { |
||
635 | codec_id = "D_WEBVTT/CAPTIONS"; |
||
636 | native_id = MATROSKA_TRACK_TYPE_SUBTITLE; |
||
637 | } else if (st->disposition & AV_DISPOSITION_DESCRIPTIONS) { |
||
638 | codec_id = "D_WEBVTT/DESCRIPTIONS"; |
||
639 | native_id = MATROSKA_TRACK_TYPE_METADATA; |
||
640 | } else if (st->disposition & AV_DISPOSITION_METADATA) { |
||
641 | codec_id = "D_WEBVTT/METADATA"; |
||
642 | native_id = MATROSKA_TRACK_TYPE_METADATA; |
||
643 | } else { |
||
644 | codec_id = "D_WEBVTT/SUBTITLES"; |
||
645 | native_id = MATROSKA_TRACK_TYPE_SUBTITLE; |
||
646 | } |
||
647 | put_ebml_string(pb, MATROSKA_ID_CODECID, codec_id); |
||
648 | } else { |
||
649 | // look for a codec ID string specific to mkv to use, |
||
650 | // if none are found, use AVI codes |
||
651 | for (j = 0; ff_mkv_codec_tags[j].id != AV_CODEC_ID_NONE; j++) { |
||
652 | if (ff_mkv_codec_tags[j].id == codec->codec_id) { |
||
653 | put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str); |
||
654 | native_id = 1; |
||
655 | break; |
||
656 | } |
||
657 | } |
||
658 | } |
||
659 | |||
660 | if (codec->codec_id == AV_CODEC_ID_OPUS) { |
||
661 | uint64_t codec_delay =av_rescale_q(codec->delay, |
||
662 | (AVRational){1, codec->sample_rate}, |
||
663 | (AVRational){1, 1000000000}); |
||
664 | put_ebml_uint(pb, MATROSKA_ID_CODECDELAY, codec_delay); |
||
665 | put_ebml_uint(pb, MATROSKA_ID_SEEKPREROLL, OPUS_SEEK_PREROLL); |
||
666 | |||
667 | } |
||
668 | |||
669 | if (mkv->mode == MODE_WEBM && !(codec->codec_id == AV_CODEC_ID_VP8 || |
||
670 | codec->codec_id == AV_CODEC_ID_VP9 || |
||
671 | ((codec->codec_id == AV_CODEC_ID_OPUS)&&(codec->strict_std_compliance <= FF_COMPLIANCE_EXPERIMENTAL)) || |
||
672 | codec->codec_id == AV_CODEC_ID_VORBIS || |
||
673 | codec->codec_id == AV_CODEC_ID_WEBVTT)) { |
||
674 | av_log(s, AV_LOG_ERROR, |
||
675 | "Only VP8,VP9 video and Vorbis,Opus(experimental, use -strict -2) audio and WebVTT subtitles are supported for WebM.\n"); |
||
676 | return AVERROR(EINVAL); |
||
677 | } |
||
678 | |||
679 | switch (codec->codec_type) { |
||
680 | case AVMEDIA_TYPE_VIDEO: |
||
681 | put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO); |
||
682 | if(st->avg_frame_rate.num && st->avg_frame_rate.den && 1.0/av_q2d(st->avg_frame_rate) > av_q2d(codec->time_base)) |
||
683 | put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, 1E9/av_q2d(st->avg_frame_rate)); |
||
684 | else |
||
685 | put_ebml_uint(pb, MATROSKA_ID_TRACKDEFAULTDURATION, av_q2d(codec->time_base)*1E9); |
||
686 | |||
687 | if (!native_id && |
||
688 | ff_codec_get_tag(ff_codec_movvideo_tags, codec->codec_id) && |
||
689 | (!ff_codec_get_tag(ff_codec_bmp_tags, codec->codec_id) |
||
690 | || codec->codec_id == AV_CODEC_ID_SVQ1 |
||
691 | || codec->codec_id == AV_CODEC_ID_SVQ3 |
||
692 | || codec->codec_id == AV_CODEC_ID_CINEPAK)) |
||
693 | qt_id = 1; |
||
694 | |||
695 | if (qt_id) |
||
696 | put_ebml_string(pb, MATROSKA_ID_CODECID, "V_QUICKTIME"); |
||
697 | else if (!native_id) { |
||
698 | // if there is no mkv-specific codec ID, use VFW mode |
||
699 | put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC"); |
||
700 | mkv->tracks[i].write_dts = 1; |
||
701 | } |
||
702 | |||
703 | subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0); |
||
704 | // XXX: interlace flag? |
||
705 | put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width); |
||
706 | put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height); |
||
707 | |||
708 | if ((tag = av_dict_get(st->metadata, "stereo_mode", NULL, 0)) || |
||
709 | (tag = av_dict_get( s->metadata, "stereo_mode", NULL, 0))) { |
||
710 | // save stereo mode flag |
||
711 | uint64_t st_mode = MATROSKA_VIDEO_STEREO_MODE_COUNT; |
||
712 | |||
713 | for (j=0; j |
||
714 | if (!strcmp(tag->value, ff_matroska_video_stereo_mode[j])){ |
||
715 | st_mode = j; |
||
716 | break; |
||
717 | } |
||
718 | |||
719 | if ((mkv->mode == MODE_WEBM && st_mode > 3 && st_mode != 11) |
||
720 | || st_mode >= MATROSKA_VIDEO_STEREO_MODE_COUNT) { |
||
721 | av_log(s, AV_LOG_ERROR, |
||
722 | "The specified stereo mode is not valid.\n"); |
||
723 | return AVERROR(EINVAL); |
||
724 | } else |
||
725 | put_ebml_uint(pb, MATROSKA_ID_VIDEOSTEREOMODE, st_mode); |
||
726 | } |
||
727 | |||
728 | if ((tag = av_dict_get(st->metadata, "alpha_mode", NULL, 0)) || |
||
729 | (tag = av_dict_get( s->metadata, "alpha_mode", NULL, 0)) || |
||
730 | (codec->pix_fmt == AV_PIX_FMT_YUVA420P)) { |
||
731 | put_ebml_uint(pb, MATROSKA_ID_VIDEOALPHAMODE, 1); |
||
732 | } |
||
733 | |||
734 | if (st->sample_aspect_ratio.num) { |
||
735 | int64_t d_width = av_rescale(codec->width, st->sample_aspect_ratio.num, st->sample_aspect_ratio.den); |
||
736 | if (d_width > INT_MAX) { |
||
737 | av_log(s, AV_LOG_ERROR, "Overflow in display width\n"); |
||
738 | return AVERROR(EINVAL); |
||
739 | } |
||
740 | put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYWIDTH , d_width); |
||
741 | put_ebml_uint(pb, MATROSKA_ID_VIDEODISPLAYHEIGHT, codec->height); |
||
742 | } |
||
743 | |||
744 | if (codec->codec_id == AV_CODEC_ID_RAWVIDEO) { |
||
745 | uint32_t color_space = av_le2ne32(codec->codec_tag); |
||
746 | put_ebml_binary(pb, MATROSKA_ID_VIDEOCOLORSPACE, &color_space, sizeof(color_space)); |
||
747 | } |
||
748 | end_ebml_master(pb, subinfo); |
||
749 | break; |
||
750 | |||
751 | case AVMEDIA_TYPE_AUDIO: |
||
752 | put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO); |
||
753 | |||
754 | if (!native_id) |
||
755 | // no mkv-specific ID, use ACM mode |
||
756 | put_ebml_string(pb, MATROSKA_ID_CODECID, "A_MS/ACM"); |
||
757 | |||
758 | subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO, 0); |
||
759 | put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels); |
||
760 | put_ebml_float (pb, MATROSKA_ID_AUDIOSAMPLINGFREQ, sample_rate); |
||
761 | if (output_sample_rate) |
||
762 | put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate); |
||
763 | if (bit_depth) |
||
764 | put_ebml_uint(pb, MATROSKA_ID_AUDIOBITDEPTH, bit_depth); |
||
765 | end_ebml_master(pb, subinfo); |
||
766 | break; |
||
767 | |||
768 | case AVMEDIA_TYPE_SUBTITLE: |
||
769 | if (!native_id) { |
||
770 | av_log(s, AV_LOG_ERROR, "Subtitle codec %d is not supported.\n", codec->codec_id); |
||
771 | return AVERROR(ENOSYS); |
||
772 | } |
||
773 | |||
774 | if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) |
||
775 | native_id = MATROSKA_TRACK_TYPE_SUBTITLE; |
||
776 | |||
777 | put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, native_id); |
||
778 | break; |
||
779 | default: |
||
780 | av_log(s, AV_LOG_ERROR, "Only audio, video, and subtitles are supported for Matroska.\n"); |
||
781 | return AVERROR(EINVAL); |
||
782 | } |
||
783 | |||
784 | if (mkv->mode != MODE_WEBM || codec->codec_id != AV_CODEC_ID_WEBVTT) { |
||
785 | ret = mkv_write_codecprivate(s, pb, codec, native_id, qt_id); |
||
786 | if (ret < 0) return ret; |
||
787 | } |
||
788 | |||
789 | end_ebml_master(pb, track); |
||
790 | |||
791 | // ms precision is the de-facto standard timescale for mkv files |
||
792 | avpriv_set_pts_info(st, 64, 1, 1000); |
||
793 | } |
||
794 | end_ebml_master(pb, tracks); |
||
795 | return 0; |
||
796 | } |
||
797 | |||
798 | static int mkv_write_chapters(AVFormatContext *s) |
||
799 | { |
||
800 | MatroskaMuxContext *mkv = s->priv_data; |
||
801 | AVIOContext *pb = s->pb; |
||
802 | ebml_master chapters, editionentry; |
||
803 | AVRational scale = {1, 1E9}; |
||
804 | int i, ret; |
||
805 | |||
806 | if (!s->nb_chapters || mkv->wrote_chapters) |
||
807 | return 0; |
||
808 | |||
809 | ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CHAPTERS, avio_tell(pb)); |
||
810 | if (ret < 0) return ret; |
||
811 | |||
812 | chapters = start_ebml_master(pb, MATROSKA_ID_CHAPTERS , 0); |
||
813 | editionentry = start_ebml_master(pb, MATROSKA_ID_EDITIONENTRY, 0); |
||
814 | put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGDEFAULT, 1); |
||
815 | put_ebml_uint(pb, MATROSKA_ID_EDITIONFLAGHIDDEN , 0); |
||
816 | for (i = 0; i < s->nb_chapters; i++) { |
||
817 | ebml_master chapteratom, chapterdisplay; |
||
818 | AVChapter *c = s->chapters[i]; |
||
819 | AVDictionaryEntry *t = NULL; |
||
820 | |||
821 | chapteratom = start_ebml_master(pb, MATROSKA_ID_CHAPTERATOM, 0); |
||
822 | put_ebml_uint(pb, MATROSKA_ID_CHAPTERUID, c->id + mkv->chapter_id_offset); |
||
823 | put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMESTART, |
||
824 | av_rescale_q(c->start, c->time_base, scale)); |
||
825 | put_ebml_uint(pb, MATROSKA_ID_CHAPTERTIMEEND, |
||
826 | av_rescale_q(c->end, c->time_base, scale)); |
||
827 | put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGHIDDEN , 0); |
||
828 | put_ebml_uint(pb, MATROSKA_ID_CHAPTERFLAGENABLED, 1); |
||
829 | if ((t = av_dict_get(c->metadata, "title", NULL, 0))) { |
||
830 | chapterdisplay = start_ebml_master(pb, MATROSKA_ID_CHAPTERDISPLAY, 0); |
||
831 | put_ebml_string(pb, MATROSKA_ID_CHAPSTRING, t->value); |
||
832 | put_ebml_string(pb, MATROSKA_ID_CHAPLANG , "und"); |
||
833 | end_ebml_master(pb, chapterdisplay); |
||
834 | } |
||
835 | end_ebml_master(pb, chapteratom); |
||
836 | } |
||
837 | end_ebml_master(pb, editionentry); |
||
838 | end_ebml_master(pb, chapters); |
||
839 | |||
840 | mkv->wrote_chapters = 1; |
||
841 | return 0; |
||
842 | } |
||
843 | |||
844 | static void mkv_write_simpletag(AVIOContext *pb, AVDictionaryEntry *t) |
||
845 | { |
||
846 | uint8_t *key = av_strdup(t->key); |
||
847 | uint8_t *p = key; |
||
848 | const uint8_t *lang = NULL; |
||
849 | ebml_master tag; |
||
850 | |||
851 | if ((p = strrchr(p, '-')) && |
||
852 | (lang = av_convert_lang_to(p + 1, AV_LANG_ISO639_2_BIBL))) |
||
853 | *p = 0; |
||
854 | |||
855 | p = key; |
||
856 | while (*p) { |
||
857 | if (*p == ' ') |
||
858 | *p = '_'; |
||
859 | else if (*p >= 'a' && *p <= 'z') |
||
860 | *p -= 'a' - 'A'; |
||
861 | p++; |
||
862 | } |
||
863 | |||
864 | tag = start_ebml_master(pb, MATROSKA_ID_SIMPLETAG, 0); |
||
865 | put_ebml_string(pb, MATROSKA_ID_TAGNAME, key); |
||
866 | if (lang) |
||
867 | put_ebml_string(pb, MATROSKA_ID_TAGLANG, lang); |
||
868 | put_ebml_string(pb, MATROSKA_ID_TAGSTRING, t->value); |
||
869 | end_ebml_master(pb, tag); |
||
870 | |||
871 | av_freep(&key); |
||
872 | } |
||
873 | |||
874 | static int mkv_write_tag(AVFormatContext *s, AVDictionary *m, unsigned int elementid, |
||
875 | unsigned int uid, ebml_master *tags) |
||
876 | { |
||
877 | MatroskaMuxContext *mkv = s->priv_data; |
||
878 | ebml_master tag, targets; |
||
879 | AVDictionaryEntry *t = NULL; |
||
880 | int ret; |
||
881 | |||
882 | if (!tags->pos) { |
||
883 | ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TAGS, avio_tell(s->pb)); |
||
884 | if (ret < 0) return ret; |
||
885 | |||
886 | *tags = start_ebml_master(s->pb, MATROSKA_ID_TAGS, 0); |
||
887 | } |
||
888 | |||
889 | tag = start_ebml_master(s->pb, MATROSKA_ID_TAG, 0); |
||
890 | targets = start_ebml_master(s->pb, MATROSKA_ID_TAGTARGETS, 0); |
||
891 | if (elementid) |
||
892 | put_ebml_uint(s->pb, elementid, uid); |
||
893 | end_ebml_master(s->pb, targets); |
||
894 | |||
895 | while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) |
||
896 | if (av_strcasecmp(t->key, "title") && av_strcasecmp(t->key, "stereo_mode")) |
||
897 | mkv_write_simpletag(s->pb, t); |
||
898 | |||
899 | end_ebml_master(s->pb, tag); |
||
900 | return 0; |
||
901 | } |
||
902 | |||
903 | static int mkv_check_tag(AVDictionary *m) |
||
904 | { |
||
905 | AVDictionaryEntry *t = NULL; |
||
906 | |||
907 | while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX))) |
||
908 | if (av_strcasecmp(t->key, "title") && av_strcasecmp(t->key, "stereo_mode")) |
||
909 | return 1; |
||
910 | |||
911 | return 0; |
||
912 | } |
||
913 | |||
914 | static int mkv_write_tags(AVFormatContext *s) |
||
915 | { |
||
916 | MatroskaMuxContext *mkv = s->priv_data; |
||
917 | ebml_master tags = {0}; |
||
918 | int i, ret; |
||
919 | |||
920 | ff_metadata_conv_ctx(s, ff_mkv_metadata_conv, NULL); |
||
921 | |||
922 | if (mkv_check_tag(s->metadata)) { |
||
923 | ret = mkv_write_tag(s, s->metadata, 0, 0, &tags); |
||
924 | if (ret < 0) return ret; |
||
925 | } |
||
926 | |||
927 | for (i = 0; i < s->nb_streams; i++) { |
||
928 | AVStream *st = s->streams[i]; |
||
929 | |||
930 | if (!mkv_check_tag(st->metadata)) |
||
931 | continue; |
||
932 | |||
933 | ret = mkv_write_tag(s, st->metadata, MATROSKA_ID_TAGTARGETS_TRACKUID, i + 1, &tags); |
||
934 | if (ret < 0) return ret; |
||
935 | } |
||
936 | |||
937 | for (i = 0; i < s->nb_chapters; i++) { |
||
938 | AVChapter *ch = s->chapters[i]; |
||
939 | |||
940 | if (!mkv_check_tag(ch->metadata)) |
||
941 | continue; |
||
942 | |||
943 | ret = mkv_write_tag(s, ch->metadata, MATROSKA_ID_TAGTARGETS_CHAPTERUID, ch->id + mkv->chapter_id_offset, &tags); |
||
944 | if (ret < 0) return ret; |
||
945 | } |
||
946 | |||
947 | if (tags.pos) |
||
948 | end_ebml_master(s->pb, tags); |
||
949 | return 0; |
||
950 | } |
||
951 | |||
952 | static int mkv_write_attachments(AVFormatContext *s) |
||
953 | { |
||
954 | MatroskaMuxContext *mkv = s->priv_data; |
||
955 | AVIOContext *pb = s->pb; |
||
956 | ebml_master attachments; |
||
957 | AVLFG c; |
||
958 | int i, ret; |
||
959 | |||
960 | if (!mkv->have_attachments) |
||
961 | return 0; |
||
962 | |||
963 | av_lfg_init(&c, av_get_random_seed()); |
||
964 | |||
965 | ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_ATTACHMENTS, avio_tell(pb)); |
||
966 | if (ret < 0) return ret; |
||
967 | |||
968 | attachments = start_ebml_master(pb, MATROSKA_ID_ATTACHMENTS, 0); |
||
969 | |||
970 | for (i = 0; i < s->nb_streams; i++) { |
||
971 | AVStream *st = s->streams[i]; |
||
972 | ebml_master attached_file; |
||
973 | AVDictionaryEntry *t; |
||
974 | const char *mimetype = NULL; |
||
975 | uint64_t fileuid; |
||
976 | |||
977 | if (st->codec->codec_type != AVMEDIA_TYPE_ATTACHMENT) |
||
978 | continue; |
||
979 | |||
980 | attached_file = start_ebml_master(pb, MATROSKA_ID_ATTACHEDFILE, 0); |
||
981 | |||
982 | if (t = av_dict_get(st->metadata, "title", NULL, 0)) |
||
983 | put_ebml_string(pb, MATROSKA_ID_FILEDESC, t->value); |
||
984 | if (!(t = av_dict_get(st->metadata, "filename", NULL, 0))) { |
||
985 | av_log(s, AV_LOG_ERROR, "Attachment stream %d has no filename tag.\n", i); |
||
986 | return AVERROR(EINVAL); |
||
987 | } |
||
988 | put_ebml_string(pb, MATROSKA_ID_FILENAME, t->value); |
||
989 | if (t = av_dict_get(st->metadata, "mimetype", NULL, 0)) |
||
990 | mimetype = t->value; |
||
991 | else if (st->codec->codec_id != AV_CODEC_ID_NONE ) { |
||
992 | int i; |
||
993 | for (i = 0; ff_mkv_mime_tags[i].id != AV_CODEC_ID_NONE; i++) |
||
994 | if (ff_mkv_mime_tags[i].id == st->codec->codec_id) { |
||
995 | mimetype = ff_mkv_mime_tags[i].str; |
||
996 | break; |
||
997 | } |
||
998 | } |
||
999 | if (!mimetype) { |
||
1000 | av_log(s, AV_LOG_ERROR, "Attachment stream %d has no mimetype tag and " |
||
1001 | "it cannot be deduced from the codec id.\n", i); |
||
1002 | return AVERROR(EINVAL); |
||
1003 | } |
||
1004 | |||
1005 | if (st->codec->flags & CODEC_FLAG_BITEXACT) { |
||
1006 | struct AVSHA *sha = av_sha_alloc(); |
||
1007 | uint8_t digest[20]; |
||
1008 | if (!sha) |
||
1009 | return AVERROR(ENOMEM); |
||
1010 | av_sha_init(sha, 160); |
||
1011 | av_sha_update(sha, st->codec->extradata, st->codec->extradata_size); |
||
1012 | av_sha_final(sha, digest); |
||
1013 | av_free(sha); |
||
1014 | fileuid = AV_RL64(digest); |
||
1015 | } else { |
||
1016 | fileuid = av_lfg_get(&c); |
||
1017 | } |
||
1018 | av_log(s, AV_LOG_VERBOSE, "Using %.16"PRIx64" for attachment %d\n", |
||
1019 | fileuid, i); |
||
1020 | |||
1021 | put_ebml_string(pb, MATROSKA_ID_FILEMIMETYPE, mimetype); |
||
1022 | put_ebml_binary(pb, MATROSKA_ID_FILEDATA, st->codec->extradata, st->codec->extradata_size); |
||
1023 | put_ebml_uint(pb, MATROSKA_ID_FILEUID, fileuid); |
||
1024 | end_ebml_master(pb, attached_file); |
||
1025 | } |
||
1026 | end_ebml_master(pb, attachments); |
||
1027 | |||
1028 | return 0; |
||
1029 | } |
||
1030 | |||
1031 | static int mkv_write_header(AVFormatContext *s) |
||
1032 | { |
||
1033 | MatroskaMuxContext *mkv = s->priv_data; |
||
1034 | AVIOContext *pb = s->pb; |
||
1035 | ebml_master ebml_header, segment_info; |
||
1036 | AVDictionaryEntry *tag; |
||
1037 | int ret, i; |
||
1038 | |||
1039 | if (!strcmp(s->oformat->name, "webm")) mkv->mode = MODE_WEBM; |
||
1040 | else mkv->mode = MODE_MATROSKAv2; |
||
1041 | |||
1042 | if (s->avoid_negative_ts < 0) |
||
1043 | s->avoid_negative_ts = 1; |
||
1044 | |||
1045 | for (i = 0; i < s->nb_streams; i++) |
||
1046 | if (s->streams[i]->codec->codec_id == AV_CODEC_ID_ATRAC3 || |
||
1047 | s->streams[i]->codec->codec_id == AV_CODEC_ID_COOK || |
||
1048 | s->streams[i]->codec->codec_id == AV_CODEC_ID_RA_288 || |
||
1049 | s->streams[i]->codec->codec_id == AV_CODEC_ID_SIPR || |
||
1050 | s->streams[i]->codec->codec_id == AV_CODEC_ID_RV10 || |
||
1051 | s->streams[i]->codec->codec_id == AV_CODEC_ID_RV20) { |
||
1052 | av_log(s, AV_LOG_ERROR, |
||
1053 | "The Matroska muxer does not yet support muxing %s\n", |
||
1054 | avcodec_get_name(s->streams[i]->codec->codec_id)); |
||
1055 | return AVERROR_PATCHWELCOME; |
||
1056 | } |
||
1057 | |||
1058 | mkv->tracks = av_mallocz(s->nb_streams * sizeof(*mkv->tracks)); |
||
1059 | if (!mkv->tracks) |
||
1060 | return AVERROR(ENOMEM); |
||
1061 | |||
1062 | ebml_header = start_ebml_master(pb, EBML_ID_HEADER, 0); |
||
1063 | put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1); |
||
1064 | put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1); |
||
1065 | put_ebml_uint (pb, EBML_ID_EBMLMAXIDLENGTH , 4); |
||
1066 | put_ebml_uint (pb, EBML_ID_EBMLMAXSIZELENGTH , 8); |
||
1067 | put_ebml_string (pb, EBML_ID_DOCTYPE , s->oformat->name); |
||
1068 | put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , 4); |
||
1069 | put_ebml_uint (pb, EBML_ID_DOCTYPEREADVERSION , 2); |
||
1070 | end_ebml_master(pb, ebml_header); |
||
1071 | |||
1072 | mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT, 0); |
||
1073 | mkv->segment_offset = avio_tell(pb); |
||
1074 | |||
1075 | // we write 2 seek heads - one at the end of the file to point to each |
||
1076 | // cluster, and one at the beginning to point to all other level one |
||
1077 | // elements (including the seek head at the end of the file), which |
||
1078 | // isn't more than 10 elements if we only write one of each other |
||
1079 | // currently defined level 1 element |
||
1080 | mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10); |
||
1081 | if (!mkv->main_seekhead) |
||
1082 | return AVERROR(ENOMEM); |
||
1083 | |||
1084 | ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_INFO, avio_tell(pb)); |
||
1085 | if (ret < 0) return ret; |
||
1086 | |||
1087 | segment_info = start_ebml_master(pb, MATROSKA_ID_INFO, 0); |
||
1088 | put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000); |
||
1089 | if ((tag = av_dict_get(s->metadata, "title", NULL, 0))) |
||
1090 | put_ebml_string(pb, MATROSKA_ID_TITLE, tag->value); |
||
1091 | if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) { |
||
1092 | uint32_t segment_uid[4]; |
||
1093 | AVLFG lfg; |
||
1094 | |||
1095 | av_lfg_init(&lfg, av_get_random_seed()); |
||
1096 | |||
1097 | for (i = 0; i < 4; i++) |
||
1098 | segment_uid[i] = av_lfg_get(&lfg); |
||
1099 | |||
1100 | put_ebml_string(pb, MATROSKA_ID_MUXINGAPP , LIBAVFORMAT_IDENT); |
||
1101 | put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT); |
||
1102 | put_ebml_binary(pb, MATROSKA_ID_SEGMENTUID, segment_uid, 16); |
||
1103 | } else { |
||
1104 | const char *ident = "Lavf"; |
||
1105 | put_ebml_string(pb, MATROSKA_ID_MUXINGAPP , ident); |
||
1106 | put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, ident); |
||
1107 | } |
||
1108 | |||
1109 | if (tag = av_dict_get(s->metadata, "creation_time", NULL, 0)) { |
||
1110 | // Adjust time so it's relative to 2001-01-01 and convert to nanoseconds. |
||
1111 | int64_t date_utc = (ff_iso8601_to_unix_time(tag->value) - 978307200) * 1000000000; |
||
1112 | uint8_t date_utc_buf[8]; |
||
1113 | AV_WB64(date_utc_buf, date_utc); |
||
1114 | put_ebml_binary(pb, MATROSKA_ID_DATEUTC, date_utc_buf, 8); |
||
1115 | } |
||
1116 | |||
1117 | // reserve space for the duration |
||
1118 | mkv->duration = 0; |
||
1119 | mkv->duration_offset = avio_tell(pb); |
||
1120 | put_ebml_void(pb, 11); // assumes double-precision float to be written |
||
1121 | end_ebml_master(pb, segment_info); |
||
1122 | |||
1123 | ret = mkv_write_tracks(s); |
||
1124 | if (ret < 0) return ret; |
||
1125 | |||
1126 | for (i = 0; i < s->nb_chapters; i++) |
||
1127 | mkv->chapter_id_offset = FFMAX(mkv->chapter_id_offset, 1LL - s->chapters[i]->id); |
||
1128 | |||
1129 | if (mkv->mode != MODE_WEBM) { |
||
1130 | ret = mkv_write_chapters(s); |
||
1131 | if (ret < 0) return ret; |
||
1132 | |||
1133 | ret = mkv_write_tags(s); |
||
1134 | if (ret < 0) return ret; |
||
1135 | |||
1136 | ret = mkv_write_attachments(s); |
||
1137 | if (ret < 0) return ret; |
||
1138 | } |
||
1139 | |||
1140 | if (!s->pb->seekable) |
||
1141 | mkv_write_seekhead(pb, mkv->main_seekhead); |
||
1142 | |||
1143 | mkv->cues = mkv_start_cues(mkv->segment_offset); |
||
1144 | if (mkv->cues == NULL) |
||
1145 | return AVERROR(ENOMEM); |
||
1146 | |||
1147 | if (pb->seekable && mkv->reserve_cues_space) { |
||
1148 | mkv->cues_pos = avio_tell(pb); |
||
1149 | put_ebml_void(pb, mkv->reserve_cues_space); |
||
1150 | } |
||
1151 | |||
1152 | av_init_packet(&mkv->cur_audio_pkt); |
||
1153 | mkv->cur_audio_pkt.size = 0; |
||
1154 | mkv->cluster_pos = -1; |
||
1155 | |||
1156 | avio_flush(pb); |
||
1157 | |||
1158 | // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or |
||
1159 | // after 4k and on a keyframe |
||
1160 | if (pb->seekable) { |
||
1161 | if (mkv->cluster_time_limit < 0) |
||
1162 | mkv->cluster_time_limit = 5000; |
||
1163 | if (mkv->cluster_size_limit < 0) |
||
1164 | mkv->cluster_size_limit = 5 * 1024 * 1024; |
||
1165 | } else { |
||
1166 | if (mkv->cluster_time_limit < 0) |
||
1167 | mkv->cluster_time_limit = 1000; |
||
1168 | if (mkv->cluster_size_limit < 0) |
||
1169 | mkv->cluster_size_limit = 32 * 1024; |
||
1170 | } |
||
1171 | |||
1172 | return 0; |
||
1173 | } |
||
1174 | |||
1175 | static int mkv_blockgroup_size(int pkt_size) |
||
1176 | { |
||
1177 | int size = pkt_size + 4; |
||
1178 | size += ebml_num_size(size); |
||
1179 | size += 2; // EBML ID for block and block duration |
||
1180 | size += 8; // max size of block duration |
||
1181 | size += ebml_num_size(size); |
||
1182 | size += 1; // blockgroup EBML ID |
||
1183 | return size; |
||
1184 | } |
||
1185 | |||
1186 | static int ass_get_duration(const uint8_t *p) |
||
1187 | { |
||
1188 | int sh, sm, ss, sc, eh, em, es, ec; |
||
1189 | uint64_t start, end; |
||
1190 | |||
1191 | if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d", |
||
1192 | &sh, &sm, &ss, &sc, &eh, &em, &es, &ec) != 8) |
||
1193 | return 0; |
||
1194 | start = 3600000LL*sh + 60000LL*sm + 1000LL*ss + 10LL*sc; |
||
1195 | end = 3600000LL*eh + 60000LL*em + 1000LL*es + 10LL*ec; |
||
1196 | return end - start; |
||
1197 | } |
||
1198 | |||
1199 | #if FF_API_ASS_SSA |
||
1200 | static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt) |
||
1201 | { |
||
1202 | MatroskaMuxContext *mkv = s->priv_data; |
||
1203 | int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size; |
||
1204 | uint8_t *start, *end, *data = pkt->data; |
||
1205 | ebml_master blockgroup; |
||
1206 | char buffer[2048]; |
||
1207 | |||
1208 | while (data_size) { |
||
1209 | int duration = ass_get_duration(data); |
||
1210 | max_duration = FFMAX(duration, max_duration); |
||
1211 | end = memchr(data, '\n', data_size); |
||
1212 | size = line_size = end ? end-data+1 : data_size; |
||
1213 | size -= end ? (end[-1]=='\r')+1 : 0; |
||
1214 | start = data; |
||
1215 | for (i=0; i<3; i++, start++) |
||
1216 | if (!(start = memchr(start, ',', size-(start-data)))) |
||
1217 | return max_duration; |
||
1218 | size -= start - data; |
||
1219 | sscanf(data, "Dialogue: %d,", &layer); |
||
1220 | i = snprintf(buffer, sizeof(buffer), "%"PRId64",%d,", |
||
1221 | s->streams[pkt->stream_index]->nb_frames, layer); |
||
1222 | size = FFMIN(i+size, sizeof(buffer)); |
||
1223 | memcpy(buffer+i, start, size-i); |
||
1224 | |||
1225 | av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, " |
||
1226 | "pts %" PRId64 ", duration %d\n", |
||
1227 | avio_tell(pb), size, pkt->pts, duration); |
||
1228 | blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(size)); |
||
1229 | put_ebml_id(pb, MATROSKA_ID_BLOCK); |
||
1230 | put_ebml_num(pb, size+4, 0); |
||
1231 | avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126 |
||
1232 | avio_wb16(pb, pkt->pts - mkv->cluster_pts); |
||
1233 | avio_w8(pb, 0); |
||
1234 | avio_write(pb, buffer, size); |
||
1235 | put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration); |
||
1236 | end_ebml_master(pb, blockgroup); |
||
1237 | |||
1238 | data += line_size; |
||
1239 | data_size -= line_size; |
||
1240 | } |
||
1241 | |||
1242 | return max_duration; |
||
1243 | } |
||
1244 | #endif |
||
1245 | |||
1246 | static int mkv_strip_wavpack(const uint8_t *src, uint8_t **pdst, int *size) |
||
1247 | { |
||
1248 | uint8_t *dst; |
||
1249 | int srclen = *size; |
||
1250 | int offset = 0; |
||
1251 | int ret; |
||
1252 | |||
1253 | dst = av_malloc(srclen); |
||
1254 | if (!dst) |
||
1255 | return AVERROR(ENOMEM); |
||
1256 | |||
1257 | while (srclen >= WV_HEADER_SIZE) { |
||
1258 | WvHeader header; |
||
1259 | |||
1260 | ret = ff_wv_parse_header(&header, src); |
||
1261 | if (ret < 0) |
||
1262 | goto fail; |
||
1263 | src += WV_HEADER_SIZE; |
||
1264 | srclen -= WV_HEADER_SIZE; |
||
1265 | |||
1266 | if (srclen < header.blocksize) { |
||
1267 | ret = AVERROR_INVALIDDATA; |
||
1268 | goto fail; |
||
1269 | } |
||
1270 | |||
1271 | if (header.initial) { |
||
1272 | AV_WL32(dst + offset, header.samples); |
||
1273 | offset += 4; |
||
1274 | } |
||
1275 | AV_WL32(dst + offset, header.flags); |
||
1276 | AV_WL32(dst + offset + 4, header.crc); |
||
1277 | offset += 8; |
||
1278 | |||
1279 | if (!(header.initial && header.final)) { |
||
1280 | AV_WL32(dst + offset, header.blocksize); |
||
1281 | offset += 4; |
||
1282 | } |
||
1283 | |||
1284 | memcpy(dst + offset, src, header.blocksize); |
||
1285 | src += header.blocksize; |
||
1286 | srclen -= header.blocksize; |
||
1287 | offset += header.blocksize; |
||
1288 | } |
||
1289 | |||
1290 | *pdst = dst; |
||
1291 | *size = offset; |
||
1292 | |||
1293 | return 0; |
||
1294 | fail: |
||
1295 | av_freep(&dst); |
||
1296 | return ret; |
||
1297 | } |
||
1298 | |||
1299 | static void mkv_write_block(AVFormatContext *s, AVIOContext *pb, |
||
1300 | unsigned int blockid, AVPacket *pkt, int flags) |
||
1301 | { |
||
1302 | MatroskaMuxContext *mkv = s->priv_data; |
||
1303 | AVCodecContext *codec = s->streams[pkt->stream_index]->codec; |
||
1304 | uint8_t *data = NULL, *side_data = NULL; |
||
1305 | int offset = 0, size = pkt->size, side_data_size = 0; |
||
1306 | int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts; |
||
1307 | uint64_t additional_id = 0, discard_padding = 0; |
||
1308 | ebml_master block_group, block_additions, block_more; |
||
1309 | |||
1310 | av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, " |
||
1311 | "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n", |
||
1312 | avio_tell(pb), pkt->size, pkt->pts, pkt->dts, pkt->duration, flags); |
||
1313 | if (codec->codec_id == AV_CODEC_ID_H264 && codec->extradata_size > 0 && |
||
1314 | (AV_RB24(codec->extradata) == 1 || AV_RB32(codec->extradata) == 1)) |
||
1315 | ff_avc_parse_nal_units_buf(pkt->data, &data, &size); |
||
1316 | else if (codec->codec_id == AV_CODEC_ID_WAVPACK) { |
||
1317 | int ret = mkv_strip_wavpack(pkt->data, &data, &size); |
||
1318 | if (ret < 0) { |
||
1319 | av_log(s, AV_LOG_ERROR, "Error stripping a WavPack packet.\n"); |
||
1320 | return; |
||
1321 | } |
||
1322 | } else |
||
1323 | data = pkt->data; |
||
1324 | |||
1325 | if (codec->codec_id == AV_CODEC_ID_PRORES) { |
||
1326 | /* Matroska specification requires to remove the first QuickTime atom |
||
1327 | */ |
||
1328 | size -= 8; |
||
1329 | offset = 8; |
||
1330 | } |
||
1331 | |||
1332 | side_data = av_packet_get_side_data(pkt, |
||
1333 | AV_PKT_DATA_SKIP_SAMPLES, |
||
1334 | &side_data_size); |
||
1335 | |||
1336 | if (side_data && side_data_size >= 10) { |
||
1337 | discard_padding = av_rescale_q(AV_RL32(side_data + 4), |
||
1338 | (AVRational){1, codec->sample_rate}, |
||
1339 | (AVRational){1, 1000000000}); |
||
1340 | } |
||
1341 | |||
1342 | side_data = av_packet_get_side_data(pkt, |
||
1343 | AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, |
||
1344 | &side_data_size); |
||
1345 | if (side_data) { |
||
1346 | additional_id = AV_RB64(side_data); |
||
1347 | side_data += 8; |
||
1348 | side_data_size -= 8; |
||
1349 | } |
||
1350 | |||
1351 | if ((side_data_size && additional_id == 1) || discard_padding) { |
||
1352 | block_group = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, 0); |
||
1353 | blockid = MATROSKA_ID_BLOCK; |
||
1354 | } |
||
1355 | |||
1356 | put_ebml_id(pb, blockid); |
||
1357 | put_ebml_num(pb, size+4, 0); |
||
1358 | avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126 |
||
1359 | avio_wb16(pb, ts - mkv->cluster_pts); |
||
1360 | avio_w8(pb, flags); |
||
1361 | avio_write(pb, data + offset, size); |
||
1362 | if (data != pkt->data) |
||
1363 | av_free(data); |
||
1364 | |||
1365 | if (discard_padding) { |
||
1366 | put_ebml_uint(pb, MATROSKA_ID_DISCARDPADDING, discard_padding); |
||
1367 | } |
||
1368 | |||
1369 | if (side_data_size && additional_id == 1) { |
||
1370 | block_additions = start_ebml_master(pb, MATROSKA_ID_BLOCKADDITIONS, 0); |
||
1371 | block_more = start_ebml_master(pb, MATROSKA_ID_BLOCKMORE, 0); |
||
1372 | put_ebml_uint(pb, MATROSKA_ID_BLOCKADDID, 1); |
||
1373 | put_ebml_id(pb, MATROSKA_ID_BLOCKADDITIONAL); |
||
1374 | put_ebml_num(pb, side_data_size, 0); |
||
1375 | avio_write(pb, side_data, side_data_size); |
||
1376 | end_ebml_master(pb, block_more); |
||
1377 | end_ebml_master(pb, block_additions); |
||
1378 | } |
||
1379 | if ((side_data_size && additional_id == 1) || discard_padding) { |
||
1380 | end_ebml_master(pb, block_group); |
||
1381 | } |
||
1382 | } |
||
1383 | |||
1384 | static int srt_get_duration(uint8_t **buf) |
||
1385 | { |
||
1386 | int i, duration = 0; |
||
1387 | |||
1388 | for (i=0; i<2 && !duration; i++) { |
||
1389 | int s_hour, s_min, s_sec, s_hsec, e_hour, e_min, e_sec, e_hsec; |
||
1390 | if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d", |
||
1391 | &s_hour, &s_min, &s_sec, &s_hsec, |
||
1392 | &e_hour, &e_min, &e_sec, &e_hsec) == 8) { |
||
1393 | s_min += 60*s_hour; e_min += 60*e_hour; |
||
1394 | s_sec += 60*s_min; e_sec += 60*e_min; |
||
1395 | s_hsec += 1000*s_sec; e_hsec += 1000*e_sec; |
||
1396 | duration = e_hsec - s_hsec; |
||
1397 | } |
||
1398 | *buf += ff_subtitles_next_line(*buf); |
||
1399 | } |
||
1400 | return duration; |
||
1401 | } |
||
1402 | |||
1403 | static int mkv_write_srt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt) |
||
1404 | { |
||
1405 | ebml_master blockgroup; |
||
1406 | AVPacket pkt2 = *pkt; |
||
1407 | int64_t duration = srt_get_duration(&pkt2.data); |
||
1408 | pkt2.size -= pkt2.data - pkt->data; |
||
1409 | |||
1410 | blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, |
||
1411 | mkv_blockgroup_size(pkt2.size)); |
||
1412 | mkv_write_block(s, pb, MATROSKA_ID_BLOCK, &pkt2, 0); |
||
1413 | put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration); |
||
1414 | end_ebml_master(pb, blockgroup); |
||
1415 | |||
1416 | return duration; |
||
1417 | } |
||
1418 | |||
1419 | static int mkv_write_vtt_blocks(AVFormatContext *s, AVIOContext *pb, AVPacket *pkt) |
||
1420 | { |
||
1421 | MatroskaMuxContext *mkv = s->priv_data; |
||
1422 | ebml_master blockgroup; |
||
1423 | int id_size, settings_size, size; |
||
1424 | uint8_t *id, *settings; |
||
1425 | int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts; |
||
1426 | const int flags = 0; |
||
1427 | |||
1428 | id_size = 0; |
||
1429 | id = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_IDENTIFIER, |
||
1430 | &id_size); |
||
1431 | |||
1432 | settings_size = 0; |
||
1433 | settings = av_packet_get_side_data(pkt, AV_PKT_DATA_WEBVTT_SETTINGS, |
||
1434 | &settings_size); |
||
1435 | |||
1436 | size = id_size + 1 + settings_size + 1 + pkt->size; |
||
1437 | |||
1438 | av_log(s, AV_LOG_DEBUG, "Writing block at offset %" PRIu64 ", size %d, " |
||
1439 | "pts %" PRId64 ", dts %" PRId64 ", duration %d, flags %d\n", |
||
1440 | avio_tell(pb), size, pkt->pts, pkt->dts, pkt->duration, flags); |
||
1441 | |||
1442 | blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(size)); |
||
1443 | |||
1444 | put_ebml_id(pb, MATROSKA_ID_BLOCK); |
||
1445 | put_ebml_num(pb, size+4, 0); |
||
1446 | avio_w8(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126 |
||
1447 | avio_wb16(pb, ts - mkv->cluster_pts); |
||
1448 | avio_w8(pb, flags); |
||
1449 | avio_printf(pb, "%.*s\n%.*s\n%.*s", id_size, id, settings_size, settings, pkt->size, pkt->data); |
||
1450 | |||
1451 | put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, pkt->duration); |
||
1452 | end_ebml_master(pb, blockgroup); |
||
1453 | |||
1454 | return pkt->duration; |
||
1455 | } |
||
1456 | |||
1457 | static void mkv_flush_dynbuf(AVFormatContext *s) |
||
1458 | { |
||
1459 | MatroskaMuxContext *mkv = s->priv_data; |
||
1460 | int bufsize; |
||
1461 | uint8_t *dyn_buf; |
||
1462 | |||
1463 | if (!mkv->dyn_bc) |
||
1464 | return; |
||
1465 | |||
1466 | bufsize = avio_close_dyn_buf(mkv->dyn_bc, &dyn_buf); |
||
1467 | avio_write(s->pb, dyn_buf, bufsize); |
||
1468 | av_free(dyn_buf); |
||
1469 | mkv->dyn_bc = NULL; |
||
1470 | } |
||
1471 | |||
1472 | static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt) |
||
1473 | { |
||
1474 | MatroskaMuxContext *mkv = s->priv_data; |
||
1475 | AVIOContext *pb = s->pb; |
||
1476 | AVCodecContext *codec = s->streams[pkt->stream_index]->codec; |
||
1477 | int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY); |
||
1478 | int duration = pkt->duration; |
||
1479 | int ret; |
||
1480 | int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts; |
||
1481 | int64_t relative_packet_pos; |
||
1482 | |||
1483 | if (ts == AV_NOPTS_VALUE) { |
||
1484 | av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n"); |
||
1485 | return AVERROR(EINVAL); |
||
1486 | } |
||
1487 | |||
1488 | if (!s->pb->seekable) { |
||
1489 | if (!mkv->dyn_bc) { |
||
1490 | if ((ret = avio_open_dyn_buf(&mkv->dyn_bc)) < 0) { |
||
1491 | av_log(s, AV_LOG_ERROR, "Failed to open dynamic buffer\n"); |
||
1492 | return ret; |
||
1493 | } |
||
1494 | } |
||
1495 | pb = mkv->dyn_bc; |
||
1496 | } |
||
1497 | |||
1498 | if (mkv->cluster_pos == -1) { |
||
1499 | mkv->cluster_pos = avio_tell(s->pb); |
||
1500 | mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER, 0); |
||
1501 | put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts)); |
||
1502 | mkv->cluster_pts = FFMAX(0, ts); |
||
1503 | } |
||
1504 | |||
1505 | relative_packet_pos = avio_tell(s->pb) - mkv->cluster.pos; |
||
1506 | |||
1507 | if (codec->codec_type != AVMEDIA_TYPE_SUBTITLE) { |
||
1508 | mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7); |
||
1509 | #if FF_API_ASS_SSA |
||
1510 | } else if (codec->codec_id == AV_CODEC_ID_SSA) { |
||
1511 | duration = mkv_write_ass_blocks(s, pb, pkt); |
||
1512 | #endif |
||
1513 | } else if (codec->codec_id == AV_CODEC_ID_SRT) { |
||
1514 | duration = mkv_write_srt_blocks(s, pb, pkt); |
||
1515 | } else if (codec->codec_id == AV_CODEC_ID_WEBVTT) { |
||
1516 | duration = mkv_write_vtt_blocks(s, pb, pkt); |
||
1517 | } else { |
||
1518 | ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP, mkv_blockgroup_size(pkt->size)); |
||
1519 | /* For backward compatibility, prefer convergence_duration. */ |
||
1520 | if (pkt->convergence_duration > 0) { |
||
1521 | duration = pkt->convergence_duration; |
||
1522 | } |
||
1523 | mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 0); |
||
1524 | put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration); |
||
1525 | end_ebml_master(pb, blockgroup); |
||
1526 | } |
||
1527 | |||
1528 | if ((codec->codec_type == AVMEDIA_TYPE_VIDEO && keyframe) || codec->codec_type == AVMEDIA_TYPE_SUBTITLE) { |
||
1529 | ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, ts, mkv->cluster_pos, relative_packet_pos, |
||
1530 | codec->codec_type == AVMEDIA_TYPE_SUBTITLE ? duration : -1); |
||
1531 | if (ret < 0) return ret; |
||
1532 | } |
||
1533 | |||
1534 | mkv->duration = FFMAX(mkv->duration, ts + duration); |
||
1535 | return 0; |
||
1536 | } |
||
1537 | |||
1538 | static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt) |
||
1539 | { |
||
1540 | MatroskaMuxContext *mkv = s->priv_data; |
||
1541 | int codec_type = s->streams[pkt->stream_index]->codec->codec_type; |
||
1542 | int keyframe = !!(pkt->flags & AV_PKT_FLAG_KEY); |
||
1543 | int cluster_size; |
||
1544 | int64_t cluster_time; |
||
1545 | AVIOContext *pb; |
||
1546 | int ret; |
||
1547 | |||
1548 | if (mkv->tracks[pkt->stream_index].write_dts) |
||
1549 | cluster_time = pkt->dts - mkv->cluster_pts; |
||
1550 | else |
||
1551 | cluster_time = pkt->pts - mkv->cluster_pts; |
||
1552 | |||
1553 | // start a new cluster every 5 MB or 5 sec, or 32k / 1 sec for streaming or |
||
1554 | // after 4k and on a keyframe |
||
1555 | if (s->pb->seekable) { |
||
1556 | pb = s->pb; |
||
1557 | cluster_size = avio_tell(pb) - mkv->cluster_pos; |
||
1558 | } else { |
||
1559 | pb = mkv->dyn_bc; |
||
1560 | cluster_size = avio_tell(pb); |
||
1561 | } |
||
1562 | |||
1563 | if (mkv->cluster_pos != -1 && |
||
1564 | (cluster_size > mkv->cluster_size_limit || |
||
1565 | cluster_time > mkv->cluster_time_limit || |
||
1566 | (codec_type == AVMEDIA_TYPE_VIDEO && keyframe && |
||
1567 | cluster_size > 4 * 1024))) { |
||
1568 | av_log(s, AV_LOG_DEBUG, "Starting new cluster at offset %" PRIu64 |
||
1569 | " bytes, pts %" PRIu64 "dts %" PRIu64 "\n", |
||
1570 | avio_tell(pb), pkt->pts, pkt->dts); |
||
1571 | end_ebml_master(pb, mkv->cluster); |
||
1572 | mkv->cluster_pos = -1; |
||
1573 | if (mkv->dyn_bc) |
||
1574 | mkv_flush_dynbuf(s); |
||
1575 | avio_flush(s->pb); |
||
1576 | } |
||
1577 | |||
1578 | // check if we have an audio packet cached |
||
1579 | if (mkv->cur_audio_pkt.size > 0) { |
||
1580 | ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt); |
||
1581 | av_free_packet(&mkv->cur_audio_pkt); |
||
1582 | if (ret < 0) { |
||
1583 | av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret); |
||
1584 | return ret; |
||
1585 | } |
||
1586 | } |
||
1587 | |||
1588 | // buffer an audio packet to ensure the packet containing the video |
||
1589 | // keyframe's timecode is contained in the same cluster for WebM |
||
1590 | if (codec_type == AVMEDIA_TYPE_AUDIO) { |
||
1591 | mkv->cur_audio_pkt = *pkt; |
||
1592 | if (pkt->buf) { |
||
1593 | mkv->cur_audio_pkt.buf = av_buffer_ref(pkt->buf); |
||
1594 | ret = mkv->cur_audio_pkt.buf ? 0 : AVERROR(ENOMEM); |
||
1595 | } else |
||
1596 | ret = av_dup_packet(&mkv->cur_audio_pkt); |
||
1597 | if (mkv->cur_audio_pkt.side_data_elems > 0) { |
||
1598 | ret = av_copy_packet_side_data(&mkv->cur_audio_pkt, &mkv->cur_audio_pkt); |
||
1599 | } |
||
1600 | } else |
||
1601 | ret = mkv_write_packet_internal(s, pkt); |
||
1602 | return ret; |
||
1603 | } |
||
1604 | |||
1605 | static int mkv_write_flush_packet(AVFormatContext *s, AVPacket *pkt) |
||
1606 | { |
||
1607 | MatroskaMuxContext *mkv = s->priv_data; |
||
1608 | AVIOContext *pb; |
||
1609 | if (s->pb->seekable) |
||
1610 | pb = s->pb; |
||
1611 | else |
||
1612 | pb = mkv->dyn_bc; |
||
1613 | if (!pkt) { |
||
1614 | if (mkv->cluster_pos != -1) { |
||
1615 | av_log(s, AV_LOG_DEBUG, "Flushing cluster at offset %" PRIu64 |
||
1616 | " bytes\n", avio_tell(pb)); |
||
1617 | end_ebml_master(pb, mkv->cluster); |
||
1618 | mkv->cluster_pos = -1; |
||
1619 | if (mkv->dyn_bc) |
||
1620 | mkv_flush_dynbuf(s); |
||
1621 | avio_flush(s->pb); |
||
1622 | } |
||
1623 | return 0; |
||
1624 | } |
||
1625 | return mkv_write_packet(s, pkt); |
||
1626 | } |
||
1627 | |||
1628 | static int mkv_write_trailer(AVFormatContext *s) |
||
1629 | { |
||
1630 | MatroskaMuxContext *mkv = s->priv_data; |
||
1631 | AVIOContext *pb = s->pb; |
||
1632 | int64_t currentpos, cuespos; |
||
1633 | int ret; |
||
1634 | |||
1635 | // check if we have an audio packet cached |
||
1636 | if (mkv->cur_audio_pkt.size > 0) { |
||
1637 | ret = mkv_write_packet_internal(s, &mkv->cur_audio_pkt); |
||
1638 | av_free_packet(&mkv->cur_audio_pkt); |
||
1639 | if (ret < 0) { |
||
1640 | av_log(s, AV_LOG_ERROR, "Could not write cached audio packet ret:%d\n", ret); |
||
1641 | return ret; |
||
1642 | } |
||
1643 | } |
||
1644 | |||
1645 | if (mkv->dyn_bc) { |
||
1646 | end_ebml_master(mkv->dyn_bc, mkv->cluster); |
||
1647 | mkv_flush_dynbuf(s); |
||
1648 | } else if (mkv->cluster_pos != -1) { |
||
1649 | end_ebml_master(pb, mkv->cluster); |
||
1650 | } |
||
1651 | |||
1652 | if (mkv->mode != MODE_WEBM) { |
||
1653 | ret = mkv_write_chapters(s); |
||
1654 | if (ret < 0) return ret; |
||
1655 | } |
||
1656 | |||
1657 | if (pb->seekable) { |
||
1658 | if (mkv->cues->num_entries) { |
||
1659 | if (mkv->reserve_cues_space) { |
||
1660 | int64_t cues_end; |
||
1661 | |||
1662 | currentpos = avio_tell(pb); |
||
1663 | avio_seek(pb, mkv->cues_pos, SEEK_SET); |
||
1664 | |||
1665 | cuespos = mkv_write_cues(pb, mkv->cues, mkv->tracks, s->nb_streams); |
||
1666 | cues_end = avio_tell(pb); |
||
1667 | if (cues_end > cuespos + mkv->reserve_cues_space) { |
||
1668 | av_log(s, AV_LOG_ERROR, "Insufficient space reserved for cues: %d " |
||
1669 | "(needed: %"PRId64").\n", mkv->reserve_cues_space, |
||
1670 | cues_end - cuespos); |
||
1671 | return AVERROR(EINVAL); |
||
1672 | } |
||
1673 | |||
1674 | if (cues_end < cuespos + mkv->reserve_cues_space) |
||
1675 | put_ebml_void(pb, mkv->reserve_cues_space - (cues_end - cuespos)); |
||
1676 | |||
1677 | avio_seek(pb, currentpos, SEEK_SET); |
||
1678 | } else { |
||
1679 | cuespos = mkv_write_cues(pb, mkv->cues, mkv->tracks, s->nb_streams); |
||
1680 | } |
||
1681 | |||
1682 | ret = mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES, cuespos); |
||
1683 | if (ret < 0) return ret; |
||
1684 | } |
||
1685 | |||
1686 | mkv_write_seekhead(pb, mkv->main_seekhead); |
||
1687 | |||
1688 | // update the duration |
||
1689 | av_log(s, AV_LOG_DEBUG, "end duration = %" PRIu64 "\n", mkv->duration); |
||
1690 | currentpos = avio_tell(pb); |
||
1691 | avio_seek(pb, mkv->duration_offset, SEEK_SET); |
||
1692 | put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration); |
||
1693 | |||
1694 | avio_seek(pb, currentpos, SEEK_SET); |
||
1695 | } |
||
1696 | |||
1697 | end_ebml_master(pb, mkv->segment); |
||
1698 | av_freep(&mkv->tracks); |
||
1699 | av_freep(&mkv->cues->entries); |
||
1700 | av_freep(&mkv->cues); |
||
1701 | |||
1702 | return 0; |
||
1703 | } |
||
1704 | |||
1705 | static int mkv_query_codec(enum AVCodecID codec_id, int std_compliance) |
||
1706 | { |
||
1707 | int i; |
||
1708 | for (i = 0; ff_mkv_codec_tags[i].id != AV_CODEC_ID_NONE; i++) |
||
1709 | if (ff_mkv_codec_tags[i].id == codec_id) |
||
1710 | return 1; |
||
1711 | |||
1712 | if (std_compliance < FF_COMPLIANCE_NORMAL) { // mkv theoretically supports any |
||
1713 | enum AVMediaType type = avcodec_get_type(codec_id); // video/audio through VFW/ACM |
||
1714 | if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO) |
||
1715 | return 1; |
||
1716 | } |
||
1717 | |||
1718 | return 0; |
||
1719 | } |
||
1720 | |||
1721 | static const AVCodecTag additional_audio_tags[] = { |
||
1722 | { AV_CODEC_ID_ALAC, 0XFFFFFFFF }, |
||
1723 | { AV_CODEC_ID_EAC3, 0XFFFFFFFF }, |
||
1724 | { AV_CODEC_ID_MLP, 0xFFFFFFFF }, |
||
1725 | { AV_CODEC_ID_OPUS, 0xFFFFFFFF }, |
||
1726 | { AV_CODEC_ID_PCM_S16BE, 0xFFFFFFFF }, |
||
1727 | { AV_CODEC_ID_PCM_S24BE, 0xFFFFFFFF }, |
||
1728 | { AV_CODEC_ID_PCM_S32BE, 0xFFFFFFFF }, |
||
1729 | { AV_CODEC_ID_QDM2, 0xFFFFFFFF }, |
||
1730 | { AV_CODEC_ID_RA_144, 0xFFFFFFFF }, |
||
1731 | { AV_CODEC_ID_RA_288, 0xFFFFFFFF }, |
||
1732 | { AV_CODEC_ID_COOK, 0xFFFFFFFF }, |
||
1733 | { AV_CODEC_ID_TRUEHD, 0xFFFFFFFF }, |
||
1734 | { AV_CODEC_ID_NONE, 0xFFFFFFFF } |
||
1735 | }; |
||
1736 | |||
1737 | static const AVCodecTag additional_video_tags[] = { |
||
1738 | { AV_CODEC_ID_RV10, 0xFFFFFFFF }, |
||
1739 | { AV_CODEC_ID_RV20, 0xFFFFFFFF }, |
||
1740 | { AV_CODEC_ID_RV30, 0xFFFFFFFF }, |
||
1741 | { AV_CODEC_ID_RV40, 0xFFFFFFFF }, |
||
1742 | { AV_CODEC_ID_VP9, 0xFFFFFFFF }, |
||
1743 | { AV_CODEC_ID_NONE, 0xFFFFFFFF } |
||
1744 | }; |
||
1745 | |||
1746 | #define OFFSET(x) offsetof(MatroskaMuxContext, x) |
||
1747 | #define FLAGS AV_OPT_FLAG_ENCODING_PARAM |
||
1748 | static const AVOption options[] = { |
||
1749 | { "reserve_index_space", "Reserve a given amount of space (in bytes) at the beginning of the file for the index (cues).", OFFSET(reserve_cues_space), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS }, |
||
1750 | { "cluster_size_limit", "Store at most the provided amount of bytes in a cluster. ", OFFSET(cluster_size_limit), AV_OPT_TYPE_INT , { .i64 = -1 }, -1, INT_MAX, FLAGS }, |
||
1751 | { "cluster_time_limit", "Store at most the provided number of milliseconds in a cluster.", OFFSET(cluster_time_limit), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS }, |
||
1752 | { NULL }, |
||
1753 | }; |
||
1754 | |||
1755 | #if CONFIG_MATROSKA_MUXER |
||
1756 | static const AVClass matroska_class = { |
||
1757 | .class_name = "matroska muxer", |
||
1758 | .item_name = av_default_item_name, |
||
1759 | .option = options, |
||
1760 | .version = LIBAVUTIL_VERSION_INT, |
||
1761 | }; |
||
1762 | |||
1763 | AVOutputFormat ff_matroska_muxer = { |
||
1764 | .name = "matroska", |
||
1765 | .long_name = NULL_IF_CONFIG_SMALL("Matroska"), |
||
1766 | .mime_type = "video/x-matroska", |
||
1767 | .extensions = "mkv", |
||
1768 | .priv_data_size = sizeof(MatroskaMuxContext), |
||
1769 | .audio_codec = CONFIG_LIBVORBIS_ENCODER ? |
||
1770 | AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3, |
||
1771 | .video_codec = CONFIG_LIBX264_ENCODER ? |
||
1772 | AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4, |
||
1773 | .write_header = mkv_write_header, |
||
1774 | .write_packet = mkv_write_flush_packet, |
||
1775 | .write_trailer = mkv_write_trailer, |
||
1776 | .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS | |
||
1777 | AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH, |
||
1778 | .codec_tag = (const AVCodecTag* const []){ |
||
1779 | ff_codec_bmp_tags, ff_codec_wav_tags, |
||
1780 | additional_audio_tags, additional_video_tags, 0 |
||
1781 | }, |
||
1782 | #if FF_API_ASS_SSA |
||
1783 | .subtitle_codec = AV_CODEC_ID_SSA, |
||
1784 | #else |
||
1785 | .subtitle_codec = AV_CODEC_ID_ASS, |
||
1786 | #endif |
||
1787 | .query_codec = mkv_query_codec, |
||
1788 | .priv_class = &matroska_class, |
||
1789 | }; |
||
1790 | #endif |
||
1791 | |||
1792 | #if CONFIG_WEBM_MUXER |
||
1793 | static const AVClass webm_class = { |
||
1794 | .class_name = "webm muxer", |
||
1795 | .item_name = av_default_item_name, |
||
1796 | .option = options, |
||
1797 | .version = LIBAVUTIL_VERSION_INT, |
||
1798 | }; |
||
1799 | |||
1800 | AVOutputFormat ff_webm_muxer = { |
||
1801 | .name = "webm", |
||
1802 | .long_name = NULL_IF_CONFIG_SMALL("WebM"), |
||
1803 | .mime_type = "video/webm", |
||
1804 | .extensions = "webm", |
||
1805 | .priv_data_size = sizeof(MatroskaMuxContext), |
||
1806 | .audio_codec = AV_CODEC_ID_VORBIS, |
||
1807 | .video_codec = AV_CODEC_ID_VP8, |
||
1808 | .subtitle_codec = AV_CODEC_ID_WEBVTT, |
||
1809 | .write_header = mkv_write_header, |
||
1810 | .write_packet = mkv_write_flush_packet, |
||
1811 | .write_trailer = mkv_write_trailer, |
||
1812 | .flags = AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS | |
||
1813 | AVFMT_TS_NONSTRICT | AVFMT_ALLOW_FLUSH, |
||
1814 | .priv_class = &webm_class, |
||
1815 | }; |
||
1816 | #endif |
||
1817 | |||
1818 | #if CONFIG_MATROSKA_AUDIO_MUXER |
||
1819 | static const AVClass mka_class = { |
||
1820 | .class_name = "matroska audio muxer", |
||
1821 | .item_name = av_default_item_name, |
||
1822 | .option = options, |
||
1823 | .version = LIBAVUTIL_VERSION_INT, |
||
1824 | }; |
||
1825 | AVOutputFormat ff_matroska_audio_muxer = { |
||
1826 | .name = "matroska", |
||
1827 | .long_name = NULL_IF_CONFIG_SMALL("Matroska Audio"), |
||
1828 | .mime_type = "audio/x-matroska", |
||
1829 | .extensions = "mka", |
||
1830 | .priv_data_size = sizeof(MatroskaMuxContext), |
||
1831 | .audio_codec = CONFIG_LIBVORBIS_ENCODER ? |
||
1832 | AV_CODEC_ID_VORBIS : AV_CODEC_ID_AC3, |
||
1833 | .video_codec = AV_CODEC_ID_NONE, |
||
1834 | .write_header = mkv_write_header, |
||
1835 | .write_packet = mkv_write_flush_packet, |
||
1836 | .write_trailer = mkv_write_trailer, |
||
1837 | .flags = AVFMT_GLOBALHEADER | AVFMT_TS_NONSTRICT | |
||
1838 | AVFMT_ALLOW_FLUSH, |
||
1839 | .codec_tag = (const AVCodecTag* const []){ |
||
1840 | ff_codec_wav_tags, additional_audio_tags, 0 |
||
1841 | }, |
||
1842 | .priv_class = &mka_class, |
||
1843 | }; |
||
1844 | #endif>>>>>>>><>>2>>>>3;>>>>>>>>>>>>>>>>>>>>>>=>>>>=>><>>>>>>>>>> |