2015-10-13 12:11:35 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Libav.
|
|
|
|
*
|
|
|
|
* Libav is free software; you can redistribute it and/or
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Libav is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with Libav; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <mfx/mfxvideo.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "libavutil/dict.h"
|
2016-05-22 07:40:46 +00:00
|
|
|
#include "libavutil/hwcontext.h"
|
|
|
|
#include "libavutil/hwcontext_qsv.h"
|
2015-10-13 12:11:35 +00:00
|
|
|
#include "libavutil/mem.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavcodec/qsv.h"
|
|
|
|
|
|
|
|
#include "avconv.h"
|
|
|
|
|
|
|
|
static int qsv_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
|
|
|
|
{
|
|
|
|
InputStream *ist = s->opaque;
|
|
|
|
|
2016-05-22 07:40:46 +00:00
|
|
|
return av_hwframe_get_buffer(ist->hw_frames_ctx, frame, 0);
|
2015-10-13 12:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qsv_uninit(AVCodecContext *s)
|
|
|
|
{
|
|
|
|
InputStream *ist = s->opaque;
|
2016-05-22 07:40:46 +00:00
|
|
|
av_buffer_unref(&ist->hw_frames_ctx);
|
2015-10-13 12:11:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 07:40:46 +00:00
|
|
|
static int qsv_device_init(InputStream *ist)
|
2015-10-13 12:11:35 +00:00
|
|
|
{
|
2016-05-22 07:40:46 +00:00
|
|
|
int err;
|
2015-10-13 12:11:35 +00:00
|
|
|
|
2016-05-22 07:40:46 +00:00
|
|
|
err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_QSV,
|
|
|
|
ist->hwaccel_device, NULL, 0);
|
|
|
|
if (err < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Error creating a QSV device\n");
|
|
|
|
return err;
|
2015-10-13 12:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-22 07:40:46 +00:00
|
|
|
int qsv_init(AVCodecContext *s)
|
2015-10-13 12:11:35 +00:00
|
|
|
{
|
2016-05-22 07:40:46 +00:00
|
|
|
InputStream *ist = s->opaque;
|
|
|
|
AVHWFramesContext *frames_ctx;
|
|
|
|
AVQSVFramesContext *frames_hwctx;
|
|
|
|
int ret;
|
2015-10-13 12:11:35 +00:00
|
|
|
|
2016-05-22 07:40:46 +00:00
|
|
|
if (!hw_device_ctx) {
|
|
|
|
ret = qsv_device_init(ist);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-10-13 12:11:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 07:40:46 +00:00
|
|
|
av_buffer_unref(&ist->hw_frames_ctx);
|
|
|
|
ist->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
|
|
|
|
if (!ist->hw_frames_ctx)
|
|
|
|
return AVERROR(ENOMEM);
|
2015-10-13 12:11:35 +00:00
|
|
|
|
2016-05-22 07:40:46 +00:00
|
|
|
frames_ctx = (AVHWFramesContext*)ist->hw_frames_ctx->data;
|
|
|
|
frames_hwctx = frames_ctx->hwctx;
|
2015-10-13 12:11:35 +00:00
|
|
|
|
2016-05-22 07:40:46 +00:00
|
|
|
frames_ctx->width = s->coded_width;
|
|
|
|
frames_ctx->height = s->coded_height;
|
|
|
|
frames_ctx->format = AV_PIX_FMT_QSV;
|
|
|
|
frames_ctx->sw_format = AV_PIX_FMT_NV12;
|
|
|
|
frames_ctx->initial_pool_size = 32;
|
|
|
|
frames_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
|
2015-10-13 12:11:35 +00:00
|
|
|
|
2016-05-22 07:40:46 +00:00
|
|
|
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;
|
2015-10-13 12:11:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-22 07:40:46 +00:00
|
|
|
ist->hwaccel_get_buffer = qsv_get_buffer;
|
|
|
|
ist->hwaccel_uninit = qsv_uninit;
|
2015-10-13 12:11:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|