diff mbox series

[FFmpeg-devel] IEC61937_EAC3 decoding support

Message ID 20210411101340.600851-1-Shulyaka@gmail.com
State New
Headers show
Series [FFmpeg-devel] IEC61937_EAC3 decoding support | expand

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Denis Shulyaka April 11, 2021, 10:13 a.m. UTC
This patch adds support to decode IEC61937_EAC3 (aka dolby digital plus).

Signed-off-by: Denis Shulyaka <Shulyaka@gmail.com>
---
 libavformat/spdifdec.c | 43 ++++++++++++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/spdifdec.c b/libavformat/spdifdec.c
index 1808fa9d65..adc5f4f255 100644
--- a/libavformat/spdifdec.c
+++ b/libavformat/spdifdec.c
@@ -93,6 +93,10 @@  static int spdif_get_offset_and_codec(AVFormatContext *s,
         *offset = 8192;
         *codec = AV_CODEC_ID_DTS;
         break;
+    case IEC61937_EAC3:
+        *offset = AC3_FRAME_SIZE << 4;
+        *codec = AV_CODEC_ID_EAC3;
+        break;
     default:
         if (s) { /* be silent during a probe */
             avpriv_request_sample(s, "Data type 0x%04x in IEC 61937",
@@ -103,9 +107,32 @@  static int spdif_get_offset_and_codec(AVFormatContext *s,
     return 0;
 }
 
-/* Largest offset between bursts we currently handle, i.e. AAC with
-   samples = 4096 */
-#define SPDIF_MAX_OFFSET 16384
+static int spdif_read_burst_payload_length(AVFormatContext *s,
+                                           enum IEC61937DataType data_type)
+{
+    AVIOContext *pb = s->pb;
+    int pkt_size_bits, pkt_size_bytes;
+
+    switch (data_type & 0xff) {
+    case IEC61937_EAC3:
+        pkt_size_bytes = avio_rl16(pb);
+
+        if (pkt_size_bytes & 0x1)
+            avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
+
+        return FFALIGN(pkt_size_bytes, 2);
+    default:
+        pkt_size_bits = avio_rl16(pb);
+
+        if (pkt_size_bits & 0xF)
+            avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
+
+        return FFALIGN(pkt_size_bits, 16) >> 3;
+    }
+}
+
+/* Largest offset between bursts we currently handle, i.e. E-AC-3 */
+#define SPDIF_MAX_OFFSET 24576
 
 static int spdif_probe(const AVProbeData *p)
 {
@@ -176,7 +203,7 @@  int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
     enum IEC61937DataType data_type;
     enum AVCodecID codec_id;
     uint32_t state = 0;
-    int pkt_size_bits, offset, ret;
+    int pkt_size, offset, ret;
 
     while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
         state = (state << 8) | avio_r8(pb);
@@ -185,12 +212,12 @@  int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
     }
 
     data_type = avio_rl16(pb);
-    pkt_size_bits = avio_rl16(pb);
+    pkt_size = spdif_read_burst_payload_length(s, data_type);
 
-    if (pkt_size_bits % 16)
-        avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
+    if (!pkt_size)
+        return AVERROR_BUG;
 
-    ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
+    ret = av_new_packet(pkt, pkt_size);
     if (ret)
         return ret;