From 53f6f28be0084f543e981e98f789087923df2747 Mon Sep 17 00:00:00 2001
From: "Gregorio Gervasio, Jr" <gregorio.gervasio@gmail.com>
Date: Sat, 1 Oct 2016 22:48:04 -0700
Subject: [PATCH 2/2] Added decoding of A53 captions.

---
 mythtv/libs/libmythtv/avformatdecoder.cpp | 24 ++++++++++++++++++++----
 mythtv/libs/libmythtv/avformatdecoder.h   |  1 +
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/mythtv/libs/libmythtv/avformatdecoder.cpp b/mythtv/libs/libmythtv/avformatdecoder.cpp
index c7a375a..6e675ac 100644
--- a/mythtv/libs/libmythtv/avformatdecoder.cpp
+++ b/mythtv/libs/libmythtv/avformatdecoder.cpp
@@ -3002,14 +3002,22 @@ void AvFormatDecoder::DecodeDTVCC(const uint8_t *buf, uint len, bool scte)
     if (len < 2+(3*cc_count))
         return;
 
+    DecodeCCx08(buf+2, cc_count*3, scte);
+}
+
+void AvFormatDecoder::DecodeCCx08(const uint8_t *buf, uint len, bool scte)
+{
+    if (!len)
+        return;
+
     bool had_608 = false, had_708 = false;
-    for (uint cur = 0; cur < cc_count; cur++)
+    for (uint cur = 0; cur < len; cur += 3)
     {
-        uint cc_code  = buf[2+(cur*3)];
+        uint cc_code  = buf[cur];
         bool cc_valid = cc_code & 0x04;
 
-        uint data1    = buf[3+(cur*3)];
-        uint data2    = buf[4+(cur*3)];
+        uint data1    = buf[cur+1];
+        uint data2    = buf[cur+2];
         uint data     = (data2 << 8) | data1;
         uint cc_type  = cc_code & 0x03;
         uint field;
@@ -3695,6 +3703,14 @@ bool AvFormatDecoder::ProcessVideoFrame(AVStream *stream, AVFrame *mpa_pic)
     for (uint i = 0; i < cc_len; i += ((cc_buf[i] & 0x1f) * 3) + 2)
         DecodeDTVCC(cc_buf + i, cc_len - i, scte);
 
+    if (cc_len == 0) {
+        // look for A53 captions
+        AVFrameSideData *side_data = av_frame_get_side_data(mpa_pic, AV_FRAME_DATA_A53_CC);
+        if (side_data && (side_data->size > 0)) {
+            DecodeCCx08(side_data->data, side_data->size, false);
+        }
+    }
+
     VideoFrame *picframe = (VideoFrame *)(mpa_pic->opaque);
 
     if (FlagIsSet(kDecodeNoDecode))
diff --git a/mythtv/libs/libmythtv/avformatdecoder.h b/mythtv/libs/libmythtv/avformatdecoder.h
index 8fcc047..cc23957 100644
--- a/mythtv/libs/libmythtv/avformatdecoder.h
+++ b/mythtv/libs/libmythtv/avformatdecoder.h
@@ -216,6 +216,7 @@ class AvFormatDecoder : public DecoderBase
     friend int close_avf(URLContext *h);
 
     void DecodeDTVCC(const uint8_t *buf, uint buf_size, bool scte);
+    void DecodeCCx08(const uint8_t *buf, uint buf_size, bool scte);
     void InitByteContext(bool forceseek = false);
     void InitVideoCodec(AVStream *stream, AVCodecContext *enc,
                         bool selectedStream = false);
-- 
2.7.4

