FFmpeg
apac.c
Go to the documentation of this file.
1 /*
2  * APAC audio decoder
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/audio_fifo.h"
22 #include "libavutil/mem.h"
23 #include "avcodec.h"
24 #include "codec_internal.h"
25 #include "decode.h"
26 #include "get_bits.h"
27 
28 typedef struct ChContext {
29  int have_code;
34  uint8_t block[32 * 2];
36 } ChContext;
37 
38 typedef struct APACContext {
40  int skip;
41 
42  int cur_ch;
44 
45  uint8_t *bitstream;
46  int64_t max_framesize;
49 } APACContext;
50 
51 static av_cold int apac_close(AVCodecContext *avctx)
52 {
53  APACContext *s = avctx->priv_data;
54 
55  av_freep(&s->bitstream);
56  s->bitstream_size = 0;
57 
58  for (int ch = 0; ch < 2; ch++) {
59  ChContext *c = &s->ch[ch];
60 
61  av_audio_fifo_free(c->samples);
62  }
63 
64  return 0;
65 }
66 
67 static av_cold int apac_init(AVCodecContext *avctx)
68 {
69  APACContext *s = avctx->priv_data;
70 
71  if (avctx->bits_per_coded_sample > 8)
73  else
75 
76  if (avctx->ch_layout.nb_channels < 1 ||
77  avctx->ch_layout.nb_channels > 2 ||
78  avctx->bits_per_coded_sample < 8 ||
79  avctx->bits_per_coded_sample > 16
80  )
81  return AVERROR_INVALIDDATA;
82 
83  for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
84  ChContext *c = &s->ch[ch];
85 
86  c->bit_length = avctx->bits_per_coded_sample;
87  c->block_length = 8;
88  c->have_code = 0;
89  c->samples = av_audio_fifo_alloc(avctx->sample_fmt, 1, 1024);
90  if (!c->samples)
91  return AVERROR(ENOMEM);
92  }
93 
94  s->max_framesize = 1024;
95  s->bitstream = av_realloc_f(s->bitstream, s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream));
96  if (!s->bitstream)
97  return AVERROR(ENOMEM);
98 
99  return 0;
100 }
101 
103 {
104  if (get_bits1(gb)) {
105  int code = get_bits(gb, 2);
106 
107  switch (code) {
108  case 0:
109  c->bit_length--;
110  break;
111  case 1:
112  c->bit_length++;
113  break;
114  case 2:
115  c->bit_length = get_bits(gb, 5);
116  break;
117  case 3:
118  c->block_length = get_bits(gb, 4);
119  return 1;
120  }
121  }
122 
123  return 0;
124 }
125 
127  int *got_frame_ptr, AVPacket *pkt)
128 {
129  APACContext *s = avctx->priv_data;
130  GetBitContext *gb = &s->gb;
131  int ret, n, buf_size, input_buf_size;
132  const uint8_t *buf;
133  int nb_samples;
134 
135  if (!pkt->size && s->bitstream_size <= 0) {
136  *got_frame_ptr = 0;
137  return 0;
138  }
139 
140  buf_size = pkt->size;
141  input_buf_size = buf_size;
142 
143  if (s->bitstream_index > 0 && s->bitstream_size > 0) {
144  memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
145  s->bitstream_index = 0;
146  }
147 
148  if (s->bitstream_index + s->bitstream_size + buf_size > s->max_framesize) {
149  s->bitstream = av_realloc_f(s->bitstream, s->bitstream_index +
150  s->bitstream_size +
151  buf_size + AV_INPUT_BUFFER_PADDING_SIZE,
152  sizeof(*s->bitstream));
153  if (!s->bitstream)
154  return AVERROR(ENOMEM);
155  s->max_framesize = s->bitstream_index + s->bitstream_size + buf_size;
156  }
157  if (pkt->data)
158  memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], pkt->data, buf_size);
159  buf = &s->bitstream[s->bitstream_index];
160  buf_size += s->bitstream_size;
161  s->bitstream_size = buf_size;
162 
163  frame->nb_samples = s->bitstream_size * 16 * 8;
164  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
165  return ret;
166 
167  if ((ret = init_get_bits8(gb, buf, buf_size)) < 0)
168  return ret;
169 
170  skip_bits(gb, s->skip);
171  s->skip = 0;
172 
173  while (get_bits_left(gb) > 0) {
174  for (int ch = s->cur_ch; ch < avctx->ch_layout.nb_channels; ch++) {
175  ChContext *c = &s->ch[ch];
176  int16_t *dst16 = (int16_t *)c->block;
177  uint8_t *dst8 = (uint8_t *)c->block;
178  void *samples[4];
179 
180  samples[0] = &c->block[0];
181  if (get_bits_left(gb) < 16 && pkt->size) {
182  s->cur_ch = ch;
183  goto end;
184  }
185 
186  if (!c->have_code && get_code(c, gb))
187  get_code(c, gb);
188  c->have_code = 0;
189 
190  if (c->block_length <= 0)
191  continue;
192 
193  if (c->bit_length < 0 ||
194  c->bit_length > 17) {
195  c->bit_length = avctx->bits_per_coded_sample;
196  s->bitstream_index = 0;
197  s->bitstream_size = 0;
198  return AVERROR_INVALIDDATA;
199  }
200 
201  if (get_bits_left(gb) < c->block_length * c->bit_length) {
202  if (pkt->size) {
203  c->have_code = 1;
204  s->cur_ch = ch;
205  goto end;
206  } else {
207  break;
208  }
209  }
210 
211  for (int i = 0; i < c->block_length; i++) {
212  int val = get_bits_long(gb, c->bit_length);
213  unsigned delta = (val & 1) ? ~(val >> 1) : (val >> 1);
214  int sample;
215 
216  delta += c->last_delta;
217  sample = c->last_sample + delta;
218  c->last_delta = delta;
219  c->last_sample = sample;
220 
221  switch (avctx->sample_fmt) {
222  case AV_SAMPLE_FMT_S16P:
223  dst16[i] = sample;
224  break;
225  case AV_SAMPLE_FMT_U8P:
226  dst8[i] = sample;
227  break;
228  }
229  }
230 
231  av_audio_fifo_write(c->samples, samples, c->block_length);
232  }
233 
234  s->cur_ch = 0;
235  }
236 end:
237  nb_samples = frame->nb_samples;
238  for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
239  nb_samples = FFMIN(av_audio_fifo_size(s->ch[ch].samples), nb_samples);
240 
241  frame->nb_samples = nb_samples;
242  for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
243  void *samples[1] = { frame->extended_data[ch] };
244  av_audio_fifo_read(s->ch[ch].samples, samples, nb_samples);
245  }
246 
247  s->skip = get_bits_count(gb) - 8 * (get_bits_count(gb) / 8);
248  n = get_bits_count(gb) / 8;
249 
250  if (nb_samples > 0 || pkt->size)
251  *got_frame_ptr = 1;
252 
253  if (s->bitstream_size > 0) {
254  s->bitstream_index += n;
255  s->bitstream_size -= n;
256  return input_buf_size;
257  }
258  return n;
259 }
260 
262  .p.name = "apac",
263  CODEC_LONG_NAME("Marian's A-pac audio"),
264  .p.type = AVMEDIA_TYPE_AUDIO,
265  .p.id = AV_CODEC_ID_APAC,
266  .priv_data_size = sizeof(APACContext),
267  .init = apac_init,
269  .close = apac_close,
270  .p.capabilities = AV_CODEC_CAP_DELAY |
271 #if FF_API_SUBFRAMES
272  AV_CODEC_CAP_SUBFRAMES |
273 #endif
275  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
276  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
279 };
AV_CODEC_ID_APAC
@ AV_CODEC_ID_APAC
Definition: codec_id.h:539
av_audio_fifo_free
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:48
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
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
av_audio_fifo_write
int av_audio_fifo_write(AVAudioFifo *af, void *const *data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:119
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVPacket::data
uint8_t * data
Definition: packet.h:520
FFCodec
Definition: codec_internal.h:126
APACContext::bitstream_size
int bitstream_size
Definition: apac.c:47
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
AVAudioFifo
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:37
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
GetBitContext
Definition: get_bits.h:108
APACContext::ch
ChContext ch[2]
Definition: apac.c:43
val
static double val(void *priv, double ch)
Definition: aeval.c:78
get_code
static int get_code(ChContext *c, GetBitContext *gb)
Definition: apac.c:102
pkt
AVPacket * pkt
Definition: movenc.c:60
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
APACContext::gb
GetBitContext gb
Definition: apac.c:39
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:286
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
decode.h
get_bits.h
APACContext::cur_ch
int cur_ch
Definition: apac.c:42
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:271
ChContext::block
uint8_t block[32 *2]
Definition: apac.c:34
av_realloc_f
#define av_realloc_f(p, o, n)
Definition: tableprint_vlc.h:32
ChContext
Definition: apac.c:28
av_audio_fifo_alloc
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:62
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
apac_init
static av_cold int apac_init(AVCodecContext *avctx)
Definition: apac.c:67
ChContext::last_sample
int last_sample
Definition: apac.c:30
ChContext::have_code
int have_code
Definition: apac.c:29
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
APACContext::skip
int skip
Definition: apac.c:40
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1575
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
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:521
AV_SAMPLE_FMT_U8P
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition: samplefmt.h:63
codec_internal.h
APACContext::bitstream_index
int bitstream_index
Definition: apac.c:48
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1057
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
sample
#define sample
Definition: flacdsp_template.c:44
av_audio_fifo_read
int av_audio_fifo_read(AVAudioFifo *af, void *const *data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:175
ff_apac_decoder
const FFCodec ff_apac_decoder
Definition: apac.c:261
av_audio_fifo_size
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:222
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1568
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
APACContext::max_framesize
int64_t max_framesize
Definition: apac.c:46
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
APACContext
Definition: apac.c:38
delta
float delta
Definition: vorbis_enc_data.h:430
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
audio_fifo.h
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
ChContext::block_length
int block_length
Definition: apac.c:33
avcodec.h
ChContext::samples
AVAudioFifo * samples
Definition: apac.c:35
ChContext::bit_length
int bit_length
Definition: apac.c:32
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
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
AVCodecContext
main external API structure.
Definition: avcodec.h:445
apac_close
static av_cold int apac_close(AVCodecContext *avctx)
Definition: apac.c:51
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
mem.h
APACContext::bitstream
uint8_t * bitstream
Definition: apac.c:45
AVPacket
This structure stores compressed data.
Definition: packet.h:497
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ChContext::last_delta
int last_delta
Definition: apac.c:31
apac_decode
static int apac_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition: apac.c:126