FFmpeg
evc.c
Go to the documentation of this file.
1 /*
2  * EVC helper functions for muxers
3  * Copyright (c) 2022 Dawid Kozinski <d.kozinski@samsung.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 "libavcodec/get_bits.h"
24 #include "libavcodec/golomb.h"
25 #include "libavcodec/evc.h"
26 #include "avformat.h"
27 #include "avio.h"
28 #include "evc.h"
29 #include "avio_internal.h"
30 
31 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.1
32 enum {
38 };
39 
40 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
41 typedef struct EVCNALUnitArray {
42  uint8_t array_completeness; // when equal to 1 indicates that all NAL units of the given type are in the following array
43  uint8_t NAL_unit_type; // indicates the type of the NAL units in the following array
44  uint16_t numNalus; // indicates the number of NAL units of the indicated type
45  uint16_t *nalUnitLength; // indicates the length in bytes of the NAL unit
46  uint8_t **nalUnit; // contains an SPS, PPS, APS or a SEI NAL unit, as specified in ISO/IEC 23094-1
48 
49 /**
50  * @brief Specifies the decoder configuration information for ISO/IEC 23094-1 video content.
51  * @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.2
52  * Carriage of network abstraction layer (NAL) unit structured video in the ISO base media file format
53  */
55  uint8_t configurationVersion; // 8 bits
56  uint8_t profile_idc; // 8 bits
57  uint8_t level_idc; // 8 bits
58  uint32_t toolset_idc_h; // 32 bits
59  uint32_t toolset_idc_l; // 32 bits
60  uint8_t chroma_format_idc; // 2 bits
61  uint8_t bit_depth_luma_minus8; // 3 bits
62  uint8_t bit_depth_chroma_minus8; // 3 bits
63  uint16_t pic_width_in_luma_samples; // 16 bits
64  uint16_t pic_height_in_luma_samples; // 16 bits
65  uint8_t reserved; // 6 bits '000000'b
66  uint8_t lengthSizeMinusOne; // 2 bits
67  uint8_t num_of_arrays; // 8 bits
70 
71 typedef struct NALU {
72  int offset;
73  uint32_t size;
74 } NALU;
75 
76 typedef struct NALUList {
77  NALU *nalus;
78  unsigned nalus_array_size;
79  unsigned nb_nalus; ///< valid entries in nalus
80 } NALUList;
81 
82 // @see ISO_IEC_23094-1 (7.3.2.1 SPS RBSP syntax)
83 static int evcc_parse_sps(const uint8_t *bs, int bs_size, EVCDecoderConfigurationRecord *evcc)
84 {
85  GetBitContext gb;
86  unsigned sps_seq_parameter_set_id;
87  int ret;
88 
90  bs_size -= EVC_NALU_HEADER_SIZE;
91 
92  ret = init_get_bits8(&gb, bs, bs_size);
93  if (ret < 0)
94  return ret;
95 
96  sps_seq_parameter_set_id = get_ue_golomb_long(&gb);
97 
98  if (sps_seq_parameter_set_id >= EVC_MAX_SPS_COUNT)
99  return AVERROR_INVALIDDATA;
100 
101  // the Baseline profile is indicated by profile_idc eqal to 0
102  // the Main profile is indicated by profile_idc eqal to 1
103  evcc->profile_idc = get_bits(&gb, 8);
104 
105  evcc->level_idc = get_bits(&gb, 8);
106 
107  evcc->toolset_idc_h = get_bits_long(&gb, 32);
108  evcc->toolset_idc_l = get_bits_long(&gb, 32);
109 
110  // 0 - monochrome
111  // 1 - 4:2:0
112  // 2 - 4:2:2
113  // 3 - 4:4:4
115  if (evcc->chroma_format_idc > 3)
116  return AVERROR_INVALIDDATA;
117 
120 
123  // EVCDecoderConfigurationRecord can't store values > 7. Limit it to bit depth 14.
124  if (evcc->bit_depth_luma_minus8 > 6 || evcc->bit_depth_chroma_minus8 > 6)
125  return AVERROR_INVALIDDATA;
126 
127  return 0;
128 }
129 
130 // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
131 static int evcc_array_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size,
132  uint8_t nal_type, int ps_array_completeness,
134 {
135  int ret;
136  uint16_t numNalus = array->numNalus;
137 
138  ret = av_reallocp_array(&array->nalUnit, numNalus + 1, sizeof(uint8_t *));
139  if (ret < 0)
140  return ret;
141 
142  ret = av_reallocp_array(&array->nalUnitLength, numNalus + 1, sizeof(uint16_t));
143  if (ret < 0)
144  return ret;
145 
146  array->nalUnit [numNalus] = (uint8_t *)nal_buf;
147  array->nalUnitLength[numNalus] = nal_size;
148  array->NAL_unit_type = nal_type;
149  array->numNalus++;
150 
151  /*
152  * When the sample entry name is 'evc1', the default and mandatory value of
153  * array_completeness is 1 for arrays of all types of parameter sets, and 0
154  * for all other arrays.
155  */
156  if (nal_type == EVC_SPS_NUT || nal_type == EVC_PPS_NUT || nal_type == EVC_APS_NUT)
157  array->array_completeness = ps_array_completeness;
158 
159  return 0;
160 }
161 
163 {
164  memset(evcc, 0, sizeof(EVCDecoderConfigurationRecord));
165  evcc->configurationVersion = 1;
166  evcc->lengthSizeMinusOne = 3; // 4 bytes
167 }
168 
170 {
171  for (unsigned i = 0; i < FF_ARRAY_ELEMS(evcc->arrays); i++) {
172  EVCNALUnitArray *const array = &evcc->arrays[i];
173  array->numNalus = 0;
174  av_freep(&array->nalUnit);
175  av_freep(&array->nalUnitLength);
176  }
177 }
178 
180 {
181  uint16_t sps_count;
182 
183  av_log(NULL, AV_LOG_TRACE, "configurationVersion: %"PRIu8"\n",
184  evcc->configurationVersion);
185  av_log(NULL, AV_LOG_TRACE, "profile_idc: %"PRIu8"\n",
186  evcc->profile_idc);
187  av_log(NULL, AV_LOG_TRACE, "level_idc: %"PRIu8"\n",
188  evcc->level_idc);
189  av_log(NULL, AV_LOG_TRACE, "toolset_idc_h: %"PRIu32"\n",
190  evcc->toolset_idc_h);
191  av_log(NULL, AV_LOG_TRACE, "toolset_idc_l: %"PRIu32"\n",
192  evcc->toolset_idc_l);
193  av_log(NULL, AV_LOG_TRACE, "chroma_format_idc: %"PRIu8"\n",
194  evcc->chroma_format_idc);
195  av_log(NULL, AV_LOG_TRACE, "bit_depth_luma_minus8: %"PRIu8"\n",
196  evcc->bit_depth_luma_minus8);
197  av_log(NULL, AV_LOG_TRACE, "bit_depth_chroma_minus8: %"PRIu8"\n",
199  av_log(NULL, AV_LOG_TRACE, "pic_width_in_luma_samples: %"PRIu16"\n",
201  av_log(NULL, AV_LOG_TRACE, "pic_height_in_luma_samples: %"PRIu16"\n",
203  av_log(NULL, AV_LOG_TRACE, "lengthSizeMinusOne: %"PRIu8"\n",
204  evcc->lengthSizeMinusOne);
205  av_log(NULL, AV_LOG_TRACE, "num_of_arrays: %"PRIu8"\n",
206  evcc->num_of_arrays);
207  for (unsigned i = 0; i < FF_ARRAY_ELEMS(evcc->arrays); i++) {
208  const EVCNALUnitArray *const array = &evcc->arrays[i];
209 
210  if(array->numNalus == 0)
211  continue;
212 
213  av_log(NULL, AV_LOG_TRACE, "array_completeness[%"PRIu8"]: %"PRIu8"\n",
214  i, array->array_completeness);
215  av_log(NULL, AV_LOG_TRACE, "NAL_unit_type[%"PRIu8"]: %"PRIu8"\n",
216  i, array->NAL_unit_type);
217  av_log(NULL, AV_LOG_TRACE, "numNalus[%"PRIu8"]: %"PRIu16"\n",
218  i, array->numNalus);
219  for ( unsigned j = 0; j < array->numNalus; j++)
221  "nalUnitLength[%"PRIu8"][%"PRIu16"]: %"PRIu16"\n",
222  i, j, array->nalUnitLength[j]);
223  }
224 
225  /*
226  * We need at least one SPS.
227  */
228  sps_count = evcc->arrays[SPS_INDEX].numNalus;
229  if (!sps_count || sps_count > EVC_MAX_SPS_COUNT)
230  return AVERROR_INVALIDDATA;
231 
232  /* unsigned int(8) configurationVersion = 1; */
233  avio_w8(pb, evcc->configurationVersion);
234 
235  /* unsigned int(8) profile_idc */
236  avio_w8(pb, evcc->profile_idc);
237 
238  /* unsigned int(8) profile_idc */
239  avio_w8(pb, evcc->level_idc);
240 
241  /* unsigned int(32) toolset_idc_h */
242  avio_wb32(pb, evcc->toolset_idc_h);
243 
244  /* unsigned int(32) toolset_idc_l */
245  avio_wb32(pb, evcc->toolset_idc_l);
246 
247  /*
248  * unsigned int(2) chroma_format_idc;
249  * unsigned int(3) bit_depth_luma_minus8;
250  * unsigned int(3) bit_depth_chroma_minus8;
251  */
252  avio_w8(pb, evcc->chroma_format_idc << 6 |
253  evcc->bit_depth_luma_minus8 << 3 |
255 
256  /* unsigned int(16) pic_width_in_luma_samples; */
258 
259  /* unsigned int(16) pic_width_in_luma_samples; */
261 
262  /*
263  * bit(6) reserved = '111111'b;
264  * unsigned int(2) chromaFormat;
265  */
266  avio_w8(pb, evcc->lengthSizeMinusOne | 0xfc);
267 
268  /* unsigned int(8) numOfArrays; */
269  avio_w8(pb, evcc->num_of_arrays);
270 
271  for (unsigned i = 0; i < FF_ARRAY_ELEMS(evcc->arrays); i++) {
272  const EVCNALUnitArray *const array = &evcc->arrays[i];
273 
274  if (!array->numNalus)
275  continue;
276 
277  /*
278  * bit(1) array_completeness;
279  * unsigned int(1) reserved = 0;
280  * unsigned int(6) NAL_unit_type;
281  */
282  avio_w8(pb, array->array_completeness << 7 |
283  array->NAL_unit_type & 0x3f);
284 
285  /* unsigned int(16) numNalus; */
286  avio_wb16(pb, array->numNalus);
287 
288  for (unsigned j = 0; j < array->numNalus; j++) {
289  /* unsigned int(16) nalUnitLength; */
290  avio_wb16(pb, array->nalUnitLength[j]);
291 
292  /* bit(8*nalUnitLength) nalUnit; */
293  avio_write(pb, array->nalUnit[j],
294  array->nalUnitLength[j]);
295  }
296  }
297 
298  return 0;
299 }
300 
301 int ff_isom_write_evcc(AVIOContext *pb, const uint8_t *data,
302  int size, int ps_array_completeness)
303 {
305  int nalu_type;
306  size_t nalu_size;
307  int bytes_to_read = size;
308  unsigned array_index;
309 
310  int ret = 0;
311 
312  if (size < 8) {
313  /* We can't write a valid evcC from the provided data */
314  return AVERROR_INVALIDDATA;
315  } else if (*data == 1) {
316  /* Data is already evcC-formatted */
317  avio_write(pb, data, size);
318  return 0;
319  }
320 
321  evcc_init(&evcc);
322 
323  while (bytes_to_read > EVC_NALU_LENGTH_PREFIX_SIZE) {
325  if (nalu_size == 0) break;
326 
328  bytes_to_read -= EVC_NALU_LENGTH_PREFIX_SIZE;
329 
330  if (bytes_to_read < nalu_size) break;
331 
332  nalu_type = evc_get_nalu_type(data, bytes_to_read);
333  if (nalu_type < EVC_NOIDR_NUT || nalu_type > EVC_UNSPEC_NUT62) {
335  goto end;
336  }
337 
338  // @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.3
339  // NAL_unit_type indicates the type of the NAL units in the following array (which shall be all of that type);
340  // - it takes a value as defined in ISO/IEC 23094-1;
341  // - it is restricted to take one of the values indicating a SPS, PPS, APS, or SEI NAL unit.
342  switch (nalu_type) {
343  case EVC_SPS_NUT:
344  array_index = SPS_INDEX;
345  break;
346  case EVC_PPS_NUT:
347  array_index = PPS_INDEX;
348  break;
349  case EVC_APS_NUT:
350  array_index = APS_INDEX;
351  break;
352  case EVC_SEI_NUT:
353  array_index = SEI_INDEX;
354  break;
355  default:
356  array_index = -1;
357  break;
358  }
359 
360  if( (array_index == SPS_INDEX) ||
361  (array_index == PPS_INDEX) ||
362  (array_index == APS_INDEX) ||
363  (array_index == SEI_INDEX) ) {
364 
365  ret = evcc_array_add_nal_unit(data, nalu_size, nalu_type, ps_array_completeness, &(evcc.arrays[array_index]));
366 
367  if (ret < 0)
368  goto end;
369  if (evcc.arrays[array_index].numNalus == 1)
370  evcc.num_of_arrays++;
371 
372  if(nalu_type == EVC_SPS_NUT) {
373  ret = evcc_parse_sps(data, nalu_size, &evcc);
374  if (ret < 0)
375  goto end;
376  }
377  }
378 
379  data += nalu_size;
380  bytes_to_read -= nalu_size;
381  }
382 
383  ret = evcc_write(pb, &evcc);
384 
385 end:
386  evcc_close(&evcc);
387  return ret;
388 }
evcc_init
static void evcc_init(EVCDecoderConfigurationRecord *evcc)
Definition: evc.c:162
EVCDecoderConfigurationRecord::arrays
EVCNALUnitArray arrays[NB_ARRAYS]
Definition: evc.c:68
PPS_INDEX
@ PPS_INDEX
Definition: evc.c:34
EVCDecoderConfigurationRecord::toolset_idc_l
uint32_t toolset_idc_l
Definition: evc.c:59
EVC_APS_NUT
@ EVC_APS_NUT
Definition: evc.h:60
APS_INDEX
@ APS_INDEX
Definition: evc.c:35
EVCDecoderConfigurationRecord::num_of_arrays
uint8_t num_of_arrays
Definition: evc.c:67
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
data
const char data[16]
Definition: mxf.c:148
EVCDecoderConfigurationRecord::bit_depth_luma_minus8
uint8_t bit_depth_luma_minus8
Definition: evc.c:61
evc_get_nalu_type
static int evc_get_nalu_type(const uint8_t *p, int bits_size)
Definition: evc.h:32
evcc_parse_sps
static int evcc_parse_sps(const uint8_t *bs, int bs_size, EVCDecoderConfigurationRecord *evcc)
Definition: evc.c:83
NALUList::nalus_array_size
unsigned nalus_array_size
Definition: avc.h:36
NALU::offset
int offset
Definition: avc.h:30
golomb.h
exp golomb vlc stuff
EVC_NALU_HEADER_SIZE
#define EVC_NALU_HEADER_SIZE
Definition: evc.h:27
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
evcc_array_add_nal_unit
static int evcc_array_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size, uint8_t nal_type, int ps_array_completeness, EVCNALUnitArray *array)
Definition: evc.c:131
NALU::size
uint32_t size
Definition: avc.h:31
GetBitContext
Definition: get_bits.h:108
EVC_SEI_NUT
@ EVC_SEI_NUT
Definition: evc.h:62
EVCDecoderConfigurationRecord::toolset_idc_h
uint32_t toolset_idc_h
Definition: evc.c:58
ff_isom_write_evcc
int ff_isom_write_evcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes EVC sample metadata to the provided AVIOContext.
Definition: evc.c:301
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
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
intreadwrite.h
NALUList::nalus
NALU * nalus
Definition: avc.h:35
get_bits.h
NALUList::nb_nalus
unsigned nb_nalus
valid entries in nalus
Definition: avc.h:37
EVCNALUnitArray::NAL_unit_type
uint8_t NAL_unit_type
Definition: evc.c:43
NULL
#define NULL
Definition: coverity.c:32
EVCDecoderConfigurationRecord::level_idc
uint8_t level_idc
Definition: evc.c:57
SPS_INDEX
@ SPS_INDEX
Definition: evc.c:33
evcc_close
static void evcc_close(EVCDecoderConfigurationRecord *evcc)
Definition: evc.c:169
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:226
EVCDecoderConfigurationRecord::bit_depth_chroma_minus8
uint8_t bit_depth_chroma_minus8
Definition: evc.c:62
EVCDecoderConfigurationRecord
Specifies the decoder configuration information for ISO/IEC 23094-1 video content.
Definition: evc.c:54
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
size
int size
Definition: twinvq_data.h:10344
avio.h
EVCNALUnitArray::nalUnitLength
uint16_t * nalUnitLength
Definition: evc.c:45
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:248
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:412
av_reallocp_array
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition: mem.c:223
EVCDecoderConfigurationRecord::lengthSizeMinusOne
uint8_t lengthSizeMinusOne
Definition: evc.c:66
EVC_NALU_LENGTH_PREFIX_SIZE
#define EVC_NALU_LENGTH_PREFIX_SIZE
Definition: evc.h:26
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:245
avio_internal.h
EVCDecoderConfigurationRecord::configurationVersion
uint8_t configurationVersion
Definition: evc.c:55
EVC_MAX_SPS_COUNT
@ EVC_MAX_SPS_COUNT
Definition: evc.h:113
SEI_INDEX
@ SEI_INDEX
Definition: evc.c:36
NALU
Definition: avc.h:29
EVCDecoderConfigurationRecord::pic_height_in_luma_samples
uint16_t pic_height_in_luma_samples
Definition: evc.c:64
EVC_PPS_NUT
@ EVC_PPS_NUT
Definition: evc.h:59
EVCDecoderConfigurationRecord::chroma_format_idc
uint8_t chroma_format_idc
Definition: evc.c:60
EVCNALUnitArray::array_completeness
uint8_t array_completeness
Definition: evc.c:42
EVC_UNSPEC_NUT62
@ EVC_UNSPEC_NUT62
Definition: evc.h:96
EVCNALUnitArray::nalUnit
uint8_t ** nalUnit
Definition: evc.c:46
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:111
ret
ret
Definition: filter_design.txt:187
EVCDecoderConfigurationRecord::reserved
uint8_t reserved
Definition: evc.c:65
avformat.h
EVCDecoderConfigurationRecord::profile_idc
uint8_t profile_idc
Definition: evc.c:56
NALUList
Definition: avc.h:34
evc_read_nal_unit_length
static uint32_t evc_read_nal_unit_length(const uint8_t *bits, int bits_size, void *logctx)
Definition: evc_parse.h:83
EVCDecoderConfigurationRecord::pic_width_in_luma_samples
uint16_t pic_width_in_luma_samples
Definition: evc.c:63
EVCNALUnitArray
Definition: evc.c:41
EVC_SPS_NUT
@ EVC_SPS_NUT
Definition: evc.h:58
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
evc.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:490
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
evcc_write
static int evcc_write(AVIOContext *pb, EVCDecoderConfigurationRecord *evcc)
Definition: evc.c:179
NB_ARRAYS
@ NB_ARRAYS
Definition: evc.c:37
EVCNALUnitArray::numNalus
uint16_t numNalus
Definition: evc.c:44