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
3737 static const uint8_t NNEDI_YDIM[] = { 6, 6, 6, 6, 4, 4, 4 };
3838 static const uint16_t NNEDI_NNS[] = { 16, 32, 64, 128, 256 };
3939
40−typedef struct PrescreenerOldCoefficients {
41− DECLARE_ALIGNED(32, float, kernel_l0)[4][14 * 4];
40+typedef struct PrescreenerCoefficients {
41+ DECLARE_ALIGNED(32, float, kernel_l0)[4][16 * 4];
4242 DECLARE_ALIGNED(32, float, bias_l0)[4];
4343
4444 DECLARE_ALIGNED(32, float, kernel_l1)[4][4];
@@ -46,15 +46,7 @@ typedef struct PrescreenerOldCoefficients { · 1 unmodified line
4646
4747 DECLARE_ALIGNED(32, float, kernel_l2)[4][8];
4848 DECLARE_ALIGNED(32, float, bias_l2)[4];
49−} PrescreenerOldCoefficients;
50−
51−typedef 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;
49+} PrescreenerCoefficients;
5850
5951 typedef struct PredictorCoefficients {
6052 int xdim, ydim, nns, nsize;
@@ -89,8 +81,7 @@ typedef struct NNEDIContext { · 28 unmodified lines
8981 int planeheight[4];
9082 int field_n;
9183
92− PrescreenerOldCoefficients prescreener_old;
93− PrescreenerNewCoefficients prescreener_new[3];
84+ PrescreenerCoefficients prescreener[4];
9485 PredictorCoefficients coeffs[2][5][7];
9586
9687 float half;
@@ -108,9 +99,9 @@ typedef struct NNEDIContext { · 11 unmodified lines
10899 int pscrn;
109100
110101 int input_size;
111− uint8_t *prescreen_buf;
112− float *input_buf;
113− float *output_buf;
102+ uint8_t **prescreen_buf;
103+ float **input_buf;
104+ float **output_buf;
114105
115106 void (*read)(const uint8_t *src, float *dst,
116107 int src_stride, int dst_stride,
@@ -120,7 +111,8 @@ typedef struct NNEDIContext { · 3 unmodified lines
120111 int width, int height, int depth, float scale);
121112 void (*prescreen[2])(AVFilterContext *ctx,
122113 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);
124116 } NNEDIContext;
125117
126118 #define OFFSET(x) offsetof(NNEDIContext, x)
@@ -222,7 +214,7 @@ static int query_formats(AVFilterContext *ctx) · 95 unmodified lines
222214 return ff_set_common_formats(ctx, fmts_list);
223215 }
224216
225−static float dot_dsp(NNEDIContext *s, const float *kernel, const float *input,
217+static float dot_dsp(const NNEDIContext *const s, const float *kernel, const float *input,
226218 int n, float scale, float bias)
227219 {
228220 float sum;
@@ -246,10 +238,9 @@ static void transform_elliott(float *input, int size) · 17 unmodified lines
246238 static void process_old(AVFilterContext *ctx,
247239 const void *src, ptrdiff_t src_stride,
248240 uint8_t *prescreen, int N,
249− void *data)
241+ const PrescreenerCoefficients *const m_data)
250242 {
251243 NNEDIContext *s = ctx->priv;
252− const PrescreenerOldCoefficients *const m_data = data;
253244 const float *src_p = src;
254245
255246 // Adjust source pointer to point to top-left of filter window.
@@ -283,10 +274,9 @@ static void process_old(AVFilterContext *ctx, · 27 unmodified lines
283274 static void process_new(AVFilterContext *ctx,
284275 const void *src, ptrdiff_t src_stride,
285276 uint8_t *prescreen, int N,
286− void *data)
277+ const PrescreenerCoefficients *const m_data)
287278 {
288279 NNEDIContext *s = ctx->priv;
289− const PrescreenerNewCoefficients *const m_data = data;
290280 const float *src_p = src;
291281
292282 // 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
344334 float *buf, float mstd[4],
345335 const PredictorCoefficients *const model)
346336 {
347− float sum = 0;
348− float sum_sq = 0;
337+ float sum = 0.f;
338+ float sum_sq = 0.f;
349339 float tmp;
350340
351341 for (int i = 0; i < model->ydim; i++) {
@@ -405,17 +395,16 @@ static void wae5(const float *softmax, const float *el, · 53 unmodified lines
405395 static void predictor(AVFilterContext *ctx,
406396 const void *src, ptrdiff_t src_stride, void *dst,
407397 const uint8_t *prescreen, int N,
408− void *data, int use_q2)
398+ const PredictorCoefficients *const model, int use_q2)
409399 {
410− NNEDIContext *s = ctx->priv;
411− const PredictorCoefficients *const model = data;
400+ const NNEDIContext *const s = ctx->priv;
412401 const float *src_p = src;
413402 float *dst_p = dst;
414403
415404 // Adjust source pointer to point to top-left of filter window.
416405 const float *window = src_p - (model->ydim / 2) * src_stride - (model->xdim / 2 - 1);
417− int filter_size = model->nsize;
418− int nns = model->nns;
406+ const int filter_size = model->nsize;
407+ const int nns = model->nns;
419408
420409 for (int i = 0; i < N; i++) {
421410 LOCAL_ALIGNED_32(float, input, [48 * 6]);
@@ -433,7 +422,7 @@ static void predictor(AVFilterContext *ctx, · 11 unmodified lines
433422 activation[nn] = dot_dsp(s, softmax_q1_filter(nn, model), input, filter_size, scale, model->softmax_bias_q1[nn]);
434423
435424 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]);
437426
438427 transform_softmax_exp(activation, nns);
439428 wae5(activation, activation + nns, nns, mstd);
@@ -449,7 +438,7 @@ static void predictor(AVFilterContext *ctx, · 9 unmodified lines
449438 wae5(activation, activation + nns, nns, mstd);
450439 }
451440
452− dst_p[i] = mstd[3] / (use_q2 ? 2 : 1);
441+ dst_p[i] = mstd[3] * (use_q2 ? 0.5f : 1.f);
453442 }
454443 }
455444
@@ -551,7 +540,7 @@ static void interpolation(const void *src, ptrdiff_t src_stride, · 95 unmodified lines
551540
552541 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
553542 {
554− NNEDIContext *s = ctx->priv;
543+ const NNEDIContext *const s = ctx->priv;
555544 AVFrame *out = s->dst;
556545 AVFrame *in = s->src;
557546 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
572561 uint8_t *dst = out->data[p] + slice_start * out->linesize[p];
573562 const int src_linesize = in->linesize[p];
574563 const int dst_linesize = out->linesize[p];
575− uint8_t *prescreen_buf = s->prescreen_buf + s->planewidth[0] * jobnr;
576− float *srcbuf = s->input_buf + s->input_size * jobnr;
564+ uint8_t *prescreen_buf = s->prescreen_buf[jobnr];
565+ float *srcbuf = s->input_buf[jobnr];
577566 const int srcbuf_stride = width + 64;
578− float *dstbuf = s->output_buf + s->input_size * jobnr;
567+ float *dstbuf = s->output_buf[jobnr];
579568 const int dstbuf_stride = width;
580569 const int slice_height = (slice_end - slice_start) / 2;
581570 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
646635 width, 1, in_scale);
647636
648637 for (int y = 0; y < slice_end - slice_start; y += 2) {
649− if (s->pscrn > 1) {
650− s->prescreen[1](ctx, srcbuf + (y / 2) * srcbuf_stride + 32,
651− srcbuf_stride, prescreen_buf, width,
652− &s->prescreener_new[s->pscrn - 2]);
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− }
638+ if (s->prescreen > 0)
639+ s->prescreen[s->pscrn > 1](ctx, srcbuf + (y / 2) * srcbuf_stride + 32,
640+ srcbuf_stride, prescreen_buf, width,
641+ &s->prescreener[s->pscrn - 1]);
658642
659643 predictor(ctx,
660644 srcbuf + (y / 2) * srcbuf_stride + 32,
@@ -831,7 +815,7 @@ static int allocate_model(PredictorCoefficients *coeffs, int xdim, int ydim, int · 170 unmodified lines
831815 int bias_size = nns;
832816 float *data;
833817
834− data = av_malloc_array(filter_size + bias_size, 4 * sizeof(float));
818+ data = av_calloc(filter_size + bias_size, 4 * sizeof(float));
835819 if (!data)
836820 return AVERROR(ENOMEM);
837821
@@ -859,17 +843,17 @@ static int read_weights(AVFilterContext *ctx, const float *bdata) · 21 unmodified lines
859843 NNEDIContext *s = ctx->priv;
860844 int ret;
861845
862− copy_weights(&s->prescreener_old.kernel_l0[0][0], 4 * 48, &bdata);
863− copy_weights(s->prescreener_old.bias_l0, 4, &bdata);
846+ copy_weights(&s->prescreener[0].kernel_l0[0][0], 4 * 48, &bdata);
847+ copy_weights(s->prescreener[0].bias_l0, 4, &bdata);
864848
865− copy_weights(&s->prescreener_old.kernel_l1[0][0], 4 * 4, &bdata);
866− copy_weights(s->prescreener_old.bias_l1, 4, &bdata);
849+ copy_weights(&s->prescreener[0].kernel_l1[0][0], 4 * 4, &bdata);
850+ copy_weights(s->prescreener[0].bias_l1, 4, &bdata);
867851
868− copy_weights(&s->prescreener_old.kernel_l2[0][0], 4 * 8, &bdata);
869− copy_weights(s->prescreener_old.bias_l2, 4, &bdata);
852+ copy_weights(&s->prescreener[0].kernel_l2[0][0], 4 * 8, &bdata);
853+ copy_weights(s->prescreener[0].bias_l2, 4, &bdata);
870854
871855 for (int i = 0; i < 3; i++) {
872− PrescreenerNewCoefficients *data = &s->prescreener_new[i];
856+ PrescreenerCoefficients *data = &s->prescreener[i + 1];
873857 float kernel_l0_shuffled[4 * 64];
874858 float kernel_l1_shuffled[4 * 4];
875859
@@ -890,14 +874,14 @@ static int read_weights(AVFilterContext *ctx, const float *bdata) · 14 unmodified lines
890874 for (int m = 0; m < 2; m++) {
891875 // Grouping by neuron count.
892876 for (int i = 0; i < 5; i++) {
893− int nns = NNEDI_NNS[i];
877+ const int nns = NNEDI_NNS[i];
894878
895879 // Grouping by window size.
896880 for (int j = 0; j < 7; j++) {
897881 PredictorCoefficients *model = &s->coeffs[m][i][j];
898− int xdim = NNEDI_XDIM[j];
899− int ydim = NNEDI_YDIM[j];
900− int filter_size = xdim * ydim;
882+ const int xdim = NNEDI_XDIM[j];
883+ const int ydim = NNEDI_YDIM[j];
884+ const int filter_size = xdim * ydim;
901885
902886 ret = allocate_model(model, xdim, ydim, nns);
903887 if (ret < 0)
@@ -927,7 +911,7 @@ static int read_weights(AVFilterContext *ctx, const float *bdata) · 23 unmodified lines
927911
928912 static float mean(const float *input, int size)
929913 {
930− float sum = 0.;
914+ float sum = 0.f;
931915
932916 for (int i = 0; i < size; i++)
933917 sum += input[i];
@@ -941,7 +925,7 @@ static void transform(float *input, int size, float mean, float half) · 7 unmodified lines
941925 input[i] = (input[i] - mean) / half;
942926 }
943927
944−static void subtract_mean_old(PrescreenerOldCoefficients *coeffs, float half)
928+static void subtract_mean_old(PrescreenerCoefficients *coeffs, float half)
945929 {
946930 for (int n = 0; n < 4; n++) {
947931 float m = mean(coeffs->kernel_l0[n], 48);
@@ -950,7 +934,7 @@ static void subtract_mean_old(PrescreenerOldCoefficients *coeffs, float half) · 2 unmodified lines
950934 }
951935 }
952936
953−static void subtract_mean_new(PrescreenerNewCoefficients *coeffs, float half)
937+static void subtract_mean_new(PrescreenerCoefficients *coeffs, float half)
954938 {
955939 for (int n = 0; n < 4; n++) {
956940 float m = mean(coeffs->kernel_l0[n], 64);
@@ -1121,10 +1105,10 @@ static int config_input(AVFilterLink *inlink) · 164 unmodified lines
11211105 break;
11221106 }
11231107
1124− subtract_mean_old(&s->prescreener_old, s->half);
1125− subtract_mean_new(&s->prescreener_new[0], s->half);
1126− subtract_mean_new(&s->prescreener_new[1], s->half);
1127− subtract_mean_new(&s->prescreener_new[2], s->half);
1108+ subtract_mean_old(&s->prescreener[0], s->half);
1109+ subtract_mean_new(&s->prescreener[1], s->half);
1110+ subtract_mean_new(&s->prescreener[2], s->half);
1111+ subtract_mean_new(&s->prescreener[3], s->half);
11281112
11291113 s->prescreen[0] = process_old;
11301114 s->prescreen[1] = process_new;
@@ -1136,19 +1120,37 @@ static int config_input(AVFilterLink *inlink) · 5 unmodified lines
11361120 }
11371121 }
11381122
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−
11431123 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));
11451125 if (!s->input_buf)
11461126 return AVERROR(ENOMEM);
11471127
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));
11491135 if (!s->output_buf)
11501136 return AVERROR(ENOMEM);
11511137
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+
11521154 return 0;
11531155 }
11541156
@@ -1156,8 +1158,19 @@ static av_cold void uninit(AVFilterContext *ctx) · 1 unmodified line
11561158 {
11571159 NNEDIContext *s = ctx->priv;
11581160
1161+ for (int i = 0; i < s->nb_threads && s->prescreen_buf; i++)
1162+ av_freep(&s->prescreen_buf[i]);
1163+
11591164 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+
11601169 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+
11611174 av_freep(&s->output_buf);
11621175 av_freep(&s->fdsp);
11631176

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