Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/ftr.c
Line
Count
Source
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18
19
#include "adts_header.h"
20
#include "avcodec.h"
21
#include "codec_internal.h"
22
#include "get_bits.h"
23
#include "decode.h"
24
25
#include "libavutil/attributes.h"
26
27
typedef struct FTRContext {
28
    AVCodecContext *aac_avctx[64];   // wrapper context for AAC
29
    int nb_context;
30
    AVPacket *packet;
31
    AVFrame *frame;
32
} FTRContext;
33
34
static av_cold int ftr_init(AVCodecContext *avctx)
35
145
{
36
145
    FTRContext *s = avctx->priv_data;
37
145
    const AVCodec *codec;
38
145
    int ret;
39
40
145
    if (avctx->ch_layout.nb_channels > 64 ||
41
53
        avctx->ch_layout.nb_channels <= 0)
42
92
        return AVERROR(EINVAL);
43
44
53
    s->packet = av_packet_alloc();
45
53
    if (!s->packet)
46
0
        return AVERROR(ENOMEM);
47
48
53
    s->frame = av_frame_alloc();
49
53
    if (!s->frame)
50
0
        return AVERROR(ENOMEM);
51
52
53
    s->nb_context = avctx->ch_layout.nb_channels;
53
54
53
    codec = avcodec_find_decoder(AV_CODEC_ID_AAC);
55
53
    if (!codec)
56
53
        return AVERROR_DECODER_NOT_FOUND;
57
58
0
    for (int i = 0; i < s->nb_context; i++) {
59
0
        s->aac_avctx[i] = avcodec_alloc_context3(codec);
60
0
        if (!s->aac_avctx[i])
61
0
            return AVERROR(ENOMEM);
62
0
        ret = avcodec_open2(s->aac_avctx[i], codec, NULL);
63
0
        if (ret < 0)
64
0
            return ret;
65
0
    }
66
67
0
    avctx->sample_fmt = s->aac_avctx[0]->sample_fmt;
68
0
    if (!av_sample_fmt_is_planar(avctx->sample_fmt))
69
0
        return AVERROR(EINVAL);
70
71
0
    return 0;
72
0
}
73
74
static int ftr_decode_frame(AVCodecContext *avctx, AVFrame *frame,
75
                            int *got_frame, AVPacket *avpkt)
76
0
{
77
0
    FTRContext *s = avctx->priv_data;
78
0
    GetBitContext gb;
79
0
    int ret, ch_offset = 0;
80
81
0
    ret = init_get_bits8(&gb, avpkt->data, avpkt->size);
82
0
    if (ret < 0)
83
0
        return ret;
84
85
0
    frame->nb_samples = 0;
86
87
0
    for (int i = 0; i < s->nb_context; i++) {
88
0
        AVCodecContext *codec_avctx = s->aac_avctx[i];
89
0
        GetBitContext gb2 = gb;
90
0
        AACADTSHeaderInfo hdr_info;
91
0
        int size;
92
93
0
        if (get_bits_left(&gb) < 64)
94
0
            return AVERROR_INVALIDDATA;
95
96
0
        memset(&hdr_info, 0, sizeof(hdr_info));
97
98
0
        size = ff_adts_header_parse(&gb2, &hdr_info);
99
0
        if (size <= 0 || size * 8 > get_bits_left(&gb))
100
0
            return AVERROR_INVALIDDATA;
101
102
0
        if (size > s->packet->size) {
103
0
            ret = av_grow_packet(s->packet, size - s->packet->size);
104
0
            if (ret < 0)
105
0
                return ret;
106
0
        }
107
108
0
        ret = av_packet_make_writable(s->packet);
109
0
        if (ret < 0)
110
0
            return ret;
111
112
0
        memcpy(s->packet->data, avpkt->data + (get_bits_count(&gb) >> 3), size);
113
0
        s->packet->size = size;
114
115
0
        if (size > 12) {
116
0
            uint8_t *buf = s->packet->data;
117
118
0
            if (buf[3] & 0x20) {
119
0
                int tmp = buf[8];
120
0
                buf[ 9] = ~buf[9];
121
0
                buf[11] = ~buf[11];
122
0
                buf[12] = ~buf[12];
123
0
                buf[ 8] = ~buf[10];
124
0
                buf[10] = ~tmp;
125
0
            }
126
0
        }
127
128
0
        ret = avcodec_send_packet(codec_avctx, s->packet);
129
0
        if (ret < 0) {
130
0
            av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
131
0
            return ret;
132
0
        }
133
134
0
        ret = avcodec_receive_frame(codec_avctx, s->frame);
135
0
        if (ret < 0)
136
0
            return ret;
137
138
0
        if (!avctx->sample_rate) {
139
0
            avctx->sample_rate = codec_avctx->sample_rate;
140
0
        } else {
141
0
            if (avctx->sample_rate != codec_avctx->sample_rate)
142
0
                return AVERROR_INVALIDDATA;
143
0
        }
144
145
0
        if (!frame->nb_samples) {
146
0
            frame->nb_samples = s->frame->nb_samples;
147
0
            if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
148
0
                return ret;
149
0
        } else {
150
0
            if (frame->nb_samples != s->frame->nb_samples)
151
0
                return AVERROR_INVALIDDATA;
152
0
        }
153
154
0
        skip_bits_long(&gb, size * 8);
155
156
0
        if (ch_offset + s->frame->ch_layout.nb_channels > avctx->ch_layout.nb_channels)
157
0
            return AVERROR_INVALIDDATA;
158
159
0
        if (avctx->sample_fmt != codec_avctx->sample_fmt)
160
0
            return AVERROR_INVALIDDATA;
161
162
0
        for (int ch = 0; ch < s->frame->ch_layout.nb_channels; ch++)
163
0
            memcpy(frame->extended_data[ch_offset + ch],
164
0
                   s->frame->extended_data[ch],
165
0
                   av_get_bytes_per_sample(codec_avctx->sample_fmt) * s->frame->nb_samples);
166
167
0
        ch_offset += s->frame->ch_layout.nb_channels;
168
169
0
        if (ch_offset >= avctx->ch_layout.nb_channels)
170
0
            break;
171
0
    }
172
173
0
    *got_frame = 1;
174
175
0
    return get_bits_count(&gb) >> 3;
176
0
}
177
178
static av_cold void ftr_flush(AVCodecContext *avctx)
179
0
{
180
0
    FTRContext *s = avctx->priv_data;
181
182
0
    for (int i = 0; i < s->nb_context; i++)
183
0
        avcodec_flush_buffers(s->aac_avctx[i]);
184
0
}
185
186
static av_cold int ftr_close(AVCodecContext *avctx)
187
145
{
188
145
    FTRContext *s = avctx->priv_data;
189
190
1.06k
    for (int i = 0; i < s->nb_context; i++)
191
918
        avcodec_free_context(&s->aac_avctx[i]);
192
145
    av_packet_free(&s->packet);
193
145
    av_frame_free(&s->frame);
194
195
145
    return 0;
196
145
}
197
198
const FFCodec ff_ftr_decoder = {
199
    .p.name         = "ftr",
200
    .p.long_name    = NULL_IF_CONFIG_SMALL("FTR Voice"),
201
    .p.type         = AVMEDIA_TYPE_AUDIO,
202
    .p.id           = AV_CODEC_ID_FTR,
203
    .init           = ftr_init,
204
    FF_CODEC_DECODE_CB(ftr_decode_frame),
205
    .close          = ftr_close,
206
    .flush          = ftr_flush,
207
    .priv_data_size = sizeof(FTRContext),
208
    .p.capabilities = AV_CODEC_CAP_DR1,
209
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
210
};