FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/iamfdec.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 100 108 92.6%
Functions: 5 5 100.0%
Branches: 67 80 83.8%

Line Branch Exec 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 6711 static int get_score(const uint8_t *buf, int buf_size, enum IAMF_OBU_Type type, int *seq)
33 {
34
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 6640 times.
6711 if (type == IAMF_OBU_IA_SEQUENCE_HEADER) {
35
3/4
✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 14 times.
71 if (buf_size < 4 || AV_RB32(buf) != MKBETAG('i','a','m','f'))
36 57 return 0;
37 14 *seq = 1;
38 14 return -1;
39 }
40
2/2
✓ Branch 0 taken 4959 times.
✓ Branch 1 taken 1681 times.
6640 if (type >= IAMF_OBU_IA_CODEC_CONFIG && type <= IAMF_OBU_IA_TEMPORAL_DELIMITER)
41
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 4908 times.
4959 return *seq ? -1 : 0;
42
3/4
✓ Branch 0 taken 1681 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1652 times.
✓ Branch 3 taken 29 times.
1681 if (type >= IAMF_OBU_IA_AUDIO_FRAME && type <= IAMF_OBU_IA_AUDIO_FRAME_ID17)
43
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1638 times.
1652 return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
44 29 return 0;
45 }
46
47 7203 static int iamf_probe(const AVProbeData *p)
48 {
49 unsigned obu_size;
50 enum IAMF_OBU_Type type;
51 7203 int seq = 0, cnt = 0, start_pos;
52 int ret;
53
54 65 while (1) {
55 7268 int size = ff_iamf_parse_obu_header(p->buf + cnt, p->buf_size - cnt,
56 &obu_size, &start_pos, &type,
57 NULL, NULL);
58
2/2
✓ Branch 0 taken 557 times.
✓ Branch 1 taken 6711 times.
7268 if (size < 0)
59 557 return 0;
60
61 6711 ret = get_score(p->buf + cnt + start_pos,
62 6711 p->buf_size - cnt - start_pos,
63 type, &seq);
64
2/2
✓ Branch 0 taken 6646 times.
✓ Branch 1 taken 65 times.
6711 if (ret >= 0)
65 6646 return ret;
66
67 65 cnt += FFMIN(size, p->buf_size - cnt);
68 }
69 return 0;
70 }
71
72 17 static int iamf_read_header(AVFormatContext *s)
73 {
74 17 IAMFDemuxContext *const c = s->priv_data;
75 17 IAMFContext *const iamf = &c->iamf;
76 int ret;
77
78 17 ret = ff_iamfdec_read_descriptors(iamf, s->pb, INT_MAX, s);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0)
80 return ret;
81
82
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 17 times.
36 for (int i = 0; i < iamf->nb_audio_elements; i++) {
83 19 IAMFAudioElement *audio_element = iamf->audio_elements[i];
84 19 const AVIAMFLayer *layer = audio_element->element->layers[audio_element->nb_layers - 1];
85 19 AVStreamGroup *stg = avformat_stream_group_create(s, AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT, NULL);
86 19 int coupled_substream_count = audio_element->layers[audio_element->nb_layers - 1].coupled_substream_count;
87 19 int side_substream_id = -1, back_substream_id = -1;
88
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!stg)
90 return AVERROR(ENOMEM);
91
92 19 av_iamf_audio_element_free(&stg->params.iamf_audio_element);
93 19 stg->id = audio_element->audio_element_id;
94 /* Transfer ownership */
95 19 stg->params.iamf_audio_element = audio_element->element;
96 19 audio_element->element = NULL;
97
98
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 19 times.
97 for (int j = 0, k = 0; j < audio_element->nb_substreams; j++) {
99 78 IAMFSubStream *substream = &audio_element->substreams[j];
100 78 AVStream *st = avformat_new_stream(s, NULL);
101
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (!st)
103 return AVERROR(ENOMEM);
104
105 78 ret = avformat_stream_group_add_stream(stg, st);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (ret < 0)
107 return ret;
108
109 78 ret = avcodec_parameters_copy(st->codecpar, substream->codecpar);
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 78 times.
78 if (ret < 0)
111 return ret;
112
113
6/6
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 59 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 4 times.
78 if (!i && !j && audio_element->layers[0].substream_count == 1)
114 13 st->disposition |= AV_DISPOSITION_DEFAULT;
115
4/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 2 times.
65 else if (audio_element->nb_layers > 1 || audio_element->layers[0].substream_count > 1)
116 63 st->disposition |= AV_DISPOSITION_DEPENDENT;
117
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 74 times.
78 if (k == av_channel_layout_index_from_channel(&layer->ch_layout, AV_CHAN_BACK_LEFT))
118 4 back_substream_id = j;
119
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 63 times.
74 else if (k == av_channel_layout_index_from_channel(&layer->ch_layout, AV_CHAN_SIDE_LEFT))
120 11 side_substream_id = j;
121 78 st->id = substream->audio_substream_id;
122 78 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
123
124
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 49 times.
78 k += 1 + (coupled_substream_count-- > 0);
125 }
126
127 // Swap back and side stream ids as our native channel layout ordering doen't match the
128 // order from ITU-R - BS.2051-3 for Systems I and J (where side channels come before back ones).
129
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
23 if (back_substream_id >= 0 && av_channel_layout_compare(&layer->ch_layout,
130 4 &(AVChannelLayout)AV_CHANNEL_LAYOUT_9POINT1POINT6)) {
131 2 const IAMFSubStream *back_substream = &audio_element->substreams[back_substream_id];
132 2 const IAMFSubStream *side_substream = &audio_element->substreams[side_substream_id];
133 2 AVStream *back_st = stg->streams[back_substream_id];
134 2 AVStream *side_st = stg->streams[side_substream_id];
135
136 2 back_st->id = side_substream->audio_substream_id;
137 2 side_st->id = back_substream->audio_substream_id;
138 }
139 }
140
141
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
34 for (int i = 0; i < iamf->nb_mix_presentations; i++) {
142 17 IAMFMixPresentation *mix_presentation = iamf->mix_presentations[i];
143 17 AVStreamGroup *stg = avformat_stream_group_create(s, AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION, NULL);
144 17 const AVIAMFMixPresentation *mix = mix_presentation->cmix;
145
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!stg)
147 return AVERROR(ENOMEM);
148
149 17 av_iamf_mix_presentation_free(&stg->params.iamf_mix_presentation);
150 17 stg->id = mix_presentation->mix_presentation_id;
151 /* Transfer ownership */
152 17 stg->params.iamf_mix_presentation = mix_presentation->mix;
153 17 mix_presentation->mix = NULL;
154
155
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 17 times.
36 for (int j = 0; j < mix->nb_submixes; j++) {
156 19 const AVIAMFSubmix *sub_mix = mix->submixes[j];
157
158
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 19 times.
40 for (int k = 0; k < sub_mix->nb_elements; k++) {
159 21 const AVIAMFSubmixElement *submix_element = sub_mix->elements[k];
160 21 AVStreamGroup *audio_element = NULL;
161
162
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 for (int l = 0; l < s->nb_stream_groups; l++)
163
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (s->stream_groups[l]->type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT &&
164
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 2 times.
23 s->stream_groups[l]->id == submix_element->audio_element_id) {
165 21 audio_element = s->stream_groups[l];
166 21 break;
167 }
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 av_assert0(audio_element);
169
170
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 21 times.
101 for (int l = 0; l < audio_element->nb_streams; l++) {
171 80 ret = avformat_stream_group_add_stream(stg, audio_element->streams[l]);
172
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 78 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
80 if (ret < 0 && ret != AVERROR(EEXIST))
173 return ret;
174 }
175 }
176 }
177 }
178
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!s->nb_streams)
180 return AVERROR_INVALIDDATA;
181
182 17 return 0;
183 }
184
185 435 static int iamf_read_packet(AVFormatContext *s, AVPacket *pkt)
186 {
187 435 IAMFDemuxContext *const c = s->priv_data;
188 int ret;
189
190 435 ret = ff_iamf_read_packet(s, c, s->pb, INT_MAX, pkt);
191
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 428 times.
435 if (ret < 0)
192 7 return ret;
193
194 428 return 0;
195 }
196
197 17 static int iamf_read_close(AVFormatContext *s)
198 {
199 17 IAMFDemuxContext *const c = s->priv_data;
200
201 17 ff_iamf_read_deinit(c);
202
203 17 return 0;
204 }
205
206 const FFInputFormat ff_iamf_demuxer = {
207 .p.name = "iamf",
208 .p.long_name = NULL_IF_CONFIG_SMALL("Raw Immersive Audio Model and Formats"),
209 .p.extensions = "iamf",
210 .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK | AVFMT_NOTIMESTAMPS | AVFMT_SHOW_IDS,
211 .priv_data_size = sizeof(IAMFDemuxContext),
212 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
213 .read_probe = iamf_probe,
214 .read_header = iamf_read_header,
215 .read_packet = iamf_read_packet,
216 .read_close = iamf_read_close,
217 };
218