2015-10-13 14:11:35 +02:00
|
|
|
/*
|
2015-10-22 16:18:02 +02:00
|
|
|
* This file is part of FFmpeg.
|
2015-10-13 14:11:35 +02:00
|
|
|
*
|
2015-10-22 16:18:02 +02:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2015-10-13 14:11:35 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2015-10-22 16:18:02 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2015-10-13 14:11:35 +02:00
|
|
|
* 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
|
2015-10-22 16:18:02 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2015-10-13 14:11:35 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <mfx/mfxvideo.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "libavutil/dict.h"
|
2016-11-13 17:37:52 +02:00
|
|
|
#include "libavutil/hwcontext.h"
|
|
|
|
#include "libavutil/hwcontext_qsv.h"
|
2015-10-13 14:11:35 +02:00
|
|
|
#include "libavutil/mem.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavcodec/qsv.h"
|
|
|
|
|
2015-10-22 16:18:02 +02:00
|
|
|
#include "ffmpeg.h"
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2017-01-05 08:48:06 +02:00
|
|
|
char *qsv_device = NULL;
|
|
|
|
|
2015-10-13 14:11:35 +02:00
|
|
|
static int qsv_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
|
|
|
|
{
|
|
|
|
InputStream *ist = s->opaque;
|
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
return av_hwframe_get_buffer(ist->hw_frames_ctx, frame, 0);
|
2015-10-13 14:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qsv_uninit(AVCodecContext *s)
|
|
|
|
{
|
|
|
|
InputStream *ist = s->opaque;
|
2016-11-13 17:37:52 +02:00
|
|
|
av_buffer_unref(&ist->hw_frames_ctx);
|
|
|
|
}
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
static int qsv_device_init(InputStream *ist)
|
|
|
|
{
|
|
|
|
int err;
|
2017-01-05 08:48:06 +02:00
|
|
|
AVDictionary *dict = NULL;
|
|
|
|
|
|
|
|
if (qsv_device) {
|
|
|
|
err = av_dict_set(&dict, "child_device", qsv_device, 0);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
}
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_QSV,
|
2017-01-05 08:48:06 +02:00
|
|
|
ist->hwaccel_device, dict, 0);
|
2016-11-13 17:37:52 +02:00
|
|
|
if (err < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Error creating a QSV device\n");
|
2017-01-05 08:48:06 +02:00
|
|
|
goto err_out;
|
2016-11-13 17:37:52 +02:00
|
|
|
}
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2017-01-05 08:48:06 +02:00
|
|
|
err_out:
|
|
|
|
if (dict)
|
|
|
|
av_dict_free(&dict);
|
|
|
|
|
|
|
|
return err;
|
2015-10-13 14:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int qsv_init(AVCodecContext *s)
|
|
|
|
{
|
|
|
|
InputStream *ist = s->opaque;
|
2016-11-13 17:37:52 +02:00
|
|
|
AVHWFramesContext *frames_ctx;
|
|
|
|
AVQSVFramesContext *frames_hwctx;
|
2015-10-13 14:11:35 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
if (!hw_device_ctx) {
|
|
|
|
ret = qsv_device_init(ist);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-10-13 14:11:35 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
av_buffer_unref(&ist->hw_frames_ctx);
|
|
|
|
ist->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
|
|
|
|
if (!ist->hw_frames_ctx)
|
2015-10-13 14:11:35 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
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;
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2016-11-13 17:37:52 +02: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 14:11:35 +02:00
|
|
|
|
|
|
|
ist->hwaccel_get_buffer = qsv_get_buffer;
|
|
|
|
ist->hwaccel_uninit = qsv_uninit;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qsv_transcode_init(OutputStream *ost)
|
|
|
|
{
|
|
|
|
InputStream *ist;
|
|
|
|
const enum AVPixelFormat *pix_fmt;
|
|
|
|
|
|
|
|
int err, i;
|
2016-11-13 17:37:52 +02:00
|
|
|
AVBufferRef *encode_frames_ref = NULL;
|
|
|
|
AVHWFramesContext *encode_frames;
|
|
|
|
AVQSVFramesContext *qsv_frames;
|
2015-10-13 14:11:35 +02:00
|
|
|
|
|
|
|
/* check if the encoder supports QSV */
|
|
|
|
if (!ost->enc->pix_fmts)
|
|
|
|
return 0;
|
|
|
|
for (pix_fmt = ost->enc->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
|
|
|
|
if (*pix_fmt == AV_PIX_FMT_QSV)
|
|
|
|
break;
|
|
|
|
if (*pix_fmt == AV_PIX_FMT_NONE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (strcmp(ost->avfilter, "null") || ost->source_index < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* check if the decoder supports QSV and the output only goes to this stream */
|
|
|
|
ist = input_streams[ost->source_index];
|
2016-08-11 09:34:01 +02:00
|
|
|
if (ist->hwaccel_id != HWACCEL_QSV || !ist->dec || !ist->dec->pix_fmts)
|
2015-10-13 14:11:35 +02:00
|
|
|
return 0;
|
|
|
|
for (pix_fmt = ist->dec->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
|
|
|
|
if (*pix_fmt == AV_PIX_FMT_QSV)
|
|
|
|
break;
|
|
|
|
if (*pix_fmt == AV_PIX_FMT_NONE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < nb_output_streams; i++)
|
|
|
|
if (output_streams[i] != ost &&
|
|
|
|
output_streams[i]->source_index == ost->source_index)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
av_log(NULL, AV_LOG_VERBOSE, "Setting up QSV transcoding\n");
|
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
if (!hw_device_ctx) {
|
|
|
|
err = qsv_device_init(ist);
|
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
// This creates a dummy hw_frames_ctx for the encoder to be
|
|
|
|
// suitably initialised. It only contains one real frame, so
|
|
|
|
// hopefully doesn't waste too much memory.
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
encode_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx);
|
|
|
|
if (!encode_frames_ref) {
|
|
|
|
err = AVERROR(ENOMEM);
|
2015-10-13 14:11:35 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2016-11-13 17:37:52 +02:00
|
|
|
encode_frames = (AVHWFramesContext*)encode_frames_ref->data;
|
|
|
|
qsv_frames = encode_frames->hwctx;
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
encode_frames->width = FFALIGN(ist->resample_width, 32);
|
|
|
|
encode_frames->height = FFALIGN(ist->resample_height, 32);
|
|
|
|
encode_frames->format = AV_PIX_FMT_QSV;
|
|
|
|
encode_frames->sw_format = AV_PIX_FMT_NV12;
|
|
|
|
encode_frames->initial_pool_size = 1;
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
qsv_frames->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
err = av_hwframe_ctx_init(encode_frames_ref);
|
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
ist->dec_ctx->pix_fmt = AV_PIX_FMT_QSV;
|
|
|
|
ist->resample_pix_fmt = AV_PIX_FMT_QSV;
|
2015-10-13 14:11:35 +02:00
|
|
|
|
2016-11-13 17:37:52 +02:00
|
|
|
ost->enc_ctx->pix_fmt = AV_PIX_FMT_QSV;
|
|
|
|
ost->enc_ctx->hw_frames_ctx = encode_frames_ref;
|
2015-10-13 14:11:35 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2016-11-13 17:37:52 +02:00
|
|
|
av_buffer_unref(&encode_frames_ref);
|
|
|
|
return err;
|
2015-10-13 14:11:35 +02:00
|
|
|
}
|