FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/ftr.c
Date: 2024-03-29 01:21:52
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 typedef struct FTRContext {
26 AVCodecContext *aac_avctx[64]; // wrapper context for AAC
27 int nb_context;
28 AVPacket *packet;
29 AVFrame *frame;
30 } FTRContext;
31
32 static av_cold int ftr_init(AVCodecContext *avctx)
33 {
34 FTRContext *s = avctx->priv_data;
35 const AVCodec *codec;
36 int ret;
37
38 if (avctx->ch_layout.nb_channels > 64 ||
39 avctx->ch_layout.nb_channels <= 0)
40 return AVERROR(EINVAL);
41
42 s->packet = av_packet_alloc();
43 if (!s->packet)
44 return AVERROR(ENOMEM);
45
46 s->frame = av_frame_alloc();
47 if (!s->frame)
48 return AVERROR(ENOMEM);
49
50 s->nb_context = avctx->ch_layout.nb_channels;
51
52 codec = avcodec_find_decoder(AV_CODEC_ID_AAC);
53 if (!codec)
54 return AVERROR_BUG;
55
56 for (int i = 0; i < s->nb_context; i++) {
57 s->aac_avctx[i] = avcodec_alloc_context3(codec);
58 if (!s->aac_avctx[i])
59 return AVERROR(ENOMEM);
60 ret = avcodec_open2(s->aac_avctx[i], codec, NULL);
61 if (ret < 0)
62 return ret;
63 }
64
65 avctx->sample_fmt = s->aac_avctx[0]->sample_fmt;
66 if (!av_sample_fmt_is_planar(avctx->sample_fmt))
67 return AVERROR(EINVAL);
68
69 return 0;
70 }
71
72 static int ftr_decode_frame(AVCodecContext *avctx, AVFrame *frame,
73 int *got_frame, AVPacket *avpkt)
74 {
75 FTRContext *s = avctx->priv_data;
76 GetBitContext gb;
77 int ret, ch_offset = 0;
78
79 ret = init_get_bits8(&gb, avpkt->data, avpkt->size);
80 if (ret < 0)
81 return ret;
82
83 frame->nb_samples = 0;
84
85 for (int i = 0; i < s->nb_context; i++) {
86 AVCodecContext *codec_avctx = s->aac_avctx[i];
87 GetBitContext gb2 = gb;
88 AACADTSHeaderInfo hdr_info;
89 int size;
90
91 if (get_bits_left(&gb) < 64)
92 return AVERROR_INVALIDDATA;
93
94 memset(&hdr_info, 0, sizeof(hdr_info));
95
96 size = ff_adts_header_parse(&gb2, &hdr_info);
97 if (size <= 0 || size * 8 > get_bits_left(&gb))
98 return AVERROR_INVALIDDATA;
99
100 if (size > s->packet->size) {
101 ret = av_grow_packet(s->packet, size - s->packet->size);
102 if (ret < 0)
103 return ret;
104 }
105
106 ret = av_packet_make_writable(s->packet);
107 if (ret < 0)
108 return ret;
109
110 memcpy(s->packet->data, avpkt->data + (get_bits_count(&gb) >> 3), size);
111 s->packet->size = size;
112
113 if (size > 12) {
114 uint8_t *buf = s->packet->data;
115
116 if (buf[3] & 0x20) {
117 int tmp = buf[8];
118 buf[ 9] = ~buf[9];
119 buf[11] = ~buf[11];
120 buf[12] = ~buf[12];
121 buf[ 8] = ~buf[10];
122 buf[10] = ~tmp;
123 }
124 }
125
126 ret = avcodec_send_packet(codec_avctx, s->packet);
127 if (ret < 0) {
128 av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
129 return ret;
130 }
131
132 ret = avcodec_receive_frame(codec_avctx, s->frame);
133 if (ret < 0)
134 return ret;
135
136 if (!avctx->sample_rate) {
137 avctx->sample_rate = codec_avctx->sample_rate;
138 } else {
139 if (avctx->sample_rate != codec_avctx->sample_rate)
140 return AVERROR_INVALIDDATA;
141 }
142
143 if (!frame->nb_samples) {
144 frame->nb_samples = s->frame->nb_samples;
145 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
146 return ret;
147 } else {
148 if (frame->nb_samples != s->frame->nb_samples)
149 return AVERROR_INVALIDDATA;
150 }
151
152 skip_bits_long(&gb, size * 8);
153
154 if (ch_offset + s->frame->ch_layout.nb_channels > avctx->ch_layout.nb_channels)
155 return AVERROR_INVALIDDATA;
156
157 if (avctx->sample_fmt != codec_avctx->sample_fmt)
158 return AVERROR_INVALIDDATA;
159
160 for (int ch = 0; ch < s->frame->ch_layout.nb_channels; ch++)
161 memcpy(frame->extended_data[ch_offset + ch],
162 s->frame->extended_data[ch],
163 av_get_bytes_per_sample(codec_avctx->sample_fmt) * s->frame->nb_samples);
164
165 ch_offset += s->frame->ch_layout.nb_channels;
166
167 if (ch_offset >= avctx->ch_layout.nb_channels)
168 break;
169 }
170
171 *got_frame = 1;
172
173 return get_bits_count(&gb) >> 3;
174 }
175
176 static void ftr_flush(AVCodecContext *avctx)
177 {
178 FTRContext *s = avctx->priv_data;
179
180 for (int i = 0; i < s->nb_context; i++)
181 avcodec_flush_buffers(s->aac_avctx[i]);
182 }
183
184 static av_cold int ftr_close(AVCodecContext *avctx)
185 {
186 FTRContext *s = avctx->priv_data;
187
188 for (int i = 0; i < s->nb_context; i++)
189 avcodec_free_context(&s->aac_avctx[i]);
190 av_packet_free(&s->packet);
191 av_frame_free(&s->frame);
192
193 return 0;
194 }
195
196 const FFCodec ff_ftr_decoder = {
197 .p.name = "ftr",
198 .p.long_name = NULL_IF_CONFIG_SMALL("FTR Voice"),
199 .p.type = AVMEDIA_TYPE_AUDIO,
200 .p.id = AV_CODEC_ID_FTR,
201 .init = ftr_init,
202 FF_CODEC_DECODE_CB(ftr_decode_frame),
203 .close = ftr_close,
204 .flush = ftr_flush,
205 .priv_data_size = sizeof(FTRContext),
206 .p.capabilities =
207 #if FF_API_SUBFRAMES
208 AV_CODEC_CAP_SUBFRAMES |
209 #endif
210 AV_CODEC_CAP_DR1,
211 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
212 };
213