]> git.sesse.net Git - ffmpeg/commitdiff
avcodec: split off A53 Closed Caption parsing code into its own file
authorJames Almer <jamrial@gmail.com>
Fri, 7 Aug 2020 19:03:29 +0000 (16:03 -0300)
committerJames Almer <jamrial@gmail.com>
Sat, 15 Aug 2020 16:01:00 +0000 (13:01 -0300)
Signed-off-by: James Almer <jamrial@gmail.com>
configure
libavcodec/Makefile
libavcodec/atsc_a53.c [new file with mode: 0644]
libavcodec/atsc_a53.h [new file with mode: 0644]

index d4a1fea9ce28955b92d40d3eb46a6692af583c5d..34c40a765a5348fed968ace57a91ba0dbfd7e485 100755 (executable)
--- a/configure
+++ b/configure
@@ -2344,6 +2344,7 @@ CONFIG_EXTRA="
     aandcttables
     ac3dsp
     adts_header
+    atsc_a53
     audio_frame_queue
     audiodsp
     blockdsp
index fc4294816e21bf0a318b3dba6222b5aae676e408..3431ba2dcaa8b00c874640c50863130a8e701472 100644 (file)
@@ -64,6 +64,7 @@ OBJS-$(CONFIG_AC3DSP)                  += ac3dsp.o ac3.o ac3tab.o
 OBJS-$(CONFIG_ADTS_HEADER)             += adts_header.o mpeg4audio.o
 OBJS-$(CONFIG_AMF)                     += amfenc.o
 OBJS-$(CONFIG_AUDIO_FRAME_QUEUE)       += audio_frame_queue.o
+OBJS-$(CONFIG_ATSC_A53)                += atsc_a53.o
 OBJS-$(CONFIG_AUDIODSP)                += audiodsp.o
 OBJS-$(CONFIG_BLOCKDSP)                += blockdsp.o
 OBJS-$(CONFIG_BSWAPDSP)                += bswapdsp.o
diff --git a/libavcodec/atsc_a53.c b/libavcodec/atsc_a53.c
new file mode 100644 (file)
index 0000000..b2d1490
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "atsc_a53.h"
+#include "get_bits.h"
+
+int ff_parse_a53_cc(AVBufferRef **pbuf, const uint8_t *data, int size)
+{
+    AVBufferRef *buf = *pbuf;
+    GetBitContext gb;
+    size_t new_size, old_size = buf ? buf->size : 0;
+    int ret, cc_count;
+
+    if (size < 3)
+        return AVERROR(EINVAL);
+
+    ret = init_get_bits8(&gb, data, size);
+    if (ret < 0)
+        return ret;
+
+    if (get_bits(&gb, 8) != 0x3) // user_data_type_code
+        return 0;
+
+    skip_bits(&gb, 1); // reserved
+    if (!get_bits(&gb, 1)) // process_cc_data_flag
+        return 0;
+
+    skip_bits(&gb, 1); // zero bit
+    cc_count = get_bits(&gb, 5);
+    if (!cc_count)
+        return 0;
+
+    skip_bits(&gb, 8); // reserved
+
+    /* 3 bytes per CC plus one byte marker_bits at the end */
+    if (cc_count * 3 >= (get_bits_left(&gb) >> 3))
+        return AVERROR(EINVAL);
+
+    new_size = (old_size + cc_count * 3);
+
+    if (new_size > INT_MAX)
+        return AVERROR(EINVAL);
+
+    /* Allow merging of the cc data from two fields. */
+    ret = av_buffer_realloc(pbuf, new_size);
+    if (ret < 0)
+        return ret;
+
+    buf = *pbuf;
+    /* Use of av_buffer_realloc assumes buffer is writeable */
+    for (int i = 0; i < cc_count; i++) {
+        buf->data[old_size++] = get_bits(&gb, 8);
+        buf->data[old_size++] = get_bits(&gb, 8);
+        buf->data[old_size++] = get_bits(&gb, 8);
+    }
+
+    return cc_count;
+}
diff --git a/libavcodec/atsc_a53.h b/libavcodec/atsc_a53.h
new file mode 100644 (file)
index 0000000..a419d80
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_ATSC_A53_H
+#define AVCODEC_ATSC_A53_H
+
+#include <stdint.h>
+
+#include "libavutil/buffer.h"
+
+/**
+ * Parse a data array for ATSC A53 Part 4 Closed Captions and store them in an AVBufferRef.
+ *
+ * @param pbuf Pointer to an AVBufferRef to append the closed captions. *pbuf may be NULL, in
+ *             which case a new buffer will be allocated and put in it.
+ * @param data The data array containing the raw A53 data.
+ * @param size Size of the data array in bytes.
+ *
+ * @return Number of closed captions parsed on success, negative error code on failure.
+ *         If no Closed Captions are parsed, *pbuf is untouched.
+ */
+int ff_parse_a53_cc(AVBufferRef **pbuf, const uint8_t *data, int size);
+
+#endif /* AVCODEC_ATSC_A53_H */