FFmpeg
iamf_parse.c
Go to the documentation of this file.
1 /*
2  * Immersive Audio Model and Formats parsing
3  * Copyright (c) 2023 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/avassert.h"
23 #include "libavutil/iamf.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/log.h"
26 #include "libavutil/mem.h"
27 #include "libavcodec/get_bits.h"
28 #include "libavcodec/flac.h"
29 #include "libavcodec/leb.h"
30 #include "libavcodec/mpeg4audio.h"
31 #include "libavcodec/put_bits.h"
32 #include "avio_internal.h"
33 #include "iamf_parse.h"
34 #include "isom.h"
35 
36 static int opus_decoder_config(IAMFCodecConfig *codec_config,
37  AVIOContext *pb, int len)
38 {
39  int ret, left = len - avio_tell(pb);
40 
41  if (left < 11 || codec_config->audio_roll_distance >= 0)
42  return AVERROR_INVALIDDATA;
43 
44  codec_config->extradata = av_malloc(left + 8);
45  if (!codec_config->extradata)
46  return AVERROR(ENOMEM);
47 
48  AV_WB32A(codec_config->extradata, MKBETAG('O','p','u','s'));
49  AV_WB32A(codec_config->extradata + 4, MKBETAG('H','e','a','d'));
50  ret = ffio_read_size(pb, codec_config->extradata + 8, left);
51  if (ret < 0)
52  return ret;
53 
54  codec_config->extradata_size = left + 8;
55  codec_config->sample_rate = 48000;
56 
57  return 0;
58 }
59 
60 static int aac_decoder_config(IAMFCodecConfig *codec_config,
61  AVIOContext *pb, int len, void *logctx)
62 {
63  MPEG4AudioConfig cfg = { 0 };
64  int object_type_id, codec_id, stream_type;
65  int ret, tag, left;
66 
67  if (codec_config->audio_roll_distance >= 0)
68  return AVERROR_INVALIDDATA;
69 
70  ff_mp4_read_descr(logctx, pb, &tag);
72  return AVERROR_INVALIDDATA;
73 
74  object_type_id = avio_r8(pb);
75  if (object_type_id != 0x40)
76  return AVERROR_INVALIDDATA;
77 
78  stream_type = avio_r8(pb);
79  if (((stream_type >> 2) != 5) || ((stream_type >> 1) & 1))
80  return AVERROR_INVALIDDATA;
81 
82  avio_skip(pb, 3); // buffer size db
83  avio_skip(pb, 4); // rc_max_rate
84  avio_skip(pb, 4); // avg bitrate
85 
86  codec_id = ff_codec_get_id(ff_mp4_obj_type, object_type_id);
87  if (codec_id && codec_id != codec_config->codec_id)
88  return AVERROR_INVALIDDATA;
89 
90  left = ff_mp4_read_descr(logctx, pb, &tag);
91  if (tag != MP4DecSpecificDescrTag ||
92  !left || left > (len - avio_tell(pb)))
93  return AVERROR_INVALIDDATA;
94 
95  // We pad extradata here because avpriv_mpeg4audio_get_config2() needs it.
96  codec_config->extradata = av_malloc((size_t)left + AV_INPUT_BUFFER_PADDING_SIZE);
97  if (!codec_config->extradata)
98  return AVERROR(ENOMEM);
99 
100  ret = ffio_read_size(pb, codec_config->extradata, left);
101  if (ret < 0)
102  return ret;
103  codec_config->extradata_size = left;
104  memset(codec_config->extradata + codec_config->extradata_size, 0,
106 
107  ret = avpriv_mpeg4audio_get_config2(&cfg, codec_config->extradata,
108  codec_config->extradata_size, 1, logctx);
109  if (ret < 0)
110  return ret;
111 
112  codec_config->sample_rate = cfg.sample_rate;
113 
114  return 0;
115 }
116 
117 static int flac_decoder_config(IAMFCodecConfig *codec_config,
118  AVIOContext *pb, int len)
119 {
120  int ret, left;
121 
122  if (codec_config->audio_roll_distance)
123  return AVERROR_INVALIDDATA;
124 
125  avio_skip(pb, 4); // METADATA_BLOCK_HEADER
126 
127  left = len - avio_tell(pb);
129  return AVERROR_INVALIDDATA;
130 
131  codec_config->extradata = av_malloc(left);
132  if (!codec_config->extradata)
133  return AVERROR(ENOMEM);
134 
135  ret = ffio_read_size(pb, codec_config->extradata, left);
136  if (ret < 0)
137  return ret;
138 
139  codec_config->extradata_size = left;
140  codec_config->sample_rate = AV_RB24(codec_config->extradata + 10) >> 4;
141 
142  return 0;
143 }
144 
145 static int ipcm_decoder_config(IAMFCodecConfig *codec_config,
146  AVIOContext *pb, int len)
147 {
148  static const enum AVCodecID sample_fmt[2][3] = {
151  };
152  int sample_format = avio_r8(pb); // 0 = BE, 1 = LE
153  int sample_size = (avio_r8(pb) / 8 - 2); // 16, 24, 32
154  if (sample_format > 1 || sample_size > 2U || codec_config->audio_roll_distance)
155  return AVERROR_INVALIDDATA;
156 
157  codec_config->codec_id = sample_fmt[sample_format][sample_size];
158  codec_config->sample_rate = avio_rb32(pb);
159 
160  if (len - avio_tell(pb))
161  return AVERROR_INVALIDDATA;
162 
163  return 0;
164 }
165 
166 static int codec_config_obu(void *s, IAMFContext *c, AVIOContext *pb, int len)
167 {
168  IAMFCodecConfig **tmp, *codec_config = NULL;
169  FFIOContext b;
170  AVIOContext *pbc;
171  uint8_t *buf;
172  enum AVCodecID avcodec_id;
173  unsigned codec_config_id, nb_samples, codec_id;
174  int16_t audio_roll_distance;
175  int ret;
176 
177  buf = av_malloc(len);
178  if (!buf)
179  return AVERROR(ENOMEM);
180 
181  ret = ffio_read_size(pb, buf, len);
182  if (ret < 0)
183  goto fail;
184 
185  ffio_init_context(&b, buf, len, 0, NULL, NULL, NULL, NULL);
186  pbc = &b.pub;
187 
188  codec_config_id = ffio_read_leb(pbc);
189  codec_id = avio_rb32(pbc);
190  nb_samples = ffio_read_leb(pbc);
191  audio_roll_distance = avio_rb16(pbc);
192 
193  switch(codec_id) {
194  case MKBETAG('O','p','u','s'):
195  avcodec_id = AV_CODEC_ID_OPUS;
196  break;
197  case MKBETAG('m','p','4','a'):
198  avcodec_id = AV_CODEC_ID_AAC;
199  break;
200  case MKBETAG('f','L','a','C'):
201  avcodec_id = AV_CODEC_ID_FLAC;
202  break;
203  default:
204  avcodec_id = AV_CODEC_ID_NONE;
205  break;
206  }
207 
208  for (int i = 0; i < c->nb_codec_configs; i++)
209  if (c->codec_configs[i]->codec_config_id == codec_config_id) {
211  goto fail;
212  }
213 
214  tmp = av_realloc_array(c->codec_configs, c->nb_codec_configs + 1, sizeof(*c->codec_configs));
215  if (!tmp) {
216  ret = AVERROR(ENOMEM);
217  goto fail;
218  }
219  c->codec_configs = tmp;
220 
221  codec_config = av_mallocz(sizeof(*codec_config));
222  if (!codec_config) {
223  ret = AVERROR(ENOMEM);
224  goto fail;
225  }
226 
227  codec_config->codec_config_id = codec_config_id;
228  codec_config->codec_id = avcodec_id;
229  codec_config->nb_samples = nb_samples;
230  codec_config->audio_roll_distance = audio_roll_distance;
231 
232  switch(codec_id) {
233  case MKBETAG('O','p','u','s'):
234  ret = opus_decoder_config(codec_config, pbc, len);
235  break;
236  case MKBETAG('m','p','4','a'):
237  ret = aac_decoder_config(codec_config, pbc, len, s);
238  break;
239  case MKBETAG('f','L','a','C'):
240  ret = flac_decoder_config(codec_config, pbc, len);
241  break;
242  case MKBETAG('i','p','c','m'):
243  ret = ipcm_decoder_config(codec_config, pbc, len);
244  break;
245  default:
246  break;
247  }
248  if (ret < 0)
249  goto fail;
250 
251  if ((codec_config->nb_samples > INT_MAX) || codec_config->nb_samples <= 0 ||
252  (-codec_config->audio_roll_distance > INT_MAX / codec_config->nb_samples)) {
254  goto fail;
255  }
256 
257  c->codec_configs[c->nb_codec_configs++] = codec_config;
258 
259  len -= avio_tell(pbc);
260  if (len)
261  av_log(s, AV_LOG_WARNING, "Underread in codec_config_obu. %d bytes left at the end\n", len);
262 
263  ret = 0;
264 fail:
265  av_free(buf);
266  if (ret < 0) {
267  if (codec_config)
268  av_free(codec_config->extradata);
269  av_free(codec_config);
270  }
271  return ret;
272 }
273 
274 static int update_extradata(AVCodecParameters *codecpar)
275 {
276  GetBitContext gb;
277  PutBitContext pb;
278  int ret;
279 
280  switch(codecpar->codec_id) {
281  case AV_CODEC_ID_OPUS:
282  AV_WB8(codecpar->extradata + 9, codecpar->ch_layout.nb_channels);
283  AV_WL16A(codecpar->extradata + 10, AV_RB16A(codecpar->extradata + 10)); // Byte swap pre-skip
284  AV_WL32A(codecpar->extradata + 12, AV_RB32A(codecpar->extradata + 12)); // Byte swap sample rate
285  AV_WL16A(codecpar->extradata + 16, AV_RB16A(codecpar->extradata + 16)); // Byte swap Output Gain
286  break;
287  case AV_CODEC_ID_AAC: {
288  uint8_t buf[6];
289  int size = FFMIN(codecpar->extradata_size, sizeof(buf));
290 
291  init_put_bits(&pb, buf, sizeof(buf));
292  ret = init_get_bits8(&gb, codecpar->extradata, size);
293  if (ret < 0)
294  return ret;
295 
296  ret = get_bits(&gb, 5);
297  put_bits(&pb, 5, ret);
298  if (ret == AOT_ESCAPE) // violates section 3.11.2, but better check for it
299  put_bits(&pb, 6, get_bits(&gb, 6));
300  ret = get_bits(&gb, 4);
301  put_bits(&pb, 4, ret);
302  if (ret == 0x0f)
303  put_bits(&pb, 24, get_bits(&gb, 24));
304 
305  skip_bits(&gb, 4);
306  put_bits(&pb, 4, codecpar->ch_layout.nb_channels); // set channel config
307  ret = get_bits_left(&gb);
308  if (ret < 0)
309  return AVERROR_INVALIDDATA;
310  ret = FFMIN(ret, put_bits_left(&pb));
311  while (ret >= 32) {
312  put_bits32(&pb, get_bits_long(&gb, 32));
313  ret -= 32;
314  }
315  put_bits(&pb, ret, get_bits_long(&gb, ret));
316  flush_put_bits(&pb);
317 
318  memcpy(codecpar->extradata, buf, put_bytes_output(&pb));
319  break;
320  }
321  case AV_CODEC_ID_FLAC: {
322  uint8_t buf[13];
323  int size = FFMIN(codecpar->extradata_size, sizeof(buf));
324 
325  init_put_bits(&pb, buf, sizeof(buf));
326  ret = init_get_bits8(&gb, codecpar->extradata, size);
327  if (ret < 0)
328  return ret;
329 
330  put_bits32(&pb, get_bits_long(&gb, 32)); // min/max blocksize
331  put_bits63(&pb, 48, get_bits64(&gb, 48)); // min/max framesize
332  put_bits(&pb, 20, get_bits(&gb, 20)); // samplerate
333  skip_bits(&gb, 3);
334  put_bits(&pb, 3, codecpar->ch_layout.nb_channels - 1);
335  ret = get_bits_left(&gb);
336  if (ret < 0)
337  return AVERROR_INVALIDDATA;
338  ret = FFMIN(ret, put_bits_left(&pb));
339  put_bits(&pb, ret, get_bits(&gb, ret));
340  flush_put_bits(&pb);
341 
342  memcpy(codecpar->extradata, buf, put_bytes_output(&pb));
343  break;
344  }
345  }
346 
347  return 0;
348 }
349 
351 {
352  if (in->u.mask & AV_CH_LAYOUT_STEREO) {
353  out->u.map[n++].id = AV_CHAN_FRONT_LEFT;
354  out->u.map[n++].id = AV_CHAN_FRONT_RIGHT;
355  in->u.mask &= ~AV_CH_LAYOUT_STEREO;
357  out->u.map[n++].id = AV_CHAN_FRONT_LEFT_OF_CENTER;
358  out->u.map[n++].id = AV_CHAN_FRONT_RIGHT_OF_CENTER;
360  } else if (in->u.mask & (AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT)) {
361  out->u.map[n++].id = AV_CHAN_SIDE_LEFT;
362  out->u.map[n++].id = AV_CHAN_SIDE_RIGHT;
364  } else if (in->u.mask & (AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT)) {
365  out->u.map[n++].id = AV_CHAN_BACK_LEFT;
366  out->u.map[n++].id = AV_CHAN_BACK_RIGHT;
368  } else if (in->u.mask & (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_FRONT_RIGHT)) {
369  out->u.map[n++].id = AV_CHAN_TOP_FRONT_LEFT;
370  out->u.map[n++].id = AV_CHAN_TOP_FRONT_RIGHT;
372  } else if (in->u.mask & (AV_CH_TOP_SIDE_LEFT|AV_CH_TOP_SIDE_RIGHT)) {
373  out->u.map[n++].id = AV_CHAN_TOP_SIDE_LEFT;
374  out->u.map[n++].id = AV_CHAN_TOP_SIDE_RIGHT;
376  } else if (in->u.mask & (AV_CH_TOP_BACK_LEFT|AV_CH_TOP_BACK_RIGHT)) {
377  out->u.map[n++].id = AV_CHAN_TOP_BACK_LEFT;
378  out->u.map[n++].id = AV_CHAN_TOP_BACK_RIGHT;
380  }
381 
382  return n;
383 }
384 
386  IAMFAudioElement *audio_element,
387  const IAMFCodecConfig *codec_config)
388 {
389  int nb_layers, k = 0;
390 
391  nb_layers = avio_r8(pb) >> 5; // get_bits(&gb, 3);
392  // skip_bits(&gb, 5); //reserved
393 
394  if (nb_layers > 6 || nb_layers == 0)
395  return AVERROR_INVALIDDATA;
396 
397  audio_element->layers = av_calloc(nb_layers, sizeof(*audio_element->layers));
398  if (!audio_element->layers)
399  return AVERROR(ENOMEM);
400 
401  audio_element->nb_layers = nb_layers;
402  for (int i = 0, n = 0; i < nb_layers; i++) {
403  AVChannelLayout ch_layout = { 0 };
404  AVIAMFLayer *layer;
405  int loudspeaker_layout, output_gain_is_present_flag;
406  int substream_count, coupled_substream_count;
407  int expanded_loudspeaker_layout = -1;
408  int ret, byte = avio_r8(pb);
409  int channels;
410 
411  layer = av_iamf_audio_element_add_layer(audio_element->element);
412  if (!layer)
413  return AVERROR(ENOMEM);
414 
415  loudspeaker_layout = byte >> 4; // get_bits(&gb, 4);
416  output_gain_is_present_flag = (byte >> 3) & 1; //get_bits1(&gb);
417  if ((byte >> 2) & 1)
418  layer->flags |= AV_IAMF_LAYER_FLAG_RECON_GAIN;
419  substream_count = avio_r8(pb);
420  coupled_substream_count = avio_r8(pb);
421 
422  if (!substream_count || coupled_substream_count > substream_count ||
423  substream_count + k > audio_element->nb_substreams)
424  return AVERROR_INVALIDDATA;
425 
426  audio_element->layers[i].substream_count = substream_count;
427  audio_element->layers[i].coupled_substream_count = coupled_substream_count;
428  if (output_gain_is_present_flag) {
429  layer->output_gain_flags = avio_r8(pb) >> 2; // get_bits(&gb, 6);
430  layer->output_gain = av_make_q(sign_extend(avio_rb16(pb), 16), 1 << 8);
431  }
432 
433  if (loudspeaker_layout == 15) {
434  if (i) {
435  av_log(s, AV_LOG_ERROR, "expanded_loudspeaker_layout set with more than one layer in Audio Element #%d\n",
436  audio_element->audio_element_id);
437  return AVERROR_INVALIDDATA;
438  }
439  expanded_loudspeaker_layout = avio_r8(pb);
440  }
441  if (expanded_loudspeaker_layout >= 0 && expanded_loudspeaker_layout < 13) {
442  av_channel_layout_copy(&ch_layout, &ff_iamf_expanded_scalable_ch_layouts[expanded_loudspeaker_layout]);
443  } else if (loudspeaker_layout < 10) {
444  av_channel_layout_copy(&ch_layout, &ff_iamf_scalable_ch_layouts[loudspeaker_layout]);
445  if (i) {
446  uint64_t mask = av_channel_layout_subset(&audio_element->element->layers[i-1]->ch_layout, UINT64_MAX);
447  // When the first layer is Mono, the second layer may not have the C channel (e.g. Stereo)
448  if (audio_element->element->layers[i-1]->ch_layout.nb_channels == 1)
449  n--;
450  else if ((ch_layout.u.mask & mask) != mask)
451  return AVERROR_INVALIDDATA;
452  ch_layout.u.mask &= ~mask;
453  }
454  } else {
455  if (expanded_loudspeaker_layout >= 0)
456  avpriv_request_sample(s, "expanded_loudspeaker_layout %d", expanded_loudspeaker_layout);
457  else
458  avpriv_request_sample(s, "loudspeaker_layout %d", loudspeaker_layout);
459  return AVERROR_PATCHWELCOME;
460  }
461 
462  channels = ch_layout.nb_channels;
463  if (i) {
464  if (ch_layout.nb_channels <= audio_element->element->layers[i-1]->ch_layout.nb_channels)
465  return AVERROR_INVALIDDATA;
466  channels -= audio_element->element->layers[i-1]->ch_layout.nb_channels;
467  }
468  if (channels != substream_count + coupled_substream_count)
469  return AVERROR_INVALIDDATA;
470 
471  for (int j = 0; j < substream_count; j++) {
472  IAMFSubStream *substream = &audio_element->substreams[k++];
473 
474  substream->codecpar->ch_layout = coupled_substream_count-- > 0 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO :
476 
477  ret = update_extradata(substream->codecpar);
478  if (ret < 0)
479  return ret;
480  }
481 
483  if (ret < 0)
484  return ret;
485  for (int j = 0; j < n; j++)
486  layer->ch_layout.u.map[j].id = av_channel_layout_channel_from_index(&audio_element->element->layers[i-1]->ch_layout, j);
487 
488  coupled_substream_count = audio_element->layers[i].coupled_substream_count;
489  while (coupled_substream_count--) {
490  n = parse_coupled_substream(&layer->ch_layout, &ch_layout, n);
491  }
492 
493  substream_count -= audio_element->layers[i].coupled_substream_count;
494  n = parse_coupled_substream(&layer->ch_layout, &ch_layout, n); // In case the first layer is Mono
495  while (substream_count--) {
496  if (ch_layout.u.mask & AV_CH_FRONT_CENTER) {
497  layer->ch_layout.u.map[n++].id = AV_CHAN_FRONT_CENTER;
498  ch_layout.u.mask &= ~AV_CH_FRONT_CENTER;
499  }
500  if (ch_layout.u.mask & AV_CH_LOW_FREQUENCY) {
501  layer->ch_layout.u.map[n++].id = AV_CHAN_LOW_FREQUENCY;
502  ch_layout.u.mask &= ~AV_CH_LOW_FREQUENCY;
503  }
504  }
505 
506  if (n != ch_layout.nb_channels)
507  return AVERROR_INVALIDDATA;
508 
510  if (ret < 0 && ret != AVERROR(ENOSYS))
511  return ret;
512  }
513 
514  if (k != audio_element->nb_substreams)
515  return AVERROR_INVALIDDATA;
516 
517  return 0;
518 }
519 
520 static int ambisonics_config(void *s, AVIOContext *pb,
521  IAMFAudioElement *audio_element,
522  const IAMFCodecConfig *codec_config)
523 {
524  AVIAMFLayer *layer;
525  unsigned ambisonics_mode;
526  int output_channel_count, substream_count, order;
527  int ret;
528 
529  ambisonics_mode = ffio_read_leb(pb);
530  if (ambisonics_mode > 1)
531  return AVERROR_INVALIDDATA;
532 
533  output_channel_count = avio_r8(pb); // C
534  substream_count = avio_r8(pb); // N
535  if (audio_element->nb_substreams != substream_count || output_channel_count == 0)
536  return AVERROR_INVALIDDATA;
537 
538  order = floor(sqrt(output_channel_count - 1));
539  /* incomplete order - some harmonics are missing */
540  if ((order + 1) * (order + 1) != output_channel_count)
541  return AVERROR_INVALIDDATA;
542 
543  audio_element->layers = av_mallocz(sizeof(*audio_element->layers));
544  if (!audio_element->layers)
545  return AVERROR(ENOMEM);
546 
547  audio_element->nb_layers = 1;
548  audio_element->layers->substream_count = substream_count;
549 
550  layer = av_iamf_audio_element_add_layer(audio_element->element);
551  if (!layer)
552  return AVERROR(ENOMEM);
553 
554  layer->ambisonics_mode = ambisonics_mode;
555  if (ambisonics_mode == 0) {
556  for (int i = 0; i < substream_count; i++) {
557  IAMFSubStream *substream = &audio_element->substreams[i];
558 
560 
561  ret = update_extradata(substream->codecpar);
562  if (ret < 0)
563  return ret;
564  }
565 
566  ret = av_channel_layout_custom_init(&layer->ch_layout, output_channel_count);
567  if (ret < 0)
568  return ret;
569 
570  for (int i = 0; i < output_channel_count; i++)
571  layer->ch_layout.u.map[i].id = avio_r8(pb) + AV_CHAN_AMBISONIC_BASE;
572 
574  if (ret < 0 && ret != AVERROR(ENOSYS))
575  return ret;
576  } else {
577  int coupled_substream_count = avio_r8(pb); // M
578  int count = substream_count + coupled_substream_count;
579  int nb_demixing_matrix = count * output_channel_count;
580 
581  audio_element->layers->coupled_substream_count = coupled_substream_count;
582 
583  layer->ch_layout = (AVChannelLayout){ .order = AV_CHANNEL_ORDER_AMBISONIC, .nb_channels = output_channel_count };
584  layer->demixing_matrix = av_malloc_array(nb_demixing_matrix, sizeof(*layer->demixing_matrix));
585  if (!layer->demixing_matrix)
586  return AVERROR(ENOMEM);
587 
588  layer->nb_demixing_matrix = nb_demixing_matrix;
589 
590  for (int i = 0; i < layer->nb_demixing_matrix; i++)
591  layer->demixing_matrix[i] = av_make_q(sign_extend(avio_rb16(pb), 16), 1 << 15);
592 
593  for (int i = 0; i < substream_count; i++) {
594  IAMFSubStream *substream = &audio_element->substreams[i];
595 
596  substream->codecpar->ch_layout = coupled_substream_count-- > 0 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO :
598 
599 
600  ret = update_extradata(substream->codecpar);
601  if (ret < 0)
602  return ret;
603  }
604  }
605 
606  return 0;
607 }
608 
609 static int param_parse(void *s, IAMFContext *c, AVIOContext *pb,
610  unsigned int type,
611  const IAMFAudioElement *audio_element,
612  AVIAMFParamDefinition **out_param_definition)
613 {
615  AVIAMFParamDefinition *param;
616  unsigned int parameter_id, parameter_rate, mode;
617  unsigned int duration = 0, constant_subblock_duration = 0, nb_subblocks = 0;
618  unsigned int total_duration = 0;
619  size_t param_size;
620 
621  parameter_id = ffio_read_leb(pb);
622 
623  for (int i = 0; i < c->nb_param_definitions; i++)
624  if (c->param_definitions[i]->param->parameter_id == parameter_id) {
625  param_definition = c->param_definitions[i];
626  break;
627  }
628 
629  parameter_rate = ffio_read_leb(pb);
630  mode = avio_r8(pb) >> 7;
631 
632  if (mode == 0) {
633  duration = ffio_read_leb(pb);
634  if (!duration)
635  return AVERROR_INVALIDDATA;
636  constant_subblock_duration = ffio_read_leb(pb);
637  if (constant_subblock_duration == 0)
638  nb_subblocks = ffio_read_leb(pb);
639  else {
640  nb_subblocks = duration / constant_subblock_duration;
641  total_duration = duration;
642  }
643  }
644 
645  param = av_iamf_param_definition_alloc(type, nb_subblocks, &param_size);
646  if (!param)
647  return AVERROR(ENOMEM);
648 
649  for (int i = 0; i < nb_subblocks; i++) {
650  void *subblock = av_iamf_param_definition_get_subblock(param, i);
651  unsigned int subblock_duration = constant_subblock_duration;
652 
653  if (constant_subblock_duration == 0) {
654  subblock_duration = ffio_read_leb(pb);
655  total_duration += subblock_duration;
656  } else if (i == nb_subblocks - 1)
657  subblock_duration = duration - i * constant_subblock_duration;
658 
659  switch (type) {
661  AVIAMFMixGain *mix = subblock;
662  mix->subblock_duration = subblock_duration;
663  break;
664  }
666  AVIAMFDemixingInfo *demix = subblock;
667  demix->subblock_duration = subblock_duration;
668  // DefaultDemixingInfoParameterData
669  av_assert0(audio_element);
670  demix->dmixp_mode = avio_r8(pb) >> 5;
671  audio_element->element->default_w = avio_r8(pb) >> 4;
672  break;
673  }
675  AVIAMFReconGain *recon = subblock;
676  recon->subblock_duration = subblock_duration;
677  break;
678  }
679  default:
680  av_free(param);
681  return AVERROR_INVALIDDATA;
682  }
683  }
684 
685  if (!mode && !constant_subblock_duration && total_duration != duration) {
686  av_log(s, AV_LOG_ERROR, "Invalid subblock durations in parameter_id %u\n", parameter_id);
687  av_free(param);
688  return AVERROR_INVALIDDATA;
689  }
690 
691  param->parameter_id = parameter_id;
692  param->parameter_rate = parameter_rate;
693  param->duration = duration;
694  param->constant_subblock_duration = constant_subblock_duration;
695  param->nb_subblocks = nb_subblocks;
696 
697  if (param_definition) {
698  if (param_definition->param_size != param_size || memcmp(param_definition->param, param, param_size)) {
699  av_log(s, AV_LOG_ERROR, "Inconsistent parameters for parameter_id %u\n", parameter_id);
700  av_free(param);
701  return AVERROR_INVALIDDATA;
702  }
703  } else {
704  IAMFParamDefinition **tmp = av_realloc_array(c->param_definitions, c->nb_param_definitions + 1,
705  sizeof(*c->param_definitions));
706  if (!tmp) {
707  av_free(param);
708  return AVERROR(ENOMEM);
709  }
710  c->param_definitions = tmp;
711 
713  if (!param_definition) {
714  av_free(param);
715  return AVERROR(ENOMEM);
716  }
717  param_definition->param = param;
718  param_definition->mode = !mode;
719  param_definition->param_size = param_size;
720  param_definition->audio_element = audio_element;
721 
722  c->param_definitions[c->nb_param_definitions++] = param_definition;
723  }
724 
725  av_assert0(out_param_definition);
726  *out_param_definition = param;
727 
728  return 0;
729 }
730 
731 static int audio_element_obu(void *s, IAMFContext *c, AVIOContext *pb, int len)
732 {
733  const IAMFCodecConfig *codec_config;
734  AVIAMFAudioElement *element;
735  IAMFAudioElement **tmp, *audio_element = NULL;
736  FFIOContext b;
737  AVIOContext *pbc;
738  uint8_t *buf;
739  unsigned audio_element_id, nb_substreams, codec_config_id, num_parameters;
740  int audio_element_type, ret;
741 
742  buf = av_malloc(len);
743  if (!buf)
744  return AVERROR(ENOMEM);
745 
746  ret = ffio_read_size(pb, buf, len);
747  if (ret < 0)
748  goto fail;
749 
750  ffio_init_context(&b, buf, len, 0, NULL, NULL, NULL, NULL);
751  pbc = &b.pub;
752 
753  audio_element_id = ffio_read_leb(pbc);
754 
755  for (int i = 0; i < c->nb_audio_elements; i++)
756  if (c->audio_elements[i]->audio_element_id == audio_element_id) {
757  av_log(s, AV_LOG_ERROR, "Duplicate audio_element_id %d\n", audio_element_id);
759  goto fail;
760  }
761 
762  audio_element_type = avio_r8(pbc) >> 5;
763  if (audio_element_type > AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE) {
764  av_log(s, AV_LOG_DEBUG, "Unknown audio_element_type referenced in an audio element. Ignoring\n");
765  ret = 0;
766  goto fail;
767  }
768 
769  codec_config_id = ffio_read_leb(pbc);
770 
771  codec_config = ff_iamf_get_codec_config(c, codec_config_id);
772  if (!codec_config) {
773  av_log(s, AV_LOG_ERROR, "Non existent codec config id %d referenced in an audio element\n", codec_config_id);
775  goto fail;
776  }
777 
778  if (codec_config->codec_id == AV_CODEC_ID_NONE) {
779  av_log(s, AV_LOG_DEBUG, "Unknown codec id referenced in an audio element. Ignoring\n");
780  ret = 0;
781  goto fail;
782  }
783 
784  tmp = av_realloc_array(c->audio_elements, c->nb_audio_elements + 1, sizeof(*c->audio_elements));
785  if (!tmp) {
786  ret = AVERROR(ENOMEM);
787  goto fail;
788  }
789  c->audio_elements = tmp;
790 
791  audio_element = av_mallocz(sizeof(*audio_element));
792  if (!audio_element) {
793  ret = AVERROR(ENOMEM);
794  goto fail;
795  }
796 
797  nb_substreams = ffio_read_leb(pbc);
798  audio_element->codec_config_id = codec_config_id;
799  audio_element->audio_element_id = audio_element_id;
800  audio_element->substreams = av_calloc(nb_substreams, sizeof(*audio_element->substreams));
801  if (!audio_element->substreams) {
802  ret = AVERROR(ENOMEM);
803  goto fail;
804  }
805  audio_element->nb_substreams = nb_substreams;
806 
807  element = audio_element->element = av_iamf_audio_element_alloc();
808  if (!element) {
809  ret = AVERROR(ENOMEM);
810  goto fail;
811  }
812  audio_element->celement = element;
813 
814  element->audio_element_type = audio_element_type;
815 
816  for (int i = 0; i < audio_element->nb_substreams; i++) {
817  IAMFSubStream *substream = &audio_element->substreams[i];
818 
819  substream->codecpar = avcodec_parameters_alloc();
820  if (!substream->codecpar) {
821  ret = AVERROR(ENOMEM);
822  goto fail;
823  }
824 
825  substream->audio_substream_id = ffio_read_leb(pbc);
826 
827  substream->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
828  substream->codecpar->codec_id = codec_config->codec_id;
829  substream->codecpar->frame_size = codec_config->nb_samples;
830  substream->codecpar->sample_rate = codec_config->sample_rate;
831  substream->codecpar->seek_preroll = -codec_config->audio_roll_distance * codec_config->nb_samples;
832 
833  switch(substream->codecpar->codec_id) {
834  case AV_CODEC_ID_AAC:
835  case AV_CODEC_ID_FLAC:
836  case AV_CODEC_ID_OPUS:
838  if (!substream->codecpar->extradata) {
839  ret = AVERROR(ENOMEM);
840  goto fail;
841  }
842  memcpy(substream->codecpar->extradata, codec_config->extradata, codec_config->extradata_size);
843  memset(substream->codecpar->extradata + codec_config->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
844  substream->codecpar->extradata_size = codec_config->extradata_size;
845  break;
846  }
847  }
848 
849  num_parameters = ffio_read_leb(pbc);
850  if (num_parameters > 2 && audio_element_type == 0) {
851  av_log(s, AV_LOG_ERROR, "Audio Element parameter count %u is invalid"
852  " for Channel representations\n", num_parameters);
854  goto fail;
855  }
856  if (num_parameters && audio_element_type != 0) {
857  av_log(s, AV_LOG_ERROR, "Audio Element parameter count %u is invalid"
858  " for Scene representations\n", num_parameters);
860  goto fail;
861  }
862 
863  for (int i = 0; i < num_parameters; i++) {
864  unsigned type;
865 
866  type = ffio_read_leb(pbc);
870  if (element->demixing_info) {
872  goto fail;
873  }
874  ret = param_parse(s, c, pbc, type, audio_element, &element->demixing_info);
876  if (element->recon_gain_info) {
878  goto fail;
879  }
880  ret = param_parse(s, c, pbc, type, audio_element, &element->recon_gain_info);
881  } else {
882  unsigned param_definition_size = ffio_read_leb(pbc);
883  avio_skip(pbc, param_definition_size);
884  }
885  if (ret < 0)
886  goto fail;
887  }
888 
889  if (audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_CHANNEL) {
890  ret = scalable_channel_layout_config(s, pbc, audio_element, codec_config);
891  if (ret < 0)
892  goto fail;
893  } else if (audio_element_type == AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE) {
894  ret = ambisonics_config(s, pbc, audio_element, codec_config);
895  if (ret < 0)
896  goto fail;
897  } else {
898  av_assert0(0);
899  }
900 
901  c->audio_elements[c->nb_audio_elements++] = audio_element;
902 
903  len -= avio_tell(pbc);
904  if (len)
905  av_log(s, AV_LOG_WARNING, "Underread in audio_element_obu. %d bytes left at the end\n", len);
906 
907  ret = 0;
908 fail:
909  av_free(buf);
910  if (ret < 0)
911  ff_iamf_free_audio_element(&audio_element);
912  return ret;
913 }
914 
915 static int label_string(AVIOContext *pb, char **label)
916 {
917  uint8_t buf[128];
918 
919  avio_get_str(pb, sizeof(buf), buf, sizeof(buf));
920 
921  if (pb->error)
922  return pb->error;
923  if (pb->eof_reached)
924  return AVERROR_INVALIDDATA;
925  *label = av_strdup(buf);
926  if (!*label)
927  return AVERROR(ENOMEM);
928 
929  return 0;
930 }
931 
932 static int mix_presentation_obu(void *s, IAMFContext *c, AVIOContext *pb, int len)
933 {
935  IAMFMixPresentation **tmp, *mix_presentation = NULL;
936  FFIOContext b;
937  AVIOContext *pbc;
938  uint8_t *buf;
939  unsigned nb_submixes, mix_presentation_id;
940  int ret;
941 
942  buf = av_malloc(len);
943  if (!buf)
944  return AVERROR(ENOMEM);
945 
946  ret = ffio_read_size(pb, buf, len);
947  if (ret < 0)
948  goto fail;
949 
950  ffio_init_context(&b, buf, len, 0, NULL, NULL, NULL, NULL);
951  pbc = &b.pub;
952 
953  mix_presentation_id = ffio_read_leb(pbc);
954 
955  for (int i = 0; i < c->nb_mix_presentations; i++)
956  if (c->mix_presentations[i]->mix_presentation_id == mix_presentation_id) {
957  av_log(s, AV_LOG_ERROR, "Duplicate mix_presentation_id %d\n", mix_presentation_id);
959  goto fail;
960  }
961 
962  tmp = av_realloc_array(c->mix_presentations, c->nb_mix_presentations + 1, sizeof(*c->mix_presentations));
963  if (!tmp) {
964  ret = AVERROR(ENOMEM);
965  goto fail;
966  }
967  c->mix_presentations = tmp;
968 
969  mix_presentation = av_mallocz(sizeof(*mix_presentation));
970  if (!mix_presentation) {
971  ret = AVERROR(ENOMEM);
972  goto fail;
973  }
974 
975  mix_presentation->mix_presentation_id = mix_presentation_id;
976  mix = mix_presentation->mix = av_iamf_mix_presentation_alloc();
977  if (!mix) {
978  ret = AVERROR(ENOMEM);
979  goto fail;
980  }
981  mix_presentation->cmix = mix;
982 
983  mix_presentation->count_label = ffio_read_leb(pbc);
984  mix_presentation->language_label = av_calloc(mix_presentation->count_label,
985  sizeof(*mix_presentation->language_label));
986  if (!mix_presentation->language_label) {
987  mix_presentation->count_label = 0;
988  ret = AVERROR(ENOMEM);
989  goto fail;
990  }
991 
992  for (int i = 0; i < mix_presentation->count_label; i++) {
993  ret = label_string(pbc, &mix_presentation->language_label[i]);
994  if (ret < 0)
995  goto fail;
996  }
997 
998  for (int i = 0; i < mix_presentation->count_label; i++) {
999  char *annotation = NULL;
1000  ret = label_string(pbc, &annotation);
1001  if (ret < 0)
1002  goto fail;
1003  ret = av_dict_set(&mix->annotations, mix_presentation->language_label[i], annotation,
1005  if (ret < 0)
1006  goto fail;
1007  }
1008 
1009  nb_submixes = ffio_read_leb(pbc);
1010  for (int i = 0; i < nb_submixes; i++) {
1011  AVIAMFSubmix *sub_mix;
1012  unsigned nb_elements, nb_layouts;
1013 
1015  if (!sub_mix) {
1016  ret = AVERROR(ENOMEM);
1017  goto fail;
1018  }
1019 
1020  nb_elements = ffio_read_leb(pbc);
1021  for (int j = 0; j < nb_elements; j++) {
1022  AVIAMFSubmixElement *submix_element;
1023  IAMFAudioElement *audio_element = NULL;
1024  unsigned int rendering_config_extension_size;
1025 
1026  submix_element = av_iamf_submix_add_element(sub_mix);
1027  if (!submix_element) {
1028  ret = AVERROR(ENOMEM);
1029  goto fail;
1030  }
1031 
1032  submix_element->audio_element_id = ffio_read_leb(pbc);
1033 
1034  for (int k = 0; k < c->nb_audio_elements; k++)
1035  if (c->audio_elements[k]->audio_element_id == submix_element->audio_element_id) {
1036  audio_element = c->audio_elements[k];
1037  break;
1038  }
1039 
1040  if (!audio_element) {
1041  av_log(s, AV_LOG_ERROR, "Invalid Audio Element with id %u referenced by Mix Parameters %u\n",
1042  submix_element->audio_element_id, mix_presentation_id);
1044  goto fail;
1045  }
1046 
1047  for (int k = 0; k < mix_presentation->count_label; k++) {
1048  char *annotation = NULL;
1049  ret = label_string(pbc, &annotation);
1050  if (ret < 0)
1051  goto fail;
1052  ret = av_dict_set(&submix_element->annotations, mix_presentation->language_label[k], annotation,
1054  if (ret < 0)
1055  goto fail;
1056  }
1057 
1058  submix_element->headphones_rendering_mode = avio_r8(pbc) >> 6;
1059 
1060  rendering_config_extension_size = ffio_read_leb(pbc);
1061  avio_skip(pbc, rendering_config_extension_size);
1062 
1064  NULL,
1065  &submix_element->element_mix_config);
1066  if (ret < 0)
1067  goto fail;
1068  submix_element->default_mix_gain = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1069  }
1070 
1072  if (ret < 0)
1073  goto fail;
1074  sub_mix->default_mix_gain = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1075 
1076  nb_layouts = ffio_read_leb(pbc);
1077  for (int j = 0; j < nb_layouts; j++) {
1078  AVIAMFSubmixLayout *submix_layout;
1079  int info_type;
1080  int byte = avio_r8(pbc);
1081 
1082  submix_layout = av_iamf_submix_add_layout(sub_mix);
1083  if (!submix_layout) {
1084  ret = AVERROR(ENOMEM);
1085  goto fail;
1086  }
1087 
1088  submix_layout->layout_type = byte >> 6;
1091  av_log(s, AV_LOG_ERROR, "Invalid Layout type %u in a submix from Mix Presentation %u\n",
1092  submix_layout->layout_type, mix_presentation_id);
1094  goto fail;
1095  }
1096  if (submix_layout->layout_type == 2) {
1097  int sound_system;
1098  sound_system = (byte >> 2) & 0xF;
1099  if (sound_system >= FF_ARRAY_ELEMS(ff_iamf_sound_system_map)) {
1101  goto fail;
1102  }
1103  av_channel_layout_copy(&submix_layout->sound_system, &ff_iamf_sound_system_map[sound_system].layout);
1104  } else
1106 
1107  info_type = avio_r8(pbc);
1108  submix_layout->integrated_loudness = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1109  submix_layout->digital_peak = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1110 
1111  if (info_type & 1)
1112  submix_layout->true_peak = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1113  if (info_type & 2) {
1114  unsigned int num_anchored_loudness = avio_r8(pbc);
1115 
1116  for (int k = 0; k < num_anchored_loudness; k++) {
1117  unsigned int anchor_element = avio_r8(pbc);
1118  AVRational anchored_loudness = av_make_q(sign_extend(avio_rb16(pbc), 16), 1 << 8);
1119  if (anchor_element == IAMF_ANCHOR_ELEMENT_DIALOGUE)
1120  submix_layout->dialogue_anchored_loudness = anchored_loudness;
1121  else if (anchor_element <= IAMF_ANCHOR_ELEMENT_ALBUM)
1122  submix_layout->album_anchored_loudness = anchored_loudness;
1123  else
1124  av_log(s, AV_LOG_DEBUG, "Unknown anchor_element. Ignoring\n");
1125  }
1126  }
1127 
1128  if (info_type & 0xFC) {
1129  unsigned int info_type_size = ffio_read_leb(pbc);
1130  avio_skip(pbc, info_type_size);
1131  }
1132  }
1133  }
1134 
1135  c->mix_presentations[c->nb_mix_presentations++] = mix_presentation;
1136 
1137  len -= avio_tell(pbc);
1138  if (len)
1139  av_log(s, AV_LOG_WARNING, "Underread in mix_presentation_obu. %d bytes left at the end\n", len);
1140 
1141  ret = 0;
1142 fail:
1143  av_free(buf);
1144  if (ret < 0)
1145  ff_iamf_free_mix_presentation(&mix_presentation);
1146  return ret;
1147 }
1148 
1149 int ff_iamf_parse_obu_header(const uint8_t *buf, int buf_size,
1150  unsigned *obu_size, int *start_pos, enum IAMF_OBU_Type *type,
1151  unsigned *skip_samples, unsigned *discard_padding)
1152 {
1153  GetBitContext gb;
1154  int ret, extension_flag, trimming, start;
1155  unsigned skip = 0, discard = 0;
1156  unsigned size;
1157 
1158  ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_IAMF_OBU_HEADER_SIZE));
1159  if (ret < 0)
1160  return ret;
1161 
1162  *type = get_bits(&gb, 5);
1163  /*redundant =*/ get_bits1(&gb);
1164  trimming = get_bits1(&gb);
1165  extension_flag = get_bits1(&gb);
1166 
1167  *obu_size = get_leb(&gb);
1168  if (*obu_size > INT_MAX)
1169  return AVERROR_INVALIDDATA;
1170 
1171  start = get_bits_count(&gb) / 8;
1172 
1173  if (trimming) {
1174  discard = get_leb(&gb); // num_samples_to_trim_at_end
1175  skip = get_leb(&gb); // num_samples_to_trim_at_start
1176  }
1177 
1178  if (skip_samples)
1179  *skip_samples = skip;
1180  if (discard_padding)
1181  *discard_padding = discard;
1182 
1183  if (extension_flag) {
1184  unsigned int extension_bytes;
1185  extension_bytes = get_leb(&gb);
1186  if (extension_bytes > INT_MAX / 8)
1187  return AVERROR_INVALIDDATA;
1188  skip_bits_long(&gb, extension_bytes * 8);
1189  }
1190 
1191  if (get_bits_left(&gb) < 0)
1192  return AVERROR_INVALIDDATA;
1193 
1194  size = *obu_size + start;
1195  if (size > INT_MAX)
1196  return AVERROR_INVALIDDATA;
1197 
1198  *obu_size -= get_bits_count(&gb) / 8 - start;
1199  *start_pos = size - *obu_size;
1200 
1201  return size;
1202 }
1203 
1205  int max_size, void *log_ctx)
1206 {
1208  int ret;
1209 
1210  while (1) {
1211  unsigned obu_size;
1212  enum IAMF_OBU_Type type;
1213  int start_pos, len, size;
1214 
1215  if ((ret = ffio_ensure_seekback(pb, FFMIN(MAX_IAMF_OBU_HEADER_SIZE, max_size))) < 0)
1216  return ret;
1217  size = avio_read(pb, header, FFMIN(MAX_IAMF_OBU_HEADER_SIZE, max_size));
1218  if (size < 0)
1219  return size;
1221 
1222  len = ff_iamf_parse_obu_header(header, size, &obu_size, &start_pos, &type, NULL, NULL);
1223  if (len < 0 || obu_size > max_size) {
1224  av_log(log_ctx, AV_LOG_ERROR, "Failed to read obu header\n");
1225  avio_seek(pb, -size, SEEK_CUR);
1226  return len;
1227  }
1228 
1230  avio_seek(pb, -size, SEEK_CUR);
1231  break;
1232  }
1233 
1234  avio_seek(pb, -(size - start_pos), SEEK_CUR);
1235  switch (type) {
1237  ret = codec_config_obu(log_ctx, c, pb, obu_size);
1238  break;
1240  ret = audio_element_obu(log_ctx, c, pb, obu_size);
1241  break;
1243  ret = mix_presentation_obu(log_ctx, c, pb, obu_size);
1244  break;
1245  default: {
1246  int64_t offset = avio_skip(pb, obu_size);
1247  if (offset < 0)
1248  ret = offset;
1249  break;
1250  }
1251  }
1252  if (ret < 0) {
1253  av_log(log_ctx, AV_LOG_ERROR, "Failed to read obu type %d\n", type);
1254  return ret;
1255  }
1256  max_size -= obu_size + start_pos;
1257  if (max_size < 0)
1258  return AVERROR_INVALIDDATA;
1259  if (!max_size)
1260  break;
1261  }
1262 
1263  return 0;
1264 }
update_extradata
static int update_extradata(AVCodecParameters *codecpar)
Definition: iamf_parse.c:274
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:338
iamf.h
ff_iamf_free_mix_presentation
void ff_iamf_free_mix_presentation(IAMFMixPresentation **pmix_presentation)
Definition: iamf.c:158
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:280
AV_IAMF_SUBMIX_LAYOUT_TYPE_LOUDSPEAKERS
@ AV_IAMF_SUBMIX_LAYOUT_TYPE_LOUDSPEAKERS
The layout follows the loudspeaker sound system convention of ITU-2051-3.
Definition: iamf.h:501
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
MP4DecConfigDescrTag
#define MP4DecConfigDescrTag
Definition: isom.h:398
ffio_init_context
void ffio_init_context(FFIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:50
mix
static int mix(int c0, int c1)
Definition: 4xm.c:717
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AV_CH_TOP_SIDE_LEFT
#define AV_CH_TOP_SIDE_LEFT
Definition: channel_layout.h:200
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
out
FILE * out
Definition: movenc.c:55
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:395
put_bytes_output
static int put_bytes_output(const PutBitContext *s)
Definition: put_bits.h:99
IAMF_OBU_IA_SEQUENCE_HEADER
@ IAMF_OBU_IA_SEQUENCE_HEADER
Definition: iamf.h:63
ff_mp4_obj_type
const AVCodecTag ff_mp4_obj_type[]
Definition: isom.c:34
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
AVIAMFAudioElement::default_w
unsigned int default_w
Default weight value as defined in section 3.6 of IAMF.
Definition: iamf.h:396
IAMFAudioElement::nb_substreams
unsigned int nb_substreams
Definition: iamf.h:99
av_iamf_param_definition_alloc
AVIAMFParamDefinition * av_iamf_param_definition_alloc(enum AVIAMFParamDefinitionType type, unsigned int nb_subblocks, size_t *out_size)
Allocates memory for AVIAMFParamDefinition, plus an array of.
Definition: iamf.c:159
AVIAMFAudioElement::layers
AVIAMFLayer ** layers
Definition: iamf.h:362
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:424
int64_t
long long int64_t
Definition: coverity.c:34
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:254
AVChannelLayout::map
AVChannelCustom * map
This member must be used when the channel order is AV_CHANNEL_ORDER_CUSTOM.
Definition: channel_layout.h:370
mask
int mask
Definition: mediacodecdec_common.c:154
AV_CH_TOP_FRONT_RIGHT
#define AV_CH_TOP_FRONT_RIGHT
Definition: channel_layout.h:189
AV_RB32A
#define AV_RB32A(p)
Definition: intreadwrite.h:575
mode
Definition: swscale.c:56
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:154
av_channel_layout_channel_from_index
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
Definition: channel_layout.c:673
AVCodecParameters::seek_preroll
int seek_preroll
Audio only.
Definition: codec_par.h:214
b
#define b
Definition: input.c:42
AVIOContext::error
int error
contains the error code or 0 if no error happened
Definition: avio.h:239
AVIAMFSubmixLayout::layout_type
enum AVIAMFSubmixLayoutType layout_type
Definition: iamf.h:520
AV_CH_TOP_FRONT_LEFT
#define AV_CH_TOP_FRONT_LEFT
Definition: channel_layout.h:187
put_bits32
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:301
AVIAMFParamDefinition
Parameters as defined in section 3.6.1 of IAMF.
Definition: iamf.h:193
AVIAMFSubmixElement::default_mix_gain
AVRational default_mix_gain
Default mix gain value to apply when there are no AVIAMFParamDefinition with element_mix_config's par...
Definition: iamf.h:472
AV_CODEC_ID_FLAC
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:472
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:324
iamf_parse.h
AVChannelLayout::mask
uint64_t mask
This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used for AV_CHANNEL_ORDER_AMBISONIC ...
Definition: channel_layout.h:351
ambisonics_config
static int ambisonics_config(void *s, AVIOContext *pb, IAMFAudioElement *audio_element, const IAMFCodecConfig *codec_config)
Definition: iamf_parse.c:520
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
FFIOContext
Definition: avio_internal.h:28
flac_decoder_config
static int flac_decoder_config(IAMFCodecConfig *codec_config, AVIOContext *pb, int len)
Definition: iamf_parse.c:117
IAMFMixPresentation::cmix
const AVIAMFMixPresentation * cmix
Definition: iamf.h:108
AV_CH_TOP_BACK_LEFT
#define AV_CH_TOP_BACK_LEFT
Definition: channel_layout.h:190
MPEG4AudioConfig
Definition: mpeg4audio.h:29
AVIAMFSubmixLayout::digital_peak
AVRational digital_peak
The digital (sampled) peak value of the audio signal, as defined in ITU-1770-4.
Definition: iamf.h:538
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:383
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:337
mpeg4audio.h
AVIAMFSubmixLayout::integrated_loudness
AVRational integrated_loudness
The program integrated loudness information, as defined in ITU-1770-4.
Definition: iamf.h:533
av_iamf_mix_presentation_add_submix
AVIAMFSubmix * av_iamf_mix_presentation_add_submix(AVIAMFMixPresentation *mix_presentation)
Allocate a submix and add it to a given AVIAMFMixPresentation.
IAMFCodecConfig::extradata
uint8_t * extradata
Definition: iamf.h:74
MP4DecSpecificDescrTag
#define MP4DecSpecificDescrTag
Definition: isom.h:399
AOT_ESCAPE
@ AOT_ESCAPE
Y Escape Value.
Definition: mpeg4audio.h:102
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:339
AV_CHAN_TOP_BACK_RIGHT
@ AV_CHAN_TOP_BACK_RIGHT
Definition: channel_layout.h:67
IAMFParamDefinition
Definition: iamf.h:121
fail
#define fail()
Definition: checkasm.h:216
GetBitContext
Definition: get_bits.h:109
AVIAMFSubmixLayout
Submix layout as defined in section 3.7.6 of IAMF.
Definition: iamf.h:517
AV_CH_BACK_LEFT
#define AV_CH_BACK_LEFT
Definition: channel_layout.h:179
IAMF_ANCHOR_ELEMENT_ALBUM
@ IAMF_ANCHOR_ELEMENT_ALBUM
Definition: iamf.h:142
put_bits_left
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:135
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
av_iamf_audio_element_alloc
AVIAMFAudioElement * av_iamf_audio_element_alloc(void)
Allocates a AVIAMFAudioElement, and initializes its fields with default values.
Definition: iamf.c:326
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
IAMF_OBU_IA_PARAMETER_BLOCK
@ IAMF_OBU_IA_PARAMETER_BLOCK
Definition: iamf.h:41
AVIAMFAudioElement::audio_element_type
enum AVIAMFAudioElementType audio_element_type
Audio element type as defined in section 3.6 of IAMF.
Definition: iamf.h:391
opus_decoder_config
static int opus_decoder_config(IAMFCodecConfig *codec_config, AVIOContext *pb, int len)
Definition: iamf_parse.c:36
AVIAMFReconGain
Recon Gain Info Parameter Data as defined in section 3.8.3 of IAMF.
Definition: iamf.h:148
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:218
param_parse
static int param_parse(void *s, IAMFContext *c, AVIOContext *pb, unsigned int type, const IAMFAudioElement *audio_element, AVIAMFParamDefinition **out_param_definition)
Definition: iamf_parse.c:609
codec_config_obu
static int codec_config_obu(void *s, IAMFContext *c, AVIOContext *pb, int len)
Definition: iamf_parse.c:166
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:79
IAMFAudioElement::element
AVIAMFAudioElement * element
element backs celement iff the AVIAMFAudioElement is owned by this structure.
Definition: iamf.h:95
AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN
@ AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN
Subblocks are of struct type AVIAMFReconGain.
Definition: iamf.h:181
AVIAMFSubmixElement::annotations
AVDictionary * annotations
A dictionary of strings describing the submix in different languages.
Definition: iamf.h:493
avassert.h
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:764
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
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:544
IAMFCodecConfig::sample_rate
int sample_rate
Definition: iamf.h:72
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:178
IAMF_OBU_IA_MIX_PRESENTATION
@ IAMF_OBU_IA_MIX_PRESENTATION
Definition: iamf.h:40
duration
int64_t duration
Definition: movenc.c:65
AVCodecParameters::frame_size
int frame_size
Audio only.
Definition: codec_par.h:195
av_iamf_submix_add_layout
AVIAMFSubmixLayout * av_iamf_submix_add_layout(AVIAMFSubmix *submix)
Allocate a submix layout and add it to a given AVIAMFSubmix.
avpriv_mpeg4audio_get_config2
int avpriv_mpeg4audio_get_config2(MPEG4AudioConfig *c, const uint8_t *buf, int size, int sync_extension, void *logctx)
Parse MPEG-4 systems extradata from a raw buffer to retrieve audio configuration.
Definition: mpeg4audio.c:165
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
IAMFSubStream::audio_substream_id
unsigned int audio_substream_id
Definition: iamf.h:83
IAMFLayer::substream_count
unsigned int substream_count
Definition: iamf.h:78
AVIAMFSubmixLayout::dialogue_anchored_loudness
AVRational dialogue_anchored_loudness
The Dialogue loudness information, as defined in ITU-1770-4.
Definition: iamf.h:546
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:217
floor
static __device__ float floor(float a)
Definition: cuda_runtime.h:173
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
av_iamf_param_definition_get_subblock
static av_always_inline void * av_iamf_param_definition_get_subblock(const AVIAMFParamDefinition *par, unsigned int idx)
Get the subblock at the specified.
Definition: iamf.h:260
AV_CHAN_SIDE_RIGHT
@ AV_CHAN_SIDE_RIGHT
Definition: channel_layout.h:60
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
AVIAMFSubmixElement::headphones_rendering_mode
enum AVIAMFHeadphonesMode headphones_rendering_mode
A value that indicates whether the referenced channel-based Audio Element shall be rendered to stereo...
Definition: iamf.h:481
ffio_read_leb
unsigned int ffio_read_leb(AVIOContext *s)
Read a unsigned integer coded as a variable number of up to eight little-endian bytes,...
Definition: aviobuf.c:930
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
AV_CH_TOP_SIDE_RIGHT
#define AV_CH_TOP_SIDE_RIGHT
Definition: channel_layout.h:201
put_bits63
static void put_bits63(PutBitContext *s, int n, uint64_t value)
Write up to 63 bits into a bitstream.
Definition: put_bits.h:344
channels
channels
Definition: aptx.h:31
get_bits.h
AVIAMFLayer::ch_layout
AVChannelLayout ch_layout
Definition: iamf.h:297
IAMFAudioElement::nb_layers
unsigned int nb_layers
Definition: iamf.h:104
ff_iamfdec_read_descriptors
int ff_iamfdec_read_descriptors(IAMFContext *c, AVIOContext *pb, int max_size, void *log_ctx)
Definition: iamf_parse.c:1204
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:410
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.h:31
PutBitContext
Definition: put_bits.h:50
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
AV_CHAN_TOP_SIDE_LEFT
@ AV_CHAN_TOP_SIDE_LEFT
Definition: channel_layout.h:77
AV_CHAN_TOP_SIDE_RIGHT
@ AV_CHAN_TOP_SIDE_RIGHT
Definition: channel_layout.h:78
IAMFAudioElement::audio_element_id
unsigned int audio_element_id
Definition: iamf.h:96
AVIAMFDemixingInfo
Demixing Info Parameter Data as defined in section 3.8.2 of IAMF.
Definition: iamf.h:128
IAMFSoundSystemMap::layout
AVChannelLayout layout
Definition: iamf.h:164
AV_CHANNEL_ORDER_AMBISONIC
@ AV_CHANNEL_ORDER_AMBISONIC
The audio is represented as the decomposition of the sound field into spherical harmonics.
Definition: channel_layout.h:155
NULL
#define NULL
Definition: coverity.c:32
get_leb
static unsigned get_leb(GetBitContext *s)
Read a unsigned integer coded as a variable number of up to eight little-endian bytes,...
Definition: leb.h:35
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
isom.h
IAMF_OBU_IA_AUDIO_ELEMENT
@ IAMF_OBU_IA_AUDIO_ELEMENT
Definition: iamf.h:39
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVIAMFParamDefinition::duration
unsigned int duration
The accumulated duration of all blocks in this parameter definition, in units of 1 / parameter_rate.
Definition: iamf.h:231
ff_iamf_get_codec_config
static IAMFCodecConfig * ff_iamf_get_codec_config(const IAMFContext *c, unsigned int codec_config_id)
Definition: iamf.h:172
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:391
audio_element_obu
static int audio_element_obu(void *s, IAMFContext *c, AVIOContext *pb, int len)
Definition: iamf_parse.c:731
AVIAMFLayer
A layer defining a Channel Layout in the Audio Element.
Definition: iamf.h:294
IAMF_OBU_IA_CODEC_CONFIG
@ IAMF_OBU_IA_CODEC_CONFIG
Definition: iamf.h:38
IAMFSubStream
Definition: iamf.h:82
IAMFAudioElement::layers
IAMFLayer * layers
Definition: iamf.h:103
FLAC_STREAMINFO_SIZE
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:32
AV_DICT_DONT_OVERWRITE
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:81
AV_CH_FRONT_CENTER
#define AV_CH_FRONT_CENTER
Definition: channel_layout.h:177
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
IAMFAudioElement::celement
const AVIAMFAudioElement * celement
Definition: iamf.h:90
AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_FRONT_LEFT_OF_CENTER
Definition: channel_layout.h:181
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_mp4_read_descr
int ff_mp4_read_descr(void *logctx, AVIOContext *pb, int *tag)
Definition: isom.c:295
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AV_CHAN_FRONT_RIGHT_OF_CENTER
@ AV_CHAN_FRONT_RIGHT_OF_CENTER
Definition: channel_layout.h:57
ff_iamf_parse_obu_header
int ff_iamf_parse_obu_header(const uint8_t *buf, int buf_size, unsigned *obu_size, int *start_pos, enum IAMF_OBU_Type *type, unsigned *skip_samples, unsigned *discard_padding)
Definition: iamf_parse.c:1149
AVIAMFLayer::output_gain_flags
unsigned int output_gain_flags
Output gain channel flags as defined in section 3.6.2 of IAMF.
Definition: iamf.h:310
ff_iamf_expanded_scalable_ch_layouts
const AVChannelLayout ff_iamf_expanded_scalable_ch_layouts[13]
Definition: iamf.c:48
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
AVIAMFSubmixElement::audio_element_id
unsigned int audio_element_id
The id of the Audio Element this submix element references.
Definition: iamf.h:455
AV_CHAN_FRONT_RIGHT
@ AV_CHAN_FRONT_RIGHT
Definition: channel_layout.h:51
AV_CHAN_FRONT_CENTER
@ AV_CHAN_FRONT_CENTER
Definition: channel_layout.h:52
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
AVIAMFSubmix
Submix layout as defined in section 3.7 of IAMF.
Definition: iamf.h:559
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:462
IAMFAudioElement
Definition: iamf.h:89
AVIAMFReconGain::subblock_duration
unsigned int subblock_duration
Duration for the given subblock, in units of 1 / parameter_rate.
Definition: iamf.h:156
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
IAMFCodecConfig::nb_samples
unsigned nb_samples
Definition: iamf.h:70
AV_CODEC_ID_PCM_S24LE
@ AV_CODEC_ID_PCM_S24LE
Definition: codec_id.h:350
AVIAMFDemixingInfo::subblock_duration
unsigned int subblock_duration
Duration for the given subblock, in units of 1 / parameter_rate.
Definition: iamf.h:136
ff_codec_get_id
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:143
IAMFCodecConfig
Definition: iamf.h:66
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:319
ff_iamf_sound_system_map
const struct IAMFSoundSystemMap ff_iamf_sound_system_map[14]
Definition: iamf.c:120
IAMFCodecConfig::extradata_size
int extradata_size
Definition: iamf.h:73
avio_get_str
int avio_get_str(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a string from pb into buf.
Definition: aviobuf.c:869
AV_CHAN_LOW_FREQUENCY
@ AV_CHAN_LOW_FREQUENCY
Definition: channel_layout.h:53
size
int size
Definition: twinvq_data.h:10344
av_make_q
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
AV_CHAN_BACK_RIGHT
@ AV_CHAN_BACK_RIGHT
Definition: channel_layout.h:55
IAMFLayer::coupled_substream_count
unsigned int coupled_substream_count
Definition: iamf.h:79
AV_WL32A
#define AV_WL32A(p, v)
Definition: intreadwrite.h:571
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
AV_CHAN_SIDE_LEFT
@ AV_CHAN_SIDE_LEFT
Definition: channel_layout.h:59
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:520
av_iamf_mix_presentation_alloc
AVIAMFMixPresentation * av_iamf_mix_presentation_alloc(void)
Allocates a AVIAMFMixPresentation, and initializes its fields with default values.
Definition: iamf.c:524
IAMFContext
Definition: iamf.h:128
header
static const uint8_t header[24]
Definition: sdr2.c:68
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:606
IAMFAudioElement::substreams
IAMFSubStream * substreams
Definition: iamf.h:98
AVIAMFParamDefinition::constant_subblock_duration
unsigned int constant_subblock_duration
The duration of every subblock in the case where all subblocks, with the optional exception of the la...
Definition: iamf.h:238
av_iamf_submix_add_element
AVIAMFSubmixElement * av_iamf_submix_add_element(AVIAMFSubmix *submix)
Allocate a submix element and add it to a given AVIAMFSubmix.
av_channel_layout_retype
int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags)
Change the AVChannelOrder of a channel layout.
Definition: channel_layout.c:885
AVIAMFAudioElement
Information on how to combine one or more audio streams, as defined in section 3.6 of IAMF.
Definition: iamf.h:359
ipcm_decoder_config
static int ipcm_decoder_config(IAMFCodecConfig *codec_config, AVIOContext *pb, int len)
Definition: iamf_parse.c:145
ffio_ensure_seekback
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:1026
AV_CHAN_TOP_FRONT_RIGHT
@ AV_CHAN_TOP_FRONT_RIGHT
Definition: channel_layout.h:64
offset
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 offset
Definition: writing_filters.txt:86
AV_CH_TOP_BACK_RIGHT
#define AV_CH_TOP_BACK_RIGHT
Definition: channel_layout.h:192
AV_CHANNEL_ORDER_NATIVE
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
Definition: channel_layout.h:125
AVIAMFMixGain
Mix Gain Parameter Data as defined in section 3.8.1 of IAMF.
Definition: iamf.h:77
AV_CH_FRONT_RIGHT_OF_CENTER
#define AV_CH_FRONT_RIGHT_OF_CENTER
Definition: channel_layout.h:182
AV_CHAN_FRONT_LEFT_OF_CENTER
@ AV_CHAN_FRONT_LEFT_OF_CENTER
Definition: channel_layout.h:56
leb.h
av_channel_layout_custom_init
int av_channel_layout_custom_init(AVChannelLayout *channel_layout, int nb_channels)
Initialize a custom channel layout with the specified number of channels.
Definition: channel_layout.c:232
AV_RB16A
#define AV_RB16A(p)
Definition: intreadwrite.h:561
avcodec_parameters_alloc
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: codec_par.c:57
get_bits64
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
Definition: get_bits.h:456
log.h
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVIAMFParamDefinition::parameter_id
unsigned int parameter_id
Identifier for the parameter substream.
Definition: iamf.h:218
avio_internal.h
AVIAMFLayer::demixing_matrix
AVRational * demixing_matrix
Demixing matrix as defined in section 3.6.3 of IAMF.
Definition: iamf.h:336
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
AVChannelLayout::u
union AVChannelLayout::@493 u
Details about which channels are present in this layout.
AVIAMFMixPresentation
Information on how to render and mix one or more AVIAMFAudioElement to generate the final audio outpu...
Definition: iamf.h:616
aac_decoder_config
static int aac_decoder_config(IAMFCodecConfig *codec_config, AVIOContext *pb, int len, void *logctx)
Definition: iamf_parse.c:60
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
param_definition
static int param_definition(const IAMFContext *iamf, const IAMFParamDefinition *param_def, AVIOContext *dyn_bc, void *log_ctx)
Definition: iamf_writer.c:691
AV_CODEC_ID_PCM_S32BE
@ AV_CODEC_ID_PCM_S32BE
Definition: codec_id.h:347
AV_CH_SIDE_RIGHT
#define AV_CH_SIDE_RIGHT
Definition: channel_layout.h:185
len
int len
Definition: vorbis_enc_data.h:426
IAMFMixPresentation::count_label
unsigned int count_label
Definition: iamf.h:117
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
parse_coupled_substream
static int parse_coupled_substream(AVChannelLayout *out, AVChannelLayout *in, int n)
Definition: iamf_parse.c:350
AVIAMFParamDefinition::nb_subblocks
unsigned int nb_subblocks
Number of subblocks in the array.
Definition: iamf.h:208
AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE
@ AV_IAMF_AUDIO_ELEMENT_TYPE_SCENE
Definition: iamf.h:349
AVIAMFLayer::nb_demixing_matrix
unsigned int nb_demixing_matrix
The length of the Demixing matrix array.
Definition: iamf.h:343
AV_WB32A
#define AV_WB32A(p, v)
Definition: intreadwrite.h:578
AV_WB8
#define AV_WB8(p, d)
Definition: intreadwrite.h:392
tag
uint32_t tag
Definition: movenc.c:2032
ret
ret
Definition: filter_design.txt:187
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:236
ff_iamf_free_audio_element
void ff_iamf_free_audio_element(IAMFAudioElement **paudio_element)
Definition: iamf.c:143
av_malloc
void * av_malloc(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:98
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:749
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
U
#define U(x)
Definition: vpx_arith.h:37
IAMF_OBU_Type
IAMF_OBU_Type
Definition: iamf.h:37
label_string
static int label_string(AVIOContext *pb, char **label)
Definition: iamf_parse.c:915
IAMFMixPresentation
Definition: iamf.h:107
IAMFMixPresentation::language_label
char ** language_label
Definition: iamf.h:118
AVIAMFSubmix::default_mix_gain
AVRational default_mix_gain
Default mix gain value to apply when there are no AVIAMFParamDefinition with output_mix_config's para...
Definition: iamf.h:606
AVIAMFSubmixLayout::album_anchored_loudness
AVRational album_anchored_loudness
The Album loudness information, as defined in ITU-1770-4.
Definition: iamf.h:550
av_channel_layout_subset
uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout, uint64_t mask)
Find out what channels from a given set are present in a channel layout, without regard for their pos...
Definition: channel_layout.c:865
AV_CHAN_TOP_BACK_LEFT
@ AV_CHAN_TOP_BACK_LEFT
Definition: channel_layout.h:65
AVIAMFSubmixLayout::true_peak
AVRational true_peak
The true peak of the audio signal, as defined in ITU-1770-4.
Definition: iamf.h:542
mode
mode
Definition: ebur128.h:83
AVIAMFSubmixLayout::sound_system
AVChannelLayout sound_system
Channel layout matching one of Sound Systems A to J of ITU-2051-3, plus 7.1.2ch, 3....
Definition: iamf.h:528
IAMFCodecConfig::codec_id
enum AVCodecID codec_id
Definition: iamf.h:68
IAMFCodecConfig::codec_config_id
unsigned codec_config_id
Definition: iamf.h:67
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:615
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:135
AV_CHAN_BACK_LEFT
@ AV_CHAN_BACK_LEFT
Definition: channel_layout.h:54
AVIOContext::eof_reached
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:238
IAMF_ANCHOR_ELEMENT_DIALOGUE
@ IAMF_ANCHOR_ELEMENT_DIALOGUE
Definition: iamf.h:141
AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN
@ AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN
Subblocks are of struct type AVIAMFMixGain.
Definition: iamf.h:173
MAX_IAMF_OBU_HEADER_SIZE
#define MAX_IAMF_OBU_HEADER_SIZE
Definition: iamf.h:34
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:321
mix_presentation_obu
static int mix_presentation_obu(void *s, IAMFContext *c, AVIOContext *pb, int len)
Definition: iamf_parse.c:932
av_iamf_audio_element_add_layer
AVIAMFLayer * av_iamf_audio_element_add_layer(AVIAMFAudioElement *audio_element)
Allocate a layer and add it to a given AVIAMFAudioElement.
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:449
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:272
AV_CODEC_ID_PCM_S32LE
@ AV_CODEC_ID_PCM_S32LE
Definition: codec_id.h:346
AVIAMFAudioElement::demixing_info
AVIAMFParamDefinition * demixing_info
Demixing information used to reconstruct a scalable channel audio representation.
Definition: iamf.h:379
AVIAMFDemixingInfo::dmixp_mode
unsigned int dmixp_mode
Pre-defined combination of demixing parameters.
Definition: iamf.h:140
mem.h
AVIAMFSubmix::output_mix_config
AVIAMFParamDefinition * output_mix_config
Information required for post-processing the mixed audio signal to generate the audio signal for play...
Definition: iamf.h:598
AVIAMFLayer::ambisonics_mode
enum AVIAMFAmbisonicsMode ambisonics_mode
Ambisonics mode as defined in section 3.6.3 of IAMF.
Definition: iamf.h:328
sample_format
enum AVSampleFormat sample_format
Definition: mediacodecdec_common.c:101
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:153
AV_CHANNEL_LAYOUT_BINAURAL
#define AV_CHANNEL_LAYOUT_BINAURAL
Definition: channel_layout.h:431
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:394
AV_IAMF_SUBMIX_LAYOUT_TYPE_BINAURAL
@ AV_IAMF_SUBMIX_LAYOUT_TYPE_BINAURAL
The layout is binaural.
Definition: iamf.h:508
AVIAMFLayer::flags
unsigned int flags
A bitmask which may contain a combination of AV_IAMF_LAYER_FLAG_* flags.
Definition: iamf.h:302
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVIAMFLayer::output_gain
AVRational output_gain
Output gain as defined in section 3.6.2 of IAMF.
Definition: iamf.h:316
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:86
AV_CHAN_TOP_FRONT_LEFT
@ AV_CHAN_TOP_FRONT_LEFT
Definition: channel_layout.h:62
AVIAMFSubmixElement::element_mix_config
AVIAMFParamDefinition * element_mix_config
Information required required for applying any processing to the referenced and rendered Audio Elemen...
Definition: iamf.h:464
AV_CHAN_AMBISONIC_BASE
@ AV_CHAN_AMBISONIC_BASE
Range of channels between AV_CHAN_AMBISONIC_BASE and AV_CHAN_AMBISONIC_END represent Ambisonic compon...
Definition: channel_layout.h:108
AVIAMFParamDefinition::parameter_rate
unsigned int parameter_rate
Sample rate for the parameter substream.
Definition: iamf.h:222
ff_iamf_scalable_ch_layouts
const AVChannelLayout ff_iamf_scalable_ch_layouts[10]
Definition: iamf.c:27
AVIAMFSubmixElement
Submix element as defined in section 3.7 of IAMF.
Definition: iamf.h:449
AV_IAMF_AUDIO_ELEMENT_TYPE_CHANNEL
@ AV_IAMF_AUDIO_ELEMENT_TYPE_CHANNEL
Definition: iamf.h:348
IAMFAudioElement::codec_config_id
unsigned int codec_config_id
Definition: iamf.h:101
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
AV_CH_BACK_RIGHT
#define AV_CH_BACK_RIGHT
Definition: channel_layout.h:180
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:665
AVIAMFAudioElement::recon_gain_info
AVIAMFParamDefinition * recon_gain_info
Recon gain information used to reconstruct a scalable channel audio representation.
Definition: iamf.h:386
AV_CHAN_FRONT_LEFT
@ AV_CHAN_FRONT_LEFT
Definition: channel_layout.h:50
IAMFMixPresentation::mix
AVIAMFMixPresentation * mix
mix backs cmix iff the AVIAMFMixPresentation is owned by this structure.
Definition: iamf.h:113
scalable_channel_layout_config
static int scalable_channel_layout_config(void *s, AVIOContext *pb, IAMFAudioElement *audio_element, const IAMFCodecConfig *codec_config)
Definition: iamf_parse.c:385
flac.h
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:97
IAMFSubStream::codecpar
AVCodecParameters * codecpar
Definition: iamf.h:86
put_bits.h
IAMFCodecConfig::audio_roll_distance
int audio_roll_distance
Definition: iamf.h:71
IAMFMixPresentation::mix_presentation_id
unsigned int mix_presentation_id
Definition: iamf.h:114
AV_CODEC_ID_PCM_S24BE
@ AV_CODEC_ID_PCM_S24BE
Definition: codec_id.h:351
AVChannelCustom::id
enum AVChannel id
Definition: channel_layout.h:284
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:383
AV_WL16A
#define AV_WL16A(p, v)
Definition: intreadwrite.h:557
AV_CH_SIDE_LEFT
#define AV_CH_SIDE_LEFT
Definition: channel_layout.h:184
AV_IAMF_PARAMETER_DEFINITION_DEMIXING
@ AV_IAMF_PARAMETER_DEFINITION_DEMIXING
Subblocks are of struct type AVIAMFDemixingInfo.
Definition: iamf.h:177
MPEG4AudioConfig::sample_rate
int sample_rate
Definition: mpeg4audio.h:32