GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/alacenc.c Lines: 292 330 88.5 %
Date: 2019-11-20 04:07:19 Branches: 139 166 83.7 %

Line Branch Exec Source
1
/*
2
 * ALAC audio encoder
3
 * Copyright (c) 2008  Jaikrishnan Menon <realityman@gmx.net>
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include "libavutil/opt.h"
23
24
#include "avcodec.h"
25
#include "put_bits.h"
26
#include "internal.h"
27
#include "lpc.h"
28
#include "mathops.h"
29
#include "alac_data.h"
30
31
#define DEFAULT_FRAME_SIZE        4096
32
#define ALAC_EXTRADATA_SIZE       36
33
#define ALAC_FRAME_HEADER_SIZE    55
34
#define ALAC_FRAME_FOOTER_SIZE    3
35
36
#define ALAC_ESCAPE_CODE          0x1FF
37
#define ALAC_MAX_LPC_ORDER        30
38
#define DEFAULT_MAX_PRED_ORDER    6
39
#define DEFAULT_MIN_PRED_ORDER    4
40
#define ALAC_MAX_LPC_PRECISION    9
41
#define ALAC_MIN_LPC_SHIFT        0
42
#define ALAC_MAX_LPC_SHIFT        9
43
44
#define ALAC_CHMODE_LEFT_RIGHT    0
45
#define ALAC_CHMODE_LEFT_SIDE     1
46
#define ALAC_CHMODE_RIGHT_SIDE    2
47
#define ALAC_CHMODE_MID_SIDE      3
48
49
typedef struct RiceContext {
50
    int history_mult;
51
    int initial_history;
52
    int k_modifier;
53
    int rice_modifier;
54
} RiceContext;
55
56
typedef struct AlacLPCContext {
57
    int lpc_order;
58
    int lpc_coeff[ALAC_MAX_LPC_ORDER+1];
59
    int lpc_quant;
60
} AlacLPCContext;
61
62
typedef struct AlacEncodeContext {
63
    const AVClass *class;
64
    AVCodecContext *avctx;
65
    int frame_size;                     /**< current frame size               */
66
    int verbatim;                       /**< current frame verbatim mode flag */
67
    int compression_level;
68
    int min_prediction_order;
69
    int max_prediction_order;
70
    int max_coded_frame_size;
71
    int write_sample_size;
72
    int extra_bits;
73
    int32_t sample_buf[2][DEFAULT_FRAME_SIZE];
74
    int32_t predictor_buf[2][DEFAULT_FRAME_SIZE];
75
    int interlacing_shift;
76
    int interlacing_leftweight;
77
    PutBitContext pbctx;
78
    RiceContext rc;
79
    AlacLPCContext lpc[2];
80
    LPCContext lpc_ctx;
81
} AlacEncodeContext;
82
83
84
1781
static void init_sample_buffers(AlacEncodeContext *s, int channels,
85
                                const uint8_t *samples[2])
86
{
87
    int ch, i;
88
1781
    int shift = av_get_bytes_per_sample(s->avctx->sample_fmt) * 8 -
89
1781
                s->avctx->bits_per_raw_sample;
90
91
#define COPY_SAMPLES(type) do {                             \
92
        for (ch = 0; ch < channels; ch++) {                 \
93
            int32_t       *bptr = s->sample_buf[ch];        \
94
            const type *sptr = (const type *)samples[ch];   \
95
            for (i = 0; i < s->frame_size; i++)             \
96
                bptr[i] = sptr[i] >> shift;                 \
97
        }                                                   \
98
    } while (0)
99
100
1781
    if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P)
101

11524221
        COPY_SAMPLES(int32_t);
102
    else
103

3044022
        COPY_SAMPLES(int16_t);
104
1781
}
105
106
14430194
static void encode_scalar(AlacEncodeContext *s, int x,
107
                          int k, int write_sample_size)
108
{
109
    int divisor, q, r;
110
111
14430194
    k = FFMIN(k, s->rc.k_modifier);
112
14430194
    divisor = (1<<k) - 1;
113
14430194
    q = x / divisor;
114
14430194
    r = x % divisor;
115
116
14430194
    if (q > 8) {
117
        // write escape code and sample value directly
118
25847
        put_bits(&s->pbctx, 9, ALAC_ESCAPE_CODE);
119
25847
        put_bits(&s->pbctx, write_sample_size, x);
120
    } else {
121
14404347
        if (q)
122
7865003
            put_bits(&s->pbctx, q, (1<<q) - 1);
123
14404347
        put_bits(&s->pbctx, 1, 0);
124
125
14404347
        if (k != 1) {
126
14401767
            if (r > 0)
127
12874329
                put_bits(&s->pbctx, k, r+1);
128
            else
129
1527438
                put_bits(&s->pbctx, k-1, 0);
130
        }
131
    }
132
14430194
}
133
134
2353
static void write_element_header(AlacEncodeContext *s,
135
                                 enum AlacRawDataBlockType element,
136
                                 int instance)
137
{
138
2353
    int encode_fs = 0;
139
140
2353
    if (s->frame_size < DEFAULT_FRAME_SIZE)
141
9
        encode_fs = 1;
142
143
2353
    put_bits(&s->pbctx, 3,  element);               // element type
144
2353
    put_bits(&s->pbctx, 4,  instance);              // element instance
145
2353
    put_bits(&s->pbctx, 12, 0);                     // unused header bits
146
2353
    put_bits(&s->pbctx, 1,  encode_fs);             // Sample count is in the header
147
2353
    put_bits(&s->pbctx, 2,  s->extra_bits >> 3);    // Extra bytes (for 24-bit)
148
2353
    put_bits(&s->pbctx, 1,  s->verbatim);           // Audio block is verbatim
149
2353
    if (encode_fs)
150
9
        put_bits32(&s->pbctx, s->frame_size);       // No. of samples in the frame
151
2353
}
152
153
3562
static void calc_predictor_params(AlacEncodeContext *s, int ch)
154
{
155
    int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
156
    int shift[MAX_LPC_ORDER];
157
    int opt_order;
158
159
3562
    if (s->compression_level == 1) {
160
1274
        s->lpc[ch].lpc_order = 6;
161
1274
        s->lpc[ch].lpc_quant = 6;
162
1274
        s->lpc[ch].lpc_coeff[0] =  160;
163
1274
        s->lpc[ch].lpc_coeff[1] = -190;
164
1274
        s->lpc[ch].lpc_coeff[2] =  170;
165
1274
        s->lpc[ch].lpc_coeff[3] = -130;
166
1274
        s->lpc[ch].lpc_coeff[4] =   80;
167
1274
        s->lpc[ch].lpc_coeff[5] =  -25;
168
    } else {
169
2288
        opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, s->sample_buf[ch],
170
                                      s->frame_size,
171
                                      s->min_prediction_order,
172
                                      s->max_prediction_order,
173
                                      ALAC_MAX_LPC_PRECISION, coefs, shift,
174
                                      FF_LPC_TYPE_LEVINSON, 0,
175
                                      ORDER_METHOD_EST, ALAC_MIN_LPC_SHIFT,
176
                                      ALAC_MAX_LPC_SHIFT, 1);
177
178
2288
        s->lpc[ch].lpc_order = opt_order;
179
2288
        s->lpc[ch].lpc_quant = shift[opt_order-1];
180
2288
        memcpy(s->lpc[ch].lpc_coeff, coefs[opt_order-1], opt_order*sizeof(int));
181
    }
182
3562
}
183
184
1781
static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
185
{
186
    int i, best;
187
    int32_t lt, rt;
188
    uint64_t sum[4];
189
    uint64_t score[4];
190
191
    /* calculate sum of 2nd order residual for each channel */
192
1781
    sum[0] = sum[1] = sum[2] = sum[3] = 0;
193
7279669
    for (i = 2; i < n; i++) {
194
7277888
        lt =  left_ch[i] - 2 *  left_ch[i - 1] +  left_ch[i - 2];
195
7277888
        rt = right_ch[i] - 2 * right_ch[i - 1] + right_ch[i - 2];
196
7277888
        sum[2] += FFABS((lt + rt) >> 1);
197
7277888
        sum[3] += FFABS(lt - rt);
198
7277888
        sum[0] += FFABS(lt);
199
7277888
        sum[1] += FFABS(rt);
200
    }
201
202
    /* calculate score for each mode */
203
1781
    score[0] = sum[0] + sum[1];
204
1781
    score[1] = sum[0] + sum[3];
205
1781
    score[2] = sum[1] + sum[3];
206
1781
    score[3] = sum[2] + sum[3];
207
208
    /* return mode with lowest score */
209
1781
    best = 0;
210
7124
    for (i = 1; i < 4; i++) {
211
5343
        if (score[i] < score[best])
212
597
            best = i;
213
    }
214
1781
    return best;
215
}
216
217
1781
static void alac_stereo_decorrelation(AlacEncodeContext *s)
218
{
219
1781
    int32_t *left = s->sample_buf[0], *right = s->sample_buf[1];
220
1781
    int i, mode, n = s->frame_size;
221
    int32_t tmp;
222
223
1781
    mode = estimate_stereo_mode(left, right, n);
224
225

1781
    switch (mode) {
226
1425
    case ALAC_CHMODE_LEFT_RIGHT:
227
1425
        s->interlacing_leftweight = 0;
228
1425
        s->interlacing_shift      = 0;
229
1425
        break;
230
145
    case ALAC_CHMODE_LEFT_SIDE:
231
592425
        for (i = 0; i < n; i++)
232
592280
            right[i] = left[i] - right[i];
233
145
        s->interlacing_leftweight = 1;
234
145
        s->interlacing_shift      = 0;
235
145
        break;
236
90
    case ALAC_CHMODE_RIGHT_SIDE:
237
368730
        for (i = 0; i < n; i++) {
238
368640
            tmp = right[i];
239
368640
            right[i] = left[i] - right[i];
240
368640
            left[i]  = tmp + (right[i] >> 31);
241
        }
242
90
        s->interlacing_leftweight = 1;
243
90
        s->interlacing_shift      = 31;
244
90
        break;
245
121
    default:
246
486923
        for (i = 0; i < n; i++) {
247
486802
            tmp = left[i];
248
486802
            left[i]  = (tmp + right[i]) >> 1;
249
486802
            right[i] =  tmp - right[i];
250
        }
251
121
        s->interlacing_leftweight = 1;
252
121
        s->interlacing_shift      = 1;
253
121
        break;
254
    }
255
1781
}
256
257
3562
static void alac_linear_predictor(AlacEncodeContext *s, int ch)
258
{
259
    int i;
260
3562
    AlacLPCContext lpc = s->lpc[ch];
261
3562
    int32_t *residual = s->predictor_buf[ch];
262
263
3562
    if (lpc.lpc_order == 31) {
264
        residual[0] = s->sample_buf[ch][0];
265
266
        for (i = 1; i < s->frame_size; i++) {
267
            residual[i] = s->sample_buf[ch][i    ] -
268
                          s->sample_buf[ch][i - 1];
269
        }
270
271
        return;
272
    }
273
274
    // generalised linear predictor
275
276
3562
    if (lpc.lpc_order > 0) {
277
3562
        int32_t *samples  = s->sample_buf[ch];
278
279
        // generate warm-up samples
280
3562
        residual[0] = samples[0];
281
34759
        for (i = 1; i <= lpc.lpc_order; i++)
282
31197
            residual[i] = sign_extend(samples[i] - samples[i-1], s->write_sample_size);
283
284
        // perform lpc on remaining samples
285
14531703
        for (i = lpc.lpc_order + 1; i < s->frame_size; i++) {
286
14528141
            int sum = 1 << (lpc.lpc_quant - 1), res_val, j;
287
288
141644105
            for (j = 0; j < lpc.lpc_order; j++) {
289
127115964
                sum += (samples[lpc.lpc_order-j] - samples[0]) *
290
127115964
                       lpc.lpc_coeff[j];
291
            }
292
293
14528141
            sum >>= lpc.lpc_quant;
294
14528141
            sum += samples[0];
295
14528141
            residual[i] = sign_extend(samples[lpc.lpc_order+1] - sum,
296
14528141
                                      s->write_sample_size);
297
14528141
            res_val = residual[i];
298
299
14528141
            if (res_val) {
300
13747876
                int index = lpc.lpc_order - 1;
301
13747876
                int neg = (res_val < 0);
302
303


74211438
                while (index >= 0 && (neg ? (res_val < 0) : (res_val > 0))) {
304
60463562
                    int val  = samples[0] - samples[lpc.lpc_order - index];
305

60463562
                    int sign = (val ? FFSIGN(val) : 0);
306
307
60463562
                    if (neg)
308
28132311
                        sign *= -1;
309
310
60463562
                    lpc.lpc_coeff[index] -= sign;
311
60463562
                    val *= sign;
312
60463562
                    res_val -= (val >> lpc.lpc_quant) * (lpc.lpc_order - index);
313
60463562
                    index--;
314
                }
315
            }
316
14528141
            samples++;
317
        }
318
    }
319
}
320
321
3562
static void alac_entropy_coder(AlacEncodeContext *s, int ch)
322
{
323
3562
    unsigned int history = s->rc.initial_history;
324
3562
    int sign_modifier = 0, i, k;
325
3562
    int32_t *samples = s->predictor_buf[ch];
326
327
14433625
    for (i = 0; i < s->frame_size;) {
328
        int x;
329
330
14430063
        k = av_log2((history >> 9) + 3);
331
332
14430063
        x  = -2 * (*samples) -1;
333
14430063
        x ^= x >> 31;
334
335
14430063
        samples++;
336
14430063
        i++;
337
338
14430063
        encode_scalar(s, x - sign_modifier, k, s->write_sample_size);
339
340
14430063
        history += x * s->rc.history_mult -
341
14430063
                   ((history * s->rc.history_mult) >> 9);
342
343
14430063
        sign_modifier = 0;
344
14430063
        if (x > 0xFFFF)
345
11363
            history = 0xFFFF;
346
347

14430063
        if (history < 128 && i < s->frame_size) {
348
131
            unsigned int block_size = 0;
349
350
131
            k = 7 - av_log2(history) + ((history + 16) >> 6);
351
352

132968
            while (*samples == 0 && i < s->frame_size) {
353
132837
                samples++;
354
132837
                i++;
355
132837
                block_size++;
356
            }
357
131
            encode_scalar(s, block_size, k, 16);
358
131
            sign_modifier = (block_size <= 0xFFFF);
359
131
            history = 0;
360
        }
361
362
    }
363
3562
}
364
365
2353
static void write_element(AlacEncodeContext *s,
366
                          enum AlacRawDataBlockType element, int instance,
367
                          const uint8_t *samples0, const uint8_t *samples1)
368
{
369
2353
    const uint8_t *samples[2] = { samples0, samples1 };
370
    int i, j, channels;
371
2353
    int prediction_type = 0;
372
2353
    PutBitContext *pb = &s->pbctx;
373
374
2353
    channels = element == TYPE_CPE ? 2 : 1;
375
376
2353
    if (s->verbatim) {
377
572
        write_element_header(s, element, instance);
378
        /* samples are channel-interleaved in verbatim mode */
379
572
        if (s->avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
380
469
            int shift = 32 - s->avctx->bits_per_raw_sample;
381
469
            const int32_t *samples_s32[2] = { (const int32_t *)samples0,
382
                                              (const int32_t *)samples1 };
383
1920469
            for (i = 0; i < s->frame_size; i++)
384
5760000
                for (j = 0; j < channels; j++)
385
3840000
                    put_sbits(pb, s->avctx->bits_per_raw_sample,
386
3840000
                              samples_s32[j][i] >> shift);
387
        } else {
388
103
            const int16_t *samples_s16[2] = { (const int16_t *)samples0,
389
                                              (const int16_t *)samples1 };
390
419053
            for (i = 0; i < s->frame_size; i++)
391
1256850
                for (j = 0; j < channels; j++)
392
837900
                    put_sbits(pb, s->avctx->bits_per_raw_sample,
393
837900
                              samples_s16[j][i]);
394
        }
395
    } else {
396
1781
        s->write_sample_size = s->avctx->bits_per_raw_sample - s->extra_bits +
397
1781
                               channels - 1;
398
399
1781
        init_sample_buffers(s, channels, samples);
400
1781
        write_element_header(s, element, instance);
401
402
        // extract extra bits if needed
403
1781
        if (s->extra_bits) {
404
1407
            uint32_t mask = (1 << s->extra_bits) - 1;
405
4221
            for (j = 0; j < channels; j++) {
406
2814
                int32_t *extra = s->predictor_buf[j];
407
2814
                int32_t *smp   = s->sample_buf[j];
408
11522814
                for (i = 0; i < s->frame_size; i++) {
409
11520000
                    extra[i] = smp[i] & mask;
410
11520000
                    smp[i] >>= s->extra_bits;
411
                }
412
            }
413
        }
414
415
1781
        if (channels == 2)
416
1781
            alac_stereo_decorrelation(s);
417
        else
418
            s->interlacing_shift = s->interlacing_leftweight = 0;
419
1781
        put_bits(pb, 8, s->interlacing_shift);
420
1781
        put_bits(pb, 8, s->interlacing_leftweight);
421
422
5343
        for (i = 0; i < channels; i++) {
423
3562
            calc_predictor_params(s, i);
424
425
3562
            put_bits(pb, 4, prediction_type);
426
3562
            put_bits(pb, 4, s->lpc[i].lpc_quant);
427
428
3562
            put_bits(pb, 3, s->rc.rice_modifier);
429
3562
            put_bits(pb, 5, s->lpc[i].lpc_order);
430
            // predictor coeff. table
431
34759
            for (j = 0; j < s->lpc[i].lpc_order; j++)
432
31197
                put_sbits(pb, 16, s->lpc[i].lpc_coeff[j]);
433
        }
434
435
        // write extra bits if needed
436
1781
        if (s->extra_bits) {
437
5761407
            for (i = 0; i < s->frame_size; i++) {
438
17280000
                for (j = 0; j < channels; j++) {
439
11520000
                    put_bits(pb, s->extra_bits, s->predictor_buf[j][i]);
440
                }
441
            }
442
        }
443
444
        // apply lpc and entropy coding to audio samples
445
5343
        for (i = 0; i < channels; i++) {
446
3562
            alac_linear_predictor(s, i);
447
448
            // TODO: determine when this will actually help. for now it's not used.
449
3562
            if (prediction_type == 15) {
450
                // 2nd pass 1st order filter
451
                int32_t *residual = s->predictor_buf[i];
452
                for (j = s->frame_size - 1; j > 0; j--)
453
                    residual[j] -= residual[j - 1];
454
            }
455
3562
            alac_entropy_coder(s, i);
456
        }
457
    }
458
2353
}
459
460
2353
static int write_frame(AlacEncodeContext *s, AVPacket *avpkt,
461
                       uint8_t * const *samples)
462
{
463
2353
    PutBitContext *pb = &s->pbctx;
464
2353
    const enum AlacRawDataBlockType *ch_elements = ff_alac_channel_elements[s->avctx->channels - 1];
465
2353
    const uint8_t *ch_map = ff_alac_channel_layout_offsets[s->avctx->channels - 1];
466
    int ch, element, sce, cpe;
467
468
2353
    init_put_bits(pb, avpkt->data, avpkt->size);
469
470
2353
    ch = element = sce = cpe = 0;
471
4706
    while (ch < s->avctx->channels) {
472
2353
        if (ch_elements[element] == TYPE_CPE) {
473
2353
            write_element(s, TYPE_CPE, cpe, samples[ch_map[ch]],
474
2353
                          samples[ch_map[ch + 1]]);
475
2353
            cpe++;
476
2353
            ch += 2;
477
        } else {
478
            write_element(s, TYPE_SCE, sce, samples[ch_map[ch]], NULL);
479
            sce++;
480
            ch++;
481
        }
482
2353
        element++;
483
    }
484
485
2353
    put_bits(pb, 3, TYPE_END);
486
2353
    flush_put_bits(pb);
487
488
2353
    return put_bits_count(pb) >> 3;
489
}
490
491
18
static av_always_inline int get_max_frame_size(int frame_size, int ch, int bps)
492
{
493
18
    int header_bits = 23 + 32 * (frame_size < DEFAULT_FRAME_SIZE);
494
18
    return FFALIGN(header_bits + bps * ch * frame_size + 3, 8) / 8;
495
}
496
497
9
static av_cold int alac_encode_close(AVCodecContext *avctx)
498
{
499
9
    AlacEncodeContext *s = avctx->priv_data;
500
9
    ff_lpc_end(&s->lpc_ctx);
501
9
    av_freep(&avctx->extradata);
502
9
    avctx->extradata_size = 0;
503
9
    return 0;
504
}
505
506
9
static av_cold int alac_encode_init(AVCodecContext *avctx)
507
{
508
9
    AlacEncodeContext *s = avctx->priv_data;
509
    int ret;
510
    uint8_t *alac_extradata;
511
512
9
    avctx->frame_size = s->frame_size = DEFAULT_FRAME_SIZE;
513
514
9
    if (avctx->sample_fmt == AV_SAMPLE_FMT_S32P) {
515
4
        if (avctx->bits_per_raw_sample != 24)
516
            av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
517
4
        avctx->bits_per_raw_sample = 24;
518
    } else {
519
5
        avctx->bits_per_raw_sample = 16;
520
5
        s->extra_bits              = 0;
521
    }
522
523
    // Set default compression level
524
9
    if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
525
2
        s->compression_level = 2;
526
    else
527
7
        s->compression_level = av_clip(avctx->compression_level, 0, 2);
528
529
    // Initialize default Rice parameters
530
9
    s->rc.history_mult    = 40;
531
9
    s->rc.initial_history = 10;
532
9
    s->rc.k_modifier      = 14;
533
9
    s->rc.rice_modifier   = 4;
534
535
9
    s->max_coded_frame_size = get_max_frame_size(avctx->frame_size,
536
                                                 avctx->channels,
537
                                                 avctx->bits_per_raw_sample);
538
539
9
    avctx->extradata = av_mallocz(ALAC_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
540
9
    if (!avctx->extradata) {
541
        ret = AVERROR(ENOMEM);
542
        goto error;
543
    }
544
9
    avctx->extradata_size = ALAC_EXTRADATA_SIZE;
545
546
9
    alac_extradata = avctx->extradata;
547
9
    AV_WB32(alac_extradata,    ALAC_EXTRADATA_SIZE);
548
9
    AV_WB32(alac_extradata+4,  MKBETAG('a','l','a','c'));
549
9
    AV_WB32(alac_extradata+12, avctx->frame_size);
550
9
    AV_WB8 (alac_extradata+17, avctx->bits_per_raw_sample);
551
9
    AV_WB8 (alac_extradata+21, avctx->channels);
552
9
    AV_WB32(alac_extradata+24, s->max_coded_frame_size);
553
9
    AV_WB32(alac_extradata+28,
554
            avctx->sample_rate * avctx->channels * avctx->bits_per_raw_sample); // average bitrate
555
9
    AV_WB32(alac_extradata+32, avctx->sample_rate);
556
557
    // Set relevant extradata fields
558
9
    if (s->compression_level > 0) {
559
7
        AV_WB8(alac_extradata+18, s->rc.history_mult);
560
7
        AV_WB8(alac_extradata+19, s->rc.initial_history);
561
7
        AV_WB8(alac_extradata+20, s->rc.k_modifier);
562
    }
563
564
#if FF_API_PRIVATE_OPT
565
FF_DISABLE_DEPRECATION_WARNINGS
566
9
    if (avctx->min_prediction_order >= 0) {
567
        if (avctx->min_prediction_order < MIN_LPC_ORDER ||
568
           avctx->min_prediction_order > ALAC_MAX_LPC_ORDER) {
569
            av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
570
                   avctx->min_prediction_order);
571
            ret = AVERROR(EINVAL);
572
            goto error;
573
        }
574
575
        s->min_prediction_order = avctx->min_prediction_order;
576
    }
577
578
9
    if (avctx->max_prediction_order >= 0) {
579
        if (avctx->max_prediction_order < MIN_LPC_ORDER ||
580
            avctx->max_prediction_order > ALAC_MAX_LPC_ORDER) {
581
            av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
582
                   avctx->max_prediction_order);
583
            ret = AVERROR(EINVAL);
584
            goto error;
585
        }
586
587
        s->max_prediction_order = avctx->max_prediction_order;
588
    }
589
FF_ENABLE_DEPRECATION_WARNINGS
590
#endif
591
592
9
    if (s->max_prediction_order < s->min_prediction_order) {
593
        av_log(avctx, AV_LOG_ERROR,
594
               "invalid prediction orders: min=%d max=%d\n",
595
               s->min_prediction_order, s->max_prediction_order);
596
        ret = AVERROR(EINVAL);
597
        goto error;
598
    }
599
600
9
    s->avctx = avctx;
601
602
9
    if ((ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
603
                           s->max_prediction_order,
604
                           FF_LPC_TYPE_LEVINSON)) < 0) {
605
        goto error;
606
    }
607
608
9
    return 0;
609
error:
610
    alac_encode_close(avctx);
611
    return ret;
612
}
613
614
2353
static int alac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
615
                             const AVFrame *frame, int *got_packet_ptr)
616
{
617
2353
    AlacEncodeContext *s = avctx->priv_data;
618
    int out_bytes, max_frame_size, ret;
619
620
2353
    s->frame_size = frame->nb_samples;
621
622
2353
    if (frame->nb_samples < DEFAULT_FRAME_SIZE)
623
9
        max_frame_size = get_max_frame_size(s->frame_size, avctx->channels,
624
                                            avctx->bits_per_raw_sample);
625
    else
626
2344
        max_frame_size = s->max_coded_frame_size;
627
628
2353
    if ((ret = ff_alloc_packet2(avctx, avpkt, 4 * max_frame_size, 0)) < 0)
629
        return ret;
630
631
    /* use verbatim mode for compression_level 0 */
632
2353
    if (s->compression_level) {
633
1781
        s->verbatim   = 0;
634
1781
        s->extra_bits = avctx->bits_per_raw_sample - 16;
635
    } else {
636
572
        s->verbatim   = 1;
637
572
        s->extra_bits = 0;
638
    }
639
640
2353
    out_bytes = write_frame(s, avpkt, frame->extended_data);
641
642
2353
    if (out_bytes > max_frame_size) {
643
        /* frame too large. use verbatim mode */
644
        s->verbatim = 1;
645
        s->extra_bits = 0;
646
        out_bytes = write_frame(s, avpkt, frame->extended_data);
647
    }
648
649
2353
    avpkt->size = out_bytes;
650
2353
    *got_packet_ptr = 1;
651
2353
    return 0;
652
}
653
654
#define OFFSET(x) offsetof(AlacEncodeContext, x)
655
#define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
656
static const AVOption options[] = {
657
    { "min_prediction_order", NULL, OFFSET(min_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MIN_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE },
658
    { "max_prediction_order", NULL, OFFSET(max_prediction_order), AV_OPT_TYPE_INT, { .i64 = DEFAULT_MAX_PRED_ORDER }, MIN_LPC_ORDER, ALAC_MAX_LPC_ORDER, AE },
659
660
    { NULL },
661
};
662
663
static const AVClass alacenc_class = {
664
    .class_name = "alacenc",
665
    .item_name  = av_default_item_name,
666
    .option     = options,
667
    .version    = LIBAVUTIL_VERSION_INT,
668
};
669
670
AVCodec ff_alac_encoder = {
671
    .name           = "alac",
672
    .long_name      = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
673
    .type           = AVMEDIA_TYPE_AUDIO,
674
    .id             = AV_CODEC_ID_ALAC,
675
    .priv_data_size = sizeof(AlacEncodeContext),
676
    .priv_class     = &alacenc_class,
677
    .init           = alac_encode_init,
678
    .encode2        = alac_encode_frame,
679
    .close          = alac_encode_close,
680
    .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME,
681
    .channel_layouts = ff_alac_channel_layouts,
682
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32P,
683
                                                     AV_SAMPLE_FMT_S16P,
684
                                                     AV_SAMPLE_FMT_NONE },
685
};