2011-03-10 06:09:00 +00:00
|
|
|
/*
|
|
|
|
* - CrystalHD decoder module -
|
|
|
|
*
|
2011-03-26 05:50:08 +00:00
|
|
|
* Copyright(C) 2010,2011 Philip Langdale <ffmpeg.philipl@overt.org>
|
2011-03-10 06:09:00 +00:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg 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.
|
|
|
|
*
|
|
|
|
* FFmpeg 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 FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* - Principles of Operation -
|
|
|
|
*
|
|
|
|
* The CrystalHD decoder operates at the bitstream level - which is an even
|
|
|
|
* higher level than the decoding hardware you typically see in modern GPUs.
|
|
|
|
* This means it has a very simple interface, in principle. You feed demuxed
|
|
|
|
* packets in one end and get decoded picture (fields/frames) out the other.
|
|
|
|
*
|
|
|
|
* Of course, nothing is ever that simple. Due, at the very least, to b-frame
|
|
|
|
* dependencies in the supported formats, the hardware has a delay between
|
|
|
|
* when a packet goes in, and when a picture comes out. Furthermore, this delay
|
|
|
|
* is not just a function of time, but also one of the dependency on additional
|
|
|
|
* frames being fed into the decoder to satisfy the b-frame dependencies.
|
|
|
|
*
|
|
|
|
* As such, a pipeline will build up that is roughly equivalent to the required
|
|
|
|
* DPB for the file being played. If that was all it took, things would still
|
|
|
|
* be simple - so, of course, it isn't.
|
|
|
|
*
|
|
|
|
* The hardware has a way of indicating that a picture is ready to be copied out,
|
|
|
|
* but this is unreliable - and sometimes the attempt will still fail so, based
|
|
|
|
* on testing, the code will wait until 3 pictures are ready before starting
|
|
|
|
* to copy out - and this has the effect of extending the pipeline.
|
|
|
|
*
|
2012-06-28 20:01:06 +00:00
|
|
|
* Finally, while it is tempting to say that once the decoder starts outputting
|
2011-03-10 06:09:00 +00:00
|
|
|
* frames, the software should never fail to return a frame from a decode(),
|
|
|
|
* this is a hard assertion to make, because the stream may switch between
|
|
|
|
* differently encoded content (number of b-frames, interlacing, etc) which
|
|
|
|
* might require a longer pipeline than before. If that happened, you could
|
|
|
|
* deadlock trying to retrieve a frame that can't be decoded without feeding
|
|
|
|
* in additional packets.
|
|
|
|
*
|
|
|
|
* As such, the code will return in the event that a picture cannot be copied
|
|
|
|
* out, leading to an increase in the length of the pipeline. This in turn,
|
|
|
|
* means we have to be sensitive to the time it takes to decode a picture;
|
|
|
|
* We do not want to give up just because the hardware needed a little more
|
|
|
|
* time to prepare the picture! For this reason, there are delays included
|
|
|
|
* in the decode() path that ensure that, under normal conditions, the hardware
|
|
|
|
* will only fail to return a frame if it really needs additional packets to
|
|
|
|
* complete the decoding.
|
|
|
|
*
|
|
|
|
* Finally, to be explicit, we do not want the pipeline to grow without bound
|
|
|
|
* for two reasons: 1) The hardware can only buffer a finite number of packets,
|
|
|
|
* and 2) The client application may not be able to cope with arbitrarily long
|
|
|
|
* delays in the video path relative to the audio path. For example. MPlayer
|
|
|
|
* can only handle a 20 picture delay (although this is arbitrary, and needs
|
|
|
|
* to be extended to fully support the CrystalHD where the delay could be up
|
|
|
|
* to 32 pictures - consider PAFF H.264 content with 16 b-frames).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Includes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <libcrystalhd/bc_dts_types.h>
|
|
|
|
#include <libcrystalhd/bc_dts_defs.h>
|
|
|
|
#include <libcrystalhd/libcrystalhd_if.h>
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
2016-07-29 09:01:36 +00:00
|
|
|
#include "h264dec.h"
|
2012-12-05 14:18:12 +00:00
|
|
|
#include "internal.h"
|
2011-03-10 06:09:00 +00:00
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-04-21 04:41:43 +00:00
|
|
|
#include "libavutil/opt.h"
|
2011-03-10 06:09:00 +00:00
|
|
|
|
2014-07-08 00:48:51 +00:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-03-15 05:14:23 +00:00
|
|
|
/** Timeout parameter passed to DtsProcOutput() in us */
|
2011-03-10 06:09:00 +00:00
|
|
|
#define OUTPUT_PROC_TIMEOUT 50
|
2011-03-15 05:14:23 +00:00
|
|
|
/** Step between fake timestamps passed to hardware in units of 100ns */
|
2011-03-10 06:09:00 +00:00
|
|
|
#define TIMESTAMP_UNIT 100000
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Module private data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
typedef enum {
|
2011-03-27 16:37:49 +00:00
|
|
|
RET_ERROR = -1,
|
|
|
|
RET_OK = 0,
|
2011-03-10 06:09:00 +00:00
|
|
|
} CopyRet;
|
|
|
|
|
|
|
|
typedef struct OpaqueList {
|
|
|
|
struct OpaqueList *next;
|
|
|
|
uint64_t fake_timestamp;
|
|
|
|
uint64_t reordered_opaque;
|
2011-03-26 05:48:41 +00:00
|
|
|
uint8_t pic_type;
|
2011-03-10 06:09:00 +00:00
|
|
|
} OpaqueList;
|
|
|
|
|
|
|
|
typedef struct {
|
2011-04-21 04:41:43 +00:00
|
|
|
AVClass *av_class;
|
2011-03-10 06:09:00 +00:00
|
|
|
AVCodecContext *avctx;
|
2013-03-13 04:53:22 +00:00
|
|
|
AVFrame *pic;
|
2011-03-10 06:09:00 +00:00
|
|
|
HANDLE dev;
|
|
|
|
|
2012-01-22 21:47:00 +00:00
|
|
|
uint8_t *orig_extradata;
|
|
|
|
uint32_t orig_extradata_size;
|
|
|
|
|
2016-09-18 19:43:25 +00:00
|
|
|
AVBSFContext *bsfc;
|
2011-03-26 05:46:22 +00:00
|
|
|
AVCodecParserContext *parser;
|
|
|
|
|
2011-03-10 06:09:00 +00:00
|
|
|
uint8_t is_70012;
|
|
|
|
uint8_t *sps_pps_buf;
|
|
|
|
uint32_t sps_pps_size;
|
|
|
|
uint8_t is_nal;
|
|
|
|
uint8_t need_second_field;
|
|
|
|
|
|
|
|
OpaqueList *head;
|
|
|
|
OpaqueList *tail;
|
2011-04-21 04:41:43 +00:00
|
|
|
|
|
|
|
/* Options */
|
|
|
|
uint32_t sWidth;
|
2016-10-15 22:50:29 +00:00
|
|
|
uint8_t bframe_bug;
|
2011-03-10 06:09:00 +00:00
|
|
|
} CHDContext;
|
|
|
|
|
2011-04-21 04:41:43 +00:00
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "crystalhd_downscale_width",
|
|
|
|
"Turn on downscaling to the specified width",
|
|
|
|
offsetof(CHDContext, sWidth),
|
2012-11-03 19:21:57 +00:00
|
|
|
AV_OPT_TYPE_INT, {.i64 = 0}, 0, UINT32_MAX,
|
2011-04-21 04:41:43 +00:00
|
|
|
AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM, },
|
|
|
|
{ NULL, },
|
|
|
|
};
|
|
|
|
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Helper functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-08-07 21:57:21 +00:00
|
|
|
static inline BC_MEDIA_SUBTYPE id2subtype(CHDContext *priv, enum AVCodecID id)
|
2011-03-10 06:09:00 +00:00
|
|
|
{
|
|
|
|
switch (id) {
|
2012-08-07 20:45:46 +00:00
|
|
|
case AV_CODEC_ID_MPEG4:
|
2011-03-10 06:09:00 +00:00
|
|
|
return BC_MSUBTYPE_DIVX;
|
2012-08-07 20:45:46 +00:00
|
|
|
case AV_CODEC_ID_MSMPEG4V3:
|
2011-03-10 06:09:00 +00:00
|
|
|
return BC_MSUBTYPE_DIVX311;
|
2012-08-07 20:45:46 +00:00
|
|
|
case AV_CODEC_ID_MPEG2VIDEO:
|
2011-03-10 06:09:00 +00:00
|
|
|
return BC_MSUBTYPE_MPEG2VIDEO;
|
2012-08-07 20:45:46 +00:00
|
|
|
case AV_CODEC_ID_VC1:
|
2011-03-10 06:09:00 +00:00
|
|
|
return BC_MSUBTYPE_VC1;
|
2012-08-07 20:45:46 +00:00
|
|
|
case AV_CODEC_ID_WMV3:
|
2011-03-10 06:09:00 +00:00
|
|
|
return BC_MSUBTYPE_WMV3;
|
2012-08-07 20:45:46 +00:00
|
|
|
case AV_CODEC_ID_H264:
|
2011-03-10 06:09:00 +00:00
|
|
|
return priv->is_nal ? BC_MSUBTYPE_AVC1 : BC_MSUBTYPE_H264;
|
|
|
|
default:
|
|
|
|
return BC_MSUBTYPE_INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void print_frame_info(CHDContext *priv, BC_DTS_PROC_OUT *output)
|
|
|
|
{
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tYBuffSz: %u\n", output->YbuffSz);
|
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tYBuffDoneSz: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->YBuffDoneSz);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tUVBuffDoneSz: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->UVBuffDoneSz);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tTimestamp: %"PRIu64"\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.timeStamp);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tPicture Number: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.picture_number);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tWidth: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.width);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tHeight: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.height);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tChroma: 0x%03x\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.chroma_format);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tPulldown: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.pulldown);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tFlags: 0x%08x\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.flags);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tFrame Rate/Res: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.frame_rate);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tAspect Ratio: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.aspect_ratio);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tColor Primaries: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.colour_primaries);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tMetaData: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.picture_meta_payload);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tSession Number: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.sess_num);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tycom: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.ycom);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tCustom Aspect: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.custom_aspect_ratio_width_height);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tFrames to Drop: %u\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.n_drop);
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(priv->avctx, AV_LOG_TRACE, "\tH264 Valid Fields: 0x%08x\n",
|
2011-03-10 06:09:00 +00:00
|
|
|
output->PicInfo.other.h264.valid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* OpaqueList functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-26 05:48:41 +00:00
|
|
|
static uint64_t opaque_list_push(CHDContext *priv, uint64_t reordered_opaque,
|
|
|
|
uint8_t pic_type)
|
2011-03-10 06:09:00 +00:00
|
|
|
{
|
|
|
|
OpaqueList *newNode = av_mallocz(sizeof (OpaqueList));
|
|
|
|
if (!newNode) {
|
|
|
|
av_log(priv->avctx, AV_LOG_ERROR,
|
|
|
|
"Unable to allocate new node in OpaqueList.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!priv->head) {
|
|
|
|
newNode->fake_timestamp = TIMESTAMP_UNIT;
|
|
|
|
priv->head = newNode;
|
|
|
|
} else {
|
|
|
|
newNode->fake_timestamp = priv->tail->fake_timestamp + TIMESTAMP_UNIT;
|
|
|
|
priv->tail->next = newNode;
|
|
|
|
}
|
|
|
|
priv->tail = newNode;
|
|
|
|
newNode->reordered_opaque = reordered_opaque;
|
2011-03-26 05:48:41 +00:00
|
|
|
newNode->pic_type = pic_type;
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
return newNode->fake_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The OpaqueList is built in decode order, while elements will be removed
|
|
|
|
* in presentation order. If frames are reordered, this means we must be
|
|
|
|
* able to remove elements that are not the first element.
|
2011-03-26 05:43:53 +00:00
|
|
|
*
|
|
|
|
* Returned node must be freed by caller.
|
2011-03-10 06:09:00 +00:00
|
|
|
*/
|
2011-03-26 05:43:53 +00:00
|
|
|
static OpaqueList *opaque_list_pop(CHDContext *priv, uint64_t fake_timestamp)
|
2011-03-10 06:09:00 +00:00
|
|
|
{
|
|
|
|
OpaqueList *node = priv->head;
|
|
|
|
|
|
|
|
if (!priv->head) {
|
|
|
|
av_log(priv->avctx, AV_LOG_ERROR,
|
|
|
|
"CrystalHD: Attempted to query non-existent timestamps.\n");
|
2011-03-26 05:43:53 +00:00
|
|
|
return NULL;
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The first element is special-cased because we have to manipulate
|
|
|
|
* the head pointer rather than the previous element in the list.
|
|
|
|
*/
|
|
|
|
if (priv->head->fake_timestamp == fake_timestamp) {
|
|
|
|
priv->head = node->next;
|
|
|
|
|
|
|
|
if (!priv->head->next)
|
|
|
|
priv->tail = priv->head;
|
|
|
|
|
2011-03-26 05:43:53 +00:00
|
|
|
node->next = NULL;
|
|
|
|
return node;
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The list is processed at arm's length so that we have the
|
|
|
|
* previous element available to rewrite its next pointer.
|
|
|
|
*/
|
|
|
|
while (node->next) {
|
2011-03-26 05:43:53 +00:00
|
|
|
OpaqueList *current = node->next;
|
|
|
|
if (current->fake_timestamp == fake_timestamp) {
|
|
|
|
node->next = current->next;
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
if (!node->next)
|
|
|
|
priv->tail = node;
|
|
|
|
|
2011-03-26 05:43:53 +00:00
|
|
|
current->next = NULL;
|
|
|
|
return current;
|
2011-03-10 06:09:00 +00:00
|
|
|
} else {
|
2011-03-26 05:43:53 +00:00
|
|
|
node = current;
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
av_log(priv->avctx, AV_LOG_VERBOSE,
|
|
|
|
"CrystalHD: Couldn't match fake_timestamp.\n");
|
2011-03-26 05:43:53 +00:00
|
|
|
return NULL;
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Video decoder API function definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void flush(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
CHDContext *priv = avctx->priv_data;
|
|
|
|
|
|
|
|
priv->need_second_field = 0;
|
|
|
|
|
2013-03-13 04:53:22 +00:00
|
|
|
av_frame_unref (priv->pic);
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
/* Flush mode 4 flushes all software and hardware buffers. */
|
|
|
|
DtsFlushInput(priv->dev, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static av_cold int uninit(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
CHDContext *priv = avctx->priv_data;
|
|
|
|
HANDLE device;
|
|
|
|
|
|
|
|
device = priv->dev;
|
|
|
|
DtsStopDecoder(device);
|
|
|
|
DtsCloseDecoder(device);
|
|
|
|
DtsDeviceClose(device);
|
|
|
|
|
2012-01-22 21:47:00 +00:00
|
|
|
/*
|
|
|
|
* Restore original extradata, so that if the decoder is
|
|
|
|
* reinitialised, the bitstream detection and filtering
|
|
|
|
* will work as expected.
|
|
|
|
*/
|
|
|
|
if (priv->orig_extradata) {
|
|
|
|
av_free(avctx->extradata);
|
|
|
|
avctx->extradata = priv->orig_extradata;
|
|
|
|
avctx->extradata_size = priv->orig_extradata_size;
|
|
|
|
priv->orig_extradata = NULL;
|
|
|
|
priv->orig_extradata_size = 0;
|
|
|
|
}
|
|
|
|
|
2011-03-26 05:46:22 +00:00
|
|
|
av_parser_close(priv->parser);
|
2011-06-13 03:21:28 +00:00
|
|
|
if (priv->bsfc) {
|
2016-09-18 19:43:25 +00:00
|
|
|
av_bsf_free(&priv->bsfc);
|
2011-06-13 03:21:28 +00:00
|
|
|
}
|
2011-03-26 05:46:22 +00:00
|
|
|
|
2014-10-30 22:35:32 +00:00
|
|
|
av_freep(&priv->sps_pps_buf);
|
2011-03-10 06:09:00 +00:00
|
|
|
|
2013-03-13 04:53:22 +00:00
|
|
|
av_frame_free (&priv->pic);
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
if (priv->head) {
|
|
|
|
OpaqueList *node = priv->head;
|
|
|
|
while (node) {
|
|
|
|
OpaqueList *next = node->next;
|
|
|
|
av_free(node);
|
|
|
|
node = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-09 03:34:00 +00:00
|
|
|
static av_cold int init_bsf(AVCodecContext *avctx, const char *bsf_name)
|
|
|
|
{
|
|
|
|
CHDContext *priv = avctx->priv_data;
|
|
|
|
const AVBitStreamFilter *bsf;
|
|
|
|
int avret;
|
|
|
|
void *extradata = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
bsf = av_bsf_get_by_name(bsf_name);
|
|
|
|
if (!bsf) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Cannot open the %s BSF!\n", bsf_name);
|
|
|
|
return AVERROR_BSF_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
avret = av_bsf_alloc(bsf, &priv->bsfc);
|
|
|
|
if (avret != 0) {
|
|
|
|
return avret;
|
|
|
|
}
|
|
|
|
|
|
|
|
avret = avcodec_parameters_from_context(priv->bsfc->par_in, avctx);
|
|
|
|
if (avret != 0) {
|
|
|
|
return avret;
|
|
|
|
}
|
|
|
|
|
|
|
|
avret = av_bsf_init(priv->bsfc);
|
|
|
|
if (avret != 0) {
|
|
|
|
return avret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Back up the extradata so it can be restored at close time. */
|
|
|
|
priv->orig_extradata = avctx->extradata;
|
|
|
|
priv->orig_extradata_size = avctx->extradata_size;
|
|
|
|
|
|
|
|
size = priv->bsfc->par_out->extradata_size;
|
|
|
|
extradata = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!extradata) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Failed to allocate copy of extradata\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
memcpy(extradata, priv->bsfc->par_out->extradata, size);
|
|
|
|
|
|
|
|
avctx->extradata = extradata;
|
|
|
|
avctx->extradata_size = size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-10 06:09:00 +00:00
|
|
|
static av_cold int init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
CHDContext* priv;
|
2016-10-09 03:34:00 +00:00
|
|
|
int avret;
|
2011-03-10 06:09:00 +00:00
|
|
|
BC_STATUS ret;
|
|
|
|
BC_INFO_CRYSTAL version;
|
|
|
|
BC_INPUT_FORMAT format = {
|
|
|
|
.FGTEnable = FALSE,
|
|
|
|
.Progressive = TRUE,
|
|
|
|
.OptFlags = 0x80000000 | vdecFrameRate59_94 | 0x40,
|
|
|
|
.width = avctx->width,
|
|
|
|
.height = avctx->height,
|
|
|
|
};
|
|
|
|
|
|
|
|
BC_MEDIA_SUBTYPE subtype;
|
|
|
|
|
|
|
|
uint32_t mode = DTS_PLAYBACK_MODE |
|
|
|
|
DTS_LOAD_FILE_PLAY_FW |
|
|
|
|
DTS_SKIP_TX_CHK_CPB |
|
|
|
|
DTS_PLAYBACK_DROP_RPT_MODE |
|
|
|
|
DTS_SINGLE_THREADED_MODE |
|
|
|
|
DTS_DFLT_RESOLUTION(vdecRESOLUTION_1080p23_976);
|
|
|
|
|
|
|
|
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD Init for %s\n",
|
|
|
|
avctx->codec->name);
|
|
|
|
|
2012-10-08 18:54:00 +00:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_YUYV422;
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
/* Initialize the library */
|
|
|
|
priv = avctx->priv_data;
|
|
|
|
priv->avctx = avctx;
|
|
|
|
priv->is_nal = avctx->extradata_size > 0 && *(avctx->extradata) == 1;
|
2013-03-13 04:53:22 +00:00
|
|
|
priv->pic = av_frame_alloc();
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
subtype = id2subtype(priv, avctx->codec->id);
|
|
|
|
switch (subtype) {
|
|
|
|
case BC_MSUBTYPE_AVC1:
|
2016-10-09 03:34:00 +00:00
|
|
|
avret = init_bsf(avctx, "h264_mp4toannexb");
|
|
|
|
if (avret != 0) {
|
|
|
|
return avret;
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
2011-06-18 17:58:42 +00:00
|
|
|
subtype = BC_MSUBTYPE_H264;
|
2016-09-18 19:43:25 +00:00
|
|
|
format.startCodeSz = 4;
|
2016-10-09 03:34:00 +00:00
|
|
|
format.pMetaData = avctx->extradata;
|
|
|
|
format.metaDataSz = avctx->extradata_size;
|
|
|
|
break;
|
2011-03-10 06:09:00 +00:00
|
|
|
case BC_MSUBTYPE_H264:
|
|
|
|
format.startCodeSz = 4;
|
|
|
|
// Fall-through
|
|
|
|
case BC_MSUBTYPE_VC1:
|
|
|
|
case BC_MSUBTYPE_WVC1:
|
|
|
|
case BC_MSUBTYPE_WMV3:
|
|
|
|
case BC_MSUBTYPE_WMVA:
|
|
|
|
case BC_MSUBTYPE_MPEG2VIDEO:
|
2016-10-15 22:50:29 +00:00
|
|
|
case BC_MSUBTYPE_DIVX:
|
2011-03-10 06:09:00 +00:00
|
|
|
case BC_MSUBTYPE_DIVX311:
|
|
|
|
format.pMetaData = avctx->extradata;
|
|
|
|
format.metaDataSz = avctx->extradata_size;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: Unknown codec name\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
format.mSubtype = subtype;
|
|
|
|
|
2011-04-21 04:41:43 +00:00
|
|
|
if (priv->sWidth) {
|
|
|
|
format.bEnableScaling = 1;
|
|
|
|
format.ScalingParams.sWidth = priv->sWidth;
|
|
|
|
}
|
|
|
|
|
2011-03-10 06:09:00 +00:00
|
|
|
/* Get a decoder instance */
|
|
|
|
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: starting up\n");
|
|
|
|
// Initialize the Link and Decoder devices
|
|
|
|
ret = DtsDeviceOpen(&priv->dev, mode);
|
|
|
|
if (ret != BC_STS_SUCCESS) {
|
|
|
|
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: DtsDeviceOpen failed\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = DtsCrystalHDVersion(priv->dev, &version);
|
|
|
|
if (ret != BC_STS_SUCCESS) {
|
|
|
|
av_log(avctx, AV_LOG_VERBOSE,
|
|
|
|
"CrystalHD: DtsCrystalHDVersion failed\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
priv->is_70012 = version.device == 0;
|
|
|
|
|
|
|
|
if (priv->is_70012 &&
|
|
|
|
(subtype == BC_MSUBTYPE_DIVX || subtype == BC_MSUBTYPE_DIVX311)) {
|
|
|
|
av_log(avctx, AV_LOG_VERBOSE,
|
|
|
|
"CrystalHD: BCM70012 doesn't support MPEG4-ASP/DivX/Xvid\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = DtsSetInputFormat(priv->dev, &format);
|
|
|
|
if (ret != BC_STS_SUCCESS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: SetInputFormat failed\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = DtsOpenDecoder(priv->dev, BC_STREAM_TYPE_ES);
|
|
|
|
if (ret != BC_STS_SUCCESS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsOpenDecoder failed\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = DtsSetColorSpace(priv->dev, OUTPUT_MODE422_YUY2);
|
|
|
|
if (ret != BC_STS_SUCCESS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsSetColorSpace failed\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ret = DtsStartDecoder(priv->dev);
|
|
|
|
if (ret != BC_STS_SUCCESS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartDecoder failed\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ret = DtsStartCapture(priv->dev);
|
|
|
|
if (ret != BC_STS_SUCCESS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartCapture failed\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-08-07 20:45:46 +00:00
|
|
|
if (avctx->codec->id == AV_CODEC_ID_H264) {
|
2011-03-26 05:46:22 +00:00
|
|
|
priv->parser = av_parser_init(avctx->codec->id);
|
|
|
|
if (!priv->parser)
|
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"Cannot open the h.264 parser! Interlaced h.264 content "
|
|
|
|
"will not be detected reliably.\n");
|
2011-06-13 03:22:20 +00:00
|
|
|
priv->parser->flags = PARSER_FLAG_COMPLETE_FRAMES;
|
2011-03-26 05:46:22 +00:00
|
|
|
}
|
2011-03-10 06:09:00 +00:00
|
|
|
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Init complete.\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
uninit(avctx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline CopyRet copy_frame(AVCodecContext *avctx,
|
|
|
|
BC_DTS_PROC_OUT *output,
|
2012-12-05 18:24:43 +00:00
|
|
|
void *data, int *got_frame)
|
2011-03-10 06:09:00 +00:00
|
|
|
{
|
|
|
|
BC_STATUS ret;
|
2012-01-22 21:46:59 +00:00
|
|
|
BC_DTS_STATUS decoder_status = { 0, };
|
2011-03-10 06:09:00 +00:00
|
|
|
uint8_t interlaced;
|
|
|
|
|
|
|
|
CHDContext *priv = avctx->priv_data;
|
2011-03-26 05:43:53 +00:00
|
|
|
int64_t pkt_pts = AV_NOPTS_VALUE;
|
2011-03-26 05:48:41 +00:00
|
|
|
uint8_t pic_type = 0;
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) ==
|
|
|
|
VDEC_FLAG_BOTTOMFIELD;
|
|
|
|
uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST);
|
|
|
|
|
|
|
|
int width = output->PicInfo.width;
|
|
|
|
int height = output->PicInfo.height;
|
|
|
|
int bwidth;
|
|
|
|
uint8_t *src = output->Ybuff;
|
|
|
|
int sStride;
|
|
|
|
uint8_t *dst;
|
|
|
|
int dStride;
|
|
|
|
|
2011-03-26 05:43:53 +00:00
|
|
|
if (output->PicInfo.timeStamp != 0) {
|
|
|
|
OpaqueList *node = opaque_list_pop(priv, output->PicInfo.timeStamp);
|
|
|
|
if (node) {
|
|
|
|
pkt_pts = node->reordered_opaque;
|
2011-03-26 05:48:41 +00:00
|
|
|
pic_type = node->pic_type;
|
2011-03-26 05:43:53 +00:00
|
|
|
av_free(node);
|
2011-03-26 05:48:41 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We will encounter a situation where a timestamp cannot be
|
|
|
|
* popped if a second field is being returned. In this case,
|
|
|
|
* each field has the same timestamp and the first one will
|
|
|
|
* cause it to be popped. To keep subsequent calculations
|
|
|
|
* simple, pic_type should be set a FIELD value - doesn't
|
|
|
|
* matter which, but I chose BOTTOM.
|
|
|
|
*/
|
|
|
|
pic_type = PICT_BOTTOM_FIELD;
|
2011-03-26 05:43:53 +00:00
|
|
|
}
|
|
|
|
av_log(avctx, AV_LOG_VERBOSE, "output \"pts\": %"PRIu64"\n",
|
|
|
|
output->PicInfo.timeStamp);
|
2011-03-26 05:48:41 +00:00
|
|
|
av_log(avctx, AV_LOG_VERBOSE, "output picture type %d\n",
|
|
|
|
pic_type);
|
2011-03-26 05:43:53 +00:00
|
|
|
}
|
|
|
|
|
2011-03-10 06:09:00 +00:00
|
|
|
ret = DtsGetDriverStatus(priv->dev, &decoder_status);
|
|
|
|
if (ret != BC_STS_SUCCESS) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"CrystalHD: GetDriverStatus failed: %u\n", ret);
|
|
|
|
return RET_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-10-10 03:54:27 +00:00
|
|
|
interlaced = output->PicInfo.flags & VDEC_FLAG_INTERLACED_SRC;
|
CrystalHD: Improve detection of h.264 content.
As previously discussed, the CrystalHD hardware returns exceptionally
useless information about interlaced h.264 content - to the extent
that it's not possible to distinguish most MBAFF and PAFF content until
it's too late.
In an attempt to compensate for this, I'm introducing two mechanisms:
1) Peeking at the picture number of the next picture
The hardware provides a capability to peek the next picture number. If
it is the same as the current picture number, then we are clearly dealing
with two fields and not a frame or fieldpair.
If this always worked, it would be all we need, but it's not guaranteed
to work. Sometimes, the next picture may not be decoded sufficiently
for the number to be known; alternately, a corruption in the stream may
cause the hardware to refuse to return the number even if the next
intact frame is decoded. In either case, the query will return 0.
If we are unable to peek the next picture number, we assume that the
picture is a frame/fieldpair and return it accordingly. If that turns
out to be incorrect, we discard the second field, and the user has
to live with the glitch. In testing, false detection can occur for
the first couple of seconds, and then the pipeline stabalizes and
we get correct detection.
2) Use the h264_parser to detect when individual input fields have
been combined into an output fieldpair.
I have multiple PAFF samples where this behaviour is detected. The
peeking mechanism described above will correctly detect that the
output is a fieldpair, but we need to know what the input type was
to ensure pipeline stability (only return one output frame per input
frame).
If we find ourselves with an output fieldpair, yet the input picture
type was a field, as reported by the parser, then we are dealing with
this case, and can make sure not to return anything on the next
decode() call.
Taken together, these allow us to remove the hard-coded hacks for
different h.264 types, and we can clearly describe the conditions
under which we can trust the hardware's claim that content is
interlaced.
Signed-off-by: Philip Langdale <philipl@overt.org>
2011-03-26 17:34:20 +00:00
|
|
|
|
2016-10-10 03:54:27 +00:00
|
|
|
av_log(avctx, AV_LOG_VERBOSE, "Interlaced state: %d\n",
|
|
|
|
interlaced);
|
2011-03-10 06:09:00 +00:00
|
|
|
|
2013-03-13 04:53:22 +00:00
|
|
|
if (priv->pic->data[0] && !priv->need_second_field)
|
|
|
|
av_frame_unref(priv->pic);
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
priv->need_second_field = interlaced && !priv->need_second_field;
|
|
|
|
|
2013-03-13 04:53:22 +00:00
|
|
|
if (!priv->pic->data[0]) {
|
lavc: factorize ff_{thread_,re,}get_buffer error messages.
Coccinelle profile used:
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_get_buffer(ctx, f, flags)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str;
@@
-if ((r = ff_reget_buffer(ctx, f)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_reget_buffer(ctx, f)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0)
+ return r;
...along with some manual patches for the remaining ones.
2013-03-12 07:41:53 +00:00
|
|
|
if (ff_get_buffer(avctx, priv->pic, AV_GET_BUFFER_FLAG_REF) < 0)
|
2011-03-10 06:09:00 +00:00
|
|
|
return RET_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
bwidth = av_image_get_linesize(avctx->pix_fmt, width, 0);
|
|
|
|
if (priv->is_70012) {
|
|
|
|
int pStride;
|
|
|
|
|
|
|
|
if (width <= 720)
|
|
|
|
pStride = 720;
|
|
|
|
else if (width <= 1280)
|
|
|
|
pStride = 1280;
|
2012-04-17 07:29:27 +00:00
|
|
|
else pStride = 1920;
|
2011-03-10 06:09:00 +00:00
|
|
|
sStride = av_image_get_linesize(avctx->pix_fmt, pStride, 0);
|
|
|
|
} else {
|
|
|
|
sStride = bwidth;
|
|
|
|
}
|
|
|
|
|
2013-03-13 04:53:22 +00:00
|
|
|
dStride = priv->pic->linesize[0];
|
|
|
|
dst = priv->pic->data[0];
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
av_log(priv->avctx, AV_LOG_VERBOSE, "CrystalHD: Copying out frame\n");
|
|
|
|
|
2016-10-15 20:30:52 +00:00
|
|
|
/*
|
|
|
|
* The hardware doesn't return the first sample of a picture.
|
|
|
|
* Ignoring why it behaves this way, it's better to copy the sample from
|
|
|
|
* the second line, rather than the next sample across because the chroma
|
|
|
|
* values should be correct (assuming the decoded video was 4:2:0, which
|
|
|
|
* it was).
|
|
|
|
*/
|
|
|
|
*((uint32_t *)src) = *((uint32_t *)(src + sStride));
|
|
|
|
|
2011-03-10 06:09:00 +00:00
|
|
|
if (interlaced) {
|
|
|
|
int dY = 0;
|
|
|
|
int sY = 0;
|
|
|
|
|
|
|
|
height /= 2;
|
|
|
|
if (bottom_field) {
|
|
|
|
av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: bottom field\n");
|
|
|
|
dY = 1;
|
|
|
|
} else {
|
|
|
|
av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: top field\n");
|
|
|
|
dY = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (sY = 0; sY < height; dY++, sY++) {
|
|
|
|
memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth);
|
2011-03-26 06:06:28 +00:00
|
|
|
dY++;
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
av_image_copy_plane(dst, dStride, src, sStride, bwidth, height);
|
|
|
|
}
|
|
|
|
|
2013-03-13 04:53:22 +00:00
|
|
|
priv->pic->interlaced_frame = interlaced;
|
2011-03-10 06:09:00 +00:00
|
|
|
if (interlaced)
|
2013-03-13 04:53:22 +00:00
|
|
|
priv->pic->top_field_first = !bottom_first;
|
2011-03-10 06:09:00 +00:00
|
|
|
|
2016-09-18 19:38:20 +00:00
|
|
|
if (pkt_pts != AV_NOPTS_VALUE) {
|
|
|
|
priv->pic->pts = pkt_pts;
|
2016-10-07 10:55:50 +00:00
|
|
|
#if FF_API_PKT_PTS
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2016-09-18 19:38:20 +00:00
|
|
|
priv->pic->pkt_pts = pkt_pts;
|
2016-10-07 10:55:50 +00:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2016-09-18 19:38:20 +00:00
|
|
|
}
|
|
|
|
av_frame_set_pkt_pos(priv->pic, -1);
|
|
|
|
av_frame_set_pkt_duration(priv->pic, 0);
|
|
|
|
av_frame_set_pkt_size(priv->pic, -1);
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
if (!priv->need_second_field) {
|
2012-12-05 18:24:43 +00:00
|
|
|
*got_frame = 1;
|
2013-03-13 04:53:22 +00:00
|
|
|
if ((ret = av_frame_ref(data, priv->pic)) < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
return RET_OK;
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline CopyRet receive_frame(AVCodecContext *avctx,
|
2012-12-05 18:24:43 +00:00
|
|
|
void *data, int *got_frame)
|
2011-03-10 06:09:00 +00:00
|
|
|
{
|
|
|
|
BC_STATUS ret;
|
|
|
|
BC_DTS_PROC_OUT output = {
|
|
|
|
.PicInfo.width = avctx->width,
|
|
|
|
.PicInfo.height = avctx->height,
|
|
|
|
};
|
|
|
|
CHDContext *priv = avctx->priv_data;
|
|
|
|
HANDLE dev = priv->dev;
|
|
|
|
|
2012-12-05 18:24:43 +00:00
|
|
|
*got_frame = 0;
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
// Request decoded data from the driver
|
|
|
|
ret = DtsProcOutputNoCopy(dev, OUTPUT_PROC_TIMEOUT, &output);
|
|
|
|
if (ret == BC_STS_FMT_CHANGE) {
|
|
|
|
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Initial format change\n");
|
|
|
|
avctx->width = output.PicInfo.width;
|
|
|
|
avctx->height = output.PicInfo.height;
|
2012-04-17 07:36:27 +00:00
|
|
|
switch ( output.PicInfo.aspect_ratio ) {
|
|
|
|
case vdecAspectRatioSquare:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 1, 1};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio12_11:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 12, 11};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio10_11:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 10, 11};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio16_11:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 16, 11};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio40_33:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 40, 33};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio24_11:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 24, 11};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio20_11:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 20, 11};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio32_11:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 32, 11};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio80_33:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 80, 33};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio18_11:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 18, 11};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio15_11:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 15, 11};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio64_33:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 64, 33};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio160_99:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) {160, 99};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio4_3:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 4, 3};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio16_9:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) { 16, 9};
|
|
|
|
break;
|
|
|
|
case vdecAspectRatio221_1:
|
|
|
|
avctx->sample_aspect_ratio = (AVRational) {221, 1};
|
|
|
|
break;
|
|
|
|
}
|
2016-10-10 03:46:38 +00:00
|
|
|
return RET_OK;
|
2011-03-10 06:09:00 +00:00
|
|
|
} else if (ret == BC_STS_SUCCESS) {
|
|
|
|
int copy_ret = -1;
|
|
|
|
if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
|
2016-10-15 22:50:29 +00:00
|
|
|
if (avctx->codec->id == AV_CODEC_ID_MPEG4 &&
|
|
|
|
output.PicInfo.timeStamp == 0 && priv->bframe_bug) {
|
|
|
|
if (!priv->bframe_bug) {
|
|
|
|
av_log(avctx, AV_LOG_VERBOSE,
|
|
|
|
"CrystalHD: Not returning packed frame twice.\n");
|
|
|
|
}
|
|
|
|
DtsReleaseOutputBuffs(dev, NULL, FALSE);
|
|
|
|
return RET_COPY_AGAIN;
|
|
|
|
}
|
|
|
|
|
2011-03-10 06:09:00 +00:00
|
|
|
print_frame_info(priv, &output);
|
|
|
|
|
2012-12-05 18:24:43 +00:00
|
|
|
copy_ret = copy_frame(avctx, &output, data, got_frame);
|
2011-03-10 06:09:00 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* An invalid frame has been consumed.
|
|
|
|
*/
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput succeeded with "
|
|
|
|
"invalid PIB\n");
|
|
|
|
copy_ret = RET_OK;
|
|
|
|
}
|
|
|
|
DtsReleaseOutputBuffs(dev, NULL, FALSE);
|
|
|
|
|
|
|
|
return copy_ret;
|
|
|
|
} else if (ret == BC_STS_BUSY) {
|
2016-10-10 03:46:38 +00:00
|
|
|
return RET_OK;
|
2011-03-10 06:09:00 +00:00
|
|
|
} else {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput failed %d\n", ret);
|
|
|
|
return RET_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
static int crystalhd_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)
|
2011-03-10 06:09:00 +00:00
|
|
|
{
|
2016-10-10 03:46:38 +00:00
|
|
|
BC_STATUS bc_ret;
|
2011-03-10 06:09:00 +00:00
|
|
|
CHDContext *priv = avctx->priv_data;
|
|
|
|
HANDLE dev = priv->dev;
|
2011-03-26 05:46:22 +00:00
|
|
|
uint8_t pic_type = 0;
|
2016-10-10 03:46:38 +00:00
|
|
|
AVPacket filtered_packet = { 0 };
|
|
|
|
int ret = 0;
|
2011-03-10 06:09:00 +00:00
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: decode_packet\n");
|
2011-03-10 06:09:00 +00:00
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
if (avpkt && avpkt->size) {
|
2011-03-10 06:09:00 +00:00
|
|
|
int32_t tx_free = (int32_t)DtsTxFreeSize(dev);
|
2011-03-26 05:46:22 +00:00
|
|
|
|
2016-10-15 22:50:29 +00:00
|
|
|
if (!priv->bframe_bug && (avpkt->size == 6 || avpkt->size == 7)) {
|
|
|
|
/*
|
|
|
|
* Drop frames trigger the bug
|
|
|
|
*/
|
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"CrystalHD: Enabling work-around for packed b-frame bug\n");
|
|
|
|
priv->bframe_bug = 1;
|
|
|
|
} else if (priv->bframe_bug && avpkt->size == 8) {
|
|
|
|
/*
|
|
|
|
* Delay frames don't trigger the bug
|
|
|
|
*/
|
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"CrystalHD: Disabling work-around for packed b-frame bug\n");
|
|
|
|
priv->bframe_bug = 0;
|
|
|
|
}
|
|
|
|
|
2016-09-18 19:43:25 +00:00
|
|
|
if (priv->bsfc) {
|
|
|
|
AVPacket filter_packet = { 0 };
|
|
|
|
|
|
|
|
ret = av_packet_ref(&filter_packet, avpkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
|
|
|
|
"failed to ref input packet\n");
|
2016-10-10 03:46:38 +00:00
|
|
|
goto exit;
|
2016-09-18 19:43:25 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
ret = av_bsf_send_packet(priv->bsfc, &filter_packet);
|
|
|
|
if (ret < 0) {
|
2016-09-18 19:43:25 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
|
|
|
|
"failed to send input packet\n");
|
2016-10-10 03:46:38 +00:00
|
|
|
goto exit;
|
2016-09-18 19:43:25 +00:00
|
|
|
}
|
2011-03-26 05:46:22 +00:00
|
|
|
|
2016-09-18 19:43:25 +00:00
|
|
|
ret = av_bsf_receive_packet(priv->bsfc, &filtered_packet);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
|
|
|
|
"failed to receive output packet\n");
|
2016-10-10 03:46:38 +00:00
|
|
|
goto exit;
|
2011-06-13 03:22:20 +00:00
|
|
|
}
|
2016-09-18 19:43:25 +00:00
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
avpkt = &filtered_packet;
|
2016-09-18 19:43:25 +00:00
|
|
|
av_packet_unref(&filter_packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->parser) {
|
2016-10-10 03:46:38 +00:00
|
|
|
uint8_t *pout;
|
|
|
|
int psize;
|
|
|
|
int index;
|
|
|
|
H264Context *h = priv->parser->priv_data;
|
|
|
|
|
|
|
|
index = av_parser_parse2(priv->parser, avctx, &pout, &psize,
|
|
|
|
avpkt->data, avpkt->size, avpkt->pts,
|
|
|
|
avpkt->dts, 0);
|
|
|
|
if (index < 0) {
|
2011-06-13 03:22:20 +00:00
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
2016-10-10 03:46:38 +00:00
|
|
|
"CrystalHD: Failed to parse h.264 packet to "
|
|
|
|
"detect interlacing.\n");
|
|
|
|
} else if (index != avpkt->size) {
|
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"CrystalHD: Failed to parse h.264 packet "
|
|
|
|
"completely. Interlaced frames may be "
|
|
|
|
"incorrectly detected.\n");
|
|
|
|
} else {
|
|
|
|
av_log(avctx, AV_LOG_VERBOSE,
|
|
|
|
"CrystalHD: parser picture type %d\n",
|
|
|
|
h->picture_structure);
|
|
|
|
pic_type = h->picture_structure;
|
2011-06-13 03:22:20 +00:00
|
|
|
}
|
2011-03-26 05:46:22 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
if (avpkt->size < tx_free) {
|
2011-03-10 06:09:00 +00:00
|
|
|
/*
|
|
|
|
* Despite being notionally opaque, either libcrystalhd or
|
|
|
|
* the hardware itself will mangle pts values that are too
|
|
|
|
* small or too large. The docs claim it should be in units
|
|
|
|
* of 100ns. Given that we're nominally dealing with a black
|
|
|
|
* box on both sides, any transform we do has no guarantee of
|
|
|
|
* avoiding mangling so we need to build a mapping to values
|
|
|
|
* we know will not be mangled.
|
|
|
|
*/
|
2016-09-18 19:38:20 +00:00
|
|
|
int64_t safe_pts = avpkt->pts == AV_NOPTS_VALUE ? 0 : avpkt->pts;
|
|
|
|
uint64_t pts = opaque_list_push(priv, safe_pts, pic_type);
|
2011-03-10 06:09:00 +00:00
|
|
|
if (!pts) {
|
2016-10-10 03:46:38 +00:00
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto exit;
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
av_log(priv->avctx, AV_LOG_VERBOSE,
|
|
|
|
"input \"pts\": %"PRIu64"\n", pts);
|
2016-10-10 03:46:38 +00:00
|
|
|
bc_ret = DtsProcInput(dev, avpkt->data, avpkt->size, pts, 0);
|
|
|
|
if (bc_ret == BC_STS_BUSY) {
|
2011-03-10 06:09:00 +00:00
|
|
|
av_log(avctx, AV_LOG_WARNING,
|
|
|
|
"CrystalHD: ProcInput returned busy\n");
|
2016-10-10 03:46:38 +00:00
|
|
|
ret = AVERROR(EAGAIN);
|
|
|
|
goto exit;
|
|
|
|
} else if (bc_ret != BC_STS_SUCCESS) {
|
2011-03-10 06:09:00 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"CrystalHD: ProcInput failed: %u\n", ret);
|
2016-10-10 03:46:38 +00:00
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Input buffer full\n");
|
|
|
|
ret = AVERROR(EAGAIN);
|
|
|
|
goto exit;
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
av_log(avctx, AV_LOG_INFO, "CrystalHD: No more input data\n");
|
2016-10-10 03:46:38 +00:00
|
|
|
ret = AVERROR_EOF;
|
|
|
|
goto exit;
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
2016-10-10 03:46:38 +00:00
|
|
|
exit:
|
|
|
|
av_packet_unref(&filtered_packet);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-03-10 06:09:00 +00:00
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
static int crystalhd_receive_frame(AVCodecContext *avctx, AVFrame *frame)
|
|
|
|
{
|
|
|
|
BC_STATUS bc_ret;
|
|
|
|
BC_DTS_STATUS decoder_status = { 0, };
|
|
|
|
CopyRet rec_ret;
|
|
|
|
CHDContext *priv = avctx->priv_data;
|
|
|
|
HANDLE dev = priv->dev;
|
|
|
|
int got_frame = 0;
|
2011-03-10 06:09:00 +00:00
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: receive_frame\n");
|
|
|
|
|
|
|
|
bc_ret = DtsGetDriverStatus(dev, &decoder_status);
|
|
|
|
if (bc_ret != BC_STS_SUCCESS) {
|
2011-03-10 06:09:00 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "CrystalHD: GetDriverStatus failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
if (decoder_status.ReadyListCount == 0) {
|
|
|
|
av_log(avctx, AV_LOG_INFO, "CrystalHD: Insufficient frames ready. Returning\n");
|
|
|
|
return AVERROR(EAGAIN);
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
rec_ret = receive_frame(avctx, frame, &got_frame);
|
|
|
|
if (rec_ret == RET_ERROR) {
|
|
|
|
return -1;
|
|
|
|
} else if (got_frame == 0) {
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-03-10 06:09:00 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 03:46:38 +00:00
|
|
|
#define DEFINE_CRYSTALHD_DECODER(x, X) \
|
|
|
|
static const AVClass x##_crystalhd_class = { \
|
|
|
|
.class_name = #x "_crystalhd", \
|
|
|
|
.item_name = av_default_item_name, \
|
|
|
|
.option = options, \
|
|
|
|
.version = LIBAVUTIL_VERSION_INT, \
|
|
|
|
}; \
|
|
|
|
AVCodec ff_##x##_crystalhd_decoder = { \
|
|
|
|
.name = #x "_crystalhd", \
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("CrystalHD " #X " decoder"), \
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO, \
|
|
|
|
.id = AV_CODEC_ID_##X, \
|
|
|
|
.priv_data_size = sizeof(CHDContext), \
|
|
|
|
.priv_class = &x##_crystalhd_class, \
|
|
|
|
.init = init, \
|
|
|
|
.close = uninit, \
|
|
|
|
.send_packet = crystalhd_decode_packet, \
|
|
|
|
.receive_frame = crystalhd_receive_frame, \
|
|
|
|
.flush = flush, \
|
|
|
|
.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \
|
|
|
|
.pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE}, \
|
|
|
|
};
|
2011-03-10 06:09:00 +00:00
|
|
|
|
|
|
|
#if CONFIG_H264_CRYSTALHD_DECODER
|
2016-10-10 03:46:38 +00:00
|
|
|
DEFINE_CRYSTALHD_DECODER(h264, H264)
|
2011-03-10 06:09:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_MPEG2_CRYSTALHD_DECODER
|
2016-10-10 03:46:38 +00:00
|
|
|
DEFINE_CRYSTALHD_DECODER(mpeg2, MPEG2VIDEO)
|
2011-03-10 06:09:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_MPEG4_CRYSTALHD_DECODER
|
2016-10-10 03:46:38 +00:00
|
|
|
DEFINE_CRYSTALHD_DECODER(mpeg4, MPEG4)
|
2011-03-10 06:09:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_MSMPEG4_CRYSTALHD_DECODER
|
2016-10-10 03:46:38 +00:00
|
|
|
DEFINE_CRYSTALHD_DECODER(msmpeg4, MSMPEG4V3)
|
2011-03-10 06:09:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_VC1_CRYSTALHD_DECODER
|
2016-10-10 03:46:38 +00:00
|
|
|
DEFINE_CRYSTALHD_DECODER(vc1, VC1)
|
2011-03-10 06:09:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_WMV3_CRYSTALHD_DECODER
|
2016-10-10 03:46:38 +00:00
|
|
|
DEFINE_CRYSTALHD_DECODER(wmv3, WMV3)
|
2011-03-10 06:09:00 +00:00
|
|
|
#endif
|