GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mlpenc.c Lines: 0 1153 0.0 %
Date: 2020-07-11 02:49:52 Branches: 0 502 0.0 %

Line Branch Exec Source
1
/**
2
 * MLP encoder
3
 * Copyright (c) 2008 Ramiro Polla
4
 * Copyright (c) 2016-2019 Jai Luthra
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
#include "avcodec.h"
24
#include "internal.h"
25
#include "put_bits.h"
26
#include "audio_frame_queue.h"
27
#include "libavutil/crc.h"
28
#include "libavutil/avstring.h"
29
#include "libavutil/samplefmt.h"
30
#include "mlp.h"
31
#include "lpc.h"
32
33
#define MAJOR_HEADER_INTERVAL 16
34
35
#define MLP_MIN_LPC_ORDER      1
36
#define MLP_MAX_LPC_ORDER      8
37
#define MLP_MIN_LPC_SHIFT      8
38
#define MLP_MAX_LPC_SHIFT     15
39
40
typedef struct {
41
    uint8_t         min_channel;         ///< The index of the first channel coded in this substream.
42
    uint8_t         max_channel;         ///< The index of the last channel coded in this substream.
43
    uint8_t         max_matrix_channel;  ///< The number of channels input into the rematrix stage.
44
45
    uint8_t         noise_shift;         ///< The left shift applied to random noise in 0x31ea substreams.
46
    uint32_t        noisegen_seed;       ///< The current seed value for the pseudorandom noise generator(s).
47
48
    int             data_check_present;  ///< Set if the substream contains extra info to check the size of VLC blocks.
49
50
    int32_t         lossless_check_data; ///< XOR of all output samples
51
52
    uint8_t         max_huff_lsbs;       ///< largest huff_lsbs
53
    uint8_t         max_output_bits;     ///< largest output bit-depth
54
} RestartHeader;
55
56
typedef struct {
57
    uint8_t         count;                  ///< number of matrices to apply
58
59
    uint8_t         outch[MAX_MATRICES];    ///< output channel for each matrix
60
    int32_t         forco[MAX_MATRICES][MAX_CHANNELS+2];    ///< forward coefficients
61
    int32_t         coeff[MAX_MATRICES][MAX_CHANNELS+2];    ///< decoding coefficients
62
    uint8_t         fbits[MAX_CHANNELS];    ///< fraction bits
63
64
    int8_t          shift[MAX_CHANNELS];    ///< Left shift to apply to decoded PCM values to get final 24-bit output.
65
} MatrixParams;
66
67
enum ParamFlags {
68
    PARAMS_DEFAULT       = 0xff,
69
    PARAM_PRESENCE_FLAGS = 1 << 8,
70
    PARAM_BLOCKSIZE      = 1 << 7,
71
    PARAM_MATRIX         = 1 << 6,
72
    PARAM_OUTSHIFT       = 1 << 5,
73
    PARAM_QUANTSTEP      = 1 << 4,
74
    PARAM_FIR            = 1 << 3,
75
    PARAM_IIR            = 1 << 2,
76
    PARAM_HUFFOFFSET     = 1 << 1,
77
    PARAM_PRESENT        = 1 << 0,
78
};
79
80
typedef struct {
81
    uint16_t        blocksize;                  ///< number of PCM samples in current audio block
82
    uint8_t         quant_step_size[MAX_CHANNELS];  ///< left shift to apply to Huffman-decoded residuals
83
84
    MatrixParams    matrix_params;
85
86
    uint8_t         param_presence_flags;       ///< Bitmask of which parameter sets are conveyed in a decoding parameter block.
87
} DecodingParams;
88
89
typedef struct BestOffset {
90
    int32_t offset;
91
    int bitcount;
92
    int lsb_bits;
93
    int32_t min;
94
    int32_t max;
95
} BestOffset;
96
97
#define HUFF_OFFSET_MIN    (-16384)
98
#define HUFF_OFFSET_MAX    ( 16383)
99
100
/** Number of possible codebooks (counting "no codebooks") */
101
#define NUM_CODEBOOKS       4
102
103
typedef struct {
104
    AVCodecContext *avctx;
105
106
    int             num_substreams;         ///< Number of substreams contained within this stream.
107
108
    int             num_channels;   /**< Number of channels in major_scratch_buffer.
109
                                     *   Normal channels + noise channels. */
110
111
    int             coded_sample_fmt [2];   ///< sample format encoded for MLP
112
    int             coded_sample_rate[2];   ///< sample rate encoded for MLP
113
    int             coded_peak_bitrate;     ///< peak bitrate for this major sync header
114
115
    int             flags;                  ///< major sync info flags
116
117
    /* channel_meaning */
118
    int             substream_info;
119
    int             fs;
120
    int             wordlength;
121
    int             channel_occupancy;
122
    int             summary_info;
123
124
    int32_t        *inout_buffer;           ///< Pointer to data currently being read from lavc or written to bitstream.
125
    int32_t        *major_inout_buffer;     ///< Buffer with all in/out data for one entire major frame interval.
126
    int32_t        *write_buffer;           ///< Pointer to data currently being written to bitstream.
127
    int32_t        *sample_buffer;          ///< Pointer to current access unit samples.
128
    int32_t        *major_scratch_buffer;   ///< Scratch buffer big enough to fit all data for one entire major frame interval.
129
    int32_t        *last_frame;             ///< Pointer to last frame with data to encode.
130
131
    int32_t        *lpc_sample_buffer;
132
133
    unsigned int    major_number_of_frames;
134
    unsigned int    next_major_number_of_frames;
135
136
    unsigned int    major_frame_size;       ///< Number of samples in current major frame being encoded.
137
    unsigned int    next_major_frame_size;  ///< Counter of number of samples for next major frame.
138
139
    int32_t        *lossless_check_data;    ///< Array with lossless_check_data for each access unit.
140
141
    unsigned int   *max_output_bits;        ///< largest output bit-depth
142
    unsigned int   *frame_size;             ///< Array with number of samples/channel in each access unit.
143
    unsigned int    frame_index;            ///< Index of current frame being encoded.
144
145
    unsigned int    one_sample_buffer_size; ///< Number of samples*channel for one access unit.
146
147
    unsigned int    max_restart_interval;   ///< Max interval of access units in between two major frames.
148
    unsigned int    min_restart_interval;   ///< Min interval of access units in between two major frames.
149
    unsigned int    restart_intervals;      ///< Number of possible major frame sizes.
150
151
    uint16_t        timestamp;              ///< Timestamp of current access unit.
152
    uint16_t        dts;                    ///< Decoding timestamp of current access unit.
153
154
    uint8_t         channel_arrangement;    ///< channel arrangement for MLP streams
155
156
    uint8_t         ch_modifier_thd0;       ///< channel modifier for TrueHD stream 0
157
    uint8_t         ch_modifier_thd1;       ///< channel modifier for TrueHD stream 1
158
    uint8_t         ch_modifier_thd2;       ///< channel modifier for TrueHD stream 2
159
160
    unsigned int    seq_size  [MAJOR_HEADER_INTERVAL];
161
    unsigned int    seq_offset[MAJOR_HEADER_INTERVAL];
162
    unsigned int    sequence_size;
163
164
    ChannelParams  *channel_params;
165
166
    BestOffset      best_offset[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS][NUM_CODEBOOKS];
167
168
    DecodingParams *decoding_params;
169
    RestartHeader   restart_header [MAX_SUBSTREAMS];
170
171
    ChannelParams   major_channel_params[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS];       ///< ChannelParams to be written to bitstream.
172
    DecodingParams  major_decoding_params[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS];    ///< DecodingParams to be written to bitstream.
173
    int             major_params_changed[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS];     ///< params_changed to be written to bitstream.
174
175
    unsigned int    major_cur_subblock_index;
176
    unsigned int    major_filter_state_subblock;
177
    unsigned int    major_number_of_subblocks;
178
179
    BestOffset    (*cur_best_offset)[NUM_CODEBOOKS];
180
    ChannelParams  *cur_channel_params;
181
    DecodingParams *cur_decoding_params;
182
    RestartHeader  *cur_restart_header;
183
184
    AudioFrameQueue afq;
185
186
    /* Analysis stage. */
187
    unsigned int    starting_frame_index;
188
    unsigned int    number_of_frames;
189
    unsigned int    number_of_samples;
190
    unsigned int    number_of_subblocks;
191
    unsigned int    seq_index;              ///< Sequence index for high compression levels.
192
193
    ChannelParams  *prev_channel_params;
194
    DecodingParams *prev_decoding_params;
195
196
    ChannelParams  *seq_channel_params;
197
    DecodingParams *seq_decoding_params;
198
199
    unsigned int    max_codebook_search;
200
201
    LPCContext      lpc_ctx;
202
} MLPEncodeContext;
203
204
static ChannelParams   restart_channel_params[MAX_CHANNELS];
205
static DecodingParams  restart_decoding_params[MAX_SUBSTREAMS];
206
static BestOffset      restart_best_offset[NUM_CODEBOOKS] = {{0}};
207
208
#define SYNC_MAJOR      0xf8726f
209
#define MAJOR_SYNC_INFO_SIGNATURE   0xB752
210
211
#define SYNC_MLP        0xbb
212
#define SYNC_TRUEHD     0xba
213
214
/* must be set for DVD-A */
215
#define FLAGS_DVDA      0x4000
216
/* FIFO delay must be constant */
217
#define FLAGS_CONST     0x8000
218
219
#define SUBSTREAM_INFO_MAX_2_CHAN   0x01
220
#define SUBSTREAM_INFO_HIGH_RATE    0x02
221
#define SUBSTREAM_INFO_ALWAYS_SET   0x04
222
#define SUBSTREAM_INFO_2_SUBSTREAMS 0x08
223
224
/****************************************************************************
225
 ************ Functions that copy, clear, or compare parameters *************
226
 ****************************************************************************/
227
228
/** Compares two FilterParams structures and returns 1 if anything has
229
 *  changed. Returns 0 if they are both equal.
230
 */
231
static int compare_filter_params(const ChannelParams *prev_cp, const ChannelParams *cp, int filter)
232
{
233
    const FilterParams *prev = &prev_cp->filter_params[filter];
234
    const FilterParams *fp = &cp->filter_params[filter];
235
    int i;
236
237
    if (prev->order != fp->order)
238
        return 1;
239
240
    if (!prev->order)
241
        return 0;
242
243
    if (prev->shift != fp->shift)
244
        return 1;
245
246
    for (i = 0; i < fp->order; i++)
247
        if (prev_cp->coeff[filter][i] != cp->coeff[filter][i])
248
            return 1;
249
250
    return 0;
251
}
252
253
/** Compare two primitive matrices and returns 1 if anything has changed.
254
 *  Returns 0 if they are both equal.
255
 */
256
static int compare_matrix_params(MLPEncodeContext *ctx, const MatrixParams *prev, const MatrixParams *mp)
257
{
258
    RestartHeader *rh = ctx->cur_restart_header;
259
    unsigned int channel, mat;
260
261
    if (prev->count != mp->count)
262
        return 1;
263
264
    if (!prev->count)
265
        return 0;
266
267
    for (channel = rh->min_channel; channel <= rh->max_channel; channel++)
268
        if (prev->fbits[channel] != mp->fbits[channel])
269
            return 1;
270
271
    for (mat = 0; mat < mp->count; mat++) {
272
        if (prev->outch[mat] != mp->outch[mat])
273
            return 1;
274
275
        for (channel = 0; channel < ctx->num_channels; channel++)
276
            if (prev->coeff[mat][channel] != mp->coeff[mat][channel])
277
                return 1;
278
    }
279
280
    return 0;
281
}
282
283
/** Compares two DecodingParams and ChannelParams structures to decide if a
284
 *  new decoding params header has to be written.
285
 */
286
static int compare_decoding_params(MLPEncodeContext *ctx)
287
{
288
    DecodingParams *prev = ctx->prev_decoding_params;
289
    DecodingParams *dp = ctx->cur_decoding_params;
290
    MatrixParams *prev_mp = &prev->matrix_params;
291
    MatrixParams *mp = &dp->matrix_params;
292
    RestartHeader  *rh = ctx->cur_restart_header;
293
    unsigned int ch;
294
    int retval = 0;
295
296
    if (prev->param_presence_flags != dp->param_presence_flags)
297
        retval |= PARAM_PRESENCE_FLAGS;
298
299
    if (prev->blocksize != dp->blocksize)
300
        retval |= PARAM_BLOCKSIZE;
301
302
    if (compare_matrix_params(ctx, prev_mp, mp))
303
        retval |= PARAM_MATRIX;
304
305
    for (ch = 0; ch <= rh->max_matrix_channel; ch++)
306
        if (prev_mp->shift[ch] != mp->shift[ch]) {
307
            retval |= PARAM_OUTSHIFT;
308
            break;
309
        }
310
311
    for (ch = 0; ch <= rh->max_channel; ch++)
312
        if (prev->quant_step_size[ch] != dp->quant_step_size[ch]) {
313
            retval |= PARAM_QUANTSTEP;
314
            break;
315
        }
316
317
    for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
318
        ChannelParams *prev_cp = &ctx->prev_channel_params[ch];
319
        ChannelParams *cp = &ctx->cur_channel_params[ch];
320
321
        if (!(retval & PARAM_FIR) &&
322
            compare_filter_params(prev_cp, cp, FIR))
323
            retval |= PARAM_FIR;
324
325
        if (!(retval & PARAM_IIR) &&
326
            compare_filter_params(prev_cp, cp, IIR))
327
            retval |= PARAM_IIR;
328
329
        if (prev_cp->huff_offset != cp->huff_offset)
330
            retval |= PARAM_HUFFOFFSET;
331
332
        if (prev_cp->codebook    != cp->codebook  ||
333
            prev_cp->huff_lsbs   != cp->huff_lsbs  )
334
            retval |= 0x1;
335
    }
336
337
    return retval;
338
}
339
340
static void copy_filter_params(ChannelParams *dst_cp, ChannelParams *src_cp, int filter)
341
{
342
    FilterParams *dst = &dst_cp->filter_params[filter];
343
    FilterParams *src = &src_cp->filter_params[filter];
344
    unsigned int order;
345
346
    dst->order = src->order;
347
348
    if (dst->order) {
349
        dst->shift = src->shift;
350
351
        dst->coeff_shift = src->coeff_shift;
352
        dst->coeff_bits = src->coeff_bits;
353
    }
354
355
    for (order = 0; order < dst->order; order++)
356
        dst_cp->coeff[filter][order] = src_cp->coeff[filter][order];
357
}
358
359
static void copy_matrix_params(MatrixParams *dst, MatrixParams *src)
360
{
361
    dst->count = src->count;
362
363
    if (dst->count) {
364
        unsigned int channel, count;
365
366
        for (channel = 0; channel < MAX_CHANNELS; channel++) {
367
368
            dst->fbits[channel] = src->fbits[channel];
369
            dst->shift[channel] = src->shift[channel];
370
371
            for (count = 0; count < MAX_MATRICES; count++)
372
                dst->coeff[count][channel] = src->coeff[count][channel];
373
        }
374
375
        for (count = 0; count < MAX_MATRICES; count++)
376
            dst->outch[count] = src->outch[count];
377
    }
378
}
379
380
static void copy_restart_frame_params(MLPEncodeContext *ctx,
381
                                      unsigned int substr)
382
{
383
    unsigned int index;
384
385
    for (index = 0; index < ctx->number_of_subblocks; index++) {
386
        DecodingParams *dp = ctx->seq_decoding_params + index*(ctx->num_substreams) + substr;
387
        unsigned int channel;
388
389
        copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);
390
391
        for (channel = 0; channel < ctx->avctx->channels; channel++) {
392
            ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
393
            unsigned int filter;
394
395
            dp->quant_step_size[channel] = ctx->cur_decoding_params->quant_step_size[channel];
396
            dp->matrix_params.shift[channel] = ctx->cur_decoding_params->matrix_params.shift[channel];
397
398
            if (index)
399
                for (filter = 0; filter < NUM_FILTERS; filter++)
400
                    copy_filter_params(cp, &ctx->cur_channel_params[channel], filter);
401
        }
402
    }
403
}
404
405
/** Clears a DecodingParams struct the way it should be after a restart header. */
406
static void clear_decoding_params(MLPEncodeContext *ctx, DecodingParams decoding_params[MAX_SUBSTREAMS])
407
{
408
    unsigned int substr;
409
410
    for (substr = 0; substr < ctx->num_substreams; substr++) {
411
        DecodingParams *dp = &decoding_params[substr];
412
413
        dp->param_presence_flags   = 0xff;
414
        dp->blocksize              = 8;
415
416
        memset(&dp->matrix_params , 0, sizeof(MatrixParams       ));
417
        memset(dp->quant_step_size, 0, sizeof(dp->quant_step_size));
418
    }
419
}
420
421
/** Clears a ChannelParams struct the way it should be after a restart header. */
422
static void clear_channel_params(MLPEncodeContext *ctx, ChannelParams channel_params[MAX_CHANNELS])
423
{
424
    unsigned int channel;
425
426
    for (channel = 0; channel < ctx->avctx->channels; channel++) {
427
        ChannelParams *cp = &channel_params[channel];
428
429
        memset(&cp->filter_params, 0, sizeof(cp->filter_params));
430
431
        /* Default audio coding is 24-bit raw PCM. */
432
        cp->huff_offset      =  0;
433
        cp->codebook         =  0;
434
        cp->huff_lsbs        = 24;
435
    }
436
}
437
438
/** Sets default vales in our encoder for a DecodingParams struct. */
439
static void default_decoding_params(MLPEncodeContext *ctx,
440
     DecodingParams decoding_params[MAX_SUBSTREAMS])
441
{
442
    unsigned int substr;
443
444
    clear_decoding_params(ctx, decoding_params);
445
446
    for (substr = 0; substr < ctx->num_substreams; substr++) {
447
        DecodingParams *dp = &decoding_params[substr];
448
        uint8_t param_presence_flags = 0;
449
450
        param_presence_flags |= PARAM_BLOCKSIZE;
451
        param_presence_flags |= PARAM_MATRIX;
452
        param_presence_flags |= PARAM_OUTSHIFT;
453
        param_presence_flags |= PARAM_QUANTSTEP;
454
        param_presence_flags |= PARAM_FIR;
455
/*      param_presence_flags |= PARAM_IIR; */
456
        param_presence_flags |= PARAM_HUFFOFFSET;
457
        param_presence_flags |= PARAM_PRESENT;
458
459
        dp->param_presence_flags = param_presence_flags;
460
    }
461
}
462
463
/****************************************************************************/
464
465
/** Calculates the smallest number of bits it takes to encode a given signed
466
 *  value in two's complement.
467
 */
468
static int inline number_sbits(int number)
469
{
470
    if (number < -1)
471
        number++;
472
473
    return av_log2(FFABS(number)) + 1 + !!number;
474
}
475
476
enum InputBitDepth {
477
    BITS_16,
478
    BITS_20,
479
    BITS_24,
480
};
481
482
static int mlp_peak_bitrate(int peak_bitrate, int sample_rate)
483
{
484
    return ((peak_bitrate << 4) - 8) / sample_rate;
485
}
486
487
static av_cold int mlp_encode_init(AVCodecContext *avctx)
488
{
489
    MLPEncodeContext *ctx = avctx->priv_data;
490
    unsigned int substr, index;
491
    unsigned int sum = 0;
492
    unsigned int size;
493
    int ret;
494
495
    ctx->avctx = avctx;
496
497
    switch (avctx->sample_rate) {
498
    case 44100 << 0:
499
        avctx->frame_size         = 40  << 0;
500
        ctx->coded_sample_rate[0] = 0x08 + 0;
501
        ctx->fs                   = 0x08 + 1;
502
        break;
503
    case 44100 << 1:
504
        avctx->frame_size         = 40  << 1;
505
        ctx->coded_sample_rate[0] = 0x08 + 1;
506
        ctx->fs                   = 0x0C + 1;
507
        break;
508
    case 44100 << 2:
509
        ctx->substream_info      |= SUBSTREAM_INFO_HIGH_RATE;
510
        avctx->frame_size         = 40  << 2;
511
        ctx->coded_sample_rate[0] = 0x08 + 2;
512
        ctx->fs                   = 0x10 + 1;
513
        break;
514
    case 48000 << 0:
515
        avctx->frame_size         = 40  << 0;
516
        ctx->coded_sample_rate[0] = 0x00 + 0;
517
        ctx->fs                   = 0x08 + 2;
518
        break;
519
    case 48000 << 1:
520
        avctx->frame_size         = 40  << 1;
521
        ctx->coded_sample_rate[0] = 0x00 + 1;
522
        ctx->fs                   = 0x0C + 2;
523
        break;
524
    case 48000 << 2:
525
        ctx->substream_info      |= SUBSTREAM_INFO_HIGH_RATE;
526
        avctx->frame_size         = 40  << 2;
527
        ctx->coded_sample_rate[0] = 0x00 + 2;
528
        ctx->fs                   = 0x10 + 2;
529
        break;
530
    default:
531
        av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d. Supported "
532
                            "sample rates are 44100, 88200, 176400, 48000, "
533
                            "96000, and 192000.\n", avctx->sample_rate);
534
        return AVERROR(EINVAL);
535
    }
536
    ctx->coded_sample_rate[1] = -1 & 0xf;
537
538
    /* TODO Keep count of bitrate and calculate real value. */
539
    ctx->coded_peak_bitrate = mlp_peak_bitrate(9600000, avctx->sample_rate);
540
541
    /* TODO support more channels. */
542
    if (avctx->channels > 2) {
543
        av_log(avctx, AV_LOG_WARNING,
544
               "Only mono and stereo are supported at the moment.\n");
545
    }
546
547
    ctx->substream_info |= SUBSTREAM_INFO_ALWAYS_SET;
548
    if (avctx->channels <= 2) {
549
        ctx->substream_info |= SUBSTREAM_INFO_MAX_2_CHAN;
550
    }
551
552
    switch (avctx->sample_fmt) {
553
    case AV_SAMPLE_FMT_S16:
554
        ctx->coded_sample_fmt[0] = BITS_16;
555
        ctx->wordlength = 16;
556
        avctx->bits_per_raw_sample = 16;
557
        break;
558
    /* TODO 20 bits: */
559
    case AV_SAMPLE_FMT_S32:
560
        ctx->coded_sample_fmt[0] = BITS_24;
561
        ctx->wordlength = 24;
562
        avctx->bits_per_raw_sample = 24;
563
        break;
564
    default:
565
        av_log(avctx, AV_LOG_ERROR, "Sample format not supported. "
566
               "Only 16- and 24-bit samples are supported.\n");
567
        return AVERROR(EINVAL);
568
    }
569
    ctx->coded_sample_fmt[1] = -1 & 0xf;
570
571
    ctx->dts = -avctx->frame_size;
572
573
    ctx->num_channels = avctx->channels + 2; /* +2 noise channels */
574
    ctx->one_sample_buffer_size = avctx->frame_size
575
                                * ctx->num_channels;
576
    /* TODO Let user pass major header interval as parameter. */
577
    ctx->max_restart_interval = MAJOR_HEADER_INTERVAL;
578
579
    ctx->max_codebook_search = 3;
580
    ctx->min_restart_interval = MAJOR_HEADER_INTERVAL;
581
    ctx->restart_intervals = ctx->max_restart_interval / ctx->min_restart_interval;
582
583
    /* TODO Let user pass parameters for LPC filter. */
584
585
    size = avctx->frame_size * ctx->max_restart_interval;
586
587
    ctx->lpc_sample_buffer = av_malloc_array(size, sizeof(int32_t));
588
    if (!ctx->lpc_sample_buffer) {
589
        av_log(avctx, AV_LOG_ERROR,
590
               "Not enough memory for buffering samples.\n");
591
        return AVERROR(ENOMEM);
592
    }
593
594
    size = ctx->one_sample_buffer_size * ctx->max_restart_interval;
595
596
    ctx->major_scratch_buffer = av_malloc_array(size, sizeof(int32_t));
597
    if (!ctx->major_scratch_buffer) {
598
        av_log(avctx, AV_LOG_ERROR,
599
               "Not enough memory for buffering samples.\n");
600
        return AVERROR(ENOMEM);
601
    }
602
603
    ctx->major_inout_buffer = av_malloc_array(size, sizeof(int32_t));
604
    if (!ctx->major_inout_buffer) {
605
        av_log(avctx, AV_LOG_ERROR,
606
               "Not enough memory for buffering samples.\n");
607
        return AVERROR(ENOMEM);
608
    }
609
610
    ff_mlp_init_crc();
611
612
    ctx->num_substreams = 1; // TODO: change this after adding multi-channel support for TrueHD
613
614
    if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
615
        /* MLP */
616
        switch(avctx->channel_layout) {
617
        case AV_CH_LAYOUT_MONO:
618
            ctx->channel_arrangement = 0; break;
619
        case AV_CH_LAYOUT_STEREO:
620
            ctx->channel_arrangement = 1; break;
621
        case AV_CH_LAYOUT_2_1:
622
            ctx->channel_arrangement = 2; break;
623
        case AV_CH_LAYOUT_QUAD:
624
            ctx->channel_arrangement = 3; break;
625
        case AV_CH_LAYOUT_2POINT1:
626
            ctx->channel_arrangement = 4; break;
627
        case AV_CH_LAYOUT_SURROUND:
628
            ctx->channel_arrangement = 7; break;
629
        case AV_CH_LAYOUT_4POINT0:
630
            ctx->channel_arrangement = 8; break;
631
        case AV_CH_LAYOUT_5POINT0_BACK:
632
            ctx->channel_arrangement = 9; break;
633
        case AV_CH_LAYOUT_3POINT1:
634
            ctx->channel_arrangement = 10; break;
635
        case AV_CH_LAYOUT_4POINT1:
636
            ctx->channel_arrangement = 11; break;
637
        case AV_CH_LAYOUT_5POINT1_BACK:
638
            ctx->channel_arrangement = 12; break;
639
        default:
640
            av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
641
            return AVERROR(EINVAL);
642
        }
643
        ctx->flags = FLAGS_DVDA;
644
        ctx->channel_occupancy = ff_mlp_ch_info[ctx->channel_arrangement].channel_occupancy;
645
        ctx->summary_info      = ff_mlp_ch_info[ctx->channel_arrangement].summary_info     ;
646
    } else {
647
        /* TrueHD */
648
        switch(avctx->channel_layout) {
649
        case AV_CH_LAYOUT_STEREO:
650
            ctx->ch_modifier_thd0    = 0;
651
            ctx->ch_modifier_thd1    = 0;
652
            ctx->ch_modifier_thd2    = 0;
653
            ctx->channel_arrangement = 1;
654
            break;
655
        case AV_CH_LAYOUT_5POINT0_BACK:
656
            ctx->ch_modifier_thd0    = 1;
657
            ctx->ch_modifier_thd1    = 1;
658
            ctx->ch_modifier_thd2    = 1;
659
            ctx->channel_arrangement = 11;
660
            break;
661
        case AV_CH_LAYOUT_5POINT1_BACK:
662
            ctx->ch_modifier_thd0    = 2;
663
            ctx->ch_modifier_thd1    = 1;
664
            ctx->ch_modifier_thd2    = 2;
665
            ctx->channel_arrangement = 15;
666
            break;
667
        default:
668
            av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
669
            return AVERROR(EINVAL);
670
        }
671
        ctx->flags = 0;
672
        ctx->channel_occupancy = 0;
673
        ctx->summary_info = 0;
674
    }
675
676
    size = sizeof(unsigned int) * ctx->max_restart_interval;
677
678
    ctx->frame_size = av_malloc(size);
679
    if (!ctx->frame_size)
680
        return AVERROR(ENOMEM);
681
682
    ctx->max_output_bits = av_malloc(size);
683
    if (!ctx->max_output_bits)
684
        return AVERROR(ENOMEM);
685
686
    size = sizeof(int32_t)
687
         * ctx->num_substreams * ctx->max_restart_interval;
688
689
    ctx->lossless_check_data = av_malloc(size);
690
    if (!ctx->lossless_check_data)
691
        return AVERROR(ENOMEM);
692
693
    for (index = 0; index < ctx->restart_intervals; index++) {
694
        ctx->seq_offset[index] = sum;
695
        ctx->seq_size  [index] = ((index + 1) * ctx->min_restart_interval) + 1;
696
        sum += ctx->seq_size[index];
697
    }
698
    ctx->sequence_size = sum;
699
    size = sizeof(ChannelParams)
700
         * ctx->restart_intervals * ctx->sequence_size * ctx->avctx->channels;
701
    ctx->channel_params = av_malloc(size);
702
    if (!ctx->channel_params) {
703
        av_log(avctx, AV_LOG_ERROR,
704
               "Not enough memory for analysis context.\n");
705
        return AVERROR(ENOMEM);
706
    }
707
708
    size = sizeof(DecodingParams)
709
         * ctx->restart_intervals * ctx->sequence_size * ctx->num_substreams;
710
    ctx->decoding_params = av_malloc(size);
711
    if (!ctx->decoding_params) {
712
        av_log(avctx, AV_LOG_ERROR,
713
               "Not enough memory for analysis context.\n");
714
        return AVERROR(ENOMEM);
715
    }
716
717
    for (substr = 0; substr < ctx->num_substreams; substr++) {
718
        RestartHeader  *rh = &ctx->restart_header [substr];
719
720
        /* TODO see if noisegen_seed is really worth it. */
721
        rh->noisegen_seed      = 0;
722
723
        rh->min_channel        = 0;
724
        rh->max_channel        = avctx->channels - 1;
725
        /* FIXME: this works for 1 and 2 channels, but check for more */
726
        rh->max_matrix_channel = rh->max_channel;
727
    }
728
729
    clear_channel_params(ctx, restart_channel_params);
730
    clear_decoding_params(ctx, restart_decoding_params);
731
732
    if ((ret = ff_lpc_init(&ctx->lpc_ctx, ctx->number_of_samples,
733
                    MLP_MAX_LPC_ORDER, FF_LPC_TYPE_LEVINSON)) < 0) {
734
        av_log(avctx, AV_LOG_ERROR,
735
               "Not enough memory for LPC context.\n");
736
        return ret;
737
    }
738
739
    ff_af_queue_init(avctx, &ctx->afq);
740
741
    return 0;
742
}
743
744
/****************************************************************************
745
 ****************** Functions that write to the bitstream *******************
746
 ****************************************************************************/
747
748
/** Writes a major sync header to the bitstream. */
749
static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
750
{
751
    PutBitContext pb;
752
753
    init_put_bits(&pb, buf, buf_size);
754
755
    put_bits(&pb, 24, SYNC_MAJOR               );
756
757
    if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
758
        put_bits(&pb,  8, SYNC_MLP                 );
759
        put_bits(&pb,  4, ctx->coded_sample_fmt [0]);
760
        put_bits(&pb,  4, ctx->coded_sample_fmt [1]);
761
        put_bits(&pb,  4, ctx->coded_sample_rate[0]);
762
        put_bits(&pb,  4, ctx->coded_sample_rate[1]);
763
        put_bits(&pb,  4, 0                        ); /* ignored */
764
        put_bits(&pb,  4, 0                        ); /* multi_channel_type */
765
        put_bits(&pb,  3, 0                        ); /* ignored */
766
        put_bits(&pb,  5, ctx->channel_arrangement );
767
    } else if (ctx->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
768
        put_bits(&pb,  8, SYNC_TRUEHD              );
769
        put_bits(&pb,  4, ctx->coded_sample_rate[0]);
770
        put_bits(&pb,  4, 0                        ); /* ignored */
771
        put_bits(&pb,  2, ctx->ch_modifier_thd0    );
772
        put_bits(&pb,  2, ctx->ch_modifier_thd1    );
773
        put_bits(&pb,  5, ctx->channel_arrangement );
774
        put_bits(&pb,  2, ctx->ch_modifier_thd2    );
775
        put_bits(&pb, 13, ctx->channel_arrangement );
776
    }
777
778
    put_bits(&pb, 16, MAJOR_SYNC_INFO_SIGNATURE);
779
    put_bits(&pb, 16, ctx->flags               );
780
    put_bits(&pb, 16, 0                        ); /* ignored */
781
    put_bits(&pb,  1, 1                        ); /* is_vbr */
782
    put_bits(&pb, 15, ctx->coded_peak_bitrate  );
783
    put_bits(&pb,  4, 1                        ); /* num_substreams */
784
    put_bits(&pb,  4, 0x1                      ); /* ignored */
785
786
    /* channel_meaning */
787
    put_bits(&pb,  8, ctx->substream_info      );
788
    put_bits(&pb,  5, ctx->fs                  );
789
    put_bits(&pb,  5, ctx->wordlength          );
790
    put_bits(&pb,  6, ctx->channel_occupancy   );
791
    put_bits(&pb,  3, 0                        ); /* ignored */
792
    put_bits(&pb, 10, 0                        ); /* speaker_layout */
793
    put_bits(&pb,  3, 0                        ); /* copy_protection */
794
    put_bits(&pb, 16, 0x8080                   ); /* ignored */
795
    put_bits(&pb,  7, 0                        ); /* ignored */
796
    put_bits(&pb,  4, 0                        ); /* source_format */
797
    put_bits(&pb,  5, ctx->summary_info        );
798
799
    flush_put_bits(&pb);
800
801
    AV_WL16(buf+26, ff_mlp_checksum16(buf, 26));
802
}
803
804
/** Writes a restart header to the bitstream. Damaged streams can start being
805
 *  decoded losslessly again after such a header and the subsequent decoding
806
 *  params header.
807
 */
808
static void write_restart_header(MLPEncodeContext *ctx, PutBitContext *pb)
809
{
810
    RestartHeader *rh = ctx->cur_restart_header;
811
    uint8_t lossless_check = xor_32_to_8(rh->lossless_check_data);
812
    unsigned int start_count = put_bits_count(pb);
813
    PutBitContext tmpb;
814
    uint8_t checksum;
815
    unsigned int ch;
816
817
    put_bits(pb, 14, 0x31ea                ); /* TODO 0x31eb */
818
    put_bits(pb, 16, ctx->timestamp        );
819
    put_bits(pb,  4, rh->min_channel       );
820
    put_bits(pb,  4, rh->max_channel       );
821
    put_bits(pb,  4, rh->max_matrix_channel);
822
    put_bits(pb,  4, rh->noise_shift       );
823
    put_bits(pb, 23, rh->noisegen_seed     );
824
    put_bits(pb,  4, 0                     ); /* TODO max_shift */
825
    put_bits(pb,  5, rh->max_huff_lsbs     );
826
    put_bits(pb,  5, rh->max_output_bits   );
827
    put_bits(pb,  5, rh->max_output_bits   );
828
    put_bits(pb,  1, rh->data_check_present);
829
    put_bits(pb,  8, lossless_check        );
830
    put_bits(pb, 16, 0                     ); /* ignored */
831
832
    for (ch = 0; ch <= rh->max_matrix_channel; ch++)
833
        put_bits(pb, 6, ch);
834
835
    /* Data must be flushed for the checksum to be correct. */
836
    tmpb = *pb;
837
    flush_put_bits(&tmpb);
838
839
    checksum = ff_mlp_restart_checksum(pb->buf, put_bits_count(pb) - start_count);
840
841
    put_bits(pb,  8, checksum);
842
}
843
844
/** Writes matrix params for all primitive matrices to the bitstream. */
845
static void write_matrix_params(MLPEncodeContext *ctx, PutBitContext *pb)
846
{
847
    DecodingParams *dp = ctx->cur_decoding_params;
848
    MatrixParams *mp = &dp->matrix_params;
849
    unsigned int mat;
850
851
    put_bits(pb, 4, mp->count);
852
853
    for (mat = 0; mat < mp->count; mat++) {
854
        unsigned int channel;
855
856
        put_bits(pb, 4, mp->outch[mat]); /* matrix_out_ch */
857
        put_bits(pb, 4, mp->fbits[mat]);
858
        put_bits(pb, 1, 0             ); /* lsb_bypass */
859
860
        for (channel = 0; channel < ctx->num_channels; channel++) {
861
            int32_t coeff = mp->coeff[mat][channel];
862
863
            if (coeff) {
864
                put_bits(pb, 1, 1);
865
866
                coeff >>= 14 - mp->fbits[mat];
867
868
                put_sbits(pb, mp->fbits[mat] + 2, coeff);
869
            } else {
870
                put_bits(pb, 1, 0);
871
            }
872
        }
873
    }
874
}
875
876
/** Writes filter parameters for one filter to the bitstream. */
877
static void write_filter_params(MLPEncodeContext *ctx, PutBitContext *pb,
878
                                unsigned int channel, unsigned int filter)
879
{
880
    FilterParams *fp = &ctx->cur_channel_params[channel].filter_params[filter];
881
882
    put_bits(pb, 4, fp->order);
883
884
    if (fp->order > 0) {
885
        int i;
886
        int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
887
888
        put_bits(pb, 4, fp->shift      );
889
        put_bits(pb, 5, fp->coeff_bits );
890
        put_bits(pb, 3, fp->coeff_shift);
891
892
        for (i = 0; i < fp->order; i++) {
893
            put_sbits(pb, fp->coeff_bits, fcoeff[i] >> fp->coeff_shift);
894
        }
895
896
        /* TODO state data for IIR filter. */
897
        put_bits(pb, 1, 0);
898
    }
899
}
900
901
/** Writes decoding parameters to the bitstream. These change very often,
902
 *  usually at almost every frame.
903
 */
904
static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb,
905
                                  int params_changed)
906
{
907
    DecodingParams *dp = ctx->cur_decoding_params;
908
    RestartHeader  *rh = ctx->cur_restart_header;
909
    MatrixParams *mp = &dp->matrix_params;
910
    unsigned int ch;
911
912
    if (dp->param_presence_flags != PARAMS_DEFAULT &&
913
        params_changed & PARAM_PRESENCE_FLAGS) {
914
        put_bits(pb, 1, 1);
915
        put_bits(pb, 8, dp->param_presence_flags);
916
    } else {
917
        put_bits(pb, 1, 0);
918
    }
919
920
    if (dp->param_presence_flags & PARAM_BLOCKSIZE) {
921
        if (params_changed       & PARAM_BLOCKSIZE) {
922
            put_bits(pb, 1, 1);
923
            put_bits(pb, 9, dp->blocksize);
924
        } else {
925
            put_bits(pb, 1, 0);
926
        }
927
    }
928
929
    if (dp->param_presence_flags & PARAM_MATRIX) {
930
        if (params_changed       & PARAM_MATRIX) {
931
            put_bits(pb, 1, 1);
932
            write_matrix_params(ctx, pb);
933
        } else {
934
            put_bits(pb, 1, 0);
935
        }
936
    }
937
938
    if (dp->param_presence_flags & PARAM_OUTSHIFT) {
939
        if (params_changed       & PARAM_OUTSHIFT) {
940
            put_bits(pb, 1, 1);
941
            for (ch = 0; ch <= rh->max_matrix_channel; ch++)
942
                put_sbits(pb, 4, mp->shift[ch]);
943
        } else {
944
            put_bits(pb, 1, 0);
945
        }
946
    }
947
948
    if (dp->param_presence_flags & PARAM_QUANTSTEP) {
949
        if (params_changed       & PARAM_QUANTSTEP) {
950
            put_bits(pb, 1, 1);
951
            for (ch = 0; ch <= rh->max_channel; ch++)
952
                put_bits(pb, 4, dp->quant_step_size[ch]);
953
        } else {
954
            put_bits(pb, 1, 0);
955
        }
956
    }
957
958
    for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
959
        ChannelParams *cp = &ctx->cur_channel_params[ch];
960
961
        if (dp->param_presence_flags & 0xF) {
962
            put_bits(pb, 1, 1);
963
964
            if (dp->param_presence_flags & PARAM_FIR) {
965
                if (params_changed       & PARAM_FIR) {
966
                    put_bits(pb, 1, 1);
967
                    write_filter_params(ctx, pb, ch, FIR);
968
                } else {
969
                    put_bits(pb, 1, 0);
970
                }
971
            }
972
973
            if (dp->param_presence_flags & PARAM_IIR) {
974
                if (params_changed       & PARAM_IIR) {
975
                    put_bits(pb, 1, 1);
976
                    write_filter_params(ctx, pb, ch, IIR);
977
                } else {
978
                    put_bits(pb, 1, 0);
979
                }
980
            }
981
982
            if (dp->param_presence_flags & PARAM_HUFFOFFSET) {
983
                if (params_changed       & PARAM_HUFFOFFSET) {
984
                    put_bits (pb,  1, 1);
985
                    put_sbits(pb, 15, cp->huff_offset);
986
                } else {
987
                    put_bits(pb, 1, 0);
988
                }
989
            }
990
            if (cp->codebook > 0 && cp->huff_lsbs > 24) {
991
                av_log(ctx->avctx, AV_LOG_ERROR, "Invalid Huff LSBs\n");
992
            }
993
994
            put_bits(pb, 2, cp->codebook );
995
            put_bits(pb, 5, cp->huff_lsbs);
996
        } else {
997
            put_bits(pb, 1, 0);
998
        }
999
    }
1000
}
1001
1002
/** Writes the residuals to the bitstream. That is, the VLC codes from the
1003
 *  codebooks (if any is used), and then the residual.
1004
 */
1005
static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb)
1006
{
1007
    DecodingParams *dp = ctx->cur_decoding_params;
1008
    RestartHeader  *rh = ctx->cur_restart_header;
1009
    int32_t *sample_buffer = ctx->write_buffer;
1010
    int32_t sign_huff_offset[MAX_CHANNELS];
1011
    int codebook_index      [MAX_CHANNELS];
1012
    int lsb_bits            [MAX_CHANNELS];
1013
    unsigned int i, ch;
1014
1015
    for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
1016
        ChannelParams *cp = &ctx->cur_channel_params[ch];
1017
        int sign_shift;
1018
1019
        lsb_bits        [ch] = cp->huff_lsbs - dp->quant_step_size[ch];
1020
        codebook_index  [ch] = cp->codebook  - 1;
1021
        sign_huff_offset[ch] = cp->huff_offset;
1022
1023
        sign_shift = lsb_bits[ch] + (cp->codebook ? 2 - cp->codebook : -1);
1024
1025
        if (cp->codebook > 0)
1026
            sign_huff_offset[ch] -= 7 << lsb_bits[ch];
1027
1028
        /* Unsign if needed. */
1029
        if (sign_shift >= 0)
1030
            sign_huff_offset[ch] -= 1 << sign_shift;
1031
    }
1032
1033
    for (i = 0; i < dp->blocksize; i++) {
1034
        for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
1035
            int32_t sample = *sample_buffer++ >> dp->quant_step_size[ch];
1036
            sample -= sign_huff_offset[ch];
1037
1038
            if (codebook_index[ch] >= 0) {
1039
                int vlc = sample >> lsb_bits[ch];
1040
                put_bits(pb, ff_mlp_huffman_tables[codebook_index[ch]][vlc][1],
1041
                             ff_mlp_huffman_tables[codebook_index[ch]][vlc][0]);
1042
            }
1043
1044
            put_sbits(pb, lsb_bits[ch], sample);
1045
        }
1046
        sample_buffer += 2; /* noise channels */
1047
    }
1048
1049
    ctx->write_buffer = sample_buffer;
1050
}
1051
1052
/** Writes the substreams data to the bitstream. */
1053
static uint8_t *write_substrs(MLPEncodeContext *ctx, uint8_t *buf, int buf_size,
1054
                              int restart_frame,
1055
                              uint16_t substream_data_len[MAX_SUBSTREAMS])
1056
{
1057
    int32_t *lossless_check_data = ctx->lossless_check_data;
1058
    unsigned int substr;
1059
    int end = 0;
1060
1061
    lossless_check_data += ctx->frame_index * ctx->num_substreams;
1062
1063
    for (substr = 0; substr < ctx->num_substreams; substr++) {
1064
        unsigned int cur_subblock_index = ctx->major_cur_subblock_index;
1065
        unsigned int num_subblocks = ctx->major_filter_state_subblock;
1066
        unsigned int subblock;
1067
        RestartHeader  *rh = &ctx->restart_header [substr];
1068
        int substr_restart_frame = restart_frame;
1069
        uint8_t parity, checksum;
1070
        PutBitContext pb, tmpb;
1071
        int params_changed;
1072
1073
        ctx->cur_restart_header = rh;
1074
1075
        init_put_bits(&pb, buf, buf_size);
1076
1077
        for (subblock = 0; subblock <= num_subblocks; subblock++) {
1078
            unsigned int subblock_index;
1079
1080
            subblock_index = cur_subblock_index++;
1081
1082
            ctx->cur_decoding_params = &ctx->major_decoding_params[subblock_index][substr];
1083
            ctx->cur_channel_params = ctx->major_channel_params[subblock_index];
1084
1085
            params_changed = ctx->major_params_changed[subblock_index][substr];
1086
1087
            if (substr_restart_frame || params_changed) {
1088
                put_bits(&pb, 1, 1);
1089
1090
                if (substr_restart_frame) {
1091
                    put_bits(&pb, 1, 1);
1092
1093
                    write_restart_header(ctx, &pb);
1094
                    rh->lossless_check_data = 0;
1095
                } else {
1096
                    put_bits(&pb, 1, 0);
1097
                }
1098
1099
                write_decoding_params(ctx, &pb, params_changed);
1100
            } else {
1101
                put_bits(&pb, 1, 0);
1102
            }
1103
1104
            write_block_data(ctx, &pb);
1105
1106
            put_bits(&pb, 1, !substr_restart_frame);
1107
1108
            substr_restart_frame = 0;
1109
        }
1110
1111
        put_bits(&pb, (-put_bits_count(&pb)) & 15, 0);
1112
1113
        rh->lossless_check_data ^= *lossless_check_data++;
1114
1115
        if (ctx->last_frame == ctx->inout_buffer) {
1116
            /* TODO find a sample and implement shorten_by. */
1117
            put_bits(&pb, 32, END_OF_STREAM);
1118
        }
1119
1120
        /* Data must be flushed for the checksum and parity to be correct. */
1121
        tmpb = pb;
1122
        flush_put_bits(&tmpb);
1123
1124
        parity   = ff_mlp_calculate_parity(buf, put_bits_count(&pb) >> 3) ^ 0xa9;
1125
        checksum = ff_mlp_checksum8       (buf, put_bits_count(&pb) >> 3);
1126
1127
        put_bits(&pb, 8, parity  );
1128
        put_bits(&pb, 8, checksum);
1129
1130
        flush_put_bits(&pb);
1131
1132
        end += put_bits_count(&pb) >> 3;
1133
        substream_data_len[substr] = end;
1134
1135
        buf += put_bits_count(&pb) >> 3;
1136
    }
1137
1138
    ctx->major_cur_subblock_index += ctx->major_filter_state_subblock + 1;
1139
    ctx->major_filter_state_subblock = 0;
1140
1141
    return buf;
1142
}
1143
1144
/** Writes the access unit and substream headers to the bitstream. */
1145
static void write_frame_headers(MLPEncodeContext *ctx, uint8_t *frame_header,
1146
                                uint8_t *substream_headers, unsigned int length,
1147
                                int restart_frame,
1148
                                uint16_t substream_data_len[MAX_SUBSTREAMS])
1149
{
1150
    uint16_t access_unit_header = 0;
1151
    uint16_t parity_nibble = 0;
1152
    unsigned int substr;
1153
1154
    parity_nibble  = ctx->dts;
1155
    parity_nibble ^= length;
1156
1157
    for (substr = 0; substr < ctx->num_substreams; substr++) {
1158
        uint16_t substr_hdr = 0;
1159
1160
        substr_hdr |= (0 << 15); /* extraword */
1161
        substr_hdr |= (!restart_frame << 14); /* !restart_frame */
1162
        substr_hdr |= (1 << 13); /* checkdata */
1163
        substr_hdr |= (0 << 12); /* ??? */
1164
        substr_hdr |= (substream_data_len[substr] / 2) & 0x0FFF;
1165
1166
        AV_WB16(substream_headers, substr_hdr);
1167
1168
        parity_nibble ^= *substream_headers++;
1169
        parity_nibble ^= *substream_headers++;
1170
    }
1171
1172
    parity_nibble ^= parity_nibble >> 8;
1173
    parity_nibble ^= parity_nibble >> 4;
1174
    parity_nibble &= 0xF;
1175
1176
    access_unit_header |= (parity_nibble ^ 0xF) << 12;
1177
    access_unit_header |= length & 0xFFF;
1178
1179
    AV_WB16(frame_header  , access_unit_header);
1180
    AV_WB16(frame_header+2, ctx->dts          );
1181
}
1182
1183
/** Writes an entire access unit to the bitstream. */
1184
static unsigned int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf,
1185
                                      int buf_size, int restart_frame)
1186
{
1187
    uint16_t substream_data_len[MAX_SUBSTREAMS];
1188
    uint8_t *buf1, *buf0 = buf;
1189
    unsigned int substr;
1190
    int total_length;
1191
1192
    if (buf_size < 4)
1193
        return AVERROR(EINVAL);
1194
1195
    /* Frame header will be written at the end. */
1196
    buf      += 4;
1197
    buf_size -= 4;
1198
1199
    if (restart_frame) {
1200
        if (buf_size < 28)
1201
            return AVERROR(EINVAL);
1202
        write_major_sync(ctx, buf, buf_size);
1203
        buf      += 28;
1204
        buf_size -= 28;
1205
    }
1206
1207
    buf1 = buf;
1208
1209
    /* Substream headers will be written at the end. */
1210
    for (substr = 0; substr < ctx->num_substreams; substr++) {
1211
        buf      += 2;
1212
        buf_size -= 2;
1213
    }
1214
1215
    buf = write_substrs(ctx, buf, buf_size, restart_frame, substream_data_len);
1216
1217
    total_length = buf - buf0;
1218
1219
    write_frame_headers(ctx, buf0, buf1, total_length / 2, restart_frame, substream_data_len);
1220
1221
    return total_length;
1222
}
1223
1224
/****************************************************************************
1225
 ****************** Functions that input data to context ********************
1226
 ****************************************************************************/
1227
1228
/** Inputs data from the samples passed by lavc into the context, shifts them
1229
 *  appropriately depending on the bit-depth, and calculates the
1230
 *  lossless_check_data that will be written to the restart header.
1231
 */
1232
static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples,
1233
                                int is24)
1234
{
1235
    int32_t *lossless_check_data = ctx->lossless_check_data;
1236
    const int32_t *samples_32 = (const int32_t *) samples;
1237
    const int16_t *samples_16 = (const int16_t *) samples;
1238
    unsigned int substr;
1239
1240
    lossless_check_data += ctx->frame_index * ctx->num_substreams;
1241
1242
    for (substr = 0; substr < ctx->num_substreams; substr++) {
1243
        RestartHeader  *rh = &ctx->restart_header [substr];
1244
        int32_t *sample_buffer = ctx->inout_buffer;
1245
        int32_t temp_lossless_check_data = 0;
1246
        uint32_t greatest = 0;
1247
        unsigned int channel;
1248
        int i;
1249
1250
        for (i = 0; i < ctx->frame_size[ctx->frame_index]; i++) {
1251
            for (channel = 0; channel <= rh->max_channel; channel++) {
1252
                uint32_t abs_sample;
1253
                int32_t sample;
1254
1255
                sample = is24 ? *samples_32++ >> 8 : *samples_16++ * 256;
1256
1257
                /* TODO Find out if number_sbits can be used for negative values. */
1258
                abs_sample = FFABS(sample);
1259
                if (greatest < abs_sample)
1260
                    greatest = abs_sample;
1261
1262
                temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;
1263
                *sample_buffer++ = sample;
1264
            }
1265
1266
            sample_buffer += 2; /* noise channels */
1267
        }
1268
1269
        ctx->max_output_bits[ctx->frame_index] = number_sbits(greatest);
1270
1271
        *lossless_check_data++ = temp_lossless_check_data;
1272
    }
1273
}
1274
1275
/** Wrapper function for inputting data in two different bit-depths. */
1276
static void input_data(MLPEncodeContext *ctx, void *samples)
1277
{
1278
    if (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
1279
        input_data_internal(ctx, samples, 1);
1280
    else
1281
        input_data_internal(ctx, samples, 0);
1282
}
1283
1284
static void input_to_sample_buffer(MLPEncodeContext *ctx)
1285
{
1286
    int32_t *sample_buffer = ctx->sample_buffer;
1287
    unsigned int index;
1288
1289
    for (index = 0; index < ctx->number_of_frames; index++) {
1290
        unsigned int cur_index = (ctx->starting_frame_index + index) % ctx->max_restart_interval;
1291
        int32_t *input_buffer = ctx->inout_buffer + cur_index * ctx->one_sample_buffer_size;
1292
        unsigned int i, channel;
1293
1294
        for (i = 0; i < ctx->frame_size[cur_index]; i++) {
1295
            for (channel = 0; channel < ctx->avctx->channels; channel++)
1296
                *sample_buffer++ = *input_buffer++;
1297
            sample_buffer += 2; /* noise_channels */
1298
            input_buffer += 2; /* noise_channels */
1299
        }
1300
    }
1301
}
1302
1303
/****************************************************************************
1304
 ********* Functions that analyze the data and set the parameters ***********
1305
 ****************************************************************************/
1306
1307
/** Counts the number of trailing zeroes in a value */
1308
static int number_trailing_zeroes(int32_t sample)
1309
{
1310
    int bits;
1311
1312
    for (bits = 0; bits < 24 && !(sample & (1<<bits)); bits++);
1313
1314
    /* All samples are 0. TODO Return previous quant_step_size to avoid
1315
     * writing a new header. */
1316
    if (bits == 24)
1317
        return 0;
1318
1319
    return bits;
1320
}
1321
1322
/** Determines how many bits are zero at the end of all samples so they can be
1323
 *  shifted out.
1324
 */
1325
static void determine_quant_step_size(MLPEncodeContext *ctx)
1326
{
1327
    DecodingParams *dp = ctx->cur_decoding_params;
1328
    RestartHeader  *rh = ctx->cur_restart_header;
1329
    MatrixParams *mp = &dp->matrix_params;
1330
    int32_t *sample_buffer = ctx->sample_buffer;
1331
    int32_t sample_mask[MAX_CHANNELS];
1332
    unsigned int channel;
1333
    int i;
1334
1335
    memset(sample_mask, 0x00, sizeof(sample_mask));
1336
1337
    for (i = 0; i < ctx->number_of_samples; i++) {
1338
        for (channel = 0; channel <= rh->max_channel; channel++)
1339
            sample_mask[channel] |= *sample_buffer++;
1340
1341
        sample_buffer += 2; /* noise channels */
1342
    }
1343
1344
    for (channel = 0; channel <= rh->max_channel; channel++)
1345
        dp->quant_step_size[channel] = number_trailing_zeroes(sample_mask[channel]) - mp->shift[channel];
1346
}
1347
1348
/** Determines the smallest number of bits needed to encode the filter
1349
 *  coefficients, and if it's possible to right-shift their values without
1350
 *  losing any precision.
1351
 */
1352
static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, int32_t *fcoeff)
1353
{
1354
    int min = INT_MAX, max = INT_MIN;
1355
    int bits, shift;
1356
    int coeff_mask = 0;
1357
    int order;
1358
1359
    for (order = 0; order < fp->order; order++) {
1360
        int coeff = fcoeff[order];
1361
1362
        if (coeff < min)
1363
            min = coeff;
1364
        if (coeff > max)
1365
            max = coeff;
1366
1367
        coeff_mask |= coeff;
1368
    }
1369
1370
    bits = FFMAX(number_sbits(min), number_sbits(max));
1371
1372
    for (shift = 0; shift < 7 && bits + shift < 16 && !(coeff_mask & (1<<shift)); shift++);
1373
1374
    fp->coeff_bits  = bits;
1375
    fp->coeff_shift = shift;
1376
}
1377
1378
/** Determines the best filter parameters for the given data and writes the
1379
 *  necessary information to the context.
1380
 *  TODO Add IIR filter predictor!
1381
 */
1382
static void set_filter_params(MLPEncodeContext *ctx,
1383
                              unsigned int channel, unsigned int filter,
1384
                              int clear_filter)
1385
{
1386
    ChannelParams *cp = &ctx->cur_channel_params[channel];
1387
    FilterParams *fp = &cp->filter_params[filter];
1388
1389
    if ((filter == IIR && ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE) ||
1390
        clear_filter) {
1391
        fp->order = 0;
1392
    } else if (filter == IIR) {
1393
        fp->order = 0;
1394
    } else if (filter == FIR) {
1395
        const int max_order = (ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE)
1396
                              ? 4 : MLP_MAX_LPC_ORDER;
1397
        int32_t *sample_buffer = ctx->sample_buffer + channel;
1398
        int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
1399
        int32_t *lpc_samples = ctx->lpc_sample_buffer;
1400
        int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
1401
        int shift[MLP_MAX_LPC_ORDER];
1402
        unsigned int i;
1403
        int order;
1404
1405
        for (i = 0; i < ctx->number_of_samples; i++) {
1406
            *lpc_samples++ = *sample_buffer;
1407
            sample_buffer += ctx->num_channels;
1408
        }
1409
1410
        order = ff_lpc_calc_coefs(&ctx->lpc_ctx, ctx->lpc_sample_buffer,
1411
                                  ctx->number_of_samples, MLP_MIN_LPC_ORDER,
1412
                                  max_order, 11, coefs, shift, FF_LPC_TYPE_LEVINSON, 0,
1413
                                  ORDER_METHOD_EST, MLP_MIN_LPC_SHIFT,
1414
                                  MLP_MAX_LPC_SHIFT, MLP_MIN_LPC_SHIFT);
1415
1416
        fp->order = order;
1417
        fp->shift = shift[order-1];
1418
1419
        for (i = 0; i < order; i++)
1420
            fcoeff[i] = coefs[order-1][i];
1421
1422
        code_filter_coeffs(ctx, fp, fcoeff);
1423
    }
1424
}
1425
1426
/** Tries to determine a good prediction filter, and applies it to the samples
1427
 *  buffer if the filter is good enough. Sets the filter data to be cleared if
1428
 *  no good filter was found.
1429
 */
1430
static void determine_filters(MLPEncodeContext *ctx)
1431
{
1432
    RestartHeader *rh = ctx->cur_restart_header;
1433
    int channel, filter;
1434
1435
    for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
1436
        for (filter = 0; filter < NUM_FILTERS; filter++)
1437
            set_filter_params(ctx, channel, filter, 0);
1438
    }
1439
}
1440
1441
enum MLPChMode {
1442
    MLP_CHMODE_LEFT_RIGHT,
1443
    MLP_CHMODE_LEFT_SIDE,
1444
    MLP_CHMODE_RIGHT_SIDE,
1445
    MLP_CHMODE_MID_SIDE,
1446
};
1447
1448
static enum MLPChMode estimate_stereo_mode(MLPEncodeContext *ctx)
1449
{
1450
    uint64_t score[4], sum[4] = { 0, 0, 0, 0, };
1451
    int32_t *right_ch = ctx->sample_buffer + 1;
1452
    int32_t *left_ch  = ctx->sample_buffer;
1453
    int i;
1454
    enum MLPChMode best = 0;
1455
1456
    for(i = 2; i < ctx->number_of_samples; i++) {
1457
        int32_t left  = left_ch [i * ctx->num_channels] - 2 * left_ch [(i - 1) * ctx->num_channels] + left_ch [(i - 2) * ctx->num_channels];
1458
        int32_t right = right_ch[i * ctx->num_channels] - 2 * right_ch[(i - 1) * ctx->num_channels] + right_ch[(i - 2) * ctx->num_channels];
1459
1460
        sum[0] += FFABS( left        );
1461
        sum[1] += FFABS(        right);
1462
        sum[2] += FFABS((left + right) >> 1);
1463
        sum[3] += FFABS( left - right);
1464
    }
1465
1466
    score[MLP_CHMODE_LEFT_RIGHT] = sum[0] + sum[1];
1467
    score[MLP_CHMODE_LEFT_SIDE]  = sum[0] + sum[3];
1468
    score[MLP_CHMODE_RIGHT_SIDE] = sum[1] + sum[3];
1469
    score[MLP_CHMODE_MID_SIDE]   = sum[2] + sum[3];
1470
1471
    for(i = 1; i < 3; i++)
1472
        if(score[i] < score[best])
1473
            best = i;
1474
1475
    return best;
1476
}
1477
1478
/** Determines how many fractional bits are needed to encode matrix
1479
 *  coefficients. Also shifts the coefficients to fit within 2.14 bits.
1480
 */
1481
static void code_matrix_coeffs(MLPEncodeContext *ctx, unsigned int mat)
1482
{
1483
    DecodingParams *dp = ctx->cur_decoding_params;
1484
    MatrixParams *mp = &dp->matrix_params;
1485
    int32_t coeff_mask = 0;
1486
    unsigned int channel;
1487
    unsigned int bits;
1488
1489
    for (channel = 0; channel < ctx->num_channels; channel++) {
1490
        int32_t coeff = mp->coeff[mat][channel];
1491
        coeff_mask |= coeff;
1492
    }
1493
1494
    for (bits = 0; bits < 14 && !(coeff_mask & (1<<bits)); bits++);
1495
1496
    mp->fbits   [mat] = 14 - bits;
1497
}
1498
1499
/** Determines best coefficients to use for the lossless matrix. */
1500
static void lossless_matrix_coeffs(MLPEncodeContext *ctx)
1501
{
1502
    DecodingParams *dp = ctx->cur_decoding_params;
1503
    MatrixParams *mp = &dp->matrix_params;
1504
    unsigned int shift = 0;
1505
    unsigned int channel;
1506
    int mat;
1507
    enum MLPChMode mode;
1508
1509
    /* No decorrelation for non-stereo. */
1510
    if (ctx->num_channels - 2 != 2) {
1511
        mp->count = 0;
1512
        return;
1513
    }
1514
1515
    mode = estimate_stereo_mode(ctx);
1516
1517
    switch(mode) {
1518
        /* TODO: add matrix for MID_SIDE */
1519
        case MLP_CHMODE_MID_SIDE:
1520
        case MLP_CHMODE_LEFT_RIGHT:
1521
            mp->count    = 0;
1522
            break;
1523
        case MLP_CHMODE_LEFT_SIDE:
1524
            mp->count    = 1;
1525
            mp->outch[0] = 1;
1526
            mp->coeff[0][0] =  1 << 14; mp->coeff[0][1] = -(1 << 14);
1527
            mp->coeff[0][2] =  0 << 14; mp->coeff[0][2] =   0 << 14;
1528
            mp->forco[0][0] =  1 << 14; mp->forco[0][1] = -(1 << 14);
1529
            mp->forco[0][2] =  0 << 14; mp->forco[0][2] =   0 << 14;
1530
            break;
1531
        case MLP_CHMODE_RIGHT_SIDE:
1532
            mp->count    = 1;
1533
            mp->outch[0] = 0;
1534
            mp->coeff[0][0] =  1 << 14; mp->coeff[0][1] =   1 << 14;
1535
            mp->coeff[0][2] =  0 << 14; mp->coeff[0][2] =   0 << 14;
1536
            mp->forco[0][0] =  1 << 14; mp->forco[0][1] = -(1 << 14);
1537
            mp->forco[0][2] =  0 << 14; mp->forco[0][2] =   0 << 14;
1538
            break;
1539
    }
1540
1541
    for (mat = 0; mat < mp->count; mat++)
1542
        code_matrix_coeffs(ctx, mat);
1543
1544
    for (channel = 0; channel < ctx->num_channels; channel++)
1545
        mp->shift[channel] = shift;
1546
}
1547
1548
/** Min and max values that can be encoded with each codebook. The values for
1549
 *  the third codebook take into account the fact that the sign shift for this
1550
 *  codebook is outside the coded value, so it has one more bit of precision.
1551
 *  It should actually be -7 -> 7, shifted down by 0.5.
1552
 */
1553
static const int codebook_extremes[3][2] = {
1554
    {-9, 8}, {-8, 7}, {-15, 14},
1555
};
1556
1557
/** Determines the amount of bits needed to encode the samples using no
1558
 *  codebooks and a specified offset.
1559
 */
1560
static void no_codebook_bits_offset(MLPEncodeContext *ctx,
1561
                                    unsigned int channel, int16_t offset,
1562
                                    int32_t min, int32_t max,
1563
                                    BestOffset *bo)
1564
{
1565
    DecodingParams *dp = ctx->cur_decoding_params;
1566
    int32_t unsign = 0;
1567
    int lsb_bits;
1568
1569
    min -= offset;
1570
    max -= offset;
1571
1572
    lsb_bits = FFMAX(number_sbits(min), number_sbits(max)) - 1;
1573
1574
    lsb_bits += !!lsb_bits;
1575
1576
    if (lsb_bits > 0)
1577
        unsign = 1 << (lsb_bits - 1);
1578
1579
    bo->offset   = offset;
1580
    bo->lsb_bits = lsb_bits;
1581
    bo->bitcount = lsb_bits * dp->blocksize;
1582
    bo->min      = offset - unsign + 1;
1583
    bo->max      = offset + unsign;
1584
}
1585
1586
/** Determines the least amount of bits needed to encode the samples using no
1587
 *  codebooks.
1588
 */
1589
static void no_codebook_bits(MLPEncodeContext *ctx,
1590
                             unsigned int channel,
1591
                             int32_t min, int32_t max,
1592
                             BestOffset *bo)
1593
{
1594
    DecodingParams *dp = ctx->cur_decoding_params;
1595
    int16_t offset;
1596
    int32_t unsign = 0;
1597
    uint32_t diff;
1598
    int lsb_bits;
1599
1600
    /* Set offset inside huffoffset's boundaries by adjusting extremes
1601
     * so that more bits are used, thus shifting the offset. */
1602
    if (min < HUFF_OFFSET_MIN)
1603
        max = FFMAX(max, 2 * HUFF_OFFSET_MIN - min + 1);
1604
    if (max > HUFF_OFFSET_MAX)
1605
        min = FFMIN(min, 2 * HUFF_OFFSET_MAX - max - 1);
1606
1607
    /* Determine offset and minimum number of bits. */
1608
    diff = max - min;
1609
1610
    lsb_bits = number_sbits(diff) - 1;
1611
1612
    if (lsb_bits > 0)
1613
        unsign = 1 << (lsb_bits - 1);
1614
1615
    /* If all samples are the same (lsb_bits == 0), offset must be
1616
     * adjusted because of sign_shift. */
1617
    offset = min + diff / 2 + !!lsb_bits;
1618
1619
    bo->offset   = offset;
1620
    bo->lsb_bits = lsb_bits;
1621
    bo->bitcount = lsb_bits * dp->blocksize;
1622
    bo->min      = max - unsign + 1;
1623
    bo->max      = min + unsign;
1624
}
1625
1626
/** Determines the least amount of bits needed to encode the samples using a
1627
 *  given codebook and a given offset.
1628
 */
1629
static inline void codebook_bits_offset(MLPEncodeContext *ctx,
1630
                                        unsigned int channel, int codebook,
1631
                                        int32_t sample_min, int32_t sample_max,
1632
                                        int16_t offset, BestOffset *bo)
1633
{
1634
    int32_t codebook_min = codebook_extremes[codebook][0];
1635
    int32_t codebook_max = codebook_extremes[codebook][1];
1636
    int32_t *sample_buffer = ctx->sample_buffer + channel;
1637
    DecodingParams *dp = ctx->cur_decoding_params;
1638
    int codebook_offset  = 7 + (2 - codebook);
1639
    int32_t unsign_offset = offset;
1640
    int lsb_bits = 0, bitcount = 0;
1641
    int offset_min = INT_MAX, offset_max = INT_MAX;
1642
    int unsign, mask;
1643
    int i;
1644
1645
    sample_min -= offset;
1646
    sample_max -= offset;
1647
1648
    while (sample_min < codebook_min || sample_max > codebook_max) {
1649
        lsb_bits++;
1650
        sample_min >>= 1;
1651
        sample_max >>= 1;
1652
    }
1653
1654
    unsign = 1 << lsb_bits;
1655
    mask   = unsign - 1;
1656
1657
    if (codebook == 2) {
1658
        unsign_offset -= unsign;
1659
        lsb_bits++;
1660
    }
1661
1662
    for (i = 0; i < dp->blocksize; i++) {
1663
        int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
1664
        int temp_min, temp_max;
1665
1666
        sample -= unsign_offset;
1667
1668
        temp_min = sample & mask;
1669
        if (temp_min < offset_min)
1670
            offset_min = temp_min;
1671
1672
        temp_max = unsign - temp_min - 1;
1673
        if (temp_max < offset_max)
1674
            offset_max = temp_max;
1675
1676
        sample >>= lsb_bits;
1677
1678
        bitcount += ff_mlp_huffman_tables[codebook][sample + codebook_offset][1];
1679
1680
        sample_buffer += ctx->num_channels;
1681
    }
1682
1683
    bo->offset   = offset;
1684
    bo->lsb_bits = lsb_bits;
1685
    bo->bitcount = lsb_bits * dp->blocksize + bitcount;
1686
    bo->min      = FFMAX(offset - offset_min, HUFF_OFFSET_MIN);
1687
    bo->max      = FFMIN(offset + offset_max, HUFF_OFFSET_MAX);
1688
}
1689
1690
/** Determines the least amount of bits needed to encode the samples using a
1691
 *  given codebook. Searches for the best offset to minimize the bits.
1692
 */
1693
static inline void codebook_bits(MLPEncodeContext *ctx,
1694
                                 unsigned int channel, int codebook,
1695
                                 int offset, int32_t min, int32_t max,
1696
                                 BestOffset *bo, int direction)
1697
{
1698
    int previous_count = INT_MAX;
1699
    int offset_min, offset_max;
1700
    int is_greater = 0;
1701
1702
    offset_min = FFMAX(min, HUFF_OFFSET_MIN);
1703
    offset_max = FFMIN(max, HUFF_OFFSET_MAX);
1704
1705
    while (offset <= offset_max && offset >= offset_min) {
1706
        BestOffset temp_bo;
1707
1708
        codebook_bits_offset(ctx, channel, codebook,
1709
                             min, max, offset,
1710
                             &temp_bo);
1711
1712
        if (temp_bo.bitcount < previous_count) {
1713
            if (temp_bo.bitcount < bo->bitcount)
1714
                *bo = temp_bo;
1715
1716
            is_greater = 0;
1717
        } else if (++is_greater >= ctx->max_codebook_search)
1718
            break;
1719
1720
        previous_count = temp_bo.bitcount;
1721
1722
        if (direction) {
1723
            offset = temp_bo.max + 1;
1724
        } else {
1725
            offset = temp_bo.min - 1;
1726
        }
1727
    }
1728
}
1729
1730
/** Determines the least amount of bits needed to encode the samples using
1731
 *  any or no codebook.
1732
 */
1733
static void determine_bits(MLPEncodeContext *ctx)
1734
{
1735
    DecodingParams *dp = ctx->cur_decoding_params;
1736
    RestartHeader  *rh = ctx->cur_restart_header;
1737
    unsigned int channel;
1738
1739
    for (channel = 0; channel <= rh->max_channel; channel++) {
1740
        ChannelParams *cp = &ctx->cur_channel_params[channel];
1741
        int32_t *sample_buffer = ctx->sample_buffer + channel;
1742
        int32_t min = INT32_MAX, max = INT32_MIN;
1743
        int no_filters_used = !cp->filter_params[FIR].order;
1744
        int average = 0;
1745
        int offset = 0;
1746
        int i;
1747
1748
        /* Determine extremes and average. */
1749
        for (i = 0; i < dp->blocksize; i++) {
1750
            int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
1751
            if (sample < min)
1752
                min = sample;
1753
            if (sample > max)
1754
                max = sample;
1755
            average += sample;
1756
            sample_buffer += ctx->num_channels;
1757
        }
1758
        average /= dp->blocksize;
1759
1760
        /* If filtering is used, we always set the offset to zero, otherwise
1761
         * we search for the offset that minimizes the bitcount. */
1762
        if (no_filters_used) {
1763
            no_codebook_bits(ctx, channel, min, max, &ctx->cur_best_offset[channel][0]);
1764
            offset = av_clip(average, HUFF_OFFSET_MIN, HUFF_OFFSET_MAX);
1765
        } else {
1766
            no_codebook_bits_offset(ctx, channel, offset, min, max, &ctx->cur_best_offset[channel][0]);
1767
        }
1768
1769
        for (i = 1; i < NUM_CODEBOOKS; i++) {
1770
            BestOffset temp_bo = { 0, INT_MAX, 0, 0, 0, };
1771
            int16_t offset_max;
1772
1773
            codebook_bits_offset(ctx, channel, i - 1,
1774
                                 min, max, offset,
1775
                                 &temp_bo);
1776
1777
            if (no_filters_used) {
1778
                offset_max = temp_bo.max;
1779
1780
                codebook_bits(ctx, channel, i - 1, temp_bo.min - 1,
1781
                            min, max, &temp_bo, 0);
1782
                codebook_bits(ctx, channel, i - 1, offset_max + 1,
1783
                            min, max, &temp_bo, 1);
1784
            }
1785
1786
            ctx->cur_best_offset[channel][i] = temp_bo;
1787
        }
1788
    }
1789
}
1790
1791
/****************************************************************************
1792
 *************** Functions that process the data in some way ****************
1793
 ****************************************************************************/
1794
1795
#define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
1796
#define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
1797
1798
#define MSB_MASK(bits)  (-(int)(1u << (bits)))
1799
1800
/** Applies the filter to the current samples, and saves the residual back
1801
 *  into the samples buffer. If the filter is too bad and overflows the
1802
 *  maximum amount of bits allowed (24), the samples buffer is left as is and
1803
 *  the function returns -1.
1804
 */
1805
static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
1806
{
1807
    FilterParams *fp[NUM_FILTERS] = { &ctx->cur_channel_params[channel].filter_params[FIR],
1808
                                      &ctx->cur_channel_params[channel].filter_params[IIR], };
1809
    int32_t *filter_state_buffer[NUM_FILTERS] = { NULL };
1810
    int32_t mask = MSB_MASK(ctx->cur_decoding_params->quant_step_size[channel]);
1811
    int32_t *sample_buffer = ctx->sample_buffer + channel;
1812
    unsigned int number_of_samples = ctx->number_of_samples;
1813
    unsigned int filter_shift = fp[FIR]->shift;
1814
    int filter;
1815
    int i, ret = 0;
1816
1817
    for (i = 0; i < NUM_FILTERS; i++) {
1818
        unsigned int size = ctx->number_of_samples;
1819
        filter_state_buffer[i] = av_malloc(size*sizeof(int32_t));
1820
        if (!filter_state_buffer[i]) {
1821
            av_log(ctx->avctx, AV_LOG_ERROR,
1822
                   "Not enough memory for applying filters.\n");
1823
            ret = AVERROR(ENOMEM);
1824
            goto free_and_return;
1825
        }
1826
    }
1827
1828
    for (i = 0; i < 8; i++) {
1829
        filter_state_buffer[FIR][i] = *sample_buffer;
1830
        filter_state_buffer[IIR][i] = *sample_buffer;
1831
1832
        sample_buffer += ctx->num_channels;
1833
    }
1834
1835
    for (i = 8; i < number_of_samples; i++) {
1836
        int32_t sample = *sample_buffer;
1837
        unsigned int order;
1838
        int64_t accum = 0;
1839
        int64_t residual;
1840
1841
        for (filter = 0; filter < NUM_FILTERS; filter++) {
1842
            int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
1843
            for (order = 0; order < fp[filter]->order; order++)
1844
                accum += (int64_t)filter_state_buffer[filter][i - 1 - order] *
1845
                         fcoeff[order];
1846
        }
1847
1848
        accum  >>= filter_shift;
1849
        residual = sample - (accum & mask);
1850
1851
        if (residual < SAMPLE_MIN(24) || residual > SAMPLE_MAX(24)) {
1852
            ret = AVERROR_INVALIDDATA;
1853
            goto free_and_return;
1854
        }
1855
1856
        filter_state_buffer[FIR][i] = sample;
1857
        filter_state_buffer[IIR][i] = (int32_t) residual;
1858
1859
        sample_buffer += ctx->num_channels;
1860
    }
1861
1862
    sample_buffer = ctx->sample_buffer + channel;
1863
    for (i = 0; i < number_of_samples; i++) {
1864
        *sample_buffer = filter_state_buffer[IIR][i];
1865
1866
        sample_buffer += ctx->num_channels;
1867
    }
1868
1869
free_and_return:
1870
    for (i = 0; i < NUM_FILTERS; i++) {
1871
        av_freep(&filter_state_buffer[i]);
1872
    }
1873
1874
    return ret;
1875
}
1876
1877
static void apply_filters(MLPEncodeContext *ctx)
1878
{
1879
    RestartHeader *rh = ctx->cur_restart_header;
1880
    int channel;
1881
1882
    for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
1883
        if (apply_filter(ctx, channel) < 0) {
1884
            /* Filter is horribly wrong.
1885
             * Clear filter params and update state. */
1886
            set_filter_params(ctx, channel, FIR, 1);
1887
            set_filter_params(ctx, channel, IIR, 1);
1888
            apply_filter(ctx, channel);
1889
        }
1890
    }
1891
}
1892
1893
/** Generates two noise channels worth of data. */
1894
static void generate_2_noise_channels(MLPEncodeContext *ctx)
1895
{
1896
    int32_t *sample_buffer = ctx->sample_buffer + ctx->num_channels - 2;
1897
    RestartHeader *rh = ctx->cur_restart_header;
1898
    unsigned int i;
1899
    uint32_t seed = rh->noisegen_seed;
1900
1901
    for (i = 0; i < ctx->number_of_samples; i++) {
1902
        uint16_t seed_shr7 = seed >> 7;
1903
        *sample_buffer++ = ((int8_t)(seed >> 15)) * (1 << rh->noise_shift);
1904
        *sample_buffer++ = ((int8_t) seed_shr7)   * (1 << rh->noise_shift);
1905
1906
        seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1907
1908
        sample_buffer += ctx->num_channels - 2;
1909
    }
1910
1911
    rh->noisegen_seed = seed & ((1 << 24)-1);
1912
}
1913
1914
/** Rematrixes all channels using chosen coefficients. */
1915
static void rematrix_channels(MLPEncodeContext *ctx)
1916
{
1917
    DecodingParams *dp = ctx->cur_decoding_params;
1918
    MatrixParams *mp = &dp->matrix_params;
1919
    int32_t *sample_buffer = ctx->sample_buffer;
1920
    unsigned int mat, i, maxchan;
1921
1922
    maxchan = ctx->num_channels;
1923
1924
    for (mat = 0; mat < mp->count; mat++) {
1925
        unsigned int msb_mask_bits = (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 8 : 0) - mp->shift[mat];
1926
        int32_t mask = MSB_MASK(msb_mask_bits);
1927
        unsigned int outch = mp->outch[mat];
1928
1929
        sample_buffer = ctx->sample_buffer;
1930
        for (i = 0; i < ctx->number_of_samples; i++) {
1931
            unsigned int src_ch;
1932
            int64_t accum = 0;
1933
1934
            for (src_ch = 0; src_ch < maxchan; src_ch++) {
1935
                int32_t sample = *(sample_buffer + src_ch);
1936
                accum += (int64_t) sample * mp->forco[mat][src_ch];
1937
            }
1938
            sample_buffer[outch] = (accum >> 14) & mask;
1939
1940
            sample_buffer += ctx->num_channels;
1941
        }
1942
    }
1943
}
1944
1945
/****************************************************************************
1946
 **** Functions that deal with determining the best parameters and output ***
1947
 ****************************************************************************/
1948
1949
typedef struct {
1950
    char    path[MAJOR_HEADER_INTERVAL + 3];
1951
    int     bitcount;
1952
} PathCounter;
1953
1954
static const char *path_counter_codebook[] = { "0", "1", "2", "3", };
1955
1956
#define ZERO_PATH               '0'
1957
#define CODEBOOK_CHANGE_BITS    21
1958
1959
static void clear_path_counter(PathCounter *path_counter)
1960
{
1961
    unsigned int i;
1962
1963
    for (i = 0; i < NUM_CODEBOOKS + 1; i++) {
1964
        path_counter[i].path[0]  = ZERO_PATH;
1965
        path_counter[i].path[1]  =      0x00;
1966
        path_counter[i].bitcount =         0;
1967
    }
1968
}
1969
1970
static int compare_best_offset(BestOffset *prev, BestOffset *cur)
1971
{
1972
    if (prev->lsb_bits != cur->lsb_bits)
1973
        return 1;
1974
1975
    return 0;
1976
}
1977
1978
static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,
1979
                                   PathCounter *src, int cur_codebook)
1980
{
1981
    BestOffset *cur_bo, *prev_bo = restart_best_offset;
1982
    int bitcount = src->bitcount;
1983
    char *path = src->path + 1;
1984
    int prev_codebook;
1985
    int i;
1986
1987
    for (i = 0; path[i]; i++)
1988
        prev_bo = ctx->best_offset[i][channel];
1989
1990
    prev_codebook = path[i - 1] - ZERO_PATH;
1991
1992
    cur_bo = ctx->best_offset[i][channel];
1993
1994
    bitcount += cur_bo[cur_codebook].bitcount;
1995
1996
    if (prev_codebook != cur_codebook ||
1997
        compare_best_offset(&prev_bo[prev_codebook], &cur_bo[cur_codebook]))
1998
        bitcount += CODEBOOK_CHANGE_BITS;
1999
2000
    return bitcount;
2001
}
2002
2003
static void set_best_codebook(MLPEncodeContext *ctx)
2004
{
2005
    DecodingParams *dp = ctx->cur_decoding_params;
2006
    RestartHeader *rh = ctx->cur_restart_header;
2007
    unsigned int channel;
2008
2009
    for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
2010
        BestOffset *cur_bo, *prev_bo = restart_best_offset;
2011
        PathCounter path_counter[NUM_CODEBOOKS + 1];
2012
        unsigned int best_codebook;
2013
        unsigned int index;
2014
        char *best_path;
2015
2016
        clear_path_counter(path_counter);
2017
2018
        for (index = 0; index < ctx->number_of_subblocks; index++) {
2019
            unsigned int best_bitcount = INT_MAX;
2020
            unsigned int codebook;
2021
2022
            cur_bo = ctx->best_offset[index][channel];
2023
2024
            for (codebook = 0; codebook < NUM_CODEBOOKS; codebook++) {
2025
                int prev_best_bitcount = INT_MAX;
2026
                int last_best;
2027
2028
                for (last_best = 0; last_best < 2; last_best++) {
2029
                    PathCounter *dst_path = &path_counter[codebook];
2030
                    PathCounter *src_path;
2031
                    int  temp_bitcount;
2032
2033
                    /* First test last path with same headers,
2034
                     * then with last best. */
2035
                    if (last_best) {
2036
                        src_path = &path_counter[NUM_CODEBOOKS];
2037
                    } else {
2038
                        if (compare_best_offset(&prev_bo[codebook], &cur_bo[codebook]))
2039
                            continue;
2040
                        else
2041
                            src_path = &path_counter[codebook];
2042
                    }
2043
2044
                    temp_bitcount = best_codebook_path_cost(ctx, channel, src_path, codebook);
2045
2046
                    if (temp_bitcount < best_bitcount) {
2047
                        best_bitcount = temp_bitcount;
2048
                        best_codebook = codebook;
2049
                    }
2050
2051
                    if (temp_bitcount < prev_best_bitcount) {
2052
                        prev_best_bitcount = temp_bitcount;
2053
                        if (src_path != dst_path)
2054
                            memcpy(dst_path, src_path, sizeof(PathCounter));
2055
                        av_strlcat(dst_path->path, path_counter_codebook[codebook], sizeof(dst_path->path));
2056
                        dst_path->bitcount = temp_bitcount;
2057
                    }
2058
                }
2059
            }
2060
2061
            prev_bo = cur_bo;
2062
2063
            memcpy(&path_counter[NUM_CODEBOOKS], &path_counter[best_codebook], sizeof(PathCounter));
2064
        }
2065
2066
        best_path = path_counter[NUM_CODEBOOKS].path + 1;
2067
2068
        /* Update context. */
2069
        for (index = 0; index < ctx->number_of_subblocks; index++) {
2070
            ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
2071
2072
            best_codebook = *best_path++ - ZERO_PATH;
2073
            cur_bo = &ctx->best_offset[index][channel][best_codebook];
2074
2075
            cp->huff_offset      = cur_bo->offset;
2076
            cp->huff_lsbs        = cur_bo->lsb_bits + dp->quant_step_size[channel];
2077
            cp->codebook         = best_codebook;
2078
        }
2079
    }
2080
}
2081
2082
/** Analyzes all collected bitcounts and selects the best parameters for each
2083
 *  individual access unit.
2084
 *  TODO This is just a stub!
2085
 */
2086
static void set_major_params(MLPEncodeContext *ctx)
2087
{
2088
    RestartHeader *rh = ctx->cur_restart_header;
2089
    unsigned int index;
2090
    unsigned int substr;
2091
    uint8_t max_huff_lsbs = 0;
2092
    uint8_t max_output_bits = 0;
2093
2094
    for (substr = 0; substr < ctx->num_substreams; substr++) {
2095
        DecodingParams *seq_dp = (DecodingParams *) ctx->decoding_params+
2096
                                 (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
2097
                                 (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
2098
2099
        ChannelParams *seq_cp = (ChannelParams *) ctx->channel_params +
2100
                                (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
2101
                                (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
2102
        unsigned int channel;
2103
        for (index = 0; index < ctx->seq_size[ctx->restart_intervals-1]; index++) {
2104
            memcpy(&ctx->major_decoding_params[index][substr], seq_dp + index*(ctx->num_substreams) + substr, sizeof(DecodingParams));
2105
            for (channel = 0; channel < ctx->avctx->channels; channel++) {
2106
                uint8_t huff_lsbs = (seq_cp + index*(ctx->avctx->channels) + channel)->huff_lsbs;
2107
                if (max_huff_lsbs < huff_lsbs)
2108
                    max_huff_lsbs = huff_lsbs;
2109
                memcpy(&ctx->major_channel_params[index][channel],
2110
                       (seq_cp + index*(ctx->avctx->channels) + channel),
2111
                       sizeof(ChannelParams));
2112
            }
2113
        }
2114
    }
2115
2116
    rh->max_huff_lsbs = max_huff_lsbs;
2117
2118
    for (index = 0; index < ctx->number_of_frames; index++)
2119
        if (max_output_bits < ctx->max_output_bits[index])
2120
            max_output_bits = ctx->max_output_bits[index];
2121
    rh->max_output_bits = max_output_bits;
2122
2123
    for (substr = 0; substr < ctx->num_substreams; substr++) {
2124
2125
        ctx->cur_restart_header = &ctx->restart_header[substr];
2126
2127
        ctx->prev_decoding_params = &restart_decoding_params[substr];
2128
        ctx->prev_channel_params = restart_channel_params;
2129
2130
        for (index = 0; index < MAJOR_HEADER_INTERVAL + 1; index++) {
2131
                ctx->cur_decoding_params = &ctx->major_decoding_params[index][substr];
2132
                ctx->cur_channel_params = ctx->major_channel_params[index];
2133
2134
                ctx->major_params_changed[index][substr] = compare_decoding_params(ctx);
2135
2136
                ctx->prev_decoding_params = ctx->cur_decoding_params;
2137
                ctx->prev_channel_params = ctx->cur_channel_params;
2138
        }
2139
    }
2140
2141
    ctx->major_number_of_subblocks = ctx->number_of_subblocks;
2142
    ctx->major_filter_state_subblock = 1;
2143
    ctx->major_cur_subblock_index = 0;
2144
}
2145
2146
static void analyze_sample_buffer(MLPEncodeContext *ctx)
2147
{
2148
    ChannelParams *seq_cp = ctx->seq_channel_params;
2149
    DecodingParams *seq_dp = ctx->seq_decoding_params;
2150
    unsigned int index;
2151
    unsigned int substr;
2152
2153
    for (substr = 0; substr < ctx->num_substreams; substr++) {
2154
2155
        ctx->cur_restart_header = &ctx->restart_header[substr];
2156
        ctx->cur_decoding_params = seq_dp + 1*(ctx->num_substreams) + substr;
2157
        ctx->cur_channel_params = seq_cp + 1*(ctx->avctx->channels);
2158
2159
        determine_quant_step_size(ctx);
2160
        generate_2_noise_channels(ctx);
2161
        lossless_matrix_coeffs   (ctx);
2162
        rematrix_channels        (ctx);
2163
        determine_filters        (ctx);
2164
        apply_filters            (ctx);
2165
2166
        copy_restart_frame_params(ctx, substr);
2167
2168
        /* Copy frame_size from frames 0...max to decoding_params 1...max + 1
2169
         * decoding_params[0] is for the filter state subblock.
2170
         */
2171
        for (index = 0; index < ctx->number_of_frames; index++) {
2172
            DecodingParams *dp = seq_dp + (index + 1)*(ctx->num_substreams) + substr;
2173
            dp->blocksize = ctx->frame_size[index];
2174
        }
2175
        /* The official encoder seems to always encode a filter state subblock
2176
         * even if there are no filters. TODO check if it is possible to skip
2177
         * the filter state subblock for no filters.
2178
         */
2179
        (seq_dp + substr)->blocksize  = 8;
2180
        (seq_dp + 1*(ctx->num_substreams) + substr)->blocksize -= 8;
2181
2182
        for (index = 0; index < ctx->number_of_subblocks; index++) {
2183
                ctx->cur_decoding_params = seq_dp + index*(ctx->num_substreams) + substr;
2184
                ctx->cur_channel_params = seq_cp + index*(ctx->avctx->channels);
2185
                ctx->cur_best_offset = ctx->best_offset[index];
2186
                determine_bits(ctx);
2187
                ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;
2188
        }
2189
2190
        set_best_codebook(ctx);
2191
    }
2192
}
2193
2194
static void process_major_frame(MLPEncodeContext *ctx)
2195
{
2196
    unsigned int substr;
2197
2198
    ctx->sample_buffer = ctx->major_inout_buffer;
2199
2200
    ctx->starting_frame_index = 0;
2201
    ctx->number_of_frames = ctx->major_number_of_frames;
2202
    ctx->number_of_samples = ctx->major_frame_size;
2203
2204
    for (substr = 0; substr < ctx->num_substreams; substr++) {
2205
        ctx->cur_restart_header = &ctx->restart_header[substr];
2206
2207
        ctx->cur_decoding_params = &ctx->major_decoding_params[1][substr];
2208
        ctx->cur_channel_params = ctx->major_channel_params[1];
2209
2210
        generate_2_noise_channels(ctx);
2211
        rematrix_channels        (ctx);
2212
2213
        apply_filters(ctx);
2214
    }
2215
}
2216
2217
/****************************************************************************/
2218
2219
static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
2220
                            const AVFrame *frame, int *got_packet)
2221
{
2222
    MLPEncodeContext *ctx = avctx->priv_data;
2223
    unsigned int bytes_written = 0;
2224
    int restart_frame, ret;
2225
    uint8_t *data;
2226
2227
    if ((ret = ff_alloc_packet2(avctx, avpkt, 87500 * avctx->channels, 0)) < 0)
2228
        return ret;
2229
2230
    /* add current frame to queue */
2231
    if ((ret = ff_af_queue_add(&ctx->afq, frame)) < 0)
2232
        return ret;
2233
2234
    data = frame->data[0];
2235
2236
    ctx->frame_index = avctx->frame_number % ctx->max_restart_interval;
2237
2238
    ctx->inout_buffer = ctx->major_inout_buffer
2239
                      + ctx->frame_index * ctx->one_sample_buffer_size;
2240
2241
    if (ctx->last_frame == ctx->inout_buffer) {
2242
        return 0;
2243
    }
2244
2245
    ctx->sample_buffer = ctx->major_scratch_buffer
2246
                       + ctx->frame_index * ctx->one_sample_buffer_size;
2247
2248
    ctx->write_buffer = ctx->inout_buffer;
2249
2250
    if (avctx->frame_number < ctx->max_restart_interval) {
2251
        if (data) {
2252
            goto input_and_return;
2253
        } else {
2254
            /* There are less frames than the requested major header interval.
2255
             * Update the context to reflect this.
2256
             */
2257
            ctx->max_restart_interval = avctx->frame_number;
2258
            ctx->frame_index = 0;
2259
2260
            ctx->sample_buffer = ctx->major_scratch_buffer;
2261
            ctx->inout_buffer = ctx->major_inout_buffer;
2262
        }
2263
    }
2264
2265
    if (ctx->frame_size[ctx->frame_index] > MAX_BLOCKSIZE) {
2266
        av_log(avctx, AV_LOG_ERROR, "Invalid frame size (%d > %d)\n",
2267
               ctx->frame_size[ctx->frame_index], MAX_BLOCKSIZE);
2268
        return AVERROR_INVALIDDATA;
2269
    }
2270
2271
    restart_frame = !ctx->frame_index;
2272
2273
    if (restart_frame) {
2274
        set_major_params(ctx);
2275
        if (ctx->min_restart_interval != ctx->max_restart_interval)
2276
            process_major_frame(ctx);
2277
    }
2278
2279
    if (ctx->min_restart_interval == ctx->max_restart_interval)
2280
        ctx->write_buffer = ctx->sample_buffer;
2281
2282
    bytes_written = write_access_unit(ctx, avpkt->data, avpkt->size, restart_frame);
2283
2284
    ctx->timestamp += ctx->frame_size[ctx->frame_index];
2285
    ctx->dts       += ctx->frame_size[ctx->frame_index];
2286
2287
input_and_return:
2288
2289
    if (data) {
2290
        ctx->frame_size[ctx->frame_index] = avctx->frame_size;
2291
        ctx->next_major_frame_size += avctx->frame_size;
2292
        ctx->next_major_number_of_frames++;
2293
        input_data(ctx, data);
2294
    } else if (!ctx->last_frame) {
2295
        ctx->last_frame = ctx->inout_buffer;
2296
    }
2297
2298
    restart_frame = (ctx->frame_index + 1) % ctx->min_restart_interval;
2299
2300
    if (!restart_frame) {
2301
        int seq_index;
2302
2303
        for (seq_index = 0;
2304
             seq_index < ctx->restart_intervals && (seq_index * ctx->min_restart_interval) <= ctx->avctx->frame_number;
2305
             seq_index++) {
2306
            unsigned int number_of_samples = 0;
2307
            unsigned int index;
2308
2309
            ctx->sample_buffer = ctx->major_scratch_buffer;
2310
            ctx->inout_buffer = ctx->major_inout_buffer;
2311
            ctx->seq_index = seq_index;
2312
2313
            ctx->starting_frame_index = (ctx->avctx->frame_number - (ctx->avctx->frame_number % ctx->min_restart_interval)
2314
                                      - (seq_index * ctx->min_restart_interval)) % ctx->max_restart_interval;
2315
            ctx->number_of_frames = ctx->next_major_number_of_frames;
2316
            ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;
2317
2318
            ctx->seq_channel_params = (ChannelParams *) ctx->channel_params +
2319
                                      (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->avctx->channels) +
2320
                                      (ctx->seq_offset[seq_index])*(ctx->avctx->channels);
2321
2322
            ctx->seq_decoding_params = (DecodingParams *) ctx->decoding_params +
2323
                                       (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->num_substreams) +
2324
                                       (ctx->seq_offset[seq_index])*(ctx->num_substreams);
2325
2326
            for (index = 0; index < ctx->number_of_frames; index++) {
2327
                number_of_samples += ctx->frame_size[(ctx->starting_frame_index + index) % ctx->max_restart_interval];
2328
            }
2329
            ctx->number_of_samples = number_of_samples;
2330
2331
            for (index = 0; index < ctx->seq_size[seq_index]; index++) {
2332
                clear_channel_params(ctx, ctx->seq_channel_params + index*(ctx->avctx->channels));
2333
                default_decoding_params(ctx, ctx->seq_decoding_params + index*(ctx->num_substreams));
2334
            }
2335
2336
            input_to_sample_buffer(ctx);
2337
2338
            analyze_sample_buffer(ctx);
2339
        }
2340
2341
        if (ctx->frame_index == (ctx->max_restart_interval - 1)) {
2342
            ctx->major_frame_size = ctx->next_major_frame_size;
2343
            ctx->next_major_frame_size = 0;
2344
            ctx->major_number_of_frames = ctx->next_major_number_of_frames;
2345
            ctx->next_major_number_of_frames = 0;
2346
2347
            if (!ctx->major_frame_size)
2348
                goto no_data_left;
2349
        }
2350
    }
2351
2352
no_data_left:
2353
2354
    ff_af_queue_remove(&ctx->afq, avctx->frame_size, &avpkt->pts,
2355
                       &avpkt->duration);
2356
    avpkt->size = bytes_written;
2357
    *got_packet = 1;
2358
    return 0;
2359
}
2360
2361
static av_cold int mlp_encode_close(AVCodecContext *avctx)
2362
{
2363
    MLPEncodeContext *ctx = avctx->priv_data;
2364
2365
    ff_lpc_end(&ctx->lpc_ctx);
2366
2367
    av_freep(&ctx->lossless_check_data);
2368
    av_freep(&ctx->major_scratch_buffer);
2369
    av_freep(&ctx->major_inout_buffer);
2370
    av_freep(&ctx->lpc_sample_buffer);
2371
    av_freep(&ctx->decoding_params);
2372
    av_freep(&ctx->channel_params);
2373
    av_freep(&ctx->frame_size);
2374
    av_freep(&ctx->max_output_bits);
2375
    ff_af_queue_close(&ctx->afq);
2376
2377
    return 0;
2378
}
2379
2380
#if CONFIG_MLP_ENCODER
2381
AVCodec ff_mlp_encoder = {
2382
    .name                   ="mlp",
2383
    .long_name              = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
2384
    .type                   = AVMEDIA_TYPE_AUDIO,
2385
    .id                     = AV_CODEC_ID_MLP,
2386
    .priv_data_size         = sizeof(MLPEncodeContext),
2387
    .init                   = mlp_encode_init,
2388
    .encode2                = mlp_encode_frame,
2389
    .close                  = mlp_encode_close,
2390
    .capabilities           = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_EXPERIMENTAL,
2391
    .sample_fmts            = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
2392
    .supported_samplerates  = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
2393
    .channel_layouts        = ff_mlp_channel_layouts,
2394
};
2395
#endif
2396
#if CONFIG_TRUEHD_ENCODER
2397
AVCodec ff_truehd_encoder = {
2398
    .name                   ="truehd",
2399
    .long_name              = NULL_IF_CONFIG_SMALL("TrueHD"),
2400
    .type                   = AVMEDIA_TYPE_AUDIO,
2401
    .id                     = AV_CODEC_ID_TRUEHD,
2402
    .priv_data_size         = sizeof(MLPEncodeContext),
2403
    .init                   = mlp_encode_init,
2404
    .encode2                = mlp_encode_frame,
2405
    .close                  = mlp_encode_close,
2406
    .capabilities           = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_EXPERIMENTAL,
2407
    .sample_fmts            = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
2408
    .supported_samplerates  = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
2409
    .channel_layouts        = (const uint64_t[]) {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT0_BACK, AV_CH_LAYOUT_5POINT1_BACK, 0},
2410
};
2411
#endif