FFmpeg
avc.c
Go to the documentation of this file.
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 "libavcodec/golomb.h"
27 #include "avio.h"
28 #include "avc.h"
29 #include "avio_internal.h"
30 #include "nal.h"
31 
32 int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
33 {
34  AVIOContext *sps_pb = NULL, *pps_pb = NULL, *sps_ext_pb = NULL;
35  uint8_t *buf, *end, *start;
36  uint8_t *sps, *pps, *sps_ext;
37  uint32_t sps_size = 0, pps_size = 0, sps_ext_size = 0;
38  int ret, nb_sps = 0, nb_pps = 0, nb_sps_ext = 0;
39 
40  if (len <= 6)
41  return AVERROR_INVALIDDATA;
42 
43  /* check for H.264 start code */
44  if (AV_RB32(data) != 0x00000001 &&
45  AV_RB24(data) != 0x000001) {
46  avio_write(pb, data, len);
47  return 0;
48  }
49 
51  if (ret < 0)
52  return ret;
53  start = buf;
54  end = buf + len;
55 
56  ret = avio_open_dyn_buf(&sps_pb);
57  if (ret < 0)
58  goto fail;
59  ret = avio_open_dyn_buf(&pps_pb);
60  if (ret < 0)
61  goto fail;
62  ret = avio_open_dyn_buf(&sps_ext_pb);
63  if (ret < 0)
64  goto fail;
65 
66  /* look for sps and pps */
67  while (end - buf > 4) {
68  uint32_t size;
69  uint8_t nal_type;
70  size = FFMIN(AV_RB32(buf), end - buf - 4);
71  buf += 4;
72  nal_type = buf[0] & 0x1f;
73 
74  if (nal_type == 7) { /* SPS */
75  nb_sps++;
76  if (size > UINT16_MAX || nb_sps >= H264_MAX_SPS_COUNT) {
78  goto fail;
79  }
80  avio_wb16(sps_pb, size);
81  avio_write(sps_pb, buf, size);
82  } else if (nal_type == 8) { /* PPS */
83  nb_pps++;
84  if (size > UINT16_MAX || nb_pps >= H264_MAX_PPS_COUNT) {
86  goto fail;
87  }
88  avio_wb16(pps_pb, size);
89  avio_write(pps_pb, buf, size);
90  } else if (nal_type == 13) { /* SPS_EXT */
91  nb_sps_ext++;
92  if (size > UINT16_MAX || nb_sps_ext >= 256) {
94  goto fail;
95  }
96  avio_wb16(sps_ext_pb, size);
97  avio_write(sps_ext_pb, buf, size);
98  }
99 
100  buf += size;
101  }
102  sps_size = avio_get_dyn_buf(sps_pb, &sps);
103  pps_size = avio_get_dyn_buf(pps_pb, &pps);
104  sps_ext_size = avio_get_dyn_buf(sps_ext_pb, &sps_ext);
105 
106  if (sps_size < 6 || !pps_size) {
108  goto fail;
109  }
110 
111  avio_w8(pb, 1); /* version */
112  avio_w8(pb, sps[3]); /* profile */
113  avio_w8(pb, sps[4]); /* profile compat */
114  avio_w8(pb, sps[5]); /* level */
115  avio_w8(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
116  avio_w8(pb, 0xe0 | nb_sps); /* 3 bits reserved (111) + 5 bits number of sps */
117 
118  avio_write(pb, sps, sps_size);
119  avio_w8(pb, nb_pps); /* number of pps */
120  avio_write(pb, pps, pps_size);
121 
122  if (sps[3] != 66 && sps[3] != 77 && sps[3] != 88) {
123  H264SPS seq;
124  ret = ff_avc_decode_sps(&seq, sps + 3, sps_size - 3);
125  if (ret < 0)
126  goto fail;
127 
128  avio_w8(pb, 0xfc | seq.chroma_format_idc); /* 6 bits reserved (111111) + chroma_format_idc */
129  avio_w8(pb, 0xf8 | (seq.bit_depth_luma - 8)); /* 5 bits reserved (11111) + bit_depth_luma_minus8 */
130  avio_w8(pb, 0xf8 | (seq.bit_depth_chroma - 8)); /* 5 bits reserved (11111) + bit_depth_chroma_minus8 */
131  avio_w8(pb, nb_sps_ext); /* number of sps ext */
132  if (nb_sps_ext)
133  avio_write(pb, sps_ext, sps_ext_size);
134  }
135 
136 fail:
137  ffio_free_dyn_buf(&sps_pb);
138  ffio_free_dyn_buf(&pps_pb);
139  ffio_free_dyn_buf(&sps_ext_pb);
140  av_free(start);
141 
142  return ret;
143 }
144 
145 int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
146 {
147  uint16_t sps_size, pps_size;
148  uint8_t *out;
149  int out_size;
150 
151  *buf = NULL;
152  if (*size >= 4 && (AV_RB32(in) == 0x00000001 || AV_RB24(in) == 0x000001))
153  return 0;
154  if (*size < 11 || in[0] != 1)
155  return AVERROR_INVALIDDATA;
156 
157  sps_size = AV_RB16(&in[6]);
158  if (11 + sps_size > *size)
159  return AVERROR_INVALIDDATA;
160  pps_size = AV_RB16(&in[9 + sps_size]);
161  if (11 + sps_size + pps_size > *size)
162  return AVERROR_INVALIDDATA;
163  out_size = 8 + sps_size + pps_size;
165  if (!out)
166  return AVERROR(ENOMEM);
167  AV_WB32(&out[0], 0x00000001);
168  memcpy(out + 4, &in[8], sps_size);
169  AV_WB32(&out[4 + sps_size], 0x00000001);
170  memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size);
171  *buf = out;
172  *size = out_size;
173  return 0;
174 }
175 
177  { 0, 1 },
178  { 1, 1 },
179  { 12, 11 },
180  { 10, 11 },
181  { 16, 11 },
182  { 40, 33 },
183  { 24, 11 },
184  { 20, 11 },
185  { 32, 11 },
186  { 80, 33 },
187  { 18, 11 },
188  { 15, 11 },
189  { 64, 33 },
190  { 160, 99 },
191  { 4, 3 },
192  { 3, 2 },
193  { 2, 1 },
194 };
195 
196 int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size)
197 {
198  int i, j, ret, rbsp_size, aspect_ratio_idc, pic_order_cnt_type;
199  int num_ref_frames_in_pic_order_cnt_cycle;
200  int delta_scale, lastScale = 8, nextScale = 8;
201  int sizeOfScalingList;
202  GetBitContext gb;
203  uint8_t *rbsp_buf;
204 
205  rbsp_buf = ff_nal_unit_extract_rbsp(buf, buf_size, &rbsp_size, 0);
206  if (!rbsp_buf)
207  return AVERROR(ENOMEM);
208 
209  ret = init_get_bits8(&gb, rbsp_buf, rbsp_size);
210  if (ret < 0)
211  goto end;
212 
213  memset(sps, 0, sizeof(*sps));
214 
215  sps->profile_idc = get_bits(&gb, 8);
216  sps->constraint_set_flags |= get_bits1(&gb) << 0; // constraint_set0_flag
217  sps->constraint_set_flags |= get_bits1(&gb) << 1; // constraint_set1_flag
218  sps->constraint_set_flags |= get_bits1(&gb) << 2; // constraint_set2_flag
219  sps->constraint_set_flags |= get_bits1(&gb) << 3; // constraint_set3_flag
220  sps->constraint_set_flags |= get_bits1(&gb) << 4; // constraint_set4_flag
221  sps->constraint_set_flags |= get_bits1(&gb) << 5; // constraint_set5_flag
222  skip_bits(&gb, 2); // reserved_zero_2bits
223  sps->level_idc = get_bits(&gb, 8);
224  sps->id = get_ue_golomb_long(&gb);
225 
226  if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
227  sps->profile_idc == 122 || sps->profile_idc == 244 || sps->profile_idc == 44 ||
228  sps->profile_idc == 83 || sps->profile_idc == 86 || sps->profile_idc == 118 ||
229  sps->profile_idc == 128 || sps->profile_idc == 138 || sps->profile_idc == 139 ||
230  sps->profile_idc == 134) {
231  sps->chroma_format_idc = get_ue_golomb_long(&gb); // chroma_format_idc
232  if (sps->chroma_format_idc == 3) {
233  skip_bits1(&gb); // separate_colour_plane_flag
234  }
235  sps->bit_depth_luma = get_ue_golomb_long(&gb) + 8;
236  sps->bit_depth_chroma = get_ue_golomb_long(&gb) + 8;
237  skip_bits1(&gb); // qpprime_y_zero_transform_bypass_flag
238  if (get_bits1(&gb)) { // seq_scaling_matrix_present_flag
239  for (i = 0; i < ((sps->chroma_format_idc != 3) ? 8 : 12); i++) {
240  if (!get_bits1(&gb)) // seq_scaling_list_present_flag
241  continue;
242  lastScale = 8;
243  nextScale = 8;
244  sizeOfScalingList = i < 6 ? 16 : 64;
245  for (j = 0; j < sizeOfScalingList; j++) {
246  if (nextScale != 0) {
247  delta_scale = get_se_golomb_long(&gb);
248  nextScale = (lastScale + delta_scale) & 0xff;
249  }
250  lastScale = nextScale == 0 ? lastScale : nextScale;
251  }
252  }
253  }
254  } else {
255  sps->chroma_format_idc = 1;
256  sps->bit_depth_luma = 8;
257  sps->bit_depth_chroma = 8;
258  }
259 
260  get_ue_golomb_long(&gb); // log2_max_frame_num_minus4
261  pic_order_cnt_type = get_ue_golomb_long(&gb);
262 
263  if (pic_order_cnt_type == 0) {
264  get_ue_golomb_long(&gb); // log2_max_pic_order_cnt_lsb_minus4
265  } else if (pic_order_cnt_type == 1) {
266  skip_bits1(&gb); // delta_pic_order_always_zero
267  get_se_golomb_long(&gb); // offset_for_non_ref_pic
268  get_se_golomb_long(&gb); // offset_for_top_to_bottom_field
269  num_ref_frames_in_pic_order_cnt_cycle = get_ue_golomb_long(&gb);
270  for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++)
271  get_se_golomb_long(&gb); // offset_for_ref_frame
272  }
273 
274  get_ue_golomb_long(&gb); // max_num_ref_frames
275  skip_bits1(&gb); // gaps_in_frame_num_value_allowed_flag
276  get_ue_golomb_long(&gb); // pic_width_in_mbs_minus1
277  get_ue_golomb_long(&gb); // pic_height_in_map_units_minus1
278 
279  sps->frame_mbs_only_flag = get_bits1(&gb);
280  if (!sps->frame_mbs_only_flag)
281  skip_bits1(&gb); // mb_adaptive_frame_field_flag
282 
283  skip_bits1(&gb); // direct_8x8_inference_flag
284 
285  if (get_bits1(&gb)) { // frame_cropping_flag
286  get_ue_golomb_long(&gb); // frame_crop_left_offset
287  get_ue_golomb_long(&gb); // frame_crop_right_offset
288  get_ue_golomb_long(&gb); // frame_crop_top_offset
289  get_ue_golomb_long(&gb); // frame_crop_bottom_offset
290  }
291 
292  if (get_bits1(&gb)) { // vui_parameters_present_flag
293  if (get_bits1(&gb)) { // aspect_ratio_info_present_flag
294  aspect_ratio_idc = get_bits(&gb, 8);
295  if (aspect_ratio_idc == 0xff) {
296  sps->sar.num = get_bits(&gb, 16);
297  sps->sar.den = get_bits(&gb, 16);
298  } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(avc_sample_aspect_ratio)) {
299  sps->sar = avc_sample_aspect_ratio[aspect_ratio_idc];
300  }
301  }
302  }
303 
304  if (!sps->sar.den) {
305  sps->sar.num = 1;
306  sps->sar.den = 1;
307  }
308 
309  ret = 0;
310  end:
311  av_free(rbsp_buf);
312  return ret;
313 }
H264SPS
Definition: avc.h:32
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
out
static FILE * out
Definition: movenc.c:55
H264SPS::bit_depth_chroma
uint8_t bit_depth_chroma
Definition: avc.h:39
get_se_golomb_long
static int get_se_golomb_long(GetBitContext *gb)
Definition: golomb.h:294
out_size
static int out_size
Definition: movenc.c:56
data
const char data[16]
Definition: mxf.c:149
avio_get_dyn_buf
int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1377
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:383
golomb.h
exp golomb vlc stuff
H264SPS::bit_depth_luma
uint8_t bit_depth_luma
Definition: avc.h:38
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:337
fail
#define fail()
Definition: checkasm.h:225
GetBitContext
Definition: get_bits.h:109
ff_avc_decode_sps
int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size)
Definition: avc.c:196
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:544
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1365
intreadwrite.h
H264_MAX_SPS_COUNT
@ H264_MAX_SPS_COUNT
Definition: h264.h:71
get_bits.h
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.h:31
H264_MAX_PPS_COUNT
@ H264_MAX_PPS_COUNT
Definition: h264.h:73
NULL
#define NULL
Definition: coverity.c:32
avc_sample_aspect_ratio
static const AVRational avc_sample_aspect_ratio[17]
Definition: avc.c:176
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
ff_nal_unit_extract_rbsp
uint8_t * ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len, uint32_t *dst_len, int header_len)
Definition: nal.c:163
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:391
avc.h
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:184
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:415
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
ff_isom_write_avcc
int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
Definition: avc.c:32
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
ff_nal_parse_units_buf
int ff_nal_parse_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: nal.c:133
size
int size
Definition: twinvq_data.h:10344
avio.h
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:206
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:416
avio_internal.h
H264SPS::chroma_format_idc
uint8_t chroma_format_idc
Definition: avc.h:37
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
len
int len
Definition: vorbis_enc_data.h:426
nal.h
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1438
ret
ret
Definition: filter_design.txt:187
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
ff_avc_write_annexb_extradata
int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
Definition: avc.c:145
pps
uint64_t pps
Definition: dovi_rpuenc.c:36
mem.h
get_ue_golomb_long
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:104
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
h264.h
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:446
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:97
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98