FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ftr.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 0 103 0.0%
Functions: 0 4 0.0%
Branches: 0 64 0.0%

Line Branch Exec 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 {
36 FTRContext *s = avctx->priv_data;
37 const AVCodec *codec;
38 int ret;
39
40 if (avctx->ch_layout.nb_channels > 64 ||
41 avctx->ch_layout.nb_channels <= 0)
42 return AVERROR(EINVAL);
43
44 s->packet = av_packet_alloc();
45 if (!s->packet)
46 return AVERROR(ENOMEM);
47
48 s->frame = av_frame_alloc();
49 if (!s->frame)
50 return AVERROR(ENOMEM);
51
52 s->nb_context = avctx->ch_layout.nb_channels;
53
54 codec = avcodec_find_decoder(AV_CODEC_ID_AAC);
55 if (!codec)
56 return AVERROR_DECODER_NOT_FOUND;
57
58 for (int i = 0; i < s->nb_context; i++) {
59 s->aac_avctx[i] = avcodec_alloc_context3(codec);
60 if (!s->aac_avctx[i])
61 return AVERROR(ENOMEM);
62 ret = avcodec_open2(s->aac_avctx[i], codec, NULL);
63 if (ret < 0)
64 return ret;
65 }
66
67 avctx->sample_fmt = s->aac_avctx[0]->sample_fmt;
68 if (!av_sample_fmt_is_planar(avctx->sample_fmt))
69 return AVERROR(EINVAL);
70
71 return 0;
72 }
73
74 static int ftr_decode_frame(AVCodecContext *avctx, AVFrame *frame,
75 int *got_frame, AVPacket *avpkt)
76 {
77 FTRContext *s = avctx->priv_data;
78 GetBitContext gb;
79 int ret, ch_offset = 0;
80
81 ret = init_get_bits8(&gb, avpkt->data, avpkt->size);
82 if (ret < 0)
83 return ret;
84
85 frame->nb_samples = 0;
86
87 for (int i = 0; i < s->nb_context; i++) {
88 AVCodecContext *codec_avctx = s->aac_avctx[i];
89 GetBitContext gb2 = gb;
90 AACADTSHeaderInfo hdr_info;
91 int size;
92
93 if (get_bits_left(&gb) < 64)
94 return AVERROR_INVALIDDATA;
95
96 memset(&hdr_info, 0, sizeof(hdr_info));
97
98 size = ff_adts_header_parse(&gb2, &hdr_info);
99 if (size <= 0 || size * 8 > get_bits_left(&gb))
100 return AVERROR_INVALIDDATA;
101
102 if (size > s->packet->size) {
103 ret = av_grow_packet(s->packet, size - s->packet->size);
104 if (ret < 0)
105 return ret;
106 }
107
108 ret = av_packet_make_writable(s->packet);
109 if (ret < 0)
110 return ret;
111
112 memcpy(s->packet->data, avpkt->data + (get_bits_count(&gb) >> 3), size);
113 s->packet->size = size;
114
115 if (size > 12) {
116 uint8_t *buf = s->packet->data;
117
118 if (buf[3] & 0x20) {
119 int tmp = buf[8];
120 buf[ 9] = ~buf[9];
121 buf[11] = ~buf[11];
122 buf[12] = ~buf[12];
123 buf[ 8] = ~buf[10];
124 buf[10] = ~tmp;
125 }
126 }
127
128 ret = avcodec_send_packet(codec_avctx, s->packet);
129 if (ret < 0) {
130 av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
131 return ret;
132 }
133
134 ret = avcodec_receive_frame(codec_avctx, s->frame);
135 if (ret < 0)
136 return ret;
137
138 if (!avctx->sample_rate) {
139 avctx->sample_rate = codec_avctx->sample_rate;
140 } else {
141 if (avctx->sample_rate != codec_avctx->sample_rate)
142 return AVERROR_INVALIDDATA;
143 }
144
145 if (!frame->nb_samples) {
146 frame->nb_samples = s->frame->nb_samples;
147 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
148 return ret;
149 } else {
150 if (frame->nb_samples != s->frame->nb_samples)
151 return AVERROR_INVALIDDATA;
152 }
153
154 skip_bits_long(&gb, size * 8);
155
156 if (ch_offset + s->frame->ch_layout.nb_channels > avctx->ch_layout.nb_channels)
157 return AVERROR_INVALIDDATA;
158
159 if (avctx->sample_fmt != codec_avctx->sample_fmt)
160 return AVERROR_INVALIDDATA;
161
162 for (int ch = 0; ch < s->frame->ch_layout.nb_channels; ch++)
163 memcpy(frame->extended_data[ch_offset + ch],
164 s->frame->extended_data[ch],
165 av_get_bytes_per_sample(codec_avctx->sample_fmt) * s->frame->nb_samples);
166
167 ch_offset += s->frame->ch_layout.nb_channels;
168
169 if (ch_offset >= avctx->ch_layout.nb_channels)
170 break;
171 }
172
173 *got_frame = 1;
174
175 return get_bits_count(&gb) >> 3;
176 }
177
178 static av_cold void ftr_flush(AVCodecContext *avctx)
179 {
180 FTRContext *s = avctx->priv_data;
181
182 for (int i = 0; i < s->nb_context; i++)
183 avcodec_flush_buffers(s->aac_avctx[i]);
184 }
185
186 static av_cold int ftr_close(AVCodecContext *avctx)
187 {
188 FTRContext *s = avctx->priv_data;
189
190 for (int i = 0; i < s->nb_context; i++)
191 avcodec_free_context(&s->aac_avctx[i]);
192 av_packet_free(&s->packet);
193 av_frame_free(&s->frame);
194
195 return 0;
196 }
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 };
211