From patchwork Sat Aug 29 11:36:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 21981 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 C3D1344992D for ; Sat, 29 Aug 2020 14:36:57 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AEE076899A8; Sat, 29 Aug 2020 14:36:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f67.google.com (mail-ej1-f67.google.com [209.85.218.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 329DE681866 for ; Sat, 29 Aug 2020 14:36:50 +0300 (EEST) Received: by mail-ej1-f67.google.com with SMTP id i26so2154752ejb.12 for ; Sat, 29 Aug 2020 04:36:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=RelyiMB74dwyXfizeS93ZGDmoUwd6KM/Dp329uISIHs=; b=Z/Y4jrIagdopcM+Tj+/k39IjWyfhCLhhi6KwxjHLqOTDAcRnpndfpKP0V/LbnJFhCw EIIsUi2I7f6ThSww76/TQ/hU9JdU2pS7xgnfDGfVN+gCoS3dPYiFoU68ygmDPoh4EfnJ JCDVaRCeANio0jm8dms59yg7tt1dPk54l3ox+Ow/qtmFs//WOSljEass7tbdJ7cEvbmC i0ZDhLiGuKxt+q8bt4LnGphUtXXZXopS/qU3ePWdRxdm6KmNEuY3lOG/ZUrzQK9gWnZi haGYcocMgjBNZIeLJwPgY9R5rMY3/5Pyd2qa/I7GkXKSC+N56m0MsCukMs7z4u6zo4Um 1DWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=RelyiMB74dwyXfizeS93ZGDmoUwd6KM/Dp329uISIHs=; b=MQB8XHFKoHGZ50zRjH72n4yAAwNKTsIzj3T3/kYdLH0kscSn6NxuYs6uYuyRt4mRaF ELvmVM31wvlkxlRI4u2HOwcGtue6UjKlg6ZaZK4seJ9m702vSB5AUpVg9BcvJqRlHFia k+9scTlZhsoWdaZENkRhowiSevuYkUW/4XaW0Xmpjjj+U5ejsTusMvRBWFrum4jgqfuV TItncSW0AOStmnpuhOj7F1dI67NjHa/4IbS1UR1BJSsUR7MPq44q28Ka/Wen5S8f5HCw lcWJ8rnyLS+O1wxF28QKyBTLOlEBkere4FjUgt54iLNBpum6r0FZDctQs8o55uzJNlVE rZ8Q== X-Gm-Message-State: AOAM530rQ/520NUivOl8t2BIxoqeqC6n2zhYfiPsPDk7Ew+az1m7HceD i/cJaIbUVhqS7aaYaD3oM2Dc4mboDbaFdA== X-Google-Smtp-Source: ABdhPJyHwoBQrlbCC2yEBv9EFmW2JTaA8atJ6Z/ri3bNOhh3fG/0I5eH0DnNtP8DUQHBrXilkerlwA== X-Received: by 2002:a17:906:644e:: with SMTP id l14mr3109939ejn.60.1598701009180; Sat, 29 Aug 2020 04:36:49 -0700 (PDT) Received: from localhost.localdomain ([94.250.163.64]) by smtp.gmail.com with ESMTPSA id a8sm1984613ejx.73.2020.08.29.04.36.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Aug 2020 04:36:48 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 29 Aug 2020 13:36:19 +0200 Message-Id: <20200829113621.9797-2-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200829113621.9797-1-onemda@gmail.com> References: <20200829113621.9797-1-onemda@gmail.com> Subject: [FFmpeg-devel] [PATCH 2/4][VERSION 2] avcodec: add FastAudio decoder 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- libavcodec/Makefile | 1 + libavcodec/allcodecs.c | 1 + libavcodec/codec_desc.c | 7 ++ libavcodec/codec_id.h | 1 + libavcodec/fastaudio.c | 200 ++++++++++++++++++++++++++++++++++++++++ libavcodec/utils.c | 2 + 6 files changed, 212 insertions(+) create mode 100644 libavcodec/fastaudio.c diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 191c4e0a7c..6f75f26c84 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -317,6 +317,7 @@ OBJS-$(CONFIG_ESCAPE124_DECODER) += escape124.o OBJS-$(CONFIG_ESCAPE130_DECODER) += escape130.o OBJS-$(CONFIG_EVRC_DECODER) += evrcdec.o acelp_vectors.o lsp.o OBJS-$(CONFIG_EXR_DECODER) += exr.o exrdsp.o +OBJS-$(CONFIG_FASTAUDIO_DECODER) += fastaudio.o OBJS-$(CONFIG_FFV1_DECODER) += ffv1dec.o ffv1.o OBJS-$(CONFIG_FFV1_ENCODER) += ffv1enc.o ffv1.o OBJS-$(CONFIG_FFWAVESYNTH_DECODER) += ffwavesynth.o diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c index 084a289c9e..3920eb37ce 100644 --- a/libavcodec/allcodecs.c +++ b/libavcodec/allcodecs.c @@ -430,6 +430,7 @@ extern AVCodec ff_dst_decoder; extern AVCodec ff_eac3_encoder; extern AVCodec ff_eac3_decoder; extern AVCodec ff_evrc_decoder; +extern AVCodec ff_fastaudio_decoder; extern AVCodec ff_ffwavesynth_decoder; extern AVCodec ff_flac_encoder; extern AVCodec ff_flac_decoder; diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c index b5bc5c3c71..9a3eaf7d98 100644 --- a/libavcodec/codec_desc.c +++ b/libavcodec/codec_desc.c @@ -3130,6 +3130,13 @@ static const AVCodecDescriptor codec_descriptors[] = { .long_name = NULL_IF_CONFIG_SMALL("CRI HCA"), .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, }, + { + .id = AV_CODEC_ID_FASTAUDIO, + .type = AVMEDIA_TYPE_AUDIO, + .name = "fastaudio", + .long_name = NULL_IF_CONFIG_SMALL("MobiClip FastAudio"), + .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY, + }, /* subtitle codecs */ { diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h index aac1174f28..aac7f63eb6 100644 --- a/libavcodec/codec_id.h +++ b/libavcodec/codec_id.h @@ -504,6 +504,7 @@ enum AVCodecID { AV_CODEC_ID_MPEGH_3D_AUDIO, AV_CODEC_ID_SIREN, AV_CODEC_ID_HCA, + AV_CODEC_ID_FASTAUDIO, /* subtitle codecs */ AV_CODEC_ID_FIRST_SUBTITLE = 0x17000, ///< A dummy ID pointing at the start of subtitle codecs. diff --git a/libavcodec/fastaudio.c b/libavcodec/fastaudio.c new file mode 100644 index 0000000000..bd9e28ec0f --- /dev/null +++ b/libavcodec/fastaudio.c @@ -0,0 +1,200 @@ +/* + * MOFLEX Fast Audio decoder + * Copyright (c) 2020 Paul B Mahol + * + * 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 "avcodec.h" +#include "bytestream.h" +#include "internal.h" +#include "mathops.h" + +typedef struct ChannelItems { + float f[8]; + float last; +} ChannelItems; + +typedef struct FastAudioContext { + float table[8][64]; + + ChannelItems *ch; +} FastAudioContext; + +static av_cold int fastaudio_init(AVCodecContext *avctx) +{ + FastAudioContext *s = avctx->priv_data; + + avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; + + for (int i = 0; i < 8; i++) + s->table[0][i] = (i - 159.5f) / 160.f; + for (int i = 0; i < 11; i++) + s->table[0][i + 8] = (i - 37.5f) / 40.f; + for (int i = 0; i < 27; i++) + s->table[0][i + 8 + 11] = (i - 13.f) / 20.f; + for (int i = 0; i < 11; i++) + s->table[0][i + 8 + 11 + 27] = (i + 27.5f) / 40.f; + for (int i = 0; i < 7; i++) + s->table[0][i + 8 + 11 + 27 + 11] = (i + 152.5f) / 160.f; + + memcpy(s->table[1], s->table[0], sizeof(s->table[0])); + + for (int i = 0; i < 7; i++) + s->table[2][i] = (i - 33.5f) / 40.f; + for (int i = 0; i < 25; i++) + s->table[2][i + 7] = (i - 13.f) / 20.f; + + for (int i = 0; i < 32; i++) + s->table[3][i] = -s->table[2][31 - i]; + + for (int i = 0; i < 16; i++) + s->table[4][i] = i * 0.22f / 3.f - 0.6f; + + for (int i = 0; i < 16; i++) + s->table[5][i] = i * 0.20f / 3.f - 0.3f; + + for (int i = 0; i < 8; i++) + s->table[6][i] = i * 0.36f / 3.f - 0.4f; + + for (int i = 0; i < 8; i++) + s->table[7][i] = i * 0.34f / 3.f - 0.2f; + + s->ch = av_calloc(avctx->channels, sizeof(*s->ch)); + if (!s->ch) + return AVERROR(ENOMEM); + + return 0; +} + +static int read_bits(int bits, int *ppos, unsigned *src) +{ + int r, pos; + + pos = *ppos; + pos += bits; + r = src[(pos - 1) / 32] >> (32 - pos % 32); + *ppos = pos; + + return r & ((1 << (bits % 32)) - 1); +} + +static const uint8_t bits[8] = { 6, 6, 5, 5, 4, 0, 3, 3, }; + +static void set_sample(int i, int j, int v, float *result, int *pads, float value) +{ + result[i * 64 + pads[i] + j * 3] = value * (2 * v - 7); +} + +static int fastaudio_decode(AVCodecContext *avctx, void *data, + int *got_frame, AVPacket *pkt) +{ + FastAudioContext *s = avctx->priv_data; + GetByteContext gb; + AVFrame *frame = data; + int subframes; + int ret; + + subframes = pkt->size / (40 * avctx->channels); + frame->nb_samples = subframes * 256; + if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) + return ret; + + bytestream2_init(&gb, pkt->data, pkt->size); + + for (int subframe = 0; subframe < subframes; subframe++) { + for (int channel = 0; channel < avctx->channels; channel++) { + ChannelItems *ch = &s->ch[channel]; + float result[256] = { 0 }; + unsigned src[10]; + int inds[4], pads[4]; + float m[8]; + int pos = 0; + + for (int i = 0; i < 10; i++) + src[i] = bytestream2_get_le32(&gb); + + for (int i = 0; i < 8; i++) + m[7 - i] = s->table[i][read_bits(bits[i], &pos, src)]; + + for (int i = 0; i < 4; i++) + inds[3 - i] = read_bits(6, &pos, src); + + for (int i = 0; i < 4; i++) + pads[3 - i] = read_bits(2, &pos, src); + + for (int i = 0, index5 = 0; i < 4; i++) { + float value = av_int2float((inds[i] + 1) << 20) * powf(2.f, 116.f); + + for (int j = 0, tmp = 0; j < 21; j++) { + set_sample(i, j, j == 20 ? tmp / 2 : read_bits(3, &pos, src), result, pads, value); + if (j % 10 == 9) + tmp = 4 * tmp + read_bits(2, &pos, src); + if (j == 20) + index5 = 2 * index5 + tmp % 2; + } + + m[2] = s->table[5][index5]; + } + + for (int i = 0; i < 256; i++) { + float x = result[i]; + + for (int j = 0; j < 8; j++) { + x -= m[j] * ch->f[j]; + ch->f[j] += m[j] * x; + } + + memmove(&ch->f[0], &ch->f[1], sizeof(float) * 7); + ch->f[7] = x; + ch->last = x + ch->last * 0.86f; + result[i] = ch->last * 2.f; + } + + memcpy(frame->extended_data[channel] + 1024 * subframe, result, 256 * sizeof(float)); + } + } + + *got_frame = 1; + + return pkt->size; +} + +static av_cold int fastaudio_close(AVCodecContext *avctx) +{ + FastAudioContext *s = avctx->priv_data; + + av_freep(&s->ch); + + return 0; +} + +AVCodec ff_fastaudio_decoder = { + .name = "fastaudio", + .long_name = NULL_IF_CONFIG_SMALL("MobiClip FastAudio"), + .type = AVMEDIA_TYPE_AUDIO, + .id = AV_CODEC_ID_FASTAUDIO, + .priv_data_size = sizeof(FastAudioContext), + .init = fastaudio_init, + .decode = fastaudio_decode, + .close = fastaudio_close, + .capabilities = AV_CODEC_CAP_DR1, + .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, + AV_SAMPLE_FMT_NONE }, +}; diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 187ce230b8..2e5185f364 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -1664,6 +1664,8 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba, if (ch > 0 && ch < INT_MAX/16) { /* calc from frame_bytes and channels */ switch (id) { + case AV_CODEC_ID_FASTAUDIO: + return frame_bytes / (40 * ch) * 256; case AV_CODEC_ID_ADPCM_IMA_MOFLEX: return (frame_bytes - 4 * ch) / (128 * ch) * 256; case AV_CODEC_ID_ADPCM_AFC: