2023-11-25 14:22:28 +00:00
|
|
|
/*
|
|
|
|
* Immersive Audio Model and Formats demuxer
|
|
|
|
* Copyright (c) 2023 James Almer <jamrial@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 "libavutil/avassert.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "avformat.h"
|
2024-02-10 14:50:43 +00:00
|
|
|
#include "demux.h"
|
2023-11-25 14:22:28 +00:00
|
|
|
#include "iamf.h"
|
2024-02-15 22:12:40 +00:00
|
|
|
#include "iamf_reader.h"
|
2023-11-25 14:22:28 +00:00
|
|
|
#include "iamf_parse.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
//return < 0 if we need more data
|
|
|
|
static int get_score(const uint8_t *buf, int buf_size, enum IAMF_OBU_Type type, int *seq)
|
|
|
|
{
|
|
|
|
if (type == IAMF_OBU_IA_SEQUENCE_HEADER) {
|
|
|
|
if (buf_size < 4 || AV_RB32(buf) != MKBETAG('i','a','m','f'))
|
|
|
|
return 0;
|
|
|
|
*seq = 1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (type >= IAMF_OBU_IA_CODEC_CONFIG && type <= IAMF_OBU_IA_TEMPORAL_DELIMITER)
|
|
|
|
return *seq ? -1 : 0;
|
|
|
|
if (type >= IAMF_OBU_IA_AUDIO_FRAME && type <= IAMF_OBU_IA_AUDIO_FRAME_ID17)
|
|
|
|
return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iamf_probe(const AVProbeData *p)
|
|
|
|
{
|
|
|
|
unsigned obu_size;
|
|
|
|
enum IAMF_OBU_Type type;
|
|
|
|
int seq = 0, cnt = 0, start_pos;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int size = ff_iamf_parse_obu_header(p->buf + cnt, p->buf_size - cnt,
|
|
|
|
&obu_size, &start_pos, &type,
|
|
|
|
NULL, NULL);
|
|
|
|
if (size < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = get_score(p->buf + cnt + start_pos,
|
|
|
|
p->buf_size - cnt - start_pos,
|
|
|
|
type, &seq);
|
|
|
|
if (ret >= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
cnt += FFMIN(size, p->buf_size - cnt);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iamf_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
IAMFDemuxContext *const c = s->priv_data;
|
|
|
|
IAMFContext *const iamf = &c->iamf;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ff_iamfdec_read_descriptors(iamf, s->pb, INT_MAX, s);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (int i = 0; i < iamf->nb_audio_elements; i++) {
|
|
|
|
IAMFAudioElement *audio_element = iamf->audio_elements[i];
|
|
|
|
AVStreamGroup *stg = avformat_stream_group_create(s, AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT, NULL);
|
|
|
|
|
|
|
|
if (!stg)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2024-01-23 12:58:39 +00:00
|
|
|
av_iamf_audio_element_free(&stg->params.iamf_audio_element);
|
2023-11-25 14:22:28 +00:00
|
|
|
stg->id = audio_element->audio_element_id;
|
2024-02-19 19:22:22 +00:00
|
|
|
/* Transfer ownership */
|
2023-11-25 14:22:28 +00:00
|
|
|
stg->params.iamf_audio_element = audio_element->element;
|
2024-02-19 19:22:22 +00:00
|
|
|
audio_element->element = NULL;
|
2023-11-25 14:22:28 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < audio_element->nb_substreams; j++) {
|
|
|
|
IAMFSubStream *substream = &audio_element->substreams[j];
|
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
|
|
|
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
ret = avformat_stream_group_add_stream(stg, st);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = avcodec_parameters_copy(st->codecpar, substream->codecpar);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2024-08-14 16:46:53 +00:00
|
|
|
if (!i && !j && audio_element->layers[0].substream_count == 1)
|
2024-02-18 21:04:14 +00:00
|
|
|
st->disposition |= AV_DISPOSITION_DEFAULT;
|
|
|
|
else
|
|
|
|
st->disposition |= AV_DISPOSITION_DEPENDENT;
|
2023-11-25 14:22:28 +00:00
|
|
|
st->id = substream->audio_substream_id;
|
|
|
|
avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < iamf->nb_mix_presentations; i++) {
|
|
|
|
IAMFMixPresentation *mix_presentation = iamf->mix_presentations[i];
|
|
|
|
AVStreamGroup *stg = avformat_stream_group_create(s, AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION, NULL);
|
2024-02-19 19:22:22 +00:00
|
|
|
const AVIAMFMixPresentation *mix = mix_presentation->cmix;
|
2023-11-25 14:22:28 +00:00
|
|
|
|
|
|
|
if (!stg)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2024-01-23 12:58:39 +00:00
|
|
|
av_iamf_mix_presentation_free(&stg->params.iamf_mix_presentation);
|
2023-11-25 14:22:28 +00:00
|
|
|
stg->id = mix_presentation->mix_presentation_id;
|
2024-02-19 19:22:22 +00:00
|
|
|
/* Transfer ownership */
|
2023-11-25 14:22:28 +00:00
|
|
|
stg->params.iamf_mix_presentation = mix_presentation->mix;
|
2024-02-19 19:22:22 +00:00
|
|
|
mix_presentation->mix = NULL;
|
2023-11-25 14:22:28 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < mix->nb_submixes; j++) {
|
2024-02-19 19:22:22 +00:00
|
|
|
const AVIAMFSubmix *sub_mix = mix->submixes[j];
|
2023-11-25 14:22:28 +00:00
|
|
|
|
|
|
|
for (int k = 0; k < sub_mix->nb_elements; k++) {
|
2024-02-19 19:22:22 +00:00
|
|
|
const AVIAMFSubmixElement *submix_element = sub_mix->elements[k];
|
2023-11-25 14:22:28 +00:00
|
|
|
AVStreamGroup *audio_element = NULL;
|
|
|
|
|
|
|
|
for (int l = 0; l < s->nb_stream_groups; l++)
|
|
|
|
if (s->stream_groups[l]->type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT &&
|
|
|
|
s->stream_groups[l]->id == submix_element->audio_element_id) {
|
|
|
|
audio_element = s->stream_groups[l];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
av_assert0(audio_element);
|
|
|
|
|
|
|
|
for (int l = 0; l < audio_element->nb_streams; l++) {
|
|
|
|
ret = avformat_stream_group_add_stream(stg, audio_element->streams[l]);
|
|
|
|
if (ret < 0 && ret != AVERROR(EEXIST))
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 18:51:45 +00:00
|
|
|
if (!s->nb_streams)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2023-11-25 14:22:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-02-15 22:12:40 +00:00
|
|
|
static int iamf_read_packet(AVFormatContext *s, AVPacket *pkt)
|
2023-11-25 14:22:28 +00:00
|
|
|
{
|
|
|
|
IAMFDemuxContext *const c = s->priv_data;
|
2024-02-15 22:12:40 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ff_iamf_read_packet(s, c, s->pb, INT_MAX, pkt);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2023-11-25 14:22:28 +00:00
|
|
|
|
2024-02-15 22:12:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iamf_read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
IAMFDemuxContext *const c = s->priv_data;
|
2023-11-25 14:22:28 +00:00
|
|
|
|
2024-02-15 22:12:40 +00:00
|
|
|
ff_iamf_read_deinit(c);
|
2023-11-25 14:22:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-02-10 14:50:43 +00:00
|
|
|
const FFInputFormat ff_iamf_demuxer = {
|
|
|
|
.p.name = "iamf",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("Raw Immersive Audio Model and Formats"),
|
|
|
|
.p.extensions = "iamf",
|
|
|
|
.p.flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK | AVFMT_NOTIMESTAMPS | AVFMT_SHOW_IDS,
|
2023-11-25 14:22:28 +00:00
|
|
|
.priv_data_size = sizeof(IAMFDemuxContext),
|
2024-03-15 17:08:11 +00:00
|
|
|
.flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
|
2023-11-25 14:22:28 +00:00
|
|
|
.read_probe = iamf_probe,
|
|
|
|
.read_header = iamf_read_header,
|
|
|
|
.read_packet = iamf_read_packet,
|
|
|
|
.read_close = iamf_read_close,
|
|
|
|
};
|