1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/*
* IAMF muxer
* Copyright (c) 2023 James Almer
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include "libavutil/avassert.h"
#include "libavutil/common.h"
#include "libavutil/iamf.h"
#include "libavcodec/put_bits.h"
#include "avformat.h"
#include "avio_internal.h"
#include "iamf.h"
#include "iamf_writer.h"
#include "internal.h"
#include "mux.h"
typedef struct IAMFMuxContext {
IAMFContext iamf;
int first_stream_id;
} IAMFMuxContext;
static int iamf_init(AVFormatContext *s)
{
IAMFMuxContext *const c = s->priv_data;
IAMFContext *const iamf = &c->iamf;
int nb_audio_elements = 0, nb_mix_presentations = 0;
int ret;
if (!s->nb_streams) {
av_log(s, AV_LOG_ERROR, "There must be at least one stream\n");
return AVERROR(EINVAL);
}
for (int i = 0; i < s->nb_streams; i++) {
if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
(s->streams[i]->codecpar->codec_tag != MKTAG('m','p','4','a') &&
s->streams[i]->codecpar->codec_tag != MKTAG('O','p','u','s') &&
s->streams[i]->codecpar->codec_tag != MKTAG('f','L','a','C') &&
s->streams[i]->codecpar->codec_tag != MKTAG('i','p','c','m'))) {
av_log(s, AV_LOG_ERROR, "Unsupported codec id %s\n",
avcodec_get_name(s->streams[i]->codecpar->codec_id));
return AVERROR(EINVAL);
}
if (s->streams[i]->codecpar->ch_layout.nb_channels > 2) {
av_log(s, AV_LOG_ERROR, "Unsupported channel layout on stream #%d\n", i);
return AVERROR(EINVAL);
}
for (int j = 0; j < i; j++) {
if (s->streams[i]->id == s->streams[j]->id) {
av_log(s, AV_LOG_ERROR, "Duplicated stream id %d\n", s->streams[j]->id);
return AVERROR(EINVAL);
}
}
}
if (!s->nb_stream_groups) {
av_log(s, AV_LOG_ERROR, "There must be at least two stream groups\n");
return AVERROR(EINVAL);
}
for (int i = 0; i < s->nb_stream_groups; i++) {
const AVStreamGroup *stg = s->stream_groups[i];
if (stg->type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT)
nb_audio_elements++;
if (stg->type == AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION)
nb_mix_presentations++;
}
if ((nb_audio_elements < 1 || nb_audio_elements > 2) || nb_mix_presentations < 1) {
av_log(s, AV_LOG_ERROR, "There must be >= 1 and <= 2 IAMF_AUDIO_ELEMENT and at least "
"one IAMF_MIX_PRESENTATION stream groups\n");
return AVERROR(EINVAL);
}
for (int i = 0; i < s->nb_stream_groups; i++) {
const AVStreamGroup *stg = s->stream_groups[i];
if (stg->type != AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT)
continue;
ret = ff_iamf_add_audio_element(iamf, stg, s);
if (ret < 0)
return ret;
}
for (int i = 0; i < s->nb_stream_groups; i++) {
const AVStreamGroup *stg = s->stream_groups[i];
if (stg->type != AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION)
continue;
ret = ff_iamf_add_mix_presentation(iamf, stg, s);
if (ret < 0)
return ret;
}
c->first_stream_id = s->streams[0]->id;
return 0;
}
static int iamf_write_header(AVFormatContext *s)
{
IAMFMuxContext *const c = s->priv_data;
IAMFContext *const iamf = &c->iamf;
int ret;
ret = ff_iamf_write_descriptors(iamf, s->pb, s);
if (ret < 0)
return ret;
c->first_stream_id = s->streams[0]->id;
return 0;
}
static inline int rescale_rational(AVRational q, int b)
{
return av_clip_int16(av_rescale(q.num, b, q.den));
}
static int write_parameter_block(AVFormatContext *s, const AVIAMFParamDefinition *param)
{
const IAMFMuxContext *const c = s->priv_data;
const IAMFContext *const iamf = &c->iamf;
uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
IAMFParamDefinition *param_definition = ff_iamf_get_param_definition(iamf, param->parameter_id);
PutBitContext pb;
AVIOContext *dyn_bc;
uint8_t *dyn_buf = NULL;
int dyn_size, ret;
if (param->type > AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN) {
av_log(s, AV_LOG_DEBUG, "Ignoring side data with unknown type %u\n",
param->type);
return 0;
}
if (!param_definition) {
av_log(s, AV_LOG_ERROR, "Non-existent Parameter Definition with ID %u referenced by a packet\n",
param->parameter_id);
return AVERROR(EINVAL);
}
if (param->type != param_definition->param->type) {
av_log(s, AV_LOG_ERROR, "Inconsistent values for Parameter Definition "
"with ID %u in a packet\n",
param->parameter_id);
return AVERROR(EINVAL);
}
ret = avio_open_dyn_buf(&dyn_bc);
if (ret < 0)
return ret;
// Sequence Header
init_put_bits(&pb, header, sizeof(header));
put_bits(&pb, 5, IAMF_OBU_IA_PARAMETER_BLOCK);
put_bits(&pb, 3, 0);
flush_put_bits(&pb);
avio_write(s->pb, header, put_bytes_count(&pb, 1));
ffio_write_leb(dyn_bc, param->parameter_id);
if (!param_definition->mode) {
ffio_write_leb(dyn_bc, param->duration);
ffio_write_leb(dyn_bc, param->constant_subblock_duration);
if (param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, param->nb_subblocks);
}
for (int i = 0; i < param->nb_subblocks; i++) {
const void *subblock = av_iamf_param_definition_get_subblock(param, i);
switch (param->type) {
case AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN: {
const AVIAMFMixGain *mix = subblock;
if (!param_definition->mode && param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, mix->subblock_duration);
ffio_write_leb(dyn_bc, mix->animation_type);
avio_wb16(dyn_bc, rescale_rational(mix->start_point_value, 1 << 8));
if (mix->animation_type >= AV_IAMF_ANIMATION_TYPE_LINEAR)
avio_wb16(dyn_bc, rescale_rational(mix->end_point_value, 1 << 8));
if (mix->animation_type == AV_IAMF_ANIMATION_TYPE_BEZIER) {
avio_wb16(dyn_bc, rescale_rational(mix->control_point_value, 1 << 8));
avio_w8(dyn_bc, av_clip_uint8(av_rescale(mix->control_point_relative_time.num, 1 << 8,
mix->control_point_relative_time.den)));
}
break;
}
case AV_IAMF_PARAMETER_DEFINITION_DEMIXING: {
const AVIAMFDemixingInfo *demix = subblock;
if (!param_definition->mode && param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, demix->subblock_duration);
avio_w8(dyn_bc, demix->dmixp_mode << 5);
break;
}
case AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN: {
const AVIAMFReconGain *recon = subblock;
const AVIAMFAudioElement *audio_element = param_definition->audio_element->celement;
if (!param_definition->mode && param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, recon->subblock_duration);
if (!audio_element) {
av_log(s, AV_LOG_ERROR, "Invalid Parameter Definition with ID %u referenced by a packet\n", param->parameter_id);
return AVERROR(EINVAL);
}
for (int j = 0; j < audio_element->nb_layers; j++) {
const AVIAMFLayer *layer = audio_element->layers[j];
if (layer->flags & AV_IAMF_LAYER_FLAG_RECON_GAIN) {
unsigned int recon_gain_flags = 0;
int k = 0;
for (; k < 7; k++)
recon_gain_flags |= (1 << k) * !!recon->recon_gain[j][k];
for (; k < 12; k++)
recon_gain_flags |= (2 << k) * !!recon->recon_gain[j][k];
if (recon_gain_flags >> 8)
recon_gain_flags |= (1 << k);
ffio_write_leb(dyn_bc, recon_gain_flags);
for (k = 0; k < 12; k++) {
if (recon->recon_gain[j][k])
avio_w8(dyn_bc, recon->recon_gain[j][k]);
}
}
}
break;
}
default:
av_assert0(0);
}
}
dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
ffio_write_leb(s->pb, dyn_size);
avio_write(s->pb, dyn_buf, dyn_size);
ffio_free_dyn_buf(&dyn_bc);
return 0;
}
static int iamf_write_packet(AVFormatContext *s, AVPacket *pkt)
{
const IAMFMuxContext *const c = s->priv_data;
AVStream *st = s->streams[pkt->stream_index];
uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
PutBitContext pb;
AVIOContext *dyn_bc;
uint8_t *side_data, *dyn_buf = NULL;
unsigned int skip_samples = 0, discard_padding = 0;
size_t side_data_size;
int dyn_size, type = st->id <= 17 ? st->id + IAMF_OBU_IA_AUDIO_FRAME_ID0 : IAMF_OBU_IA_AUDIO_FRAME;
int ret;
if (!pkt->size) {
uint8_t *new_extradata = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, NULL);
if (!new_extradata)
return AVERROR_INVALIDDATA;
// TODO: update FLAC Streaminfo on seekable output
return 0;
}
if (s->nb_stream_groups && st->id == c->first_stream_id) {
AVIAMFParamDefinition *mix =
(AVIAMFParamDefinition *)av_packet_get_side_data(pkt, AV_PKT_DATA_IAMF_MIX_GAIN_PARAM, NULL);
AVIAMFParamDefinition *demix =
(AVIAMFParamDefinition *)av_packet_get_side_data(pkt, AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM, NULL);
AVIAMFParamDefinition *recon =
(AVIAMFParamDefinition *)av_packet_get_side_data(pkt, AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM, NULL);
if (mix) {
ret = write_parameter_block(s, mix);
if (ret < 0)
return ret;
}
if (demix) {
ret = write_parameter_block(s, demix);
if (ret < 0)
return ret;
}
if (recon) {
ret = write_parameter_block(s, recon);
if (ret < 0)
return ret;
}
}
side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES,
&side_data_size);
if (side_data && side_data_size >= 10) {
skip_samples = AV_RL32(side_data);
discard_padding = AV_RL32(side_data + 4);
}
ret = avio_open_dyn_buf(&dyn_bc);
if (ret < 0)
return ret;
init_put_bits(&pb, header, sizeof(header));
put_bits(&pb, 5, type);
put_bits(&pb, 1, 0); // obu_redundant_copy
put_bits(&pb, 1, skip_samples || discard_padding);
put_bits(&pb, 1, 0); // obu_extension_flag
flush_put_bits(&pb);
avio_write(s->pb, header, put_bytes_count(&pb, 1));
if (skip_samples || discard_padding) {
ffio_write_leb(dyn_bc, discard_padding);
ffio_write_leb(dyn_bc, skip_samples);
}
if (st->id > 17)
ffio_write_leb(dyn_bc, st->id);
dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
ffio_write_leb(s->pb, dyn_size + pkt->size);
avio_write(s->pb, dyn_buf, dyn_size);
ffio_free_dyn_buf(&dyn_bc);
avio_write(s->pb, pkt->data, pkt->size);
return 0;
}
static void iamf_deinit(AVFormatContext *s)
{
IAMFMuxContext *const c = s->priv_data;
IAMFContext *const iamf = &c->iamf;
ff_iamf_uninit_context(iamf);
return;
}
static const AVCodecTag iamf_codec_tags[] = {
{ AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
{ AV_CODEC_ID_FLAC, MKTAG('f','L','a','C') },
{ AV_CODEC_ID_OPUS, MKTAG('O','p','u','s') },
{ AV_CODEC_ID_PCM_S16LE, MKTAG('i','p','c','m') },
{ AV_CODEC_ID_PCM_S16BE, MKTAG('i','p','c','m') },
{ AV_CODEC_ID_PCM_S24LE, MKTAG('i','p','c','m') },
{ AV_CODEC_ID_PCM_S24BE, MKTAG('i','p','c','m') },
{ AV_CODEC_ID_PCM_S32LE, MKTAG('i','p','c','m') },
{ AV_CODEC_ID_PCM_S32BE, MKTAG('i','p','c','m') },
{ AV_CODEC_ID_NONE, MKTAG('i','p','c','m') }
};
const FFOutputFormat ff_iamf_muxer = {
.p.name = "iamf",
.p.long_name = NULL_IF_CONFIG_SMALL("Raw Immersive Audio Model and Formats"),
.p.extensions = "iamf",
.priv_data_size = sizeof(IAMFMuxContext),
.p.audio_codec = AV_CODEC_ID_OPUS,
.init = iamf_init,
.deinit = iamf_deinit,
.write_header = iamf_write_header,
.write_packet = iamf_write_packet,
.p.codec_tag = (const AVCodecTag* const []){ iamf_codec_tags, NULL },
.p.flags = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS,
};
|