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