FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
crystalhd.c
Go to the documentation of this file.
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 /*
24  * - Principles of Operation -
25  *
26  * The CrystalHD decoder operates at the bitstream level - which is an even
27  * higher level than the decoding hardware you typically see in modern GPUs.
28  * This means it has a very simple interface, in principle. You feed demuxed
29  * packets in one end and get decoded picture (fields/frames) out the other.
30  *
31  * Of course, nothing is ever that simple. Due, at the very least, to b-frame
32  * dependencies in the supported formats, the hardware has a delay between
33  * when a packet goes in, and when a picture comes out. Furthermore, this delay
34  * is not just a function of time, but also one of the dependency on additional
35  * frames being fed into the decoder to satisfy the b-frame dependencies.
36  *
37  * As such, a pipeline will build up that is roughly equivalent to the required
38  * DPB for the file being played. If that was all it took, things would still
39  * be simple - so, of course, it isn't.
40  *
41  * The hardware has a way of indicating that a picture is ready to be copied out,
42  * but this is unreliable - and sometimes the attempt will still fail so, based
43  * on testing, the code will wait until 3 pictures are ready before starting
44  * to copy out - and this has the effect of extending the pipeline.
45  *
46  * Finally, while it is tempting to say that once the decoder starts outputting
47  * frames, the software should never fail to return a frame from a decode(),
48  * this is a hard assertion to make, because the stream may switch between
49  * differently encoded content (number of b-frames, interlacing, etc) which
50  * might require a longer pipeline than before. If that happened, you could
51  * deadlock trying to retrieve a frame that can't be decoded without feeding
52  * in additional packets.
53  *
54  * As such, the code will return in the event that a picture cannot be copied
55  * out, leading to an increase in the length of the pipeline. This in turn,
56  * means we have to be sensitive to the time it takes to decode a picture;
57  * We do not want to give up just because the hardware needed a little more
58  * time to prepare the picture! For this reason, there are delays included
59  * in the decode() path that ensure that, under normal conditions, the hardware
60  * will only fail to return a frame if it really needs additional packets to
61  * complete the decoding.
62  *
63  * Finally, to be explicit, we do not want the pipeline to grow without bound
64  * for two reasons: 1) The hardware can only buffer a finite number of packets,
65  * and 2) The client application may not be able to cope with arbitrarily long
66  * delays in the video path relative to the audio path. For example. MPlayer
67  * can only handle a 20 picture delay (although this is arbitrary, and needs
68  * to be extended to fully support the CrystalHD where the delay could be up
69  * to 32 pictures - consider PAFF H.264 content with 16 b-frames).
70  */
71 
72 /*****************************************************************************
73  * Includes
74  ****************************************************************************/
75 
76 #define _XOPEN_SOURCE 600
77 #include <inttypes.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 
81 #include <libcrystalhd/bc_dts_types.h>
82 #include <libcrystalhd/bc_dts_defs.h>
83 #include <libcrystalhd/libcrystalhd_if.h>
84 
85 #include "avcodec.h"
86 #include "h264dec.h"
87 #include "internal.h"
88 #include "libavutil/imgutils.h"
89 #include "libavutil/intreadwrite.h"
90 #include "libavutil/opt.h"
91 
92 #if HAVE_UNISTD_H
93 #include <unistd.h>
94 #endif
95 
96 /** Timeout parameter passed to DtsProcOutput() in us */
97 #define OUTPUT_PROC_TIMEOUT 50
98 /** Step between fake timestamps passed to hardware in units of 100ns */
99 #define TIMESTAMP_UNIT 100000
100 /** Initial value in us of the wait in decode() */
101 #define BASE_WAIT 10000
102 /** Increment in us to adjust wait in decode() */
103 #define WAIT_UNIT 1000
104 
105 
106 /*****************************************************************************
107  * Module private data
108  ****************************************************************************/
109 
110 typedef enum {
111  RET_ERROR = -1,
112  RET_OK = 0,
116 } CopyRet;
117 
118 typedef struct OpaqueList {
119  struct OpaqueList *next;
120  uint64_t fake_timestamp;
123 } OpaqueList;
124 
125 typedef struct {
130 
133 
136 
139  uint32_t sps_pps_size;
144  uint64_t decode_wait;
145 
146  uint64_t last_picture;
147 
150 
151  /* Options */
152  uint32_t sWidth;
154 } CHDContext;
155 
156 static const AVOption options[] = {
157  { "crystalhd_downscale_width",
158  "Turn on downscaling to the specified width",
159  offsetof(CHDContext, sWidth),
160  AV_OPT_TYPE_INT, {.i64 = 0}, 0, UINT32_MAX,
162  { NULL, },
163 };
164 
165 
166 /*****************************************************************************
167  * Helper functions
168  ****************************************************************************/
169 
170 static inline BC_MEDIA_SUBTYPE id2subtype(CHDContext *priv, enum AVCodecID id)
171 {
172  switch (id) {
173  case AV_CODEC_ID_MPEG4:
174  return BC_MSUBTYPE_DIVX;
176  return BC_MSUBTYPE_DIVX311;
178  return BC_MSUBTYPE_MPEG2VIDEO;
179  case AV_CODEC_ID_VC1:
180  return BC_MSUBTYPE_VC1;
181  case AV_CODEC_ID_WMV3:
182  return BC_MSUBTYPE_WMV3;
183  case AV_CODEC_ID_H264:
184  return priv->is_nal ? BC_MSUBTYPE_AVC1 : BC_MSUBTYPE_H264;
185  default:
186  return BC_MSUBTYPE_INVALID;
187  }
188 }
189 
190 static inline void print_frame_info(CHDContext *priv, BC_DTS_PROC_OUT *output)
191 {
192  av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffSz: %u\n", output->YbuffSz);
193  av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffDoneSz: %u\n",
194  output->YBuffDoneSz);
195  av_log(priv->avctx, AV_LOG_VERBOSE, "\tUVBuffDoneSz: %u\n",
196  output->UVBuffDoneSz);
197  av_log(priv->avctx, AV_LOG_VERBOSE, "\tTimestamp: %"PRIu64"\n",
198  output->PicInfo.timeStamp);
199  av_log(priv->avctx, AV_LOG_VERBOSE, "\tPicture Number: %u\n",
200  output->PicInfo.picture_number);
201  av_log(priv->avctx, AV_LOG_VERBOSE, "\tWidth: %u\n",
202  output->PicInfo.width);
203  av_log(priv->avctx, AV_LOG_VERBOSE, "\tHeight: %u\n",
204  output->PicInfo.height);
205  av_log(priv->avctx, AV_LOG_VERBOSE, "\tChroma: 0x%03x\n",
206  output->PicInfo.chroma_format);
207  av_log(priv->avctx, AV_LOG_VERBOSE, "\tPulldown: %u\n",
208  output->PicInfo.pulldown);
209  av_log(priv->avctx, AV_LOG_VERBOSE, "\tFlags: 0x%08x\n",
210  output->PicInfo.flags);
211  av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrame Rate/Res: %u\n",
212  output->PicInfo.frame_rate);
213  av_log(priv->avctx, AV_LOG_VERBOSE, "\tAspect Ratio: %u\n",
214  output->PicInfo.aspect_ratio);
215  av_log(priv->avctx, AV_LOG_VERBOSE, "\tColor Primaries: %u\n",
216  output->PicInfo.colour_primaries);
217  av_log(priv->avctx, AV_LOG_VERBOSE, "\tMetaData: %u\n",
218  output->PicInfo.picture_meta_payload);
219  av_log(priv->avctx, AV_LOG_VERBOSE, "\tSession Number: %u\n",
220  output->PicInfo.sess_num);
221  av_log(priv->avctx, AV_LOG_VERBOSE, "\tycom: %u\n",
222  output->PicInfo.ycom);
223  av_log(priv->avctx, AV_LOG_VERBOSE, "\tCustom Aspect: %u\n",
224  output->PicInfo.custom_aspect_ratio_width_height);
225  av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrames to Drop: %u\n",
226  output->PicInfo.n_drop);
227  av_log(priv->avctx, AV_LOG_VERBOSE, "\tH264 Valid Fields: 0x%08x\n",
228  output->PicInfo.other.h264.valid);
229 }
230 
231 
232 /*****************************************************************************
233  * OpaqueList functions
234  ****************************************************************************/
235 
236 static uint64_t opaque_list_push(CHDContext *priv, uint64_t reordered_opaque,
237  uint8_t pic_type)
238 {
239  OpaqueList *newNode = av_mallocz(sizeof (OpaqueList));
240  if (!newNode) {
241  av_log(priv->avctx, AV_LOG_ERROR,
242  "Unable to allocate new node in OpaqueList.\n");
243  return 0;
244  }
245  if (!priv->head) {
246  newNode->fake_timestamp = TIMESTAMP_UNIT;
247  priv->head = newNode;
248  } else {
249  newNode->fake_timestamp = priv->tail->fake_timestamp + TIMESTAMP_UNIT;
250  priv->tail->next = newNode;
251  }
252  priv->tail = newNode;
253  newNode->reordered_opaque = reordered_opaque;
254  newNode->pic_type = pic_type;
255 
256  return newNode->fake_timestamp;
257 }
258 
259 /*
260  * The OpaqueList is built in decode order, while elements will be removed
261  * in presentation order. If frames are reordered, this means we must be
262  * able to remove elements that are not the first element.
263  *
264  * Returned node must be freed by caller.
265  */
266 static OpaqueList *opaque_list_pop(CHDContext *priv, uint64_t fake_timestamp)
267 {
268  OpaqueList *node = priv->head;
269 
270  if (!priv->head) {
271  av_log(priv->avctx, AV_LOG_ERROR,
272  "CrystalHD: Attempted to query non-existent timestamps.\n");
273  return NULL;
274  }
275 
276  /*
277  * The first element is special-cased because we have to manipulate
278  * the head pointer rather than the previous element in the list.
279  */
280  if (priv->head->fake_timestamp == fake_timestamp) {
281  priv->head = node->next;
282 
283  if (!priv->head->next)
284  priv->tail = priv->head;
285 
286  node->next = NULL;
287  return node;
288  }
289 
290  /*
291  * The list is processed at arm's length so that we have the
292  * previous element available to rewrite its next pointer.
293  */
294  while (node->next) {
295  OpaqueList *current = node->next;
296  if (current->fake_timestamp == fake_timestamp) {
297  node->next = current->next;
298 
299  if (!node->next)
300  priv->tail = node;
301 
302  current->next = NULL;
303  return current;
304  } else {
305  node = current;
306  }
307  }
308 
309  av_log(priv->avctx, AV_LOG_VERBOSE,
310  "CrystalHD: Couldn't match fake_timestamp.\n");
311  return NULL;
312 }
313 
314 
315 /*****************************************************************************
316  * Video decoder API function definitions
317  ****************************************************************************/
318 
319 static void flush(AVCodecContext *avctx)
320 {
321  CHDContext *priv = avctx->priv_data;
322 
323  avctx->has_b_frames = 0;
324  priv->last_picture = -1;
325  priv->output_ready = 0;
326  priv->need_second_field = 0;
327  priv->skip_next_output = 0;
328  priv->decode_wait = BASE_WAIT;
329 
330  av_frame_unref (priv->pic);
331 
332  /* Flush mode 4 flushes all software and hardware buffers. */
333  DtsFlushInput(priv->dev, 4);
334 }
335 
336 
337 static av_cold int uninit(AVCodecContext *avctx)
338 {
339  CHDContext *priv = avctx->priv_data;
340  HANDLE device;
341 
342  device = priv->dev;
343  DtsStopDecoder(device);
344  DtsCloseDecoder(device);
345  DtsDeviceClose(device);
346 
347  /*
348  * Restore original extradata, so that if the decoder is
349  * reinitialised, the bitstream detection and filtering
350  * will work as expected.
351  */
352  if (priv->orig_extradata) {
353  av_free(avctx->extradata);
354  avctx->extradata = priv->orig_extradata;
355  avctx->extradata_size = priv->orig_extradata_size;
356  priv->orig_extradata = NULL;
357  priv->orig_extradata_size = 0;
358  }
359 
360  av_parser_close(priv->parser);
361  if (priv->bsfc) {
362  av_bsf_free(&priv->bsfc);
363  }
364 
365  av_freep(&priv->sps_pps_buf);
366 
367  av_frame_free (&priv->pic);
368 
369  if (priv->head) {
370  OpaqueList *node = priv->head;
371  while (node) {
372  OpaqueList *next = node->next;
373  av_free(node);
374  node = next;
375  }
376  }
377 
378  return 0;
379 }
380 
381 
382 static av_cold int init(AVCodecContext *avctx)
383 {
384  CHDContext* priv;
385  BC_STATUS ret;
386  BC_INFO_CRYSTAL version;
387  BC_INPUT_FORMAT format = {
388  .FGTEnable = FALSE,
389  .Progressive = TRUE,
390  .OptFlags = 0x80000000 | vdecFrameRate59_94 | 0x40,
391  .width = avctx->width,
392  .height = avctx->height,
393  };
394 
395  BC_MEDIA_SUBTYPE subtype;
396 
397  uint32_t mode = DTS_PLAYBACK_MODE |
398  DTS_LOAD_FILE_PLAY_FW |
399  DTS_SKIP_TX_CHK_CPB |
400  DTS_PLAYBACK_DROP_RPT_MODE |
401  DTS_SINGLE_THREADED_MODE |
402  DTS_DFLT_RESOLUTION(vdecRESOLUTION_1080p23_976);
403 
404  av_log(avctx, AV_LOG_VERBOSE, "CrystalHD Init for %s\n",
405  avctx->codec->name);
406 
407  avctx->pix_fmt = AV_PIX_FMT_YUYV422;
408 
409  /* Initialize the library */
410  priv = avctx->priv_data;
411  priv->avctx = avctx;
412  priv->is_nal = avctx->extradata_size > 0 && *(avctx->extradata) == 1;
413  priv->last_picture = -1;
414  priv->decode_wait = BASE_WAIT;
415  priv->pic = av_frame_alloc();
416 
417  subtype = id2subtype(priv, avctx->codec->id);
418  switch (subtype) {
419  case BC_MSUBTYPE_AVC1:
420  {
421  const AVBitStreamFilter *bsf;
422  int avret;
423 
424  bsf = av_bsf_get_by_name("h264_mp4toannexb");
425  if (!bsf) {
426  av_log(avctx, AV_LOG_ERROR,
427  "Cannot open the h264_mp4toannexb BSF!\n");
428  return AVERROR_BSF_NOT_FOUND;
429  }
430  avret = av_bsf_alloc(bsf, &priv->bsfc);
431  if (avret != 0) {
432  return AVERROR(ENOMEM);
433  }
434  avret = avcodec_parameters_from_context(priv->bsfc->par_in, avctx);
435  if (avret != 0) {
436  return AVERROR(ENOMEM);
437  }
438  avret = av_bsf_init(priv->bsfc);
439  if (avret != 0) {
440  return AVERROR(ENOMEM);
441  }
442 
443  format.metaDataSz = priv->bsfc->par_out->extradata_size;
444  format.pMetaData = av_malloc(format.metaDataSz + AV_INPUT_BUFFER_PADDING_SIZE);
445  if (!format.pMetaData) {
446  av_log(avctx, AV_LOG_ERROR,
447  "Failed to allocate copy of extradata\n");
448  return AVERROR(ENOMEM);
449  }
450  memcpy(format.pMetaData, priv->bsfc->par_out->extradata, format.metaDataSz);
451 
452  /* Back up the extradata so it can be restored at close time. */
453  priv->orig_extradata = avctx->extradata;
454  priv->orig_extradata_size = avctx->extradata_size;
455  avctx->extradata = format.pMetaData;
456  avctx->extradata_size = format.metaDataSz;
457  }
458  subtype = BC_MSUBTYPE_H264;
459  format.startCodeSz = 4;
460  break;
461  case BC_MSUBTYPE_H264:
462  format.startCodeSz = 4;
463  // Fall-through
464  case BC_MSUBTYPE_VC1:
465  case BC_MSUBTYPE_WVC1:
466  case BC_MSUBTYPE_WMV3:
467  case BC_MSUBTYPE_WMVA:
468  case BC_MSUBTYPE_MPEG2VIDEO:
469  case BC_MSUBTYPE_DIVX:
470  case BC_MSUBTYPE_DIVX311:
471  format.pMetaData = avctx->extradata;
472  format.metaDataSz = avctx->extradata_size;
473  break;
474  default:
475  av_log(avctx, AV_LOG_ERROR, "CrystalHD: Unknown codec name\n");
476  return AVERROR(EINVAL);
477  }
478  format.mSubtype = subtype;
479 
480  if (priv->sWidth) {
481  format.bEnableScaling = 1;
482  format.ScalingParams.sWidth = priv->sWidth;
483  }
484 
485  /* Get a decoder instance */
486  av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: starting up\n");
487  // Initialize the Link and Decoder devices
488  ret = DtsDeviceOpen(&priv->dev, mode);
489  if (ret != BC_STS_SUCCESS) {
490  av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: DtsDeviceOpen failed\n");
491  goto fail;
492  }
493 
494  ret = DtsCrystalHDVersion(priv->dev, &version);
495  if (ret != BC_STS_SUCCESS) {
496  av_log(avctx, AV_LOG_VERBOSE,
497  "CrystalHD: DtsCrystalHDVersion failed\n");
498  goto fail;
499  }
500  priv->is_70012 = version.device == 0;
501 
502  if (priv->is_70012 &&
503  (subtype == BC_MSUBTYPE_DIVX || subtype == BC_MSUBTYPE_DIVX311)) {
504  av_log(avctx, AV_LOG_VERBOSE,
505  "CrystalHD: BCM70012 doesn't support MPEG4-ASP/DivX/Xvid\n");
506  goto fail;
507  }
508 
509  ret = DtsSetInputFormat(priv->dev, &format);
510  if (ret != BC_STS_SUCCESS) {
511  av_log(avctx, AV_LOG_ERROR, "CrystalHD: SetInputFormat failed\n");
512  goto fail;
513  }
514 
515  ret = DtsOpenDecoder(priv->dev, BC_STREAM_TYPE_ES);
516  if (ret != BC_STS_SUCCESS) {
517  av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsOpenDecoder failed\n");
518  goto fail;
519  }
520 
521  ret = DtsSetColorSpace(priv->dev, OUTPUT_MODE422_YUY2);
522  if (ret != BC_STS_SUCCESS) {
523  av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsSetColorSpace failed\n");
524  goto fail;
525  }
526  ret = DtsStartDecoder(priv->dev);
527  if (ret != BC_STS_SUCCESS) {
528  av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartDecoder failed\n");
529  goto fail;
530  }
531  ret = DtsStartCapture(priv->dev);
532  if (ret != BC_STS_SUCCESS) {
533  av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartCapture failed\n");
534  goto fail;
535  }
536 
537  if (avctx->codec->id == AV_CODEC_ID_H264) {
538  priv->parser = av_parser_init(avctx->codec->id);
539  if (!priv->parser)
540  av_log(avctx, AV_LOG_WARNING,
541  "Cannot open the h.264 parser! Interlaced h.264 content "
542  "will not be detected reliably.\n");
544  }
545  av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Init complete.\n");
546 
547  return 0;
548 
549  fail:
550  uninit(avctx);
551  return -1;
552 }
553 
554 
555 static inline CopyRet copy_frame(AVCodecContext *avctx,
556  BC_DTS_PROC_OUT *output,
557  void *data, int *got_frame)
558 {
559  BC_STATUS ret;
560  BC_DTS_STATUS decoder_status = { 0, };
561  uint8_t trust_interlaced;
563 
564  CHDContext *priv = avctx->priv_data;
565  int64_t pkt_pts = AV_NOPTS_VALUE;
566  uint8_t pic_type = 0;
567 
568  uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) ==
569  VDEC_FLAG_BOTTOMFIELD;
570  uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST);
571 
572  int width = output->PicInfo.width;
573  int height = output->PicInfo.height;
574  int bwidth;
575  uint8_t *src = output->Ybuff;
576  int sStride;
577  uint8_t *dst;
578  int dStride;
579 
580  if (output->PicInfo.timeStamp != 0) {
581  OpaqueList *node = opaque_list_pop(priv, output->PicInfo.timeStamp);
582  if (node) {
583  pkt_pts = node->reordered_opaque;
584  pic_type = node->pic_type;
585  av_free(node);
586  } else {
587  /*
588  * We will encounter a situation where a timestamp cannot be
589  * popped if a second field is being returned. In this case,
590  * each field has the same timestamp and the first one will
591  * cause it to be popped. To keep subsequent calculations
592  * simple, pic_type should be set a FIELD value - doesn't
593  * matter which, but I chose BOTTOM.
594  */
595  pic_type = PICT_BOTTOM_FIELD;
596  }
597  av_log(avctx, AV_LOG_VERBOSE, "output \"pts\": %"PRIu64"\n",
598  output->PicInfo.timeStamp);
599  av_log(avctx, AV_LOG_VERBOSE, "output picture type %d\n",
600  pic_type);
601  }
602 
603  ret = DtsGetDriverStatus(priv->dev, &decoder_status);
604  if (ret != BC_STS_SUCCESS) {
605  av_log(avctx, AV_LOG_ERROR,
606  "CrystalHD: GetDriverStatus failed: %u\n", ret);
607  return RET_ERROR;
608  }
609 
610  /*
611  * For most content, we can trust the interlaced flag returned
612  * by the hardware, but sometimes we can't. These are the
613  * conditions under which we can trust the flag:
614  *
615  * 1) It's not h.264 content
616  * 2) The UNKNOWN_SRC flag is not set
617  * 3) We know we're expecting a second field
618  * 4) The hardware reports this picture and the next picture
619  * have the same picture number.
620  *
621  * Note that there can still be interlaced content that will
622  * fail this check, if the hardware hasn't decoded the next
623  * picture or if there is a corruption in the stream. (In either
624  * case a 0 will be returned for the next picture number)
625  */
626  trust_interlaced = avctx->codec->id != AV_CODEC_ID_H264 ||
627  !(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
628  priv->need_second_field ||
629  (decoder_status.picNumFlags & ~0x40000000) ==
630  output->PicInfo.picture_number;
631 
632  /*
633  * If we got a false negative for trust_interlaced on the first field,
634  * we will realise our mistake here when we see that the picture number is that
635  * of the previous picture. We cannot recover the frame and should discard the
636  * second field to keep the correct number of output frames.
637  */
638  if (output->PicInfo.picture_number == priv->last_picture && !priv->need_second_field) {
639  av_log(avctx, AV_LOG_WARNING,
640  "Incorrectly guessed progressive frame. Discarding second field\n");
641  /* Returning without providing a picture. */
642  return RET_OK;
643  }
644 
645  interlaced = (output->PicInfo.flags & VDEC_FLAG_INTERLACED_SRC) &&
646  trust_interlaced;
647 
648  if (!trust_interlaced && (decoder_status.picNumFlags & ~0x40000000) == 0) {
649  av_log(avctx, AV_LOG_VERBOSE,
650  "Next picture number unknown. Assuming progressive frame.\n");
651  }
652 
653  av_log(avctx, AV_LOG_VERBOSE, "Interlaced state: %d | trust_interlaced %d\n",
654  interlaced, trust_interlaced);
655 
656  if (priv->pic->data[0] && !priv->need_second_field)
657  av_frame_unref(priv->pic);
658 
659  priv->need_second_field = interlaced && !priv->need_second_field;
660 
661  if (!priv->pic->data[0]) {
662  if (ff_get_buffer(avctx, priv->pic, AV_GET_BUFFER_FLAG_REF) < 0)
663  return RET_ERROR;
664  }
665 
666  bwidth = av_image_get_linesize(avctx->pix_fmt, width, 0);
667  if (priv->is_70012) {
668  int pStride;
669 
670  if (width <= 720)
671  pStride = 720;
672  else if (width <= 1280)
673  pStride = 1280;
674  else pStride = 1920;
675  sStride = av_image_get_linesize(avctx->pix_fmt, pStride, 0);
676  } else {
677  sStride = bwidth;
678  }
679 
680  dStride = priv->pic->linesize[0];
681  dst = priv->pic->data[0];
682 
683  av_log(priv->avctx, AV_LOG_VERBOSE, "CrystalHD: Copying out frame\n");
684 
685  if (interlaced) {
686  int dY = 0;
687  int sY = 0;
688 
689  height /= 2;
690  if (bottom_field) {
691  av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: bottom field\n");
692  dY = 1;
693  } else {
694  av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: top field\n");
695  dY = 0;
696  }
697 
698  for (sY = 0; sY < height; dY++, sY++) {
699  memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth);
700  dY++;
701  }
702  } else {
703  av_image_copy_plane(dst, dStride, src, sStride, bwidth, height);
704  }
705 
707  if (interlaced)
708  priv->pic->top_field_first = !bottom_first;
709 
710  priv->pic->pkt_pts = pkt_pts;
711 
712  if (!priv->need_second_field) {
713  *got_frame = 1;
714  if ((ret = av_frame_ref(data, priv->pic)) < 0) {
715  return ret;
716  }
717  }
718 
719  /*
720  * Two types of PAFF content have been observed. One form causes the
721  * hardware to return a field pair and the other individual fields,
722  * even though the input is always individual fields. We must skip
723  * copying on the next decode() call to maintain pipeline length in
724  * the first case.
725  */
726  if (!interlaced && (output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) &&
727  (pic_type == PICT_TOP_FIELD || pic_type == PICT_BOTTOM_FIELD)) {
728  av_log(priv->avctx, AV_LOG_VERBOSE, "Fieldpair from two packets.\n");
729  return RET_SKIP_NEXT_COPY;
730  }
731 
732  /*
733  * The logic here is purely based on empirical testing with samples.
734  * If we need a second field, it could come from a second input packet,
735  * or it could come from the same field-pair input packet at the current
736  * field. In the first case, we should return and wait for the next time
737  * round to get the second field, while in the second case, we should
738  * ask the decoder for it immediately.
739  *
740  * Testing has shown that we are dealing with the fieldpair -> two fields
741  * case if the VDEC_FLAG_UNKNOWN_SRC is not set or if the input picture
742  * type was PICT_FRAME (in this second case, the flag might still be set)
743  */
744  return priv->need_second_field &&
745  (!(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ||
746  pic_type == PICT_FRAME) ?
748 }
749 
750 
751 static inline CopyRet receive_frame(AVCodecContext *avctx,
752  void *data, int *got_frame)
753 {
754  BC_STATUS ret;
755  BC_DTS_PROC_OUT output = {
756  .PicInfo.width = avctx->width,
757  .PicInfo.height = avctx->height,
758  };
759  CHDContext *priv = avctx->priv_data;
760  HANDLE dev = priv->dev;
761 
762  *got_frame = 0;
763 
764  // Request decoded data from the driver
765  ret = DtsProcOutputNoCopy(dev, OUTPUT_PROC_TIMEOUT, &output);
766  if (ret == BC_STS_FMT_CHANGE) {
767  av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Initial format change\n");
768  avctx->width = output.PicInfo.width;
769  avctx->height = output.PicInfo.height;
770  switch ( output.PicInfo.aspect_ratio ) {
771  case vdecAspectRatioSquare:
772  avctx->sample_aspect_ratio = (AVRational) { 1, 1};
773  break;
774  case vdecAspectRatio12_11:
775  avctx->sample_aspect_ratio = (AVRational) { 12, 11};
776  break;
777  case vdecAspectRatio10_11:
778  avctx->sample_aspect_ratio = (AVRational) { 10, 11};
779  break;
780  case vdecAspectRatio16_11:
781  avctx->sample_aspect_ratio = (AVRational) { 16, 11};
782  break;
783  case vdecAspectRatio40_33:
784  avctx->sample_aspect_ratio = (AVRational) { 40, 33};
785  break;
786  case vdecAspectRatio24_11:
787  avctx->sample_aspect_ratio = (AVRational) { 24, 11};
788  break;
789  case vdecAspectRatio20_11:
790  avctx->sample_aspect_ratio = (AVRational) { 20, 11};
791  break;
792  case vdecAspectRatio32_11:
793  avctx->sample_aspect_ratio = (AVRational) { 32, 11};
794  break;
795  case vdecAspectRatio80_33:
796  avctx->sample_aspect_ratio = (AVRational) { 80, 33};
797  break;
798  case vdecAspectRatio18_11:
799  avctx->sample_aspect_ratio = (AVRational) { 18, 11};
800  break;
801  case vdecAspectRatio15_11:
802  avctx->sample_aspect_ratio = (AVRational) { 15, 11};
803  break;
804  case vdecAspectRatio64_33:
805  avctx->sample_aspect_ratio = (AVRational) { 64, 33};
806  break;
807  case vdecAspectRatio160_99:
808  avctx->sample_aspect_ratio = (AVRational) {160, 99};
809  break;
810  case vdecAspectRatio4_3:
811  avctx->sample_aspect_ratio = (AVRational) { 4, 3};
812  break;
813  case vdecAspectRatio16_9:
814  avctx->sample_aspect_ratio = (AVRational) { 16, 9};
815  break;
816  case vdecAspectRatio221_1:
817  avctx->sample_aspect_ratio = (AVRational) {221, 1};
818  break;
819  }
820  return RET_COPY_AGAIN;
821  } else if (ret == BC_STS_SUCCESS) {
822  int copy_ret = -1;
823  if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
824  if (priv->last_picture == -1) {
825  /*
826  * Init to one less, so that the incrementing code doesn't
827  * need to be special-cased.
828  */
829  priv->last_picture = output.PicInfo.picture_number - 1;
830  }
831 
832  if (avctx->codec->id == AV_CODEC_ID_MPEG4 &&
833  output.PicInfo.timeStamp == 0 && priv->bframe_bug) {
834  av_log(avctx, AV_LOG_VERBOSE,
835  "CrystalHD: Not returning packed frame twice.\n");
836  priv->last_picture++;
837  DtsReleaseOutputBuffs(dev, NULL, FALSE);
838  return RET_COPY_AGAIN;
839  }
840 
841  print_frame_info(priv, &output);
842 
843  if (priv->last_picture + 1 < output.PicInfo.picture_number) {
844  av_log(avctx, AV_LOG_WARNING,
845  "CrystalHD: Picture Number discontinuity\n");
846  /*
847  * Have we lost frames? If so, we need to shrink the
848  * pipeline length appropriately.
849  *
850  * XXX: I have no idea what the semantics of this situation
851  * are so I don't even know if we've lost frames or which
852  * ones.
853  *
854  * In any case, only warn the first time.
855  */
856  priv->last_picture = output.PicInfo.picture_number - 1;
857  }
858 
859  copy_ret = copy_frame(avctx, &output, data, got_frame);
860  if (*got_frame > 0) {
861  avctx->has_b_frames--;
862  priv->last_picture++;
863  av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Pipeline length: %u\n",
864  avctx->has_b_frames);
865  }
866  } else {
867  /*
868  * An invalid frame has been consumed.
869  */
870  av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput succeeded with "
871  "invalid PIB\n");
872  avctx->has_b_frames--;
873  copy_ret = RET_OK;
874  }
875  DtsReleaseOutputBuffs(dev, NULL, FALSE);
876 
877  return copy_ret;
878  } else if (ret == BC_STS_BUSY) {
879  return RET_COPY_AGAIN;
880  } else {
881  av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput failed %d\n", ret);
882  return RET_ERROR;
883  }
884 }
885 
886 
887 static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
888 {
889  BC_STATUS ret;
890  BC_DTS_STATUS decoder_status = { 0, };
891  CopyRet rec_ret;
892  CHDContext *priv = avctx->priv_data;
893  HANDLE dev = priv->dev;
894  uint8_t *in_data = avpkt->data;
895  int len = avpkt->size;
896  int free_data = 0;
897  uint8_t pic_type = 0;
898 
899  av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: decode_frame\n");
900 
901  if (avpkt->size == 7 && !priv->bframe_bug) {
902  /*
903  * The use of a drop frame triggers the bug
904  */
905  av_log(avctx, AV_LOG_INFO,
906  "CrystalHD: Enabling work-around for packed b-frame bug\n");
907  priv->bframe_bug = 1;
908  } else if (avpkt->size == 8 && priv->bframe_bug) {
909  /*
910  * Delay frames don't trigger the bug
911  */
912  av_log(avctx, AV_LOG_INFO,
913  "CrystalHD: Disabling work-around for packed b-frame bug\n");
914  priv->bframe_bug = 0;
915  }
916 
917  if (len) {
918  int32_t tx_free = (int32_t)DtsTxFreeSize(dev);
919 
920  if (priv->bsfc) {
921  int ret = 0;
922  AVPacket filter_packet = { 0 };
923  AVPacket filtered_packet = { 0 };
924 
925  ret = av_packet_ref(&filter_packet, avpkt);
926  if (ret < 0) {
927  av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
928  "failed to ref input packet\n");
929  return ret;
930  }
931 
932  ret = av_bsf_send_packet(priv->bsfc, &filter_packet);
933  if (ret < 0) {
934  av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
935  "failed to send input packet\n");
936  return ret;
937  }
938 
939  ret = av_bsf_receive_packet(priv->bsfc, &filtered_packet);
940  if (ret < 0) {
941  av_log(avctx, AV_LOG_ERROR, "CrystalHD: mpv4toannexb filter "
942  "failed to receive output packet\n");
943  return ret;
944  }
945 
946  in_data = filtered_packet.data;
947  len = filtered_packet.size;
948 
949  av_packet_unref(&filter_packet);
950  }
951 
952  if (priv->parser) {
953  int ret = 0;
954 
955  free_data = ret > 0;
956 
957  if (ret >= 0) {
958  uint8_t *pout;
959  int psize;
960  int index;
961  H264Context *h = priv->parser->priv_data;
962 
963  index = av_parser_parse2(priv->parser, avctx, &pout, &psize,
964  in_data, len, avctx->internal->pkt->pts,
965  avctx->internal->pkt->dts, 0);
966  if (index < 0) {
967  av_log(avctx, AV_LOG_WARNING,
968  "CrystalHD: Failed to parse h.264 packet to "
969  "detect interlacing.\n");
970  } else if (index != len) {
971  av_log(avctx, AV_LOG_WARNING,
972  "CrystalHD: Failed to parse h.264 packet "
973  "completely. Interlaced frames may be "
974  "incorrectly detected.\n");
975  } else {
976  av_log(avctx, AV_LOG_VERBOSE,
977  "CrystalHD: parser picture type %d\n",
978  h->picture_structure);
979  pic_type = h->picture_structure;
980  }
981  } else {
982  av_log(avctx, AV_LOG_WARNING,
983  "CrystalHD: mp4toannexb filter failed to filter "
984  "packet. Interlaced frames may be incorrectly "
985  "detected.\n");
986  }
987  }
988 
989  if (len < tx_free - 1024) {
990  /*
991  * Despite being notionally opaque, either libcrystalhd or
992  * the hardware itself will mangle pts values that are too
993  * small or too large. The docs claim it should be in units
994  * of 100ns. Given that we're nominally dealing with a black
995  * box on both sides, any transform we do has no guarantee of
996  * avoiding mangling so we need to build a mapping to values
997  * we know will not be mangled.
998  */
999  uint64_t pts = opaque_list_push(priv, avctx->internal->pkt->pts, pic_type);
1000  if (!pts) {
1001  if (free_data) {
1002  av_freep(&in_data);
1003  }
1004  return AVERROR(ENOMEM);
1005  }
1006  av_log(priv->avctx, AV_LOG_VERBOSE,
1007  "input \"pts\": %"PRIu64"\n", pts);
1008  ret = DtsProcInput(dev, in_data, len, pts, 0);
1009  if (free_data) {
1010  av_freep(&in_data);
1011  }
1012  if (ret == BC_STS_BUSY) {
1013  av_log(avctx, AV_LOG_WARNING,
1014  "CrystalHD: ProcInput returned busy\n");
1015  usleep(BASE_WAIT);
1016  return AVERROR(EBUSY);
1017  } else if (ret != BC_STS_SUCCESS) {
1018  av_log(avctx, AV_LOG_ERROR,
1019  "CrystalHD: ProcInput failed: %u\n", ret);
1020  return -1;
1021  }
1022  avctx->has_b_frames++;
1023  } else {
1024  av_log(avctx, AV_LOG_WARNING, "CrystalHD: Input buffer full\n");
1025  len = 0; // We didn't consume any bytes.
1026  }
1027  } else {
1028  av_log(avctx, AV_LOG_INFO, "CrystalHD: No more input data\n");
1029  }
1030 
1031  if (priv->skip_next_output) {
1032  av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Skipping next output.\n");
1033  priv->skip_next_output = 0;
1034  avctx->has_b_frames--;
1035  return len;
1036  }
1037 
1038  ret = DtsGetDriverStatus(dev, &decoder_status);
1039  if (ret != BC_STS_SUCCESS) {
1040  av_log(avctx, AV_LOG_ERROR, "CrystalHD: GetDriverStatus failed\n");
1041  return -1;
1042  }
1043 
1044  /*
1045  * No frames ready. Don't try to extract.
1046  *
1047  * Empirical testing shows that ReadyListCount can be a damn lie,
1048  * and ProcOut still fails when count > 0. The same testing showed
1049  * that two more iterations were needed before ProcOutput would
1050  * succeed.
1051  */
1052  if (priv->output_ready < 2) {
1053  if (decoder_status.ReadyListCount != 0)
1054  priv->output_ready++;
1055  usleep(BASE_WAIT);
1056  av_log(avctx, AV_LOG_INFO, "CrystalHD: Filling pipeline.\n");
1057  return len;
1058  } else if (decoder_status.ReadyListCount == 0) {
1059  /*
1060  * After the pipeline is established, if we encounter a lack of frames
1061  * that probably means we're not giving the hardware enough time to
1062  * decode them, so start increasing the wait time at the end of a
1063  * decode call.
1064  */
1065  usleep(BASE_WAIT);
1066  priv->decode_wait += WAIT_UNIT;
1067  av_log(avctx, AV_LOG_INFO, "CrystalHD: No frames ready. Returning\n");
1068  return len;
1069  }
1070 
1071  do {
1072  rec_ret = receive_frame(avctx, data, got_frame);
1073  if (rec_ret == RET_OK && *got_frame == 0) {
1074  /*
1075  * This case is for when the encoded fields are stored
1076  * separately and we get a separate avpkt for each one. To keep
1077  * the pipeline stable, we should return nothing and wait for
1078  * the next time round to grab the second field.
1079  * H.264 PAFF is an example of this.
1080  */
1081  av_log(avctx, AV_LOG_VERBOSE, "Returning after first field.\n");
1082  avctx->has_b_frames--;
1083  } else if (rec_ret == RET_COPY_NEXT_FIELD) {
1084  /*
1085  * This case is for when the encoded fields are stored in a
1086  * single avpkt but the hardware returns then separately. Unless
1087  * we grab the second field before returning, we'll slip another
1088  * frame in the pipeline and if that happens a lot, we're sunk.
1089  * So we have to get that second field now.
1090  * Interlaced mpeg2 and vc1 are examples of this.
1091  */
1092  av_log(avctx, AV_LOG_VERBOSE, "Trying to get second field.\n");
1093  while (1) {
1094  usleep(priv->decode_wait);
1095  ret = DtsGetDriverStatus(dev, &decoder_status);
1096  if (ret == BC_STS_SUCCESS &&
1097  decoder_status.ReadyListCount > 0) {
1098  rec_ret = receive_frame(avctx, data, got_frame);
1099  if ((rec_ret == RET_OK && *got_frame > 0) ||
1100  rec_ret == RET_ERROR)
1101  break;
1102  }
1103  }
1104  av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Got second field.\n");
1105  } else if (rec_ret == RET_SKIP_NEXT_COPY) {
1106  /*
1107  * Two input packets got turned into a field pair. Gawd.
1108  */
1109  av_log(avctx, AV_LOG_VERBOSE,
1110  "Don't output on next decode call.\n");
1111  priv->skip_next_output = 1;
1112  }
1113  /*
1114  * If rec_ret == RET_COPY_AGAIN, that means that either we just handled
1115  * a FMT_CHANGE event and need to go around again for the actual frame,
1116  * we got a busy status and need to try again, or we're dealing with
1117  * packed b-frames, where the hardware strangely returns the packed
1118  * p-frame twice. We choose to keep the second copy as it carries the
1119  * valid pts.
1120  */
1121  } while (rec_ret == RET_COPY_AGAIN);
1122  usleep(priv->decode_wait);
1123  return len;
1124 }
1125 
1126 
1127 #if CONFIG_H264_CRYSTALHD_DECODER
1128 static AVClass h264_class = {
1129  "h264_crystalhd",
1131  options,
1133 };
1134 
1135 AVCodec ff_h264_crystalhd_decoder = {
1136  .name = "h264_crystalhd",
1137  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (CrystalHD acceleration)"),
1138  .type = AVMEDIA_TYPE_VIDEO,
1139  .id = AV_CODEC_ID_H264,
1140  .priv_data_size = sizeof(CHDContext),
1141  .init = init,
1142  .close = uninit,
1143  .decode = decode,
1144  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1145  .flush = flush,
1147  .priv_class = &h264_class,
1148 };
1149 #endif
1150 
1151 #if CONFIG_MPEG2_CRYSTALHD_DECODER
1152 static AVClass mpeg2_class = {
1153  "mpeg2_crystalhd",
1155  options,
1157 };
1158 
1159 AVCodec ff_mpeg2_crystalhd_decoder = {
1160  .name = "mpeg2_crystalhd",
1161  .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Video (CrystalHD acceleration)"),
1162  .type = AVMEDIA_TYPE_VIDEO,
1163  .id = AV_CODEC_ID_MPEG2VIDEO,
1164  .priv_data_size = sizeof(CHDContext),
1165  .init = init,
1166  .close = uninit,
1167  .decode = decode,
1168  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1169  .flush = flush,
1171  .priv_class = &mpeg2_class,
1172 };
1173 #endif
1174 
1175 #if CONFIG_MPEG4_CRYSTALHD_DECODER
1176 static AVClass mpeg4_class = {
1177  "mpeg4_crystalhd",
1179  options,
1181 };
1182 
1183 AVCodec ff_mpeg4_crystalhd_decoder = {
1184  .name = "mpeg4_crystalhd",
1185  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 (CrystalHD acceleration)"),
1186  .type = AVMEDIA_TYPE_VIDEO,
1187  .id = AV_CODEC_ID_MPEG4,
1188  .priv_data_size = sizeof(CHDContext),
1189  .init = init,
1190  .close = uninit,
1191  .decode = decode,
1192  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1193  .flush = flush,
1195  .priv_class = &mpeg4_class,
1196 };
1197 #endif
1198 
1199 #if CONFIG_MSMPEG4_CRYSTALHD_DECODER
1200 static AVClass msmpeg4_class = {
1201  "msmpeg4_crystalhd",
1203  options,
1205 };
1206 
1207 AVCodec ff_msmpeg4_crystalhd_decoder = {
1208  .name = "msmpeg4_crystalhd",
1209  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 Microsoft variant version 3 (CrystalHD acceleration)"),
1210  .type = AVMEDIA_TYPE_VIDEO,
1211  .id = AV_CODEC_ID_MSMPEG4V3,
1212  .priv_data_size = sizeof(CHDContext),
1213  .init = init,
1214  .close = uninit,
1215  .decode = decode,
1217  .flush = flush,
1219  .priv_class = &msmpeg4_class,
1220 };
1221 #endif
1222 
1223 #if CONFIG_VC1_CRYSTALHD_DECODER
1224 static AVClass vc1_class = {
1225  "vc1_crystalhd",
1227  options,
1229 };
1230 
1231 AVCodec ff_vc1_crystalhd_decoder = {
1232  .name = "vc1_crystalhd",
1233  .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 (CrystalHD acceleration)"),
1234  .type = AVMEDIA_TYPE_VIDEO,
1235  .id = AV_CODEC_ID_VC1,
1236  .priv_data_size = sizeof(CHDContext),
1237  .init = init,
1238  .close = uninit,
1239  .decode = decode,
1240  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1241  .flush = flush,
1243  .priv_class = &vc1_class,
1244 };
1245 #endif
1246 
1247 #if CONFIG_WMV3_CRYSTALHD_DECODER
1248 static AVClass wmv3_class = {
1249  "wmv3_crystalhd",
1251  options,
1253 };
1254 
1255 AVCodec ff_wmv3_crystalhd_decoder = {
1256  .name = "wmv3_crystalhd",
1257  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 (CrystalHD acceleration)"),
1258  .type = AVMEDIA_TYPE_VIDEO,
1259  .id = AV_CODEC_ID_WMV3,
1260  .priv_data_size = sizeof(CHDContext),
1261  .init = init,
1262  .close = uninit,
1263  .decode = decode,
1264  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
1265  .flush = flush,
1267  .priv_class = &wmv3_class,
1268 };
1269 #endif
uint64_t last_picture
Definition: crystalhd.c:146
void av_bsf_free(AVBSFContext **ctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:36
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:1684
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...
Definition: imgutils.c:75
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
uint8_t is_nal
Definition: crystalhd.c:140
static OpaqueList * opaque_list_pop(CHDContext *priv, uint64_t fake_timestamp)
Definition: crystalhd.c:266
uint8_t * orig_extradata
Definition: crystalhd.c:131
The bitstream filter state.
Definition: avcodec.h:5737
int size
Definition: avcodec.h:1601
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:2086
AVPacket * pkt
Current packet as passed into the decoder, to avoid having to pass the packet into every function...
Definition: internal.h:144
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1903
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: avcodec.h:1013
int version
Definition: avisynth_c.h:766
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
H264Context.
Definition: h264dec.h:341
static CopyRet receive_frame(AVCodecContext *avctx, void *data, int *got_frame)
Definition: crystalhd.c:751
uint8_t bframe_bug
Definition: crystalhd.c:153
static av_cold int init(AVCodecContext *avctx)
Definition: crystalhd.c:382
AVCodec.
Definition: avcodec.h:3599
int picture_structure
Definition: h264dec.h:412
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:135
uint32_t sWidth
Definition: crystalhd.c:152
#define TIMESTAMP_UNIT
Step between fake timestamps passed to hardware in units of 100ns.
Definition: crystalhd.c:99
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:82
AVCodecContext * avctx
Definition: crystalhd.c:127
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:983
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:199
OpaqueList * tail
Definition: crystalhd.c:149
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:141
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:337
mode
Definition: f_perms.c:27
AVOptions.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:375
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1790
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:38
#define height
uint8_t * data
Definition: avcodec.h:1600
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:318
#define av_log(a,...)
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:576
enum AVCodecID id
Definition: avcodec.h:3613
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:191
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1997
static const AVClass h264_class
Definition: h264dec.c:1205
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
CopyRet
Definition: crystalhd.c:110
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:154
#define FALSE
Definition: windows2linux.h:37
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
static void flush(AVCodecContext *avctx)
Definition: crystalhd.c:319
#define PICT_TOP_FIELD
Definition: mpegutils.h:37
const char * name
Name of the codec implementation.
Definition: avcodec.h:3606
static uint64_t opaque_list_push(CHDContext *priv, uint64_t reordered_opaque, uint8_t pic_type)
Definition: crystalhd.c:236
#define WAIT_UNIT
Increment in us to adjust wait in decode()
Definition: crystalhd.c:103
#define fail()
Definition: checkasm.h:82
uint32_t sps_pps_size
Definition: crystalhd.c:139
AVCodecParserContext * parser
Definition: crystalhd.c:135
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3997
static void print_frame_info(CHDContext *priv, BC_DTS_PROC_OUT *output)
Definition: crystalhd.c:190
uint8_t interlaced
Definition: mxfenc.c:1824
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.
Definition: parser.c:136
#define width
static const AVOption options[]
Definition: crystalhd.c:156
int width
picture width / height.
Definition: avcodec.h:1862
PVOID HANDLE
static int filter_packet(AVFormatContext *avf, ConcatStream *cs, AVPacket *pkt)
Definition: concatdec.c:521
OpaqueList * head
Definition: crystalhd.c:148
uint8_t * sps_pps_buf
Definition: crystalhd.c:138
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:176
int32_t
void av_parser_close(AVCodecParserContext *s)
Definition: parser.c:235
#define TRUE
Definition: windows2linux.h:33
uint64_t reordered_opaque
Definition: crystalhd.c:121
uint64_t fake_timestamp
Definition: crystalhd.c:120
H.264 / AVC / MPEG-4 part10 codec.
#define AVERROR_BSF_NOT_FOUND
Bitstream filter not found.
Definition: error.h:49
#define src
Definition: vp9dsp.c:530
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:196
HANDLE dev
Definition: crystalhd.c:129
struct OpaqueList * next
Definition: crystalhd.c:119
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
AVCodecParserContext * av_parser_init(int codec_id)
Definition: parser.c:51
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
uint32_t orig_extradata_size
Definition: crystalhd.c:132
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:281
main external API structure.
Definition: avcodec.h:1675
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:567
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:937
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:63
int extradata_size
Definition: avcodec.h:1791
static const char * format
Definition: movenc.c:47
Describe the class of an AVClass context structure.
Definition: log.h:67
static BC_MEDIA_SUBTYPE id2subtype(CHDContext *priv, enum AVCodecID id)
Definition: crystalhd.c:170
int index
Definition: gxfenc.c:89
uint8_t need_second_field
Definition: crystalhd.c:142
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: utils.c:4117
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:276
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
Definition: frame.h:273
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:485
static int64_t pts
Global timestamp for the audio frames.
uint8_t is_70012
Definition: crystalhd.c:137
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
uint8_t output_ready
Definition: crystalhd.c:141
AVClass * av_class
Definition: crystalhd.c:126
common internal api header.
#define OUTPUT_PROC_TIMEOUT
Timeout parameter passed to DtsProcOutput() in us.
Definition: crystalhd.c:97
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:5014
AVBSFContext * bsfc
Definition: crystalhd.c:134
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:733
static const AVClass mpeg4_class
void * priv_data
Definition: avcodec.h:1717
#define PICT_FRAME
Definition: mpegutils.h:39
#define av_free(p)
uint64_t decode_wait
Definition: crystalhd.c:144
#define BASE_WAIT
Initial value in us of the wait in decode()
Definition: crystalhd.c:101
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:323
int len
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1725
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3993
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1599
#define av_freep(p)
uint8_t pic_type
Definition: crystalhd.c:122
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:887
AVFrame * pic
Definition: crystalhd.c:128
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.
Definition: imgutils.c:287
uint8_t skip_next_output
Definition: crystalhd.c:143
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1577
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5763
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1353
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:958
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1593
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:242
static CopyRet copy_frame(AVCodecContext *avctx, BC_DTS_PROC_OUT *output, void *data, int *got_frame)
Definition: crystalhd.c:555