From patchwork Fri Jul 26 16:45:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gaullier Nicolas X-Patchwork-Id: 14087 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 DB4B0449EBD for ; Fri, 26 Jul 2019 19:45:28 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C628E68AC8D; Fri, 26 Jul 2019 19:45:28 +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 547F668AC68 for ; Fri, 26 Jul 2019 19:45:21 +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 45wFLm6y53zHh8x; Fri, 26 Jul 2019 16:45:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arkena.com; s=20150421; t=1564159520; bh=I2CtAE3nGxwJjMMPAX0bggb1OTnatMMUq5H8Um5qorY=; h=From:To:Cc:Subject:Date:Message-Id; b=nw3lp00zP0akrGZML0RsHJ9VEo2fr+QWNpzPhwKiwS5MABI0dUbumonr7zZJ4yc5e SR2v7lPPGRIDUDUotvbOP7HYj+g7g9k7eBDBkCWgcdiNy0QcyHt2kNNK2RXSpcXp+N gw/wqeJJstCbkyjQ8k9Ihyf6567JycVBXdoshuq8= Received: from arkena.com (unknown [172.16.3.159]) by secu2 (Postfix) with ESMTP id 8EC5F3FA6D; Fri, 26 Jul 2019 18:23:11 +0200 (CEST) From: Nicolas Gaullier To: ffmpeg-devel@ffmpeg.org Date: Fri, 26 Jul 2019 18:45:15 +0200 Message-Id: <20190726164517.8428-2-nicolas.gaullier@arkena.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20190726164517.8428-1-nicolas.gaullier@arkena.com> References: <20190726164517.8428-1-nicolas.gaullier@arkena.com> Subject: [FFmpeg-devel] [PATCH 1/3] 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 | 57 +++++++++++++++++++++++++++++++++++++++++++---------- libavformat/s337m.h | 48 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 10 deletions(-) create mode 100644 libavformat/s337m.h diff --git a/libavformat/s337m.c b/libavformat/s337m.c index 48ab66a6da..589fec53a9 100644 --- a/libavformat/s337m.c +++ b/libavformat/s337m.c @@ -21,15 +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)) +#include "s337m.h" static int s337m_get_offset_and_codec(AVFormatContext *s, uint64_t state, @@ -141,7 +133,7 @@ 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 s337m_read_packet(AVFormatContext *s, AVPacket *pkt) { AVIOContext *pb = s->pb; uint64_t state = 0; @@ -204,3 +196,48 @@ AVInputFormat ff_s337m_demuxer = { .read_packet = s337m_read_packet, .flags = AVFMT_GENERIC_INDEX, }; + +int s337m_probe_stream(AVFormatContext *s, AVStream **st) +{ + if (((*st)->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE || (*st)->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) && + (*st)->codecpar->channels == 2) + { + int64_t avio_current_offset; + int bufsize, pos = 0; + double s337m_phase; + uint8_t *buf = av_mallocz(S337M_MAX_OFFSET); + if (!buf) + return AVERROR(ENOMEM); + avio_current_offset = avio_tell(s->pb); + bufsize = avio_read(s->pb, buf, S337M_MAX_OFFSET); + avio_seek(s->pb, avio_current_offset, SEEK_SET); + while (pos < bufsize - 5 && buf[pos] == 0) + pos++; + s337m_phase = (double)pos * 4 / (*st)->codecpar->bits_per_coded_sample / (*st)->codecpar->sample_rate; + if (s337m_phase >= S337M_PHASE_PROBE_MIN && pos < bufsize - 5) { + 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); + } + } + av_freep(&buf); + if (data_type >= 0 && !s337m_get_offset_and_codec(s, state, data_type, data_size, &offset, &(*st)->codecpar->codec_id)) + { + av_log(s, 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(s, AV_LOG_WARNING, "Dolby E phase is out of valid range (%.6fs-%.6fs)\n", DOLBY_E_PHASE_MIN, DOLBY_E_PHASE_MAX); + } + } + } + return 0; +} diff --git a/libavformat/s337m.h b/libavformat/s337m.h new file mode 100644 index 0000000000..900b966233 --- /dev/null +++ b/libavformat/s337m.h @@ -0,0 +1,48 @@ +/* + * 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_MAX_OFFSET 2002*6 + +#define S337M_PHASE_PROBE_MIN 0.000000 +#define DOLBY_E_PHASE_MIN 0.000610 +#define DOLBY_E_PHASE_MAX 0.001050 + +/** + * Detect s337m synch words in a PCM_S16LE/S24LE stereo stream + * On success, codec is updated + * Requires first sample to have enough and clean guard band (S337M_PHASE_PROBE_MIN, set to zero) + */ +int s337m_probe_stream(AVFormatContext *s, AVStream **st); + +int s337m_read_packet(AVFormatContext *s, AVPacket *pkt); + +#endif /* AVFORMAT_S337M_H */