]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mxf.c
simplify
[ffmpeg] / libavformat / mxf.c
index a01b1952a427ce4149648b15445daaae62a3b444..8591adb248011ecc27b9d208e87e11be399335fb 100644 (file)
@@ -2,18 +2,20 @@
  * 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
  */
 
@@ -41,7 +43,7 @@
  * Only tracks with associated descriptors will be decoded. "Highly Desirable" SMPTE 377M D.1
  */
 
-#define DEBUG
+//#define DEBUG
 
 #include "avformat.h"
 
@@ -102,6 +104,8 @@ typedef struct MXFDescriptor {
     UID *sub_descriptors_refs;
     int sub_descriptors_count;
     int linked_track_id;
+    uint8_t *extradata;
+    int extradata_size;
 } MXFDescriptor;
 
 typedef struct MXFPackage {
@@ -156,6 +160,11 @@ typedef struct MXFCodecUL {
     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);
@@ -167,7 +176,7 @@ static const uint8_t mxf_essence_element_key[]             = { 0x06,0x0e,0x2b,0x
 
 #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)
@@ -207,7 +216,37 @@ static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
         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)
@@ -220,12 +259,25 @@ 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);
     }
@@ -262,7 +314,7 @@ static int mxf_read_metadata_preface(MXFContext *mxf, KLVPacket *klv)
                 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);
@@ -289,7 +341,7 @@ static int mxf_read_metadata_content_storage(MXFContext *mxf, KLVPacket *klv)
                 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);
@@ -297,7 +349,7 @@ static int mxf_read_metadata_content_storage(MXFContext *mxf, KLVPacket *klv)
                 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);
@@ -317,7 +369,12 @@ static int mxf_read_metadata_source_clip(MXFContext *mxf, KLVPacket *klv)
         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);
@@ -339,7 +396,6 @@ static int mxf_read_metadata_source_clip(MXFContext *mxf, KLVPacket *klv)
         default:
             url_fskip(pb, size);
         }
-        bytes_read += size + 4;
     }
     source_clip->type = SourceClip;
     return mxf_add_metadata_set(mxf, source_clip);
@@ -365,7 +421,7 @@ static int mxf_read_metadata_material_package(MXFContext *mxf, KLVPacket *klv)
                 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);
@@ -440,7 +496,7 @@ static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
                 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);
@@ -472,7 +528,7 @@ static int mxf_read_metadata_source_package(MXFContext *mxf, KLVPacket *klv)
                 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 */
@@ -512,7 +568,7 @@ static int mxf_read_metadata_multiple_descriptor(MXFContext *mxf, KLVPacket *klv
                 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);
@@ -596,6 +652,11 @@ static int mxf_read_metadata_generic_descriptor(MXFContext *mxf, KLVPacket *klv)
         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);
         }
@@ -606,8 +667,12 @@ static int mxf_read_metadata_generic_descriptor(MXFContext *mxf, KLVPacket *klv)
 }
 
 /* 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 */
@@ -623,7 +688,7 @@ static const MXFCodecUL mxf_codec_uls[] = {
     { { 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 */
@@ -650,7 +715,8 @@ static const MXFCodecUL mxf_sound_essence_container_uls[] = {
     { { 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 },
 };
 
@@ -664,6 +730,16 @@ static const MXFCodecUL *mxf_get_codec_ul(const MXFCodecUL *uls, UID *uid)
     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;
@@ -775,14 +851,9 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
         }
 
 #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) {
@@ -807,12 +878,16 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
             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)
@@ -838,6 +913,10 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
                     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) {
@@ -867,11 +946,29 @@ static const MXFMetadataReadTableEntry mxf_metadata_read_table[] = {
     { { 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;
@@ -881,7 +978,7 @@ static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
             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 */
@@ -949,6 +1046,27 @@ static int mxf_probe(AVProbeData *p) {
     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",
@@ -958,5 +1076,5 @@ AVInputFormat mxf_demuxer = {
     mxf_read_header,
     mxf_read_packet,
     mxf_read_close,
-    NULL,
+    mxf_read_seek,
 };