FFmpeg
liblc3dec.c
Go to the documentation of this file.
1 /*
2  * LC3 decoder wrapper
3  * Copyright (C) 2024 Antoine Soulier <asoulier@google.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <lc3.h>
21 
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/mem.h"
24 
25 #include "avcodec.h"
26 #include "codec.h"
27 #include "codec_internal.h"
28 #include "decode.h"
29 #include "internal.h"
30 
31 #define DECODER_MAX_CHANNELS 2
32 
33 typedef struct LibLC3DecContext {
35  void *decoder_mem;
36  lc3_decoder_t decoder[DECODER_MAX_CHANNELS];
38 
40 {
41  LibLC3DecContext *liblc3 = avctx->priv_data;
42  int channels = avctx->ch_layout.nb_channels;
43  int ep_mode;
44  unsigned decoder_size;
45 
46  if (avctx->extradata_size < 6)
47  return AVERROR_INVALIDDATA;
49  av_log(avctx, AV_LOG_ERROR,
50  "Invalid number of channels %d. Max %d channels are accepted\n",
52  return AVERROR(EINVAL);
53  }
54 
55  liblc3->frame_us = AV_RL16(avctx->extradata + 0) * 10;
56  liblc3->srate_hz = avctx->sample_rate;
57  ep_mode = AV_RL16(avctx->extradata + 2);
58  liblc3->hr_mode = AV_RL16(avctx->extradata + 4);
59  if (ep_mode != 0) {
60  av_log(avctx, AV_LOG_ERROR,
61  "Error protection mode is not supported.\n");
62  return AVERROR(EINVAL);
63  }
64 
65  av_log(avctx, AV_LOG_INFO,
66  "Decoding %.1f ms frames.\n", liblc3->frame_us / 1000.f);
67  if (liblc3->hr_mode)
68  av_log(avctx, AV_LOG_INFO, "High-resolution mode enabled.\n");
69 
70  decoder_size = lc3_hr_decoder_size(
71  liblc3->hr_mode, liblc3->frame_us, liblc3->srate_hz);
72  if (!decoder_size)
73  return AVERROR_INVALIDDATA;
74 
75  liblc3->decoder_mem = av_malloc_array(channels, decoder_size);
76  if (!liblc3->decoder_mem)
77  return AVERROR(ENOMEM);
78 
79  for (int ch = 0; ch < channels; ch++) {
80  liblc3->decoder[ch] = lc3_hr_setup_decoder(
81  liblc3->hr_mode, liblc3->frame_us, liblc3->srate_hz, 0,
82  (char *)liblc3->decoder_mem + ch * decoder_size);
83  }
84 
87 
88  avctx->delay = lc3_hr_delay_samples(
89  liblc3->hr_mode, liblc3->frame_us, liblc3->srate_hz);
90  avctx->internal->skip_samples = avctx->delay;
91 
92  return 0;
93 }
94 
96 {
97  LibLC3DecContext *liblc3 = avctx->priv_data;
98 
99  av_freep(&liblc3->decoder_mem);
100 
101  return 0;
102 }
103 
105  int *got_frame_ptr, AVPacket *avpkt)
106 {
107  LibLC3DecContext *liblc3 = avctx->priv_data;
108  int channels = avctx->ch_layout.nb_channels;
109  uint8_t *in = avpkt->data;
110  int block_bytes, ret;
111 
112  frame->nb_samples = av_rescale(
113  liblc3->frame_us, liblc3->srate_hz, 1000*1000);
114  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
115  return ret;
116 
117  block_bytes = avpkt->size;
118  int is_planar = avctx->sample_fmt == AV_SAMPLE_FMT_FLTP;
119 
120  for (int ch = 0; ch < channels; ch++) {
121  int nbytes = block_bytes / channels + (ch < block_bytes % channels);
122  float *pcm_data = is_planar ? (float*)frame->extended_data[ch] :
123  (float*)frame->extended_data[0] + ch;
124  int stride = is_planar ? 1 : channels;
125 
126  ret = lc3_decode(liblc3->decoder[ch], in, nbytes,
127  LC3_PCM_FORMAT_FLOAT, pcm_data, stride);
128  if (ret < 0)
129  return AVERROR_INVALIDDATA;
130 
131  in += nbytes;
132  }
133 
134  frame->nb_samples = FFMIN(frame->nb_samples, avpkt->duration);
135 
136  *got_frame_ptr = 1;
137 
138  return avpkt->size;
139 }
140 
142  .p.name = "liblc3",
143  CODEC_LONG_NAME("LC3 (Low Complexity Communication Codec)"),
144  .p.type = AVMEDIA_TYPE_AUDIO,
145  .p.id = AV_CODEC_ID_LC3,
146  .p.capabilities = AV_CODEC_CAP_DR1,
147  .p.wrapper_name = "liblc3",
148  .priv_data_size = sizeof(LibLC3DecContext),
149  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
153 };
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:66
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
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
LibLC3DecContext::frame_us
int frame_us
Definition: liblc3dec.c:34
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1032
AVCodecInternal::skip_samples
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
Definition: internal.h:125
LibLC3DecContext
Definition: liblc3dec.c:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:588
FFCodec
Definition: codec_internal.h:127
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:606
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:329
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:583
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:197
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1047
liblc3_decode_init
static av_cold int liblc3_decode_init(AVCodecContext *avctx)
Definition: liblc3dec.c:39
AV_CODEC_ID_LC3
@ AV_CODEC_ID_LC3
Definition: codec_id.h:566
codec.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
av_cold
#define av_cold
Definition: attributes.h:106
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:523
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:347
intreadwrite.h
ff_liblc3_decoder
const FFCodec ff_liblc3_decoder
Definition: liblc3dec.c:141
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
channels
channels
Definition: aptx.h:31
decode.h
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
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:474
DECODER_MAX_CHANNELS
#define DECODER_MAX_CHANNELS
Definition: liblc3dec.c:31
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1729
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:550
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:589
codec_internal.h
LibLC3DecContext::decoder_mem
void * decoder_mem
Definition: liblc3dec.c:35
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1039
liblc3_decode
static int liblc3_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: liblc3dec.c:104
LibLC3DecContext::srate_hz
int srate_hz
Definition: liblc3dec.c:34
LibLC3DecContext::hr_mode
int hr_mode
Definition: liblc3dec.c:34
AVCodecContext::request_sample_fmt
enum AVSampleFormat request_sample_fmt
desired sample format
Definition: avcodec.h:1087
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:522
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
liblc3_decode_close
static av_cold int liblc3_decode_close(AVCodecContext *avctx)
Definition: liblc3dec.c:95
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
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
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
LibLC3DecContext::decoder
lc3_decoder_t decoder[DECODER_MAX_CHANNELS]
Definition: liblc3dec.c:36
AVCodecContext
main external API structure.
Definition: avcodec.h:439
mem.h
AVPacket
This structure stores compressed data.
Definition: packet.h:565
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:466
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
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
stride
#define stride
Definition: h264pred_template.c:536
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:60