Browse code

Merge commit '98114d70e48caf871b0fe9b8e5bf8ebd989b845d'

* commit '98114d70e48caf871b0fe9b8e5bf8ebd989b845d':
lavf: VAAPI scale filter

Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>

Derek Buitenhuis authored on 2016/04/18 23:22:24
Showing 6 changed files
... ...
@@ -26,6 +26,7 @@ version <next>:
26 26
 - colorspace filter
27 27
 - hdcd filter
28 28
 - readvitc filter
29
+- VAAPI-accelerated format conversion and scaling
29 30
 
30 31
 version 3.0:
31 32
 - Common Encryption (CENC) MP4 encoding and decoding support
... ...
@@ -3002,6 +3002,7 @@ vidstabtransform_filter_deps="libvidstab"
3002 3002
 zmq_filter_deps="libzmq"
3003 3003
 zoompan_filter_deps="swscale"
3004 3004
 zscale_filter_deps="libzimg"
3005
+scale_vaapi_filter_deps="vaapi VAProcPipelineParameterBuffer"
3005 3006
 
3006 3007
 # examples
3007 3008
 avcodec_example_deps="avcodec avutil"
... ...
@@ -5455,6 +5456,7 @@ check_type "d3d9.h dxva2api.h" DXVA2_ConfigPictureDecode -D_WIN32_WINNT=0x0602
5455 5455
 
5456 5456
 check_type "va/va.h" "VAPictureParameterBufferHEVC"
5457 5457
 check_type "va/va.h" "VADecPictureParameterBufferVP9"
5458
+check_type "va/va.h va/va_vpp.h" "VAProcPipelineParameterBuffer"
5458 5459
 
5459 5460
 check_type "vdpau/vdpau.h" "VdpPictureInfoHEVC"
5460 5461
 
... ...
@@ -242,6 +242,7 @@ OBJS-$(CONFIG_SCALE2REF_FILTER)              += vf_scale.o
242 242
 OBJS-$(CONFIG_SELECT_FILTER)                 += f_select.o
243 243
 OBJS-$(CONFIG_SELECTIVECOLOR_FILTER)         += vf_selectivecolor.o
244 244
 OBJS-$(CONFIG_SENDCMD_FILTER)                += f_sendcmd.o
245
+OBJS-$(CONFIG_SCALE_VAAPI_FILTER)            += vf_scale_vaapi.o
245 246
 OBJS-$(CONFIG_SETDAR_FILTER)                 += vf_aspect.o
246 247
 OBJS-$(CONFIG_SETFIELD_FILTER)               += vf_setfield.o
247 248
 OBJS-$(CONFIG_SETPTS_FILTER)                 += setpts.o
... ...
@@ -258,6 +258,7 @@ void avfilter_register_all(void)
258 258
     REGISTER_FILTER(SAB,            sab,            vf);
259 259
     REGISTER_FILTER(SCALE,          scale,          vf);
260 260
     REGISTER_FILTER(SCALE2REF,      scale2ref,      vf);
261
+    REGISTER_FILTER(SCALE_VAAPI,    scale_vaapi,    vf);
261 262
     REGISTER_FILTER(SELECT,         select,         vf);
262 263
     REGISTER_FILTER(SELECTIVECOLOR, selectivecolor, vf);
263 264
     REGISTER_FILTER(SENDCMD,        sendcmd,        vf);
... ...
@@ -31,7 +31,7 @@
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33 33
 #define LIBAVFILTER_VERSION_MINOR  43
34
-#define LIBAVFILTER_VERSION_MICRO 100
34
+#define LIBAVFILTER_VERSION_MICRO 101
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
                                                LIBAVFILTER_VERSION_MINOR, \
38 38
new file mode 100644
... ...
@@ -0,0 +1,460 @@
0
+/*
1
+ * This file is part of FFmpeg.
2
+ *
3
+ * FFmpeg is free software; you can redistribute it and/or
4
+ * modify it under the terms of the GNU Lesser General Public
5
+ * License as published by the Free Software Foundation; either
6
+ * version 2.1 of the License, or (at your option) any later version.
7
+ *
8
+ * FFmpeg is distributed in the hope that it will be useful,
9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
+ * Lesser General Public License for more details.
12
+ *
13
+ * You should have received a copy of the GNU Lesser General Public
14
+ * License along with FFmpeg; if not, write to the Free Software
15
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+ */
17
+
18
+#include <string.h>
19
+
20
+#include <va/va.h>
21
+#include <va/va_vpp.h>
22
+
23
+#include "libavutil/avassert.h"
24
+#include "libavutil/hwcontext.h"
25
+#include "libavutil/hwcontext_vaapi.h"
26
+#include "libavutil/mem.h"
27
+#include "libavutil/opt.h"
28
+#include "libavutil/pixdesc.h"
29
+
30
+#include "avfilter.h"
31
+#include "formats.h"
32
+#include "internal.h"
33
+
34
+typedef struct ScaleVAAPIContext {
35
+    const AVClass *class;
36
+
37
+    AVVAAPIDeviceContext *hwctx;
38
+    AVBufferRef *device_ref;
39
+
40
+    int valid_ids;
41
+    VAConfigID  va_config;
42
+    VAContextID va_context;
43
+
44
+    AVBufferRef       *input_frames_ref;
45
+    AVHWFramesContext *input_frames;
46
+
47
+    AVBufferRef       *output_frames_ref;
48
+    AVHWFramesContext *output_frames;
49
+
50
+    char *output_format_string;
51
+    enum AVPixelFormat output_format;
52
+    int output_width;
53
+    int output_height;
54
+
55
+} ScaleVAAPIContext;
56
+
57
+
58
+static int scale_vaapi_query_formats(AVFilterContext *avctx)
59
+{
60
+    enum AVPixelFormat pix_fmts[] = {
61
+        AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
62
+    };
63
+
64
+    ff_formats_ref(ff_make_format_list(pix_fmts),
65
+                   &avctx->inputs[0]->out_formats);
66
+    ff_formats_ref(ff_make_format_list(pix_fmts),
67
+                   &avctx->outputs[0]->in_formats);
68
+
69
+    return 0;
70
+}
71
+
72
+static int scale_vaapi_pipeline_uninit(ScaleVAAPIContext *ctx)
73
+{
74
+    if (ctx->va_context != VA_INVALID_ID) {
75
+        vaDestroyContext(ctx->hwctx->display, ctx->va_context);
76
+        ctx->va_context = VA_INVALID_ID;
77
+    }
78
+
79
+    if (ctx->va_config != VA_INVALID_ID) {
80
+        vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
81
+        ctx->va_config = VA_INVALID_ID;
82
+    }
83
+
84
+    av_buffer_unref(&ctx->output_frames_ref);
85
+    av_buffer_unref(&ctx->device_ref);
86
+    ctx->hwctx = 0;
87
+
88
+    return 0;
89
+}
90
+
91
+static int scale_vaapi_config_input(AVFilterLink *inlink)
92
+{
93
+    AVFilterContext *avctx = inlink->dst;
94
+    ScaleVAAPIContext *ctx = avctx->priv;
95
+
96
+    scale_vaapi_pipeline_uninit(ctx);
97
+
98
+    if (!inlink->hw_frames_ctx) {
99
+        av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
100
+               "required to associate the processing device.\n");
101
+        return AVERROR(EINVAL);
102
+    }
103
+
104
+    ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
105
+    ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
106
+
107
+    return 0;
108
+}
109
+
110
+static int scale_vaapi_config_output(AVFilterLink *outlink)
111
+{
112
+    AVFilterContext *avctx = outlink->src;
113
+    ScaleVAAPIContext *ctx = avctx->priv;
114
+    AVVAAPIHWConfig *hwconfig = NULL;
115
+    AVHWFramesConstraints *constraints = NULL;
116
+    AVVAAPIFramesContext *va_frames;
117
+    VAStatus vas;
118
+    int err, i;
119
+
120
+    scale_vaapi_pipeline_uninit(ctx);
121
+
122
+    ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
123
+    ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
124
+
125
+    av_assert0(ctx->va_config == VA_INVALID_ID);
126
+    vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
127
+                         VAEntrypointVideoProc, 0, 0, &ctx->va_config);
128
+    if (vas != VA_STATUS_SUCCESS) {
129
+        av_log(ctx, AV_LOG_ERROR, "Failed to create processing pipeline "
130
+               "config: %d (%s).\n", vas, vaErrorStr(vas));
131
+        err = AVERROR(EIO);
132
+        goto fail;
133
+    }
134
+
135
+    hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
136
+    if (!hwconfig) {
137
+        err = AVERROR(ENOMEM);
138
+        goto fail;
139
+    }
140
+    hwconfig->config_id = ctx->va_config;
141
+
142
+    constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
143
+                                                      hwconfig);
144
+    if (!constraints) {
145
+        err = AVERROR(ENOMEM);
146
+        goto fail;
147
+    }
148
+
149
+    if (ctx->output_format == AV_PIX_FMT_NONE)
150
+        ctx->output_format = ctx->input_frames->sw_format;
151
+    if (constraints->valid_sw_formats) {
152
+        for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
153
+            if (ctx->output_format == constraints->valid_sw_formats[i])
154
+                break;
155
+        }
156
+        if (constraints->valid_sw_formats[i] == AV_PIX_FMT_NONE) {
157
+            av_log(ctx, AV_LOG_ERROR, "Hardware does not support output "
158
+                   "format %s.\n", av_get_pix_fmt_name(ctx->output_format));
159
+            err = AVERROR(EINVAL);
160
+            goto fail;
161
+        }
162
+    }
163
+
164
+    if (ctx->output_width  < constraints->min_width  ||
165
+        ctx->output_height < constraints->min_height ||
166
+        ctx->output_width  > constraints->max_width  ||
167
+        ctx->output_height > constraints->max_height) {
168
+        av_log(ctx, AV_LOG_ERROR, "Hardware does not support scaling to "
169
+               "size %dx%d (constraints: width %d-%d height %d-%d).\n",
170
+               ctx->output_width, ctx->output_height,
171
+               constraints->min_width,  constraints->max_width,
172
+               constraints->min_height, constraints->max_height);
173
+        err = AVERROR(EINVAL);
174
+        goto fail;
175
+    }
176
+
177
+    ctx->output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
178
+    if (!ctx->output_frames_ref) {
179
+        av_log(ctx, AV_LOG_ERROR, "Failed to create HW frame context "
180
+               "for output.\n");
181
+        err = AVERROR(ENOMEM);
182
+        goto fail;
183
+    }
184
+
185
+    ctx->output_frames = (AVHWFramesContext*)ctx->output_frames_ref->data;
186
+
187
+    ctx->output_frames->format    = AV_PIX_FMT_VAAPI;
188
+    ctx->output_frames->sw_format = ctx->output_format;
189
+    ctx->output_frames->width     = ctx->output_width;
190
+    ctx->output_frames->height    = ctx->output_height;
191
+
192
+    // The number of output frames we need is determined by what follows
193
+    // the filter.  If it's an encoder with complex frame reference
194
+    // structures then this could be very high.
195
+    ctx->output_frames->initial_pool_size = 10;
196
+
197
+    err = av_hwframe_ctx_init(ctx->output_frames_ref);
198
+    if (err < 0) {
199
+        av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
200
+               "context for output: %d\n", err);
201
+        goto fail;
202
+    }
203
+
204
+    va_frames = ctx->output_frames->hwctx;
205
+
206
+    av_assert0(ctx->va_context == VA_INVALID_ID);
207
+    vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
208
+                          ctx->output_width, ctx->output_height,
209
+                          VA_PROGRESSIVE,
210
+                          va_frames->surface_ids, va_frames->nb_surfaces,
211
+                          &ctx->va_context);
212
+    if (vas != VA_STATUS_SUCCESS) {
213
+        av_log(ctx, AV_LOG_ERROR, "Failed to create processing pipeline "
214
+               "context: %d (%s).\n", vas, vaErrorStr(vas));
215
+        return AVERROR(EIO);
216
+    }
217
+
218
+    outlink->w = ctx->output_width;
219
+    outlink->h = ctx->output_height;
220
+
221
+    outlink->hw_frames_ctx = av_buffer_ref(ctx->output_frames_ref);
222
+    if (!outlink->hw_frames_ctx) {
223
+        err = AVERROR(ENOMEM);
224
+        goto fail;
225
+    }
226
+
227
+    av_freep(&hwconfig);
228
+    av_hwframe_constraints_free(&constraints);
229
+    return 0;
230
+
231
+fail:
232
+    av_buffer_unref(&ctx->output_frames_ref);
233
+    av_freep(&hwconfig);
234
+    av_hwframe_constraints_free(&constraints);
235
+    return err;
236
+}
237
+
238
+static int vaapi_proc_colour_standard(enum AVColorSpace av_cs)
239
+{
240
+    switch(av_cs) {
241
+#define CS(av, va) case AVCOL_SPC_ ## av: return VAProcColorStandard ## va;
242
+        CS(BT709,     BT709);
243
+        CS(BT470BG,   BT601);
244
+        CS(SMPTE170M, SMPTE170M);
245
+        CS(SMPTE240M, SMPTE240M);
246
+#undef CS
247
+    default:
248
+        return VAProcColorStandardNone;
249
+    }
250
+}
251
+
252
+static int scale_vaapi_filter_frame(AVFilterLink *inlink, AVFrame *input_frame)
253
+{
254
+    AVFilterContext *avctx = inlink->dst;
255
+    AVFilterLink *outlink = avctx->outputs[0];
256
+    ScaleVAAPIContext *ctx = avctx->priv;
257
+    AVFrame *output_frame = NULL;
258
+    VASurfaceID input_surface, output_surface;
259
+    VAProcPipelineParameterBuffer params;
260
+    VABufferID params_id;
261
+    VAStatus vas;
262
+    int err;
263
+
264
+    av_log(ctx, AV_LOG_DEBUG, "Filter input: %s, %ux%u (%"PRId64").\n",
265
+           av_get_pix_fmt_name(input_frame->format),
266
+           input_frame->width, input_frame->height, input_frame->pts);
267
+
268
+    if (ctx->va_context == VA_INVALID_ID)
269
+        return AVERROR(EINVAL);
270
+
271
+    input_surface = (VASurfaceID)(uintptr_t)input_frame->data[3];
272
+    av_log(ctx, AV_LOG_DEBUG, "Using surface %#x for scale input.\n",
273
+           input_surface);
274
+
275
+    output_frame = av_frame_alloc();
276
+    if (!output_frame) {
277
+        av_log(ctx, AV_LOG_ERROR, "Failed to allocate output frame.");
278
+        err = AVERROR(ENOMEM);
279
+        goto fail;
280
+    }
281
+
282
+    err = av_hwframe_get_buffer(ctx->output_frames_ref, output_frame, 0);
283
+    if (err < 0) {
284
+        av_log(ctx, AV_LOG_ERROR, "Failed to get surface for "
285
+               "output: %d\n.", err);
286
+    }
287
+
288
+    output_surface = (VASurfaceID)(uintptr_t)output_frame->data[3];
289
+    av_log(ctx, AV_LOG_DEBUG, "Using surface %#x for scale output.\n",
290
+           output_surface);
291
+
292
+    memset(&params, 0, sizeof(params));
293
+
294
+    params.surface = input_surface;
295
+    params.surface_region = 0;
296
+    params.surface_color_standard =
297
+        vaapi_proc_colour_standard(input_frame->colorspace);
298
+
299
+    params.output_region = 0;
300
+    params.output_background_color = 0xff000000;
301
+    params.output_color_standard = params.surface_color_standard;
302
+
303
+    params.pipeline_flags = 0;
304
+    params.filter_flags = VA_FILTER_SCALING_HQ;
305
+
306
+    vas = vaBeginPicture(ctx->hwctx->display,
307
+                         ctx->va_context, output_surface);
308
+    if (vas != VA_STATUS_SUCCESS) {
309
+        av_log(ctx, AV_LOG_ERROR, "Failed to attach new picture: "
310
+               "%d (%s).\n", vas, vaErrorStr(vas));
311
+        err = AVERROR(EIO);
312
+        goto fail;
313
+    }
314
+
315
+    vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
316
+                         VAProcPipelineParameterBufferType,
317
+                         sizeof(params), 1, &params, &params_id);
318
+    if (vas != VA_STATUS_SUCCESS) {
319
+        av_log(ctx, AV_LOG_ERROR, "Failed to create parameter buffer: "
320
+               "%d (%s).\n", vas, vaErrorStr(vas));
321
+        err = AVERROR(EIO);
322
+        goto fail_after_begin;
323
+    }
324
+    av_log(ctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n",
325
+           params_id);
326
+
327
+    vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
328
+                          &params_id, 1);
329
+    if (vas != VA_STATUS_SUCCESS) {
330
+        av_log(ctx, AV_LOG_ERROR, "Failed to render parameter buffer: "
331
+               "%d (%s).\n", vas, vaErrorStr(vas));
332
+        err = AVERROR(EIO);
333
+        goto fail_after_begin;
334
+    }
335
+
336
+    vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
337
+    if (vas != VA_STATUS_SUCCESS) {
338
+        av_log(ctx, AV_LOG_ERROR, "Failed to start picture processing: "
339
+               "%d (%s).\n", vas, vaErrorStr(vas));
340
+        err = AVERROR(EIO);
341
+        goto fail_after_render;
342
+    }
343
+
344
+    // This doesn't get freed automatically for some reason.
345
+    vas = vaDestroyBuffer(ctx->hwctx->display, params_id);
346
+    if (vas != VA_STATUS_SUCCESS) {
347
+        av_log(ctx, AV_LOG_ERROR, "Failed to free parameter buffer: "
348
+               "%d (%s).\n", vas, vaErrorStr(vas));
349
+        err = AVERROR(EIO);
350
+        goto fail;
351
+    }
352
+
353
+    av_frame_copy_props(output_frame, input_frame);
354
+    av_frame_free(&input_frame);
355
+
356
+    av_log(ctx, AV_LOG_DEBUG, "Filter output: %s, %ux%u (%"PRId64").\n",
357
+           av_get_pix_fmt_name(output_frame->format),
358
+           output_frame->width, output_frame->height, output_frame->pts);
359
+
360
+    return ff_filter_frame(outlink, output_frame);
361
+
362
+    // We want to make sure that if vaBeginPicture has been called, we also
363
+    // call vaRenderPicture and vaEndPicture.  These calls may well fail or
364
+    // do something else nasty, but once we're in this failure case there
365
+    // isn't much else we can do.
366
+fail_after_begin:
367
+    vaRenderPicture(ctx->hwctx->display, ctx->va_context, &params_id, 1);
368
+fail_after_render:
369
+    vaEndPicture(ctx->hwctx->display, ctx->va_context);
370
+fail:
371
+    av_frame_free(&input_frame);
372
+    av_frame_free(&output_frame);
373
+    return err;
374
+}
375
+
376
+static av_cold int scale_vaapi_init(AVFilterContext *avctx)
377
+{
378
+    ScaleVAAPIContext *ctx = avctx->priv;
379
+
380
+    ctx->va_config  = VA_INVALID_ID;
381
+    ctx->va_context = VA_INVALID_ID;
382
+    ctx->valid_ids  = 1;
383
+
384
+    if (ctx->output_format_string) {
385
+        ctx->output_format = av_get_pix_fmt(ctx->output_format_string);
386
+        if (ctx->output_format == AV_PIX_FMT_NONE) {
387
+            av_log(ctx, AV_LOG_ERROR, "Invalid output format.\n");
388
+            return AVERROR(EINVAL);
389
+        }
390
+    } else {
391
+        // Use the input format once that is configured.
392
+        ctx->output_format = AV_PIX_FMT_NONE;
393
+    }
394
+
395
+    return 0;
396
+}
397
+
398
+static av_cold void scale_vaapi_uninit(AVFilterContext *avctx)
399
+{
400
+    ScaleVAAPIContext *ctx = avctx->priv;
401
+
402
+    if (ctx->valid_ids)
403
+        scale_vaapi_pipeline_uninit(ctx);
404
+
405
+    av_buffer_unref(&ctx->input_frames_ref);
406
+    av_buffer_unref(&ctx->output_frames_ref);
407
+    av_buffer_unref(&ctx->device_ref);
408
+}
409
+
410
+
411
+#define OFFSET(x) offsetof(ScaleVAAPIContext, x)
412
+#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM)
413
+static const AVOption scale_vaapi_options[] = {
414
+    { "w", "Output video width",
415
+      OFFSET(output_width),  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
416
+    { "h", "Output video height",
417
+      OFFSET(output_height), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, .flags = FLAGS },
418
+    { "format", "Output video format (software format of hardware frames)",
419
+      OFFSET(output_format_string), AV_OPT_TYPE_STRING, .flags = FLAGS },
420
+    { NULL },
421
+};
422
+
423
+static const AVClass scale_vaapi_class = {
424
+    .class_name = "scale_vaapi",
425
+    .item_name  = av_default_item_name,
426
+    .option     = scale_vaapi_options,
427
+    .version    = LIBAVUTIL_VERSION_INT,
428
+};
429
+
430
+static const AVFilterPad scale_vaapi_inputs[] = {
431
+    {
432
+        .name         = "default",
433
+        .type         = AVMEDIA_TYPE_VIDEO,
434
+        .filter_frame = &scale_vaapi_filter_frame,
435
+        .config_props = &scale_vaapi_config_input,
436
+    },
437
+    { NULL }
438
+};
439
+
440
+static const AVFilterPad scale_vaapi_outputs[] = {
441
+    {
442
+        .name = "default",
443
+        .type = AVMEDIA_TYPE_VIDEO,
444
+        .config_props = &scale_vaapi_config_output,
445
+    },
446
+    { NULL }
447
+};
448
+
449
+AVFilter ff_vf_scale_vaapi = {
450
+    .name          = "scale_vaapi",
451
+    .description   = NULL_IF_CONFIG_SMALL("Scale to/from VAAPI surfaces."),
452
+    .priv_size     = sizeof(ScaleVAAPIContext),
453
+    .init          = &scale_vaapi_init,
454
+    .uninit        = &scale_vaapi_uninit,
455
+    .query_formats = &scale_vaapi_query_formats,
456
+    .inputs        = scale_vaapi_inputs,
457
+    .outputs       = scale_vaapi_outputs,
458
+    .priv_class    = &scale_vaapi_class,
459
+};