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
36
37 #define FLAC_SUBFRAME_CONSTANT 0
38 #define FLAC_SUBFRAME_VERBATIM 1
39 #define FLAC_SUBFRAME_FIXED 8
40 #define FLAC_SUBFRAME_LPC 32
41
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
47
51 };
52
68
74
83
87
91
100
124
128
129
130 /**
131 * Write streaminfo metadata block to byte array.
132 */
134 {
136
139
140 /* streaminfo metadata block */
148 /* write 36-bit sample count in 2 put_bits() calls */
152 memcpy(&header[18], s->
md5sum, 16);
153 }
154
155
156 /**
157 * Set blocksize based on samplerate.
158 * Choose the closest predefined blocksize >= BLOCK_TIME_MS milliseconds.
159 */
161 {
162 int i;
163 int target;
164 int blocksize;
165
168 target = (samplerate * block_time_ms) / 1000;
169 for (i = 0; i < 16; i++) {
173 }
174 }
175 return blocksize;
176 }
177
178
180 {
183
185
189 break;
192 break;
194 av_log(avctx,
AV_LOG_DEBUG,
" lpc type: Levinson-Durbin recursion with Welch window\n");
195 break;
199 break;
200 }
201
204
208 break;
211 break;
214 break;
217 break;
220 break;
223 break;
224 }
225
226
229
231
234 }
235
236
238 {
244
246
251 break;
257 break;
258 }
259
262 channels, FLAC_MAX_CHANNELS);
264 }
266
267 /* find samplerate in table */
268 if (freq < 1)
269 return -1;
270 for (i = 4; i < 12; i++) {
275 break;
276 }
277 }
278 /* if not in table, samplerate is non-standard */
279 if (i == 12) {
280 if (freq % 1000 == 0 && freq < 255000) {
283 } else if (freq % 10 == 0 && freq < 655350) {
286 } else if (freq < 65535) {
289 } else {
292 }
294 }
295
296 /* set compression option defaults based on avctx->compression_level */
299 else
301
303 if (level > 12) {
307 }
308
309 s->
options.
block_time_ms = ((
int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
310
316 FF_LPC_TYPE_LEVINSON})[level];
317
319 s->
options.
min_prediction_order = ((
int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
321 s->
options.
max_prediction_order = ((
int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
322
328 ORDER_METHOD_SEARCH})[level];
329
334 }
336 s->
options.
min_partition_order = ((
int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
338 s->
options.
max_partition_order = ((
int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
339
340 #if FF_API_PRIVATE_OPT
346 "invalid min prediction order %d, clamped to %d\n",
349 }
355 }
357 }
362 "invalid max prediction order %d, clamped to %d\n",
365 }
371 }
373 }
375 #endif
382 "invalid min prediction order %d, clamped to %d\n",
385 }
388 "invalid max prediction order %d, clamped to %d\n",
391 }
392 }
393
398 }
399
406 }
407 } else {
409 }
411
412 /* set maximum encoded frame size in verbatim mode */
416
417 /* initialize MD5 context */
422
424 if (!streaminfo)
429
432
433 if (channels == 3 &&
435 channels == 4 &&
438 channels == 5 &&
441 channels == 6 &&
446 "output stream will have incorrect "
447 "channel layout.\n");
448 } else {
450 "will use Flac channel layout for "
451 "%d channels.\n", channels);
452 }
453 }
454
457
461
463
464 return ret;
465 }
466
467
469 {
470 int i, ch;
472
474
475 for (i = 0; i < 16; i++) {
480 break;
481 }
482 }
483 if (i == 16) {
488 } else {
491 }
492 }
493
494 for (ch = 0; ch < s->
channels; ch++) {
496
499
502 else
504 }
505
507 }
508
509
510 /**
511 * Copy channel-interleaved input samples into separate subframes.
512 */
514 {
515 int i, j, ch;
519
520 #define COPY_SAMPLES(bits) do { \
521 const int ## bits ## _t *samples0 = samples; \
522 frame = &s->frame; \
523 for (i = 0, j = 0; i < frame->blocksize; i++) \
524 for (ch = 0; ch < s->channels; ch++, j++) \
525 frame->subframes[ch].samples[i] = samples0[j] >> shift; \
526 } while (0)
527
530 else
532 }
533
534
536 {
537 int i;
539
540 for (i = 0; i <
n; i++) {
542 v ^= v >> 31;
543 count += (v >> k) + 1 + k;
544 }
546 }
547
548
550 int pred_order)
551 {
552 int p, porder, psize;
553 int i, part_end;
555
556 /* subframe header */
557 count += 8;
558
561
562 /* subframe */
567 } else {
568 /* warm-up samples */
569 count += pred_order * sub->
obits;
570
571 /* LPC coefficients */
574
575 /* rice-encoded block */
576 count += 2;
577
578 /* partition order */
581 count += 4;
582
583 /* residual */
584 i = pred_order;
585 part_end = psize;
586 for (p = 0; p < 1 << porder; p++) {
590 i = part_end;
592 }
593 }
594
596 }
597
598
599 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
600
601 /**
602 * Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
603 */
605 {
606 int k;
607 uint64_t sum2;
608
609 if (sum <= n >> 1)
610 return 0;
611 sum2 = sum - (n >> 1);
612 k =
av_log2(av_clipl_int32(sum2 / n));
613 return FFMIN(k, max_param);
614 }
615
617 {
618 int bestk = 0;
619 int64_t bestbits = INT64_MAX;
620 int k;
621
622 for (k = 0; k <= max_param; k++) {
623 int64_t
bits = sums[k][i];
624 if (bits < bestbits) {
626 bestk = k;
627 }
628 }
629
630 return bestk;
631 }
632
635 int n,
int pred_order,
int max_param,
int exact)
636 {
637 int i;
638 int k, cnt, part;
639 uint64_t all_bits;
640
641 part = (1 << porder);
642 all_bits = 4 * part;
643
644 cnt = (n >> porder) - pred_order;
645 for (i = 0; i < part; i++) {
646 if (exact) {
648 all_bits += sums[k][i];
649 } else {
652 }
654 cnt = n >> porder;
655 }
656
658
659 return all_bits;
660 }
661
662
665 {
666 int i, k;
667 int parts;
668 const uint32_t *res, *res_end;
669
670 /* sums for highest level */
671 parts = (1 << pmax);
672
673 for (k = 0; k <= kmax; k++) {
674 res = &data[pred_order];
675 res_end = &data[n >> pmax];
676 for (i = 0; i < parts; i++) {
677 if (kmax) {
678 uint64_t sum = (1LL + k) * (res_end - res);
679 while (res < res_end)
680 sum += *(res++) >> k;
681 sums[k][i] = sum;
682 } else {
683 uint64_t sum = 0;
684 while (res < res_end)
685 sum += *(res++);
686 sums[k][i] = sum;
687 }
688 res_end += n >> pmax;
689 }
690 }
691 }
692
694 {
695 int i, k;
696 int parts = (1 <<
level);
697 for (i = 0; i < parts; i++) {
698 for (k=0; k<=kmax; k++)
699 sums[k][i] = sums[k][2*i] + sums[k][2*i+1];
700 }
701 }
702
706 int pmin, int pmax,
708 {
709 int i;
711 int opt_porder;
714
718
720
721 for (i = 0; i <
n; i++)
722 udata[i] = (2 * data[i]) ^ (data[i] >> 31);
723
724 calc_sum_top(pmax, exact ? kmax : 0, udata, n, pred_order, sums);
725
726 opt_porder = pmin;
727 bits[pmin] = UINT32_MAX;
728 for (i = pmax; ; ) {
730 if (bits[i] < bits[opt_porder] || pmax == pmin) {
731 opt_porder = i;
732 *rc = tmp_rc;
733 }
734 if (i == pmin)
735 break;
737 }
738
739 return bits[opt_porder];
740 }
741
742
744 {
746 if (order > 0)
748 return porder;
749 }
750
751
754 {
759
766 }
767
768
770 int order)
771 {
772 int i;
773
774 for (i = 0; i < order; i++)
775 res[i] = smp[i];
776
777 if (order == 0) {
778 for (i = order; i <
n; i++)
779 res[i] = smp[i];
780 } else if (order == 1) {
781 for (i = order; i <
n; i++)
782 res[i] = smp[i] - smp[i-1];
783 } else if (order == 2) {
784 int a = smp[order-1] - smp[order-2];
785 for (i = order; i <
n; i += 2) {
786 int b = smp[i ] - smp[i-1];
788 a = smp[i+1] - smp[i ];
790 }
791 } else if (order == 3) {
792 int a = smp[order-1] - smp[order-2];
793 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
794 for (i = order; i <
n; i += 2) {
795 int b = smp[i ] - smp[i-1];
798 a = smp[i+1] - smp[i ];
800 res[i+1] = c - d;
801 }
802 } else {
803 int a = smp[order-1] - smp[order-2];
804 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
805 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
806 for (i = order; i <
n; i += 2) {
807 int b = smp[i ] - smp[i-1];
810 res[i ] = f - e;
811 a = smp[i+1] - smp[i ];
813 e = c - d;
814 res[i+1] = e - f;
815 }
816 }
817 }
818
819
821 {
823 int min_order, max_order, opt_order, omethod;
829
835
836 /* CONSTANT */
837 for (i = 1; i <
n; i++)
838 if(smp[i] != smp[0])
839 break;
840 if (i == n) {
842 res[0] = smp[0];
844 }
845
846 /* VERBATIM */
849 memcpy(res, smp, n *
sizeof(
int32_t));
851 }
852
856
857 /* FIXED */
864 opt_order = 0;
865 bits[0] = UINT32_MAX;
866 for (i = min_order; i <= max_order; i++) {
869 if (bits[i] < bits[opt_order])
870 opt_order = i;
871 }
872 sub->
order = opt_order;
874 if (sub->
order != max_order) {
877 }
879 }
880
881 /* LPC */
887
891 int levels = 1 << omethod;
893 int order = -1;
894 int opt_index = levels-1;
895 opt_order = max_order-1;
896 bits[opt_index] = UINT32_MAX;
897 for (i = levels-1; i >= 0; i--) {
898 int last_order = order;
899 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
900 order = av_clip(order, min_order - 1, max_order - 1);
901 if (order == last_order)
902 continue;
905 shift[order]);
906 } else {
908 shift[order]);
909 }
911 if (bits[i] < bits[opt_index]) {
912 opt_index = i;
913 opt_order = order;
914 }
915 }
916 opt_order++;
918 // brute-force optimal order search
920 opt_order = 0;
921 bits[0] = UINT32_MAX;
922 for (i = min_order-1; i < max_order; i++) {
925 } else {
927 }
929 if (bits[i] < bits[opt_order])
930 opt_order = i;
931 }
932 opt_order++;
935 int step;
936
937 opt_order = min_order - 1 + (max_order-min_order)/3;
938 memset(bits, -1, sizeof(bits));
939
940 for (step = 16; step; step >>= 1) {
941 int last = opt_order;
942 for (i = last-step; i <= last+step; i += step) {
943 if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
944 continue;
947 } else {
949 }
951 if (bits[i] < bits[opt_order])
952 opt_order = i;
953 }
954 }
955 opt_order++;
956 }
957
959 int allsteps = 1;
960 int i, step, improved;
961 int64_t best_score = INT64_MAX;
963
965
966 for (i=0; i<opt_order; i++)
967 allsteps *= 3;
968
969 do {
970 improved = 0;
971 for (step = 0; step < allsteps; step++) {
972 int tmp = step;
974 int64_t score = 0;
975 int diffsum = 0;
976
977 for (i=0; i<opt_order; i++) {
978 int diff = ((tmp + 1) % 3) - 1;
979 lpc_try[i] = av_clip(coefs[opt_order - 1][i] + diff, -qmax, qmax);
980 tmp /= 3;
982 }
983 if (diffsum >8)
984 continue;
985
988 } else {
990 }
992 if (score < best_score) {
993 best_score = score;
994 memcpy(coefs[opt_order-1], lpc_try, sizeof(*coefs));
995 improved=1;
996 }
997 }
998 } while(improved);
999 }
1000
1001 sub->
order = opt_order;
1004 for (i = 0; i < sub->
order; i++)
1006
1009 } else {
1011 }
1012
1014
1016 }
1017
1018
1020 {
1023
1024 /*
1025 <14> Sync code
1026 <1> Reserved
1027 <1> Blocking strategy
1028 <4> Block size in inter-channel samples
1029 <4> Sample rate
1030 <4> Channel assignment
1031 <3> Sample size in bits
1032 <1> Reserved
1033 */
1034 count = 32;
1035
1036 /* coded frame number */
1038
1039 /* explicit block size */
1041 count += 8;
1043 count += 16;
1044
1045 /* explicit sample rate */
1047
1048 /* frame header CRC-8 */
1049 count += 8;
1050
1052 }
1053
1054
1056 {
1057 int ch;
1059
1061
1062 for (ch = 0; ch < s->
channels; ch++)
1064
1065 count += (8 - (count & 7)) & 7; // byte alignment
1066 count += 16; // CRC-16
1067
1068 count >>= 3;
1069 if (count > INT_MAX)
1072 }
1073
1074
1076 {
1077 int ch, i;
1078
1079 for (ch = 0; ch < s->
channels; ch++) {
1082
1085 if (v & 1)
1086 break;
1087 }
1088
1089 if (v && !(v & 1)) {
1091
1094
1097
1098 /* for 24-bit, check if removing wasted bits makes the range better
1099 suited for using RICE instead of RICE2 for entropy coding */
1102 }
1103 }
1104 }
1105
1106
1108 int max_rice_param)
1109 {
1110 int i, best;
1112 uint64_t sum[4];
1113 uint64_t score[4];
1114 int k;
1115
1116 /* calculate sum of 2nd order residual for each channel */
1117 sum[0] = sum[1] = sum[2] = sum[3] = 0;
1118 for (i = 2; i <
n; i++) {
1119 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
1120 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
1121 sum[2] +=
FFABS((lt + rt) >> 1);
1122 sum[3] +=
FFABS(lt - rt);
1123 sum[0] +=
FFABS(lt);
1124 sum[1] +=
FFABS(rt);
1125 }
1126 /* estimate bit counts */
1127 for (i = 0; i < 4; i++) {
1130 }
1131
1132 /* calculate score for each mode */
1133 score[0] = sum[0] + sum[1];
1134 score[1] = sum[0] + sum[3];
1135 score[2] = sum[1] + sum[3];
1136 score[3] = sum[2] + sum[3];
1137
1138 /* return mode with lowest score */
1139 best = 0;
1140 for (i = 1; i < 4; i++)
1141 if (score[i] < score[best])
1142 best = i;
1143
1144 return best;
1145 }
1146
1147
1148 /**
1149 * Perform stereo channel decorrelation.
1150 */
1152 {
1156
1161
1164 return;
1165 }
1166
1170 } else
1172
1173 /* perform decorrelation and adjust bits-per-sample */
1175 return;
1178 for (i = 0; i <
n; i++) {
1179 tmp = left[i];
1180 left[i] = (tmp + right[i]) >> 1;
1181 right[i] = tmp - right[i];
1182 }
1185 for (i = 0; i <
n; i++)
1186 right[i] = left[i] - right[i];
1188 } else {
1189 for (i = 0; i <
n; i++)
1190 left[i] -= right[i];
1192 }
1193 }
1194
1195
1197 {
1200 }
1201
1202
1204 {
1206 int crc;
1207
1209
1213
1216 else
1218
1222
1225 else if (frame->
bs_code[0] == 7)
1227
1232
1237 }
1238
1239
1241 {
1242 int ch;
1243
1244 for (ch = 0; ch < s->
channels; ch++) {
1246 int i, p, porder, psize;
1250
1251 /* subframe header */
1257
1258 /* subframe */
1262 while (res < frame_end)
1264 } else {
1265 /* warm-up samples */
1266 for (i = 0; i < sub->
order; i++)
1268
1269 /* LPC coefficients */
1274 for (i = 0; i < sub->
order; i++)
1276 }
1277
1278 /* rice-encoded block */
1280
1281 /* partition order */
1285
1286 /* residual */
1288 for (p = 0; p < 1 << porder; p++) {
1291 while (res < part_end)
1293 part_end =
FFMIN(frame_end, part_end + psize);
1294 }
1295 }
1296 }
1297 }
1298
1299
1301 {
1302 int crc;
1308 }
1309
1310
1312 {
1318 }
1319
1320
1322 {
1326
1331 }
1332
1334 buf = (
const uint8_t *)samples;
1335 #if HAVE_BIGENDIAN
1337 (const uint16_t *) samples, buf_size / 2);
1339 #endif
1340 } else {
1341 int i;
1342 const int32_t *samples0 = samples;
1344
1348 }
1350 }
1352
1353 return 0;
1354 }
1355
1356
1359 {
1361 int frame_bytes, out_bytes, ret;
1362
1364
1365 /* when the last block is reached, update the header in extradata */
1366 if (!frame) {
1370
1371 #if FF_API_SIDEDATA_ONLY_PKT
1375 #else
1377 #endif
1380 if (!side_data)
1383
1385
1386 *got_packet_ptr = 1;
1388 }
1389
1390 return 0;
1391 }
1392
1393 /* change max_framesize for small final frame */
1398 }
1399
1401
1403
1405
1407
1409
1410 /* Fall back on verbatim mode if the compressed frame is larger than it
1411 would be if encoded uncompressed. */
1415 if (frame_bytes < 0) {
1417 return frame_bytes;
1418 }
1419 }
1420
1422 return ret;
1423
1425
1430 return ret;
1431 }
1434 if (out_bytes < s->min_framesize)
1436
1439 avpkt->
size = out_bytes;
1440
1442
1443 *got_packet_ptr = 1;
1444 return 0;
1445 }
1446
1447
1449 {
1455 }
1458 return 0;
1459 }
1460
1461 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1464 {
"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" },
1469 {
"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 },
1472 {
"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" },
1479 {
"ch_mode",
"Stereo decorrelation mode", offsetof(
FlacEncodeContext, options.ch_mode),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1,
FLAC_CHMODE_MID_SIDE,
FLAGS,
"ch_mode" },
1489
1491 };
1492
1498 };
1499
1514 };
uint32_t rc_udata[FLAC_MAX_BLOCKSIZE]
#define rice_encode_count(sum, n, k)
const char const char void * val
#define ORDER_METHOD_SEARCH
static int shift(int a, int b)
This structure describes decoded (raw) audio or video data.
#define ORDER_METHOD_8LEVEL
ptrdiff_t const GLvoid * data
static void put_sbits(PutBitContext *pb, int n, int32_t value)
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 LIBAVUTIL_VERSION_INT
av_cold void ff_flacdsp_init(FLACDSPContext *c, enum AVSampleFormat fmt, int channels, int bps)
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 max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
void(* bswap16_buf)(uint16_t *dst, const uint16_t *src, int len)
static av_cold int init(AVCodecContext *avctx)
int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
Calculate an estimate for the maximum frame size based on verbatim mode.
#define MAX_PARTITION_ORDER
#define PUT_UTF8(val, tmp, PUT_BYTE)
Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
#define FLAC_MAX_BLOCKSIZE
#define AV_CH_LAYOUT_STEREO
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
#define AV_CH_LAYOUT_5POINT0
void(* lpc32_encode)(int32_t *res, const int32_t *smp, int len, int order, const int32_t coefs[32], int shift)
void av_md5_update(AVMD5 *ctx, const uint8_t *src, int len)
Update hash value.
static int select_blocksize(int samplerate, int block_time_ms)
Set blocksize based on samplerate.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
attribute_deprecated int side_data_only_packets
Encoding only and set by default.
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
enum AVSampleFormat sample_fmt
audio sample format
do not use LPC prediction or use all zero coefficients
int32_t coefs[MAX_LPC_ORDER]
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static uint64_t find_subframe_rice_params(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
int params[MAX_PARTITIONS]
static const uint8_t header[24]
#define FLAC_MIN_BLOCKSIZE
static void write_subframes(FlacEncodeContext *s)
#define AV_CH_LAYOUT_5POINT1
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define ORDER_METHOD_4LEVEL
unsigned int md5_buffer_size
FLAC (Free Lossless Audio Codec) decoder/demuxer common functions.
int exact_rice_parameters
uint64_t rc_sums[32][MAX_PARTITIONS]
void(* lpc16_encode)(int32_t *res, const int32_t *smp, int len, int order, const int32_t coefs[32], int shift)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define FLAC_SUBFRAME_LPC
enum CodingMode coding_mode
simple assert() macros that are a bit more flexible than ISO C assert().
#define AV_CH_LAYOUT_QUAD
const char * name
Name of the codec implementation.
#define COPY_SAMPLES(bits)
#define FLAC_SUBFRAME_VERBATIM
int32_t samples[FLAC_MAX_BLOCKSIZE]
static void remove_wasted_bits(FlacEncodeContext *s)
#define FLAC_SUBFRAME_CONSTANT
uint64_t channel_layout
Audio channel layout.
static int put_bits_count(PutBitContext *s)
#define ORDER_METHOD_2LEVEL
static uint64_t calc_optimal_rice_params(RiceContext *rc, int porder, uint64_t sums[32][MAX_PARTITIONS], int n, int pred_order, int max_param, int exact)
static void frame_end(MpegEncContext *s)
#define FLAC_SUBFRAME_FIXED
static int encode_residual_ch(FlacEncodeContext *s, int ch)
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
static uint64_t rice_count_exact(const int32_t *res, int n, int k)
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
static int encode_frame(FlacEncodeContext *s)
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
#define FLAC_STREAMINFO_SIZE
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
#define AV_CH_FRONT_CENTER
int prediction_order_method
#define AV_CH_LAYOUT_5POINT1_BACK
static int get_max_p_order(int max_porder, int n, int order)
static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
static int find_optimal_param_exact(uint64_t sums[32][MAX_PARTITIONS], int i, int max_param)
attribute_deprecated int max_prediction_order
static void set_sr_golomb_flac(PutBitContext *pb, int i, int k, int limit, int esc_len)
write signed golomb rice code (flac).
static const AVOption options[]
static void channel_decorrelation(FlacEncodeContext *s)
Perform stereo channel decorrelation.
int frame_size
Number of samples per channel in an audio frame.
const int ff_flac_sample_rate_table[16]
Libavcodec external API header.
static void calc_sum_next(int level, uint64_t sums[32][MAX_PARTITIONS], int kmax)
AVSampleFormat
Audio sample formats.
int sample_rate
samples per second
static void write_frame_header(FlacEncodeContext *s)
static void calc_sum_top(int pmax, int kmax, const uint32_t *data, int n, int pred_order, uint64_t sums[32][MAX_PARTITIONS])
main external API structure.
static int count_frame_header(FlacEncodeContext *s)
Levinson-Durbin recursion.
void av_md5_init(AVMD5 *ctx)
Initialize MD5 hashing.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Describe the class of an AVClass context structure.
use the codec default LPC type
#define AV_CH_LAYOUT_5POINT0_BACK
static uint64_t calc_rice_params(RiceContext *rc, uint32_t udata[FLAC_MAX_BLOCKSIZE], uint64_t sums[32][MAX_PARTITIONS], int pmin, int pmax, const int32_t *data, int n, int pred_order, int exact)
static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n, int order)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
int max_encoded_framesize
static void write_utf8(PutBitContext *pb, uint32_t val)
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
#define MAX_LPC_PRECISION
static void copy_samples(FlacEncodeContext *s, const void *samples)
Copy channel-interleaved input samples into separate subframes.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
static void write_frame_footer(FlacEncodeContext *s)
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
FlacSubframe subframes[FLAC_MAX_CHANNELS]
CompressionOptions options
FFLPCType
LPC analysis type.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
#define FF_DISABLE_DEPRECATION_WARNINGS
static int estimate_stereo_mode(const int32_t *left_ch, const int32_t *right_ch, int n, int max_rice_param)
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
int32_t residual[FLAC_MAX_BLOCKSIZE+11]
const int32_t ff_flac_blocksize_table[16]
#define AV_CODEC_CAP_LOSSLESS
Codec is lossless.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static av_cold int flac_encode_close(AVCodecContext *avctx)
static av_cold void dprint_compression_options(FlacEncodeContext *s)
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
static int find_optimal_param(uint64_t sum, int n, int max_param)
Solve for d/dk(rice_encode_count) = n-((sum-(n>>1))>>(k+1)) = 0.
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define FF_ENABLE_DEPRECATION_WARNINGS
attribute_deprecated int min_prediction_order
int channels
number of audio channels
static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
static const AVClass flac_encoder_class
static enum AVSampleFormat sample_fmts[]
static void init_frame(FlacEncodeContext *s, int nb_samples)
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
static int update_md5_sum(FlacEncodeContext *s, const void *samples)
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static av_cold int flac_encode_init(AVCodecContext *avctx)
static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
Write streaminfo metadata block to byte array.
#define FLAC_MAX_CHANNELS