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
143 memcpy(&header[18], s->
md5sum, 16);
159 target = (samplerate * block_time_ms) / 1000;
160 for (i = 0; i < 16; i++) {
185 av_log(avctx,
AV_LOG_DEBUG,
" lpc type: Levinson-Durbin recursion with Welch window\n");
253 channels, FLAC_MAX_CHANNELS);
261 for (i = 4; i < 12; i++) {
271 if (freq % 1000 == 0 && freq < 255000) {
274 }
else if (freq % 10 == 0 && freq < 655350) {
277 }
else if (freq < 65535) {
300 s->
options.
block_time_ms = ((
int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
307 FF_LPC_TYPE_LEVINSON})[level];
309 s->
options.
min_prediction_order = ((
int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
310 s->
options.
max_prediction_order = ((
int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
317 ORDER_METHOD_SEARCH})[level];
325 s->
options.
min_partition_order = ((
int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
327 s->
options.
max_partition_order = ((
int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
402 #if FF_API_OLD_ENCODE_AUDIO
421 "output stream will have incorrect "
422 "channel layout.\n");
425 "will use Flac channel layout for "
426 "%d channels.\n", channels);
450 for (i = 0; i < 16; i++) {
469 for (ch = 0; ch < s->
channels; ch++) {
495 #define COPY_SAMPLES(bits) do { \
496 const int ## bits ## _t *samples0 = samples; \
498 for (i = 0, j = 0; i < frame->blocksize; i++) \
499 for (ch = 0; ch < s->channels; ch++, j++) \
500 frame->subframes[ch].samples[i] = samples0[j] >> shift; \
515 for (i = 0; i < n; i++) {
518 count += (v >> k) + 1 + k;
527 int p, porder, psize;
541 count += pred_order * sub->
obits;
558 for (p = 0; p < 1 << porder; p++) {
571 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
583 sum2 = sum - (n >> 1);
584 k =
av_log2(av_clipl_int32(sum2 / n));
585 return FFMIN(k, max_param);
590 uint64_t *sums,
int n,
int pred_order)
593 int k, cnt, part, max_param;
598 part = (1 << porder);
601 cnt = (n >> porder) - pred_order;
602 for (i = 0; i < part; i++) {
615 static void calc_sums(
int pmin,
int pmax, uint32_t *
data,
int n,
int pred_order,
620 uint32_t *res, *res_end;
624 res = &data[pred_order];
625 res_end = &data[n >> pmax];
626 for (i = 0; i < parts; i++) {
628 while (res < res_end)
631 res_end += n >> pmax;
634 for (i = pmax - 1; i >= pmin; i--) {
636 for (j = 0; j < parts; j++)
637 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
659 for (i = 0; i < n; i++)
660 udata[i] = (2*data[i]) ^ (data[i]>>31);
662 calc_sums(pmin, pmax, udata, n, pred_order, sums);
665 bits[pmin] = UINT32_MAX;
666 for (i = pmin; i <= pmax; i++) {
668 if (bits[i] <= bits[opt_porder]) {
675 return bits[opt_porder];
710 for (i = 0; i < order; i++)
714 for (i = order; i < n; i++)
716 }
else if (order == 1) {
717 for (i = order; i < n; i++)
718 res[i] = smp[i] - smp[i-1];
719 }
else if (order == 2) {
720 int a = smp[order-1] - smp[order-2];
721 for (i = order; i < n; i += 2) {
722 int b = smp[i ] - smp[i-1];
724 a = smp[i+1] - smp[i ];
727 }
else if (order == 3) {
728 int a = smp[order-1] - smp[order-2];
729 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
730 for (i = order; i < n; i += 2) {
731 int b = smp[i ] - smp[i-1];
734 a = smp[i+1] - smp[i ];
739 int a = smp[order-1] - smp[order-2];
740 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
741 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
742 for (i = order; i < n; i += 2) {
743 int b = smp[i ] - smp[i-1];
747 a = smp[i+1] - smp[i ];
759 int min_order, max_order, opt_order, omethod;
773 for (i = 1; i < n; i++)
785 memcpy(res, smp, n *
sizeof(
int32_t));
801 bits[0] = UINT32_MAX;
802 for (i = min_order; i <= max_order; i++) {
805 if (bits[i] < bits[opt_order])
808 sub->
order = opt_order;
810 if (sub->
order != max_order) {
827 int levels = 1 << omethod;
830 int opt_index = levels-1;
831 opt_order = max_order-1;
832 bits[opt_index] = UINT32_MAX;
833 for (i = levels-1; i >= 0; i--) {
834 int last_order = order;
835 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
836 order = av_clip(order, min_order - 1, max_order - 1);
837 if (order == last_order)
842 if (bits[i] < bits[opt_index]) {
852 bits[0] = UINT32_MAX;
853 for (i = min_order-1; i < max_order; i++) {
856 if (bits[i] < bits[opt_order])
864 opt_order = min_order - 1 + (max_order-min_order)/3;
865 memset(bits, -1,
sizeof(bits));
867 for (step = 16; step; step >>= 1) {
868 int last = opt_order;
869 for (i = last-step; i <= last+step; i += step) {
870 if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
874 if (bits[i] < bits[opt_order])
881 sub->
order = opt_order;
884 for (i = 0; i < sub->
order; i++)
938 for (ch = 0; ch < s->
channels; ch++)
941 count += (8 - (count & 7)) & 7;
955 for (ch = 0; ch < s->
channels; ch++) {
993 sum[0] = sum[1] = sum[2] = sum[3] = 0;
994 for (i = 2; i < n; i++) {
995 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
996 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
997 sum[2] +=
FFABS((lt + rt) >> 1);
998 sum[3] +=
FFABS(lt - rt);
1000 sum[1] +=
FFABS(rt);
1003 for (i = 0; i < 4; i++) {
1009 score[0] = sum[0] + sum[1];
1010 score[1] = sum[0] + sum[3];
1011 score[2] = sum[1] + sum[3];
1012 score[3] = sum[2] + sum[3];
1016 for (i = 1; i < 4; i++)
1017 if (score[i] < score[best])
1054 for (i = 0; i < n; i++) {
1056 left[i] = (tmp + right[i]) >> 1;
1057 right[i] = tmp - right[i];
1061 for (i = 0; i < n; i++)
1062 right[i] = left[i] - right[i];
1065 for (i = 0; i < n; i++)
1066 left[i] -= right[i];
1101 else if (frame->
bs_code[0] == 7)
1120 for (ch = 0; ch < s->
channels; ch++) {
1122 int i, p, porder, psize;
1138 while (res < frame_end)
1142 for (i = 0; i < sub->
order; i++)
1150 for (i = 0; i < sub->
order; i++)
1164 for (p = 0; p < 1 << porder; p++) {
1167 while (res < part_end)
1169 part_end =
FFMIN(frame_end, part_end + psize);
1210 buf = (
const uint8_t *)samples;
1213 (
const uint16_t *)samples, buf_size / 2);
1223 *tmp++ = (v ) & 0xFF;
1224 *tmp++ = (v >> 8) & 0xFF;
1225 *tmp++ = (v >> 16) & 0xFF;
1239 int frame_bytes, out_bytes, ret;
1273 if (frame_bytes < 0) {
1292 if (out_bytes < s->min_framesize)
1297 avpkt->
size = out_bytes;
1298 *got_packet_ptr = 1;
1313 #if FF_API_OLD_ENCODE_AUDIO
1319 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1322 {
"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" },
1327 {
"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 },
1330 {
"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" },
1337 {
"ch_mode",
"Stereo decorrelation mode", offsetof(
FlacEncodeContext, options.ch_mode),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1,
FLAC_CHMODE_MID_SIDE,
FLAGS,
"ch_mode" },