Mercurial > libavformat.hg
annotate matroskaenc.c @ 2467:12ec768e5166 libavformat
Write the creation time
author | conrad |
---|---|
date | Wed, 05 Sep 2007 00:24:01 +0000 |
parents | 6395824043bd |
children | 9f5ae944ea07 |
rev | line source |
---|---|
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
1 /* |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
2 * Matroska file muxer |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
3 * Copyright (c) 2007 David Conrad |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
4 * |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
5 * This file is part of FFmpeg. |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
6 * |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
9 * License as published by the Free Software Foundation; either |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
11 * |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
15 * Lesser General Public License for more details. |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
16 * |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
20 */ |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
21 |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
22 #include "avformat.h" |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
23 #include "riff.h" |
2435
4dde24899d01
Correctly write CodecPrivate element for Vorbis and Theora
conrad
parents:
2434
diff
changeset
|
24 #include "xiph.h" |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
25 #include "matroska.h" |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
26 |
2447 | 27 typedef struct mkv_seekhead_entry { |
28 unsigned int elementid; | |
29 uint64_t segmentpos; | |
30 } mkv_seekhead_entry; | |
31 | |
32 typedef struct mkv_seekhead { | |
33 offset_t filepos; | |
2450 | 34 offset_t segment_offset; ///< the file offset to the beginning of the segment |
35 int reserved_size; ///< -1 if appending to file | |
2447 | 36 int max_entries; |
37 mkv_seekhead_entry *entries; | |
38 int num_entries; | |
39 } mkv_seekhead; | |
40 | |
2451 | 41 typedef struct { |
42 uint64_t pts; | |
43 int tracknum; | |
44 offset_t cluster_pos; ///< file offset of the cluster containing the block | |
45 } mkv_cuepoint; | |
46 | |
47 typedef struct { | |
48 offset_t segment_offset; | |
49 mkv_cuepoint *entries; | |
50 int num_entries; | |
51 } mkv_cues; | |
52 | |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
53 typedef struct MatroskaMuxContext { |
2448 | 54 offset_t segment; |
55 offset_t segment_offset; | |
56 offset_t cluster; | |
2451 | 57 offset_t cluster_pos; ///< file offset of the current cluster |
2448 | 58 uint64_t cluster_pts; |
59 offset_t duration_offset; | |
60 uint64_t duration; | |
2447 | 61 mkv_seekhead *main_seekhead; |
62 mkv_seekhead *cluster_seekhead; | |
2451 | 63 mkv_cues *cues; |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
64 } MatroskaMuxContext; |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
65 |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
66 static void put_ebml_id(ByteIOContext *pb, unsigned int id) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
67 { |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
68 if (id >= 0x3fffff) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
69 put_byte(pb, id >> 24); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
70 if (id >= 0x7fff) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
71 put_byte(pb, id >> 16); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
72 if (id >= 0xff) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
73 put_byte(pb, id >> 8); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
74 put_byte(pb, id); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
75 } |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
76 |
2447 | 77 static int ebml_id_size(unsigned int id) |
78 { | |
2457 | 79 return (av_log2(id+1)-1)/7+1; |
2447 | 80 } |
81 | |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
82 // XXX: test this thoroughly and get rid of minbytes hack (currently needed to |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
83 // use up all of the space reserved in start_ebml_master) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
84 static void put_ebml_size(ByteIOContext *pb, uint64_t size, int minbytes) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
85 { |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
86 int bytes = minbytes; |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
87 |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
88 // sizes larger than this are currently undefined in EBML |
2440
69e2592531b6
Write unknown size if the size given is too large for EBML (greater than 2^56-1)
conrad
parents:
2439
diff
changeset
|
89 // so write "unknown" size |
69e2592531b6
Write unknown size if the size given is too large for EBML (greater than 2^56-1)
conrad
parents:
2439
diff
changeset
|
90 size = FFMIN(size, (1ULL<<56)-1); |
69e2592531b6
Write unknown size if the size given is too large for EBML (greater than 2^56-1)
conrad
parents:
2439
diff
changeset
|
91 |
69e2592531b6
Write unknown size if the size given is too large for EBML (greater than 2^56-1)
conrad
parents:
2439
diff
changeset
|
92 while (size >> (bytes*7 + 7)) bytes++; |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
93 |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
94 put_byte(pb, (0x80 >> bytes) | (size >> bytes*8)); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
95 for (bytes -= 1; bytes >= 0; bytes--) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
96 put_byte(pb, size >> bytes*8); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
97 } |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
98 |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
99 static void put_ebml_uint(ByteIOContext *pb, unsigned int elementid, uint64_t val) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
100 { |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
101 int bytes = 1; |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
102 while (val >> bytes*8) bytes++; |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
103 |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
104 put_ebml_id(pb, elementid); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
105 put_ebml_size(pb, bytes, 0); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
106 for (bytes -= 1; bytes >= 0; bytes--) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
107 put_byte(pb, val >> bytes*8); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
108 } |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
109 |
2430 | 110 static void put_ebml_float(ByteIOContext *pb, unsigned int elementid, double val) |
111 { | |
112 put_ebml_id(pb, elementid); | |
113 put_ebml_size(pb, 8, 0); | |
114 put_be64(pb, av_dbl2int(val)); | |
115 } | |
116 | |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
117 static void put_ebml_binary(ByteIOContext *pb, unsigned int elementid, |
2429 | 118 const uint8_t *buf, int size) |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
119 { |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
120 put_ebml_id(pb, elementid); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
121 put_ebml_size(pb, size, 0); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
122 put_buffer(pb, buf, size); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
123 } |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
124 |
2429 | 125 static void put_ebml_string(ByteIOContext *pb, unsigned int elementid, const char *str) |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
126 { |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
127 put_ebml_binary(pb, elementid, str, strlen(str)); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
128 } |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
129 |
2467 | 130 // avtime is POSIX time, microseconds |
131 static void put_ebml_date(ByteIOContext *pb, unsigned int elementid, int64_t avtime) | |
132 { | |
133 // midnight Jan 1, 1970 in UNIX epoch, microseconds | |
134 const int64_t ebmlDateStart = 978307200ULL * 1000000; | |
135 int64_t ebmlTime = (avtime - ebmlDateStart) * 1000; | |
136 | |
137 put_ebml_id(pb, elementid); | |
138 put_ebml_size(pb, 8, 0); | |
139 put_be64(pb, ebmlTime); | |
140 } | |
141 | |
2442 | 142 // this reserves exactly the amount of space specified by size, which must be at least 2 |
143 static void put_ebml_void(ByteIOContext *pb, uint64_t size) | |
144 { | |
145 offset_t currentpos = url_ftell(pb); | |
146 | |
147 if (size < 2) | |
148 return; | |
149 | |
150 put_ebml_id(pb, EBML_ID_VOID); | |
151 // we need to subtract the length needed to store the size from the size we need to reserve | |
152 // so 2 cases, we use 8 bytes to store the size if possible, 1 byte otherwise | |
153 if (size < 10) | |
154 put_ebml_size(pb, size-1, 0); | |
155 else | |
156 put_ebml_size(pb, size-9, 7); | |
157 url_fseek(pb, currentpos + size, SEEK_SET); | |
158 } | |
159 | |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
160 static offset_t start_ebml_master(ByteIOContext *pb, unsigned int elementid) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
161 { |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
162 put_ebml_id(pb, elementid); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
163 // XXX: this always reserves the maximum needed space to store any size value |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
164 // we should be smarter (additional parameter for expected size?) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
165 put_ebml_size(pb, (1ULL<<56)-1, 0); // largest unknown size |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
166 return url_ftell(pb); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
167 } |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
168 |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
169 static void end_ebml_master(ByteIOContext *pb, offset_t start) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
170 { |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
171 offset_t pos = url_ftell(pb); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
172 |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
173 url_fseek(pb, start - 8, SEEK_SET); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
174 put_ebml_size(pb, pos - start, 7); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
175 url_fseek(pb, pos, SEEK_SET); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
176 } |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
177 |
2447 | 178 // initializes a mkv_seekhead element to be ready to index level 1 matroska elements |
179 // if numelements is greater than 0, it reserves enough space for that many elements | |
180 // at the current file position and writes the seekhead there, otherwise the seekhead | |
181 // will be appended to the file when end_mkv_seekhead() is called | |
182 static mkv_seekhead * mkv_start_seekhead(ByteIOContext *pb, offset_t segment_offset, int numelements) | |
183 { | |
184 mkv_seekhead *new_seekhead = av_mallocz(sizeof(mkv_seekhead)); | |
185 if (new_seekhead == NULL) | |
186 return NULL; | |
187 | |
188 new_seekhead->segment_offset = segment_offset; | |
189 | |
190 if (numelements > 0) { | |
191 new_seekhead->filepos = url_ftell(pb); | |
192 // 21 bytes max for a seek entry, 10 bytes max for the SeekHead ID and size, | |
193 // and 3 bytes to guarantee that an EBML void element will fit afterwards | |
194 // XXX: 28 bytes right now because begin_ebml_master() reserves more than necessary | |
195 new_seekhead->reserved_size = numelements * 28 + 13; | |
196 new_seekhead->max_entries = numelements; | |
197 put_ebml_void(pb, new_seekhead->reserved_size); | |
198 } | |
199 return new_seekhead; | |
200 } | |
201 | |
202 static int mkv_add_seekhead_entry(mkv_seekhead *seekhead, unsigned int elementid, uint64_t filepos) | |
203 { | |
204 mkv_seekhead_entry *entries = seekhead->entries; | |
205 int new_entry = seekhead->num_entries; | |
206 | |
207 // don't store more elements than we reserved space for | |
208 if (seekhead->max_entries > 0 && seekhead->max_entries <= seekhead->num_entries) | |
209 return -1; | |
210 | |
211 entries = av_realloc(entries, (seekhead->num_entries + 1) * sizeof(mkv_seekhead_entry)); | |
212 if (entries == NULL) | |
213 return -1; | |
214 | |
215 entries[new_entry].elementid = elementid; | |
216 entries[new_entry].segmentpos = filepos - seekhead->segment_offset; | |
217 | |
218 seekhead->entries = entries; | |
219 seekhead->num_entries++; | |
220 | |
221 return 0; | |
222 } | |
223 | |
224 // returns the file offset where the seekhead was written and frees the seekhead | |
225 static offset_t mkv_write_seekhead(ByteIOContext *pb, mkv_seekhead *seekhead) | |
226 { | |
227 offset_t metaseek, seekentry, currentpos; | |
228 int i; | |
229 | |
230 currentpos = url_ftell(pb); | |
231 | |
232 if (seekhead->reserved_size > 0) | |
233 url_fseek(pb, seekhead->filepos, SEEK_SET); | |
234 | |
235 metaseek = start_ebml_master(pb, MATROSKA_ID_SEEKHEAD); | |
236 for (i = 0; i < seekhead->num_entries; i++) { | |
237 mkv_seekhead_entry *entry = &seekhead->entries[i]; | |
238 | |
239 seekentry = start_ebml_master(pb, MATROSKA_ID_SEEKENTRY); | |
240 | |
241 put_ebml_id(pb, MATROSKA_ID_SEEKID); | |
242 put_ebml_size(pb, ebml_id_size(entry->elementid), 0); | |
243 put_ebml_id(pb, entry->elementid); | |
244 | |
245 put_ebml_uint(pb, MATROSKA_ID_SEEKPOSITION, entry->segmentpos); | |
246 end_ebml_master(pb, seekentry); | |
247 } | |
248 end_ebml_master(pb, metaseek); | |
249 | |
250 if (seekhead->reserved_size > 0) { | |
251 uint64_t remaining = seekhead->filepos + seekhead->reserved_size - url_ftell(pb); | |
252 put_ebml_void(pb, remaining); | |
253 url_fseek(pb, currentpos, SEEK_SET); | |
254 | |
255 currentpos = seekhead->filepos; | |
256 } | |
257 av_free(seekhead->entries); | |
258 av_free(seekhead); | |
259 | |
260 return currentpos; | |
261 } | |
262 | |
2451 | 263 static mkv_cues * mkv_start_cues(offset_t segment_offset) |
264 { | |
265 mkv_cues *cues = av_mallocz(sizeof(mkv_cues)); | |
266 if (cues == NULL) | |
267 return NULL; | |
268 | |
269 cues->segment_offset = segment_offset; | |
270 return cues; | |
271 } | |
272 | |
273 static int mkv_add_cuepoint(mkv_cues *cues, AVPacket *pkt, offset_t cluster_pos) | |
274 { | |
275 mkv_cuepoint *entries = cues->entries; | |
276 int new_entry = cues->num_entries; | |
277 | |
278 entries = av_realloc(entries, (cues->num_entries + 1) * sizeof(mkv_cuepoint)); | |
279 if (entries == NULL) | |
280 return -1; | |
281 | |
282 entries[new_entry].pts = pkt->pts; | |
283 entries[new_entry].tracknum = pkt->stream_index + 1; | |
284 entries[new_entry].cluster_pos = cluster_pos - cues->segment_offset; | |
285 | |
286 cues->entries = entries; | |
287 cues->num_entries++; | |
288 return 0; | |
289 } | |
290 | |
291 static offset_t mkv_write_cues(ByteIOContext *pb, mkv_cues *cues) | |
292 { | |
293 offset_t currentpos, cues_element; | |
294 int i, j; | |
295 | |
296 currentpos = url_ftell(pb); | |
297 cues_element = start_ebml_master(pb, MATROSKA_ID_CUES); | |
298 | |
299 for (i = 0; i < cues->num_entries; i++) { | |
300 offset_t cuepoint, track_positions; | |
301 mkv_cuepoint *entry = &cues->entries[i]; | |
302 uint64_t pts = entry->pts; | |
303 | |
304 cuepoint = start_ebml_master(pb, MATROSKA_ID_POINTENTRY); | |
305 put_ebml_uint(pb, MATROSKA_ID_CUETIME, pts); | |
306 | |
307 // put all the entries from different tracks that have the exact same | |
308 // timestamp into the same CuePoint | |
309 for (j = 0; j < cues->num_entries - i && entry[j].pts == pts; j++) { | |
310 track_positions = start_ebml_master(pb, MATROSKA_ID_CUETRACKPOSITION); | |
311 put_ebml_uint(pb, MATROSKA_ID_CUETRACK , entry[j].tracknum ); | |
312 put_ebml_uint(pb, MATROSKA_ID_CUECLUSTERPOSITION, entry[j].cluster_pos); | |
313 end_ebml_master(pb, track_positions); | |
314 } | |
315 i += j - 1; | |
316 end_ebml_master(pb, cuepoint); | |
317 } | |
318 end_ebml_master(pb, cues_element); | |
319 | |
320 av_free(cues->entries); | |
321 av_free(cues); | |
322 return currentpos; | |
323 } | |
324 | |
2444
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
325 static int put_xiph_codecpriv(ByteIOContext *pb, AVCodecContext *codec) |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
326 { |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
327 offset_t codecprivate; |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
328 uint8_t *header_start[3]; |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
329 int header_len[3]; |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
330 int first_header_size; |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
331 int j, k; |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
332 |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
333 if (codec->codec_id == CODEC_ID_VORBIS) |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
334 first_header_size = 30; |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
335 else |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
336 first_header_size = 42; |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
337 |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
338 if (ff_split_xiph_headers(codec->extradata, codec->extradata_size, |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
339 first_header_size, header_start, header_len) < 0) { |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
340 av_log(codec, AV_LOG_ERROR, "Extradata corrupt.\n"); |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
341 return -1; |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
342 } |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
343 |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
344 codecprivate = start_ebml_master(pb, MATROSKA_ID_CODECPRIVATE); |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
345 put_byte(pb, 2); // number packets - 1 |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
346 for (j = 0; j < 2; j++) { |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
347 for (k = 0; k < header_len[j] / 255; k++) |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
348 put_byte(pb, 255); |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
349 put_byte(pb, header_len[j] % 255); |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
350 } |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
351 for (j = 0; j < 3; j++) |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
352 put_buffer(pb, header_start[j], header_len[j]); |
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
353 end_ebml_master(pb, codecprivate); |
2446
b2f9523ee424
Make sure to return a value in functions that return a value
conrad
parents:
2445
diff
changeset
|
354 |
b2f9523ee424
Make sure to return a value in functions that return a value
conrad
parents:
2445
diff
changeset
|
355 return 0; |
2444
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
356 } |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
357 |
2458
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
358 static void get_aac_sample_rates(AVCodecContext *codec, int *sample_rate, int *output_sample_rate) |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
359 { |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
360 static const int aac_sample_rates[] = { |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
361 96000, 88200, 64000, 48000, 44100, 32000, |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
362 24000, 22050, 16000, 12000, 11025, 8000, |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
363 }; |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
364 int sri; |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
365 |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
366 if (codec->extradata_size < 2) { |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
367 av_log(codec, AV_LOG_WARNING, "no aac extradata, unable to determine sample rate\n"); |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
368 return; |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
369 } |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
370 |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
371 sri = ((codec->extradata[0] << 1) & 0xE) | (codec->extradata[1] >> 7); |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
372 if (sri > 12) { |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
373 av_log(codec, AV_LOG_WARNING, "aac samplerate index out of bounds\n"); |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
374 return; |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
375 } |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
376 *sample_rate = aac_sample_rates[sri]; |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
377 |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
378 // if sbr, get output sample rate as well |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
379 if (codec->extradata_size == 5) { |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
380 sri = (codec->extradata[4] >> 3) & 0xF; |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
381 if (sri > 12) { |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
382 av_log(codec, AV_LOG_WARNING, "aac output samplerate index out of bounds\n"); |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
383 return; |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
384 } |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
385 *output_sample_rate = aac_sample_rates[sri]; |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
386 } |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
387 } |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
388 |
2445
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
389 static int mkv_write_tracks(AVFormatContext *s) |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
390 { |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
391 MatroskaMuxContext *mkv = s->priv_data; |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
392 ByteIOContext *pb = &s->pb; |
2445
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
393 offset_t tracks; |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
394 int i, j; |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
395 |
2447 | 396 if (mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_TRACKS, url_ftell(pb)) < 0) |
397 return -1; | |
398 | |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
399 tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
400 for (i = 0; i < s->nb_streams; i++) { |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
401 AVStream *st = s->streams[i]; |
2432
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
402 AVCodecContext *codec = st->codec; |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
403 offset_t subinfo, track; |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
404 int native_id = 0; |
2454 | 405 int bit_depth = av_get_bits_per_sample(codec->codec_id); |
2458
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
406 int sample_rate = codec->sample_rate; |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
407 int output_sample_rate = 0; |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
408 |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
409 if (codec->codec_id == CODEC_ID_AAC) |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
410 get_aac_sample_rates(codec, &sample_rate, &output_sample_rate); |
2432
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
411 |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
412 track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY); |
2439 | 413 put_ebml_uint (pb, MATROSKA_ID_TRACKNUMBER , i + 1); |
414 put_ebml_uint (pb, MATROSKA_ID_TRACKUID , i + 1); | |
2432
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
415 put_ebml_uint (pb, MATROSKA_ID_TRACKFLAGLACING , 0); // no lacing (yet) |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
416 |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
417 if (st->language[0]) |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
418 put_ebml_string(pb, MATROSKA_ID_TRACKLANGUAGE, st->language); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
419 |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
420 // look for a codec id string specific to mkv to use, if none are found, use AVI codes |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
421 for (j = 0; ff_mkv_codec_tags[j].id != CODEC_ID_NONE; j++) { |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
422 if (ff_mkv_codec_tags[j].id == codec->codec_id) { |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
423 put_ebml_string(pb, MATROSKA_ID_CODECID, ff_mkv_codec_tags[j].str); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
424 native_id = 1; |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
425 break; |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
426 } |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
427 } |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
428 |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
429 // XXX: CodecPrivate for vorbis, theora, aac, native mpeg4, ... |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
430 if (native_id) { |
2435
4dde24899d01
Correctly write CodecPrivate element for Vorbis and Theora
conrad
parents:
2434
diff
changeset
|
431 if (codec->codec_id == CODEC_ID_VORBIS || codec->codec_id == CODEC_ID_THEORA) { |
2444
d954330691c5
Move Xiph's CodecPrivate writing code to its own function
conrad
parents:
2443
diff
changeset
|
432 if (put_xiph_codecpriv(pb, codec) < 0) |
2435
4dde24899d01
Correctly write CodecPrivate element for Vorbis and Theora
conrad
parents:
2434
diff
changeset
|
433 return -1; |
2459 | 434 } else if (codec->extradata_size) { |
2435
4dde24899d01
Correctly write CodecPrivate element for Vorbis and Theora
conrad
parents:
2434
diff
changeset
|
435 put_ebml_binary(pb, MATROSKA_ID_CODECPRIVATE, codec->extradata, codec->extradata_size); |
4dde24899d01
Correctly write CodecPrivate element for Vorbis and Theora
conrad
parents:
2434
diff
changeset
|
436 } |
2432
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
437 } |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
438 |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
439 switch (codec->codec_type) { |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
440 case CODEC_TYPE_VIDEO: |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
441 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_VIDEO); |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
442 |
2432
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
443 if (!native_id) { |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
444 offset_t bmp_header; |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
445 // if there is no mkv-specific codec id, use VFW mode |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
446 if (!codec->codec_tag) |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
447 codec->codec_tag = codec_get_tag(codec_bmp_tags, codec->codec_id); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
448 |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
449 put_ebml_string(pb, MATROSKA_ID_CODECID, MATROSKA_CODEC_ID_VIDEO_VFW_FOURCC); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
450 bmp_header = start_ebml_master(pb, MATROSKA_ID_CODECPRIVATE); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
451 put_bmp_header(pb, codec, codec_bmp_tags, 0); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
452 end_ebml_master(pb, bmp_header); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
453 } |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
454 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
455 // XXX: interlace flag? |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
456 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELWIDTH , codec->width); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
457 put_ebml_uint (pb, MATROSKA_ID_VIDEOPIXELHEIGHT, codec->height); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
458 // XXX: display width/height |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
459 end_ebml_master(pb, subinfo); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
460 break; |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
461 |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
462 case CODEC_TYPE_AUDIO: |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
463 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_AUDIO); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
464 |
2452
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
465 if (!native_id) { |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
466 offset_t wav_header; |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
467 // no mkv-specific ID, use ACM mode |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
468 codec->codec_tag = codec_get_tag(codec_wav_tags, codec->codec_id); |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
469 if (!codec->codec_tag) { |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
470 av_log(s, AV_LOG_ERROR, "no codec id found for stream %d", i); |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
471 return -1; |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
472 } |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
473 |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
474 put_ebml_string(pb, MATROSKA_ID_CODECID, MATROSKA_CODEC_ID_AUDIO_ACM); |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
475 wav_header = start_ebml_master(pb, MATROSKA_ID_CODECPRIVATE); |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
476 put_wav_header(pb, codec); |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
477 end_ebml_master(pb, wav_header); |
1b6ea48f7f24
Write wav header if there is no native audio codec ID
conrad
parents:
2451
diff
changeset
|
478 } |
2432
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
479 subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKAUDIO); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
480 put_ebml_uint (pb, MATROSKA_ID_AUDIOCHANNELS , codec->channels); |
2458
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
481 put_ebml_float (pb, MATROSKA_ID_AUDIOSAMPLINGFREQ, sample_rate); |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
482 if (output_sample_rate) |
7286ab3bf026
Determine the output sample rate for SBR AAC and write it
conrad
parents:
2457
diff
changeset
|
483 put_ebml_float(pb, MATROSKA_ID_AUDIOOUTSAMPLINGFREQ, output_sample_rate); |
2453 | 484 if (bit_depth) |
485 put_ebml_uint(pb, MATROSKA_ID_AUDIOBITDEPTH, bit_depth); | |
2432
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
486 end_ebml_master(pb, subinfo); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
487 break; |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
488 |
2461 | 489 case CODEC_TYPE_SUBTITLE: |
490 put_ebml_uint(pb, MATROSKA_ID_TRACKTYPE, MATROSKA_TRACK_TYPE_SUBTITLE); | |
491 break; | |
2432
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
492 default: |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
493 av_log(s, AV_LOG_ERROR, "Only audio and video are supported for Matroska."); |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
494 break; |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
495 } |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
496 end_ebml_master(pb, track); |
2432
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
497 |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
498 // ms precision is the de-facto standard timescale for mkv files |
68743e7fa627
First stab at writing the tracks element, still needs some additional cases for certain codecs
conrad
parents:
2431
diff
changeset
|
499 av_set_pts_info(st, 64, 1, 1000); |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
500 } |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
501 end_ebml_master(pb, tracks); |
2446
b2f9523ee424
Make sure to return a value in functions that return a value
conrad
parents:
2445
diff
changeset
|
502 return 0; |
2445
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
503 } |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
504 |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
505 static int mkv_write_header(AVFormatContext *s) |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
506 { |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
507 MatroskaMuxContext *mkv = s->priv_data; |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
508 ByteIOContext *pb = &s->pb; |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
509 offset_t ebml_header, segment_info; |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
510 |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
511 ebml_header = start_ebml_master(pb, EBML_ID_HEADER); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
512 put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
513 put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
514 put_ebml_uint (pb, EBML_ID_EBMLMAXIDLENGTH , 4); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
515 put_ebml_uint (pb, EBML_ID_EBMLMAXSIZELENGTH , 8); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
516 put_ebml_string (pb, EBML_ID_DOCTYPE , "matroska"); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
517 put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , 2); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
518 put_ebml_uint (pb, EBML_ID_DOCTYPEREADVERSION , 2); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
519 end_ebml_master(pb, ebml_header); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
520 |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
521 mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT); |
2447 | 522 mkv->segment_offset = url_ftell(pb); |
523 | |
524 // we write 2 seek heads - one at the end of the file to point to each cluster, and | |
525 // one at the beginning to point to all other level one elements (including the seek | |
526 // head at the end of the file), which isn't more than 10 elements if we only write one | |
527 // of each other currently defined level 1 element | |
528 mkv->main_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 10); | |
529 mkv->cluster_seekhead = mkv_start_seekhead(pb, mkv->segment_offset, 0); | |
530 | |
531 if (mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_INFO, url_ftell(pb)) < 0) | |
532 return -1; | |
2445
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
533 |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
534 segment_info = start_ebml_master(pb, MATROSKA_ID_INFO); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
535 put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
536 if (strlen(s->title)) |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
537 put_ebml_string(pb, MATROSKA_ID_TITLE, s->title); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
538 if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) { |
2466 | 539 put_ebml_string(pb, MATROSKA_ID_MUXINGAPP , LIBAVFORMAT_IDENT); |
2445
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
540 put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT); |
2467 | 541 put_ebml_date (pb, MATROSKA_ID_DATEUTC , av_gettime() ); |
2445
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
542 } |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
543 // XXX: segment UID |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
544 // reserve space for the duration |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
545 mkv->duration = 0; |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
546 mkv->duration_offset = url_ftell(pb); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
547 put_ebml_void(pb, 11); // assumes double-precision float to be written |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
548 end_ebml_master(pb, segment_info); |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
549 |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
550 if (mkv_write_tracks(s) < 0) |
86466f60fc5d
Move writing the tracks element to its own function
conrad
parents:
2444
diff
changeset
|
551 return -1; |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
552 |
2447 | 553 if (mkv_add_seekhead_entry(mkv->cluster_seekhead, MATROSKA_ID_CLUSTER, url_ftell(pb)) < 0) |
554 return -1; | |
555 | |
2451 | 556 mkv->cluster_pos = url_ftell(pb); |
2433
0c047310f205
Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents:
2432
diff
changeset
|
557 mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER); |
0c047310f205
Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents:
2432
diff
changeset
|
558 put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, 0); |
2437 | 559 mkv->cluster_pts = 0; |
2433
0c047310f205
Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents:
2432
diff
changeset
|
560 |
2451 | 561 mkv->cues = mkv_start_cues(mkv->segment_offset); |
562 if (mkv->cues == NULL) | |
563 return -1; | |
564 | |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
565 return 0; |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
566 } |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
567 |
2460 | 568 static void mkv_write_block(AVFormatContext *s, unsigned int blockid, AVPacket *pkt, int flags) |
569 { | |
570 MatroskaMuxContext *mkv = s->priv_data; | |
571 ByteIOContext *pb = &s->pb; | |
572 | |
573 put_ebml_id(pb, blockid); | |
574 put_ebml_size(pb, pkt->size + 4, 0); | |
575 put_byte(pb, 0x80 | (pkt->stream_index + 1)); // this assumes stream_index is less than 126 | |
576 put_be16(pb, pkt->pts - mkv->cluster_pts); | |
577 put_byte(pb, flags); | |
578 put_buffer(pb, pkt->data, pkt->size); | |
579 } | |
580 | |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
581 static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
582 { |
2437 | 583 MatroskaMuxContext *mkv = s->priv_data; |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
584 ByteIOContext *pb = &s->pb; |
2463 | 585 AVCodecContext *codec = s->streams[pkt->stream_index]->codec; |
2455 | 586 int keyframe = !!(pkt->flags & PKT_FLAG_KEY); |
2433
0c047310f205
Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents:
2432
diff
changeset
|
587 |
2437 | 588 // start a new cluster every 5 MB or 5 sec |
589 if (url_ftell(pb) > mkv->cluster + 5*1024*1024 || pkt->pts > mkv->cluster_pts + 5000) { | |
590 end_ebml_master(pb, mkv->cluster); | |
2447 | 591 |
592 if (mkv_add_seekhead_entry(mkv->cluster_seekhead, MATROSKA_ID_CLUSTER, url_ftell(pb)) < 0) | |
593 return -1; | |
594 | |
2451 | 595 mkv->cluster_pos = url_ftell(pb); |
2437 | 596 mkv->cluster = start_ebml_master(pb, MATROSKA_ID_CLUSTER); |
597 put_ebml_uint(pb, MATROSKA_ID_CLUSTERTIMECODE, pkt->pts); | |
598 mkv->cluster_pts = pkt->pts; | |
599 } | |
600 | |
2463 | 601 if (codec->codec_type != CODEC_TYPE_SUBTITLE) { |
2462 | 602 mkv_write_block(s, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe << 7); |
2461 | 603 } else { |
604 offset_t blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP); | |
605 mkv_write_block(s, MATROSKA_ID_BLOCK, pkt, 0); | |
606 put_ebml_uint(pb, MATROSKA_ID_DURATION, pkt->duration); | |
607 end_ebml_master(pb, blockgroup); | |
608 } | |
2443 | 609 |
2463 | 610 if (codec->codec_type == CODEC_TYPE_VIDEO && keyframe) { |
2451 | 611 if (mkv_add_cuepoint(mkv->cues, pkt, mkv->cluster_pos) < 0) |
612 return -1; | |
613 } | |
614 | |
2443 | 615 mkv->duration = pkt->pts + pkt->duration; |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
616 return 0; |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
617 } |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
618 |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
619 static int mkv_write_trailer(AVFormatContext *s) |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
620 { |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
621 MatroskaMuxContext *mkv = s->priv_data; |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
622 ByteIOContext *pb = &s->pb; |
2451 | 623 offset_t currentpos, second_seekhead, cuespos; |
2443 | 624 |
2433
0c047310f205
Write one cluster and SimpleBlocks for the frames. Should now create playable mkv files for some video codecs (H.264 and VP3 checked)
conrad
parents:
2432
diff
changeset
|
625 end_ebml_master(pb, mkv->cluster); |
2443 | 626 |
2451 | 627 cuespos = mkv_write_cues(pb, mkv->cues); |
2447 | 628 second_seekhead = mkv_write_seekhead(pb, mkv->cluster_seekhead); |
2451 | 629 |
630 mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_CUES , cuespos); | |
2447 | 631 mkv_add_seekhead_entry(mkv->main_seekhead, MATROSKA_ID_SEEKHEAD, second_seekhead); |
632 mkv_write_seekhead(pb, mkv->main_seekhead); | |
633 | |
2443 | 634 // update the duration |
635 currentpos = url_ftell(pb); | |
636 url_fseek(pb, mkv->duration_offset, SEEK_SET); | |
637 put_ebml_float(pb, MATROSKA_ID_DURATION, mkv->duration); | |
638 url_fseek(pb, currentpos, SEEK_SET); | |
639 | |
2425
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
640 end_ebml_master(pb, mkv->segment); |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
641 return 0; |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
642 } |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
643 |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
644 AVOutputFormat matroska_muxer = { |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
645 "matroska", |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
646 "Matroska File Format", |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
647 "video/x-matroska", |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
648 "mkv", |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
649 sizeof(MatroskaMuxContext), |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
650 CODEC_ID_MP2, |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
651 CODEC_ID_MPEG4, |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
652 mkv_write_header, |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
653 mkv_write_packet, |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
654 mkv_write_trailer, |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
655 .codec_tag = (const AVCodecTag*[]){codec_bmp_tags, codec_wav_tags, 0}, |
8c51e92edd7d
Beginning of mkv muxer, only EBML head is written correctly
conrad
parents:
diff
changeset
|
656 }; |