summaryrefslogtreecommitdiff
path: root/libavfilter/vf_srcnn.c
blob: db1c40cf1e7a1856c3ddcab73fca651ebd65c5e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
 * Copyright (c) 2018 Sergey Lavrushkin
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * Filter implementing image super-resolution using deep convolutional networks.
 * https://arxiv.org/abs/1501.00092
 */

#include "avfilter.h"
#include "formats.h"
#include "internal.h"
#include "libavutil/opt.h"
#include "vf_srcnn.h"
#include "libavformat/avio.h"

typedef struct Convolution
{
    double* kernel;
    double* biases;
    int32_t size, input_channels, output_channels;
} Convolution;

typedef struct SRCNNContext {
    const AVClass *class;

    /// SRCNN convolutions
    struct Convolution conv1, conv2, conv3;
    /// Path to binary file with kernels specifications
    char* config_file_path;
    /// Buffers for network input/output and feature maps
    double* input_output_buf;
    double* conv1_buf;
    double* conv2_buf;
} SRCNNContext;


#define OFFSET(x) offsetof(SRCNNContext, x)
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
static const AVOption srcnn_options[] = {
    { "config_file", "path to configuration file with network parameters", OFFSET(config_file_path), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
    { NULL }
};

AVFILTER_DEFINE_CLASS(srcnn);

#define CHECK_FILE_SIZE(file_size, srcnn_size, avio_context)    if (srcnn_size > file_size){ \
                                                                    av_log(context, AV_LOG_ERROR, "error reading configuration file\n");\
                                                                    avio_closep(&avio_context); \
                                                                    return AVERROR(EIO); \
                                                                }

#define CHECK_ALLOCATION(call, end_call)    if (call){ \
                                                av_log(context, AV_LOG_ERROR, "could not allocate memory for convolutions\n"); \
                                                end_call; \
                                                return AVERROR(ENOMEM); \
                                            }

static int allocate_read_conv_data(Convolution* conv, AVIOContext* config_file_context)
{
    int32_t kernel_size = conv->output_channels * conv->size * conv->size * conv->input_channels;
    int32_t i;

    conv->kernel = av_malloc(kernel_size * sizeof(double));
    if (!conv->kernel){
        return AVERROR(ENOMEM);
    }
    for (i = 0; i < kernel_size; ++i){
        conv->kernel[i] = av_int2double(avio_rl64(config_file_context));
    }

    conv->biases = av_malloc(conv->output_channels * sizeof(double));
    if (!conv->biases){
        return AVERROR(ENOMEM);
    }
    for (i = 0; i < conv->output_channels; ++i){
        conv->biases[i] = av_int2double(avio_rl64(config_file_context));
    }

    return 0;
}

static int allocate_copy_conv_data(Convolution* conv, const double* kernel, const double* biases)
{
    int32_t kernel_size = conv->output_channels * conv->size * conv->size * conv->input_channels;

    conv->kernel = av_malloc(kernel_size * sizeof(double));
    if (!conv->kernel){
        return AVERROR(ENOMEM);
    }
    memcpy(conv->kernel, kernel, kernel_size * sizeof(double));

    conv->biases = av_malloc(conv->output_channels * sizeof(double));
    if (!conv->kernel){
        return AVERROR(ENOMEM);
    }
    memcpy(conv->biases, biases, conv->output_channels * sizeof(double));

    return 0;
}

static av_cold int init(AVFilterContext* context)
{
    SRCNNContext *srcnn_context = context->priv;
    AVIOContext* config_file_context;
    int64_t file_size, srcnn_size;

    /// Check specified confguration file name and read network weights from it
    if (!srcnn_context->config_file_path){
        av_log(context, AV_LOG_INFO, "configuration file for network was not specified, using default weights for x2 upsampling\n");

        /// Create convolution kernels and copy default weights
        srcnn_context->conv1.input_channels = 1;
        srcnn_context->conv1.output_channels = 64;
        srcnn_context->conv1.size = 9;
        CHECK_ALLOCATION(allocate_copy_conv_data(&srcnn_context->conv1, conv1_kernel, conv1_biases), )

        srcnn_context->conv2.input_channels = 64;
        srcnn_context->conv2.output_channels = 32;
        srcnn_context->conv2.size = 1;
        CHECK_ALLOCATION(allocate_copy_conv_data(&srcnn_context->conv2, conv2_kernel, conv2_biases), )

        srcnn_context->conv3.input_channels = 32;
        srcnn_context->conv3.output_channels = 1;
        srcnn_context->conv3.size = 5;
        CHECK_ALLOCATION(allocate_copy_conv_data(&srcnn_context->conv3, conv3_kernel, conv3_biases), )
    }
    else if (avio_check(srcnn_context->config_file_path, AVIO_FLAG_READ) > 0){
        if (avio_open(&config_file_context, srcnn_context->config_file_path, AVIO_FLAG_READ) < 0){
            av_log(context, AV_LOG_ERROR, "failed to open configuration file\n");
            return AVERROR(EIO);
        }

        file_size = avio_size(config_file_context);

        /// Create convolution kernels and read weights from file
        srcnn_context->conv1.input_channels = 1;
        srcnn_context->conv1.size = (int32_t)avio_rl32(config_file_context);
        srcnn_context->conv1.output_channels = (int32_t)avio_rl32(config_file_context);
        srcnn_size = 8 + (srcnn_context->conv1.output_channels * srcnn_context->conv1.size *
                          srcnn_context->conv1.size * srcnn_context->conv1.input_channels +
                          srcnn_context->conv1.output_channels << 3);
        CHECK_FILE_SIZE(file_size, srcnn_size, config_file_context)
        CHECK_ALLOCATION(allocate_read_conv_data(&srcnn_context->conv1, config_file_context), avio_closep(&config_file_context))

        srcnn_context->conv2.input_channels = (int32_t)avio_rl32(config_file_context);
        srcnn_context->conv2.size = (int32_t)avio_rl32(config_file_context);
        srcnn_context->conv2.output_channels = (int32_t)avio_rl32(config_file_context);
        srcnn_size += 12 + (srcnn_context->conv2.output_channels * srcnn_context->conv2.size *
                            srcnn_context->conv2.size * srcnn_context->conv2.input_channels +
                            srcnn_context->conv2.output_channels << 3);
        CHECK_FILE_SIZE(file_size, srcnn_size, config_file_context)
        CHECK_ALLOCATION(allocate_read_conv_data(&srcnn_context->conv2, config_file_context), avio_closep(&config_file_context))

        srcnn_context->conv3.input_channels = (int32_t)avio_rl32(config_file_context);
        srcnn_context->conv3.size = (int32_t)avio_rl32(config_file_context);
        srcnn_context->conv3.output_channels = 1;
        srcnn_size += 8 + (srcnn_context->conv3.output_channels * srcnn_context->conv3.size *
                           srcnn_context->conv3.size * srcnn_context->conv3.input_channels
                           + srcnn_context->conv3.output_channels << 3);
        if (file_size != srcnn_size){
            av_log(context, AV_LOG_ERROR, "error reading configuration file\n");
            avio_closep(&config_file_context);
            return AVERROR(EIO);
        }
        CHECK_ALLOCATION(allocate_read_conv_data(&srcnn_context->conv3, config_file_context), avio_closep(&config_file_context))

        avio_closep(&config_file_context);
    }
    else{
        av_log(context, AV_LOG_ERROR, "specified configuration file does not exist or not readable\n");
        return AVERROR(EIO);
    }

    return 0;
}

static int query_formats(AVFilterContext* context)
{
    const enum AVPixelFormat pixel_formats[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
                                                AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_GRAY8,
                                                AV_PIX_FMT_NONE};
    AVFilterFormats *formats_list;

    formats_list = ff_make_format_list(pixel_formats);
    if (!formats_list){
        av_log(context, AV_LOG_ERROR, "could not create formats list\n");
        return AVERROR(ENOMEM);
    }
    return ff_set_common_formats(context, formats_list);
}

static int config_props(AVFilterLink* inlink)
{
    AVFilterContext *context = inlink->dst;
    SRCNNContext *srcnn_context = context->priv;
    int min_dim;

    /// Check if input data width or height is too low
    min_dim = FFMIN(inlink->w, inlink->h);
    if (min_dim <= srcnn_context->conv1.size >> 1 || min_dim <= srcnn_context->conv2.size >> 1 || min_dim <= srcnn_context->conv3.size >> 1){
        av_log(context, AV_LOG_ERROR, "input width or height is too low\n");
        return AVERROR(EIO);
    }

    /// Allocate network buffers
    srcnn_context->input_output_buf = av_malloc(inlink->h * inlink->w * sizeof(double));
    srcnn_context->conv1_buf = av_malloc(inlink->h * inlink->w * srcnn_context->conv1.output_channels * sizeof(double));
    srcnn_context->conv2_buf = av_malloc(inlink->h * inlink->w * srcnn_context->conv2.output_channels * sizeof(double));

    if (!srcnn_context->input_output_buf || !srcnn_context->conv1_buf || !srcnn_context->conv2_buf){
        av_log(context, AV_LOG_ERROR, "could not allocate memory for srcnn buffers\n");
        return AVERROR(ENOMEM);
    }

    return 0;
}

typedef struct ThreadData{
    uint8_t* out;
    int out_linesize, height, width;
} ThreadData;

typedef struct ConvThreadData
{
    const Convolution* conv;
    const double* input;
    double* output;
    int height, width;
} ConvThreadData;

/// Convert uint8 data to double and scale it to use in network
static int uint8_to_double(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
{
    SRCNNContext* srcnn_context = context->priv;
    const ThreadData* td = arg;
    const int slice_start = (td->height *  jobnr     ) / nb_jobs;
    const int slice_end   = (td->height * (jobnr + 1)) / nb_jobs;
    const uint8_t* src = td->out + slice_start * td->out_linesize;
    double* dst = srcnn_context->input_output_buf + slice_start * td->width;
    int y, x;

    for (y = slice_start; y < slice_end; ++y){
        for (x = 0; x < td->width; ++x){
            dst[x] = (double)src[x] / 255.0;
        }
        src += td->out_linesize;
        dst += td->width;
    }

    return 0;
}

/// Convert double data from network to uint8 and scale it to output as filter result
static int double_to_uint8(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
{
    SRCNNContext* srcnn_context = context->priv;
    const ThreadData* td = arg;
    const int slice_start = (td->height *  jobnr     ) / nb_jobs;
    const int slice_end   = (td->height * (jobnr + 1)) / nb_jobs;
    const double* src = srcnn_context->input_output_buf + slice_start * td->width;
    uint8_t* dst = td->out + slice_start * td->out_linesize;
    int y, x;

    for (y = slice_start; y < slice_end; ++y){
        for (x = 0; x < td->width; ++x){
            dst[x] = (uint8_t)(255.0 * FFMIN(src[x], 1.0));
        }
        src += td->width;
        dst += td->out_linesize;
    }

    return 0;
}

#define CLAMP_TO_EDGE(x, w) ((x) < 0 ? 0 : ((x) >= (w) ? (w - 1) : (x)))

static int convolve(AVFilterContext* context, void* arg, int jobnr, int nb_jobs)
{
    const ConvThreadData* td = arg;
    const int slice_start = (td->height *  jobnr     ) / nb_jobs;
    const int slice_end   = (td->height * (jobnr + 1)) / nb_jobs;
    const double* src = td->input;
    double* dst = td->output + slice_start * td->width * td->conv->output_channels;
    int y, x;
    int32_t n_filter, ch, kernel_y, kernel_x;
    int32_t radius = td->conv->size >> 1;
    int src_linesize = td->width * td->conv->input_channels;
    int filter_linesize = td->conv->size * td->conv->input_channels;
    int filter_size = td->conv->size * filter_linesize;

    for (y = slice_start; y < slice_end; ++y){
        for (x = 0; x < td->width; ++x){
            for (n_filter = 0; n_filter < td->conv->output_channels; ++n_filter){
                dst[n_filter] = td->conv->biases[n_filter];
                for (ch = 0; ch < td->conv->input_channels; ++ch){
                    for (kernel_y = 0; kernel_y < td->conv->size; ++kernel_y){
                        for (kernel_x = 0; kernel_x < td->conv->size; ++kernel_x){
                            dst[n_filter] += src[CLAMP_TO_EDGE(y + kernel_y - radius, td->height) * src_linesize +
                                                 CLAMP_TO_EDGE(x + kernel_x - radius, td->width) * td->conv->input_channels + ch] *
                                             td->conv->kernel[n_filter * filter_size + kernel_y * filter_linesize +
                                                              kernel_x * td->conv->input_channels + ch];
                        }
                    }
                }
                dst[n_filter] = FFMAX(dst[n_filter], 0.0);
            }
            dst += td->conv->output_channels;
        }
    }

    return 0;
}

static int filter_frame(AVFilterLink* inlink, AVFrame* in)
{
    AVFilterContext* context = inlink->dst;
    SRCNNContext* srcnn_context = context->priv;
    AVFilterLink* outlink = context->outputs[0];
    AVFrame* out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
    ThreadData td;
    ConvThreadData ctd;
    int nb_threads;

    if (!out){
        av_log(context, AV_LOG_ERROR, "could not allocate memory for output frame\n");
        av_frame_free(&in);
        return AVERROR(ENOMEM);
    }
    av_frame_copy_props(out, in);
    av_frame_copy(out, in);
    av_frame_free(&in);
    td.out = out->data[0];
    td.out_linesize = out->linesize[0];
    td.height = ctd.height = out->height;
    td.width = ctd.width = out->width;

    nb_threads = ff_filter_get_nb_threads(context);
    context->internal->execute(context, uint8_to_double, &td, NULL, FFMIN(td.height, nb_threads));
    ctd.conv = &srcnn_context->conv1;
    ctd.input = srcnn_context->input_output_buf;
    ctd.output = srcnn_context->conv1_buf;
    context->internal->execute(context, convolve, &ctd, NULL, FFMIN(ctd.height, nb_threads));
    ctd.conv = &srcnn_context->conv2;
    ctd.input = srcnn_context->conv1_buf;
    ctd.output = srcnn_context->conv2_buf;
    context->internal->execute(context, convolve, &ctd, NULL, FFMIN(ctd.height, nb_threads));
    ctd.conv = &srcnn_context->conv3;
    ctd.input = srcnn_context->conv2_buf;
    ctd.output = srcnn_context->input_output_buf;
    context->internal->execute(context, convolve, &ctd, NULL, FFMIN(ctd.height, nb_threads));
    context->internal->execute(context, double_to_uint8, &td, NULL, FFMIN(td.height, nb_threads));

    return ff_filter_frame(outlink, out);
}

static av_cold void uninit(AVFilterContext* context)
{
    SRCNNContext* srcnn_context = context->priv;

    /// Free convolution data
    av_freep(&srcnn_context->conv1.kernel);
    av_freep(&srcnn_context->conv1.biases);
    av_freep(&srcnn_context->conv2.kernel);
    av_freep(&srcnn_context->conv2.biases);
    av_freep(&srcnn_context->conv3.kernel);
    av_freep(&srcnn_context->conv3.kernel);

    /// Free network buffers
    av_freep(&srcnn_context->input_output_buf);
    av_freep(&srcnn_context->conv1_buf);
    av_freep(&srcnn_context->conv2_buf);
}

static const AVFilterPad srcnn_inputs[] = {
    {
        .name         = "default",
        .type         = AVMEDIA_TYPE_VIDEO,
        .config_props = config_props,
        .filter_frame = filter_frame,
    },
    { NULL }
};

static const AVFilterPad srcnn_outputs[] = {
    {
        .name = "default",
        .type = AVMEDIA_TYPE_VIDEO,
    },
    { NULL }
};

AVFilter ff_vf_srcnn = {
    .name          = "srcnn",
    .description   = NULL_IF_CONFIG_SMALL("Apply super resolution convolutional neural network to the input. Use bicubic upsamping with corresponding scaling factor before."),
    .priv_size     = sizeof(SRCNNContext),
    .init          = init,
    .uninit        = uninit,
    .query_formats = query_formats,
    .inputs        = srcnn_inputs,
    .outputs       = srcnn_outputs,
    .priv_class    = &srcnn_class,
    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
};