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 
86  avctx->delay = lc3_hr_delay_samples(
87  liblc3->hr_mode, liblc3->frame_us, liblc3->srate_hz);
88  avctx->internal->skip_samples = avctx->delay;
89 
90  return 0;
91 }
92 
94 {
95  LibLC3DecContext *liblc3 = avctx->priv_data;
96 
97  av_freep(&liblc3->decoder_mem);
98 
99  return 0;
100 }
101 
103  int *got_frame_ptr, AVPacket *avpkt)
104 {
105  LibLC3DecContext *liblc3 = avctx->priv_data;
106  int channels = avctx->ch_layout.nb_channels;
107  uint8_t *in = avpkt->data;
108  int block_bytes, ret;
109 
110  frame->nb_samples = av_rescale(
111  liblc3->frame_us, liblc3->srate_hz, 1000*1000);
112  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
113  return ret;
114 
115  block_bytes = avpkt->size;
116  for (int ch = 0; ch < channels; ch++) {
117  int nbytes = block_bytes / channels + (ch < block_bytes % channels);
118 
119  ret = lc3_decode(liblc3->decoder[ch], in, nbytes,
120  LC3_PCM_FORMAT_FLOAT, frame->data[ch], 1);
121  if (ret < 0)
122  return AVERROR_INVALIDDATA;
123 
124  in += nbytes;
125  }
126 
127  frame->nb_samples = FFMIN(frame->nb_samples, avpkt->duration);
128 
129  *got_frame_ptr = 1;
130 
131  return avpkt->size;
132 }
133 
135  .p.name = "liblc3",
136  CODEC_LONG_NAME("LC3 (Low Complexity Communication Codec)"),
137  .p.type = AVMEDIA_TYPE_AUDIO,
138  .p.id = AV_CODEC_ID_LC3,
139  .p.capabilities = AV_CODEC_CAP_DR1,
140  .p.wrapper_name = "liblc3",
141  .priv_data_size = sizeof(LibLC3DecContext),
142  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
144  .close = liblc3_decode_close,
146 };
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:43
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:1056
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:389
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:539
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:557
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:607
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1071
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:552
codec.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:530
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
intreadwrite.h
ff_liblc3_decoder
const FFCodec ff_liblc3_decoder
Definition: liblc3dec.c:134
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
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:296
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:486
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:1697
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
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:540
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:1063
liblc3_decode
static int liblc3_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: liblc3dec.c:102
LibLC3DecContext::srate_hz
int srate_hz
Definition: liblc3dec.c:34
LibLC3DecContext::hr_mode
int hr_mode
Definition: liblc3dec.c:34
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:529
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
liblc3_decode_close
static av_cold int liblc3_decode_close(AVCodecContext *avctx)
Definition: liblc3dec.c:93
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
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:264
LibLC3DecContext::decoder
lc3_decoder_t decoder[DECODER_MAX_CHANNELS]
Definition: liblc3dec.c:36
AVCodecContext
main external API structure.
Definition: avcodec.h:451
mem.h
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
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