/src/ffmpeg/libavformat/iamfdec.c
Line | Count | Source |
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 | 559k | { |
34 | 559k | if (type == IAMF_OBU_IA_SEQUENCE_HEADER) { |
35 | 53.5k | if (buf_size < 4 || AV_RB32(buf) != MKBETAG('i','a','m','f')) |
36 | 52.5k | return 0; |
37 | 911 | *seq = 1; |
38 | 911 | return -1; |
39 | 53.5k | } |
40 | 505k | if (type >= IAMF_OBU_IA_CODEC_CONFIG && type <= IAMF_OBU_IA_TEMPORAL_DELIMITER) |
41 | 258k | return *seq ? -1 : 0; |
42 | 247k | if (type >= IAMF_OBU_IA_AUDIO_FRAME && type <= IAMF_OBU_IA_AUDIO_FRAME_ID17) |
43 | 233k | return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0; |
44 | 13.7k | return 0; |
45 | 247k | } |
46 | | |
47 | | static int iamf_probe(const AVProbeData *p) |
48 | 964k | { |
49 | 964k | unsigned obu_size; |
50 | 964k | enum IAMF_OBU_Type type; |
51 | 964k | int seq = 0, cnt = 0, start_pos; |
52 | 964k | int ret; |
53 | | |
54 | 971k | while (1) { |
55 | 971k | int size = ff_iamf_parse_obu_header(p->buf + cnt, p->buf_size - cnt, |
56 | 971k | &obu_size, &start_pos, &type, |
57 | 971k | NULL, NULL); |
58 | 971k | if (size < 0) |
59 | 412k | return 0; |
60 | | |
61 | 559k | ret = get_score(p->buf + cnt + start_pos, |
62 | 559k | p->buf_size - cnt - start_pos, |
63 | 559k | type, &seq); |
64 | 559k | if (ret >= 0) |
65 | 552k | return ret; |
66 | | |
67 | 6.78k | cnt += FFMIN(size, p->buf_size - cnt); |
68 | 6.78k | } |
69 | 0 | return 0; |
70 | 964k | } |
71 | | |
72 | | static int iamf_read_header(AVFormatContext *s) |
73 | 4.30k | { |
74 | 4.30k | IAMFDemuxContext *const c = s->priv_data; |
75 | 4.30k | IAMFContext *const iamf = &c->iamf; |
76 | 4.30k | int ret; |
77 | | |
78 | 4.30k | ret = ff_iamfdec_read_descriptors(iamf, s->pb, INT_MAX, s); |
79 | 4.30k | if (ret < 0) |
80 | 2.58k | return ret; |
81 | | |
82 | 3.35k | for (int i = 0; i < iamf->nb_audio_elements; i++) { |
83 | 1.63k | IAMFAudioElement *audio_element = iamf->audio_elements[i]; |
84 | 1.63k | AVStreamGroup *stg = avformat_stream_group_create(s, AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT, NULL); |
85 | | |
86 | 1.63k | if (!stg) |
87 | 0 | return AVERROR(ENOMEM); |
88 | | |
89 | 1.63k | av_iamf_audio_element_free(&stg->params.iamf_audio_element); |
90 | 1.63k | stg->id = audio_element->audio_element_id; |
91 | | /* Transfer ownership */ |
92 | 1.63k | stg->params.iamf_audio_element = audio_element->element; |
93 | 1.63k | audio_element->element = NULL; |
94 | | |
95 | 10.1k | for (int j = 0; j < audio_element->nb_substreams; j++) { |
96 | 8.53k | IAMFSubStream *substream = &audio_element->substreams[j]; |
97 | 8.53k | AVStream *st = avformat_new_stream(s, NULL); |
98 | | |
99 | 8.53k | if (!st) |
100 | 0 | return AVERROR(ENOMEM); |
101 | | |
102 | 8.53k | ret = avformat_stream_group_add_stream(stg, st); |
103 | 8.53k | if (ret < 0) |
104 | 0 | return ret; |
105 | | |
106 | 8.53k | ret = avcodec_parameters_copy(st->codecpar, substream->codecpar); |
107 | 8.53k | if (ret < 0) |
108 | 0 | return ret; |
109 | | |
110 | 8.53k | if (!i && !j && audio_element->layers[0].substream_count == 1) |
111 | 123 | st->disposition |= AV_DISPOSITION_DEFAULT; |
112 | 8.41k | else if (audio_element->nb_layers > 1 || audio_element->layers[0].substream_count > 1) |
113 | 4.59k | st->disposition |= AV_DISPOSITION_DEPENDENT; |
114 | 8.53k | st->id = substream->audio_substream_id; |
115 | 8.53k | avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); |
116 | 8.53k | } |
117 | 1.63k | } |
118 | | |
119 | 2.04k | for (int i = 0; i < iamf->nb_mix_presentations; i++) { |
120 | 317 | IAMFMixPresentation *mix_presentation = iamf->mix_presentations[i]; |
121 | 317 | AVStreamGroup *stg = avformat_stream_group_create(s, AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION, NULL); |
122 | 317 | const AVIAMFMixPresentation *mix = mix_presentation->cmix; |
123 | | |
124 | 317 | if (!stg) |
125 | 0 | return AVERROR(ENOMEM); |
126 | | |
127 | 317 | av_iamf_mix_presentation_free(&stg->params.iamf_mix_presentation); |
128 | 317 | stg->id = mix_presentation->mix_presentation_id; |
129 | | /* Transfer ownership */ |
130 | 317 | stg->params.iamf_mix_presentation = mix_presentation->mix; |
131 | 317 | mix_presentation->mix = NULL; |
132 | | |
133 | 745 | for (int j = 0; j < mix->nb_submixes; j++) { |
134 | 428 | const AVIAMFSubmix *sub_mix = mix->submixes[j]; |
135 | | |
136 | 521 | for (int k = 0; k < sub_mix->nb_elements; k++) { |
137 | 93 | const AVIAMFSubmixElement *submix_element = sub_mix->elements[k]; |
138 | 93 | AVStreamGroup *audio_element = NULL; |
139 | | |
140 | 93 | for (int l = 0; l < s->nb_stream_groups; l++) |
141 | 93 | if (s->stream_groups[l]->type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT && |
142 | 93 | s->stream_groups[l]->id == submix_element->audio_element_id) { |
143 | 93 | audio_element = s->stream_groups[l]; |
144 | 93 | break; |
145 | 93 | } |
146 | 93 | av_assert0(audio_element); |
147 | | |
148 | 388 | for (int l = 0; l < audio_element->nb_streams; l++) { |
149 | 295 | ret = avformat_stream_group_add_stream(stg, audio_element->streams[l]); |
150 | 295 | if (ret < 0 && ret != AVERROR(EEXIST)) |
151 | 0 | return ret; |
152 | 295 | } |
153 | 93 | } |
154 | 428 | } |
155 | 317 | } |
156 | | |
157 | 1.72k | if (!s->nb_streams) |
158 | 112 | return AVERROR_INVALIDDATA; |
159 | | |
160 | 1.61k | return 0; |
161 | 1.72k | } |
162 | | |
163 | | static int iamf_read_packet(AVFormatContext *s, AVPacket *pkt) |
164 | 409k | { |
165 | 409k | IAMFDemuxContext *const c = s->priv_data; |
166 | 409k | int ret; |
167 | | |
168 | 409k | ret = ff_iamf_read_packet(s, c, s->pb, INT_MAX, 0, pkt); |
169 | 409k | if (ret < 0) |
170 | 2.98k | return ret; |
171 | | |
172 | 406k | return 0; |
173 | 409k | } |
174 | | |
175 | | static int iamf_read_close(AVFormatContext *s) |
176 | 4.30k | { |
177 | 4.30k | IAMFDemuxContext *const c = s->priv_data; |
178 | | |
179 | 4.30k | ff_iamf_read_deinit(c); |
180 | | |
181 | 4.30k | return 0; |
182 | 4.30k | } |
183 | | |
184 | | const FFInputFormat ff_iamf_demuxer = { |
185 | | .p.name = "iamf", |
186 | | .p.long_name = NULL_IF_CONFIG_SMALL("Raw Immersive Audio Model and Formats"), |
187 | | .p.extensions = "iamf", |
188 | | .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK | AVFMT_NOTIMESTAMPS | AVFMT_SHOW_IDS, |
189 | | .priv_data_size = sizeof(IAMFDemuxContext), |
190 | | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, |
191 | | .read_probe = iamf_probe, |
192 | | .read_header = iamf_read_header, |
193 | | .read_packet = iamf_read_packet, |
194 | | .read_close = iamf_read_close, |
195 | | }; |