FFmpeg  2.1.1
deshake_opencl.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * transform input video
24  */
25 
26 #include "libavutil/common.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/pixdesc.h"
29 #include "deshake_opencl.h"
31 
32 #define MATRIX_SIZE 6
33 #define PLANE_NUM 3
34 
36  int width, int height, int cw, int ch,
37  const float *matrix_y, const float *matrix_uv,
39  enum FillMethod fill, AVFrame *in, AVFrame *out)
40 {
41  int ret = 0;
42  const size_t global_work_size = width * height + 2 * ch * cw;
43  cl_int status;
44  DeshakeContext *deshake = ctx->priv;
45  FFOpenclParam opencl_param = {0};
46 
47  opencl_param.ctx = ctx;
48  opencl_param.kernel = deshake->opencl_ctx.kernel_env.kernel;
49  ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_y, (uint8_t *)matrix_y, deshake->opencl_ctx.matrix_size * sizeof(cl_float));
50  if (ret < 0)
51  return ret;
52  ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_uv, (uint8_t *)matrix_uv, deshake->opencl_ctx.matrix_size * sizeof(cl_float));
53  if (ret < 0)
54  return ret;
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 = ff_opencl_set_parameter(&opencl_param,
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(deshake->opencl_ctx.cl_matrix_y),
64  FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_matrix_uv),
65  FF_OPENCL_PARAM_INFO(interpolate),
71  FF_OPENCL_PARAM_INFO(height),
72  FF_OPENCL_PARAM_INFO(width),
75  NULL);
76  if (ret < 0)
77  return ret;
78  status = clEnqueueNDRangeKernel(deshake->opencl_ctx.kernel_env.command_queue,
79  deshake->opencl_ctx.kernel_env.kernel, 1, NULL,
80  &global_work_size, NULL, 0, NULL, NULL);
81  if (status != CL_SUCCESS) {
82  av_log(ctx, AV_LOG_ERROR, "OpenCL run kernel error occurred: %s\n", av_opencl_errstr(status));
83  return AVERROR_EXTERNAL;
84  }
85  clFinish(deshake->opencl_ctx.kernel_env.command_queue);
86  ret = av_opencl_buffer_read_image(out->data, deshake->opencl_ctx.out_plane_size,
87  deshake->opencl_ctx.plane_num, deshake->opencl_ctx.cl_outbuf,
88  deshake->opencl_ctx.cl_outbuf_size);
89  if (ret < 0)
90  return ret;
91  return ret;
92 }
93 
95 {
96  int ret = 0;
97  DeshakeContext *deshake = ctx->priv;
98  ret = av_opencl_init(NULL);
99  if (ret < 0)
100  return ret;
101  deshake->opencl_ctx.matrix_size = MATRIX_SIZE;
102  deshake->opencl_ctx.plane_num = PLANE_NUM;
103  ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_y,
104  deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
105  if (ret < 0)
106  return ret;
107  ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_uv,
108  deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
109  if (ret < 0)
110  return ret;
111  if (!deshake->opencl_ctx.kernel_env.kernel) {
112  ret = av_opencl_create_kernel(&deshake->opencl_ctx.kernel_env, "avfilter_transform");
113  if (ret < 0) {
114  av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel for name 'avfilter_transform'\n");
115  return ret;
116  }
117  }
118  return ret;
119 }
120 
122 {
123  DeshakeContext *deshake = ctx->priv;
124  av_opencl_buffer_release(&deshake->opencl_ctx.cl_inbuf);
125  av_opencl_buffer_release(&deshake->opencl_ctx.cl_outbuf);
126  av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_y);
127  av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_uv);
128  av_opencl_release_kernel(&deshake->opencl_ctx.kernel_env);
130 }
131 
132 
134 {
135  int ret = 0;
136  AVFilterLink *link = ctx->inputs[0];
137  DeshakeContext *deshake = ctx->priv;
138  const int hshift = av_pix_fmt_desc_get(link->format)->log2_chroma_h;
139  int chroma_height = FF_CEIL_RSHIFT(link->h, hshift);
140 
141  if ((!deshake->opencl_ctx.cl_inbuf) || (!deshake->opencl_ctx.cl_outbuf)) {
142  deshake->opencl_ctx.in_plane_size[0] = (in->linesize[0] * in->height);
143  deshake->opencl_ctx.in_plane_size[1] = (in->linesize[1] * chroma_height);
144  deshake->opencl_ctx.in_plane_size[2] = (in->linesize[2] * chroma_height);
145  deshake->opencl_ctx.out_plane_size[0] = (out->linesize[0] * out->height);
146  deshake->opencl_ctx.out_plane_size[1] = (out->linesize[1] * chroma_height);
147  deshake->opencl_ctx.out_plane_size[2] = (out->linesize[2] * chroma_height);
148  deshake->opencl_ctx.cl_inbuf_size = deshake->opencl_ctx.in_plane_size[0] +
149  deshake->opencl_ctx.in_plane_size[1] +
150  deshake->opencl_ctx.in_plane_size[2];
151  deshake->opencl_ctx.cl_outbuf_size = deshake->opencl_ctx.out_plane_size[0] +
152  deshake->opencl_ctx.out_plane_size[1] +
153  deshake->opencl_ctx.out_plane_size[2];
154  if (!deshake->opencl_ctx.cl_inbuf) {
155  ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_inbuf,
156  deshake->opencl_ctx.cl_inbuf_size,
157  CL_MEM_READ_ONLY, NULL);
158  if (ret < 0)
159  return ret;
160  }
161  if (!deshake->opencl_ctx.cl_outbuf) {
162  ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_outbuf,
163  deshake->opencl_ctx.cl_outbuf_size,
164  CL_MEM_READ_WRITE, NULL);
165  if (ret < 0)
166  return ret;
167  }
168  }
169  ret = av_opencl_buffer_write_image(deshake->opencl_ctx.cl_inbuf,
170  deshake->opencl_ctx.cl_inbuf_size,
171  0, in->data,deshake->opencl_ctx.in_plane_size,
172  deshake->opencl_ctx.plane_num);
173  if(ret < 0)
174  return ret;
175  return ret;
176 }
#define AVERROR_EXTERNAL
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
void * priv
private data for use by the filter
Definition: avfilter.h:648
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
const char * av_opencl_errstr(cl_int status)
Get OpenCL error string.
Definition: opencl.c:166
#define PLANE_NUM
InterpolateMethod
Definition: transform.h:39
uint8_t
#define FF_CEIL_RSHIFT(a, b)
Definition: avcodec.h:916
static void interpolate(float *out, float v1, float v2, int size)
Definition: twinvq.c:84
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: avcodec.h:4147
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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_dlog(ac->avr,"%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
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:77
FillMethod
Definition: transform.h:51
ret
Definition: avfilter.c:961
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:660
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:730
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1938
int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
static int width
Definition: utils.c:158
void av_opencl_release_kernel(AVOpenCLKernelEnv *env)
Release kernel object.
Definition: opencl.c:433
int av_opencl_create_kernel(AVOpenCLKernelEnv *env, const char *kernel_name)
Create kernel object in the specified kernel environment.
Definition: opencl.c:391
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:635
cl_kernel kernel
void av_opencl_buffer_release(cl_mem *cl_buf)
Release OpenCL buffer.
Definition: opencl.c:671
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
int ff_opencl_set_parameter(FFOpenclParam *opencl_param,...)
#define MATRIX_SIZE
int ff_opencl_deshake_init(AVFilterContext *ctx)
int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
Write OpenCL buffer with data from src_buf.
Definition: opencl.c:684
void av_opencl_uninit(void)
Release OpenCL environment.
Definition: opencl.c:617
#define FF_OPENCL_PARAM_INFO(a)
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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_dlog(ac->avr,"%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
int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env)
Initialize the run time OpenCL environment and compile the kernel code registered with av_opencl_regi...
Definition: opencl.c:588
#define AVERROR(e)
An instance of a filter.
Definition: avfilter.h:627
int height
Definition: frame.h:145
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
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:771