From: Tsukasa OMOTO <henry0312@gmail.com>
Date: Thu, 21 Aug 2014 18:57:36 +0900
Subject: [PATCH] mpegts: support ARIB subtitles in libavformat

---
 libavcodec/avcodec.h    |  2 ++
 libavcodec/codec_desc.c | 14 ++++++++++++++
 libavformat/mpegts.c    | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+)

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index d39e7f1..b9533f2 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -522,6 +522,8 @@ enum AVCodecID {
     AV_CODEC_ID_VPLAYER    = MKBETAG('V','P','l','r'),
     AV_CODEC_ID_PJS        = MKBETAG('P','h','J','S'),
     AV_CODEC_ID_ASS        = MKBETAG('A','S','S',' '),  ///< ASS as defined in Matroska
+    AV_CODEC_ID_ARIB_SUB_A = MKBETAG('A','R','B','A'),
+    AV_CODEC_ID_ARIB_SUB_C = MKBETAG('A','R','B','C'),
 
     /* other specific kind of codecs (generally used for attachments) */
     AV_CODEC_ID_FIRST_UNKNOWN = 0x18000,           ///< A dummy ID pointing at the start of various fake codecs.
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 9e9728b..ea1c276 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2662,6 +2662,20 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
         .props     = AV_CODEC_PROP_TEXT_SUB,
     },
+    {
+        .id        = AV_CODEC_ID_ARIB_SUB_A,
+        .type      = AVMEDIA_TYPE_SUBTITLE,
+        .name      = "arib_sub (A)",
+        .long_name = NULL_IF_CONFIG_SMALL("ARIB subtitles: Profile A"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
+    },
+    {
+        .id        = AV_CODEC_ID_ARIB_SUB_C,
+        .type      = AVMEDIA_TYPE_SUBTITLE,
+        .name      = "arib_sub (C)",
+        .long_name = NULL_IF_CONFIG_SMALL("ARIB subtitles: Profile C"),
+        .props     = AV_CODEC_PROP_TEXT_SUB,
+    },
 
     /* other kind of codecs and pseudo-codecs */
     {
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index a118689..3cb0c5f 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -1861,6 +1861,40 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
                                             pes->sub_st->index);
                 pes->sub_st->codec->codec_tag = st->codec->codec_tag;
             }
+
+            /*
+             * ARIB Subtitles
+             *
+             * @see 4.2.8.3 Stream format identification in ARIB TR-B14
+             * @see 6.2.7.3 Stream format identification in ARIB TR-B14
+             * @see 6.2.16 Stream identifier descriptor in Part 2 in ARIB STD-B10
+             */
+            if (stream_type == 0x06) {
+                // check component_tag in Stream identifier descriptor
+                switch (st->stream_identifier - 1) {
+                case 0x30:
+                case 0x31:
+                case 0x32:
+                case 0x33:
+                case 0x34:
+                case 0x35:
+                case 0x36:
+                case 0x37:
+                    // @see 4.2.8.1 Operation of component tags in ARIB TR-B14
+                    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
+                    st->codec->codec_id   = AV_CODEC_ID_ARIB_SUB_A;
+                    break;
+                case 0x87:
+                    // @see 6.2.7.1 Operation of component tags in ARIB TR-B14
+                    st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
+                    st->codec->codec_id   = AV_CODEC_ID_ARIB_SUB_C;
+                    break;
+                default:
+                    st->codec->codec_type = AVMEDIA_TYPE_UNKNOWN;
+                    st->codec->codec_id   = AV_CODEC_ID_NONE;
+                    break;
+                }
+            }
         }
         p = desc_list_end;
     }
-- 
2.1.0

