130 #define OFFSET(x) offsetof(ShowCWTContext, x)
131 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
205 for (
int n = 0; n <
s->nb_threads; n++)
211 for (
int n = 0; n <
s->nb_threads; n++)
217 for (
int n = 0; n <
s->frequency_band_count; n++)
255 int frequency_band_count,
256 float frequency_range,
257 float frequency_offset,
258 int frequency_scale,
float deviation)
263 for (
int y = 0; y < frequency_band_count; y++) {
264 float frequency = frequency_range * (1.f - (
float)y / frequency_band_count) + frequency_offset;
265 float frequency_derivative = frequency_range / frequency_band_count;
267 switch (frequency_scale) {
269 frequency =
powf(2.
f, frequency);
270 frequency_derivative *= logf(2.
f) * frequency;
273 frequency = 600.f * sinhf(frequency / 6.
f);
274 frequency_derivative *=
sqrtf(frequency * frequency + 360000.
f) / 6.f;
277 frequency = 700.f * (
powf(10.
f, frequency / 2595.
f) - 1.f);
278 frequency_derivative *= (frequency + 700.f) * logf(10.
f) / 2595.f;
281 frequency = 676170.4f / (47.06538f -
expf(frequency * 0.08950404
f)) - 14678.49
f;
282 frequency_derivative *= (frequency * frequency + 14990.4f * frequency + 4577850.f) / 160514.
f;
285 frequency = frequency * frequency;
286 frequency_derivative *= 2.f *
sqrtf(frequency);
289 frequency = frequency * frequency * frequency;
290 frequency_derivative *= 3.f *
powf(frequency, 2.
f / 3.
f);
293 frequency = frequency * frequency * frequency * frequency;
294 frequency_derivative *= 4.f *
powf(frequency, 3.
f / 4.
f);
297 frequency = 2.f *
powf(frequency, 3.
f / 2.
f) / 3.f;
298 frequency_derivative *=
sqrtf(frequency);
305 ret = 1.f / (frequency_derivative * deviation);
313 const float max =
s->maximum_intensity;
314 const float min =
s->minimum_intensity;
347 const int hop_size =
s->hop_size;
349 float *cache = (
float *)
s->cache->extended_data[ch];
352 const int offset = (
s->input_padding_size - hop_size) >> 1;
362 if (fin &&
s->hop_index + fin->
nb_samples < hop_size)
365 memset(
src, 0,
sizeof(
float) *
s->fft_size);
366 for (
int n = 0; n < hop_size; n++)
374 #define DRAW_BAR_COLOR(x) \
381 float mul = (Y - ht) * bh[0]; \
382 dstY[x] = av_clip_uint8(lrintf(Y * mul * 255.f)); \
383 dstU[x] = av_clip_uint8(lrintf((U-0.5f) * 128.f + 128)); \
384 dstV[x] = av_clip_uint8(lrintf((V-0.5f) * 128.f + 128)); \
389 float Y,
float U,
float V)
391 float *bh = ((
float *)
s->bh_out->extended_data[0]) + y;
392 const ptrdiff_t ylinesize =
s->outpicref->linesize[0];
393 const ptrdiff_t ulinesize =
s->outpicref->linesize[1];
394 const ptrdiff_t vlinesize =
s->outpicref->linesize[2];
395 const int direction =
s->direction;
396 const int sono_size =
s->sono_size;
397 const int bar_size =
s->bar_size;
398 const float rcp_bar_h = 1.f / bar_size;
399 uint8_t *dstY, *dstU, *dstV;
402 bh[0] = 1.f / (
Y + 0.0001f);
405 dstY =
s->outpicref->data[0] + y * ylinesize;
406 dstU =
s->outpicref->data[1] + y * ulinesize;
407 dstV =
s->outpicref->data[2] + y * vlinesize;
408 for (
int x = 0; x < bar_size; x++) {
409 float ht = (bar_size - x) * rcp_bar_h;
414 dstY =
s->outpicref->data[0] + y * ylinesize;
415 dstU =
s->outpicref->data[1] + y * ulinesize;
416 dstV =
s->outpicref->data[2] + y * vlinesize;
417 for (
int x = 0; x < bar_size; x++) {
418 float ht = x * rcp_bar_h;
423 dstY =
s->outpicref->data[0] +
w - 1 - y;
424 dstU =
s->outpicref->data[1] +
w - 1 - y;
425 dstV =
s->outpicref->data[2] +
w - 1 - y;
426 for (
int x = 0; x < bar_size; x++) {
427 float ht = (bar_size - x) * rcp_bar_h;
435 dstY =
s->outpicref->data[0] +
w - 1 - y + ylinesize * sono_size;
436 dstU =
s->outpicref->data[1] +
w - 1 - y + ulinesize * sono_size;
437 dstV =
s->outpicref->data[2] +
w - 1 - y + vlinesize * sono_size;
438 for (
int x = 0; x < bar_size; x++) {
439 float ht = x * rcp_bar_h;
452 const ptrdiff_t ylinesize =
s->outpicref->linesize[0];
453 const ptrdiff_t ulinesize =
s->outpicref->linesize[1];
454 const ptrdiff_t vlinesize =
s->outpicref->linesize[2];
455 const ptrdiff_t alinesize =
s->outpicref->linesize[3];
456 const float log_factor = 1.f/logf(
s->logarithmic_basis);
457 const int count =
s->frequency_band_count;
458 const int start = (count * jobnr) / nb_jobs;
459 const int end = (count * (jobnr+1)) / nb_jobs;
460 const int nb_channels =
s->nb_channels;
461 const int iscale =
s->intensity_scale;
462 const int ihop_index =
s->ihop_index;
463 const int ihop_size =
s->ihop_size;
464 const float rotation =
s->rotation;
465 const int direction =
s->direction;
466 uint8_t *dstY, *dstU, *dstV, *dstA;
467 const int sono_size =
s->sono_size;
468 const int bar_size =
s->bar_size;
469 const int mode =
s->mode;
470 const int w_1 =
s->w - 1;
471 const int x =
s->pos;
474 for (
int y = start; y < end; y++) {
476 0 * ihop_size + ihop_index;
484 dstY =
s->outpicref->data[0] + y * ylinesize;
485 dstU =
s->outpicref->data[1] + y * ulinesize;
486 dstV =
s->outpicref->data[2] + y * vlinesize;
487 dstA =
s->outpicref->data[3] ?
s->outpicref->data[3] + y * alinesize :
NULL;
491 dstY =
s->outpicref->data[0] + x * ylinesize + w_1 - y;
492 dstU =
s->outpicref->data[1] + x * ulinesize + w_1 - y;
493 dstV =
s->outpicref->data[2] + x * vlinesize + w_1 - y;
494 dstA =
s->outpicref->data[3] ?
s->outpicref->data[3] + x * alinesize + w_1 - y :
NULL;
504 switch (
s->direction) {
506 memmove(dstY, dstY + 1, w_1);
507 memmove(dstU, dstU + 1, w_1);
508 memmove(dstV, dstV + 1, w_1);
510 memmove(dstA, dstA + 1, w_1);
513 memmove(dstY + 1, dstY, w_1);
514 memmove(dstU + 1, dstU, w_1);
515 memmove(dstV + 1, dstV, w_1);
517 memmove(dstA + 1, dstA, w_1);
540 u = hypotf(
src[0].re,
src[0].im);
554 U = 0.5f + 0.5f * z *
u;
555 V = 0.5f + 0.5f * z * v;
571 const int nb_channels =
s->nb_channels;
572 const float yf = 1.f / nb_channels;
576 for (
int ch = 0; ch < nb_channels; ch++) {
580 z = hypotf(srcn[0].re, srcn[0].im);
584 U += z * yf *
sinf(2.
f *
M_PI * (ch * yf + rotation));
585 V += z * yf *
cosf(2.
f *
M_PI * (ch * yf + rotation));
601 Y = hypotf(
src[0].re,
src[0].im);
604 U = 0.5f + 0.5f *
U *
Y /
M_PI;
620 Y = 0.5f + 0.5f *
Y /
M_PI;
632 Y = hypotf(
src[0].re,
src[0].im);
653 const int ch = *(
int *)
arg;
657 const int output_padding_size =
s->output_padding_size;
658 const int input_padding_size =
s->input_padding_size;
659 const float scale = 1.f / input_padding_size;
660 const int ihop_size =
s->ihop_size;
661 const int count =
s->frequency_band_count;
662 const int start = (count * jobnr) / nb_jobs;
663 const int end = (count * (jobnr+1)) / nb_jobs;
665 for (
int y = start; y < end; y++) {
671 const unsigned *
index = (
const unsigned *)
s->index;
672 const int kernel_start =
s->kernel_start[y];
673 const int kernel_stop =
s->kernel_stop[y];
674 const int kernel_range = kernel_stop - kernel_start + 1;
677 if (kernel_start >= 0) {
679 memcpy(srcx, fft_out + kernel_start,
sizeof(*fft_out) * kernel_range);
682 memcpy(srcx+
offset, fft_out,
sizeof(*fft_out) * (kernel_range-
offset));
683 memcpy(srcx, fft_out+input_padding_size-
offset,
sizeof(*fft_out)*
offset);
686 s->fdsp->vector_fmul_scalar((
float *)srcx, (
const float *)srcx,
scale,
FFALIGN(kernel_range * 2, 4));
687 s->fdsp->vector_fmul((
float *)dstx, (
const float *)srcx,
688 (
const float *)kernel,
FFALIGN(kernel_range * 2, 16));
690 memset(isrc, 0,
sizeof(*isrc) * output_padding_size);
692 const unsigned *kindex =
index + kernel_start;
693 for (
int i = 0;
i < kernel_range;
i++) {
694 const unsigned n = kindex[
i];
696 isrc[n].re += dstx[
i].re;
697 isrc[n].im += dstx[
i].im;
700 for (
int i = 0;
i < kernel_range;
i++) {
701 const unsigned n = (
i-kernel_start) & (output_padding_size-1);
703 isrc[n].re += dstx[
i].re;
704 isrc[n].im += dstx[
i].im;
708 s->itx_fn(
s->ifft[jobnr], idst, isrc,
sizeof(*isrc));
710 memcpy(chout, idst,
sizeof(*chout) * ihop_size);
711 for (
int n = 0; n < ihop_size; n++) {
712 chout[n].
re += over[n].
re;
713 chout[n].
im += over[n].
im;
715 memcpy(over, idst + ihop_size,
sizeof(*over) * ihop_size);
724 const int size =
s->input_padding_size;
725 const int output_sample_count =
s->output_sample_count;
726 const int fsize =
s->frequency_band_count;
727 int *kernel_start =
s->kernel_start;
728 int *kernel_stop =
s->kernel_stop;
729 unsigned *
index =
s->index;
730 int range_min = INT_MAX;
731 int range_max = 0,
ret = 0;
738 for (
int y = 0; y <
fsize; y++) {
740 int start = INT_MIN, stop = INT_MAX;
741 const float frequency =
s->frequency_band[y*2];
742 const float deviation = 1.f / (
s->frequency_band[y*2+1] *
743 output_sample_count);
748 memset(tkernel, 0,
size *
sizeof(*tkernel));
749 for (
int n =
a; n <
b; n++) {
750 float ff,
f = n+0.5f-frequency;
752 ff =
expf(-
f*
f*deviation);
753 tkernel[n+
range] = ff;
756 for (
int n =
a; n <
b; n++) {
757 if (tkernel[n+
range] != 0.
f) {
758 if (tkernel[n+
range] > FLT_MIN)
765 for (
int n =
b; n >=
a; n--) {
766 if (tkernel[n+
range] != 0.
f) {
767 if (tkernel[n+
range] > FLT_MIN)
774 if (start == INT_MIN || stop == INT_MAX) {
779 kernel_start[y] = start;
780 kernel_stop[y] = stop;
788 for (
int n = 0; n <= stop - start; n++) {
789 kernel[n].
re = tkernel[n+
range+start];
790 kernel[n].
im = tkernel[n+
range+start];
793 range_min =
FFMIN(range_min, stop+1-start);
794 range_max =
FFMAX(range_max, stop+1-start);
796 s->kernel[y] = kernel;
799 for (
int n = 0; n <
size; n++)
800 index[n] = n & (
s->output_padding_size - 1);
816 const float limit_frequency =
inlink->sample_rate * 0.5f;
817 float maximum_frequency =
fminf(
s->maximum_frequency, limit_frequency);
818 float minimum_frequency =
s->minimum_frequency;
822 if (minimum_frequency >= maximum_frequency) {
824 minimum_frequency, maximum_frequency);
834 switch (
s->direction) {
837 s->bar_size =
s->w *
s->bar_ratio;
838 s->sono_size =
s->w -
s->bar_size;
839 s->frequency_band_count =
s->h;
843 s->bar_size =
s->h *
s->bar_ratio;
844 s->sono_size =
s->h -
s->bar_size;
845 s->frequency_band_count =
s->w;
849 switch (
s->frequency_scale) {
851 minimum_frequency = logf(minimum_frequency) / logf(2.
f);
852 maximum_frequency = logf(maximum_frequency) / logf(2.
f);
855 minimum_frequency = 6.f * asinhf(minimum_frequency / 600.
f);
856 maximum_frequency = 6.f * asinhf(maximum_frequency / 600.
f);
859 minimum_frequency = 2595.f *
log10f(1.
f + minimum_frequency / 700.
f);
860 maximum_frequency = 2595.f *
log10f(1.
f + maximum_frequency / 700.
f);
863 minimum_frequency = 11.17268f * logf(1.
f + (46.06538
f * minimum_frequency) / (minimum_frequency + 14678.49
f));
864 maximum_frequency = 11.17268f * logf(1.
f + (46.06538
f * maximum_frequency) / (maximum_frequency + 14678.49
f));
867 minimum_frequency =
sqrtf(minimum_frequency);
868 maximum_frequency =
sqrtf(maximum_frequency);
871 minimum_frequency =
cbrtf(minimum_frequency);
872 maximum_frequency =
cbrtf(maximum_frequency);
875 minimum_frequency =
powf(minimum_frequency, 0.25
f);
876 maximum_frequency =
powf(maximum_frequency, 0.25
f);
879 minimum_frequency =
powf(9.
f * (minimum_frequency * minimum_frequency) / 4.
f, 1.
f / 3.
f);
880 maximum_frequency =
powf(9.
f * (maximum_frequency * maximum_frequency) / 4.
f, 1.
f / 3.
f);
884 s->frequency_band =
av_calloc(
s->frequency_band_count,
885 sizeof(*
s->frequency_band) * 2);
886 if (!
s->frequency_band)
889 s->nb_consumed_samples =
inlink->sample_rate *
891 s->frequency_band_count, maximum_frequency - minimum_frequency,
892 minimum_frequency,
s->frequency_scale,
s->deviation);
893 s->nb_consumed_samples =
FFMIN(
s->nb_consumed_samples, 65536);
896 s->nb_channels =
inlink->ch_layout.nb_channels;
900 s->input_sample_count = 1 << (32 -
ff_clz(
s->nb_consumed_samples));
901 s->input_padding_size = 1 << (32 -
ff_clz(
s->input_sample_count));
903 s->output_padding_size = 1 << (32 -
ff_clz(
s->output_sample_count));
905 s->hop_size =
s->input_sample_count;
906 s->ihop_size =
s->output_padding_size >> 1;
919 for (
int n = 0; n <
s->nb_threads; n++) {
929 for (
int n = 0; n <
s->nb_threads; n++) {
940 s->kernel =
av_calloc(
s->frequency_band_count,
sizeof(*
s->kernel));
947 s->index =
av_calloc(
s->input_padding_size,
sizeof(*
s->index));
948 s->kernel_start =
av_calloc(
s->frequency_band_count,
sizeof(*
s->kernel_start));
949 s->kernel_stop =
av_calloc(
s->frequency_band_count,
sizeof(*
s->kernel_stop));
950 if (!
s->outpicref || !
s->fft_in || !
s->fft_out || !
s->src_x || !
s->dst_x || !
s->over ||
951 !
s->ifft_in || !
s->ifft_out || !
s->kernel_start || !
s->kernel_stop || !
s->ch_out ||
952 !
s->cache || !
s->index || !
s->bh_out || !
s->kernel)
955 s->ch_out->format =
inlink->format;
956 s->ch_out->nb_samples = 2 *
s->ihop_size *
inlink->ch_layout.nb_channels;
957 s->ch_out->ch_layout.nb_channels =
s->frequency_band_count;
962 s->ifft_in->format =
inlink->format;
963 s->ifft_in->nb_samples =
s->ifft_size * 2;
964 s->ifft_in->ch_layout.nb_channels =
s->nb_threads;
969 s->ifft_out->format =
inlink->format;
970 s->ifft_out->nb_samples =
s->ifft_size * 2;
971 s->ifft_out->ch_layout.nb_channels =
s->nb_threads;
976 s->src_x->format =
inlink->format;
977 s->src_x->nb_samples =
s->fft_size * 2;
978 s->src_x->ch_layout.nb_channels =
s->nb_threads;
983 s->dst_x->format =
inlink->format;
984 s->dst_x->nb_samples =
s->fft_size * 2;
985 s->dst_x->ch_layout.nb_channels =
s->nb_threads;
990 s->outpicref->sample_aspect_ratio = (
AVRational){1,1};
992 for (
int y = 0; y < outlink->
h; y++) {
993 memset(
s->outpicref->data[0] + y *
s->outpicref->linesize[0], 0, outlink->
w);
994 memset(
s->outpicref->data[1] + y *
s->outpicref->linesize[1], 128, outlink->
w);
995 memset(
s->outpicref->data[2] + y *
s->outpicref->linesize[2], 128, outlink->
w);
996 if (
s->outpicref->data[3])
997 memset(
s->outpicref->data[3] + y *
s->outpicref->linesize[3], 0, outlink->
w);
1003 for (
int n = 0; n <
s->frequency_band_count; n++) {
1004 s->frequency_band[2*n ] *=
factor;
1005 s->frequency_band[2*n+1] *=
factor;
1017 switch (
s->direction) {
1020 s->pos =
s->bar_size;
1024 s->pos =
s->sono_size;
1029 if (strcmp(
s->rate_str,
"auto")) {
1034 s->frame_rate =
s->auto_frame_rate;
1051 const int nb_planes = 3 + (
s->outpicref->data[3] !=
NULL);
1056 switch (
s->direction) {
1058 for (
int p = 0; p < nb_planes; p++) {
1059 ptrdiff_t linesize =
s->outpicref->linesize[p];
1061 for (
int y =
s->h - 1; y >
s->bar_size; y--) {
1062 uint8_t *
dst =
s->outpicref->data[p] + y * linesize;
1064 memmove(
dst,
dst - linesize,
s->w);
1069 for (
int p = 0; p < nb_planes; p++) {
1070 ptrdiff_t linesize =
s->outpicref->linesize[p];
1072 for (
int y = 0; y <
s->sono_size; y++) {
1073 uint8_t *
dst =
s->outpicref->data[p] + y * linesize;
1075 memmove(
dst,
dst + linesize,
s->w);
1088 switch (
s->direction) {
1091 if (
s->pos >=
s->w) {
1092 s->pos =
s->bar_size;
1099 s->pos =
s->sono_size;
1105 if (
s->pos >=
s->h) {
1106 s->pos =
s->bar_size;
1113 s->pos =
s->sono_size;
1120 switch (
s->direction) {
1123 s->pos =
s->bar_size;
1127 s->pos =
s->sono_size;
1134 switch (
s->direction) {
1136 for (
int p = 0; p < nb_planes; p++) {
1137 ptrdiff_t linesize =
s->outpicref->linesize[p];
1138 const int size =
s->w -
s->pos;
1139 const int fill = p > 0 && p < 3 ? 128 : 0;
1140 const int x =
s->pos;
1142 for (
int y = 0; y <
s->h; y++) {
1143 uint8_t *
dst =
s->outpicref->data[p] + y * linesize + x;
1150 for (
int p = 0; p < nb_planes; p++) {
1151 ptrdiff_t linesize =
s->outpicref->linesize[p];
1152 const int size =
s->w -
s->pos;
1153 const int fill = p > 0 && p < 3 ? 128 : 0;
1155 for (
int y = 0; y <
s->h; y++) {
1156 uint8_t *
dst =
s->outpicref->data[p] + y * linesize;
1163 for (
int p = 0; p < nb_planes; p++) {
1164 ptrdiff_t linesize =
s->outpicref->linesize[p];
1165 const int fill = p > 0 && p < 3 ? 128 : 0;
1167 for (
int y =
s->pos; y < s->
h; y++) {
1168 uint8_t *
dst =
s->outpicref->data[p] + y * linesize;
1170 memset(
dst, fill,
s->w);
1175 for (
int p = 0; p < nb_planes; p++) {
1176 ptrdiff_t linesize =
s->outpicref->linesize[p];
1177 const int fill = p > 0 && p < 3 ? 128 : 0;
1179 for (
int y =
s->h -
s->pos; y >= 0; y--) {
1180 uint8_t *
dst =
s->outpicref->data[p] + y * linesize;
1182 memset(
dst, fill,
s->w);
1193 const int offset = (
s->input_padding_size -
s->hop_size) >> 1;
1197 s->outpicref->duration = 1;
1201 if (
s->ihop_index >=
s->ihop_size)
1202 s->ihop_index =
s->hop_index = 0;
1207 if (
s->old_pts <
s->outpicref->pts) {
1217 s->old_pts =
s->outpicref->pts;
1233 const int count =
s->nb_channels;
1234 const int start = (count * jobnr) / nb_jobs;
1235 const int end = (count * (jobnr+1)) / nb_jobs;
1237 for (
int ch = start; ch < end; ch++)
1256 if (
s->hop_index <
s->hop_size) {
1263 if (
ret > 0 ||
s->eof) {
1265 FFMIN(
s->nb_threads,
s->nb_channels));
1267 if (
s->hop_index == 0) {
1268 s->in_pts = fin->
pts;
1275 s->hop_index =
s->hop_size;
1280 if (
s->hop_index >=
s->hop_size ||
s->ihop_index > 0) {
1281 for (
int ch = 0; ch <
s->nb_channels &&
s->ihop_index == 0; ch++) {
1309 s->hop_index >=
s->hop_size ||
s->eof) {
1332 .description =
NULL_IF_CONFIG_SMALL(
"Convert input audio to a CWT (Continuous Wavelet Transform) spectrum video output."),
1339 .priv_class = &showcwt_class,