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
29
31 {
32 const uint8_t *
a = p + 4 - ((intptr_t)p & 3);
33
34 for (end -= 3; p <
a && p < end; p++) {
35 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
36 return p;
37 }
38
39 for (end -= 3; p < end; p += 4) {
40 uint32_t x = *(const uint32_t*)p;
41 // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
42 // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
43 if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
44 if (p[1] == 0) {
45 if (p[0] == 0 && p[2] == 1)
46 return p;
47 if (p[2] == 0 && p[3] == 1)
48 return p+1;
49 }
50 if (p[3] == 0) {
51 if (p[2] == 0 && p[4] == 1)
52 return p+2;
53 if (p[4] == 0 && p[5] == 1)
54 return p+3;
55 }
56 }
57 }
58
59 for (end += 3; p < end; p++) {
60 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
61 return p;
62 }
63
64 return end + 3;
65 }
66
71 }
72
74 const uint8_t *buf_in,
int size)
75 {
76 const uint8_t *p = buf_in;
77 const uint8_t *end = p +
size;
78 const uint8_t *nal_start, *nal_end;
79
82 for (;;) {
83 const size_t nalu_limit = SIZE_MAX /
sizeof(*
list->nalus);
84 while (nal_start < end && !*(nal_start++));
85 if (nal_start == end)
86 break;
87
89 if (pb) {
91 avio_write(pb, nal_start, nal_end - nal_start);
92 }
else if (
list->nb_nalus >= nalu_limit) {
94 } else {
96 (
list->nb_nalus + 1) *
sizeof(*
list->nalus));
100 tmp[
list->nb_nalus++] = (
NALU){ .offset = nal_start - p,
101 .size = nal_end - nal_start };
102 }
103 size += 4 + nal_end - nal_start;
104 nal_start = nal_end;
105 }
107 }
108
110 {
112 }
113
115 {
118 }
119
121 const uint8_t *buf)
122 {
123 for (
unsigned i = 0;
i <
list->nb_nalus;
i++) {
126 }
127 }
128
130 {
135
137
139 return 0;
140 }
141
143 {
145 uint8_t *buf, *end, *start;
146 uint8_t *
sps, *
pps, *sps_ext;
147 uint32_t sps_size = 0, pps_size = 0, sps_ext_size = 0;
148 int ret, nb_sps = 0, nb_pps = 0, nb_sps_ext = 0;
149
152
153 /* check for H.264 start code */
157 return 0;
158 }
159
163 start = buf;
165
175
176 /* look for sps and pps */
177 while (end - buf > 4) {
179 uint8_t nal_type;
181 buf += 4;
182 nal_type = buf[0] & 0x1f;
183
184 if (nal_type == 7) { /* SPS */
185 nb_sps++;
189 }
192 } else if (nal_type == 8) { /* PPS */
193 nb_pps++;
197 }
200 } else if (nal_type == 13) { /* SPS_EXT */
201 nb_sps_ext++;
202 if (
size > UINT16_MAX || nb_sps_ext >= 256) {
205 }
208 }
209
211 }
215
216 if (sps_size < 6 || !pps_size) {
219 }
220
225 avio_w8(pb, 0xff);
/* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
226 avio_w8(pb, 0xe0 | nb_sps);
/* 3 bits reserved (111) + 5 bits number of sps */
227
229 avio_w8(pb, nb_pps);
/* number of pps */
231
232 if (
sps[3] != 66 &&
sps[3] != 77 &&
sps[3] != 88) {
237
241 avio_w8(pb, nb_sps_ext);
/* number of sps ext */
242 if (nb_sps_ext)
244 }
245
251
253 }
254
256 {
257 uint16_t sps_size, pps_size;
260
263 return 0;
264 if (*
size < 11 || in[0] != 1)
266
268 if (11 + sps_size > *
size)
270 pps_size =
AV_RB16(&in[9 + sps_size]);
271 if (11 + sps_size + pps_size > *
size)
278 memcpy(
out + 4, &in[8], sps_size);
280 memcpy(
out + 8 + sps_size, &in[11 + sps_size], pps_size);
283 return 0;
284 }
285
287 const uint8_t *end,
288 int nal_length_size)
289 {
290 unsigned int res = 0;
291
292 if (end - start < nal_length_size)
294 while (nal_length_size--)
295 res = (res << 8) | *start++;
296
297 if (res > end - start)
299
300 return start + res;
301 }
302
304 uint32_t *dst_len, int header_len)
305 {
306 uint8_t *dst;
308
310 if (!dst)
312
313 /* NAL unit header */
315 while (
i < header_len &&
i < src_len)
317
318 while (
i + 2 < src_len)
322 i++;
// remove emulation_prevention_three_byte
323 } else
325
328
330
332 return dst;
333 }
334
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
358 ;
360 }
361
364 int sign = -(v & 1);
365 return ((v >> 1) ^ sign) - sign;
366 }
367
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 int delta_scale, lastScale = 8, nextScale = 8;
373 int sizeOfScalingList;
375 uint8_t *rbsp_buf;
376
378 if (!rbsp_buf)
380
383 goto end;
384
385 memset(
sps, 0,
sizeof(*
sps));
386
388 sps->constraint_set_flags |=
get_bits1(&gb) << 0;
// constraint_set0_flag
389 sps->constraint_set_flags |=
get_bits1(&gb) << 1;
// constraint_set1_flag
390 sps->constraint_set_flags |=
get_bits1(&gb) << 2;
// constraint_set2_flag
391 sps->constraint_set_flags |=
get_bits1(&gb) << 3;
// constraint_set3_flag
392 sps->constraint_set_flags |=
get_bits1(&gb) << 4;
// constraint_set4_flag
393 sps->constraint_set_flags |=
get_bits1(&gb) << 5;
// constraint_set5_flag
394 skip_bits(&gb, 2);
// reserved_zero_2bits
397
398 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) {
404 if (
sps->chroma_format_idc == 3) {
405 skip_bits1(&gb);
// separate_colour_plane_flag
406 }
409 skip_bits1(&gb);
// qpprime_y_zero_transform_bypass_flag
410 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) {
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
434
435 if (pic_order_cnt_type == 0) {
437 } else if (pic_order_cnt_type == 1) {
438 skip_bits1(&gb);
// delta_pic_order_always_zero
442 for (
i = 0;
i < num_ref_frames_in_pic_order_cnt_cycle;
i++)
444 }
445
447 skip_bits1(&gb);
// gaps_in_frame_num_value_allowed_flag
450
452 if (!
sps->frame_mbs_only_flag)
453 skip_bits1(&gb);
// mb_adaptive_frame_field_flag
454
456
457 if (
get_bits1(&gb)) {
// frame_cropping_flag
462 }
463
464 if (
get_bits1(&gb)) {
// vui_parameters_present_flag
465 if (
get_bits1(&gb)) {
// aspect_ratio_info_present_flag
466 aspect_ratio_idc =
get_bits(&gb, 8);
467 if (aspect_ratio_idc == 0xff) {
472 }
473 }
474 }
475
479 }
480
482 end:
485 }