]> git.sesse.net Git - ffmpeg/blob - libavfilter/vaapi_vpp.c
avdevice: migrate to AVFormatContext->url
[ffmpeg] / libavfilter / vaapi_vpp.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <string.h>
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/pixdesc.h"
23 #include "formats.h"
24
25 #include "vaapi_vpp.h"
26
27 int ff_vaapi_vpp_query_formats(AVFilterContext *avctx)
28 {
29     enum AVPixelFormat pix_fmts[] = {
30         AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE,
31     };
32     int err;
33
34     if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
35                               &avctx->inputs[0]->out_formats)) < 0)
36         return err;
37     if ((err = ff_formats_ref(ff_make_format_list(pix_fmts),
38                               &avctx->outputs[0]->in_formats)) < 0)
39         return err;
40
41     return 0;
42 }
43
44 void ff_vaapi_vpp_pipeline_uninit(AVFilterContext *avctx)
45 {
46     VAAPIVPPContext *ctx   = avctx->priv;
47     int i;
48     for (i = 0; i < ctx->nb_filter_buffers; i++) {
49         if (ctx->filter_buffers[i] != VA_INVALID_ID) {
50             vaDestroyBuffer(ctx->hwctx->display, ctx->filter_buffers[i]);
51             ctx->filter_buffers[i] = VA_INVALID_ID;
52         }
53     }
54     ctx->nb_filter_buffers = 0;
55
56     if (ctx->va_context != VA_INVALID_ID) {
57         vaDestroyContext(ctx->hwctx->display, ctx->va_context);
58         ctx->va_context = VA_INVALID_ID;
59     }
60
61     if (ctx->va_config != VA_INVALID_ID) {
62         vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
63         ctx->va_config = VA_INVALID_ID;
64     }
65
66     av_buffer_unref(&ctx->output_frames_ref);
67     av_buffer_unref(&ctx->device_ref);
68     ctx->hwctx = NULL;
69 }
70
71 int ff_vaapi_vpp_config_input(AVFilterLink *inlink)
72 {
73     AVFilterContext *avctx = inlink->dst;
74     VAAPIVPPContext *ctx   = avctx->priv;
75
76     if (ctx->pipeline_uninit)
77         ctx->pipeline_uninit(avctx);
78
79     if (!inlink->hw_frames_ctx) {
80         av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
81                "required to associate the processing device.\n");
82         return AVERROR(EINVAL);
83     }
84
85     ctx->input_frames_ref = av_buffer_ref(inlink->hw_frames_ctx);
86     if (!ctx->input_frames_ref) {
87         av_log(avctx, AV_LOG_ERROR, "A input frames reference create "
88                "failed.\n");
89         return AVERROR(ENOMEM);
90     }
91     ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
92
93     return 0;
94 }
95
96 int ff_vaapi_vpp_config_output(AVFilterLink *outlink)
97 {
98     AVFilterContext *avctx = outlink->src;
99     VAAPIVPPContext *ctx   = avctx->priv;
100     AVVAAPIHWConfig *hwconfig = NULL;
101     AVHWFramesConstraints *constraints = NULL;
102     AVVAAPIFramesContext *va_frames;
103     VAStatus vas;
104     int err, i;
105
106     if (ctx->pipeline_uninit)
107         ctx->pipeline_uninit(avctx);
108
109     if (!ctx->output_width)
110         ctx->output_width  = avctx->inputs[0]->w;
111     if (!ctx->output_height)
112         ctx->output_height = avctx->inputs[0]->h;
113
114     av_assert0(ctx->input_frames);
115     ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
116     if (!ctx->device_ref) {
117         av_log(avctx, AV_LOG_ERROR, "A device reference create "
118                "failed.\n");
119         return AVERROR(ENOMEM);
120     }
121     ctx->hwctx = ((AVHWDeviceContext*)ctx->device_ref->data)->hwctx;
122
123     av_assert0(ctx->va_config == VA_INVALID_ID);
124     vas = vaCreateConfig(ctx->hwctx->display, VAProfileNone,
125                          VAEntrypointVideoProc, NULL, 0, &ctx->va_config);
126     if (vas != VA_STATUS_SUCCESS) {
127         av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
128                "config: %d (%s).\n", vas, vaErrorStr(vas));
129         err = AVERROR(EIO);
130         goto fail;
131     }
132
133     hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
134     if (!hwconfig) {
135         err = AVERROR(ENOMEM);
136         goto fail;
137     }
138     hwconfig->config_id = ctx->va_config;
139
140     constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
141                                                       hwconfig);
142     if (!constraints) {
143         err = AVERROR(ENOMEM);
144         goto fail;
145     }
146
147     if (ctx->output_format == AV_PIX_FMT_NONE)
148         ctx->output_format = ctx->input_frames->sw_format;
149     if (constraints->valid_sw_formats) {
150         for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
151             if (ctx->output_format == constraints->valid_sw_formats[i])
152                 break;
153         }
154         if (constraints->valid_sw_formats[i] == AV_PIX_FMT_NONE) {
155             av_log(avctx, AV_LOG_ERROR, "Hardware does not support output "
156                    "format %s.\n", av_get_pix_fmt_name(ctx->output_format));
157             err = AVERROR(EINVAL);
158             goto fail;
159         }
160     }
161
162     if (ctx->output_width  < constraints->min_width  ||
163         ctx->output_height < constraints->min_height ||
164         ctx->output_width  > constraints->max_width  ||
165         ctx->output_height > constraints->max_height) {
166         av_log(avctx, AV_LOG_ERROR, "Hardware does not support scaling to "
167                "size %dx%d (constraints: width %d-%d height %d-%d).\n",
168                ctx->output_width, ctx->output_height,
169                constraints->min_width,  constraints->max_width,
170                constraints->min_height, constraints->max_height);
171         err = AVERROR(EINVAL);
172         goto fail;
173     }
174
175     ctx->output_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
176     if (!ctx->output_frames_ref) {
177         av_log(avctx, AV_LOG_ERROR, "Failed to create HW frame context "
178                "for output.\n");
179         err = AVERROR(ENOMEM);
180         goto fail;
181     }
182
183     ctx->output_frames = (AVHWFramesContext*)ctx->output_frames_ref->data;
184
185     ctx->output_frames->format    = AV_PIX_FMT_VAAPI;
186     ctx->output_frames->sw_format = ctx->output_format;
187     ctx->output_frames->width     = ctx->output_width;
188     ctx->output_frames->height    = ctx->output_height;
189
190     // The number of output frames we need is determined by what follows
191     // the filter.  If it's an encoder with complex frame reference
192     // structures then this could be very high.
193     ctx->output_frames->initial_pool_size = 10;
194
195     err = av_hwframe_ctx_init(ctx->output_frames_ref);
196     if (err < 0) {
197         av_log(avctx, AV_LOG_ERROR, "Failed to initialise VAAPI frame "
198                "context for output: %d\n", err);
199         goto fail;
200     }
201
202     va_frames = ctx->output_frames->hwctx;
203
204     av_assert0(ctx->va_context == VA_INVALID_ID);
205     vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
206                           ctx->output_width, ctx->output_height,
207                           VA_PROGRESSIVE,
208                           va_frames->surface_ids, va_frames->nb_surfaces,
209                           &ctx->va_context);
210     if (vas != VA_STATUS_SUCCESS) {
211         av_log(avctx, AV_LOG_ERROR, "Failed to create processing pipeline "
212                "context: %d (%s).\n", vas, vaErrorStr(vas));
213         return AVERROR(EIO);
214     }
215
216     outlink->w = ctx->output_width;
217     outlink->h = ctx->output_height;
218
219     if (ctx->build_filter_params) {
220         err = ctx->build_filter_params(avctx);
221         if (err < 0)
222             goto fail;
223     }
224
225     outlink->hw_frames_ctx = av_buffer_ref(ctx->output_frames_ref);
226     if (!outlink->hw_frames_ctx) {
227         err = AVERROR(ENOMEM);
228         goto fail;
229     }
230
231     av_freep(&hwconfig);
232     av_hwframe_constraints_free(&constraints);
233     return 0;
234
235 fail:
236     av_buffer_unref(&ctx->output_frames_ref);
237     av_freep(&hwconfig);
238     av_hwframe_constraints_free(&constraints);
239     return err;
240 }
241
242 int ff_vaapi_vpp_colour_standard(enum AVColorSpace av_cs)
243 {
244     switch(av_cs) {
245 #define CS(av, va) case AVCOL_SPC_ ## av: return VAProcColorStandard ## va;
246         CS(BT709,     BT709);
247         CS(BT470BG,   BT601);
248         CS(SMPTE170M, SMPTE170M);
249         CS(SMPTE240M, SMPTE240M);
250 #undef CS
251     default:
252         return VAProcColorStandardNone;
253     }
254 }
255
256 int ff_vaapi_vpp_make_param_buffers(AVFilterContext *avctx,
257                                     int type,
258                                     const void *data,
259                                     size_t size,
260                                     int count)
261 {
262     VAStatus vas;
263     VABufferID buffer;
264     VAAPIVPPContext *ctx   = avctx->priv;
265
266     av_assert0(ctx->nb_filter_buffers + 1 <= VAProcFilterCount);
267
268     vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
269                          type, size, count, (void*)data, &buffer);
270     if (vas != VA_STATUS_SUCCESS) {
271         av_log(avctx, AV_LOG_ERROR, "Failed to create parameter "
272                "buffer (type %d): %d (%s).\n",
273                type, vas, vaErrorStr(vas));
274         return AVERROR(EIO);
275     }
276
277     ctx->filter_buffers[ctx->nb_filter_buffers++] = buffer;
278
279     av_log(avctx, AV_LOG_DEBUG, "Param buffer (type %d, %zu bytes, count %d) "
280            "is %#x.\n", type, size, count, buffer);
281     return 0;
282 }
283
284
285 int ff_vaapi_vpp_render_picture(AVFilterContext *avctx,
286                                 VAProcPipelineParameterBuffer *params,
287                                 VASurfaceID output_surface)
288 {
289     VABufferID params_id;
290     VAStatus vas;
291     int err = 0;
292     VAAPIVPPContext *ctx   = avctx->priv;
293
294     vas = vaBeginPicture(ctx->hwctx->display,
295                          ctx->va_context, output_surface);
296     if (vas != VA_STATUS_SUCCESS) {
297         av_log(avctx, AV_LOG_ERROR, "Failed to attach new picture: "
298                "%d (%s).\n", vas, vaErrorStr(vas));
299         err = AVERROR(EIO);
300         goto fail;
301     }
302
303     vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
304                          VAProcPipelineParameterBufferType,
305                          sizeof(*params), 1, params, &params_id);
306     if (vas != VA_STATUS_SUCCESS) {
307         av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer: "
308                "%d (%s).\n", vas, vaErrorStr(vas));
309         err = AVERROR(EIO);
310         goto fail_after_begin;
311     }
312     av_log(avctx, AV_LOG_DEBUG, "Pipeline parameter buffer is %#x.\n",
313            params_id);
314
315     vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
316                           &params_id, 1);
317     if (vas != VA_STATUS_SUCCESS) {
318         av_log(avctx, AV_LOG_ERROR, "Failed to render parameter buffer: "
319                "%d (%s).\n", vas, vaErrorStr(vas));
320         err = AVERROR(EIO);
321         goto fail_after_begin;
322     }
323
324     vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
325     if (vas != VA_STATUS_SUCCESS) {
326         av_log(avctx, AV_LOG_ERROR, "Failed to start picture processing: "
327                "%d (%s).\n", vas, vaErrorStr(vas));
328         err = AVERROR(EIO);
329         goto fail_after_render;
330     }
331
332     if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
333         AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
334         vas = vaDestroyBuffer(ctx->hwctx->display, params_id);
335         if (vas != VA_STATUS_SUCCESS) {
336             av_log(avctx, AV_LOG_ERROR, "Failed to free parameter buffer: "
337                    "%d (%s).\n", vas, vaErrorStr(vas));
338             // And ignore.
339         }
340     }
341
342     return 0;
343
344     // We want to make sure that if vaBeginPicture has been called, we also
345     // call vaRenderPicture and vaEndPicture.  These calls may well fail or
346     // do something else nasty, but once we're in this failure case there
347     // isn't much else we can do.
348 fail_after_begin:
349     vaRenderPicture(ctx->hwctx->display, ctx->va_context, &params_id, 1);
350 fail_after_render:
351     vaEndPicture(ctx->hwctx->display, ctx->va_context);
352 fail:
353     return err;
354 }
355
356 void ff_vaapi_vpp_ctx_init(AVFilterContext *avctx)
357 {
358     int i;
359     VAAPIVPPContext *ctx   = avctx->priv;
360
361     ctx->va_config  = VA_INVALID_ID;
362     ctx->va_context = VA_INVALID_ID;
363     ctx->valid_ids  = 1;
364
365     for (i = 0; i < VAProcFilterCount; i++)
366         ctx->filter_buffers[i] = VA_INVALID_ID;
367     ctx->nb_filter_buffers = 0;
368 }
369
370 void ff_vaapi_vpp_ctx_uninit(AVFilterContext *avctx)
371 {
372     VAAPIVPPContext *ctx   = avctx->priv;
373     if (ctx->valid_ids && ctx->pipeline_uninit)
374         ctx->pipeline_uninit(avctx);
375
376     av_buffer_unref(&ctx->input_frames_ref);
377     av_buffer_unref(&ctx->output_frames_ref);
378     av_buffer_unref(&ctx->device_ref);
379 }