From patchwork Mon Sep 7 13:14:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zane van Iperen X-Patchwork-Id: 22152 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 294C544A117 for ; Mon, 7 Sep 2020 16:14:25 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 065F668AB33; Mon, 7 Sep 2020 16:14:25 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail2.protonmail.ch (mail2.protonmail.ch [185.70.40.22]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 37E776880DE for ; Mon, 7 Sep 2020 16:14:19 +0300 (EEST) Date: Mon, 07 Sep 2020 13:14:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zanevaniperen.com; s=protonmail2; t=1599484458; bh=mjWPqnMVz3Tk0sOL66SIFA/lpJc+zCCxrbnLYV56PGo=; h=Date:To:From:Cc:Reply-To:Subject:In-Reply-To:References:From; b=erYYQp7NWRJ+IFeUI1n1LAnuZkp8W4silaznhUr/35lxeCATGcskLf5Cq3/2ST3OR eVgPkdhwOV7mIlDZYvyKpXJ3gj7hQEsQcvhMvLZ0+/m1hYMw4AftimpPG6wcntRKFu 6DqLj7B/4V524kv9IZ47PYE+jOpYHWz8Wb4RRga0pazTjVuVTM5JouokZSq1jrAJ3A 7re8NZzytIOPxiC+Vt0PmC5/W1oOqj1sGG+1r1+PvxDn9Mge/joPY8gcU9JE7Ue6hA mFs8VHJwgsoXck4IfX//BoJPvFvEUTcjJ7tfQ6qsrJXEn2QJSE8Z8mjbOPLbGKiA5Y yjCnVit1Qj4zQ== To: ffmpeg-devel@ffmpeg.org From: Zane van Iperen Message-ID: <20200907131326.32737-3-zane@zanevaniperen.com> In-Reply-To: <20200907131326.32737-1-zane@zanevaniperen.com> References: <20200907131326.32737-1-zane@zanevaniperen.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.2 required=10.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on mailout.protonmail.ch Subject: [FFmpeg-devel] [PATCH 2/5] avformat/argo_asf: split functionality into a header 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: Zane van Iperen Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" For future use by the argo_brp demuxer. Adds: - void ff_argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf); - int ff_argo_asf_validate_file_header(AVFormatContext *s, const ArgoASFFileHeader *hdr); - void ff_argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf); - int ff_argo_asf_fill_stream(AVStream *st, const ArgoASFChunkHeader *ckhdr); Signed-off-by: Zane van Iperen --- libavformat/argo_asf.c | 204 +++++++++++++++++++---------------------- libavformat/argo_asf.h | 67 ++++++++++++++ 2 files changed, 159 insertions(+), 112 deletions(-) create mode 100644 libavformat/argo_asf.h diff --git a/libavformat/argo_asf.c b/libavformat/argo_asf.c index 208280b4d9..ee8c85f735 100644 --- a/libavformat/argo_asf.c +++ b/libavformat/argo_asf.c @@ -24,39 +24,7 @@ #include "libavutil/intreadwrite.h" #include "libavutil/avassert.h" #include "libavutil/opt.h" - -#define ASF_TAG MKTAG('A', 'S', 'F', '\0') -#define ASF_FILE_HEADER_SIZE 24 -#define ASF_CHUNK_HEADER_SIZE 20 -#define ASF_SAMPLE_COUNT 32 - -typedef struct ArgoASFFileHeader { - uint32_t magic; /*< Magic Number, {'A', 'S', 'F', '\0'} */ - uint16_t version_major; /*< File Major Version. */ - uint16_t version_minor; /*< File Minor Version. */ - uint32_t num_chunks; /*< No. chunks in the file. */ - uint32_t chunk_offset; /*< Offset to the first chunk from the start of the file. */ - int8_t name[8]; /*< Name. */ -} ArgoASFFileHeader; - -typedef struct ArgoASFChunkHeader { - uint32_t num_blocks; /*< No. blocks in the chunk. */ - uint32_t num_samples; /*< No. samples per channel in a block. Always 32. */ - uint32_t unk1; /*< Unknown */ - uint16_t sample_rate; /*< Sample rate. */ - uint16_t unk2; /*< Unknown. */ - uint32_t flags; /*< Stream flags. */ -} ArgoASFChunkHeader; - -enum { - ASF_CF_BITS_PER_SAMPLE = (1 << 0), /*< 16-bit if set, 8 otherwise. */ - ASF_CF_STEREO = (1 << 1), /*< Stereo if set, mono otherwise. */ - ASF_CF_ALWAYS1_1 = (1 << 2), /*< Unknown, always seems to be set. */ - ASF_CF_ALWAYS1_2 = (1 << 3), /*< Unknown, always seems to be set. */ - - ASF_CF_ALWAYS1 = ASF_CF_ALWAYS1_1 | ASF_CF_ALWAYS1_2, - ASF_CF_ALWAYS0 = ~(ASF_CF_BITS_PER_SAMPLE | ASF_CF_STEREO | ASF_CF_ALWAYS1) -}; +#include "argo_asf.h" typedef struct ArgoASFDemuxContext { ArgoASFFileHeader fhdr; @@ -71,8 +39,7 @@ typedef struct ArgoASFMuxContext { const char *name; } ArgoASFMuxContext; -#if CONFIG_ARGO_ASF_DEMUXER -static void argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf) +void ff_argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf) { hdr->magic = AV_RL32(buf + 0); hdr->version_major = AV_RL16(buf + 4); @@ -83,7 +50,23 @@ static void argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *bu hdr->name[i] = AV_RL8(buf + 16 + i); } -static void argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf) +int ff_argo_asf_validate_file_header(AVFormatContext *s, const ArgoASFFileHeader *hdr) +{ + if (hdr->magic != ASF_TAG || hdr->num_chunks == 0) + return AVERROR_INVALIDDATA; + + if (hdr->num_chunks > 1) { + avpriv_request_sample(s, ">1 chunk"); + return AVERROR_PATCHWELCOME; + } + + if (hdr->chunk_offset < ASF_FILE_HEADER_SIZE) + return AVERROR_INVALIDDATA; + + return 0; +} + +void ff_argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf) { hdr->num_blocks = AV_RL32(buf + 0); hdr->num_samples = AV_RL32(buf + 4); @@ -93,6 +76,73 @@ static void argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t * hdr->flags = AV_RL32(buf + 16); } +int ff_argo_asf_fill_stream(AVStream *st, const ArgoASFFileHeader *fhdr, + const ArgoASFChunkHeader *ckhdr) +{ + if (ckhdr->num_samples != ASF_SAMPLE_COUNT) { + av_log(st, AV_LOG_ERROR, "Invalid sample count. Got %u, expected %d\n", + ckhdr->num_samples, ASF_SAMPLE_COUNT); + return AVERROR_INVALIDDATA; + } + + if ((ckhdr->flags & ASF_CF_ALWAYS1) != ASF_CF_ALWAYS1 || (ckhdr->flags & ASF_CF_ALWAYS0) != 0) { + avpriv_request_sample(st, "Nonstandard flags (0x%08X)", ckhdr->flags); + return AVERROR_PATCHWELCOME; + } + + st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; + st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO; + st->codecpar->format = AV_SAMPLE_FMT_S16P; + + if (ckhdr->flags & ASF_CF_STEREO) { + st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO; + st->codecpar->channels = 2; + } else { + st->codecpar->channel_layout = AV_CH_LAYOUT_MONO; + st->codecpar->channels = 1; + } + + /* v1.1 files (FX Fighter) are all marked as 44100, but are actually 22050. */ + if (fhdr->version_major == 1 && fhdr->version_minor == 1) + st->codecpar->sample_rate = 22050; + else + st->codecpar->sample_rate = ckhdr->sample_rate; + + st->codecpar->bits_per_coded_sample = 4; + + if (ckhdr->flags & ASF_CF_BITS_PER_SAMPLE) + st->codecpar->bits_per_raw_sample = 16; + else + st->codecpar->bits_per_raw_sample = 8; + + if (st->codecpar->bits_per_raw_sample != 16) { + /* The header allows for these, but I've never seen any files with them. */ + avpriv_request_sample(st, "Non 16-bit samples"); + return AVERROR_PATCHWELCOME; + } + + /* + * (nchannel control bytes) + ((bytes_per_channel) * nchannel) + * For mono, this is 17. For stereo, this is 34. + */ + st->codecpar->frame_size = st->codecpar->channels + + (ckhdr->num_samples / 2) * + st->codecpar->channels; + + st->codecpar->block_align = st->codecpar->frame_size; + + st->codecpar->bit_rate = st->codecpar->channels * + st->codecpar->sample_rate * + st->codecpar->bits_per_coded_sample; + + avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); + st->start_time = 0; + st->duration = ckhdr->num_blocks * ckhdr->num_samples; + st->nb_frames = ckhdr->num_blocks; + return 0; +} + +#if CONFIG_ARGO_ASF_DEMUXER /* * Known versions: * 1.1: https://samples.ffmpeg.org/game-formats/brender/part2.zip @@ -115,7 +165,7 @@ static int argo_asf_probe(const AVProbeData *p) av_assert0(AVPROBE_PADDING_SIZE >= ASF_FILE_HEADER_SIZE); - argo_asf_parse_file_header(&hdr, p->buf); + ff_argo_asf_parse_file_header(&hdr, p->buf); if (hdr.magic != ASF_TAG) return 0; @@ -142,20 +192,10 @@ static int argo_asf_read_header(AVFormatContext *s) else if (ret != ASF_FILE_HEADER_SIZE) return AVERROR(EIO); - argo_asf_parse_file_header(&asf->fhdr, buf); - - if (asf->fhdr.magic != ASF_TAG) - return AVERROR_INVALIDDATA; - - if (asf->fhdr.num_chunks == 0) { - return AVERROR_INVALIDDATA; - } else if (asf->fhdr.num_chunks > 1) { - avpriv_request_sample(s, ">1 chunk"); - return AVERROR_PATCHWELCOME; - } + ff_argo_asf_parse_file_header(&asf->fhdr, buf); - if (asf->fhdr.chunk_offset < ASF_FILE_HEADER_SIZE) - return AVERROR_INVALIDDATA; + if ((ret = ff_argo_asf_validate_file_header(s, &asf->fhdr)) < 0) + return ret; if ((ret = avio_skip(pb, asf->fhdr.chunk_offset - ASF_FILE_HEADER_SIZE)) < 0) return ret; @@ -165,69 +205,9 @@ static int argo_asf_read_header(AVFormatContext *s) else if (ret != ASF_CHUNK_HEADER_SIZE) return AVERROR(EIO); - argo_asf_parse_chunk_header(&asf->ckhdr, buf); - - if (asf->ckhdr.num_samples != ASF_SAMPLE_COUNT) { - av_log(s, AV_LOG_ERROR, "Invalid sample count. Got %u, expected %d\n", - asf->ckhdr.num_samples, ASF_SAMPLE_COUNT); - return AVERROR_INVALIDDATA; - } - - if ((asf->ckhdr.flags & ASF_CF_ALWAYS1) != ASF_CF_ALWAYS1 || (asf->ckhdr.flags & ASF_CF_ALWAYS0) != 0) { - avpriv_request_sample(s, "Nonstandard flags (0x%08X)", asf->ckhdr.flags); - return AVERROR_PATCHWELCOME; - } - - st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; - st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO; - st->codecpar->format = AV_SAMPLE_FMT_S16P; - - if (asf->ckhdr.flags & ASF_CF_STEREO) { - st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO; - st->codecpar->channels = 2; - } else { - st->codecpar->channel_layout = AV_CH_LAYOUT_MONO; - st->codecpar->channels = 1; - } - - /* v1.1 files (FX Fighter) are all marked as 44100, but are actually 22050. */ - if (asf->fhdr.version_major == 1 && asf->fhdr.version_minor == 1) - st->codecpar->sample_rate = 22050; - else - st->codecpar->sample_rate = asf->ckhdr.sample_rate; - - st->codecpar->bits_per_coded_sample = 4; - - if (asf->ckhdr.flags & ASF_CF_BITS_PER_SAMPLE) - st->codecpar->bits_per_raw_sample = 16; - else - st->codecpar->bits_per_raw_sample = 8; - - if (st->codecpar->bits_per_raw_sample != 16) { - /* The header allows for these, but I've never seen any files with them. */ - avpriv_request_sample(s, "Non 16-bit samples"); - return AVERROR_PATCHWELCOME; - } + ff_argo_asf_parse_chunk_header(&asf->ckhdr, buf); - /* - * (nchannel control bytes) + ((bytes_per_channel) * nchannel) - * For mono, this is 17. For stereo, this is 34. - */ - st->codecpar->frame_size = st->codecpar->channels + - (asf->ckhdr.num_samples / 2) * - st->codecpar->channels; - - st->codecpar->block_align = st->codecpar->frame_size; - - st->codecpar->bit_rate = st->codecpar->channels * - st->codecpar->sample_rate * - st->codecpar->bits_per_coded_sample; - - avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); - st->start_time = 0; - st->duration = asf->ckhdr.num_blocks * asf->ckhdr.num_samples; - st->nb_frames = asf->ckhdr.num_blocks; - return 0; + return ff_argo_asf_fill_stream(st, &asf->fhdr, &asf->ckhdr); } static int argo_asf_read_packet(AVFormatContext *s, AVPacket *pkt) diff --git a/libavformat/argo_asf.h b/libavformat/argo_asf.h new file mode 100644 index 0000000000..905769dafe --- /dev/null +++ b/libavformat/argo_asf.h @@ -0,0 +1,67 @@ +/* + * Argonaut Games ASF (de)muxer + * + * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com) + * + * 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_ARGO_ASF_H +#define AVFORMAT_ARGO_ASF_H + +#include + +#define ASF_TAG MKTAG('A', 'S', 'F', '\0') +#define ASF_FILE_HEADER_SIZE 24 +#define ASF_CHUNK_HEADER_SIZE 20 +#define ASF_SAMPLE_COUNT 32 + +typedef struct ArgoASFFileHeader { + uint32_t magic; /*< Magic Number, {'A', 'S', 'F', '\0'} */ + uint16_t version_major; /*< File Major Version. */ + uint16_t version_minor; /*< File Minor Version. */ + uint32_t num_chunks; /*< No. chunks in the file. */ + uint32_t chunk_offset; /*< Offset to the first chunk from the start of the file. */ + int8_t name[8]; /*< Name. */ +} ArgoASFFileHeader; + +typedef struct ArgoASFChunkHeader { + uint32_t num_blocks; /*< No. blocks in the chunk. */ + uint32_t num_samples; /*< No. samples per channel in a block. Always 32. */ + uint32_t unk1; /*< Unknown */ + uint16_t sample_rate; /*< Sample rate. */ + uint16_t unk2; /*< Unknown. */ + uint32_t flags; /*< Stream flags. */ +} ArgoASFChunkHeader; + +enum { + ASF_CF_BITS_PER_SAMPLE = (1 << 0), /*< 16-bit if set, 8 otherwise. */ + ASF_CF_STEREO = (1 << 1), /*< Stereo if set, mono otherwise. */ + ASF_CF_ALWAYS1_1 = (1 << 2), /*< Unknown, always seems to be set. */ + ASF_CF_ALWAYS1_2 = (1 << 3), /*< Unknown, always seems to be set. */ + + ASF_CF_ALWAYS1 = ASF_CF_ALWAYS1_1 | ASF_CF_ALWAYS1_2, + ASF_CF_ALWAYS0 = ~(ASF_CF_BITS_PER_SAMPLE | ASF_CF_STEREO | ASF_CF_ALWAYS1) +}; + +void ff_argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf); +int ff_argo_asf_validate_file_header(AVFormatContext *s, const ArgoASFFileHeader *hdr); +void ff_argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf); +int ff_argo_asf_fill_stream(AVStream *st, const ArgoASFFileHeader *fhdr, + const ArgoASFChunkHeader *ckhdr); + +#endif /* AVFORMAT_ARGO_ASF_H */