From patchwork Tue Mar 16 08:30:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zane van Iperen X-Patchwork-Id: 26410 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 BF16744B21B for ; Tue, 16 Mar 2021 10:30:36 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A283868AF87; Tue, 16 Mar 2021 10:30:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from out2.migadu.com (out2.migadu.com [188.165.223.204]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DDFB068AF85 for ; Tue, 16 Mar 2021 10:30:29 +0200 (EET) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zanevaniperen.com; s=key1; t=1615883428; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EUMutLR4yg+OCQ2SpkcHKKPgEmLmhRlT87e1ZkYoI80=; b=GEYr06B/g0AoBqW/H1QNkNHLyYaBsEi706iFLCCwF3ImGLeRY6AHihpADTU3OfRnumtiJj 84974CD89S4x50UtnaPZIGFxaQI1V21GbU67zfpe7qLiKggAPzsiTD2JjCjFxYgICASBl9 L41aTx4MDeBdcndyDHNSUDGBbYV4yDgMHDntSmvbrscepIX17iKC9GNrCYPHWC9zNXoHaG kBfw85Yrp69N2Zu0RnZRh3HvMKRstHbTc1nhHMqGusk6hv0XEVuJTF/79eF0GIB/wBFWfi yag1Sxbs3yoRfgmcxJu2Hhf+hYw/C88iFYlelSWN4DkWAQVNgrbYrHVFa1J3IA== From: Zane van Iperen To: ffmpeg-devel@ffmpeg.org Date: Tue, 16 Mar 2021 18:30:19 +1000 Message-Id: <20210316083019.25686-1-zane@zanevaniperen.com> In-Reply-To: <87eb807f-e604-e675-3494-eb0476148712@gmail.com> References: <87eb807f-e604-e675-3494-eb0476148712@gmail.com> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: git-morningstar@zanevaniperen.com Subject: [FFmpeg-devel] [PATCH v2] avformat/pp_bnk: treat music files are stereo 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" These files are technically a series of planar mono tracks. If the "music" flag is set, merge the packets from the two mono tracks, essentially replicating: [0:a:0][0:a:1]join=inputs=2:channel_layout=stereo[a] Signed-off-by: Zane van Iperen --- libavformat/pp_bnk.c | 51 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/libavformat/pp_bnk.c b/libavformat/pp_bnk.c index 8364de1fd9..eba53a01a3 100644 --- a/libavformat/pp_bnk.c +++ b/libavformat/pp_bnk.c @@ -55,6 +55,7 @@ typedef struct PPBnkCtx { int track_count; PPBnkCtxTrack *tracks; uint32_t current_track; + int is_music; } PPBnkCtx; enum { @@ -194,8 +195,12 @@ static int pp_bnk_read_header(AVFormatContext *s) goto fail; } + ctx->is_music = (hdr.flags & PP_BNK_FLAG_MUSIC) && + (ctx->track_count == 2) && + (ctx->tracks[0].data_size == ctx->tracks[1].data_size); + /* Build the streams. */ - for (int i = 0; i < ctx->track_count; i++) { + for (int i = 0; i < (ctx->is_music ? 1 : ctx->track_count); i++) { if (!(st = avformat_new_stream(s, NULL))) { ret = AVERROR(ENOMEM); goto fail; @@ -204,14 +209,21 @@ static int pp_bnk_read_header(AVFormatContext *s) par = st->codecpar; par->codec_type = AVMEDIA_TYPE_AUDIO; par->codec_id = AV_CODEC_ID_ADPCM_IMA_CUNNING; - par->format = AV_SAMPLE_FMT_S16; - par->channel_layout = AV_CH_LAYOUT_MONO; - par->channels = 1; + par->format = AV_SAMPLE_FMT_S16P; + + if (ctx->is_music) { + par->channel_layout = AV_CH_LAYOUT_STEREO; + par->channels = 2; + } else { + par->channel_layout = AV_CH_LAYOUT_MONO; + par->channels = 1; + } + par->sample_rate = hdr.sample_rate; par->bits_per_coded_sample = 4; par->bits_per_raw_sample = 16; par->block_align = 1; - par->bit_rate = par->sample_rate * par->bits_per_coded_sample; + par->bit_rate = par->sample_rate * par->bits_per_coded_sample * par->channels; avpriv_set_pts_info(st, 64, 1, par->sample_rate); st->start_time = 0; @@ -253,7 +265,22 @@ static int pp_bnk_read_packet(AVFormatContext *s, AVPacket *pkt) size = FFMIN(trk->data_size - trk->bytes_read, PP_BNK_MAX_READ_SIZE); - if ((ret = av_get_packet(s->pb, pkt, size)) == AVERROR_EOF) { + if (!ctx->is_music) + ret = av_new_packet(pkt, size); + else if (ctx->current_track == 0) + ret = av_new_packet(pkt, size * 2); + else + ret = 0; + + if (ret < 0) + return ret; + + if (ctx->is_music) + ret = avio_read(s->pb, pkt->data + size * ctx->current_track, size); + else + ret = avio_read(s->pb, pkt->data, size); + + if (ret == AVERROR_EOF) { /* If we've hit EOF, don't attempt this track again. */ trk->data_size = trk->bytes_read; continue; @@ -265,6 +292,18 @@ static int pp_bnk_read_packet(AVFormatContext *s, AVPacket *pkt) pkt->flags &= ~AV_PKT_FLAG_CORRUPT; pkt->stream_index = ctx->current_track++; pkt->duration = ret * 2; + + if (ctx->is_music) { + if (pkt->stream_index == 0) { + ctx->current_track--; + continue; + } + + pkt->stream_index = 0; + } else { + pkt->size = ret; + } + return 0; }