* MXF demuxer.
* Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
*
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
*
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
* Only tracks with associated descriptors will be decoded. "Highly Desirable" SMPTE 377M D.1
*/
-#define DEBUG
+//#define DEBUG
#include "avformat.h"
UID *sub_descriptors_refs;
int sub_descriptors_count;
int linked_track_id;
+ uint8_t *extradata;
+ int extradata_size;
} MXFDescriptor;
typedef struct MXFPackage {
enum MXFWrappingScheme wrapping;
} MXFCodecUL;
+typedef struct MXFDataDefinitionUL {
+ UID uid;
+ enum CodecType type;
+} MXFDataDefinitionUL;
+
typedef struct MXFMetadataReadTableEntry {
const UID key;
int (*read)(MXFContext *mxf, KLVPacket *klv);
#define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
-#define PRINT_KEY(x) dprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", \
+#define PRINT_KEY(s, x) dprintf("%s %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n", s, \
(x)[0], (x)[1], (x)[2], (x)[3], (x)[4], (x)[5], (x)[6], (x)[7], (x)[8], (x)[9], (x)[10], (x)[11], (x)[12], (x)[13], (x)[14], (x)[15])
static int64_t klv_decode_ber_length(ByteIOContext *pb)
if (!memcmp(klv->key + sizeof(mxf_essence_element_key), track->track_number, sizeof(track->track_number)))
return i;
}
- return -1;
+ /* return 0 if only one stream, for OP Atom files with 0 as track number */
+ return s->nb_streams == 1 ? 0 : -1;
+}
+
+/* XXX: use AVBitStreamFilter */
+static int mxf_get_d10_aes3_packet(ByteIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length)
+{
+ uint8_t buffer[61444];
+ uint8_t *buf_ptr, *end_ptr, *data_ptr;
+
+ if (length > 61444) /* worst case PAL 1920 samples 8 channels */
+ return -1;
+ get_buffer(pb, buffer, length);
+ av_new_packet(pkt, length);
+ data_ptr = pkt->data;
+ end_ptr = buffer + length;
+ buf_ptr = buffer + 4; /* skip SMPTE 331M header */
+ for (; buf_ptr < end_ptr; buf_ptr += 4) {
+ if (st->codec->bits_per_sample == 24) {
+ data_ptr[0] = (buf_ptr[2] >> 4) | ((buf_ptr[3] & 0x0f) << 4);
+ data_ptr[1] = (buf_ptr[1] >> 4) | ((buf_ptr[2] & 0x0f) << 4);
+ data_ptr[2] = (buf_ptr[0] >> 4) | ((buf_ptr[1] & 0x0f) << 4);
+ data_ptr += 3;
+ } else {
+ data_ptr[0] = (buf_ptr[2] >> 4) | ((buf_ptr[3] & 0x0f) << 4);
+ data_ptr[1] = (buf_ptr[1] >> 4) | ((buf_ptr[2] & 0x0f) << 4);
+ data_ptr += 2;
+ }
+ }
+ pkt->size = data_ptr - pkt->data;
+ return 0;
}
static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
return -1;
}
#ifdef DEBUG
- PRINT_KEY(klv.key);
+ PRINT_KEY("read packet", klv.key);
#endif
if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
- av_get_packet(&s->pb, pkt, klv.length);
- pkt->stream_index = mxf_get_stream_index(s, &klv);
- return pkt->stream_index == -1 ? -1 : 0;
+ int index = mxf_get_stream_index(s, &klv);
+ if (index < 0) {
+ av_log(s, AV_LOG_ERROR, "error getting stream index\n");
+ url_fskip(&s->pb, klv.length);
+ return -1;
+ }
+ /* check for 8 channels AES3 element */
+ if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
+ if (mxf_get_d10_aes3_packet(&s->pb, s->streams[index], pkt, klv.length) < 0) {
+ av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
+ return -1;
+ }
+ } else
+ av_get_packet(&s->pb, pkt, klv.length);
+ pkt->stream_index = index;
+ return 0;
} else
url_fskip(&s->pb, klv.length);
}
return -1;
mxf->essence_containers_uls = av_malloc(mxf->essence_containers_uls_count * sizeof(UID));
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, mxf->essence_containers_uls, mxf->essence_containers_uls_count * sizeof(UID));
+ get_buffer(pb, (uint8_t *)mxf->essence_containers_uls, mxf->essence_containers_uls_count * sizeof(UID));
break;
default:
url_fskip(pb, size);
return -1;
mxf->packages_refs = av_malloc(mxf->packages_count * sizeof(UID));
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, mxf->packages_refs, mxf->packages_count * sizeof(UID));
+ get_buffer(pb, (uint8_t *)mxf->packages_refs, mxf->packages_count * sizeof(UID));
break;
case 0x1902:
mxf->essence_container_data_sets_count = get_be32(pb);
return -1;
mxf->essence_container_data_sets_refs = av_malloc(mxf->essence_container_data_sets_count * sizeof(UID));
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, mxf->essence_container_data_sets_refs, mxf->essence_container_data_sets_count * sizeof(UID));
+ get_buffer(pb, (uint8_t *)mxf->essence_container_data_sets_refs, mxf->essence_container_data_sets_count * sizeof(UID));
break;
default:
url_fskip(pb, size);
int tag = get_be16(pb);
int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
+ bytes_read += size + 4;
dprintf("tag 0x%04X, size %d\n", tag, size);
+ if (!size) { /* ignore empty tag, needed for some files with empty UMID tag */
+ av_log(mxf->fc, AV_LOG_ERROR, "local tag 0x%04X with 0 size\n", tag);
+ continue;
+ }
switch (tag) {
case 0x3C0A:
get_buffer(pb, source_clip->uid, 16);
default:
url_fskip(pb, size);
}
- bytes_read += size + 4;
}
source_clip->type = SourceClip;
return mxf_add_metadata_set(mxf, source_clip);
return -1;
package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, package->tracks_refs, package->tracks_count * sizeof(UID));
+ get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
break;
default:
url_fskip(pb, size);
return -1;
sequence->structural_components_refs = av_malloc(sequence->structural_components_count * sizeof(UID));
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
+ get_buffer(pb, (uint8_t *)sequence->structural_components_refs, sequence->structural_components_count * sizeof(UID));
break;
default:
url_fskip(pb, size);
return -1;
package->tracks_refs = av_malloc(package->tracks_count * sizeof(UID));
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, package->tracks_refs, package->tracks_count * sizeof(UID));
+ get_buffer(pb, (uint8_t *)package->tracks_refs, package->tracks_count * sizeof(UID));
break;
case 0x4401:
/* UMID, only get last 16 bytes */
return -1;
descriptor->sub_descriptors_refs = av_malloc(descriptor->sub_descriptors_count * sizeof(UID));
url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
- get_buffer(pb, descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
+ get_buffer(pb, (uint8_t *)descriptor->sub_descriptors_refs, descriptor->sub_descriptors_count * sizeof(UID));
break;
default:
url_fskip(pb, size);
case 0x3401:
mxf_read_metadata_pixel_layout(pb, descriptor);
break;
+ case 0x8201: /* Private tag used by SONY C0023S01.mxf */
+ descriptor->extradata = av_malloc(size);
+ descriptor->extradata_size = size;
+ get_buffer(pb, descriptor->extradata, size);
+ break;
default:
url_fskip(pb, size);
}
}
/* SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */
-static const UID picture_essence_track_ul = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x01,0x00,0x00,0x00 };
-static const UID sound_essence_track_ul = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x02,0x00,0x00,0x00 };
+static const MXFDataDefinitionUL mxf_data_definition_uls[] = {
+ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x01,0x00,0x00,0x00 }, CODEC_TYPE_VIDEO },
+ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x02,0x00,0x00,0x00 }, CODEC_TYPE_AUDIO },
+ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x05,0x01,0x03,0x02,0x02,0x02,0x02,0x00,0x00 }, CODEC_TYPE_AUDIO },
+ { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, CODEC_TYPE_DATA },
+};
static const MXFCodecUL mxf_codec_uls[] = {
/* PictureEssenceCoding */
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 }, CODEC_ID_DVVIDEO, Frame }, /* DVCPRO50 PAL */
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 }, CODEC_ID_DVVIDEO, Frame }, /* DVCPRO25 PAL */
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 }, CODEC_ID_DVVIDEO, Frame }, /* DV25 IEC PAL */
- //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 }, CODEC_ID_JPEG2K, Frame }, /* JPEG2000 Codestream */
+ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 }, CODEC_ID_JPEG2000, Frame }, /* JPEG2000 Codestream */
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 }, CODEC_ID_RAWVIDEO, Frame }, /* Uncompressed */
/* SoundEssenceCompression */
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, CODEC_ID_PCM_S16LE, Frame }, /* Uncompressed */
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x40,0x01 }, CODEC_ID_MP2, Frame }, /* MPEG-ES Frame wrapped, 0x40 ??? stream id */
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0xc0,0x01 }, CODEC_ID_MP2, Frame }, /* MPEG-ES Frame wrapped, 0xc0 MPA stream id */
{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0xc0,0x02 }, CODEC_ID_MP2, Clip }, /* MPEG-ES Clip wrapped, 0xc0 MPA stream id */
- //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 }, CODEC_ID_PCM_AES3, Frame }, /* D-10 Mapping 30Mbps PAL Extended Template */
+ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 }, CODEC_ID_PCM_S16BE, Frame }, /* D-10 Mapping 30Mbps PAL Extended Template */
+ { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, CODEC_ID_PCM_S16BE, Frame }, /* D-10 Mapping 50Mbps PAL Extended Template */
{ { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, CODEC_ID_NONE, Frame },
};
return uls;
}
+static enum CodecType mxf_get_codec_type(const MXFDataDefinitionUL *uls, UID *uid)
+{
+ while (uls->type != CODEC_TYPE_DATA) {
+ if(!memcmp(uls->uid, *uid, 16))
+ break;
+ uls++;
+ }
+ return uls->type;
+}
+
static void *mxf_resolve_strong_ref(MXFContext *mxf, UID *strong_ref)
{
int i;
}
#ifdef DEBUG
- PRINT_KEY(source_track->sequence->data_definition_ul);
+ PRINT_KEY("data definition ul", source_track->sequence->data_definition_ul);
#endif
- if (!memcmp(source_track->sequence->data_definition_ul, picture_essence_track_ul, 16))
- st->codec->codec_type = CODEC_TYPE_VIDEO;
- else if (!memcmp(source_track->sequence->data_definition_ul, sound_essence_track_ul, 16))
- st->codec->codec_type = CODEC_TYPE_AUDIO;
- else
- st->codec->codec_type = CODEC_TYPE_DATA;
+ st->codec->codec_type = mxf_get_codec_type(mxf_data_definition_uls, &source_track->sequence->data_definition_ul);
source_package->descriptor = mxf_resolve_strong_ref(mxf, &source_package->descriptor_ref);
if (source_package->descriptor) {
continue;
}
#ifdef DEBUG
- PRINT_KEY(descriptor->essence_codec_ul);
- PRINT_KEY(descriptor->essence_container_ul);
+ PRINT_KEY("essence codec ul", descriptor->essence_codec_ul);
+ PRINT_KEY("essence container ul", descriptor->essence_container_ul);
#endif
/* TODO: drop PictureEssenceCoding and SoundEssenceCompression, only check EssenceContainer */
codec_ul = mxf_get_codec_ul(mxf_codec_uls, &descriptor->essence_codec_ul);
st->codec->codec_id = codec_ul->id;
+ if (descriptor->extradata) {
+ st->codec->extradata = descriptor->extradata;
+ st->codec->extradata_size = descriptor->extradata_size;
+ }
if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
container_ul = mxf_get_codec_ul(mxf_picture_essence_container_uls, &descriptor->essence_container_ul);
if (st->codec->codec_id == CODEC_ID_NONE)
st->codec->codec_id = CODEC_ID_PCM_S24BE;
else if (descriptor->bits_per_sample == 32)
st->codec->codec_id = CODEC_ID_PCM_S32BE;
+ if (descriptor->essence_container_ul[13] == 0x01) /* D-10 Mapping */
+ st->codec->channels = 8; /* force channels to 8 */
+ } else if (st->codec->codec_id == CODEC_ID_MP2) {
+ st->need_parsing = 1;
}
}
if (container_ul && container_ul->wrapping == Clip) {
{ { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, NULL },
};
+static int mxf_read_sync(ByteIOContext *pb, const uint8_t *key, unsigned size)
+{
+ int i, b;
+ for (i = 0; i < size && !url_feof(pb); i++) {
+ b = get_byte(pb);
+ if (b == key[0])
+ i = 0;
+ else if (b != key[i])
+ i = -1;
+ }
+ return i == size;
+}
+
static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
MXFContext *mxf = s->priv_data;
KLVPacket klv;
+ if (!mxf_read_sync(&s->pb, mxf_header_partition_pack_key, 14)) {
+ av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");
+ return -1;
+ }
+ url_fseek(&s->pb, -14, SEEK_CUR);
mxf->fc = s;
while (!url_feof(&s->pb)) {
const MXFMetadataReadTableEntry *function;
return -1;
}
#ifdef DEBUG
- PRINT_KEY(klv.key);
+ PRINT_KEY("read header", klv.key);
#endif
if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
/* FIXME avoid seek */
return 0;
}
+/* rudimentary binary seek */
+/* XXX: use MXF Index */
+static int mxf_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
+{
+ AVStream *st = s->streams[stream_index];
+ int64_t seconds;
+
+ if (!s->bit_rate)
+ return -1;
+ if (sample_time < 0)
+ sample_time = 0;
+ seconds = av_rescale(sample_time, st->time_base.num, st->time_base.den);
+ url_fseek(&s->pb, (s->bit_rate * seconds) >> 3, SEEK_SET);
+ if (!mxf_read_sync(&s->pb, mxf_essence_element_key, 12))
+ return -1;
+
+ /* found KLV key */
+ url_fseek(&s->pb, -12, SEEK_CUR);
+ av_update_cur_dts(s, st, sample_time);
+ return 0;
+}
AVInputFormat mxf_demuxer = {
"mxf",
mxf_read_header,
mxf_read_packet,
mxf_read_close,
- NULL,
+ mxf_read_seek,
};