2
0

crystalhd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * - CrystalHD decoder module -
  3. *
  4. * Copyright(C) 2010,2011 Philip Langdale <ffmpeg.philipl@overt.org>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /*
  23. * - Principles of Operation -
  24. *
  25. * The CrystalHD decoder operates at the bitstream level - which is an even
  26. * higher level than the decoding hardware you typically see in modern GPUs.
  27. * This means it has a very simple interface, in principle. You feed demuxed
  28. * packets in one end and get decoded picture (fields/frames) out the other.
  29. *
  30. * Of course, nothing is ever that simple. Due, at the very least, to b-frame
  31. * dependencies in the supported formats, the hardware has a delay between
  32. * when a packet goes in, and when a picture comes out. Furthermore, this delay
  33. * is not just a function of time, but also one of the dependency on additional
  34. * frames being fed into the decoder to satisfy the b-frame dependencies.
  35. *
  36. * As such, the hardware can only be used effectively with a decode API that
  37. * doesn't assume a 1:1 relationship between input packets and output frames.
  38. * The new avcodec decode API is such an API (an m:n API) while the old one is
  39. * 1:1. Consequently, we no longer support the old API, which allows us to avoid
  40. * the vicious hacks that are required to approximate 1:1 operation.
  41. */
  42. /*****************************************************************************
  43. * Includes
  44. ****************************************************************************/
  45. #define _XOPEN_SOURCE 600
  46. #include <inttypes.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <libcrystalhd/bc_dts_types.h>
  50. #include <libcrystalhd/bc_dts_defs.h>
  51. #include <libcrystalhd/libcrystalhd_if.h>
  52. #include "avcodec.h"
  53. #include "decode.h"
  54. #include "internal.h"
  55. #include "libavutil/imgutils.h"
  56. #include "libavutil/intreadwrite.h"
  57. #include "libavutil/opt.h"
  58. #if HAVE_UNISTD_H
  59. #include <unistd.h>
  60. #endif
  61. /** Timeout parameter passed to DtsProcOutput() in us */
  62. #define OUTPUT_PROC_TIMEOUT 50
  63. /** Step between fake timestamps passed to hardware in units of 100ns */
  64. #define TIMESTAMP_UNIT 100000
  65. /*****************************************************************************
  66. * Module private data
  67. ****************************************************************************/
  68. typedef enum {
  69. RET_ERROR = -1,
  70. RET_OK = 0,
  71. RET_COPY_AGAIN = 1,
  72. } CopyRet;
  73. typedef struct OpaqueList {
  74. struct OpaqueList *next;
  75. uint64_t fake_timestamp;
  76. uint64_t reordered_opaque;
  77. } OpaqueList;
  78. typedef struct {
  79. AVClass *av_class;
  80. AVCodecContext *avctx;
  81. HANDLE dev;
  82. uint8_t is_70012;
  83. uint8_t need_second_field;
  84. uint8_t draining;
  85. OpaqueList *head;
  86. OpaqueList *tail;
  87. /* Options */
  88. uint32_t sWidth;
  89. } CHDContext;
  90. static const AVOption options[] = {
  91. { "crystalhd_downscale_width",
  92. "Turn on downscaling to the specified width",
  93. offsetof(CHDContext, sWidth),
  94. AV_OPT_TYPE_INT, {.i64 = 0}, 0, UINT32_MAX,
  95. AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM, },
  96. { NULL, },
  97. };
  98. /*****************************************************************************
  99. * Helper functions
  100. ****************************************************************************/
  101. static inline BC_MEDIA_SUBTYPE id2subtype(CHDContext *priv, enum AVCodecID id)
  102. {
  103. switch (id) {
  104. case AV_CODEC_ID_MPEG4:
  105. return BC_MSUBTYPE_DIVX;
  106. case AV_CODEC_ID_MSMPEG4V3:
  107. return BC_MSUBTYPE_DIVX311;
  108. case AV_CODEC_ID_MPEG2VIDEO:
  109. return BC_MSUBTYPE_MPEG2VIDEO;
  110. case AV_CODEC_ID_VC1:
  111. return BC_MSUBTYPE_VC1;
  112. case AV_CODEC_ID_WMV3:
  113. return BC_MSUBTYPE_WMV3;
  114. case AV_CODEC_ID_H264:
  115. return BC_MSUBTYPE_H264;
  116. default:
  117. return BC_MSUBTYPE_INVALID;
  118. }
  119. }
  120. static inline void print_frame_info(CHDContext *priv, BC_DTS_PROC_OUT *output)
  121. {
  122. av_log(priv->avctx, AV_LOG_TRACE, "\tYBuffSz: %u\n", output->YbuffSz);
  123. av_log(priv->avctx, AV_LOG_TRACE, "\tYBuffDoneSz: %u\n",
  124. output->YBuffDoneSz);
  125. av_log(priv->avctx, AV_LOG_TRACE, "\tUVBuffDoneSz: %u\n",
  126. output->UVBuffDoneSz);
  127. av_log(priv->avctx, AV_LOG_TRACE, "\tTimestamp: %"PRIu64"\n",
  128. output->PicInfo.timeStamp);
  129. av_log(priv->avctx, AV_LOG_TRACE, "\tPicture Number: %u\n",
  130. output->PicInfo.picture_number);
  131. av_log(priv->avctx, AV_LOG_TRACE, "\tWidth: %u\n",
  132. output->PicInfo.width);
  133. av_log(priv->avctx, AV_LOG_TRACE, "\tHeight: %u\n",
  134. output->PicInfo.height);
  135. av_log(priv->avctx, AV_LOG_TRACE, "\tChroma: 0x%03x\n",
  136. output->PicInfo.chroma_format);
  137. av_log(priv->avctx, AV_LOG_TRACE, "\tPulldown: %u\n",
  138. output->PicInfo.pulldown);
  139. av_log(priv->avctx, AV_LOG_TRACE, "\tFlags: 0x%08x\n",
  140. output->PicInfo.flags);
  141. av_log(priv->avctx, AV_LOG_TRACE, "\tFrame Rate/Res: %u\n",
  142. output->PicInfo.frame_rate);
  143. av_log(priv->avctx, AV_LOG_TRACE, "\tAspect Ratio: %u\n",
  144. output->PicInfo.aspect_ratio);
  145. av_log(priv->avctx, AV_LOG_TRACE, "\tColor Primaries: %u\n",
  146. output->PicInfo.colour_primaries);
  147. av_log(priv->avctx, AV_LOG_TRACE, "\tMetaData: %u\n",
  148. output->PicInfo.picture_meta_payload);
  149. av_log(priv->avctx, AV_LOG_TRACE, "\tSession Number: %u\n",
  150. output->PicInfo.sess_num);
  151. av_log(priv->avctx, AV_LOG_TRACE, "\tycom: %u\n",
  152. output->PicInfo.ycom);
  153. av_log(priv->avctx, AV_LOG_TRACE, "\tCustom Aspect: %u\n",
  154. output->PicInfo.custom_aspect_ratio_width_height);
  155. av_log(priv->avctx, AV_LOG_TRACE, "\tFrames to Drop: %u\n",
  156. output->PicInfo.n_drop);
  157. av_log(priv->avctx, AV_LOG_TRACE, "\tH264 Valid Fields: 0x%08x\n",
  158. output->PicInfo.other.h264.valid);
  159. }
  160. /*****************************************************************************
  161. * OpaqueList functions
  162. ****************************************************************************/
  163. static uint64_t opaque_list_push(CHDContext *priv, uint64_t reordered_opaque)
  164. {
  165. OpaqueList *newNode = av_mallocz(sizeof (OpaqueList));
  166. if (!newNode) {
  167. av_log(priv->avctx, AV_LOG_ERROR,
  168. "Unable to allocate new node in OpaqueList.\n");
  169. return 0;
  170. }
  171. if (!priv->head) {
  172. newNode->fake_timestamp = TIMESTAMP_UNIT;
  173. priv->head = newNode;
  174. } else {
  175. newNode->fake_timestamp = priv->tail->fake_timestamp + TIMESTAMP_UNIT;
  176. priv->tail->next = newNode;
  177. }
  178. priv->tail = newNode;
  179. newNode->reordered_opaque = reordered_opaque;
  180. return newNode->fake_timestamp;
  181. }
  182. /*
  183. * The OpaqueList is built in decode order, while elements will be removed
  184. * in presentation order. If frames are reordered, this means we must be
  185. * able to remove elements that are not the first element.
  186. *
  187. * Returned node must be freed by caller.
  188. */
  189. static OpaqueList *opaque_list_pop(CHDContext *priv, uint64_t fake_timestamp)
  190. {
  191. OpaqueList *node = priv->head;
  192. if (!priv->head) {
  193. av_log(priv->avctx, AV_LOG_ERROR,
  194. "CrystalHD: Attempted to query non-existent timestamps.\n");
  195. return NULL;
  196. }
  197. /*
  198. * The first element is special-cased because we have to manipulate
  199. * the head pointer rather than the previous element in the list.
  200. */
  201. if (priv->head->fake_timestamp == fake_timestamp) {
  202. priv->head = node->next;
  203. if (!priv->head->next)
  204. priv->tail = priv->head;
  205. node->next = NULL;
  206. return node;
  207. }
  208. /*
  209. * The list is processed at arm's length so that we have the
  210. * previous element available to rewrite its next pointer.
  211. */
  212. while (node->next) {
  213. OpaqueList *current = node->next;
  214. if (current->fake_timestamp == fake_timestamp) {
  215. node->next = current->next;
  216. if (!node->next)
  217. priv->tail = node;
  218. current->next = NULL;
  219. return current;
  220. } else {
  221. node = current;
  222. }
  223. }
  224. av_log(priv->avctx, AV_LOG_VERBOSE,
  225. "CrystalHD: Couldn't match fake_timestamp.\n");
  226. return NULL;
  227. }
  228. /*****************************************************************************
  229. * Video decoder API function definitions
  230. ****************************************************************************/
  231. static void flush(AVCodecContext *avctx)
  232. {
  233. CHDContext *priv = avctx->priv_data;
  234. priv->need_second_field = 0;
  235. priv->draining = 0;
  236. /* Flush mode 4 flushes all software and hardware buffers. */
  237. DtsFlushInput(priv->dev, 4);
  238. }
  239. static av_cold int uninit(AVCodecContext *avctx)
  240. {
  241. CHDContext *priv = avctx->priv_data;
  242. HANDLE device;
  243. device = priv->dev;
  244. DtsStopDecoder(device);
  245. DtsCloseDecoder(device);
  246. DtsDeviceClose(device);
  247. if (priv->head) {
  248. OpaqueList *node = priv->head;
  249. while (node) {
  250. OpaqueList *next = node->next;
  251. av_free(node);
  252. node = next;
  253. }
  254. }
  255. return 0;
  256. }
  257. static av_cold int init(AVCodecContext *avctx)
  258. {
  259. CHDContext* priv;
  260. BC_STATUS ret;
  261. BC_INFO_CRYSTAL version;
  262. BC_INPUT_FORMAT format = {
  263. .FGTEnable = FALSE,
  264. .Progressive = TRUE,
  265. .OptFlags = 0x80000000 | vdecFrameRate59_94 | 0x40,
  266. .width = avctx->width,
  267. .height = avctx->height,
  268. };
  269. BC_MEDIA_SUBTYPE subtype;
  270. uint32_t mode = DTS_PLAYBACK_MODE |
  271. DTS_LOAD_FILE_PLAY_FW |
  272. DTS_SKIP_TX_CHK_CPB |
  273. DTS_PLAYBACK_DROP_RPT_MODE |
  274. DTS_SINGLE_THREADED_MODE |
  275. DTS_DFLT_RESOLUTION(vdecRESOLUTION_1080p23_976);
  276. av_log(avctx, AV_LOG_VERBOSE, "CrystalHD Init for %s\n",
  277. avctx->codec->name);
  278. avctx->pix_fmt = AV_PIX_FMT_YUYV422;
  279. /* Initialize the library */
  280. priv = avctx->priv_data;
  281. priv->avctx = avctx;
  282. priv->draining = 0;
  283. subtype = id2subtype(priv, avctx->codec->id);
  284. switch (subtype) {
  285. case BC_MSUBTYPE_H264:
  286. format.startCodeSz = 4;
  287. // Fall-through
  288. case BC_MSUBTYPE_VC1:
  289. case BC_MSUBTYPE_WVC1:
  290. case BC_MSUBTYPE_WMV3:
  291. case BC_MSUBTYPE_WMVA:
  292. case BC_MSUBTYPE_MPEG2VIDEO:
  293. case BC_MSUBTYPE_DIVX:
  294. case BC_MSUBTYPE_DIVX311:
  295. format.pMetaData = avctx->extradata;
  296. format.metaDataSz = avctx->extradata_size;
  297. break;
  298. default:
  299. av_log(avctx, AV_LOG_ERROR, "CrystalHD: Unknown codec name\n");
  300. return AVERROR(EINVAL);
  301. }
  302. format.mSubtype = subtype;
  303. if (priv->sWidth) {
  304. format.bEnableScaling = 1;
  305. format.ScalingParams.sWidth = priv->sWidth;
  306. }
  307. /* Get a decoder instance */
  308. av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: starting up\n");
  309. // Initialize the Link and Decoder devices
  310. ret = DtsDeviceOpen(&priv->dev, mode);
  311. if (ret != BC_STS_SUCCESS) {
  312. av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: DtsDeviceOpen failed\n");
  313. goto fail;
  314. }
  315. ret = DtsCrystalHDVersion(priv->dev, &version);
  316. if (ret != BC_STS_SUCCESS) {
  317. av_log(avctx, AV_LOG_VERBOSE,
  318. "CrystalHD: DtsCrystalHDVersion failed\n");
  319. goto fail;
  320. }
  321. priv->is_70012 = version.device == 0;
  322. if (priv->is_70012 &&
  323. (subtype == BC_MSUBTYPE_DIVX || subtype == BC_MSUBTYPE_DIVX311)) {
  324. av_log(avctx, AV_LOG_VERBOSE,
  325. "CrystalHD: BCM70012 doesn't support MPEG4-ASP/DivX/Xvid\n");
  326. goto fail;
  327. }
  328. ret = DtsSetInputFormat(priv->dev, &format);
  329. if (ret != BC_STS_SUCCESS) {
  330. av_log(avctx, AV_LOG_ERROR, "CrystalHD: SetInputFormat failed\n");
  331. goto fail;
  332. }
  333. ret = DtsOpenDecoder(priv->dev, BC_STREAM_TYPE_ES);
  334. if (ret != BC_STS_SUCCESS) {
  335. av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsOpenDecoder failed\n");
  336. goto fail;
  337. }
  338. ret = DtsSetColorSpace(priv->dev, OUTPUT_MODE422_YUY2);
  339. if (ret != BC_STS_SUCCESS) {
  340. av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsSetColorSpace failed\n");
  341. goto fail;
  342. }
  343. ret = DtsStartDecoder(priv->dev);
  344. if (ret != BC_STS_SUCCESS) {
  345. av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartDecoder failed\n");
  346. goto fail;
  347. }
  348. ret = DtsStartCapture(priv->dev);
  349. if (ret != BC_STS_SUCCESS) {
  350. av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartCapture failed\n");
  351. goto fail;
  352. }
  353. av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Init complete.\n");
  354. return 0;
  355. fail:
  356. uninit(avctx);
  357. return -1;
  358. }
  359. static inline CopyRet copy_frame(AVCodecContext *avctx,
  360. BC_DTS_PROC_OUT *output,
  361. AVFrame *frame, int *got_frame)
  362. {
  363. BC_STATUS ret;
  364. BC_DTS_STATUS decoder_status = { 0, };
  365. uint8_t interlaced;
  366. CHDContext *priv = avctx->priv_data;
  367. int64_t pkt_pts = AV_NOPTS_VALUE;
  368. uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) ==
  369. VDEC_FLAG_BOTTOMFIELD;
  370. uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST);
  371. int width = output->PicInfo.width;
  372. int height = output->PicInfo.height;
  373. int bwidth;
  374. uint8_t *src = output->Ybuff;
  375. int sStride;
  376. uint8_t *dst;
  377. int dStride;
  378. if (output->PicInfo.timeStamp != 0) {
  379. OpaqueList *node = opaque_list_pop(priv, output->PicInfo.timeStamp);
  380. if (node) {
  381. pkt_pts = node->reordered_opaque;
  382. av_free(node);
  383. } else {
  384. /*
  385. * We will encounter a situation where a timestamp cannot be
  386. * popped if a second field is being returned. In this case,
  387. * each field has the same timestamp and the first one will
  388. * cause it to be popped. We'll avoid overwriting the valid
  389. * timestamp below.
  390. */
  391. }
  392. av_log(avctx, AV_LOG_VERBOSE, "output \"pts\": %"PRIu64"\n",
  393. output->PicInfo.timeStamp);
  394. }
  395. ret = DtsGetDriverStatus(priv->dev, &decoder_status);
  396. if (ret != BC_STS_SUCCESS) {
  397. av_log(avctx, AV_LOG_ERROR,
  398. "CrystalHD: GetDriverStatus failed: %u\n", ret);
  399. return RET_ERROR;
  400. }
  401. interlaced = output->PicInfo.flags & VDEC_FLAG_INTERLACED_SRC;
  402. av_log(avctx, AV_LOG_VERBOSE, "Interlaced state: %d\n",
  403. interlaced);
  404. priv->need_second_field = interlaced && !priv->need_second_field;
  405. if (!frame->data[0]) {
  406. if (ff_get_buffer(avctx, frame, 0) < 0)
  407. return RET_ERROR;
  408. }
  409. bwidth = av_image_get_linesize(avctx->pix_fmt, width, 0);
  410. if (bwidth < 0)
  411. return RET_ERROR;
  412. if (priv->is_70012) {
  413. int pStride;
  414. if (width <= 720)
  415. pStride = 720;
  416. else if (width <= 1280)
  417. pStride = 1280;
  418. else pStride = 1920;
  419. sStride = av_image_get_linesize(avctx->pix_fmt, pStride, 0);
  420. if (sStride < 0)
  421. return RET_ERROR;
  422. } else {
  423. sStride = bwidth;
  424. }
  425. dStride = frame->linesize[0];
  426. dst = frame->data[0];
  427. av_log(priv->avctx, AV_LOG_VERBOSE, "CrystalHD: Copying out frame\n");
  428. /*
  429. * The hardware doesn't return the first sample of a picture.
  430. * Ignoring why it behaves this way, it's better to copy the sample from
  431. * the second line, rather than the next sample across because the chroma
  432. * values should be correct (assuming the decoded video was 4:2:0, which
  433. * it was).
  434. */
  435. *((uint32_t *)src) = *((uint32_t *)(src + sStride));
  436. if (interlaced) {
  437. int dY = 0;
  438. int sY = 0;
  439. height /= 2;
  440. if (bottom_field) {
  441. av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: bottom field\n");
  442. dY = 1;
  443. } else {
  444. av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: top field\n");
  445. dY = 0;
  446. }
  447. for (sY = 0; sY < height; dY++, sY++) {
  448. memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth);
  449. dY++;
  450. }
  451. } else {
  452. av_image_copy_plane(dst, dStride, src, sStride, bwidth, height);
  453. }
  454. frame->interlaced_frame = interlaced;
  455. if (interlaced)
  456. frame->top_field_first = !bottom_first;
  457. frame->pts = pkt_pts;
  458. #if FF_API_PKT_PTS
  459. FF_DISABLE_DEPRECATION_WARNINGS
  460. frame->pkt_pts = pkt_pts;
  461. FF_ENABLE_DEPRECATION_WARNINGS
  462. #endif
  463. frame->pkt_pos = -1;
  464. frame->pkt_duration = 0;
  465. frame->pkt_size = -1;
  466. if (!priv->need_second_field) {
  467. *got_frame = 1;
  468. } else {
  469. return RET_COPY_AGAIN;
  470. }
  471. return RET_OK;
  472. }
  473. static inline CopyRet receive_frame(AVCodecContext *avctx,
  474. AVFrame *frame, int *got_frame)
  475. {
  476. BC_STATUS ret;
  477. BC_DTS_PROC_OUT output = {
  478. .PicInfo.width = avctx->width,
  479. .PicInfo.height = avctx->height,
  480. };
  481. CHDContext *priv = avctx->priv_data;
  482. HANDLE dev = priv->dev;
  483. *got_frame = 0;
  484. // Request decoded data from the driver
  485. ret = DtsProcOutputNoCopy(dev, OUTPUT_PROC_TIMEOUT, &output);
  486. if (ret == BC_STS_FMT_CHANGE) {
  487. av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Initial format change\n");
  488. avctx->width = output.PicInfo.width;
  489. avctx->height = output.PicInfo.height;
  490. switch ( output.PicInfo.aspect_ratio ) {
  491. case vdecAspectRatioSquare:
  492. avctx->sample_aspect_ratio = (AVRational) { 1, 1};
  493. break;
  494. case vdecAspectRatio12_11:
  495. avctx->sample_aspect_ratio = (AVRational) { 12, 11};
  496. break;
  497. case vdecAspectRatio10_11:
  498. avctx->sample_aspect_ratio = (AVRational) { 10, 11};
  499. break;
  500. case vdecAspectRatio16_11:
  501. avctx->sample_aspect_ratio = (AVRational) { 16, 11};
  502. break;
  503. case vdecAspectRatio40_33:
  504. avctx->sample_aspect_ratio = (AVRational) { 40, 33};
  505. break;
  506. case vdecAspectRatio24_11:
  507. avctx->sample_aspect_ratio = (AVRational) { 24, 11};
  508. break;
  509. case vdecAspectRatio20_11:
  510. avctx->sample_aspect_ratio = (AVRational) { 20, 11};
  511. break;
  512. case vdecAspectRatio32_11:
  513. avctx->sample_aspect_ratio = (AVRational) { 32, 11};
  514. break;
  515. case vdecAspectRatio80_33:
  516. avctx->sample_aspect_ratio = (AVRational) { 80, 33};
  517. break;
  518. case vdecAspectRatio18_11:
  519. avctx->sample_aspect_ratio = (AVRational) { 18, 11};
  520. break;
  521. case vdecAspectRatio15_11:
  522. avctx->sample_aspect_ratio = (AVRational) { 15, 11};
  523. break;
  524. case vdecAspectRatio64_33:
  525. avctx->sample_aspect_ratio = (AVRational) { 64, 33};
  526. break;
  527. case vdecAspectRatio160_99:
  528. avctx->sample_aspect_ratio = (AVRational) {160, 99};
  529. break;
  530. case vdecAspectRatio4_3:
  531. avctx->sample_aspect_ratio = (AVRational) { 4, 3};
  532. break;
  533. case vdecAspectRatio16_9:
  534. avctx->sample_aspect_ratio = (AVRational) { 16, 9};
  535. break;
  536. case vdecAspectRatio221_1:
  537. avctx->sample_aspect_ratio = (AVRational) {221, 1};
  538. break;
  539. }
  540. return RET_COPY_AGAIN;
  541. } else if (ret == BC_STS_SUCCESS) {
  542. int copy_ret = -1;
  543. if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
  544. print_frame_info(priv, &output);
  545. copy_ret = copy_frame(avctx, &output, frame, got_frame);
  546. } else {
  547. /*
  548. * An invalid frame has been consumed.
  549. */
  550. av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput succeeded with "
  551. "invalid PIB\n");
  552. copy_ret = RET_COPY_AGAIN;
  553. }
  554. DtsReleaseOutputBuffs(dev, NULL, FALSE);
  555. return copy_ret;
  556. } else if (ret == BC_STS_BUSY) {
  557. return RET_COPY_AGAIN;
  558. } else {
  559. av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput failed %d\n", ret);
  560. return RET_ERROR;
  561. }
  562. }
  563. static int crystalhd_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)
  564. {
  565. BC_STATUS bc_ret;
  566. CHDContext *priv = avctx->priv_data;
  567. HANDLE dev = priv->dev;
  568. int ret = 0;
  569. av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: decode_packet\n");
  570. if (avpkt && avpkt->size) {
  571. uint64_t pts;
  572. /*
  573. * Despite being notionally opaque, either libcrystalhd or
  574. * the hardware itself will mangle pts values that are too
  575. * small or too large. The docs claim it should be in units
  576. * of 100ns. Given that we're nominally dealing with a black
  577. * box on both sides, any transform we do has no guarantee of
  578. * avoiding mangling so we need to build a mapping to values
  579. * we know will not be mangled.
  580. */
  581. pts = opaque_list_push(priv, avpkt->pts);
  582. if (!pts) {
  583. ret = AVERROR(ENOMEM);
  584. goto exit;
  585. }
  586. av_log(priv->avctx, AV_LOG_VERBOSE,
  587. "input \"pts\": %"PRIu64"\n", pts);
  588. bc_ret = DtsProcInput(dev, avpkt->data, avpkt->size, pts, 0);
  589. if (bc_ret == BC_STS_BUSY) {
  590. av_log(avctx, AV_LOG_WARNING,
  591. "CrystalHD: ProcInput returned busy\n");
  592. ret = AVERROR(EAGAIN);
  593. goto exit;
  594. } else if (bc_ret != BC_STS_SUCCESS) {
  595. av_log(avctx, AV_LOG_ERROR,
  596. "CrystalHD: ProcInput failed: %u\n", ret);
  597. ret = -1;
  598. goto exit;
  599. }
  600. } else {
  601. av_log(avctx, AV_LOG_INFO, "CrystalHD: No more input data\n");
  602. priv->draining = 1;
  603. ret = AVERROR_EOF;
  604. goto exit;
  605. }
  606. exit:
  607. return ret;
  608. }
  609. static int crystalhd_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  610. {
  611. BC_STATUS bc_ret;
  612. BC_DTS_STATUS decoder_status = { 0, };
  613. CopyRet rec_ret;
  614. CHDContext *priv = avctx->priv_data;
  615. HANDLE dev = priv->dev;
  616. int got_frame = 0;
  617. int ret = 0;
  618. AVPacket pkt = {0};
  619. av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: receive_frame\n");
  620. ret = ff_decode_get_packet(avctx, &pkt);
  621. if (ret < 0 && ret != AVERROR_EOF) {
  622. return ret;
  623. }
  624. while (pkt.size > DtsTxFreeSize(dev)) {
  625. /*
  626. * Block until there is space in the buffer for the next packet.
  627. * We assume that the hardware will make forward progress at this
  628. * point, although in pathological cases that may not happen.
  629. */
  630. av_log(avctx, AV_LOG_TRACE, "CrystalHD: Waiting for space in input buffer\n");
  631. }
  632. ret = crystalhd_decode_packet(avctx, &pkt);
  633. av_packet_unref(&pkt);
  634. // crystalhd_is_buffer_full() should avoid this.
  635. if (ret == AVERROR(EAGAIN)) {
  636. ret = AVERROR_EXTERNAL;
  637. }
  638. if (ret < 0 && ret != AVERROR_EOF) {
  639. return ret;
  640. }
  641. do {
  642. bc_ret = DtsGetDriverStatus(dev, &decoder_status);
  643. if (bc_ret != BC_STS_SUCCESS) {
  644. av_log(avctx, AV_LOG_ERROR, "CrystalHD: GetDriverStatus failed\n");
  645. return -1;
  646. }
  647. if (decoder_status.ReadyListCount == 0) {
  648. av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Insufficient frames ready. Returning\n");
  649. got_frame = 0;
  650. rec_ret = RET_OK;
  651. break;
  652. }
  653. rec_ret = receive_frame(avctx, frame, &got_frame);
  654. } while (rec_ret == RET_COPY_AGAIN);
  655. if (rec_ret == RET_ERROR) {
  656. return -1;
  657. } else if (got_frame == 0) {
  658. return priv->draining ? AVERROR_EOF : AVERROR(EAGAIN);
  659. } else {
  660. return 0;
  661. }
  662. }
  663. #define DEFINE_CRYSTALHD_DECODER(x, X, bsf_name) \
  664. static const AVClass x##_crystalhd_class = { \
  665. .class_name = #x "_crystalhd", \
  666. .item_name = av_default_item_name, \
  667. .option = options, \
  668. .version = LIBAVUTIL_VERSION_INT, \
  669. }; \
  670. AVCodec ff_##x##_crystalhd_decoder = { \
  671. .name = #x "_crystalhd", \
  672. .long_name = NULL_IF_CONFIG_SMALL("CrystalHD " #X " decoder"), \
  673. .type = AVMEDIA_TYPE_VIDEO, \
  674. .id = AV_CODEC_ID_##X, \
  675. .priv_data_size = sizeof(CHDContext), \
  676. .priv_class = &x##_crystalhd_class, \
  677. .init = init, \
  678. .close = uninit, \
  679. .receive_frame = crystalhd_receive_frame, \
  680. .flush = flush, \
  681. .bsfs = bsf_name, \
  682. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
  683. .pix_fmts = (const enum AVPixelFormat[]){AV_PIX_FMT_YUYV422, AV_PIX_FMT_NONE}, \
  684. .wrapper_name = "crystalhd", \
  685. };
  686. #if CONFIG_H264_CRYSTALHD_DECODER
  687. DEFINE_CRYSTALHD_DECODER(h264, H264, "h264_mp4toannexb")
  688. #endif
  689. #if CONFIG_MPEG2_CRYSTALHD_DECODER
  690. DEFINE_CRYSTALHD_DECODER(mpeg2, MPEG2VIDEO, NULL)
  691. #endif
  692. #if CONFIG_MPEG4_CRYSTALHD_DECODER
  693. DEFINE_CRYSTALHD_DECODER(mpeg4, MPEG4, "mpeg4_unpack_bframes")
  694. #endif
  695. #if CONFIG_MSMPEG4_CRYSTALHD_DECODER
  696. DEFINE_CRYSTALHD_DECODER(msmpeg4, MSMPEG4V3, NULL)
  697. #endif
  698. #if CONFIG_VC1_CRYSTALHD_DECODER
  699. DEFINE_CRYSTALHD_DECODER(vc1, VC1, NULL)
  700. #endif
  701. #if CONFIG_WMV3_CRYSTALHD_DECODER
  702. DEFINE_CRYSTALHD_DECODER(wmv3, WMV3, NULL)
  703. #endif