FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avc.c
Date: 2026-07-21 08:37:06
Exec Total Coverage
Lines: 115 188 61.2%
Functions: 2 3 66.7%
Branches: 43 132 32.6%

Line Branch Exec Source
1 /*
2 * AVC helper functions for muxers
3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/mem.h"
24 #include "libavcodec/h264.h"
25 #include "libavcodec/get_bits.h"
26 #include "libavcodec/golomb.h"
27 #include "avio.h"
28 #include "avc.h"
29 #include "avio_internal.h"
30 #include "nal.h"
31
32 62 int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
33 {
34 62 AVIOContext *sps_pb = NULL, *pps_pb = NULL, *sps_ext_pb = NULL;
35 uint8_t *buf, *end, *start;
36 uint8_t *sps, *pps, *sps_ext;
37 62 uint32_t sps_size = 0, pps_size = 0, sps_ext_size = 0;
38 62 int ret, nb_sps = 0, nb_pps = 0, nb_sps_ext = 0;
39
40
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 if (len <= 6)
41 return AVERROR_INVALIDDATA;
42
43 /* check for H.264 start code */
44
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 4 times.
62 if (AV_RB32(data) != 0x00000001 &&
45
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 AV_RB24(data) != 0x000001) {
46 58 avio_write(pb, data, len);
47 58 return 0;
48 }
49
50 4 ret = ff_nal_parse_units_buf(data, &buf, &len);
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
52 return ret;
53 4 start = buf;
54 4 end = buf + len;
55
56 4 ret = avio_open_dyn_buf(&sps_pb);
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
58 goto fail;
59 4 ret = avio_open_dyn_buf(&pps_pb);
60
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
61 goto fail;
62 4 ret = avio_open_dyn_buf(&sps_ext_pb);
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
64 goto fail;
65
66 /* look for sps and pps */
67
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 while (end - buf > 4) {
68 uint32_t size;
69 uint8_t nal_type;
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 size = FFMIN(AV_RB32(buf), end - buf - 4);
71 8 buf += 4;
72 8 nal_type = buf[0] & 0x1f;
73
74
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (nal_type == 7) { /* SPS */
75 4 nb_sps++;
76
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (size > UINT16_MAX || nb_sps >= H264_MAX_SPS_COUNT) {
77 ret = AVERROR_INVALIDDATA;
78 goto fail;
79 }
80 4 avio_wb16(sps_pb, size);
81 4 avio_write(sps_pb, buf, size);
82
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 } else if (nal_type == 8) { /* PPS */
83 4 nb_pps++;
84
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (size > UINT16_MAX || nb_pps >= H264_MAX_PPS_COUNT) {
85 ret = AVERROR_INVALIDDATA;
86 goto fail;
87 }
88 4 avio_wb16(pps_pb, size);
89 4 avio_write(pps_pb, buf, size);
90 } else if (nal_type == 13) { /* SPS_EXT */
91 nb_sps_ext++;
92 if (size > UINT16_MAX || nb_sps_ext >= 256) {
93 ret = AVERROR_INVALIDDATA;
94 goto fail;
95 }
96 avio_wb16(sps_ext_pb, size);
97 avio_write(sps_ext_pb, buf, size);
98 }
99
100 8 buf += size;
101 }
102 4 sps_size = avio_get_dyn_buf(sps_pb, &sps);
103 4 pps_size = avio_get_dyn_buf(pps_pb, &pps);
104 4 sps_ext_size = avio_get_dyn_buf(sps_ext_pb, &sps_ext);
105
106
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (sps_size < 6 || !pps_size) {
107 ret = AVERROR_INVALIDDATA;
108 goto fail;
109 }
110
111 4 avio_w8(pb, 1); /* version */
112 4 avio_w8(pb, sps[3]); /* profile */
113 4 avio_w8(pb, sps[4]); /* profile compat */
114 4 avio_w8(pb, sps[5]); /* level */
115 4 avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
116 4 avio_w8(pb, 0xe0 | nb_sps); /* 3 bits reserved (111) + 5 bits number of sps */
117
118 4 avio_write(pb, sps, sps_size);
119 4 avio_w8(pb, nb_pps); /* number of pps */
120 4 avio_write(pb, pps, pps_size);
121
122
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
4 if (sps[3] != 66 && sps[3] != 77 && sps[3] != 88) {
123 H264SPS seq;
124 3 ret = ff_avc_decode_sps(&seq, sps + 3, sps_size - 3);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
126 goto fail;
127
128 3 avio_w8(pb, 0xfc | seq.chroma_format_idc); /* 6 bits reserved (111111) + chroma_format_idc */
129 3 avio_w8(pb, 0xf8 | (seq.bit_depth_luma - 8)); /* 5 bits reserved (11111) + bit_depth_luma_minus8 */
130 3 avio_w8(pb, 0xf8 | (seq.bit_depth_chroma - 8)); /* 5 bits reserved (11111) + bit_depth_chroma_minus8 */
131 3 avio_w8(pb, nb_sps_ext); /* number of sps ext */
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (nb_sps_ext)
133 avio_write(pb, sps_ext, sps_ext_size);
134 }
135
136 1 fail:
137 4 ffio_free_dyn_buf(&sps_pb);
138 4 ffio_free_dyn_buf(&pps_pb);
139 4 ffio_free_dyn_buf(&sps_ext_pb);
140 4 av_free(start);
141
142 4 return ret;
143 }
144
145 int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
146 {
147 uint16_t sps_size, pps_size;
148 uint8_t *out;
149 int out_size;
150
151 *buf = NULL;
152 if (*size >= 4 && (AV_RB32(in) == 0x00000001 || AV_RB24(in) == 0x000001))
153 return 0;
154 if (*size < 11 || in[0] != 1)
155 return AVERROR_INVALIDDATA;
156
157 sps_size = AV_RB16(&in[6]);
158 if (11 + sps_size > *size)
159 return AVERROR_INVALIDDATA;
160 pps_size = AV_RB16(&in[9 + sps_size]);
161 if (11 + sps_size + pps_size > *size)
162 return AVERROR_INVALIDDATA;
163 out_size = 8 + sps_size + pps_size;
164 out = av_mallocz(out_size + AV_INPUT_BUFFER_PADDING_SIZE);
165 if (!out)
166 return AVERROR(ENOMEM);
167 AV_WB32(&out[0], 0x00000001);
168 memcpy(out + 4, &in[8], sps_size);
169 AV_WB32(&out[4 + sps_size], 0x00000001);
170 memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size);
171 *buf = out;
172 *size = out_size;
173 return 0;
174 }
175
176 static const AVRational avc_sample_aspect_ratio[17] = {
177 { 0, 1 },
178 { 1, 1 },
179 { 12, 11 },
180 { 10, 11 },
181 { 16, 11 },
182 { 40, 33 },
183 { 24, 11 },
184 { 20, 11 },
185 { 32, 11 },
186 { 80, 33 },
187 { 18, 11 },
188 { 15, 11 },
189 { 64, 33 },
190 { 160, 99 },
191 { 4, 3 },
192 { 3, 2 },
193 { 2, 1 },
194 };
195
196 3 int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size)
197 {
198 int i, j, ret, rbsp_size, aspect_ratio_idc, pic_order_cnt_type;
199 int num_ref_frames_in_pic_order_cnt_cycle;
200 3 int delta_scale, lastScale = 8, nextScale = 8;
201 int sizeOfScalingList;
202 GetBitContext gb;
203 uint8_t *rbsp_buf;
204
205 3 rbsp_buf = ff_nal_unit_extract_rbsp(buf, buf_size, &rbsp_size, 0);
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!rbsp_buf)
207 return AVERROR(ENOMEM);
208
209 3 ret = init_get_bits8(&gb, rbsp_buf, rbsp_size);
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
211 goto end;
212
213 3 memset(sps, 0, sizeof(*sps));
214
215 3 sps->profile_idc = get_bits(&gb, 8);
216 3 sps->constraint_set_flags |= get_bits1(&gb) << 0; // constraint_set0_flag
217 3 sps->constraint_set_flags |= get_bits1(&gb) << 1; // constraint_set1_flag
218 3 sps->constraint_set_flags |= get_bits1(&gb) << 2; // constraint_set2_flag
219 3 sps->constraint_set_flags |= get_bits1(&gb) << 3; // constraint_set3_flag
220 3 sps->constraint_set_flags |= get_bits1(&gb) << 4; // constraint_set4_flag
221 3 sps->constraint_set_flags |= get_bits1(&gb) << 5; // constraint_set5_flag
222 3 skip_bits(&gb, 2); // reserved_zero_2bits
223 3 sps->level_idc = get_bits(&gb, 8);
224 3 sps->id = get_ue_golomb_long(&gb);
225
226
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
227 sps->profile_idc == 122 || sps->profile_idc == 244 || sps->profile_idc == 44 ||
228 sps->profile_idc == 83 || sps->profile_idc == 86 || sps->profile_idc == 118 ||
229 sps->profile_idc == 128 || sps->profile_idc == 138 || sps->profile_idc == 139 ||
230 sps->profile_idc == 134) {
231 3 sps->chroma_format_idc = get_ue_golomb_long(&gb); // chroma_format_idc
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (sps->chroma_format_idc == 3) {
233 skip_bits1(&gb); // separate_colour_plane_flag
234 }
235 3 sps->bit_depth_luma = get_ue_golomb_long(&gb) + 8;
236 3 sps->bit_depth_chroma = get_ue_golomb_long(&gb) + 8;
237 3 skip_bits1(&gb); // qpprime_y_zero_transform_bypass_flag
238
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (get_bits1(&gb)) { // seq_scaling_matrix_present_flag
239 for (i = 0; i < ((sps->chroma_format_idc != 3) ? 8 : 12); i++) {
240 if (!get_bits1(&gb)) // seq_scaling_list_present_flag
241 continue;
242 lastScale = 8;
243 nextScale = 8;
244 sizeOfScalingList = i < 6 ? 16 : 64;
245 for (j = 0; j < sizeOfScalingList; j++) {
246 if (nextScale != 0) {
247 delta_scale = get_se_golomb_long(&gb);
248 nextScale = (lastScale + delta_scale) & 0xff;
249 }
250 lastScale = nextScale == 0 ? lastScale : nextScale;
251 }
252 }
253 }
254 } else {
255 sps->chroma_format_idc = 1;
256 sps->bit_depth_luma = 8;
257 sps->bit_depth_chroma = 8;
258 }
259
260 3 get_ue_golomb_long(&gb); // log2_max_frame_num_minus4
261 3 pic_order_cnt_type = get_ue_golomb_long(&gb);
262
263
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (pic_order_cnt_type == 0) {
264 3 get_ue_golomb_long(&gb); // log2_max_pic_order_cnt_lsb_minus4
265 } else if (pic_order_cnt_type == 1) {
266 skip_bits1(&gb); // delta_pic_order_always_zero
267 get_se_golomb_long(&gb); // offset_for_non_ref_pic
268 get_se_golomb_long(&gb); // offset_for_top_to_bottom_field
269 num_ref_frames_in_pic_order_cnt_cycle = get_ue_golomb_long(&gb);
270 for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++)
271 get_se_golomb_long(&gb); // offset_for_ref_frame
272 }
273
274 3 get_ue_golomb_long(&gb); // max_num_ref_frames
275 3 skip_bits1(&gb); // gaps_in_frame_num_value_allowed_flag
276 3 get_ue_golomb_long(&gb); // pic_width_in_mbs_minus1
277 3 get_ue_golomb_long(&gb); // pic_height_in_map_units_minus1
278
279 3 sps->frame_mbs_only_flag = get_bits1(&gb);
280
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (!sps->frame_mbs_only_flag)
281 2 skip_bits1(&gb); // mb_adaptive_frame_field_flag
282
283 3 skip_bits1(&gb); // direct_8x8_inference_flag
284
285
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 if (get_bits1(&gb)) { // frame_cropping_flag
286 2 get_ue_golomb_long(&gb); // frame_crop_left_offset
287 2 get_ue_golomb_long(&gb); // frame_crop_right_offset
288 2 get_ue_golomb_long(&gb); // frame_crop_top_offset
289 2 get_ue_golomb_long(&gb); // frame_crop_bottom_offset
290 }
291
292
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (get_bits1(&gb)) { // vui_parameters_present_flag
293
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 if (get_bits1(&gb)) { // aspect_ratio_info_present_flag
294 2 aspect_ratio_idc = get_bits(&gb, 8);
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (aspect_ratio_idc == 0xff) {
296 sps->sar.num = get_bits(&gb, 16);
297 sps->sar.den = get_bits(&gb, 16);
298
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(avc_sample_aspect_ratio)) {
299 2 sps->sar = avc_sample_aspect_ratio[aspect_ratio_idc];
300 }
301 }
302 }
303
304
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (!sps->sar.den) {
305 1 sps->sar.num = 1;
306 1 sps->sar.den = 1;
307 }
308
309 3 ret = 0;
310 3 end:
311 3 av_free(rbsp_buf);
312 3 return ret;
313 }
314