FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/iamfdec.c
Date: 2024-05-03 15:42:48
Exec Total Coverage
Lines: 83 91 91.2%
Functions: 5 5 100.0%
Branches: 51 66 77.3%

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 6625 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 68 times.
✓ Branch 1 taken 6557 times.
6625 if (type == IAMF_OBU_IA_SEQUENCE_HEADER) {
35
3/4
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 11 times.
68 if (buf_size < 4 || AV_RB32(buf) != MKBETAG('i','a','m','f'))
36 57 return 0;
37 11 *seq = 1;
38 11 return -1;
39 }
40
2/2
✓ Branch 0 taken 4894 times.
✓ Branch 1 taken 1663 times.
6557 if (type >= IAMF_OBU_IA_CODEC_CONFIG && type <= IAMF_OBU_IA_TEMPORAL_DELIMITER)
41
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 4855 times.
4894 return *seq ? -1 : 0;
42
3/4
✓ Branch 0 taken 1663 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1634 times.
✓ Branch 3 taken 29 times.
1663 if (type >= IAMF_OBU_IA_AUDIO_FRAME && type <= IAMF_OBU_IA_AUDIO_FRAME_ID17)
43
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1623 times.
1634 return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
44 29 return 0;
45 }
46
47 7128 static int iamf_probe(const AVProbeData *p)
48 {
49 unsigned obu_size;
50 enum IAMF_OBU_Type type;
51 7128 int seq = 0, cnt = 0, start_pos;
52 int ret;
53
54 50 while (1) {
55 7178 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 553 times.
✓ Branch 1 taken 6625 times.
7178 if (size < 0)
59 553 return 0;
60
61 6625 ret = get_score(p->buf + cnt + start_pos,
62 6625 p->buf_size - cnt - start_pos,
63 type, &seq);
64
2/2
✓ Branch 0 taken 6575 times.
✓ Branch 1 taken 50 times.
6625 if (ret >= 0)
65 6575 return ret;
66
67 50 cnt += FFMIN(size, p->buf_size - cnt);
68 }
69 return 0;
70 }
71
72 13 static int iamf_read_header(AVFormatContext *s)
73 {
74 13 IAMFDemuxContext *const c = s->priv_data;
75 13 IAMFContext *const iamf = &c->iamf;
76 int ret;
77
78 13 ret = ff_iamfdec_read_descriptors(iamf, s->pb, INT_MAX, s);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0)
80 return ret;
81
82
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 for (int i = 0; i < iamf->nb_audio_elements; i++) {
83 13 IAMFAudioElement *audio_element = iamf->audio_elements[i];
84 13 AVStreamGroup *stg = avformat_stream_group_create(s, AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT, NULL);
85
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!stg)
87 return AVERROR(ENOMEM);
88
89 13 av_iamf_audio_element_free(&stg->params.iamf_audio_element);
90 13 stg->id = audio_element->audio_element_id;
91 /* Transfer ownership */
92 13 stg->params.iamf_audio_element = audio_element->element;
93 13 audio_element->element = NULL;
94
95
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 13 times.
69 for (int j = 0; j < audio_element->nb_substreams; j++) {
96 56 IAMFSubStream *substream = &audio_element->substreams[j];
97 56 AVStream *st = avformat_new_stream(s, NULL);
98
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (!st)
100 return AVERROR(ENOMEM);
101
102 56 ret = avformat_stream_group_add_stream(stg, st);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (ret < 0)
104 return ret;
105
106 56 ret = avcodec_parameters_copy(st->codecpar, substream->codecpar);
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (ret < 0)
108 return ret;
109
110
5/6
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 43 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 2 times.
56 if (!i && !j && audio_element->layers[0].substream_count == 1)
111 11 st->disposition |= AV_DISPOSITION_DEFAULT;
112 else
113 45 st->disposition |= AV_DISPOSITION_DEPENDENT;
114 56 st->id = substream->audio_substream_id;
115 56 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
116 }
117 }
118
119
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 for (int i = 0; i < iamf->nb_mix_presentations; i++) {
120 13 IAMFMixPresentation *mix_presentation = iamf->mix_presentations[i];
121 13 AVStreamGroup *stg = avformat_stream_group_create(s, AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION, NULL);
122 13 const AVIAMFMixPresentation *mix = mix_presentation->cmix;
123
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!stg)
125 return AVERROR(ENOMEM);
126
127 13 av_iamf_mix_presentation_free(&stg->params.iamf_mix_presentation);
128 13 stg->id = mix_presentation->mix_presentation_id;
129 /* Transfer ownership */
130 13 stg->params.iamf_mix_presentation = mix_presentation->mix;
131 13 mix_presentation->mix = NULL;
132
133
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 13 times.
28 for (int j = 0; j < mix->nb_submixes; j++) {
134 15 const AVIAMFSubmix *sub_mix = mix->submixes[j];
135
136
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
30 for (int k = 0; k < sub_mix->nb_elements; k++) {
137 15 const AVIAMFSubmixElement *submix_element = sub_mix->elements[k];
138 15 AVStreamGroup *audio_element = NULL;
139
140
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 for (int l = 0; l < s->nb_stream_groups; l++)
141
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (s->stream_groups[l]->type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT &&
142
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 s->stream_groups[l]->id == submix_element->audio_element_id) {
143 15 audio_element = s->stream_groups[l];
144 15 break;
145 }
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 av_assert0(audio_element);
147
148
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 15 times.
73 for (int l = 0; l < audio_element->nb_streams; l++) {
149 58 ret = avformat_stream_group_add_stream(stg, audio_element->streams[l]);
150
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
58 if (ret < 0 && ret != AVERROR(EEXIST))
151 return ret;
152 }
153 }
154 }
155 }
156
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (!s->nb_streams)
158 return AVERROR_INVALIDDATA;
159
160 13 return 0;
161 }
162
163 300 static int iamf_read_packet(AVFormatContext *s, AVPacket *pkt)
164 {
165 300 IAMFDemuxContext *const c = s->priv_data;
166 int ret;
167
168 300 ret = ff_iamf_read_packet(s, c, s->pb, INT_MAX, pkt);
169
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 295 times.
300 if (ret < 0)
170 5 return ret;
171
172 295 return 0;
173 }
174
175 13 static int iamf_read_close(AVFormatContext *s)
176 {
177 13 IAMFDemuxContext *const c = s->priv_data;
178
179 13 ff_iamf_read_deinit(c);
180
181 13 return 0;
182 }
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 };
196