History

avformat/lc3: add file format for LC3/LC3plus transport

A file format is described in Bluetooth SIG LC3 and ETSI TS 103 634, for test purpose.

Antoine SOULIER committed 2 years ago

Commit 257bc2a1 parent b2bc4ac5 files · +263 −0Browse files

Showing the changes to libavformat (3 of 5 files). Show all

Unified · Split

3 files
  1. libavformat/Makefile +2 −0
  2. libavformat/allformats.c +2 −0
  3. libavformat/lc3.c +252 −0
libavformat/Makefile+2−0
@@ -332,6 +332,8 @@ OBJS-$(CONFIG_KVAG_DEMUXER) += kvag.o · 331 unmodified lines
332332 OBJS-$(CONFIG_KVAG_MUXER) += kvag.o rawenc.o
333333 OBJS-$(CONFIG_LAF_DEMUXER) += lafdec.o
334334 OBJS-$(CONFIG_LATM_MUXER) += latmenc.o rawenc.o
335+OBJS-$(CONFIG_LC3_DEMUXER) += lc3.o
336+OBJS-$(CONFIG_LC3_MUXER) += lc3.o
335337 OBJS-$(CONFIG_LMLM4_DEMUXER) += lmlm4.o
336338 OBJS-$(CONFIG_LOAS_DEMUXER) += loasdec.o rawdec.o
337339 OBJS-$(CONFIG_LUODAT_DEMUXER) += luodatdec.o
libavformat/allformats.c+2−0
@@ -252,6 +252,8 @@ extern const FFInputFormat ff_kvag_demuxer; · 251 unmodified lines
252252 extern const FFOutputFormat ff_kvag_muxer;
253253 extern const FFInputFormat ff_laf_demuxer;
254254 extern const FFOutputFormat ff_latm_muxer;
255+extern const FFInputFormat ff_lc3_demuxer;
256+extern const FFOutputFormat ff_lc3_muxer;
255257 extern const FFInputFormat ff_lmlm4_demuxer;
256258 extern const FFInputFormat ff_loas_demuxer;
257259 extern const FFInputFormat ff_luodat_demuxer;
libavformat/lc3.c+252−0
@@ -0,0 +1,252 @@
1+/*
2+ * LC3 muxer and demuxer
3+ * Copyright (C) 2024 Antoine Soulier <asoulier@google.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+/**
23+ * @file
24+ * Based on the file format specified by :
25+ *
26+ * - Bluetooth SIG - Low Complexity Communication Codec Test Suite
27+ * https://www.bluetooth.org/docman/handlers/downloaddoc.ashx?doc_id=502301
28+ * 3.2.8.2 Reference LC3 Codec Bitstream Format
29+ *
30+ * - ETSI TI 103 634 V1.4.1 - Low Complexity Communication Codec plus
31+ * https://www.etsi.org/deliver/etsi_ts/103600_103699/103634/01.04.01_60/ts_103634v010401p.pdf
32+ * LC3plus conformance script package
33+ */
34+
35+#include "config_components.h"
36+
37+#include "libavcodec/packet.h"
38+#include "libavutil/intreadwrite.h"
39+
40+#include "avformat.h"
41+#include "avio.h"
42+#include "demux.h"
43+#include "internal.h"
44+#include "mux.h"
45+
46+static int check_frame_length(void *avcl, int srate_hz, int frame_us)
47+{
48+ if (srate_hz != 8000 && srate_hz != 16000 && srate_hz != 24000 &&
49+ srate_hz != 32000 && srate_hz != 48000 && srate_hz != 96000) {
50+ if (avcl)
51+ av_log(avcl, AV_LOG_ERROR,
52+ "Invalid LC3 sample rate: %d Hz.\n", srate_hz);
53+ return -1;
54+ }
55+
56+ if (frame_us != 2500 && frame_us != 5000 &&
57+ frame_us != 7500 && frame_us != 10000) {
58+ if (avcl)
59+ av_log(avcl, AV_LOG_ERROR,
60+ "Invalid LC3 frame duration: %.1f ms.\n", frame_us / 1000.f);
61+ return -1;
62+ }
63+
64+ return 0;
65+}
66+
67+#if CONFIG_LC3_DEMUXER
68+
69+typedef struct LC3DemuxContext {
70+ int frame_samples;
71+ int64_t end_dts;
72+} LC3DemuxContext;
73+
74+static int lc3_read_probe(const AVProbeData *p)
75+{
76+ int frame_us, srate_hz;
77+
78+ if (p->buf_size < 12)
79+ return 0;
80+
81+ if (AV_RB16(p->buf + 0) != 0x1ccc ||
82+ AV_RL16(p->buf + 2) < 9 * sizeof(uint16_t))
83+ return 0;
84+
85+ srate_hz = AV_RL16(p->buf + 4) * 100;
86+ frame_us = AV_RL16(p->buf + 10) * 10;
87+ if (check_frame_length(NULL, srate_hz, frame_us) < 0)
88+ return 0;
89+
90+ return AVPROBE_SCORE_MAX;
91+}
92+
93+static int lc3_read_header(AVFormatContext *s)
94+{
95+ LC3DemuxContext *lc3 = s->priv_data;
96+ AVStream *st = NULL;
97+ uint16_t tag, hdr_size;
98+ uint32_t length;
99+ int srate_hz, frame_us, channels, bit_rate;
100+ int ep_mode, hr_mode;
101+ int num_extra_params;
102+ int delay, ret;
103+
104+ tag = avio_rb16(s->pb);
105+ hdr_size = avio_rl16(s->pb);
106+
107+ if (tag != 0x1ccc || hdr_size < 9 * sizeof(uint16_t))
108+ return AVERROR_INVALIDDATA;
109+
110+ num_extra_params = hdr_size / sizeof(uint16_t) - 9;
111+
112+ srate_hz = avio_rl16(s->pb) * 100;
113+ bit_rate = avio_rl16(s->pb) * 100;
114+ channels = avio_rl16(s->pb);
115+ frame_us = avio_rl16(s->pb) * 10;
116+ ep_mode = avio_rl16(s->pb) != 0;
117+ length = avio_rl32(s->pb);
118+ hr_mode = num_extra_params >= 1 && avio_rl16(s->pb);
119+
120+ if (check_frame_length(s, srate_hz, frame_us) < 0)
121+ return AVERROR_INVALIDDATA;
122+
123+ st = avformat_new_stream(s, NULL);
124+ if (!st)
125+ return AVERROR(ENOMEM);
126+
127+ avpriv_set_pts_info(st, 64, 1, srate_hz);
128+ avpriv_update_cur_dts(s, st, 0);
129+ st->duration = length;
130+
131+ st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
132+ st->codecpar->codec_id = AV_CODEC_ID_LC3;
133+ st->codecpar->sample_rate = srate_hz;
134+ st->codecpar->bit_rate = bit_rate;
135+ st->codecpar->ch_layout.nb_channels = channels;
136+
137+ if ((ret = ff_alloc_extradata(st->codecpar, 6)) < 0)
138+ return ret;
139+
140+ AV_WL16(st->codecpar->extradata + 0, frame_us / 10);
141+ AV_WL16(st->codecpar->extradata + 2, ep_mode);
142+ AV_WL16(st->codecpar->extradata + 4, hr_mode);
143+
144+ lc3->frame_samples = av_rescale(frame_us, srate_hz, 1000*1000);
145+
146+ delay = av_rescale(frame_us == 7500 ? 4000 : 2500, srate_hz, 1000*1000);
147+ lc3->end_dts = length ? length + delay : -1;
148+
149+ return 0;
150+}
151+
152+static int lc3_read_packet(AVFormatContext *s, AVPacket *pkt)
153+{
154+ LC3DemuxContext *lc3 = s->priv_data;
155+ AVStream *st = s->streams[0];
156+ AVIOContext *pb = s->pb;
157+ int64_t pos = avio_tell(pb);
158+ int64_t remaining_samples;
159+ int ret;
160+
161+ ret = av_get_packet(s->pb, pkt, avio_rl16(pb));
162+ if (ret < 0)
163+ return ret;
164+
165+ pkt->pos = pos;
166+
167+ remaining_samples = lc3->end_dts < 0 ? lc3->frame_samples :
168+ FFMAX(lc3->end_dts - ffstream(st)->cur_dts, 0);
169+ pkt->duration = FFMIN(lc3->frame_samples, remaining_samples);
170+
171+ return 0;
172+}
173+
174+const FFInputFormat ff_lc3_demuxer = {
175+ .p.name = "lc3",
176+ .p.long_name = NULL_IF_CONFIG_SMALL("LC3 (Low Complexity Communication Codec)"),
177+ .p.extensions = "lc3",
178+ .p.flags = AVFMT_GENERIC_INDEX,
179+ .priv_data_size = sizeof(LC3DemuxContext),
180+ .read_probe = lc3_read_probe,
181+ .read_header = lc3_read_header,
182+ .read_packet = lc3_read_packet,
183+};
184+
185+#endif /* CONFIG_LC3_DEMUXER */
186+
187+#if CONFIG_LC3_MUXER
188+
189+static av_cold int lc3_muxer_init(AVFormatContext *s)
190+{
191+ if (s->nb_streams != 1) {
192+ av_log(s, AV_LOG_ERROR, "This muxer only supports a single stream.\n");
193+ return AVERROR(EINVAL);
194+ }
195+
196+ return 0;
197+}
198+
199+static int lc3_write_header(AVFormatContext *s)
200+{
201+ AVStream *st = s->streams[0];
202+ int channels = st->codecpar->ch_layout.nb_channels;
203+ int srate_hz = st->codecpar->sample_rate;
204+ int bit_rate = st->codecpar->bit_rate;
205+ int frame_us, ep_mode, hr_mode;
206+ uint32_t nb_samples = av_rescale_q(
207+ st->duration, st->time_base, (AVRational){ 1, srate_hz });
208+
209+ if (st->codecpar->extradata_size < 6)
210+ return AVERROR_INVALIDDATA;
211+
212+ frame_us = AV_RL16(st->codecpar->extradata + 0) * 10;
213+ ep_mode = AV_RL16(st->codecpar->extradata + 2) != 0;
214+ hr_mode = AV_RL16(st->codecpar->extradata + 4) != 0;
215+
216+ if (check_frame_length(s, srate_hz, frame_us) < 0)
217+ return AVERROR_INVALIDDATA;
218+
219+ avio_wb16(s->pb, 0x1ccc);
220+ avio_wl16(s->pb, (9 + hr_mode) * sizeof(uint16_t));
221+ avio_wl16(s->pb, srate_hz / 100);
222+ avio_wl16(s->pb, bit_rate / 100);
223+ avio_wl16(s->pb, channels);
224+ avio_wl16(s->pb, frame_us / 10);
225+ avio_wl16(s->pb, ep_mode);
226+ avio_wl32(s->pb, nb_samples);
227+ if (hr_mode)
228+ avio_wl16(s->pb, hr_mode);
229+
230+ return 0;
231+}
232+
233+static int lc3_write_packet(AVFormatContext *s, AVPacket *pkt)
234+{
235+ avio_wl16(s->pb, pkt->size);
236+ avio_write(s->pb, pkt->data, pkt->size);
237+ return 0;
238+}
239+
240+const FFOutputFormat ff_lc3_muxer = {
241+ .p.name = "lc3",
242+ .p.long_name = NULL_IF_CONFIG_SMALL("LC3 (Low Complexity Communication Codec)"),
243+ .p.extensions = "lc3",
244+ .p.audio_codec = AV_CODEC_ID_LC3,
245+ .p.video_codec = AV_CODEC_ID_NONE,
246+ .p.flags = AVFMT_NOTIMESTAMPS,
247+ .init = lc3_muxer_init,
248+ .write_header = lc3_write_header,
249+ .write_packet = lc3_write_packet,
250+};
251+
252+#endif /* CONFIG_LC3_MUXER */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t