37 #define FLAC_SUBFRAME_CONSTANT 0
38 #define FLAC_SUBFRAME_VERBATIM 1
39 #define FLAC_SUBFRAME_FIXED 8
40 #define FLAC_SUBFRAME_LPC 32
42 #define MAX_FIXED_ORDER 4
43 #define MAX_PARTITION_ORDER 8
44 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
45 #define MAX_LPC_PRECISION 15
46 #define MAX_LPC_SHIFT 15
146 memcpy(&header[18], s->
md5sum, 16);
162 target = (samplerate * block_time_ms) / 1000;
163 for (i = 0; i < 16; i++) {
188 av_log(avctx,
AV_LOG_DEBUG,
" lpc type: Levinson-Durbin recursion with Welch window\n");
256 channels, FLAC_MAX_CHANNELS);
264 for (i = 4; i < 12; i++) {
274 if (freq % 1000 == 0 && freq < 255000) {
277 }
else if (freq % 10 == 0 && freq < 655350) {
280 }
else if (freq < 65535) {
303 s->
options.
block_time_ms = ((
int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
310 FF_LPC_TYPE_LEVINSON})[level];
312 s->
options.
min_prediction_order = ((
int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
313 s->
options.
max_prediction_order = ((
int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
320 ORDER_METHOD_SEARCH})[level];
328 s->
options.
min_partition_order = ((
int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
330 s->
options.
max_partition_order = ((
int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
418 "output stream will have incorrect "
419 "channel layout.\n");
422 "will use Flac channel layout for "
423 "%d channels.\n", channels);
447 for (i = 0; i < 16; i++) {
466 for (ch = 0; ch < s->
channels; ch++) {
492 #define COPY_SAMPLES(bits) do { \
493 const int ## bits ## _t *samples0 = samples; \
495 for (i = 0, j = 0; i < frame->blocksize; i++) \
496 for (ch = 0; ch < s->channels; ch++, j++) \
497 frame->subframes[ch].samples[i] = samples0[j] >> shift; \
512 for (i = 0; i <
n; i++) {
515 count += (v >> k) + 1 + k;
524 int p, porder, psize;
538 count += pred_order * sub->
obits;
555 for (p = 0; p < 1 << porder; p++) {
568 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
580 sum2 = sum - (n >> 1);
581 k =
av_log2(av_clipl_int32(sum2 / n));
582 return FFMIN(k, max_param);
587 uint64_t *sums,
int n,
int pred_order)
590 int k, cnt, part, max_param;
595 part = (1 << porder);
598 cnt = (n >> porder) - pred_order;
599 for (i = 0; i < part; i++) {
617 uint32_t *res, *res_end;
621 res = &data[pred_order];
622 res_end = &data[n >> pmax];
623 for (i = 0; i < parts; i++) {
625 while (res < res_end)
628 res_end += n >> pmax;
631 for (i = pmax - 1; i >= pmin; i--) {
633 for (j = 0; j < parts; j++)
634 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
656 for (i = 0; i <
n; i++)
657 udata[i] = (2*data[i]) ^ (data[i]>>31);
659 calc_sums(pmin, pmax, udata, n, pred_order, sums);
662 bits[pmin] = UINT32_MAX;
663 for (i = pmin; i <= pmax; i++) {
665 if (bits[i] <= bits[opt_porder]) {
672 return bits[opt_porder];
707 for (i = 0; i < order; i++)
711 for (i = order; i <
n; i++)
713 }
else if (order == 1) {
714 for (i = order; i <
n; i++)
715 res[i] = smp[i] - smp[i-1];
716 }
else if (order == 2) {
717 int a = smp[order-1] - smp[order-2];
718 for (i = order; i <
n; i += 2) {
719 int b = smp[i ] - smp[i-1];
721 a = smp[i+1] - smp[i ];
724 }
else if (order == 3) {
725 int a = smp[order-1] - smp[order-2];
726 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
727 for (i = order; i <
n; i += 2) {
728 int b = smp[i ] - smp[i-1];
731 a = smp[i+1] - smp[i ];
736 int a = smp[order-1] - smp[order-2];
737 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
738 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
739 for (i = order; i <
n; i += 2) {
740 int b = smp[i ] - smp[i-1];
744 a = smp[i+1] - smp[i ];
756 int min_order, max_order, opt_order, omethod;
770 for (i = 1; i <
n; i++)
782 memcpy(res, smp, n *
sizeof(
int32_t));
798 bits[0] = UINT32_MAX;
799 for (i = min_order; i <= max_order; i++) {
802 if (bits[i] < bits[opt_order])
805 sub->
order = opt_order;
807 if (sub->
order != max_order) {
824 int levels = 1 << omethod;
827 int opt_index = levels-1;
828 opt_order = max_order-1;
829 bits[opt_index] = UINT32_MAX;
830 for (i = levels-1; i >= 0; i--) {
831 int last_order = order;
832 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
833 order = av_clip(order, min_order - 1, max_order - 1);
834 if (order == last_order)
839 if (bits[i] < bits[opt_index]) {
849 bits[0] = UINT32_MAX;
850 for (i = min_order-1; i < max_order; i++) {
853 if (bits[i] < bits[opt_order])
861 opt_order = min_order - 1 + (max_order-min_order)/3;
862 memset(bits, -1,
sizeof(bits));
864 for (step = 16; step; step >>= 1) {
865 int last = opt_order;
866 for (i = last-step; i <= last+step; i += step) {
867 if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
871 if (bits[i] < bits[opt_order])
878 sub->
order = opt_order;
881 for (i = 0; i < sub->
order; i++)
935 for (ch = 0; ch < s->
channels; ch++)
938 count += (8 - (count & 7)) & 7;
952 for (ch = 0; ch < s->
channels; ch++) {
990 sum[0] = sum[1] = sum[2] = sum[3] = 0;
991 for (i = 2; i <
n; i++) {
992 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
993 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
994 sum[2] +=
FFABS((lt + rt) >> 1);
995 sum[3] +=
FFABS(lt - rt);
1000 for (i = 0; i < 4; i++) {
1006 score[0] = sum[0] + sum[1];
1007 score[1] = sum[0] + sum[3];
1008 score[2] = sum[1] + sum[3];
1009 score[3] = sum[2] + sum[3];
1013 for (i = 1; i < 4; i++)
1014 if (score[i] < score[best])
1051 for (i = 0; i <
n; i++) {
1053 left[i] = (tmp + right[i]) >> 1;
1054 right[i] = tmp - right[i];
1058 for (i = 0; i <
n; i++)
1059 right[i] = left[i] - right[i];
1062 for (i = 0; i <
n; i++)
1063 left[i] -= right[i];
1098 else if (frame->
bs_code[0] == 7)
1117 for (ch = 0; ch < s->
channels; ch++) {
1119 int i, p, porder, psize;
1135 while (res < frame_end)
1139 for (i = 0; i < sub->
order; i++)
1147 for (i = 0; i < sub->
order; i++)
1161 for (p = 0; p < 1 << porder; p++) {
1164 while (res < part_end)
1166 part_end =
FFMIN(frame_end, part_end + psize);
1207 buf = (
const uint8_t *)samples;
1210 (
const uint16_t *) samples, buf_size / 2);
1215 const int32_t *samples0 = samples;
1220 *tmp++ = (
v ) & 0xFF;
1221 *tmp++ = (v >> 8) & 0xFF;
1222 *tmp++ = (v >> 16) & 0xFF;
1236 int frame_bytes, out_bytes,
ret;
1255 *got_packet_ptr = 1;
1284 if (frame_bytes < 0) {
1303 if (out_bytes < s->min_framesize)
1308 avpkt->
size = out_bytes;
1312 *got_packet_ptr = 1;
1330 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1333 {
"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,
"lpc_type" },
1338 {
"lpc_passes",
"Number of passes to use for Cholesky factorization during LPC analysis", offsetof(
FlacEncodeContext, options.lpc_passes),
AV_OPT_TYPE_INT, {.i64 = 2 }, 1, INT_MAX,
FLAGS },
1341 {
"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,
"predm" },
1348 {
"ch_mode",
"Stereo decorrelation mode", offsetof(
FlacEncodeContext, options.ch_mode),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1,
FLAC_CHMODE_MID_SIDE,
FLAGS,
"ch_mode" },