From patchwork Sun Mar 29 11:13:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 18484 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 57ED744927C for ; Sun, 29 Mar 2020 14:14:13 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4053568B436; Sun, 29 Mar 2020 14:14:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A06EE68B051 for ; Sun, 29 Mar 2020 14:14:06 +0300 (EEST) Received: by mail-wr1-f65.google.com with SMTP id h15so17413173wrx.9 for ; Sun, 29 Mar 2020 04:14:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4hR2PHSzlzacyERwY7IEx4xOtLw6KdYHUU+rCcPvTck=; b=qVFukzLSytH0NTSG+RmOIFxPIXpDoIoEa6mb9nnNtRc2rn0cO+rVcBv8Jrn0LAkiGX JtCSV2SQ8xJXtBajAR66cVK+lWZD+LpS21kgYc2/GlfwZVI0TUPS6Kn1unPsMvaYfHko 4ShEwsfvOJ0IAJ9p47tcRbw8v/G6b7jBY56yRLBLmG2cso46MX81Q+Srf12kcgwEAhSp nSiOMSR5sHBoqm+s05Zrxu1uuveq4zWpJUB/KOpemLaDKB8LpuXOqvS/QMJPIA9S5U+c mdImE8bKm+WDAKORunRfQGzge3G6Fc9Msl/ox9shslMdT7VGDKSVQ7r10qOWG1opnAQV 5vKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4hR2PHSzlzacyERwY7IEx4xOtLw6KdYHUU+rCcPvTck=; b=Ndrd6lg414qQjxQ75sIe1piuQV2syB8k5V6FVdUdQ8X6s8JMPOlWoXa6nj1zy3/5jG kDyG0SG8rB10V73NDAAhvMkZPBw4MnLw+pE4lzEXsyOeRgBVUirOq1vXCMj6DAod6k2r 8beLS6Zl5qcScw7NFe3Sfk2eXp75zMb4Qu+IrHfSaFD9lyvLpb5RHVOo++8ylVRNb/XI LfDJHadunS9LSpkXsjrkpEloqf3BbHAOqEwmkFnzAovhFuWOBDyejSxeBlZJ1Aoy8oRn DhwxsWB7W2+wcgkyYJ1IaFxfDHXmuht2zPJatM9PYQMzVzAJ0D2jNzz2RaaeV3+pTF8A gE3Q== X-Gm-Message-State: ANhLgQ2c5yB+H4gtYQx+VDOpwu2pibWqplZzme0MCZPkEliE554I5n6d pWYf6uwItL4Zp0hxRCNLC+KdB9IL X-Google-Smtp-Source: ADFU+vu/q4pjL2uX+wc/RaSmO2GJ4QxveAJB/7zpfjQBMXhjIkA1D4r2D32FyDvRT2V3yjPvn3xSpA== X-Received: by 2002:adf:a190:: with SMTP id u16mr9500021wru.225.1585480445551; Sun, 29 Mar 2020 04:14:05 -0700 (PDT) Received: from sblaptop.fritz.box (ipbcc1ab57.dynamic.kabel-deutschland.de. [188.193.171.87]) by smtp.gmail.com with ESMTPSA id f12sm16122672wmh.4.2020.03.29.04.14.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Mar 2020 04:14:05 -0700 (PDT) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Sun, 29 Mar 2020 13:13:22 +0200 Message-Id: <20200329111325.2686-2-andreas.rheinhardt@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200329111325.2686-1-andreas.rheinhardt@gmail.com> References: <20200329111325.2686-1-andreas.rheinhardt@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/5] avformat/smacker: Create audio streams immediately 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: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The Smacker demuxer currently parses several fields that indicate how many audio streams a file contains. This data is parsed and stored into arrays in the demuxer's context and although the data is used only to initialize the audio streams, it is kept for the whole lifetime of the demuxer. This has been changed: The data is used directly to create the audio streams and no longer kept at all. This also simplifies error handling in case adding a new stream fails: Several arrays which until now have been allocated between parsing the data determining how many audio streams to create and actually creating them would need to be freed in this case. Now the streams are created first, so freeing is no longer an issue. Signed-off-by: Andreas Rheinhardt --- libavformat/smacker.c | 82 ++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/libavformat/smacker.c b/libavformat/smacker.c index 4db3ec326f..b08f7bb4e6 100644 --- a/libavformat/smacker.c +++ b/libavformat/smacker.c @@ -51,8 +51,6 @@ typedef struct SmackerContext { uint32_t flags; uint32_t audio[7]; uint32_t treesize; - uint8_t aflags[7]; - uint32_t rates[7]; uint32_t pad; /* frame info */ uint32_t *frm_size; @@ -107,7 +105,7 @@ static int smacker_read_header(AVFormatContext *s) { AVIOContext *pb = s->pb; SmackerContext *smk = s->priv_data; - AVStream *st, *ast[7]; + AVStream *st; int i, ret; int tbase; @@ -153,9 +151,45 @@ static int smacker_read_header(AVFormatContext *s) if ((ret = ffio_read_size(pb, st->codecpar->extradata, 16)) < 0) return ret; + /* handle possible audio streams */ for(i = 0; i < 7; i++) { - smk->rates[i] = avio_rl24(pb); - smk->aflags[i] = avio_r8(pb); + uint32_t rate = avio_rl24(pb); + uint8_t aflag = avio_r8(pb); + + smk->indexes[i] = -1; + + if (rate) { + AVStream *ast = avformat_new_stream(s, NULL); + if (!ast) + return AVERROR(ENOMEM); + + smk->indexes[i] = ast->index; + ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; + if (aflag & SMK_AUD_BINKAUD) { + ast->codecpar->codec_id = AV_CODEC_ID_BINKAUDIO_RDFT; + } else if (aflag & SMK_AUD_USEDCT) { + ast->codecpar->codec_id = AV_CODEC_ID_BINKAUDIO_DCT; + } else if (aflag & SMK_AUD_PACKED) { + ast->codecpar->codec_id = AV_CODEC_ID_SMACKAUDIO; + ast->codecpar->codec_tag = MKTAG('S', 'M', 'K', 'A'); + } else { + ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8; + } + if (aflag & SMK_AUD_STEREO) { + ast->codecpar->channels = 2; + ast->codecpar->channel_layout = AV_CH_LAYOUT_STEREO; + } else { + ast->codecpar->channels = 1; + ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO; + } + ast->codecpar->sample_rate = rate; + ast->codecpar->bits_per_coded_sample = (aflag & SMK_AUD_16BITS) ? 16 : 8; + if (ast->codecpar->bits_per_coded_sample == 16 && + ast->codecpar->codec_id == AV_CODEC_ID_PCM_U8) + ast->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; + avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate + * ast->codecpar->channels * ast->codecpar->bits_per_coded_sample / 8); + } } smk->pad = avio_rl32(pb); /* setup data */ @@ -194,44 +228,6 @@ static int smacker_read_header(AVFormatContext *s) av_reduce(&tbase, &smk->pts_inc, tbase, smk->pts_inc, (1UL<<31)-1); avpriv_set_pts_info(st, 33, smk->pts_inc, tbase); st->duration = smk->frames; - /* handle possible audio streams */ - for(i = 0; i < 7; i++) { - smk->indexes[i] = -1; - if (smk->rates[i]) { - ast[i] = avformat_new_stream(s, NULL); - if (!ast[i]) { - av_freep(&smk->frm_size); - av_freep(&smk->frm_flags); - return AVERROR(ENOMEM); - } - smk->indexes[i] = ast[i]->index; - ast[i]->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; - if (smk->aflags[i] & SMK_AUD_BINKAUD) { - ast[i]->codecpar->codec_id = AV_CODEC_ID_BINKAUDIO_RDFT; - } else if (smk->aflags[i] & SMK_AUD_USEDCT) { - ast[i]->codecpar->codec_id = AV_CODEC_ID_BINKAUDIO_DCT; - } else if (smk->aflags[i] & SMK_AUD_PACKED){ - ast[i]->codecpar->codec_id = AV_CODEC_ID_SMACKAUDIO; - ast[i]->codecpar->codec_tag = MKTAG('S', 'M', 'K', 'A'); - } else { - ast[i]->codecpar->codec_id = AV_CODEC_ID_PCM_U8; - } - if (smk->aflags[i] & SMK_AUD_STEREO) { - ast[i]->codecpar->channels = 2; - ast[i]->codecpar->channel_layout = AV_CH_LAYOUT_STEREO; - } else { - ast[i]->codecpar->channels = 1; - ast[i]->codecpar->channel_layout = AV_CH_LAYOUT_MONO; - } - ast[i]->codecpar->sample_rate = smk->rates[i]; - ast[i]->codecpar->bits_per_coded_sample = (smk->aflags[i] & SMK_AUD_16BITS) ? 16 : 8; - if(ast[i]->codecpar->bits_per_coded_sample == 16 && ast[i]->codecpar->codec_id == AV_CODEC_ID_PCM_U8) - ast[i]->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE; - avpriv_set_pts_info(ast[i], 64, 1, ast[i]->codecpar->sample_rate - * ast[i]->codecpar->channels * ast[i]->codecpar->bits_per_coded_sample / 8); - } - } - /* load trees to extradata, they will be unpacked by decoder */ ret = avio_read(pb, st->codecpar->extradata + 16, st->codecpar->extradata_size - 16);