FFmpeg
iamfdec.c
Go to the documentation of this file.
1 /*
2  * Immersive Audio Model and Formats demuxer
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/intreadwrite.h"
24 #include "avformat.h"
25 #include "demux.h"
26 #include "iamf.h"
27 #include "iamf_reader.h"
28 #include "iamf_parse.h"
29 #include "internal.h"
30 
31 //return < 0 if we need more data
32 static int get_score(const uint8_t *buf, int buf_size, enum IAMF_OBU_Type type, int *seq)
33 {
35  if (buf_size < 4 || AV_RB32(buf) != MKBETAG('i','a','m','f'))
36  return 0;
37  *seq = 1;
38  return -1;
39  }
41  return *seq ? -1 : 0;
43  return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
44  return 0;
45 }
46 
47 static int iamf_probe(const AVProbeData *p)
48 {
49  unsigned obu_size;
50  enum IAMF_OBU_Type type;
51  int seq = 0, cnt = 0, start_pos;
52  int ret;
53 
54  while (1) {
55  int size = ff_iamf_parse_obu_header(p->buf + cnt, p->buf_size - cnt,
56  &obu_size, &start_pos, &type,
57  NULL, NULL);
58  if (size < 0)
59  return 0;
60 
61  ret = get_score(p->buf + cnt + start_pos,
62  p->buf_size - cnt - start_pos,
63  type, &seq);
64  if (ret >= 0)
65  return ret;
66 
67  cnt += FFMIN(size, p->buf_size - cnt);
68  }
69  return 0;
70 }
71 
73 {
74  IAMFDemuxContext *const c = s->priv_data;
75  IAMFContext *const iamf = &c->iamf;
76  int ret;
77 
78  ret = ff_iamfdec_read_descriptors(iamf, s->pb, INT_MAX, s);
79  if (ret < 0)
80  return ret;
81 
82  for (int i = 0; i < iamf->nb_audio_elements; i++) {
83  IAMFAudioElement *audio_element = iamf->audio_elements[i];
85 
86  if (!stg)
87  return AVERROR(ENOMEM);
88 
90  stg->id = audio_element->audio_element_id;
91  /* Transfer ownership */
92  stg->params.iamf_audio_element = audio_element->element;
93  audio_element->element = NULL;
94 
95  for (int j = 0; j < audio_element->nb_substreams; j++) {
96  IAMFSubStream *substream = &audio_element->substreams[j];
98 
99  if (!st)
100  return AVERROR(ENOMEM);
101 
103  if (ret < 0)
104  return ret;
105 
106  ret = avcodec_parameters_copy(st->codecpar, substream->codecpar);
107  if (ret < 0)
108  return ret;
109 
110  if (!i && !j && audio_element->layers[0].substream_count == 1)
112  else
114  st->id = substream->audio_substream_id;
115  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
116  }
117  }
118 
119  for (int i = 0; i < iamf->nb_mix_presentations; i++) {
120  IAMFMixPresentation *mix_presentation = iamf->mix_presentations[i];
122  const AVIAMFMixPresentation *mix = mix_presentation->cmix;
123 
124  if (!stg)
125  return AVERROR(ENOMEM);
126 
128  stg->id = mix_presentation->mix_presentation_id;
129  /* Transfer ownership */
130  stg->params.iamf_mix_presentation = mix_presentation->mix;
131  mix_presentation->mix = NULL;
132 
133  for (int j = 0; j < mix->nb_submixes; j++) {
134  const AVIAMFSubmix *sub_mix = mix->submixes[j];
135 
136  for (int k = 0; k < sub_mix->nb_elements; k++) {
137  const AVIAMFSubmixElement *submix_element = sub_mix->elements[k];
138  AVStreamGroup *audio_element = NULL;
139 
140  for (int l = 0; l < s->nb_stream_groups; l++)
141  if (s->stream_groups[l]->type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT &&
142  s->stream_groups[l]->id == submix_element->audio_element_id) {
143  audio_element = s->stream_groups[l];
144  break;
145  }
146  av_assert0(audio_element);
147 
148  for (int l = 0; l < audio_element->nb_streams; l++) {
149  ret = avformat_stream_group_add_stream(stg, audio_element->streams[l]);
150  if (ret < 0 && ret != AVERROR(EEXIST))
151  return ret;
152  }
153  }
154  }
155  }
156 
157  return 0;
158 }
159 
161 {
162  IAMFDemuxContext *const c = s->priv_data;
163  int ret;
164 
165  ret = ff_iamf_read_packet(s, c, s->pb, INT_MAX, pkt);
166  if (ret < 0)
167  return ret;
168 
169  return 0;
170 }
171 
173 {
174  IAMFDemuxContext *const c = s->priv_data;
175 
177 
178  return 0;
179 }
180 
182  .p.name = "iamf",
183  .p.long_name = NULL_IF_CONFIG_SMALL("Raw Immersive Audio Model and Formats"),
184  .p.extensions = "iamf",
186  .priv_data_size = sizeof(IAMFDemuxContext),
187  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
192 };
AVIAMFSubmix::elements
AVIAMFSubmixElement ** elements
Array of submix elements.
Definition: iamf.h:552
AVFMT_NO_BYTE_SEEK
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:487
AVStreamGroup::id
int64_t id
Group type-specific group ID.
Definition: avformat.h:1109
mix
static int mix(int c0, int c1)
Definition: 4xm.c:716
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
AVFMT_SHOW_IDS
#define AVFMT_SHOW_IDS
Show format stream IDs numbers.
Definition: avformat.h:477
IAMF_OBU_IA_SEQUENCE_HEADER
@ IAMF_OBU_IA_SEQUENCE_HEADER
Definition: iamf.h:63
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
IAMFAudioElement::nb_substreams
unsigned int nb_substreams
Definition: iamf.h:99
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
AV_DISPOSITION_DEFAULT
#define AV_DISPOSITION_DEFAULT
The stream should be chosen by default among other streams of the same type, unless the user has expl...
Definition: avformat.h:621
iamf_probe
static int iamf_probe(const AVProbeData *p)
Definition: iamfdec.c:47
av_iamf_mix_presentation_free
void av_iamf_mix_presentation_free(AVIAMFMixPresentation **pmix_presentation)
Free an AVIAMFMixPresentation and all its contents.
Definition: iamf.c:534
iamf_read_header
static int iamf_read_header(AVFormatContext *s)
Definition: iamfdec.c:72
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
iamf_parse.h
IAMFMixPresentation::cmix
const AVIAMFMixPresentation * cmix
Definition: iamf.h:108
ff_iamf_read_packet
int ff_iamf_read_packet(AVFormatContext *s, IAMFDemuxContext *c, AVIOContext *pb, int max_size, AVPacket *pkt)
Definition: iamf_reader.c:267
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:853
av_iamf_audio_element_free
void av_iamf_audio_element_free(AVIAMFAudioElement **paudio_element)
Free an AVIAMFAudioElement and all its contents.
Definition: iamf.c:336
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
AVStreamGroup::params
union AVStreamGroup::@305 params
Group type-specific parameters.
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
IAMFAudioElement::element
AVIAMFAudioElement * element
element backs celement iff the AVIAMFAudioElement is owned by this structure.
Definition: iamf.h:95
IAMFContext::audio_elements
IAMFAudioElement ** audio_elements
Definition: iamf.h:131
avassert.h
pkt
AVPacket * pkt
Definition: movenc.c:60
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION
@ AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION
Definition: avformat.h:1083
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
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
ff_iamfdec_read_descriptors
int ff_iamfdec_read_descriptors(IAMFContext *c, AVIOContext *pb, int max_size, void *log_ctx)
Definition: iamf_parse.c:1041
IAMFContext::nb_mix_presentations
int nb_mix_presentations
Definition: iamf.h:134
FF_INFMT_FLAG_INIT_CLEANUP
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: demux.h:35
IAMFAudioElement::audio_element_id
unsigned int audio_element_id
Definition: iamf.h:96
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
iamf_read_packet
static int iamf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: iamfdec.c:160
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
NULL
#define NULL
Definition: coverity.c:32
IAMF_OBU_IA_CODEC_CONFIG
@ IAMF_OBU_IA_CODEC_CONFIG
Definition: iamf.h:38
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
IAMFSubStream
Definition: iamf.h:82
IAMFAudioElement::layers
IAMFLayer * layers
Definition: iamf.h:103
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
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
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:986
AVIAMFSubmixElement::audio_element_id
unsigned int audio_element_id
The id of the Audio Element this submix element references.
Definition: iamf.h:443
avformat_stream_group_add_stream
int avformat_stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
Add an already allocated stream to a stream group.
Definition: options.c:495
AVIAMFSubmix
Submix layout as defined in section 3.7 of IAMF.
Definition: iamf.h:543
IAMFAudioElement
Definition: iamf.h:89
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
size
int size
Definition: twinvq_data.h:10344
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
AV_RB32
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_RB32
Definition: bytestream.h:96
AVStreamGroup::iamf_audio_element
struct AVIAMFAudioElement * iamf_audio_element
Definition: avformat.h:1123
IAMFContext
Definition: iamf.h:128
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
IAMFAudioElement::substreams
IAMFSubStream * substreams
Definition: iamf.h:98
AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT
@ AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT
Definition: avformat.h:1082
AVStreamGroup::streams
AVStream ** streams
A list of streams in the group.
Definition: avformat.h:1156
AVStreamGroup::iamf_mix_presentation
struct AVIAMFMixPresentation * iamf_mix_presentation
Definition: avformat.h:1124
IAMFContext::nb_audio_elements
int nb_audio_elements
Definition: iamf.h:132
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
get_score
static int get_score(const uint8_t *buf, int buf_size, enum IAMF_OBU_Type type, int *seq)
Definition: iamfdec.c:32
AVIAMFMixPresentation
Information on how to render and mix one or more AVIAMFAudioElement to generate the final audio outpu...
Definition: iamf.h:600
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
demux.h
AV_DISPOSITION_DEPENDENT
#define AV_DISPOSITION_DEPENDENT
The stream is intended to be mixed with another stream before presentation.
Definition: avformat.h:709
IAMF_OBU_IA_AUDIO_FRAME
@ IAMF_OBU_IA_AUDIO_FRAME
Definition: iamf.h:43
AVStream::disposition
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:812
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:755
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
ff_iamf_demuxer
const FFInputFormat ff_iamf_demuxer
Definition: iamfdec.c:181
avformat.h
iamf_read_close
static int iamf_read_close(AVFormatContext *s)
Definition: iamfdec.c:172
IAMF_OBU_Type
IAMF_OBU_Type
Definition: iamf.h:37
AVStreamGroup
Definition: avformat.h:1090
IAMFMixPresentation
Definition: iamf.h:107
AVStreamGroup::nb_streams
unsigned int nb_streams
Number of elements in AVStreamGroup.streams.
Definition: avformat.h:1143
AVIAMFSubmix::nb_elements
unsigned int nb_elements
Number of elements in the submix.
Definition: iamf.h:559
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
IAMFContext::mix_presentations
IAMFMixPresentation ** mix_presentations
Definition: iamf.h:133
avformat_stream_group_create
AVStreamGroup * avformat_stream_group_create(AVFormatContext *s, enum AVStreamGroupParamsType type, AVDictionary **options)
Add a new empty stream group to a media file.
Definition: options.c:422
AVPacket
This structure stores compressed data.
Definition: packet.h:501
FFInputFormat
Definition: demux.h:37
iamf.h
AVIAMFSubmixElement
Submix element as defined in section 3.7 of IAMF.
Definition: iamf.h:437
IAMF_OBU_IA_TEMPORAL_DELIMITER
@ IAMF_OBU_IA_TEMPORAL_DELIMITER
Definition: iamf.h:42
IAMFDemuxContext
Definition: iamf_reader.h:32
IAMFMixPresentation::mix
AVIAMFMixPresentation * mix
mix backs cmix iff the AVIAMFMixPresentation is owned by this structure.
Definition: iamf.h:113
IAMFSubStream::codecpar
AVCodecParameters * codecpar
Definition: iamf.h:86
IAMFMixPresentation::mix_presentation_id
unsigned int mix_presentation_id
Definition: iamf.h:114
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:106
iamf_reader.h
ff_iamf_read_deinit
void ff_iamf_read_deinit(IAMFDemuxContext *c)
Definition: iamf_reader.c:327
IAMF_OBU_IA_AUDIO_FRAME_ID17
@ IAMF_OBU_IA_AUDIO_FRAME_ID17
Definition: iamf.h:61