forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlpenc.c
2415 lines (1939 loc) · 82.8 KB
/
mlpenc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* MLP encoder
* Copyright (c) 2008 Ramiro Polla
* Copyright (c) 2016-2019 Jai Luthra
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "internal.h"
#include "put_bits.h"
#include "audio_frame_queue.h"
#include "libavutil/crc.h"
#include "libavutil/avstring.h"
#include "libavutil/samplefmt.h"
#include "mlp.h"
#include "lpc.h"
#define MAJOR_HEADER_INTERVAL 16
#define MLP_MIN_LPC_ORDER 1
#define MLP_MAX_LPC_ORDER 8
#define MLP_MIN_LPC_SHIFT 8
#define MLP_MAX_LPC_SHIFT 15
typedef struct {
uint8_t min_channel; ///< The index of the first channel coded in this substream.
uint8_t max_channel; ///< The index of the last channel coded in this substream.
uint8_t max_matrix_channel; ///< The number of channels input into the rematrix stage.
uint8_t noise_shift; ///< The left shift applied to random noise in 0x31ea substreams.
uint32_t noisegen_seed; ///< The current seed value for the pseudorandom noise generator(s).
int data_check_present; ///< Set if the substream contains extra info to check the size of VLC blocks.
int32_t lossless_check_data; ///< XOR of all output samples
uint8_t max_huff_lsbs; ///< largest huff_lsbs
uint8_t max_output_bits; ///< largest output bit-depth
} RestartHeader;
typedef struct {
uint8_t count; ///< number of matrices to apply
uint8_t outch[MAX_MATRICES]; ///< output channel for each matrix
int32_t forco[MAX_MATRICES][MAX_CHANNELS+2]; ///< forward coefficients
int32_t coeff[MAX_MATRICES][MAX_CHANNELS+2]; ///< decoding coefficients
uint8_t fbits[MAX_CHANNELS]; ///< fraction bits
int8_t shift[MAX_CHANNELS]; ///< Left shift to apply to decoded PCM values to get final 24-bit output.
} MatrixParams;
enum ParamFlags {
PARAMS_DEFAULT = 0xff,
PARAM_PRESENCE_FLAGS = 1 << 8,
PARAM_BLOCKSIZE = 1 << 7,
PARAM_MATRIX = 1 << 6,
PARAM_OUTSHIFT = 1 << 5,
PARAM_QUANTSTEP = 1 << 4,
PARAM_FIR = 1 << 3,
PARAM_IIR = 1 << 2,
PARAM_HUFFOFFSET = 1 << 1,
PARAM_PRESENT = 1 << 0,
};
typedef struct {
uint16_t blocksize; ///< number of PCM samples in current audio block
uint8_t quant_step_size[MAX_CHANNELS]; ///< left shift to apply to Huffman-decoded residuals
MatrixParams matrix_params;
uint8_t param_presence_flags; ///< Bitmask of which parameter sets are conveyed in a decoding parameter block.
} DecodingParams;
typedef struct BestOffset {
int32_t offset;
int bitcount;
int lsb_bits;
int32_t min;
int32_t max;
} BestOffset;
#define HUFF_OFFSET_MIN (-16384)
#define HUFF_OFFSET_MAX ( 16383)
/** Number of possible codebooks (counting "no codebooks") */
#define NUM_CODEBOOKS 4
typedef struct {
AVCodecContext *avctx;
int num_substreams; ///< Number of substreams contained within this stream.
int num_channels; /**< Number of channels in major_scratch_buffer.
* Normal channels + noise channels. */
int coded_sample_fmt [2]; ///< sample format encoded for MLP
int coded_sample_rate[2]; ///< sample rate encoded for MLP
int coded_peak_bitrate; ///< peak bitrate for this major sync header
int flags; ///< major sync info flags
/* channel_meaning */
int substream_info;
int fs;
int wordlength;
int channel_occupancy;
int summary_info;
int32_t *inout_buffer; ///< Pointer to data currently being read from lavc or written to bitstream.
int32_t *major_inout_buffer; ///< Buffer with all in/out data for one entire major frame interval.
int32_t *write_buffer; ///< Pointer to data currently being written to bitstream.
int32_t *sample_buffer; ///< Pointer to current access unit samples.
int32_t *major_scratch_buffer; ///< Scratch buffer big enough to fit all data for one entire major frame interval.
int32_t *last_frame; ///< Pointer to last frame with data to encode.
int32_t *lpc_sample_buffer;
unsigned int major_number_of_frames;
unsigned int next_major_number_of_frames;
unsigned int major_frame_size; ///< Number of samples in current major frame being encoded.
unsigned int next_major_frame_size; ///< Counter of number of samples for next major frame.
int32_t *lossless_check_data; ///< Array with lossless_check_data for each access unit.
unsigned int *max_output_bits; ///< largest output bit-depth
unsigned int *frame_size; ///< Array with number of samples/channel in each access unit.
unsigned int frame_index; ///< Index of current frame being encoded.
unsigned int one_sample_buffer_size; ///< Number of samples*channel for one access unit.
unsigned int max_restart_interval; ///< Max interval of access units in between two major frames.
unsigned int min_restart_interval; ///< Min interval of access units in between two major frames.
unsigned int restart_intervals; ///< Number of possible major frame sizes.
uint16_t timestamp; ///< Timestamp of current access unit.
uint16_t dts; ///< Decoding timestamp of current access unit.
uint8_t channel_arrangement; ///< channel arrangement for MLP streams
uint8_t ch_modifier_thd0; ///< channel modifier for TrueHD stream 0
uint8_t ch_modifier_thd1; ///< channel modifier for TrueHD stream 1
uint8_t ch_modifier_thd2; ///< channel modifier for TrueHD stream 2
unsigned int seq_size [MAJOR_HEADER_INTERVAL];
unsigned int seq_offset[MAJOR_HEADER_INTERVAL];
unsigned int sequence_size;
ChannelParams *channel_params;
BestOffset best_offset[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS][NUM_CODEBOOKS];
DecodingParams *decoding_params;
RestartHeader restart_header [MAX_SUBSTREAMS];
ChannelParams major_channel_params[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS]; ///< ChannelParams to be written to bitstream.
DecodingParams major_decoding_params[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS]; ///< DecodingParams to be written to bitstream.
int major_params_changed[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS]; ///< params_changed to be written to bitstream.
unsigned int major_cur_subblock_index;
unsigned int major_filter_state_subblock;
unsigned int major_number_of_subblocks;
BestOffset (*cur_best_offset)[NUM_CODEBOOKS];
ChannelParams *cur_channel_params;
DecodingParams *cur_decoding_params;
RestartHeader *cur_restart_header;
AudioFrameQueue afq;
/* Analysis stage. */
unsigned int starting_frame_index;
unsigned int number_of_frames;
unsigned int number_of_samples;
unsigned int number_of_subblocks;
unsigned int seq_index; ///< Sequence index for high compression levels.
ChannelParams *prev_channel_params;
DecodingParams *prev_decoding_params;
ChannelParams *seq_channel_params;
DecodingParams *seq_decoding_params;
unsigned int max_codebook_search;
LPCContext lpc_ctx;
} MLPEncodeContext;
static ChannelParams restart_channel_params[MAX_CHANNELS];
static DecodingParams restart_decoding_params[MAX_SUBSTREAMS];
static BestOffset restart_best_offset[NUM_CODEBOOKS] = {{0}};
#define SYNC_MAJOR 0xf8726f
#define MAJOR_SYNC_INFO_SIGNATURE 0xB752
#define SYNC_MLP 0xbb
#define SYNC_TRUEHD 0xba
/* must be set for DVD-A */
#define FLAGS_DVDA 0x4000
/* FIFO delay must be constant */
#define FLAGS_CONST 0x8000
#define SUBSTREAM_INFO_MAX_2_CHAN 0x01
#define SUBSTREAM_INFO_HIGH_RATE 0x02
#define SUBSTREAM_INFO_ALWAYS_SET 0x04
#define SUBSTREAM_INFO_2_SUBSTREAMS 0x08
/****************************************************************************
************ Functions that copy, clear, or compare parameters *************
****************************************************************************/
/** Compares two FilterParams structures and returns 1 if anything has
* changed. Returns 0 if they are both equal.
*/
static int compare_filter_params(const ChannelParams *prev_cp, const ChannelParams *cp, int filter)
{
const FilterParams *prev = &prev_cp->filter_params[filter];
const FilterParams *fp = &cp->filter_params[filter];
int i;
if (prev->order != fp->order)
return 1;
if (!prev->order)
return 0;
if (prev->shift != fp->shift)
return 1;
for (i = 0; i < fp->order; i++)
if (prev_cp->coeff[filter][i] != cp->coeff[filter][i])
return 1;
return 0;
}
/** Compare two primitive matrices and returns 1 if anything has changed.
* Returns 0 if they are both equal.
*/
static int compare_matrix_params(MLPEncodeContext *ctx, const MatrixParams *prev, const MatrixParams *mp)
{
RestartHeader *rh = ctx->cur_restart_header;
unsigned int channel, mat;
if (prev->count != mp->count)
return 1;
if (!prev->count)
return 0;
for (channel = rh->min_channel; channel <= rh->max_channel; channel++)
if (prev->fbits[channel] != mp->fbits[channel])
return 1;
for (mat = 0; mat < mp->count; mat++) {
if (prev->outch[mat] != mp->outch[mat])
return 1;
for (channel = 0; channel < ctx->num_channels; channel++)
if (prev->coeff[mat][channel] != mp->coeff[mat][channel])
return 1;
}
return 0;
}
/** Compares two DecodingParams and ChannelParams structures to decide if a
* new decoding params header has to be written.
*/
static int compare_decoding_params(MLPEncodeContext *ctx)
{
DecodingParams *prev = ctx->prev_decoding_params;
DecodingParams *dp = ctx->cur_decoding_params;
MatrixParams *prev_mp = &prev->matrix_params;
MatrixParams *mp = &dp->matrix_params;
RestartHeader *rh = ctx->cur_restart_header;
unsigned int ch;
int retval = 0;
if (prev->param_presence_flags != dp->param_presence_flags)
retval |= PARAM_PRESENCE_FLAGS;
if (prev->blocksize != dp->blocksize)
retval |= PARAM_BLOCKSIZE;
if (compare_matrix_params(ctx, prev_mp, mp))
retval |= PARAM_MATRIX;
for (ch = 0; ch <= rh->max_matrix_channel; ch++)
if (prev_mp->shift[ch] != mp->shift[ch]) {
retval |= PARAM_OUTSHIFT;
break;
}
for (ch = 0; ch <= rh->max_channel; ch++)
if (prev->quant_step_size[ch] != dp->quant_step_size[ch]) {
retval |= PARAM_QUANTSTEP;
break;
}
for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
ChannelParams *prev_cp = &ctx->prev_channel_params[ch];
ChannelParams *cp = &ctx->cur_channel_params[ch];
if (!(retval & PARAM_FIR) &&
compare_filter_params(prev_cp, cp, FIR))
retval |= PARAM_FIR;
if (!(retval & PARAM_IIR) &&
compare_filter_params(prev_cp, cp, IIR))
retval |= PARAM_IIR;
if (prev_cp->huff_offset != cp->huff_offset)
retval |= PARAM_HUFFOFFSET;
if (prev_cp->codebook != cp->codebook ||
prev_cp->huff_lsbs != cp->huff_lsbs )
retval |= 0x1;
}
return retval;
}
static void copy_filter_params(ChannelParams *dst_cp, ChannelParams *src_cp, int filter)
{
FilterParams *dst = &dst_cp->filter_params[filter];
FilterParams *src = &src_cp->filter_params[filter];
unsigned int order;
dst->order = src->order;
if (dst->order) {
dst->shift = src->shift;
dst->coeff_shift = src->coeff_shift;
dst->coeff_bits = src->coeff_bits;
}
for (order = 0; order < dst->order; order++)
dst_cp->coeff[filter][order] = src_cp->coeff[filter][order];
}
static void copy_matrix_params(MatrixParams *dst, MatrixParams *src)
{
dst->count = src->count;
if (dst->count) {
unsigned int channel, count;
for (channel = 0; channel < MAX_CHANNELS; channel++) {
dst->fbits[channel] = src->fbits[channel];
dst->shift[channel] = src->shift[channel];
for (count = 0; count < MAX_MATRICES; count++)
dst->coeff[count][channel] = src->coeff[count][channel];
}
for (count = 0; count < MAX_MATRICES; count++)
dst->outch[count] = src->outch[count];
}
}
static void copy_restart_frame_params(MLPEncodeContext *ctx,
unsigned int substr)
{
unsigned int index;
for (index = 0; index < ctx->number_of_subblocks; index++) {
DecodingParams *dp = ctx->seq_decoding_params + index*(ctx->num_substreams) + substr;
unsigned int channel;
copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);
for (channel = 0; channel < ctx->avctx->channels; channel++) {
ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
unsigned int filter;
dp->quant_step_size[channel] = ctx->cur_decoding_params->quant_step_size[channel];
dp->matrix_params.shift[channel] = ctx->cur_decoding_params->matrix_params.shift[channel];
if (index)
for (filter = 0; filter < NUM_FILTERS; filter++)
copy_filter_params(cp, &ctx->cur_channel_params[channel], filter);
}
}
}
/** Clears a DecodingParams struct the way it should be after a restart header. */
static void clear_decoding_params(MLPEncodeContext *ctx, DecodingParams decoding_params[MAX_SUBSTREAMS])
{
unsigned int substr;
for (substr = 0; substr < ctx->num_substreams; substr++) {
DecodingParams *dp = &decoding_params[substr];
dp->param_presence_flags = 0xff;
dp->blocksize = 8;
memset(&dp->matrix_params , 0, sizeof(MatrixParams ));
memset(dp->quant_step_size, 0, sizeof(dp->quant_step_size));
}
}
/** Clears a ChannelParams struct the way it should be after a restart header. */
static void clear_channel_params(MLPEncodeContext *ctx, ChannelParams channel_params[MAX_CHANNELS])
{
unsigned int channel;
for (channel = 0; channel < ctx->avctx->channels; channel++) {
ChannelParams *cp = &channel_params[channel];
memset(&cp->filter_params, 0, sizeof(cp->filter_params));
/* Default audio coding is 24-bit raw PCM. */
cp->huff_offset = 0;
cp->codebook = 0;
cp->huff_lsbs = 24;
}
}
/** Sets default vales in our encoder for a DecodingParams struct. */
static void default_decoding_params(MLPEncodeContext *ctx,
DecodingParams decoding_params[MAX_SUBSTREAMS])
{
unsigned int substr;
clear_decoding_params(ctx, decoding_params);
for (substr = 0; substr < ctx->num_substreams; substr++) {
DecodingParams *dp = &decoding_params[substr];
uint8_t param_presence_flags = 0;
param_presence_flags |= PARAM_BLOCKSIZE;
param_presence_flags |= PARAM_MATRIX;
param_presence_flags |= PARAM_OUTSHIFT;
param_presence_flags |= PARAM_QUANTSTEP;
param_presence_flags |= PARAM_FIR;
/* param_presence_flags |= PARAM_IIR; */
param_presence_flags |= PARAM_HUFFOFFSET;
param_presence_flags |= PARAM_PRESENT;
dp->param_presence_flags = param_presence_flags;
}
}
/****************************************************************************/
/** Calculates the smallest number of bits it takes to encode a given signed
* value in two's complement.
*/
static int inline number_sbits(int number)
{
if (number < -1)
number++;
return av_log2(FFABS(number)) + 1 + !!number;
}
enum InputBitDepth {
BITS_16,
BITS_20,
BITS_24,
};
static int mlp_peak_bitrate(int peak_bitrate, int sample_rate)
{
return ((peak_bitrate << 4) - 8) / sample_rate;
}
static av_cold int mlp_encode_init(AVCodecContext *avctx)
{
MLPEncodeContext *ctx = avctx->priv_data;
unsigned int substr, index;
unsigned int sum = 0;
unsigned int size;
int ret;
ctx->avctx = avctx;
switch (avctx->sample_rate) {
case 44100 << 0:
avctx->frame_size = 40 << 0;
ctx->coded_sample_rate[0] = 0x08 + 0;
ctx->fs = 0x08 + 1;
break;
case 44100 << 1:
avctx->frame_size = 40 << 1;
ctx->coded_sample_rate[0] = 0x08 + 1;
ctx->fs = 0x0C + 1;
break;
case 44100 << 2:
ctx->substream_info |= SUBSTREAM_INFO_HIGH_RATE;
avctx->frame_size = 40 << 2;
ctx->coded_sample_rate[0] = 0x08 + 2;
ctx->fs = 0x10 + 1;
break;
case 48000 << 0:
avctx->frame_size = 40 << 0;
ctx->coded_sample_rate[0] = 0x00 + 0;
ctx->fs = 0x08 + 2;
break;
case 48000 << 1:
avctx->frame_size = 40 << 1;
ctx->coded_sample_rate[0] = 0x00 + 1;
ctx->fs = 0x0C + 2;
break;
case 48000 << 2:
ctx->substream_info |= SUBSTREAM_INFO_HIGH_RATE;
avctx->frame_size = 40 << 2;
ctx->coded_sample_rate[0] = 0x00 + 2;
ctx->fs = 0x10 + 2;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d. Supported "
"sample rates are 44100, 88200, 176400, 48000, "
"96000, and 192000.\n", avctx->sample_rate);
return -1;
}
ctx->coded_sample_rate[1] = -1 & 0xf;
/* TODO Keep count of bitrate and calculate real value. */
ctx->coded_peak_bitrate = mlp_peak_bitrate(9600000, avctx->sample_rate);
/* TODO support more channels. */
if (avctx->channels > 2) {
av_log(avctx, AV_LOG_WARNING,
"Only mono and stereo are supported at the moment.\n");
}
ctx->substream_info |= SUBSTREAM_INFO_ALWAYS_SET;
if (avctx->channels <= 2) {
ctx->substream_info |= SUBSTREAM_INFO_MAX_2_CHAN;
}
switch (avctx->sample_fmt) {
case AV_SAMPLE_FMT_S16:
ctx->coded_sample_fmt[0] = BITS_16;
ctx->wordlength = 16;
avctx->bits_per_raw_sample = 16;
break;
/* TODO 20 bits: */
case AV_SAMPLE_FMT_S32:
ctx->coded_sample_fmt[0] = BITS_24;
ctx->wordlength = 24;
avctx->bits_per_raw_sample = 24;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Sample format not supported. "
"Only 16- and 24-bit samples are supported.\n");
return -1;
}
ctx->coded_sample_fmt[1] = -1 & 0xf;
ctx->dts = -avctx->frame_size;
ctx->num_channels = avctx->channels + 2; /* +2 noise channels */
ctx->one_sample_buffer_size = avctx->frame_size
* ctx->num_channels;
/* TODO Let user pass major header interval as parameter. */
ctx->max_restart_interval = MAJOR_HEADER_INTERVAL;
ctx->max_codebook_search = 3;
ctx->min_restart_interval = MAJOR_HEADER_INTERVAL;
ctx->restart_intervals = ctx->max_restart_interval / ctx->min_restart_interval;
/* TODO Let user pass parameters for LPC filter. */
size = avctx->frame_size * ctx->max_restart_interval;
ctx->lpc_sample_buffer = av_malloc_array(size, sizeof(int32_t));
if (!ctx->lpc_sample_buffer) {
av_log(avctx, AV_LOG_ERROR,
"Not enough memory for buffering samples.\n");
return AVERROR(ENOMEM);
}
size = ctx->one_sample_buffer_size * ctx->max_restart_interval;
ctx->major_scratch_buffer = av_malloc_array(size, sizeof(int32_t));
if (!ctx->major_scratch_buffer) {
av_log(avctx, AV_LOG_ERROR,
"Not enough memory for buffering samples.\n");
return AVERROR(ENOMEM);
}
ctx->major_inout_buffer = av_malloc_array(size, sizeof(int32_t));
if (!ctx->major_inout_buffer) {
av_log(avctx, AV_LOG_ERROR,
"Not enough memory for buffering samples.\n");
return AVERROR(ENOMEM);
}
ff_mlp_init_crc();
ctx->num_substreams = 1; // TODO: change this after adding multi-channel support for TrueHD
if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
/* MLP */
switch(avctx->channel_layout) {
case AV_CH_LAYOUT_MONO:
ctx->channel_arrangement = 0; break;
case AV_CH_LAYOUT_STEREO:
ctx->channel_arrangement = 1; break;
case AV_CH_LAYOUT_2_1:
ctx->channel_arrangement = 2; break;
case AV_CH_LAYOUT_QUAD:
ctx->channel_arrangement = 3; break;
case AV_CH_LAYOUT_2POINT1:
ctx->channel_arrangement = 4; break;
case AV_CH_LAYOUT_SURROUND:
ctx->channel_arrangement = 7; break;
case AV_CH_LAYOUT_4POINT0:
ctx->channel_arrangement = 8; break;
case AV_CH_LAYOUT_5POINT0_BACK:
ctx->channel_arrangement = 9; break;
case AV_CH_LAYOUT_3POINT1:
ctx->channel_arrangement = 10; break;
case AV_CH_LAYOUT_4POINT1:
ctx->channel_arrangement = 11; break;
case AV_CH_LAYOUT_5POINT1_BACK:
ctx->channel_arrangement = 12; break;
default:
av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
return -1;
}
ctx->flags = FLAGS_DVDA;
ctx->channel_occupancy = ff_mlp_ch_info[ctx->channel_arrangement].channel_occupancy;
ctx->summary_info = ff_mlp_ch_info[ctx->channel_arrangement].summary_info ;
} else {
/* TrueHD */
switch(avctx->channel_layout) {
case AV_CH_LAYOUT_STEREO:
ctx->ch_modifier_thd0 = 0;
ctx->ch_modifier_thd1 = 0;
ctx->ch_modifier_thd2 = 0;
ctx->channel_arrangement = 1;
break;
case AV_CH_LAYOUT_5POINT0_BACK:
ctx->ch_modifier_thd0 = 1;
ctx->ch_modifier_thd1 = 1;
ctx->ch_modifier_thd2 = 1;
ctx->channel_arrangement = 11;
break;
case AV_CH_LAYOUT_5POINT1_BACK:
ctx->ch_modifier_thd0 = 2;
ctx->ch_modifier_thd1 = 1;
ctx->ch_modifier_thd2 = 2;
ctx->channel_arrangement = 15;
break;
default:
av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
return -1;
}
ctx->flags = 0;
ctx->channel_occupancy = 0;
ctx->summary_info = 0;
}
size = sizeof(unsigned int) * ctx->max_restart_interval;
ctx->frame_size = av_malloc(size);
if (!ctx->frame_size)
return AVERROR(ENOMEM);
ctx->max_output_bits = av_malloc(size);
if (!ctx->max_output_bits)
return AVERROR(ENOMEM);
size = sizeof(int32_t)
* ctx->num_substreams * ctx->max_restart_interval;
ctx->lossless_check_data = av_malloc(size);
if (!ctx->lossless_check_data)
return AVERROR(ENOMEM);
for (index = 0; index < ctx->restart_intervals; index++) {
ctx->seq_offset[index] = sum;
ctx->seq_size [index] = ((index + 1) * ctx->min_restart_interval) + 1;
sum += ctx->seq_size[index];
}
ctx->sequence_size = sum;
size = sizeof(ChannelParams)
* ctx->restart_intervals * ctx->sequence_size * ctx->avctx->channels;
ctx->channel_params = av_malloc(size);
if (!ctx->channel_params) {
av_log(avctx, AV_LOG_ERROR,
"Not enough memory for analysis context.\n");
return AVERROR(ENOMEM);
}
size = sizeof(DecodingParams)
* ctx->restart_intervals * ctx->sequence_size * ctx->num_substreams;
ctx->decoding_params = av_malloc(size);
if (!ctx->decoding_params) {
av_log(avctx, AV_LOG_ERROR,
"Not enough memory for analysis context.\n");
return AVERROR(ENOMEM);
}
for (substr = 0; substr < ctx->num_substreams; substr++) {
RestartHeader *rh = &ctx->restart_header [substr];
/* TODO see if noisegen_seed is really worth it. */
rh->noisegen_seed = 0;
rh->min_channel = 0;
rh->max_channel = avctx->channels - 1;
/* FIXME: this works for 1 and 2 channels, but check for more */
rh->max_matrix_channel = rh->max_channel;
}
clear_channel_params(ctx, restart_channel_params);
clear_decoding_params(ctx, restart_decoding_params);
if ((ret = ff_lpc_init(&ctx->lpc_ctx, ctx->number_of_samples,
MLP_MAX_LPC_ORDER, FF_LPC_TYPE_LEVINSON)) < 0) {
av_log(avctx, AV_LOG_ERROR,
"Not enough memory for LPC context.\n");
return ret;
}
ff_af_queue_init(avctx, &ctx->afq);
return 0;
}
/****************************************************************************
****************** Functions that write to the bitstream *******************
****************************************************************************/
/** Writes a major sync header to the bitstream. */
static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
{
PutBitContext pb;
init_put_bits(&pb, buf, buf_size);
put_bits(&pb, 24, SYNC_MAJOR );
if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
put_bits(&pb, 8, SYNC_MLP );
put_bits(&pb, 4, ctx->coded_sample_fmt [0]);
put_bits(&pb, 4, ctx->coded_sample_fmt [1]);
put_bits(&pb, 4, ctx->coded_sample_rate[0]);
put_bits(&pb, 4, ctx->coded_sample_rate[1]);
put_bits(&pb, 4, 0 ); /* ignored */
put_bits(&pb, 4, 0 ); /* multi_channel_type */
put_bits(&pb, 3, 0 ); /* ignored */
put_bits(&pb, 5, ctx->channel_arrangement );
} else if (ctx->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
put_bits(&pb, 8, SYNC_TRUEHD );
put_bits(&pb, 4, ctx->coded_sample_rate[0]);
put_bits(&pb, 4, 0 ); /* ignored */
put_bits(&pb, 2, ctx->ch_modifier_thd0 );
put_bits(&pb, 2, ctx->ch_modifier_thd1 );
put_bits(&pb, 5, ctx->channel_arrangement );
put_bits(&pb, 2, ctx->ch_modifier_thd2 );
put_bits(&pb, 13, ctx->channel_arrangement );
}
put_bits(&pb, 16, MAJOR_SYNC_INFO_SIGNATURE);
put_bits(&pb, 16, ctx->flags );
put_bits(&pb, 16, 0 ); /* ignored */
put_bits(&pb, 1, 1 ); /* is_vbr */
put_bits(&pb, 15, ctx->coded_peak_bitrate );
put_bits(&pb, 4, 1 ); /* num_substreams */
put_bits(&pb, 4, 0x1 ); /* ignored */
/* channel_meaning */
put_bits(&pb, 8, ctx->substream_info );
put_bits(&pb, 5, ctx->fs );
put_bits(&pb, 5, ctx->wordlength );
put_bits(&pb, 6, ctx->channel_occupancy );
put_bits(&pb, 3, 0 ); /* ignored */
put_bits(&pb, 10, 0 ); /* speaker_layout */
put_bits(&pb, 3, 0 ); /* copy_protection */
put_bits(&pb, 16, 0x8080 ); /* ignored */
put_bits(&pb, 7, 0 ); /* ignored */
put_bits(&pb, 4, 0 ); /* source_format */
put_bits(&pb, 5, ctx->summary_info );
flush_put_bits(&pb);
AV_WL16(buf+26, ff_mlp_checksum16(buf, 26));
}
/** Writes a restart header to the bitstream. Damaged streams can start being
* decoded losslessly again after such a header and the subsequent decoding
* params header.
*/
static void write_restart_header(MLPEncodeContext *ctx, PutBitContext *pb)
{
RestartHeader *rh = ctx->cur_restart_header;
uint8_t lossless_check = xor_32_to_8(rh->lossless_check_data);
unsigned int start_count = put_bits_count(pb);
PutBitContext tmpb;
uint8_t checksum;
unsigned int ch;
put_bits(pb, 14, 0x31ea ); /* TODO 0x31eb */
put_bits(pb, 16, ctx->timestamp );
put_bits(pb, 4, rh->min_channel );
put_bits(pb, 4, rh->max_channel );
put_bits(pb, 4, rh->max_matrix_channel);
put_bits(pb, 4, rh->noise_shift );
put_bits(pb, 23, rh->noisegen_seed );
put_bits(pb, 4, 0 ); /* TODO max_shift */
put_bits(pb, 5, rh->max_huff_lsbs );
put_bits(pb, 5, rh->max_output_bits );
put_bits(pb, 5, rh->max_output_bits );
put_bits(pb, 1, rh->data_check_present);
put_bits(pb, 8, lossless_check );
put_bits(pb, 16, 0 ); /* ignored */
for (ch = 0; ch <= rh->max_matrix_channel; ch++)
put_bits(pb, 6, ch);
/* Data must be flushed for the checksum to be correct. */
tmpb = *pb;
flush_put_bits(&tmpb);
checksum = ff_mlp_restart_checksum(pb->buf, put_bits_count(pb) - start_count);
put_bits(pb, 8, checksum);
}
/** Writes matrix params for all primitive matrices to the bitstream. */
static void write_matrix_params(MLPEncodeContext *ctx, PutBitContext *pb)
{
DecodingParams *dp = ctx->cur_decoding_params;
MatrixParams *mp = &dp->matrix_params;
unsigned int mat;
put_bits(pb, 4, mp->count);
for (mat = 0; mat < mp->count; mat++) {
unsigned int channel;
put_bits(pb, 4, mp->outch[mat]); /* matrix_out_ch */
put_bits(pb, 4, mp->fbits[mat]);
put_bits(pb, 1, 0 ); /* lsb_bypass */
for (channel = 0; channel < ctx->num_channels; channel++) {
int32_t coeff = mp->coeff[mat][channel];
if (coeff) {
put_bits(pb, 1, 1);
coeff >>= 14 - mp->fbits[mat];
put_sbits(pb, mp->fbits[mat] + 2, coeff);
} else {
put_bits(pb, 1, 0);
}
}
}
}
/** Writes filter parameters for one filter to the bitstream. */
static void write_filter_params(MLPEncodeContext *ctx, PutBitContext *pb,
unsigned int channel, unsigned int filter)
{
FilterParams *fp = &ctx->cur_channel_params[channel].filter_params[filter];
put_bits(pb, 4, fp->order);
if (fp->order > 0) {
int i;
int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
put_bits(pb, 4, fp->shift );
put_bits(pb, 5, fp->coeff_bits );
put_bits(pb, 3, fp->coeff_shift);
for (i = 0; i < fp->order; i++) {
put_sbits(pb, fp->coeff_bits, fcoeff[i] >> fp->coeff_shift);
}
/* TODO state data for IIR filter. */
put_bits(pb, 1, 0);
}
}
/** Writes decoding parameters to the bitstream. These change very often,
* usually at almost every frame.
*/
static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb,
int params_changed)
{
DecodingParams *dp = ctx->cur_decoding_params;
RestartHeader *rh = ctx->cur_restart_header;
MatrixParams *mp = &dp->matrix_params;
unsigned int ch;
if (dp->param_presence_flags != PARAMS_DEFAULT &&
params_changed & PARAM_PRESENCE_FLAGS) {
put_bits(pb, 1, 1);
put_bits(pb, 8, dp->param_presence_flags);
} else {
put_bits(pb, 1, 0);
}
if (dp->param_presence_flags & PARAM_BLOCKSIZE) {
if (params_changed & PARAM_BLOCKSIZE) {
put_bits(pb, 1, 1);
put_bits(pb, 9, dp->blocksize);
} else {
put_bits(pb, 1, 0);
}
}
if (dp->param_presence_flags & PARAM_MATRIX) {
if (params_changed & PARAM_MATRIX) {
put_bits(pb, 1, 1);
write_matrix_params(ctx, pb);
} else {
put_bits(pb, 1, 0);
}
}
if (dp->param_presence_flags & PARAM_OUTSHIFT) {
if (params_changed & PARAM_OUTSHIFT) {
put_bits(pb, 1, 1);
for (ch = 0; ch <= rh->max_matrix_channel; ch++)
put_sbits(pb, 4, mp->shift[ch]);
} else {
put_bits(pb, 1, 0);
}
}
if (dp->param_presence_flags & PARAM_QUANTSTEP) {
if (params_changed & PARAM_QUANTSTEP) {
put_bits(pb, 1, 1);
for (ch = 0; ch <= rh->max_channel; ch++)
put_bits(pb, 4, dp->quant_step_size[ch]);
} else {
put_bits(pb, 1, 0);
}
}
for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
ChannelParams *cp = &ctx->cur_channel_params[ch];
if (dp->param_presence_flags & 0xF) {
put_bits(pb, 1, 1);
if (dp->param_presence_flags & PARAM_FIR) {
if (params_changed & PARAM_FIR) {
put_bits(pb, 1, 1);
write_filter_params(ctx, pb, ch, FIR);
} else {
put_bits(pb, 1, 0);
}
}
if (dp->param_presence_flags & PARAM_IIR) {
if (params_changed & PARAM_IIR) {
put_bits(pb, 1, 1);
write_filter_params(ctx, pb, ch, IIR);
} else {
put_bits(pb, 1, 0);
}
}
if (dp->param_presence_flags & PARAM_HUFFOFFSET) {
if (params_changed & PARAM_HUFFOFFSET) {
put_bits (pb, 1, 1);
put_sbits(pb, 15, cp->huff_offset);
} else {
put_bits(pb, 1, 0);
}
}
if (cp->codebook > 0 && cp->huff_lsbs > 24) {
av_log(ctx->avctx, AV_LOG_ERROR, "Invalid Huff LSBs\n");
}
put_bits(pb, 2, cp->codebook );
put_bits(pb, 5, cp->huff_lsbs);
} else {
put_bits(pb, 1, 0);
}
}
}