Go to the documentation of this file.
39 #define FLAC_SUBFRAME_CONSTANT 0
40 #define FLAC_SUBFRAME_VERBATIM 1
41 #define FLAC_SUBFRAME_FIXED 8
42 #define FLAC_SUBFRAME_LPC 32
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
151 put_bits(&pb, 5,
s->avctx->bits_per_raw_sample - 1);
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);
175 count += ch * ((7+
bps+7)/8);
178 count += (( 2*
bps+1) * blocksize + 7) / 8;
180 count += ( ch*
bps * blocksize + 7) / 8;
200 target = (samplerate * block_time_ms) / 1000;
201 for (
i = 0;
i < 16;
i++) {
226 av_log(avctx,
AV_LOG_DEBUG,
" lpc type: Levinson-Durbin recursion with Welch window\n");
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");
314 for (
i = 1;
i < 12;
i++) {
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) {
340 s->samplerate = freq;
345 s->options.compression_level = 5;
349 level =
s->options.compression_level;
352 s->options.compression_level);
356 s->options.block_time_ms = ((
int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[
level];
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];
370 if (
s->options.prediction_order_method < 0)
377 if (
s->options.min_partition_order >
s->options.max_partition_order) {
379 s->options.min_partition_order,
s->options.max_partition_order);
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];
388 s->options.min_prediction_order = 0;
389 s->options.max_prediction_order = 0;
393 "invalid min prediction order %d, clamped to %d\n",
399 "invalid max prediction order %d, clamped to %d\n",
405 if (
s->options.max_prediction_order <
s->options.min_prediction_order) {
407 s->options.min_prediction_order,
s->options.max_prediction_order);
421 s->max_blocksize =
s->avctx->frame_size;
426 s->avctx->bits_per_raw_sample);
442 s->min_framesize =
s->max_framesize;
457 "output stream will have incorrect "
458 "channel layout.\n");
461 "will use Flac channel layout for "
485 for (
i = 0;
i < 16;
i++) {
489 frame->bs_code[1] = 0;
494 frame->blocksize = nb_samples;
495 if (
frame->blocksize <= 256) {
496 frame->bs_code[0] = 6;
499 frame->bs_code[0] = 7;
504 for (ch = 0; ch <
s->channels; ch++) {
508 sub->
obits =
s->avctx->bits_per_raw_sample;
516 frame->verbatim_only = 0;
528 s->avctx->bits_per_raw_sample;
530 #define COPY_SAMPLES(bits) do { \
531 const int ## bits ## _t *samples0 = samples; \
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; \
550 for (
i = 0;
i < n;
i++) {
551 unsigned v = ((unsigned)(res[
i]) << 1) ^ (res[
i] >> 31);
552 count += (v >> k) + 1 + k;
561 int p, porder, psize;
575 count +=
s->frame.blocksize * sub->
obits;
578 count += pred_order * sub->
obits;
582 count += 4 + 5 + pred_order *
s->options.lpc_coeff_precision;
589 psize =
s->frame.blocksize >> porder;
595 for (p = 0; p < 1 << porder; p++) {
600 part_end =
FFMIN(
s->frame.blocksize, part_end + psize);
608 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
620 sum2 = sum - (n >> 1);
622 return FFMIN(k, max_param);
628 int64_t bestbits = INT64_MAX;
631 for (k = 0; k <= max_param; k++) {
632 int64_t
bits = sums[k][
i];
633 if (
bits < bestbits) {
644 int n,
int pred_order,
int max_param,
int exact)
650 part = (1 << porder);
653 cnt = (n >> porder) - pred_order;
654 for (
i = 0;
i < part;
i++) {
657 all_bits += sums[k][
i];
677 const uint32_t *res, *res_end;
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++) {
687 uint64_t sum = (1LL + k) * (res_end - res);
688 while (res < res_end)
689 sum += *(res++) >> k;
693 while (res < res_end)
697 res_end += n >> pmax;
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];
716 const int32_t *
data,
int n,
int pred_order,
int exact)
730 for (
i = pred_order;
i < n;
i++)
731 udata[
i] = ((
unsigned)(
data[
i]) << 1) ^ (
data[
i] >> 31);
733 calc_sum_top(pmax, exact ? kmax : 0, udata, n, pred_order, sums);
736 bits[pmin] = UINT32_MAX;
739 if (
bits[
i] <
bits[opt_porder] || pmax == pmin) {
748 return bits[opt_porder];
765 s->frame.blocksize, pred_order);
767 s->frame.blocksize, pred_order);
771 bits += 4 + 5 + pred_order *
s->options.lpc_coeff_precision;
773 s->frame.blocksize, pred_order,
s->options.exact_rice_parameters);
783 for (
i = 0;
i < order;
i++)
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 ];
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 ];
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 ];
834 #define ENCODE_RESIDUAL_FIXED_WITH_RESIDUAL_LIMIT() \
836 for (int i = 0; i < order; i++) \
839 for (int i = order; i < n; i++) { \
840 if (smp[i] == INT32_MIN) \
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) \
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) \
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) \
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) \
889 #define LPC_ENCODE_WITH_RESIDUAL_LIMIT() \
891 for (int i = 0; i < order; 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]; \
899 if (tmp <= INT32_MIN || tmp > INT32_MAX) \
920 const int64_t *smp_33bps,
int len,
923 uint64_t max_residual_value = 0;
924 int64_t max_sample_value = ((int64_t)(1) << (
bps-1));
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;
934 }
else if (max_residual_value > INT32_MAX) {
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);
940 s->flac_dsp.lpc32_encode(res, smp,
len, order, coefs,
shift);
945 #define DEFAULT_TO_VERBATIM() \
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); \
956 int min_order, max_order, opt_order, omethod;
965 sub = &
frame->subframes[ch];
968 smp_33bps =
frame->samples_33bps;
969 n =
frame->blocksize;
972 if (sub->
obits > 32) {
973 for (
i = 1;
i < n;
i++)
974 if(smp_33bps[
i] != smp_33bps[0])
981 for (
i = 1;
i < n;
i++)
992 if (
frame->verbatim_only || n < 5) {
996 min_order =
s->options.min_prediction_order;
997 max_order =
s->options.max_prediction_order;
998 omethod =
s->options.prediction_order_method;
1008 bits[0] = UINT32_MAX;
1009 for (
i = min_order;
i <= max_order;
i++) {
1010 if (sub->
obits == 33) {
1013 }
else if (sub->
obits +
i >= 32) {
1022 if (opt_order == 0 &&
bits[0] == UINT32_MAX) {
1027 sub->
order = opt_order;
1029 if (sub->
order != max_order) {
1030 if (sub->
obits == 33)
1032 else if (sub->
obits +
i >= 32)
1043 if (sub->
obits == 33)
1048 for (
i = 0;
i < n;
i++)
1049 smp[
i] = smp_33bps[
i] >> 1;
1052 s->options.lpc_coeff_precision, coefs,
shift,
s->options.lpc_type,
1053 s->options.lpc_passes, omethod,
1059 int levels = 1 << omethod;
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)
1084 bits[0] = UINT32_MAX;
1085 for (
i = min_order-1;
i < max_order;
i++) {
1097 opt_order = min_order - 1 + (max_order-min_order)/3;
1101 int last = opt_order;
1103 if (i < min_order-1 || i >= max_order ||
bits[
i] < UINT32_MAX)
1115 if (
s->options.multi_dim_quant) {
1117 int i,
step, improved;
1118 int64_t best_score = INT64_MAX;
1121 qmax = (1 << (
s->options.lpc_coeff_precision - 1)) - 1;
1123 for (
i=0;
i<opt_order;
i++)
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);
1146 if (score < best_score) {
1148 memcpy(coefs[opt_order-1], lpc_try,
sizeof(*coefs));
1155 sub->
order = opt_order;
1194 if (
s->frame.bs_code[0] == 6)
1196 else if (
s->frame.bs_code[0] == 7)
1200 count += ((
s->sr_code[0] == 12) + (
s->sr_code[0] > 12) * 2) * 8;
1216 for (ch = 0; ch <
s->channels; ch++)
1219 count += (8 - (count & 7)) & 7;
1223 if (count > INT_MAX)
1231 int ch,
i, wasted_bits;
1233 for (ch = 0; ch <
s->channels; ch++) {
1236 if (sub->
obits > 32) {
1238 for (
i = 0;
i <
s->frame.blocksize;
i++) {
1239 v |=
s->frame.samples_33bps[
i];
1251 for (
i = 0;
i <
s->frame.blocksize;
i++)
1252 sub->
samples[
i] =
s->frame.samples_33bps[
i] >> v;
1256 for (
i = 0;
i <
s->frame.blocksize;
i++) {
1267 for (
i = 0;
i <
s->frame.blocksize;
i++)
1272 sub->
wasted = wasted_bits;
1273 sub->
obits -= wasted_bits;
1277 if (sub->
obits <= 17)
1284 int max_rice_param,
int bps)
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);
1305 for (
int i = 2;
i < n;
i++) {
1306 lt = (int64_t)left_ch[
i] - 2*(int64_t)left_ch[
i-1] + left_ch[
i-2];
1307 rt = (int64_t)right_ch[
i] - 2*(int64_t)right_ch[
i-1] + right_ch[
i-2];
1308 sum[2] +=
FFABS((lt + rt) >> 1);
1309 sum[3] +=
FFABS(lt - rt);
1310 sum[0] +=
FFABS(lt);
1311 sum[1] +=
FFABS(rt);
1315 for (
int i = 0;
i < 4;
i++) {
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];
1328 for (
int i = 1;
i < 4;
i++)
1329 if (score[
i] < score[best])
1343 int64_t *side_33bps;
1347 n =
frame->blocksize;
1349 right =
frame->subframes[1].samples;
1350 side_33bps =
frame->samples_33bps;
1352 if (
s->channels != 2) {
1357 if (
s->options.ch_mode < 0) {
1358 int max_rice_param = (1 <<
frame->subframes[0].rc.coding_mode) - 2;
1361 frame->ch_mode =
s->options.ch_mode;
1366 if(
s->avctx->bits_per_raw_sample == 32) {
1369 for (
int i = 0;
i < n;
i++) {
1372 side_33bps[
i] =
tmp - right[
i];
1374 frame->subframes[1].obits++;
1376 for (
int i = 0;
i < n;
i++)
1377 side_33bps[
i] = (int64_t)
left[
i] - right[
i];
1378 frame->subframes[1].obits++;
1380 for (
int i = 0;
i < n;
i++)
1381 side_33bps[
i] = (int64_t)
left[
i] - right[
i];
1382 frame->subframes[0].obits++;
1387 for (
int i = 0;
i < n;
i++) {
1390 right[
i] =
tmp - right[
i];
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++;
1398 for (
int i = 0;
i < n;
i++)
1400 frame->subframes[0].obits++;
1433 if (
frame->bs_code[0] == 6)
1435 else if (
frame->bs_code[0] == 7)
1438 if (
s->sr_code[0] == 12)
1440 else if (
s->sr_code[0] > 12)
1454 v = ((unsigned)(
i) << 1) ^ (
i >> 31);
1463 unsigned mask = UINT32_MAX >> (32-k);
1473 for (ch = 0; ch <
s->channels; ch++) {
1475 int p, porder, psize;
1489 if(sub->
obits == 33)
1491 else if(sub->
obits == 32)
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) {
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++)
1518 for (
int i = 0;
i < sub->
order;
i++)
1524 int cbits =
s->options.lpc_coeff_precision;
1527 for (
int i = 0;
i < sub->
order;
i++)
1536 psize =
s->frame.blocksize >> porder;
1541 for (p = 0; p < 1 << porder; p++) {
1544 while (res < part_end)
1577 int buf_size =
s->frame.blocksize *
s->channels *
1578 ((
s->avctx->bits_per_raw_sample + 7) / 8);
1580 if (
s->avctx->bits_per_raw_sample > 16 || HAVE_BIGENDIAN) {
1586 if (
s->avctx->bits_per_raw_sample <= 16) {
1587 buf = (
const uint8_t *)
samples;
1589 s->bdsp.bswap16_buf((uint16_t *)
s->md5_buffer,
1590 (
const uint16_t *)
samples, buf_size / 2);
1591 buf =
s->md5_buffer;
1593 }
else if (
s->avctx->bits_per_raw_sample <= 24) {
1596 uint8_t *
tmp =
s->md5_buffer;
1598 for (
i = 0;
i <
s->frame.blocksize *
s->channels;
i++) {
1602 buf =
s->md5_buffer;
1607 uint8_t *
tmp =
s->md5_buffer;
1609 for (
i = 0;
i <
s->frame.blocksize *
s->channels;
i++)
1611 buf =
s->md5_buffer;
1623 int frame_bytes, out_bytes,
ret;
1629 s->max_framesize =
s->max_encoded_framesize;
1640 avpkt->
pts =
s->next_pts;
1642 *got_packet_ptr = 1;
1668 if (frame_bytes < 0 || frame_bytes >
s->max_framesize) {
1669 s->frame.verbatim_only = 1;
1671 if (frame_bytes < 0) {
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;
1697 *got_packet_ptr = 1;
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,
"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,
"predm" },
1730 {
"ch_mode",
"Stereo decorrelation mode", offsetof(
FlacEncodeContext,
options.ch_mode),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1,
FLAC_CHMODE_MID_SIDE,
FLAGS,
"ch_mode" },
int frame_size
Number of samples per channel in an audio frame.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LPC_ENCODE_WITH_RESIDUAL_LIMIT()
#define PUT_UTF8(val, tmp, PUT_BYTE)
FFLPCType
LPC analysis type.
int32_t samples[FLAC_MAX_BLOCKSIZE]
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
static av_cold int flac_encode_init(AVCodecContext *avctx)
static int put_bytes_output(const PutBitContext *s)
int sample_rate
samples per second
int exact_rice_parameters
#define MAX_PARTITION_ORDER
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
static int encode_residual_fixed_with_residual_limit_33bps(int32_t *res, const int64_t *smp, int n, int order)
#define FF_CODEC_CAP_EOF_FLUSH
The encoder has AV_CODEC_CAP_DELAY set, but does not actually have delay - it only wants to be flushe...
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 min_shift, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
static void put_sbits(PutBitContext *pb, int n, int32_t value)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static int flac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
#define AV_CHANNEL_LAYOUT_2_2
This structure describes decoded (raw) audio or video data.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
@ FF_LPC_TYPE_CHOLESKY
Cholesky factorization.
int prediction_order_method
static int select_blocksize(int samplerate, int block_time_ms)
Set blocksize based on samplerate.
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static uint64_t find_subframe_rice_params(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
#define ORDER_METHOD_4LEVEL
AVCodec p
The public AVCodec.
AVChannelLayout ch_layout
Audio channel layout.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
#define DEFAULT_TO_VERBATIM()
static double val(void *priv, double ch)
FLACEncDSPContext flac_dsp
#define FF_CODEC_ENCODE_CB(func)
static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples)
Compute the end timestamp of a frame.
#define AV_CHANNEL_LAYOUT_SURROUND
@ FF_LPC_TYPE_DEFAULT
use the codec default LPC type
const int32_t ff_flac_blocksize_table[16]
static int lpc_encode_choose_datapath(FlacEncodeContext *s, int32_t bps, int32_t *res, const int32_t *smp, const int64_t *smp_33bps, int len, int order, int32_t *coefs, int shift)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static void write_subframes(FlacEncodeContext *s)
static const uint16_t mask[17]
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
int(* init)(AVBSFContext *ctx)
static int flac_get_max_frame_size(int blocksize, int ch, int bps)
Calculate an estimate for the maximum frame size based on verbatim mode.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static void remove_wasted_bits(FlacEncodeContext *s)
#define FLAC_SUBFRAME_LPC
#define COPY_SAMPLES(bits)
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)
#define FLAC_SUBFRAME_VERBATIM
#define CODEC_LONG_NAME(str)
@ FLAC_CHMODE_INDEPENDENT
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
#define FLAC_SUBFRAME_CONSTANT
const int ff_flac_sample_rate_table[16]
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
FlacSubframe subframes[FLAC_MAX_CHANNELS]
av_cold void ff_bswapdsp_init(BswapDSPContext *c)
int64_t samples_33bps[FLAC_MAX_BLOCKSIZE]
#define FLAC_SUBFRAME_FIXED
static av_always_inline int64_t ff_samples_to_time_base(const AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
const char * av_default_item_name(void *ptr)
Return the context name.
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
#define FLAC_STREAMINFO_SIZE
#define ORDER_METHOD_SEARCH
static void put_sbits63(PutBitContext *pb, int n, int64_t value)
static int estimate_stereo_mode(const int32_t *left_ch, const int32_t *right_ch, int n, int max_rice_param, int bps)
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
static uint64_t rice_count_exact(const int32_t *res, int n, int k)
int max_encoded_framesize
static int encode_residual_ch(FlacEncodeContext *s, int ch)
static int get_max_p_order(int max_porder, int n, int order)
#define ORDER_METHOD_8LEVEL
static int find_optimal_param_exact(uint64_t sums[32][MAX_PARTITIONS], int i, int max_param)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
unsigned int md5_buffer_size
static int encode_residual_fixed_with_residual_limit(int32_t *res, const int32_t *smp, int n, int order)
An AVChannelLayout holds information about the channel layout of audio data.
static int shift(int a, int b)
static void channel_decorrelation(FlacEncodeContext *s)
Perform stereo channel decorrelation.
@ FF_LPC_TYPE_NB
Not part of ABI.
enum AVSampleFormat sample_fmt
audio sample format
static int encode_frame(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])
int32_t residual[FLAC_MAX_BLOCKSIZE+11]
CompressionOptions options
static const uint8_t header[24]
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
static int write_frame(FlacEncodeContext *s, AVPacket *avpkt)
static const AVOption options[]
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
int32_t coefs[MAX_LPC_ORDER]
static void calc_sum_next(int level, uint64_t sums[32][MAX_PARTITIONS], int kmax)
void av_md5_init(AVMD5 *ctx)
Initialize MD5 hashing.
int nb_samples
number of audio samples (per channel) described by this frame
#define i(width, name, range_min, range_max)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static void write_utf8(PutBitContext *pb, uint32_t val)
#define AV_CHANNEL_LAYOUT_QUAD
static int count_frame_header(FlacEncodeContext *s)
const FFCodec ff_flac_encoder
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
AVSampleFormat
Audio sample formats.
static void write_frame_header(FlacEncodeContext *s)
av_cold void ff_flacencdsp_init(FLACEncDSPContext *c)
static void write_frame_footer(FlacEncodeContext *s)
@ AV_SAMPLE_FMT_S16
signed 16 bits
const char * name
Name of the codec implementation.
static void copy_samples(FlacEncodeContext *s, const void *samples)
Copy channel-interleaved input samples into separate subframes.
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
static int lpc_encode_with_residual_limit_33bps(int32_t *res, const int64_t *smp, int len, int order, int32_t *coefs, int shift)
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)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n, int order)
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
#define FLAC_MAX_CHANNELS
#define MAX_LPC_PRECISION
main external API structure.
#define ENCODE_RESIDUAL_FIXED_WITH_RESIDUAL_LIMIT()
uint64_t rc_sums[32][MAX_PARTITIONS]
uint32_t rc_udata[FLAC_MAX_BLOCKSIZE]
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
static uint64_t subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub, int pred_order)
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Filter the word “frame” indicates either a video frame or a group of audio samples
static int lpc_encode_with_residual_limit(int32_t *res, const int32_t *smp, int len, int order, int32_t *coefs, int shift)
void av_md5_update(AVMD5 *ctx, const uint8_t *src, size_t len)
Update hash value.
static void init_frame(FlacEncodeContext *s, int nb_samples)
int params[MAX_PARTITIONS]
#define FLAC_MAX_BLOCKSIZE
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
This structure stores compressed data.
enum CodingMode coding_mode
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
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_cold void dprint_compression_options(FlacEncodeContext *s)
static int update_md5_sum(FlacEncodeContext *s, const void *samples)
static av_cold int flac_encode_close(AVCodecContext *avctx)
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
static const AVClass flac_encoder_class
static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
Write streaminfo metadata block to byte array.
#define AV_CHANNEL_LAYOUT_5POINT0
#define ORDER_METHOD_2LEVEL
#define FLAC_MIN_BLOCKSIZE
static void set_sr_golomb_flac(PutBitContext *pb, int i, int k)
@ FF_LPC_TYPE_NONE
do not use LPC prediction or use all zero coefficients
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
#define AV_CHANNEL_LAYOUT_5POINT1
@ AV_SAMPLE_FMT_S32
signed 32 bits
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
#define rice_encode_count(sum, n, k)
@ FF_LPC_TYPE_FIXED
fixed LPC coefficients