1 /*
2 * The simplest AC-3 encoder
3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2006-2010 Justin Ruggles <justin.ruggles@gmail.com>
5 * Copyright (c) 2006-2010 Prakash Punnoor <prakash@punnoor.de>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * The simplest AC-3 encoder.
27 */
28
29 #include <stdint.h>
30
47
52
53 #define CMIXLEV_NUM_OPTIONS 3
56 };
57
58 #define SURMIXLEV_NUM_OPTIONS 3
61 };
62
63 #define EXTMIXLEV_NUM_OPTIONS 8
67 };
68
69
70 /**
71 * LUT for number of exponent groups.
72 * exponent_group_tab[coupling][exponent strategy-1][number of coefficients]
73 */
75
76
77 /**
78 * List of supported channel layouts.
79 */
99 0
100 };
101
102
103 /**
104 * LUT to select the bandwidth code based on the bit rate, sample rate, and
105 * number of full-bandwidth channels.
106 * bandwidth_tab[fbw_channels-1][sample rate code][bit rate code]
107 */
109 // 32 40 48 56 64 80 96 112 128 160 192 224 256 320 384 448 512 576 640
110
111 { { 0, 0, 0, 12, 16, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
112 { 0, 0, 0, 16, 20, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
113 { 0, 0, 0, 32, 40, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
114
115 { { 0, 0, 0, 0, 0, 0, 0, 20, 24, 32, 48, 48, 48, 48, 48, 48, 48, 48, 48 },
116 { 0, 0, 0, 0, 0, 0, 4, 24, 28, 36, 56, 56, 56, 56, 56, 56, 56, 56, 56 },
117 { 0, 0, 0, 0, 0, 0, 20, 44, 52, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60 } },
118
119 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 24, 32, 40, 48, 48, 48, 48, 48, 48 },
120 { 0, 0, 0, 0, 0, 0, 0, 0, 4, 20, 28, 36, 44, 56, 56, 56, 56, 56, 56 },
121 { 0, 0, 0, 0, 0, 0, 0, 0, 20, 40, 48, 60, 60, 60, 60, 60, 60, 60, 60 } },
122
123 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 32, 48, 48, 48, 48, 48, 48 },
124 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 28, 36, 56, 56, 56, 56, 56, 56 },
125 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 48, 60, 60, 60, 60, 60, 60, 60 } },
126
127 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 20, 32, 40, 48, 48, 48, 48 },
128 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 24, 36, 44, 56, 56, 56, 56 },
129 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 44, 60, 60, 60, 60, 60, 60 } }
130 };
131
132
133 /**
134 * LUT to select the coupling start band based on the bit rate, sample rate, and
135 * number of full-bandwidth channels. -1 = coupling off
136 * ac3_coupling_start_tab[channel_mode-2][sample rate code][bit rate code]
137 *
138 * TODO: more testing for optimal parameters.
139 * multi-channel tests at 44.1kHz and 32kHz.
140 */
142 // 32 40 48 56 64 80 96 112 128 160 192 224 256 320 384 448 512 576 640
143
144 // 2/0
145 { { 0, 0, 0, 0, 0, 0, 0, 1, 1, 7, 8, 11, 12, -1, -1, -1, -1, -1, -1 },
146 { 0, 0, 0, 0, 0, 0, 1, 3, 5, 7, 10, 12, 13, -1, -1, -1, -1, -1, -1 },
147 { 0, 0, 0, 0, 1, 2, 2, 9, 13, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
148
149 // 3/0
150 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
151 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
152 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
153
154 // 2/1 - untested
155 { { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
156 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 6, 9, 11, 12, 13, -1, -1, -1, -1 },
157 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
158
159 // 3/1
160 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
161 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
162 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
163
164 // 2/2 - untested
165 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
166 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 10, 11, 11, 12, 12, 14, -1 },
167 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
168
169 // 3/2
170 { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
171 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 8, 11, 12, 12, -1, -1 },
172 { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } },
173 };
174
175
176 /**
177 * Adjust the frame size to make the average bit rate match the target bit rate.
178 * This is only needed for 11025, 22050, and 44100 sample rates or any E-AC-3.
179 *
180 * @param s AC-3 encoder private context
181 */
183 {
187 }
192 }
193
194
195 /**
196 * Set the initial coupling strategy parameters prior to coupling analysis.
197 *
198 * @param s AC-3 encoder private context
199 */
201 {
203 int got_cpl_snr;
204 int num_cpl_blocks;
205
206 /* set coupling use flags for each block/channel */
207 /* TODO: turn coupling on/off and adjust start band based on bit usage */
212 }
213
214 /* enable coupling for each block if at least 2 channels have coupling
215 enabled for that block */
216 got_cpl_snr = 0;
217 num_cpl_blocks = 0;
229 }
230
232 if (blk) {
236 break;
237 }
238 }
239 }
241
242 if (!blk || (block->
cpl_in_use && !got_cpl_snr)) {
245 got_cpl_snr = 1;
246 } else {
248 }
249 }
250 if (!num_cpl_blocks)
252
253 /* set bandwidth for each channel */
259 else
261 }
262 }
263 }
264
265
266 /**
267 * Apply stereo rematrixing to coefficients based on rematrixing flags.
268 *
269 * @param s AC-3 encoder private context
270 */
272 {
273 int nb_coefs;
277
279 return;
280
287 if (flags[bnd]) {
290 for (i = start; i <
end; i++) {
295 }
296 }
297 }
298 }
299 }
300
301
302 /*
303 * Initialize exponent tables.
304 */
306 {
307 int expstr, i, grpsize;
308
310 grpsize = 3 << expstr;
311 for (i = 12; i < 256; i++) {
314 }
315 }
316 /* LFE */
318
319 if (CONFIG_EAC3_ENCODER && s->
eac3)
321 }
322
323
324 /*
325 * Extract exponents from the MDCT coefficients.
326 */
328 {
332
334 }
335
336
337 /**
338 * Exponent Difference Threshold.
339 * New exponents are sent if their SAD exceed this number.
340 */
341 #define EXP_DIFF_THRESHOLD 500
342
343 /**
344 * Table used to select exponent strategy based on exponent reuse block interval.
345 */
351 };
352
353 /*
354 * Calculate exponent strategies for all channels.
355 * Array arrangement is reversed to simplify the per-channel calculation.
356 */
358 {
360
361 for (ch = !s->
cpl_on; ch <= s->fbw_channels; ch++) {
364 int exp_diff;
365
366 /* estimate if the exponent variation & decide if they should be
367 reused in the next frame */
374 continue;
377 continue;
378 }
381 continue;
382 }
389 }
390
391 /* now select the encoding strategy type : if exponents are often
392 recoded, we use a coarse encoding */
393 blk = 0;
394 while (blk < s->num_blocks) {
395 blk1 = blk + 1;
396 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE)
397 blk1++;
399 blk = blk1;
400 }
401 }
407 }
408
409 /* for E-AC-3, determine frame exponent strategy */
410 if (CONFIG_EAC3_ENCODER && s->
eac3)
412 }
413
414
415 /**
416 * Update the exponents so that they are the ones the decoder will decode.
417 *
418 * @param[in,out] exp array of exponents for 1 block in 1 channel
419 * @param nb_exps number of exponents in active bandwidth
420 * @param exp_strategy exponent strategy for the block
421 * @param cpl indicates if the block is in the coupling channel
422 */
424 int cpl)
425 {
426 int nb_groups, i, k;
427
429
430 /* for each group, compute the minimum exponent */
431 switch(exp_strategy) {
433 for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
435 if (exp[k+1] < exp_min)
436 exp_min = exp[k+1];
437 exp[i-cpl] = exp_min;
438 k += 2;
439 }
440 break;
442 for (i = 1, k = 1-cpl; i <= nb_groups; i++) {
444 if (exp[k+1] < exp_min)
445 exp_min = exp[k+1];
446 if (exp[k+2] < exp_min)
447 exp_min = exp[k+2];
448 if (exp[k+3] < exp_min)
449 exp_min = exp[k+3];
450 exp[i-cpl] = exp_min;
451 k += 4;
452 }
453 break;
454 }
455
456 /* constraint for DC exponent */
457 if (!cpl && exp[0] > 15)
458 exp[0] = 15;
459
460 /* decrease the delta between each groups to within 2 so that they can be
461 differentially encoded */
462 for (i = 1; i <= nb_groups; i++)
463 exp[i] =
FFMIN(exp[i], exp[i-1] + 2);
464 i--;
465 while (--i >= 0)
466 exp[i] =
FFMIN(exp[i], exp[i+1] + 2);
467
468 if (cpl)
469 exp[-1] = exp[0] & ~1;
470
471 /* now we have the exponent values the decoder will see */
472 switch (exp_strategy) {
474 for (i = nb_groups, k = (nb_groups * 2)-cpl; i > 0; i--) {
476 exp[k--] = exp1;
477 exp[k--] = exp1;
478 }
479 break;
481 for (i = nb_groups, k = (nb_groups * 4)-cpl; i > 0; i--) {
482 exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i-cpl];
483 k -= 4;
484 }
485 break;
486 }
487 }
488
489
490 /*
491 * Encode exponents from original extracted form to what the decoder will see.
492 * This copies and groups exponents based on exponent strategy and reduces
493 * deltas between adjacent exponent groups so that they can be differentially
494 * encoded.
495 */
497 {
498 int blk, blk1, ch, cpl;
500 int nb_coefs, num_reuse_blocks;
501
502 for (ch = !s->
cpl_on; ch <= s->channels; ch++) {
505
507 blk = 0;
508 while (blk < s->num_blocks) {
512 blk++;
513 continue;
514 }
516 blk1 = blk + 1;
517
518 /* count the number of EXP_REUSE blocks after the current block
519 and set exponent reference block numbers */
521 while (blk1 < s->num_blocks && exp_strategy[blk1] ==
EXP_REUSE) {
523 blk1++;
524 }
525 num_reuse_blocks = blk1 - blk - 1;
526
527 /* for the EXP_REUSE case we select the min of the exponents */
530
532
534 blk = blk1;
535 }
536 }
537
538 /* reference block numbers have been changed, so reset ref_bap_set */
540 }
541
542
543 /*
544 * Count exponent bits based on bandwidth, coupling, and exponent strategies.
545 */
547 {
549 int nb_groups, bit_count;
550
551 bit_count = 0;
554 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
558
560 continue;
561
563 bit_count += 4 + (nb_groups * 7);
564 }
565 }
566
567 return bit_count;
568 }
569
570
571 /**
572 * Group exponents.
573 * 3 delta-encoded exponents are in each 7-bit group. The number of groups
574 * varies depending on exponent strategy and bandwidth.
575 *
576 * @param s AC-3 encoder private context
577 */
579 {
581 int group_size, nb_groups;
583 int delta0, delta1, delta2;
584 int exp0, exp1;
585
588 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
591 continue;
593 group_size = exp_strategy + (exp_strategy ==
EXP_D45);
596
597 /* DC exponent */
598 exp1 = *p++;
600
601 /* remaining exponents are delta encoded */
602 for (i = 1; i <= nb_groups; i++) {
603 /* merge three delta in one code */
604 exp0 = exp1;
605 exp1 = p[0];
606 p += group_size;
607 delta0 = exp1 - exp0 + 2;
609
610 exp0 = exp1;
611 exp1 = p[0];
612 p += group_size;
613 delta1 = exp1 - exp0 + 2;
615
616 exp0 = exp1;
617 exp1 = p[0];
618 p += group_size;
619 delta2 = exp1 - exp0 + 2;
621
622 block->
grouped_exp[ch][i] = ((delta0 * 5 + delta1) * 5) + delta2;
623 }
624 }
625 }
626 }
627
628
629 /**
630 * Calculate final exponents from the supplied MDCT coefficients and exponent shift.
631 * Extract exponents from MDCT coefficients, calculate exponent strategies,
632 * and encode final exponents.
633 *
634 * @param s AC-3 encoder private context
635 */
637 {
639
641
643
644 emms_c();
645 }
646
647
648 /*
649 * Count frame bits that are based solely on fixed parameters.
650 * This only has to be run once when the encoder is initialized.
651 */
653 {
654 static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
656 int frame_bits;
657
658 /* assumptions:
659 * no dynamic range codes
660 * bit allocation parameters do not change between blocks
661 * no delta bit allocation
662 * no skipped data
663 * no auxiliary data
664 * no E-AC-3 metadata
665 */
666
667 /* header */
668 frame_bits = 16; /* sync info */
670 /* bitstream info header */
671 frame_bits += 35;
672 frame_bits += 1 + 1;
674 frame_bits++;
675 frame_bits++;
676 /* audio frame header */
678 frame_bits += 2;
679 frame_bits += 10;
680 /* exponent strategy */
683 else
687 /* converter exponent strategy */
689 frame_bits++;
690 else
692 /* snr offsets */
693 frame_bits += 10;
694 /* block start info */
696 frame_bits++;
697 } else {
698 frame_bits += 49;
700 }
701
702 /* audio blocks */
705 /* block switch flags */
707
708 /* dither flags */
710 }
711
712 /* dynamic range */
713 frame_bits++;
714
715 /* spectral extension */
717 frame_bits++;
718
720 /* exponent strategy */
723 frame_bits++;
724
725 /* bit allocation params */
726 frame_bits++;
727 if (!blk)
728 frame_bits += 2 + 2 + 2 + 2 + 3;
729 }
730
731 /* converter snr offset */
733 frame_bits++;
734
736 /* delta bit allocation */
737 frame_bits++;
738
739 /* skipped data */
740 frame_bits++;
741 }
742 }
743
744 /* auxiliary data */
745 frame_bits++;
746
747 /* CRC */
748 frame_bits += 1 + 16;
749
751 }
752
753
754 /*
755 * Initialize bit allocation.
756 * Set default parameter codes and calculate parameter values.
757 */
759 {
760 int ch;
761
762 /* init default parameters */
768 for (ch = 0; ch <= s->
channels; ch++)
770
771 /* initial snr offset */
773
774 /* compute real values */
775 /* currently none of these values change during encoding, so we can just
776 set them once at initialization */
784
786 }
787
788
789 /*
790 * Count the bits used to encode the frame, minus exponents and mantissas.
791 * Bits based on fixed parameters have already been counted, so now we just
792 * have to add the bits based on parameters that change during encoding.
793 */
795 {
798 int frame_bits = 0;
799
800 /* header */
804 frame_bits += 2;
806 frame_bits += 6;
808 frame_bits += 6;
810 frame_bits += 1 + 1 + 2;
812 frame_bits++;
813 frame_bits++;
814 }
816 frame_bits += 3 + 1 + 1;
818 frame_bits += 2 + 2;
820 frame_bits += 2;
821 frame_bits++;
823 frame_bits += 5 + 2 + 1;
824 frame_bits++;
825 }
826 /* coupling */
828 frame_bits++;
831 frame_bits++;
833 frame_bits++;
834 }
835 }
836 /* coupling exponent strategy */
839 frame_bits += 5 * s->
cpl_on;
840 } else {
843 }
844 }
845 } else {
847 frame_bits += 7;
850 frame_bits += 14;
852 frame_bits += 14;
853 }
854 }
855
856 /* audio blocks */
859
860 /* coupling strategy */
862 frame_bits++;
865 frame_bits++;
868 frame_bits++;
872 frame_bits++;
873 frame_bits += 4 + 4;
875 frame_bits++;
876 else
878 }
879 }
880
881 /* coupling coordinates */
886 frame_bits++;
888 frame_bits += 2;
890 }
891 }
892 }
893 }
894
895 /* stereo rematrixing */
897 if (!s->
eac3 || blk > 0)
898 frame_bits++;
901 }
902
903 /* bandwidth codes & gain range */
907 frame_bits += 6;
908 frame_bits += 2;
909 }
910 }
911
912 /* coupling exponent strategy */
914 frame_bits += 2;
915
916 /* snr offsets and fast gain codes */
918 frame_bits++;
921 }
922
923 /* coupling leak info */
926 frame_bits++;
928 frame_bits += 3 + 3;
929 }
930 }
931
933 }
934
935
936 /*
937 * Calculate masking curve based on the final exponents.
938 * Also calculate the power spectral densities to use in future calculations.
939 */
941 {
943
946 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
947 /* We only need psd and mask for calculating bap.
948 Since we currently do not calculate bap when exponent
949 strategy is EXP_REUSE we do not need to calculate psd or mask. */
960 }
961 }
962 }
963 }
964
965
966 /*
967 * Ensure that bap for each block and channel point to the current bap_buffer.
968 * They may have been switched during the bit allocation search.
969 */
971 {
974
976 return;
977
979 for (ch = 0; ch <= s->
channels; ch++) {
983 }
985 }
986
987
988 /**
989 * Initialize mantissa counts.
990 * These are set so that they are padded to the next whole group size when bits
991 * are counted in compute_mantissa_size.
992 *
993 * @param[in,out] mant_cnt running counts for each bap value for each block
994 */
996 {
998
1000 memset(mant_cnt[blk], 0, sizeof(mant_cnt[blk]));
1001 mant_cnt[
blk][1] = mant_cnt[
blk][2] = 2;
1002 mant_cnt[
blk][4] = 1;
1003 }
1004 }
1005
1006
1007 /**
1008 * Update mantissa bit counts for all blocks in 1 channel in a given bandwidth
1009 * range.
1010 *
1011 * @param s AC-3 encoder private context
1012 * @param ch channel index
1013 * @param[in,out] mant_cnt running counts for each bap value for each block
1014 * @param start starting coefficient bin
1015 * @param end ending coefficient bin
1016 */
1020 {
1022
1026 continue;
1030 }
1031 }
1032
1033
1034 /*
1035 * Count the number of mantissa bits in the frame based on the bap values.
1036 */
1038 {
1039 int ch, max_end_freq;
1041
1043
1045 for (ch = !s->
cpl_enabled; ch <= s->channels; ch++)
1047 max_end_freq);
1048
1050 }
1051
1052
1053 /**
1054 * Run the bit allocation with a given SNR offset.
1055 * This calculates the bit allocation pointers that will be used to determine
1056 * the quantization of each mantissa.
1057 *
1058 * @param s AC-3 encoder private context
1059 * @param snr_offset SNR offset, 0 to 1023
1060 * @return the number of bits needed for mantissas if the given SNR offset is
1061 * is used.
1062 */
1064 {
1066
1067 snr_offset = (snr_offset - 240) << 2;
1068
1072
1073 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1074 /* Currently the only bit allocation parameters which vary across
1075 blocks within a frame are the exponent values. We can take
1076 advantage of that by reusing the bit allocation pointers
1077 whenever we reuse exponents. */
1083 }
1084 }
1085 }
1087 }
1088
1089
1090 /*
1091 * Constant bitrate bit allocation search.
1092 * Find the largest SNR offset that will allow data to fit in the frame.
1093 */
1095 {
1096 int ch;
1097 int bits_left;
1098 int snr_offset, snr_incr;
1099
1101 if (bits_left < 0)
1103
1105
1106 /* if previous frame SNR offset was 1023, check if current frame can also
1107 use SNR offset of 1023. if so, skip the search. */
1110 return 0;
1111 }
1112
1113 while (snr_offset >= 0 &&
1115 snr_offset -= 64;
1116 }
1117 if (snr_offset < 0)
1119
1121 for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
1122 while (snr_offset + snr_incr <= 1023 &&
1123 bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
1124 snr_offset += snr_incr;
1126 }
1127 }
1130
1132 for (ch = !s->
cpl_on; ch <= s->channels; ch++)
1134
1135 return 0;
1136 }
1137
1138
1139 /*
1140 * Perform bit allocation search.
1141 * Finds the SNR offset value that maximizes quality and fits in the specified
1142 * frame size. Output is the SNR offset and a set of bit allocation pointers
1143 * used to quantize the mantissas.
1144 */
1146 {
1148
1150
1152
1154 }
1155
1156
1157 /**
1158 * Symmetric quantization on 'levels' levels.
1159 *
1160 * @param c unquantized coefficient
1161 * @param e exponent
1162 * @param levels number of quantization levels
1163 * @return quantized coefficient
1164 */
1166 {
1167 int v = (((levels *
c) >> (24 - e)) + levels) >> 1;
1170 }
1171
1172
1173 /**
1174 * Asymmetric quantization on 2^qbits levels.
1175 *
1176 * @param c unquantized coefficient
1177 * @param e exponent
1178 * @param qbits number of quantization bits
1179 * @return quantized coefficient
1180 */
1182 {
1184
1185 c = (((c << e) >> (24 - qbits)) + 1) >> 1;
1186 m = (1 << (qbits-1));
1187 if (c >= m)
1188 c = m - 1;
1191 }
1192
1193
1194 /**
1195 * Quantize a set of mantissas for a single channel in a single block.
1196 *
1197 * @param s Mantissa count context
1198 * @param fixed_coef unquantized fixed-point coefficients
1199 * @param exp exponents
1200 * @param bap bit allocation pointer indices
1201 * @param[out] qmant quantized coefficients
1202 * @param start_freq starting coefficient bin
1203 * @param end_freq ending coefficient bin
1204 */
1207 int16_t *qmant, int start_freq,
1208 int end_freq)
1209 {
1210 int i;
1211
1212 for (i = start_freq; i < end_freq; i++) {
1213 int c = fixed_coef[i];
1214 int e = exp[i];
1216 if (v)
1217 switch (v) {
1218 case 1:
1221 case 0:
1225 break;
1226 case 1:
1229 v = 128;
1230 break;
1231 default:
1234 v = 128;
1235 break;
1236 }
1237 break;
1238 case 2:
1241 case 0:
1245 break;
1246 case 1:
1249 v = 128;
1250 break;
1251 default:
1254 v = 128;
1255 break;
1256 }
1257 break;
1258 case 3:
1260 break;
1261 case 4:
1264 case 0:
1268 break;
1269 default:
1272 v = 128;
1273 break;
1274 }
1275 break;
1276 case 5:
1278 break;
1279 case 14:
1281 break;
1282 case 15:
1284 break;
1285 default:
1287 break;
1288 }
1290 }
1291 }
1292
1293
1294 /**
1295 * Quantize mantissas using coefficients, exponents, and bit allocation pointers.
1296 *
1297 * @param s AC-3 encoder private context
1298 */
1300 {
1301 int blk, ch, ch0=0, got_cpl;
1302
1306
1308 for (ch = 1; ch <= s->
channels; ch++) {
1310 ch0 = ch - 1;
1312 got_cpl = 1;
1313 }
1319 ch = ch0;
1320 }
1321 }
1322 }
1323
1324
1325 /*
1326 * Write the AC-3 frame header to the output bitstream.
1327 */
1329 {
1331
1332 put_bits(&s->
pb, 16, 0x0b77);
/* frame header */
1333 put_bits(&s->
pb, 16, 0);
/* crc1: will be filled later */
1347 put_bits(&s->
pb, 1, 0);
/* no compression control word */
1353 }
1357 /* alternate bit stream syntax */
1365 }
1371 put_bits(&s->
pb, 9, 0);
/* xbsi2 and encinfo : reserved */
1372 }
1373 } else {
1376 }
1377 put_bits(&s->
pb, 1, 0);
/* no additional bit stream info */
1378 }
1379
1380
1381 /*
1382 * Write one audio block to the output bitstream.
1383 */
1385 {
1386 int ch, i, baie, bnd, got_cpl,
av_uninit(ch0);
1388
1389 /* block switching */
1393 }
1394
1395 /* dither flags */
1399 }
1400
1401 /* dynamic range codes */
1403
1404 /* spectral extension */
1407
1408 /* channel coupling */
1415 int start_sub, end_sub;
1417 put_bits(&s->
pb, 1, 0);
/* enhanced coupling */
1421 }
1423 put_bits(&s->
pb, 1, 0);
/* phase flags in use */
1428 /* coupling band structure */
1431 } else {
1432 for (bnd = start_sub+1; bnd < end_sub; bnd++)
1434 }
1435 }
1436 }
1437
1438 /* coupling coordinates */
1449 }
1450 }
1451 }
1452 }
1453 }
1454
1455 /* stereo rematrixing */
1457 if (!s->
eac3 || blk > 0)
1460 /* rematrixing flags */
1463 }
1464 }
1465
1466 /* exponent strategy */
1468 for (ch = !block->
cpl_in_use; ch <= s->fbw_channels; ch++)
1472 }
1473
1474 /* bandwidth */
1478 }
1479
1480 /* exponents */
1481 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1482 int nb_groups;
1483 int cpl = (ch ==
CPL_CH);
1484
1486 continue;
1487
1488 /* DC exponent */
1490
1491 /* exponent groups */
1493 for (i = 1; i <= nb_groups; i++)
1495
1496 /* gain range info */
1499 }
1500
1501 /* bit allocation info */
1503 baie = (blk == 0);
1505 if (baie) {
1511 }
1512 }
1513
1514 /* snr offset */
1519 for (ch = !block->
cpl_in_use; ch <= s->channels; ch++) {
1522 }
1523 }
1524 } else {
1525 put_bits(&s->
pb, 1, 0);
/* no converter snr offset */
1526 }
1527
1528 /* coupling leak */
1535 }
1536 }
1537
1539 put_bits(&s->
pb, 1, 0);
/* no delta bit allocation */
1540 put_bits(&s->
pb, 1, 0);
/* no data to skip */
1541 }
1542
1543 /* mantissas */
1545 for (ch = 1; ch <= s->
channels; ch++) {
1547
1549 ch0 = ch - 1;
1551 got_cpl = 1;
1552 }
1553 for (i = s->
start_freq[ch]; i < block->end_freq[ch]; i++) {
1554 q = block->
qmant[ch][i];
1556 switch (b) {
1557 case 0: break;
1558 case 1:
if (q != 128)
put_bits (&s->
pb, 5, q);
break;
1559 case 2:
if (q != 128)
put_bits (&s->
pb, 7, q);
break;
1561 case 4:
if (q != 128)
put_bits (&s->
pb, 7, q);
break;
1565 }
1566 }
1568 ch = ch0;
1569 }
1570 }
1571
1572
1573 /** CRC-16 Polynomial */
1574 #define CRC16_POLY ((1 << 0) | (1 << 2) | (1 << 15) | (1 << 16))
1575
1576
1578 {
1580
1581 c = 0;
1582 while (a) {
1583 if (a & 1)
1585 a = a >> 1;
1586 b = b << 1;
1587 if (b & (1 << 16))
1589 }
1591 }
1592
1593
1595 {
1597 r = 1;
1598 while (n) {
1599 if (n & 1)
1602 n >>= 1;
1603 }
1605 }
1606
1607
1608 /*
1609 * Fill the end of the frame with 0's and compute the two CRCs.
1610 */
1612 {
1614 int frame_size_58, pad_bytes, crc1, crc2_partial, crc2, crc_inv;
1616
1618
1619 /* pad the remainder of the frame with zeros */
1625 if (pad_bytes > 0)
1627
1629 /* compute crc2 */
1631 } else {
1632 /* compute crc1 */
1633 /* this is not so easy because it is at the beginning of the data... */
1638
1639 /* compute crc2 */
1640 crc2_partial =
av_crc(crc_ctx, 0, frame + frame_size_58,
1642 }
1644 /* ensure crc2 does not match sync word by flipping crcrsv bit if needed */
1645 if (crc2 == 0x770B) {
1648 }
1651 }
1652
1653
1654 /**
1655 * Write the frame to the output bitstream.
1656 *
1657 * @param s AC-3 encoder private context
1658 * @param frame output data buffer
1659 */
1661 {
1663
1665
1667
1670
1672 }
1673
1674
1676 {
1677 #ifdef DEBUG
1680 char strbuf[32];
1681
1683 case 6:
av_strlcpy(strbuf,
"AC-3 (alt syntax)", 32);
break;
1684 case 8:
av_strlcpy(strbuf,
"AC-3 (standard)", 32);
break;
1685 case 9:
av_strlcpy(strbuf,
"AC-3 (dnet half-rate)", 32);
break;
1686 case 10:
av_strlcpy(strbuf,
"AC-3 (dnet quater-rate)", 32);
break;
1687 case 16:
av_strlcpy(strbuf,
"E-AC-3 (enhanced)", 32);
break;
1688 default:
snprintf(strbuf, 32,
"ERROR");
1689 }
1693 av_dlog(avctx,
"channel_layout: %s\n", strbuf);
1699
1700 av_dlog(avctx,
"per_frame_metadata: %s\n",
1705 else
1706 av_dlog(avctx,
"center_mixlev: {not written}\n");
1710 else
1711 av_dlog(avctx,
"surround_mixlev: {not written}\n");
1719 }
1720 av_dlog(avctx,
"room_type: %s\n", strbuf);
1721 } else {
1722 av_dlog(avctx,
"mixing_level: {not written}\n");
1723 av_dlog(avctx,
"room_type: {not written}\n");
1724 }
1733 }
1734 av_dlog(avctx,
"dsur_mode: %s\n", strbuf);
1735 } else {
1736 av_dlog(avctx,
"dsur_mode: {not written}\n");
1737 }
1739
1747 }
1748 av_dlog(avctx,
"dmix_mode: %s\n", strbuf);
1749 av_dlog(avctx,
"ltrt_cmixlev: %0.3f (%d)\n",
1751 av_dlog(avctx,
"ltrt_surmixlev: %0.3f (%d)\n",
1753 av_dlog(avctx,
"loro_cmixlev: %0.3f (%d)\n",
1755 av_dlog(avctx,
"loro_surmixlev: %0.3f (%d)\n",
1757 } else {
1758 av_dlog(avctx,
"extended bitstream info 1: {not written}\n");
1759 }
1766 }
1767 av_dlog(avctx,
"dsurex_mode: %s\n", strbuf);
1773 }
1774 av_dlog(avctx,
"dheadphone_mode: %s\n", strbuf);
1775
1780 }
1781 av_dlog(avctx,
"ad_conv_type: %s\n", strbuf);
1782 } else {
1783 av_dlog(avctx,
"extended bitstream info 2: {not written}\n");
1784 }
1785 }
1786 #endif
1787 }
1788
1789
1790 #define FLT_OPTION_THRESHOLD 0.01
1791
1793 {
1794 int i;
1795
1796 for (i = 0; i < v_list_size; i++) {
1799 break;
1800 }
1801 if (i == v_list_size)
1802 return -1;
1803
1804 return i;
1805 }
1806
1807
1809 float *opt_param, const float *list,
1810 int list_size, int default_value, int min_value,
1811 int *ctx_param)
1812 {
1814 if (mixlev < min_value) {
1815 mixlev = default_value;
1816 if (*opt_param >= 0.0) {
1818 "default value: %0.3f\n", opt_name, list[mixlev]);
1819 }
1820 }
1821 *opt_param = list[mixlev];
1822 *ctx_param = mixlev;
1823 }
1824
1825
1826 /**
1827 * Validate metadata options as set by AVOption system.
1828 * These values can optionally be changed per-frame.
1829 *
1830 * @param s AC-3 encoder private context
1831 */
1833 {
1836
1842
1843 /* determine mixing metadata / xbsi1 use */
1847 }
1852 }
1857 }
1858
1860 /* determine info metadata use */
1874 }
1875 } else {
1876 /* determine audio production info use */
1879
1880 /* determine xbsi2 use */
1887 }
1888
1889 /* validate AC-3 mixing levels */
1895 }
1900 }
1901 }
1902
1903 /* validate extended bsi 1 / mixing metadata */
1905 /* default preferred stereo downmix */
1909 /* validate Lt/Rt center mix level */
1914 /* validate Lo/Ro center mix level */
1919 }
1921 /* validate Lt/Rt surround mix level */
1926 /* validate Lo/Ro surround mix level */
1931 }
1932 }
1933
1934 /* validate audio service type / channels combination */
1942 "specified number of channels\n");
1944 }
1945
1946 /* validate extended bsi 2 / info metadata */
1948 /* default dolby headphone mode */
1951 /* default dolby surround ex mode */
1954 /* default A/D converter type */
1957 }
1958
1959 /* copyright & original defaults */
1961 /* default copyright */
1964 /* default original */
1967 }
1968
1969 /* dolby surround mode default */
1973 }
1974
1975 /* validate audio production info */
1979 "room_type is set\n");
1981 }
1984 "80dB and 111dB\n");
1986 }
1987 /* default room type */
1990 }
1991
1992 /* set bitstream id for alternate bitstream syntax */
1995 static int warn_once = 1;
1996 if (warn_once) {
1998 "not compatible with reduced samplerates. writing of "
1999 "extended bitstream information will be disabled.\n");
2000 warn_once = 0;
2001 }
2002 } else {
2004 }
2005 }
2006
2007 return 0;
2008 }
2009
2010
2011 /**
2012 * Finalize encoding and free any memory allocated by the encoder.
2013 *
2014 * @param avctx Codec context
2015 */
2017 {
2020
2023 for (ch = 0; ch < s->
channels; ch++)
2051 }
2052
2054
2055 return 0;
2056 }
2057
2058
2059 /*
2060 * Set channel information during initialization.
2061 */
2063 uint64_t *channel_layout)
2064 {
2065 int ch_layout;
2066
2069 if (*channel_layout > 0x7FF)
2071 ch_layout = *channel_layout;
2072 if (!ch_layout)
2074
2081
2082 switch (ch_layout) {
2088 case AV_CH_LAYOUT_QUAD:
2090 case AV_CH_LAYOUT_5POINT0:
2092 default:
2094 }
2097
2099 *channel_layout = ch_layout;
2102
2103 return 0;
2104 }
2105
2106
2108 {
2111
2112 /* validate channel layout */
2115 "encoder will guess the layout, but it "
2116 "might be incorrect.\n");
2117 }
2119 if (ret) {
2122 }
2123
2124 /* validate sample rate */
2125 /* note: max_sr could be changed from 2 to 5 for E-AC-3 once we find a
2126 decoder that supports half sample rate so we can validate that
2127 the generated files are correct. */
2128 max_sr = s->
eac3 ? 2 : 8;
2129 for (i = 0; i <= max_sr; i++) {
2131 break;
2132 }
2133 if (i > max_sr) {
2136 }
2141
2142 /* select a default bit rate if not set by the user */
2145 case 1: avctx->
bit_rate = 96000;
break;
2146 case 2: avctx->
bit_rate = 192000;
break;
2147 case 3: avctx->
bit_rate = 320000;
break;
2148 case 4: avctx->
bit_rate = 384000;
break;
2149 case 5: avctx->
bit_rate = 448000;
break;
2150 }
2151 }
2152
2153 /* validate bit rate */
2155 int max_br, min_br, wpf, min_br_dist, min_br_code;
2156 int num_blks_code, num_blocks, frame_samples;
2157
2158 /* calculate min/max bitrate */
2159 /* TODO: More testing with 3 and 2 blocks. All E-AC-3 samples I've
2160 found use either 6 blocks or 1 block, even though 2 or 3 blocks
2161 would work as far as the bit rate is concerned. */
2162 for (num_blks_code = 3; num_blks_code >= 0; num_blks_code--) {
2163 num_blocks = ((int[]){ 1, 2, 3, 6 })[num_blks_code];
2165 max_br = 2048 * s->
sample_rate / frame_samples * 16;
2166 min_br = ((s->
sample_rate + (frame_samples-1)) / frame_samples) * 16;
2168 break;
2169 }
2172 "for this sample rate\n", min_br, max_br);
2174 }
2177
2178 /* calculate words-per-frame for the selected bitrate */
2181
2182 /* find the closest AC-3 bitrate code to the selected bitrate.
2183 this is needed for lookup tables for bandwidth and coupling
2184 parameter selection */
2185 min_br_code = -1;
2186 min_br_dist = INT_MAX;
2187 for (i = 0; i < 19; i++) {
2189 if (br_dist < min_br_dist) {
2190 min_br_dist = br_dist;
2191 min_br_code = i;
2192 }
2193 }
2194
2195 /* make sure the minimum frame size is below the average frame size */
2198 wpf--;
2200 } else {
2201 int best_br = 0, best_code = 0, best_diff = INT_MAX;
2202 for (i = 0; i < 19; i++) {
2205 if (diff < best_diff) {
2206 best_br = br;
2207 best_code = i;
2209 }
2210 if (!best_diff)
2211 break;
2212 }
2218 }
2221
2222 /* validate cutoff */
2226 }
2230
2232 if (ret)
2234
2237
2240
2241 return 0;
2242 }
2243
2244
2245 /*
2246 * Set bandwidth for all channels.
2247 * The user can optionally supply a cutoff frequency. Otherwise an appropriate
2248 * default value will be used.
2249 */
2251 {
2253
2255 /* calculate bandwidth based on user-specified cutoff frequency */
2256 int fbw_coeffs;
2259 } else {
2260 /* use default bandwidth setting */
2262 }
2263
2264 /* set number of coefficients for each channel */
2269 }
2270 /* LFE channel always has 7 coefs */
2275 }
2276
2277 /* initialize coupling strategy */
2281 } else {
2283 if (cpl_start < 0) {
2286 else
2287 cpl_start = 15;
2288 }
2289 }
2290 }
2292 int i, cpl_start_band, cpl_end_band;
2294
2296 cpl_start_band = av_clip(cpl_start, 0,
FFMIN(cpl_end_band-1, 15));
2297
2299
2301 *cpl_band_sizes = 12;
2302 for (i = cpl_start_band + 1; i < cpl_end_band; i++) {
2304 *cpl_band_sizes += 12;
2305 } else {
2307 cpl_band_sizes++;
2308 *cpl_band_sizes = 12;
2309 }
2310 }
2311
2316 }
2317 }
2318
2319
2321 {
2324 int channels = s->
channels + 1;
/* includes coupling channel */
2325 int channel_blocks = channels * s->
num_blocks;
2327
2329 goto alloc_fail;
2330
2354 }
2358 alloc_fail);
2360 alloc_fail);
2362 alloc_fail);
2364 alloc_fail);
2366 alloc_fail);
2368 alloc_fail);
2370 alloc_fail);
2373 alloc_fail);
2375 alloc_fail);
2376 }
2377
2378 for (ch = 0; ch < channels; ch++) {
2379 /* arrangement: block, channel, coeff */
2388 }
2389
2390 /* arrangement: channel, block, coeff */
2393 }
2394 }
2395
2403 for (ch = 0; ch < channels; ch++)
2405 }
2406 } else {
2411 for (ch = 0; ch < channels; ch++)
2413 }
2414 }
2415
2416 return 0;
2417 alloc_fail:
2419 }
2420
2421
2423 {
2425 int ret, frame_size_58;
2426
2428
2430
2432
2434 if (ret)
2436
2439
2443
2446
2447 /* calculate crc_inv for both possible frame sizes */
2453 }
2454
2455 /* set function pointers */
2460 } else if (CONFIG_AC3_ENCODER || CONFIG_EAC3_ENCODER) {
2464 }
2465 if (CONFIG_EAC3_ENCODER && s->
eac3)
2467 else
2469
2471
2473
2475
2477 if (ret)
2478 goto init_fail;
2479
2481 if (ret)
2482 goto init_fail;
2483
2487
2489
2490 return 0;
2491 init_fail:
2494 }