ffmpeg_qsv.c
fb472e1a
 /*
dd8a4b0f
  * This file is part of FFmpeg.
fb472e1a
  *
dd8a4b0f
  * FFmpeg is free software; you can redistribute it and/or
fb472e1a
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
dd8a4b0f
  * FFmpeg is distributed in the hope that it will be useful,
fb472e1a
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
dd8a4b0f
  * License along with FFmpeg; if not, write to the Free Software
fb472e1a
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include <mfx/mfxvideo.h>
 #include <stdlib.h>
 
 #include "libavutil/dict.h"
03cef34a
 #include "libavutil/hwcontext.h"
 #include "libavutil/hwcontext_qsv.h"
fb472e1a
 #include "libavutil/mem.h"
 #include "libavutil/opt.h"
 #include "libavcodec/qsv.h"
 
dd8a4b0f
 #include "ffmpeg.h"
fb472e1a
 
1a79b8f8
 char *qsv_device = NULL;
 
fb472e1a
 static int qsv_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
 {
     InputStream *ist = s->opaque;
 
03cef34a
     return av_hwframe_get_buffer(ist->hw_frames_ctx, frame, 0);
fb472e1a
 }
 
 static void qsv_uninit(AVCodecContext *s)
 {
     InputStream *ist = s->opaque;
03cef34a
     av_buffer_unref(&ist->hw_frames_ctx);
 }
fb472e1a
 
03cef34a
 static int qsv_device_init(InputStream *ist)
 {
     int err;
1a79b8f8
     AVDictionary *dict = NULL;
 
     if (qsv_device) {
         err = av_dict_set(&dict, "child_device", qsv_device, 0);
         if (err < 0)
             return err;
     }
fb472e1a
 
03cef34a
     err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_QSV,
1a79b8f8
                                  ist->hwaccel_device, dict, 0);
03cef34a
     if (err < 0) {
         av_log(NULL, AV_LOG_ERROR, "Error creating a QSV device\n");
1a79b8f8
         goto err_out;
03cef34a
     }
fb472e1a
 
1a79b8f8
 err_out:
     if (dict)
         av_dict_free(&dict);
 
     return err;
fb472e1a
 }
 
 int qsv_init(AVCodecContext *s)
 {
     InputStream *ist = s->opaque;
03cef34a
     AVHWFramesContext *frames_ctx;
     AVQSVFramesContext *frames_hwctx;
fb472e1a
     int ret;
 
03cef34a
     if (!hw_device_ctx) {
         ret = qsv_device_init(ist);
         if (ret < 0)
             return ret;
fb472e1a
     }
 
03cef34a
     av_buffer_unref(&ist->hw_frames_ctx);
     ist->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
     if (!ist->hw_frames_ctx)
fb472e1a
         return AVERROR(ENOMEM);
 
03cef34a
     frames_ctx   = (AVHWFramesContext*)ist->hw_frames_ctx->data;
     frames_hwctx = frames_ctx->hwctx;
 
     frames_ctx->width             = FFALIGN(s->coded_width,  32);
     frames_ctx->height            = FFALIGN(s->coded_height, 32);
     frames_ctx->format            = AV_PIX_FMT_QSV;
     frames_ctx->sw_format         = s->sw_pix_fmt;
     frames_ctx->initial_pool_size = 64;
     frames_hwctx->frame_type      = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
fb472e1a
 
03cef34a
     ret = av_hwframe_ctx_init(ist->hw_frames_ctx);
     if (ret < 0) {
         av_log(NULL, AV_LOG_ERROR, "Error initializing a QSV frame pool\n");
         return ret;
     }
fb472e1a
 
     ist->hwaccel_get_buffer = qsv_get_buffer;
     ist->hwaccel_uninit     = qsv_uninit;
 
     return 0;
 }