FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/adtsenc.c
Date: 2026-09-15 15:24:09
Exec Total Coverage
Lines: 71 101 70.3%
Functions: 6 6 100.0%
Branches: 20 42 47.6%

Line Branch Exec Source
1 /*
2 * ADTS muxer.
3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
4 * Mans Rullgard <mans@mansr.com>
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 "libavcodec/get_bits.h"
24 #include "libavcodec/put_bits.h"
25 #include "libavcodec/codec_id.h"
26 #include "libavcodec/codec_par.h"
27 #include "libavcodec/packet.h"
28 #include "libavcodec/mpeg4audio.h"
29 #include "libavcodec/mpeg4audio_copy_pce.h"
30 #include "libavutil/opt.h"
31 #include "avformat.h"
32 #include "apetag.h"
33 #include "id3v2.h"
34 #include "mux.h"
35
36 #define ADTS_HEADER_SIZE 7
37
38 typedef struct ADTSContext {
39 AVClass *class;
40 int write_adts;
41 int objecttype;
42 int sample_rate_index;
43 int channel_conf;
44 int pce_size;
45 int apetag;
46 int id3v2tag;
47 int mpeg_id;
48 uint8_t pce_data[MAX_PCE_SIZE];
49 } ADTSContext;
50
51 #define ADTS_MAX_FRAME_BYTES ((1 << 14) - 1)
52
53 9 static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, const uint8_t *buf, int size)
54 {
55 GetBitContext gb;
56 PutBitContext pb;
57 MPEG4AudioConfig m4ac;
58 int off, ret;
59
60 9 ret = init_get_bits8(&gb, buf, size);
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
62 return ret;
63 9 off = avpriv_mpeg4audio_get_config2(&m4ac, buf, size, 1, s);
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (off < 0)
65 return off;
66 9 skip_bits_long(&gb, off);
67 9 adts->objecttype = m4ac.object_type - 1;
68 9 adts->sample_rate_index = m4ac.sampling_index;
69 9 adts->channel_conf = m4ac.chan_config;
70
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (adts->objecttype > 3U) {
72 av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
73 return AVERROR_INVALIDDATA;
74 }
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (adts->sample_rate_index == 15) {
76 av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
77 return AVERROR_INVALIDDATA;
78 }
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (adts->channel_conf > 7) {
80 av_log(s, AV_LOG_ERROR, "channelConfiguration > 7 is not supported in ADTS\n");
81 return AVERROR_INVALIDDATA;
82 }
83
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (get_bits(&gb, 1)) {
84 av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
85 return AVERROR_INVALIDDATA;
86 }
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (get_bits(&gb, 1)) {
88 av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
89 return AVERROR_INVALIDDATA;
90 }
91
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (get_bits(&gb, 1)) {
92 av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
93 return AVERROR_INVALIDDATA;
94 }
95
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 if (!adts->channel_conf) {
96 8 init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
97
98 8 put_bits(&pb, 3, 5); //ID_PCE
99 8 adts->pce_size = (ff_copy_pce_data(&pb, &gb) + 3) / 8;
100 8 flush_put_bits(&pb);
101 }
102
103 9 adts->write_adts = 1;
104
105 9 return 0;
106 }
107
108 9 static int adts_init(AVFormatContext *s)
109 {
110 9 ADTSContext *adts = s->priv_data;
111 9 AVCodecParameters *par = s->streams[0]->codecpar;
112
113
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (par->extradata_size > 0)
114 9 return adts_decode_extradata(s, adts, par->extradata,
115 par->extradata_size);
116
117 return 0;
118 }
119
120 9 static int adts_write_header(AVFormatContext *s)
121 {
122 9 ADTSContext *adts = s->priv_data;
123
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (adts->id3v2tag)
125 ff_id3v2_write_simple(s, 4, ID3v2_DEFAULT_MAGIC);
126
127 9 return 0;
128 }
129
130 375 static int adts_write_frame_header(AVFormatContext *s, ADTSContext *ctx,
131 uint8_t *buf, int size, int pce_size)
132 {
133 PutBitContext pb;
134
135 375 unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
375 if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
137 av_log(s, AV_LOG_ERROR, "frame size too large: %u (max %d)\n",
138 full_frame_size, ADTS_MAX_FRAME_BYTES);
139 return AVERROR_INVALIDDATA;
140 }
141
142 375 init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
143
144 /* adts_fixed_header */
145 375 put_bits(&pb, 12, 0xfff); /* syncword */
146 375 put_bits(&pb, 1, ctx->mpeg_id); /* ID */
147 375 put_bits(&pb, 2, 0); /* layer */
148 375 put_bits(&pb, 1, 1); /* protection_absent */
149 375 put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
150 375 put_bits(&pb, 4, ctx->sample_rate_index);
151 375 put_bits(&pb, 1, 0); /* private_bit */
152 375 put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
153 375 put_bits(&pb, 1, 0); /* original_copy */
154 375 put_bits(&pb, 1, 0); /* home */
155
156 /* adts_variable_header */
157 375 put_bits(&pb, 1, 0); /* copyright_identification_bit */
158 375 put_bits(&pb, 1, 0); /* copyright_identification_start */
159 375 put_bits(&pb, 13, full_frame_size); /* aac_frame_length */
160 375 put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
161 375 put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
162
163 375 flush_put_bits(&pb);
164
165 375 return 0;
166 }
167
168 375 static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
169 {
170 375 ADTSContext *adts = s->priv_data;
171 375 AVCodecParameters *par = s->streams[0]->codecpar;
172 375 AVIOContext *pb = s->pb;
173 uint8_t buf[ADTS_HEADER_SIZE];
174
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
375 if (!pkt->size)
176 return 0;
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
375 if (!par->extradata_size) {
178 uint8_t *side_data;
179 size_t side_data_size;
180 int ret;
181
182 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
183 &side_data_size);
184 if (side_data_size) {
185 ret = adts_decode_extradata(s, adts, side_data, side_data_size);
186 if (ret < 0)
187 return ret;
188 ret = ff_alloc_extradata(par, side_data_size);
189 if (ret < 0)
190 return ret;
191 memcpy(par->extradata, side_data, side_data_size);
192 }
193 }
194
1/2
✓ Branch 0 taken 375 times.
✗ Branch 1 not taken.
375 if (adts->write_adts) {
195 375 int err = adts_write_frame_header(s, adts, buf, pkt->size,
196 adts->pce_size);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
375 if (err < 0)
198 return err;
199 375 avio_write(pb, buf, ADTS_HEADER_SIZE);
200
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 367 times.
375 if (adts->pce_size) {
201 8 avio_write(pb, adts->pce_data, adts->pce_size);
202 8 adts->pce_size = 0;
203 }
204 }
205 375 avio_write(pb, pkt->data, pkt->size);
206
207 375 return 0;
208 }
209
210 9 static int adts_write_trailer(AVFormatContext *s)
211 {
212 9 ADTSContext *adts = s->priv_data;
213
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (adts->apetag)
215 ff_ape_write_tag(s);
216
217 9 return 0;
218 }
219
220 #define ENC AV_OPT_FLAG_ENCODING_PARAM
221 #define OFFSET(obj) offsetof(ADTSContext, obj)
222 static const AVOption options[] = {
223 { "write_id3v2", "Enable ID3v2 tag writing", OFFSET(id3v2tag), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
224 { "write_apetag", "Enable APE tag writing", OFFSET(apetag), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
225 { "write_mpeg2", "Set MPEG version to MPEG-2", OFFSET(mpeg_id), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
226 { NULL },
227 };
228
229 static const AVClass adts_muxer_class = {
230 .class_name = "ADTS muxer",
231 .item_name = av_default_item_name,
232 .option = options,
233 .version = LIBAVUTIL_VERSION_INT,
234 };
235
236 const FFOutputFormat ff_adts_muxer = {
237 .p.name = "adts",
238 .p.long_name = NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
239 .p.mime_type = "audio/aac",
240 .p.extensions = "aac,adts",
241 .priv_data_size = sizeof(ADTSContext),
242 .p.audio_codec = AV_CODEC_ID_AAC,
243 .p.video_codec = AV_CODEC_ID_NONE,
244 .p.subtitle_codec = AV_CODEC_ID_NONE,
245 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
246 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
247 .init = adts_init,
248 .write_header = adts_write_header,
249 .write_packet = adts_write_packet,
250 .write_trailer = adts_write_trailer,
251 .p.priv_class = &adts_muxer_class,
252 .p.flags = AVFMT_NOTIMESTAMPS,
253 };
254