1 /**
2 * MLP encoder
3 * Copyright (c) 2008 Ramiro Polla
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
31
32 #define MAJOR_HEADER_INTERVAL 16
33
34 #define MLP_MIN_LPC_ORDER 1
35 #define MLP_MAX_LPC_ORDER 8
36 #define MLP_MIN_LPC_SHIFT 8
37 #define MLP_MAX_LPC_SHIFT 15
38
43
45 uint32_t
noisegen_seed;
///< The current seed value for the pseudorandom noise generator(s).
46
47 int data_check_present;
///< Set if the substream contains extra info to check the size of VLC blocks.
48
50
54
57
62
63 int8_t
shift[
MAX_CHANNELS];
///< Left shift to apply to decoded PCM values to get final 24-bit output.
65
77 };
78
80 uint16_t
blocksize;
///< number of PCM samples in current audio block
82
84
87
95
96 #define HUFF_OFFSET_MIN -16384
97 #define HUFF_OFFSET_MAX 16383
98
99 /** Number of possible codebooks (counting "no codebooks") */
100 #define NUM_CODEBOOKS 4
101
104
106
108 * Normal channels + noise channels. */
109
110 int coded_sample_fmt [2];
///< sample format encoded for MLP
111 int coded_sample_rate[2];
///< sample rate encoded for MLP
113
114 int flags;
///< major sync info flags
115
116 /* channel_meaning */
122
129
131
134
135 unsigned int major_frame_size;
///< Number of samples in current major frame being encoded.
137
139
141 unsigned int *
frame_size;
///< Array with number of samples/channel in each access unit.
142 unsigned int frame_index;
///< Index of current frame being encoded.
143
145
149
150 uint16_t
timestamp;
///< Timestamp of current access unit.
151 uint16_t
dts;
///< Decoding timestamp of current access unit.
152
154
158
162
164
166
169
173
177
182
184
185 /* Analysis stage. */
190 unsigned int seq_index;
///< Sequence index for high compression levels.
191
194
197
199
202
206
207 #define SYNC_MAJOR 0xf8726f
208 #define MAJOR_SYNC_INFO_SIGNATURE 0xB752
209
210 #define SYNC_MLP 0xbb
211 #define SYNC_TRUEHD 0xba
212
213 /* must be set for DVD-A */
214 #define FLAGS_DVDA 0x4000
215 /* FIFO delay must be constant */
216 #define FLAGS_CONST 0x8000
217
218 #define SUBSTREAM_INFO_MAX_2_CHAN 0x01
219 #define SUBSTREAM_INFO_HIGH_RATE 0x02
220 #define SUBSTREAM_INFO_ALWAYS_SET 0x04
221 #define SUBSTREAM_INFO_2_SUBSTREAMS 0x08
222
223 /****************************************************************************
224 ************ Functions that copy, clear, or compare parameters *************
225 ****************************************************************************/
226
227 /** Compares two FilterParams structures and returns 1 if anything has
228 * changed. Returns 0 if they are both equal.
229 */
231 {
234 int i;
235
237 return 1;
238
240 return 0;
241
243 return 1;
244
245 for (i = 0; i < fp->
order; i++)
246 if (prev_cp->
coeff[filter][i] != cp->
coeff[filter][i])
247 return 1;
248
249 return 0;
250 }
251
252 /** Compare two primitive matrices and returns 1 if anything has changed.
253 * Returns 0 if they are both equal.
254 */
256 {
259
261 return 1;
262
264 return 0;
265
267 if (prev->
fbits[channel] != mp->
fbits[channel])
268 return 1;
269
270 for (mat = 0; mat < mp->
count; mat++) {
272 return 1;
273
274 for (channel = 0; channel < ctx->
num_channels; channel++)
275 if (prev->
coeff[mat][channel] != mp->
coeff[mat][channel])
276 return 1;
277 }
278
279 return 0;
280 }
281
282 /** Compares two DecodingParams and ChannelParams structures to decide if a
283 * new decoding params header has to be written.
284 */
286 {
293 int retval = 0;
294
297
300
303
307 break;
308 }
309
313 break;
314 }
315
316 for (ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
319
323
327
330
333 retval |= 0x1;
334 }
335
336 return retval;
337 }
338
340 {
343 unsigned int order;
344
346
349
352 }
353
354 for (order = 0; order < dst->
order; order++)
355 dst_cp->
coeff[filter][order] = src_cp->
coeff[filter][order];
356 }
357
359 {
361
364
366
369
371 dst->
coeff[count][channel] = src->
coeff[count][channel];
372 }
373
376 }
377 }
378
380 unsigned int substr)
381 {
383
387
389
390 for (channel = 0; channel < ctx->
avctx->
channels; channel++) {
393
396
397 if (index)
400 }
401 }
402 }
403
404 /** Clears a DecodingParams struct the way it should be after a restart header. */
406 {
407 unsigned int substr;
408
411
414
417 }
418 }
419
420 /** Clears a ChannelParams struct the way it should be after a restart header. */
422 {
424
425 for (channel = 0; channel < ctx->
avctx->
channels; channel++) {
427
429
430 /* Default audio coding is 24-bit raw PCM. */
434 }
435 }
436
437 /** Sets default vales in our encoder for a DecodingParams struct. */
440 {
441 unsigned int substr;
442
444
447 uint8_t param_presence_flags = 0;
448
454 /* param_presence_flags |= PARAM_IIR; */
457
459 }
460 }
461
462 /****************************************************************************/
463
464 /** Calculates the smallest number of bits it takes to encode a given signed
465 * value in two's complement.
466 */
468 {
469 if (number < 0)
470 number++;
471
473 }
474
479 };
480
482 {
484 }
485
487 {
489 unsigned int substr,
index;
490 unsigned int sum = 0;
492 int ret;
493
495
497 case 44100 << 0:
501 break;
502 case 44100 << 1:
506 break;
507 case 44100 << 2:
512 break;
513 case 48000 << 0:
517 break;
518 case 48000 << 1:
522 break;
523 case 48000 << 2:
528 break;
529 default:
531 "sample rates are 44100, 88200, 176400, 48000, "
533 return -1;
534 }
536
537 /* TODO Keep count of bitrate and calculate real value. */
539
540 /* TODO support more channels. */
543 "Only mono and stereo are supported at the moment.\n");
544 }
545
549 }
550
556 break;
557 /* TODO 20 bits: */
562 break;
563 default:
565 "Only 16- and 24-bit samples are supported.\n");
566 return -1;
567 }
569
571
575 /* TODO Let user pass major header interval as parameter. */
577
581
582 /* TODO Let user pass parameters for LPC filter. */
583
585
589 "Not enough memory for buffering samples.\n");
591 }
592
594
598 "Not enough memory for buffering samples.\n");
600 }
601
605 "Not enough memory for buffering samples.\n");
607 }
608
610
611 ctx->
num_substreams = 1;
// TODO: change this after adding multi-channel support for TrueHD
612
614 /* MLP */
638 default:
640 return -1;
641 }
645 } else {
646 /* TrueHD */
653 break;
659 break;
665 break;
666 default:
668 return -1;
669 }
673 }
674
676
680
684
687
691
696 }
703 "Not enough memory for analysis context.\n");
705 }
706
712 "Not enough memory for analysis context.\n");
714 }
715
718
719 /* TODO see if noisegen_seed is really worth it. */
721
724 /* FIXME: this works for 1 and 2 channels, but check for more */
726 }
727
730
734 "Not enough memory for LPC context.\n");
735 return ret;
736 }
737
739
740 return 0;
741 }
742
743 /****************************************************************************
744 ****************** Functions that write to the bitstream *******************
745 ****************************************************************************/
746
747 /** Writes a major sync header to the bitstream. */
749 {
751
753
755
763 put_bits(&pb, 4, 0 );
/* multi_channel_type */
775 }
776
779 put_bits(&pb, 16, 0 );
/* ignored */
782 put_bits(&pb, 4, 1 );
/* num_substreams */
783 put_bits(&pb, 4, 0x1 );
/* ignored */
784
785 /* channel_meaning */
791 put_bits(&pb, 10, 0 );
/* speaker_layout */
792 put_bits(&pb, 3, 0 );
/* copy_protection */
793 put_bits(&pb, 16, 0x8080 );
/* ignored */
795 put_bits(&pb, 4, 0 );
/* source_format */
797
799
801 }
802
803 /** Writes a restart header to the bitstream. Damaged streams can start being
804 * decoded losslessly again after such a header and the subsequent decoding
805 * params header.
806 */
808 {
815
816 put_bits(pb, 14, 0x31ea );
/* TODO 0x31eb */
823 put_bits(pb, 4, 0 );
/* TODO max_shift */
830
833
834 /* Data must be flushed for the checksum to be correct. */
835 tmpb = *pb;
837
839
841 }
842
843 /** Writes matrix params for all primitive matrices to the bitstream. */
845 {
848 unsigned int mat;
849
851
852 for (mat = 0; mat < mp->
count; mat++) {
854
857 put_bits(pb, 1, 0 );
/* lsb_bypass */
858
859 for (channel = 0; channel < ctx->
num_channels; channel++) {
861
862 if (coeff) {
864
865 coeff >>= 14 - mp->
fbits[mat];
866
868 } else {
870 }
871 }
872 }
873 }
874
875 /** Writes filter parameters for one filter to the bitstream. */
878 {
880
882
884 int i;
886
890
891 for (i = 0; i < fp->
order; i++) {
893 }
894
895 /* TODO state data for IIR filter. */
897 }
898 }
899
900 /** Writes decoding parameters to the bitstream. These change very often,
901 * usually at almost every frame.
902 */
904 int params_changed)
905 {
910
915 } else {
917 }
918
923 } else {
925 }
926 }
927
932 } else {
934 }
935 }
936
942 } else {
944 }
945 }
946
952 } else {
954 }
955 }
956
957 for (ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
959
962
967 } else {
969 }
970 }
971
976 } else {
978 }
979 }
980
985 } else {
987 }
988 }
989
992 } else {
994 }
995 }
996 }
997
998 /** Writes the residuals to the bitstream. That is, the VLC codes from the
999 * codebooks (if any is used), and then the residual.
1000 */
1002 {
1010
1011 for (ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
1013 int sign_shift;
1014
1018
1019 sign_shift = lsb_bits[
ch] - 1;
1020
1022 sign_huff_offset[
ch] -= 7 << lsb_bits[
ch];
1024 }
1025
1026 /* Unsign if needed. */
1027 if (sign_shift >= 0)
1028 sign_huff_offset[
ch] -= 1 << sign_shift;
1029 }
1030
1032 for (ch = rh->
min_channel; ch <= rh->max_channel; ch++) {
1034
1035 sample -= sign_huff_offset[
ch];
1036
1037 if (codebook_index[ch] >= 0) {
1038 int vlc = sample >> lsb_bits[
ch];
1041 }
1042
1044 }
1045 sample_buffer += 2; /* noise channels */
1046 }
1047
1049 }
1050
1051 /** Writes the substreams data to the bitstream. */
1053 int restart_frame,
1055 {
1057 unsigned int substr;
1059
1061
1065 unsigned int subblock;
1067 int substr_restart_frame = restart_frame;
1070 int params_changed;
1071
1073
1075
1076 for (subblock = 0; subblock <= num_subblocks; subblock++) {
1077 unsigned int subblock_index;
1078
1079 subblock_index = cur_subblock_index++;
1080
1083
1085
1086 if (substr_restart_frame || params_changed) {
1088
1089 if (substr_restart_frame) {
1091
1094 } else {
1096 }
1097
1099 } else {
1101 }
1102
1104
1105 put_bits(&pb, 1, !substr_restart_frame);
1106
1107 substr_restart_frame = 0;
1108 }
1109
1111
1113
1115 /* TODO find a sample and implement shorten_by. */
1117 }
1118
1119 /* Data must be flushed for the checksum and parity to be correct. */
1120 tmpb = pb;
1122
1125
1128
1130
1132 substream_data_len[substr] =
end;
1133
1135 }
1136
1139
1141 }
1142
1143 /** Writes the access unit and substream headers to the bitstream. */
1146 int restart_frame,
1148 {
1149 uint16_t access_unit_header = 0;
1150 uint16_t parity_nibble = 0;
1151 unsigned int substr;
1152
1153 parity_nibble = ctx->
dts;
1155
1157 uint16_t substr_hdr = 0;
1158
1159 substr_hdr |= (0 << 15); /* extraword */
1160 substr_hdr |= (!restart_frame << 14); /* !restart_frame */
1161 substr_hdr |= (1 << 13); /* checkdata */
1162 substr_hdr |= (0 << 12); /* ??? */
1163 substr_hdr |= (substream_data_len[substr] / 2) & 0x0FFF;
1164
1165 AV_WB16(substream_headers, substr_hdr);
1166
1167 parity_nibble ^= *substream_headers++;
1168 parity_nibble ^= *substream_headers++;
1169 }
1170
1171 parity_nibble ^= parity_nibble >> 8;
1172 parity_nibble ^= parity_nibble >> 4;
1173 parity_nibble &= 0xF;
1174
1175 access_unit_header |= (parity_nibble ^ 0xF) << 12;
1176 access_unit_header |= length & 0xFFF;
1177
1178 AV_WB16(frame_header , access_unit_header);
1180 }
1181
1182 /** Writes an entire access unit to the bitstream. */
1184 int buf_size, int restart_frame)
1185 {
1188 unsigned int substr;
1189 int total_length;
1190
1191 if (buf_size < 4)
1192 return -1;
1193
1194 /* Frame header will be written at the end. */
1195 buf += 4;
1196 buf_size -= 4;
1197
1198 if (restart_frame) {
1199 if (buf_size < 28)
1200 return -1;
1202 buf += 28;
1203 buf_size -= 28;
1204 }
1205
1207
1208 /* Substream headers will be written at the end. */
1210 buf += 2;
1211 buf_size -= 2;
1212 }
1213
1214 buf =
write_substrs(ctx, buf, buf_size, restart_frame, substream_data_len);
1215
1216 total_length = buf - buf0;
1217
1218 write_frame_headers(ctx, buf0, buf1, total_length / 2, restart_frame, substream_data_len);
1219
1220 return total_length;
1221 }
1222
1223 /****************************************************************************
1224 ****************** Functions that input data to context ********************
1225 ****************************************************************************/
1226
1227 /** Inputs data from the samples passed by lavc into the context, shifts them
1228 * appropriately depending on the bit-depth, and calculates the
1229 * lossless_check_data that will be written to the restart header.
1230 */
1232 int is24)
1233 {
1236 const int16_t *samples_16 = (const int16_t *) samples;
1237 unsigned int substr;
1238
1240
1244 int32_t temp_lossless_check_data = 0;
1245 uint32_t greatest = 0;
1247 int i;
1248
1250 for (channel = 0; channel <= rh->
max_channel; channel++) {
1251 uint32_t abs_sample;
1253
1254 sample = is24 ? *samples_32++ >> 8 : *samples_16++ << 8;
1255
1256 /* TODO Find out if number_sbits can be used for negative values. */
1257 abs_sample =
FFABS(sample);
1258 if (greatest < abs_sample)
1259 greatest = abs_sample;
1260
1261 temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;
1262 *sample_buffer++ =
sample;
1263 }
1264
1265 sample_buffer += 2; /* noise channels */
1266 }
1267
1269
1270 *lossless_check_data++ = temp_lossless_check_data;
1271 }
1272 }
1273
1274 /** Wrapper function for inputting data in two different bit-depths. */
1276 {
1279 else
1281 }
1282
1284 {
1287
1292
1295 *sample_buffer++ = *input_buffer++;
1296 sample_buffer += 2; /* noise_channels */
1297 input_buffer += 2; /* noise_channels */
1298 }
1299 }
1300 }
1301
1302 /****************************************************************************
1303 ********* Functions that analyze the data and set the parameters ***********
1304 ****************************************************************************/
1305
1306 /** Counts the number of trailing zeroes in a value */
1308 {
1310
1311 for (bits = 0; bits < 24 && !(sample & (1<<
bits)); bits++);
1312
1313 /* All samples are 0. TODO Return previous quant_step_size to avoid
1314 * writing a new header. */
1315 if (bits == 24)
1316 return 0;
1317
1319 }
1320
1321 /** Determines how many bits are zero at the end of all samples so they can be
1322 * shifted out.
1323 */
1325 {
1332 int i;
1333
1334 memset(sample_mask, 0x00, sizeof(sample_mask));
1335
1337 for (channel = 0; channel <= rh->
max_channel; channel++)
1338 sample_mask[channel] |= *sample_buffer++;
1339
1340 sample_buffer += 2; /* noise channels */
1341 }
1342
1343 for (channel = 0; channel <= rh->
max_channel; channel++)
1345 }
1346
1347 /** Determines the smallest number of bits needed to encode the filter
1348 * coefficients, and if it's possible to right-shift their values without
1349 * losing any precision.
1350 */
1352 {
1353 int min = INT_MAX, max = INT_MIN;
1355 int coeff_mask = 0;
1356 int order;
1357
1358 for (order = 0; order < fp->
order; order++) {
1359 int coeff = fcoeff[order];
1360
1361 if (coeff < min)
1363 if (coeff > max)
1365
1366 coeff_mask |=
coeff;
1367 }
1368
1370
1371 for (shift = 0; shift < 7 && bits + shift < 16 && !(coeff_mask & (1<<
shift)); shift++);
1372
1375 }
1376
1377 /** Determines the best filter parameters for the given data and writes the
1378 * necessary information to the context.
1379 * TODO Add IIR filter predictor!
1380 */
1383 int clear_filter)
1384 {
1387
1389 clear_filter) {
1391 }
else if (filter ==
IIR) {
1393 }
else if (filter ==
FIR) {
1401 unsigned int i;
1402 int order;
1403
1405 *lpc_samples++ = *sample_buffer;
1407 }
1408
1414
1416 fp->
shift = shift[order-1];
1417
1418 for (i = 0; i < order; i++)
1419 fcoeff[i] = coefs[order-1][i];
1420
1422 }
1423 }
1424
1425 /** Tries to determine a good prediction filter, and applies it to the samples
1426 * buffer if the filter is good enough. Sets the filter data to be cleared if
1427 * no good filter was found.
1428 */
1430 {
1433
1437 }
1438 }
1439
1445 };
1446
1448 {
1449 uint64_t score[4], sum[4] = { 0, 0, 0, 0, };
1452 int i;
1454
1458
1459 sum[0] +=
FFABS( left );
1460 sum[1] +=
FFABS( right);
1461 sum[2] +=
FFABS((left + right) >> 1);
1462 sum[3] +=
FFABS( left - right);
1463 }
1464
1469
1470 for(i = 1; i < 3; i++)
1471 if(score[i] < score[best])
1472 best = i;
1473
1474 return best;
1475 }
1476
1477 /** Determines how many fractional bits are needed to encode matrix
1478 * coefficients. Also shifts the coefficients to fit within 2.14 bits.
1479 */
1481 {
1487
1488 for (channel = 0; channel < ctx->
num_channels; channel++) {
1490 coeff_mask |=
coeff;
1491 }
1492
1493 for (bits = 0; bits < 14 && !(coeff_mask & (1<<
bits)); bits++);
1494
1496 }
1497
1498 /** Determines best coefficients to use for the lossless matrix. */
1500 {
1503 unsigned int shift = 0;
1505 int mat;
1507
1508 /* No decorrelation for non-stereo. */
1511 return;
1512 }
1513
1515
1516 switch(mode) {
1517 /* TODO: add matrix for MID_SIDE */
1521 break;
1525 mp->
coeff[0][0] = 1 << 14; mp->
coeff[0][1] = -(1 << 14);
1526 mp->
coeff[0][2] = 0 << 14; mp->
coeff[0][2] = 0 << 14;
1527 mp->
forco[0][0] = 1 << 14; mp->
forco[0][1] = -(1 << 14);
1528 mp->
forco[0][2] = 0 << 14; mp->
forco[0][2] = 0 << 14;
1529 break;
1533 mp->
coeff[0][0] = 1 << 14; mp->
coeff[0][1] = 1 << 14;
1534 mp->
coeff[0][2] = 0 << 14; mp->
coeff[0][2] = 0 << 14;
1535 mp->
forco[0][0] = 1 << 14; mp->
forco[0][1] = -(1 << 14);
1536 mp->
forco[0][2] = 0 << 14; mp->
forco[0][2] = 0 << 14;
1537 break;
1538 }
1539
1540 for (mat = 0; mat < mp->
count; mat++)
1542
1543 for (channel = 0; channel < ctx->
num_channels; channel++)
1544 mp->
shift[channel] = shift;
1545 }
1546
1547 /** Min and max values that can be encoded with each codebook. The values for
1548 * the third codebook take into account the fact that the sign shift for this
1549 * codebook is outside the coded value, so it has one more bit of precision.
1550 * It should actually be -7 -> 7, shifted down by 0.5.
1551 */
1553 {-9, 8}, {-8, 7}, {-15, 14},
1554 };
1555
1556 /** Determines the amount of bits needed to encode the samples using no
1557 * codebooks and a specified offset.
1558 */
1563 {
1566 int lsb_bits;
1567
1570
1572
1573 lsb_bits += !!lsb_bits;
1574
1575 unsign = 1 << (lsb_bits - 1);
1576
1580 bo->
min = offset - unsign + 1;
1581 bo->
max = offset + unsign;
1582 }
1583
1584 /** Determines the least amount of bits needed to encode the samples using no
1585 * codebooks.
1586 */
1591 {
1596 int lsb_bits;
1597
1598 /* Set offset inside huffoffset's boundaries by adjusting extremes
1599 * so that more bits are used, thus shifting the offset. */
1604
1605 /* Determine offset and minimum number of bits. */
1607
1609
1610 unsign = 1 << (lsb_bits - 1);
1611
1612 /* If all samples are the same (lsb_bits == 0), offset must be
1613 * adjusted because of sign_shift. */
1614 offset = min + diff / 2 + !!lsb_bits;
1615
1619 bo->
min = max - unsign + 1;
1620 bo->
max = min + unsign;
1621 }
1622
1623 /** Determines the least amount of bits needed to encode the samples using a
1624 * given codebook and a given offset.
1625 */
1627 unsigned int channel,
int codebook,
1630 {
1635 int codebook_offset = 7 + (2 - codebook);
1637 int lsb_bits = 0, bitcount = 0;
1638 int offset_min = INT_MAX, offset_max = INT_MAX;
1640 int i;
1641
1644
1645 while (sample_min < codebook_min || sample_max > codebook_max) {
1646 lsb_bits++;
1647 sample_min >>= 1;
1648 sample_max >>= 1;
1649 }
1650
1651 unsign = 1 << lsb_bits;
1652 mask = unsign - 1;
1653
1654 if (codebook == 2) {
1655 unsign_offset -= unsign;
1656 lsb_bits++;
1657 }
1658
1661 int temp_min, temp_max;
1662
1663 sample -= unsign_offset;
1664
1665 temp_min = sample &
mask;
1666 if (temp_min < offset_min)
1667 offset_min = temp_min;
1668
1669 temp_max = unsign - temp_min - 1;
1670 if (temp_max < offset_max)
1671 offset_max = temp_max;
1672
1673 sample >>= lsb_bits;
1674
1676
1678 }
1679
1685 }
1686
1687 /** Determines the least amount of bits needed to encode the samples using a
1688 * given codebook. Searches for the best offset to minimize the bits.
1689 */
1691 unsigned int channel,
int codebook,
1694 {
1695 int previous_count = INT_MAX;
1696 int offset_min, offset_max;
1697 int is_greater = 0;
1698
1701
1702 for (;;) {
1704
1706 min, max, offset,
1707 &temp_bo);
1708
1709 if (temp_bo.
bitcount < previous_count) {
1711 *bo = temp_bo;
1712
1713 is_greater = 0;
1715 break;
1716
1718
1719 if (direction) {
1720 offset = temp_bo.
max + 1;
1721 if (offset > offset_max)
1722 break;
1723 } else {
1724 offset = temp_bo.
min - 1;
1725 if (offset < offset_min)
1726 break;
1727 }
1728 }
1729 }
1730
1731 /** Determines the least amount of bits needed to encode the samples using
1732 * any or no codebook.
1733 */
1735 {
1739
1740 for (channel = 0; channel <= rh->
max_channel; channel++) {
1745 int average = 0;
1747 int i;
1748
1749 /* Determine extremes and average. */
1752 if (sample < min)
1754 if (sample > max)
1758 }
1760
1761 /* If filtering is used, we always set the offset to zero, otherwise
1762 * we search for the offset that minimizes the bitcount. */
1763 if (no_filters_used) {
1766 } else {
1768 }
1769
1771 BestOffset temp_bo = { 0, INT_MAX, 0, 0, 0, };
1772 int16_t offset_max;
1773
1775 min, max, offset,
1776 &temp_bo);
1777
1778 if (no_filters_used) {
1779 offset_max = temp_bo.
max;
1780
1782 min, max, &temp_bo, 0);
1784 min, max, &temp_bo, 1);
1785 }
1786
1788 }
1789 }
1790 }
1791
1792 /****************************************************************************
1793 *************** Functions that process the data in some way ****************
1794 ****************************************************************************/
1795
1796 #define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
1797 #define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
1798
1799 #define MSB_MASK(bits) (-1u << bits)
1800
1801 /** Applies the filter to the current samples, and saves the residual back
1802 * into the samples buffer. If the filter is too bad and overflows the
1803 * maximum amount of bits allowed (16 or 24), the samples buffer is left as is and
1804 * the function returns -1.
1805 */
1807 {
1814 unsigned int filter_shift = fp[
FIR]->
shift;
1816 int i;
1817
1821 if (!filter_state_buffer[i]) {
1823 "Not enough memory for applying filters.\n");
1824 return -1;
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
1833 }
1834
1835 for (i = 8; i < number_of_samples; i++) {
1837 unsigned int order;
1838 int64_t accum = 0;
1840
1841 for (filter = 0; filter <
NUM_FILTERS; 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
1852 return -1;
1853
1856
1858 }
1859
1861 for (i = 0; i < number_of_samples; i++) {
1862 *sample_buffer = filter_state_buffer[
IIR][i];
1863
1865 }
1866
1869 }
1870
1871 return 0;
1872 }
1873
1875 {
1878
1881 /* Filter is horribly wrong.
1882 * Clear filter params and update state. */
1886 }
1887 }
1888 }
1889
1890 /** Generates two noise channels worth of data. */
1892 {
1895 unsigned int i;
1897
1899 uint16_t seed_shr7 = seed >> 7;
1900 *sample_buffer++ = ((int8_t)(seed >> 15)) << rh->
noise_shift;
1901 *sample_buffer++ = ((int8_t) seed_shr7) << rh->
noise_shift;
1902
1903 seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1904
1906 }
1907
1909 }
1910
1911 /** Rematrixes all channels using chosen coefficients. */
1913 {
1917 unsigned int mat, i, maxchan;
1918
1920
1921 for (mat = 0; mat < mp->
count; mat++) {
1924 unsigned int outch = mp->
outch[mat];
1925
1928 unsigned int src_ch;
1929 int64_t accum = 0;
1930
1931 for (src_ch = 0; src_ch < maxchan; src_ch++) {
1933 accum += (int64_t) sample * mp->
forco[mat][src_ch];
1934 }
1935 sample_buffer[outch] = (accum >> 14) &
mask;
1936
1938 }
1939 }
1940 }
1941
1942 /****************************************************************************
1943 **** Functions that deal with determining the best parameters and output ***
1944 ****************************************************************************/
1945
1950
1952
1953 #define ZERO_PATH '0'
1954 #define CODEBOOK_CHANGE_BITS 21
1955
1957 {
1958 unsigned int i;
1959
1962 path_counter[i].
path[1] = 0x00;
1964 }
1965 }
1966
1968 {
1970 return 1;
1971
1972 return 0;
1973 }
1974
1977 {
1980 char *path = src->
path + 1;
1981 int prev_codebook;
1982 int i;
1983
1984 for (i = 0; path[i]; i++)
1986
1987 prev_codebook = path[i - 1] -
ZERO_PATH;
1988
1990
1991 bitcount += cur_bo[cur_codebook].
bitcount;
1992
1993 if (prev_codebook != cur_codebook ||
1996
1997 return bitcount;
1998 }
1999
2001 {
2005
2009 unsigned int best_codebook;
2011 char *best_path;
2012
2014
2016 unsigned int best_bitcount = INT_MAX;
2017 unsigned int codebook;
2018
2020
2022 int prev_best_bitcount = INT_MAX;
2023 int last_best;
2024
2025 for (last_best = 0; last_best < 2; last_best++) {
2028 int temp_bitcount;
2029
2030 /* First test last path with same headers,
2031 * then with last best. */
2032 if (last_best) {
2034 } else {
2036 continue;
2037 else
2038 src_path = &path_counter[codebook];
2039 }
2040
2042
2043 if (temp_bitcount < best_bitcount) {
2044 best_bitcount = temp_bitcount;
2045 best_codebook = codebook;
2046 }
2047
2048 if (temp_bitcount < prev_best_bitcount) {
2049 prev_best_bitcount = temp_bitcount;
2050 if (src_path != dst_path)
2053 dst_path->
bitcount = temp_bitcount;
2054 }
2055 }
2056 }
2057
2058 prev_bo = cur_bo;
2059
2060 memcpy(&path_counter[NUM_CODEBOOKS], &path_counter[best_codebook],
sizeof(
PathCounter));
2061 }
2062
2064
2065 /* Update context. */
2068
2069 best_codebook = *best_path++ -
ZERO_PATH;
2071
2075 }
2076 }
2077 }
2078
2079 /** Analyzes all collected bitcounts and selects the best parameters for each
2080 * individual access unit.
2081 * TODO This is just a stub!
2082 */
2084 {
2087 unsigned int substr;
2090
2095
2102 for (channel = 0; channel < ctx->
avctx->
channels; channel++) {
2104 if (max_huff_lsbs < huff_lsbs)
2105 max_huff_lsbs = huff_lsbs;
2109 }
2110 }
2111 }
2112
2114
2116 if (max_output_bits < ctx->max_output_bits[index])
2119
2121
2123
2126
2130
2132
2135 }
2136 }
2137
2141 }
2142
2144 {
2148 unsigned int substr;
2149
2151
2155
2162
2164
2165 /* Copy frame_size from frames 0...max to decoding_params 1...max + 1
2166 * decoding_params[0] is for the filter state subblock.
2167 */
2171 }
2172 /* The official encoder seems to always encode a filter state subblock
2173 * even if there are no filters. TODO check if it is possible to skip
2174 * the filter state subblock for no filters.
2175 */
2176 (seq_dp + substr)->blocksize = 8;
2178
2185 }
2186
2188 }
2189 }
2190
2192 {
2193 unsigned int substr;
2194
2196
2200
2204
2206
2209
2212
2215 }
2216 }
2217
2218 /****************************************************************************/
2219
2222 {
2224 unsigned int bytes_written = 0;
2225 int restart_frame, ret;
2227
2229 return ret;
2230
2231 if (!frame)
2232 return 1;
2233
2234 /* add current frame to queue */
2235 if (frame) {
2237 return ret;
2238 }
2239
2240 data = frame->
data[0];
2241
2243
2246
2248 return 0;
2249 }
2250
2253
2255
2257 if (data) {
2258 goto input_and_return;
2259 } else {
2260 /* There are less frames than the requested major header interval.
2261 * Update the context to reflect this.
2262 */
2265
2268 }
2269 }
2270
2274 return -1;
2275 }
2276
2278
2279 if (restart_frame) {
2283 }
2284
2287
2289
2292
2293 input_and_return:
2294
2295 if (data) {
2302 }
2303
2305
2306 if (!restart_frame) {
2307 int seq_index;
2308
2309 for (seq_index = 0;
2311 seq_index++) {
2312 unsigned int number_of_samples = 0;
2314
2318
2323
2327
2331
2334 }
2336
2337 for (index = 0; index < ctx->
seq_size[seq_index]; index++) {
2340 }
2341
2343
2345 }
2346
2352
2354 goto no_data_left;
2355 }
2356 }
2357
2358 no_data_left:
2359
2362 avpkt->
size = bytes_written;
2363 *got_packet = 1;
2364 return 0;
2365 }
2366
2368 {
2370
2372
2381
2382 return 0;
2383 }
2384
2385 #if CONFIG_MLP_ENCODER
2397 .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
2399 };
2400 #endif
2401 #if CONFIG_TRUEHD_ENCODER
2413 .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
2415 };
2416 #endif
uint8_t shift
Right shift to apply to output of filter.
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
unsigned int seq_size[MAJOR_HEADER_INTERVAL]
int32_t * lpc_sample_buffer
static void analyze_sample_buffer(MLPEncodeContext *ctx)
#define SAMPLE_MIN(bitdepth)
#define AV_CH_LAYOUT_4POINT1
static void default_decoding_params(MLPEncodeContext *ctx, DecodingParams decoding_params[MAX_SUBSTREAMS])
Sets default vales in our encoder for a DecodingParams struct.
DecodingParams * seq_decoding_params
static int shift(int a, int b)
FilterParams filter_params[NUM_FILTERS]
static int mlp_peak_bitrate(int peak_bitrate, int sample_rate)
This structure describes decoded (raw) audio or video data.
static int compare_decoding_params(MLPEncodeContext *ctx)
Compares two DecodingParams and ChannelParams structures to decide if a new decoding params header ha...
#define SUBSTREAM_INFO_ALWAYS_SET
ptrdiff_t const GLvoid * data
unsigned int seq_offset[MAJOR_HEADER_INTERVAL]
unsigned int number_of_subblocks
static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel, PathCounter *src, int cur_codebook)
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void write_filter_params(MLPEncodeContext *ctx, PutBitContext *pb, unsigned int channel, unsigned int filter)
Writes filter parameters for one filter to the bitstream.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_CH_LAYOUT_SURROUND
uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
static av_cold int init(AVCodecContext *avctx)
unsigned int min_restart_interval
Min interval of access units in between two major frames.
static int compare_matrix_params(MLPEncodeContext *ctx, const MatrixParams *prev, const MatrixParams *mp)
Compare two primitive matrices and returns 1 if anything has changed.
static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet)
uint8_t param_presence_flags
Bitmask of which parameter sets are conveyed in a decoding parameter block.
DecodingParams major_decoding_params[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS]
DecodingParams to be written to bitstream.
static void codebook_bits_offset(MLPEncodeContext *ctx, unsigned int channel, int codebook, int32_t sample_min, int32_t sample_max, int16_t offset, BestOffset *bo)
Determines the least amount of bits needed to encode the samples using a given codebook and a given o...
#define MAJOR_SYNC_INFO_SIGNATURE
unsigned int major_cur_subblock_index
unsigned int major_number_of_frames
const uint8_t ff_mlp_huffman_tables[3][18][2]
Tables defining the Huffman codes.
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
static void code_matrix_coeffs(MLPEncodeContext *ctx, unsigned int mat)
Determines how many fractional bits are needed to encode matrix coefficients.
#define AV_CH_LAYOUT_4POINT0
#define SUBSTREAM_INFO_MAX_2_CHAN
unsigned int next_major_frame_size
Counter of number of samples for next major frame.
int coded_sample_fmt[2]
sample format encoded for MLP
#define AV_CH_LAYOUT_STEREO
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
static void copy_matrix_params(MatrixParams *dst, MatrixParams *src)
const uint64_t ff_mlp_channel_layouts[12]
int32_t * sample_buffer
Pointer to current access unit samples.
static const int codebook_extremes[3][2]
Min and max values that can be encoded with each codebook.
static void write_matrix_params(MLPEncodeContext *ctx, PutBitContext *pb)
Writes matrix params for all primitive matrices to the bitstream.
int32_t * major_scratch_buffer
Scratch buffer big enough to fit all data for one entire major frame interval.
static int compare_filter_params(const ChannelParams *prev_cp, const ChannelParams *cp, int filter)
Compares two FilterParams structures and returns 1 if anything has changed.
#define NUM_CODEBOOKS
Number of possible codebooks (counting "no codebooks")
static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, int32_t *fcoeff)
Determines the smallest number of bits needed to encode the filter coefficients, and if it's possible...
unsigned int number_of_frames
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
DecodingParams * prev_decoding_params
int32_t forco[MAX_MATRICES][MAX_CHANNELS+2]
forward coefficients
uint16_t blocksize
number of PCM samples in current audio block
static void process_major_frame(MLPEncodeContext *ctx)
int8_t shift[MAX_CHANNELS]
Left shift to apply to decoded PCM values to get final 24-bit output.
enum AVSampleFormat sample_fmt
audio sample format
#define MAJOR_HEADER_INTERVAL
MLP encoder Copyright (c) 2008 Ramiro Polla.
uint8_t fbits[MAX_CHANNELS]
fraction bits
static av_cold int mlp_encode_close(AVCodecContext *avctx)
static uint8_t xor_32_to_8(uint32_t value)
XOR four bytes into one.
static void clear_channel_params(MLPEncodeContext *ctx, ChannelParams channel_params[MAX_CHANNELS])
Clears a ChannelParams struct the way it should be after a restart header.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
ChannelParams * seq_channel_params
static av_cold int end(AVCodecContext *avctx)
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
static void no_codebook_bits(MLPEncodeContext *ctx, unsigned int channel, int32_t min, int32_t max, BestOffset *bo)
Determines the least amount of bits needed to encode the samples using no codebooks.
Public header for CRC hash function implementation.
static const char * path_counter_codebook[]
#define MLP_MIN_LPC_ORDER
static void input_data(MLPEncodeContext *ctx, void *samples)
Wrapper function for inputting data in two different bit-depths.
static void clear_path_counter(PathCounter *path_counter)
static void apply_filters(MLPEncodeContext *ctx)
static ChannelParams restart_channel_params[MAX_CHANNELS]
static uint8_t * write_substrs(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, int restart_frame, uint16_t substream_data_len[MAX_SUBSTREAMS])
Writes the substreams data to the bitstream.
int16_t huff_offset
Offset to apply to residual values.
unsigned int sequence_size
static void set_major_params(MLPEncodeContext *ctx)
Analyzes all collected bitcounts and selects the best parameters for each individual access unit...
static void generate_2_noise_channels(MLPEncodeContext *ctx)
Generates two noise channels worth of data.
int flags
major sync info flags
ChannelParams major_channel_params[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS]
ChannelParams to be written to bitstream.
#define NUM_FILTERS
number of allowed filters
uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size)
XOR together all the bytes of a buffer.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int number_trailing_zeroes(int32_t sample)
Counts the number of trailing zeroes in a value.
unsigned int major_frame_size
Number of samples in current major frame being encoded.
static const uint16_t mask[17]
int32_t * lossless_check_data
Array with lossless_check_data for each access unit.
static void determine_filters(MLPEncodeContext *ctx)
Tries to determine a good prediction filter, and applies it to the samples buffer if the filter is go...
unsigned int starting_frame_index
RestartHeader restart_header[MAX_SUBSTREAMS]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
unsigned int major_filter_state_subblock
#define AV_CH_LAYOUT_QUAD
#define SAMPLE_MAX(bitdepth)
const char * name
Name of the codec implementation.
static av_cold int mlp_encode_init(AVCodecContext *avctx)
uint16_t dts
Decoding timestamp of current access unit.
static const uint8_t offset[127][2]
static void write_restart_header(MLPEncodeContext *ctx, PutBitContext *pb)
Writes a restart header to the bitstream.
int num_channels
Number of channels in major_scratch_buffer.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
static BestOffset restart_best_offset[NUM_CODEBOOKS]
int coded_sample_rate[2]
sample rate encoded for MLP
uint64_t channel_layout
Audio channel layout.
static int put_bits_count(PutBitContext *s)
uint8_t ch_modifier_thd2
channel modifier for TrueHD stream 2
unsigned int major_number_of_subblocks
#define MLP_MIN_LPC_SHIFT
uint8_t quant_step_size[MAX_CHANNELS]
left shift to apply to Huffman-decoded residuals
uint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size)
Calculate an 8-bit checksum over a restart header – a non-multiple-of-8 number of bits...
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
const ChannelInformation ff_mlp_ch_info[21]
Tables defining channel information.
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
unsigned int max_restart_interval
Max interval of access units in between two major frames.
#define AV_CH_LAYOUT_3POINT1
int32_t * write_buffer
Pointer to data currently being written to bitstream.
#define MLP_MAX_LPC_ORDER
static enum MLPChMode estimate_stereo_mode(MLPEncodeContext *ctx)
uint8_t ch_modifier_thd0
channel modifier for TrueHD stream 0
DecodingParams * cur_decoding_params
ChannelParams * prev_channel_params
int num_substreams
Number of substreams contained within this stream.
int32_t coeff[MAX_MATRICES][MAX_CHANNELS+2]
decoding coefficients
static void copy_restart_frame_params(MLPEncodeContext *ctx, unsigned int substr)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
uint16_t timestamp
Timestamp of current access unit.
int32_t coeff[NUM_FILTERS][MAX_FIR_ORDER]
static volatile int checksum
#define AV_CH_LAYOUT_5POINT1_BACK
RestartHeader * cur_restart_header
static void codebook_bits(MLPEncodeContext *ctx, unsigned int channel, int codebook, int offset, int32_t min, int32_t max, BestOffset *bo, int direction)
Determines the least amount of bits needed to encode the samples using a given codebook.
unsigned int restart_intervals
Number of possible major frame sizes.
int major_params_changed[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS]
params_changed to be written to bitstream.
static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
Writes a major sync header to the bitstream.
int frame_size
Number of samples per channel in an audio frame.
ChannelParams * cur_channel_params
unsigned int * frame_size
Array with number of samples/channel in each access unit.
static void determine_quant_step_size(MLPEncodeContext *ctx)
Determines how many bits are zero at the end of all samples so they can be shifted out...
uint8_t codebook
Which VLC codebook to use to read residuals.
Libavcodec external API header.
AVSampleFormat
Audio sample formats.
unsigned int * max_output_bits
largest output bit-depth
unsigned int max_codebook_search
int sample_rate
samples per second
uint8_t order
number of taps in filter
unsigned int number_of_samples
main external API structure.
static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb)
Writes the residuals to the bitstream.
Levinson-Durbin recursion.
uint8_t channel_arrangement
channel arrangement for MLP streams
MatrixParams matrix_params
int32_t * last_frame
Pointer to last frame with data to encode.
static void lossless_matrix_coeffs(MLPEncodeContext *ctx)
Determines best coefficients to use for the lossless matrix.
#define AV_CH_LAYOUT_5POINT0_BACK
BestOffset(* cur_best_offset)[NUM_CODEBOOKS]
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
static void copy_filter_params(ChannelParams *dst_cp, ChannelParams *src_cp, int filter)
unsigned int seq_index
Sequence index for high compression levels.
int ff_lpc_calc_coefs(LPCContext *s, const int32_t *samples, int blocksize, int min_order, int max_order, int precision, int32_t coefs[][MAX_LPC_ORDER], int *shift, enum FFLPCType lpc_type, int lpc_passes, int omethod, int min_shift, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
int32_t * major_inout_buffer
Buffer with all in/out data for one entire major frame interval.
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
uint8_t huff_lsbs
Size of residual suffix not encoded using VLC.
#define CODEBOOK_CHANGE_BITS
static void set_best_codebook(MLPEncodeContext *ctx)
static void input_to_sample_buffer(MLPEncodeContext *ctx)
unsigned int next_major_number_of_frames
static void write_frame_headers(MLPEncodeContext *ctx, uint8_t *frame_header, uint8_t *substream_headers, unsigned int length, int restart_frame, uint16_t substream_data_len[MAX_SUBSTREAMS])
Writes the access unit and substream headers to the bitstream.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
uint8_t count
number of matrices to apply
DecodingParams * decoding_params
uint8_t outch[MAX_MATRICES]
output channel for each matrix
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
#define MLP_MAX_LPC_SHIFT
int32_t * inout_buffer
Pointer to data currently being read from lavc or written to bitstream.
#define AV_CH_LAYOUT_2POINT1
channel
Use these values when setting the channel map with ebur128_set_channel().
#define MAX_SUBSTREAMS
Maximum number of substreams that can be decoded.
BestOffset best_offset[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS][NUM_CODEBOOKS]
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_YASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
static void set_filter_params(MLPEncodeContext *ctx, unsigned int channel, unsigned int filter, int clear_filter)
Determines the best filter parameters for the given data and writes the necessary information to the ...
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static void clear_decoding_params(MLPEncodeContext *ctx, DecodingParams decoding_params[MAX_SUBSTREAMS])
Clears a DecodingParams struct the way it should be after a restart header.
static int number_sbits(int number)
Calculates the smallest number of bits it takes to encode a given signed value in two's complement...
sample data coding information
int channels
number of audio channels
av_cold void ff_mlp_init_crc(void)
static unsigned int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, int restart_frame)
Writes an entire access unit to the bitstream.
static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb, int params_changed)
Writes decoding parameters to the bitstream.
static const double coeff[2][5]
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
ChannelParams * channel_params
static enum AVSampleFormat sample_fmts[]
int frame_number
Frame counter, set by libavcodec.
unsigned int one_sample_buffer_size
Number of samples*channel for one access unit.
uint8_t ch_modifier_thd1
channel modifier for TrueHD stream 1
static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
Applies the filter to the current samples, and saves the residual back into the samples buffer...
#define av_malloc_array(a, b)
#define SUBSTREAM_INFO_HIGH_RATE
static void rematrix_channels(MLPEncodeContext *ctx)
Rematrixes all channels using chosen coefficients.
char path[MAJOR_HEADER_INTERVAL+3]
static void no_codebook_bits_offset(MLPEncodeContext *ctx, unsigned int channel, int16_t offset, int32_t min, int32_t max, BestOffset *bo)
Determines the amount of bits needed to encode the samples using no codebooks and a specified offset...
int coded_peak_bitrate
peak bitrate for this major sync header
#define AV_CH_LAYOUT_MONO
static void determine_bits(MLPEncodeContext *ctx)
Determines the least amount of bits needed to encode the samples using any or no codebook.
This structure stores compressed data.
mode
Use these values in ebur128_init (or'ed).
static DecodingParams restart_decoding_params[MAX_SUBSTREAMS]
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static int compare_best_offset(BestOffset *prev, BestOffset *cur)
static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples, int is24)
Inputs data from the samples passed by lavc into the context, shifts them appropriately depending on ...
unsigned int frame_index
Index of current frame being encoded.
uint8_t ff_mlp_checksum8(const uint8_t *buf, unsigned int buf_size)
MLP uses checksums that seem to be based on the standard CRC algorithm, but are not (in implementatio...