LCOV - code coverage report
Current view: top level - src - ffmpeg_cuvid.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 16 76 21.1 %
Date: 2017-01-28 02:43:52 Functions: 1 3 33.3 %

          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 "libavutil/hwcontext.h"
      20             : 
      21             : #include "ffmpeg.h"
      22             : 
      23             : typedef struct CUVIDContext {
      24             :     AVBufferRef *hw_frames_ctx;
      25             : } CUVIDContext;
      26             : 
      27           0 : static void cuvid_uninit(AVCodecContext *avctx)
      28             : {
      29           0 :     InputStream  *ist = avctx->opaque;
      30           0 :     CUVIDContext *ctx = ist->hwaccel_ctx;
      31             : 
      32           0 :     if (ctx) {
      33           0 :         av_buffer_unref(&ctx->hw_frames_ctx);
      34           0 :         av_freep(&ctx);
      35             :     }
      36             : 
      37           0 :     av_buffer_unref(&ist->hw_frames_ctx);
      38             : 
      39           0 :     ist->hwaccel_ctx = 0;
      40           0 :     ist->hwaccel_uninit = 0;
      41           0 : }
      42             : 
      43           0 : int cuvid_init(AVCodecContext *avctx)
      44             : {
      45           0 :     InputStream  *ist = avctx->opaque;
      46           0 :     CUVIDContext *ctx = ist->hwaccel_ctx;
      47             : 
      48           0 :     av_log(NULL, AV_LOG_TRACE, "Initializing cuvid hwaccel\n");
      49             : 
      50           0 :     if (!ctx) {
      51           0 :         av_log(NULL, AV_LOG_ERROR, "CUVID transcoding is not initialized. "
      52             :                "-hwaccel cuvid should only be used for one-to-one CUVID transcoding "
      53             :                "with no (software) filters.\n");
      54           0 :         return AVERROR(EINVAL);
      55             :     }
      56             : 
      57           0 :     return 0;
      58             : }
      59             : 
      60        5029 : int cuvid_transcode_init(OutputStream *ost)
      61             : {
      62             :     InputStream *ist;
      63             :     const enum AVPixelFormat *pix_fmt;
      64             :     AVHWFramesContext *hwframe_ctx;
      65        5029 :     AVBufferRef *device_ref = NULL;
      66        5029 :     CUVIDContext *ctx = NULL;
      67        5029 :     int ret = 0;
      68             : 
      69        5029 :     av_log(NULL, AV_LOG_TRACE, "Initializing cuvid transcoding\n");
      70             : 
      71        5029 :     if (ost->source_index < 0)
      72          29 :         return 0;
      73             : 
      74        5000 :     ist = input_streams[ost->source_index];
      75             : 
      76             :     /* check if the encoder supports CUVID */
      77        5000 :     if (!ost->enc->pix_fmts)
      78        4492 :         goto cancel;
      79        4112 :     for (pix_fmt = ost->enc->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
      80        3604 :         if (*pix_fmt == AV_PIX_FMT_CUDA)
      81           0 :             break;
      82         508 :     if (*pix_fmt == AV_PIX_FMT_NONE)
      83         508 :         goto cancel;
      84             : 
      85             :     /* check if the decoder supports CUVID */
      86           0 :     if (ist->hwaccel_id != HWACCEL_CUVID || !ist->dec || !ist->dec->pix_fmts)
      87             :         goto cancel;
      88           0 :     for (pix_fmt = ist->dec->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
      89           0 :         if (*pix_fmt == AV_PIX_FMT_CUDA)
      90           0 :             break;
      91           0 :     if (*pix_fmt == AV_PIX_FMT_NONE)
      92           0 :         goto cancel;
      93             : 
      94           0 :     av_log(NULL, AV_LOG_VERBOSE, "Setting up CUVID transcoding\n");
      95             : 
      96           0 :     if (ist->hwaccel_ctx) {
      97           0 :         ctx = ist->hwaccel_ctx;
      98             :     } else {
      99           0 :         ctx = av_mallocz(sizeof(*ctx));
     100           0 :         if (!ctx) {
     101           0 :             ret = AVERROR(ENOMEM);
     102           0 :             goto error;
     103             :         }
     104             :     }
     105             : 
     106           0 :     if (!ctx->hw_frames_ctx) {
     107           0 :         ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_CUDA,
     108           0 :                                      ist->hwaccel_device, NULL, 0);
     109           0 :         if (ret < 0)
     110           0 :             goto error;
     111             : 
     112           0 :         ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref);
     113           0 :         if (!ctx->hw_frames_ctx) {
     114           0 :             av_log(NULL, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed\n");
     115           0 :             ret = AVERROR(ENOMEM);
     116           0 :             goto error;
     117             :         }
     118           0 :         av_buffer_unref(&device_ref);
     119             : 
     120           0 :         ist->hw_frames_ctx = av_buffer_ref(ctx->hw_frames_ctx);
     121           0 :         if (!ist->hw_frames_ctx) {
     122           0 :             av_log(NULL, AV_LOG_ERROR, "av_buffer_ref failed\n");
     123           0 :             ret = AVERROR(ENOMEM);
     124           0 :             goto error;
     125             :         }
     126             : 
     127           0 :         ist->hwaccel_ctx = ctx;
     128           0 :         ist->resample_pix_fmt = AV_PIX_FMT_CUDA;
     129           0 :         ist->hwaccel_uninit = cuvid_uninit;
     130             : 
     131             :         /* This is a bit hacky, av_hwframe_ctx_init is called by the cuvid decoder
     132             :          * once it has probed the necessary format information. But as filters/nvenc
     133             :          * need to know the format/sw_format, set them here so they are happy.
     134             :          * This is fine as long as CUVID doesn't add another supported pix_fmt.
     135             :          */
     136           0 :         hwframe_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
     137           0 :         hwframe_ctx->format = AV_PIX_FMT_CUDA;
     138           0 :         hwframe_ctx->sw_format = AV_PIX_FMT_NV12;
     139             :     }
     140             : 
     141           0 :     return 0;
     142             : 
     143             : error:
     144           0 :     av_freep(&ctx);
     145           0 :     av_buffer_unref(&device_ref);
     146           0 :     return ret;
     147             : 
     148             : cancel:
     149        5000 :     if (ist->hwaccel_id == HWACCEL_CUVID) {
     150           0 :         av_log(NULL, AV_LOG_ERROR, "CUVID hwaccel requested, but impossible to achieve.\n");
     151           0 :         return AVERROR(EINVAL);
     152             :     }
     153             : 
     154        5000 :     return 0;
     155             : }

Generated by: LCOV version 1.12