Coverage Report

Created: 2026-04-01 07:42

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