/src/ffmpeg/libavcodec/h264_parse.c
Line | Count | 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 | | 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 | 1.70M | { |
35 | 1.70M | int list, i, j; |
36 | 1.70M | int luma_def, chroma_def; |
37 | | |
38 | 1.70M | pwt->use_weight = 0; |
39 | 1.70M | pwt->use_weight_chroma = 0; |
40 | | |
41 | 1.70M | pwt->luma_log2_weight_denom = get_ue_golomb_31(gb); |
42 | 1.70M | if (pwt->luma_log2_weight_denom > 7U) { |
43 | 587k | av_log(logctx, AV_LOG_ERROR, "luma_log2_weight_denom %d is out of range\n", pwt->luma_log2_weight_denom); |
44 | 587k | pwt->luma_log2_weight_denom = 0; |
45 | 587k | } |
46 | 1.70M | luma_def = 1 << pwt->luma_log2_weight_denom; |
47 | | |
48 | 1.70M | if (sps->chroma_format_idc) { |
49 | 1.61M | pwt->chroma_log2_weight_denom = get_ue_golomb_31(gb); |
50 | 1.61M | if (pwt->chroma_log2_weight_denom > 7U) { |
51 | 627k | av_log(logctx, AV_LOG_ERROR, "chroma_log2_weight_denom %d is out of range\n", pwt->chroma_log2_weight_denom); |
52 | 627k | pwt->chroma_log2_weight_denom = 0; |
53 | 627k | } |
54 | 1.61M | chroma_def = 1 << pwt->chroma_log2_weight_denom; |
55 | 1.61M | } |
56 | | |
57 | 2.03M | for (list = 0; list < 2; list++) { |
58 | 1.88M | pwt->luma_weight_flag[list] = 0; |
59 | 1.88M | pwt->chroma_weight_flag[list] = 0; |
60 | 8.23M | for (i = 0; i < ref_count[list]; i++) { |
61 | 6.61M | int luma_weight_flag, chroma_weight_flag; |
62 | | |
63 | 6.61M | luma_weight_flag = get_bits1(gb); |
64 | 6.61M | if (luma_weight_flag) { |
65 | 2.34M | pwt->luma_weight[i][list][0] = get_se_golomb(gb); |
66 | 2.34M | pwt->luma_weight[i][list][1] = get_se_golomb(gb); |
67 | 2.34M | if ((int8_t)pwt->luma_weight[i][list][0] != pwt->luma_weight[i][list][0] || |
68 | 2.28M | (int8_t)pwt->luma_weight[i][list][1] != pwt->luma_weight[i][list][1]) |
69 | 100k | goto out_range_weight; |
70 | 2.24M | if (pwt->luma_weight[i][list][0] != luma_def || |
71 | 2.15M | pwt->luma_weight[i][list][1] != 0) { |
72 | 2.15M | pwt->use_weight = 1; |
73 | 2.15M | pwt->luma_weight_flag[list] = 1; |
74 | 2.15M | } |
75 | 4.26M | } else { |
76 | 4.26M | pwt->luma_weight[i][list][0] = luma_def; |
77 | 4.26M | pwt->luma_weight[i][list][1] = 0; |
78 | 4.26M | } |
79 | | |
80 | 6.51M | if (sps->chroma_format_idc) { |
81 | 6.23M | chroma_weight_flag = get_bits1(gb); |
82 | 6.23M | if (chroma_weight_flag) { |
83 | 1.98M | int j; |
84 | 5.71M | for (j = 0; j < 2; j++) { |
85 | 3.89M | pwt->chroma_weight[i][list][j][0] = get_se_golomb(gb); |
86 | 3.89M | pwt->chroma_weight[i][list][j][1] = get_se_golomb(gb); |
87 | 3.89M | if ((int8_t)pwt->chroma_weight[i][list][j][0] != pwt->chroma_weight[i][list][j][0] || |
88 | 3.81M | (int8_t)pwt->chroma_weight[i][list][j][1] != pwt->chroma_weight[i][list][j][1]) { |
89 | 166k | pwt->chroma_weight[i][list][j][0] = chroma_def; |
90 | 166k | pwt->chroma_weight[i][list][j][1] = 0; |
91 | 166k | goto out_range_weight; |
92 | 166k | } |
93 | 3.72M | if (pwt->chroma_weight[i][list][j][0] != chroma_def || |
94 | 3.61M | pwt->chroma_weight[i][list][j][1] != 0) { |
95 | 3.61M | pwt->use_weight_chroma = 1; |
96 | 3.61M | pwt->chroma_weight_flag[list] = 1; |
97 | 3.61M | } |
98 | 3.72M | } |
99 | 4.24M | } else { |
100 | 4.24M | int j; |
101 | 12.7M | for (j = 0; j < 2; j++) { |
102 | 8.49M | pwt->chroma_weight[i][list][j][0] = chroma_def; |
103 | 8.49M | pwt->chroma_weight[i][list][j][1] = 0; |
104 | 8.49M | } |
105 | 4.24M | } |
106 | 6.23M | } |
107 | | |
108 | | // for MBAFF |
109 | 6.35M | if (picture_structure == PICT_FRAME) { |
110 | 4.10M | pwt->luma_weight[16 + 2 * i][list][0] = pwt->luma_weight[16 + 2 * i + 1][list][0] = pwt->luma_weight[i][list][0]; |
111 | 4.10M | pwt->luma_weight[16 + 2 * i][list][1] = pwt->luma_weight[16 + 2 * i + 1][list][1] = pwt->luma_weight[i][list][1]; |
112 | 4.10M | if (sps->chroma_format_idc) { |
113 | 11.6M | for (j = 0; j < 2; j++) { |
114 | 7.74M | 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 | 7.74M | 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 | 7.74M | } |
117 | 3.87M | } |
118 | 4.10M | } |
119 | 6.35M | } |
120 | 1.61M | if (slice_type_nos != AV_PICTURE_TYPE_B) |
121 | 1.28M | break; |
122 | 1.61M | } |
123 | 1.43M | pwt->use_weight = pwt->use_weight || pwt->use_weight_chroma; |
124 | 1.43M | return 0; |
125 | 267k | out_range_weight: |
126 | 267k | avpriv_request_sample(logctx, "Out of range weight"); |
127 | 267k | return AVERROR_INVALIDDATA; |
128 | 1.70M | } |
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 | | int ff_h264_check_intra4x4_pred_mode(int8_t *pred_mode_cache, void *logctx, |
135 | | int top_samples_available, int left_samples_available) |
136 | 2.60M | { |
137 | 2.60M | static const int8_t top[12] = { |
138 | 2.60M | -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0 |
139 | 2.60M | }; |
140 | 2.60M | static const int8_t left[12] = { |
141 | 2.60M | 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED |
142 | 2.60M | }; |
143 | 2.60M | int i; |
144 | | |
145 | 2.60M | if (!(top_samples_available & 0x8000)) { |
146 | 5.27M | for (i = 0; i < 4; i++) { |
147 | 4.33M | int status = top[pred_mode_cache[scan8[0] + i]]; |
148 | 4.33M | if (status < 0) { |
149 | 272k | av_log(logctx, AV_LOG_ERROR, |
150 | 272k | "top block unavailable for requested intra mode %d\n", |
151 | 272k | status); |
152 | 272k | return AVERROR_INVALIDDATA; |
153 | 4.06M | } else if (status) { |
154 | 3.45M | pred_mode_cache[scan8[0] + i] = status; |
155 | 3.45M | } |
156 | 4.33M | } |
157 | 1.21M | } |
158 | | |
159 | 2.33M | if ((left_samples_available & 0x8888) != 0x8888) { |
160 | 844k | static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 }; |
161 | 3.77M | for (i = 0; i < 4; i++) |
162 | 3.06M | if (!(left_samples_available & mask[i])) { |
163 | 3.06M | int status = left[pred_mode_cache[scan8[0] + 8 * i]]; |
164 | 3.06M | if (status < 0) { |
165 | 128k | av_log(logctx, AV_LOG_ERROR, |
166 | 128k | "left block unavailable for requested intra4x4 mode %d\n", |
167 | 128k | status); |
168 | 128k | return AVERROR_INVALIDDATA; |
169 | 2.93M | } else if (status) { |
170 | 2.69M | pred_mode_cache[scan8[0] + 8 * i] = status; |
171 | 2.69M | } |
172 | 3.06M | } |
173 | 844k | } |
174 | | |
175 | 2.20M | return 0; |
176 | 2.33M | } |
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 | | 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 | 5.76M | { |
186 | 5.76M | static const int8_t top[4] = { LEFT_DC_PRED8x8, 1, -1, -1 }; |
187 | 5.76M | static const int8_t left[5] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 }; |
188 | | |
189 | 5.76M | if (mode > 3U) { |
190 | 18.2k | av_log(logctx, AV_LOG_ERROR, |
191 | 18.2k | "out of range intra chroma pred mode\n"); |
192 | 18.2k | return AVERROR_INVALIDDATA; |
193 | 18.2k | } |
194 | | |
195 | 5.75M | if (!(top_samples_available & 0x8000)) { |
196 | 1.63M | mode = top[mode]; |
197 | 1.63M | if (mode < 0) { |
198 | 121k | av_log(logctx, AV_LOG_ERROR, |
199 | 121k | "top block unavailable for requested intra mode\n"); |
200 | 121k | return AVERROR_INVALIDDATA; |
201 | 121k | } |
202 | 1.63M | } |
203 | | |
204 | 5.62M | if ((left_samples_available & 0x8080) != 0x8080) { |
205 | 826k | mode = left[mode]; |
206 | 826k | if (mode < 0) { |
207 | 82.9k | av_log(logctx, AV_LOG_ERROR, |
208 | 82.9k | "left block unavailable for requested intra mode\n"); |
209 | 82.9k | return AVERROR_INVALIDDATA; |
210 | 82.9k | } |
211 | 743k | if (is_chroma && (left_samples_available & 0x8080)) { |
212 | | // mad cow disease mode, aka MBAFF + constrained_intra_pred |
213 | 3.68k | mode = ALZHEIMER_DC_L0T_PRED8x8 + |
214 | 3.68k | (!(left_samples_available & 0x8000)) + |
215 | 3.68k | 2 * (mode == DC_128_PRED8x8); |
216 | 3.68k | } |
217 | 743k | } |
218 | | |
219 | 5.54M | return mode; |
220 | 5.62M | } |
221 | | |
222 | | 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 | 6.12M | { |
226 | 6.12M | int list_count; |
227 | 6.12M | int num_ref_idx_active_override_flag; |
228 | | |
229 | | // set defaults, might be overridden a few lines later |
230 | 6.12M | ref_count[0] = pps->ref_count[0]; |
231 | 6.12M | ref_count[1] = pps->ref_count[1]; |
232 | | |
233 | 6.12M | if (slice_type_nos != AV_PICTURE_TYPE_I) { |
234 | 4.45M | unsigned max[2]; |
235 | 4.45M | max[0] = max[1] = picture_structure == PICT_FRAME ? 15 : 31; |
236 | | |
237 | 4.45M | num_ref_idx_active_override_flag = get_bits1(gb); |
238 | | |
239 | 4.45M | if (num_ref_idx_active_override_flag) { |
240 | 2.03M | ref_count[0] = get_ue_golomb(gb) + 1; |
241 | 2.03M | if (slice_type_nos == AV_PICTURE_TYPE_B) { |
242 | 707k | ref_count[1] = get_ue_golomb(gb) + 1; |
243 | 707k | } else |
244 | | // full range is spec-ok in this case, even for frames |
245 | 1.32M | ref_count[1] = 1; |
246 | 2.03M | } |
247 | | |
248 | 4.45M | if (slice_type_nos == AV_PICTURE_TYPE_B) |
249 | 1.60M | list_count = 2; |
250 | 2.84M | else |
251 | 2.84M | list_count = 1; |
252 | | |
253 | 4.45M | if (ref_count[0] - 1 > max[0] || (list_count == 2 && (ref_count[1] - 1 > max[1]))) { |
254 | 276k | av_log(logctx, AV_LOG_ERROR, "reference overflow %u > %u or %u > %u\n", |
255 | 276k | ref_count[0] - 1, max[0], ref_count[1] - 1, max[1]); |
256 | 276k | ref_count[0] = ref_count[1] = 0; |
257 | 276k | *plist_count = 0; |
258 | 276k | goto fail; |
259 | 4.17M | } else if (ref_count[1] - 1 > max[1]) { |
260 | 41.5k | av_log(logctx, AV_LOG_DEBUG, "reference overflow %u > %u \n", |
261 | 41.5k | ref_count[1] - 1, max[1]); |
262 | 41.5k | ref_count[1] = 0; |
263 | 41.5k | } |
264 | | |
265 | 4.45M | } else { |
266 | 1.67M | list_count = 0; |
267 | 1.67M | ref_count[0] = ref_count[1] = 0; |
268 | 1.67M | } |
269 | | |
270 | 5.85M | *plist_count = list_count; |
271 | | |
272 | 5.85M | return 0; |
273 | 276k | fail: |
274 | 276k | *plist_count = 0; |
275 | 276k | ref_count[0] = 0; |
276 | 276k | ref_count[1] = 0; |
277 | 276k | return AVERROR_INVALIDDATA; |
278 | 6.12M | } |
279 | | |
280 | | 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 | 7.89M | { |
284 | 7.89M | const int max_frame_num = 1 << sps->log2_max_frame_num; |
285 | 7.89M | int64_t field_poc[2]; |
286 | | |
287 | 7.89M | pc->frame_num_offset = pc->prev_frame_num_offset; |
288 | 7.89M | if (pc->frame_num < pc->prev_frame_num) |
289 | 960k | pc->frame_num_offset += max_frame_num; |
290 | | |
291 | 7.89M | if (sps->poc_type == 0) { |
292 | 5.21M | const int max_poc_lsb = 1 << sps->log2_max_poc_lsb; |
293 | 5.21M | if (pc->prev_poc_lsb < 0) |
294 | 674k | pc->prev_poc_lsb = pc->poc_lsb; |
295 | | |
296 | 5.21M | if (pc->poc_lsb < pc->prev_poc_lsb && |
297 | 847k | pc->prev_poc_lsb - pc->poc_lsb >= max_poc_lsb / 2) |
298 | 291k | pc->poc_msb = pc->prev_poc_msb + max_poc_lsb; |
299 | 4.92M | else if (pc->poc_lsb > pc->prev_poc_lsb && |
300 | 2.11M | pc->prev_poc_lsb - pc->poc_lsb < -max_poc_lsb / 2) |
301 | 818k | pc->poc_msb = pc->prev_poc_msb - max_poc_lsb; |
302 | 4.10M | else |
303 | 4.10M | pc->poc_msb = pc->prev_poc_msb; |
304 | 5.21M | field_poc[0] = |
305 | 5.21M | field_poc[1] = pc->poc_msb + pc->poc_lsb; |
306 | 5.21M | if (picture_structure == PICT_FRAME) |
307 | 4.00M | field_poc[1] += pc->delta_poc_bottom; |
308 | 5.21M | } else if (sps->poc_type == 1) { |
309 | 1.50M | int abs_frame_num; |
310 | 1.50M | int64_t expected_delta_per_poc_cycle, expectedpoc; |
311 | 1.50M | int i; |
312 | | |
313 | 1.50M | if (sps->poc_cycle_length != 0) |
314 | 889k | abs_frame_num = pc->frame_num_offset + pc->frame_num; |
315 | 619k | else |
316 | 619k | abs_frame_num = 0; |
317 | | |
318 | 1.50M | if (nal_ref_idc == 0 && abs_frame_num > 0) |
319 | 113k | abs_frame_num--; |
320 | | |
321 | 1.50M | expected_delta_per_poc_cycle = 0; |
322 | 13.2M | for (i = 0; i < sps->poc_cycle_length; i++) |
323 | | // FIXME integrate during sps parse |
324 | 11.7M | expected_delta_per_poc_cycle += sps->offset_for_ref_frame[i]; |
325 | | |
326 | 1.50M | if (abs_frame_num > 0) { |
327 | 774k | int poc_cycle_cnt = (abs_frame_num - 1) / sps->poc_cycle_length; |
328 | 774k | int frame_num_in_poc_cycle = (abs_frame_num - 1) % sps->poc_cycle_length; |
329 | | |
330 | 774k | expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle; |
331 | 5.78M | for (i = 0; i <= frame_num_in_poc_cycle; i++) |
332 | 5.01M | expectedpoc = expectedpoc + sps->offset_for_ref_frame[i]; |
333 | 774k | } else |
334 | 734k | expectedpoc = 0; |
335 | | |
336 | 1.50M | if (nal_ref_idc == 0) |
337 | 238k | expectedpoc = expectedpoc + sps->offset_for_non_ref_pic; |
338 | | |
339 | 1.50M | field_poc[0] = expectedpoc + pc->delta_poc[0]; |
340 | 1.50M | field_poc[1] = field_poc[0] + sps->offset_for_top_to_bottom_field; |
341 | | |
342 | 1.50M | if (picture_structure == PICT_FRAME) |
343 | 1.07M | field_poc[1] += pc->delta_poc[1]; |
344 | 1.50M | } else { |
345 | 1.16M | int poc = 2 * (pc->frame_num_offset + pc->frame_num); |
346 | | |
347 | 1.16M | if (!nal_ref_idc) |
348 | 260k | poc--; |
349 | | |
350 | 1.16M | field_poc[0] = poc; |
351 | 1.16M | field_poc[1] = poc; |
352 | 1.16M | } |
353 | | |
354 | 7.89M | if ( field_poc[0] != (int)field_poc[0] |
355 | 7.81M | || field_poc[1] != (int)field_poc[1]) |
356 | 96.7k | return AVERROR_INVALIDDATA; |
357 | | |
358 | 7.79M | if (picture_structure != PICT_BOTTOM_FIELD) |
359 | 7.26M | pic_field_poc[0] = field_poc[0]; |
360 | 7.79M | if (picture_structure != PICT_TOP_FIELD) |
361 | 6.54M | pic_field_poc[1] = field_poc[1]; |
362 | 7.79M | *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]); |
363 | | |
364 | 7.79M | return 0; |
365 | 7.89M | } |
366 | | |
367 | | static int decode_extradata_ps(const uint8_t *data, int size, H264ParamSets *ps, |
368 | | int is_avc, void *logctx) |
369 | 139k | { |
370 | 139k | H2645Packet pkt = { 0 }; |
371 | 139k | int flags = (H2645_FLAG_IS_NALFF * !!is_avc) | H2645_FLAG_SMALL_PADDING; |
372 | 139k | int i, ret = 0; |
373 | | |
374 | 139k | ret = ff_h2645_packet_split(&pkt, data, size, logctx, 2, AV_CODEC_ID_H264, flags); |
375 | 139k | if (ret < 0) { |
376 | 16.0k | ret = 0; |
377 | 16.0k | goto fail; |
378 | 16.0k | } |
379 | | |
380 | 305k | for (i = 0; i < pkt.nb_nals; i++) { |
381 | 218k | H2645NAL *nal = &pkt.nals[i]; |
382 | 218k | switch (nal->type) { |
383 | 78.8k | case H264_NAL_SPS: { |
384 | 78.8k | GetBitContext tmp_gb = nal->gb; |
385 | 78.8k | ret = ff_h264_decode_seq_parameter_set(&tmp_gb, logctx, ps, 0); |
386 | 78.8k | if (ret >= 0) |
387 | 31.1k | break; |
388 | 47.6k | av_log(logctx, AV_LOG_DEBUG, |
389 | 47.6k | "SPS decoding failure, trying again with the complete NAL\n"); |
390 | 47.6k | init_get_bits8(&tmp_gb, nal->raw_data + 1, nal->raw_size - 1); |
391 | 47.6k | ret = ff_h264_decode_seq_parameter_set(&tmp_gb, logctx, ps, 0); |
392 | 47.6k | if (ret >= 0) |
393 | 3.30k | break; |
394 | 44.3k | ret = ff_h264_decode_seq_parameter_set(&nal->gb, logctx, ps, 1); |
395 | 44.3k | if (ret < 0) |
396 | 23.8k | goto fail; |
397 | 20.5k | break; |
398 | 44.3k | } |
399 | 48.3k | case H264_NAL_PPS: |
400 | 48.3k | ret = ff_h264_decode_picture_parameter_set(&nal->gb, logctx, ps, |
401 | 48.3k | nal->size_bits); |
402 | 48.3k | if (ret < 0) |
403 | 12.8k | goto fail; |
404 | 35.4k | break; |
405 | 91.4k | default: |
406 | 91.4k | av_log(logctx, AV_LOG_VERBOSE, "Ignoring NAL type %d in extradata\n", |
407 | 91.4k | nal->type); |
408 | 91.4k | break; |
409 | 218k | } |
410 | 218k | } |
411 | | |
412 | 139k | fail: |
413 | 139k | ff_h2645_packet_uninit(&pkt); |
414 | 139k | return ret; |
415 | 123k | } |
416 | | |
417 | | /* There are (invalid) samples in the wild with mp4-style extradata, where the |
418 | | * parameter sets are stored unescaped (i.e. as RBSP). |
419 | | * This function catches the parameter set decoding failure and tries again |
420 | | * after escaping it */ |
421 | | static int decode_extradata_ps_mp4(const uint8_t *buf, int buf_size, H264ParamSets *ps, |
422 | | int err_recognition, void *logctx) |
423 | 87.8k | { |
424 | 87.8k | int ret; |
425 | | |
426 | 87.8k | ret = decode_extradata_ps(buf, buf_size, ps, 1, logctx); |
427 | 87.8k | if (ret < 0 && !(err_recognition & AV_EF_EXPLODE)) { |
428 | 14.6k | GetByteContext gbc; |
429 | 14.6k | PutByteContext pbc; |
430 | 14.6k | uint8_t *escaped_buf; |
431 | 14.6k | int escaped_buf_size; |
432 | | |
433 | 14.6k | av_log(logctx, AV_LOG_WARNING, |
434 | 14.6k | "SPS decoding failure, trying again after escaping the NAL\n"); |
435 | | |
436 | 14.6k | if (buf_size / 2 >= (INT16_MAX - AV_INPUT_BUFFER_PADDING_SIZE) / 3) |
437 | 228 | return AVERROR(ERANGE); |
438 | 14.4k | escaped_buf_size = buf_size * 3 / 2 + AV_INPUT_BUFFER_PADDING_SIZE; |
439 | 14.4k | escaped_buf = av_mallocz(escaped_buf_size); |
440 | 14.4k | if (!escaped_buf) |
441 | 0 | return AVERROR(ENOMEM); |
442 | | |
443 | 14.4k | bytestream2_init(&gbc, buf, buf_size); |
444 | 14.4k | bytestream2_init_writer(&pbc, escaped_buf, escaped_buf_size); |
445 | | |
446 | 2.02M | while (bytestream2_get_bytes_left(&gbc)) { |
447 | 2.00M | if (bytestream2_get_bytes_left(&gbc) >= 3 && |
448 | 1.98M | bytestream2_peek_be24(&gbc) <= 3) { |
449 | 240k | bytestream2_put_be24(&pbc, 3); |
450 | 240k | bytestream2_skip(&gbc, 2); |
451 | 240k | } else |
452 | 1.76M | bytestream2_put_byte(&pbc, bytestream2_get_byte(&gbc)); |
453 | 2.00M | } |
454 | | |
455 | 14.4k | escaped_buf_size = bytestream2_tell_p(&pbc); |
456 | 14.4k | AV_WB16(escaped_buf, escaped_buf_size - 2); |
457 | | |
458 | 14.4k | (void)decode_extradata_ps(escaped_buf, escaped_buf_size, ps, 1, logctx); |
459 | | // lorex.mp4 decodes ok even with extradata decoding failing |
460 | 14.4k | av_freep(&escaped_buf); |
461 | 14.4k | } |
462 | | |
463 | 87.5k | return 0; |
464 | 87.8k | } |
465 | | |
466 | | int ff_h264_decode_extradata(const uint8_t *data, int size, H264ParamSets *ps, |
467 | | int *is_avc, int *nal_length_size, |
468 | | int err_recognition, void *logctx) |
469 | 83.5k | { |
470 | 83.5k | int ret; |
471 | | |
472 | 83.5k | if (!data || size <= 0) |
473 | 0 | return AVERROR(EINVAL); |
474 | | |
475 | 83.5k | if (data[0] == 1) { |
476 | 45.9k | int i, cnt, nalsize; |
477 | 45.9k | const uint8_t *p = data; |
478 | | |
479 | 45.9k | *is_avc = 1; |
480 | | |
481 | 45.9k | if (size < 7) { |
482 | 1.40k | av_log(logctx, AV_LOG_ERROR, "avcC %d too short\n", size); |
483 | 1.40k | return AVERROR_INVALIDDATA; |
484 | 1.40k | } |
485 | | |
486 | | // Decode sps from avcC |
487 | 44.5k | cnt = *(p + 5) & 0x1f; // Number of sps |
488 | 44.5k | p += 6; |
489 | 96.9k | for (i = 0; i < cnt; i++) { |
490 | 56.9k | nalsize = AV_RB16(p) + 2; |
491 | 56.9k | if (nalsize > size - (p - data)) |
492 | 4.54k | return AVERROR_INVALIDDATA; |
493 | 52.4k | ret = decode_extradata_ps_mp4(p, nalsize, ps, err_recognition, logctx); |
494 | 52.4k | if (ret < 0) { |
495 | 50 | av_log(logctx, AV_LOG_ERROR, |
496 | 50 | "Decoding sps %d from avcC failed\n", i); |
497 | 50 | return ret; |
498 | 50 | } |
499 | 52.3k | p += nalsize; |
500 | 52.3k | } |
501 | | // Decode pps from avcC |
502 | 39.9k | cnt = *(p++); // Number of pps |
503 | 75.2k | for (i = 0; i < cnt; i++) { |
504 | 44.9k | nalsize = AV_RB16(p) + 2; |
505 | 44.9k | if (nalsize > size - (p - data)) |
506 | 9.55k | return AVERROR_INVALIDDATA; |
507 | 35.4k | ret = decode_extradata_ps_mp4(p, nalsize, ps, err_recognition, logctx); |
508 | 35.4k | if (ret < 0) { |
509 | 178 | av_log(logctx, AV_LOG_ERROR, |
510 | 178 | "Decoding pps %d from avcC failed\n", i); |
511 | 178 | return ret; |
512 | 178 | } |
513 | 35.2k | p += nalsize; |
514 | 35.2k | } |
515 | | // Store right nal length size that will be used to parse all other nals |
516 | 30.2k | *nal_length_size = (data[4] & 0x03) + 1; |
517 | 37.5k | } else { |
518 | 37.5k | *is_avc = 0; |
519 | 37.5k | ret = decode_extradata_ps(data, size, ps, 0, logctx); |
520 | 37.5k | if (ret < 0) |
521 | 10.0k | return ret; |
522 | 37.5k | } |
523 | 57.7k | return size; |
524 | 83.5k | } |
525 | | |
526 | | /** |
527 | | * Compute profile from profile_idc and constraint_set?_flags. |
528 | | * |
529 | | * @param sps SPS |
530 | | * |
531 | | * @return profile as defined by AV_PROFILE_H264_* |
532 | | */ |
533 | | int ff_h264_get_profile(const SPS *sps) |
534 | 7.56M | { |
535 | 7.56M | int profile = sps->profile_idc; |
536 | | |
537 | 7.56M | switch (sps->profile_idc) { |
538 | 406k | case AV_PROFILE_H264_BASELINE: |
539 | | // constraint_set1_flag set to 1 |
540 | 406k | profile |= (sps->constraint_set_flags & 1 << 1) ? AV_PROFILE_H264_CONSTRAINED : 0; |
541 | 406k | break; |
542 | 225k | case AV_PROFILE_H264_HIGH_10: |
543 | 991k | case AV_PROFILE_H264_HIGH_422: |
544 | 3.43M | case AV_PROFILE_H264_HIGH_444_PREDICTIVE: |
545 | | // constraint_set3_flag set to 1 |
546 | 3.43M | profile |= (sps->constraint_set_flags & 1 << 3) ? AV_PROFILE_H264_INTRA : 0; |
547 | 3.43M | break; |
548 | 7.56M | } |
549 | | |
550 | 7.56M | return profile; |
551 | 7.56M | } |