correctly write codec ul, mpeg-2 needs profile and level to be set
authorBaptiste Coudurier <baptiste.coudurier@gmail.com>
Sun, 31 Aug 2008 02:41:31 +0000 (02:41 +0000)
committerBaptiste Coudurier <baptiste.coudurier@gmail.com>
Sun, 31 Aug 2008 02:41:31 +0000 (02:41 +0000)
Originally committed as revision 15094 to svn://svn.ffmpeg.org/ffmpeg/trunk

libavformat/mxfenc.c

index 5e16a2b..46f2eb7 100644 (file)
@@ -41,20 +41,25 @@ typedef struct {
 typedef struct {
     UID track_essence_element_key;
     const UID *essence_container_ul;
+    const UID *codec_ul;
 } MXFStreamContext;
 
 typedef struct {
     UID container_ul;
     UID element_ul;
+    UID codec_ul;
     enum CodecID id;
 } MXFContainerEssencePair;
 
 static const MXFContainerEssencePair mxf_essence_container_uls[] = {
     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
-      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 }, CODEC_ID_MPEG2VIDEO },
+      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
+      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 }, CODEC_ID_MPEG2VIDEO },
     { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
-      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 }, CODEC_ID_PCM_S16LE },
+      { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
+      { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 }, CODEC_ID_PCM_S16LE },
     { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
+      { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
       { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, CODEC_ID_NONE },
 };
 
@@ -586,6 +591,9 @@ static void mxf_write_generic_desc(ByteIOContext *pb, const MXFDescriptorWriteTa
 
     mxf_write_local_tag(pb, 16, 0x3004);
     put_buffer(pb, *sc->essence_container_ul, 16);
+
+    mxf_write_local_tag(pb, 16, 0x3201);
+    put_buffer(pb, *sc->codec_ul, 16);
 }
 
 static void mxf_write_mpegvideo_desc(AVFormatContext *s, const MXFDescriptorWriteTableEntry *desc_tbl, AVStream *st)
@@ -603,10 +611,6 @@ static void mxf_write_mpegvideo_desc(AVFormatContext *s, const MXFDescriptorWrit
     mxf_write_local_tag(pb, 8, 0x320E);
     put_be32(pb, st->codec->height * st->sample_aspect_ratio.den);
     put_be32(pb, st->codec->width  * st->sample_aspect_ratio.num);
-
-    // tmp write, will modified later
-    mxf_write_local_tag(pb, 16, 0x3201);
-    put_buffer(pb, ff_mxf_codec_uls->uid, 16);
 }
 
 static void mxf_write_wav_desc(AVFormatContext *s, const MXFDescriptorWriteTableEntry *desc_tbl, AVStream *st)
@@ -625,10 +629,6 @@ static void mxf_write_wav_desc(AVFormatContext *s, const MXFDescriptorWriteTable
 
     mxf_write_local_tag(pb, 4, 0x3D01);
     put_be32(pb, st->codec->bits_per_sample);
-
-    // tmp write, will modified later
-    mxf_write_local_tag(pb, 16, 0x3201);
-    put_buffer(pb, (ff_mxf_codec_uls + 8)->uid, 16);
 }
 
 static const MXFDescriptorWriteTableEntry mxf_descriptor_write_table[] = {
@@ -713,6 +713,41 @@ static void mxf_write_partition(AVFormatContext *s, int64_t byte_position, int b
     mxf_write_essence_container_refs(s);
 }
 
+static const UID mxf_mpeg2_codec_uls[] = {
+    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
+    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
+    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
+    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
+    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
+    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
+    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
+    { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
+};
+
+static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
+{
+    if (avctx->profile == 4) { // Main
+        if (avctx->level == 8) // Main
+            return avctx->gop_size ?
+                &mxf_mpeg2_codec_uls[1] :
+                &mxf_mpeg2_codec_uls[0];
+        else if (avctx->level == 4) // High
+            return avctx->gop_size ?
+                &mxf_mpeg2_codec_uls[5] :
+                &mxf_mpeg2_codec_uls[4];
+    } else if (avctx->profile == 0) { // 422
+        if (avctx->level == 5) // Main
+            return avctx->gop_size ?
+                &mxf_mpeg2_codec_uls[3] :
+                &mxf_mpeg2_codec_uls[2];
+        else if (avctx->level == 2) // High
+            return avctx->gop_size ?
+                &mxf_mpeg2_codec_uls[7] :
+                &mxf_mpeg2_codec_uls[6];
+    }
+    return NULL;
+}
+
 static int mux_write_header(AVFormatContext *s)
 {
     MXFContext *mxf = s->priv_data;
@@ -739,6 +774,22 @@ static int mux_write_header(AVFormatContext *s)
                    "codec not currently supported in container\n", i);
             return -1;
         }
+
+        if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
+            if (st->codec->profile == FF_PROFILE_UNKNOWN ||
+                st->codec->level == FF_LEVEL_UNKNOWN) {
+                av_log(s, AV_LOG_ERROR, "track %d: profile and level must be set for mpeg-2\n", i);
+                return -1;
+            }
+            sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec);
+            if (!sc->codec_ul) {
+                av_log(s, AV_LOG_ERROR, "track %d: could not find codec ul for mpeg-2, "
+                       "unsupported profile/level\n", i);
+                return -1;
+            }
+        } else
+            sc->codec_ul = &mxf_essence_container_uls[index].codec_ul;
+
         if (!present[index]) {
             mxf->essence_containers_indices[mxf->essence_container_count++] = index;
             present[index] = 1;