FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/hevc/parse.c
Date: 2024-07-16 12:46:59
Exec Total Coverage
Lines: 54 67 80.6%
Functions: 2 2 100.0%
Branches: 28 41 68.3%

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 "bytestream.h"
20 #include "h2645_parse.h"
21 #include "hevc.h"
22 #include "parse.h"
23
24 581 static int hevc_decode_nal_units(const uint8_t *buf, int buf_size, HEVCParamSets *ps,
25 HEVCSEI *sei, int is_nalff, int nal_length_size,
26 int err_recognition, int apply_defdispwin, void *logctx)
27 {
28 int i;
29 581 int ret = 0;
30 581 H2645Packet pkt = { 0 };
31
32 581 ret = ff_h2645_packet_split(&pkt, buf, buf_size, logctx, is_nalff,
33 nal_length_size, AV_CODEC_ID_HEVC, 1, 0);
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 581 times.
581 if (ret < 0) {
35 goto done;
36 }
37
38
2/2
✓ Branch 0 taken 1476 times.
✓ Branch 1 taken 580 times.
2056 for (i = 0; i < pkt.nb_nals; i++) {
39 1476 H2645NAL *nal = &pkt.nals[i];
40
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1472 times.
1476 if (nal->nuh_layer_id > 0)
41 4 continue;
42
43 /* ignore everything except parameter sets and VCL NALUs */
44
4/5
✓ Branch 0 taken 439 times.
✓ Branch 1 taken 436 times.
✓ Branch 2 taken 575 times.
✓ Branch 3 taken 22 times.
✗ Branch 4 not taken.
1472 switch (nal->type) {
45 439 case HEVC_NAL_VPS:
46 439 ret = ff_hevc_decode_nal_vps(&nal->gb, logctx, ps);
47
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 439 times.
439 if (ret < 0)
48 goto done;
49 439 break;
50 436 case HEVC_NAL_SPS:
51 436 ret = ff_hevc_decode_nal_sps(&nal->gb, logctx, ps, apply_defdispwin);
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 436 times.
436 if (ret < 0)
53 goto done;
54 436 break;
55 575 case HEVC_NAL_PPS:
56 575 ret = ff_hevc_decode_nal_pps(&nal->gb, logctx, ps);
57
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 574 times.
575 if (ret < 0)
58 1 goto done;
59 574 break;
60 22 case HEVC_NAL_SEI_PREFIX:
61 case HEVC_NAL_SEI_SUFFIX:
62 22 ret = ff_hevc_decode_nal_sei(&nal->gb, logctx, sei, ps, nal->type);
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
64 goto done;
65 22 break;
66 default:
67 av_log(logctx, AV_LOG_VERBOSE, "Ignoring NAL type %d in extradata\n", nal->type);
68 break;
69 }
70 }
71
72 580 done:
73 581 ff_h2645_packet_uninit(&pkt);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 581 times.
581 if (err_recognition & AV_EF_EXPLODE)
75 return ret;
76
77 581 return 0;
78 }
79
80 422 int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps,
81 HEVCSEI *sei, int *is_nalff, int *nal_length_size,
82 int err_recognition, int apply_defdispwin, void *logctx)
83 {
84 422 int ret = 0;
85 GetByteContext gb;
86
87 422 bytestream2_init(&gb, data, size);
88
89 /* data[0] == 1 is configurationVersion from 14496-15.
90 * data[0] == 0 is for backward compatibility predates the standard.
91 *
92 * Minimum number of bytes of hvcC with 0 numOfArrays is 23.
93 */
94
6/10
✓ Branch 0 taken 422 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 360 times.
✓ Branch 3 taken 62 times.
✓ Branch 4 taken 360 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 360 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 360 times.
422 if (size >= 23 && ((data[0] == 1) || (data[0] == 0 && (data[1] || data[2] > 1)))) {
95 /* It seems the extradata is encoded as hvcC format. */
96 int i, j, num_arrays, nal_len_size;
97
98 62 *is_nalff = 1;
99
100 62 bytestream2_skip(&gb, 21);
101 62 nal_len_size = (bytestream2_get_byte(&gb) & 3) + 1;
102 62 num_arrays = bytestream2_get_byte(&gb);
103
104 /* nal units in the hvcC always have length coded with 2 bytes,
105 * so put a fake nal_length_size = 2 while parsing them */
106 62 *nal_length_size = 2;
107
108 /* Decode nal units from hvcC. */
109
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 62 times.
274 for (i = 0; i < num_arrays; i++) {
110 212 int type = bytestream2_get_byte(&gb) & 0x3f;
111 212 int cnt = bytestream2_get_be16(&gb);
112
113
2/2
✓ Branch 0 taken 221 times.
✓ Branch 1 taken 212 times.
433 for (j = 0; j < cnt; j++) {
114 // +2 for the nal size field
115 221 int nalsize = bytestream2_peek_be16(&gb) + 2;
116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 221 times.
221 if (bytestream2_get_bytes_left(&gb) < nalsize) {
117 av_log(logctx, AV_LOG_ERROR,
118 "Invalid NAL unit size in extradata.\n");
119 return AVERROR_INVALIDDATA;
120 }
121
122 221 ret = hevc_decode_nal_units(gb.buffer, nalsize, ps, sei, *is_nalff,
123 *nal_length_size, err_recognition, apply_defdispwin,
124 logctx);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
221 if (ret < 0) {
126 av_log(logctx, AV_LOG_ERROR,
127 "Decoding nal unit %d %d from hvcC failed\n",
128 type, i);
129 return ret;
130 }
131 221 bytestream2_skip(&gb, nalsize);
132 }
133 }
134
135 /* Now store right nal length size, that will be used to parse
136 * all other nals */
137 62 *nal_length_size = nal_len_size;
138 } else {
139 360 *is_nalff = 0;
140 360 ret = hevc_decode_nal_units(data, size, ps, sei, *is_nalff, *nal_length_size,
141 err_recognition, apply_defdispwin, logctx);
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 360 times.
360 if (ret < 0)
143 return ret;
144 }
145
146 422 return ret;
147 }
148