2 * AVC helper functions for muxers
3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "libavcodec/h264.h"
24 #include "libavcodec/get_bits.h"
29 static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p, const uint8_t *end)
31 const uint8_t *a = p + 4 - ((intptr_t)p & 3);
33 for (end -= 3; p < a && p < end; p++) {
34 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
38 for (end -= 3; p < end; p += 4) {
39 uint32_t x = *(const uint32_t*)p;
40 // if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
41 // if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
42 if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
44 if (p[0] == 0 && p[2] == 1)
46 if (p[2] == 0 && p[3] == 1)
50 if (p[2] == 0 && p[4] == 1)
52 if (p[4] == 0 && p[5] == 1)
58 for (end += 3; p < end; p++) {
59 if (p[0] == 0 && p[1] == 0 && p[2] == 1)
66 const uint8_t *ff_avc_find_startcode(const uint8_t *p, const uint8_t *end){
67 const uint8_t *out= ff_avc_find_startcode_internal(p, end);
68 if(p<out && out<end && !out[-1]) out--;
72 int ff_avc_parse_nal_units(AVIOContext *pb, const uint8_t *buf_in, int size)
74 const uint8_t *p = buf_in;
75 const uint8_t *end = p + size;
76 const uint8_t *nal_start, *nal_end;
79 nal_start = ff_avc_find_startcode(p, end);
81 while (nal_start < end && !*(nal_start++));
85 nal_end = ff_avc_find_startcode(nal_start, end);
86 avio_wb32(pb, nal_end - nal_start);
87 avio_write(pb, nal_start, nal_end - nal_start);
88 size += 4 + nal_end - nal_start;
94 int ff_avc_parse_nal_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
97 int ret = avio_open_dyn_buf(&pb);
101 ff_avc_parse_nal_units(pb, buf_in, *size);
104 *size = avio_close_dyn_buf(pb, buf);
108 int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
110 AVIOContext *sps_pb = NULL, *pps_pb = NULL;
111 uint8_t *buf = NULL, *end, *start = NULL;
112 uint8_t *sps = NULL, *pps = NULL;
113 uint32_t sps_size = 0, pps_size = 0;
114 int ret, nb_sps = 0, nb_pps = 0;
117 return AVERROR_INVALIDDATA;
119 /* check for H.264 start code */
120 if (AV_RB32(data) != 0x00000001 &&
121 AV_RB24(data) != 0x000001) {
122 avio_write(pb, data, len);
126 ret = ff_avc_parse_nal_units_buf(data, &buf, &len);
132 ret = avio_open_dyn_buf(&sps_pb);
135 ret = avio_open_dyn_buf(&pps_pb);
139 /* look for sps and pps */
140 while (end - buf > 4) {
143 size = FFMIN(AV_RB32(buf), end - buf - 4);
145 nal_type = buf[0] & 0x1f;
147 if (nal_type == 7) { /* SPS */
149 if (size > UINT16_MAX || nb_sps >= H264_MAX_SPS_COUNT) {
150 ret = AVERROR_INVALIDDATA;
153 avio_wb16(sps_pb, size);
154 avio_write(sps_pb, buf, size);
155 } else if (nal_type == 8) { /* PPS */
157 if (size > UINT16_MAX || nb_pps >= H264_MAX_PPS_COUNT) {
158 ret = AVERROR_INVALIDDATA;
161 avio_wb16(pps_pb, size);
162 avio_write(pps_pb, buf, size);
167 sps_size = avio_close_dyn_buf(sps_pb, &sps);
168 pps_size = avio_close_dyn_buf(pps_pb, &pps);
170 if (sps_size < 6 || !pps_size) {
171 ret = AVERROR_INVALIDDATA;
175 avio_w8(pb, 1); /* version */
176 avio_w8(pb, sps[3]); /* profile */
177 avio_w8(pb, sps[4]); /* profile compat */
178 avio_w8(pb, sps[5]); /* level */
179 avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
180 avio_w8(pb, 0xe0 | nb_sps); /* 3 bits reserved (111) + 5 bits number of sps */
182 avio_write(pb, sps, sps_size);
183 avio_w8(pb, nb_pps); /* number of pps */
184 avio_write(pb, pps, pps_size);
188 avio_close_dyn_buf(sps_pb, &sps);
190 avio_close_dyn_buf(pps_pb, &pps);
198 int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
200 uint16_t sps_size, pps_size;
205 if (*size >= 4 && (AV_RB32(in) == 0x00000001 || AV_RB24(in) == 0x000001))
207 if (*size < 11 || in[0] != 1)
208 return AVERROR_INVALIDDATA;
210 sps_size = AV_RB16(&in[6]);
211 if (11 + sps_size > *size)
212 return AVERROR_INVALIDDATA;
213 pps_size = AV_RB16(&in[9 + sps_size]);
214 if (11 + sps_size + pps_size > *size)
215 return AVERROR_INVALIDDATA;
216 out_size = 8 + sps_size + pps_size;
217 out = av_mallocz(out_size + AV_INPUT_BUFFER_PADDING_SIZE);
219 return AVERROR(ENOMEM);
220 AV_WB32(&out[0], 0x00000001);
221 memcpy(out + 4, &in[8], sps_size);
222 AV_WB32(&out[4 + sps_size], 0x00000001);
223 memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size);
229 const uint8_t *ff_avc_mp4_find_startcode(const uint8_t *start,
233 unsigned int res = 0;
235 if (end - start < nal_length_size)
237 while (nal_length_size--)
238 res = (res << 8) | *start++;
240 if (res > end - start)
246 uint8_t *ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
247 uint32_t *dst_len, int header_len)
252 dst = av_malloc(src_len + AV_INPUT_BUFFER_PADDING_SIZE);
256 /* NAL unit header */
258 while (i < header_len && i < src_len)
259 dst[len++] = src[i++];
261 while (i + 2 < src_len)
262 if (!src[i] && !src[i + 1] && src[i + 2] == 3) {
263 dst[len++] = src[i++];
264 dst[len++] = src[i++];
265 i++; // remove emulation_prevention_three_byte
267 dst[len++] = src[i++];
270 dst[len++] = src[i++];
272 memset(dst + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
278 static const AVRational avc_sample_aspect_ratio[17] = {
298 static inline int get_ue_golomb(GetBitContext *gb) {
300 for (i = 0; i < 32 && !get_bits1(gb); i++)
302 return get_bitsz(gb, i) + (1 << i) - 1;
305 static inline int get_se_golomb(GetBitContext *gb) {
306 int v = get_ue_golomb(gb) + 1;
308 return ((v >> 1) ^ sign) - sign;
311 H264SequenceParameterSet *ff_avc_decode_sps(const uint8_t *buf, int buf_size)
313 int i, j, ret, rbsp_size, aspect_ratio_idc, pic_order_cnt_type;
314 int num_ref_frames_in_pic_order_cnt_cycle;
315 int delta_scale, lastScale = 8, nextScale = 8;
316 int sizeOfScalingList;
317 H264SequenceParameterSet *sps = NULL;
321 rbsp_buf = ff_nal_unit_extract_rbsp(buf, buf_size, &rbsp_size, 0);
325 ret = init_get_bits8(&gb, rbsp_buf, rbsp_size);
329 sps = av_mallocz(sizeof(*sps));
333 sps->profile_idc = get_bits(&gb, 8);
334 sps->constraint_set_flags |= get_bits1(&gb) << 0; // constraint_set0_flag
335 sps->constraint_set_flags |= get_bits1(&gb) << 1; // constraint_set1_flag
336 sps->constraint_set_flags |= get_bits1(&gb) << 2; // constraint_set2_flag
337 sps->constraint_set_flags |= get_bits1(&gb) << 3; // constraint_set3_flag
338 sps->constraint_set_flags |= get_bits1(&gb) << 4; // constraint_set4_flag
339 sps->constraint_set_flags |= get_bits1(&gb) << 5; // constraint_set5_flag
340 skip_bits(&gb, 2); // reserved_zero_2bits
341 sps->level_idc = get_bits(&gb, 8);
342 sps->id = get_ue_golomb(&gb);
344 if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
345 sps->profile_idc == 122 || sps->profile_idc == 244 || sps->profile_idc == 44 ||
346 sps->profile_idc == 83 || sps->profile_idc == 86 || sps->profile_idc == 118 ||
347 sps->profile_idc == 128 || sps->profile_idc == 138 || sps->profile_idc == 139 ||
348 sps->profile_idc == 134) {
349 sps->chroma_format_idc = get_ue_golomb(&gb); // chroma_format_idc
350 if (sps->chroma_format_idc == 3) {
351 skip_bits1(&gb); // separate_colour_plane_flag
353 sps->bit_depth_luma = get_ue_golomb(&gb) + 8;
354 get_ue_golomb(&gb); // bit_depth_chroma_minus8
355 skip_bits1(&gb); // qpprime_y_zero_transform_bypass_flag
356 if (get_bits1(&gb)) { // seq_scaling_matrix_present_flag
357 for (i = 0; i < ((sps->chroma_format_idc != 3) ? 8 : 12); i++) {
358 if (!get_bits1(&gb)) // seq_scaling_list_present_flag
362 sizeOfScalingList = i < 6 ? 16 : 64;
363 for (j = 0; j < sizeOfScalingList; j++) {
364 if (nextScale != 0) {
365 delta_scale = get_se_golomb(&gb);
366 nextScale = (lastScale + delta_scale) & 0xff;
368 lastScale = nextScale == 0 ? lastScale : nextScale;
373 sps->chroma_format_idc = 1;
374 sps->bit_depth_luma = 8;
377 get_ue_golomb(&gb); // log2_max_frame_num_minus4
378 pic_order_cnt_type = get_ue_golomb(&gb);
380 if (pic_order_cnt_type == 0) {
381 get_ue_golomb(&gb); // log2_max_pic_order_cnt_lsb_minus4
382 } else if (pic_order_cnt_type == 1) {
383 skip_bits1(&gb); // delta_pic_order_always_zero
384 get_se_golomb(&gb); // offset_for_non_ref_pic
385 get_se_golomb(&gb); // offset_for_top_to_bottom_field
386 num_ref_frames_in_pic_order_cnt_cycle = get_ue_golomb(&gb);
387 for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++)
388 get_se_golomb(&gb); // offset_for_ref_frame
391 get_ue_golomb(&gb); // max_num_ref_frames
392 skip_bits1(&gb); // gaps_in_frame_num_value_allowed_flag
393 get_ue_golomb(&gb); // pic_width_in_mbs_minus1
394 get_ue_golomb(&gb); // pic_height_in_map_units_minus1
396 sps->frame_mbs_only_flag = get_bits1(&gb);
397 if (!sps->frame_mbs_only_flag)
398 skip_bits1(&gb); // mb_adaptive_frame_field_flag
400 skip_bits1(&gb); // direct_8x8_inference_flag
402 if (get_bits1(&gb)) { // frame_cropping_flag
403 get_ue_golomb(&gb); // frame_crop_left_offset
404 get_ue_golomb(&gb); // frame_crop_right_offset
405 get_ue_golomb(&gb); // frame_crop_top_offset
406 get_ue_golomb(&gb); // frame_crop_bottom_offset
409 if (get_bits1(&gb)) { // vui_parameters_present_flag
410 if (get_bits1(&gb)) { // aspect_ratio_info_present_flag
411 aspect_ratio_idc = get_bits(&gb, 8);
412 if (aspect_ratio_idc == 0xff) {
413 sps->sar.num = get_bits(&gb, 16);
414 sps->sar.den = get_bits(&gb, 16);
415 } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(avc_sample_aspect_ratio)) {
416 sps->sar = avc_sample_aspect_ratio[aspect_ratio_idc];