changeset 3716:2ca19db359bd libavformat

Dirac encapsulation in MPEG-TS patch by Anuradha Suraparaju, anuradha rd.bbc.co uk
author diego
date Wed, 13 Aug 2008 19:29:35 +0000
parents 460059833ae3
children f65dc584662d
files mpegts.c mpegts.h mpegtsenc.c
diffstat 3 files changed, 62 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mpegts.c	Wed Aug 13 18:32:16 2008 +0000
+++ b/mpegts.c	Wed Aug 13 19:29:35 2008 +0000
@@ -482,6 +482,7 @@
     int comp_page = 0, anc_page = 0; /* initialize to kill warnings */
     char language[4] = {0}; /* initialize to kill warnings */
     int has_hdmv_descr = 0;
+    int has_dirac_descr = 0;
 
 #ifdef DEBUG_SI
     av_log(ts->stream, AV_LOG_DEBUG, "PMT: len %i\n", section_len);
@@ -589,6 +590,18 @@
                 language[2] = get8(&p, desc_end);
                 language[3] = 0;
                 break;
+            case REGISTRATION_DESCRIPTOR: /*MPEG-2 Registration descriptor */
+                {
+                    uint8_t bytes[4];
+                    bytes[0] = get8(&p, desc_end);
+                    bytes[1] = get8(&p, desc_end);
+                    bytes[2] = get8(&p, desc_end);
+                    bytes[3] = get8(&p, desc_end);
+                    if(bytes[0] == 'd' && bytes[1] == 'r' &&
+                       bytes[2] == 'a' && bytes[3] == 'c')
+                        has_dirac_descr = 1;
+                    break;
+                }
             default:
                 break;
             }
@@ -610,12 +623,14 @@
         case STREAM_TYPE_VIDEO_MPEG4:
         case STREAM_TYPE_VIDEO_H264:
         case STREAM_TYPE_VIDEO_VC1:
+        case STREAM_TYPE_VIDEO_DIRAC:
         case STREAM_TYPE_AUDIO_AAC:
         case STREAM_TYPE_AUDIO_AC3:
         case STREAM_TYPE_AUDIO_DTS:
         case STREAM_TYPE_AUDIO_HDMV_DTS:
         case STREAM_TYPE_SUBTITLE_DVB:
-            if(stream_type == STREAM_TYPE_AUDIO_HDMV_DTS && !has_hdmv_descr)
+            if((stream_type == STREAM_TYPE_AUDIO_HDMV_DTS && !has_hdmv_descr)
+            || (stream_type == STREAM_TYPE_VIDEO_DIRAC    && !has_dirac_descr))
                 break;
             if(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES){
                 pes= ts->pids[pid]->u.pes_filter.opaque;
@@ -944,6 +959,10 @@
         codec_type = CODEC_TYPE_VIDEO;
         codec_id = CODEC_ID_VC1;
         break;
+    case STREAM_TYPE_VIDEO_DIRAC:
+        codec_type = CODEC_TYPE_VIDEO;
+        codec_id = CODEC_ID_DIRAC;
+        break;
     case STREAM_TYPE_AUDIO_AAC:
         codec_type = CODEC_TYPE_AUDIO;
         codec_id = CODEC_ID_AAC;
--- a/mpegts.h	Wed Aug 13 18:32:16 2008 +0000
+++ b/mpegts.h	Wed Aug 13 19:29:35 2008 +0000
@@ -52,6 +52,7 @@
 #define STREAM_TYPE_VIDEO_MPEG4     0x10
 #define STREAM_TYPE_VIDEO_H264      0x1b
 #define STREAM_TYPE_VIDEO_VC1       0xea
+#define STREAM_TYPE_VIDEO_DIRAC     0xd1
 
 #define STREAM_TYPE_AUDIO_AC3       0x81
 #define STREAM_TYPE_AUDIO_DTS       0x8a
--- a/mpegtsenc.c	Wed Aug 13 18:32:16 2008 +0000
+++ b/mpegtsenc.c	Wed Aug 13 19:29:35 2008 +0000
@@ -220,6 +220,9 @@
         case CODEC_ID_H264:
             stream_type = STREAM_TYPE_VIDEO_H264;
             break;
+        case CODEC_ID_DIRAC:
+            stream_type = STREAM_TYPE_VIDEO_DIRAC;
+            break;
         case CODEC_ID_MP2:
         case CODEC_ID_MP3:
             stream_type = STREAM_TYPE_AUDIO_MPEG1;
@@ -267,6 +270,16 @@
                 put16(&q, 1); /* ancillary page id */
             }
             break;
+        case CODEC_TYPE_VIDEO:
+            if (stream_type == STREAM_TYPE_VIDEO_DIRAC) {
+                *q++ = 0x05; /*MPEG-2 registration descriptor*/
+                *q++ = 4;
+                *q++ = 'd';
+                *q++ = 'r';
+                *q++ = 'a';
+                *q++ = 'c';
+            }
+            break;
         }
 
         val = 0xf000 | (q - desc_length_ptr - 2);
@@ -527,13 +540,17 @@
             *q++ = 0;
         }
         if (is_start) {
+            int pes_extension = 0;
             /* write PES header */
             *q++ = 0x00;
             *q++ = 0x00;
             *q++ = 0x01;
             private_code = 0;
             if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
-                *q++ = 0xe0;
+                if (st->codec->codec_id == CODEC_ID_DIRAC) {
+                    *q++ = 0xfd;
+                } else
+                    *q++ = 0xe0;
             } else if (st->codec->codec_type == CODEC_TYPE_AUDIO &&
                        (st->codec->codec_id == CODEC_ID_MP2 ||
                         st->codec->codec_id == CODEC_ID_MP3)) {
@@ -554,6 +571,19 @@
                 header_len += 5;
                 flags |= 0x40;
             }
+            if (st->codec->codec_type == CODEC_TYPE_VIDEO &&
+                st->codec->codec_id == CODEC_ID_DIRAC) {
+                /* set PES_extension_flag */
+                pes_extension = 1;
+                flags |= 0x01;
+
+                /*
+                * One byte for PES2 extension flag +
+                * one byte for extension length +
+                * one byte for extension id
+                */
+                header_len += 3;
+            }
             len = payload_size + header_len + 3;
             if (private_code != 0)
                 len++;
@@ -574,6 +604,16 @@
                 write_pts(q, 1, dts);
                 q += 5;
             }
+            if (pes_extension && st->codec->codec_id == CODEC_ID_DIRAC) {
+                flags = 0x01;  /* set PES_extension_flag_2 */
+                *q++ = flags;
+                *q++ = 0x80 | 0x01;  /* marker bit + extension length */
+                /*
+                * Set the stream id extension flag bit to 0 and
+                * write the extended stream id
+                */
+                *q++ = 0x00 | 0x60;
+            }
             if (private_code != 0)
                 *q++ = private_code;
             is_start = 0;