ffmpeg / libavformat / mxfdec.c @ 8a47ad5e
History | View | Annotate | Download (37.9 KB)
1 |
/*
|
---|---|
2 |
* MXF demuxer.
|
3 |
* Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
|
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 |
/*
|
23 |
* References
|
24 |
* SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
|
25 |
* SMPTE 377M MXF File Format Specifications
|
26 |
* SMPTE 378M Operational Pattern 1a
|
27 |
* SMPTE 379M MXF Generic Container
|
28 |
* SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
|
29 |
* SMPTE 382M Mapping AES3 and Broadcast Wave Audio into the MXF Generic Container
|
30 |
* SMPTE 383M Mapping DV-DIF Data to the MXF Generic Container
|
31 |
*
|
32 |
* Principle
|
33 |
* Search for Track numbers which will identify essence element KLV packets.
|
34 |
* Search for SourcePackage which define tracks which contains Track numbers.
|
35 |
* Material Package contains tracks with reference to SourcePackage tracks.
|
36 |
* Search for Descriptors (Picture, Sound) which contains codec info and parameters.
|
37 |
* Assign Descriptors to correct Tracks.
|
38 |
*
|
39 |
* Metadata reading functions read Local Tags, get InstanceUID(0x3C0A) then add MetaDataSet to MXFContext.
|
40 |
* Metadata parsing resolves Strong References to objects.
|
41 |
*
|
42 |
* Simple demuxer, only OP1A supported and some files might not work at all.
|
43 |
* Only tracks with associated descriptors will be decoded. "Highly Desirable" SMPTE 377M D.1
|
44 |
*/
|
45 |
|
46 |
//#define DEBUG
|
47 |
|
48 |
#include "libavutil/aes.h" |
49 |
#include "mxf.h" |
50 |
|
51 |
typedef struct { |
52 |
UID uid; |
53 |
enum MXFMetadataSetType type;
|
54 |
UID source_container_ul; |
55 |
} MXFCryptoContext; |
56 |
|
57 |
typedef struct { |
58 |
UID uid; |
59 |
enum MXFMetadataSetType type;
|
60 |
UID source_package_uid; |
61 |
UID data_definition_ul; |
62 |
int64_t duration; |
63 |
int64_t start_position; |
64 |
int source_track_id;
|
65 |
} MXFStructuralComponent; |
66 |
|
67 |
typedef struct { |
68 |
UID uid; |
69 |
enum MXFMetadataSetType type;
|
70 |
UID data_definition_ul; |
71 |
UID *structural_components_refs; |
72 |
int structural_components_count;
|
73 |
int64_t duration; |
74 |
} MXFSequence; |
75 |
|
76 |
typedef struct { |
77 |
UID uid; |
78 |
enum MXFMetadataSetType type;
|
79 |
MXFSequence *sequence; /* mandatory, and only one */
|
80 |
UID sequence_ref; |
81 |
int track_id;
|
82 |
uint8_t track_number[4];
|
83 |
AVRational edit_rate; |
84 |
} MXFTrack; |
85 |
|
86 |
typedef struct { |
87 |
UID uid; |
88 |
enum MXFMetadataSetType type;
|
89 |
UID essence_container_ul; |
90 |
UID essence_codec_ul; |
91 |
AVRational sample_rate; |
92 |
AVRational aspect_ratio; |
93 |
int width;
|
94 |
int height;
|
95 |
int channels;
|
96 |
int bits_per_sample;
|
97 |
UID *sub_descriptors_refs; |
98 |
int sub_descriptors_count;
|
99 |
int linked_track_id;
|
100 |
uint8_t *extradata; |
101 |
int extradata_size;
|
102 |
} MXFDescriptor; |
103 |
|
104 |
typedef struct { |
105 |
UID uid; |
106 |
enum MXFMetadataSetType type;
|
107 |
} MXFIndexTableSegment; |
108 |
|
109 |
typedef struct { |
110 |
UID uid; |
111 |
enum MXFMetadataSetType type;
|
112 |
UID package_uid; |
113 |
UID *tracks_refs; |
114 |
int tracks_count;
|
115 |
MXFDescriptor *descriptor; /* only one */
|
116 |
UID descriptor_ref; |
117 |
} MXFPackage; |
118 |
|
119 |
typedef struct { |
120 |
UID uid; |
121 |
enum MXFMetadataSetType type;
|
122 |
} MXFMetadataSet; |
123 |
|
124 |
typedef struct { |
125 |
UID *packages_refs; |
126 |
int packages_count;
|
127 |
MXFMetadataSet **metadata_sets; |
128 |
int metadata_sets_count;
|
129 |
AVFormatContext *fc; |
130 |
struct AVAES *aesc;
|
131 |
uint8_t *local_tags; |
132 |
int local_tags_count;
|
133 |
} MXFContext; |
134 |
|
135 |
enum MXFWrappingScheme {
|
136 |
Frame, |
137 |
Clip, |
138 |
}; |
139 |
|
140 |
typedef struct { |
141 |
const UID key;
|
142 |
int (*read)();
|
143 |
int ctx_size;
|
144 |
enum MXFMetadataSetType type;
|
145 |
} MXFMetadataReadTableEntry; |
146 |
|
147 |
/* partial keys to match */
|
148 |
static const uint8_t mxf_header_partition_pack_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x02 }; |
149 |
static const uint8_t mxf_essence_element_key[] = { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01 }; |
150 |
static const uint8_t mxf_klv_key[] = { 0x06,0x0e,0x2b,0x34 }; |
151 |
/* complete keys to match */
|
152 |
static const uint8_t mxf_crypto_source_container_ul[] = { 0x06,0x0e,0x2b,0x34,0x01,0x01,0x01,0x09,0x06,0x01,0x01,0x02,0x02,0x00,0x00,0x00 }; |
153 |
static const uint8_t mxf_encrypted_triplet_key[] = { 0x06,0x0e,0x2b,0x34,0x02,0x04,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x7e,0x01,0x00 }; |
154 |
static const uint8_t mxf_encrypted_essence_container[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0b,0x01,0x00 }; |
155 |
static const uint8_t mxf_sony_mpeg4_extradata[] = { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x01,0x0e,0x06,0x06,0x02,0x02,0x01,0x00,0x00 }; |
156 |
|
157 |
#define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y))) |
158 |
|
159 |
static int64_t klv_decode_ber_length(ByteIOContext *pb)
|
160 |
{ |
161 |
uint64_t size = get_byte(pb); |
162 |
if (size & 0x80) { /* long form */ |
163 |
int bytes_num = size & 0x7f; |
164 |
/* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
|
165 |
if (bytes_num > 8) |
166 |
return -1; |
167 |
size = 0;
|
168 |
while (bytes_num--)
|
169 |
size = size << 8 | get_byte(pb);
|
170 |
} |
171 |
return size;
|
172 |
} |
173 |
|
174 |
static int mxf_read_sync(ByteIOContext *pb, const uint8_t *key, unsigned size) |
175 |
{ |
176 |
int i, b;
|
177 |
for (i = 0; i < size && !url_feof(pb); i++) { |
178 |
b = get_byte(pb); |
179 |
if (b == key[0]) |
180 |
i = 0;
|
181 |
else if (b != key[i]) |
182 |
i = -1;
|
183 |
} |
184 |
return i == size;
|
185 |
} |
186 |
|
187 |
static int klv_read_packet(KLVPacket *klv, ByteIOContext *pb) |
188 |
{ |
189 |
if (!mxf_read_sync(pb, mxf_klv_key, 4)) |
190 |
return -1; |
191 |
klv->offset = url_ftell(pb) - 4;
|
192 |
memcpy(klv->key, mxf_klv_key, 4);
|
193 |
get_buffer(pb, klv->key + 4, 12); |
194 |
klv->length = klv_decode_ber_length(pb); |
195 |
return klv->length == -1 ? -1 : 0; |
196 |
} |
197 |
|
198 |
static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv) |
199 |
{ |
200 |
int i;
|
201 |
|
202 |
for (i = 0; i < s->nb_streams; i++) { |
203 |
MXFTrack *track = s->streams[i]->priv_data; |
204 |
/* SMPTE 379M 7.3 */
|
205 |
if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number))) |
206 |
return i;
|
207 |
} |
208 |
/* return 0 if only one stream, for OP Atom files with 0 as track number */
|
209 |
return s->nb_streams == 1 ? 0 : -1; |
210 |
} |
211 |
|
212 |
/* XXX: use AVBitStreamFilter */
|
213 |
static int mxf_get_d10_aes3_packet(ByteIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length) |
214 |
{ |
215 |
uint8_t buffer[61444];
|
216 |
const uint8_t *buf_ptr, *end_ptr;
|
217 |
uint8_t *data_ptr; |
218 |
int i;
|
219 |
|
220 |
if (length > 61444) /* worst case PAL 1920 samples 8 channels */ |
221 |
return -1; |
222 |
get_buffer(pb, buffer, length); |
223 |
av_new_packet(pkt, length); |
224 |
data_ptr = pkt->data; |
225 |
end_ptr = buffer + length; |
226 |
buf_ptr = buffer + 4; /* skip SMPTE 331M header */ |
227 |
for (; buf_ptr < end_ptr; ) {
|
228 |
for (i = 0; i < st->codec->channels; i++) { |
229 |
uint32_t sample = bytestream_get_le32(&buf_ptr); |
230 |
if (st->codec->bits_per_coded_sample == 24) |
231 |
bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff); |
232 |
else
|
233 |
bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff); |
234 |
} |
235 |
buf_ptr += 32 - st->codec->channels*4; // always 8 channels stored SMPTE 331M |
236 |
} |
237 |
pkt->size = data_ptr - pkt->data; |
238 |
return 0; |
239 |
} |
240 |
|
241 |
static int mxf_decrypt_triplet(AVFormatContext *s, AVPacket *pkt, KLVPacket *klv) |
242 |
{ |
243 |
static const uint8_t checkv[16] = {0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b}; |
244 |
MXFContext *mxf = s->priv_data; |
245 |
ByteIOContext *pb = s->pb; |
246 |
int64_t end = url_ftell(pb) + klv->length; |
247 |
uint64_t size; |
248 |
uint64_t orig_size; |
249 |
uint64_t plaintext_size; |
250 |
uint8_t ivec[16];
|
251 |
uint8_t tmpbuf[16];
|
252 |
int index;
|
253 |
|
254 |
if (!mxf->aesc && s->key && s->keylen == 16) { |
255 |
mxf->aesc = av_malloc(av_aes_size); |
256 |
if (!mxf->aesc)
|
257 |
return -1; |
258 |
av_aes_init(mxf->aesc, s->key, 128, 1); |
259 |
} |
260 |
// crypto context
|
261 |
url_fskip(pb, klv_decode_ber_length(pb)); |
262 |
// plaintext offset
|
263 |
klv_decode_ber_length(pb); |
264 |
plaintext_size = get_be64(pb); |
265 |
// source klv key
|
266 |
klv_decode_ber_length(pb); |
267 |
get_buffer(pb, klv->key, 16);
|
268 |
if (!IS_KLV_KEY(klv, mxf_essence_element_key))
|
269 |
return -1; |
270 |
index = mxf_get_stream_index(s, klv); |
271 |
if (index < 0) |
272 |
return -1; |
273 |
// source size
|
274 |
klv_decode_ber_length(pb); |
275 |
orig_size = get_be64(pb); |
276 |
if (orig_size < plaintext_size)
|
277 |
return -1; |
278 |
// enc. code
|
279 |
size = klv_decode_ber_length(pb); |
280 |
if (size < 32 || size - 32 < orig_size) |
281 |
return -1; |
282 |
get_buffer(pb, ivec, 16);
|
283 |
get_buffer(pb, tmpbuf, 16);
|
284 |
if (mxf->aesc)
|
285 |
av_aes_crypt(mxf->aesc, tmpbuf, tmpbuf, 1, ivec, 1); |
286 |
if (memcmp(tmpbuf, checkv, 16)) |
287 |
av_log(s, AV_LOG_ERROR, "probably incorrect decryption key\n");
|
288 |
size -= 32;
|
289 |
av_get_packet(pb, pkt, size); |
290 |
size -= plaintext_size; |
291 |
if (mxf->aesc)
|
292 |
av_aes_crypt(mxf->aesc, &pkt->data[plaintext_size], |
293 |
&pkt->data[plaintext_size], size >> 4, ivec, 1); |
294 |
pkt->size = orig_size; |
295 |
pkt->stream_index = index; |
296 |
url_fskip(pb, end - url_ftell(pb)); |
297 |
return 0; |
298 |
} |
299 |
|
300 |
static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt) |
301 |
{ |
302 |
KLVPacket klv; |
303 |
|
304 |
while (!url_feof(s->pb)) {
|
305 |
if (klv_read_packet(&klv, s->pb) < 0) |
306 |
return -1; |
307 |
PRINT_KEY(s, "read packet", klv.key);
|
308 |
dprintf(s, "size %lld offset %#llx\n", klv.length, klv.offset);
|
309 |
if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) {
|
310 |
int res = mxf_decrypt_triplet(s, pkt, &klv);
|
311 |
if (res < 0) { |
312 |
av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
|
313 |
return -1; |
314 |
} |
315 |
return 0; |
316 |
} |
317 |
if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
|
318 |
int index = mxf_get_stream_index(s, &klv);
|
319 |
if (index < 0) { |
320 |
av_log(s, AV_LOG_ERROR, "error getting stream index %d\n", AV_RB32(klv.key+12)); |
321 |
goto skip;
|
322 |
} |
323 |
if (s->streams[index]->discard == AVDISCARD_ALL)
|
324 |
goto skip;
|
325 |
/* check for 8 channels AES3 element */
|
326 |
if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) { |
327 |
if (mxf_get_d10_aes3_packet(s->pb, s->streams[index], pkt, klv.length) < 0) { |
328 |
av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
|
329 |
return -1; |
330 |
} |
331 |
} else
|
332 |
av_get_packet(s->pb, pkt, klv.length); |
333 |
pkt->stream_index = index; |
334 |
pkt->pos = klv.offset; |
335 |
return 0; |
336 |
} else
|
337 |
skip: |
338 |
url_fskip(s->pb, klv.length); |
339 |
} |
340 |
return AVERROR(EIO);
|
341 |
} |
342 |
|
343 |
static int mxf_read_primer_pack(MXFContext *mxf) |
344 |
{ |
345 |
ByteIOContext *pb = mxf->fc->pb; |
346 |
int item_num = get_be32(pb);
|
347 |
int item_len = get_be32(pb);
|
348 |
|
349 |
if (item_len != 18) { |
350 |
av_log(mxf->fc, AV_LOG_ERROR, "unsupported primer pack item length\n");
|
351 |
return -1; |
352 |
} |
353 |
if (item_num > UINT_MAX / item_len)
|
354 |
return -1; |
355 |
mxf->local_tags_count = item_num; |
356 |
mxf->local_tags = av_malloc(item_num*item_len); |
357 |
if (!mxf->local_tags)
|
358 |
return -1; |
359 |
get_buffer(pb, mxf->local_tags, item_num*item_len); |
360 |
return 0; |
361 |
} |
362 |
|
363 |
static int mxf_add_metadata_set(MXFContext *mxf, void *metadata_set) |
364 |
{ |
365 |
mxf->metadata_sets = av_realloc(mxf->metadata_sets, (mxf->metadata_sets_count + 1) * sizeof(*mxf->metadata_sets)); |
366 |
if (!mxf->metadata_sets)
|
367 |
return -1; |
368 |
mxf->metadata_sets[mxf->metadata_sets_count] = metadata_set; |
369 |
mxf->metadata_sets_count++; |
370 |
return 0; |
371 |
} |
372 |
|
373 |
static int mxf_read_cryptographic_context(MXFCryptoContext *cryptocontext, ByteIOContext *pb, int tag, int size, UID uid) |
374 |
{ |
375 |
if (size != 16) |
376 |
return -1; |
377 |
if (IS_KLV_KEY(uid, mxf_crypto_source_container_ul))
|
378 |
get_buffer(pb, cryptocontext->source_container_ul, 16);
|
379 |
return 0; |
380 |
} |
381 |
|
382 |
static int mxf_read_content_storage(MXFContext *mxf, ByteIOContext *pb, int tag) |
383 |
{ |
384 |
switch (tag) {
|
385 |
case 0x1901: |
386 |
mxf->packages_count = get_be32(pb); |
387 |
if (mxf->packages_count >= UINT_MAX / sizeof(UID)) |
388 |
return -1; |
389 |
mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
|
390 |
if (!mxf->packages_refs)
|
391 |
return -1; |
392 |
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */ |
393 |
get_buffer(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
|
394 |
break;
|
395 |
} |
396 |
return 0; |
397 |
} |
398 |
|
399 |
static int mxf_read_source_clip(MXFStructuralComponent *source_clip, ByteIOContext *pb, int tag) |
400 |
{ |
401 |
switch(tag) {
|
402 |
case 0x0202: |
403 |
source_clip->duration = get_be64(pb); |
404 |
break;
|
405 |
case 0x1201: |
406 |
source_clip->start_position = get_be64(pb); |
407 |
break;
|
408 |
case 0x1101: |
409 |
/* UMID, only get last 16 bytes */
|
410 |
url_fskip(pb, 16);
|
411 |
get_buffer(pb, source_clip->source_package_uid, 16);
|
412 |
break;
|
413 |
case 0x1102: |
414 |
source_clip->source_track_id = get_be32(pb); |
415 |
break;
|
416 |
} |
417 |
return 0; |
418 |
} |
419 |
|
420 |
static int mxf_read_material_package(MXFPackage *package, ByteIOContext *pb, int tag) |
421 |
{ |
422 |
switch(tag) {
|
423 |
case 0x4403: |
424 |
package->tracks_count = get_be32(pb); |
425 |
if (package->tracks_count >= UINT_MAX / sizeof(UID)) |
426 |
return -1; |
427 |
package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
|
428 |
if (!package->tracks_refs)
|
429 |
return -1; |
430 |
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */ |
431 |
get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
|
432 |
break;
|
433 |
} |
434 |
return 0; |
435 |
} |
436 |
|
437 |
static int mxf_read_track(MXFTrack *track, ByteIOContext *pb, int tag) |
438 |
{ |
439 |
switch(tag) {
|
440 |
case 0x4801: |
441 |
track->track_id = get_be32(pb); |
442 |
break;
|
443 |
case 0x4804: |
444 |
get_buffer(pb, track->track_number, 4);
|
445 |
break;
|
446 |
case 0x4B01: |
447 |
track->edit_rate.den = get_be32(pb); |
448 |
track->edit_rate.num = get_be32(pb); |
449 |
break;
|
450 |
case 0x4803: |
451 |
get_buffer(pb, track->sequence_ref, 16);
|
452 |
break;
|
453 |
} |
454 |
return 0; |
455 |
} |
456 |
|
457 |
static int mxf_read_sequence(MXFSequence *sequence, ByteIOContext *pb, int tag) |
458 |
{ |
459 |
switch(tag) {
|
460 |
case 0x0202: |
461 |
sequence->duration = get_be64(pb); |
462 |
break;
|
463 |
case 0x0201: |
464 |
get_buffer(pb, sequence->data_definition_ul, 16);
|
465 |
break;
|
466 |
case 0x1001: |
467 |
sequence->structural_components_count = get_be32(pb); |
468 |
if (sequence->structural_components_count >= UINT_MAX / sizeof(UID)) |
469 |
return -1; |
470 |
sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
|
471 |
if (!sequence->structural_components_refs)
|
472 |
return -1; |
473 |
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */ |
474 |
get_buffer(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
|
475 |
break;
|
476 |
} |
477 |
return 0; |
478 |
} |
479 |
|
480 |
static int mxf_read_source_package(MXFPackage *package, ByteIOContext *pb, int tag) |
481 |
{ |
482 |
switch(tag) {
|
483 |
case 0x4403: |
484 |
package->tracks_count = get_be32(pb); |
485 |
if (package->tracks_count >= UINT_MAX / sizeof(UID)) |
486 |
return -1; |
487 |
package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
|
488 |
if (!package->tracks_refs)
|
489 |
return -1; |
490 |
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */ |
491 |
get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
|
492 |
break;
|
493 |
case 0x4401: |
494 |
/* UMID, only get last 16 bytes */
|
495 |
url_fskip(pb, 16);
|
496 |
get_buffer(pb, package->package_uid, 16);
|
497 |
break;
|
498 |
case 0x4701: |
499 |
get_buffer(pb, package->descriptor_ref, 16);
|
500 |
break;
|
501 |
} |
502 |
return 0; |
503 |
} |
504 |
|
505 |
static int mxf_read_index_table_segment(MXFIndexTableSegment *segment, ByteIOContext *pb, int tag) |
506 |
{ |
507 |
switch(tag) {
|
508 |
case 0x3F05: dprintf(NULL, "EditUnitByteCount %d\n", get_be32(pb)); break; |
509 |
case 0x3F06: dprintf(NULL, "IndexSID %d\n", get_be32(pb)); break; |
510 |
case 0x3F07: dprintf(NULL, "BodySID %d\n", get_be32(pb)); break; |
511 |
case 0x3F0B: dprintf(NULL, "IndexEditRate %d/%d\n", get_be32(pb), get_be32(pb)); break; |
512 |
case 0x3F0C: dprintf(NULL, "IndexStartPosition %lld\n", get_be64(pb)); break; |
513 |
case 0x3F0D: dprintf(NULL, "IndexDuration %lld\n", get_be64(pb)); break; |
514 |
} |
515 |
return 0; |
516 |
} |
517 |
|
518 |
static void mxf_read_pixel_layout(ByteIOContext *pb, MXFDescriptor *descriptor) |
519 |
{ |
520 |
int code;
|
521 |
|
522 |
do {
|
523 |
code = get_byte(pb); |
524 |
dprintf(NULL, "pixel layout: code %#x\n", code); |
525 |
switch (code) {
|
526 |
case 0x52: /* R */ |
527 |
descriptor->bits_per_sample += get_byte(pb); |
528 |
break;
|
529 |
case 0x47: /* G */ |
530 |
descriptor->bits_per_sample += get_byte(pb); |
531 |
break;
|
532 |
case 0x42: /* B */ |
533 |
descriptor->bits_per_sample += get_byte(pb); |
534 |
break;
|
535 |
default:
|
536 |
get_byte(pb); |
537 |
} |
538 |
} while (code != 0); /* SMPTE 377M E.2.46 */ |
539 |
} |
540 |
|
541 |
static int mxf_read_generic_descriptor(MXFDescriptor *descriptor, ByteIOContext *pb, int tag, int size, UID uid) |
542 |
{ |
543 |
switch(tag) {
|
544 |
case 0x3F01: |
545 |
descriptor->sub_descriptors_count = get_be32(pb); |
546 |
if (descriptor->sub_descriptors_count >= UINT_MAX / sizeof(UID)) |
547 |
return -1; |
548 |
descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
|
549 |
if (!descriptor->sub_descriptors_refs)
|
550 |
return -1; |
551 |
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */ |
552 |
get_buffer(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
|
553 |
break;
|
554 |
case 0x3004: |
555 |
get_buffer(pb, descriptor->essence_container_ul, 16);
|
556 |
break;
|
557 |
case 0x3006: |
558 |
descriptor->linked_track_id = get_be32(pb); |
559 |
break;
|
560 |
case 0x3201: /* PictureEssenceCoding */ |
561 |
get_buffer(pb, descriptor->essence_codec_ul, 16);
|
562 |
break;
|
563 |
case 0x3203: |
564 |
descriptor->width = get_be32(pb); |
565 |
break;
|
566 |
case 0x3202: |
567 |
descriptor->height = get_be32(pb); |
568 |
break;
|
569 |
case 0x320E: |
570 |
descriptor->aspect_ratio.num = get_be32(pb); |
571 |
descriptor->aspect_ratio.den = get_be32(pb); |
572 |
break;
|
573 |
case 0x3D03: |
574 |
descriptor->sample_rate.num = get_be32(pb); |
575 |
descriptor->sample_rate.den = get_be32(pb); |
576 |
break;
|
577 |
case 0x3D06: /* SoundEssenceCompression */ |
578 |
get_buffer(pb, descriptor->essence_codec_ul, 16);
|
579 |
break;
|
580 |
case 0x3D07: |
581 |
descriptor->channels = get_be32(pb); |
582 |
break;
|
583 |
case 0x3D01: |
584 |
descriptor->bits_per_sample = get_be32(pb); |
585 |
break;
|
586 |
case 0x3401: |
587 |
mxf_read_pixel_layout(pb, descriptor); |
588 |
break;
|
589 |
default:
|
590 |
/* Private uid used by SONY C0023S01.mxf */
|
591 |
if (IS_KLV_KEY(uid, mxf_sony_mpeg4_extradata)) {
|
592 |
descriptor->extradata = av_malloc(size); |
593 |
if (!descriptor->extradata)
|
594 |
return -1; |
595 |
descriptor->extradata_size = size; |
596 |
get_buffer(pb, descriptor->extradata, size); |
597 |
} |
598 |
break;
|
599 |
} |
600 |
return 0; |
601 |
} |
602 |
|
603 |
/*
|
604 |
* Match an uid independently of the version byte and up to len common bytes
|
605 |
* Returns: boolean
|
606 |
*/
|
607 |
static int mxf_match_uid(const UID key, const UID uid, int len) |
608 |
{ |
609 |
int i;
|
610 |
for (i = 0; i < len; i++) { |
611 |
if (i != 7 && key[i] != uid[i]) |
612 |
return 0; |
613 |
} |
614 |
return 1; |
615 |
} |
616 |
|
617 |
static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid) |
618 |
{ |
619 |
while (uls->id != CODEC_ID_NONE) {
|
620 |
if(mxf_match_uid(uls->uid, *uid, uls->matching_len))
|
621 |
break;
|
622 |
uls++; |
623 |
} |
624 |
return uls;
|
625 |
} |
626 |
|
627 |
static enum CodecType mxf_get_codec_type(const MXFDataDefinitionUL *uls, UID *uid) |
628 |
{ |
629 |
while (uls->type != CODEC_TYPE_DATA) {
|
630 |
if(mxf_match_uid(uls->uid, *uid, 16)) |
631 |
break;
|
632 |
uls++; |
633 |
} |
634 |
return uls->type;
|
635 |
} |
636 |
|
637 |
static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref, enum MXFMetadataSetType type) |
638 |
{ |
639 |
int i;
|
640 |
|
641 |
if (!strong_ref)
|
642 |
return NULL; |
643 |
for (i = 0; i < mxf->metadata_sets_count; i++) { |
644 |
if (!memcmp(*strong_ref, mxf->metadata_sets[i]->uid, 16) && |
645 |
(type == AnyType || mxf->metadata_sets[i]->type == type)) { |
646 |
return mxf->metadata_sets[i];
|
647 |
} |
648 |
} |
649 |
return NULL; |
650 |
} |
651 |
|
652 |
static const MXFCodecUL mxf_essence_container_uls[] = { |
653 |
// video essence container uls
|
654 |
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 }, 14, CODEC_ID_MPEG2VIDEO }, /* MPEG-ES Frame wrapped */ |
655 |
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, 14, CODEC_ID_DVVIDEO }, /* DV 625 25mbps */ |
656 |
// sound essence container uls
|
657 |
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 }, 14, CODEC_ID_PCM_S16LE }, /* BWF Frame wrapped */ |
658 |
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, 14, CODEC_ID_MP2 }, /* MPEG-ES Frame wrapped, 0x40 ??? stream id */ |
659 |
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, 14, CODEC_ID_PCM_S16LE }, /* D-10 Mapping 50Mbps PAL Extended Template */ |
660 |
{ { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, 0, CODEC_ID_NONE }, |
661 |
}; |
662 |
|
663 |
static int mxf_parse_structural_metadata(MXFContext *mxf) |
664 |
{ |
665 |
MXFPackage *material_package = NULL;
|
666 |
MXFPackage *temp_package = NULL;
|
667 |
int i, j, k;
|
668 |
|
669 |
dprintf(mxf->fc, "metadata sets count %d\n", mxf->metadata_sets_count);
|
670 |
/* TODO: handle multiple material packages (OP3x) */
|
671 |
for (i = 0; i < mxf->packages_count; i++) { |
672 |
material_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[i], MaterialPackage); |
673 |
if (material_package) break; |
674 |
} |
675 |
if (!material_package) {
|
676 |
av_log(mxf->fc, AV_LOG_ERROR, "no material package found\n");
|
677 |
return -1; |
678 |
} |
679 |
|
680 |
for (i = 0; i < material_package->tracks_count; i++) { |
681 |
MXFPackage *source_package = NULL;
|
682 |
MXFTrack *material_track = NULL;
|
683 |
MXFTrack *source_track = NULL;
|
684 |
MXFTrack *temp_track = NULL;
|
685 |
MXFDescriptor *descriptor = NULL;
|
686 |
MXFStructuralComponent *component = NULL;
|
687 |
UID *essence_container_ul = NULL;
|
688 |
const MXFCodecUL *codec_ul = NULL; |
689 |
const MXFCodecUL *container_ul = NULL; |
690 |
AVStream *st; |
691 |
|
692 |
if (!(material_track = mxf_resolve_strong_ref(mxf, &material_package->tracks_refs[i], Track))) {
|
693 |
av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track strong ref\n");
|
694 |
continue;
|
695 |
} |
696 |
|
697 |
if (!(material_track->sequence = mxf_resolve_strong_ref(mxf, &material_track->sequence_ref, Sequence))) {
|
698 |
av_log(mxf->fc, AV_LOG_ERROR, "could not resolve material track sequence strong ref\n");
|
699 |
return -1; |
700 |
} |
701 |
|
702 |
/* TODO: handle multiple source clips */
|
703 |
for (j = 0; j < material_track->sequence->structural_components_count; j++) { |
704 |
/* TODO: handle timecode component */
|
705 |
component = mxf_resolve_strong_ref(mxf, &material_track->sequence->structural_components_refs[j], SourceClip); |
706 |
if (!component)
|
707 |
continue;
|
708 |
|
709 |
for (k = 0; k < mxf->packages_count; k++) { |
710 |
temp_package = mxf_resolve_strong_ref(mxf, &mxf->packages_refs[k], SourcePackage); |
711 |
if (!temp_package)
|
712 |
continue;
|
713 |
if (!memcmp(temp_package->package_uid, component->source_package_uid, 16)) { |
714 |
source_package = temp_package; |
715 |
break;
|
716 |
} |
717 |
} |
718 |
if (!source_package) {
|
719 |
av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source package found\n", material_track->track_id);
|
720 |
break;
|
721 |
} |
722 |
for (k = 0; k < source_package->tracks_count; k++) { |
723 |
if (!(temp_track = mxf_resolve_strong_ref(mxf, &source_package->tracks_refs[k], Track))) {
|
724 |
av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track strong ref\n");
|
725 |
return -1; |
726 |
} |
727 |
if (temp_track->track_id == component->source_track_id) {
|
728 |
source_track = temp_track; |
729 |
break;
|
730 |
} |
731 |
} |
732 |
if (!source_track) {
|
733 |
av_log(mxf->fc, AV_LOG_ERROR, "material track %d: no corresponding source track found\n", material_track->track_id);
|
734 |
break;
|
735 |
} |
736 |
} |
737 |
if (!source_track)
|
738 |
continue;
|
739 |
|
740 |
st = av_new_stream(mxf->fc, source_track->track_id); |
741 |
if (!st) {
|
742 |
av_log(mxf->fc, AV_LOG_ERROR, "could not allocate stream\n");
|
743 |
return -1; |
744 |
} |
745 |
st->priv_data = source_track; |
746 |
st->duration = component->duration; |
747 |
if (st->duration == -1) |
748 |
st->duration = AV_NOPTS_VALUE; |
749 |
st->start_time = component->start_position; |
750 |
av_set_pts_info(st, 64, material_track->edit_rate.num, material_track->edit_rate.den);
|
751 |
|
752 |
if (!(source_track->sequence = mxf_resolve_strong_ref(mxf, &source_track->sequence_ref, Sequence))) {
|
753 |
av_log(mxf->fc, AV_LOG_ERROR, "could not resolve source track sequence strong ref\n");
|
754 |
return -1; |
755 |
} |
756 |
|
757 |
PRINT_KEY(mxf->fc, "data definition ul", source_track->sequence->data_definition_ul);
|
758 |
st->codec->codec_type = mxf_get_codec_type(ff_mxf_data_definition_uls, &source_track->sequence->data_definition_ul); |
759 |
|
760 |
source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref, AnyType); |
761 |
if (source_package->descriptor) {
|
762 |
if (source_package->descriptor->type == MultipleDescriptor) {
|
763 |
for (j = 0; j < source_package->descriptor->sub_descriptors_count; j++) { |
764 |
MXFDescriptor *sub_descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor->sub_descriptors_refs[j], Descriptor); |
765 |
|
766 |
if (!sub_descriptor) {
|
767 |
av_log(mxf->fc, AV_LOG_ERROR, "could not resolve sub descriptor strong ref\n");
|
768 |
continue;
|
769 |
} |
770 |
if (sub_descriptor->linked_track_id == source_track->track_id) {
|
771 |
descriptor = sub_descriptor; |
772 |
break;
|
773 |
} |
774 |
} |
775 |
} else if (source_package->descriptor->type == Descriptor) |
776 |
descriptor = source_package->descriptor; |
777 |
} |
778 |
if (!descriptor) {
|
779 |
av_log(mxf->fc, AV_LOG_INFO, "source track %d: stream %d, no descriptor found\n", source_track->track_id, st->index);
|
780 |
continue;
|
781 |
} |
782 |
PRINT_KEY(mxf->fc, "essence codec ul", descriptor->essence_codec_ul);
|
783 |
PRINT_KEY(mxf->fc, "essence container ul", descriptor->essence_container_ul);
|
784 |
essence_container_ul = &descriptor->essence_container_ul; |
785 |
/* HACK: replacing the original key with mxf_encrypted_essence_container
|
786 |
* is not allowed according to s429-6, try to find correct information anyway */
|
787 |
if (IS_KLV_KEY(essence_container_ul, mxf_encrypted_essence_container)) {
|
788 |
av_log(mxf->fc, AV_LOG_INFO, "broken encrypted mxf file\n");
|
789 |
for (k = 0; k < mxf->metadata_sets_count; k++) { |
790 |
MXFMetadataSet *metadata = mxf->metadata_sets[k]; |
791 |
if (metadata->type == CryptoContext) {
|
792 |
essence_container_ul = &((MXFCryptoContext *)metadata)->source_container_ul; |
793 |
break;
|
794 |
} |
795 |
} |
796 |
} |
797 |
/* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
|
798 |
codec_ul = mxf_get_codec_ul(ff_mxf_codec_uls, &descriptor->essence_codec_ul); |
799 |
st->codec->codec_id = codec_ul->id; |
800 |
if (descriptor->extradata) {
|
801 |
st->codec->extradata = descriptor->extradata; |
802 |
st->codec->extradata_size = descriptor->extradata_size; |
803 |
} |
804 |
if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
|
805 |
container_ul = mxf_get_codec_ul(mxf_essence_container_uls, essence_container_ul); |
806 |
if (st->codec->codec_id == CODEC_ID_NONE)
|
807 |
st->codec->codec_id = container_ul->id; |
808 |
st->codec->width = descriptor->width; |
809 |
st->codec->height = descriptor->height; |
810 |
st->codec->bits_per_coded_sample = descriptor->bits_per_sample; /* Uncompressed */
|
811 |
st->need_parsing = AVSTREAM_PARSE_HEADERS; |
812 |
} else if (st->codec->codec_type == CODEC_TYPE_AUDIO) { |
813 |
container_ul = mxf_get_codec_ul(mxf_essence_container_uls, essence_container_ul); |
814 |
if (st->codec->codec_id == CODEC_ID_NONE)
|
815 |
st->codec->codec_id = container_ul->id; |
816 |
st->codec->channels = descriptor->channels; |
817 |
st->codec->bits_per_coded_sample = descriptor->bits_per_sample; |
818 |
st->codec->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den; |
819 |
/* TODO: implement CODEC_ID_RAWAUDIO */
|
820 |
if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
|
821 |
if (descriptor->bits_per_sample == 24) |
822 |
st->codec->codec_id = CODEC_ID_PCM_S24LE; |
823 |
else if (descriptor->bits_per_sample == 32) |
824 |
st->codec->codec_id = CODEC_ID_PCM_S32LE; |
825 |
} else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) { |
826 |
if (descriptor->bits_per_sample == 24) |
827 |
st->codec->codec_id = CODEC_ID_PCM_S24BE; |
828 |
else if (descriptor->bits_per_sample == 32) |
829 |
st->codec->codec_id = CODEC_ID_PCM_S32BE; |
830 |
} else if (st->codec->codec_id == CODEC_ID_MP2) { |
831 |
st->need_parsing = AVSTREAM_PARSE_FULL; |
832 |
} |
833 |
} |
834 |
if (st->codec->codec_type != CODEC_TYPE_DATA && (*essence_container_ul)[15] > 0x01) { |
835 |
av_log(mxf->fc, AV_LOG_WARNING, "only frame wrapped mappings are correctly supported\n");
|
836 |
st->need_parsing = AVSTREAM_PARSE_FULL; |
837 |
} |
838 |
} |
839 |
return 0; |
840 |
} |
841 |
|
842 |
static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = { |
843 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x05,0x01,0x00 }, mxf_read_primer_pack }, |
844 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x18,0x00 }, mxf_read_content_storage, 0, AnyType }, |
845 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x37,0x00 }, mxf_read_source_package, sizeof(MXFPackage), SourcePackage }, |
846 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x36,0x00 }, mxf_read_material_package, sizeof(MXFPackage), MaterialPackage }, |
847 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x0F,0x00 }, mxf_read_sequence, sizeof(MXFSequence), Sequence }, |
848 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x11,0x00 }, mxf_read_source_clip, sizeof(MXFStructuralComponent), SourceClip }, |
849 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x44,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), MultipleDescriptor }, |
850 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x42,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Generic Sound */ |
851 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x28,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* CDCI */ |
852 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x29,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* RGBA */ |
853 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* MPEG 2 Video */ |
854 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* Wave */ |
855 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 }, mxf_read_generic_descriptor, sizeof(MXFDescriptor), Descriptor }, /* AES3 */ |
856 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3A,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Static Track */ |
857 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x3B,0x00 }, mxf_read_track, sizeof(MXFTrack), Track }, /* Generic Track */ |
858 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x04,0x01,0x02,0x02,0x00,0x00 }, mxf_read_cryptographic_context, sizeof(MXFCryptoContext), CryptoContext }, |
859 |
{ { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 }, mxf_read_index_table_segment, sizeof(MXFIndexTableSegment), IndexTableSegment }, |
860 |
{ { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL, 0, AnyType }, |
861 |
}; |
862 |
|
863 |
static int mxf_read_local_tags(MXFContext *mxf, KLVPacket *klv, int (*read_child)(), int ctx_size, enum MXFMetadataSetType type) |
864 |
{ |
865 |
ByteIOContext *pb = mxf->fc->pb; |
866 |
MXFMetadataSet *ctx = ctx_size ? av_mallocz(ctx_size) : mxf; |
867 |
uint64_t klv_end = url_ftell(pb) + klv->length; |
868 |
|
869 |
if (!ctx)
|
870 |
return -1; |
871 |
while (url_ftell(pb) + 4 < klv_end) { |
872 |
int tag = get_be16(pb);
|
873 |
int size = get_be16(pb); /* KLV specified by 0x53 */ |
874 |
uint64_t next = url_ftell(pb) + size; |
875 |
UID uid = {0};
|
876 |
|
877 |
dprintf(mxf->fc, "local tag %#04x size %d\n", tag, size);
|
878 |
if (!size) { /* ignore empty tag, needed for some files with empty UMID tag */ |
879 |
av_log(mxf->fc, AV_LOG_ERROR, "local tag %#04x with 0 size\n", tag);
|
880 |
continue;
|
881 |
} |
882 |
if (tag > 0x7FFF) { /* dynamic tag */ |
883 |
int i;
|
884 |
for (i = 0; i < mxf->local_tags_count; i++) { |
885 |
int local_tag = AV_RB16(mxf->local_tags+i*18); |
886 |
if (local_tag == tag) {
|
887 |
memcpy(uid, mxf->local_tags+i*18+2, 16); |
888 |
dprintf(mxf->fc, "local tag %#04x\n", local_tag);
|
889 |
PRINT_KEY(mxf->fc, "uid", uid);
|
890 |
} |
891 |
} |
892 |
} |
893 |
if (ctx_size && tag == 0x3C0A) |
894 |
get_buffer(pb, ctx->uid, 16);
|
895 |
else if (read_child(ctx, pb, tag, size, uid) < 0) |
896 |
return -1; |
897 |
|
898 |
url_fseek(pb, next, SEEK_SET); |
899 |
} |
900 |
if (ctx_size) ctx->type = type;
|
901 |
return ctx_size ? mxf_add_metadata_set(mxf, ctx) : 0; |
902 |
} |
903 |
|
904 |
static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap) |
905 |
{ |
906 |
MXFContext *mxf = s->priv_data; |
907 |
KLVPacket klv; |
908 |
|
909 |
if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) { |
910 |
av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
|
911 |
return -1; |
912 |
} |
913 |
url_fseek(s->pb, -14, SEEK_CUR);
|
914 |
mxf->fc = s; |
915 |
while (!url_feof(s->pb)) {
|
916 |
const MXFMetadataReadTableEntry *metadata;
|
917 |
|
918 |
if (klv_read_packet(&klv, s->pb) < 0) |
919 |
return -1; |
920 |
PRINT_KEY(s, "read header", klv.key);
|
921 |
dprintf(s, "size %lld offset %#llx\n", klv.length, klv.offset);
|
922 |
if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||
|
923 |
IS_KLV_KEY(klv.key, mxf_essence_element_key)) { |
924 |
/* FIXME avoid seek */
|
925 |
url_fseek(s->pb, klv.offset, SEEK_SET); |
926 |
break;
|
927 |
} |
928 |
|
929 |
for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {
|
930 |
if (IS_KLV_KEY(klv.key, metadata->key)) {
|
931 |
int (*read)() = klv.key[5] == 0x53 ? mxf_read_local_tags : metadata->read; |
932 |
if (read(mxf, &klv, metadata->read, metadata->ctx_size, metadata->type) < 0) { |
933 |
av_log(s, AV_LOG_ERROR, "error reading header metadata\n");
|
934 |
return -1; |
935 |
} |
936 |
break;
|
937 |
} |
938 |
} |
939 |
if (!metadata->read)
|
940 |
url_fskip(s->pb, klv.length); |
941 |
} |
942 |
return mxf_parse_structural_metadata(mxf);
|
943 |
} |
944 |
|
945 |
static int mxf_read_close(AVFormatContext *s) |
946 |
{ |
947 |
MXFContext *mxf = s->priv_data; |
948 |
int i;
|
949 |
|
950 |
av_freep(&mxf->packages_refs); |
951 |
for (i = 0; i < mxf->metadata_sets_count; i++) { |
952 |
switch (mxf->metadata_sets[i]->type) {
|
953 |
case MultipleDescriptor:
|
954 |
av_freep(&((MXFDescriptor *)mxf->metadata_sets[i])->sub_descriptors_refs); |
955 |
break;
|
956 |
case Sequence:
|
957 |
av_freep(&((MXFSequence *)mxf->metadata_sets[i])->structural_components_refs); |
958 |
break;
|
959 |
case SourcePackage:
|
960 |
case MaterialPackage:
|
961 |
av_freep(&((MXFPackage *)mxf->metadata_sets[i])->tracks_refs); |
962 |
break;
|
963 |
case Track:
|
964 |
mxf->metadata_sets[i] = NULL; /* will be freed later */ |
965 |
break;
|
966 |
default:
|
967 |
break;
|
968 |
} |
969 |
av_freep(&mxf->metadata_sets[i]); |
970 |
} |
971 |
av_freep(&mxf->metadata_sets); |
972 |
av_freep(&mxf->aesc); |
973 |
av_freep(&mxf->local_tags); |
974 |
return 0; |
975 |
} |
976 |
|
977 |
static int mxf_probe(AVProbeData *p) { |
978 |
uint8_t *bufp = p->buf; |
979 |
uint8_t *end = p->buf + p->buf_size; |
980 |
|
981 |
if (p->buf_size < sizeof(mxf_header_partition_pack_key)) |
982 |
return 0; |
983 |
|
984 |
/* Must skip Run-In Sequence and search for MXF header partition pack key SMPTE 377M 5.5 */
|
985 |
end -= sizeof(mxf_header_partition_pack_key);
|
986 |
for (; bufp < end; bufp++) {
|
987 |
if (IS_KLV_KEY(bufp, mxf_header_partition_pack_key))
|
988 |
return AVPROBE_SCORE_MAX;
|
989 |
} |
990 |
return 0; |
991 |
} |
992 |
|
993 |
/* rudimentary byte seek */
|
994 |
/* XXX: use MXF Index */
|
995 |
static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags) |
996 |
{ |
997 |
AVStream *st = s->streams[stream_index]; |
998 |
int64_t seconds; |
999 |
|
1000 |
if (!s->bit_rate)
|
1001 |
return -1; |
1002 |
if (sample_time < 0) |
1003 |
sample_time = 0;
|
1004 |
seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den); |
1005 |
url_fseek(s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET);
|
1006 |
av_update_cur_dts(s, st, sample_time); |
1007 |
return 0; |
1008 |
} |
1009 |
|
1010 |
AVInputFormat mxf_demuxer = { |
1011 |
"mxf",
|
1012 |
NULL_IF_CONFIG_SMALL("Material eXchange Format"),
|
1013 |
sizeof(MXFContext),
|
1014 |
mxf_probe, |
1015 |
mxf_read_header, |
1016 |
mxf_read_packet, |
1017 |
mxf_read_close, |
1018 |
mxf_read_seek, |
1019 |
}; |