FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/iamfenc.c
Date: 2026-04-25 22:17:55
Exec Total Coverage
Lines: 69 85 81.2%
Functions: 5 5 100.0%
Branches: 42 60 70.0%

Line Branch Exec Source
1 /*
2 * IAMF muxer
3 * Copyright (c) 2023 James Almer
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 <stdint.h>
23
24 #include "avformat.h"
25 #include "iamf.h"
26 #include "iamf_writer.h"
27 #include "internal.h"
28 #include "mux.h"
29
30 typedef struct IAMFMuxContext {
31 IAMFContext iamf;
32
33 int64_t descriptors_offset;
34 int update_extradata;
35
36 int first_stream_id;
37 } IAMFMuxContext;
38
39 8 static int iamf_init(AVFormatContext *s)
40 {
41 8 IAMFMuxContext *const c = s->priv_data;
42 8 IAMFContext *const iamf = &c->iamf;
43 8 int nb_audio_elements = 0, nb_mix_presentations = 0;
44 int ret;
45
46
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 8 times.
48 for (int i = 0; i < s->nb_streams; i++) {
47
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
48
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 (s->streams[i]->codecpar->codec_tag != MKTAG('m','p','4','a') &&
49
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 8 times.
40 s->streams[i]->codecpar->codec_tag != MKTAG('O','p','u','s') &&
50
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 s->streams[i]->codecpar->codec_tag != MKTAG('f','L','a','C') &&
51 s->streams[i]->codecpar->codec_tag != MKTAG('i','p','c','m'))) {
52 av_log(s, AV_LOG_ERROR, "Unsupported codec id %s\n",
53 avcodec_get_name(s->streams[i]->codecpar->codec_id));
54 return AVERROR(EINVAL);
55 }
56
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (s->streams[i]->codecpar->ch_layout.nb_channels > 2) {
58 av_log(s, AV_LOG_ERROR, "Unsupported channel layout on stream #%d\n", i);
59 return AVERROR(EINVAL);
60 }
61
62
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 40 times.
145 for (int j = 0; j < i; j++) {
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 105 times.
105 if (s->streams[i]->id == s->streams[j]->id) {
64 av_log(s, AV_LOG_ERROR, "Duplicated stream id %d\n", s->streams[j]->id);
65 return AVERROR(EINVAL);
66 }
67 }
68 }
69
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (s->nb_stream_groups <= 1) {
71 av_log(s, AV_LOG_ERROR, "There must be at least two stream groups\n");
72 return AVERROR(EINVAL);
73 }
74
75
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 8 times.
25 for (int i = 0; i < s->nb_stream_groups; i++) {
76 17 const AVStreamGroup *stg = s->stream_groups[i];
77
78
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8 times.
17 if (stg->type == AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT)
79 9 nb_audio_elements++;
80
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9 times.
17 if (stg->type == AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION)
81 8 nb_mix_presentations++;
82 }
83
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
8 if ((nb_audio_elements < 1 || nb_audio_elements > 2) || nb_mix_presentations < 1) {
84 av_log(s, AV_LOG_ERROR, "There must be >= 1 and <= 2 IAMF_AUDIO_ELEMENT and at least "
85 "one IAMF_MIX_PRESENTATION stream groups\n");
86 return AVERROR(EINVAL);
87 }
88
89
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 8 times.
25 for (int i = 0; i < s->nb_stream_groups; i++) {
90 17 const AVStreamGroup *stg = s->stream_groups[i];
91
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9 times.
17 if (stg->type != AV_STREAM_GROUP_PARAMS_IAMF_AUDIO_ELEMENT)
92 8 continue;
93
94 9 ret = ff_iamf_add_audio_element(iamf, stg, s);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
96 return ret;
97 }
98
99
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 8 times.
25 for (int i = 0; i < s->nb_stream_groups; i++) {
100 17 const AVStreamGroup *stg = s->stream_groups[i];
101
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 8 times.
17 if (stg->type != AV_STREAM_GROUP_PARAMS_IAMF_MIX_PRESENTATION)
102 9 continue;
103
104 8 ret = ff_iamf_add_mix_presentation(iamf, stg, s);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
106 return ret;
107 }
108
109 8 c->first_stream_id = s->streams[0]->id;
110
111 8 return 0;
112 }
113
114 8 static int iamf_write_header(AVFormatContext *s)
115 {
116 8 IAMFMuxContext *const c = s->priv_data;
117 8 IAMFContext *const iamf = &c->iamf;
118 int ret;
119
120 8 c->descriptors_offset = avio_tell(s->pb);
121 8 ret = ff_iamf_write_descriptors(iamf, s->pb, s);
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
123 return ret;
124
125 8 c->first_stream_id = s->streams[0]->id;
126
127 8 return 0;
128 }
129
130 1460 static int iamf_write_packet(AVFormatContext *s, AVPacket *pkt)
131 {
132 1460 IAMFMuxContext *const c = s->priv_data;
133 1460 AVStream *st = s->streams[pkt->stream_index];
134 1460 int ret = 0;
135
136
2/2
✓ Branch 0 taken 343 times.
✓ Branch 1 taken 1117 times.
1460 if (st->id == c->first_stream_id)
137 343 ret = ff_iamf_write_parameter_blocks(&c->iamf, s->pb, pkt, s);
138
1/2
✓ Branch 0 taken 1460 times.
✗ Branch 1 not taken.
1460 if (!ret)
139 1460 ret = ff_iamf_write_audio_frame(&c->iamf, s->pb, st->id, pkt);
140
3/4
✓ Branch 0 taken 1460 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 1428 times.
1460 if (!ret && !pkt->size)
141 32 c->update_extradata = 1;
142
143 1460 return ret;
144 }
145
146 8 static int iamf_write_trailer(AVFormatContext *s)
147 {
148 8 const IAMFMuxContext *const c = s->priv_data;
149 8 const IAMFContext *const iamf = &c->iamf;
150 int64_t pos;
151 int ret;
152
153
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
8 if (!c->update_extradata || !(s->pb->seekable & AVIO_SEEKABLE_NORMAL))
154 2 return 0;
155
156 6 pos = avio_tell(s->pb);
157 6 avio_seek(s->pb, c->descriptors_offset, SEEK_SET);
158 6 ret = ff_iamf_write_descriptors(iamf, s->pb, s);
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
160 return ret;
161
162 6 avio_seek(s->pb, pos, SEEK_SET);
163
164 6 return 0;
165 }
166
167 8 static void iamf_deinit(AVFormatContext *s)
168 {
169 8 IAMFMuxContext *const c = s->priv_data;
170 8 IAMFContext *const iamf = &c->iamf;
171
172 8 ff_iamf_uninit_context(iamf);
173 8 }
174
175 static const AVCodecTag iamf_codec_tags[] = {
176 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
177 { AV_CODEC_ID_FLAC, MKTAG('f','L','a','C') },
178 { AV_CODEC_ID_OPUS, MKTAG('O','p','u','s') },
179 { AV_CODEC_ID_PCM_S16LE, MKTAG('i','p','c','m') },
180 { AV_CODEC_ID_PCM_S16BE, MKTAG('i','p','c','m') },
181 { AV_CODEC_ID_PCM_S24LE, MKTAG('i','p','c','m') },
182 { AV_CODEC_ID_PCM_S24BE, MKTAG('i','p','c','m') },
183 { AV_CODEC_ID_PCM_S32LE, MKTAG('i','p','c','m') },
184 { AV_CODEC_ID_PCM_S32BE, MKTAG('i','p','c','m') },
185 { AV_CODEC_ID_NONE, MKTAG('i','p','c','m') }
186 };
187
188 const FFOutputFormat ff_iamf_muxer = {
189 .p.name = "iamf",
190 .p.long_name = NULL_IF_CONFIG_SMALL("Raw Immersive Audio Model and Formats"),
191 .p.extensions = "iamf",
192 .priv_data_size = sizeof(IAMFMuxContext),
193 .p.audio_codec = AV_CODEC_ID_OPUS,
194 .init = iamf_init,
195 .deinit = iamf_deinit,
196 .write_header = iamf_write_header,
197 .write_packet = iamf_write_packet,
198 .write_trailer = iamf_write_trailer,
199 .p.codec_tag = (const AVCodecTag* const []){ iamf_codec_tags, NULL },
200 .p.flags = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS,
201 };
202