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 "avio.h"
27 #include "avc.h"
28 #include "avio_internal.h"
29 #include "nal.h"
30 
31 int ff_isom_write_avcc(AVIOContext *pb, const uint8_t *data, int len)
32 {
33  AVIOContext *sps_pb = NULL, *pps_pb = NULL, *sps_ext_pb = NULL;
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 
39  if (len <= 6)
40  return AVERROR_INVALIDDATA;
41 
42  /* check for H.264 start code */
43  if (AV_RB32(data) != 0x00000001 &&
44  AV_RB24(data) != 0x000001) {
45  avio_write(pb, data, len);
46  return 0;
47  }
48 
50  if (ret < 0)
51  return ret;
52  start = buf;
53  end = buf + len;
54 
55  ret = avio_open_dyn_buf(&sps_pb);
56  if (ret < 0)
57  goto fail;
58  ret = avio_open_dyn_buf(&pps_pb);
59  if (ret < 0)
60  goto fail;
61  ret = avio_open_dyn_buf(&sps_ext_pb);
62  if (ret < 0)
63  goto fail;
64 
65  /* look for sps and pps */
66  while (end - buf > 4) {
67  uint32_t size;
68  uint8_t nal_type;
69  size = FFMIN(AV_RB32(buf), end - buf - 4);
70  buf += 4;
71  nal_type = buf[0] & 0x1f;
72 
73  if (nal_type == 7) { /* SPS */
74  nb_sps++;
75  if (size > UINT16_MAX || nb_sps >= H264_MAX_SPS_COUNT) {
77  goto fail;
78  }
79  avio_wb16(sps_pb, size);
80  avio_write(sps_pb, buf, size);
81  } else if (nal_type == 8) { /* PPS */
82  nb_pps++;
83  if (size > UINT16_MAX || nb_pps >= H264_MAX_PPS_COUNT) {
85  goto fail;
86  }
87  avio_wb16(pps_pb, size);
88  avio_write(pps_pb, buf, size);
89  } else if (nal_type == 13) { /* SPS_EXT */
90  nb_sps_ext++;
91  if (size > UINT16_MAX || nb_sps_ext >= 256) {
93  goto fail;
94  }
95  avio_wb16(sps_ext_pb, size);
96  avio_write(sps_ext_pb, buf, size);
97  }
98 
99  buf += size;
100  }
101  sps_size = avio_get_dyn_buf(sps_pb, &sps);
102  pps_size = avio_get_dyn_buf(pps_pb, &pps);
103  sps_ext_size = avio_get_dyn_buf(sps_ext_pb, &sps_ext);
104 
105  if (sps_size < 6 || !pps_size) {
107  goto fail;
108  }
109 
110  avio_w8(pb, 1); /* version */
111  avio_w8(pb, sps[3]); /* profile */
112  avio_w8(pb, sps[4]); /* profile compat */
113  avio_w8(pb, sps[5]); /* level */
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 
117  avio_write(pb, sps, sps_size);
118  avio_w8(pb, nb_pps); /* number of pps */
119  avio_write(pb, pps, pps_size);
120 
121  if (sps[3] != 66 && sps[3] != 77 && sps[3] != 88) {
122  H264SPS seq;
123  ret = ff_avc_decode_sps(&seq, sps + 3, sps_size - 3);
124  if (ret < 0)
125  goto fail;
126 
127  avio_w8(pb, 0xfc | seq.chroma_format_idc); /* 6 bits reserved (111111) + chroma_format_idc */
128  avio_w8(pb, 0xf8 | (seq.bit_depth_luma - 8)); /* 5 bits reserved (11111) + bit_depth_luma_minus8 */
129  avio_w8(pb, 0xf8 | (seq.bit_depth_chroma - 8)); /* 5 bits reserved (11111) + bit_depth_chroma_minus8 */
130  avio_w8(pb, nb_sps_ext); /* number of sps ext */
131  if (nb_sps_ext)
132  avio_write(pb, sps_ext, sps_ext_size);
133  }
134 
135 fail:
136  ffio_free_dyn_buf(&sps_pb);
137  ffio_free_dyn_buf(&pps_pb);
138  ffio_free_dyn_buf(&sps_ext_pb);
139  av_free(start);
140 
141  return ret;
142 }
143 
144 int ff_avc_write_annexb_extradata(const uint8_t *in, uint8_t **buf, int *size)
145 {
146  uint16_t sps_size, pps_size;
147  uint8_t *out;
148  int out_size;
149 
150  *buf = NULL;
151  if (*size >= 4 && (AV_RB32(in) == 0x00000001 || AV_RB24(in) == 0x000001))
152  return 0;
153  if (*size < 11 || in[0] != 1)
154  return AVERROR_INVALIDDATA;
155 
156  sps_size = AV_RB16(&in[6]);
157  if (11 + sps_size > *size)
158  return AVERROR_INVALIDDATA;
159  pps_size = AV_RB16(&in[9 + sps_size]);
160  if (11 + sps_size + pps_size > *size)
161  return AVERROR_INVALIDDATA;
162  out_size = 8 + sps_size + pps_size;
164  if (!out)
165  return AVERROR(ENOMEM);
166  AV_WB32(&out[0], 0x00000001);
167  memcpy(out + 4, &in[8], sps_size);
168  AV_WB32(&out[4 + sps_size], 0x00000001);
169  memcpy(out + 8 + sps_size, &in[11 + sps_size], pps_size);
170  *buf = out;
171  *size = out_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 
195 static inline int get_ue_golomb(GetBitContext *gb) {
196  int i;
197  for (i = 0; i < 32 && !get_bits1(gb); i++)
198  ;
199  return get_bitsz(gb, i) + (1 << i) - 1;
200 }
201 
202 static inline int get_se_golomb(GetBitContext *gb) {
203  int v = get_ue_golomb(gb) + 1;
204  int sign = -(v & 1);
205  return ((v >> 1) ^ sign) - sign;
206 }
207 
208 int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size)
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;
214  GetBitContext gb;
215  uint8_t *rbsp_buf;
216 
217  rbsp_buf = ff_nal_unit_extract_rbsp(buf, buf_size, &rbsp_size, 0);
218  if (!rbsp_buf)
219  return AVERROR(ENOMEM);
220 
221  ret = init_get_bits8(&gb, rbsp_buf, rbsp_size);
222  if (ret < 0)
223  goto end;
224 
225  memset(sps, 0, sizeof(*sps));
226 
227  sps->profile_idc = get_bits(&gb, 8);
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
235  sps->level_idc = get_bits(&gb, 8);
236  sps->id = get_ue_golomb(&gb);
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) {
243  sps->chroma_format_idc = get_ue_golomb(&gb); // chroma_format_idc
244  if (sps->chroma_format_idc == 3) {
245  skip_bits1(&gb); // separate_colour_plane_flag
246  }
247  sps->bit_depth_luma = get_ue_golomb(&gb) + 8;
248  sps->bit_depth_chroma = get_ue_golomb(&gb) + 8;
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) {
259  delta_scale = get_se_golomb(&gb);
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 
272  get_ue_golomb(&gb); // log2_max_frame_num_minus4
273  pic_order_cnt_type = get_ue_golomb(&gb);
274 
275  if (pic_order_cnt_type == 0) {
276  get_ue_golomb(&gb); // log2_max_pic_order_cnt_lsb_minus4
277  } else if (pic_order_cnt_type == 1) {
278  skip_bits1(&gb); // delta_pic_order_always_zero
279  get_se_golomb(&gb); // offset_for_non_ref_pic
280  get_se_golomb(&gb); // offset_for_top_to_bottom_field
281  num_ref_frames_in_pic_order_cnt_cycle = get_ue_golomb(&gb);
282  for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++)
283  get_se_golomb(&gb); // offset_for_ref_frame
284  }
285 
286  get_ue_golomb(&gb); // max_num_ref_frames
287  skip_bits1(&gb); // gaps_in_frame_num_value_allowed_flag
288  get_ue_golomb(&gb); // pic_width_in_mbs_minus1
289  get_ue_golomb(&gb); // pic_height_in_map_units_minus1
290 
291  sps->frame_mbs_only_flag = get_bits1(&gb);
292  if (!sps->frame_mbs_only_flag)
293  skip_bits1(&gb); // mb_adaptive_frame_field_flag
294 
295  skip_bits1(&gb); // direct_8x8_inference_flag
296 
297  if (get_bits1(&gb)) { // frame_cropping_flag
298  get_ue_golomb(&gb); // frame_crop_left_offset
299  get_ue_golomb(&gb); // frame_crop_right_offset
300  get_ue_golomb(&gb); // frame_crop_top_offset
301  get_ue_golomb(&gb); // frame_crop_bottom_offset
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) {
308  sps->sar.num = get_bits(&gb, 16);
309  sps->sar.den = get_bits(&gb, 16);
310  } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(avc_sample_aspect_ratio)) {
311  sps->sar = avc_sample_aspect_ratio[aspect_ratio_idc];
312  }
313  }
314  }
315 
316  if (!sps->sar.den) {
317  sps->sar.num = 1;
318  sps->sar.den = 1;
319  }
320 
321  ret = 0;
322  end:
323  av_free(rbsp_buf);
324  return ret;
325 }
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
FILE * out
Definition: movenc.c:55
H264SPS::bit_depth_chroma
uint8_t bit_depth_chroma
Definition: avc.h:39
out_size
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:1374
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
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:335
fail
#define fail()
Definition: checkasm.h:189
GetBitContext
Definition: get_bits.h:108
ff_avc_decode_sps
int ff_avc_decode_sps(H264SPS *sps, const uint8_t *buf, int buf_size)
Definition: avc.c:208
get_se_golomb
static int get_se_golomb(GetBitContext *gb)
Definition: avc.c:202
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:545
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1362
intreadwrite.h
get_bits.h
NULL
#define NULL
Definition: coverity.c:32
avc_sample_aspect_ratio
static const AVRational avc_sample_aspect_ratio[17]
Definition: avc.c:175
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:160
get_ue_golomb
static int get_ue_golomb(GetBitContext *gb)
Definition: avc.c:195
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
avc.h
H264_MAX_SPS_COUNT
@ H264_MAX_SPS_COUNT
Definition: h264.h:71
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:179
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:31
ff_nal_parse_units_buf
int ff_nal_parse_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: nal.c:130
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:201
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:413
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
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
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
len
int len
Definition: vorbis_enc_data.h:426
nal.h
H264_MAX_PPS_COUNT
@ H264_MAX_PPS_COUNT
Definition: h264.h:73
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1435
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:144
pps
uint64_t pps
Definition: dovi_rpuenc.c:35
mem.h
get_bitsz
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:351
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
h264.h
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:443
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