130 #define OFFSET(x) offsetof(ShowCWTContext, x)
131 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
204 for (
int n = 0; n <
s->nb_threads; n++)
210 for (
int n = 0; n <
s->nb_threads; n++)
216 for (
int n = 0; n <
s->frequency_band_count; n++)
254 int frequency_band_count,
255 float frequency_range,
256 float frequency_offset,
257 int frequency_scale,
float deviation)
262 for (
int y = 0; y < frequency_band_count; y++) {
263 float frequency = frequency_range * (1.f - (
float)y / frequency_band_count) + frequency_offset;
264 float frequency_derivative = frequency_range / frequency_band_count;
266 switch (frequency_scale) {
268 frequency =
powf(2.
f, frequency);
269 frequency_derivative *= logf(2.
f) * frequency;
272 frequency = 600.f * sinhf(frequency / 6.
f);
273 frequency_derivative *=
sqrtf(frequency * frequency + 360000.
f) / 6.f;
276 frequency = 700.f * (
powf(10.
f, frequency / 2595.
f) - 1.f);
277 frequency_derivative *= (frequency + 700.f) * logf(10.
f) / 2595.f;
280 frequency = 676170.4f / (47.06538f -
expf(frequency * 0.08950404
f)) - 14678.49
f;
281 frequency_derivative *= (frequency * frequency + 14990.4f * frequency + 4577850.f) / 160514.
f;
284 frequency = frequency * frequency;
285 frequency_derivative *= 2.f *
sqrtf(frequency);
288 frequency = frequency * frequency * frequency;
289 frequency_derivative *= 3.f *
powf(frequency, 2.
f / 3.
f);
292 frequency = frequency * frequency * frequency * frequency;
293 frequency_derivative *= 4.f *
powf(frequency, 3.
f / 4.
f);
300 ret = 1.f / (frequency_derivative * deviation);
308 const float max =
s->maximum_intensity;
309 const float min =
s->minimum_intensity;
342 const int hop_size =
s->hop_size;
344 float *cache = (
float *)
s->cache->extended_data[ch];
347 const int offset = (
s->input_padding_size - hop_size) >> 1;
357 if (fin &&
s->hop_index + fin->
nb_samples < hop_size)
360 memset(
src, 0,
sizeof(
float) *
s->fft_size);
361 for (
int n = 0; n < hop_size; n++)
364 s->tx_fn(
s->fft[jobnr], dst,
src,
sizeof(*
src));
369 #define DRAW_BAR_COLOR(x) \
376 float mul = (Y - ht) * bh[0]; \
377 dstY[x] = av_clip_uint8(lrintf(Y * mul * 255.f)); \
378 dstU[x] = av_clip_uint8(lrintf((U-0.5f) * 128.f + 128)); \
379 dstV[x] = av_clip_uint8(lrintf((V-0.5f) * 128.f + 128)); \
384 float Y,
float U,
float V)
386 float *bh = ((
float *)
s->bh_out->extended_data[0]) + y;
387 const ptrdiff_t ylinesize =
s->outpicref->linesize[0];
388 const ptrdiff_t ulinesize =
s->outpicref->linesize[1];
389 const ptrdiff_t vlinesize =
s->outpicref->linesize[2];
390 const int direction =
s->direction;
391 const int bar_size =
s->bar_size;
392 const float rcp_bar_h = 1.f / bar_size;
393 uint8_t *dstY, *dstU, *dstV;
394 const int w_1 =
s->w - 1;
396 bh[0] = 1.f / (
Y + 0.0001f);
399 dstY =
s->outpicref->data[0] + y * ylinesize;
400 dstU =
s->outpicref->data[1] + y * ulinesize;
401 dstV =
s->outpicref->data[2] + y * vlinesize;
402 for (
int x = 0; x < bar_size; x++) {
403 float ht = (bar_size - x) * rcp_bar_h;
408 dstY =
s->outpicref->data[0] + y * ylinesize;
409 dstU =
s->outpicref->data[1] + y * ulinesize;
410 dstV =
s->outpicref->data[2] + y * vlinesize;
411 for (
int x = 0; x < bar_size; x++) {
412 float ht = x * rcp_bar_h;
417 dstY =
s->outpicref->data[0] + w_1 - y;
418 dstU =
s->outpicref->data[1] + w_1 - y;
419 dstV =
s->outpicref->data[2] + w_1 - y;
420 for (
int x = 0; x < bar_size; x++) {
421 float ht = (bar_size - x) * rcp_bar_h;
429 dstY =
s->outpicref->data[0] + w_1 - y + ylinesize * (
s->h - 1 - bar_size);
430 dstU =
s->outpicref->data[1] + w_1 - y + ulinesize * (
s->h - 1 - bar_size);
431 dstV =
s->outpicref->data[2] + w_1 - y + vlinesize * (
s->h - 1 - bar_size);
432 for (
int x = 0; x < bar_size; x++) {
433 float ht = x * rcp_bar_h;
446 const ptrdiff_t ylinesize =
s->outpicref->linesize[0];
447 const ptrdiff_t ulinesize =
s->outpicref->linesize[1];
448 const ptrdiff_t vlinesize =
s->outpicref->linesize[2];
449 const ptrdiff_t alinesize =
s->outpicref->linesize[3];
450 const float log_factor = 1.f/logf(
s->logarithmic_basis);
451 const int count =
s->frequency_band_count;
452 const int start = (count * jobnr) / nb_jobs;
453 const int end = (count * (jobnr+1)) / nb_jobs;
454 const int nb_channels =
s->nb_channels;
455 const int iscale =
s->intensity_scale;
456 const int ihop_index =
s->ihop_index;
457 const int ihop_size =
s->ihop_size;
458 const float rotation =
s->rotation;
459 const int direction =
s->direction;
460 uint8_t *dstY, *dstU, *dstV, *dstA;
461 const int bar_size =
s->bar_size;
462 const int mode =
s->mode;
463 const int w_1 =
s->w - 1;
464 const int x =
s->pos;
467 for (
int y = start; y < end; y++) {
469 0 * ihop_size + ihop_index;
474 dstY =
s->outpicref->data[0] + y * ylinesize;
475 dstU =
s->outpicref->data[1] + y * ulinesize;
476 dstV =
s->outpicref->data[2] + y * vlinesize;
477 dstA =
s->outpicref->data[3] ?
s->outpicref->data[3] + y * alinesize :
NULL;
481 dstY =
s->outpicref->data[0] + x * ylinesize + w_1 - y;
482 dstU =
s->outpicref->data[1] + x * ulinesize + w_1 - y;
483 dstV =
s->outpicref->data[2] + x * vlinesize + w_1 - y;
484 dstA =
s->outpicref->data[3] ?
s->outpicref->data[3] + x * alinesize + w_1 - y :
NULL;
494 switch (
s->direction) {
496 memmove(dstY, dstY + 1, w_1);
497 memmove(dstU, dstU + 1, w_1);
498 memmove(dstV, dstV + 1, w_1);
500 memmove(dstA, dstA + 1, w_1);
503 memmove(dstY + 1, dstY, w_1);
504 memmove(dstU + 1, dstU, w_1);
505 memmove(dstV + 1, dstV, w_1);
507 memmove(dstA + 1, dstA, w_1);
529 u = hypotf(
src[0].re,
src[0].im);
543 U = 0.5f + 0.5f * z *
u;
544 V = 0.5f + 0.5f * z * v;
558 const int nb_channels =
s->nb_channels;
559 const float yf = 1.f / nb_channels;
563 for (
int ch = 0; ch < nb_channels; ch++) {
567 z = hypotf(srcn[0].re, srcn[0].im);
571 U += z * yf *
sinf(2.
f *
M_PI * (ch * yf + rotation));
572 V += z * yf *
cosf(2.
f *
M_PI * (ch * yf + rotation));
586 Y = hypotf(
src[0].re,
src[0].im);
589 U = 0.5f + 0.5f *
U *
Y /
M_PI;
602 Y = 0.5f + 0.5f *
Y /
M_PI;
611 Y = hypotf(
src[0].re,
src[0].im);
630 const int ch = *(
int *)
arg;
634 const int output_padding_size =
s->output_padding_size;
635 const int input_padding_size =
s->input_padding_size;
636 const float scale = 1.f / input_padding_size;
637 const int ihop_size =
s->ihop_size;
638 const int count =
s->frequency_band_count;
639 const int start = (count * jobnr) / nb_jobs;
640 const int end = (count * (jobnr+1)) / nb_jobs;
642 for (
int y = start; y < end; y++) {
648 const unsigned *
index = (
const unsigned *)
s->index;
649 const int kernel_start =
s->kernel_start[y];
650 const int kernel_stop =
s->kernel_stop[y];
651 const int kernel_range = kernel_stop - kernel_start + 1;
654 if (kernel_start >= 0) {
656 memcpy(srcx, fft_out + kernel_start,
sizeof(*fft_out) * kernel_range);
659 memcpy(srcx+
offset, fft_out,
sizeof(*fft_out) * (kernel_range-
offset));
660 memcpy(srcx, fft_out+input_padding_size-
offset,
sizeof(*fft_out)*
offset);
663 s->fdsp->vector_fmul_scalar((
float *)srcx, (
const float *)srcx,
scale,
FFALIGN(kernel_range * 2, 4));
664 s->fdsp->vector_fmul((
float *)dstx, (
const float *)srcx,
665 (
const float *)kernel,
FFALIGN(kernel_range * 2, 16));
667 memset(isrc, 0,
sizeof(*isrc) * output_padding_size);
669 for (
int i = 0;
i < kernel_range;
i++) {
670 const unsigned n =
index[
i + kernel_start];
672 isrc[n].re += dstx[
i].re;
673 isrc[n].im += dstx[
i].im;
676 for (
int i = 0;
i < kernel_range;
i++) {
677 const unsigned n = (
i-kernel_start) & (output_padding_size-1);
679 isrc[n].re += dstx[
i].re;
680 isrc[n].im += dstx[
i].im;
684 s->itx_fn(
s->ifft[jobnr], idst, isrc,
sizeof(*isrc));
686 memcpy(chout, idst,
sizeof(*chout) * ihop_size);
687 for (
int n = 0; n < ihop_size; n++) {
688 chout[n].
re += over[n].
re;
689 chout[n].
im += over[n].
im;
691 memcpy(over, idst + ihop_size,
sizeof(*over) * ihop_size);
700 const int size =
s->input_padding_size;
701 const int output_sample_count =
s->output_sample_count;
702 const int fsize =
s->frequency_band_count;
703 int *kernel_start =
s->kernel_start;
704 int *kernel_stop =
s->kernel_stop;
705 unsigned *
index =
s->index;
706 int range_min = INT_MAX;
707 int range_max = 0,
ret = 0;
714 for (
int y = 0; y <
fsize; y++) {
716 int start = INT_MIN, stop = INT_MAX;
717 const float frequency =
s->frequency_band[y*2];
718 const float deviation = 1.f / (
s->frequency_band[y*2+1] *
719 output_sample_count);
724 memset(tkernel, 0,
size *
sizeof(*tkernel));
725 for (
int n =
a; n <
b; n++) {
726 float ff,
f = n+0.5f-frequency;
728 ff =
expf(-
f*
f*deviation);
729 tkernel[n+
range] = ff;
732 for (
int n =
a; n <
b; n++) {
733 if (tkernel[n+
range] != 0.
f) {
734 if (tkernel[n+
range] > FLT_MIN)
741 for (
int n =
b; n >=
a; n--) {
742 if (tkernel[n+
range] != 0.
f) {
743 if (tkernel[n+
range] > FLT_MIN)
750 if (start == INT_MIN || stop == INT_MAX) {
755 kernel_start[y] = start;
756 kernel_stop[y] = stop;
764 for (
int n = 0; n <= stop - start; n++) {
765 kernel[n].
re = tkernel[n+
range+start];
766 kernel[n].
im = tkernel[n+
range+start];
769 range_min =
FFMIN(range_min, stop+1-start);
770 range_max =
FFMAX(range_max, stop+1-start);
772 s->kernel[y] = kernel;
775 for (
int n = 0; n <
size; n++)
776 index[n] = n & (
s->output_padding_size - 1);
791 float maximum_frequency =
fminf(
s->maximum_frequency,
inlink->sample_rate * 0.5f);
792 float minimum_frequency =
s->minimum_frequency;
796 if (minimum_frequency >= maximum_frequency) {
798 minimum_frequency, maximum_frequency);
808 switch (
s->direction) {
811 s->bar_size =
s->w *
s->bar_ratio;
812 s->frequency_band_count =
s->h;
816 s->bar_size =
s->h *
s->bar_ratio;
817 s->frequency_band_count =
s->w;
821 switch (
s->frequency_scale) {
823 minimum_frequency = logf(minimum_frequency) / logf(2.
f);
824 maximum_frequency = logf(maximum_frequency) / logf(2.
f);
827 minimum_frequency = 6.f * asinhf(minimum_frequency / 600.
f);
828 maximum_frequency = 6.f * asinhf(maximum_frequency / 600.
f);
831 minimum_frequency = 2595.f *
log10f(1.
f + minimum_frequency / 700.
f);
832 maximum_frequency = 2595.f *
log10f(1.
f + maximum_frequency / 700.
f);
835 minimum_frequency = 11.17268f * logf(1.
f + (46.06538
f * minimum_frequency) / (minimum_frequency + 14678.49
f));
836 maximum_frequency = 11.17268f * logf(1.
f + (46.06538
f * maximum_frequency) / (maximum_frequency + 14678.49
f));
839 minimum_frequency =
sqrtf(minimum_frequency);
840 maximum_frequency =
sqrtf(maximum_frequency);
843 minimum_frequency =
cbrtf(minimum_frequency);
844 maximum_frequency =
cbrtf(maximum_frequency);
847 minimum_frequency =
powf(minimum_frequency, 0.25
f);
848 maximum_frequency =
powf(maximum_frequency, 0.25
f);
852 s->frequency_band =
av_calloc(
s->frequency_band_count,
853 sizeof(*
s->frequency_band) * 2);
854 if (!
s->frequency_band)
857 s->nb_consumed_samples =
inlink->sample_rate *
859 s->frequency_band_count, maximum_frequency - minimum_frequency,
860 minimum_frequency,
s->frequency_scale,
s->deviation);
861 s->nb_consumed_samples =
FFMIN(
s->nb_consumed_samples, 65536);
864 s->nb_channels =
inlink->ch_layout.nb_channels;
868 s->input_sample_count = 1 << (32 -
ff_clz(
s->nb_consumed_samples));
869 s->input_padding_size = 1 << (32 -
ff_clz(
s->input_sample_count));
871 s->output_padding_size = 1 << (32 -
ff_clz(
s->output_sample_count));
873 s->hop_size =
s->input_sample_count;
874 s->ihop_size =
s->output_padding_size >> 1;
887 for (
int n = 0; n <
s->nb_threads; n++) {
897 for (
int n = 0; n <
s->nb_threads; n++) {
908 s->kernel =
av_calloc(
s->frequency_band_count,
sizeof(*
s->kernel));
915 s->index =
av_calloc(
s->input_padding_size,
sizeof(*
s->index));
916 s->kernel_start =
av_calloc(
s->frequency_band_count,
sizeof(*
s->kernel_start));
917 s->kernel_stop =
av_calloc(
s->frequency_band_count,
sizeof(*
s->kernel_stop));
918 if (!
s->outpicref || !
s->fft_in || !
s->fft_out || !
s->src_x || !
s->dst_x || !
s->over ||
919 !
s->ifft_in || !
s->ifft_out || !
s->kernel_start || !
s->kernel_stop || !
s->ch_out ||
920 !
s->cache || !
s->index || !
s->bh_out || !
s->kernel)
923 s->ch_out->format =
inlink->format;
924 s->ch_out->nb_samples = 2 *
s->ihop_size *
inlink->ch_layout.nb_channels;
925 s->ch_out->ch_layout.nb_channels =
s->frequency_band_count;
930 s->ifft_in->format =
inlink->format;
931 s->ifft_in->nb_samples =
s->ifft_size * 2;
932 s->ifft_in->ch_layout.nb_channels =
s->nb_threads;
937 s->ifft_out->format =
inlink->format;
938 s->ifft_out->nb_samples =
s->ifft_size * 2;
939 s->ifft_out->ch_layout.nb_channels =
s->nb_threads;
944 s->src_x->format =
inlink->format;
945 s->src_x->nb_samples =
s->fft_size * 2;
946 s->src_x->ch_layout.nb_channels =
s->nb_threads;
951 s->dst_x->format =
inlink->format;
952 s->dst_x->nb_samples =
s->fft_size * 2;
953 s->dst_x->ch_layout.nb_channels =
s->nb_threads;
958 s->outpicref->sample_aspect_ratio = (
AVRational){1,1};
960 for (
int y = 0; y < outlink->
h; y++) {
961 memset(
s->outpicref->data[0] + y *
s->outpicref->linesize[0], 0, outlink->
w);
962 memset(
s->outpicref->data[1] + y *
s->outpicref->linesize[1], 128, outlink->
w);
963 memset(
s->outpicref->data[2] + y *
s->outpicref->linesize[2], 128, outlink->
w);
964 if (
s->outpicref->data[3])
965 memset(
s->outpicref->data[3] + y *
s->outpicref->linesize[3], 0, outlink->
w);
971 for (
int n = 0; n <
s->frequency_band_count; n++) {
972 s->frequency_band[2*n ] *=
factor;
973 s->frequency_band[2*n+1] *=
factor;
985 switch (
s->direction) {
987 s->pos =
s->bar_size;
990 s->pos =
FFMAX(0,
s->w - 2 -
s->bar_size);
993 s->pos =
s->bar_size;
996 s->pos =
FFMAX(0,
s->h - 2 -
s->bar_size);
1001 if (strcmp(
s->rate_str,
"auto")) {
1004 s->frame_rate =
s->auto_frame_rate;
1021 const int nb_planes = 3 + (
s->outpicref->data[3] !=
NULL);
1026 switch (
s->direction) {
1028 for (
int p = 0; p < nb_planes; p++) {
1029 ptrdiff_t linesize =
s->outpicref->linesize[p];
1031 for (
int y =
s->h - 1; y >
s->bar_size; y--) {
1032 uint8_t *dst =
s->outpicref->data[p] + y * linesize;
1034 memmove(dst, dst - linesize,
s->w);
1039 for (
int p = 0; p < nb_planes; p++) {
1040 ptrdiff_t linesize =
s->outpicref->linesize[p];
1042 for (
int y = 0; y <
s->h - 2 -
s->bar_size; y++) {
1043 uint8_t *dst =
s->outpicref->data[p] + y * linesize;
1045 memmove(dst, dst + linesize,
s->w);
1058 switch (
s->direction) {
1061 if (
s->pos >=
s->w) {
1062 s->pos =
s->bar_size;
1069 s->pos =
FFMAX(0,
s->w - 2 -
s->bar_size);
1075 if (
s->pos >=
s->h) {
1076 s->pos =
s->bar_size;
1083 s->pos =
FFMAX(0,
s->h - 2 -
s->bar_size);
1090 switch (
s->direction) {
1093 s->pos =
s->bar_size;
1096 s->pos =
FFMAX(0,
s->w - 2 -
s->bar_size);
1099 s->pos =
FFMAX(0,
s->h - 2 -
s->bar_size);
1106 switch (
s->direction) {
1108 for (
int p = 0; p < nb_planes; p++) {
1109 ptrdiff_t linesize =
s->outpicref->linesize[p];
1110 const int size =
s->w -
s->pos;
1111 const int fill = p > 0 && p < 3 ? 128 : 0;
1112 const int x =
s->pos;
1114 for (
int y = 0; y <
s->h; y++) {
1115 uint8_t *dst =
s->outpicref->data[p] + y * linesize + x;
1117 memset(dst, fill,
size);
1122 for (
int p = 0; p < nb_planes; p++) {
1123 ptrdiff_t linesize =
s->outpicref->linesize[p];
1124 const int size =
s->w -
s->pos;
1125 const int fill = p > 0 && p < 3 ? 128 : 0;
1127 for (
int y = 0; y <
s->h; y++) {
1128 uint8_t *dst =
s->outpicref->data[p] + y * linesize;
1130 memset(dst, fill,
size);
1135 for (
int p = 0; p < nb_planes; p++) {
1136 ptrdiff_t linesize =
s->outpicref->linesize[p];
1137 const int fill = p > 0 && p < 3 ? 128 : 0;
1139 for (
int y =
s->pos; y < s->
h; y++) {
1140 uint8_t *dst =
s->outpicref->data[p] + y * linesize;
1142 memset(dst, fill,
s->w);
1147 for (
int p = 0; p < nb_planes; p++) {
1148 ptrdiff_t linesize =
s->outpicref->linesize[p];
1149 const int fill = p > 0 && p < 3 ? 128 : 0;
1151 for (
int y =
s->h -
s->pos; y >= 0; y--) {
1152 uint8_t *dst =
s->outpicref->data[p] + y * linesize;
1154 memset(dst, fill,
s->w);
1164 int64_t pts_offset =
s->new_frame ? 0LL :
av_rescale(
s->ihop_index,
s->hop_size,
s->ihop_size);
1165 const int offset = (
s->input_padding_size -
s->hop_size) >> 1;
1169 s->outpicref->duration = 1;
1173 if (
s->ihop_index >=
s->ihop_size)
1174 s->ihop_index =
s->hop_index = 0;
1179 if (
s->old_pts <
s->outpicref->pts) {
1189 s->old_pts =
s->outpicref->pts;
1205 const int count =
s->nb_channels;
1206 const int start = (count * jobnr) / nb_jobs;
1207 const int end = (count * (jobnr+1)) / nb_jobs;
1209 for (
int ch = start; ch < end; ch++)
1228 if (
s->hop_index <
s->hop_size) {
1235 if (
ret > 0 ||
s->eof) {
1237 FFMIN(
s->nb_threads,
s->nb_channels));
1239 if (
s->hop_index == 0)
1240 s->in_pts = fin->
pts;
1244 s->hop_index =
s->hop_size;
1249 if (
s->hop_index >=
s->hop_size ||
s->ihop_index > 0) {
1250 for (
int ch = 0; ch <
s->nb_channels &&
s->ihop_index == 0; ch++) {
1278 s->hop_index >=
s->hop_size ||
s->eof) {
1301 .description =
NULL_IF_CONFIG_SMALL(
"Convert input audio to a CWT (Continuous Wavelet Transform) spectrum video output."),
1308 .priv_class = &showcwt_class,