History

avconv_vaapi: Convert to use hw_frames_ctx only

Most of the functionality here has moved into lavc.

Mark Thompson committed 10 years ago

Commit 3e8651a1 parent ccd03161 file · +23 −330Browse files

Unified · Split

avconv_vaapi.c+23−330
@@ -18,28 +18,10 @@ · 17 unmodified lines
18 18
19#include "config.h"19#include "config.h"
20 20
21#include <fcntl.h>
22#include <unistd.h>
23
24#include <va/va.h>
25#if HAVE_VAAPI_X11
26# include <va/va_x11.h>
27#endif
28#if HAVE_VAAPI_DRM
29# include <va/va_drm.h>
30#endif
31
32#include "libavutil/avassert.h"21#include "libavutil/avassert.h"
33#include "libavutil/avconfig.h"
34#include "libavutil/buffer.h"
35#include "libavutil/frame.h"22#include "libavutil/frame.h"
36#include "libavutil/hwcontext.h"23#include "libavutil/hwcontext.h"
37#include "libavutil/hwcontext_vaapi.h"24#include "libavutil/log.h"
38#include "libavutil/imgutils.h"
39#include "libavutil/opt.h"
40#include "libavutil/pixfmt.h"
41
42#include "libavcodec/vaapi.h"
43 25
44#include "avconv.h"26#include "avconv.h"
45 27
@@ -60,23 +42,11 @@ typedef struct VAAPIDecoderContext { · 14 unmodified lines
60 AVBufferRef *frames_ref;42 AVBufferRef *frames_ref;
61 AVHWFramesContext *frames;43 AVHWFramesContext *frames;
62 44
63 VAProfile va_profile;
64 VAEntrypoint va_entrypoint;
65 VAConfigID va_config;
66 VAContextID va_context;
67
68 enum AVPixelFormat decode_format;
69 int decode_width;
70 int decode_height;
71 int decode_surfaces;
72
73 // The output need not have the same format, width and height as the45 // The output need not have the same format, width and height as the
74 // decoded frames - the copy for non-direct-mapped access is actually46 // decoded frames - the copy for non-direct-mapped access is actually
75 // a whole vpp instance which can do arbitrary scaling and format47 // a whole vpp instance which can do arbitrary scaling and format
76 // conversion.48 // conversion.
77 enum AVPixelFormat output_format;49 enum AVPixelFormat output_format;
78
79 struct vaapi_context decoder_vaapi_context;
80} VAAPIDecoderContext;50} VAAPIDecoderContext;
81 51
82 52
@@ -144,257 +114,12 @@ fail: · 61 unmodified lines
144 return err;114 return err;
145}115}
146 116
147
148static const struct {
149 enum AVCodecID codec_id;
150 int codec_profile;
151 VAProfile va_profile;
152} vaapi_profile_map[] = {
153#define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v }
154 MAP(MPEG2VIDEO, MPEG2_SIMPLE, MPEG2Simple ),
155 MAP(MPEG2VIDEO, MPEG2_MAIN, MPEG2Main ),
156 MAP(H263, UNKNOWN, H263Baseline),
157 MAP(MPEG4, MPEG4_SIMPLE, MPEG4Simple ),
158 MAP(MPEG4, MPEG4_ADVANCED_SIMPLE,
159 MPEG4AdvancedSimple),
160 MAP(MPEG4, MPEG4_MAIN, MPEG4Main ),
161 MAP(H264, H264_CONSTRAINED_BASELINE,
162 H264ConstrainedBaseline),
163 MAP(H264, H264_BASELINE, H264Baseline),
164 MAP(H264, H264_MAIN, H264Main ),
165 MAP(H264, H264_HIGH, H264High ),
166#if VA_CHECK_VERSION(0, 37, 0)
167 MAP(HEVC, HEVC_MAIN, HEVCMain ),
168#endif
169 MAP(WMV3, VC1_SIMPLE, VC1Simple ),
170 MAP(WMV3, VC1_MAIN, VC1Main ),
171 MAP(WMV3, VC1_COMPLEX, VC1Advanced ),
172 MAP(WMV3, VC1_ADVANCED, VC1Advanced ),
173 MAP(VC1, VC1_SIMPLE, VC1Simple ),
174 MAP(VC1, VC1_MAIN, VC1Main ),
175 MAP(VC1, VC1_COMPLEX, VC1Advanced ),
176 MAP(VC1, VC1_ADVANCED, VC1Advanced ),
177#if VA_CHECK_VERSION(0, 35, 0)
178 MAP(VP8, UNKNOWN, VP8Version0_3 ),
179#endif
180#if VA_CHECK_VERSION(0, 37, 1)
181 MAP(VP9, VP9_0, VP9Profile0 ),
182#endif
183#undef MAP
184};
185
186static int vaapi_build_decoder_config(VAAPIDecoderContext *ctx,
187 AVCodecContext *avctx,
188 int fallback_allowed)
189{
190 AVVAAPIDeviceContext *hwctx = ctx->device->hwctx;
191 AVVAAPIHWConfig *hwconfig = NULL;
192 AVHWFramesConstraints *constraints = NULL;
193 VAStatus vas;
194 int err, i, j;
195 int loglevel = fallback_allowed ? AV_LOG_VERBOSE : AV_LOG_ERROR;
196 const AVCodecDescriptor *codec_desc;
197 const AVPixFmtDescriptor *pix_desc;
198 enum AVPixelFormat pix_fmt;
199 VAProfile profile, *profile_list = NULL;
200 int profile_count, exact_match, alt_profile;
201
202 codec_desc = avcodec_descriptor_get(avctx->codec_id);
203 if (!codec_desc) {
204 err = AVERROR(EINVAL);
205 goto fail;
206 }
207
208 profile_count = vaMaxNumProfiles(hwctx->display);
209 profile_list = av_malloc(profile_count * sizeof(VAProfile));
210 if (!profile_list) {
211 err = AVERROR(ENOMEM);
212 goto fail;
213 }
214
215 vas = vaQueryConfigProfiles(hwctx->display,
216 profile_list, &profile_count);
217 if (vas != VA_STATUS_SUCCESS) {
218 av_log(ctx, loglevel, "Failed to query profiles: %d (%s).\n",
219 vas, vaErrorStr(vas));
220 err = AVERROR(EIO);
221 goto fail;
222 }
223
224 profile = VAProfileNone;
225 exact_match = 0;
226
227 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
228 int profile_match = 0;
229 if (avctx->codec_id != vaapi_profile_map[i].codec_id)
230 continue;
231 if (avctx->profile == vaapi_profile_map[i].codec_profile)
232 profile_match = 1;
233 profile = vaapi_profile_map[i].va_profile;
234 for (j = 0; j < profile_count; j++) {
235 if (profile == profile_list[j]) {
236 exact_match = profile_match;
237 break;
238 }
239 }
240 if (j < profile_count) {
241 if (exact_match)
242 break;
243 alt_profile = vaapi_profile_map[i].codec_profile;
244 }
245 }
246 av_freep(&profile_list);
247
248 if (profile == VAProfileNone) {
249 av_log(ctx, loglevel, "No VAAPI support for codec %s.\n",
250 codec_desc->name);
251 err = AVERROR(ENOSYS);
252 goto fail;
253 }
254 if (!exact_match) {
255 if (fallback_allowed || !hwaccel_lax_profile_check) {
256 av_log(ctx, loglevel, "No VAAPI support for codec %s "
257 "profile %d.\n", codec_desc->name, avctx->profile);
258 if (!fallback_allowed) {
259 av_log(ctx, AV_LOG_WARNING, "If you want attempt decoding "
260 "anyway with a possibly-incompatible profile, add "
261 "the option -hwaccel_lax_profile_check.\n");
262 }
263 err = AVERROR(EINVAL);
264 goto fail;
265 } else {
266 av_log(ctx, AV_LOG_WARNING, "No VAAPI support for codec %s "
267 "profile %d: trying instead with profile %d.\n",
268 codec_desc->name, avctx->profile, alt_profile);
269 av_log(ctx, AV_LOG_WARNING, "This may fail or give "
270 "incorrect results, depending on your hardware.\n");
271 }
272 }
273
274 ctx->va_profile = profile;
275 ctx->va_entrypoint = VAEntrypointVLD;
276
277 vas = vaCreateConfig(hwctx->display, ctx->va_profile,
278 ctx->va_entrypoint, 0, 0, &ctx->va_config);
279 if (vas != VA_STATUS_SUCCESS) {
280 av_log(ctx, AV_LOG_ERROR, "Failed to create decode pipeline "
281 "configuration: %d (%s).\n", vas, vaErrorStr(vas));
282 err = AVERROR(EIO);
283 goto fail;
284 }
285
286 hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
287 if (!hwconfig) {
288 err = AVERROR(ENOMEM);
289 goto fail;
290 }
291 hwconfig->config_id = ctx->va_config;
292
293 constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
294 hwconfig);
295 if (!constraints)
296 goto fail;
297
298 // Decide on the decoder target format.
299 // If the user specified something with -hwaccel_output_format then
300 // try to use that to minimise conversions later.
301 ctx->decode_format = AV_PIX_FMT_NONE;
302 if (ctx->output_format != AV_PIX_FMT_NONE &&
303 ctx->output_format != AV_PIX_FMT_VAAPI) {
304 for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
305 if (constraints->valid_sw_formats[i] == ctx->decode_format) {
306 ctx->decode_format = ctx->output_format;
307 av_log(ctx, AV_LOG_DEBUG, "Using decode format %s (output "
308 "format).\n", av_get_pix_fmt_name(ctx->decode_format));
309 break;
310 }
311 }
312 }
313 // Otherwise, we would like to try to choose something which matches the
314 // decoder output, but there isn't enough information available here to
315 // do so. Assume for now that we are always dealing with YUV 4:2:0, so
316 // pick a format which does that.
317 if (ctx->decode_format == AV_PIX_FMT_NONE) {
318 for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
319 pix_fmt = constraints->valid_sw_formats[i];
320 pix_desc = av_pix_fmt_desc_get(pix_fmt);
321 if (pix_desc->nb_components == 3 &&
322 pix_desc->log2_chroma_w == 1 &&
323 pix_desc->log2_chroma_h == 1) {
324 ctx->decode_format = pix_fmt;
325 av_log(ctx, AV_LOG_DEBUG, "Using decode format %s (format "
326 "matched).\n", av_get_pix_fmt_name(ctx->decode_format));
327 break;
328 }
329 }
330 }
331 // Otherwise pick the first in the list and hope for the best.
332 if (ctx->decode_format == AV_PIX_FMT_NONE) {
333 ctx->decode_format = constraints->valid_sw_formats[0];
334 av_log(ctx, AV_LOG_DEBUG, "Using decode format %s (first in list).\n",
335 av_get_pix_fmt_name(ctx->decode_format));
336 if (i > 1) {
337 // There was a choice, and we picked randomly. Warn the user
338 // that they might want to choose intelligently instead.
339 av_log(ctx, AV_LOG_WARNING, "Using randomly chosen decode "
340 "format %s.\n", av_get_pix_fmt_name(ctx->decode_format));
341 }
342 }
343
344 // Ensure the picture size is supported by the hardware.
345 ctx->decode_width = avctx->coded_width;
346 ctx->decode_height = avctx->coded_height;
347 if (ctx->decode_width < constraints->min_width ||
348 ctx->decode_height < constraints->min_height ||
349 ctx->decode_width > constraints->max_width ||
350 ctx->decode_height >constraints->max_height) {
351 av_log(ctx, AV_LOG_ERROR, "VAAPI hardware does not support image "
352 "size %dx%d (constraints: width %d-%d height %d-%d).\n",
353 ctx->decode_width, ctx->decode_height,
354 constraints->min_width, constraints->max_width,
355 constraints->min_height, constraints->max_height);
356 err = AVERROR(EINVAL);
357 goto fail;
358 }
359
360 av_hwframe_constraints_free(&constraints);
361 av_freep(&hwconfig);
362
363 // Decide how many reference frames we need. This might be doable more
364 // nicely based on the codec and input stream?
365 ctx->decode_surfaces = DEFAULT_SURFACES;
366 // For frame-threaded decoding, one additional surfaces is needed for
367 // each thread.
368 if (avctx->active_thread_type & FF_THREAD_FRAME)
369 ctx->decode_surfaces += avctx->thread_count;
370
371 return 0;
372
373fail:
374 av_hwframe_constraints_free(&constraints);
375 av_freep(&hwconfig);
376 vaDestroyConfig(hwctx->display, ctx->va_config);
377 av_freep(&profile_list);
378 return err;
379}
380
381static void vaapi_decode_uninit(AVCodecContext *avctx)117static void vaapi_decode_uninit(AVCodecContext *avctx)
382{118{
383 InputStream *ist = avctx->opaque;119 InputStream *ist = avctx->opaque;
384 VAAPIDecoderContext *ctx = ist->hwaccel_ctx;120 VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
385 121
386 if (ctx) {122 if (ctx) {
387 AVVAAPIDeviceContext *hwctx = ctx->device->hwctx;
388
389 if (ctx->va_context != VA_INVALID_ID) {
390 vaDestroyContext(hwctx->display, ctx->va_context);
391 ctx->va_context = VA_INVALID_ID;
392 }
393 if (ctx->va_config != VA_INVALID_ID) {
394 vaDestroyConfig(hwctx->display, ctx->va_config);
395 ctx->va_config = VA_INVALID_ID;
396 }
397
398 av_buffer_unref(&ctx->frames_ref);123 av_buffer_unref(&ctx->frames_ref);
399 av_buffer_unref(&ctx->device_ref);124 av_buffer_unref(&ctx->device_ref);
400 av_free(ctx);125 av_free(ctx);
@@ -402,19 +127,16 @@ static void vaapi_decode_uninit(AVCodecContext *avctx) · 1 unmodified line
402 127
403 av_buffer_unref(&ist->hw_frames_ctx);128 av_buffer_unref(&ist->hw_frames_ctx);
404 129
405 ist->hwaccel_ctx = 0;130 ist->hwaccel_ctx = NULL;
406 ist->hwaccel_uninit = 0;131 ist->hwaccel_uninit = NULL;
407 ist->hwaccel_get_buffer = 0;132 ist->hwaccel_get_buffer = NULL;
408 ist->hwaccel_retrieve_data = 0;133 ist->hwaccel_retrieve_data = NULL;
409}134}
410 135
411int vaapi_decode_init(AVCodecContext *avctx)136int vaapi_decode_init(AVCodecContext *avctx)
412{137{
413 InputStream *ist = avctx->opaque;138 InputStream *ist = avctx->opaque;
414 AVVAAPIDeviceContext *hwctx;
415 AVVAAPIFramesContext *avfc;
416 VAAPIDecoderContext *ctx;139 VAAPIDecoderContext *ctx;
417 VAStatus vas;
418 int err;140 int err;
419 int loglevel = (ist->hwaccel_id != HWACCEL_VAAPI ? AV_LOG_VERBOSE141 int loglevel = (ist->hwaccel_id != HWACCEL_VAAPI ? AV_LOG_VERBOSE
420 : AV_LOG_ERROR);142 : AV_LOG_ERROR);
@@ -439,20 +161,7 @@ int vaapi_decode_init(AVCodecContext *avctx) · 18 unmodified lines
439 ctx->device_ref = av_buffer_ref(hw_device_ctx);161 ctx->device_ref = av_buffer_ref(hw_device_ctx);
440 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;162 ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
441 163
442 ctx->va_config = VA_INVALID_ID;
443 ctx->va_context = VA_INVALID_ID;
444
445 hwctx = ctx->device->hwctx;
446
447 ctx->output_format = ist->hwaccel_output_format;164 ctx->output_format = ist->hwaccel_output_format;
448
449 err = vaapi_build_decoder_config(ctx, avctx,
450 ist->hwaccel_id != HWACCEL_VAAPI);
451 if (err < 0) {
452 av_log(ctx, loglevel, "No supported configuration for this codec.");
453 goto fail;
454 }
455
456 avctx->pix_fmt = ctx->output_format;165 avctx->pix_fmt = ctx->output_format;
457 166
458 ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);167 ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
@@ -464,11 +173,21 @@ int vaapi_decode_init(AVCodecContext *avctx) · 5 unmodified lines
464 173
465 ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;174 ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
466 175
467 ctx->frames->format = AV_PIX_FMT_VAAPI;176 ctx->frames->format = AV_PIX_FMT_VAAPI;
468 ctx->frames->sw_format = ctx->decode_format;177 ctx->frames->width = avctx->coded_width;
469 ctx->frames->width = ctx->decode_width;178 ctx->frames->height = avctx->coded_height;
470 ctx->frames->height = ctx->decode_height;179
471 ctx->frames->initial_pool_size = ctx->decode_surfaces;180 // It would be nice if we could query the available formats here,
181 // but unfortunately we don't have a VAConfigID to do it with.
182 // For now, just assume an NV12 format (or P010 if 10-bit).
183 ctx->frames->sw_format = (avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
184 AV_PIX_FMT_P010 : AV_PIX_FMT_NV12);
185
186 // For frame-threaded decoding, at least one additional surface
187 // is needed for each thread.
188 ctx->frames->initial_pool_size = DEFAULT_SURFACES;
189 if (avctx->active_thread_type & FF_THREAD_FRAME)
190 ctx->frames->initial_pool_size += avctx->thread_count;
472 191
473 err = av_hwframe_ctx_init(ctx->frames_ref);192 err = av_hwframe_ctx_init(ctx->frames_ref);
474 if (err < 0) {193 if (err < 0) {
@@ -477,27 +196,6 @@ int vaapi_decode_init(AVCodecContext *avctx) · 2 unmodified lines
477 goto fail;196 goto fail;
478 }197 }
479 198
480 avfc = ctx->frames->hwctx;
481
482 vas = vaCreateContext(hwctx->display, ctx->va_config,
483 ctx->decode_width, ctx->decode_height,
484 VA_PROGRESSIVE,
485 avfc->surface_ids, avfc->nb_surfaces,
486 &ctx->va_context);
487 if (vas != VA_STATUS_SUCCESS) {
488 av_log(ctx, AV_LOG_ERROR, "Failed to create decode pipeline "
489 "context: %d (%s).\n", vas, vaErrorStr(vas));
490 err = AVERROR(EINVAL);
491 goto fail;
492 }
493
494 av_log(ctx, AV_LOG_DEBUG, "VAAPI decoder (re)init complete.\n");
495
496 // We would like to set this on the AVCodecContext for use by whoever gets
497 // the frames from the decoder, but unfortunately the AVCodecContext we
498 // have here need not be the "real" one (H.264 makes many copies for
499 // threading purposes). To avoid the problem, we instead store it in the
500 // InputStream and propagate it from there.
501 ist->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);199 ist->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
502 if (!ist->hw_frames_ctx) {200 if (!ist->hw_frames_ctx) {
503 err = AVERROR(ENOMEM);201 err = AVERROR(ENOMEM);
@@ -505,14 +203,9 @@ int vaapi_decode_init(AVCodecContext *avctx) · 1 unmodified line
505 }203 }
506 204
507 ist->hwaccel_ctx = ctx;205 ist->hwaccel_ctx = ctx;
508 ist->hwaccel_uninit = vaapi_decode_uninit;206 ist->hwaccel_uninit = &vaapi_decode_uninit;
509 ist->hwaccel_get_buffer = vaapi_get_buffer;207 ist->hwaccel_get_buffer = &vaapi_get_buffer;
510 ist->hwaccel_retrieve_data = vaapi_retrieve_data;208 ist->hwaccel_retrieve_data = &vaapi_retrieve_data;
511
512 ctx->decoder_vaapi_context.display = hwctx->display;
513 ctx->decoder_vaapi_context.config_id = ctx->va_config;
514 ctx->decoder_vaapi_context.context_id = ctx->va_context;
515 avctx->hwaccel_context = &ctx->decoder_vaapi_context;
516 209
517 return 0;210 return 0;
518 211

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t