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
30
32 {
34 uint8_t *buf, *end, *start;
35 uint8_t *
sps, *
pps, *sps_ext;
36 uint32_t sps_size = 0, pps_size = 0, sps_ext_size = 0;
37 int ret, nb_sps = 0, nb_pps = 0, nb_sps_ext = 0;
38
41
42 /* check for H.264 start code */
46 return 0;
47 }
48
52 start = buf;
54
64
65 /* look for sps and pps */
66 while (end - buf > 4) {
68 uint8_t nal_type;
70 buf += 4;
71 nal_type = buf[0] & 0x1f;
72
73 if (nal_type == 7) { /* SPS */
74 nb_sps++;
78 }
81 } else if (nal_type == 8) { /* PPS */
82 nb_pps++;
86 }
89 } else if (nal_type == 13) { /* SPS_EXT */
90 nb_sps_ext++;
91 if (
size > UINT16_MAX || nb_sps_ext >= 256) {
94 }
97 }
98
100 }
104
105 if (sps_size < 6 || !pps_size) {
108 }
109
114 avio_w8(pb, 0xff);
/* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
115 avio_w8(pb, 0xe0 | nb_sps);
/* 3 bits reserved (111) + 5 bits number of sps */
116
118 avio_w8(pb, nb_pps);
/* number of pps */
120
121 if (
sps[3] != 66 &&
sps[3] != 77 &&
sps[3] != 88) {
126
130 avio_w8(pb, nb_sps_ext);
/* number of sps ext */
131 if (nb_sps_ext)
133 }
134
140
142 }
143
145 {
146 uint16_t sps_size, pps_size;
149
152 return 0;
153 if (*
size < 11 || in[0] != 1)
155
157 if (11 + sps_size > *
size)
159 pps_size =
AV_RB16(&in[9 + sps_size]);
160 if (11 + sps_size + pps_size > *
size)
167 memcpy(
out + 4, &in[8], sps_size);
169 memcpy(
out + 8 + sps_size, &in[11 + sps_size], pps_size);
172 return 0;
173 }
174
176 { 0, 1 },
177 { 1, 1 },
178 { 12, 11 },
179 { 10, 11 },
180 { 16, 11 },
181 { 40, 33 },
182 { 24, 11 },
183 { 20, 11 },
184 { 32, 11 },
185 { 80, 33 },
186 { 18, 11 },
187 { 15, 11 },
188 { 64, 33 },
189 { 160, 99 },
190 { 4, 3 },
191 { 3, 2 },
192 { 2, 1 },
193 };
194
198 ;
200 }
201
204 int sign = -(v & 1);
205 return ((v >> 1) ^ sign) - sign;
206 }
207
209 {
210 int i, j,
ret, rbsp_size, aspect_ratio_idc, pic_order_cnt_type;
211 int num_ref_frames_in_pic_order_cnt_cycle;
212 int delta_scale, lastScale = 8, nextScale = 8;
213 int sizeOfScalingList;
215 uint8_t *rbsp_buf;
216
218 if (!rbsp_buf)
220
223 goto end;
224
225 memset(
sps, 0,
sizeof(*
sps));
226
228 sps->constraint_set_flags |=
get_bits1(&gb) << 0;
// constraint_set0_flag
229 sps->constraint_set_flags |=
get_bits1(&gb) << 1;
// constraint_set1_flag
230 sps->constraint_set_flags |=
get_bits1(&gb) << 2;
// constraint_set2_flag
231 sps->constraint_set_flags |=
get_bits1(&gb) << 3;
// constraint_set3_flag
232 sps->constraint_set_flags |=
get_bits1(&gb) << 4;
// constraint_set4_flag
233 sps->constraint_set_flags |=
get_bits1(&gb) << 5;
// constraint_set5_flag
234 skip_bits(&gb, 2);
// reserved_zero_2bits
237
238 if (
sps->profile_idc == 100 ||
sps->profile_idc == 110 ||
239 sps->profile_idc == 122 ||
sps->profile_idc == 244 ||
sps->profile_idc == 44 ||
240 sps->profile_idc == 83 ||
sps->profile_idc == 86 ||
sps->profile_idc == 118 ||
241 sps->profile_idc == 128 ||
sps->profile_idc == 138 ||
sps->profile_idc == 139 ||
242 sps->profile_idc == 134) {
244 if (
sps->chroma_format_idc == 3) {
245 skip_bits1(&gb);
// separate_colour_plane_flag
246 }
249 skip_bits1(&gb);
// qpprime_y_zero_transform_bypass_flag
250 if (
get_bits1(&gb)) {
// seq_scaling_matrix_present_flag
251 for (
i = 0;
i < ((
sps->chroma_format_idc != 3) ? 8 : 12);
i++) {
252 if (!
get_bits1(&gb))
// seq_scaling_list_present_flag
253 continue;
254 lastScale = 8;
255 nextScale = 8;
256 sizeOfScalingList =
i < 6 ? 16 : 64;
257 for (j = 0; j < sizeOfScalingList; j++) {
258 if (nextScale != 0) {
260 nextScale = (lastScale + delta_scale) & 0xff;
261 }
262 lastScale = nextScale == 0 ? lastScale : nextScale;
263 }
264 }
265 }
266 } else {
267 sps->chroma_format_idc = 1;
268 sps->bit_depth_luma = 8;
269 sps->bit_depth_chroma = 8;
270 }
271
274
275 if (pic_order_cnt_type == 0) {
277 } else if (pic_order_cnt_type == 1) {
278 skip_bits1(&gb);
// delta_pic_order_always_zero
282 for (
i = 0;
i < num_ref_frames_in_pic_order_cnt_cycle;
i++)
284 }
285
287 skip_bits1(&gb);
// gaps_in_frame_num_value_allowed_flag
290
292 if (!
sps->frame_mbs_only_flag)
293 skip_bits1(&gb);
// mb_adaptive_frame_field_flag
294
296
297 if (
get_bits1(&gb)) {
// frame_cropping_flag
302 }
303
304 if (
get_bits1(&gb)) {
// vui_parameters_present_flag
305 if (
get_bits1(&gb)) {
// aspect_ratio_info_present_flag
306 aspect_ratio_idc =
get_bits(&gb, 8);
307 if (aspect_ratio_idc == 0xff) {
312 }
313 }
314 }
315
319 }
320
322 end:
325 }