1 /*
2 * MLP decoder
3 * Copyright (c) 2007-2008 Ian Caulfield
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 /**
23 * @file
24 * MLP decoder
25 */
26
27 #include <stdint.h>
28
40 #include "config.h"
41
42 /** number of bits used for VLC lookup - longest Huffman code is 9 */
43 #if ARCH_ARM
44 #define VLC_BITS 5
45 #define VLC_STATIC_SIZE 64
46 #else
48 #define VLC_STATIC_SIZE 512
49 #endif
50
52 /// Set if a valid restart header has been read. Otherwise the substream cannot be decoded.
54
55 //@{
56 /** restart header data */
57 /// The type of noise to be used in the rematrix stage.
59
60 /// The index of the first channel coded in this substream.
62 /// The index of the last channel coded in this substream.
64 /// The number of channels input into the rematrix stage.
66 /// For each channel output by the matrix, the output channel to map it to
68 /// The channel layout for this substream
70 /// The matrix encoding mode for this substream
72
73 /// Channel coding parameters for channels in the substream
75
76 /// The left shift applied to random noise in 0x31ea substreams.
78 /// The current seed value for the pseudorandom noise generator(s).
80
81 /// Set if the substream contains extra info to check the size of VLC blocks.
83
84 /// Bitmask of which parameter sets are conveyed in a decoding parameter block.
86 #define PARAM_BLOCKSIZE (1 << 7)
87 #define PARAM_MATRIX (1 << 6)
88 #define PARAM_OUTSHIFT (1 << 5)
89 #define PARAM_QUANTSTEP (1 << 4)
90 #define PARAM_FIR (1 << 3)
91 #define PARAM_IIR (1 << 2)
92 #define PARAM_HUFFOFFSET (1 << 1)
93 #define PARAM_PRESENCE (1 << 0)
94 //@}
95
96 //@{
97 /** matrix data */
98
99 /// Number of matrices to be applied.
101
102 /// matrix output channel
104
105 /// Whether the LSBs of the matrix output are encoded in the bitstream.
107 /// Matrix coefficients, stored as 2.14 fixed point.
109 /// Left shift to apply to noise values in 0x31eb substreams.
111 //@}
112
113 /// Left shift to apply to Huffman-decoded residuals.
115
116 /// number of PCM samples in current audio block
118 /// Number of PCM samples decoded so far in this frame.
120
121 /// Left shift to apply to decoded PCM values to get final 24-bit output.
123
124 /// Running XOR of all output samples.
126
128
131
132 /// Current access unit being read has a major sync.
134
135 /// Size of the major sync unit, in bytes
137
138 /// Set if a valid major sync block has been read. Otherwise no decoding is possible.
140
141 /// Number of substreams contained within this stream.
143
144 /// Index of the last substream to decode - further substreams are skipped.
146
147 /// Stream needs channel reordering to comply with FFmpeg's channel order
149
150 /// number of PCM samples contained in each frame
152 /// next power of two above the number of samples in each frame
154
156
159
163
166
181 };
182
185 {
186 int i;
187
189 return 0;
190
194 return 0;
195 }
196
198
199 /** Initialize static data, constant between all invocations of the codec. */
200
202 {
203 if (!huff_vlc[0].
bits) {
213 }
214
216 }
217
219 unsigned int substr, unsigned int ch)
220 {
226
228 sign_huff_offset -= 7 << lsb_bits;
229
230 if (sign_shift >= 0)
231 sign_huff_offset -= 1 << sign_shift;
232
233 return sign_huff_offset;
234 }
235
236 /** Read a sample, consisting of either, both or neither of entropy-coded MSBs
237 * and plain LSBs. */
238
240 unsigned int substr, unsigned int pos)
241 {
243 unsigned int mat, channel;
244
248
249 for (channel = s->
min_channel; channel <= s->max_channel; channel++) {
253 int lsb_bits = cp->
huff_lsbs - quant_step_size;
254 int result = 0;
255
256 if (codebook > 0)
259
260 if (result < 0)
262
263 if (lsb_bits > 0)
264 result = (result << lsb_bits) +
get_bits(gbp, lsb_bits);
265
267 result <<= quant_step_size;
268
270 }
271
272 return 0;
273 }
274
276 {
278 int substr;
279
285
286 return 0;
287 }
288
289 /** Read a major sync info header - contains high level information about
290 * the stream - sample rate, channel arrangement etc. Most of this
291 * information is not actually necessary for decoding, only for playback.
292 */
293
295 {
298
301
305 }
308 "Channel group 2 cannot have more bits per sample than group 1.\n");
310 }
311
314 "Channel groups with differing sample rates are not currently supported.\n");
316 }
317
321 }
324 "Sampling rate %d is greater than the supported maximum (%d).\n",
327 }
330 "Block size %d is greater than the supported maximum (%d).\n",
333 }
336 "Block size pow2 %d is greater than the supported maximum (%d).\n",
339 }
340
346 }
349 "%d substreams (more than the "
350 "maximum supported by the decoder)",
353 }
354
356
359
361
362 /* limit to decoding 3 substreams, as the 4th is used by Dolby Atmos for non-audio data */
364
367
371 else
377
381
382 /* Set the layout for each substream. When there's more than one, the first
383 * substream is Stereo. Subsequent substreams' layouts are indicated in the
384 * major sync. */
388 "unexpected stream_type %X in MLP",
391 }
395 } else {
398 "unexpected stream_type %X in !MLP",
401 }
407 else
410
416 }
417 }
418
420
421 /* Parse the TrueHD decoder channel modifiers and set each substream's
422 * AVMatrixEncoding accordingly.
423 *
424 * The meaning of the modifiers depends on the channel layout:
425 *
426 * - THD_CH_MODIFIER_LTRT, THD_CH_MODIFIER_LBINRBIN only apply to 2-channel
427 *
428 * - THD_CH_MODIFIER_MONO applies to 1-channel or 2-channel (dual mono)
429 *
430 * - THD_CH_MODIFIER_SURROUNDEX, THD_CH_MODIFIER_NOTSURROUNDEX only apply to
431 * layouts with an Ls/Rs channel pair
432 */
441
447
452 break;
455 break;
456 default:
457 break;
458 }
459 }
460
461 return 0;
462 }
463
464 /** Read a restart header from a block in a substream. This contains parameters
465 * required to decode the audio that do not change very often. Generally
466 * (always) present only in blocks following a major sync. */
467
470 {
472 unsigned int ch;
473 int sync_word, tmp;
477 int min_channel, max_channel, max_matrix_channel;
481
483
484 if (sync_word != 0x31ea >> 1) {
486 "restart header sync incorrect (got 0x%04x)\n", sync_word);
488 }
489
491
495 }
496
497 skip_bits(gbp, 16);
/* Output timestamp */
498
501 max_matrix_channel =
get_bits(gbp, 4);
502
503 if (max_matrix_channel > std_max_matrix_channel) {
505 "Max matrix channel cannot be greater than %d.\n",
506 std_max_matrix_channel);
508 }
509
510 if (max_channel != max_matrix_channel) {
512 "Max channel must be equal max matrix channel.\n");
514 }
515
516 /* This should happen for TrueHD streams with >6 channels and MLP's noise
517 * type. It is not yet known if this is allowed. */
520 "%d channels (more than the "
521 "maximum supported by the decoder)",
522 max_channel + 2);
524 }
525
526 if (min_channel > max_channel) {
528 "Substream min channel cannot be greater than max channel.\n");
530 }
531
535
536 #if FF_API_REQUEST_CHANNELS
542 "Extracting %d-channel downmix from substream %d. "
543 "Further substreams will be skipped.\n",
547 } else
548 #endif
552 "Extracting %d-channel downmix (0x%"PRIx64") from substream %d. "
553 "Further substreams will be skipped.\n",
556 }
557
560
562
568 if (tmp != lossless_check)
570 "Lossless check failed - expected %02x, calculated %02x.\n",
571 lossless_check, tmp);
572 }
573
575
577
582 ch_assign);
584 channel);
585 }
588 "Assignment of matrix channel %d to invalid output channel %d",
589 ch, ch_assign);
591 }
593 }
594
596
599
600 /* Set default decoding parameters. */
605
608
609 for (ch = s->
min_channel; ch <= s->max_channel; ch++) {
615
616 /* Default audio coding is 24-bit raw PCM. */
621 }
622
630
641 }
642 }
643
644 }
645
646 return 0;
647 }
648
649 /** Read parameters for one of the prediction filters. */
650
652 unsigned int substr, unsigned int channel,
654 {
658 const char fchar = filter ? 'I' : 'F';
659 int i, order;
660
661 // Filter is 0 for FIR, 1 for IIR.
663
667 }
668
670 if (order > max_order) {
672 "%cIR filter order %d is greater than maximum %d.\n",
673 fchar, order, max_order);
675 }
677
678 if (order > 0) {
680 int coeff_bits, coeff_shift;
681
683
686 if (coeff_bits < 1 || coeff_bits > 16) {
688 "%cIR filter coeff_bits must be between 1 and 16.\n",
689 fchar);
691 }
692 if (coeff_bits + coeff_shift > 16) {
694 "Sum of coeff_bits and coeff_shift for %cIR filter must be 16 or less.\n",
695 fchar);
697 }
698
699 for (i = 0; i < order; i++)
700 fcoeff[i] =
get_sbits(gbp, coeff_bits) << coeff_shift;
701
703 int state_bits, state_shift;
704
707 "FIR filter has state data specified.\n");
709 }
710
713
714 /* TODO: Check validity of state data. */
715
716 for (i = 0; i < order; i++)
717 fp->
state[i] = state_bits ?
get_sbits(gbp, state_bits) << state_shift : 0;
718 }
719 }
720
721 return 0;
722 }
723
724 /** Read parameters for primitive matrices. */
725
727 {
729 unsigned int mat, ch;
733
737 }
738
740
743 "Number of primitive matrices cannot be greater than %d.\n",
744 max_primitive_matrices);
746 }
747
749 int frac_bits, max_chan;
753
756 "Invalid channel %d specified as output from matrix.\n",
759 }
760 if (frac_bits > 14) {
762 "Too many fractional bits specified.\n");
764 }
765
768 max_chan+=2;
769
770 for (ch = 0; ch <= max_chan; ch++) {
771 int coeff_val = 0;
773 coeff_val =
get_sbits(gbp, frac_bits + 2);
774
775 s->
matrix_coeff[mat][ch] = coeff_val << (14 - frac_bits);
776 }
777
780 else
782 }
783
784 return 0;
785 }
786
787 /** Read channel parameters. */
788
791 {
797
802
807
811 }
812
816 "FIR and IIR filters must use the same precision.\n");
818 }
819 /* The FIR and IIR filters must have the same precision.
820 * To simplify the filtering code, only the precision of the
821 * FIR filter is considered. If only the IIR filter is employed,
822 * the FIR filter precision is set to that of the IIR filter, so
823 * that the filtering code can use it. */
826
830
833
838 }
839
841
842 return 0;
843 }
844
845 /** Read decoding parameters that change more often than those in the restart
846 * header. */
847
849 unsigned int substr)
850 {
852 unsigned int ch;
854
858
866 }
867 }
868
873
883 }
884
889
891
893 }
894
895 for (ch = s->
min_channel; ch <= s->max_channel; ch++)
899
900 return 0;
901 }
902
903 #define MSB_MASK(bits) (-1u << (bits))
904
905 /** Generate PCM samples using the prediction filters and residual values
906 * read from the data stream, and update the filter state. */
907
909 unsigned int channel)
910 {
918 unsigned int filter_shift = fir->
shift;
920
923
928
931 }
932
933 /** Read a block of PCM residual data (or actual if no filtering active). */
934
936 unsigned int substr)
937 {
939 unsigned int i, ch, expected_stream_pos = 0;
941
944 expected_stream_pos +=
get_bits(gbp, 16);
946 "Substreams with VLC block size check info");
947 }
948
952 }
953
956
960
961 for (ch = s->
min_channel; ch <= s->max_channel; ch++)
963
965
970 }
971
972 return 0;
973 }
974
975 /** Data table used for TrueHD noise generation function. */
976
978 30, 51, 22, 54, 3, 7, -4, 38, 14, 55, 46, 81, 22, 58, -3, 2,
979 52, 31, -7, 51, 15, 44, 74, 30, 85, -17, 10, 33, 18, 80, 28, 62,
980 10, 32, 23, 69, 72, 26, 35, 17, 73, 60, 8, 56, 2, 6, -2, -5,
981 51, 4, 11, 50, 66, 76, 21, 44, 33, 47, 1, 26, 64, 48, 57, 40,
982 38, 16, -10, -28, 92, 22, -18, 29, -10, 5, -13, 49, 19, 24, 70, 34,
983 61, 48, 30, 14, -6, 25, 58, 33, 42, 60, 67, 17, 54, 17, 22, 30,
984 67, 44, -9, 50, -11, 43, 40, 32, 59, 82, 13, 49, -14, 55, 60, 36,
985 48, 49, 31, 47, 15, 12, 4, 65, 1, 23, 29, 39, 45, -2, 84, 69,
986 0, 72, 37, 57, 27, 41, -15, -16, 35, 31, 14, 61, 24, 0, 27, 24,
987 16, 41, 55, 34, 53, 9, 56, 12, 25, 29, 53, 5, 20, -20, -8, 20,
988 13, 28, -3, 78, 38, 16, 11, 62, 46, 29, 21, 24, 46, 65, 43, -23,
989 89, 18, 74, 21, 38, -12, 19, 12, -19, 8, 15, 33, 4, 57, 9, -8,
990 36, 35, 26, 28, 7, 83, 63, 79, 75, 11, 3, 87, 37, 47, 34, 40,
991 39, 19, 20, 42, 27, 34, 39, 77, 13, 42, 59, 64, 45, -1, 32, 37,
992 45, -5, 53, -6, 7, 36, 50, 23, 6, 32, 9, -21, 18, 71, 27, 52,
993 -25, 31, 35, 42, -1, 68, 63, 52, 26, 43, 66, 37, 41, 25, 40, 70,
994 };
995
996 /** Noise generation functions.
997 * I'm not sure what these are for - they seem to be some kind of pseudorandom
998 * sequence generators, used to generate noise data which is used when the
999 * channels are rematrixed. I'm not sure if they provide a practical benefit
1000 * to compression, or just obfuscate the decoder. Are they for some kind of
1001 * dithering? */
1002
1003 /** Generate two channels of noise, used in the matrix when
1004 * restart sync word == 0x31ea. */
1005
1007 {
1009 unsigned int i;
1012
1013 for (i = 0; i < s->
blockpos; i++) {
1014 uint16_t seed_shr7 = seed >> 7;
1017
1018 seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1019 }
1020
1022 }
1023
1024 /** Generate a block of noise, used when restart sync word == 0x31eb. */
1025
1027 {
1029 unsigned int i;
1031
1033 uint8_t seed_shr15 = seed >> 15;
1035 seed = (seed << 8) ^ seed_shr15 ^ (seed_shr15 << 5);
1036 }
1037
1039 }
1040
1041 /** Write the audio data into the output buffer. */
1042
1045 {
1048 unsigned int mat;
1049 unsigned int maxchan;
1052
1056 }
1057
1061 }
1062
1066 maxchan += 2;
1067 } else {
1069 }
1070
1071 /* Apply the channel matrices in turn to reconstruct the original audio
1072 * samples. */
1080 dest_ch,
1082 maxchan,
1086 }
1087
1088 /* get output buffer */
1099 is32);
1100
1101 /* Update matrix encoding side data */
1104
1105 *got_frame_ptr = 1;
1106
1107 return 0;
1108 }
1109
1110 /** Read an access unit from the stream.
1111 * @return negative on error, 0 if not enough data is present in the input stream,
1112 * otherwise the number of bytes consumed. */
1113
1115 int *got_frame_ptr,
AVPacket *avpkt)
1116 {
1118 int buf_size = avpkt->
size;
1121 unsigned int length, substr;
1122 unsigned int substream_start;
1123 unsigned int header_size = 4;
1124 unsigned int substr_header_size = 0;
1129
1130 if (buf_size < 4)
1132
1133 length = (
AV_RB16(buf) & 0xfff) * 2;
1134
1137
1139
1143 goto error;
1146 }
1147
1150 "Stream parameters not seen; skipping frame.\n");
1151 *got_frame_ptr = 0;
1153 }
1154
1155 substream_start = 0;
1156
1158 int extraword_present, checkdata_present,
end, nonrestart_substr;
1159
1164
1166
1167 substr_header_size += 2;
1168
1169 if (extraword_present) {
1172 goto error;
1173 }
1175 substr_header_size += 2;
1176 }
1177
1180 goto error;
1181 }
1182
1183 if (end + header_size + substr_header_size > length) {
1185 "Indicated length of substream %d data goes off end of "
1186 "packet.\n", substr);
1187 end = length - header_size - substr_header_size;
1188 }
1189
1190 if (end < substream_start) {
1192 "Indicated end offset of substream %d data "
1193 "is smaller than calculated start offset.\n",
1194 substr);
1195 goto error;
1196 }
1197
1199 continue;
1200
1201 substream_parity_present[substr] = checkdata_present;
1202 substream_data_len[substr] = end - substream_start;
1203 substream_start =
end;
1204 }
1205
1208
1209 if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) {
1211 goto error;
1212 }
1213
1214 buf += header_size + substr_header_size;
1215
1219
1222
1224 do {
1227 /* A restart header should be present. */
1229 goto next_substr;
1231 }
1232
1234 goto next_substr;
1236 goto next_substr;
1237 }
1238
1240 goto next_substr;
1241
1244
1246 goto substream_length_mismatch;
1247
1249
1251
1252 if (substream_data_len[substr] * 8 -
get_bits_count(&gb) >= 32) {
1253 int shorten_by;
1254
1257
1263
1266 }
1267
1268 if (substream_parity_present[substr]) {
1270
1272 goto substream_length_mismatch;
1273
1276
1277 if ((
get_bits(&gb, 8) ^ parity) != 0xa9 )
1281 }
1282
1284 goto substream_length_mismatch;
1285
1286 next_substr:
1289 "No restart header present in substream %d.\n", substr);
1290
1291 buf += substream_data_len[substr];
1292 }
1293
1296
1298
1299 substream_length_mismatch:
1302
1303 error:
1306 }
1307
1308 #if CONFIG_MLP_DECODER
1318 };
1319 #endif
1320 #if CONFIG_TRUEHD_DECODER
1330 };
1331 #endif /* CONFIG_TRUEHD_DECODER */