FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/aptxdec.c
Date: 2021-11-29 18:02:40
Exec Total Coverage
Lines: 0 67 0.0%
Branches: 0 26 0.0%

Line Branch Exec Source
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 "libavutil/channel_layout.h"
24 #include "aptx.h"
25
26 /*
27 * Half-band QMF synthesis filter realized with a polyphase FIR filter.
28 * Join 2 subbands and upsample by 2.
29 * So for each 2 subbands sample that goes in, a pair of samples goes out.
30 */
31 av_always_inline
32 static void aptx_qmf_polyphase_synthesis(FilterSignal signal[NB_FILTERS],
33 const int32_t coeffs[NB_FILTERS][FILTER_TAPS],
34 int shift,
35 int32_t low_subband_input,
36 int32_t high_subband_input,
37 int32_t samples[NB_FILTERS])
38 {
39 int32_t subbands[NB_FILTERS];
40 int i;
41
42 subbands[0] = low_subband_input + high_subband_input;
43 subbands[1] = low_subband_input - high_subband_input;
44
45 for (i = 0; i < NB_FILTERS; i++) {
46 aptx_qmf_filter_signal_push(&signal[i], subbands[1-i]);
47 samples[i] = aptx_qmf_convolution(&signal[i], coeffs[i], shift);
48 }
49 }
50
51 /*
52 * Two stage QMF synthesis tree.
53 * Join 4 subbands and upsample by 4.
54 * So for each 4 subbands sample that goes in, a group of 4 samples goes out.
55 */
56 static void aptx_qmf_tree_synthesis(QMFAnalysis *qmf,
57 int32_t subband_samples[4],
58 int32_t samples[4])
59 {
60 int32_t intermediate_samples[4];
61 int i;
62
63 /* Join 4 subbands into 2 intermediate subbands upsampled to 2 samples. */
64 for (i = 0; i < 2; i++)
65 aptx_qmf_polyphase_synthesis(qmf->inner_filter_signal[i],
66 aptx_qmf_inner_coeffs, 22,
67 subband_samples[2*i+0],
68 subband_samples[2*i+1],
69 &intermediate_samples[2*i]);
70
71 /* Join 2 samples from intermediate subbands upsampled to 4 samples. */
72 for (i = 0; i < 2; i++)
73 aptx_qmf_polyphase_synthesis(qmf->outer_filter_signal,
74 aptx_qmf_outer_coeffs, 21,
75 intermediate_samples[0+i],
76 intermediate_samples[2+i],
77 &samples[2*i]);
78 }
79
80
81 static void aptx_decode_channel(Channel *channel, int32_t samples[4])
82 {
83 int32_t subband_samples[4];
84 int subband;
85 for (subband = 0; subband < NB_SUBBANDS; subband++)
86 subband_samples[subband] = channel->prediction[subband].previous_reconstructed_sample;
87 aptx_qmf_tree_synthesis(&channel->qmf, subband_samples, samples);
88 }
89
90 static void aptx_unpack_codeword(Channel *channel, uint16_t codeword)
91 {
92 channel->quantize[0].quantized_sample = sign_extend(codeword >> 0, 7);
93 channel->quantize[1].quantized_sample = sign_extend(codeword >> 7, 4);
94 channel->quantize[2].quantized_sample = sign_extend(codeword >> 11, 2);
95 channel->quantize[3].quantized_sample = sign_extend(codeword >> 13, 3);
96 channel->quantize[3].quantized_sample = (channel->quantize[3].quantized_sample & ~1)
97 | aptx_quantized_parity(channel);
98 }
99
100 static void aptxhd_unpack_codeword(Channel *channel, uint32_t codeword)
101 {
102 channel->quantize[0].quantized_sample = sign_extend(codeword >> 0, 9);
103 channel->quantize[1].quantized_sample = sign_extend(codeword >> 9, 6);
104 channel->quantize[2].quantized_sample = sign_extend(codeword >> 15, 4);
105 channel->quantize[3].quantized_sample = sign_extend(codeword >> 19, 5);
106 channel->quantize[3].quantized_sample = (channel->quantize[3].quantized_sample & ~1)
107 | aptx_quantized_parity(channel);
108 }
109
110 static int aptx_decode_samples(AptXContext *ctx,
111 const uint8_t *input,
112 int32_t samples[NB_CHANNELS][4])
113 {
114 int channel, ret;
115
116 for (channel = 0; channel < NB_CHANNELS; channel++) {
117 ff_aptx_generate_dither(&ctx->channels[channel]);
118
119 if (ctx->hd)
120 aptxhd_unpack_codeword(&ctx->channels[channel],
121 AV_RB24(input + 3*channel));
122 else
123 aptx_unpack_codeword(&ctx->channels[channel],
124 AV_RB16(input + 2*channel));
125 ff_aptx_invert_quantize_and_prediction(&ctx->channels[channel], ctx->hd);
126 }
127
128 ret = aptx_check_parity(ctx->channels, &ctx->sync_idx);
129
130 for (channel = 0; channel < NB_CHANNELS; channel++)
131 aptx_decode_channel(&ctx->channels[channel], samples[channel]);
132
133 return ret;
134 }
135
136 static int aptx_decode_frame(AVCodecContext *avctx, void *data,
137 int *got_frame_ptr, AVPacket *avpkt)
138 {
139 AptXContext *s = avctx->priv_data;
140 AVFrame *frame = data;
141 int pos, opos, channel, sample, ret;
142
143 if (avpkt->size < s->block_size) {
144 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
145 return AVERROR_INVALIDDATA;
146 }
147
148 /* get output buffer */
149 frame->channels = NB_CHANNELS;
150 frame->format = AV_SAMPLE_FMT_S32P;
151 frame->nb_samples = 4 * avpkt->size / s->block_size;
152 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
153 return ret;
154
155 for (pos = 0, opos = 0; opos < frame->nb_samples; pos += s->block_size, opos += 4) {
156 int32_t samples[NB_CHANNELS][4];
157
158 if (aptx_decode_samples(s, &avpkt->data[pos], samples)) {
159 av_log(avctx, AV_LOG_ERROR, "Synchronization error\n");
160 return AVERROR_INVALIDDATA;
161 }
162
163 for (channel = 0; channel < NB_CHANNELS; channel++)
164 for (sample = 0; sample < 4; sample++)
165 AV_WN32A(&frame->data[channel][4*(opos+sample)],
166 samples[channel][sample] * 256);
167 }
168
169 *got_frame_ptr = 1;
170 return s->block_size * frame->nb_samples / 4;
171 }
172
173 #if CONFIG_APTX_DECODER
174 const AVCodec ff_aptx_decoder = {
175 .name = "aptx",
176 .long_name = NULL_IF_CONFIG_SMALL("aptX (Audio Processing Technology for Bluetooth)"),
177 .type = AVMEDIA_TYPE_AUDIO,
178 .id = AV_CODEC_ID_APTX,
179 .priv_data_size = sizeof(AptXContext),
180 .init = ff_aptx_init,
181 .decode = aptx_decode_frame,
182 .capabilities = AV_CODEC_CAP_DR1,
183 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
184 .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
185 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
186 AV_SAMPLE_FMT_NONE },
187 };
188 #endif
189
190 #if CONFIG_APTX_HD_DECODER
191 const AVCodec ff_aptx_hd_decoder = {
192 .name = "aptx_hd",
193 .long_name = NULL_IF_CONFIG_SMALL("aptX HD (Audio Processing Technology for Bluetooth)"),
194 .type = AVMEDIA_TYPE_AUDIO,
195 .id = AV_CODEC_ID_APTX_HD,
196 .priv_data_size = sizeof(AptXContext),
197 .init = ff_aptx_init,
198 .decode = aptx_decode_frame,
199 .capabilities = AV_CODEC_CAP_DR1,
200 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
201 .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_STEREO, 0},
202 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P,
203 AV_SAMPLE_FMT_NONE },
204 };
205 #endif
206