FFmpeg
qoadec.c
Go to the documentation of this file.
1 /*
2  * QOA decoder
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "avcodec.h"
22 #include "codec_internal.h"
23 #include "decode.h"
24 #include "get_bits.h"
25 #include "bytestream.h"
26 #include "mathops.h"
27 
28 #define QOA_SLICE_LEN 20
29 #define QOA_LMS_LEN 4
30 
31 typedef struct QOAChannel {
34 } QOAChannel;
35 
36 typedef struct QOAContext {
37  QOAChannel ch[256];
38 } QOAContext;
39 
40 static const int16_t qoa_dequant_tab[16][8] = {
41  { 1, -1, 3, -3, 5, -5, 7, -7},
42  { 5, -5, 18, -18, 32, -32, 49, -49},
43  { 16, -16, 53, -53, 95, -95, 147, -147},
44  { 34, -34, 113, -113, 203, -203, 315, -315},
45  { 63, -63, 210, -210, 378, -378, 588, -588},
46  { 104, -104, 345, -345, 621, -621, 966, -966},
47  { 158, -158, 528, -528, 950, -950, 1477, -1477},
48  { 228, -228, 760, -760, 1368, -1368, 2128, -2128},
49  { 316, -316, 1053, -1053, 1895, -1895, 2947, -2947},
50  { 422, -422, 1405, -1405, 2529, -2529, 3934, -3934},
51  { 548, -548, 1828, -1828, 3290, -3290, 5117, -5117},
52  { 696, -696, 2320, -2320, 4176, -4176, 6496, -6496},
53  { 868, -868, 2893, -2893, 5207, -5207, 8099, -8099},
54  {1064, -1064, 3548, -3548, 6386, -6386, 9933, -9933},
55  {1286, -1286, 4288, -4288, 7718, -7718, 12005, -12005},
56  {1536, -1536, 5120, -5120, 9216, -9216, 14336, -14336},
57 };
58 
60 {
62 
63  return 0;
64 }
65 
66 static int qoa_lms_predict(QOAChannel *lms)
67 {
68  int prediction = 0;
69  for (int i = 0; i < QOA_LMS_LEN; i++)
70  prediction += (unsigned)lms->weights[i] * lms->history[i];
71  return prediction >> 13;
72 }
73 
74 static void qoa_lms_update(QOAChannel *lms, int sample, int residual)
75 {
76  int delta = residual >> 4;
77  for (int i = 0; i < QOA_LMS_LEN; i++)
78  lms->weights[i] += lms->history[i] < 0 ? -delta : delta;
79  for (int i = 0; i < QOA_LMS_LEN-1; i++)
80  lms->history[i] = lms->history[i+1];
81  lms->history[QOA_LMS_LEN-1] = sample;
82 }
83 
85  int *got_frame_ptr, AVPacket *avpkt)
86 {
87  QOAContext *s = avctx->priv_data;
88  int ret, frame_size, nb_channels, sample_rate;
89  GetByteContext gb;
90  int16_t *samples;
91 
92  bytestream2_init(&gb, avpkt->data, avpkt->size);
93 
94  nb_channels = bytestream2_get_byte(&gb);
95  sample_rate = bytestream2_get_be24(&gb);
96  if (!sample_rate || !nb_channels)
97  return AVERROR_INVALIDDATA;
98 
99  if (nb_channels != avctx->ch_layout.nb_channels) {
101  av_channel_layout_default(&avctx->ch_layout, nb_channels);
102  if ((ret = av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout)) < 0)
103  return ret;
104  }
105 
106  frame->sample_rate = avctx->sample_rate = sample_rate;
107 
108  frame->nb_samples = bytestream2_get_be16(&gb);
109  frame_size = bytestream2_get_be16(&gb);
110  if (frame_size > avpkt->size)
111  return AVERROR_INVALIDDATA;
112 
113  if (avpkt->size < 8 + QOA_LMS_LEN * 4 * nb_channels +
114  8LL * ((frame->nb_samples + QOA_SLICE_LEN - 1) / QOA_SLICE_LEN) * nb_channels)
115  return AVERROR_INVALIDDATA;
116 
117  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
118  return ret;
119  samples = (int16_t *)frame->data[0];
120 
121  for (int ch = 0; ch < nb_channels; ch++) {
122  QOAChannel *qch = &s->ch[ch];
123 
124  for (int n = 0; n < QOA_LMS_LEN; n++)
125  qch->history[n] = sign_extend(bytestream2_get_be16u(&gb), 16);
126  for (int n = 0; n < QOA_LMS_LEN; n++)
127  qch->weights[n] = sign_extend(bytestream2_get_be16u(&gb), 16);
128  }
129 
130  for (int sample_index = 0; sample_index < frame->nb_samples;
131  sample_index += QOA_SLICE_LEN) {
132  for (int ch = 0; ch < nb_channels; ch++) {
133  QOAChannel *lms = &s->ch[ch];
134  uint64_t slice = bytestream2_get_be64u(&gb);
135  int scalefactor = (slice >> 60) & 0xf;
136  int slice_start = sample_index * nb_channels + ch;
137  int slice_end = av_clip(sample_index + QOA_SLICE_LEN, 0, frame->nb_samples) * nb_channels + ch;
138 
139  for (int si = slice_start; si < slice_end; si += nb_channels) {
140  int predicted = qoa_lms_predict(lms);
141  int quantized = (slice >> 57) & 0x7;
142  int dequantized = qoa_dequant_tab[scalefactor][quantized];
143  int reconstructed = av_clip_int16(predicted + dequantized);
144 
145  samples[si] = reconstructed;
146  slice <<= 3;
147 
148  qoa_lms_update(lms, reconstructed, dequantized);
149  }
150  }
151  }
152 
153  *got_frame_ptr = 1;
154 
155  return avpkt->size;
156 }
157 
159  .p.name = "qoa",
160  CODEC_LONG_NAME("QOA (Quite OK Audio)"),
161  .p.type = AVMEDIA_TYPE_AUDIO,
162  .p.id = AV_CODEC_ID_QOA,
163  .priv_data_size = sizeof(QOAContext),
166  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
168  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
170 };
av_clip
#define av_clip
Definition: common.h:100
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
GetByteContext
Definition: bytestream.h:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVPacket::data
uint8_t * data
Definition: packet.h:533
FFCodec
Definition: codec_internal.h:126
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
sample_rate
sample_rate
Definition: ffmpeg_filter.c:424
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition: mpeg12dec.c:1719
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
av_cold
#define av_cold
Definition: attributes.h:90
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:286
s
#define s(width, name)
Definition: cbs_vp9.c:198
frame_size
int frame_size
Definition: mxfenc.c:2424
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
qoa_lms_predict
static int qoa_lms_predict(QOAChannel *lms)
Definition: qoadec.c:66
decode.h
get_bits.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:271
av_clip_int16
#define av_clip_int16
Definition: common.h:115
qoa_lms_update
static void qoa_lms_update(QOAChannel *lms, int sample, int residual)
Definition: qoadec.c:74
QOAChannel
Definition: qoadec.c:31
mathops.h
AV_CODEC_ID_QOA
@ AV_CODEC_ID_QOA
Definition: codec_id.h:545
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
QOAContext::ch
QOAChannel ch[256]
Definition: qoadec.c:37
qoa_decode_frame
static int qoa_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: qoadec.c:84
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1575
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
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:534
codec_internal.h
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1057
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
sample
#define sample
Definition: flacdsp_template.c:44
QOAChannel::history
int history[QOA_LMS_LEN]
Definition: qoadec.c:32
QOAChannel::weights
int weights[QOA_LMS_LEN]
Definition: qoadec.c:33
prediction
static int64_t prediction(int delta, ChannelContext *c)
Definition: misc4.c:78
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:834
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
delta
float delta
Definition: vorbis_enc_data.h:430
xf
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:590
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
qoa_dequant_tab
static const int16_t qoa_dequant_tab[16][8]
Definition: qoadec.c:40
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
AVCodecContext
main external API structure.
Definition: avcodec.h:445
QOAContext
Definition: qoadec.c:36
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:437
ff_qoa_decoder
const FFCodec ff_qoa_decoder
Definition: qoadec.c:158
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:131
slice_start
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition: dec.c:704
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:444
QOA_SLICE_LEN
#define QOA_SLICE_LEN
Definition: qoadec.c:28
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVPacket
This structure stores compressed data.
Definition: packet.h:510
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
qoa_decode_init
static av_cold int qoa_decode_init(AVCodecContext *avctx)
Definition: qoadec.c:59
QOA_LMS_LEN
#define QOA_LMS_LEN
Definition: qoadec.c:29