FFmpeg
fastaudio.c
Go to the documentation of this file.
1 /*
2  * MOFLEX Fast Audio decoder
3  * Copyright (c) 2015-2016 Florian Nouwt
4  * Copyright (c) 2017 Adib Surani
5  * Copyright (c) 2020 Paul B Mahol
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "codec_internal.h"
27 #include "decode.h"
28 
29 typedef struct ChannelItems {
30  float f[8];
31  float last;
32 } ChannelItems;
33 
34 typedef struct FastAudioContext {
35  float table[8][64];
36 
39 
41 {
42  FastAudioContext *s = avctx->priv_data;
43 
45 
46  for (int i = 0; i < 8; i++)
47  s->table[0][i] = (i - 159.5f) / 160.f;
48  for (int i = 0; i < 11; i++)
49  s->table[0][i + 8] = (i - 37.5f) / 40.f;
50  for (int i = 0; i < 27; i++)
51  s->table[0][i + 8 + 11] = (i - 13.f) / 20.f;
52  for (int i = 0; i < 11; i++)
53  s->table[0][i + 8 + 11 + 27] = (i + 27.5f) / 40.f;
54  for (int i = 0; i < 7; i++)
55  s->table[0][i + 8 + 11 + 27 + 11] = (i + 152.5f) / 160.f;
56 
57  memcpy(s->table[1], s->table[0], sizeof(s->table[0]));
58 
59  for (int i = 0; i < 7; i++)
60  s->table[2][i] = (i - 33.5f) / 40.f;
61  for (int i = 0; i < 25; i++)
62  s->table[2][i + 7] = (i - 13.f) / 20.f;
63 
64  for (int i = 0; i < 32; i++)
65  s->table[3][i] = -s->table[2][31 - i];
66 
67  for (int i = 0; i < 16; i++)
68  s->table[4][i] = i * 0.22f / 3.f - 0.6f;
69 
70  for (int i = 0; i < 16; i++)
71  s->table[5][i] = i * 0.20f / 3.f - 0.3f;
72 
73  for (int i = 0; i < 8; i++)
74  s->table[6][i] = i * 0.36f / 3.f - 0.4f;
75 
76  for (int i = 0; i < 8; i++)
77  s->table[7][i] = i * 0.34f / 3.f - 0.2f;
78 
79  s->ch = av_calloc(avctx->ch_layout.nb_channels, sizeof(*s->ch));
80  if (!s->ch)
81  return AVERROR(ENOMEM);
82 
83  return 0;
84 }
85 
86 static int read_bits(int bits, int *ppos, unsigned *src)
87 {
88  int r, pos;
89 
90  pos = *ppos;
91  pos += bits;
92  r = src[(pos - 1) / 32] >> ((-pos) & 31);
93  *ppos = pos;
94 
95  return r & ((1 << bits) - 1);
96 }
97 
98 static const uint8_t bits[8] = { 6, 6, 5, 5, 4, 0, 3, 3, };
99 
100 static void set_sample(int i, int j, int v, float *result, int *pads, float value)
101 {
102  result[i * 64 + pads[i] + j * 3] = value * (2 * v - 7);
103 }
104 
106  int *got_frame, AVPacket *pkt)
107 {
108  FastAudioContext *s = avctx->priv_data;
109  GetByteContext gb;
110  int subframes;
111  int ret;
112 
113  subframes = pkt->size / (40 * avctx->ch_layout.nb_channels);
114  frame->nb_samples = subframes * 256;
115  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
116  return ret;
117 
118  bytestream2_init(&gb, pkt->data, pkt->size);
119 
120  for (int subframe = 0; subframe < subframes; subframe++) {
121  for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
122  ChannelItems *ch = &s->ch[channel];
123  float result[256] = { 0 };
124  unsigned src[10];
125  int inds[4], pads[4];
126  float m[8];
127  int pos = 0;
128 
129  for (int i = 0; i < 10; i++)
130  src[i] = bytestream2_get_le32(&gb);
131 
132  for (int i = 0; i < 8; i++)
133  m[7 - i] = s->table[i][read_bits(bits[i], &pos, src)];
134 
135  for (int i = 0; i < 4; i++)
136  inds[3 - i] = read_bits(6, &pos, src);
137 
138  for (int i = 0; i < 4; i++)
139  pads[3 - i] = read_bits(2, &pos, src);
140 
141  for (int i = 0, index5 = 0; i < 4; i++) {
142  float value = av_int2float((inds[i] + 1) << 20) * powf(2.f, 116.f);
143 
144  for (int j = 0, tmp = 0; j < 21; j++) {
145  set_sample(i, j, j == 20 ? tmp / 2 : read_bits(3, &pos, src), result, pads, value);
146  if (j % 10 == 9)
147  tmp = 4 * tmp + read_bits(2, &pos, src);
148  if (j == 20)
149  index5 = FFMIN(2 * index5 + tmp % 2, 63);
150  }
151 
152  m[2] = s->table[5][index5];
153  }
154 
155  for (int i = 0; i < 256; i++) {
156  float x = result[i];
157 
158  for (int j = 0; j < 8; j++) {
159  x -= m[j] * ch->f[j];
160  ch->f[j] += m[j] * x;
161  }
162 
163  memmove(&ch->f[0], &ch->f[1], sizeof(float) * 7);
164  ch->f[7] = x;
165  ch->last = x + ch->last * 0.86f;
166  result[i] = ch->last * 2.f;
167  }
168 
169  memcpy(frame->extended_data[channel] + 1024 * subframe, result, 256 * sizeof(float));
170  }
171  }
172 
173  *got_frame = 1;
174 
175  return pkt->size;
176 }
177 
179 {
180  FastAudioContext *s = avctx->priv_data;
181 
182  av_freep(&s->ch);
183 
184  return 0;
185 }
186 
188  .p.name = "fastaudio",
189  CODEC_LONG_NAME("MobiClip FastAudio"),
190  .p.type = AVMEDIA_TYPE_AUDIO,
191  .p.id = AV_CODEC_ID_FASTAUDIO,
192  .priv_data_size = sizeof(FastAudioContext),
193  .init = fastaudio_init,
195  .close = fastaudio_close,
196  .p.capabilities = AV_CODEC_CAP_DR1,
197  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
199 };
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
r
const char * r
Definition: vf_curves.c:116
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
GetByteContext
Definition: bytestream.h:33
fastaudio_close
static av_cold int fastaudio_close(AVCodecContext *avctx)
Definition: fastaudio.c:178
AV_CODEC_ID_FASTAUDIO
@ AV_CODEC_ID_FASTAUDIO
Definition: codec_id.h:527
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVPacket::data
uint8_t * data
Definition: packet.h:374
FastAudioContext::ch
ChannelItems * ch
Definition: fastaudio.c:37
FFCodec
Definition: codec_internal.h:119
FastAudioContext
Definition: fastaudio.c:34
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:303
init
static int init
Definition: av_tx.c:47
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:123
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2059
av_int2float
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
fastaudio_init
static av_cold int fastaudio_init(AVCodecContext *avctx)
Definition: fastaudio.c:40
pkt
AVPacket * pkt
Definition: movenc.c:59
av_cold
#define av_cold
Definition: attributes.h:90
set_sample
static void set_sample(int i, int j, int v, float *result, int *pads, float value)
Definition: fastaudio.c:100
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:298
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:264
ff_fastaudio_decoder
const FFCodec ff_fastaudio_decoder
Definition: fastaudio.c:187
ChannelItems::last
float last
Definition: fastaudio.c:31
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
FastAudioContext::table
float table[8][64]
Definition: fastaudio.c:35
f
f
Definition: af_crystalizer.c:122
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1450
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
ChannelItems::f
float f[8]
Definition: fastaudio.c:30
powf
#define powf(x, y)
Definition: libm.h:50
codec_internal.h
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1023
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
read_bits
static int read_bits(int bits, int *ppos, unsigned *src)
Definition: fastaudio.c:86
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:211
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:272
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
pos
unsigned int pos
Definition: spdifenc.c:412
ChannelItems
Definition: fastaudio.c:29
AVCodecContext
main external API structure.
Definition: avcodec.h:398
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:425
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
fastaudio_decode
static int fastaudio_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: fastaudio.c:105
bits
static const uint8_t bits[8]
Definition: fastaudio.c:98
channel
channel
Definition: ebur128.h:39