LCOV - code coverage report
Current view: top level - src - ffmpeg_vaapi.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 104 0.0 %
Date: 2017-03-16 17:17:40 Functions: 0 5 0.0 %

          Line data    Source code
       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 "config.h"
      20             : 
      21             : #include "libavutil/avassert.h"
      22             : #include "libavutil/frame.h"
      23             : #include "libavutil/hwcontext.h"
      24             : #include "libavutil/log.h"
      25             : 
      26             : #include "ffmpeg.h"
      27             : 
      28             : 
      29             : static AVClass vaapi_class = {
      30             :     .class_name = "vaapi",
      31             :     .item_name  = av_default_item_name,
      32             :     .version    = LIBAVUTIL_VERSION_INT,
      33             : };
      34             : 
      35             : #define DEFAULT_SURFACES 20
      36             : 
      37             : typedef struct VAAPIDecoderContext {
      38             :     const AVClass *class;
      39             : 
      40             :     AVBufferRef       *device_ref;
      41             :     AVHWDeviceContext *device;
      42             :     AVBufferRef       *frames_ref;
      43             :     AVHWFramesContext *frames;
      44             : 
      45             :     // The output need not have the same format, width and height as the
      46             :     // decoded frames - the copy for non-direct-mapped access is actually
      47             :     // a whole vpp instance which can do arbitrary scaling and format
      48             :     // conversion.
      49             :     enum AVPixelFormat output_format;
      50             : } VAAPIDecoderContext;
      51             : 
      52             : 
      53           0 : static int vaapi_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
      54             : {
      55           0 :     InputStream *ist = avctx->opaque;
      56           0 :     VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
      57             :     int err;
      58             : 
      59           0 :     err = av_hwframe_get_buffer(ctx->frames_ref, frame, 0);
      60           0 :     if (err < 0) {
      61           0 :         av_log(ctx, AV_LOG_ERROR, "Failed to allocate decoder surface.\n");
      62             :     } else {
      63           0 :         av_log(ctx, AV_LOG_DEBUG, "Decoder given surface %#x.\n",
      64           0 :                (unsigned int)(uintptr_t)frame->data[3]);
      65             :     }
      66           0 :     return err;
      67             : }
      68             : 
      69           0 : static int vaapi_retrieve_data(AVCodecContext *avctx, AVFrame *input)
      70             : {
      71           0 :     InputStream *ist = avctx->opaque;
      72           0 :     VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
      73           0 :     AVFrame *output = 0;
      74             :     int err;
      75             : 
      76           0 :     av_assert0(input->format == AV_PIX_FMT_VAAPI);
      77             : 
      78           0 :     if (ctx->output_format == AV_PIX_FMT_VAAPI) {
      79             :         // Nothing to do.
      80           0 :         return 0;
      81             :     }
      82             : 
      83           0 :     av_log(ctx, AV_LOG_DEBUG, "Retrieve data from surface %#x.\n",
      84           0 :            (unsigned int)(uintptr_t)input->data[3]);
      85             : 
      86           0 :     output = av_frame_alloc();
      87           0 :     if (!output)
      88           0 :         return AVERROR(ENOMEM);
      89             : 
      90           0 :     output->format = ctx->output_format;
      91             : 
      92           0 :     err = av_hwframe_transfer_data(output, input, 0);
      93           0 :     if (err < 0) {
      94           0 :         av_log(ctx, AV_LOG_ERROR, "Failed to transfer data to "
      95             :                "output frame: %d.\n", err);
      96           0 :         goto fail;
      97             :     }
      98             : 
      99           0 :     err = av_frame_copy_props(output, input);
     100           0 :     if (err < 0) {
     101           0 :         av_frame_unref(output);
     102           0 :         goto fail;
     103             :     }
     104             : 
     105           0 :     av_frame_unref(input);
     106           0 :     av_frame_move_ref(input, output);
     107           0 :     av_frame_free(&output);
     108             : 
     109           0 :     return 0;
     110             : 
     111             : fail:
     112           0 :     if (output)
     113           0 :         av_frame_free(&output);
     114           0 :     return err;
     115             : }
     116             : 
     117           0 : static void vaapi_decode_uninit(AVCodecContext *avctx)
     118             : {
     119           0 :     InputStream *ist = avctx->opaque;
     120           0 :     VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
     121             : 
     122           0 :     if (ctx) {
     123           0 :         av_buffer_unref(&ctx->frames_ref);
     124           0 :         av_buffer_unref(&ctx->device_ref);
     125           0 :         av_free(ctx);
     126             :     }
     127             : 
     128           0 :     av_buffer_unref(&ist->hw_frames_ctx);
     129             : 
     130           0 :     ist->hwaccel_ctx           = NULL;
     131           0 :     ist->hwaccel_uninit        = NULL;
     132           0 :     ist->hwaccel_get_buffer    = NULL;
     133           0 :     ist->hwaccel_retrieve_data = NULL;
     134           0 : }
     135             : 
     136           0 : int vaapi_decode_init(AVCodecContext *avctx)
     137             : {
     138           0 :     InputStream *ist = avctx->opaque;
     139             :     VAAPIDecoderContext *ctx;
     140             :     int err;
     141           0 :     int loglevel = (ist->hwaccel_id != HWACCEL_VAAPI ? AV_LOG_VERBOSE
     142           0 :                                                      : AV_LOG_ERROR);
     143             : 
     144           0 :     if (ist->hwaccel_ctx)
     145           0 :         vaapi_decode_uninit(avctx);
     146             : 
     147             :     // We have -hwaccel without -vaapi_device, so just initialise here with
     148             :     // the device passed as -hwaccel_device (if -vaapi_device was passed, it
     149             :     // will always have been called before now).
     150           0 :     if (!hw_device_ctx) {
     151           0 :         err = vaapi_device_init(ist->hwaccel_device);
     152           0 :         if (err < 0)
     153           0 :             return err;
     154             :     }
     155             : 
     156           0 :     ctx = av_mallocz(sizeof(*ctx));
     157           0 :     if (!ctx)
     158           0 :         return AVERROR(ENOMEM);
     159           0 :     ctx->class = &vaapi_class;
     160           0 :     ist->hwaccel_ctx = ctx;
     161             : 
     162           0 :     ctx->device_ref = av_buffer_ref(hw_device_ctx);
     163           0 :     ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
     164             : 
     165           0 :     ctx->output_format = ist->hwaccel_output_format;
     166           0 :     avctx->pix_fmt = ctx->output_format;
     167             : 
     168           0 :     ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
     169           0 :     if (!ctx->frames_ref) {
     170           0 :         av_log(ctx, loglevel, "Failed to create VAAPI frame context.\n");
     171           0 :         err = AVERROR(ENOMEM);
     172           0 :         goto fail;
     173             :     }
     174             : 
     175           0 :     ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
     176             : 
     177           0 :     ctx->frames->format = AV_PIX_FMT_VAAPI;
     178           0 :     ctx->frames->width  = avctx->coded_width;
     179           0 :     ctx->frames->height = avctx->coded_height;
     180             : 
     181             :     // It would be nice if we could query the available formats here,
     182             :     // but unfortunately we don't have a VAConfigID to do it with.
     183             :     // For now, just assume an NV12 format (or P010 if 10-bit).
     184           0 :     ctx->frames->sw_format = (avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P10 ?
     185           0 :                               AV_PIX_FMT_P010 : AV_PIX_FMT_NV12);
     186             : 
     187             :     // For frame-threaded decoding, at least one additional surface
     188             :     // is needed for each thread.
     189           0 :     ctx->frames->initial_pool_size = DEFAULT_SURFACES;
     190           0 :     if (avctx->active_thread_type & FF_THREAD_FRAME)
     191           0 :         ctx->frames->initial_pool_size += avctx->thread_count;
     192             : 
     193           0 :     err = av_hwframe_ctx_init(ctx->frames_ref);
     194           0 :     if (err < 0) {
     195           0 :         av_log(ctx, loglevel, "Failed to initialise VAAPI frame "
     196             :                "context: %d\n", err);
     197           0 :         goto fail;
     198             :     }
     199             : 
     200           0 :     ist->hw_frames_ctx = av_buffer_ref(ctx->frames_ref);
     201           0 :     if (!ist->hw_frames_ctx) {
     202           0 :         err = AVERROR(ENOMEM);
     203           0 :         goto fail;
     204             :     }
     205             : 
     206           0 :     ist->hwaccel_uninit        = &vaapi_decode_uninit;
     207           0 :     ist->hwaccel_get_buffer    = &vaapi_get_buffer;
     208           0 :     ist->hwaccel_retrieve_data = &vaapi_retrieve_data;
     209             : 
     210           0 :     return 0;
     211             : 
     212             : fail:
     213           0 :     vaapi_decode_uninit(avctx);
     214           0 :     return err;
     215             : }
     216             : 
     217             : static AVClass *vaapi_log = &vaapi_class;
     218             : 
     219           0 : av_cold int vaapi_device_init(const char *device)
     220             : {
     221             :     int err;
     222             : 
     223           0 :     av_buffer_unref(&hw_device_ctx);
     224             : 
     225           0 :     err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
     226             :                                  device, NULL, 0);
     227           0 :     if (err < 0) {
     228           0 :         av_log(&vaapi_log, AV_LOG_ERROR, "Failed to create a VAAPI device\n");
     229           0 :         return err;
     230             :     }
     231             : 
     232           0 :     return 0;
     233             : }

Generated by: LCOV version 1.12