76 #define _XOPEN_SOURCE 600
82 #include <libcrystalhd/bc_dts_types.h>
83 #include <libcrystalhd/bc_dts_defs.h>
84 #include <libcrystalhd/libcrystalhd_if.h>
89 #include "libavutil/imgutils.h"
90 #include "libavutil/intreadwrite.h"
91 #include "libavutil/opt.h"
94 #define OUTPUT_PROC_TIMEOUT 50
96 #define TIMESTAMP_UNIT 100000
98 #define BASE_WAIT 10000
100 #define WAIT_UNIT 1000
154 {
"crystalhd_downscale_width",
155 "Turn on downscaling to the specified width",
171 return BC_MSUBTYPE_DIVX;
173 return BC_MSUBTYPE_DIVX311;
175 return BC_MSUBTYPE_MPEG2VIDEO;
177 return BC_MSUBTYPE_VC1;
179 return BC_MSUBTYPE_WMV3;
181 return priv->
is_nal ? BC_MSUBTYPE_AVC1 : BC_MSUBTYPE_H264;
183 return BC_MSUBTYPE_INVALID;
191 output->YBuffDoneSz);
193 output->UVBuffDoneSz);
195 output->PicInfo.timeStamp);
197 output->PicInfo.picture_number);
199 output->PicInfo.width);
201 output->PicInfo.height);
203 output->PicInfo.chroma_format);
205 output->PicInfo.pulldown);
207 output->PicInfo.flags);
209 output->PicInfo.frame_rate);
211 output->PicInfo.aspect_ratio);
213 output->PicInfo.colour_primaries);
215 output->PicInfo.picture_meta_payload);
217 output->PicInfo.sess_num);
219 output->PicInfo.ycom);
221 output->PicInfo.custom_aspect_ratio_width_height);
223 output->PicInfo.n_drop);
225 output->PicInfo.other.h264.valid);
239 "Unable to allocate new node in OpaqueList.\n");
244 priv->
head = newNode;
249 priv->
tail = newNode;
269 "CrystalHD: Attempted to query non-existent timestamps.\n");
299 current->
next = NULL;
307 "CrystalHD: Couldn't match fake_timestamp.\n");
330 DtsFlushInput(priv->
dev, 4);
340 DtsStopDecoder(device);
341 DtsCloseDecoder(device);
342 DtsDeviceClose(device);
384 BC_INPUT_FORMAT format = {
387 .OptFlags = 0x80000000 | vdecFrameRate59_94 | 0x40,
388 .width = avctx->
width,
392 BC_MEDIA_SUBTYPE subtype;
394 uint32_t
mode = DTS_PLAYBACK_MODE |
395 DTS_LOAD_FILE_PLAY_FW |
396 DTS_SKIP_TX_CHK_CPB |
397 DTS_PLAYBACK_DROP_RPT_MODE |
398 DTS_SINGLE_THREADED_MODE |
399 DTS_DFLT_RESOLUTION(vdecRESOLUTION_1080p23_976);
416 case BC_MSUBTYPE_AVC1:
425 "Failed to allocate copy of extradata\n");
434 "Cannot open the h264_mp4toannexb BSF!\n");
438 &dummy_int, NULL, 0, 0);
440 subtype = BC_MSUBTYPE_H264;
442 case BC_MSUBTYPE_H264:
443 format.startCodeSz = 4;
445 case BC_MSUBTYPE_VC1:
446 case BC_MSUBTYPE_WVC1:
447 case BC_MSUBTYPE_WMV3:
448 case BC_MSUBTYPE_WMVA:
449 case BC_MSUBTYPE_MPEG2VIDEO:
450 case BC_MSUBTYPE_DIVX:
451 case BC_MSUBTYPE_DIVX311:
459 format.mSubtype = subtype;
462 format.bEnableScaling = 1;
463 format.ScalingParams.sWidth = priv->
sWidth;
469 ret = DtsDeviceOpen(&priv->
dev, mode);
470 if (ret != BC_STS_SUCCESS) {
475 ret = DtsCrystalHDVersion(priv->
dev, &version);
476 if (ret != BC_STS_SUCCESS) {
478 "CrystalHD: DtsCrystalHDVersion failed\n");
481 priv->
is_70012 = version.device == 0;
484 (subtype == BC_MSUBTYPE_DIVX || subtype == BC_MSUBTYPE_DIVX311)) {
486 "CrystalHD: BCM70012 doesn't support MPEG4-ASP/DivX/Xvid\n");
490 ret = DtsSetInputFormat(priv->
dev, &format);
491 if (ret != BC_STS_SUCCESS) {
496 ret = DtsOpenDecoder(priv->
dev, BC_STREAM_TYPE_ES);
497 if (ret != BC_STS_SUCCESS) {
502 ret = DtsSetColorSpace(priv->
dev, OUTPUT_MODE422_YUY2);
503 if (ret != BC_STS_SUCCESS) {
507 ret = DtsStartDecoder(priv->
dev);
508 if (ret != BC_STS_SUCCESS) {
512 ret = DtsStartCapture(priv->
dev);
513 if (ret != BC_STS_SUCCESS) {
522 "Cannot open the h.264 parser! Interlaced h.264 content "
523 "will not be detected reliably.\n");
537 BC_DTS_PROC_OUT *output,
538 void *
data,
int *got_frame)
541 BC_DTS_STATUS decoder_status = { 0, };
549 uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) ==
550 VDEC_FLAG_BOTTOMFIELD;
551 uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST);
553 int width = output->PicInfo.width;
554 int height = output->PicInfo.height;
561 if (output->PicInfo.timeStamp != 0) {
579 output->PicInfo.timeStamp);
584 ret = DtsGetDriverStatus(priv->
dev, &decoder_status);
585 if (ret != BC_STS_SUCCESS) {
587 "CrystalHD: GetDriverStatus failed: %u\n", ret);
608 !(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
610 (decoder_status.picNumFlags & ~0x40000000) ==
611 output->PicInfo.picture_number;
621 "Incorrectly guessed progressive frame. Discarding second field\n");
626 interlaced = (output->PicInfo.flags & VDEC_FLAG_INTERLACED_SRC) &&
629 if (!trust_interlaced && (decoder_status.picNumFlags & ~0x40000000) == 0) {
631 "Next picture number unknown. Assuming progressive frame.\n");
635 interlaced, trust_interlaced);
653 else if (width <= 1280)
679 for (sY = 0; sY <
height; dY++, sY++) {
680 memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth);
707 if (!interlaced && (output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) &&
726 (!(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
733 void *
data,
int *got_frame)
736 BC_DTS_PROC_OUT output = {
737 .PicInfo.width = avctx->
width,
738 .PicInfo.height = avctx->
height,
747 if (ret == BC_STS_FMT_CHANGE) {
749 avctx->
width = output.PicInfo.width;
750 avctx->
height = output.PicInfo.height;
751 switch ( output.PicInfo.aspect_ratio ) {
752 case vdecAspectRatioSquare:
755 case vdecAspectRatio12_11:
758 case vdecAspectRatio10_11:
761 case vdecAspectRatio16_11:
764 case vdecAspectRatio40_33:
767 case vdecAspectRatio24_11:
770 case vdecAspectRatio20_11:
773 case vdecAspectRatio32_11:
776 case vdecAspectRatio80_33:
779 case vdecAspectRatio18_11:
782 case vdecAspectRatio15_11:
785 case vdecAspectRatio64_33:
788 case vdecAspectRatio160_99:
791 case vdecAspectRatio4_3:
794 case vdecAspectRatio16_9:
797 case vdecAspectRatio221_1:
802 }
else if (ret == BC_STS_SUCCESS) {
804 if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
814 output.PicInfo.timeStamp == 0 && priv->
bframe_bug) {
816 "CrystalHD: Not returning packed frame twice.\n");
818 DtsReleaseOutputBuffs(dev, NULL,
FALSE);
824 if (priv->
last_picture + 1 < output.PicInfo.picture_number) {
826 "CrystalHD: Picture Number discontinuity\n");
840 copy_ret =
copy_frame(avctx, &output, data, got_frame);
841 if (*got_frame > 0) {
856 DtsReleaseOutputBuffs(dev, NULL,
FALSE);
859 }
else if (ret == BC_STS_BUSY) {
871 BC_DTS_STATUS decoder_status = { 0, };
887 "CrystalHD: Enabling work-around for packed b-frame bug\n");
894 "CrystalHD: Disabling work-around for packed b-frame bug\n");
907 avpkt->
data, len, 0);
918 in_data, len, avctx->
pkt->
pts,
922 "CrystalHD: Failed to parse h.264 packet to "
923 "detect interlacing.\n");
924 }
else if (index != len) {
926 "CrystalHD: Failed to parse h.264 packet "
927 "completely. Interlaced frames may be "
928 "incorrectly detected.\n");
931 "CrystalHD: parser picture type %d\n",
937 "CrystalHD: mp4toannexb filter failed to filter "
938 "packet. Interlaced frames may be incorrectly "
943 if (len < tx_free - 1024) {
961 "input \"pts\": %"PRIu64
"\n", pts);
962 ret = DtsProcInput(dev, in_data, len, pts, 0);
966 if (ret == BC_STS_BUSY) {
968 "CrystalHD: ProcInput returned busy\n");
971 }
else if (ret != BC_STS_SUCCESS) {
973 "CrystalHD: ProcInput failed: %u\n", ret);
992 ret = DtsGetDriverStatus(dev, &decoder_status);
993 if (ret != BC_STS_SUCCESS) {
1007 if (decoder_status.ReadyListCount != 0)
1012 }
else if (decoder_status.ReadyListCount == 0) {
1027 if (rec_ret ==
RET_OK && *got_frame == 0) {
1049 ret = DtsGetDriverStatus(dev, &decoder_status);
1050 if (ret == BC_STS_SUCCESS &&
1051 decoder_status.ReadyListCount > 0) {
1053 if ((rec_ret ==
RET_OK && *got_frame > 0) ||
1064 "Don't output on next decode call.\n");
1081 #if CONFIG_H264_CRYSTALHD_DECODER
1089 AVCodec ff_h264_crystalhd_decoder = {
1090 .
name =
"h264_crystalhd",
1091 .long_name =
NULL_IF_CONFIG_SMALL(
"H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (CrystalHD acceleration)"),
1105 #if CONFIG_MPEG2_CRYSTALHD_DECODER
1106 static AVClass mpeg2_class = {
1113 AVCodec ff_mpeg2_crystalhd_decoder = {
1114 .
name =
"mpeg2_crystalhd",
1125 .priv_class = &mpeg2_class,
1129 #if CONFIG_MPEG4_CRYSTALHD_DECODER
1137 AVCodec ff_mpeg4_crystalhd_decoder = {
1138 .
name =
"mpeg4_crystalhd",
1153 #if CONFIG_MSMPEG4_CRYSTALHD_DECODER
1154 static AVClass msmpeg4_class = {
1155 "msmpeg4_crystalhd",
1161 AVCodec ff_msmpeg4_crystalhd_decoder = {
1162 .
name =
"msmpeg4_crystalhd",
1163 .long_name =
NULL_IF_CONFIG_SMALL(
"MPEG-4 Part 2 Microsoft variant version 3 (CrystalHD acceleration)"),
1173 .priv_class = &msmpeg4_class,
1177 #if CONFIG_VC1_CRYSTALHD_DECODER
1185 AVCodec ff_vc1_crystalhd_decoder = {
1186 .
name =
"vc1_crystalhd",
1197 .priv_class = &vc1_class,
1201 #if CONFIG_WMV3_CRYSTALHD_DECODER
1209 AVCodec ff_wmv3_crystalhd_decoder = {
1210 .
name =
"wmv3_crystalhd",
1221 .priv_class = &wmv3_class,
#define PICT_BOTTOM_FIELD
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane...
This structure describes decoded (raw) audio or video data.
static const AVClass h264_class
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
static OpaqueList * opaque_list_pop(CHDContext *priv, uint64_t fake_timestamp)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static CopyRet receive_frame(AVCodecContext *avctx, void *data, int *got_frame)
static av_cold int init(AVCodecContext *avctx)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
#define TIMESTAMP_UNIT
Step between fake timestamps passed to hardware in units of 100ns.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
const char * av_default_item_name(void *ctx)
Return the context name.
static av_cold int uninit(AVCodecContext *avctx)
const char * name
Name of the codec implementation.
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by a given frame.
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
#define AV_OPT_FLAG_VIDEO_PARAM
#define AV_LOG_VERBOSE
Detailed information.
int interlaced_frame
The content of the picture is interlaced.
const OptionDef options[]
H.264 / AVC / MPEG4 part10 codec.
AVCodecID
Identify the syntax and semantics of the bitstream.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int has_b_frames
Size of the frame reordering buffer in the decoder.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
void av_bitstream_filter_close(AVBitStreamFilterContext *bsf)
Release bitstream filter context.
#define PARSER_FLAG_COMPLETE_FRAMES
static void flush(AVCodecContext *avctx)
static uint64_t opaque_list_push(CHDContext *priv, uint64_t reordered_opaque, uint8_t pic_type)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVBitStreamFilterContext * bsfc
#define WAIT_UNIT
Increment in us to adjust wait in decode()
Libavcodec external API header.
AVPixelFormat
Pixel format.
AVCodecParserContext * parser
static void print_frame_info(CHDContext *priv, BC_DTS_PROC_OUT *output)
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts, int64_t pos)
Parse a packet.
int width
picture width / height.
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
void av_parser_close(AVCodecParserContext *s)
AVCodecParserContext * av_parser_init(int codec_id)
uint64_t reordered_opaque
preferred ID for MPEG-1/2 video decoding
#define AV_LOG_INFO
Standard information.
int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
Filter bitstream.
uint32_t orig_extradata_size
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
main external API structure.
static void close(AVCodecParserContext *s)
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
BYTE int const BYTE int int int height
Describe the class of an AVClass context structure.
static BC_MEDIA_SUBTYPE id2subtype(CHDContext *priv, enum AVCodecID id)
uint8_t need_second_field
rational number numerator/denominator
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
#define AVERROR_BSF_NOT_FOUND
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
common internal api header.
#define OUTPUT_PROC_TIMEOUT
Timeout parameter passed to DtsProcOutput() in us.
#define CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
static const AVClass mpeg4_class
#define BASE_WAIT
Initial value in us of the wait in decode()
int top_field_first
If the content is interlaced, is top field displayed first.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
AVPacket * pkt
Current packet as passed into the decoder, to avoid having to pass the packet into every function...
This structure stores compressed data.
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
AVBitStreamFilterContext * av_bitstream_filter_init(const char *name)
Create and initialize a bitstream filter context given a bitstream filter name.
#define AV_NOPTS_VALUE
Undefined timestamp value.
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static CopyRet copy_frame(AVCodecContext *avctx, BC_DTS_PROC_OUT *output, void *data, int *got_frame)