History

avfilter/vf_nnedi: simplify code even more

Paul B Mahol committed 5 years ago

Commit c737f6e1 parent 553eb071 file · +82 −69Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/vf_nnedi.c+82−69
@@ -37,8 +37,8 @@ static const uint8_t NNEDI_XDIM[] = { 8, 16, 32, 48, 8, 16, 32 }; · 36 unmodified lines
37static const uint8_t NNEDI_YDIM[] = { 6, 6, 6, 6, 4, 4, 4 };37static const uint8_t NNEDI_YDIM[] = { 6, 6, 6, 6, 4, 4, 4 };
38static const uint16_t NNEDI_NNS[] = { 16, 32, 64, 128, 256 };38static const uint16_t NNEDI_NNS[] = { 16, 32, 64, 128, 256 };
39 39
40typedef struct PrescreenerOldCoefficients {40typedef struct PrescreenerCoefficients {
41 DECLARE_ALIGNED(32, float, kernel_l0)[4][14 * 4];41 DECLARE_ALIGNED(32, float, kernel_l0)[4][16 * 4];
42 DECLARE_ALIGNED(32, float, bias_l0)[4];42 DECLARE_ALIGNED(32, float, bias_l0)[4];
43 43
44 DECLARE_ALIGNED(32, float, kernel_l1)[4][4];44 DECLARE_ALIGNED(32, float, kernel_l1)[4][4];
@@ -46,15 +46,7 @@ typedef struct PrescreenerOldCoefficients { · 1 unmodified line
46 46
47 DECLARE_ALIGNED(32, float, kernel_l2)[4][8];47 DECLARE_ALIGNED(32, float, kernel_l2)[4][8];
48 DECLARE_ALIGNED(32, float, bias_l2)[4];48 DECLARE_ALIGNED(32, float, bias_l2)[4];
49} PrescreenerOldCoefficients;49} PrescreenerCoefficients;
50
51typedef struct PrescreenerNewCoefficients {
52 DECLARE_ALIGNED(32, float, kernel_l0)[4][16 * 4];
53 DECLARE_ALIGNED(32, float, bias_l0)[4];
54
55 DECLARE_ALIGNED(32, float, kernel_l1)[4][4];
56 DECLARE_ALIGNED(32, float, bias_l1)[4];
57} PrescreenerNewCoefficients;
58 50
59typedef struct PredictorCoefficients {51typedef struct PredictorCoefficients {
60 int xdim, ydim, nns, nsize;52 int xdim, ydim, nns, nsize;
@@ -89,8 +81,7 @@ typedef struct NNEDIContext { · 28 unmodified lines
89 int planeheight[4];81 int planeheight[4];
90 int field_n;82 int field_n;
91 83
92 PrescreenerOldCoefficients prescreener_old;84 PrescreenerCoefficients prescreener[4];
93 PrescreenerNewCoefficients prescreener_new[3];
94 PredictorCoefficients coeffs[2][5][7];85 PredictorCoefficients coeffs[2][5][7];
95 86
96 float half;87 float half;
@@ -108,9 +99,9 @@ typedef struct NNEDIContext { · 11 unmodified lines
108 int pscrn;99 int pscrn;
109 100
110 int input_size;101 int input_size;
111 uint8_t *prescreen_buf;102 uint8_t **prescreen_buf;
112 float *input_buf;103 float **input_buf;
113 float *output_buf;104 float **output_buf;
114 105
115 void (*read)(const uint8_t *src, float *dst,106 void (*read)(const uint8_t *src, float *dst,
116 int src_stride, int dst_stride,107 int src_stride, int dst_stride,
@@ -120,7 +111,8 @@ typedef struct NNEDIContext { · 3 unmodified lines
120 int width, int height, int depth, float scale);111 int width, int height, int depth, float scale);
121 void (*prescreen[2])(AVFilterContext *ctx,112 void (*prescreen[2])(AVFilterContext *ctx,
122 const void *src, ptrdiff_t src_stride,113 const void *src, ptrdiff_t src_stride,
123 uint8_t *prescreen, int N, void *data);114 uint8_t *prescreen, int N,
115 const PrescreenerCoefficients *const coeffs);
124} NNEDIContext;116} NNEDIContext;
125 117
126#define OFFSET(x) offsetof(NNEDIContext, x)118#define OFFSET(x) offsetof(NNEDIContext, x)
@@ -222,7 +214,7 @@ static int query_formats(AVFilterContext *ctx) · 95 unmodified lines
222 return ff_set_common_formats(ctx, fmts_list);214 return ff_set_common_formats(ctx, fmts_list);
223}215}
224 216
225static float dot_dsp(NNEDIContext *s, const float *kernel, const float *input,217static float dot_dsp(const NNEDIContext *const s, const float *kernel, const float *input,
226 int n, float scale, float bias)218 int n, float scale, float bias)
227{219{
228 float sum;220 float sum;
@@ -246,10 +238,9 @@ static void transform_elliott(float *input, int size) · 17 unmodified lines
246static void process_old(AVFilterContext *ctx,238static void process_old(AVFilterContext *ctx,
247 const void *src, ptrdiff_t src_stride,239 const void *src, ptrdiff_t src_stride,
248 uint8_t *prescreen, int N,240 uint8_t *prescreen, int N,
249 void *data)241 const PrescreenerCoefficients *const m_data)
250{242{
251 NNEDIContext *s = ctx->priv;243 NNEDIContext *s = ctx->priv;
252 const PrescreenerOldCoefficients *const m_data = data;
253 const float *src_p = src;244 const float *src_p = src;
254 245
255 // Adjust source pointer to point to top-left of filter window.246 // Adjust source pointer to point to top-left of filter window.
@@ -283,10 +274,9 @@ static void process_old(AVFilterContext *ctx, · 27 unmodified lines
283static void process_new(AVFilterContext *ctx,274static void process_new(AVFilterContext *ctx,
284 const void *src, ptrdiff_t src_stride,275 const void *src, ptrdiff_t src_stride,
285 uint8_t *prescreen, int N,276 uint8_t *prescreen, int N,
286 void *data)277 const PrescreenerCoefficients *const m_data)
287{278{
288 NNEDIContext *s = ctx->priv;279 NNEDIContext *s = ctx->priv;
289 const PrescreenerNewCoefficients *const m_data = data;
290 const float *src_p = src;280 const float *src_p = src;
291 281
292 // Adjust source pointer to point to top-left of filter window.282 // Adjust source pointer to point to top-left of filter window.
@@ -344,8 +334,8 @@ static void gather_input(const float *src, ptrdiff_t src_stride, · 51 unmodified lines
344 float *buf, float mstd[4],334 float *buf, float mstd[4],
345 const PredictorCoefficients *const model)335 const PredictorCoefficients *const model)
346{336{
347 float sum = 0;337 float sum = 0.f;
348 float sum_sq = 0;338 float sum_sq = 0.f;
349 float tmp;339 float tmp;
350 340
351 for (int i = 0; i < model->ydim; i++) {341 for (int i = 0; i < model->ydim; i++) {
@@ -405,17 +395,16 @@ static void wae5(const float *softmax, const float *el, · 53 unmodified lines
405static void predictor(AVFilterContext *ctx,395static void predictor(AVFilterContext *ctx,
406 const void *src, ptrdiff_t src_stride, void *dst,396 const void *src, ptrdiff_t src_stride, void *dst,
407 const uint8_t *prescreen, int N,397 const uint8_t *prescreen, int N,
408 void *data, int use_q2)398 const PredictorCoefficients *const model, int use_q2)
409{399{
410 NNEDIContext *s = ctx->priv;400 const NNEDIContext *const s = ctx->priv;
411 const PredictorCoefficients *const model = data;
412 const float *src_p = src;401 const float *src_p = src;
413 float *dst_p = dst;402 float *dst_p = dst;
414 403
415 // Adjust source pointer to point to top-left of filter window.404 // Adjust source pointer to point to top-left of filter window.
416 const float *window = src_p - (model->ydim / 2) * src_stride - (model->xdim / 2 - 1);405 const float *window = src_p - (model->ydim / 2) * src_stride - (model->xdim / 2 - 1);
417 int filter_size = model->nsize;406 const int filter_size = model->nsize;
418 int nns = model->nns;407 const int nns = model->nns;
419 408
420 for (int i = 0; i < N; i++) {409 for (int i = 0; i < N; i++) {
421 LOCAL_ALIGNED_32(float, input, [48 * 6]);410 LOCAL_ALIGNED_32(float, input, [48 * 6]);
@@ -433,7 +422,7 @@ static void predictor(AVFilterContext *ctx, · 11 unmodified lines
433 activation[nn] = dot_dsp(s, softmax_q1_filter(nn, model), input, filter_size, scale, model->softmax_bias_q1[nn]);422 activation[nn] = dot_dsp(s, softmax_q1_filter(nn, model), input, filter_size, scale, model->softmax_bias_q1[nn]);
434 423
435 for (int nn = 0; nn < nns; nn++)424 for (int nn = 0; nn < nns; nn++)
436 activation[model->nns + nn] = dot_dsp(s, elliott_q1_filter(nn, model), input, filter_size, scale, model->elliott_bias_q1[nn]);425 activation[nns + nn] = dot_dsp(s, elliott_q1_filter(nn, model), input, filter_size, scale, model->elliott_bias_q1[nn]);
437 426
438 transform_softmax_exp(activation, nns);427 transform_softmax_exp(activation, nns);
439 wae5(activation, activation + nns, nns, mstd);428 wae5(activation, activation + nns, nns, mstd);
@@ -449,7 +438,7 @@ static void predictor(AVFilterContext *ctx, · 9 unmodified lines
449 wae5(activation, activation + nns, nns, mstd);438 wae5(activation, activation + nns, nns, mstd);
450 }439 }
451 440
452 dst_p[i] = mstd[3] / (use_q2 ? 2 : 1);441 dst_p[i] = mstd[3] * (use_q2 ? 0.5f : 1.f);
453 }442 }
454}443}
455 444
@@ -551,7 +540,7 @@ static void interpolation(const void *src, ptrdiff_t src_stride, · 95 unmodified lines
551 540
552static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)541static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
553{542{
554 NNEDIContext *s = ctx->priv;543 const NNEDIContext *const s = ctx->priv;
555 AVFrame *out = s->dst;544 AVFrame *out = s->dst;
556 AVFrame *in = s->src;545 AVFrame *in = s->src;
557 const float in_scale = s->in_scale;546 const float in_scale = s->in_scale;
@@ -572,10 +561,10 @@ static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) · 14 unmodified lines
572 uint8_t *dst = out->data[p] + slice_start * out->linesize[p];561 uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
573 const int src_linesize = in->linesize[p];562 const int src_linesize = in->linesize[p];
574 const int dst_linesize = out->linesize[p];563 const int dst_linesize = out->linesize[p];
575 uint8_t *prescreen_buf = s->prescreen_buf + s->planewidth[0] * jobnr;564 uint8_t *prescreen_buf = s->prescreen_buf[jobnr];
576 float *srcbuf = s->input_buf + s->input_size * jobnr;565 float *srcbuf = s->input_buf[jobnr];
577 const int srcbuf_stride = width + 64;566 const int srcbuf_stride = width + 64;
578 float *dstbuf = s->output_buf + s->input_size * jobnr;567 float *dstbuf = s->output_buf[jobnr];
579 const int dstbuf_stride = width;568 const int dstbuf_stride = width;
580 const int slice_height = (slice_end - slice_start) / 2;569 const int slice_height = (slice_end - slice_start) / 2;
581 const int last_slice = slice_end == height;570 const int last_slice = slice_end == height;
@@ -646,15 +635,10 @@ static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) · 64 unmodified lines
646 width, 1, in_scale);635 width, 1, in_scale);
647 636
648 for (int y = 0; y < slice_end - slice_start; y += 2) {637 for (int y = 0; y < slice_end - slice_start; y += 2) {
649 if (s->pscrn > 1) {638 if (s->prescreen > 0)
650 s->prescreen[1](ctx, srcbuf + (y / 2) * srcbuf_stride + 32,639 s->prescreen[s->pscrn > 1](ctx, srcbuf + (y / 2) * srcbuf_stride + 32,
651 srcbuf_stride, prescreen_buf, width,640 srcbuf_stride, prescreen_buf, width,
652 &s->prescreener_new[s->pscrn - 2]);641 &s->prescreener[s->pscrn - 1]);
653 } else if (s->pscrn == 1) {
654 s->prescreen[0](ctx, srcbuf + (y / 2) * srcbuf_stride + 32,
655 srcbuf_stride, prescreen_buf, width,
656 &s->prescreener_old);
657 }
658 642
659 predictor(ctx,643 predictor(ctx,
660 srcbuf + (y / 2) * srcbuf_stride + 32,644 srcbuf + (y / 2) * srcbuf_stride + 32,
@@ -831,7 +815,7 @@ static int allocate_model(PredictorCoefficients *coeffs, int xdim, int ydim, int · 170 unmodified lines
831 int bias_size = nns;815 int bias_size = nns;
832 float *data;816 float *data;
833 817
834 data = av_malloc_array(filter_size + bias_size, 4 * sizeof(float));818 data = av_calloc(filter_size + bias_size, 4 * sizeof(float));
835 if (!data)819 if (!data)
836 return AVERROR(ENOMEM);820 return AVERROR(ENOMEM);
837 821
@@ -859,17 +843,17 @@ static int read_weights(AVFilterContext *ctx, const float *bdata) · 21 unmodified lines
859 NNEDIContext *s = ctx->priv;843 NNEDIContext *s = ctx->priv;
860 int ret;844 int ret;
861 845
862 copy_weights(&s->prescreener_old.kernel_l0[0][0], 4 * 48, &bdata);846 copy_weights(&s->prescreener[0].kernel_l0[0][0], 4 * 48, &bdata);
863 copy_weights(s->prescreener_old.bias_l0, 4, &bdata);847 copy_weights(s->prescreener[0].bias_l0, 4, &bdata);
864 848
865 copy_weights(&s->prescreener_old.kernel_l1[0][0], 4 * 4, &bdata);849 copy_weights(&s->prescreener[0].kernel_l1[0][0], 4 * 4, &bdata);
866 copy_weights(s->prescreener_old.bias_l1, 4, &bdata);850 copy_weights(s->prescreener[0].bias_l1, 4, &bdata);
867 851
868 copy_weights(&s->prescreener_old.kernel_l2[0][0], 4 * 8, &bdata);852 copy_weights(&s->prescreener[0].kernel_l2[0][0], 4 * 8, &bdata);
869 copy_weights(s->prescreener_old.bias_l2, 4, &bdata);853 copy_weights(s->prescreener[0].bias_l2, 4, &bdata);
870 854
871 for (int i = 0; i < 3; i++) {855 for (int i = 0; i < 3; i++) {
872 PrescreenerNewCoefficients *data = &s->prescreener_new[i];856 PrescreenerCoefficients *data = &s->prescreener[i + 1];
873 float kernel_l0_shuffled[4 * 64];857 float kernel_l0_shuffled[4 * 64];
874 float kernel_l1_shuffled[4 * 4];858 float kernel_l1_shuffled[4 * 4];
875 859
@@ -890,14 +874,14 @@ static int read_weights(AVFilterContext *ctx, const float *bdata) · 14 unmodified lines
890 for (int m = 0; m < 2; m++) {874 for (int m = 0; m < 2; m++) {
891 // Grouping by neuron count.875 // Grouping by neuron count.
892 for (int i = 0; i < 5; i++) {876 for (int i = 0; i < 5; i++) {
893 int nns = NNEDI_NNS[i];877 const int nns = NNEDI_NNS[i];
894 878
895 // Grouping by window size.879 // Grouping by window size.
896 for (int j = 0; j < 7; j++) {880 for (int j = 0; j < 7; j++) {
897 PredictorCoefficients *model = &s->coeffs[m][i][j];881 PredictorCoefficients *model = &s->coeffs[m][i][j];
898 int xdim = NNEDI_XDIM[j];882 const int xdim = NNEDI_XDIM[j];
899 int ydim = NNEDI_YDIM[j];883 const int ydim = NNEDI_YDIM[j];
900 int filter_size = xdim * ydim;884 const int filter_size = xdim * ydim;
901 885
902 ret = allocate_model(model, xdim, ydim, nns);886 ret = allocate_model(model, xdim, ydim, nns);
903 if (ret < 0)887 if (ret < 0)
@@ -927,7 +911,7 @@ static int read_weights(AVFilterContext *ctx, const float *bdata) · 23 unmodified lines
927 911
928static float mean(const float *input, int size)912static float mean(const float *input, int size)
929{913{
930 float sum = 0.;914 float sum = 0.f;
931 915
932 for (int i = 0; i < size; i++)916 for (int i = 0; i < size; i++)
933 sum += input[i];917 sum += input[i];
@@ -941,7 +925,7 @@ static void transform(float *input, int size, float mean, float half) · 7 unmodified lines
941 input[i] = (input[i] - mean) / half;925 input[i] = (input[i] - mean) / half;
942}926}
943 927
944static void subtract_mean_old(PrescreenerOldCoefficients *coeffs, float half)928static void subtract_mean_old(PrescreenerCoefficients *coeffs, float half)
945{929{
946 for (int n = 0; n < 4; n++) {930 for (int n = 0; n < 4; n++) {
947 float m = mean(coeffs->kernel_l0[n], 48);931 float m = mean(coeffs->kernel_l0[n], 48);
@@ -950,7 +934,7 @@ static void subtract_mean_old(PrescreenerOldCoefficients *coeffs, float half) · 2 unmodified lines
950 }934 }
951}935}
952 936
953static void subtract_mean_new(PrescreenerNewCoefficients *coeffs, float half)937static void subtract_mean_new(PrescreenerCoefficients *coeffs, float half)
954{938{
955 for (int n = 0; n < 4; n++) {939 for (int n = 0; n < 4; n++) {
956 float m = mean(coeffs->kernel_l0[n], 64);940 float m = mean(coeffs->kernel_l0[n], 64);
@@ -1121,10 +1105,10 @@ static int config_input(AVFilterLink *inlink) · 164 unmodified lines
1121 break;1105 break;
1122 }1106 }
1123 1107
1124 subtract_mean_old(&s->prescreener_old, s->half);1108 subtract_mean_old(&s->prescreener[0], s->half);
1125 subtract_mean_new(&s->prescreener_new[0], s->half);1109 subtract_mean_new(&s->prescreener[1], s->half);
1126 subtract_mean_new(&s->prescreener_new[1], s->half);1110 subtract_mean_new(&s->prescreener[2], s->half);
1127 subtract_mean_new(&s->prescreener_new[2], s->half);1111 subtract_mean_new(&s->prescreener[3], s->half);
1128 1112
1129 s->prescreen[0] = process_old;1113 s->prescreen[0] = process_old;
1130 s->prescreen[1] = process_new;1114 s->prescreen[1] = process_new;
@@ -1136,19 +1120,37 @@ static int config_input(AVFilterLink *inlink) · 5 unmodified lines
1136 }1120 }
1137 }1121 }
1138 1122
1139 s->prescreen_buf = av_calloc(s->nb_threads * s->planewidth[0], sizeof(*s->prescreen_buf));
1140 if (!s->prescreen_buf)
1141 return AVERROR(ENOMEM);
1142
1143 s->input_size = (s->planewidth[0] + 64) * (s->planeheight[0] + 6);1123 s->input_size = (s->planewidth[0] + 64) * (s->planeheight[0] + 6);
1144 s->input_buf = av_calloc(s->nb_threads * s->input_size, sizeof(*s->input_buf));1124 s->input_buf = av_calloc(s->nb_threads, sizeof(*s->input_buf));
1145 if (!s->input_buf)1125 if (!s->input_buf)
1146 return AVERROR(ENOMEM);1126 return AVERROR(ENOMEM);
1147 1127
1148 s->output_buf = av_calloc(s->nb_threads * s->input_size, sizeof(*s->output_buf));1128 for (int i = 0; i < s->nb_threads; i++) {
1129 s->input_buf[i] = av_calloc(s->input_size, sizeof(**s->input_buf));
1130 if (!s->input_buf[i])
1131 return AVERROR(ENOMEM);
1132 }
1133
1134 s->output_buf = av_calloc(s->nb_threads, sizeof(*s->output_buf));
1149 if (!s->output_buf)1135 if (!s->output_buf)
1150 return AVERROR(ENOMEM);1136 return AVERROR(ENOMEM);
1151 1137
1138 for (int i = 0; i < s->nb_threads; i++) {
1139 s->output_buf[i] = av_calloc(s->input_size, sizeof(**s->output_buf));
1140 if (!s->output_buf[i])
1141 return AVERROR(ENOMEM);
1142 }
1143
1144 s->prescreen_buf = av_calloc(s->nb_threads, sizeof(*s->prescreen_buf));
1145 if (!s->prescreen_buf)
1146 return AVERROR(ENOMEM);
1147
1148 for (int i = 0; i < s->nb_threads; i++) {
1149 s->prescreen_buf[i] = av_calloc(s->planewidth[0], sizeof(**s->prescreen_buf));
1150 if (!s->prescreen_buf[i])
1151 return AVERROR(ENOMEM);
1152 }
1153
1152 return 0;1154 return 0;
1153}1155}
1154 1156
@@ -1156,8 +1158,19 @@ static av_cold void uninit(AVFilterContext *ctx) · 1 unmodified line
1156{1158{
1157 NNEDIContext *s = ctx->priv;1159 NNEDIContext *s = ctx->priv;
1158 1160
1161 for (int i = 0; i < s->nb_threads && s->prescreen_buf; i++)
1162 av_freep(&s->prescreen_buf[i]);
1163
1159 av_freep(&s->prescreen_buf);1164 av_freep(&s->prescreen_buf);
1165
1166 for (int i = 0; i < s->nb_threads && s->input_buf; i++)
1167 av_freep(&s->input_buf[i]);
1168
1160 av_freep(&s->input_buf);1169 av_freep(&s->input_buf);
1170
1171 for (int i = 0; i < s->nb_threads && s->output_buf; i++)
1172 av_freep(&s->output_buf[i]);
1173
1161 av_freep(&s->output_buf);1174 av_freep(&s->output_buf);
1162 av_freep(&s->fdsp);1175 av_freep(&s->fdsp);
1163 1176

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t