From patchwork Tue Aug 6 15:08:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaullier Nicolas X-Patchwork-Id: 14270 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 57F6C447AC7 for ; Tue, 6 Aug 2019 18:08:25 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4416668A9D8; Tue, 6 Aug 2019 18:08:25 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtp-2.arkena.net (smtp-2.arkena.net [95.81.173.75]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 88CFA68A41A for ; Tue, 6 Aug 2019 18:08:17 +0300 (EEST) Received: from secu2 (unknown [10.180.103.10]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp-2.arkena.net (Postfix) with ESMTPSA id 462ygj1CwLzHh8w; Tue, 6 Aug 2019 15:08:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arkena.com; s=20150421; t=1565104097; bh=ovOHX2J3ta5+1bokIwTUmlFxfNgSQfqMCA84frk+u+M=; h=From:To:Cc:Subject:Date:Message-Id; b=c5xxbtgQ5x/5cXS+FBnnaOobz3FpnlSepHeKhEkbEYYYFSgbLjV+7dZSqnzd5mnW1 pEmv6EqrJzor9Z/GfIXtUcD+b1KCLmw+1aVMzYGqDd6o5b+3mMsw9kxSzFe+aL+SCE AOguN6ebtCMLlSvCJGvxVuuWWjIdwFnWCwtF+oe8= Received: from arkena.com (unknown [172.16.3.159]) by secu2 (Postfix) with ESMTP id 3B8CC3FA71; Tue, 6 Aug 2019 16:45:28 +0200 (CEST) From: Nicolas Gaullier To: ffmpeg-devel@ffmpeg.org Date: Tue, 6 Aug 2019 17:08:12 +0200 Message-Id: <20190806150814.5676-2-nicolas.gaullier@arkena.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20190806150814.5676-1-nicolas.gaullier@arkena.com> References: <20190806150814.5676-1-nicolas.gaullier@arkena.com> Subject: [FFmpeg-devel] [PATCH v3 3/6] avformat/s337m: Make available as subdemuxer 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavformat/s337m.c | 64 ++++++++++++++++++++++++++++++++++++++++++++--------- libavformat/s337m.h | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 libavformat/s337m.h diff --git a/libavformat/s337m.c b/libavformat/s337m.c index 22140297e6..3ec78d36e4 100644 --- a/libavformat/s337m.c +++ b/libavformat/s337m.c @@ -21,17 +21,7 @@ #include "libavutil/intreadwrite.h" #include "avformat.h" #include "spdif.h" - -#define MARKER_16LE 0x72F81F4E -#define MARKER_20LE 0x20876FF0E154 -#define MARKER_24LE 0x72F8961F4EA5 - -#define IS_16LE_MARKER(state) ((state & 0xFFFFFFFF) == MARKER_16LE) -#define IS_20LE_MARKER(state) ((state & 0xF0FFFFF0FFFF) == MARKER_20LE) -#define IS_24LE_MARKER(state) ((state & 0xFFFFFFFFFFFF) == MARKER_24LE) -#define IS_LE_MARKER(state) (IS_16LE_MARKER(state) || IS_20LE_MARKER(state) || IS_24LE_MARKER(state)) - -int avpriv_s337m_get_packet(void *avc, AVIOContext *pb, AVPacket *pkt, int size, enum AVCodecID *codec); +#include "s337m.h" static int s337m_get_offset_and_codec(void *avc, uint64_t state, @@ -129,6 +119,58 @@ static int s337m_probe(const AVProbeData *p) return 0; } +int avpriv_s337m_probe_stream(void *avc, AVIOContext *pb, AVStream **st, int size) +{ + if ( size >= S337M_MIN_OFFSET && + ((*st)->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE || (*st)->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) && + (*st)->codecpar->channels == 2) { + uint8_t *buf; + int64_t data_offset; + int pos = 0; + + size = FFMIN(size, S337M_MAX_OFFSET); + if (!(buf = av_mallocz(size))) { + return AVERROR(ENOMEM); + } + data_offset = avio_tell(pb); + if (avio_read(pb, buf, size) != size) { + av_freep(&buf); + return AVERROR(EIO); + } + avio_seek(pb, data_offset, SEEK_SET); + + while (pos < size - 9 && !buf[pos]) { + pos++; + } + if (pos < size - 9 && pos >= S337M_PROBE_GUARDBAND_MIN_BYTES) { + uint64_t state; + int data_type = -1, data_size, offset; + + if ((*st)->codecpar->bits_per_coded_sample == 16) { + state = AV_RB32(buf + pos); + if (IS_16LE_MARKER(state)) { + data_type = AV_RL16(buf + pos + 4); + data_size = AV_RL16(buf + pos + 6); + } + } else if ((*st)->codecpar->bits_per_coded_sample == 24) { + state = AV_RB48(buf + pos); + if (IS_20LE_MARKER(state) || IS_24LE_MARKER(state)) { + data_type = AV_RL24(buf + pos + 6); + data_size = AV_RL24(buf + pos + 9); + } + } + if (data_type >= 0 && !s337m_get_offset_and_codec(avc, state, data_type, data_size, &offset, &(*st)->codecpar->codec_id)) { + double s337m_phase = pos * 4 / (*st)->codecpar->bits_per_coded_sample / (*st)->codecpar->sample_rate; + av_log(avc, AV_LOG_INFO, "s337m detected with phase = %.6fs\n", s337m_phase); + if ((*st)->codecpar->codec_id == AV_CODEC_ID_DOLBY_E && (s337m_phase < DOLBY_E_PHASE_MIN || s337m_phase > DOLBY_E_PHASE_MAX)) + av_log(avc, AV_LOG_WARNING, "Dolby E phase is out of valid range (%.6fs-%.6fs)\n", DOLBY_E_PHASE_MIN, DOLBY_E_PHASE_MAX); + } + } + av_freep(&buf); + } + return 0; +} + static int s337m_read_header(AVFormatContext *s) { s->ctx_flags |= AVFMTCTX_NOHEADER; diff --git a/libavformat/s337m.h b/libavformat/s337m.h new file mode 100644 index 0000000000..0f21a23a30 --- /dev/null +++ b/libavformat/s337m.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2017 foo86 + * + * 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 + +#define MARKER_16LE 0x72F81F4E +#define MARKER_20LE 0x20876FF0E154 +#define MARKER_24LE 0x72F8961F4EA5 + +#define IS_16LE_MARKER(state) ((state & 0xFFFFFFFF) == MARKER_16LE) +#define IS_20LE_MARKER(state) ((state & 0xF0FFFFF0FFFF) == MARKER_20LE) +#define IS_24LE_MARKER(state) ((state & 0xFFFFFFFFFFFF) == MARKER_24LE) +#define IS_LE_MARKER(state) (IS_16LE_MARKER(state) || IS_20LE_MARKER(state) || IS_24LE_MARKER(state)) + +#define S337M_MIN_OFFSET 1601*4 +#define S337M_MAX_OFFSET 2002*6 + +#define S337M_PROBE_GUARDBAND_MIN_BYTES 0 +#define DOLBY_E_PHASE_MIN 0.000610 +#define DOLBY_E_PHASE_MAX 0.001050 + +/** + * Detect s337m packets in a PCM_S16LE/S24LE stereo stream + * If found, the codec of the stream is updated + * Requires a single sample with enough (S337M_PROBE_GUARDBAND_MIN_BYTES) and clean (set to zero) guard band + * @param avc For av_log + * @param pb Associated IO context + * @param st Streams + * @param size Maximum IO read size available for probing at current position + * @return = 0 if no error encountered (even if no s337m was found) + */ +int avpriv_s337m_probe_stream(void *avc, AVIOContext *pb, AVStream **st, int size); + +/** + * Read s337m packets in a PCM_S16LE/S24LE stereo stream + * Returns the first inner packet found + * @param avc For av_log + * @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 + * @return = 0 on success (an error is raised if no s337m was found) + */ +int avpriv_s337m_get_packet(void *avc, AVIOContext *pb, AVPacket *pkt, int size, enum AVCodecID *codec); + +#endif /* AVFORMAT_S337M_H */