1 /*
2 * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #include "libavutil/hwcontext.h"
24 #include "libavutil/hwcontext_cuda_internal.h"
25 #include "libavutil/cuda_check.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28
29 #include "avfilter.h"
30 #include "internal.h"
31
32 #include "cuda/load_helper.h"
33
34 #define CHECK_CU(x) FF_CUDA_CHECK_DL(ctx, s->hwctx->internal->cuda_dl, x)
35
36 #define HIST_SIZE (3*256)
37 #define DIV_UP(a, b) ( ((a) + (b) - 1) / (b) )
38 #define BLOCKX 32
39 #define BLOCKY 16
40
41 static const enum AVPixelFormat supported_formats[] = {
42 AV_PIX_FMT_NV12,
43 AV_PIX_FMT_YUV420P,
44 AV_PIX_FMT_YUV444P,
45 AV_PIX_FMT_P010,
46 AV_PIX_FMT_P016,
47 AV_PIX_FMT_YUV444P16,
48 };
49
50 struct thumb_frame {
51 AVFrame *buf; ///< cached frame
52 int histogram[HIST_SIZE]; ///< RGB color distribution histogram of the frame
53 };
54
55 typedef struct ThumbnailCudaContext {
56 const AVClass *class;
57 int n; ///< current frame
58 int n_frames; ///< number of frames for analysis
59 struct thumb_frame *frames; ///< the n_frames frames
60 AVRational tb; ///< copy of the input timebase to ease access
61
62 AVBufferRef *hw_frames_ctx;
63 AVCUDADeviceContext *hwctx;
64
65 CUmodule cu_module;
66
67 CUfunction cu_func_uchar;
68 CUfunction cu_func_uchar2;
69 CUfunction cu_func_ushort;
70 CUfunction cu_func_ushort2;
71 CUstream cu_stream;
72
73 CUdeviceptr data;
74
75 } ThumbnailCudaContext;
76
77 #define OFFSET(x) offsetof(ThumbnailCudaContext, x)
78 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
79
80 static const AVOption thumbnail_cuda_options[] = {
81 { "n", "set the frames batch size", OFFSET(n_frames), AV_OPT_TYPE_INT, {.i64=100}, 2, INT_MAX, FLAGS },
82 { NULL }
83 };
84
85 AVFILTER_DEFINE_CLASS(thumbnail_cuda);
86
init(AVFilterContext * ctx)87 static av_cold int init(AVFilterContext *ctx)
88 {
89 ThumbnailCudaContext *s = ctx->priv;
90
91 s->frames = av_calloc(s->n_frames, sizeof(*s->frames));
92 if (!s->frames) {
93 av_log(ctx, AV_LOG_ERROR,
94 "Allocation failure, try to lower the number of frames\n");
95 return AVERROR(ENOMEM);
96 }
97 av_log(ctx, AV_LOG_VERBOSE, "batch size: %d frames\n", s->n_frames);
98 return 0;
99 }
100
101 /**
102 * @brief Compute Sum-square deviation to estimate "closeness".
103 * @param hist color distribution histogram
104 * @param median average color distribution histogram
105 * @return sum of squared errors
106 */
frame_sum_square_err(const int * hist,const double * median)107 static double frame_sum_square_err(const int *hist, const double *median)
108 {
109 int i;
110 double err, sum_sq_err = 0;
111
112 for (i = 0; i < HIST_SIZE; i++) {
113 err = median[i] - (double)hist[i];
114 sum_sq_err += err*err;
115 }
116 return sum_sq_err;
117 }
118
get_best_frame(AVFilterContext * ctx)119 static AVFrame *get_best_frame(AVFilterContext *ctx)
120 {
121 AVFrame *picref;
122 ThumbnailCudaContext *s = ctx->priv;
123 int i, j, best_frame_idx = 0;
124 int nb_frames = s->n;
125 double avg_hist[HIST_SIZE] = {0}, sq_err, min_sq_err = -1;
126
127 // average histogram of the N frames
128 for (j = 0; j < FF_ARRAY_ELEMS(avg_hist); j++) {
129 for (i = 0; i < nb_frames; i++)
130 avg_hist[j] += (double)s->frames[i].histogram[j];
131 avg_hist[j] /= nb_frames;
132 }
133
134 // find the frame closer to the average using the sum of squared errors
135 for (i = 0; i < nb_frames; i++) {
136 sq_err = frame_sum_square_err(s->frames[i].histogram, avg_hist);
137 if (i == 0 || sq_err < min_sq_err)
138 best_frame_idx = i, min_sq_err = sq_err;
139 }
140
141 // free and reset everything (except the best frame buffer)
142 for (i = 0; i < nb_frames; i++) {
143 memset(s->frames[i].histogram, 0, sizeof(s->frames[i].histogram));
144 if (i != best_frame_idx)
145 av_frame_free(&s->frames[i].buf);
146 }
147 s->n = 0;
148
149 // raise the chosen one
150 picref = s->frames[best_frame_idx].buf;
151 av_log(ctx, AV_LOG_INFO, "frame id #%d (pts_time=%f) selected "
152 "from a set of %d images\n", best_frame_idx,
153 picref->pts * av_q2d(s->tb), nb_frames);
154 s->frames[best_frame_idx].buf = NULL;
155
156 return picref;
157 }
158
thumbnail_kernel(AVFilterContext * ctx,CUfunction func,int channels,int * histogram,uint8_t * src_dptr,int src_width,int src_height,int src_pitch,int pixel_size)159 static int thumbnail_kernel(AVFilterContext *ctx, CUfunction func, int channels,
160 int *histogram, uint8_t *src_dptr, int src_width, int src_height, int src_pitch, int pixel_size)
161 {
162 int ret;
163 ThumbnailCudaContext *s = ctx->priv;
164 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
165 CUtexObject tex = 0;
166 void *args[] = { &tex, &histogram, &src_width, &src_height };
167
168 CUDA_TEXTURE_DESC tex_desc = {
169 .filterMode = CU_TR_FILTER_MODE_LINEAR,
170 .flags = CU_TRSF_READ_AS_INTEGER,
171 };
172
173 CUDA_RESOURCE_DESC res_desc = {
174 .resType = CU_RESOURCE_TYPE_PITCH2D,
175 .res.pitch2D.format = pixel_size == 1 ?
176 CU_AD_FORMAT_UNSIGNED_INT8 :
177 CU_AD_FORMAT_UNSIGNED_INT16,
178 .res.pitch2D.numChannels = channels,
179 .res.pitch2D.width = src_width,
180 .res.pitch2D.height = src_height,
181 .res.pitch2D.pitchInBytes = src_pitch,
182 .res.pitch2D.devPtr = (CUdeviceptr)src_dptr,
183 };
184
185 ret = CHECK_CU(cu->cuTexObjectCreate(&tex, &res_desc, &tex_desc, NULL));
186 if (ret < 0)
187 goto exit;
188
189 ret = CHECK_CU(cu->cuLaunchKernel(func,
190 DIV_UP(src_width, BLOCKX), DIV_UP(src_height, BLOCKY), 1,
191 BLOCKX, BLOCKY, 1, 0, s->cu_stream, args, NULL));
192 exit:
193 if (tex)
194 CHECK_CU(cu->cuTexObjectDestroy(tex));
195
196 return ret;
197 }
198
thumbnail(AVFilterContext * ctx,int * histogram,AVFrame * in)199 static int thumbnail(AVFilterContext *ctx, int *histogram, AVFrame *in)
200 {
201 AVHWFramesContext *in_frames_ctx = (AVHWFramesContext*)in->hw_frames_ctx->data;
202 ThumbnailCudaContext *s = ctx->priv;
203
204 switch (in_frames_ctx->sw_format) {
205 case AV_PIX_FMT_NV12:
206 thumbnail_kernel(ctx, s->cu_func_uchar, 1,
207 histogram, in->data[0], in->width, in->height, in->linesize[0], 1);
208 thumbnail_kernel(ctx, s->cu_func_uchar2, 2,
209 histogram + 256, in->data[1], in->width / 2, in->height / 2, in->linesize[1], 1);
210 break;
211 case AV_PIX_FMT_YUV420P:
212 thumbnail_kernel(ctx, s->cu_func_uchar, 1,
213 histogram, in->data[0], in->width, in->height, in->linesize[0], 1);
214 thumbnail_kernel(ctx, s->cu_func_uchar, 1,
215 histogram + 256, in->data[1], in->width / 2, in->height / 2, in->linesize[1], 1);
216 thumbnail_kernel(ctx, s->cu_func_uchar, 1,
217 histogram + 512, in->data[2], in->width / 2, in->height / 2, in->linesize[2], 1);
218 break;
219 case AV_PIX_FMT_YUV444P:
220 thumbnail_kernel(ctx, s->cu_func_uchar, 1,
221 histogram, in->data[0], in->width, in->height, in->linesize[0], 1);
222 thumbnail_kernel(ctx, s->cu_func_uchar, 1,
223 histogram + 256, in->data[1], in->width, in->height, in->linesize[1], 1);
224 thumbnail_kernel(ctx, s->cu_func_uchar, 1,
225 histogram + 512, in->data[2], in->width, in->height, in->linesize[2], 1);
226 break;
227 case AV_PIX_FMT_P010LE:
228 case AV_PIX_FMT_P016LE:
229 thumbnail_kernel(ctx, s->cu_func_ushort, 1,
230 histogram, in->data[0], in->width, in->height, in->linesize[0], 2);
231 thumbnail_kernel(ctx, s->cu_func_ushort2, 2,
232 histogram + 256, in->data[1], in->width / 2, in->height / 2, in->linesize[1], 2);
233 break;
234 case AV_PIX_FMT_YUV444P16:
235 thumbnail_kernel(ctx, s->cu_func_ushort2, 1,
236 histogram, in->data[0], in->width, in->height, in->linesize[0], 2);
237 thumbnail_kernel(ctx, s->cu_func_ushort2, 1,
238 histogram + 256, in->data[1], in->width, in->height, in->linesize[1], 2);
239 thumbnail_kernel(ctx, s->cu_func_ushort2, 1,
240 histogram + 512, in->data[2], in->width, in->height, in->linesize[2], 2);
241 break;
242 default:
243 return AVERROR_BUG;
244 }
245
246 return 0;
247 }
248
filter_frame(AVFilterLink * inlink,AVFrame * frame)249 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
250 {
251 AVFilterContext *ctx = inlink->dst;
252 ThumbnailCudaContext *s = ctx->priv;
253 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
254 AVFilterLink *outlink = ctx->outputs[0];
255 int *hist = s->frames[s->n].histogram;
256 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)s->hw_frames_ctx->data;
257 CUcontext dummy;
258 CUDA_MEMCPY2D cpy = { 0 };
259 int ret = 0;
260
261 // keep a reference of each frame
262 s->frames[s->n].buf = frame;
263
264 ret = CHECK_CU(cu->cuCtxPushCurrent(s->hwctx->cuda_ctx));
265 if (ret < 0)
266 return ret;
267
268 CHECK_CU(cu->cuMemsetD8Async(s->data, 0, HIST_SIZE * sizeof(int), s->cu_stream));
269
270 thumbnail(ctx, (int*)s->data, frame);
271
272 cpy.srcMemoryType = CU_MEMORYTYPE_DEVICE;
273 cpy.dstMemoryType = CU_MEMORYTYPE_HOST;
274 cpy.srcDevice = s->data;
275 cpy.dstHost = hist;
276 cpy.srcPitch = HIST_SIZE * sizeof(int);
277 cpy.dstPitch = HIST_SIZE * sizeof(int);
278 cpy.WidthInBytes = HIST_SIZE * sizeof(int);
279 cpy.Height = 1;
280
281 ret = CHECK_CU(cu->cuMemcpy2DAsync(&cpy, s->cu_stream));
282 if (ret < 0)
283 return ret;
284
285 if (hw_frames_ctx->sw_format == AV_PIX_FMT_NV12 || hw_frames_ctx->sw_format == AV_PIX_FMT_YUV420P ||
286 hw_frames_ctx->sw_format == AV_PIX_FMT_P010LE || hw_frames_ctx->sw_format == AV_PIX_FMT_P016LE)
287 {
288 int i;
289 for (i = 256; i < HIST_SIZE; i++)
290 hist[i] = 4 * hist[i];
291 }
292
293 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
294 if (ret < 0)
295 return ret;
296
297 // no selection until the buffer of N frames is filled up
298 s->n++;
299 if (s->n < s->n_frames)
300 return 0;
301
302 return ff_filter_frame(outlink, get_best_frame(ctx));
303 }
304
uninit(AVFilterContext * ctx)305 static av_cold void uninit(AVFilterContext *ctx)
306 {
307 ThumbnailCudaContext *s = ctx->priv;
308
309 if (s->hwctx) {
310 CudaFunctions *cu = s->hwctx->internal->cuda_dl;
311
312 if (s->data) {
313 CHECK_CU(cu->cuMemFree(s->data));
314 s->data = 0;
315 }
316
317 if (s->cu_module) {
318 CHECK_CU(cu->cuModuleUnload(s->cu_module));
319 s->cu_module = NULL;
320 }
321 }
322
323 if (s->frames) {
324 for (int i = 0; i < s->n_frames && s->frames[i].buf; i++)
325 av_frame_free(&s->frames[i].buf);
326 av_freep(&s->frames);
327 }
328 }
329
request_frame(AVFilterLink * link)330 static int request_frame(AVFilterLink *link)
331 {
332 AVFilterContext *ctx = link->src;
333 ThumbnailCudaContext *s = ctx->priv;
334 int ret = ff_request_frame(ctx->inputs[0]);
335
336 if (ret == AVERROR_EOF && s->n) {
337 ret = ff_filter_frame(link, get_best_frame(ctx));
338 if (ret < 0)
339 return ret;
340 ret = AVERROR_EOF;
341 }
342 if (ret < 0)
343 return ret;
344 return 0;
345 }
346
format_is_supported(enum AVPixelFormat fmt)347 static int format_is_supported(enum AVPixelFormat fmt)
348 {
349 int i;
350
351 for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
352 if (supported_formats[i] == fmt)
353 return 1;
354 return 0;
355 }
356
config_props(AVFilterLink * inlink)357 static int config_props(AVFilterLink *inlink)
358 {
359 AVFilterContext *ctx = inlink->dst;
360 ThumbnailCudaContext *s = ctx->priv;
361 AVHWFramesContext *hw_frames_ctx = (AVHWFramesContext*)inlink->hw_frames_ctx->data;
362 AVCUDADeviceContext *device_hwctx = hw_frames_ctx->device_ctx->hwctx;
363 CUcontext dummy, cuda_ctx = device_hwctx->cuda_ctx;
364 CudaFunctions *cu = device_hwctx->internal->cuda_dl;
365 int ret;
366
367 extern const unsigned char ff_vf_thumbnail_cuda_ptx_data[];
368 extern const unsigned int ff_vf_thumbnail_cuda_ptx_len;
369
370 s->hwctx = device_hwctx;
371 s->cu_stream = s->hwctx->stream;
372
373 ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_ctx));
374 if (ret < 0)
375 return ret;
376
377 ret = ff_cuda_load_module(ctx, device_hwctx, &s->cu_module, ff_vf_thumbnail_cuda_ptx_data, ff_vf_thumbnail_cuda_ptx_len);
378 if (ret < 0)
379 return ret;
380
381 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar, s->cu_module, "Thumbnail_uchar"));
382 if (ret < 0)
383 return ret;
384
385 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_uchar2, s->cu_module, "Thumbnail_uchar2"));
386 if (ret < 0)
387 return ret;
388
389 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort, s->cu_module, "Thumbnail_ushort"));
390 if (ret < 0)
391 return ret;
392
393 ret = CHECK_CU(cu->cuModuleGetFunction(&s->cu_func_ushort2, s->cu_module, "Thumbnail_ushort2"));
394 if (ret < 0)
395 return ret;
396
397 ret = CHECK_CU(cu->cuMemAlloc(&s->data, HIST_SIZE * sizeof(int)));
398 if (ret < 0)
399 return ret;
400
401 CHECK_CU(cu->cuCtxPopCurrent(&dummy));
402
403 s->hw_frames_ctx = ctx->inputs[0]->hw_frames_ctx;
404
405 ctx->outputs[0]->hw_frames_ctx = av_buffer_ref(s->hw_frames_ctx);
406 if (!ctx->outputs[0]->hw_frames_ctx)
407 return AVERROR(ENOMEM);
408
409 s->tb = inlink->time_base;
410
411 if (!format_is_supported(hw_frames_ctx->sw_format)) {
412 av_log(ctx, AV_LOG_ERROR, "Unsupported input format: %s\n", av_get_pix_fmt_name(hw_frames_ctx->sw_format));
413 return AVERROR(ENOSYS);
414 }
415
416 return 0;
417 }
418
419 static const AVFilterPad thumbnail_cuda_inputs[] = {
420 {
421 .name = "default",
422 .type = AVMEDIA_TYPE_VIDEO,
423 .config_props = config_props,
424 .filter_frame = filter_frame,
425 },
426 };
427
428 static const AVFilterPad thumbnail_cuda_outputs[] = {
429 {
430 .name = "default",
431 .type = AVMEDIA_TYPE_VIDEO,
432 .request_frame = request_frame,
433 },
434 };
435
436 const AVFilter ff_vf_thumbnail_cuda = {
437 .name = "thumbnail_cuda",
438 .description = NULL_IF_CONFIG_SMALL("Select the most representative frame in a given sequence of consecutive frames."),
439 .priv_size = sizeof(ThumbnailCudaContext),
440 .init = init,
441 .uninit = uninit,
442 FILTER_INPUTS(thumbnail_cuda_inputs),
443 FILTER_OUTPUTS(thumbnail_cuda_outputs),
444 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_CUDA),
445 .priv_class = &thumbnail_cuda_class,
446 .flags_internal = FF_FILTER_FLAG_HWFRAME_AWARE,
447 };
448