2010-04-23 16:05:58 +00:00
|
|
|
/*
|
|
|
|
* gstvaapidecoder_ffmpeg.c - FFmpeg-based decoder
|
|
|
|
*
|
2012-01-16 09:41:10 +00:00
|
|
|
* Copyright (C) 2010-2011 Splitted-Desktop Systems
|
2012-01-16 10:03:51 +00:00
|
|
|
* Copyright (C) 2011-2012 Intel Corporation
|
2010-04-23 16:05:58 +00:00
|
|
|
*
|
2010-05-03 07:07:27 +00:00
|
|
|
* This library 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.
|
2010-04-23 16:05:58 +00:00
|
|
|
*
|
2010-05-03 07:07:27 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2010-04-23 16:05:58 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2010-05-03 07:07:27 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2010-04-23 16:05:58 +00:00
|
|
|
*
|
2010-05-03 07:07:27 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA
|
2010-04-23 16:05:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:gstvaapidecoder_ffmpeg
|
|
|
|
* @short_description: FFmpeg-based decoder
|
|
|
|
*/
|
|
|
|
|
2012-01-30 17:12:59 +00:00
|
|
|
#include "sysdeps.h"
|
2010-07-01 11:11:18 +00:00
|
|
|
#ifdef HAVE_LIBAVCODEC_AVCODEC_H
|
|
|
|
# include <libavcodec/avcodec.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FFMPEG_AVCODEC_H
|
|
|
|
# include <ffmpeg/avcodec.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIBAVCODEC_VAAPI_H
|
|
|
|
# include <libavcodec/vaapi.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FFMPEG_VAAPI_H
|
|
|
|
# include <ffmpeg/vaapi.h>
|
|
|
|
#endif
|
2010-04-23 16:05:58 +00:00
|
|
|
#include "gstvaapidecoder_ffmpeg.h"
|
|
|
|
#include "gstvaapidecoder_priv.h"
|
|
|
|
#include "gstvaapidisplay_priv.h"
|
|
|
|
#include "gstvaapiobject_priv.h"
|
|
|
|
|
|
|
|
#define DEBUG 1
|
|
|
|
#include "gstvaapidebug.h"
|
|
|
|
|
|
|
|
G_DEFINE_TYPE(GstVaapiDecoderFfmpeg,
|
|
|
|
gst_vaapi_decoder_ffmpeg,
|
|
|
|
GST_VAAPI_TYPE_DECODER);
|
|
|
|
|
|
|
|
#define GST_VAAPI_DECODER_FFMPEG_GET_PRIVATE(obj) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE((obj), \
|
|
|
|
GST_VAAPI_TYPE_DECODER_FFMPEG, \
|
|
|
|
GstVaapiDecoderFfmpegPrivate))
|
|
|
|
|
|
|
|
typedef struct _GstVaapiContextFfmpeg GstVaapiContextFfmpeg;
|
|
|
|
struct _GstVaapiContextFfmpeg {
|
|
|
|
struct vaapi_context base;
|
2010-04-30 12:04:12 +00:00
|
|
|
GstVaapiProfile profile;
|
|
|
|
GstVaapiEntrypoint entrypoint;
|
2010-04-23 16:05:58 +00:00
|
|
|
GstVaapiDecoderFfmpeg *decoder;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GstVaapiDecoderFfmpegPrivate {
|
2010-05-03 08:32:46 +00:00
|
|
|
GstClockTime in_timestamp; /* timestamp from the demuxer */
|
2010-04-23 16:05:58 +00:00
|
|
|
AVFrame *frame;
|
2010-04-27 15:26:19 +00:00
|
|
|
AVCodecParserContext *pctx;
|
2010-04-23 16:05:58 +00:00
|
|
|
AVCodecContext *avctx;
|
|
|
|
GstVaapiContextFfmpeg *vactx;
|
|
|
|
guint is_constructed : 1;
|
2010-05-03 08:33:23 +00:00
|
|
|
guint is_opened : 1;
|
2010-04-23 16:05:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Converts codec to FFmpeg codec id */
|
|
|
|
static enum CodecID
|
|
|
|
get_codec_id_from_codec(GstVaapiCodec codec)
|
|
|
|
{
|
|
|
|
switch (codec) {
|
|
|
|
case GST_VAAPI_CODEC_MPEG1: return CODEC_ID_MPEG1VIDEO;
|
|
|
|
case GST_VAAPI_CODEC_MPEG2: return CODEC_ID_MPEG2VIDEO;
|
|
|
|
case GST_VAAPI_CODEC_MPEG4: return CODEC_ID_MPEG4;
|
|
|
|
case GST_VAAPI_CODEC_H263: return CODEC_ID_H263;
|
|
|
|
case GST_VAAPI_CODEC_H264: return CODEC_ID_H264;
|
2010-05-03 14:53:18 +00:00
|
|
|
case GST_VAAPI_CODEC_WMV3: return CODEC_ID_WMV3;
|
2010-04-23 16:05:58 +00:00
|
|
|
case GST_VAAPI_CODEC_VC1: return CODEC_ID_VC1;
|
|
|
|
}
|
|
|
|
return CODEC_ID_NONE;
|
|
|
|
}
|
|
|
|
|
2010-04-30 12:04:12 +00:00
|
|
|
/** Converts PixelFormat to entrypoint */
|
|
|
|
static GstVaapiEntrypoint
|
|
|
|
get_entrypoint(enum PixelFormat pix_fmt)
|
|
|
|
{
|
|
|
|
switch (pix_fmt) {
|
|
|
|
case PIX_FMT_VAAPI_VLD: return GST_VAAPI_ENTRYPOINT_VLD;
|
|
|
|
case PIX_FMT_VAAPI_IDCT: return GST_VAAPI_ENTRYPOINT_IDCT;
|
|
|
|
case PIX_FMT_VAAPI_MOCO: return GST_VAAPI_ENTRYPOINT_MOCO;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-23 16:05:58 +00:00
|
|
|
/** Finds a suitable profile from FFmpeg context */
|
|
|
|
static GstVaapiProfile
|
2010-04-30 12:04:12 +00:00
|
|
|
get_profile(AVCodecContext *avctx, GstVaapiEntrypoint entrypoint)
|
2010-04-23 16:05:58 +00:00
|
|
|
{
|
|
|
|
GstVaapiContextFfmpeg * const vactx = avctx->hwaccel_context;
|
|
|
|
GstVaapiDisplay *display;
|
2010-04-30 12:04:12 +00:00
|
|
|
GstVaapiProfile profiles[4];
|
2010-04-23 16:05:58 +00:00
|
|
|
guint i, n_profiles = 0;
|
|
|
|
|
2010-04-30 12:04:12 +00:00
|
|
|
#define ADD_PROFILE(profile) do { \
|
|
|
|
profiles[n_profiles++] = GST_VAAPI_PROFILE_##profile; \
|
2010-04-23 16:05:58 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
switch (avctx->codec_id) {
|
|
|
|
case CODEC_ID_MPEG1VIDEO:
|
|
|
|
ADD_PROFILE(MPEG1);
|
|
|
|
break;
|
|
|
|
case CODEC_ID_MPEG2VIDEO:
|
|
|
|
ADD_PROFILE(MPEG2_MAIN);
|
|
|
|
ADD_PROFILE(MPEG2_SIMPLE);
|
|
|
|
break;
|
|
|
|
case CODEC_ID_H263:
|
|
|
|
ADD_PROFILE(H263_BASELINE);
|
|
|
|
/* fall-through */
|
|
|
|
case CODEC_ID_MPEG4:
|
|
|
|
ADD_PROFILE(MPEG4_MAIN);
|
|
|
|
ADD_PROFILE(MPEG4_ADVANCED_SIMPLE);
|
|
|
|
ADD_PROFILE(MPEG4_SIMPLE);
|
|
|
|
break;
|
|
|
|
case CODEC_ID_H264:
|
|
|
|
if (avctx->profile == 66) /* baseline */
|
|
|
|
ADD_PROFILE(H264_BASELINE);
|
|
|
|
else {
|
|
|
|
if (avctx->profile == 77) /* main */
|
|
|
|
ADD_PROFILE(H264_MAIN);
|
|
|
|
ADD_PROFILE(H264_HIGH);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CODEC_ID_WMV3:
|
|
|
|
if (avctx->profile == 0) /* simple */
|
|
|
|
ADD_PROFILE(VC1_SIMPLE);
|
|
|
|
ADD_PROFILE(VC1_MAIN);
|
|
|
|
break;
|
|
|
|
case CODEC_ID_VC1:
|
|
|
|
ADD_PROFILE(VC1_ADVANCED);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef ADD_PROFILE
|
|
|
|
|
|
|
|
display = GST_VAAPI_DECODER_DISPLAY(vactx->decoder);
|
|
|
|
if (!display)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < n_profiles; i++)
|
2010-04-30 12:04:12 +00:00
|
|
|
if (gst_vaapi_display_has_decoder(display, profiles[i], entrypoint))
|
|
|
|
return profiles[i];
|
2010-04-23 16:05:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-30 12:04:12 +00:00
|
|
|
/** Ensures VA context is correctly set up for the current FFmpeg context */
|
|
|
|
static GstVaapiContext *
|
|
|
|
get_context(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
GstVaapiContextFfmpeg * const vactx = avctx->hwaccel_context;
|
|
|
|
GstVaapiDecoder * const decoder = GST_VAAPI_DECODER(vactx->decoder);
|
|
|
|
GstVaapiDisplay *display;
|
|
|
|
GstVaapiContext *context;
|
|
|
|
gboolean success;
|
|
|
|
|
2010-05-03 17:04:00 +00:00
|
|
|
if (!avctx->coded_width || !avctx->coded_height)
|
2010-04-30 12:04:12 +00:00
|
|
|
return NULL;
|
|
|
|
|
2010-05-15 15:33:20 +00:00
|
|
|
gst_vaapi_decoder_set_framerate(
|
|
|
|
decoder,
|
|
|
|
avctx->time_base.den / avctx->ticks_per_frame,
|
|
|
|
avctx->time_base.num
|
|
|
|
);
|
|
|
|
|
|
|
|
gst_vaapi_decoder_set_pixel_aspect_ratio(
|
|
|
|
decoder,
|
|
|
|
avctx->sample_aspect_ratio.num,
|
|
|
|
avctx->sample_aspect_ratio.den
|
|
|
|
);
|
|
|
|
|
2010-04-30 12:04:12 +00:00
|
|
|
success = gst_vaapi_decoder_ensure_context(
|
|
|
|
decoder,
|
|
|
|
vactx->profile,
|
|
|
|
vactx->entrypoint,
|
2010-05-03 17:04:00 +00:00
|
|
|
avctx->coded_width,
|
|
|
|
avctx->coded_height
|
2010-04-30 12:04:12 +00:00
|
|
|
);
|
|
|
|
if (!success) {
|
|
|
|
GST_DEBUG("failed to reset VA context:");
|
|
|
|
GST_DEBUG(" profile 0x%08x", vactx->profile);
|
|
|
|
GST_DEBUG(" entrypoint %d", vactx->entrypoint);
|
|
|
|
GST_DEBUG(" surface size %dx%d", avctx->width, avctx->height);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
display = GST_VAAPI_DECODER_DISPLAY(decoder);
|
|
|
|
context = GST_VAAPI_DECODER_CONTEXT(decoder);
|
|
|
|
vactx->base.display = GST_VAAPI_DISPLAY_VADISPLAY(display);
|
|
|
|
vactx->base.context_id = GST_VAAPI_OBJECT_ID(context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2010-04-28 22:30:50 +00:00
|
|
|
/** Sets AVCodecContext.extradata with additional codec data */
|
|
|
|
static gboolean
|
|
|
|
set_codec_data(AVCodecContext *avctx, const guchar *buf, guint buf_size)
|
|
|
|
{
|
|
|
|
av_freep(&avctx->extradata);
|
2010-04-30 12:04:12 +00:00
|
|
|
avctx->extradata_size = 0;
|
|
|
|
if (!buf || buf_size < 1)
|
|
|
|
return TRUE;
|
|
|
|
|
2010-04-28 22:30:50 +00:00
|
|
|
avctx->extradata = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!avctx->extradata)
|
|
|
|
return FALSE;
|
|
|
|
avctx->extradata_size = buf_size;
|
|
|
|
memcpy(avctx->extradata, buf, buf_size);
|
|
|
|
memset(avctx->extradata + buf_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-04-23 16:05:58 +00:00
|
|
|
/** AVCodecContext.get_format() implementation */
|
|
|
|
static enum PixelFormat
|
|
|
|
gst_vaapi_decoder_ffmpeg_get_format(AVCodecContext *avctx, const enum PixelFormat *fmt)
|
|
|
|
{
|
|
|
|
GstVaapiContextFfmpeg * const vactx = avctx->hwaccel_context;
|
|
|
|
GstVaapiProfile profile;
|
2010-04-30 12:04:12 +00:00
|
|
|
GstVaapiEntrypoint entrypoint;
|
2010-04-23 16:05:58 +00:00
|
|
|
guint i;
|
|
|
|
|
|
|
|
/* XXX: only VLD entrypoint is supported at this time */
|
2010-04-30 12:04:12 +00:00
|
|
|
for (i = 0; fmt[i] != PIX_FMT_NONE; i++) {
|
|
|
|
entrypoint = get_entrypoint(fmt[i]);
|
|
|
|
if (entrypoint != GST_VAAPI_ENTRYPOINT_VLD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
profile = get_profile(avctx, entrypoint);
|
|
|
|
if (profile) {
|
|
|
|
vactx->profile = profile;
|
|
|
|
vactx->entrypoint = entrypoint;
|
|
|
|
return fmt[i];
|
|
|
|
}
|
2010-04-23 16:05:58 +00:00
|
|
|
}
|
|
|
|
return PIX_FMT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** AVCodecContext.get_buffer() implementation */
|
|
|
|
static int
|
|
|
|
gst_vaapi_decoder_ffmpeg_get_buffer(AVCodecContext *avctx, AVFrame *pic)
|
|
|
|
{
|
2010-05-03 08:32:46 +00:00
|
|
|
GstVaapiContextFfmpeg * const vactx = avctx->hwaccel_context;
|
2010-04-30 12:04:12 +00:00
|
|
|
GstVaapiContext *context;
|
2010-04-23 16:05:58 +00:00
|
|
|
GstVaapiSurface *surface;
|
2012-01-05 16:09:35 +00:00
|
|
|
GstVaapiSurfaceProxy *proxy;
|
2010-04-23 16:05:58 +00:00
|
|
|
GstVaapiID surface_id;
|
|
|
|
|
2010-04-30 12:04:12 +00:00
|
|
|
context = get_context(avctx);
|
|
|
|
if (!context)
|
|
|
|
return -1;
|
|
|
|
|
2010-04-23 16:05:58 +00:00
|
|
|
surface = gst_vaapi_context_get_surface(context);
|
|
|
|
if (!surface) {
|
|
|
|
GST_DEBUG("failed to get a free VA surface");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-05 16:09:35 +00:00
|
|
|
proxy = gst_vaapi_surface_proxy_new(context, surface);
|
|
|
|
if (!proxy) {
|
|
|
|
GST_DEBUG("failed to create proxy surface");
|
|
|
|
gst_vaapi_context_put_surface(context, surface);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-23 16:05:58 +00:00
|
|
|
surface_id = GST_VAAPI_OBJECT_ID(surface);
|
|
|
|
GST_DEBUG("surface %" GST_VAAPI_ID_FORMAT, GST_VAAPI_ID_ARGS(surface_id));
|
|
|
|
|
|
|
|
pic->type = FF_BUFFER_TYPE_USER;
|
|
|
|
pic->age = 1;
|
2012-01-05 16:09:35 +00:00
|
|
|
pic->data[0] = (uint8_t *)proxy;
|
2010-04-23 16:05:58 +00:00
|
|
|
pic->data[1] = NULL;
|
|
|
|
pic->data[2] = NULL;
|
|
|
|
pic->data[3] = (uint8_t *)(uintptr_t)surface_id;
|
|
|
|
pic->linesize[0] = 0;
|
|
|
|
pic->linesize[1] = 0;
|
|
|
|
pic->linesize[2] = 0;
|
|
|
|
pic->linesize[3] = 0;
|
2010-05-03 08:32:46 +00:00
|
|
|
pic->pts = vactx->decoder->priv->in_timestamp;
|
2010-04-23 16:05:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** AVCodecContext.reget_buffer() implementation */
|
|
|
|
static int
|
|
|
|
gst_vaapi_decoder_ffmpeg_reget_buffer(AVCodecContext *avctx, AVFrame *pic)
|
|
|
|
{
|
|
|
|
GST_DEBUG("UNIMPLEMENTED");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** AVCodecContext.release_buffer() implementation */
|
|
|
|
static void
|
|
|
|
gst_vaapi_decoder_ffmpeg_release_buffer(AVCodecContext *avctx, AVFrame *pic)
|
|
|
|
{
|
2012-01-05 16:09:35 +00:00
|
|
|
GstVaapiSurfaceProxy * const proxy = GST_VAAPI_SURFACE_PROXY(pic->data[0]);
|
2010-04-23 16:05:58 +00:00
|
|
|
GstVaapiID surface_id = GST_VAAPI_ID(GPOINTER_TO_UINT(pic->data[3]));
|
|
|
|
|
|
|
|
GST_DEBUG("surface %" GST_VAAPI_ID_FORMAT, GST_VAAPI_ID_ARGS(surface_id));
|
|
|
|
|
2012-01-05 16:09:35 +00:00
|
|
|
g_object_unref(proxy);
|
|
|
|
|
2010-04-23 16:05:58 +00:00
|
|
|
pic->data[0] = NULL;
|
|
|
|
pic->data[1] = NULL;
|
|
|
|
pic->data[2] = NULL;
|
|
|
|
pic->data[3] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-04-28 22:16:10 +00:00
|
|
|
gst_vaapi_decoder_ffmpeg_close(GstVaapiDecoderFfmpeg *ffdecoder)
|
2010-04-23 16:05:58 +00:00
|
|
|
{
|
|
|
|
GstVaapiDecoderFfmpegPrivate * const priv = ffdecoder->priv;
|
|
|
|
|
2010-04-27 15:26:19 +00:00
|
|
|
if (priv->avctx) {
|
2010-05-13 21:39:58 +00:00
|
|
|
if (priv->is_opened) {
|
2010-05-13 21:27:43 +00:00
|
|
|
avcodec_close(priv->avctx);
|
2010-05-13 21:39:58 +00:00
|
|
|
priv->is_opened = FALSE;
|
|
|
|
}
|
2010-04-28 21:50:44 +00:00
|
|
|
av_freep(&priv->avctx->extradata);
|
2010-04-29 23:09:07 +00:00
|
|
|
priv->avctx->extradata_size = 0;
|
2010-04-23 16:05:58 +00:00
|
|
|
}
|
|
|
|
|
2010-04-29 09:40:38 +00:00
|
|
|
if (priv->pctx) {
|
2010-04-27 15:26:19 +00:00
|
|
|
av_parser_close(priv->pctx);
|
2010-04-29 09:40:38 +00:00
|
|
|
priv->pctx = NULL;
|
|
|
|
}
|
2010-04-23 16:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-04-28 22:16:10 +00:00
|
|
|
gst_vaapi_decoder_ffmpeg_open(GstVaapiDecoderFfmpeg *ffdecoder, GstBuffer *buffer)
|
2010-04-23 16:05:58 +00:00
|
|
|
{
|
|
|
|
GstVaapiDecoderFfmpegPrivate * const priv = ffdecoder->priv;
|
2010-04-28 22:16:10 +00:00
|
|
|
GstVaapiDisplay * const display = GST_VAAPI_DECODER_DISPLAY(ffdecoder);
|
2010-04-29 23:09:07 +00:00
|
|
|
GstBuffer * const codec_data = GST_VAAPI_DECODER_CODEC_DATA(ffdecoder);
|
2010-04-28 22:16:10 +00:00
|
|
|
GstVaapiCodec codec = GST_VAAPI_DECODER_CODEC(ffdecoder);
|
2010-04-27 15:26:19 +00:00
|
|
|
enum CodecID codec_id;
|
2010-04-23 16:05:58 +00:00
|
|
|
AVCodec *ffcodec;
|
2010-05-03 16:17:51 +00:00
|
|
|
gboolean try_parser, need_parser;
|
2010-04-28 22:16:10 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
gst_vaapi_decoder_ffmpeg_close(ffdecoder);
|
2010-04-23 16:05:58 +00:00
|
|
|
|
2010-04-29 23:09:07 +00:00
|
|
|
if (codec_data) {
|
|
|
|
const guchar *data = GST_BUFFER_DATA(codec_data);
|
|
|
|
const guint size = GST_BUFFER_SIZE(codec_data);
|
|
|
|
if (!set_codec_data(priv->avctx, data, size))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-04-27 15:26:19 +00:00
|
|
|
codec_id = get_codec_id_from_codec(codec);
|
|
|
|
if (codec_id == CODEC_ID_NONE)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ffcodec = avcodec_find_decoder(codec_id);
|
|
|
|
if (!ffcodec)
|
|
|
|
return FALSE;
|
2010-04-23 16:05:58 +00:00
|
|
|
|
2010-04-30 08:18:07 +00:00
|
|
|
switch (codec_id) {
|
|
|
|
case CODEC_ID_H264:
|
|
|
|
/* For AVC1 formats, sequence headers are in extradata and
|
|
|
|
input encoded buffers represent the whole NAL unit */
|
2010-05-03 16:17:51 +00:00
|
|
|
try_parser = priv->avctx->extradata_size == 0;
|
|
|
|
need_parser = try_parser;
|
2010-04-30 08:18:07 +00:00
|
|
|
break;
|
2010-05-03 14:53:18 +00:00
|
|
|
case CODEC_ID_WMV3:
|
|
|
|
/* There is no WMV3 parser in FFmpeg */
|
2010-05-03 16:17:51 +00:00
|
|
|
try_parser = FALSE;
|
|
|
|
need_parser = FALSE;
|
2010-05-03 14:53:18 +00:00
|
|
|
break;
|
2010-05-03 15:35:22 +00:00
|
|
|
case CODEC_ID_VC1:
|
|
|
|
/* For VC-1, sequence headers ae in extradata and input encoded
|
|
|
|
buffers represent the whole slice */
|
2010-05-03 16:17:51 +00:00
|
|
|
try_parser = priv->avctx->extradata_size == 0;
|
|
|
|
need_parser = FALSE;
|
2010-05-03 15:35:22 +00:00
|
|
|
break;
|
2010-04-30 08:18:07 +00:00
|
|
|
default:
|
2010-05-03 16:17:51 +00:00
|
|
|
try_parser = TRUE;
|
|
|
|
need_parser = TRUE;
|
2010-04-30 08:18:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-03 16:17:51 +00:00
|
|
|
if (try_parser) {
|
2010-04-29 23:09:07 +00:00
|
|
|
priv->pctx = av_parser_init(codec_id);
|
2010-05-03 16:17:51 +00:00
|
|
|
if (!priv->pctx && need_parser)
|
2010-04-28 22:30:50 +00:00
|
|
|
return FALSE;
|
2010-05-03 16:17:51 +00:00
|
|
|
}
|
2010-04-29 23:09:07 +00:00
|
|
|
|
2010-05-03 16:17:51 +00:00
|
|
|
/* XXX: av_find_stream_info() does this and some codecs really
|
|
|
|
want hard an extradata buffer for initialization (e.g. VC-1) */
|
|
|
|
if (!priv->avctx->extradata && priv->pctx && priv->pctx->parser->split) {
|
|
|
|
const guchar *buf = GST_BUFFER_DATA(buffer);
|
|
|
|
guint buf_size = GST_BUFFER_SIZE(buffer);
|
|
|
|
buf_size = priv->pctx->parser->split(priv->avctx, buf, buf_size);
|
|
|
|
if (buf_size > 0 && !set_codec_data(priv->avctx, buf, buf_size))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->pctx && !need_parser) {
|
|
|
|
av_parser_close(priv->pctx);
|
|
|
|
priv->pctx = NULL;
|
2010-04-28 22:30:50 +00:00
|
|
|
}
|
|
|
|
|
2010-05-03 15:29:18 +00:00
|
|
|
/* Use size information from the demuxer, whenever available */
|
|
|
|
priv->avctx->coded_width = GST_VAAPI_DECODER_WIDTH(ffdecoder);
|
|
|
|
priv->avctx->coded_height = GST_VAAPI_DECODER_HEIGHT(ffdecoder);
|
|
|
|
|
2010-04-28 22:16:10 +00:00
|
|
|
GST_VAAPI_DISPLAY_LOCK(display);
|
|
|
|
ret = avcodec_open(priv->avctx, ffcodec);
|
|
|
|
GST_VAAPI_DISPLAY_UNLOCK(display);
|
2010-04-30 12:04:12 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
2010-04-28 22:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_decoder_ffmpeg_destroy(GstVaapiDecoderFfmpeg *ffdecoder)
|
|
|
|
{
|
|
|
|
GstVaapiDecoderFfmpegPrivate * const priv = ffdecoder->priv;
|
|
|
|
|
|
|
|
gst_vaapi_decoder_ffmpeg_close(ffdecoder);
|
|
|
|
|
|
|
|
if (priv->vactx) {
|
|
|
|
g_free(priv->vactx);
|
|
|
|
priv->vactx = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_freep(&priv->avctx);
|
|
|
|
av_freep(&priv->frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_vaapi_decoder_ffmpeg_create(GstVaapiDecoderFfmpeg *ffdecoder)
|
|
|
|
{
|
|
|
|
GstVaapiDecoderFfmpegPrivate * const priv = ffdecoder->priv;
|
|
|
|
|
2010-05-03 21:49:35 +00:00
|
|
|
if (!GST_VAAPI_DECODER_CODEC(ffdecoder))
|
|
|
|
return FALSE;
|
|
|
|
|
2010-04-23 16:05:58 +00:00
|
|
|
if (!priv->frame) {
|
|
|
|
priv->frame = avcodec_alloc_frame();
|
|
|
|
if (!priv->frame)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-04-27 15:26:19 +00:00
|
|
|
if (!priv->avctx) {
|
|
|
|
priv->avctx = avcodec_alloc_context();
|
|
|
|
if (!priv->avctx)
|
2010-04-23 16:05:58 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-04-27 15:26:19 +00:00
|
|
|
if (!priv->vactx) {
|
|
|
|
priv->vactx = g_new(GstVaapiContextFfmpeg, 1);
|
|
|
|
if (!priv->vactx)
|
|
|
|
return FALSE;
|
2010-04-23 16:05:58 +00:00
|
|
|
}
|
2010-04-27 15:26:19 +00:00
|
|
|
memset(&priv->vactx->base, 0, sizeof(priv->vactx->base));
|
|
|
|
priv->vactx->decoder = ffdecoder;
|
2010-04-23 16:05:58 +00:00
|
|
|
|
|
|
|
priv->avctx->hwaccel_context = priv->vactx;
|
|
|
|
priv->avctx->get_format = gst_vaapi_decoder_ffmpeg_get_format;
|
|
|
|
priv->avctx->get_buffer = gst_vaapi_decoder_ffmpeg_get_buffer;
|
|
|
|
priv->avctx->reget_buffer = gst_vaapi_decoder_ffmpeg_reget_buffer;
|
|
|
|
priv->avctx->release_buffer = gst_vaapi_decoder_ffmpeg_release_buffer;
|
|
|
|
priv->avctx->thread_count = 1;
|
|
|
|
priv->avctx->draw_horiz_band = NULL;
|
|
|
|
priv->avctx->slice_flags = SLICE_FLAG_CODED_ORDER|SLICE_FLAG_ALLOW_FIELD;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-01-26 13:54:31 +00:00
|
|
|
static GstVaapiDecoderStatus
|
|
|
|
render_frame(GstVaapiDecoderFfmpeg *decoder, AVFrame *frame)
|
|
|
|
{
|
|
|
|
GstVaapiDecoder * const base_decoder = GST_VAAPI_DECODER(decoder);
|
|
|
|
GstVaapiSurfaceProxy *proxy;
|
|
|
|
|
|
|
|
proxy = GST_VAAPI_SURFACE_PROXY(frame->data[0]);
|
|
|
|
if (!proxy)
|
|
|
|
return GST_VAAPI_DECODER_STATUS_ERROR_INVALID_SURFACE;
|
|
|
|
|
|
|
|
gst_vaapi_surface_proxy_set_timestamp(proxy, frame->pts);
|
|
|
|
gst_vaapi_decoder_push_surface_proxy(base_decoder, g_object_ref(proxy));
|
|
|
|
return GST_VAAPI_DECODER_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2010-04-27 15:26:19 +00:00
|
|
|
static GstVaapiDecoderStatus
|
2010-04-23 16:05:58 +00:00
|
|
|
decode_frame(GstVaapiDecoderFfmpeg *ffdecoder, guchar *buf, guint buf_size)
|
|
|
|
{
|
|
|
|
GstVaapiDecoderFfmpegPrivate * const priv = ffdecoder->priv;
|
|
|
|
GstVaapiDisplay * const display = GST_VAAPI_DECODER_DISPLAY(ffdecoder);
|
2010-04-26 08:53:18 +00:00
|
|
|
int bytes_read, got_picture = 0;
|
2011-07-22 13:55:47 +00:00
|
|
|
AVPacket pkt;
|
|
|
|
|
|
|
|
av_init_packet(&pkt);
|
|
|
|
pkt.data = buf;
|
|
|
|
pkt.size = buf_size;
|
2010-04-23 16:05:58 +00:00
|
|
|
|
|
|
|
GST_VAAPI_DISPLAY_LOCK(display);
|
2011-07-22 13:55:47 +00:00
|
|
|
bytes_read = avcodec_decode_video2(
|
2010-04-23 16:05:58 +00:00
|
|
|
priv->avctx,
|
|
|
|
priv->frame,
|
|
|
|
&got_picture,
|
2011-07-22 13:55:47 +00:00
|
|
|
&pkt
|
2010-04-23 16:05:58 +00:00
|
|
|
);
|
|
|
|
GST_VAAPI_DISPLAY_UNLOCK(display);
|
2010-04-27 15:26:19 +00:00
|
|
|
if (!got_picture)
|
|
|
|
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
|
2010-06-14 14:14:42 +00:00
|
|
|
if (bytes_read < 0)
|
|
|
|
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
|
2010-04-27 15:26:19 +00:00
|
|
|
|
2012-01-26 13:54:31 +00:00
|
|
|
return render_frame(ffdecoder, priv->frame);
|
2010-04-23 16:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstVaapiDecoderStatus
|
2010-04-27 15:26:19 +00:00
|
|
|
gst_vaapi_decoder_ffmpeg_decode(GstVaapiDecoder *decoder, GstBuffer *buffer)
|
2010-04-23 16:05:58 +00:00
|
|
|
{
|
|
|
|
GstVaapiDecoderFfmpeg * const ffdecoder = GST_VAAPI_DECODER_FFMPEG(decoder);
|
|
|
|
GstVaapiDecoderFfmpegPrivate * const priv = ffdecoder->priv;
|
2010-04-27 15:26:19 +00:00
|
|
|
GstClockTime inbuf_ts;
|
2010-05-15 09:43:28 +00:00
|
|
|
guchar *outbuf;
|
|
|
|
gint inbuf_ofs, inbuf_size, outbuf_size;
|
2010-04-27 15:26:19 +00:00
|
|
|
gboolean got_frame;
|
2010-04-23 16:05:58 +00:00
|
|
|
|
2010-04-28 21:58:58 +00:00
|
|
|
g_return_val_if_fail(priv->is_constructed,
|
|
|
|
GST_VAAPI_DECODER_STATUS_ERROR_INIT_FAILED);
|
2010-04-23 16:05:58 +00:00
|
|
|
|
2010-05-03 08:33:23 +00:00
|
|
|
if (!priv->is_opened) {
|
|
|
|
priv->is_opened = gst_vaapi_decoder_ffmpeg_open(ffdecoder, buffer);
|
|
|
|
if (!priv->is_opened)
|
2010-04-28 22:16:10 +00:00
|
|
|
return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CODEC;
|
|
|
|
}
|
|
|
|
|
2010-05-15 09:43:28 +00:00
|
|
|
inbuf_ofs = 0;
|
2010-04-27 15:26:19 +00:00
|
|
|
inbuf_size = GST_BUFFER_SIZE(buffer);
|
|
|
|
inbuf_ts = GST_BUFFER_TIMESTAMP(buffer);
|
|
|
|
|
2010-04-29 23:09:07 +00:00
|
|
|
if (priv->pctx) {
|
|
|
|
do {
|
2011-07-22 13:55:47 +00:00
|
|
|
int parsed_size;
|
|
|
|
parsed_size = av_parser_parse2(
|
2010-04-29 23:09:07 +00:00
|
|
|
priv->pctx,
|
|
|
|
priv->avctx,
|
|
|
|
&outbuf, &outbuf_size,
|
2010-05-15 09:43:28 +00:00
|
|
|
GST_BUFFER_DATA(buffer) + inbuf_ofs, inbuf_size,
|
2011-07-22 13:55:47 +00:00
|
|
|
inbuf_ts, inbuf_ts,
|
|
|
|
AV_NOPTS_VALUE
|
2010-04-29 23:09:07 +00:00
|
|
|
);
|
|
|
|
got_frame = outbuf && outbuf_size > 0;
|
|
|
|
|
|
|
|
if (parsed_size > 0) {
|
2010-05-15 09:43:28 +00:00
|
|
|
inbuf_ofs += parsed_size;
|
2010-04-29 23:09:07 +00:00
|
|
|
inbuf_size -= parsed_size;
|
|
|
|
}
|
|
|
|
} while (!got_frame && inbuf_size > 0);
|
2010-05-03 08:32:46 +00:00
|
|
|
inbuf_ts = priv->pctx->pts;
|
2010-04-29 23:09:07 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-05-15 09:43:28 +00:00
|
|
|
outbuf = GST_BUFFER_DATA(buffer);
|
2010-04-29 23:09:07 +00:00
|
|
|
outbuf_size = inbuf_size;
|
2010-05-15 09:43:28 +00:00
|
|
|
got_frame = outbuf && outbuf_size > 0;
|
|
|
|
inbuf_ofs = inbuf_size;
|
|
|
|
inbuf_size = 0;
|
2010-04-29 23:09:07 +00:00
|
|
|
}
|
2010-04-23 16:05:58 +00:00
|
|
|
|
2010-05-15 09:43:28 +00:00
|
|
|
if (inbuf_size > 0 &&
|
|
|
|
!gst_vaapi_decoder_push_buffer_sub(decoder, buffer,
|
|
|
|
inbuf_ofs, inbuf_size))
|
|
|
|
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
|
|
|
|
|
2010-04-27 15:26:19 +00:00
|
|
|
if (!got_frame && !GST_BUFFER_IS_EOS(buffer))
|
|
|
|
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
|
|
|
|
|
2010-05-03 08:32:46 +00:00
|
|
|
priv->in_timestamp = inbuf_ts;
|
2010-04-27 15:26:19 +00:00
|
|
|
return decode_frame(ffdecoder, outbuf, outbuf_size);
|
2010-04-23 16:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_decoder_ffmpeg_finalize(GObject *object)
|
|
|
|
{
|
|
|
|
GstVaapiDecoderFfmpeg * const ffdecoder = GST_VAAPI_DECODER_FFMPEG(object);
|
|
|
|
|
|
|
|
gst_vaapi_decoder_ffmpeg_destroy(ffdecoder);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS(gst_vaapi_decoder_ffmpeg_parent_class)->finalize(object);
|
|
|
|
}
|
|
|
|
|
2010-04-28 21:58:58 +00:00
|
|
|
static void
|
|
|
|
gst_vaapi_decoder_ffmpeg_constructed(GObject *object)
|
|
|
|
{
|
|
|
|
GstVaapiDecoderFfmpeg * const ffdecoder = GST_VAAPI_DECODER_FFMPEG(object);
|
|
|
|
GstVaapiDecoderFfmpegPrivate * const priv = ffdecoder->priv;
|
|
|
|
GObjectClass *parent_class;
|
|
|
|
|
|
|
|
parent_class = G_OBJECT_CLASS(gst_vaapi_decoder_ffmpeg_parent_class);
|
|
|
|
if (parent_class->constructed)
|
|
|
|
parent_class->constructed(object);
|
|
|
|
|
|
|
|
priv->is_constructed = gst_vaapi_decoder_ffmpeg_create(ffdecoder);
|
|
|
|
}
|
|
|
|
|
2010-04-23 16:05:58 +00:00
|
|
|
static void
|
|
|
|
gst_vaapi_decoder_ffmpeg_class_init(GstVaapiDecoderFfmpegClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass * const object_class = G_OBJECT_CLASS(klass);
|
|
|
|
GstVaapiDecoderClass * const decoder_class = GST_VAAPI_DECODER_CLASS(klass);
|
|
|
|
|
|
|
|
g_type_class_add_private(klass, sizeof(GstVaapiDecoderFfmpegPrivate));
|
|
|
|
|
|
|
|
object_class->finalize = gst_vaapi_decoder_ffmpeg_finalize;
|
2010-04-28 21:58:58 +00:00
|
|
|
object_class->constructed = gst_vaapi_decoder_ffmpeg_constructed;
|
2010-04-23 16:05:58 +00:00
|
|
|
|
|
|
|
decoder_class->decode = gst_vaapi_decoder_ffmpeg_decode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gst_vaapi_decoder_ffmpeg_init_once_cb(gpointer user_data)
|
|
|
|
{
|
2010-07-01 11:11:18 +00:00
|
|
|
avcodec_register_all();
|
2010-04-23 16:05:58 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
gst_vaapi_decoder_ffmpeg_init_once(void)
|
|
|
|
{
|
|
|
|
static GOnce once = G_ONCE_INIT;
|
|
|
|
|
|
|
|
g_once(&once, gst_vaapi_decoder_ffmpeg_init_once_cb, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_vaapi_decoder_ffmpeg_init(GstVaapiDecoderFfmpeg *decoder)
|
|
|
|
{
|
|
|
|
GstVaapiDecoderFfmpegPrivate *priv;
|
|
|
|
|
|
|
|
gst_vaapi_decoder_ffmpeg_init_once();
|
|
|
|
|
|
|
|
priv = GST_VAAPI_DECODER_FFMPEG_GET_PRIVATE(decoder);
|
|
|
|
decoder->priv = priv;
|
|
|
|
priv->frame = NULL;
|
2010-04-27 15:26:19 +00:00
|
|
|
priv->pctx = NULL;
|
2010-04-23 16:05:58 +00:00
|
|
|
priv->avctx = NULL;
|
|
|
|
priv->vactx = NULL;
|
|
|
|
priv->is_constructed = FALSE;
|
2010-05-03 08:33:23 +00:00
|
|
|
priv->is_opened = FALSE;
|
2010-04-23 16:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_vaapi_decoder_ffmpeg_new:
|
|
|
|
* @display: a #GstVaapiDisplay
|
2010-05-03 15:11:32 +00:00
|
|
|
* @caps: a #GstCaps holding codec information
|
|
|
|
*
|
2010-05-03 21:25:26 +00:00
|
|
|
* Creates a new #GstVaapiDecoder based on FFmpeg where the codec is
|
|
|
|
* determined from @caps. The @caps can hold extra information like
|
|
|
|
* codec-data and pictured coded size.
|
2010-05-03 15:11:32 +00:00
|
|
|
*
|
|
|
|
* Return value: the newly allocated #GstVaapiDecoder object
|
|
|
|
*/
|
|
|
|
GstVaapiDecoder *
|
2010-05-03 22:28:02 +00:00
|
|
|
gst_vaapi_decoder_ffmpeg_new(GstVaapiDisplay *display, GstCaps *caps)
|
2010-05-03 15:11:32 +00:00
|
|
|
{
|
2010-05-03 21:49:35 +00:00
|
|
|
GstVaapiDecoderFfmpeg *ffdecoder;
|
2010-05-03 15:11:32 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail(GST_VAAPI_IS_DISPLAY(display), NULL);
|
|
|
|
g_return_val_if_fail(GST_IS_CAPS(caps), NULL);
|
|
|
|
|
2010-05-03 21:49:35 +00:00
|
|
|
ffdecoder = g_object_new(
|
|
|
|
GST_VAAPI_TYPE_DECODER_FFMPEG,
|
|
|
|
"display", display,
|
|
|
|
"caps", caps,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
if (!ffdecoder->priv->is_constructed) {
|
|
|
|
g_object_unref(ffdecoder);
|
2010-05-03 15:11:32 +00:00
|
|
|
return NULL;
|
2010-05-03 21:49:35 +00:00
|
|
|
}
|
|
|
|
return GST_VAAPI_DECODER_CAST(ffdecoder);
|
2010-05-03 15:11:32 +00:00
|
|
|
}
|