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 "avio.h" | ||
27 | #include "avc.h" | ||
28 | #include "avio_internal.h" | ||
29 | |||
30 | 1728 | static const uint8_t *avc_find_startcode_internal(const uint8_t *p, const uint8_t *end) | |
31 | { | ||
32 | 1728 | const uint8_t *a = p + 4 - ((intptr_t)p & 3); | |
33 | |||
34 |
3/4✓ Branch 0 taken 4965 times.
✓ Branch 1 taken 1109 times.
✓ Branch 2 taken 4965 times.
✗ Branch 3 not taken.
|
6074 | for (end -= 3; p < a && p < end; p++) { |
35 |
6/6✓ Branch 0 taken 1421 times.
✓ Branch 1 taken 3544 times.
✓ Branch 2 taken 1235 times.
✓ Branch 3 taken 186 times.
✓ Branch 4 taken 619 times.
✓ Branch 5 taken 616 times.
|
4965 | if (p[0] == 0 && p[1] == 0 && p[2] == 1) |
36 | 619 | return p; | |
37 | } | ||
38 | |||
39 |
2/2✓ Branch 0 taken 458836 times.
✓ Branch 1 taken 579 times.
|
459415 | for (end -= 3; p < end; p += 4) { |
40 | 458836 | uint32_t x = *(const uint32_t*)p; | |
41 | // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian | ||
42 | // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian | ||
43 |
2/2✓ Branch 0 taken 5123 times.
✓ Branch 1 taken 453713 times.
|
458836 | if ((x - 0x01010101) & (~x) & 0x80808080) { // generic |
44 |
2/2✓ Branch 0 taken 1515 times.
✓ Branch 1 taken 3608 times.
|
5123 | if (p[1] == 0) { |
45 |
4/4✓ Branch 0 taken 241 times.
✓ Branch 1 taken 1274 times.
✓ Branch 2 taken 129 times.
✓ Branch 3 taken 112 times.
|
1515 | if (p[0] == 0 && p[2] == 1) |
46 | 129 | return p; | |
47 |
4/4✓ Branch 0 taken 292 times.
✓ Branch 1 taken 1094 times.
✓ Branch 2 taken 197 times.
✓ Branch 3 taken 95 times.
|
1386 | if (p[2] == 0 && p[3] == 1) |
48 | 197 | return p+1; | |
49 | } | ||
50 |
2/2✓ Branch 0 taken 1529 times.
✓ Branch 1 taken 3268 times.
|
4797 | if (p[3] == 0) { |
51 |
4/4✓ Branch 0 taken 204 times.
✓ Branch 1 taken 1325 times.
✓ Branch 2 taken 98 times.
✓ Branch 3 taken 106 times.
|
1529 | if (p[2] == 0 && p[4] == 1) |
52 | 98 | return p+2; | |
53 |
4/4✓ Branch 0 taken 244 times.
✓ Branch 1 taken 1187 times.
✓ Branch 2 taken 106 times.
✓ Branch 3 taken 138 times.
|
1431 | if (p[4] == 0 && p[5] == 1) |
54 | 106 | return p+3; | |
55 | } | ||
56 | } | ||
57 | } | ||
58 | |||
59 |
2/2✓ Branch 0 taken 871 times.
✓ Branch 1 taken 579 times.
|
1450 | for (end += 3; p < end; p++) { |
60 |
3/6✓ Branch 0 taken 5 times.
✓ Branch 1 taken 866 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
871 | if (p[0] == 0 && p[1] == 0 && p[2] == 1) |
61 | ✗ | return p; | |
62 | } | ||
63 | |||
64 | 579 | return end + 3; | |
65 | } | ||
66 | |||
67 | 1728 | const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){ | |
68 | 1728 | const uint8_t *out = avc_find_startcode_internal(p, end); | |
69 |
6/6✓ Branch 0 taken 1725 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1146 times.
✓ Branch 3 taken 579 times.
✓ Branch 4 taken 733 times.
✓ Branch 5 taken 413 times.
|
1728 | if(p<out && out<end && !out[-1]) out--; |
70 | 1728 | return out; | |
71 | } | ||
72 | |||
73 | 579 | static int avc_parse_nal_units(AVIOContext *pb, NALUList *list, | |
74 | const uint8_t *buf_in, int size) | ||
75 | { | ||
76 | 579 | const uint8_t *p = buf_in; | |
77 | 579 | const uint8_t *end = p + size; | |
78 | const uint8_t *nal_start, *nal_end; | ||
79 | |||
80 | 579 | size = 0; | |
81 | 579 | nal_start = ff_avc_find_startcode(p, end); | |
82 | 1149 | for (;;) { | |
83 | 1728 | const size_t nalu_limit = SIZE_MAX / sizeof(*list->nalus); | |
84 |
4/4✓ Branch 0 taken 4180 times.
✓ Branch 1 taken 579 times.
✓ Branch 2 taken 3031 times.
✓ Branch 3 taken 1149 times.
|
4759 | while (nal_start < end && !*(nal_start++)); |
85 |
2/2✓ Branch 0 taken 579 times.
✓ Branch 1 taken 1149 times.
|
1728 | if (nal_start == end) |
86 | 579 | break; | |
87 | |||
88 | 1149 | nal_end = ff_avc_find_startcode(nal_start, end); | |
89 |
2/2✓ Branch 0 taken 869 times.
✓ Branch 1 taken 280 times.
|
1149 | if (pb) { |
90 | 869 | avio_wb32(pb, nal_end - nal_start); | |
91 | 869 | avio_write(pb, nal_start, nal_end - nal_start); | |
92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
|
280 | } else if (list->nb_nalus >= nalu_limit) { |
93 | ✗ | return AVERROR(ERANGE); | |
94 | } else { | ||
95 | 280 | NALU *tmp = av_fast_realloc(list->nalus, &list->nalus_array_size, | |
96 | 280 | (list->nb_nalus + 1) * sizeof(*list->nalus)); | |
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
|
280 | if (!tmp) |
98 | ✗ | return AVERROR(ENOMEM); | |
99 | 280 | list->nalus = tmp; | |
100 | 280 | tmp[list->nb_nalus++] = (NALU){ .offset = nal_start - p, | |
101 | 280 | .size = nal_end - nal_start }; | |
102 | } | ||
103 | 1149 | size += 4 + nal_end - nal_start; | |
104 | 1149 | nal_start = nal_end; | |
105 | } | ||
106 | 579 | return size; | |
107 | } | ||
108 | |||
109 | 500 | int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size) | |
110 | { | ||
111 | 500 | return avc_parse_nal_units(pb, NULL, buf_in, size); | |
112 | } | ||
113 | |||
114 | 79 | int ff_nal_units_create_list(NALUList *list, const uint8_t *buf, int size) | |
115 | { | ||
116 | 79 | list->nb_nalus = 0; | |
117 | 79 | return avc_parse_nal_units(NULL, list, buf, size); | |
118 | } | ||
119 | |||
120 | 79 | void ff_nal_units_write_list(const NALUList *list, AVIOContext *pb, | |
121 | const uint8_t *buf) | ||
122 | { | ||
123 |
2/2✓ Branch 0 taken 280 times.
✓ Branch 1 taken 79 times.
|
359 | for (unsigned i = 0; i < list->nb_nalus; i++) { |
124 | 280 | avio_wb32(pb, list->nalus[i].size); | |
125 | 280 | avio_write(pb, buf + list->nalus[i].offset, list->nalus[i].size); | |
126 | } | ||
127 | 79 | } | |
128 | |||
129 | 4 | int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size) | |
130 | { | ||
131 | AVIOContext *pb; | ||
132 | 4 | int ret = avio_open_dyn_buf(&pb); | |
133 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if(ret < 0) |
134 | ✗ | return ret; | |
135 | |||
136 | 4 | ff_avc_parse_nal_units(pb, buf_in, *size); | |
137 | |||
138 | 4 | *size = avio_close_dyn_buf(pb, buf); | |
139 | 4 | return 0; | |
140 | } | ||
141 | |||
142 | 57 | int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len) | |
143 | { | ||
144 | 57 | AVIOContext *sps_pb = NULL, *pps_pb = NULL, *sps_ext_pb = NULL; | |
145 | uint8_t *buf, *end, *start; | ||
146 | uint8_t *sps, *pps, *sps_ext; | ||
147 | 57 | uint32_t sps_size = 0, pps_size = 0, sps_ext_size = 0; | |
148 | 57 | int ret, nb_sps = 0, nb_pps = 0, nb_sps_ext = 0; | |
149 | |||
150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | if (len <= 6) |
151 | ✗ | return AVERROR_INVALIDDATA; | |
152 | |||
153 | /* check for H.264 start code */ | ||
154 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 1 times.
|
57 | if (AV_RB32(data) != 0x00000001 && |
155 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 2 times.
|
56 | AV_RB24(data) != 0x000001) { |
156 | 54 | avio_write(pb, data, len); | |
157 | 54 | return 0; | |
158 | } | ||
159 | |||
160 | 3 | ret = ff_avc_parse_nal_units_buf(data, &buf, &len); | |
161 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
162 | ✗ | return ret; | |
163 | 3 | start = buf; | |
164 | 3 | end = buf + len; | |
165 | |||
166 | 3 | ret = avio_open_dyn_buf(&sps_pb); | |
167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
168 | ✗ | goto fail; | |
169 | 3 | ret = avio_open_dyn_buf(&pps_pb); | |
170 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
171 | ✗ | goto fail; | |
172 | 3 | ret = avio_open_dyn_buf(&sps_ext_pb); | |
173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
174 | ✗ | goto fail; | |
175 | |||
176 | /* look for sps and pps */ | ||
177 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | while (end - buf > 4) { |
178 | uint32_t size; | ||
179 | uint8_t nal_type; | ||
180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | size = FFMIN(AV_RB32(buf), end - buf - 4); |
181 | 6 | buf += 4; | |
182 | 6 | nal_type = buf[0] & 0x1f; | |
183 | |||
184 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (nal_type == 7) { /* SPS */ |
185 | 3 | nb_sps++; | |
186 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (size > UINT16_MAX || nb_sps >= H264_MAX_SPS_COUNT) { |
187 | ✗ | ret = AVERROR_INVALIDDATA; | |
188 | ✗ | goto fail; | |
189 | } | ||
190 | 3 | avio_wb16(sps_pb, size); | |
191 | 3 | avio_write(sps_pb, buf, size); | |
192 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | } else if (nal_type == 8) { /* PPS */ |
193 | 3 | nb_pps++; | |
194 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (size > UINT16_MAX || nb_pps >= H264_MAX_PPS_COUNT) { |
195 | ✗ | ret = AVERROR_INVALIDDATA; | |
196 | ✗ | goto fail; | |
197 | } | ||
198 | 3 | avio_wb16(pps_pb, size); | |
199 | 3 | avio_write(pps_pb, buf, size); | |
200 | ✗ | } else if (nal_type == 13) { /* SPS_EXT */ | |
201 | ✗ | nb_sps_ext++; | |
202 | ✗ | if (size > UINT16_MAX || nb_sps_ext >= 256) { | |
203 | ✗ | ret = AVERROR_INVALIDDATA; | |
204 | ✗ | goto fail; | |
205 | } | ||
206 | ✗ | avio_wb16(sps_ext_pb, size); | |
207 | ✗ | avio_write(sps_ext_pb, buf, size); | |
208 | } | ||
209 | |||
210 | 6 | buf += size; | |
211 | } | ||
212 | 3 | sps_size = avio_get_dyn_buf(sps_pb, &sps); | |
213 | 3 | pps_size = avio_get_dyn_buf(pps_pb, &pps); | |
214 | 3 | sps_ext_size = avio_get_dyn_buf(sps_ext_pb, &sps_ext); | |
215 | |||
216 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
|
3 | if (sps_size < 6 || !pps_size) { |
217 | ✗ | ret = AVERROR_INVALIDDATA; | |
218 | ✗ | goto fail; | |
219 | } | ||
220 | |||
221 | 3 | avio_w8(pb, 1); /* version */ | |
222 | 3 | avio_w8(pb, sps[3]); /* profile */ | |
223 | 3 | avio_w8(pb, sps[4]); /* profile compat */ | |
224 | 3 | avio_w8(pb, sps[5]); /* level */ | |
225 | 3 | avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */ | |
226 | 3 | avio_w8(pb, 0xe0 | nb_sps); /* 3 bits reserved (111) + 5 bits number of sps */ | |
227 | |||
228 | 3 | avio_write(pb, sps, sps_size); | |
229 | 3 | avio_w8(pb, nb_pps); /* number of pps */ | |
230 | 3 | avio_write(pb, pps, pps_size); | |
231 | |||
232 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
3 | if (sps[3] != 66 && sps[3] != 77 && sps[3] != 88) { |
233 | H264SPS seq; | ||
234 | 3 | ret = ff_avc_decode_sps(&seq, sps + 3, sps_size - 3); | |
235 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
236 | ✗ | goto fail; | |
237 | |||
238 | 3 | avio_w8(pb, 0xfc | seq.chroma_format_idc); /* 6 bits reserved (111111) + chroma_format_idc */ | |
239 | 3 | avio_w8(pb, 0xf8 | (seq.bit_depth_luma - 8)); /* 5 bits reserved (11111) + bit_depth_luma_minus8 */ | |
240 | 3 | avio_w8(pb, 0xf8 | (seq.bit_depth_chroma - 8)); /* 5 bits reserved (11111) + bit_depth_chroma_minus8 */ | |
241 | 3 | avio_w8(pb, nb_sps_ext); /* number of sps ext */ | |
242 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (nb_sps_ext) |
243 | ✗ | avio_write(pb, sps_ext, sps_ext_size); | |
244 | } | ||
245 | |||
246 | ✗ | fail: | |
247 | 3 | ffio_free_dyn_buf(&sps_pb); | |
248 | 3 | ffio_free_dyn_buf(&pps_pb); | |
249 | 3 | ffio_free_dyn_buf(&sps_ext_pb); | |
250 | 3 | av_free(start); | |
251 | |||
252 | 3 | return ret; | |
253 | } | ||
254 | |||
255 | ✗ | int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size) | |
256 | { | ||
257 | uint16_t sps_size, pps_size; | ||
258 | uint8_t *out; | ||
259 | int out_size; | ||
260 | |||
261 | ✗ | *buf = NULL; | |
262 | ✗ | if (*size >= 4 && (AV_RB32(in) == 0x00000001 || AV_RB24(in) == 0x000001)) | |
263 | ✗ | return 0; | |
264 | ✗ | if (*size < 11 || in[0] != 1) | |
265 | ✗ | return AVERROR_INVALIDDATA; | |
266 | |||
267 | ✗ | sps_size = AV_RB16(&in[6]); | |
268 | ✗ | if (11 + sps_size > *size) | |
269 | ✗ | return AVERROR_INVALIDDATA; | |
270 | ✗ | pps_size = AV_RB16(&in[9 + sps_size]); | |
271 | ✗ | if (11 + sps_size + pps_size > *size) | |
272 | ✗ | return AVERROR_INVALIDDATA; | |
273 | ✗ | out_size = 8 + sps_size + pps_size; | |
274 | ✗ | out = av_mallocz(out_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
275 | ✗ | if (!out) | |
276 | ✗ | return AVERROR(ENOMEM); | |
277 | ✗ | AV_WB32(&out[0], 0x00000001); | |
278 | ✗ | memcpy(out + 4, &in[8], sps_size); | |
279 | ✗ | AV_WB32(&out[4 + sps_size], 0x00000001); | |
280 | ✗ | memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size); | |
281 | ✗ | *buf = out; | |
282 | ✗ | *size = out_size; | |
283 | ✗ | return 0; | |
284 | } | ||
285 | |||
286 | ✗ | const uint8_t *ff_avc_mp4_find_startcode(const uint8_t *start, | |
287 | const uint8_t *end, | ||
288 | int nal_length_size) | ||
289 | { | ||
290 | ✗ | unsigned int res = 0; | |
291 | |||
292 | ✗ | if (end - start < nal_length_size) | |
293 | ✗ | return NULL; | |
294 | ✗ | while (nal_length_size--) | |
295 | ✗ | res = (res << 8) | *start++; | |
296 | |||
297 | ✗ | if (res > end - start) | |
298 | ✗ | return NULL; | |
299 | |||
300 | ✗ | return start + res; | |
301 | } | ||
302 | |||
303 | 6 | uint8_t *ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len, | |
304 | uint32_t *dst_len, int header_len) | ||
305 | { | ||
306 | uint8_t *dst; | ||
307 | uint32_t i, len; | ||
308 | |||
309 | 6 | dst = av_malloc(src_len + AV_INPUT_BUFFER_PADDING_SIZE); | |
310 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!dst) |
311 | ✗ | return NULL; | |
312 | |||
313 | /* NAL unit header */ | ||
314 | 6 | i = len = 0; | |
315 |
3/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
12 | while (i < header_len && i < src_len) |
316 | 6 | dst[len++] = src[i++]; | |
317 | |||
318 |
2/2✓ Branch 0 taken 116 times.
✓ Branch 1 taken 6 times.
|
122 | while (i + 2 < src_len) |
319 |
6/6✓ Branch 0 taken 25 times.
✓ Branch 1 taken 91 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 2 times.
|
116 | if (!src[i] && !src[i + 1] && src[i + 2] == 3) { |
320 | 12 | dst[len++] = src[i++]; | |
321 | 12 | dst[len++] = src[i++]; | |
322 | 12 | i++; // remove emulation_prevention_three_byte | |
323 | } else | ||
324 | 104 | dst[len++] = src[i++]; | |
325 | |||
326 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | while (i < src_len) |
327 | 12 | dst[len++] = src[i++]; | |
328 | |||
329 | 6 | memset(dst + len, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
330 | |||
331 | 6 | *dst_len = len; | |
332 | 6 | return dst; | |
333 | } | ||
334 | |||
335 | static const AVRational avc_sample_aspect_ratio[17] = { | ||
336 | { 0, 1 }, | ||
337 | { 1, 1 }, | ||
338 | { 12, 11 }, | ||
339 | { 10, 11 }, | ||
340 | { 16, 11 }, | ||
341 | { 40, 33 }, | ||
342 | { 24, 11 }, | ||
343 | { 20, 11 }, | ||
344 | { 32, 11 }, | ||
345 | { 80, 33 }, | ||
346 | { 18, 11 }, | ||
347 | { 15, 11 }, | ||
348 | { 64, 33 }, | ||
349 | { 160, 99 }, | ||
350 | { 4, 3 }, | ||
351 | { 3, 2 }, | ||
352 | { 2, 1 }, | ||
353 | }; | ||
354 | |||
355 | 38 | static inline int get_ue_golomb(GetBitContext *gb) { | |
356 | int i; | ||
357 |
3/4✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 38 times.
|
87 | for (i = 0; i < 32 && !get_bits1(gb); i++) |
358 | ; | ||
359 | 38 | return get_bitsz(gb, i) + (1 << i) - 1; | |
360 | } | ||
361 | |||
362 | ✗ | static inline int get_se_golomb(GetBitContext *gb) { | |
363 | ✗ | int v = get_ue_golomb(gb) + 1; | |
364 | ✗ | int sign = -(v & 1); | |
365 | ✗ | return ((v >> 1) ^ sign) - sign; | |
366 | } | ||
367 | |||
368 | 3 | int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size) | |
369 | { | ||
370 | int i, j, ret, rbsp_size, aspect_ratio_idc, pic_order_cnt_type; | ||
371 | int num_ref_frames_in_pic_order_cnt_cycle; | ||
372 | 3 | int delta_scale, lastScale = 8, nextScale = 8; | |
373 | int sizeOfScalingList; | ||
374 | GetBitContext gb; | ||
375 | uint8_t *rbsp_buf; | ||
376 | |||
377 | 3 | rbsp_buf = ff_nal_unit_extract_rbsp(buf, buf_size, &rbsp_size, 0); | |
378 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!rbsp_buf) |
379 | ✗ | return AVERROR(ENOMEM); | |
380 | |||
381 | 3 | ret = init_get_bits8(&gb, rbsp_buf, rbsp_size); | |
382 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
383 | ✗ | goto end; | |
384 | |||
385 | 3 | memset(sps, 0, sizeof(*sps)); | |
386 | |||
387 | 3 | sps->profile_idc = get_bits(&gb, 8); | |
388 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 0; // constraint_set0_flag | |
389 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 1; // constraint_set1_flag | |
390 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 2; // constraint_set2_flag | |
391 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 3; // constraint_set3_flag | |
392 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 4; // constraint_set4_flag | |
393 | 3 | sps->constraint_set_flags |= get_bits1(&gb) << 5; // constraint_set5_flag | |
394 | 3 | skip_bits(&gb, 2); // reserved_zero_2bits | |
395 | 3 | sps->level_idc = get_bits(&gb, 8); | |
396 | 3 | sps->id = get_ue_golomb(&gb); | |
397 | |||
398 |
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 || |
399 | ✗ | sps->profile_idc == 122 || sps->profile_idc == 244 || sps->profile_idc == 44 || | |
400 | ✗ | sps->profile_idc == 83 || sps->profile_idc == 86 || sps->profile_idc == 118 || | |
401 | ✗ | sps->profile_idc == 128 || sps->profile_idc == 138 || sps->profile_idc == 139 || | |
402 | ✗ | sps->profile_idc == 134) { | |
403 | 3 | sps->chroma_format_idc = get_ue_golomb(&gb); // chroma_format_idc | |
404 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (sps->chroma_format_idc == 3) { |
405 | ✗ | skip_bits1(&gb); // separate_colour_plane_flag | |
406 | } | ||
407 | 3 | sps->bit_depth_luma = get_ue_golomb(&gb) + 8; | |
408 | 3 | sps->bit_depth_chroma = get_ue_golomb(&gb) + 8; | |
409 | 3 | skip_bits1(&gb); // qpprime_y_zero_transform_bypass_flag | |
410 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (get_bits1(&gb)) { // seq_scaling_matrix_present_flag |
411 | ✗ | for (i = 0; i < ((sps->chroma_format_idc != 3) ? 8 : 12); i++) { | |
412 | ✗ | if (!get_bits1(&gb)) // seq_scaling_list_present_flag | |
413 | ✗ | continue; | |
414 | ✗ | lastScale = 8; | |
415 | ✗ | nextScale = 8; | |
416 | ✗ | sizeOfScalingList = i < 6 ? 16 : 64; | |
417 | ✗ | for (j = 0; j < sizeOfScalingList; j++) { | |
418 | ✗ | if (nextScale != 0) { | |
419 | ✗ | delta_scale = get_se_golomb(&gb); | |
420 | ✗ | nextScale = (lastScale + delta_scale) & 0xff; | |
421 | } | ||
422 | ✗ | lastScale = nextScale == 0 ? lastScale : nextScale; | |
423 | } | ||
424 | } | ||
425 | } | ||
426 | } else { | ||
427 | ✗ | sps->chroma_format_idc = 1; | |
428 | ✗ | sps->bit_depth_luma = 8; | |
429 | ✗ | sps->bit_depth_chroma = 8; | |
430 | } | ||
431 | |||
432 | 3 | get_ue_golomb(&gb); // log2_max_frame_num_minus4 | |
433 | 3 | pic_order_cnt_type = get_ue_golomb(&gb); | |
434 | |||
435 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (pic_order_cnt_type == 0) { |
436 | 3 | get_ue_golomb(&gb); // log2_max_pic_order_cnt_lsb_minus4 | |
437 | ✗ | } else if (pic_order_cnt_type == 1) { | |
438 | ✗ | skip_bits1(&gb); // delta_pic_order_always_zero | |
439 | ✗ | get_se_golomb(&gb); // offset_for_non_ref_pic | |
440 | ✗ | get_se_golomb(&gb); // offset_for_top_to_bottom_field | |
441 | ✗ | num_ref_frames_in_pic_order_cnt_cycle = get_ue_golomb(&gb); | |
442 | ✗ | for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) | |
443 | ✗ | get_se_golomb(&gb); // offset_for_ref_frame | |
444 | } | ||
445 | |||
446 | 3 | get_ue_golomb(&gb); // max_num_ref_frames | |
447 | 3 | skip_bits1(&gb); // gaps_in_frame_num_value_allowed_flag | |
448 | 3 | get_ue_golomb(&gb); // pic_width_in_mbs_minus1 | |
449 | 3 | get_ue_golomb(&gb); // pic_height_in_map_units_minus1 | |
450 | |||
451 | 3 | sps->frame_mbs_only_flag = get_bits1(&gb); | |
452 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (!sps->frame_mbs_only_flag) |
453 | 2 | skip_bits1(&gb); // mb_adaptive_frame_field_flag | |
454 | |||
455 | 3 | skip_bits1(&gb); // direct_8x8_inference_flag | |
456 | |||
457 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if (get_bits1(&gb)) { // frame_cropping_flag |
458 | 2 | get_ue_golomb(&gb); // frame_crop_left_offset | |
459 | 2 | get_ue_golomb(&gb); // frame_crop_right_offset | |
460 | 2 | get_ue_golomb(&gb); // frame_crop_top_offset | |
461 | 2 | get_ue_golomb(&gb); // frame_crop_bottom_offset | |
462 | } | ||
463 | |||
464 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | if (get_bits1(&gb)) { // vui_parameters_present_flag |
465 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if (get_bits1(&gb)) { // aspect_ratio_info_present_flag |
466 | 2 | aspect_ratio_idc = get_bits(&gb, 8); | |
467 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (aspect_ratio_idc == 0xff) { |
468 | ✗ | sps->sar.num = get_bits(&gb, 16); | |
469 | ✗ | sps->sar.den = get_bits(&gb, 16); | |
470 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(avc_sample_aspect_ratio)) { |
471 | 2 | sps->sar = avc_sample_aspect_ratio[aspect_ratio_idc]; | |
472 | } | ||
473 | } | ||
474 | } | ||
475 | |||
476 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (!sps->sar.den) { |
477 | 1 | sps->sar.num = 1; | |
478 | 1 | sps->sar.den = 1; | |
479 | } | ||
480 | |||
481 | 3 | ret = 0; | |
482 | 3 | end: | |
483 | 3 | av_free(rbsp_buf); | |
484 | 3 | return ret; | |
485 | } | ||
486 |