From patchwork Fri Oct 2 22:23:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Gaullier X-Patchwork-Id: 22705 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 BFEDC44BDEC for ; Sat, 3 Oct 2020 01:24:03 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AEFAC68A494; Sat, 3 Oct 2020 01:24:03 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from srv-infra-1.infra.inf.glb.tvvideoms.com (unknown [213.205.126.156]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 053C76882DB for ; Sat, 3 Oct 2020 01:23:56 +0300 (EEST) Received: from cji.paris (unknown [172.16.3.159]) by srv-infra-1.infra.inf.glb.tvvideoms.com (Postfix) with ESMTP id 63B0F201EE; Fri, 2 Oct 2020 22:23:55 +0000 (UTC) From: Nicolas Gaullier To: ffmpeg-devel@ffmpeg.org Date: Sat, 3 Oct 2020 00:23:39 +0200 Message-Id: <20201002222346.1196-3-nicolas.gaullier@cji.paris> X-Mailer: git-send-email 2.27.0.windows.1 In-Reply-To: <20201002222346.1196-1-nicolas.gaullier@cji.paris> References: <20201002222346.1196-1-nicolas.gaullier@cji.paris> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 2/9] avformat/s337m: Split read_packet/get_packet 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 Cc: Nicolas Gaullier Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Prepare use of s337m_get_packet from outside. --- libavformat/s337m.c | 26 ++++++++++++++++++++------ libavformat/s337m.h | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 libavformat/s337m.h diff --git a/libavformat/s337m.c b/libavformat/s337m.c index 36e1047af8..7f6eecb25c 100644 --- a/libavformat/s337m.c +++ b/libavformat/s337m.c @@ -21,6 +21,7 @@ #include "libavutil/intreadwrite.h" #include "avformat.h" #include "spdif.h" +#include "s337m.h" #define MARKER_16LE 0x72F81F4E #define MARKER_20LE 0x20876FF0E154 @@ -141,18 +142,20 @@ static void bswap_buf24(uint8_t *data, int size) FFSWAP(uint8_t, data[0], data[2]); } -static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt) +int ff_s337m_get_packet(AVIOContext *pb, AVPacket *pkt, int size, enum AVCodecID *codec, void *avc) { - AVIOContext *pb = s->pb; uint64_t state = 0; int ret, data_type, data_size, offset; - enum AVCodecID codec; - int64_t pos; + int64_t pos, orig_pos = avio_tell(pb); while (!IS_LE_MARKER(state)) { state = (state << 8) | avio_r8(pb); if (avio_feof(pb)) return AVERROR_EOF; + if (avio_tell(pb) - orig_pos + 6 >= size) { + av_log(avc, AV_LOG_ERROR, "s337m : sync bytes not found at packet pos=0x%"PRIx64" size=%d\n", orig_pos, size); + return AVERROR_INVALIDDATA; + } } if (IS_16LE_MARKER(state)) { @@ -165,10 +168,10 @@ static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt) pos = avio_tell(pb); - if ((ret = s337m_get_offset_and_codec(s, state, data_type, data_size, &offset, &codec)) < 0) + if ((ret = s337m_get_offset_and_codec(avc, state, data_type, data_size, &offset, codec)) < 0) return ret; - if ((ret = av_new_packet(pkt, offset)) < 0) + if (ret = av_new_packet(pkt, FFMIN(offset, size - (pos - orig_pos))) < 0) return ret; pkt->pos = pos; @@ -182,6 +185,17 @@ static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt) else bswap_buf24(pkt->data, pkt->size); + return 0; +} + +static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + enum AVCodecID codec; + int ret; + + if ((ret = ff_s337m_get_packet(s->pb, pkt, avio_size(s->pb), &codec, s)) < 0) + return ret; + if (!s->nb_streams) { AVStream *st = avformat_new_stream(s, NULL); if (!st) { diff --git a/libavformat/s337m.h b/libavformat/s337m.h new file mode 100644 index 0000000000..f7bd0c16f6 --- /dev/null +++ b/libavformat/s337m.h @@ -0,0 +1,37 @@ +/* + * SMPTE ST 337 common header + * + * 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 AVFORMAT_S337M_H +#define AVFORMAT_S337M_H + +/** + * Read s337m packets in a PCM_S16LE/S24LE stereo stream + * Returns the first inner packet found + * Note that it does not require a clean guard band + * @param pb Associated IO context + * @param pkt On success, returns a DOLBY E packet + * @param size Maximum IO read size available for reading at current position + * @param codec Returns AV_CODEC_ID_DOLBY_E + * @param avc For av_log + * @return = 0 on success (an error is raised if no s337m was found) + */ +int ff_s337m_get_packet(AVIOContext *pb, AVPacket *pkt, int size, enum AVCodecID *codec, void *avc); + +#endif /* AVFORMAT_S337M_H */