ffmpeg / libavcodec / crystalhd.c @ ca0eed7e
History | View | Annotate | Download (33.8 KB)
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 outputing
|
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 |
#include <unistd.h> |
81 |
|
82 |
#include <libcrystalhd/bc_dts_types.h> |
83 |
#include <libcrystalhd/bc_dts_defs.h> |
84 |
#include <libcrystalhd/libcrystalhd_if.h> |
85 |
|
86 |
#include "avcodec.h" |
87 |
#include "libavutil/imgutils.h" |
88 |
#include "libavutil/intreadwrite.h" |
89 |
|
90 |
/** Timeout parameter passed to DtsProcOutput() in us */
|
91 |
#define OUTPUT_PROC_TIMEOUT 50 |
92 |
/** Step between fake timestamps passed to hardware in units of 100ns */
|
93 |
#define TIMESTAMP_UNIT 100000 |
94 |
/** Initial value in us of the wait in decode() */
|
95 |
#define BASE_WAIT 10000 |
96 |
/** Increment in us to adjust wait in decode() */
|
97 |
#define WAIT_UNIT 1000 |
98 |
|
99 |
|
100 |
/*****************************************************************************
|
101 |
* Module private data
|
102 |
****************************************************************************/
|
103 |
|
104 |
typedef enum { |
105 |
RET_ERROR = -1,
|
106 |
RET_OK = 0,
|
107 |
RET_COPY_AGAIN = 1,
|
108 |
RET_SKIP_NEXT_COPY = 2,
|
109 |
RET_COPY_NEXT_FIELD = 3,
|
110 |
} CopyRet; |
111 |
|
112 |
typedef struct OpaqueList { |
113 |
struct OpaqueList *next;
|
114 |
uint64_t fake_timestamp; |
115 |
uint64_t reordered_opaque; |
116 |
} OpaqueList; |
117 |
|
118 |
typedef struct { |
119 |
AVCodecContext *avctx; |
120 |
AVFrame pic; |
121 |
HANDLE dev; |
122 |
|
123 |
uint8_t is_70012; |
124 |
uint8_t *sps_pps_buf; |
125 |
uint32_t sps_pps_size; |
126 |
uint8_t is_nal; |
127 |
uint8_t output_ready; |
128 |
uint8_t need_second_field; |
129 |
uint8_t skip_next_output; |
130 |
uint64_t decode_wait; |
131 |
|
132 |
uint64_t last_picture; |
133 |
|
134 |
OpaqueList *head; |
135 |
OpaqueList *tail; |
136 |
} CHDContext; |
137 |
|
138 |
|
139 |
/*****************************************************************************
|
140 |
* Helper functions
|
141 |
****************************************************************************/
|
142 |
|
143 |
static inline BC_MEDIA_SUBTYPE id2subtype(CHDContext *priv, enum CodecID id) |
144 |
{ |
145 |
switch (id) {
|
146 |
case CODEC_ID_MPEG4:
|
147 |
return BC_MSUBTYPE_DIVX;
|
148 |
case CODEC_ID_MSMPEG4V3:
|
149 |
return BC_MSUBTYPE_DIVX311;
|
150 |
case CODEC_ID_MPEG2VIDEO:
|
151 |
return BC_MSUBTYPE_MPEG2VIDEO;
|
152 |
case CODEC_ID_VC1:
|
153 |
return BC_MSUBTYPE_VC1;
|
154 |
case CODEC_ID_WMV3:
|
155 |
return BC_MSUBTYPE_WMV3;
|
156 |
case CODEC_ID_H264:
|
157 |
return priv->is_nal ? BC_MSUBTYPE_AVC1 : BC_MSUBTYPE_H264;
|
158 |
default:
|
159 |
return BC_MSUBTYPE_INVALID;
|
160 |
} |
161 |
} |
162 |
|
163 |
static inline void print_frame_info(CHDContext *priv, BC_DTS_PROC_OUT *output) |
164 |
{ |
165 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffSz: %u\n", output->YbuffSz);
|
166 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tYBuffDoneSz: %u\n",
|
167 |
output->YBuffDoneSz); |
168 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tUVBuffDoneSz: %u\n",
|
169 |
output->UVBuffDoneSz); |
170 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tTimestamp: %"PRIu64"\n", |
171 |
output->PicInfo.timeStamp); |
172 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tPicture Number: %u\n",
|
173 |
output->PicInfo.picture_number); |
174 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tWidth: %u\n",
|
175 |
output->PicInfo.width); |
176 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tHeight: %u\n",
|
177 |
output->PicInfo.height); |
178 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tChroma: 0x%03x\n",
|
179 |
output->PicInfo.chroma_format); |
180 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tPulldown: %u\n",
|
181 |
output->PicInfo.pulldown); |
182 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tFlags: 0x%08x\n",
|
183 |
output->PicInfo.flags); |
184 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrame Rate/Res: %u\n",
|
185 |
output->PicInfo.frame_rate); |
186 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tAspect Ratio: %u\n",
|
187 |
output->PicInfo.aspect_ratio); |
188 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tColor Primaries: %u\n",
|
189 |
output->PicInfo.colour_primaries); |
190 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tMetaData: %u\n",
|
191 |
output->PicInfo.picture_meta_payload); |
192 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tSession Number: %u\n",
|
193 |
output->PicInfo.sess_num); |
194 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tycom: %u\n",
|
195 |
output->PicInfo.ycom); |
196 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tCustom Aspect: %u\n",
|
197 |
output->PicInfo.custom_aspect_ratio_width_height); |
198 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tFrames to Drop: %u\n",
|
199 |
output->PicInfo.n_drop); |
200 |
av_log(priv->avctx, AV_LOG_VERBOSE, "\tH264 Valid Fields: 0x%08x\n",
|
201 |
output->PicInfo.other.h264.valid); |
202 |
} |
203 |
|
204 |
|
205 |
/*****************************************************************************
|
206 |
* OpaqueList functions
|
207 |
****************************************************************************/
|
208 |
|
209 |
static uint64_t opaque_list_push(CHDContext *priv, uint64_t reordered_opaque)
|
210 |
{ |
211 |
OpaqueList *newNode = av_mallocz(sizeof (OpaqueList));
|
212 |
if (!newNode) {
|
213 |
av_log(priv->avctx, AV_LOG_ERROR, |
214 |
"Unable to allocate new node in OpaqueList.\n");
|
215 |
return 0; |
216 |
} |
217 |
if (!priv->head) {
|
218 |
newNode->fake_timestamp = TIMESTAMP_UNIT; |
219 |
priv->head = newNode; |
220 |
} else {
|
221 |
newNode->fake_timestamp = priv->tail->fake_timestamp + TIMESTAMP_UNIT; |
222 |
priv->tail->next = newNode; |
223 |
} |
224 |
priv->tail = newNode; |
225 |
newNode->reordered_opaque = reordered_opaque; |
226 |
|
227 |
return newNode->fake_timestamp;
|
228 |
} |
229 |
|
230 |
/*
|
231 |
* The OpaqueList is built in decode order, while elements will be removed
|
232 |
* in presentation order. If frames are reordered, this means we must be
|
233 |
* able to remove elements that are not the first element.
|
234 |
*/
|
235 |
static uint64_t opaque_list_pop(CHDContext *priv, uint64_t fake_timestamp)
|
236 |
{ |
237 |
OpaqueList *node = priv->head; |
238 |
|
239 |
if (!priv->head) {
|
240 |
av_log(priv->avctx, AV_LOG_ERROR, |
241 |
"CrystalHD: Attempted to query non-existent timestamps.\n");
|
242 |
return AV_NOPTS_VALUE;
|
243 |
} |
244 |
|
245 |
/*
|
246 |
* The first element is special-cased because we have to manipulate
|
247 |
* the head pointer rather than the previous element in the list.
|
248 |
*/
|
249 |
if (priv->head->fake_timestamp == fake_timestamp) {
|
250 |
uint64_t reordered_opaque = node->reordered_opaque; |
251 |
priv->head = node->next; |
252 |
av_free(node); |
253 |
|
254 |
if (!priv->head->next)
|
255 |
priv->tail = priv->head; |
256 |
|
257 |
return reordered_opaque;
|
258 |
} |
259 |
|
260 |
/*
|
261 |
* The list is processed at arm's length so that we have the
|
262 |
* previous element available to rewrite its next pointer.
|
263 |
*/
|
264 |
while (node->next) {
|
265 |
OpaqueList *next = node->next; |
266 |
if (next->fake_timestamp == fake_timestamp) {
|
267 |
uint64_t reordered_opaque = next->reordered_opaque; |
268 |
node->next = next->next; |
269 |
av_free(next); |
270 |
|
271 |
if (!node->next)
|
272 |
priv->tail = node; |
273 |
|
274 |
return reordered_opaque;
|
275 |
} else {
|
276 |
node = next; |
277 |
} |
278 |
} |
279 |
|
280 |
av_log(priv->avctx, AV_LOG_VERBOSE, |
281 |
"CrystalHD: Couldn't match fake_timestamp.\n");
|
282 |
return AV_NOPTS_VALUE;
|
283 |
} |
284 |
|
285 |
|
286 |
/*****************************************************************************
|
287 |
* Video decoder API function definitions
|
288 |
****************************************************************************/
|
289 |
|
290 |
static void flush(AVCodecContext *avctx) |
291 |
{ |
292 |
CHDContext *priv = avctx->priv_data; |
293 |
|
294 |
avctx->has_b_frames = 0;
|
295 |
priv->last_picture = -1;
|
296 |
priv->output_ready = 0;
|
297 |
priv->need_second_field = 0;
|
298 |
priv->skip_next_output = 0;
|
299 |
priv->decode_wait = BASE_WAIT; |
300 |
|
301 |
if (priv->pic.data[0]) |
302 |
avctx->release_buffer(avctx, &priv->pic); |
303 |
|
304 |
/* Flush mode 4 flushes all software and hardware buffers. */
|
305 |
DtsFlushInput(priv->dev, 4);
|
306 |
} |
307 |
|
308 |
|
309 |
static av_cold int uninit(AVCodecContext *avctx) |
310 |
{ |
311 |
CHDContext *priv = avctx->priv_data; |
312 |
HANDLE device; |
313 |
|
314 |
device = priv->dev; |
315 |
DtsStopDecoder(device); |
316 |
DtsCloseDecoder(device); |
317 |
DtsDeviceClose(device); |
318 |
|
319 |
av_free(priv->sps_pps_buf); |
320 |
|
321 |
if (priv->pic.data[0]) |
322 |
avctx->release_buffer(avctx, &priv->pic); |
323 |
|
324 |
if (priv->head) {
|
325 |
OpaqueList *node = priv->head; |
326 |
while (node) {
|
327 |
OpaqueList *next = node->next; |
328 |
av_free(node); |
329 |
node = next; |
330 |
} |
331 |
} |
332 |
|
333 |
return 0; |
334 |
} |
335 |
|
336 |
|
337 |
static av_cold int init(AVCodecContext *avctx) |
338 |
{ |
339 |
CHDContext* priv; |
340 |
BC_STATUS ret; |
341 |
BC_INFO_CRYSTAL version; |
342 |
BC_INPUT_FORMAT format = { |
343 |
.FGTEnable = FALSE, |
344 |
.Progressive = TRUE, |
345 |
.OptFlags = 0x80000000 | vdecFrameRate59_94 | 0x40, |
346 |
.width = avctx->width, |
347 |
.height = avctx->height, |
348 |
}; |
349 |
|
350 |
BC_MEDIA_SUBTYPE subtype; |
351 |
|
352 |
uint32_t mode = DTS_PLAYBACK_MODE | |
353 |
DTS_LOAD_FILE_PLAY_FW | |
354 |
DTS_SKIP_TX_CHK_CPB | |
355 |
DTS_PLAYBACK_DROP_RPT_MODE | |
356 |
DTS_SINGLE_THREADED_MODE | |
357 |
DTS_DFLT_RESOLUTION(vdecRESOLUTION_1080p23_976); |
358 |
|
359 |
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD Init for %s\n",
|
360 |
avctx->codec->name); |
361 |
|
362 |
avctx->pix_fmt = PIX_FMT_YUYV422; |
363 |
|
364 |
/* Initialize the library */
|
365 |
priv = avctx->priv_data; |
366 |
priv->avctx = avctx; |
367 |
priv->is_nal = avctx->extradata_size > 0 && *(avctx->extradata) == 1; |
368 |
priv->last_picture = -1;
|
369 |
priv->decode_wait = BASE_WAIT; |
370 |
|
371 |
subtype = id2subtype(priv, avctx->codec->id); |
372 |
switch (subtype) {
|
373 |
case BC_MSUBTYPE_AVC1:
|
374 |
{ |
375 |
uint8_t *dummy_p; |
376 |
int dummy_int;
|
377 |
AVBitStreamFilterContext *bsfc; |
378 |
|
379 |
uint32_t orig_data_size = avctx->extradata_size; |
380 |
uint8_t *orig_data = av_malloc(orig_data_size); |
381 |
if (!orig_data) {
|
382 |
av_log(avctx, AV_LOG_ERROR, |
383 |
"Failed to allocate copy of extradata\n");
|
384 |
return AVERROR(ENOMEM);
|
385 |
} |
386 |
memcpy(orig_data, avctx->extradata, orig_data_size); |
387 |
|
388 |
|
389 |
bsfc = av_bitstream_filter_init("h264_mp4toannexb");
|
390 |
if (!bsfc) {
|
391 |
av_log(avctx, AV_LOG_ERROR, |
392 |
"Cannot open the h264_mp4toannexb BSF!\n");
|
393 |
av_free(orig_data); |
394 |
return AVERROR_BSF_NOT_FOUND;
|
395 |
} |
396 |
av_bitstream_filter_filter(bsfc, avctx, NULL, &dummy_p,
|
397 |
&dummy_int, NULL, 0, 0); |
398 |
av_bitstream_filter_close(bsfc); |
399 |
|
400 |
priv->sps_pps_buf = avctx->extradata; |
401 |
priv->sps_pps_size = avctx->extradata_size; |
402 |
avctx->extradata = orig_data; |
403 |
avctx->extradata_size = orig_data_size; |
404 |
|
405 |
format.pMetaData = priv->sps_pps_buf; |
406 |
format.metaDataSz = priv->sps_pps_size; |
407 |
format.startCodeSz = (avctx->extradata[4] & 0x03) + 1; |
408 |
} |
409 |
break;
|
410 |
case BC_MSUBTYPE_H264:
|
411 |
format.startCodeSz = 4;
|
412 |
// Fall-through
|
413 |
case BC_MSUBTYPE_VC1:
|
414 |
case BC_MSUBTYPE_WVC1:
|
415 |
case BC_MSUBTYPE_WMV3:
|
416 |
case BC_MSUBTYPE_WMVA:
|
417 |
case BC_MSUBTYPE_MPEG2VIDEO:
|
418 |
case BC_MSUBTYPE_DIVX:
|
419 |
case BC_MSUBTYPE_DIVX311:
|
420 |
format.pMetaData = avctx->extradata; |
421 |
format.metaDataSz = avctx->extradata_size; |
422 |
break;
|
423 |
default:
|
424 |
av_log(avctx, AV_LOG_ERROR, "CrystalHD: Unknown codec name\n");
|
425 |
return AVERROR(EINVAL);
|
426 |
} |
427 |
format.mSubtype = subtype; |
428 |
|
429 |
/* Get a decoder instance */
|
430 |
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: starting up\n");
|
431 |
// Initialize the Link and Decoder devices
|
432 |
ret = DtsDeviceOpen(&priv->dev, mode); |
433 |
if (ret != BC_STS_SUCCESS) {
|
434 |
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: DtsDeviceOpen failed\n");
|
435 |
goto fail;
|
436 |
} |
437 |
|
438 |
ret = DtsCrystalHDVersion(priv->dev, &version); |
439 |
if (ret != BC_STS_SUCCESS) {
|
440 |
av_log(avctx, AV_LOG_VERBOSE, |
441 |
"CrystalHD: DtsCrystalHDVersion failed\n");
|
442 |
goto fail;
|
443 |
} |
444 |
priv->is_70012 = version.device == 0;
|
445 |
|
446 |
if (priv->is_70012 &&
|
447 |
(subtype == BC_MSUBTYPE_DIVX || subtype == BC_MSUBTYPE_DIVX311)) { |
448 |
av_log(avctx, AV_LOG_VERBOSE, |
449 |
"CrystalHD: BCM70012 doesn't support MPEG4-ASP/DivX/Xvid\n");
|
450 |
goto fail;
|
451 |
} |
452 |
|
453 |
ret = DtsSetInputFormat(priv->dev, &format); |
454 |
if (ret != BC_STS_SUCCESS) {
|
455 |
av_log(avctx, AV_LOG_ERROR, "CrystalHD: SetInputFormat failed\n");
|
456 |
goto fail;
|
457 |
} |
458 |
|
459 |
ret = DtsOpenDecoder(priv->dev, BC_STREAM_TYPE_ES); |
460 |
if (ret != BC_STS_SUCCESS) {
|
461 |
av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsOpenDecoder failed\n");
|
462 |
goto fail;
|
463 |
} |
464 |
|
465 |
ret = DtsSetColorSpace(priv->dev, OUTPUT_MODE422_YUY2); |
466 |
if (ret != BC_STS_SUCCESS) {
|
467 |
av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsSetColorSpace failed\n");
|
468 |
goto fail;
|
469 |
} |
470 |
ret = DtsStartDecoder(priv->dev); |
471 |
if (ret != BC_STS_SUCCESS) {
|
472 |
av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartDecoder failed\n");
|
473 |
goto fail;
|
474 |
} |
475 |
ret = DtsStartCapture(priv->dev); |
476 |
if (ret != BC_STS_SUCCESS) {
|
477 |
av_log(avctx, AV_LOG_ERROR, "CrystalHD: DtsStartCapture failed\n");
|
478 |
goto fail;
|
479 |
} |
480 |
|
481 |
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Init complete.\n");
|
482 |
|
483 |
return 0; |
484 |
|
485 |
fail:
|
486 |
uninit(avctx); |
487 |
return -1; |
488 |
} |
489 |
|
490 |
|
491 |
/*
|
492 |
* The CrystalHD doesn't report interlaced H.264 content in a way that allows
|
493 |
* us to distinguish between specific cases that require different handling.
|
494 |
* So, for now, we have to hard-code the behaviour we want.
|
495 |
*
|
496 |
* The default behaviour is to assume MBAFF with input and output fieldpairs.
|
497 |
*
|
498 |
* Define ASSUME_PAFF_OVER_MBAFF to treat input as PAFF with separate input
|
499 |
* and output fields.
|
500 |
*
|
501 |
* Define ASSUME_TWO_INPUTS_ONE_OUTPUT to treat input as separate fields but
|
502 |
* output as a single fieldpair.
|
503 |
*
|
504 |
* Define both to mess up your playback.
|
505 |
*/
|
506 |
#define ASSUME_PAFF_OVER_MBAFF 0 |
507 |
#define ASSUME_TWO_INPUTS_ONE_OUTPUT 0 |
508 |
static inline CopyRet copy_frame(AVCodecContext *avctx, |
509 |
BC_DTS_PROC_OUT *output, |
510 |
void *data, int *data_size, |
511 |
uint8_t second_field) |
512 |
{ |
513 |
BC_STATUS ret; |
514 |
BC_DTS_STATUS decoder_status; |
515 |
uint8_t is_paff; |
516 |
uint8_t next_frame_same; |
517 |
uint8_t interlaced; |
518 |
|
519 |
CHDContext *priv = avctx->priv_data; |
520 |
|
521 |
uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) == |
522 |
VDEC_FLAG_BOTTOMFIELD; |
523 |
uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST); |
524 |
|
525 |
int width = output->PicInfo.width;
|
526 |
int height = output->PicInfo.height;
|
527 |
int bwidth;
|
528 |
uint8_t *src = output->Ybuff; |
529 |
int sStride;
|
530 |
uint8_t *dst; |
531 |
int dStride;
|
532 |
|
533 |
ret = DtsGetDriverStatus(priv->dev, &decoder_status); |
534 |
if (ret != BC_STS_SUCCESS) {
|
535 |
av_log(avctx, AV_LOG_ERROR, |
536 |
"CrystalHD: GetDriverStatus failed: %u\n", ret);
|
537 |
return RET_ERROR;
|
538 |
} |
539 |
|
540 |
is_paff = ASSUME_PAFF_OVER_MBAFF || |
541 |
!(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC); |
542 |
next_frame_same = output->PicInfo.picture_number == |
543 |
(decoder_status.picNumFlags & ~0x40000000);
|
544 |
interlaced = ((output->PicInfo.flags & |
545 |
VDEC_FLAG_INTERLACED_SRC) && is_paff) || |
546 |
next_frame_same || bottom_field || second_field; |
547 |
|
548 |
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: next_frame_same: %u | %u | %u\n",
|
549 |
next_frame_same, output->PicInfo.picture_number, |
550 |
decoder_status.picNumFlags & ~0x40000000);
|
551 |
|
552 |
if (priv->pic.data[0] && !priv->need_second_field) |
553 |
avctx->release_buffer(avctx, &priv->pic); |
554 |
|
555 |
priv->need_second_field = interlaced && !priv->need_second_field; |
556 |
|
557 |
priv->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | |
558 |
FF_BUFFER_HINTS_REUSABLE; |
559 |
if (!priv->pic.data[0]) { |
560 |
if (avctx->get_buffer(avctx, &priv->pic) < 0) { |
561 |
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
562 |
return RET_ERROR;
|
563 |
} |
564 |
} |
565 |
|
566 |
bwidth = av_image_get_linesize(avctx->pix_fmt, width, 0);
|
567 |
if (priv->is_70012) {
|
568 |
int pStride;
|
569 |
|
570 |
if (width <= 720) |
571 |
pStride = 720;
|
572 |
else if (width <= 1280) |
573 |
pStride = 1280;
|
574 |
else if (width <= 1080) |
575 |
pStride = 1080;
|
576 |
sStride = av_image_get_linesize(avctx->pix_fmt, pStride, 0);
|
577 |
} else {
|
578 |
sStride = bwidth; |
579 |
} |
580 |
|
581 |
dStride = priv->pic.linesize[0];
|
582 |
dst = priv->pic.data[0];
|
583 |
|
584 |
av_log(priv->avctx, AV_LOG_VERBOSE, "CrystalHD: Copying out frame\n");
|
585 |
|
586 |
if (interlaced) {
|
587 |
int dY = 0; |
588 |
int sY = 0; |
589 |
|
590 |
height /= 2;
|
591 |
if (bottom_field) {
|
592 |
av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: bottom field\n");
|
593 |
dY = 1;
|
594 |
} else {
|
595 |
av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: top field\n");
|
596 |
dY = 0;
|
597 |
} |
598 |
|
599 |
for (sY = 0; sY < height; dY++, sY++) { |
600 |
memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth); |
601 |
dY++; |
602 |
} |
603 |
} else {
|
604 |
av_image_copy_plane(dst, dStride, src, sStride, bwidth, height); |
605 |
} |
606 |
|
607 |
priv->pic.interlaced_frame = interlaced; |
608 |
if (interlaced)
|
609 |
priv->pic.top_field_first = !bottom_first; |
610 |
|
611 |
if (output->PicInfo.timeStamp != 0) { |
612 |
priv->pic.pkt_pts = opaque_list_pop(priv, output->PicInfo.timeStamp); |
613 |
av_log(avctx, AV_LOG_VERBOSE, "output \"pts\": %"PRIu64"\n", |
614 |
priv->pic.pkt_pts); |
615 |
} |
616 |
|
617 |
if (!priv->need_second_field) {
|
618 |
*data_size = sizeof(AVFrame);
|
619 |
*(AVFrame *)data = priv->pic; |
620 |
} |
621 |
|
622 |
if (ASSUME_TWO_INPUTS_ONE_OUTPUT &&
|
623 |
output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) { |
624 |
av_log(priv->avctx, AV_LOG_VERBOSE, "Fieldpair from two packets.\n");
|
625 |
return RET_SKIP_NEXT_COPY;
|
626 |
} |
627 |
|
628 |
/*
|
629 |
* Testing has shown that in all cases where we don't want to return the
|
630 |
* full frame immediately, VDEC_FLAG_UNKNOWN_SRC is set.
|
631 |
*/
|
632 |
return priv->need_second_field &&
|
633 |
!(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC) ? |
634 |
RET_COPY_NEXT_FIELD : RET_OK; |
635 |
} |
636 |
|
637 |
|
638 |
static inline CopyRet receive_frame(AVCodecContext *avctx, |
639 |
void *data, int *data_size, |
640 |
uint8_t second_field) |
641 |
{ |
642 |
BC_STATUS ret; |
643 |
BC_DTS_PROC_OUT output = { |
644 |
.PicInfo.width = avctx->width, |
645 |
.PicInfo.height = avctx->height, |
646 |
}; |
647 |
CHDContext *priv = avctx->priv_data; |
648 |
HANDLE dev = priv->dev; |
649 |
|
650 |
*data_size = 0;
|
651 |
|
652 |
// Request decoded data from the driver
|
653 |
ret = DtsProcOutputNoCopy(dev, OUTPUT_PROC_TIMEOUT, &output); |
654 |
if (ret == BC_STS_FMT_CHANGE) {
|
655 |
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Initial format change\n");
|
656 |
avctx->width = output.PicInfo.width; |
657 |
avctx->height = output.PicInfo.height; |
658 |
return RET_COPY_AGAIN;
|
659 |
} else if (ret == BC_STS_SUCCESS) { |
660 |
int copy_ret = -1; |
661 |
if (output.PoutFlags & BC_POUT_FLAGS_PIB_VALID) {
|
662 |
if (priv->last_picture == -1) { |
663 |
/*
|
664 |
* Init to one less, so that the incrementing code doesn't
|
665 |
* need to be special-cased.
|
666 |
*/
|
667 |
priv->last_picture = output.PicInfo.picture_number - 1;
|
668 |
} |
669 |
|
670 |
if (avctx->codec->id == CODEC_ID_MPEG4 &&
|
671 |
output.PicInfo.timeStamp == 0) {
|
672 |
av_log(avctx, AV_LOG_VERBOSE, |
673 |
"CrystalHD: Not returning packed frame twice.\n");
|
674 |
priv->last_picture++; |
675 |
DtsReleaseOutputBuffs(dev, NULL, FALSE);
|
676 |
return RET_COPY_AGAIN;
|
677 |
} |
678 |
|
679 |
print_frame_info(priv, &output); |
680 |
|
681 |
if (priv->last_picture + 1 < output.PicInfo.picture_number) { |
682 |
av_log(avctx, AV_LOG_WARNING, |
683 |
"CrystalHD: Picture Number discontinuity\n");
|
684 |
/*
|
685 |
* Have we lost frames? If so, we need to shrink the
|
686 |
* pipeline length appropriately.
|
687 |
*
|
688 |
* XXX: I have no idea what the semantics of this situation
|
689 |
* are so I don't even know if we've lost frames or which
|
690 |
* ones.
|
691 |
*
|
692 |
* In any case, only warn the first time.
|
693 |
*/
|
694 |
priv->last_picture = output.PicInfo.picture_number - 1;
|
695 |
} |
696 |
|
697 |
copy_ret = copy_frame(avctx, &output, data, data_size, second_field); |
698 |
if (*data_size > 0) { |
699 |
avctx->has_b_frames--; |
700 |
priv->last_picture++; |
701 |
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Pipeline length: %u\n",
|
702 |
avctx->has_b_frames); |
703 |
} |
704 |
} else {
|
705 |
/*
|
706 |
* An invalid frame has been consumed.
|
707 |
*/
|
708 |
av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput succeeded with "
|
709 |
"invalid PIB\n");
|
710 |
avctx->has_b_frames--; |
711 |
copy_ret = RET_OK; |
712 |
} |
713 |
DtsReleaseOutputBuffs(dev, NULL, FALSE);
|
714 |
|
715 |
return copy_ret;
|
716 |
} else if (ret == BC_STS_BUSY) { |
717 |
return RET_COPY_AGAIN;
|
718 |
} else {
|
719 |
av_log(avctx, AV_LOG_ERROR, "CrystalHD: ProcOutput failed %d\n", ret);
|
720 |
return RET_ERROR;
|
721 |
} |
722 |
} |
723 |
|
724 |
|
725 |
static int decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) |
726 |
{ |
727 |
BC_STATUS ret; |
728 |
BC_DTS_STATUS decoder_status; |
729 |
CopyRet rec_ret; |
730 |
CHDContext *priv = avctx->priv_data; |
731 |
HANDLE dev = priv->dev; |
732 |
int len = avpkt->size;
|
733 |
|
734 |
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: decode_frame\n");
|
735 |
|
736 |
if (len) {
|
737 |
int32_t tx_free = (int32_t)DtsTxFreeSize(dev); |
738 |
if (len < tx_free - 1024) { |
739 |
/*
|
740 |
* Despite being notionally opaque, either libcrystalhd or
|
741 |
* the hardware itself will mangle pts values that are too
|
742 |
* small or too large. The docs claim it should be in units
|
743 |
* of 100ns. Given that we're nominally dealing with a black
|
744 |
* box on both sides, any transform we do has no guarantee of
|
745 |
* avoiding mangling so we need to build a mapping to values
|
746 |
* we know will not be mangled.
|
747 |
*/
|
748 |
uint64_t pts = opaque_list_push(priv, avctx->pkt->pts); |
749 |
if (!pts) {
|
750 |
return AVERROR(ENOMEM);
|
751 |
} |
752 |
av_log(priv->avctx, AV_LOG_VERBOSE, |
753 |
"input \"pts\": %"PRIu64"\n", pts); |
754 |
ret = DtsProcInput(dev, avpkt->data, len, pts, 0);
|
755 |
if (ret == BC_STS_BUSY) {
|
756 |
av_log(avctx, AV_LOG_WARNING, |
757 |
"CrystalHD: ProcInput returned busy\n");
|
758 |
usleep(BASE_WAIT); |
759 |
return AVERROR(EBUSY);
|
760 |
} else if (ret != BC_STS_SUCCESS) { |
761 |
av_log(avctx, AV_LOG_ERROR, |
762 |
"CrystalHD: ProcInput failed: %u\n", ret);
|
763 |
return -1; |
764 |
} |
765 |
avctx->has_b_frames++; |
766 |
} else {
|
767 |
av_log(avctx, AV_LOG_WARNING, "CrystalHD: Input buffer full\n");
|
768 |
len = 0; // We didn't consume any bytes. |
769 |
} |
770 |
} else {
|
771 |
av_log(avctx, AV_LOG_INFO, "CrystalHD: No more input data\n");
|
772 |
} |
773 |
|
774 |
if (priv->skip_next_output) {
|
775 |
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Skipping next output.\n");
|
776 |
priv->skip_next_output = 0;
|
777 |
avctx->has_b_frames--; |
778 |
return len;
|
779 |
} |
780 |
|
781 |
ret = DtsGetDriverStatus(dev, &decoder_status); |
782 |
if (ret != BC_STS_SUCCESS) {
|
783 |
av_log(avctx, AV_LOG_ERROR, "CrystalHD: GetDriverStatus failed\n");
|
784 |
return -1; |
785 |
} |
786 |
|
787 |
/*
|
788 |
* No frames ready. Don't try to extract.
|
789 |
*
|
790 |
* Empirical testing shows that ReadyListCount can be a damn lie,
|
791 |
* and ProcOut still fails when count > 0. The same testing showed
|
792 |
* that two more iterations were needed before ProcOutput would
|
793 |
* succeed.
|
794 |
*/
|
795 |
if (priv->output_ready < 2) { |
796 |
if (decoder_status.ReadyListCount != 0) |
797 |
priv->output_ready++; |
798 |
usleep(BASE_WAIT); |
799 |
av_log(avctx, AV_LOG_INFO, "CrystalHD: Filling pipeline.\n");
|
800 |
return len;
|
801 |
} else if (decoder_status.ReadyListCount == 0) { |
802 |
/*
|
803 |
* After the pipeline is established, if we encounter a lack of frames
|
804 |
* that probably means we're not giving the hardware enough time to
|
805 |
* decode them, so start increasing the wait time at the end of a
|
806 |
* decode call.
|
807 |
*/
|
808 |
usleep(BASE_WAIT); |
809 |
priv->decode_wait += WAIT_UNIT; |
810 |
av_log(avctx, AV_LOG_INFO, "CrystalHD: No frames ready. Returning\n");
|
811 |
return len;
|
812 |
} |
813 |
|
814 |
do {
|
815 |
rec_ret = receive_frame(avctx, data, data_size, 0);
|
816 |
if (rec_ret == RET_OK && *data_size == 0) { |
817 |
/*
|
818 |
* This case is for when the encoded fields are stored
|
819 |
* separately and we get a separate avpkt for each one. To keep
|
820 |
* the pipeline stable, we should return nothing and wait for
|
821 |
* the next time round to grab the second field.
|
822 |
* H.264 PAFF is an example of this.
|
823 |
*/
|
824 |
av_log(avctx, AV_LOG_VERBOSE, "Returning after first field.\n");
|
825 |
avctx->has_b_frames--; |
826 |
} else if (rec_ret == RET_COPY_NEXT_FIELD) { |
827 |
/*
|
828 |
* This case is for when the encoded fields are stored in a
|
829 |
* single avpkt but the hardware returns then separately. Unless
|
830 |
* we grab the second field before returning, we'll slip another
|
831 |
* frame in the pipeline and if that happens a lot, we're sunk.
|
832 |
* So we have to get that second field now.
|
833 |
* Interlaced mpeg2 and vc1 are examples of this.
|
834 |
*/
|
835 |
av_log(avctx, AV_LOG_VERBOSE, "Trying to get second field.\n");
|
836 |
while (1) { |
837 |
usleep(priv->decode_wait); |
838 |
ret = DtsGetDriverStatus(dev, &decoder_status); |
839 |
if (ret == BC_STS_SUCCESS &&
|
840 |
decoder_status.ReadyListCount > 0) {
|
841 |
rec_ret = receive_frame(avctx, data, data_size, 1);
|
842 |
if ((rec_ret == RET_OK && *data_size > 0) || |
843 |
rec_ret == RET_ERROR) |
844 |
break;
|
845 |
} |
846 |
} |
847 |
av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: Got second field.\n");
|
848 |
} else if (rec_ret == RET_SKIP_NEXT_COPY) { |
849 |
/*
|
850 |
* Two input packets got turned into a field pair. Gawd.
|
851 |
*/
|
852 |
av_log(avctx, AV_LOG_VERBOSE, |
853 |
"Don't output on next decode call.\n");
|
854 |
priv->skip_next_output = 1;
|
855 |
} |
856 |
/*
|
857 |
* If rec_ret == RET_COPY_AGAIN, that means that either we just handled
|
858 |
* a FMT_CHANGE event and need to go around again for the actual frame,
|
859 |
* we got a busy status and need to try again, or we're dealing with
|
860 |
* packed b-frames, where the hardware strangely returns the packed
|
861 |
* p-frame twice. We choose to keep the second copy as it carries the
|
862 |
* valid pts.
|
863 |
*/
|
864 |
} while (rec_ret == RET_COPY_AGAIN);
|
865 |
usleep(priv->decode_wait); |
866 |
return len;
|
867 |
} |
868 |
|
869 |
|
870 |
#if CONFIG_H264_CRYSTALHD_DECODER
|
871 |
AVCodec ff_h264_crystalhd_decoder = { |
872 |
.name = "h264_crystalhd",
|
873 |
.type = AVMEDIA_TYPE_VIDEO, |
874 |
.id = CODEC_ID_H264, |
875 |
.priv_data_size = sizeof(CHDContext),
|
876 |
.init = init, |
877 |
.close = uninit, |
878 |
.decode = decode, |
879 |
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL, |
880 |
.flush = flush, |
881 |
.long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (CrystalHD acceleration)"),
|
882 |
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE}, |
883 |
}; |
884 |
#endif
|
885 |
|
886 |
#if CONFIG_MPEG2_CRYSTALHD_DECODER
|
887 |
AVCodec ff_mpeg2_crystalhd_decoder = { |
888 |
.name = "mpeg2_crystalhd",
|
889 |
.type = AVMEDIA_TYPE_VIDEO, |
890 |
.id = CODEC_ID_MPEG2VIDEO, |
891 |
.priv_data_size = sizeof(CHDContext),
|
892 |
.init = init, |
893 |
.close = uninit, |
894 |
.decode = decode, |
895 |
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL, |
896 |
.flush = flush, |
897 |
.long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Video (CrystalHD acceleration)"),
|
898 |
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE}, |
899 |
}; |
900 |
#endif
|
901 |
|
902 |
#if CONFIG_MPEG4_CRYSTALHD_DECODER
|
903 |
AVCodec ff_mpeg4_crystalhd_decoder = { |
904 |
.name = "mpeg4_crystalhd",
|
905 |
.type = AVMEDIA_TYPE_VIDEO, |
906 |
.id = CODEC_ID_MPEG4, |
907 |
.priv_data_size = sizeof(CHDContext),
|
908 |
.init = init, |
909 |
.close = uninit, |
910 |
.decode = decode, |
911 |
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL, |
912 |
.flush = flush, |
913 |
.long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 (CrystalHD acceleration)"),
|
914 |
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE}, |
915 |
}; |
916 |
#endif
|
917 |
|
918 |
#if CONFIG_MSMPEG4_CRYSTALHD_DECODER
|
919 |
AVCodec ff_msmpeg4_crystalhd_decoder = { |
920 |
.name = "msmpeg4_crystalhd",
|
921 |
.type = AVMEDIA_TYPE_VIDEO, |
922 |
.id = CODEC_ID_MSMPEG4V3, |
923 |
.priv_data_size = sizeof(CHDContext),
|
924 |
.init = init, |
925 |
.close = uninit, |
926 |
.decode = decode, |
927 |
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL, |
928 |
.flush = flush, |
929 |
.long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Part 2 Microsoft variant version 3 (CrystalHD acceleration)"),
|
930 |
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE}, |
931 |
}; |
932 |
#endif
|
933 |
|
934 |
#if CONFIG_VC1_CRYSTALHD_DECODER
|
935 |
AVCodec ff_vc1_crystalhd_decoder = { |
936 |
.name = "vc1_crystalhd",
|
937 |
.type = AVMEDIA_TYPE_VIDEO, |
938 |
.id = CODEC_ID_VC1, |
939 |
.priv_data_size = sizeof(CHDContext),
|
940 |
.init = init, |
941 |
.close = uninit, |
942 |
.decode = decode, |
943 |
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL, |
944 |
.flush = flush, |
945 |
.long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 (CrystalHD acceleration)"),
|
946 |
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE}, |
947 |
}; |
948 |
#endif
|
949 |
|
950 |
#if CONFIG_WMV3_CRYSTALHD_DECODER
|
951 |
AVCodec ff_wmv3_crystalhd_decoder = { |
952 |
.name = "wmv3_crystalhd",
|
953 |
.type = AVMEDIA_TYPE_VIDEO, |
954 |
.id = CODEC_ID_WMV3, |
955 |
.priv_data_size = sizeof(CHDContext),
|
956 |
.init = init, |
957 |
.close = uninit, |
958 |
.decode = decode, |
959 |
.capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_EXPERIMENTAL, |
960 |
.flush = flush, |
961 |
.long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 (CrystalHD acceleration)"),
|
962 |
.pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUYV422, PIX_FMT_NONE}, |
963 |
}; |
964 |
#endif
|