GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/ralf.c Lines: 240 294 81.6 %
Date: 2020-10-23 17:01:47 Branches: 124 170 72.9 %

Line Branch Exec Source
1
/*
2
 * RealAudio Lossless decoder
3
 *
4
 * Copyright (c) 2012 Konstantin Shishkov
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24
 * @file
25
 * This is a decoder for Real Audio Lossless format.
26
 * Dedicated to the mastermind behind it, Ralph Wiggum.
27
 */
28
29
#include "libavutil/attributes.h"
30
#include "libavutil/channel_layout.h"
31
#include "avcodec.h"
32
#include "get_bits.h"
33
#include "golomb.h"
34
#include "internal.h"
35
#include "unary.h"
36
#include "ralfdata.h"
37
38
#define FILTER_NONE 0
39
#define FILTER_RAW  642
40
41
typedef struct VLCSet {
42
    VLC filter_params;
43
    VLC bias;
44
    VLC coding_mode;
45
    VLC filter_coeffs[10][11];
46
    VLC short_codes[15];
47
    VLC long_codes[125];
48
} VLCSet;
49
50
#define RALF_MAX_PKT_SIZE 8192
51
52
typedef struct RALFContext {
53
    int version;
54
    int max_frame_size;
55
    VLCSet sets[3];
56
    int32_t channel_data[2][4096];
57
58
    int     filter_params;   ///< combined filter parameters for the current channel data
59
    int     filter_length;   ///< length of the filter for the current channel data
60
    int     filter_bits;     ///< filter precision for the current channel data
61
    int32_t filter[64];
62
63
    unsigned bias[2];        ///< a constant value added to channel data after filtering
64
65
    int num_blocks;          ///< number of blocks inside the frame
66
    int sample_offset;
67
    int block_size[1 << 12]; ///< size of the blocks
68
    int block_pts[1 << 12];  ///< block start time (in milliseconds)
69
70
    uint8_t pkt[16384];
71
    int     has_pkt;
72
} RALFContext;
73
74
#define MAX_ELEMS 644 // no RALF table uses more than that
75
76
1518
static av_cold int init_ralf_vlc(VLC *vlc, const uint8_t *data, int elems)
77
{
78
    uint8_t  lens[MAX_ELEMS];
79
    uint16_t codes[MAX_ELEMS];
80
    int counts[17], prefixes[18];
81
    int i, cur_len;
82
1518
    int max_bits = 0;
83
1518
    int nb = 0;
84
85
27324
    for (i = 0; i <= 16; i++)
86
25806
        counts[i] = 0;
87
382086
    for (i = 0; i < elems; i++) {
88
380568
        cur_len  = (nb ? *data & 0xF : *data >> 4) + 1;
89
380568
        counts[cur_len]++;
90
380568
        max_bits = FFMAX(max_bits, cur_len);
91
380568
        lens[i]  = cur_len;
92
380568
        data    += nb;
93
380568
        nb      ^= 1;
94
    }
95
1518
    prefixes[1] = 0;
96
25806
    for (i = 1; i <= 16; i++)
97
24288
        prefixes[i + 1] = (prefixes[i] + counts[i]) << 1;
98
99
382086
    for (i = 0; i < elems; i++)
100
380568
        codes[i] = prefixes[lens[i]]++;
101
102
1518
    return ff_init_vlc_sparse(vlc, FFMIN(max_bits, 9), elems,
103
                              lens, 1, 1, codes, 2, 2, NULL, 0, 0, 0);
104
}
105
106
2
static av_cold int decode_close(AVCodecContext *avctx)
107
{
108
2
    RALFContext *ctx = avctx->priv_data;
109
    int i, j, k;
110
111
8
    for (i = 0; i < 3; i++) {
112
6
        ff_free_vlc(&ctx->sets[i].filter_params);
113
6
        ff_free_vlc(&ctx->sets[i].bias);
114
6
        ff_free_vlc(&ctx->sets[i].coding_mode);
115
66
        for (j = 0; j < 10; j++)
116
720
            for (k = 0; k < 11; k++)
117
660
                ff_free_vlc(&ctx->sets[i].filter_coeffs[j][k]);
118
96
        for (j = 0; j < 15; j++)
119
90
            ff_free_vlc(&ctx->sets[i].short_codes[j]);
120
756
        for (j = 0; j < 125; j++)
121
750
            ff_free_vlc(&ctx->sets[i].long_codes[j]);
122
    }
123
124
2
    return 0;
125
}
126
127
2
static av_cold int decode_init(AVCodecContext *avctx)
128
{
129
2
    RALFContext *ctx = avctx->priv_data;
130
    int i, j, k;
131
    int ret;
132
133

2
    if (avctx->extradata_size < 24 || memcmp(avctx->extradata, "LSD:", 4)) {
134
        av_log(avctx, AV_LOG_ERROR, "Extradata is not groovy, dude\n");
135
        return AVERROR_INVALIDDATA;
136
    }
137
138
2
    ctx->version = AV_RB16(avctx->extradata + 4);
139
2
    if (ctx->version != 0x103) {
140
        avpriv_request_sample(avctx, "Unknown version %X", ctx->version);
141
        return AVERROR_PATCHWELCOME;
142
    }
143
144
2
    avctx->channels    = AV_RB16(avctx->extradata + 8);
145
2
    avctx->sample_rate = AV_RB32(avctx->extradata + 12);
146

2
    if (avctx->channels < 1 || avctx->channels > 2
147

2
        || avctx->sample_rate < 8000 || avctx->sample_rate > 96000) {
148
        av_log(avctx, AV_LOG_ERROR, "Invalid coding parameters %d Hz %d ch\n",
149
               avctx->sample_rate, avctx->channels);
150
        return AVERROR_INVALIDDATA;
151
    }
152
2
    avctx->sample_fmt     = AV_SAMPLE_FMT_S16P;
153
4
    avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO
154
2
                                                   : AV_CH_LAYOUT_MONO;
155
156
2
    ctx->max_frame_size = AV_RB32(avctx->extradata + 16);
157

2
    if (ctx->max_frame_size > (1 << 20) || !ctx->max_frame_size) {
158
        av_log(avctx, AV_LOG_ERROR, "invalid frame size %d\n",
159
               ctx->max_frame_size);
160
    }
161
2
    ctx->max_frame_size = FFMAX(ctx->max_frame_size, avctx->sample_rate);
162
163
8
    for (i = 0; i < 3; i++) {
164
6
        ret = init_ralf_vlc(&ctx->sets[i].filter_params, filter_param_def[i],
165
                            FILTERPARAM_ELEMENTS);
166
6
        if (ret < 0) {
167
            decode_close(avctx);
168
            return ret;
169
        }
170
6
        ret = init_ralf_vlc(&ctx->sets[i].bias, bias_def[i], BIAS_ELEMENTS);
171
6
        if (ret < 0) {
172
            decode_close(avctx);
173
            return ret;
174
        }
175
6
        ret = init_ralf_vlc(&ctx->sets[i].coding_mode, coding_mode_def[i],
176
                            CODING_MODE_ELEMENTS);
177
6
        if (ret < 0) {
178
            decode_close(avctx);
179
            return ret;
180
        }
181
66
        for (j = 0; j < 10; j++) {
182
720
            for (k = 0; k < 11; k++) {
183
660
                ret = init_ralf_vlc(&ctx->sets[i].filter_coeffs[j][k],
184
660
                                    filter_coeffs_def[i][j][k],
185
                                    FILTER_COEFFS_ELEMENTS);
186
660
                if (ret < 0) {
187
                    decode_close(avctx);
188
                    return ret;
189
                }
190
            }
191
        }
192
96
        for (j = 0; j < 15; j++) {
193
90
            ret = init_ralf_vlc(&ctx->sets[i].short_codes[j],
194
90
                                short_codes_def[i][j], SHORT_CODES_ELEMENTS);
195
90
            if (ret < 0) {
196
                decode_close(avctx);
197
                return ret;
198
            }
199
        }
200
756
        for (j = 0; j < 125; j++) {
201
750
            ret = init_ralf_vlc(&ctx->sets[i].long_codes[j],
202
750
                                long_codes_def[i][j], LONG_CODES_ELEMENTS);
203
750
            if (ret < 0) {
204
                decode_close(avctx);
205
                return ret;
206
            }
207
        }
208
    }
209
210
2
    return 0;
211
}
212
213
860917
static inline int extend_code(GetBitContext *gb, int val, int range, int bits)
214
{
215
860917
    if (val == 0) {
216
7314
        val = -range - get_ue_golomb(gb);
217
853603
    } else if (val == range * 2) {
218
4290
        val =  range + get_ue_golomb(gb);
219
    } else {
220
849313
        val -= range;
221
    }
222
860917
    if (bits)
223
3829
        val = ((unsigned)val << bits) | get_bits(gb, bits);
224
860917
    return val;
225
}
226
227
428
static int decode_channel(RALFContext *ctx, GetBitContext *gb, int ch,
228
                          int length, int mode, int bits)
229
{
230
    int i, t;
231
    int code_params;
232
428
    VLCSet *set = ctx->sets + mode;
233
    VLC *code_vlc; int range, range2, add_bits;
234
428
    int *dst = ctx->channel_data[ch];
235
236
428
    ctx->filter_params = get_vlc2(gb, set->filter_params.table, 9, 2);
237
428
    if (ctx->filter_params > 1) {
238
428
        ctx->filter_bits   = (ctx->filter_params - 2) >> 6;
239
428
        ctx->filter_length = ctx->filter_params - (ctx->filter_bits << 6) - 1;
240
    }
241
242
428
    if (ctx->filter_params == FILTER_RAW) {
243
        for (i = 0; i < length; i++)
244
            dst[i] = get_bits(gb, bits);
245
        ctx->bias[ch] = 0;
246
        return 0;
247
    }
248
249
428
    ctx->bias[ch] = get_vlc2(gb, set->bias.table, 9, 2);
250
428
    ctx->bias[ch] = extend_code(gb, ctx->bias[ch], 127, 4);
251
252
428
    if (ctx->filter_params == FILTER_NONE) {
253
        memset(dst, 0, sizeof(*dst) * length);
254
        return 0;
255
    }
256
257
428
    if (ctx->filter_params > 1) {
258
428
        int cmode = 0, coeff = 0;
259
428
        VLC *vlc = set->filter_coeffs[ctx->filter_bits] + 5;
260
261
428
        add_bits = ctx->filter_bits;
262
263
3829
        for (i = 0; i < ctx->filter_length; i++) {
264
3401
            t = get_vlc2(gb, vlc[cmode].table, vlc[cmode].bits, 2);
265
3401
            t = extend_code(gb, t, 21, add_bits);
266
3401
            if (!cmode)
267
758
                coeff -= 12U << add_bits;
268
3401
            coeff = (unsigned)t - coeff;
269
3401
            ctx->filter[i] = coeff;
270
271
3401
            cmode = coeff >> add_bits;
272
3401
            if (cmode < 0) {
273
1483
                cmode = -1 - av_log2(-cmode);
274
1483
                if (cmode < -5)
275
4
                    cmode = -5;
276
1918
            } else if (cmode > 0) {
277
1506
                cmode =  1 + av_log2(cmode);
278
1506
                if (cmode > 5)
279
2
                    cmode = 5;
280
            }
281
        }
282
    }
283
284
428
    code_params = get_vlc2(gb, set->coding_mode.table, set->coding_mode.bits, 2);
285
428
    if (code_params >= 15) {
286
428
        add_bits = av_clip((code_params / 5 - 3) / 2, 0, 10);
287

428
        if (add_bits > 9 && (code_params % 5) != 2)
288
            add_bits--;
289
428
        range    = 10;
290
428
        range2   = 21;
291
428
        code_vlc = set->long_codes + (code_params - 15);
292
    } else {
293
        add_bits = 0;
294
        range    = 6;
295
        range2   = 13;
296
        code_vlc = set->short_codes + code_params;
297
    }
298
299
428972
    for (i = 0; i < length; i += 2) {
300
        int code1, code2;
301
302
428544
        t = get_vlc2(gb, code_vlc->table, code_vlc->bits, 2);
303
428544
        code1 = t / range2;
304
428544
        code2 = t % range2;
305
428544
        dst[i]     = extend_code(gb, code1, range, 0) * (1U << add_bits);
306
428544
        dst[i + 1] = extend_code(gb, code2, range, 0) * (1U << add_bits);
307
428544
        if (add_bits) {
308
428544
            dst[i]     |= get_bits(gb, add_bits);
309
428544
            dst[i + 1] |= get_bits(gb, add_bits);
310
        }
311
    }
312
313
428
    return 0;
314
}
315
316
428
static void apply_lpc(RALFContext *ctx, int ch, int length, int bits)
317
{
318
    int i, j, acc;
319
428
    int *audio = ctx->channel_data[ch];
320
428
    int bias = 1 << (ctx->filter_bits - 1);
321
428
    int max_clip = (1 << bits) - 1, min_clip = -max_clip - 1;
322
323
857088
    for (i = 1; i < length; i++) {
324
856660
        int flen = FFMIN(ctx->filter_length, i);
325
326
856660
        acc = 0;
327
8543700
        for (j = 0; j < flen; j++)
328
7687040
            acc += (unsigned)ctx->filter[j] * audio[i - j - 1];
329
856660
        if (acc < 0) {
330
426048
            acc = (acc + bias - 1) >> ctx->filter_bits;
331
426048
            acc = FFMAX(acc, min_clip);
332
        } else {
333
430612
            acc = ((unsigned)acc + bias) >> ctx->filter_bits;
334
430612
            acc = FFMIN(acc, max_clip);
335
        }
336
856660
        audio[i] += acc;
337
    }
338
428
}
339
340
214
static int decode_block(AVCodecContext *avctx, GetBitContext *gb,
341
                        int16_t *dst0, int16_t *dst1)
342
{
343
214
    RALFContext *ctx = avctx->priv_data;
344
    int len, ch, ret;
345
    int dmode, mode[2], bits[2];
346
    int *ch0, *ch1;
347
    int i;
348
    unsigned int t, t2;
349
350
214
    len = 12 - get_unary(gb, 0, 6);
351
352
214
    if (len <= 7) len ^= 1; // codes for length = 6 and 7 are swapped
353
214
    len = 1 << len;
354
355
214
    if (ctx->sample_offset + len > ctx->max_frame_size) {
356
        av_log(avctx, AV_LOG_ERROR,
357
               "Decoder's stomach is crying, it ate too many samples\n");
358
        return AVERROR_INVALIDDATA;
359
    }
360
361
214
    if (avctx->channels > 1)
362
214
        dmode = get_bits(gb, 2) + 1;
363
    else
364
        dmode = 0;
365
366
214
    mode[0] = (dmode == 4) ? 1 : 0;
367
214
    mode[1] = (dmode >= 2) ? 2 : 0;
368
214
    bits[0] = 16;
369
214
    bits[1] = (mode[1] == 2) ? 17 : 16;
370
371
642
    for (ch = 0; ch < avctx->channels; ch++) {
372
428
        if ((ret = decode_channel(ctx, gb, ch, len, mode[ch], bits[ch])) < 0)
373
            return ret;
374

428
        if (ctx->filter_params > 1 && ctx->filter_params != FILTER_RAW) {
375
428
            ctx->filter_bits += 3;
376
428
            apply_lpc(ctx, ch, len, bits[ch]);
377
        }
378
428
        if (get_bits_left(gb) < 0)
379
            return AVERROR_INVALIDDATA;
380
    }
381
214
    ch0 = ctx->channel_data[0];
382
214
    ch1 = ctx->channel_data[1];
383

214
    switch (dmode) {
384
    case 0:
385
        for (i = 0; i < len; i++)
386
            dst0[i] = ch0[i] + ctx->bias[0];
387
        break;
388
1
    case 1:
389
4097
        for (i = 0; i < len; i++) {
390
4096
            dst0[i] = ch0[i] + ctx->bias[0];
391
4096
            dst1[i] = ch1[i] + ctx->bias[1];
392
        }
393
1
        break;
394
79
    case 2:
395
179535
        for (i = 0; i < len; i++) {
396
179456
            ch0[i] += ctx->bias[0];
397
179456
            dst0[i] = ch0[i];
398
179456
            dst1[i] = ch0[i] - (ch1[i] + ctx->bias[1]);
399
        }
400
79
        break;
401
43
    case 3:
402
60715
        for (i = 0; i < len; i++) {
403
60672
            t  = ch0[i] + ctx->bias[0];
404
60672
            t2 = ch1[i] + ctx->bias[1];
405
60672
            dst0[i] = t + t2;
406
60672
            dst1[i] = t;
407
        }
408
43
        break;
409
91
    case 4:
410
184411
        for (i = 0; i < len; i++) {
411
184320
            t  =   ch1[i] + ctx->bias[1];
412
184320
            t2 = ((ch0[i] + ctx->bias[0]) * 2) | (t & 1);
413
184320
            dst0[i] = (int)(t2 + t) / 2;
414
184320
            dst1[i] = (int)(t2 - t) / 2;
415
        }
416
91
        break;
417
    }
418
419
214
    ctx->sample_offset += len;
420
421
214
    return 0;
422
}
423
424
190
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
425
                        AVPacket *avpkt)
426
{
427
190
    RALFContext *ctx = avctx->priv_data;
428
190
    AVFrame *frame   = data;
429
    int16_t *samples0;
430
    int16_t *samples1;
431
    int ret;
432
    GetBitContext gb;
433
    int table_size, table_bytes, i;
434
    const uint8_t *src, *block_pointer;
435
    int src_size;
436
    int bytes_left;
437
438
190
    if (ctx->has_pkt) {
439
39
        ctx->has_pkt = 0;
440
39
        table_bytes = (AV_RB16(avpkt->data) + 7) >> 3;
441

39
        if (table_bytes + 3 > avpkt->size || avpkt->size > RALF_MAX_PKT_SIZE) {
442
            av_log(avctx, AV_LOG_ERROR, "Wrong packet's breath smells of wrong data!\n");
443
            return AVERROR_INVALIDDATA;
444
        }
445
39
        if (memcmp(ctx->pkt, avpkt->data, 2 + table_bytes)) {
446
            av_log(avctx, AV_LOG_ERROR, "Wrong packet tails are wrong!\n");
447
            return AVERROR_INVALIDDATA;
448
        }
449
450
39
        src      = ctx->pkt;
451
39
        src_size = RALF_MAX_PKT_SIZE + avpkt->size;
452
39
        memcpy(ctx->pkt + RALF_MAX_PKT_SIZE, avpkt->data + 2 + table_bytes,
453
39
               avpkt->size - 2 - table_bytes);
454
    } else {
455
151
        if (avpkt->size == RALF_MAX_PKT_SIZE) {
456
39
            memcpy(ctx->pkt, avpkt->data, avpkt->size);
457
39
            ctx->has_pkt   = 1;
458
39
            *got_frame_ptr = 0;
459
460
39
            return avpkt->size;
461
        }
462
112
        src      = avpkt->data;
463
112
        src_size = avpkt->size;
464
    }
465
466
151
    frame->nb_samples = ctx->max_frame_size;
467
151
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
468
        return ret;
469
151
    samples0 = (int16_t *)frame->data[0];
470
151
    samples1 = (int16_t *)frame->data[1];
471
472
151
    if (src_size < 5) {
473
        av_log(avctx, AV_LOG_ERROR, "too short packets are too short!\n");
474
        return AVERROR_INVALIDDATA;
475
    }
476
151
    table_size  = AV_RB16(src);
477
151
    table_bytes = (table_size + 7) >> 3;
478
151
    if (src_size < table_bytes + 3) {
479
        av_log(avctx, AV_LOG_ERROR, "short packets are short!\n");
480
        return AVERROR_INVALIDDATA;
481
    }
482
151
    init_get_bits(&gb, src + 2, table_size);
483
151
    ctx->num_blocks = 0;
484
366
    while (get_bits_left(&gb) > 0) {
485
215
        if (ctx->num_blocks >= FF_ARRAY_ELEMS(ctx->block_size))
486
            return AVERROR_INVALIDDATA;
487
215
        ctx->block_size[ctx->num_blocks] = get_bits(&gb, 13 + avctx->channels);
488
215
        if (get_bits1(&gb)) {
489
64
            ctx->block_pts[ctx->num_blocks] = get_bits(&gb, 9);
490
        } else {
491
151
            ctx->block_pts[ctx->num_blocks] = 0;
492
        }
493
215
        ctx->num_blocks++;
494
    }
495
496
151
    block_pointer = src      + table_bytes + 2;
497
151
    bytes_left    = src_size - table_bytes - 2;
498
151
    ctx->sample_offset = 0;
499
365
    for (i = 0; i < ctx->num_blocks; i++) {
500
215
        if (bytes_left < ctx->block_size[i]) {
501
1
            av_log(avctx, AV_LOG_ERROR, "I'm pedaling backwards\n");
502
1
            break;
503
        }
504
214
        init_get_bits(&gb, block_pointer, ctx->block_size[i] * 8);
505
214
        if (decode_block(avctx, &gb, samples0 + ctx->sample_offset,
506
214
                                     samples1 + ctx->sample_offset) < 0) {
507
            av_log(avctx, AV_LOG_ERROR, "Sir, I got carsick in your office. Not decoding the rest of packet.\n");
508
            break;
509
        }
510
214
        block_pointer += ctx->block_size[i];
511
214
        bytes_left    -= ctx->block_size[i];
512
    }
513
514
151
    frame->nb_samples = ctx->sample_offset;
515
151
    *got_frame_ptr    = ctx->sample_offset > 0;
516
517
151
    return avpkt->size;
518
}
519
520
static void decode_flush(AVCodecContext *avctx)
521
{
522
    RALFContext *ctx = avctx->priv_data;
523
524
    ctx->has_pkt = 0;
525
}
526
527
528
AVCodec ff_ralf_decoder = {
529
    .name           = "ralf",
530
    .long_name      = NULL_IF_CONFIG_SMALL("RealAudio Lossless"),
531
    .type           = AVMEDIA_TYPE_AUDIO,
532
    .id             = AV_CODEC_ID_RALF,
533
    .priv_data_size = sizeof(RALFContext),
534
    .init           = decode_init,
535
    .close          = decode_close,
536
    .decode         = decode_frame,
537
    .flush          = decode_flush,
538
    .capabilities   = AV_CODEC_CAP_DR1,
539
    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
540
                                                      AV_SAMPLE_FMT_NONE },
541
};