LCOV - code coverage report
Current view: top level - src - ffmpeg_vdpau.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 79 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 <stdint.h>
      20             : 
      21             : #include "ffmpeg.h"
      22             : 
      23             : #include "libavcodec/vdpau.h"
      24             : 
      25             : #include "libavutil/buffer.h"
      26             : #include "libavutil/frame.h"
      27             : #include "libavutil/hwcontext.h"
      28             : #include "libavutil/hwcontext_vdpau.h"
      29             : #include "libavutil/pixfmt.h"
      30             : 
      31             : typedef struct VDPAUContext {
      32             :     AVBufferRef *hw_frames_ctx;
      33             :     AVFrame *tmp_frame;
      34             : } VDPAUContext;
      35             : 
      36           0 : static void vdpau_uninit(AVCodecContext *s)
      37             : {
      38           0 :     InputStream  *ist = s->opaque;
      39           0 :     VDPAUContext *ctx = ist->hwaccel_ctx;
      40             : 
      41           0 :     ist->hwaccel_uninit        = NULL;
      42           0 :     ist->hwaccel_get_buffer    = NULL;
      43           0 :     ist->hwaccel_retrieve_data = NULL;
      44             : 
      45           0 :     av_buffer_unref(&ctx->hw_frames_ctx);
      46           0 :     av_frame_free(&ctx->tmp_frame);
      47             : 
      48           0 :     av_freep(&ist->hwaccel_ctx);
      49           0 :     av_freep(&s->hwaccel_context);
      50           0 : }
      51             : 
      52           0 : static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
      53             : {
      54           0 :     InputStream         *ist = s->opaque;
      55           0 :     VDPAUContext        *ctx = ist->hwaccel_ctx;
      56             : 
      57           0 :     return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
      58             : }
      59             : 
      60           0 : static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
      61             : {
      62           0 :     InputStream        *ist = s->opaque;
      63           0 :     VDPAUContext       *ctx = ist->hwaccel_ctx;
      64             :     int ret;
      65             : 
      66           0 :     ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
      67           0 :     if (ret < 0)
      68           0 :         return ret;
      69             : 
      70           0 :     ret = av_frame_copy_props(ctx->tmp_frame, frame);
      71           0 :     if (ret < 0) {
      72           0 :         av_frame_unref(ctx->tmp_frame);
      73           0 :         return ret;
      74             :     }
      75             : 
      76           0 :     av_frame_unref(frame);
      77           0 :     av_frame_move_ref(frame, ctx->tmp_frame);
      78             : 
      79           0 :     return 0;
      80             : }
      81             : 
      82           0 : static int vdpau_alloc(AVCodecContext *s)
      83             : {
      84           0 :     InputStream  *ist = s->opaque;
      85           0 :     int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
      86             :     VDPAUContext *ctx;
      87             :     int ret;
      88             : 
      89           0 :     AVBufferRef          *device_ref = NULL;
      90             :     AVHWDeviceContext    *device_ctx;
      91             :     AVVDPAUDeviceContext *device_hwctx;
      92             :     AVHWFramesContext    *frames_ctx;
      93             : 
      94           0 :     ctx = av_mallocz(sizeof(*ctx));
      95           0 :     if (!ctx)
      96           0 :         return AVERROR(ENOMEM);
      97             : 
      98           0 :     ist->hwaccel_ctx           = ctx;
      99           0 :     ist->hwaccel_uninit        = vdpau_uninit;
     100           0 :     ist->hwaccel_get_buffer    = vdpau_get_buffer;
     101           0 :     ist->hwaccel_retrieve_data = vdpau_retrieve_data;
     102             : 
     103           0 :     ctx->tmp_frame = av_frame_alloc();
     104           0 :     if (!ctx->tmp_frame)
     105           0 :         goto fail;
     106             : 
     107           0 :     ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_VDPAU,
     108           0 :                                  ist->hwaccel_device, NULL, 0);
     109           0 :     if (ret < 0)
     110           0 :         goto fail;
     111           0 :     device_ctx   = (AVHWDeviceContext*)device_ref->data;
     112           0 :     device_hwctx = device_ctx->hwctx;
     113             : 
     114           0 :     ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref);
     115           0 :     if (!ctx->hw_frames_ctx)
     116           0 :         goto fail;
     117           0 :     av_buffer_unref(&device_ref);
     118             : 
     119           0 :     frames_ctx            = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
     120           0 :     frames_ctx->format    = AV_PIX_FMT_VDPAU;
     121           0 :     frames_ctx->sw_format = s->sw_pix_fmt;
     122           0 :     frames_ctx->width     = s->coded_width;
     123           0 :     frames_ctx->height    = s->coded_height;
     124             : 
     125           0 :     ret = av_hwframe_ctx_init(ctx->hw_frames_ctx);
     126           0 :     if (ret < 0)
     127           0 :         goto fail;
     128             : 
     129           0 :     if (av_vdpau_bind_context(s, device_hwctx->device, device_hwctx->get_proc_address, 0))
     130           0 :         goto fail;
     131             : 
     132           0 :     av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU to decode input stream #%d:%d.\n",
     133           0 :            ist->file_index, ist->st->index);
     134             : 
     135           0 :     return 0;
     136             : 
     137             : fail:
     138           0 :     av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
     139           0 :            ist->file_index, ist->st->index);
     140           0 :     av_buffer_unref(&device_ref);
     141           0 :     vdpau_uninit(s);
     142           0 :     return AVERROR(EINVAL);
     143             : }
     144             : 
     145           0 : int vdpau_init(AVCodecContext *s)
     146             : {
     147           0 :     InputStream *ist = s->opaque;
     148             : 
     149           0 :     if (!ist->hwaccel_ctx) {
     150           0 :         int ret = vdpau_alloc(s);
     151           0 :         if (ret < 0)
     152           0 :             return ret;
     153             :     }
     154             : 
     155           0 :     ist->hwaccel_get_buffer    = vdpau_get_buffer;
     156           0 :     ist->hwaccel_retrieve_data = vdpau_retrieve_data;
     157             : 
     158           0 :     return 0;
     159             : }

Generated by: LCOV version 1.12