FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/av1_parse.h
Date: 2023-06-04 16:45:34
Exec Total Coverage
Lines: 45 49 91.8%
Functions: 3 3 100.0%
Branches: 28 34 82.4%

Line Branch Exec Source
1 /*
2 * AV1 common parsing code
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #ifndef AVCODEC_AV1_PARSE_H
22 #define AVCODEC_AV1_PARSE_H
23
24 #include <limits.h>
25 #include <stdint.h>
26
27 #include "libavutil/error.h"
28 #include "libavutil/intmath.h"
29 #include "libavutil/macros.h"
30
31 #include "av1.h"
32 #include "get_bits.h"
33
34 // OBU header fields + max leb128 length
35 #define MAX_OBU_HEADER_SIZE (2 + 8)
36
37 typedef struct AV1OBU {
38 /** Size of payload */
39 int size;
40 const uint8_t *data;
41
42 /**
43 * Size, in bits, of just the data, excluding the trailing_one_bit and
44 * any trailing padding.
45 */
46 int size_bits;
47
48 /** Size of entire OBU, including header */
49 int raw_size;
50 const uint8_t *raw_data;
51
52 int type;
53
54 int temporal_id;
55 int spatial_id;
56 } AV1OBU;
57
58 /** An input packet split into OBUs */
59 typedef struct AV1Packet {
60 AV1OBU *obus;
61 int nb_obus;
62 int obus_allocated;
63 unsigned obus_allocated_size;
64 } AV1Packet;
65
66 /**
67 * Extract an OBU from a raw bitstream.
68 *
69 * @note This function does not copy or store any bitstream data. All
70 * the pointers in the AV1OBU structure will be valid as long
71 * as the input buffer also is.
72 */
73 int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length,
74 void *logctx);
75
76 /**
77 * Split an input packet into OBUs.
78 *
79 * @note This function does not copy or store any bitstream data. All
80 * the pointers in the AV1Packet structure will be valid as
81 * long as the input buffer also is.
82 */
83 int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length,
84 void *logctx);
85
86 /**
87 * Free all the allocated memory in the packet.
88 */
89 void ff_av1_packet_uninit(AV1Packet *pkt);
90
91 3425 static inline int64_t leb128(GetBitContext *gb) {
92 3425 int64_t ret = 0;
93 int i;
94
95
2/2
✓ Branch 0 taken 4118 times.
✓ Branch 1 taken 6 times.
4124 for (i = 0; i < 8; i++) {
96 4118 int byte = get_bits(gb, 8);
97 4118 ret |= (int64_t)(byte & 0x7f) << (i * 7);
98
2/2
✓ Branch 0 taken 3419 times.
✓ Branch 1 taken 699 times.
4118 if (!(byte & 0x80))
99 3419 break;
100 }
101 3425 return ret;
102 }
103
104 1495 static inline int parse_obu_header(const uint8_t *buf, int buf_size,
105 int64_t *obu_size, int *start_pos, int *type,
106 int *temporal_id, int *spatial_id)
107 {
108 GetBitContext gb;
109 int ret, extension_flag, has_size_flag;
110 int64_t size;
111
112 1495 ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1495 times.
1495 if (ret < 0)
114 return ret;
115
116
2/2
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 1461 times.
1495 if (get_bits1(&gb) != 0) // obu_forbidden_bit
117 34 return AVERROR_INVALIDDATA;
118
119 1461 *type = get_bits(&gb, 4);
120 1461 extension_flag = get_bits1(&gb);
121 1461 has_size_flag = get_bits1(&gb);
122 1461 skip_bits1(&gb); // obu_reserved_1bit
123
124
2/2
✓ Branch 0 taken 1044 times.
✓ Branch 1 taken 417 times.
1461 if (extension_flag) {
125 1044 *temporal_id = get_bits(&gb, 3);
126 1044 *spatial_id = get_bits(&gb, 2);
127 1044 skip_bits(&gb, 3); // extension_header_reserved_3bits
128 } else {
129 417 *temporal_id = *spatial_id = 0;
130 }
131
132 1316 *obu_size = has_size_flag ? leb128(&gb)
133
2/2
✓ Branch 0 taken 1316 times.
✓ Branch 1 taken 145 times.
1461 : buf_size - 1 - extension_flag;
134
135
2/2
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 1427 times.
1461 if (get_bits_left(&gb) < 0)
136 34 return AVERROR_INVALIDDATA;
137
138 1427 *start_pos = get_bits_count(&gb) / 8;
139
140 1427 size = *obu_size + *start_pos;
141
142
2/2
✓ Branch 0 taken 650 times.
✓ Branch 1 taken 777 times.
1427 if (size > buf_size)
143 650 return AVERROR_INVALIDDATA;
144
145 777 return size;
146 }
147
148 137 static inline int get_obu_bit_length(const uint8_t *buf, int size, int type)
149 {
150 int v;
151
152 /* There are no trailing bits on these */
153
3/4
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 124 times.
✗ Branch 3 not taken.
137 if (type == AV1_OBU_TILE_GROUP ||
154
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 82 times.
124 type == AV1_OBU_TILE_LIST ||
155 type == AV1_OBU_FRAME) {
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55 times.
55 if (size > INT_MAX / 8)
157 return AVERROR(ERANGE);
158 else
159 55 return size * 8;
160 }
161
162
3/4
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
82 while (size > 0 && buf[size - 1] == 0)
163 size--;
164
165
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 39 times.
82 if (!size)
166 43 return 0;
167
168 39 v = buf[size - 1];
169
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (size > INT_MAX / 8)
171 return AVERROR(ERANGE);
172 39 size *= 8;
173
174 /* Remove the trailing_one_bit and following trailing zeros */
175
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (v)
176 39 size -= ff_ctz(v) + 1;
177
178 39 return size;
179 }
180
181 AVRational ff_av1_framerate(int64_t ticks_per_frame, int64_t units_per_tick,
182 int64_t time_scale);
183
184 #endif /* AVCODEC_AV1_PARSE_H */
185