FFmpeg
aptxdec.c
Go to the documentation of this file.
1 /*
2  * Audio Processing Technology codec for Bluetooth (aptX)
3  *
4  * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "aptx.h"
24 
25 /*
26  * Half-band QMF synthesis filter realized with a polyphase FIR filter.
27  * Join 2 subbands and upsample by 2.
28  * So for each 2 subbands sample that goes in, a pair of samples goes out.
29  */
32  const int32_t coeffs[NB_FILTERS][FILTER_TAPS],
33  int shift,
34  int32_t low_subband_input,
35  int32_t high_subband_input,
36  int32_t samples[NB_FILTERS])
37 {
39  int i;
40 
41  subbands[0] = low_subband_input + high_subband_input;
42  subbands[1] = low_subband_input - high_subband_input;
43 
44  for (i = 0; i < NB_FILTERS; i++) {
45  aptx_qmf_filter_signal_push(&signal[i], subbands[1-i]);
46  samples[i] = aptx_qmf_convolution(&signal[i], coeffs[i], shift);
47  }
48 }
49 
50 /*
51  * Two stage QMF synthesis tree.
52  * Join 4 subbands and upsample by 4.
53  * So for each 4 subbands sample that goes in, a group of 4 samples goes out.
54  */
56  int32_t subband_samples[4],
57  int32_t samples[4])
58 {
59  int32_t intermediate_samples[4];
60  int i;
61 
62  /* Join 4 subbands into 2 intermediate subbands upsampled to 2 samples. */
63  for (i = 0; i < 2; i++)
66  subband_samples[2*i+0],
67  subband_samples[2*i+1],
68  &intermediate_samples[2*i]);
69 
70  /* Join 2 samples from intermediate subbands upsampled to 4 samples. */
71  for (i = 0; i < 2; i++)
74  intermediate_samples[0+i],
75  intermediate_samples[2+i],
76  &samples[2*i]);
77 }
78 
79 
81 {
82  int32_t subband_samples[4];
83  int subband;
84  for (subband = 0; subband < NB_SUBBANDS; subband++)
85  subband_samples[subband] = channel->prediction[subband].previous_reconstructed_sample;
86  aptx_qmf_tree_synthesis(&channel->qmf, subband_samples, samples);
87 }
88 
89 static void aptx_unpack_codeword(Channel *channel, uint16_t codeword)
90 {
91  channel->quantize[0].quantized_sample = sign_extend(codeword >> 0, 7);
92  channel->quantize[1].quantized_sample = sign_extend(codeword >> 7, 4);
93  channel->quantize[2].quantized_sample = sign_extend(codeword >> 11, 2);
94  channel->quantize[3].quantized_sample = sign_extend(codeword >> 13, 3);
95  channel->quantize[3].quantized_sample = (channel->quantize[3].quantized_sample & ~1)
96  | aptx_quantized_parity(channel);
97 }
98 
99 static void aptxhd_unpack_codeword(Channel *channel, uint32_t codeword)
100 {
101  channel->quantize[0].quantized_sample = sign_extend(codeword >> 0, 9);
102  channel->quantize[1].quantized_sample = sign_extend(codeword >> 9, 6);
103  channel->quantize[2].quantized_sample = sign_extend(codeword >> 15, 4);
104  channel->quantize[3].quantized_sample = sign_extend(codeword >> 19, 5);
105  channel->quantize[3].quantized_sample = (channel->quantize[3].quantized_sample & ~1)
106  | aptx_quantized_parity(channel);
107 }
108 
110  const uint8_t *input,
112 {
113  int channel, ret;
114 
115  for (channel = 0; channel < NB_CHANNELS; channel++) {
116  ff_aptx_generate_dither(&ctx->channels[channel]);
117 
118  if (ctx->hd)
119  aptxhd_unpack_codeword(&ctx->channels[channel],
120  AV_RB24(input + 3*channel));
121  else
122  aptx_unpack_codeword(&ctx->channels[channel],
123  AV_RB16(input + 2*channel));
124  ff_aptx_invert_quantize_and_prediction(&ctx->channels[channel], ctx->hd);
125  }
126 
127  ret = aptx_check_parity(ctx->channels, &ctx->sync_idx);
128 
129  for (channel = 0; channel < NB_CHANNELS; channel++)
130  aptx_decode_channel(&ctx->channels[channel], samples[channel]);
131 
132  return ret;
133 }
134 
135 static int aptx_decode_frame(AVCodecContext *avctx, void *data,
136  int *got_frame_ptr, AVPacket *avpkt)
137 {
138  AptXContext *s = avctx->priv_data;
139  AVFrame *frame = data;
140  int pos, opos, channel, sample, ret;
141 
142  if (avpkt->size < s->block_size) {
143  av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
144  return AVERROR_INVALIDDATA;
145  }
146 
147  /* get output buffer */
148  frame->channels = NB_CHANNELS;
149  frame->format = AV_SAMPLE_FMT_S32P;
150  frame->nb_samples = 4 * avpkt->size / s->block_size;
151  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
152  return ret;
153 
154  for (pos = 0, opos = 0; opos < frame->nb_samples; pos += s->block_size, opos += 4) {
156 
157  if (aptx_decode_samples(s, &avpkt->data[pos], samples)) {
158  av_log(avctx, AV_LOG_ERROR, "Synchronization error\n");
159  return AVERROR_INVALIDDATA;
160  }
161 
162  for (channel = 0; channel < NB_CHANNELS; channel++)
163  for (sample = 0; sample < 4; sample++)
164  AV_WN32A(&frame->data[channel][4*(opos+sample)],
165  samples[channel][sample] * 256);
166  }
167 
168  *got_frame_ptr = 1;
169  return s->block_size * frame->nb_samples / 4;
170 }
171 
172 #if CONFIG_APTX_DECODER
174  .name = "aptx",
175  .long_name = NULL_IF_CONFIG_SMALL("aptX (Audio Processing Technology for Bluetooth)"),
176  .type = AVMEDIA_TYPE_AUDIO,
177  .id = AV_CODEC_ID_APTX,
178  .priv_data_size = sizeof(AptXContext),
179  .init = ff_aptx_init,
181  .capabilities = AV_CODEC_CAP_DR1,
182  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
183  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
184  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
186 };
187 #endif
188 
189 #if CONFIG_APTX_HD_DECODER
191  .name = "aptx_hd",
192  .long_name = NULL_IF_CONFIG_SMALL("aptX HD (Audio Processing Technology for Bluetooth)"),
193  .type = AVMEDIA_TYPE_AUDIO,
194  .id = AV_CODEC_ID_APTX_HD,
195  .priv_data_size = sizeof(AptXContext),
196  .init = ff_aptx_init,
198  .capabilities = AV_CODEC_CAP_DR1,
199  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
200  .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
201  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
203 };
204 #endif
Prediction prediction[NB_SUBBANDS]
Definition: aptx.h:91
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int shift(int a, int b)
Definition: sonic.c:82
This structure describes decoded (raw) audio or video data.
Definition: frame.h:308
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: packet.h:364
int block_size
Definition: aptx.h:96
static av_always_inline int32_t aptx_qmf_convolution(FilterSignal *signal, const int32_t coeffs[FILTER_TAPS], int shift)
Definition: aptx.h:177
#define AV_CH_LAYOUT_STEREO
void ff_aptx_invert_quantize_and_prediction(Channel *channel, int hd)
Definition: aptx.c:496
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
#define sample
AVCodec.
Definition: codec.h:190
#define AV_WN32A(p, v)
Definition: intreadwrite.h:538
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
Definition: aptx.h:83
static void aptx_decode_channel(Channel *channel, int32_t samples[4])
Definition: aptxdec.c:80
static av_always_inline void aptx_qmf_filter_signal_push(FilterSignal *signal, int32_t sample)
Definition: aptx.h:165
static int32_t aptx_quantized_parity(Channel *channel)
Definition: aptx.h:191
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
uint8_t
static void aptx_qmf_tree_synthesis(QMFAnalysis *qmf, int32_t subband_samples[4], int32_t samples[4])
Definition: aptxdec.c:55
Quantize quantize[NB_SUBBANDS]
Definition: aptx.h:89
static const int32_t aptx_qmf_inner_coeffs[NB_FILTERS][FILTER_TAPS]
Definition: aptx.h:150
uint8_t * data
Definition: packet.h:363
static const int32_t aptx_qmf_outer_coeffs[NB_FILTERS][FILTER_TAPS]
Definition: aptx.h:135
int32_t sync_idx
Definition: aptx.h:97
int hd
Definition: aptx.h:95
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AVCodec ff_aptx_hd_decoder
void ff_aptx_generate_dither(Channel *channel)
Definition: aptx.c:384
FilterSignal inner_filter_signal[NB_FILTERS][NB_FILTERS]
Definition: aptx.h:57
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
unsigned int pos
Definition: spdifenc.c:410
const char * name
Name of the codec implementation.
Definition: codec.h:197
int channels
number of audio channels, only used for audio.
Definition: frame.h:614
av_cold int ff_aptx_init(AVCodecContext *avctx)
Definition: aptx.c:507
signed 32 bits, planar
Definition: samplefmt.h:68
QMFAnalysis qmf
Definition: aptx.h:88
subbands
Definition: aptx.h:39
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
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
#define s(width, name)
Definition: cbs_vp9.c:257
int32_t previous_reconstructed_sample
Definition: aptx.h:78
int32_t quantized_sample
Definition: aptx.h:61
Channel channels[NB_CHANNELS]
Definition: aptx.h:98
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:381
static void aptx_unpack_codeword(Channel *channel, uint16_t codeword)
Definition: aptxdec.c:89
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:87
main external API structure.
Definition: avcodec.h:526
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1872
static void aptxhd_unpack_codeword(Channel *channel, uint32_t codeword)
Definition: aptxdec.c:99
static const uint16_t channel_layouts[7]
Definition: dca_lbr.c:113
static int aptx_check_parity(Channel channels[NB_CHANNELS], int32_t *idx)
Definition: aptx.h:204
static int aptx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: aptxdec.c:135
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 input
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:130
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:322
FilterSignal outer_filter_signal[NB_FILTERS]
Definition: aptx.h:56
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
static av_always_inline void aptx_qmf_polyphase_synthesis(FilterSignal signal[NB_FILTERS], const int32_t coeffs[NB_FILTERS][FILTER_TAPS], int shift, int32_t low_subband_input, int32_t high_subband_input, int32_t samples[NB_FILTERS])
Definition: aptxdec.c:31
void * priv_data
Definition: avcodec.h:553
AVCodec ff_aptx_decoder
static int aptx_decode_samples(AptXContext *ctx, const uint8_t *input, int32_t samples[NB_CHANNELS][4])
Definition: aptxdec.c:109
#define FILTER_TAPS
Definition: aptx.h:48
Filter the word “frame” indicates either a video frame or a group of audio samples
#define av_always_inline
Definition: attributes.h:45
This structure stores compressed data.
Definition: packet.h:340
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:374
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
int i
Definition: input.c:407