From patchwork Fri Feb 26 05:28:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lynne X-Patchwork-Id: 25995 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 D5E8144A3DF for ; Fri, 26 Feb 2021 07:28:33 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 62EB468A569; Fri, 26 Feb 2021 07:28:33 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from w4.tutanota.de (w4.tutanota.de [81.3.6.165]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4084168A562 for ; Fri, 26 Feb 2021 07:28:27 +0200 (EET) Received: from w3.tutanota.de (unknown [192.168.1.164]) by w4.tutanota.de (Postfix) with ESMTP id E12DF106026C for ; Fri, 26 Feb 2021 05:28:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1614317306; s=s1; d=lynne.ee; h=From:From:To:To:Subject:Subject:Content-Description:Content-ID:Content-Type:Content-Type:Content-Transfer-Encoding:Cc:Date:Date:In-Reply-To:MIME-Version:MIME-Version:Message-ID:Message-ID:Reply-To:References:Sender; bh=8iqdyF544zJmgxoj+Lm/Fj/fUkWGV9WeCp1b4WdC5mU=; b=l+A/XVi+RVz9bZbnh13WH1pbvz2o+UOhRRl1IlOoLOZHwr+E2AYGR1XbpZNHfnNw RCrOEPnHtybHZe8SFxTFPkDuB8OX2ngx2uaNiXmDRru26GOn3GbUHum8lwNyqOqZTFB 4v+5Ak4p5oBUmB3hwNmWDVrbvK+tVfXMguN2GmuVVMF73fHQpKPsPQHZosehSRjGdZI P+0f8qb3cL78MaKEgnqlARSEgrnRVfR3HcHqvI22geganpnDYQ/ufcPHzoncd26QIT7 S2tA+qvdDl/NTGLLbDmjqtPB755mo8W3IB1Eq0mGqc4S4FcZKR0nJ+mE5mwrF3Efcrt xx7HcPRZRQ== Date: Fri, 26 Feb 2021 06:28:26 +0100 (CET) From: Lynne To: Ffmpeg Devel Message-ID: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] aeaenc: add an aea muxer 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" This allows to (re)mux Sony ATRAC1 files. Patch attached Subject: [PATCH 2/2] aeaenc: add an aea muxer This allows to (re)mux Sony ATRAC1 files. --- libavformat/Makefile | 1 + libavformat/aeaenc.c | 107 +++++++++++++++++++++++++++++++++++++++ libavformat/allformats.c | 1 + 3 files changed, 109 insertions(+) create mode 100644 libavformat/aeaenc.c diff --git a/libavformat/Makefile b/libavformat/Makefile index 10fee749c8..aced6edba5 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -80,6 +80,7 @@ OBJS-$(CONFIG_ADTS_MUXER) += adtsenc.o apetag.o img2.o \ OBJS-$(CONFIG_ADX_DEMUXER) += adxdec.o OBJS-$(CONFIG_ADX_MUXER) += rawenc.o OBJS-$(CONFIG_AEA_DEMUXER) += aea.o pcm.o +OBJS-$(CONFIG_AEA_MUXER) += aeaenc.o rawenc.o OBJS-$(CONFIG_AFC_DEMUXER) += afc.o OBJS-$(CONFIG_AIFF_DEMUXER) += aiffdec.o pcm.o isom.o \ mov_chan.o replaygain.o diff --git a/libavformat/aeaenc.c b/libavformat/aeaenc.c new file mode 100644 index 0000000000..c25adc15d4 --- /dev/null +++ b/libavformat/aeaenc.c @@ -0,0 +1,107 @@ +/* + * 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 + */ + +#include "libavutil/intreadwrite.h" +#include "avformat.h" +#include "rawenc.h" + +#define AT1_SU_SIZE 212 + +typedef struct AEAEncContext { + uint32_t frame_cnt; +} AEAEncContext; + +static int aea_write_header(AVFormatContext *s) +{ + AVDictionaryEntry *dtmp; + AVCodecParameters *par = s->streams[0]->codecpar; + AVIOContext *pb = s->pb; + + if (par->channels != 1 && par->channels != 2) { + av_log(s, AV_LOG_ERROR, "Unsupported number of channels %i!\n", par->channels); + return AVERROR(EINVAL); + } + + if (par->sample_rate != 44100) { + av_log(s, AV_LOG_ERROR, "Unsupported samplerate %i!\n", par->sample_rate); + return AVERROR(EINVAL); + } + + if (par->block_align != (AT1_SU_SIZE * par->channels)) { + av_log(s, AV_LOG_ERROR, "Unsupported block align %i!\n", par->block_align); + return AVERROR(EINVAL); + } + + avio_wl32(pb, 0x800); // magic + + dtmp = av_dict_get(s->metadata, "title", NULL, 0); + if (dtmp) { + int len = FFMIN(strlen(dtmp->value), 15); + avio_write(pb, dtmp->value, len); + for (int i = 0; i < (16 - len); i++) + avio_w8(pb, 0); + } else { + avio_wl64(pb, 0); + avio_wl64(pb, 0); + } + + avio_skip(pb, 240); // not needed + + avio_wl32(pb, 0); // #frames field overwritten when closing + avio_w8 (pb, par->channels); // Number of channels + + /* Skip to 2048 */ + avio_skip(pb, 2048 - avio_tell(pb)); + + return 0; +} + +static int aea_write_packet(AVFormatContext *s, AVPacket *pkt) +{ + AEAEncContext *ctx = s->priv_data; + ctx->frame_cnt++; + return ff_raw_write_packet(s, pkt); +} + +static int aea_write_trailer(AVFormatContext *s) +{ + AVIOContext *pb = s->pb; + AEAEncContext *ctx = s->priv_data; + + if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && ctx->frame_cnt > 1) { + int64_t end = avio_tell(pb); + + avio_seek(pb, 260, SEEK_SET); + avio_wl32(pb, ctx->frame_cnt); + avio_seek(pb, end, SEEK_SET); + } + + return 0; +} + +AVOutputFormat ff_aea_muxer = { + .name = "aea", + .long_name = NULL_IF_CONFIG_SMALL("MD STUDIO audio"), + .priv_data_size = sizeof(AEAEncContext), + .extensions = "aea", + .audio_codec = AV_CODEC_ID_ATRAC1, + .write_header = aea_write_header, + .write_packet = aea_write_packet, + .write_trailer = aea_write_trailer, + .flags = AVFMT_NOTIMESTAMPS, +}; diff --git a/libavformat/allformats.c b/libavformat/allformats.c index f837ddabc8..6501a5d2f5 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -44,6 +44,7 @@ extern AVOutputFormat ff_adts_muxer; extern AVInputFormat ff_adx_demuxer; extern AVOutputFormat ff_adx_muxer; extern AVInputFormat ff_aea_demuxer; +extern AVOutputFormat ff_aea_muxer; extern AVInputFormat ff_afc_demuxer; extern AVInputFormat ff_aiff_demuxer; extern AVOutputFormat ff_aiff_muxer;