FFmpeg
Loading...
Searching...
No Matches
aptxenc.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 "config_components.h"
24
26#include "aptx.h"
27#include "audio_frame_queue.h"
28#include "codec_internal.h"
29#include "encode.h"
30#include "internal.h"
31
36
37/*
38 * Half-band QMF analysis filter realized with a polyphase FIR filter.
39 * Split into 2 subbands and downsample by 2.
40 * So for each pair of samples that goes in, one sample goes out,
41 * split into 2 separate subbands.
42 */
45 const int32_t coeffs[NB_FILTERS][FILTER_TAPS],
46 int shift,
47 int32_t samples[NB_FILTERS],
48 int32_t *low_subband_output,
49 int32_t *high_subband_output)
50{
52 int i;
53
54 for (i = 0; i < NB_FILTERS; i++) {
55 aptx_qmf_filter_signal_push(&signal[i], samples[NB_FILTERS-1-i]);
56 subbands[i] = aptx_qmf_convolution(&signal[i], coeffs[i], shift);
57 }
58
59 *low_subband_output = av_clip_intp2(subbands[0] + subbands[1], 23);
60 *high_subband_output = av_clip_intp2(subbands[0] - subbands[1], 23);
61}
62
63/*
64 * Two stage QMF analysis tree.
65 * Split 4 input samples into 4 subbands and downsample by 4.
66 * So for each group of 4 samples that goes in, one sample goes out,
67 * split into 4 separate subbands.
68 */
70 int32_t samples[4],
71 int32_t subband_samples[4])
72{
73 int32_t intermediate_samples[4];
74 int i;
75
76 /* Split 4 input samples into 2 intermediate subbands downsampled to 2 samples */
77 for (i = 0; i < 2; i++)
80 &samples[2*i],
81 &intermediate_samples[0+i],
82 &intermediate_samples[2+i]);
83
84 /* Split 2 intermediate subband samples into 4 final subbands downsampled to 1 sample */
85 for (i = 0; i < 2; i++)
88 &intermediate_samples[2*i],
89 &subband_samples[2*i+0],
90 &subband_samples[2*i+1]);
91}
92
95 const int32_t *intervals, int32_t nb_intervals)
96{
97 int32_t idx = 0;
98 int i;
99
100 for (i = nb_intervals >> 1; i > 0; i >>= 1)
101 if (MUL64(factor, intervals[idx + i]) <= ((int64_t)value << 24))
102 idx += i;
103
104 return idx;
105}
106
108 int32_t sample_difference,
110 int32_t quantization_factor,
112{
113 const int32_t *intervals = tables->quantize_intervals;
114 int32_t quantized_sample, dithered_sample, parity_change;
115 int32_t d, mean, interval, inv, sample_difference_abs;
117
118 sample_difference_abs = FFABS(sample_difference);
119 sample_difference_abs = FFMIN(sample_difference_abs, (1 << 23) - 1);
120
121 quantized_sample = aptx_bin_search(sample_difference_abs >> 4,
122 quantization_factor,
123 intervals, tables->tables_size);
124
125 d = rshift32_clip24(MULH(dither, dither), 7) - (1 << 23);
126 d = rshift64(MUL64(d, tables->quantize_dither_factors[quantized_sample]), 23);
127
128 intervals += quantized_sample;
129 mean = (intervals[1] + intervals[0]) / 2;
130 interval = (intervals[1] - intervals[0]) * (-(sample_difference < 0) | 1);
131
132 dithered_sample = rshift64_clip24(MUL64(dither, interval) + ((int64_t)av_clip_intp2(mean + d, 23) << 32), 32);
133 error = ((int64_t)sample_difference_abs << 20) - MUL64(dithered_sample, quantization_factor);
134 quantize->error = FFABS(rshift64(error, 23));
135
136 parity_change = quantized_sample;
137 if (error < 0)
138 quantized_sample--;
139 else
140 parity_change--;
141
142 inv = -(sample_difference < 0);
143 quantize->quantized_sample = quantized_sample ^ inv;
144 quantize->quantized_sample_parity_change = parity_change ^ inv;
145}
146
147static void aptx_encode_channel(Channel *channel, int32_t samples[4], int hd)
148{
149 int32_t subband_samples[4];
150 int subband;
151 aptx_qmf_tree_analysis(&channel->qmf, samples, subband_samples);
153 for (subband = 0; subband < NB_SUBBANDS; subband++) {
154 int32_t diff = av_clip_intp2(subband_samples[subband] - channel->prediction[subband].predicted_sample, 23);
155 aptx_quantize_difference(&channel->quantize[subband], diff,
156 channel->dither[subband],
157 channel->invert_quantize[subband].quantization_factor,
158 &ff_aptx_quant_tables[hd][subband]);
159 }
160}
161
163{
164 if (aptx_check_parity(channels, idx)) {
165 int i;
166 Channel *c;
167 static const int map[] = { 1, 2, 0, 3 };
168 Quantize *min = &channels[NB_CHANNELS-1].quantize[map[0]];
169 for (c = &channels[NB_CHANNELS-1]; c >= channels; c--)
170 for (i = 0; i < NB_SUBBANDS; i++)
171 if (c->quantize[map[i]].error < min->error)
172 min = &c->quantize[map[i]];
173
174 /* Forcing the desired parity is done by offsetting by 1 the quantized
175 * sample from the subband featuring the smallest quantization error. */
176 min->quantized_sample = min->quantized_sample_parity_change;
177 }
178}
179
181{
183 return (((channel->quantize[3].quantized_sample & 0x06) | parity) << 13)
184 | (((channel->quantize[2].quantized_sample & 0x03) ) << 11)
185 | (((channel->quantize[1].quantized_sample & 0x0F) ) << 7)
186 | (((channel->quantize[0].quantized_sample & 0x7F) ) << 0);
187}
188
190{
192 return (((channel->quantize[3].quantized_sample & 0x01E) | parity) << 19)
193 | (((channel->quantize[2].quantized_sample & 0x00F) ) << 15)
194 | (((channel->quantize[1].quantized_sample & 0x03F) ) << 9)
195 | (((channel->quantize[0].quantized_sample & 0x1FF) ) << 0);
196}
197
199 int32_t samples[NB_CHANNELS][4],
200 uint8_t *output)
201{
202 int channel;
203 for (channel = 0; channel < NB_CHANNELS; channel++)
204 aptx_encode_channel(&ctx->channels[channel], samples[channel], ctx->hd);
205
206 aptx_insert_sync(ctx->channels, &ctx->sync_idx);
207
208 for (channel = 0; channel < NB_CHANNELS; channel++) {
210 if (ctx->hd)
211 AV_WB24(output + 3*channel,
212 aptxhd_pack_codeword(&ctx->channels[channel]));
213 else
214 AV_WB16(output + 2*channel,
215 aptx_pack_codeword(&ctx->channels[channel]));
216 }
217}
218
219static int aptx_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
220 const AVFrame *frame, int *got_packet_ptr)
221{
222 AptXEncContext *const s0 = avctx->priv_data;
223 AptXContext *const s = &s0->common;
224 int pos, ipos, channel, sample, output_size, ret;
225
226 if ((ret = ff_af_queue_add(&s0->afq, frame)) < 0)
227 return ret;
228
229 output_size = s->block_size * frame->nb_samples/4;
230 if ((ret = ff_get_encode_buffer(avctx, avpkt, output_size, 0)) < 0)
231 return ret;
232
233 for (pos = 0, ipos = 0; pos < output_size; pos += s->block_size, ipos += 4) {
234 int32_t samples[NB_CHANNELS][4];
235
236 for (channel = 0; channel < NB_CHANNELS; channel++)
237 for (sample = 0; sample < 4; sample++)
238 samples[channel][sample] = (int32_t)AV_RN32A(&frame->data[channel][4*(ipos+sample)]) >> 8;
239
240 aptx_encode_samples(s, samples, avpkt->data + pos);
241 }
242
243 ret = ff_af_queue_remove(&s0->afq, frame->nb_samples, avpkt);
244 if (ret < 0)
245 return ret;
246
247 *got_packet_ptr = 1;
248 return 0;
249}
250
252{
253 AptXEncContext *const s = avctx->priv_data;
254 ff_af_queue_close(&s->afq);
255 return 0;
256}
257
259{
260 AptXEncContext *const s = avctx->priv_data;
261
262 ff_af_queue_init(avctx, &s->afq);
263
264 if (!avctx->frame_size || avctx->frame_size % 4)
265 avctx->frame_size = 1024;
266 avctx->internal->pad_samples = 4;
267
268 return ff_aptx_init(avctx);
269}
270
271#if CONFIG_APTX_ENCODER
272const FFCodec ff_aptx_encoder = {
273 .p.name = "aptx",
274 CODEC_LONG_NAME("aptX (Audio Processing Technology for Bluetooth)"),
275 .p.type = AVMEDIA_TYPE_AUDIO,
276 .p.id = AV_CODEC_ID_APTX,
278 .priv_data_size = sizeof(AptXEncContext),
281 .close = aptx_close,
284 CODEC_SAMPLERATES(8000, 16000, 24000, 32000, 44100, 48000),
285};
286#endif
287
288#if CONFIG_APTX_HD_ENCODER
290 .p.name = "aptx_hd",
291 CODEC_LONG_NAME("aptX HD (Audio Processing Technology for Bluetooth)"),
292 .p.type = AVMEDIA_TYPE_AUDIO,
293 .p.id = AV_CODEC_ID_APTX_HD,
295 .priv_data_size = sizeof(AptXEncContext),
298 .close = aptx_close,
301 CODEC_SAMPLERATES(8000, 16000, 24000, 32000, 44100, 48000),
302};
303#endif
const FFCodec ff_aptx_hd_encoder
const FFCodec ff_aptx_encoder
av_cold int ff_aptx_init(AVCodecContext *avctx)
Definition aptx.c:508
ConstTables ff_aptx_quant_tables[2][NB_SUBBANDS]
Definition aptx.c:313
void ff_aptx_invert_quantize_and_prediction(Channel *channel, int hd)
Definition aptx.c:497
void ff_aptx_generate_dither(Channel *channel)
Definition aptx.c:385
#define NB_FILTERS
Definition aptx.h:45
static av_always_inline void aptx_qmf_filter_signal_push(FilterSignal *signal, int32_t sample)
Definition aptx.h:162
static const int32_t aptx_qmf_outer_coeffs[NB_FILTERS][FILTER_TAPS]
Definition aptx.h:132
static int aptx_check_parity(Channel channels[NB_CHANNELS], int32_t *idx)
Definition aptx.h:201
static int32_t aptx_quantized_parity(Channel *channel)
Definition aptx.h:188
static const int32_t aptx_qmf_inner_coeffs[NB_FILTERS][FILTER_TAPS]
Definition aptx.h:147
channels
Definition aptx.h:31
@ NB_CHANNELS
Definition aptx.h:34
static av_always_inline int32_t aptx_qmf_convolution(FilterSignal *signal, const int32_t coeffs[FILTER_TAPS], int shift)
Definition aptx.h:174
#define FILTER_TAPS
Definition aptx.h:46
subbands
Definition aptx.h:37
@ NB_SUBBANDS
Definition aptx.h:42
static void aptx_quantize_difference(Quantize *quantize, int32_t sample_difference, int32_t dither, int32_t quantization_factor, ConstTables *tables)
Definition aptxenc.c:107
static av_cold int aptx_encode_init(AVCodecContext *avctx)
Definition aptxenc.c:258
static uint32_t aptxhd_pack_codeword(Channel *channel)
Definition aptxenc.c:189
static av_cold int aptx_close(AVCodecContext *avctx)
Definition aptxenc.c:251
static void aptx_encode_samples(AptXContext *ctx, int32_t samples[NB_CHANNELS][4], uint8_t *output)
Definition aptxenc.c:198
static void aptx_encode_channel(Channel *channel, int32_t samples[4], int hd)
Definition aptxenc.c:147
static void aptx_qmf_tree_analysis(QMFAnalysis *qmf, int32_t samples[4], int32_t subband_samples[4])
Definition aptxenc.c:69
static int aptx_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition aptxenc.c:219
static void aptx_insert_sync(Channel channels[NB_CHANNELS], int32_t *idx)
Definition aptxenc.c:162
static uint16_t aptx_pack_codeword(Channel *channel)
Definition aptxenc.c:180
static av_always_inline void aptx_qmf_polyphase_analysis(FilterSignal signal[NB_FILTERS], const int32_t coeffs[NB_FILTERS][FILTER_TAPS], int shift, int32_t samples[NB_FILTERS], int32_t *low_subband_output, int32_t *high_subband_output)
Definition aptxenc.c:44
static av_always_inline int32_t aptx_bin_search(int32_t value, int32_t factor, const int32_t *intervals, int32_t nb_intervals)
Definition aptxenc.c:94
av_cold void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
int32_t
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
static int quantize(CinepakEncContext *s, int h, uint8_t *data[4], int linesize[4], int v1mode, strip_info *info, mb_encoding encoding)
Definition cinepakenc.c:698
#define CODEC_CH_LAYOUTS(...)
#define CODEC_SAMPLERATES(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
#define av_clip_intp2
Definition common.h:121
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
long long int64_t
Definition coverity.c:34
#define min(a, b)
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition ebur128.h:39
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
double value
Definition eval.c:102
#define sample
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_APTX
Definition codec_id.h:538
@ AV_CODEC_ID_APTX_HD
Definition codec_id.h:539
#define AV_CHANNEL_LAYOUT_STEREO
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition samplefmt.h:65
const VDPAUPixFmtMap * map
#define AV_WB24(p, d)
#define AV_RN32A(p)
#define AV_WB16(p, v)
static int shift(int a, int b)
Definition bonk.c:261
common internal api header.
static const int factor[16]
Definition vf_pp7.c:98
#define av_always_inline
Definition attributes.h:72
#define av_cold
Definition attributes.h:117
#define FFMIN(a, b)
Definition macros.h:49
#define MUL64(a, b)
Definition mathops.h:56
#define MULH
Definition mathops.h:42
unsigned int pos
Definition spdifenc.c:431
main external API structure.
Definition avcodec.h:443
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
int pad_samples
Audio encoders can set this flag during init to indicate that they want the small last frame to be pa...
Definition internal.h:67
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
uint8_t * data
Definition packet.h:603
AudioFrameQueue afq
Definition aptxenc.c:34
AptXContext common
Definition aptxenc.c:33
Definition aptx.h:81
FilterSignal inner_filter_signal[NB_FILTERS][NB_FILTERS]
Definition aptx.h:55
FilterSignal outer_filter_signal[NB_FILTERS]
Definition aptx.h:54
static void error(const char *err)
static AVFormatContext * ctx
Definition movenc.c:49
static const uint8_t *const tables[]
static const uint16_t dither[8][8]
Definition vf_gradfun.c:46
mcdeint parity
Definition vf_mcdeint.c:293
static float mean(const float *input, int size)
Definition vf_nnedi.c:861
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static double c[64]