From patchwork Tue Dec 18 16:04:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Guo, Yejun" X-Patchwork-Id: 11456 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 56BD044B7D1 for ; Tue, 18 Dec 2018 10:11:54 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7D42768AA01; Tue, 18 Dec 2018 10:11:54 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3A05C68A9FF for ; Tue, 18 Dec 2018 10:11:46 +0200 (EET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Dec 2018 00:11:46 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,367,1539673200"; d="scan'208";a="260356651" Received: from yguo18-skl-u1604.sh.intel.com ([10.239.13.25]) by orsmga004.jf.intel.com with ESMTP; 18 Dec 2018 00:11:45 -0800 From: "Guo, Yejun" To: ffmpeg-devel@ffmpeg.org Date: Wed, 19 Dec 2018 00:04:04 +0800 Message-Id: <1545149044-9552-1-git-send-email-yejun.guo@intel.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH V7] Add a filter implementing HDR image generation from a single exposure using deep CNNs X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" see the algorithm's paper and code below. the filter's parameter looks like: sdr2hdr=model_filename=/path_to_tensorflow_graph.pb:out_fmt=gbrp10le The input of the deep CNN model is RGB24 while the output is float for each color channel. This is the filter's default behavior to output format with gbrpf32le. And gbrp10le is also supported as the output, so we can see the rendering result in a player, as a reference. Each CNN model file is tied with the image resolution due to the conv transpose layer of tensorflow, see discussion at https://github.com/tensorflow/tensorflow/issues/2118#issuecomment-441146241. It is not expected to be fixed in near future, and so we have to prepare different model files for different resolutions. For your convenience, I uploaded several models (1080p, 720p) under https://drive.google.com/drive/folders/1URsRY5g-VdE-kHlP5vQoLoimMIZ-SX00?usp=sharing To generate the model file, we need to modify the script a little, I have finished the change and uploaded to https://github.com/guoyejun/hdrcnn.git, One example to generate model file for 720p: python hdrcnn_save.py --params hdrcnn_params.npz --im_dir data/img_001.png --width 1280 --height 720 The filter only works when tensorflow C api is supported in the system, native backend is not supported since there are some different types of layers in the deep CNN model, besides CONV and DEPTH_TO_SPACE. https://arxiv.org/pdf/1710.07480.pdf: author = "Eilertsen, Gabriel and Kronander, Joel, and Denes, Gyorgy and Mantiuk, RafaƂ and Unger, Jonas", title = "HDR image reconstruction from a single exposure using deep CNNs", journal = "ACM Transactions on Graphics (TOG)", number = "6", volume = "36", articleno = "178", year = "2017" https://github.com/gabrieleilertsen/hdrcnn btw, as a whole solution, metadata should also be generated from the sdr video, so to be encoded as a HDR video. Not supported yet. This patch just focuses on this paper. Result: This filter accepts 8bit frame (RGB24) and outputs 10bit/float frame, and there's no reference image, so it is not feasible to use criteria such as PNSR, SSIM. I choose the same method described in the paper to demo the filter effect, that means the frames before/after the filter are reduced by 3 stops. The native video (test.native.mp4) is created from 7 png files at https://github.com/gabrieleilertsen/hdrcnn/tree/master/data (the size of the image is enlarged to 1920*1080 with extra area filled with white) with command line: ffmpeg -f image2 -i ./img_%03d.png -c:v libx264 -preset veryslow -crf 1 test.native.mp4. And two rgb24 videos are generated before/after the filter with -3 stops by modifying the code a little, see in the video folder at the google drive (the same place as where the model file locates). I also dump png files from generated videos and combine the before/after pngs into one file, see in png folder at the google drive. Signed-off-by: Guo, Yejun --- configure | 1 + doc/filters.texi | 37 ++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_sdr2hdr.c | 299 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 339 insertions(+) create mode 100644 libavfilter/vf_sdr2hdr.c diff --git a/configure b/configure index be49c19..5a60408 100755 --- a/configure +++ b/configure @@ -3449,6 +3449,7 @@ sab_filter_deps="gpl swscale" scale2ref_filter_deps="swscale" scale_filter_deps="swscale" scale_qsv_filter_deps="libmfx" +sdr2hdr_filter_deps="libtensorflow" select_filter_select="scene_sad" sharpness_vaapi_filter_deps="vaapi" showcqt_filter_deps="avcodec avformat swscale" diff --git a/doc/filters.texi b/doc/filters.texi index ac4c9b4..a671429 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -14981,6 +14981,43 @@ Scale a subtitle stream (b) to match the main video (a) in size before overlayin @end example @end itemize +@section sdr2hdr + +HDR image generation from a single exposure using deep CNNs with TensorFlow C library. +The input format of the filter is RGB24, there's no meta data generated for HDR video yet. + +@itemize +@item +paper: see @url{https://arxiv.org/pdf/1710.07480.pdf} + +@item +code with model and trained parameters: see @url{https://github.com/gabrieleilertsen/hdrcnn} +@end itemize + +The filter accepts the following options: + +@table @option + +@item model_filename +Set path to model file specifying network architecture and its parameters, can download +from @url{https://drive.google.com/drive/folders/1URsRY5g-VdE-kHlP5vQoLoimMIZ-SX00?usp=sharing} + +@item out_fmt +the data format of the filter's output. + +It accepts the following values: +@table @samp +@item gbrpf32le +force gbrpf32le output + +@item gbrp10le +force gbrp10le output +@end table + +Default value is @samp{gbrpf32le}. + +@end table + @anchor{selectivecolor} @section selectivecolor diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 6e26581..a2f5c46 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -370,6 +370,7 @@ OBJS-$(CONFIG_SOBEL_OPENCL_FILTER) += vf_convolution_opencl.o opencl.o OBJS-$(CONFIG_SPLIT_FILTER) += split.o OBJS-$(CONFIG_SPP_FILTER) += vf_spp.o OBJS-$(CONFIG_SR_FILTER) += vf_sr.o +OBJS-$(CONFIG_SDR2HDR_FILTER) += vf_sdr2hdr.o OBJS-$(CONFIG_SSIM_FILTER) += vf_ssim.o framesync.o OBJS-$(CONFIG_STEREO3D_FILTER) += vf_stereo3d.o OBJS-$(CONFIG_STREAMSELECT_FILTER) += f_streamselect.o framesync.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index a600069..a8a8787 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -326,6 +326,7 @@ extern AVFilter ff_vf_scale_npp; extern AVFilter ff_vf_scale_qsv; extern AVFilter ff_vf_scale_vaapi; extern AVFilter ff_vf_scale2ref; +extern AVFilter ff_vf_sdr2hdr; extern AVFilter ff_vf_select; extern AVFilter ff_vf_selectivecolor; extern AVFilter ff_vf_sendcmd; diff --git a/libavfilter/vf_sdr2hdr.c b/libavfilter/vf_sdr2hdr.c new file mode 100644 index 0000000..7d78cb0 --- /dev/null +++ b/libavfilter/vf_sdr2hdr.c @@ -0,0 +1,299 @@ +/* + * Copyright (c) 2018 Guo Yejun + * + * 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 HDR image generation from a single exposure using deep CNNs. + * https://arxiv.org/pdf/1710.07480.pdf + */ + +#include + +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "libavutil/opt.h" +#include "libavutil/qsort.h" +#include "libavutil/avassert.h" +#include "libavformat/avio.h" +#include "libswscale/swscale.h" +#include "dnn_interface.h" + +typedef struct SDR2HDRContext { + const AVClass *class; + + char* model_filename; + enum AVPixelFormat out_fmt; + DNNModule* dnn_module; + DNNModel* model; + DNNData input, output; +} SDR2HDRContext; + +#define OFFSET(x) offsetof(SDR2HDRContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_VIDEO_PARAM +static const AVOption sdr2hdr_options[] = { + { "model_filename", "path to model file specifying network architecture and its parameters", OFFSET(model_filename), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS }, + { "out_fmt", "the data format of the filter's output, it could be gbrpf32le [default] or gbrp10le", OFFSET(out_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64=AV_PIX_FMT_GBRPF32LE}, AV_PIX_FMT_NONE, AV_PIX_FMT_NB - 1, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(sdr2hdr); + +static av_cold int init(AVFilterContext* context) +{ + SDR2HDRContext* ctx = context->priv; + + if (ctx->out_fmt != AV_PIX_FMT_GBRPF32LE && ctx->out_fmt != AV_PIX_FMT_GBRP10LE) { + av_log(context, AV_LOG_ERROR, "could not support the output format\n"); + return AVERROR(ENOSYS); + } + + ctx->dnn_module = ff_get_dnn_module(DNN_TF); + if (!ctx->dnn_module){ + av_log(context, AV_LOG_ERROR, "could not create DNN module for tensorflow backend\n"); + return AVERROR(ENOMEM); + } + if (!ctx->model_filename){ + av_log(context, AV_LOG_ERROR, "model file for network was not specified\n"); + return AVERROR(EIO); + } + if (!ctx->dnn_module->load_model) { + av_log(context, AV_LOG_ERROR, "load_model for network was not specified\n"); + return AVERROR(EIO); + } + ctx->model = (ctx->dnn_module->load_model)(ctx->model_filename); + if (!ctx->model){ + av_log(context, AV_LOG_ERROR, "could not load DNN model\n"); + return AVERROR(EIO); + } + return 0; +} + +static int query_formats(AVFilterContext* context) +{ + const enum AVPixelFormat in_formats[] = {AV_PIX_FMT_RGB24, + AV_PIX_FMT_NONE}; + enum AVPixelFormat out_formats[2]; + SDR2HDRContext* ctx = context->priv; + AVFilterFormats* formats_list; + int ret = 0; + + formats_list = ff_make_format_list(in_formats); + if ((ret = ff_formats_ref(formats_list, &context->inputs[0]->out_formats)) < 0) + return ret; + + out_formats[0] = ctx->out_fmt; + out_formats[1] = AV_PIX_FMT_NONE; + formats_list = ff_make_format_list(out_formats); + if ((ret = ff_formats_ref(formats_list, &context->outputs[0]->in_formats)) < 0) + return ret; + + return 0; +} + +static int config_props(AVFilterLink* inlink) +{ + AVFilterContext* context = inlink->dst; + SDR2HDRContext* ctx = context->priv; + AVFilterLink* outlink = context->outputs[0]; + DNNReturnType result; + + // the model requires multiple of 32, so that autoencoder + // pooling+upsampling yields same size as input image + ctx->input.width = FFALIGN(inlink->w, 32); + ctx->input.height = FFALIGN(inlink->h, 32); + ctx->input.channels = 3; + + result = (ctx->model->set_input_output)(ctx->model->model, &ctx->input, &ctx->output); + if (result != DNN_SUCCESS){ + av_log(context, AV_LOG_ERROR, "could not set input and output for the model\n"); + return AVERROR(EIO); + } + + memset(ctx->input.data, 0, ctx->input.channels * ctx->input.width * ctx->input.height * sizeof(float)); + outlink->h = inlink->h; + outlink->w = inlink->w; + return 0; +} + +static float qsort_comparison_function_float(const void *a, const void *b) +{ + return *(const float *)a - *(const float *)b; +} + +static int filter_frame(AVFilterLink* inlink, AVFrame* in) +{ + DNNReturnType dnn_result = DNN_SUCCESS; + AVFilterContext* context = inlink->dst; + SDR2HDRContext* ctx = context->priv; + AVFilterLink* outlink = context->outputs[0]; + AVFrame* out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + int total_pixels = in->height * in->width; + + 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); + + for (int i = 0; i < in->height; ++i) { + for (int j = 0; j < in->linesize[0]; ++j) { + ctx->input.data[i * ctx->input.width * ctx->input.channels + j] = + in->data[0][i * in->linesize[0] + j] / 255.0f; + } + } + + dnn_result = (ctx->dnn_module->execute_model)(ctx->model); + if (dnn_result != DNN_SUCCESS){ + av_log(context, AV_LOG_ERROR, "failed to execute loaded model\n"); + return AVERROR(EIO); + } + + if (ctx->out_fmt == AV_PIX_FMT_GBRPF32LE) { + float* outg = (float*)out->data[0]; + float* outb = (float*)out->data[1]; + float* outr = (float*)out->data[2]; + + for (int i = 0; i < out->height; ++i) { + for (int j = 0; j < out->width; ++j) { + float r = ctx->output.data[i * ctx->output.width * ctx->output.channels + j * 3]; + float g = ctx->output.data[i * ctx->output.width * ctx->output.channels + j * 3 + 1]; + float b = ctx->output.data[i * ctx->output.width * ctx->output.channels + j * 3 + 2]; + outr[i * out->width + j] = r; + outg[i * out->width + j] = g; + outb[i * out->width + j] = b; + } + } + } else if (ctx->out_fmt == AV_PIX_FMT_GBRP10LE) { + // here, we just use a rough mapping to the 10bit contents. + // meta data generation for HDR video encoding is not supported yet + + int16_t* outg = (int16_t*)out->data[0]; + int16_t* outb = (int16_t*)out->data[1]; + int16_t* outr = (int16_t*)out->data[2]; + + // decide the max value of the valid frame area from the model output. + // (the model might generates values with a larger area due to 32 alignment) + float max = 1.0f; + float* converted_data = (float*)av_malloc(total_pixels * 3 * sizeof(float)); + + for (int i = 0; i < out->height; ++i) { + for (int j = 0; j < out->width; ++j) { + float r = ctx->output.data[i * ctx->output.width * ctx->output.channels + j * 3]; + float g = ctx->output.data[i * ctx->output.width * ctx->output.channels + j * 3 + 1]; + float b = ctx->output.data[i * ctx->output.width * ctx->output.channels + j * 3 + 2]; + r = sqrt(r); + g = sqrt(g); + b = sqrt(b); + max = FFMAX(r, max); + max = FFMAX(g, max); + max = FFMAX(b, max); + + converted_data[i * out->width * 3 + j * 3] = r; + converted_data[i * out->width * 3 + j * 3 + 1] = g; + converted_data[i * out->width * 3 + j * 3 + 2] = b; + } + } + + if (max > 1.0f) { + AV_QSORT(converted_data, total_pixels * 3, float, qsort_comparison_function_float); + // 0.5% pixels are clipped + max = converted_data[(int)(total_pixels * 3 * 0.995)]; + max = FFMAX(max, 1.0f); + + for (int i = 0; i < out->height; ++i) { + for (int j = 0; j < out->width; ++j) { + float r = ctx->output.data[i * ctx->output.width * ctx->output.channels + j * 3]; + float g = ctx->output.data[i * ctx->output.width * ctx->output.channels + j * 3 + 1]; + float b = ctx->output.data[i * ctx->output.width * ctx->output.channels + j * 3 + 2]; + r = sqrt(r); + g = sqrt(g); + b = sqrt(b); + r = FFMIN(r, max); + g = FFMIN(g, max); + b = FFMIN(b, max); + + converted_data[i * out->width * 3 + j * 3] = r; + converted_data[i * out->width * 3 + j * 3 + 1] = g; + converted_data[i * out->width * 3 + j * 3 + 2] = b; + } + } + } + + for (int i = 0; i < total_pixels; ++i) { + float r = converted_data[i*3]; + float g = converted_data[i*3+1]; + float b = converted_data[i*3+2]; + outr[i] = r / max * 1023; + outg[i] = g / max * 1023; + outb[i] = b / max * 1023; + } + + av_free(converted_data); + } else { + av_assert0(!"should not reach here"); + } + + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +static av_cold void uninit(AVFilterContext* context) +{ + SDR2HDRContext* ctx = context->priv; + + if (ctx->dnn_module){ + (ctx->dnn_module->free_model)(&ctx->model); + av_freep(&ctx->dnn_module); + } +} + +static const AVFilterPad sdr2hdr_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_props, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad sdr2hdr_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +AVFilter ff_vf_sdr2hdr = { + .name = "sdr2hdr", + .description = NULL_IF_CONFIG_SMALL("HDR image generation from a single exposure using deep CNNs."), + .priv_size = sizeof(SDR2HDRContext), + .init = init, + .uninit = uninit, + .query_formats = query_formats, + .inputs = sdr2hdr_inputs, + .outputs = sdr2hdr_outputs, + .priv_class = &sdr2hdr_class, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, +};