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 static inline void uvlc(GetBitContext *gb)
80 {
81  int leading_zeros = 0;
82 
83  while (get_bits_left(gb)) {
84  if (get_bits1(gb))
85  break;
86  leading_zeros++;
87  }
88 
89  if (leading_zeros >= 32)
90  return;
91 
92  skip_bits_long(gb, leading_zeros);
93 }
94 
96 {
97  int twelve_bit = 0;
98  int high_bitdepth = get_bits1(gb);
99  if (seq_params->profile == FF_PROFILE_AV1_PROFESSIONAL && high_bitdepth)
100  twelve_bit = get_bits1(gb);
101 
102  seq_params->bitdepth = 8 + (high_bitdepth * 2) + (twelve_bit * 2);
103 
104  if (seq_params->profile == FF_PROFILE_AV1_HIGH)
105  seq_params->monochrome = 0;
106  else
107  seq_params->monochrome = get_bits1(gb);
108 
109  seq_params->color_description_present_flag = get_bits1(gb);
110  if (seq_params->color_description_present_flag) {
111  seq_params->color_primaries = get_bits(gb, 8);
112  seq_params->transfer_characteristics = get_bits(gb, 8);
113  seq_params->matrix_coefficients = get_bits(gb, 8);
114  } else {
118  }
119 
120  if (seq_params->monochrome) {
121  seq_params->color_range = get_bits1(gb);
122  seq_params->chroma_subsampling_x = 1;
123  seq_params->chroma_subsampling_y = 1;
124  seq_params->chroma_sample_position = 0;
125  return 0;
126  } else if (seq_params->color_primaries == AVCOL_PRI_BT709 &&
128  seq_params->matrix_coefficients == AVCOL_SPC_RGB) {
129  seq_params->chroma_subsampling_x = 0;
130  seq_params->chroma_subsampling_y = 0;
131  } else {
132  seq_params->color_range = get_bits1(gb);
133 
134  if (seq_params->profile == FF_PROFILE_AV1_MAIN) {
135  seq_params->chroma_subsampling_x = 1;
136  seq_params->chroma_subsampling_y = 1;
137  } else if (seq_params->profile == FF_PROFILE_AV1_HIGH) {
138  seq_params->chroma_subsampling_x = 0;
139  seq_params->chroma_subsampling_y = 0;
140  } else {
141  if (twelve_bit) {
142  seq_params->chroma_subsampling_x = get_bits1(gb);
143  if (seq_params->chroma_subsampling_x)
144  seq_params->chroma_subsampling_y = get_bits1(gb);
145  else
146  seq_params->chroma_subsampling_y = 0;
147  } else {
148  seq_params->chroma_subsampling_x = 1;
149  seq_params->chroma_subsampling_y = 0;
150  }
151  }
152  if (seq_params->chroma_subsampling_x && seq_params->chroma_subsampling_y)
153  seq_params->chroma_sample_position = get_bits(gb, 2);
154  }
155 
156  skip_bits1(gb); // separate_uv_delta_q
157 
158  return 0;
159 }
160 
161 static int parse_sequence_header(AV1SequenceParameters *seq_params, const uint8_t *buf, int size)
162 {
163  GetBitContext gb;
164  int reduced_still_picture_header;
165  int frame_width_bits_minus_1, frame_height_bits_minus_1;
166  int size_bits, ret;
167 
168  size_bits = get_obu_bit_length(buf, size, AV1_OBU_SEQUENCE_HEADER);
169  if (size_bits < 0)
170  return size_bits;
171 
172  ret = init_get_bits(&gb, buf, size_bits);
173  if (ret < 0)
174  return ret;
175 
176  memset(seq_params, 0, sizeof(*seq_params));
177 
178  seq_params->profile = get_bits(&gb, 3);
179 
180  skip_bits1(&gb); // still_picture
181  reduced_still_picture_header = get_bits1(&gb);
182 
183  if (reduced_still_picture_header) {
184  seq_params->level = get_bits(&gb, 5);
185  seq_params->tier = 0;
186  } else {
187  int initial_display_delay_present_flag, operating_points_cnt_minus_1;
188  int decoder_model_info_present_flag, buffer_delay_length_minus_1;
189 
190  if (get_bits1(&gb)) { // timing_info_present_flag
191  skip_bits_long(&gb, 32); // num_units_in_display_tick
192  skip_bits_long(&gb, 32); // time_scale
193 
194  if (get_bits1(&gb)) // equal_picture_interval
195  uvlc(&gb); // num_ticks_per_picture_minus_1
196 
197  decoder_model_info_present_flag = get_bits1(&gb);
198  if (decoder_model_info_present_flag) {
199  buffer_delay_length_minus_1 = get_bits(&gb, 5);
200  skip_bits_long(&gb, 32); // num_units_in_decoding_tick
201  skip_bits(&gb, 10); // buffer_removal_time_length_minus_1 (5)
202  // frame_presentation_time_length_minus_1 (5)
203  }
204  } else
205  decoder_model_info_present_flag = 0;
206 
207  initial_display_delay_present_flag = get_bits1(&gb);
208 
209  operating_points_cnt_minus_1 = get_bits(&gb, 5);
210  for (int i = 0; i <= operating_points_cnt_minus_1; i++) {
211  int seq_level_idx, seq_tier;
212 
213  skip_bits(&gb, 12); // operating_point_idc
214  seq_level_idx = get_bits(&gb, 5);
215 
216  if (seq_level_idx > 7)
217  seq_tier = get_bits1(&gb);
218  else
219  seq_tier = 0;
220 
221  if (decoder_model_info_present_flag) {
222  if (get_bits1(&gb)) { // decoder_model_present_for_this_op
223  skip_bits_long(&gb, buffer_delay_length_minus_1 + 1); // decoder_buffer_delay
224  skip_bits_long(&gb, buffer_delay_length_minus_1 + 1); // encoder_buffer_delay
225  skip_bits1(&gb); // low_delay_mode_flag
226  }
227  }
228 
229  if (initial_display_delay_present_flag) {
230  if (get_bits1(&gb)) // initial_display_delay_present_for_this_op
231  skip_bits(&gb, 4); // initial_display_delay_minus_1
232  }
233 
234  if (i == 0) {
235  seq_params->level = seq_level_idx;
236  seq_params->tier = seq_tier;
237  }
238  }
239  }
240 
241  frame_width_bits_minus_1 = get_bits(&gb, 4);
242  frame_height_bits_minus_1 = get_bits(&gb, 4);
243 
244  skip_bits(&gb, frame_width_bits_minus_1 + 1); // max_frame_width_minus_1
245  skip_bits(&gb, frame_height_bits_minus_1 + 1); // max_frame_height_minus_1
246 
247  if (!reduced_still_picture_header) {
248  if (get_bits1(&gb)) // frame_id_numbers_present_flag
249  skip_bits(&gb, 7); // delta_frame_id_length_minus_2 (4), additional_frame_id_length_minus_1 (3)
250  }
251 
252  skip_bits(&gb, 3); // use_128x128_superblock (1), enable_filter_intra (1), enable_intra_edge_filter (1)
253 
254  if (!reduced_still_picture_header) {
255  int enable_order_hint, seq_force_screen_content_tools;
256 
257  skip_bits(&gb, 4); // enable_intraintra_compound (1), enable_masked_compound (1)
258  // enable_warped_motion (1), enable_dual_filter (1)
259 
260  enable_order_hint = get_bits1(&gb);
261  if (enable_order_hint)
262  skip_bits(&gb, 2); // enable_jnt_comp (1), enable_ref_frame_mvs (1)
263 
264  if (get_bits1(&gb)) // seq_choose_screen_content_tools
265  seq_force_screen_content_tools = 2;
266  else
267  seq_force_screen_content_tools = get_bits1(&gb);
268 
269  if (seq_force_screen_content_tools) {
270  if (!get_bits1(&gb)) // seq_choose_integer_mv
271  skip_bits1(&gb); // seq_force_integer_mv
272  }
273 
274  if (enable_order_hint)
275  skip_bits(&gb, 3); // order_hint_bits_minus_1
276  }
277 
278  skip_bits(&gb, 3); // enable_superres (1), enable_cdef (1), enable_restoration (1)
279 
280  parse_color_config(seq_params, &gb);
281 
282  skip_bits1(&gb); // film_grain_params_present
283 
284  if (get_bits_left(&gb))
285  return AVERROR_INVALIDDATA;
286 
287  return 0;
288 }
289 
291 {
292  int64_t obu_size;
293  int start_pos, type, temporal_id, spatial_id;
294 
295  if (size <= 0)
296  return AVERROR_INVALIDDATA;
297 
298  while (size > 0) {
299  int len = parse_obu_header(buf, size, &obu_size, &start_pos,
300  &type, &temporal_id, &spatial_id);
301  if (len < 0)
302  return len;
303 
304  switch (type) {
306  if (!obu_size)
307  return AVERROR_INVALIDDATA;
308 
309  return parse_sequence_header(seq, buf + start_pos, obu_size);
310  default:
311  break;
312  }
313  size -= len;
314  buf += len;
315  }
316 
317  return AVERROR_INVALIDDATA;
318 }
319 
321 {
322  AVIOContext *seq_pb = NULL, *meta_pb = NULL;
323  AV1SequenceParameters seq_params;
324  PutBitContext pbc;
325  uint8_t header[4];
326  uint8_t *seq = NULL, *meta = NULL;
327  int64_t obu_size;
328  int start_pos, type, temporal_id, spatial_id;
329  int ret, nb_seq = 0, seq_size, meta_size;
330 
331  if (size <= 0)
332  return AVERROR_INVALIDDATA;
333 
334  ret = avio_open_dyn_buf(&seq_pb);
335  if (ret < 0)
336  return ret;
337  ret = avio_open_dyn_buf(&meta_pb);
338  if (ret < 0)
339  goto fail;
340 
341  while (size > 0) {
342  int len = parse_obu_header(buf, size, &obu_size, &start_pos,
343  &type, &temporal_id, &spatial_id);
344  if (len < 0) {
345  ret = len;
346  goto fail;
347  }
348 
349  switch (type) {
351  nb_seq++;
352  if (!obu_size || nb_seq > 1) {
353  ret = AVERROR_INVALIDDATA;
354  goto fail;
355  }
356  ret = parse_sequence_header(&seq_params, buf + start_pos, obu_size);
357  if (ret < 0)
358  goto fail;
359 
360  avio_write(seq_pb, buf, len);
361  break;
362  case AV1_OBU_METADATA:
363  if (!obu_size) {
364  ret = AVERROR_INVALIDDATA;
365  goto fail;
366  }
367  avio_write(meta_pb, buf, len);
368  break;
369  default:
370  break;
371  }
372  size -= len;
373  buf += len;
374  }
375 
376  seq_size = avio_close_dyn_buf(seq_pb, &seq);
377  if (!seq_size) {
378  ret = AVERROR_INVALIDDATA;
379  goto fail;
380  }
381 
382  init_put_bits(&pbc, header, sizeof(header));
383 
384  put_bits(&pbc, 1, 1); // marker
385  put_bits(&pbc, 7, 1); // version
386  put_bits(&pbc, 3, seq_params.profile);
387  put_bits(&pbc, 5, seq_params.level);
388  put_bits(&pbc, 1, seq_params.tier);
389  put_bits(&pbc, 1, seq_params.bitdepth > 8);
390  put_bits(&pbc, 1, seq_params.bitdepth == 12);
391  put_bits(&pbc, 1, seq_params.monochrome);
392  put_bits(&pbc, 1, seq_params.chroma_subsampling_x);
393  put_bits(&pbc, 1, seq_params.chroma_subsampling_y);
394  put_bits(&pbc, 2, seq_params.chroma_sample_position);
395  put_bits(&pbc, 8, 0); // padding
396  flush_put_bits(&pbc);
397 
398  avio_write(pb, header, sizeof(header));
399  avio_write(pb, seq, seq_size);
400 
401  meta_size = avio_close_dyn_buf(meta_pb, &meta);
402  if (meta_size)
403  avio_write(pb, meta, meta_size);
404 
405 fail:
406  if (!seq)
407  avio_close_dyn_buf(seq_pb, &seq);
408  if (!meta)
409  avio_close_dyn_buf(meta_pb, &meta);
410  av_free(seq);
411  av_free(meta);
412 
413  return ret;
414 }
#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:1459
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
uint8_t transfer_characteristics
Definition: av1.h:39
GLint GLenum type
Definition: opengl_enc.c:104
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1430
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB)
Definition: pixfmt.h:498
uint8_t chroma_sample_position
Definition: av1.h:36
uint8_t monochrome
Definition: av1.h:33
uint8_t chroma_subsampling_x
Definition: av1.h:34
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:320
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:259
uint8_t color_range
Definition: av1.h:41
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:161
#define fail()
Definition: checkasm.h:121
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
uint8_t color_description_present_flag
Definition: av1.h:37
static void uvlc(GetBitContext *gb)
Definition: av1.c:79
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.
uint8_t color_primaries
Definition: av1.h:38
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
#define FF_PROFILE_AV1_PROFESSIONAL
Definition: avcodec.h:2996
#define FF_PROFILE_AV1_MAIN
Definition: avcodec.h:2994
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:95
IEC 61966-2-1 (sRGB or sYCC)
Definition: pixfmt.h:482
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.h:35
int ff_av1_parse_seq_header(AV1SequenceParameters *seq, const uint8_t *buf, int size)
Parses a Sequence Header from the the provided buffer.
Definition: av1.c:290
uint8_t level
Definition: av1.h:30
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
uint8_t tier
Definition: av1.h:31
#define av_free(p)
int len
FILE * out
Definition: movenc.c:54
#define av_freep(p)
uint8_t matrix_coefficients
Definition: av1.h:40
uint8_t bitdepth
Definition: av1.h:32
#define FF_PROFILE_AV1_HIGH
Definition: avcodec.h:2995
uint8_t profile
Definition: av1.h:29
bitstream writer API