FFmpeg
lc3.c
Go to the documentation of this file.
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 
133  st->codecpar->sample_rate = srate_hz;
134  st->codecpar->bit_rate = bit_rate;
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 
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 int lc3_write_header(AVFormatContext *s)
190 {
191  AVStream *st = s->streams[0];
193  int srate_hz = st->codecpar->sample_rate;
194  int bit_rate = st->codecpar->bit_rate;
195  int frame_us, ep_mode, hr_mode;
196  uint32_t nb_samples = av_rescale_q(
197  st->duration, st->time_base, (AVRational){ 1, srate_hz });
198 
199  if (st->codecpar->extradata_size < 6)
200  return AVERROR_INVALIDDATA;
201 
202  frame_us = AV_RL16(st->codecpar->extradata + 0) * 10;
203  ep_mode = AV_RL16(st->codecpar->extradata + 2) != 0;
204  hr_mode = AV_RL16(st->codecpar->extradata + 4) != 0;
205 
206  if (check_frame_length(s, srate_hz, frame_us) < 0)
207  return AVERROR_INVALIDDATA;
208 
209  avio_wb16(s->pb, 0x1ccc);
210  avio_wl16(s->pb, (9 + hr_mode) * sizeof(uint16_t));
211  avio_wl16(s->pb, srate_hz / 100);
212  avio_wl16(s->pb, bit_rate / 100);
213  avio_wl16(s->pb, channels);
214  avio_wl16(s->pb, frame_us / 10);
215  avio_wl16(s->pb, ep_mode);
216  avio_wl32(s->pb, nb_samples);
217  if (hr_mode)
218  avio_wl16(s->pb, hr_mode);
219 
220  return 0;
221 }
222 
223 static int lc3_write_packet(AVFormatContext *s, AVPacket *pkt)
224 {
225  avio_wl16(s->pb, pkt->size);
226  avio_write(s->pb, pkt->data, pkt->size);
227  return 0;
228 }
229 
231  .p.name = "lc3",
232  .p.long_name = NULL_IF_CONFIG_SMALL("LC3 (Low Complexity Communication Codec)"),
233  .p.extensions = "lc3",
234  .p.audio_codec = AV_CODEC_ID_LC3,
235  .p.video_codec = AV_CODEC_ID_NONE,
236  .p.subtitle_codec = AV_CODEC_ID_NONE,
237  .p.flags = AVFMT_NOTIMESTAMPS,
238  .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
240  .write_header = lc3_write_header,
241  .write_packet = lc3_write_packet,
242 };
243 
244 #endif /* CONFIG_LC3_MUXER */
frame_samples
static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
Definition: sync_queue.c:141
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
AVPacket::data
uint8_t * data
Definition: packet.h:524
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:542
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition: mux.h:59
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
avpriv_update_cur_dts
void avpriv_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
Definition: seek.c:36
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:437
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:853
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:417
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
AV_CODEC_ID_LC3
@ AV_CODEC_ID_LC3
Definition: codec_id.h:546
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:802
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
channels
channels
Definition: aptx.h:31
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:782
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
FFOutputFormat
Definition: mux.h:61
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:73
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
f
f
Definition: af_crystalizer.c:121
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:525
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
avio.h
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:410
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:357
check_frame_length
static int check_frame_length(void *avcl, int srate_hz, int frame_us)
Definition: lc3.c:46
ff_lc3_demuxer
const FFInputFormat ff_lc3_demuxer
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
packet.h
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition: mux.h:50
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
demux.h
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:104
tag
uint32_t tag
Definition: movenc.c:1787
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:746
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:501
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:544
FFInputFormat
Definition: demux.h:37
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:443
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_lc3_muxer
const FFOutputFormat ff_lc3_muxer
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:240
mux.h