From patchwork Mon Aug 10 15:49:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 21587 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 50CA244A46B for ; Mon, 10 Aug 2020 19:19:33 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2F276689AD2; Mon, 10 Aug 2020 19:19:33 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 22C9B6881FE for ; Mon, 10 Aug 2020 19:19:26 +0300 (EEST) Received: by mail-pj1-f43.google.com with SMTP id c6so88495pje.1 for ; Mon, 10 Aug 2020 09:19:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=AbZ4/pK6HMGXQG9d21LSB7xVFZ/Kx8m/TKJKs7pkqZk=; b=NLMbcscVaa0cQ1ptqlCBdj+ldGCsCA0oW3siiVUXgDtMxCpjp1qZyXo3gHYP9Tf7Ci bpapz6YBB8QsJr/magsWnHwzt9n2qWKMav5o+B9e/V8XwD31UCZdCro8LUCEUbzmdvl8 1TQkfe9zdz/hrKvzfG7DgEmNyYDIRCCB8ksiXW92ehMF8b+8stYpvcI48m9OTFWXrTe7 T0o7GoVsdlwGTq8V3lQi3gUJQjiPVCusC8c2sOA5g4GbzFVvQ5Lv00NeRQuX4TcWOYzk qfvPvNTUSpTXUwwoa7mm3SGgj7iNyFp3ySd7gtgBfUAY44Ew9nMgTmfdg5zp5bNertXw lTuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=AbZ4/pK6HMGXQG9d21LSB7xVFZ/Kx8m/TKJKs7pkqZk=; b=JoN7Qz2FwnWvP5JL4Va0/pS/w1h1dD5jQOuXnvOQqZlVJdTCd97UwF/XHECTxlpbZ9 9jdpnNVBFvwaFTj9jwnoJb9YKsxoqoZFAw/pGvTPxHR1pjezht3NUGvn30KkfMgZDjgj IlmhF3TgCa5PbekqhuCKsLqiwtFv6Q+3GnsZVpK9TPNPpAsjgDHQe3WhkwxdaWmf3Puw NEuodKBdmMZrrh9zdnfKoEDWs4V4uSQHvoMMffIfyFQTzlL1dmBK6i52dg4FGYKYYwhL UBq/+96pN7FwufbT6i+/yd3lZjgPD3kxWD58yi/v1TvC5cGcn7n4cVC8Ze+rrnwHQRA/ Czhw== X-Gm-Message-State: AOAM531NRDL52XXxkhHpSnCrHr1iIR3OFtG0yV7Sxymrlz2k7xZxI++i 97uhr6xHlMrbU3YAZ2zTXSz7PJrF X-Google-Smtp-Source: ABdhPJxKcCea97HfVtJOizLrZLGKnoJ3bdRa8PzBMa0SDOT90Upl9USClD4kOu4wcsNvIYhle6xvZA== X-Received: by 2002:a0c:bd96:: with SMTP id n22mr28223765qvg.19.1597074608732; Mon, 10 Aug 2020 08:50:08 -0700 (PDT) Received: from localhost.localdomain ([181.23.72.45]) by smtp.gmail.com with ESMTPSA id o15sm13793249qkk.95.2020.08.10.08.50.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Aug 2020 08:50:07 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Mon, 10 Aug 2020 12:49:37 -0300 Message-Id: <20200810154941.4988-1-jamrial@gmail.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/5] avcodec: split off A53 Closed Caption parsing code into its own file X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: James Almer --- configure | 1 + libavcodec/Makefile | 1 + libavcodec/atsc_a53.c | 76 +++++++++++++++++++++++++++++++++++++++++++ libavcodec/atsc_a53.h | 39 ++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 libavcodec/atsc_a53.c create mode 100644 libavcodec/atsc_a53.h diff --git a/configure b/configure index 8de1afcb99..37fe01e8be 100755 --- a/configure +++ b/configure @@ -2344,6 +2344,7 @@ CONFIG_EXTRA=" aandcttables ac3dsp adts_header + atsc_a53 audio_frame_queue audiodsp blockdsp diff --git a/libavcodec/Makefile b/libavcodec/Makefile index fc4294816e..3431ba2dca 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -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 index 0000000000..f040b0c10e --- /dev/null +++ b/libavcodec/atsc_a53.c @@ -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 +#include + +#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 index 0000000000..a419d801b9 --- /dev/null +++ b/libavcodec/atsc_a53.h @@ -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 + +#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 */