FFmpeg
Loading...
Searching...
No Matches
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
35
36typedef struct QOAContext {
39
40static 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
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
74static 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;
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)
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};
const FFCodec ff_qoa_decoder
Definition qoadec.c:158
Libavcodec external API header.
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define av_clip
Definition common.h:100
#define av_clip_int16
Definition common.h:115
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define sample
static const uint8_t frame_size[4]
Definition g723_1.h:222
bitstream reader API header.
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_QOA
Definition codec_id.h:558
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
static int qoa_lms_predict(QOAChannel *lms)
Definition qoadec.c:66
static int qoa_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition qoadec.c:84
#define QOA_LMS_LEN
Definition qoadec.c:29
#define QOA_SLICE_LEN
Definition qoadec.c:28
static av_cold int qoa_decode_init(AVCodecContext *avctx)
Definition qoadec.c:59
static const int16_t qoa_dequant_tab[16][8]
Definition qoadec.c:40
static void qoa_lms_update(QOAChannel *lms, int sample, int residual)
Definition qoadec.c:74
#define av_cold
Definition attributes.h:117
static av_const int sign_extend(int val, unsigned bits)
Definition mathops.h:135
static int64_t prediction(int delta, ChannelContext *c)
Definition misc4.c:84
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1697
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int sample_rate
samples per second
Definition avcodec.h:1040
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
int weights[QOA_LMS_LEN]
Definition qoadec.c:33
int history[QOA_LMS_LEN]
Definition qoadec.c:32
QOAChannel ch[256]
Definition qoadec.c:37
float delta
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844