FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h264_parse.c
Date: 2024-07-14 13:34:57
Exec Total Coverage
Lines: 218 294 74.1%
Functions: 9 9 100.0%
Branches: 151 200 75.5%

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 "libavutil/mem.h"
20 #include "bytestream.h"
21 #include "get_bits.h"
22 #include "golomb.h"
23 #include "h264.h"
24 #include "h264pred.h"
25 #include "h264_parse.h"
26 #include "h264_ps.h"
27 #include "h2645_parse.h"
28 #include "mpegutils.h"
29
30 6442 int ff_h264_pred_weight_table(GetBitContext *gb, const SPS *sps,
31 const int *ref_count, int slice_type_nos,
32 H264PredWeightTable *pwt,
33 int picture_structure, void *logctx)
34 {
35 int list, i, j;
36 int luma_def, chroma_def;
37
38 6442 pwt->use_weight = 0;
39 6442 pwt->use_weight_chroma = 0;
40
41 6442 pwt->luma_log2_weight_denom = get_ue_golomb_31(gb);
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6442 times.
6442 if (pwt->luma_log2_weight_denom > 7U) {
43 av_log(logctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is out of range\n", pwt->luma_log2_weight_denom);
44 pwt->luma_log2_weight_denom = 0;
45 }
46 6442 luma_def = 1 << pwt->luma_log2_weight_denom;
47
48
1/2
✓ Branch 0 taken 6442 times.
✗ Branch 1 not taken.
6442 if (sps->chroma_format_idc) {
49 6442 pwt->chroma_log2_weight_denom = get_ue_golomb_31(gb);
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6442 times.
6442 if (pwt->chroma_log2_weight_denom > 7U) {
51 av_log(logctx, AV_LOG_ERROR, "chroma_log2_weight_denom %d is out of range\n", pwt->chroma_log2_weight_denom);
52 pwt->chroma_log2_weight_denom = 0;
53 }
54 6442 chroma_def = 1 << pwt->chroma_log2_weight_denom;
55 }
56
57
2/2
✓ Branch 0 taken 6852 times.
✓ Branch 1 taken 410 times.
7262 for (list = 0; list < 2; list++) {
58 6852 pwt->luma_weight_flag[list] = 0;
59 6852 pwt->chroma_weight_flag[list] = 0;
60
2/2
✓ Branch 0 taken 21005 times.
✓ Branch 1 taken 6852 times.
27857 for (i = 0; i < ref_count[list]; i++) {
61 int luma_weight_flag, chroma_weight_flag;
62
63 21005 luma_weight_flag = get_bits1(gb);
64
2/2
✓ Branch 0 taken 6728 times.
✓ Branch 1 taken 14277 times.
21005 if (luma_weight_flag) {
65 6728 pwt->luma_weight[i][list][0] = get_se_golomb(gb);
66 6728 pwt->luma_weight[i][list][1] = get_se_golomb(gb);
67
1/2
✓ Branch 0 taken 6728 times.
✗ Branch 1 not taken.
6728 if ((int8_t)pwt->luma_weight[i][list][0] != pwt->luma_weight[i][list][0] ||
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6728 times.
6728 (int8_t)pwt->luma_weight[i][list][1] != pwt->luma_weight[i][list][1])
69 goto out_range_weight;
70
2/2
✓ Branch 0 taken 2468 times.
✓ Branch 1 taken 4260 times.
6728 if (pwt->luma_weight[i][list][0] != luma_def ||
71
2/2
✓ Branch 0 taken 1688 times.
✓ Branch 1 taken 780 times.
2468 pwt->luma_weight[i][list][1] != 0) {
72 5948 pwt->use_weight = 1;
73 5948 pwt->luma_weight_flag[list] = 1;
74 }
75 } else {
76 14277 pwt->luma_weight[i][list][0] = luma_def;
77 14277 pwt->luma_weight[i][list][1] = 0;
78 }
79
80
1/2
✓ Branch 0 taken 21005 times.
✗ Branch 1 not taken.
21005 if (sps->chroma_format_idc) {
81 21005 chroma_weight_flag = get_bits1(gb);
82
2/2
✓ Branch 0 taken 2893 times.
✓ Branch 1 taken 18112 times.
21005 if (chroma_weight_flag) {
83 int j;
84
2/2
✓ Branch 0 taken 5786 times.
✓ Branch 1 taken 2893 times.
8679 for (j = 0; j < 2; j++) {
85 5786 pwt->chroma_weight[i][list][j][0] = get_se_golomb(gb);
86 5786 pwt->chroma_weight[i][list][j][1] = get_se_golomb(gb);
87
1/2
✓ Branch 0 taken 5786 times.
✗ Branch 1 not taken.
5786 if ((int8_t)pwt->chroma_weight[i][list][j][0] != pwt->chroma_weight[i][list][j][0] ||
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5786 times.
5786 (int8_t)pwt->chroma_weight[i][list][j][1] != pwt->chroma_weight[i][list][j][1]) {
89 pwt->chroma_weight[i][list][j][0] = chroma_def;
90 pwt->chroma_weight[i][list][j][1] = 0;
91 goto out_range_weight;
92 }
93
2/2
✓ Branch 0 taken 623 times.
✓ Branch 1 taken 5163 times.
5786 if (pwt->chroma_weight[i][list][j][0] != chroma_def ||
94
2/2
✓ Branch 0 taken 582 times.
✓ Branch 1 taken 41 times.
623 pwt->chroma_weight[i][list][j][1] != 0) {
95 5745 pwt->use_weight_chroma = 1;
96 5745 pwt->chroma_weight_flag[list] = 1;
97 }
98 }
99 } else {
100 int j;
101
2/2
✓ Branch 0 taken 36224 times.
✓ Branch 1 taken 18112 times.
54336 for (j = 0; j < 2; j++) {
102 36224 pwt->chroma_weight[i][list][j][0] = chroma_def;
103 36224 pwt->chroma_weight[i][list][j][1] = 0;
104 }
105 }
106 }
107
108 // for MBAFF
109
2/2
✓ Branch 0 taken 18799 times.
✓ Branch 1 taken 2206 times.
21005 if (picture_structure == PICT_FRAME) {
110 18799 pwt->luma_weight[16 + 2 * i][list][0] = pwt->luma_weight[16 + 2 * i + 1][list][0] = pwt->luma_weight[i][list][0];
111 18799 pwt->luma_weight[16 + 2 * i][list][1] = pwt->luma_weight[16 + 2 * i + 1][list][1] = pwt->luma_weight[i][list][1];
112
1/2
✓ Branch 0 taken 18799 times.
✗ Branch 1 not taken.
18799 if (sps->chroma_format_idc) {
113
2/2
✓ Branch 0 taken 37598 times.
✓ Branch 1 taken 18799 times.
56397 for (j = 0; j < 2; j++) {
114 37598 pwt->chroma_weight[16 + 2 * i][list][j][0] = pwt->chroma_weight[16 + 2 * i + 1][list][j][0] = pwt->chroma_weight[i][list][j][0];
115 37598 pwt->chroma_weight[16 + 2 * i][list][j][1] = pwt->chroma_weight[16 + 2 * i + 1][list][j][1] = pwt->chroma_weight[i][list][j][1];
116 }
117 }
118 }
119 }
120
2/2
✓ Branch 0 taken 6032 times.
✓ Branch 1 taken 820 times.
6852 if (slice_type_nos != AV_PICTURE_TYPE_B)
121 6032 break;
122 }
123
4/4
✓ Branch 0 taken 3354 times.
✓ Branch 1 taken 3088 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 3349 times.
6442 pwt->use_weight = pwt->use_weight || pwt->use_weight_chroma;
124 6442 return 0;
125 out_range_weight:
126 avpriv_request_sample(logctx, "Out of range weight");
127 return AVERROR_INVALIDDATA;
128 }
129
130 /**
131 * Check if the top & left blocks are available if needed and
132 * change the dc mode so it only uses the available blocks.
133 */
134 3111295 int ff_h264_check_intra4x4_pred_mode(int8_t *pred_mode_cache, void *logctx,
135 int top_samples_available, int left_samples_available)
136 {
137 static const int8_t top[12] = {
138 -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
139 };
140 static const int8_t left[12] = {
141 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
142 };
143 int i;
144
145
2/2
✓ Branch 0 taken 214557 times.
✓ Branch 1 taken 2896738 times.
3111295 if (!(top_samples_available & 0x8000)) {
146
2/2
✓ Branch 0 taken 858228 times.
✓ Branch 1 taken 214557 times.
1072785 for (i = 0; i < 4; i++) {
147 858228 int status = top[pred_mode_cache[scan8[0] + i]];
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 858228 times.
858228 if (status < 0) {
149 av_log(logctx, AV_LOG_ERROR,
150 "top block unavailable for requested intra mode %d\n",
151 status);
152 return AVERROR_INVALIDDATA;
153
2/2
✓ Branch 0 taken 555802 times.
✓ Branch 1 taken 302426 times.
858228 } else if (status) {
154 555802 pred_mode_cache[scan8[0] + i] = status;
155 }
156 }
157 }
158
159
2/2
✓ Branch 0 taken 68277 times.
✓ Branch 1 taken 3043018 times.
3111295 if ((left_samples_available & 0x8888) != 0x8888) {
160 static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
161
2/2
✓ Branch 0 taken 273108 times.
✓ Branch 1 taken 68277 times.
341385 for (i = 0; i < 4; i++)
162
2/2
✓ Branch 0 taken 273054 times.
✓ Branch 1 taken 54 times.
273108 if (!(left_samples_available & mask[i])) {
163 273054 int status = left[pred_mode_cache[scan8[0] + 8 * i]];
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 273054 times.
273054 if (status < 0) {
165 av_log(logctx, AV_LOG_ERROR,
166 "left block unavailable for requested intra4x4 mode %d\n",
167 status);
168 return AVERROR_INVALIDDATA;
169
2/2
✓ Branch 0 taken 100778 times.
✓ Branch 1 taken 172276 times.
273054 } else if (status) {
170 100778 pred_mode_cache[scan8[0] + 8 * i] = status;
171 }
172 }
173 }
174
175 3111295 return 0;
176 }
177
178 /**
179 * Check if the top & left blocks are available if needed and
180 * change the dc mode so it only uses the available blocks.
181 */
182 4998751 int ff_h264_check_intra_pred_mode(void *logctx, int top_samples_available,
183 int left_samples_available,
184 int mode, int is_chroma)
185 {
186 static const int8_t top[4] = { LEFT_DC_PRED8x8, 1, -1, -1 };
187 static const int8_t left[5] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
188
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4998751 times.
4998751 if (mode > 3U) {
190 av_log(logctx, AV_LOG_ERROR,
191 "out of range intra chroma pred mode\n");
192 return AVERROR_INVALIDDATA;
193 }
194
195
2/2
✓ Branch 0 taken 387588 times.
✓ Branch 1 taken 4611163 times.
4998751 if (!(top_samples_available & 0x8000)) {
196 387588 mode = top[mode];
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 387588 times.
387588 if (mode < 0) {
198 av_log(logctx, AV_LOG_ERROR,
199 "top block unavailable for requested intra mode\n");
200 return AVERROR_INVALIDDATA;
201 }
202 }
203
204
2/2
✓ Branch 0 taken 106556 times.
✓ Branch 1 taken 4892195 times.
4998751 if ((left_samples_available & 0x8080) != 0x8080) {
205 106556 mode = left[mode];
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 106556 times.
106556 if (mode < 0) {
207 av_log(logctx, AV_LOG_ERROR,
208 "left block unavailable for requested intra mode\n");
209 return AVERROR_INVALIDDATA;
210 }
211
4/4
✓ Branch 0 taken 86262 times.
✓ Branch 1 taken 20294 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 86235 times.
106556 if (is_chroma && (left_samples_available & 0x8080)) {
212 // mad cow disease mode, aka MBAFF + constrained_intra_pred
213 54 mode = ALZHEIMER_DC_L0T_PRED8x8 +
214
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 16 times.
27 (!(left_samples_available & 0x8000)) +
215
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 20 times.
27 2 * (mode == DC_128_PRED8x8);
216 }
217 }
218
219 4998751 return mode;
220 }
221
222 57845 int ff_h264_parse_ref_count(int *plist_count, int ref_count[2],
223 GetBitContext *gb, const PPS *pps,
224 int slice_type_nos, int picture_structure, void *logctx)
225 {
226 int list_count;
227 int num_ref_idx_active_override_flag;
228
229 // set defaults, might be overridden a few lines later
230 57845 ref_count[0] = pps->ref_count[0];
231 57845 ref_count[1] = pps->ref_count[1];
232
233
2/2
✓ Branch 0 taken 51558 times.
✓ Branch 1 taken 6287 times.
57845 if (slice_type_nos != AV_PICTURE_TYPE_I) {
234 unsigned max[2];
235
2/2
✓ Branch 0 taken 40541 times.
✓ Branch 1 taken 11017 times.
51558 max[0] = max[1] = picture_structure == PICT_FRAME ? 15 : 31;
236
237 51558 num_ref_idx_active_override_flag = get_bits1(gb);
238
239
2/2
✓ Branch 0 taken 21851 times.
✓ Branch 1 taken 29707 times.
51558 if (num_ref_idx_active_override_flag) {
240 21851 ref_count[0] = get_ue_golomb(gb) + 1;
241
2/2
✓ Branch 0 taken 6815 times.
✓ Branch 1 taken 15036 times.
21851 if (slice_type_nos == AV_PICTURE_TYPE_B) {
242 6815 ref_count[1] = get_ue_golomb(gb) + 1;
243 } else
244 // full range is spec-ok in this case, even for frames
245 15036 ref_count[1] = 1;
246 }
247
248
2/2
✓ Branch 0 taken 14151 times.
✓ Branch 1 taken 37407 times.
51558 if (slice_type_nos == AV_PICTURE_TYPE_B)
249 14151 list_count = 2;
250 else
251 37407 list_count = 1;
252
253
4/6
✓ Branch 0 taken 51558 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14151 times.
✓ Branch 3 taken 37407 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14151 times.
51558 if (ref_count[0] - 1 > max[0] || (list_count == 2 && (ref_count[1] - 1 > max[1]))) {
254 av_log(logctx, AV_LOG_ERROR, "reference overflow %u > %u or %u > %u\n",
255 ref_count[0] - 1, max[0], ref_count[1] - 1, max[1]);
256 ref_count[0] = ref_count[1] = 0;
257 *plist_count = 0;
258 goto fail;
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51558 times.
51558 } else if (ref_count[1] - 1 > max[1]) {
260 av_log(logctx, AV_LOG_DEBUG, "reference overflow %u > %u \n",
261 ref_count[1] - 1, max[1]);
262 ref_count[1] = 0;
263 }
264
265 } else {
266 6287 list_count = 0;
267 6287 ref_count[0] = ref_count[1] = 0;
268 }
269
270 57845 *plist_count = list_count;
271
272 57845 return 0;
273 fail:
274 *plist_count = 0;
275 ref_count[0] = 0;
276 ref_count[1] = 0;
277 return AVERROR_INVALIDDATA;
278 }
279
280 60694 int ff_h264_init_poc(int pic_field_poc[2], int *pic_poc,
281 const SPS *sps, H264POCContext *pc,
282 int picture_structure, int nal_ref_idc)
283 {
284 60694 const int max_frame_num = 1 << sps->log2_max_frame_num;
285 int64_t field_poc[2];
286
287 60694 pc->frame_num_offset = pc->prev_frame_num_offset;
288
2/2
✓ Branch 0 taken 488 times.
✓ Branch 1 taken 60206 times.
60694 if (pc->frame_num < pc->prev_frame_num)
289 488 pc->frame_num_offset += max_frame_num;
290
291
2/2
✓ Branch 0 taken 45760 times.
✓ Branch 1 taken 14934 times.
60694 if (sps->poc_type == 0) {
292 45760 const int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
293
2/2
✓ Branch 0 taken 805 times.
✓ Branch 1 taken 44955 times.
45760 if (pc->prev_poc_lsb < 0)
294 805 pc->prev_poc_lsb = pc->poc_lsb;
295
296
2/2
✓ Branch 0 taken 21120 times.
✓ Branch 1 taken 24640 times.
45760 if (pc->poc_lsb < pc->prev_poc_lsb &&
297
2/2
✓ Branch 0 taken 474 times.
✓ Branch 1 taken 20646 times.
21120 pc->prev_poc_lsb - pc->poc_lsb >= max_poc_lsb / 2)
298 474 pc->poc_msb = pc->prev_poc_msb + max_poc_lsb;
299
2/2
✓ Branch 0 taken 21976 times.
✓ Branch 1 taken 23310 times.
45286 else if (pc->poc_lsb > pc->prev_poc_lsb &&
300
2/2
✓ Branch 0 taken 338 times.
✓ Branch 1 taken 21638 times.
21976 pc->prev_poc_lsb - pc->poc_lsb < -max_poc_lsb / 2)
301 338 pc->poc_msb = pc->prev_poc_msb - max_poc_lsb;
302 else
303 44948 pc->poc_msb = pc->prev_poc_msb;
304 45760 field_poc[0] =
305 45760 field_poc[1] = pc->poc_msb + pc->poc_lsb;
306
2/2
✓ Branch 0 taken 33672 times.
✓ Branch 1 taken 12088 times.
45760 if (picture_structure == PICT_FRAME)
307 33672 field_poc[1] += pc->delta_poc_bottom;
308
2/2
✓ Branch 0 taken 6173 times.
✓ Branch 1 taken 8761 times.
14934 } else if (sps->poc_type == 1) {
309 int abs_frame_num;
310 int64_t expected_delta_per_poc_cycle, expectedpoc;
311 int i;
312
313
1/2
✓ Branch 0 taken 6173 times.
✗ Branch 1 not taken.
6173 if (sps->poc_cycle_length != 0)
314 6173 abs_frame_num = pc->frame_num_offset + pc->frame_num;
315 else
316 abs_frame_num = 0;
317
318
3/4
✓ Branch 0 taken 397 times.
✓ Branch 1 taken 5776 times.
✓ Branch 2 taken 397 times.
✗ Branch 3 not taken.
6173 if (nal_ref_idc == 0 && abs_frame_num > 0)
319 397 abs_frame_num--;
320
321 6173 expected_delta_per_poc_cycle = 0;
322
2/2
✓ Branch 0 taken 10007 times.
✓ Branch 1 taken 6173 times.
16180 for (i = 0; i < sps->poc_cycle_length; i++)
323 // FIXME integrate during sps parse
324 10007 expected_delta_per_poc_cycle += sps->offset_for_ref_frame[i];
325
326
2/2
✓ Branch 0 taken 6091 times.
✓ Branch 1 taken 82 times.
6173 if (abs_frame_num > 0) {
327 6091 int poc_cycle_cnt = (abs_frame_num - 1) / sps->poc_cycle_length;
328 6091 int frame_num_in_poc_cycle = (abs_frame_num - 1) % sps->poc_cycle_length;
329
330 6091 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
331
2/2
✓ Branch 0 taken 7913 times.
✓ Branch 1 taken 6091 times.
14004 for (i = 0; i <= frame_num_in_poc_cycle; i++)
332 7913 expectedpoc = expectedpoc + sps->offset_for_ref_frame[i];
333 } else
334 82 expectedpoc = 0;
335
336
2/2
✓ Branch 0 taken 397 times.
✓ Branch 1 taken 5776 times.
6173 if (nal_ref_idc == 0)
337 397 expectedpoc = expectedpoc + sps->offset_for_non_ref_pic;
338
339 6173 field_poc[0] = expectedpoc + pc->delta_poc[0];
340 6173 field_poc[1] = field_poc[0] + sps->offset_for_top_to_bottom_field;
341
342
2/2
✓ Branch 0 taken 5242 times.
✓ Branch 1 taken 931 times.
6173 if (picture_structure == PICT_FRAME)
343 5242 field_poc[1] += pc->delta_poc[1];
344 } else {
345 8761 int poc = 2 * (pc->frame_num_offset + pc->frame_num);
346
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8761 times.
8761 if (!nal_ref_idc)
348 poc--;
349
350 8761 field_poc[0] = poc;
351 8761 field_poc[1] = poc;
352 }
353
354
1/2
✓ Branch 0 taken 60694 times.
✗ Branch 1 not taken.
60694 if ( field_poc[0] != (int)field_poc[0]
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60694 times.
60694 || field_poc[1] != (int)field_poc[1])
356 return AVERROR_INVALIDDATA;
357
358
2/2
✓ Branch 0 taken 54284 times.
✓ Branch 1 taken 6410 times.
60694 if (picture_structure != PICT_BOTTOM_FIELD)
359 54284 pic_field_poc[0] = field_poc[0];
360
2/2
✓ Branch 0 taken 54085 times.
✓ Branch 1 taken 6609 times.
60694 if (picture_structure != PICT_TOP_FIELD)
361 54085 pic_field_poc[1] = field_poc[1];
362 60694 *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
363
364 60694 return 0;
365 }
366
367 921 static int decode_extradata_ps(const uint8_t *data, int size, H264ParamSets *ps,
368 int is_avc, void *logctx)
369 {
370 921 H2645Packet pkt = { 0 };
371 921 int i, ret = 0;
372
373 921 ret = ff_h2645_packet_split(&pkt, data, size, logctx, is_avc, 2, AV_CODEC_ID_H264, 1, 0);
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 921 times.
921 if (ret < 0) {
375 ret = 0;
376 goto fail;
377 }
378
379
2/2
✓ Branch 0 taken 1143 times.
✓ Branch 1 taken 921 times.
2064 for (i = 0; i < pkt.nb_nals; i++) {
380 1143 H2645NAL *nal = &pkt.nals[i];
381
2/3
✓ Branch 0 taken 571 times.
✓ Branch 1 taken 572 times.
✗ Branch 2 not taken.
1143 switch (nal->type) {
382 571 case H264_NAL_SPS: {
383 571 GetBitContext tmp_gb = nal->gb;
384 571 ret = ff_h264_decode_seq_parameter_set(&tmp_gb, logctx, ps, 0);
385
2/2
✓ Branch 0 taken 568 times.
✓ Branch 1 taken 3 times.
571 if (ret >= 0)
386 571 break;
387 3 av_log(logctx, AV_LOG_DEBUG,
388 "SPS decoding failure, trying again with the complete NAL\n");
389 3 init_get_bits8(&tmp_gb, nal->raw_data + 1, nal->raw_size - 1);
390 3 ret = ff_h264_decode_seq_parameter_set(&tmp_gb, logctx, ps, 0);
391
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (ret >= 0)
392 3 break;
393 ret = ff_h264_decode_seq_parameter_set(&nal->gb, logctx, ps, 1);
394 if (ret < 0)
395 goto fail;
396 break;
397 }
398 572 case H264_NAL_PPS:
399 572 ret = ff_h264_decode_picture_parameter_set(&nal->gb, logctx, ps,
400 nal->size_bits);
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 572 times.
572 if (ret < 0)
402 goto fail;
403 572 break;
404 default:
405 av_log(logctx, AV_LOG_VERBOSE, "Ignoring NAL type %d in extradata\n",
406 nal->type);
407 break;
408 }
409 }
410
411 921 fail:
412 921 ff_h2645_packet_uninit(&pkt);
413 921 return ret;
414 }
415
416 /* There are (invalid) samples in the wild with mp4-style extradata, where the
417 * parameter sets are stored unescaped (i.e. as RBSP).
418 * This function catches the parameter set decoding failure and tries again
419 * after escaping it */
420 700 static int decode_extradata_ps_mp4(const uint8_t *buf, int buf_size, H264ParamSets *ps,
421 int err_recognition, void *logctx)
422 {
423 int ret;
424
425 700 ret = decode_extradata_ps(buf, buf_size, ps, 1, logctx);
426
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
700 if (ret < 0 && !(err_recognition & AV_EF_EXPLODE)) {
427 GetByteContext gbc;
428 PutByteContext pbc;
429 uint8_t *escaped_buf;
430 int escaped_buf_size;
431
432 av_log(logctx, AV_LOG_WARNING,
433 "SPS decoding failure, trying again after escaping the NAL\n");
434
435 if (buf_size / 2 >= (INT16_MAX - AV_INPUT_BUFFER_PADDING_SIZE) / 3)
436 return AVERROR(ERANGE);
437 escaped_buf_size = buf_size * 3 / 2 + AV_INPUT_BUFFER_PADDING_SIZE;
438 escaped_buf = av_mallocz(escaped_buf_size);
439 if (!escaped_buf)
440 return AVERROR(ENOMEM);
441
442 bytestream2_init(&gbc, buf, buf_size);
443 bytestream2_init_writer(&pbc, escaped_buf, escaped_buf_size);
444
445 while (bytestream2_get_bytes_left(&gbc)) {
446 if (bytestream2_get_bytes_left(&gbc) >= 3 &&
447 bytestream2_peek_be24(&gbc) <= 3) {
448 bytestream2_put_be24(&pbc, 3);
449 bytestream2_skip(&gbc, 2);
450 } else
451 bytestream2_put_byte(&pbc, bytestream2_get_byte(&gbc));
452 }
453
454 escaped_buf_size = bytestream2_tell_p(&pbc);
455 AV_WB16(escaped_buf, escaped_buf_size - 2);
456
457 (void)decode_extradata_ps(escaped_buf, escaped_buf_size, ps, 1, logctx);
458 // lorex.mp4 decodes ok even with extradata decoding failing
459 av_freep(&escaped_buf);
460 }
461
462 700 return 0;
463 }
464
465 576 int ff_h264_decode_extradata(const uint8_t *data, int size, H264ParamSets *ps,
466 int *is_avc, int *nal_length_size,
467 int err_recognition, void *logctx)
468 {
469 int ret;
470
471
2/4
✓ Branch 0 taken 576 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 576 times.
576 if (!data || size <= 0)
472 return AVERROR(EINVAL);
473
474
2/2
✓ Branch 0 taken 355 times.
✓ Branch 1 taken 221 times.
576 if (data[0] == 1) {
475 int i, cnt, nalsize;
476 355 const uint8_t *p = data;
477
478 355 *is_avc = 1;
479
480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 355 times.
355 if (size < 7) {
481 av_log(logctx, AV_LOG_ERROR, "avcC %d too short\n", size);
482 return AVERROR_INVALIDDATA;
483 }
484
485 // Decode sps from avcC
486 355 cnt = *(p + 5) & 0x1f; // Number of sps
487 355 p += 6;
488
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 355 times.
705 for (i = 0; i < cnt; i++) {
489 350 nalsize = AV_RB16(p) + 2;
490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 350 times.
350 if (nalsize > size - (p - data))
491 return AVERROR_INVALIDDATA;
492 350 ret = decode_extradata_ps_mp4(p, nalsize, ps, err_recognition, logctx);
493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 350 times.
350 if (ret < 0) {
494 av_log(logctx, AV_LOG_ERROR,
495 "Decoding sps %d from avcC failed\n", i);
496 return ret;
497 }
498 350 p += nalsize;
499 }
500 // Decode pps from avcC
501 355 cnt = *(p++); // Number of pps
502
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 355 times.
705 for (i = 0; i < cnt; i++) {
503 350 nalsize = AV_RB16(p) + 2;
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 350 times.
350 if (nalsize > size - (p - data))
505 return AVERROR_INVALIDDATA;
506 350 ret = decode_extradata_ps_mp4(p, nalsize, ps, err_recognition, logctx);
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 350 times.
350 if (ret < 0) {
508 av_log(logctx, AV_LOG_ERROR,
509 "Decoding pps %d from avcC failed\n", i);
510 return ret;
511 }
512 350 p += nalsize;
513 }
514 // Store right nal length size that will be used to parse all other nals
515 355 *nal_length_size = (data[4] & 0x03) + 1;
516 } else {
517 221 *is_avc = 0;
518 221 ret = decode_extradata_ps(data, size, ps, 0, logctx);
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
221 if (ret < 0)
520 return ret;
521 }
522 576 return size;
523 }
524
525 /**
526 * Compute profile from profile_idc and constraint_set?_flags.
527 *
528 * @param sps SPS
529 *
530 * @return profile as defined by AV_PROFILE_H264_*
531 */
532 60694 int ff_h264_get_profile(const SPS *sps)
533 {
534 60694 int profile = sps->profile_idc;
535
536
3/3
✓ Branch 0 taken 14387 times.
✓ Branch 1 taken 2360 times.
✓ Branch 2 taken 43947 times.
60694 switch (sps->profile_idc) {
537 14387 case AV_PROFILE_H264_BASELINE:
538 // constraint_set1_flag set to 1
539 14387 profile |= (sps->constraint_set_flags & 1 << 1) ? AV_PROFILE_H264_CONSTRAINED : 0;
540 14387 break;
541 2360 case AV_PROFILE_H264_HIGH_10:
542 case AV_PROFILE_H264_HIGH_422:
543 case AV_PROFILE_H264_HIGH_444_PREDICTIVE:
544 // constraint_set3_flag set to 1
545 2360 profile |= (sps->constraint_set_flags & 1 << 3) ? AV_PROFILE_H264_INTRA : 0;
546 2360 break;
547 }
548
549 60694 return profile;
550 }
551