FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/adtsenc.c
Date: 2022-12-09 07:38:14
Exec Total Coverage
Lines: 65 101 64.4%
Functions: 6 6 100.0%
Branches: 18 42 42.9%

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