FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
deshake_opencl.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com>
3  * Copyright (C) 2013 Lenny Wang
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * transform input video
25  */
26 
27 #include "libavutil/common.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/pixdesc.h"
30 #include "deshake_opencl.h"
32 
33 #define PLANE_NUM 3
34 #define ROUND_TO_16(a) (((((a) - 1)/16)+1)*16)
35 
37  int width, int height, int cw, int ch,
38  const float *matrix_y, const float *matrix_uv,
40  enum FillMethod fill, AVFrame *in, AVFrame *out)
41 {
42  int ret = 0;
43  cl_int status;
44  DeshakeContext *deshake = ctx->priv;
45  float4 packed_matrix_lu = {matrix_y[0], matrix_y[1], matrix_y[2], matrix_y[5]};
46  float4 packed_matrix_ch = {matrix_uv[0], matrix_uv[1], matrix_uv[2], matrix_uv[5]};
47  size_t global_worksize_lu[2] = {(size_t)ROUND_TO_16(width), (size_t)ROUND_TO_16(height)};
48  size_t global_worksize_ch[2] = {(size_t)ROUND_TO_16(cw), (size_t)(2*ROUND_TO_16(ch))};
49  size_t local_worksize[2] = {16, 16};
50  FFOpenclParam param_lu = {0};
51  FFOpenclParam param_ch = {0};
52  param_lu.ctx = param_ch.ctx = ctx;
53  param_lu.kernel = deshake->opencl_ctx.kernel_luma;
54  param_ch.kernel = deshake->opencl_ctx.kernel_chroma;
55 
56  if ((unsigned int)interpolate > INTERPOLATE_BIQUADRATIC) {
57  av_log(ctx, AV_LOG_ERROR, "Selected interpolate method is invalid\n");
58  return AVERROR(EINVAL);
59  }
60  ret = avpriv_opencl_set_parameter(&param_lu,
61  FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_inbuf),
62  FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_outbuf),
63  FF_OPENCL_PARAM_INFO(packed_matrix_lu),
64  FF_OPENCL_PARAM_INFO(interpolate),
68  FF_OPENCL_PARAM_INFO(height),
69  FF_OPENCL_PARAM_INFO(width),
70  NULL);
71  if (ret < 0)
72  return ret;
73  ret = avpriv_opencl_set_parameter(&param_ch,
74  FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_inbuf),
75  FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_outbuf),
76  FF_OPENCL_PARAM_INFO(packed_matrix_ch),
77  FF_OPENCL_PARAM_INFO(interpolate),
83  FF_OPENCL_PARAM_INFO(height),
84  FF_OPENCL_PARAM_INFO(width),
87  NULL);
88  if (ret < 0)
89  return ret;
90  status = clEnqueueNDRangeKernel(deshake->opencl_ctx.command_queue,
91  deshake->opencl_ctx.kernel_luma, 2, NULL,
92  global_worksize_lu, local_worksize, 0, NULL, NULL);
93  status |= clEnqueueNDRangeKernel(deshake->opencl_ctx.command_queue,
94  deshake->opencl_ctx.kernel_chroma, 2, NULL,
95  global_worksize_ch, local_worksize, 0, NULL, NULL);
96  if (status != CL_SUCCESS) {
97  av_log(ctx, AV_LOG_ERROR, "OpenCL run kernel error occurred: %s\n", av_opencl_errstr(status));
98  return AVERROR_EXTERNAL;
99  }
100  ret = av_opencl_buffer_read_image(out->data, deshake->opencl_ctx.out_plane_size,
101  deshake->opencl_ctx.plane_num, deshake->opencl_ctx.cl_outbuf,
102  deshake->opencl_ctx.cl_outbuf_size);
103  if (ret < 0)
104  return ret;
105  return ret;
106 }
107 
109 {
110  int ret = 0;
111  DeshakeContext *deshake = ctx->priv;
112  ret = av_opencl_init(NULL);
113  if (ret < 0)
114  return ret;
115  deshake->opencl_ctx.plane_num = PLANE_NUM;
116  deshake->opencl_ctx.command_queue = av_opencl_get_command_queue();
117  if (!deshake->opencl_ctx.command_queue) {
118  av_log(ctx, AV_LOG_ERROR, "Unable to get OpenCL command queue in filter 'deshake'\n");
119  return AVERROR(EINVAL);
120  }
121  deshake->opencl_ctx.program = av_opencl_compile("avfilter_transform", NULL);
122  if (!deshake->opencl_ctx.program) {
123  av_log(ctx, AV_LOG_ERROR, "OpenCL failed to compile program 'avfilter_transform'\n");
124  return AVERROR(EINVAL);
125  }
126  if (!deshake->opencl_ctx.kernel_luma) {
127  deshake->opencl_ctx.kernel_luma = clCreateKernel(deshake->opencl_ctx.program,
128  "avfilter_transform_luma", &ret);
129  if (ret != CL_SUCCESS) {
130  av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel 'avfilter_transform_luma'\n");
131  return AVERROR(EINVAL);
132  }
133  }
134  if (!deshake->opencl_ctx.kernel_chroma) {
135  deshake->opencl_ctx.kernel_chroma = clCreateKernel(deshake->opencl_ctx.program,
136  "avfilter_transform_chroma", &ret);
137  if (ret != CL_SUCCESS) {
138  av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel 'avfilter_transform_chroma'\n");
139  return AVERROR(EINVAL);
140  }
141  }
142  return ret;
143 }
144 
146 {
147  DeshakeContext *deshake = ctx->priv;
148  av_opencl_buffer_release(&deshake->opencl_ctx.cl_inbuf);
149  av_opencl_buffer_release(&deshake->opencl_ctx.cl_outbuf);
150  clReleaseKernel(deshake->opencl_ctx.kernel_luma);
151  clReleaseKernel(deshake->opencl_ctx.kernel_chroma);
152  clReleaseProgram(deshake->opencl_ctx.program);
153  deshake->opencl_ctx.command_queue = NULL;
155 }
156 
158 {
159  int ret = 0;
160  AVFilterLink *link = ctx->inputs[0];
161  DeshakeContext *deshake = ctx->priv;
162  const int hshift = av_pix_fmt_desc_get(link->format)->log2_chroma_h;
163  int chroma_height = FF_CEIL_RSHIFT(link->h, hshift);
164 
165  if ((!deshake->opencl_ctx.cl_inbuf) || (!deshake->opencl_ctx.cl_outbuf)) {
166  deshake->opencl_ctx.in_plane_size[0] = (in->linesize[0] * in->height);
167  deshake->opencl_ctx.in_plane_size[1] = (in->linesize[1] * chroma_height);
168  deshake->opencl_ctx.in_plane_size[2] = (in->linesize[2] * chroma_height);
169  deshake->opencl_ctx.out_plane_size[0] = (out->linesize[0] * out->height);
170  deshake->opencl_ctx.out_plane_size[1] = (out->linesize[1] * chroma_height);
171  deshake->opencl_ctx.out_plane_size[2] = (out->linesize[2] * chroma_height);
172  deshake->opencl_ctx.cl_inbuf_size = deshake->opencl_ctx.in_plane_size[0] +
173  deshake->opencl_ctx.in_plane_size[1] +
174  deshake->opencl_ctx.in_plane_size[2];
175  deshake->opencl_ctx.cl_outbuf_size = deshake->opencl_ctx.out_plane_size[0] +
176  deshake->opencl_ctx.out_plane_size[1] +
177  deshake->opencl_ctx.out_plane_size[2];
178  if (!deshake->opencl_ctx.cl_inbuf) {
179  ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_inbuf,
180  deshake->opencl_ctx.cl_inbuf_size,
181  CL_MEM_READ_ONLY, NULL);
182  if (ret < 0)
183  return ret;
184  }
185  if (!deshake->opencl_ctx.cl_outbuf) {
186  ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_outbuf,
187  deshake->opencl_ctx.cl_outbuf_size,
188  CL_MEM_READ_WRITE, NULL);
189  if (ret < 0)
190  return ret;
191  }
192  }
193  ret = av_opencl_buffer_write_image(deshake->opencl_ctx.cl_inbuf,
194  deshake->opencl_ctx.cl_inbuf_size,
195  0, in->data,deshake->opencl_ctx.in_plane_size,
196  deshake->opencl_ctx.plane_num);
197  if(ret < 0)
198  return ret;
199  return ret;
200 }
#define NULL
Definition: coverity.c:32
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2090
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
const char * av_opencl_errstr(cl_int status)
Get OpenCL error string.
Definition: opencl.c:165
#define PLANE_NUM
InterpolateMethod
Definition: transform.h:39
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
Public dictionary API.
static void interpolate(float *out, float v1, float v2, int size)
Definition: twinvq.c:84
#define av_log(a,...)
int ff_opencl_transform(AVFilterContext *ctx, int width, int height, int cw, int ch, const float *matrix_y, const float *matrix_uv, enum InterpolateMethod interpolate, enum FillMethod fill, AVFrame *in, AVFrame *out)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
#define AVERROR(e)
Definition: error.h:43
void * priv
private data for use by the filter
Definition: avfilter.h:654
int avpriv_opencl_set_parameter(FFOpenclParam *opencl_param,...)
FillMethod
Definition: transform.h:51
ret
Definition: avfilter.c:974
#define FF_CEIL_RSHIFT(a, b)
Definition: common.h:57
cl_program av_opencl_compile(const char *program_name, const char *build_opts)
compile specific OpenCL kernel source
Definition: opencl.c:449
int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
Create OpenCL buffer.
Definition: opencl.c:659
int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset, uint8_t **src_data, int *plane_size, int plane_num)
Write image data from memory to OpenCL buffer.
Definition: opencl.c:729
int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
cl_kernel kernel
void av_opencl_buffer_release(cl_mem *cl_buf)
Release OpenCL buffer.
Definition: opencl.c:670
cl_command_queue av_opencl_get_command_queue(void)
get OpenCL command queue
Definition: opencl.c:498
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
int ff_opencl_deshake_init(AVFilterContext *ctx)
common internal and external API header
void av_opencl_uninit(void)
Release OpenCL environment.
Definition: opencl.c:623
#define FF_OPENCL_PARAM_INFO(a)
int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env)
Initialize the run time OpenCL environment.
Definition: opencl.c:596
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
An instance of a filter.
Definition: avfilter.h:633
int height
Definition: frame.h:220
#define ROUND_TO_16(a)
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
void ff_opencl_deshake_uninit(AVFilterContext *ctx)
int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num, cl_mem src_cl_buf, size_t cl_buffer_size)
Read image data from OpenCL buffer.
Definition: opencl.c:770
static int width