]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mxf.c
Replace lena.pnm
[ffmpeg] / libavformat / mxf.c
index c0b209978114eea5f9d84fff101125beb7d911b7..19ee35f3afa49dae3ea43b6321d706d51dcd1908 100644 (file)
 /*
- * MXF demuxer.
- * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
+ * MXF
+ * 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 Libav.
+ *
+ * Libav 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,
+ * Libav 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 Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-/*
- * References
- * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
- * SMPTE 377M MXF File Format Specifications
- * SMPTE 378M Operational Pattern 1a
- * SMPTE 379M MXF Generic Container
- * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
- * SMPTE 382M Mapping AES3 and Broadcast Wave Audio into the MXF Generic Container
- * SMPTE 383M Mapping DV-DIF Data to the MXF Generic Container
- *
- * Principle
- * Search for Track numbers which will identify essence element KLV packets.
- * Search for SourcePackage which define tracks which contains Track numbers.
- * Material Package tracks does not contain Tracks numbers.
- * Search for Descriptors (Picture, Sound) which contains codec info and parameters.
- * Assign Descriptors to correct Tracks.
- *
- * Preliminary demuxer, only OP1A supported and some files might not work at all.
- */
-
-//#define DEBUG
-
-#include "avformat.h"
-#include "dsputil.h"
-#include "riff.h"
-
-typedef struct {
-    AVStream *stream;
-    uint8_t track_uid[16];
-    uint8_t sequence_uid[16];
-    int track_id;
-    int track_number;
-} MXFTrack;
-
-typedef struct {
-    DECLARE_ALIGNED_16(uint8_t, essence_container[16]);
-    DECLARE_ALIGNED_16(uint8_t, essence_compression[16]);
-    enum CodecType codec_type;
-    AVRational sample_rate;
-    AVRational aspect_ratio;
-    int width;
-    int height;
-    int channels;
-    int bits_per_sample;
-    int block_align;
-    int linked_track_id;
-    int kind;
-} MXFDescriptor;
-
-typedef struct {
-    AVFormatContext *fc;
-    MXFTrack *tracks;
-    MXFDescriptor *descriptors;
-    int descriptors_count;
-    int tracks_count;
-} MXFContext;
-
-typedef struct {
-    DECLARE_ALIGNED_16(uint8_t, key[16]);
-    offset_t offset;
-    uint64_t length;
-} KLVPacket;
+#include "libavutil/common.h"
+#include "mxf.h"
 
-static const uint8_t mxf_metadata_source_package_key[]           = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x37, 0x00 };
-static const uint8_t mxf_metadata_sequence_key[]                 = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0F, 0x00 };
-static const uint8_t mxf_metadata_generic_sound_descriptor_key[] = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x42, 0x00 };
-static const uint8_t mxf_metadata_cdci_descriptor_key[]          = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x28, 0x00 };
-static const uint8_t mxf_metadata_mpegvideo_descriptor_key[]     = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x51, 0x00 };
-static const uint8_t mxf_metadata_wave_descriptor_key[]          = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x48, 0x00 };
-static const uint8_t mxf_metadata_track_key[]                    = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x53, 0x01, 0x01, 0x0d, 0x01, 0x01, 0x01, 0x01, 0x01, 0x3b, 0x00 };
-static const uint8_t mxf_header_partition_pack_key[]             = { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01, 0x01, 0x02 };
-static const uint8_t mxf_essence_element_key[]                   = { 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x02, 0x01 };
-
-#define IS_KLV_KEY(x, y) (!memcmp(x, y, sizeof(y)))
-
-#define PRINT_KEY(x) \
-do { \
-    int iterpk; \
-    for (iterpk = 0; iterpk < 16; iterpk++) { \
-        av_log(NULL, AV_LOG_DEBUG, "%02X ", x[iterpk]); \
-    } \
-    av_log(NULL, AV_LOG_DEBUG, "\n"); \
-} while (0); \
-
-static int64_t klv_decode_ber_length(ByteIOContext *pb)
-{
-    int64_t size = 0;
-    uint8_t length = get_byte(pb);
-    int type = length >> 7;
+/**
+ * SMPTE RP224 http://www.smpte-ra.org/mdd/index.html
+ */
+const MXFCodecUL ff_mxf_data_definition_uls[] = {
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x01,0x00,0x00,0x00 }, 13, AVMEDIA_TYPE_VIDEO },
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x02,0x02,0x00,0x00,0x00 }, 13, AVMEDIA_TYPE_AUDIO },
+    { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  0,  AVMEDIA_TYPE_DATA },
+};
 
-    if (type) { /* long form */
-        int bytes_num = length & 0x7f;
-        /* SMPTE 379M 5.3.4 guarantee that bytes_num must not exceed 8 bytes */
-        if (bytes_num > 8)
-            return -1;
-        while (bytes_num--)
-            size = size << 8 | get_byte(pb);
-    } else {
-        size = length & 0x7f;
-    }
-    return size;
-}
+const MXFCodecUL ff_mxf_codec_uls[] = {
+    /* PictureEssenceCoding */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, 14, AV_CODEC_ID_MPEG2VIDEO }, /* MP@ML Long GoP */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, 14, AV_CODEC_ID_MPEG2VIDEO }, /* D-10 50Mbps PAL */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, 14, AV_CODEC_ID_MPEG2VIDEO }, /* MP@HL Long GoP */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, 14, AV_CODEC_ID_MPEG2VIDEO }, /* 422P@HL I-Frame */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x20,0x02,0x03 }, 14,      AV_CODEC_ID_MPEG4 }, /* XDCAM proxy_pal030926.mxf */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 }, 13,    AV_CODEC_ID_DVVIDEO }, /* DV25 IEC PAL */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 }, 14,   AV_CODEC_ID_JPEG2000 }, /* JPEG2000 Codestream */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13,   AV_CODEC_ID_RAWVIDEO }, /* Uncompressed */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }, 15,   AV_CODEC_ID_RAWVIDEO }, /* Uncompressed 422 8-bit */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x71,0x00,0x00,0x00 }, 13,      AV_CODEC_ID_DNXHD }, /* SMPTE VC-3/DNxHD */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x03,0x02,0x00,0x00 }, 14,      AV_CODEC_ID_DNXHD }, /* SMPTE VC-3/DNxHD */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x32,0x00,0x00 }, 14,       AV_CODEC_ID_H264 }, /* H.264/MPEG-4 AVC Intra */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x01,0x31,0x11,0x01 }, 14,       AV_CODEC_ID_H264 }, /* H.264/MPEG-4 AVC SPS/PPS in-band */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x01,0x01,0x02,0x02,0x00 }, 15,       AV_CODEC_ID_V210 }, /* V210 */
+    /* SoundEssenceCompression */
+    { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x03,0x04,0x02,0x02,0x02,0x03,0x03,0x01,0x00 }, 14,        AV_CODEC_ID_AAC }, /* MPEG2 AAC ADTS (legacy) */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, 13,  AV_CODEC_ID_PCM_S16LE }, /* Uncompressed */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x7F,0x00,0x00,0x00 }, 13,  AV_CODEC_ID_PCM_S16LE },
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x07,0x04,0x02,0x02,0x01,0x7E,0x00,0x00,0x00 }, 13,  AV_CODEC_ID_PCM_S16BE }, /* From Omneon MXF file */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x04,0x04,0x02,0x02,0x02,0x03,0x01,0x01,0x00 }, 15,   AV_CODEC_ID_PCM_ALAW }, /* XDCAM Proxy C0023S01.mxf */
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x01,0x00 }, 15,        AV_CODEC_ID_AC3 },
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x05,0x00 }, 15,        AV_CODEC_ID_MP2 }, /* MP2 or MP3 */
+  //{ { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x02,0x03,0x02,0x1C,0x00 }, 15,    AV_CODEC_ID_DOLBY_E }, /* Dolby-E */
+    { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  0,       AV_CODEC_ID_NONE },
+};
 
-static int klv_read_packet(KLVPacket *klv, ByteIOContext *pb)
-{
-    klv->offset = url_ftell(pb);
-    get_buffer(pb, klv->key, 16);
-    klv->length = klv_decode_ber_length(pb);
-    if (klv->length == -1)
-        return -1;
-    else
-        return 0;
-}
+const MXFCodecUL ff_mxf_pixel_format_uls[] = {
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x01,0x01,0x02,0x01,0x01 }, 16, AV_PIX_FMT_UYVY422 },
+    { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x01,0x01,0x02,0x01,0x02 }, 16, AV_PIX_FMT_YUYV422 },
+    { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },  0,    AV_PIX_FMT_NONE },
+};
 
-static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
-{
-    int id = BE_32(klv->key + 12); /* SMPTE 379M 7.3 */
-    int i;
+static const struct {
+    enum AVPixelFormat pix_fmt;
+    const char data[16];
+} ff_mxf_pixel_layouts[] = {
+    /**
+     * See SMPTE 377M E.2.46
+     *
+     * Note: Only RGB, palette based and "abnormal" YUV pixel formats like 4:2:2:4 go here.
+     *       For regular YUV, use CDCIPictureEssenceDescriptor.
+     *
+     * Note: Do not use these for encoding descriptors for little-endian formats until we
+     *       get samples or official word from SMPTE on how/if those can be encoded.
+     */
+    {AV_PIX_FMT_ABGR,    {'A', 8,  'B', 8,  'G', 8, 'R', 8                 }},
+    {AV_PIX_FMT_ARGB,    {'A', 8,  'R', 8,  'G', 8, 'B', 8                 }},
+    {AV_PIX_FMT_BGR24,   {'B', 8,  'G', 8,  'R', 8                         }},
+    {AV_PIX_FMT_BGRA,    {'B', 8,  'G', 8,  'R', 8, 'A', 8                 }},
+    {AV_PIX_FMT_RGB24,   {'R', 8,  'G', 8,  'B', 8                         }},
+    {AV_PIX_FMT_RGB444BE,{'F', 4,  'R', 4,  'G', 4, 'B', 4                 }},
+    {AV_PIX_FMT_RGB48BE, {'R', 8,  'r', 8,  'G', 8, 'g', 8, 'B', 8, 'b', 8 }},
+    {AV_PIX_FMT_RGB48BE, {'R', 16, 'G', 16, 'B', 16                        }},
+    {AV_PIX_FMT_RGB48LE, {'r', 8,  'R', 8,  'g', 8, 'G', 8, 'b', 8, 'B', 8 }},
+    {AV_PIX_FMT_RGB555BE,{'F', 1,  'R', 5,  'G', 5, 'B', 5                 }},
+    {AV_PIX_FMT_RGB565BE,{'R', 5,  'G', 6,  'B', 5                         }},
+    {AV_PIX_FMT_RGBA,    {'R', 8,  'G', 8,  'B', 8, 'A', 8                 }},
+    {AV_PIX_FMT_PAL8,    {'P', 8                                           }},
+};
 
-    for (i = 0; i < s->nb_streams; i++) {
-        if (s->streams[i]->id == id)
-            return i;
-    }
-    return -1;
-}
+static const int num_pixel_layouts = FF_ARRAY_ELEMS(ff_mxf_pixel_layouts);
 
-static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
+int ff_mxf_decode_pixel_layout(const char pixel_layout[16], enum AVPixelFormat *pix_fmt)
 {
-    KLVPacket klv;
+    int x;
 
-    while (!url_feof(&s->pb)) {
-        if (klv_read_packet(&klv, &s->pb) < 0)
-            return -1;
-        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);
-            if (pkt->stream_index == -1)
-                return -1;
+    for(x = 0; x < num_pixel_layouts; x++) {
+        if (!memcmp(pixel_layout, ff_mxf_pixel_layouts[x].data, 16)) {
+            *pix_fmt = ff_mxf_pixel_layouts[x].pix_fmt;
             return 0;
-        } else
-            url_fskip(&s->pb, klv.length);
-    }
-    return AVERROR_IO;
-}
-
-static int mxf_read_metadata_track(MXFContext *mxf, KLVPacket *klv)
-{
-    ByteIOContext *pb = &mxf->fc->pb;
-    AVRational time_base = (AVRational){0, 0};
-    uint8_t sequence_uid[16];
-    uint8_t track_uid[16];
-    int track_number = 0;
-    int track_id = 0;
-    int bytes_read = 0;
-    int i;
-
-    while (bytes_read < klv->length) {
-        int tag = get_be16(pb);
-        int size = get_be16(pb); /* SMPTE 336M Table 8 KLV specified length, 0x53 */
-
-        switch (tag) {
-        case 0x4801:
-            track_id = get_be32(pb);
-            break;
-        case 0x4804:
-            track_number = get_be32(pb);
-            break;
-        case 0x4B01:
-            time_base.den = get_be32(pb);
-            time_base.num = get_be32(pb);
-            break;
-        case 0x4803:
-            get_buffer(pb, sequence_uid, 16);
-            break;
-        case 0x3C0A:
-            get_buffer(pb, track_uid, 16);
-            break;
-        default:
-            url_fskip(pb, size);
         }
-        bytes_read += size + 4;
     }
-    for (i = 0; i < mxf->tracks_count; i++)
-        if (!memcmp(track_uid, mxf->tracks[i].track_uid, 16)) {
-            mxf->tracks[i].track_id = track_id;
-            mxf->tracks[i].track_number = track_number;
-            mxf->tracks[i].stream->time_base = time_base;
-            mxf->tracks[i].stream->id = track_number;
-            memcpy(mxf->tracks[i].sequence_uid, sequence_uid, 16);
-        }
-    return bytes_read;
-}
 
-static int mxf_read_metadata_sequence(MXFContext *mxf, KLVPacket *klv)
-{
-    ByteIOContext *pb = &mxf->fc->pb;
-    uint8_t sequence_uid[16];
-    uint8_t data_definition[16];
-    uint64_t duration = AV_NOPTS_VALUE;
-    int bytes_read = 0;
-    int i;
-
-    while (bytes_read < klv->length) {
-        int tag = get_be16(pb);
-        int size = get_be16(pb); /* KLV specified by 0x53 */
-
-        switch (tag) {
-        case 0x3C0A:
-            get_buffer(pb, sequence_uid, 16);
-            break;
-        case 0x0202:
-            duration = get_be64(pb);
-            break;
-        case 0x0201:
-            get_buffer(pb, data_definition, 16);
-            break;
-        default:
-            url_fskip(pb, size);
-        }
-        bytes_read += size + 4;
-    }
-
-    for (i = 0; i < mxf->tracks_count; i++)
-        if (!memcmp(sequence_uid, mxf->tracks[i].sequence_uid, 16)) {
-            mxf->tracks[i].stream->start_time = 0;
-            mxf->tracks[i].stream->duration = duration;
-            if (data_definition[11] == 0x02 && data_definition[12] == 0x01)
-                mxf->tracks[i].stream->codec->codec_type = CODEC_TYPE_VIDEO;
-            else if (data_definition[11] == 0x02 && data_definition[12] == 0x02)
-                mxf->tracks[i].stream->codec->codec_type = CODEC_TYPE_AUDIO;
-            else if (data_definition[11] == 0x01) /* SMPTE 12M Time Code track */
-                mxf->tracks[i].stream->codec->codec_type = CODEC_TYPE_DATA;
-        }
-    return bytes_read;
-}
-
-static int mxf_read_metadata_source_package(MXFContext *mxf, KLVPacket *klv)
-{
-    ByteIOContext *pb = &mxf->fc->pb;
-    int tracks_count;
-    int bytes_read = 0;
-    int i;
-
-    while (bytes_read < klv->length) {
-        int tag = get_be16(pb);
-        int size = get_be16(pb); /* KLV specified by 0x53 */
-
-        switch (tag) {
-        case 0x4403:
-            tracks_count = get_be32(pb);
-            if(tracks_count >= UINT_MAX / sizeof(*mxf->tracks) ||
-               tracks_count >= UINT_MAX / sizeof(*mxf->descriptors))
-                return -1;
-            mxf->tracks_count += tracks_count; /* op2a contains multiple source packages */
-            mxf->tracks = av_realloc(mxf->tracks, mxf->tracks_count * sizeof(*mxf->tracks));
-            mxf->descriptors = av_realloc(mxf->descriptors, mxf->tracks_count * sizeof(*mxf->descriptors));
-            url_fskip(pb, 4); /* useless size of objects, always 16 according to specs */
-            for (i = mxf->tracks_count - tracks_count; i < mxf->tracks_count; i++) {
-                mxf->tracks[i].stream = av_new_stream(mxf->fc, 0);
-                get_buffer(pb, mxf->tracks[i].track_uid, 16);
-            }
-            break;
-        default:
-            url_fskip(pb, size);
-        }
-        bytes_read += size + 4;
-    }
-    return bytes_read;
-}
-
-static int mxf_read_metadata_descriptor(MXFContext *mxf, KLVPacket *klv)
-{
-    ByteIOContext *pb = &mxf->fc->pb;
-    MXFDescriptor *desc;
-    int bytes_read = 0;
-
-    if (mxf->descriptors_count == mxf->tracks_count)
-        return -1;
-    desc = &mxf->descriptors[mxf->descriptors_count++];
-    desc->kind = klv->key[14];
-    desc->linked_track_id = -1;
-
-    while (bytes_read < klv->length) {
-        int tag = get_be16(pb);
-        int size = get_be16(pb); /* KLV specified by 0x53 */
-
-        switch (tag) {
-        case 0x3004:
-            get_buffer(pb, desc->essence_container, 16);
-            break;
-        case 0x3006:
-            desc->linked_track_id = get_be32(pb);
-            break;
-        case 0x3201: /* PictureEssenceCoding */
-            desc->codec_type = CODEC_TYPE_VIDEO;
-            get_buffer(pb, desc->essence_compression, 16);
-            break;
-        case 0x3203:
-            desc->width = get_be32(pb);
-            break;
-        case 0x3202:
-            desc->height = get_be32(pb);
-            break;
-        case 0x320E:
-            desc->aspect_ratio.num = get_be32(pb);
-            desc->aspect_ratio.den = get_be32(pb);
-            break;
-        case 0x3D0A:
-            desc->block_align = get_be16(pb);
-            break;
-        case 0x3D03:
-            desc->sample_rate.num = get_be32(pb);
-            desc->sample_rate.den = get_be32(pb);
-            break;
-        case 0x3D06: /* SoundEssenceCompression */
-            desc->codec_type = CODEC_TYPE_AUDIO;
-            get_buffer(pb, desc->essence_compression, 16);
-            break;
-        case 0x3D07:
-            desc->channels = get_be32(pb);
-            break;
-        case 0x3D01:
-            desc->bits_per_sample = get_be32(pb);
-            break;
-        default:
-            url_fskip(pb, size);
-        }
-        bytes_read += size + 4;
-    }
-    return bytes_read;
+    return -1;
 }
 
-/* SMPTE RP224 http://www.smpte-ra.org/mdd/index.html */
-static const CodecTag mxf_sound_essence_labels[] = {
-    { CODEC_ID_PCM_S16LE, 0x01000000 },/* Uncompressed Sound Coding */
-    { CODEC_ID_PCM_S16LE, 0x017F0000 },/* Uncompressed Sound Coding */
-    { CODEC_ID_PCM_S16BE, 0x017E0000 },/* Uncompressed Sound Coding Big Endian*/
-    { CODEC_ID_PCM_ALAW,  0x02030101 },
-    { CODEC_ID_AC3,       0x02030201 },
-  //{ CODEC_ID_MP1,       0x02030104 },
-    { CODEC_ID_MP2,       0x02030105 },/* MP2 or MP3 */
-  //{ CODEC_ID_MP2,       0x02030106 },/* MPEG-2 Layer 1 */
-  //{ CODEC_ID_???,       0x0203010C },/* Dolby E */
-  //{ CODEC_ID_???,       0x02030301 },/* MPEG-2 AAC */
-    { 0, 0 },
-};
-
-static const CodecTag mxf_picture_essence_labels[] = {
-    { CODEC_ID_RAWVIDEO,   0x0100 },
-    { CODEC_ID_MPEG2VIDEO, 0x0201 },
-    { CODEC_ID_DVVIDEO,    0x0202 },
-  //{ CODEC_ID_???,        0x0207 },/* D-11 HDCAM */
-    { 0, 0 },
+static const MXFSamplesPerFrame mxf_spf[] = {
+    { { 1001, 24000 }, { 2002, 0,    0,    0,    0,    0 } }, // FILM 23.976
+    { { 1, 24},        { 2000, 0,    0,    0,    0,    0 } }, // FILM 24
+    { { 1001, 30000 }, { 1602, 1601, 1602, 1601, 1602, 0 } }, // NTSC 29.97
+    { { 1001, 60000 }, { 801,  801,  801,  801,  800,  0 } }, // NTSC 59.94
+    { { 1, 25 },       { 1920, 0,    0,    0,    0,    0 } }, // PAL 25
+    { { 1, 50 },       { 960,  0,    0,    0,    0,    0 } }, // PAL 50
 };
 
-static const CodecTag mxf_container_picture_labels[] = {
-    { CODEC_ID_MPEG2VIDEO, 0x0201 }, /* D-10 Mapping */
-    { CODEC_ID_DVVIDEO,    0x0202 }, /* DV Mapping */
-  //{ CODEC_ID_???,        0x0203 }, /* HDCAM D-11 Mapping */
-    { CODEC_ID_MPEG2VIDEO, 0x0204 }, /* MPEG ES Mapping */
+static const AVRational mxf_time_base[] = {
+    { 1001, 24000 },
+    { 1, 24},
+    { 1001, 30000 },
+    { 1001, 60000 },
+    { 1, 25 },
+    { 1, 50 },
+    { 0, 0}
 };
 
-static const CodecTag mxf_container_sound_labels[] = {
-  //{ CODEC_ID_PCM_S16??,  0x0201 }, /* D-10 Mapping */
-    { CODEC_ID_MP2,        0x0204 }, /* MPEG ES Mapping */
-    { CODEC_ID_PCM_S16LE,  0x0206 }, /* AES BWF Mapping */
-    { CODEC_ID_PCM_ALAW,   0x020A },
-    { 0, 0 },
-};
-
-static void mxf_resolve_track_descriptor(MXFContext *mxf)
-{
-    uint32_t container_label;
-    uint32_t essence_label;
-    int i, j;
-
-    for (i = 0; i < mxf->descriptors_count; i++) {
-        for (j = 0; j < mxf->tracks_count; j++) {
-            AVStream *st = mxf->tracks[j].stream;
-            MXFDescriptor *desc = &mxf->descriptors[i];
-
-            if ((desc->linked_track_id == -1 && st->codec->codec_type == desc->codec_type)
-                || desc->linked_track_id == mxf->tracks[j].track_id) {
-                if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
-                    st->codec->channels = desc->channels;
-                    st->codec->bits_per_sample = desc->bits_per_sample;
-                    st->codec->block_align = desc->block_align;
-                    st->codec->sample_rate = desc->sample_rate.num / desc->sample_rate.den;
-
-                    container_label = BE_16(desc->essence_container + 12);
-                    essence_label = BE_32(desc->essence_compression + 11);
-                    st->codec->codec_id = codec_get_id(mxf_sound_essence_labels, essence_label);
-                    if (st->codec->codec_id == CODEC_ID_PCM_S16LE) {
-                        if (desc->bits_per_sample == 24)
-                            st->codec->codec_id = CODEC_ID_PCM_S24LE;
-                        else if (desc->bits_per_sample == 32)
-                            st->codec->codec_id = CODEC_ID_PCM_S32LE;
-                    }
-                    if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
-                        if (desc->bits_per_sample == 24)
-                            st->codec->codec_id = CODEC_ID_PCM_S24BE;
-                        else if (desc->bits_per_sample == 32)
-                            st->codec->codec_id = CODEC_ID_PCM_S32BE;
-                    }
-                    if (!st->codec->codec_id)
-                        st->codec->codec_id = codec_get_id(mxf_container_sound_labels, container_label);
-
-                } else if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
-                    st->codec->width = desc->width;
-                    st->codec->height = desc->height;
-
-                    container_label = BE_16(desc->essence_container + 12);
-                    essence_label = BE_16(desc->essence_compression + 11);
-                    st->codec->codec_id = codec_get_id(mxf_picture_essence_labels, essence_label);
-                    if (!st->codec->codec_id)
-                        st->codec->codec_id = codec_get_id(mxf_container_picture_labels, container_label);
-                }
-            }
-        }
-    }
-}
-
-static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)
+const MXFSamplesPerFrame *ff_mxf_get_samples_per_frame(AVFormatContext *s,
+                                                       AVRational time_base)
 {
-    MXFContext *mxf = s->priv_data;
-    KLVPacket klv;
-    int ret = 0;
-
-    mxf->fc = s;
-    while (!url_feof(&s->pb)) {
-        if (klv_read_packet(&klv, &s->pb) < 0)
-            return -1;
-        if (IS_KLV_KEY(klv.key, mxf_metadata_track_key))
-            ret = mxf_read_metadata_track(mxf, &klv);
-        else if (IS_KLV_KEY(klv.key, mxf_metadata_source_package_key))
-            ret = mxf_read_metadata_source_package(mxf, &klv);
-        else if (IS_KLV_KEY(klv.key, mxf_metadata_sequence_key))
-            ret = mxf_read_metadata_sequence(mxf, &klv);
-        else if (IS_KLV_KEY(klv.key, mxf_metadata_wave_descriptor_key))
-            ret = mxf_read_metadata_descriptor(mxf, &klv);
-        else if (IS_KLV_KEY(klv.key, mxf_metadata_mpegvideo_descriptor_key))
-            ret = mxf_read_metadata_descriptor(mxf, &klv);
-        else if (IS_KLV_KEY(klv.key, mxf_metadata_cdci_descriptor_key))
-            ret = mxf_read_metadata_descriptor(mxf, &klv);
-        else if (IS_KLV_KEY(klv.key, mxf_metadata_generic_sound_descriptor_key))
-            ret = mxf_read_metadata_descriptor(mxf, &klv);
-        else if (IS_KLV_KEY(klv.key, mxf_essence_element_key)) {
-            /* FIXME avoid seek */
-            url_fseek(&s->pb, klv.offset, SEEK_SET);
-            break;
-        } else
-            url_fskip(&s->pb, klv.length);
-        if (ret < 0)
-            return ret;
-    }
-    mxf_resolve_track_descriptor(mxf);
-    return 0;
-}
+    int idx = av_find_nearest_q_idx(time_base, mxf_time_base);
+    AVRational diff = av_sub_q(time_base, mxf_time_base[idx]);
 
-static int mxf_read_close(AVFormatContext *s)
-{
-    MXFContext *mxf = s->priv_data;
+    diff.num = abs(diff.num);
 
-    av_freep(&mxf->tracks);
-    av_freep(&mxf->descriptors);
-    return 0;
-}
+    if (av_cmp_q(diff, (AVRational){1, 1000}) >= 0)
+        return NULL;
 
-static int mxf_probe(AVProbeData *p) {
-    /* KLV packet describing MXF header partition pack */
-    if (p->buf_size < sizeof(mxf_header_partition_pack_key))
-        return 0;
+    if (av_cmp_q(time_base, mxf_time_base[idx]))
+        av_log(s, AV_LOG_WARNING,
+               "%d/%d input time base matched %d/%d container time base\n",
+               time_base.num, time_base.den,
+               mxf_spf[idx].time_base.num,
+               mxf_spf[idx].time_base.den);
 
-    if (IS_KLV_KEY(p->buf, mxf_header_partition_pack_key))
-        return AVPROBE_SCORE_MAX;
-    else
-        return 0;
+    return &mxf_spf[idx];
 }
-
-
-AVInputFormat mxf_demuxer = {
-    "mxf",
-    "MXF format",
-    sizeof(MXFContext),
-    mxf_probe,
-    mxf_read_header,
-    mxf_read_packet,
-    mxf_read_close,
-    NULL,
-};