1 /*
2 * FLAC audio encoder
3 * Copyright (c) 2006 Justin Ruggles <justin.ruggles@gmail.com>
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
28
38
39 #define FLAC_SUBFRAME_CONSTANT 0
40 #define FLAC_SUBFRAME_VERBATIM 1
41 #define FLAC_SUBFRAME_FIXED 8
42 #define FLAC_SUBFRAME_LPC 32
43
44 #define MAX_FIXED_ORDER 4
45 #define MAX_PARTITION_ORDER 8
46 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
47 #define MAX_LPC_PRECISION 15
48 #define MIN_LPC_SHIFT 0
49 #define MAX_LPC_SHIFT 15
50
54 };
55
71
77
86
90
94
104
128
132
133
134 /**
135 * Write streaminfo metadata block to byte array.
136 */
138 {
140
143
144 /* streaminfo metadata block */
151 put_bits(&pb, 5,
s->avctx->bits_per_raw_sample - 1);
152 /* write 36-bit sample count in 2 put_bits() calls */
153 put_bits(&pb, 24, (
s->sample_count & 0xFFFFFF000LL) >> 12);
154 put_bits(&pb, 12,
s->sample_count & 0x000000FFFLL);
156 memcpy(&
header[18],
s->md5sum, 16);
157 }
158
159
160 /**
161 * Calculate an estimate for the maximum frame size based on verbatim mode.
162 * @param blocksize block size, in samples
163 * @param ch number of channels
164 * @param bps bits-per-sample
165 */
167 {
168 /* Technically, there is no limit to FLAC frame size, but an encoder
169 should not write a frame that is larger than if verbatim encoding mode
170 were to be used. */
171
172 int count;
173
174 count = 16; /* frame header */
175 count += ch * ((7+
bps+7)/8);
/* subframe headers */
176 if (ch == 2) {
177 /* for stereo, need to account for using decorrelation */
178 count += (( 2*
bps+1) * blocksize + 7) / 8;
179 } else {
180 count += ( ch*
bps * blocksize + 7) / 8;
181 }
182 count += 2; /* frame footer */
183
184 return count;
185 }
186
187
188 /**
189 * Set blocksize based on samplerate.
190 * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
191 */
193 {
195 int target;
196 int blocksize;
197
200 target = (samplerate * block_time_ms) / 1000;
201 for (
i = 0;
i < 16;
i++) {
205 }
206 }
207 return blocksize;
208 }
209
210
212 {
215
217
221 break;
224 break;
226 av_log(avctx,
AV_LOG_DEBUG,
" lpc type: Levinson-Durbin recursion with Welch window\n");
227 break;
231 break;
232 }
233
236
240 break;
243 break;
246 break;
249 break;
252 break;
255 break;
256 }
257
258
261
263
266 }
267
268
270 {
275 uint8_t *streaminfo;
276
278
283 break;
292 "encoding as 24 bits-per-sample, more is considered "
293 "experimental. Add -strict experimental if you want "
294 "to encode more than 24 bits-per-sample\n");
297 } else {
300 }
301 break;
302 }
303
308 }
310
311 /* find samplerate in table */
312 if (freq < 1)
314 for (
i = 1;
i < 12;
i++) {
319 break;
320 }
321 }
322 /* if not in table, samplerate is non-standard */
324 if (freq % 1000 == 0 && freq < 255000) {
326 s->sr_code[1] = freq / 1000;
327 } else if (freq % 10 == 0 && freq < 655350) {
329 s->sr_code[1] = freq / 10;
330 } else if (freq < 65535) {
332 s->sr_code[1] = freq;
333 } else if (freq < 1048576) {
336 } else {
339 }
340 s->samplerate = freq;
341 }
342
343 /* set compression option defaults based on avctx->compression_level */
345 s->options.compression_level = 5;
346 else
348
349 level =
s->options.compression_level;
352 s->options.compression_level);
354 }
355
356 s->options.block_time_ms = ((
int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[
level];
357
364
365 if (
s->options.min_prediction_order < 0)
366 s->options.min_prediction_order = ((
int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[
level];
367 if (
s->options.max_prediction_order < 0)
368 s->options.max_prediction_order = ((
int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[
level];
369
370 if (
s->options.prediction_order_method < 0)
376
377 if (
s->options.min_partition_order >
s->options.max_partition_order) {
379 s->options.min_partition_order,
s->options.max_partition_order);
381 }
382 if (
s->options.min_partition_order < 0)
383 s->options.min_partition_order = ((
int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[
level];
384 if (
s->options.max_partition_order < 0)
385 s->options.max_partition_order = ((
int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[
level];
386
388 s->options.min_prediction_order = 0;
389 s->options.max_prediction_order = 0;
393 "invalid min prediction order %d, clamped to %d\n",
396 }
399 "invalid max prediction order %d, clamped to %d\n",
402 }
403 }
404
405 if (
s->options.max_prediction_order <
s->options.min_prediction_order) {
407 s->options.min_prediction_order,
s->options.max_prediction_order);
409 }
410
417 }
418 } else {
420 }
421 s->max_blocksize =
s->avctx->frame_size;
422
423 /* set maximum encoded frame size in verbatim mode */
426 s->avctx->bits_per_raw_sample);
427
428 /* initialize MD5 context */
433
435 if (!streaminfo)
440
442 s->min_framesize =
s->max_framesize;
443
457 "output stream will have incorrect "
458 "channel layout.\n");
459 } else {
461 "will use Flac channel layout for "
463 }
464 }
465
468
471
473
475 }
476
477
479 {
482
484
485 for (
i = 0;
i < 16;
i++) {
489 frame->bs_code[1] = 0;
490 break;
491 }
492 }
494 frame->blocksize = nb_samples;
495 if (
frame->blocksize <= 256) {
496 frame->bs_code[0] = 6;
498 } else {
499 frame->bs_code[0] = 7;
501 }
502 }
503
504 for (ch = 0; ch <
s->channels; ch++) {
506
508 sub->
obits =
s->avctx->bits_per_raw_sample;
509
512 else
514 }
515
516 frame->verbatim_only = 0;
517 }
518
519
520 /**
521 * Copy channel-interleaved input samples into separate subframes.
522 */
524 {
528 s->avctx->bits_per_raw_sample;
529
530 #define COPY_SAMPLES(bits) do { \
531 const int ## bits ## _t *samples0 = samples; \
532 frame = &s->frame; \
533 for (i = 0, j = 0; i < frame->blocksize; i++) \
534 for (ch = 0; ch < s->channels; ch++, j++) \
535 frame->subframes[ch].samples[i] = samples0[j] >> shift; \
536 } while (0)
537
540 else
542 }
543
544
546 {
548 uint64_t count = 0;
549
550 for (
i = 0;
i < n;
i++) {
551 unsigned v = ((unsigned)(res[
i]) << 1) ^ (res[
i] >> 31);
552 count += (v >> k) + 1 + k;
553 }
554 return count;
555 }
556
557
559 int pred_order)
560 {
561 int p, porder, psize;
563 uint64_t count = 0;
564
565 /* subframe header */
566 count += 8;
567
570
571 /* subframe */
575 count +=
s->frame.blocksize * sub->
obits;
576 } else {
577 /* warm-up samples */
578 count += pred_order * sub->
obits;
579
580 /* LPC coefficients */
582 count += 4 + 5 + pred_order *
s->options.lpc_coeff_precision;
583
584 /* rice-encoded block */
585 count += 2;
586
587 /* partition order */
589 psize =
s->frame.blocksize >> porder;
590 count += 4;
591
592 /* residual */
594 part_end = psize;
595 for (p = 0; p < 1 << porder; p++) {
600 part_end =
FFMIN(
s->frame.blocksize, part_end + psize);
601 }
602 }
603
604 return count;
605 }
606
607
608 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
609
610 /**
611 * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
612 */
614 {
615 int k;
616 uint64_t sum2;
617
618 if (sum <= n >> 1)
619 return 0;
620 sum2 = sum - (n >> 1);
622 return FFMIN(k, max_param);
623 }
624
626 {
627 int bestk = 0;
629 int k;
630
631 for (k = 0; k <= max_param; k++) {
633 if (
bits < bestbits) {
635 bestk = k;
636 }
637 }
638
639 return bestk;
640 }
641
644 int n, int pred_order, int max_param, int exact)
645 {
647 int k, cnt, part;
648 uint64_t all_bits;
649
650 part = (1 << porder);
651 all_bits = 4 * part;
652
653 cnt = (n >> porder) - pred_order;
654 for (
i = 0;
i < part;
i++) {
655 if (exact) {
657 all_bits += sums[k][
i];
658 } else {
661 }
663 cnt = n >> porder;
664 }
665
667
668 return all_bits;
669 }
670
671
674 {
676 int parts;
677 const uint32_t *res, *res_end;
678
679 /* sums for highest level */
680 parts = (1 << pmax);
681
682 for (k = 0; k <= kmax; k++) {
683 res = &
data[pred_order];
684 res_end = &
data[n >> pmax];
685 for (
i = 0;
i < parts;
i++) {
686 if (kmax) {
687 uint64_t sum = (1LL + k) * (res_end - res);
688 while (res < res_end)
689 sum += *(res++) >> k;
691 } else {
692 uint64_t sum = 0;
693 while (res < res_end)
694 sum += *(res++);
696 }
697 res_end += n >> pmax;
698 }
699 }
700 }
701
703 {
705 int parts = (1 <<
level);
706 for (
i = 0;
i < parts;
i++) {
707 for (k=0; k<=kmax; k++)
708 sums[k][
i] = sums[k][2*
i] + sums[k][2*
i+1];
709 }
710 }
711
715 int pmin, int pmax,
716 const int32_t *
data,
int n,
int pred_order,
int exact)
717 {
720 int opt_porder;
723
727
729
730 for (
i = pred_order;
i < n;
i++)
731 udata[
i] = ((
unsigned)(
data[
i]) << 1) ^ (
data[
i] >> 31);
732
733 calc_sum_top(pmax, exact ? kmax : 0, udata, n, pred_order, sums);
734
735 opt_porder = pmin;
736 bits[pmin] = UINT32_MAX;
739 if (
bits[
i] <
bits[opt_porder] || pmax == pmin) {
741 *rc = tmp_rc;
742 }
744 break;
746 }
747
748 return bits[opt_porder];
749 }
750
751
753 {
755 if (order > 0)
757 return porder;
758 }
759
760
763 {
765 s->frame.blocksize, pred_order);
767 s->frame.blocksize, pred_order);
768
771 bits += 4 + 5 + pred_order *
s->options.lpc_coeff_precision;
773 s->frame.blocksize, pred_order,
s->options.exact_rice_parameters);
775 }
776
777
779 int order)
780 {
782
783 for (
i = 0;
i < order;
i++)
785
786 if (order == 0) {
787 for (
i = order;
i < n;
i++)
789 } else if (order == 1) {
790 for (
i = order;
i < n;
i++)
791 res[
i] = smp[
i] - smp[
i-1];
792 } else if (order == 2) {
793 int a = smp[order-1] - smp[order-2];
794 for (
i = order;
i < n;
i += 2) {
795 int b = smp[
i ] - smp[
i-1];
797 a = smp[
i+1] - smp[
i ];
799 }
800 } else if (order == 3) {
801 int a = smp[order-1] - smp[order-2];
802 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
803 for (
i = order;
i < n;
i += 2) {
804 int b = smp[
i ] - smp[
i-1];
807 a = smp[
i+1] - smp[
i ];
810 }
811 } else {
812 int a = smp[order-1] - smp[order-2];
813 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
814 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
815 for (
i = order;
i < n;
i += 2) {
816 int b = smp[
i ] - smp[
i-1];
820 a = smp[
i+1] - smp[
i ];
824 }
825 }
826 }
827
828
829 /* These four functions check for every residual whether it can be
830 * contained in <INT32_MIN,INT32_MAX]. In case it doesn't, the
831 * function that called this function has to try something else.
832 * Each function is duplicated, once for int32_t input, once for
833 * int64_t input */
834 #define ENCODE_RESIDUAL_FIXED_WITH_RESIDUAL_LIMIT() \
835 { \
836 for (int i = 0; i < order; i++) \
837 res[i] = smp[i]; \
838 if (order == 0) { \
839 for (int i = order; i < n; i++) { \
840 if (smp[i] == INT32_MIN) \
841 return 1; \
842 res[i] = smp[i]; \
843 } \
844 } else if (order == 1) { \
845 for (int i = order; i < n; i++) { \
846 int64_t res64 = (int64_t)smp[i] - smp[i-1]; \
847 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
848 return 1; \
849 res[i] = res64; \
850 } \
851 } else if (order == 2) { \
852 for (int i = order; i < n; i++) { \
853 int64_t res64 = (int64_t)smp[i] - 2*(int64_t)smp[i-1] + smp[i-2]; \
854 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
855 return 1; \
856 res[i] = res64; \
857 } \
858 } else if (order == 3) { \
859 for (int i = order; i < n; i++) { \
860 int64_t res64 = (int64_t)smp[i] - 3*(int64_t)smp[i-1] + 3*(int64_t)smp[i-2] - smp[i-3]; \
861 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
862 return 1; \
863 res[i] = res64; \
864 } \
865 } else { \
866 for (int i = order; i < n; i++) { \
867 int64_t res64 = (int64_t)smp[i] - 4*(int64_t)smp[i-1] + 6*(int64_t)smp[i-2] - 4*(int64_t)smp[i-3] + smp[i-4]; \
868 if (res64 <= INT32_MIN || res64 > INT32_MAX) \
869 return 1; \
870 res[i] = res64; \
871 } \
872 } \
873 return 0; \
874 }
875
877 int n, int order)
878 {
880 }
881
882
884 int n, int order)
885 {
887 }
888
889 #define LPC_ENCODE_WITH_RESIDUAL_LIMIT() \
890 { \
891 for (int i = 0; i < order; i++) \
892 res[i] = smp[i]; \
893 for (int i = order; i < len; i++) { \
894 int64_t p = 0, tmp; \
895 for (int j = 0; j < order; j++) \
896 p += (int64_t)coefs[j]*smp[(i-1)-j]; \
897 p >>= shift; \
898 tmp = smp[i] - p; \
899 if (tmp <= INT32_MIN || tmp > INT32_MAX) \
900 return 1; \
901 res[i] = tmp; \
902 } \
903 return 0; \
904 }
905
908 {
910 }
911
914 {
916 }
917
922 {
923 uint64_t max_residual_value = 0;
925 /* This calculates the max size of any residual with the current
926 * predictor, so we know whether we need to check the residual */
927 for (
int i = 0;
i < order;
i++)
928 max_residual_value +=
FFABS(max_sample_value * coefs[
i]);
929 max_residual_value >>=
shift;
930 max_residual_value += max_sample_value;
933 return 1;
934 } else if (max_residual_value > INT32_MAX) {
936 return 1;
937 }
else if (
bps +
s->options.lpc_coeff_precision +
av_log2(order) <= 32) {
938 s->flac_dsp.lpc16_encode(res, smp,
len, order, coefs,
shift);
939 } else {
940 s->flac_dsp.lpc32_encode(res, smp,
len, order, coefs,
shift);
941 }
942 return 0;
943 }
944
945 #define DEFAULT_TO_VERBATIM() \
946 { \
947 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM; \
948 if (sub->obits <= 32) \
949 memcpy(res, smp, n * sizeof(int32_t)); \
950 return subframe_count_exact(s, sub, 0); \
951 }
952
954 {
956 int min_order, max_order, opt_order, omethod;
963
965 sub = &
frame->subframes[ch];
968 smp_33bps =
frame->samples_33bps;
969 n =
frame->blocksize;
970
971 /* CONSTANT */
972 if (sub->
obits > 32) {
973 for (
i = 1;
i < n;
i++)
974 if(smp_33bps[
i] != smp_33bps[0])
975 break;
979 }
980 } else {
981 for (
i = 1;
i < n;
i++)
983 break;
986 res[0] = smp[0];
988 }
989 }
990
991 /* VERBATIM */
992 if (
frame->verbatim_only || n < 5) {
994 }
995
996 min_order =
s->options.min_prediction_order;
997 max_order =
s->options.max_prediction_order;
998 omethod =
s->options.prediction_order_method;
999
1000 /* FIXED */
1007 opt_order = 0;
1008 bits[0] = UINT32_MAX;
1009 for (
i = min_order;
i <= max_order;
i++) {
1010 if (sub->
obits == 33) {
1012 continue;
1013 }
else if (sub->
obits +
i >= 32) {
1015 continue;
1016 } else
1021 }
1022 if (opt_order == 0 &&
bits[0] == UINT32_MAX) {
1023 /* No predictor found with residuals within <INT32_MIN,INT32_MAX],
1024 * so encode a verbatim subframe instead */
1026 }
1027 sub->
order = opt_order;
1029 if (sub->
order != max_order) {
1030 if (sub->
obits == 33)
1032 else if (sub->
obits +
i >= 32)
1034 else
1037 }
1039 }
1040
1041 /* LPC */
1043 if (sub->
obits == 33)
1044 /* As ff_lpc_calc_coefs is shared with other codecs and the LSB
1045 * probably isn't predictable anyway, throw away LSB for analysis
1046 * so it fits 32 bit int and existing function can be used
1047 * unmodified */
1048 for (
i = 0;
i < n;
i++)
1049 smp[
i] = smp_33bps[
i] >> 1;
1050
1052 s->options.lpc_coeff_precision, coefs,
shift,
s->options.lpc_type,
1053 s->options.lpc_passes, omethod,
1055
1059 int levels = 1 << omethod;
1061 int order = -1;
1062 int opt_index = levels-1;
1063 opt_order = max_order-1;
1064 bits[opt_index] = UINT32_MAX;
1065 for (
i = levels-1;
i >= 0;
i--) {
1066 int last_order = order;
1067 order = min_order + (((max_order-min_order+1) * (
i+1)) / levels)-1;
1068 order =
av_clip(order, min_order - 1, max_order - 1);
1069 if (order == last_order)
1070 continue;
1072 continue;
1076 opt_order = order;
1077 }
1078 }
1079 opt_order++;
1081 // brute-force optimal order search
1083 opt_order = 0;
1084 bits[0] = UINT32_MAX;
1085 for (
i = min_order-1;
i < max_order;
i++) {
1087 continue;
1091 }
1092 opt_order++;
1096
1097 opt_order = min_order - 1 + (max_order-min_order)/3;
1099
1101 int last = opt_order;
1103 if (i < min_order-1 || i >= max_order ||
bits[
i] < UINT32_MAX)
1104 continue;
1106 continue;
1110 }
1111 }
1112 opt_order++;
1113 }
1114
1115 if (
s->options.multi_dim_quant) {
1116 int allsteps = 1;
1117 int i,
step, improved;
1118 int64_t best_score = INT64_MAX;
1120
1121 qmax = (1 << (
s->options.lpc_coeff_precision - 1)) - 1;
1122
1123 for (
i=0;
i<opt_order;
i++)
1124 allsteps *= 3;
1125
1126 do {
1127 improved = 0;
1132 int diffsum = 0;
1133
1134 for (
i=0;
i<opt_order;
i++) {
1135 int diff = ((
tmp + 1) % 3) - 1;
1136 lpc_try[
i] =
av_clip(coefs[opt_order - 1][
i] +
diff, -qmax, qmax);
1139 }
1140 if (diffsum >8)
1141 continue;
1142
1144 continue;
1146 if (score < best_score) {
1147 best_score = score;
1148 memcpy(coefs[opt_order-1], lpc_try, sizeof(*coefs));
1149 improved=1;
1150 }
1151 }
1152 } while(improved);
1153 }
1154
1155 sub->
order = opt_order;
1160
1162 /* No predictor found with residuals within <INT32_MIN,INT32_MAX],
1163 * so encode a verbatim subframe instead */
1165 }
1166
1168
1170 }
1171
1172
1174 {
1176 int count;
1177
1178 /*
1179 <14> Sync code
1180 <1> Reserved
1181 <1> Blocking strategy
1182 <4> Block size in inter-channel samples
1183 <4> Sample rate
1184 <4> Channel assignment
1185 <3> Sample size in bits
1186 <1> Reserved
1187 */
1188 count = 32;
1189
1190 /* coded frame number */
1192
1193 /* explicit block size */
1194 if (
s->frame.bs_code[0] == 6)
1195 count += 8;
1196 else if (
s->frame.bs_code[0] == 7)
1197 count += 16;
1198
1199 /* explicit sample rate */
1200 count += ((
s->sr_code[0] == 12) + (
s->sr_code[0] > 12) * 2) * 8;
1201
1202 /* frame header CRC-8 */
1203 count += 8;
1204
1205 return count;
1206 }
1207
1208
1210 {
1211 int ch;
1212 uint64_t count;
1213
1215
1216 for (ch = 0; ch <
s->channels; ch++)
1218
1219 count += (8 - (count & 7)) & 7; // byte alignment
1220 count += 16; // CRC-16
1221
1222 count >>= 3;
1223 if (count > INT_MAX)
1225 return count;
1226 }
1227
1228
1230 {
1231 int ch,
i, wasted_bits;
1232
1233 for (ch = 0; ch <
s->channels; ch++) {
1235
1236 if (sub->
obits > 32) {
1238 for (
i = 0;
i <
s->frame.blocksize;
i++) {
1239 v |=
s->frame.samples_33bps[
i];
1240 if (v & 1)
1241 break;
1242 }
1243
1244 if (!v || (v & 1))
1245 return;
1246
1248
1249 /* If any wasted bits are found, samples are moved
1250 * from frame.samples_33bps to frame.subframes[ch] */
1251 for (
i = 0;
i <
s->frame.blocksize;
i++)
1252 sub->
samples[
i] =
s->frame.samples_33bps[
i] >> v;
1253 wasted_bits = v;
1254 } else {
1256 for (
i = 0;
i <
s->frame.blocksize;
i++) {
1258 if (v & 1)
1259 break;
1260 }
1261
1262 if (!v || (v & 1))
1263 return;
1264
1266
1267 for (
i = 0;
i <
s->frame.blocksize;
i++)
1269 wasted_bits = v;
1270 }
1271
1272 sub->
wasted = wasted_bits;
1273 sub->
obits -= wasted_bits;
1274
1275 /* for 24-bit, check if removing wasted bits makes the range better
1276 * suited for using RICE instead of RICE2 for entropy coding */
1277 if (sub->
obits <= 17)
1279 }
1280 }
1281
1282
1284 int max_rice_param,
int bps)
1285 {
1286 int best;
1287 uint64_t sum[4];
1288 uint64_t score[4];
1289 int k;
1290
1291 /* calculate sum of 2nd order residual for each channel */
1292 sum[0] = sum[1] = sum[2] = sum[3] = 0;
1295 for (
int i = 2;
i < n;
i++) {
1296 lt = left_ch[
i] - 2*left_ch[
i-1] + left_ch[
i-2];
1297 rt = right_ch[
i] - 2*right_ch[
i-1] + right_ch[
i-2];
1298 sum[2] +=
FFABS((lt + rt) >> 1);
1299 sum[3] +=
FFABS(lt - rt);
1300 sum[0] +=
FFABS(lt);
1301 sum[1] +=
FFABS(rt);
1302 }
1303 } else {
1305 for (
int i = 2;
i < n;
i++) {
1308 sum[2] +=
FFABS((lt + rt) >> 1);
1309 sum[3] +=
FFABS(lt - rt);
1310 sum[0] +=
FFABS(lt);
1311 sum[1] +=
FFABS(rt);
1312 }
1313 }
1314 /* estimate bit counts */
1315 for (
int i = 0;
i < 4;
i++) {
1318 }
1319
1320 /* calculate score for each mode */
1321 score[0] = sum[0] + sum[1];
1322 score[1] = sum[0] + sum[3];
1323 score[2] = sum[1] + sum[3];
1324 score[3] = sum[2] + sum[3];
1325
1326 /* return mode with lowest score */
1327 best = 0;
1328 for (
int i = 1;
i < 4;
i++)
1329 if (score[
i] < score[best])
1331
1332 return best;
1333 }
1334
1335
1336 /**
1337 * Perform stereo channel decorrelation.
1338 */
1340 {
1344 int n;
1345
1347 n =
frame->blocksize;
1349 right =
frame->subframes[1].samples;
1350 side_33bps =
frame->samples_33bps;
1351
1352 if (
s->channels != 2) {
1354 return;
1355 }
1356
1357 if (
s->options.ch_mode < 0) {
1358 int max_rice_param = (1 <<
frame->subframes[0].rc.coding_mode) - 2;
1360 } else
1361 frame->ch_mode =
s->options.ch_mode;
1362
1363 /* perform decorrelation and adjust bits-per-sample */
1365 return;
1366 if(
s->avctx->bits_per_raw_sample == 32) {
1369 for (
int i = 0;
i < n;
i++) {
1372 side_33bps[
i] =
tmp - right[
i];
1373 }
1374 frame->subframes[1].obits++;
1376 for (
int i = 0;
i < n;
i++)
1378 frame->subframes[1].obits++;
1379 } else {
1380 for (
int i = 0;
i < n;
i++)
1382 frame->subframes[0].obits++;
1383 }
1384 } else {
1387 for (
int i = 0;
i < n;
i++) {
1390 right[
i] =
tmp - right[
i];
1391 }
1392 frame->subframes[1].obits++;
1394 for (
int i = 0;
i < n;
i++)
1395 right[
i] =
left[
i] - right[
i];
1396 frame->subframes[1].obits++;
1397 } else {
1398 for (
int i = 0;
i < n;
i++)
1400 frame->subframes[0].obits++;
1401 }
1402 }
1403 }
1404
1405
1407 {
1410 }
1411
1412
1414 {
1416 int crc;
1417
1419
1423
1426 else
1428
1432
1433 if (
frame->bs_code[0] == 6)
1435 else if (
frame->bs_code[0] == 7)
1437
1438 if (
s->sr_code[0] == 12)
1440 else if (
s->sr_code[0] > 12)
1442
1447 }
1448
1449
1451 {
1452 unsigned v, e;
1453
1454 v = ((unsigned)(
i) << 1) ^ (
i >> 31);
1455
1456 e = (v >> k) + 1;
1457 while (e > 31) {
1459 e -= 31;
1460 }
1462 if (k) {
1463 unsigned mask = UINT32_MAX >> (32-k);
1465 }
1466 }
1467
1468
1470 {
1471 int ch;
1472
1473 for (ch = 0; ch <
s->channels; ch++) {
1475 int p, porder, psize;
1479
1480 /* subframe header */
1486
1487 /* subframe */
1489 if(sub->
obits == 33)
1491 else if(sub->
obits == 32)
1493 else
1496 if (sub->
obits == 33) {
1497 int64_t *res64 =
s->frame.samples_33bps;
1498 int64_t *frame_end64 = &
s->frame.samples_33bps[
s->frame.blocksize];
1499 while (res64 < frame_end64)
1501 }
else if (sub->
obits == 32) {
1504 } else {
1507 }
1508 } else {
1509 /* warm-up samples */
1510 if (sub->
obits == 33) {
1511 for (
int i = 0;
i < sub->
order;
i++)
1514 }
else if (sub->
obits == 32) {
1515 for (
int i = 0;
i < sub->
order;
i++)
1517 } else {
1518 for (
int i = 0;
i < sub->
order;
i++)
1520 }
1521
1522 /* LPC coefficients */
1524 int cbits =
s->options.lpc_coeff_precision;
1527 for (
int i = 0;
i < sub->
order;
i++)
1529 }
1530
1531 /* rice-encoded block */
1533
1534 /* partition order */
1536 psize =
s->frame.blocksize >> porder;
1538
1539 /* residual */
1541 for (p = 0; p < 1 << porder; p++) {
1544 while (res < part_end)
1547 }
1548 }
1549 }
1550 }
1551
1552
1554 {
1555 int crc;
1561 }
1562
1563
1565 {
1571 }
1572
1573
1575 {
1576 const uint8_t *buf;
1577 int buf_size =
s->frame.blocksize *
s->channels *
1578 ((
s->avctx->bits_per_raw_sample + 7) / 8);
1579
1580 if (
s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
1584 }
1585
1586 if (
s->avctx->bits_per_raw_sample <= 16) {
1587 buf = (
const uint8_t *)
samples;
1588 #if HAVE_BIGENDIAN
1589 s->bdsp.bswap16_buf((uint16_t *)
s->md5_buffer,
1590 (
const uint16_t *)
samples, buf_size / 2);
1591 buf =
s->md5_buffer;
1592 #endif
1593 }
else if (
s->avctx->bits_per_raw_sample <= 24) {
1596 uint8_t *
tmp =
s->md5_buffer;
1597
1598 for (
i = 0;
i <
s->frame.blocksize *
s->channels;
i++) {
1601 }
1602 buf =
s->md5_buffer;
1603 } else {
1604 /* s->avctx->bits_per_raw_sample <= 32 */
1607 uint8_t *
tmp =
s->md5_buffer;
1608
1609 for (
i = 0;
i <
s->frame.blocksize *
s->channels;
i++)
1611 buf =
s->md5_buffer;
1612 }
1614
1615 return 0;
1616 }
1617
1618
1621 {
1623 int frame_bytes, out_bytes,
ret;
1624
1626
1627 /* when the last block is reached, update the header in extradata */
1629 s->max_framesize =
s->max_encoded_framesize;
1632
1636 if (!side_data)
1639
1640 avpkt->
pts =
s->next_pts;
1641
1642 *got_packet_ptr = 1;
1644 }
1645
1646 return 0;
1647 }
1648
1649 /* change max_framesize for small final frame */
1654 }
1655
1657
1659
1661
1663
1665
1666 /* Fall back on verbatim mode if the compressed frame is larger than it
1667 would be if encoded uncompressed. */
1668 if (frame_bytes < 0 || frame_bytes >
s->max_framesize) {
1669 s->frame.verbatim_only = 1;
1671 if (frame_bytes < 0) {
1673 return frame_bytes;
1674 }
1675 }
1676
1679
1681
1687 }
1688 if (out_bytes >
s->max_encoded_framesize)
1689 s->max_encoded_framesize = out_bytes;
1690 if (out_bytes < s->min_framesize)
1691 s->min_framesize = out_bytes;
1692
1694
1696
1697 *got_packet_ptr = 1;
1698 return 0;
1699 }
1700
1701
1703 {
1705
1709 return 0;
1710 }
1711
1712 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1715 {
"lpc_type",
"LPC algorithm", offsetof(
FlacEncodeContext,
options.lpc_type),
AV_OPT_TYPE_INT, {.i64 =
FF_LPC_TYPE_DEFAULT },
FF_LPC_TYPE_DEFAULT,
FF_LPC_TYPE_NB-1,
FLAGS, .unit =
"lpc_type" },
1723 {
"prediction_order_method",
"Search method for selecting prediction order", offsetof(
FlacEncodeContext,
options.prediction_order_method),
AV_OPT_TYPE_INT, {.i64 = -1 }, -1,
ORDER_METHOD_LOG,
FLAGS, .unit =
"predm" },
1730 {
"ch_mode",
"Stereo decorrelation mode", offsetof(
FlacEncodeContext,
options.ch_mode),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1,
FLAC_CHMODE_MID_SIDE,
FLAGS, .unit =
"ch_mode" },
1740
1742 };
1743
1749 };
1750
1768 };