2015-07-11 15:21:39 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Sebastien Zwickert
|
|
|
|
*
|
2016-01-20 14:43:56 +00:00
|
|
|
* mpv 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.
|
2015-07-11 15:21:39 +00:00
|
|
|
*
|
|
|
|
* mpv 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
|
2016-01-20 14:43:56 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2015-07-11 15:21:39 +00:00
|
|
|
*
|
2016-01-20 14:43:56 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-11 15:21:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libavcodec/version.h>
|
|
|
|
#include <libavcodec/videotoolbox.h>
|
|
|
|
|
|
|
|
#include "common/av_common.h"
|
|
|
|
#include "common/msg.h"
|
|
|
|
#include "video/mp_image.h"
|
|
|
|
#include "video/decode/lavc.h"
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
|
|
static int probe(struct vd_lavc_hwdec *hwdec, struct mp_hwdec_info *info,
|
|
|
|
const char *decoder)
|
|
|
|
{
|
|
|
|
hwdec_request_api(info, "videotoolbox");
|
2015-11-17 20:07:41 +00:00
|
|
|
if (!info || !info->hwctx || info->hwctx->type != HWDEC_VIDEOTOOLBOX)
|
2015-07-11 15:21:39 +00:00
|
|
|
return HWDEC_ERR_NO_CTX;
|
|
|
|
switch (mp_codec_to_av_codec_id(decoder)) {
|
|
|
|
case AV_CODEC_ID_H264:
|
|
|
|
case AV_CODEC_ID_H263:
|
|
|
|
case AV_CODEC_ID_MPEG1VIDEO:
|
|
|
|
case AV_CODEC_ID_MPEG2VIDEO:
|
|
|
|
case AV_CODEC_ID_MPEG4:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return HWDEC_ERR_NO_CODEC;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init(struct lavc_ctx *ctx)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct videotoolbox_error {
|
|
|
|
int code;
|
|
|
|
char *reason;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct videotoolbox_error videotoolbox_errors[] = {
|
|
|
|
{ AVERROR(ENOSYS),
|
|
|
|
"Hardware doesn't support accelerated decoding for this stream"
|
|
|
|
" or Videotoolbox decoder is not available at the moment (another"
|
|
|
|
" application is using it)."
|
|
|
|
},
|
|
|
|
{ AVERROR(EINVAL),
|
|
|
|
"Invalid configuration provided to VTDecompressionSessionCreate" },
|
|
|
|
{ AVERROR_INVALIDDATA,
|
|
|
|
"Generic error returned by the decoder layer. The cause can be Videotoolbox"
|
|
|
|
" found errors in the bitstream." },
|
|
|
|
{ 0, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static void print_videotoolbox_error(struct mp_log *log, int lev, char *message,
|
|
|
|
int error_code)
|
|
|
|
{
|
|
|
|
for (int n = 0; videotoolbox_errors[n].code < 0; n++)
|
|
|
|
if (videotoolbox_errors[n].code == error_code) {
|
|
|
|
mp_msg(log, lev, "%s: %s (%d)\n",
|
|
|
|
message, videotoolbox_errors[n].reason, error_code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_msg(log, lev, "%s: %d\n", message, error_code);
|
|
|
|
}
|
|
|
|
|
2015-08-19 19:33:18 +00:00
|
|
|
static int init_decoder(struct lavc_ctx *ctx, int w, int h)
|
2015-07-11 15:21:39 +00:00
|
|
|
{
|
|
|
|
av_videotoolbox_default_free(ctx->avctx);
|
|
|
|
|
|
|
|
AVVideotoolboxContext *vtctx = av_videotoolbox_alloc_context();
|
2015-11-17 20:07:41 +00:00
|
|
|
vtctx->cv_pix_fmt_type = (uintptr_t)ctx->hwdec_info->hwctx->priv;
|
2015-07-11 15:21:39 +00:00
|
|
|
int err = av_videotoolbox_default_init2(ctx->avctx, vtctx);
|
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
print_videotoolbox_error(ctx->log, MSGL_ERR, "failed to init videotoolbox decoder", err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uninit(struct lavc_ctx *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->avctx)
|
|
|
|
av_videotoolbox_default_free(ctx->avctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct vd_lavc_hwdec mp_vd_lavc_videotoolbox = {
|
|
|
|
.type = HWDEC_VIDEOTOOLBOX,
|
|
|
|
.image_format = IMGFMT_VIDEOTOOLBOX,
|
|
|
|
.probe = probe,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.init_decoder = init_decoder,
|
|
|
|
};
|