mxf.c 17.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
/*
 * 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
 * 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.
 *
 * This library 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
 * 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;

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;

    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;
}

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;
}

static int mxf_get_stream_index(AVFormatContext *s, KLVPacket *klv)
{
    int id = BE_32(klv->key + 12); /* SMPTE 379M 7.3 */
    int i;

    for (i = 0; i < s->nb_streams; i++) {
        if (s->streams[i]->id == id)
            return i;
    }
    return -1;
}

static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    KLVPacket klv;

    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;
            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;
}

/* 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 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 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)
{
    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;
}

static int mxf_read_close(AVFormatContext *s)
{
    MXFContext *mxf = s->priv_data;

    av_freep(&mxf->tracks);
    av_freep(&mxf->descriptors);
    return 0;
}

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 (IS_KLV_KEY(p->buf, mxf_header_partition_pack_key))
        return AVPROBE_SCORE_MAX;
    else
        return 0;
}


AVInputFormat mxf_demuxer = {
    "mxf",
    "MXF format",
    sizeof(MXFContext),
    mxf_probe,
    mxf_read_header,
    mxf_read_packet,
    mxf_read_close,
    NULL,
};