FFmpeg
av1.c
Go to the documentation of this file.
1 /*
2  * AV1 helper functions for muxers
3  * Copyright (c) 2018 James Almer <jamrial@gmail.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/mem.h"
23 #include "libavcodec/av1.h"
24 #include "libavcodec/av1_parse.h"
25 #include "libavcodec/profiles.h"
26 #include "libavcodec/put_bits.h"
27 #include "av1.h"
28 #include "avio.h"
29 
31 {
32  const uint8_t *end = buf + size;
33  int64_t obu_size;
34  int start_pos, type, temporal_id, spatial_id;
35 
36  size = 0;
37  while (buf < end) {
38  int len = parse_obu_header(buf, end - buf, &obu_size, &start_pos,
39  &type, &temporal_id, &spatial_id);
40  if (len < 0)
41  return len;
42 
43  switch (type) {
46  case AV1_OBU_TILE_LIST:
47  case AV1_OBU_PADDING:
48  break;
49  default:
50  avio_write(pb, buf, len);
51  size += len;
52  break;
53  }
54  buf += len;
55  }
56 
57  return size;
58 }
59 
61 {
62  AVIOContext *pb;
63  int ret;
64 
65  ret = avio_open_dyn_buf(&pb);
66  if (ret < 0)
67  return ret;
68 
69  ret = ff_av1_filter_obus(pb, buf, *size);
70  if (ret < 0)
71  return ret;
72 
73  av_freep(out);
74  *size = avio_close_dyn_buf(pb, out);
75 
76  return ret;
77 }
78 
79 typedef struct AV1SequenceParameters {
90 
91 static inline void uvlc(GetBitContext *gb)
92 {
93  int leading_zeros = 0;
94 
95  while (get_bits_left(gb)) {
96  if (get_bits1(gb))
97  break;
98  leading_zeros++;
99  }
100 
101  if (leading_zeros >= 32)
102  return;
103 
104  skip_bits_long(gb, leading_zeros);
105 }
106 
108 {
109  int color_primaries, transfer_characteristics, matrix_coefficients;
110 
111  seq_params->high_bitdepth = get_bits1(gb);
112  if (seq_params->seq_profile == FF_PROFILE_AV1_PROFESSIONAL && seq_params->high_bitdepth)
113  seq_params->twelve_bit = get_bits1(gb);
114 
115  if (seq_params->seq_profile == FF_PROFILE_AV1_HIGH)
116  seq_params->monochrome = 0;
117  else
118  seq_params->monochrome = get_bits1(gb);
119 
120  if (get_bits1(gb)) { // color_description_present_flag
121  color_primaries = get_bits(gb, 8);
122  transfer_characteristics = get_bits(gb, 8);
123  matrix_coefficients = get_bits(gb, 8);
124  } else {
125  color_primaries = AVCOL_PRI_UNSPECIFIED;
126  transfer_characteristics = AVCOL_TRC_UNSPECIFIED;
127  matrix_coefficients = AVCOL_SPC_UNSPECIFIED;
128  }
129 
130  if (seq_params->monochrome) {
131  skip_bits1(gb); // color_range
132  seq_params->chroma_subsampling_x = 1;
133  seq_params->chroma_subsampling_y = 1;
134  seq_params->chroma_sample_position = 0;
135  return 0;
136  } else if (color_primaries == AVCOL_PRI_BT709 &&
137  transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
138  matrix_coefficients == AVCOL_SPC_RGB) {
139  seq_params->chroma_subsampling_x = 0;
140  seq_params->chroma_subsampling_y = 0;
141  } else {
142  skip_bits1(gb); // color_range
143 
144  if (seq_params->seq_profile == FF_PROFILE_AV1_MAIN) {
145  seq_params->chroma_subsampling_x = 1;
146  seq_params->chroma_subsampling_y = 1;
147  } else if (seq_params->seq_profile == FF_PROFILE_AV1_HIGH) {
148  seq_params->chroma_subsampling_x = 0;
149  seq_params->chroma_subsampling_y = 0;
150  } else {
151  if (seq_params->twelve_bit) {
152  seq_params->chroma_subsampling_x = get_bits1(gb);
153  if (seq_params->chroma_subsampling_x)
154  seq_params->chroma_subsampling_y = get_bits1(gb);
155  else
156  seq_params->chroma_subsampling_y = 0;
157  } else {
158  seq_params->chroma_subsampling_x = 1;
159  seq_params->chroma_subsampling_y = 0;
160  }
161  }
162  if (seq_params->chroma_subsampling_x && seq_params->chroma_subsampling_y)
163  seq_params->chroma_sample_position = get_bits(gb, 2);
164  }
165 
166  skip_bits1(gb); // separate_uv_delta_q
167 
168  return 0;
169 }
170 
171 static int parse_sequence_header(AV1SequenceParameters *seq_params, const uint8_t *buf, int size)
172 {
173  GetBitContext gb;
174  int reduced_still_picture_header;
175  int frame_width_bits_minus_1, frame_height_bits_minus_1;
176  int size_bits, ret;
177 
178  size_bits = get_obu_bit_length(buf, size, AV1_OBU_SEQUENCE_HEADER);
179  if (size_bits < 0)
180  return size_bits;
181 
182  ret = init_get_bits(&gb, buf, size_bits);
183  if (ret < 0)
184  return ret;
185 
186  memset(seq_params, 0, sizeof(*seq_params));
187 
188  seq_params->seq_profile = get_bits(&gb, 3);
189 
190  skip_bits1(&gb); // still_picture
191  reduced_still_picture_header = get_bits1(&gb);
192 
193  if (reduced_still_picture_header) {
194  seq_params->seq_level_idx_0 = get_bits(&gb, 5);
195  seq_params->seq_tier_0 = 0;
196  } else {
197  int initial_display_delay_present_flag, operating_points_cnt_minus_1;
198  int decoder_model_info_present_flag, buffer_delay_length_minus_1;
199 
200  if (get_bits1(&gb)) { // timing_info_present_flag
201  skip_bits_long(&gb, 32); // num_units_in_display_tick
202  skip_bits_long(&gb, 32); // time_scale
203 
204  if (get_bits1(&gb)) // equal_picture_interval
205  uvlc(&gb); // num_ticks_per_picture_minus_1
206 
207  decoder_model_info_present_flag = get_bits1(&gb);
208  if (decoder_model_info_present_flag) {
209  buffer_delay_length_minus_1 = get_bits(&gb, 5);
210  skip_bits_long(&gb, 32); // num_units_in_decoding_tick
211  skip_bits(&gb, 10); // buffer_removal_time_length_minus_1 (5)
212  // frame_presentation_time_length_minus_1 (5)
213  }
214  } else
215  decoder_model_info_present_flag = 0;
216 
217  initial_display_delay_present_flag = get_bits1(&gb);
218 
219  operating_points_cnt_minus_1 = get_bits(&gb, 5);
220  for (int i = 0; i <= operating_points_cnt_minus_1; i++) {
221  int seq_level_idx, seq_tier;
222 
223  skip_bits(&gb, 12); // operating_point_idc
224  seq_level_idx = get_bits(&gb, 5);
225 
226  if (seq_level_idx > 7)
227  seq_tier = get_bits1(&gb);
228  else
229  seq_tier = 0;
230 
231  if (decoder_model_info_present_flag) {
232  if (get_bits1(&gb)) { // decoder_model_present_for_this_op
233  skip_bits_long(&gb, buffer_delay_length_minus_1 + 1); // decoder_buffer_delay
234  skip_bits_long(&gb, buffer_delay_length_minus_1 + 1); // encoder_buffer_delay
235  skip_bits1(&gb); // low_delay_mode_flag
236  }
237  }
238 
239  if (initial_display_delay_present_flag) {
240  if (get_bits1(&gb)) // initial_display_delay_present_for_this_op
241  skip_bits(&gb, 4); // initial_display_delay_minus_1
242  }
243 
244  if (i == 0) {
245  seq_params->seq_level_idx_0 = seq_level_idx;
246  seq_params->seq_tier_0 = seq_tier;
247  }
248  }
249  }
250 
251  frame_width_bits_minus_1 = get_bits(&gb, 4);
252  frame_height_bits_minus_1 = get_bits(&gb, 4);
253 
254  skip_bits(&gb, frame_width_bits_minus_1 + 1); // max_frame_width_minus_1
255  skip_bits(&gb, frame_height_bits_minus_1 + 1); // max_frame_height_minus_1
256 
257  if (!reduced_still_picture_header) {
258  if (get_bits1(&gb)) // frame_id_numbers_present_flag
259  skip_bits(&gb, 7); // delta_frame_id_length_minus_2 (4), additional_frame_id_length_minus_1 (3)
260  }
261 
262  skip_bits(&gb, 3); // use_128x128_superblock (1), enable_filter_intra (1), enable_intra_edge_filter (1)
263 
264  if (!reduced_still_picture_header) {
265  int enable_order_hint, seq_force_screen_content_tools;
266 
267  skip_bits(&gb, 4); // enable_intraintra_compound (1), enable_masked_compound (1)
268  // enable_warped_motion (1), enable_dual_filter (1)
269 
270  enable_order_hint = get_bits1(&gb);
271  if (enable_order_hint)
272  skip_bits(&gb, 2); // enable_jnt_comp (1), enable_ref_frame_mvs (1)
273 
274  if (get_bits1(&gb)) // seq_choose_screen_content_tools
275  seq_force_screen_content_tools = 2;
276  else
277  seq_force_screen_content_tools = get_bits1(&gb);
278 
279  if (seq_force_screen_content_tools) {
280  if (!get_bits1(&gb)) // seq_choose_integer_mv
281  skip_bits1(&gb); // seq_force_integer_mv
282  }
283 
284  if (enable_order_hint)
285  skip_bits(&gb, 3); // order_hint_bits_minus_1
286  }
287 
288  skip_bits(&gb, 3); // enable_superres (1), enable_cdef (1), enable_restoration (1)
289 
290  parse_color_config(seq_params, &gb);
291 
292  skip_bits1(&gb); // film_grain_params_present
293 
294  if (get_bits_left(&gb))
295  return AVERROR_INVALIDDATA;
296 
297  return 0;
298 }
299 
301 {
302  AVIOContext *seq_pb = NULL, *meta_pb = NULL;
303  AV1SequenceParameters seq_params;
304  PutBitContext pbc;
305  uint8_t header[4];
306  uint8_t *seq = NULL, *meta = NULL;
307  int64_t obu_size;
308  int start_pos, type, temporal_id, spatial_id;
309  int ret, nb_seq = 0, seq_size, meta_size;
310 
311  if (size <= 0)
312  return AVERROR_INVALIDDATA;
313 
314  ret = avio_open_dyn_buf(&seq_pb);
315  if (ret < 0)
316  return ret;
317  ret = avio_open_dyn_buf(&meta_pb);
318  if (ret < 0)
319  goto fail;
320 
321  while (size > 0) {
322  int len = parse_obu_header(buf, size, &obu_size, &start_pos,
323  &type, &temporal_id, &spatial_id);
324  if (len < 0) {
325  ret = len;
326  goto fail;
327  }
328 
329  switch (type) {
331  nb_seq++;
332  if (!obu_size || nb_seq > 1) {
333  ret = AVERROR_INVALIDDATA;
334  goto fail;
335  }
336  ret = parse_sequence_header(&seq_params, buf + start_pos, obu_size);
337  if (ret < 0)
338  goto fail;
339 
340  avio_write(seq_pb, buf, len);
341  break;
342  case AV1_OBU_METADATA:
343  if (!obu_size) {
344  ret = AVERROR_INVALIDDATA;
345  goto fail;
346  }
347  avio_write(meta_pb, buf, len);
348  break;
349  default:
350  break;
351  }
352  size -= len;
353  buf += len;
354  }
355 
356  seq_size = avio_close_dyn_buf(seq_pb, &seq);
357  if (!seq_size) {
358  ret = AVERROR_INVALIDDATA;
359  goto fail;
360  }
361 
362  init_put_bits(&pbc, header, sizeof(header));
363 
364  put_bits(&pbc, 1, 1); // marker
365  put_bits(&pbc, 7, 1); // version
366  put_bits(&pbc, 3, seq_params.seq_profile);
367  put_bits(&pbc, 5, seq_params.seq_level_idx_0);
368  put_bits(&pbc, 1, seq_params.seq_tier_0);
369  put_bits(&pbc, 1, seq_params.high_bitdepth);
370  put_bits(&pbc, 1, seq_params.twelve_bit);
371  put_bits(&pbc, 1, seq_params.monochrome);
372  put_bits(&pbc, 1, seq_params.chroma_subsampling_x);
373  put_bits(&pbc, 1, seq_params.chroma_subsampling_y);
374  put_bits(&pbc, 2, seq_params.chroma_sample_position);
375  put_bits(&pbc, 8, 0); // padding
376  flush_put_bits(&pbc);
377 
378  avio_write(pb, header, sizeof(header));
379  avio_write(pb, seq, seq_size);
380 
381  meta_size = avio_close_dyn_buf(meta_pb, &meta);
382  if (meta_size)
383  avio_write(pb, meta, meta_size);
384 
385 fail:
386  if (!seq)
387  avio_close_dyn_buf(seq_pb, &seq);
388  if (!meta)
389  avio_close_dyn_buf(meta_pb, &meta);
390  av_free(seq);
391  av_free(meta);
392 
393  return ret;
394 }
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
Buffered I/O operations.
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1420
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
Memory handling functions.
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
int ff_av1_filter_obus(AVIOContext *pb, const uint8_t *buf, int size)
Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and write the resulting bitstream ...
Definition: av1.c:30
GLint GLenum type
Definition: opengl_enc.c:104
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1391
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:497
uint8_t chroma_sample_position
Definition: av1.c:88
uint8_t monochrome
Definition: av1.c:85
uint8_t chroma_subsampling_x
Definition: av1.c:86
uint8_t seq_tier_0
Definition: av1.c:82
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size)
Writes AV1 extradata (Sequence Header and Metadata OBUs) to the provided AVIOContext.
Definition: av1.c:300
ptrdiff_t size
Definition: opengl_enc.c:100
static const uint8_t header[24]
Definition: sdr2.c:67
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:260
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:445
static int parse_sequence_header(AV1SequenceParameters *seq_params, const uint8_t *buf, int size)
Definition: av1.c:171
#define fail()
Definition: checkasm.h:120
static const struct TransferCharacteristics transfer_characteristics[AVCOL_TRC_NB]
static int parse_obu_header(const uint8_t *buf, int buf_size, int64_t *obu_size, int *start_pos, int *type, int *temporal_id, int *spatial_id)
Definition: av1_parse.h:99
static const struct ColorPrimaries color_primaries[AVCOL_PRI_NB]
uint8_t twelve_bit
Definition: av1.c:84
static void uvlc(GetBitContext *gb)
Definition: av1.c:91
int ff_av1_filter_obus_buf(const uint8_t *buf, uint8_t **out, int *size)
Filter out AV1 OBUs not meant to be present in ISOBMFF sample data and write the resulting bitstream ...
Definition: av1.c:60
void * buf
Definition: avisynth_c.h:766
AV1 common definitions.
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
uint8_t seq_level_idx_0
Definition: av1.c:81
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
#define FF_PROFILE_AV1_PROFESSIONAL
Definition: avcodec.h:2989
#define FF_PROFILE_AV1_MAIN
Definition: avcodec.h:2987
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
static int parse_color_config(AV1SequenceParameters *seq_params, GetBitContext *gb)
Definition: av1.c:107
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:481
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
static int get_obu_bit_length(const uint8_t *buf, int size, int type)
Definition: av1_parse.h:143
uint8_t chroma_subsampling_y
Definition: av1.c:87
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
#define av_free(p)
int len
uint8_t seq_profile
Definition: av1.c:80
FILE * out
Definition: movenc.c:54
#define av_freep(p)
uint8_t high_bitdepth
Definition: av1.c:83
#define FF_PROFILE_AV1_HIGH
Definition: avcodec.h:2988
bitstream writer API