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