[PATCH] CrystalHD decoder support v6
Hi all, A relatively minor update Changes ------- * Fixed off-by-one error in source-stride on 70012 * Implemented an adaptive delay mechanism for the enforced delay between decode() calls. Still not perfect but now handles a wider range of samples. Outstanding issues ------------------ No change here: * 70012 still doesn't work right. It needs different decode() timing and I haven't investigated it completely. * Interlaced H.264 support remains incomplete due to lack of a way to distinguish the cases. MBAFF as produced by x264 is assumed for now. --phil
[...]
+/* + * The CrystalHD doesn't report interlaced H.264 content in a way that allows + * us to distinguish between specific cases that require different handling. + * So, for now, we have to hard-code the behaviour we want. + * + * The default behaviour is to assume MBAFF with input and output fieldpairs. + * + * Define ASSUME_PAFF_OVER_MBAFF to treat input as PAFF with separate input + * and output fields. + * + * Define ASSUME_TWO_INPUTS_ONE_OUTPUT to treat input as separate fields but + * output as a single fieldpair. + * + * Define both to mess up your playback. + */ +#define ASSUME_PAFF_OVER_MBAFF 0 +#define ASSUME_TWO_INPUTS_ONE_OUTPUT 0 +static inline CopyRet copy_frame(AVCodecContext *avctx, + BC_DTS_PROC_OUT *output, + void *data, int *data_size, + uint8_t second_field) +{ + BC_STATUS ret; + BC_DTS_STATUS decoder_status; + uint8_t is_paff; + uint8_t next_frame_same; + uint8_t interlaced; + uint8_t need_second_field; + + CHDContext *priv = avctx->priv_data; + + uint8_t bottom_field = (output->PicInfo.flags & VDEC_FLAG_BOTTOMFIELD) == + VDEC_FLAG_BOTTOMFIELD; + uint8_t bottom_first = !!(output->PicInfo.flags & VDEC_FLAG_BOTTOM_FIRST); + + int width = output->PicInfo.width; + int height = output->PicInfo.height; + int bwidth; + uint8_t *src = output->Ybuff; + int sStride; + uint8_t *dst; + int dStride; + + ret = DtsGetDriverStatus(priv->dev, &decoder_status); + if (ret != BC_STS_SUCCESS) { + av_log(avctx, AV_LOG_ERROR, + "CrystalHD: GetDriverStatus failed: %u\n", ret); + return RET_ERROR; + } + + is_paff = ASSUME_PAFF_OVER_MBAFF || + !(output->PicInfo.flags & VDEC_FLAG_UNKNOWN_SRC); + next_frame_same = output->PicInfo.picture_number == + (decoder_status.picNumFlags & ~0x40000000); + interlaced = ((output->PicInfo.flags & + VDEC_FLAG_INTERLACED_SRC) && is_paff) || + next_frame_same || bottom_field || second_field;
see libavcodec/h264_parser.c: if(h->sps.frame_mbs_only_flag){ h->s.picture_structure= PICT_FRAME; }else{ if(get_bits1(&h->s.gb)) { //field_pic_flag h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb); //bottom_field_flag } else { h->s.picture_structure= PICT_FRAME; } } above will tell you if its top/bottom field or frame iam not completely sure how to cleanly hook that up though but its likely better than hardcoding guesses. mpeg2 field pictures can also be detected through the corresponding parser
+ /* + * XXX: Is the first field always the first one encountered? + */
this question is not very well written "is the X always the X"
+ need_second_field = interlaced && !bottom_field; + + av_log(avctx, AV_LOG_VERBOSE, "CrystalHD: next_frame_same: %u | %u | %u\n", + next_frame_same, output->PicInfo.picture_number, + decoder_status.picNumFlags & ~0x40000000); + + priv->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | + FF_BUFFER_HINTS_REUSABLE; + if(avctx->reget_buffer(avctx, &priv->pic) < 0) { + av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); + return RET_ERROR; + }
reget_buffer() doesnt feel correct here, why do you need it instead of get_buffer()? also might be able to directly return the frames from the decoder without using get_buffer()
+ + bwidth = av_image_get_linesize(avctx->pix_fmt, width, 0); + if (priv->is_70012) { + int pStride; + + if (width <= 720) + pStride = 720; + else if (width <= 1280) + pStride = 1280; + else if (width <= 1080) + pStride = 1080; + sStride = av_image_get_linesize(avctx->pix_fmt, pStride, 0); + } else { + sStride = bwidth; + } + + dStride = priv->pic.linesize[0]; + dst = priv->pic.data[0]; + + av_log(priv->avctx, AV_LOG_VERBOSE, "CrystalHD: Copying out frame\n"); + + if (interlaced) { + int dY = 0; + int sY = 0; + + height /= 2; + if (bottom_field) { + av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: bottom field\n"); + dY = 1; + } else { + av_log(priv->avctx, AV_LOG_VERBOSE, "Interlaced: top field\n"); + dY = 0; + }
dY= bottom_field
+ + for (sY = 0; sY < height; dY++, sY++) { + memcpy(&(dst[dY * dStride]), &(src[sY * sStride]), bwidth); + if (interlaced) + dY++;
interlaced is alwys true here
+ } + } else { + av_image_copy_plane(dst, dStride, src, sStride, bwidth, height); + }
+ + priv->pic.interlaced_frame = interlaced; + if (interlaced) + priv->pic.top_field_first = !bottom_first; + + if (output->PicInfo.timeStamp != 0) { + priv->pic.pkt_pts = opaque_list_pop(priv, output->PicInfo.timeStamp); + av_log(avctx, AV_LOG_VERBOSE, "output \"pts\": %"PRIu64"\n", + priv->pic.pkt_pts); + }
make sure pkt_dts is set to AV_NOPTS_VALUE (see avcodec_decode_video2) or try to set it correctly but thats harder [...]
+ /* + * No frames ready. Don't try to extract. + * + * Empirical testing shows that ReadyListCount can be a damn lie,
maybe its not fields/frames but slices that are ready? or maybe its something to do with reordering that it doesnt match the expectation (this could be tested with streams with alot of slices and 1 slice = 1 frame, and streams with various reordering structures)
+ * and ProcOut still fails when count > 0. The same testing showed + * that two more iterations were needed before ProcOutput would + * succeed. + */ + if (priv->output_ready < 2) { + if (decoder_status.ReadyListCount != 0) + priv->output_ready++; + usleep(BASE_WAIT); + av_log(avctx, AV_LOG_INFO, "CrystalHD: Filling pipeline.\n"); + return len; + } else if (decoder_status.ReadyListCount == 0) { + /* + * After the pipeline is established, if we encounter a lack of frames + * that probably means we're not giving the hardware enough time to + * decode them, so start increasing the wait time at the end of a + * decode call. + */ + usleep(BASE_WAIT); + priv->decode_wait += WAIT_UNIT; + av_log(avctx, AV_LOG_INFO, "CrystalHD: No frames ready. Returning\n"); + return len; + } + + do { + rec_ret = receive_frame(avctx, data, data_size, 0); + if (rec_ret == 0 && *data_size == 0) { + if (avctx->codec->id == CODEC_ID_H264) { + /* + * This case is for when the encoded fields are stored + * separately and we get a separate avpkt for each one. To keep + * the pipeline stable, we should return nothing and wait for + * the next time round to grab the second field. + * H.264 PAFF is an example of this. + */ + av_log(avctx, AV_LOG_VERBOSE, "Returning after first field.\n"); + avctx->has_b_frames--; + } else { + /* + * This case is for when the encoded fields are stored in a + * single avpkt but the hardware returns then separately. Unless + * we grab the second field before returning, we'll slip another + * frame in the pipeline and if that happens a lot, we're sunk. + * So we have to get that second field now. + * Interlaced mpeg2 and vc1 are examples of this. + */ + av_log(avctx, AV_LOG_VERBOSE, "Trying to get second field.\n"); + while (1) { + usleep(priv->decode_wait); + ret = DtsGetDriverStatus(dev, &decoder_status); + if (ret == BC_STS_SUCCESS && + decoder_status.ReadyListCount > 0) { + rec_ret = receive_frame(avctx, data, data_size, 1); + if ((rec_ret == 0 && *data_size > 0) || + rec_ret == RET_ERROR) + break; + } + }
would it make sense to move all this to a seperate thread to avoid blocking the outside vissible decoder with usleep() calls? or is the waiting time negliible? [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB DNS cache poisoning attacks, popular search engine, Google internet authority dont be evil, please
On Sun, 6 Mar 2011 04:40:36 +0100 Michael Niedermayer <michaelni at gmx.at> wrote:
see libavcodec/h264_parser.c: if(h->sps.frame_mbs_only_flag){ h->s.picture_structure= PICT_FRAME; }else{ if(get_bits1(&h->s.gb)) { //field_pic_flag h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb); //bottom_field_flag } else { h->s.picture_structure= PICT_FRAME; } }
above will tell you if its top/bottom field or frame iam not completely sure how to cleanly hook that up though but its likely better than hardcoding guesses. mpeg2 field pictures can also be detected through the corresponding parser
Yeah, but doing that will require introducing a lot more knowledge of the bitstream, which I desperately want to avoid - hence why I'm hoping I can get firmware fixes out of Broadcom so that it reports picture characteristics correctly.
+ /* + * XXX: Is the first field always the first one encountered? + */
this question is not very well written "is the X always the X"
Indeed. It's meant to ask "Is the top field always the first one encountered?" - ie: Are fields always ordered top -> bottom in the bistream even if the display order is bottom -> top?
reget_buffer() doesnt feel correct here, why do you need it instead of get_buffer()?
It helps reduce the amount of state I have to maintain when dealing with interlaced frames. Without it, I'd have to keep the previous buffer if I only got one frame on the first decode() call so that I could write the second field in on the next call. with reget_buffer(), I can just write the field without caring.
also might be able to directly return the frames from the decoder without using get_buffer()
So, the driver and library design mean that there's no way to get the hardware to write directly to the final buffer - so there are always going to be more copies than you really want. In the specific case here, I believe there's always going to be a copy - it's just a question of who does it. You can ask the library to do it, but I never got that to work, it would always muck up the stride. Or you could try and pass the library buffer out, but in a real client like mplayer, that just means the client will immediately do a copy after the decode() because it can't guarantee how long the buffer is available for, and if you're doing accelerated rendering, it has to be in an acceleratable buffer anyway. So (re)get_buffer is obtaining an acceleratable buffer and we end up in the same place. Finally, interlaced content means you need the separate buffer to merge the fields.
would it make sense to move all this to a seperate thread to avoid blocking the outside vissible decoder with usleep() calls? or is the waiting time negliible?
The wait is definitely not negligible - it's measured in 10s of milliseconds. The big challenge is that it's almost impossible to know when the right behaviour is to wait and when you need to feed another packet into the hardware. The hardware won't tell you it needs more packets to construct the next frame, and even counting packets up to h.264 maximums won't help because of mpeg ps/ts where packets != pictures. And sure, I could drag in a bunch of parsers to try and work it all out but that will complicate things immensely. So, if I move this to another thread, the functional difference would be that during the waiting period, decode() would return immediately and not consume any input bytes or return any frames, rather than blocking inside decode(). Does that make any difference in practice? As for why it needs to wait: If you don't wait and just keep feeding packets in, you will build up a huge pipeline (100+ packets) and the client is likely to freak out in some way. As you know, mplayer maintains its own pts queue and after 24 pts are queued, it just starts dropping them and sync goes out the window. --phil
On Sun, Mar 06, 2011 at 10:26:11AM -0800, Philip Langdale wrote:
On Sun, 6 Mar 2011 04:40:36 +0100 Michael Niedermayer <michaelni at gmx.at> wrote:
see libavcodec/h264_parser.c: if(h->sps.frame_mbs_only_flag){ h->s.picture_structure= PICT_FRAME; }else{ if(get_bits1(&h->s.gb)) { //field_pic_flag h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb); //bottom_field_flag } else { h->s.picture_structure= PICT_FRAME; } }
above will tell you if its top/bottom field or frame iam not completely sure how to cleanly hook that up though but its likely better than hardcoding guesses. mpeg2 field pictures can also be detected through the corresponding parser
Yeah, but doing that will require introducing a lot more knowledge of the bitstream, which I desperately want to avoid - hence why I'm hoping I can get firmware fixes out of Broadcom so that it reports picture characteristics correctly.
why not just open the parser for the current codec and parse the frame?
+ /* + * XXX: Is the first field always the first one encountered? + */
this question is not very well written "is the X always the X"
Indeed. It's meant to ask "Is the top field always the first one encountered?" - ie: Are fields always ordered top -> bottom in the bistream even if the display order is bottom -> top?
no
reget_buffer() doesnt feel correct here, why do you need it instead of get_buffer()?
It helps reduce the amount of state I have to maintain when dealing with interlaced frames. Without it, I'd have to keep the previous buffer if I only got one frame on the first decode() call so that I could write the second field in on the next call. with reget_buffer(), I can just write the field without caring.
reget_buffer() is expensive (memcpy of whole frame) in many cases
also might be able to directly return the frames from the decoder without using get_buffer()
So, the driver and library design mean that there's no way to get the hardware to write directly to the final buffer - so there are always going to be more copies than you really want. In the specific case here, I believe there's always going to be a copy - it's just a question of who does it. You can ask the library to do it, but I never got that to work, it would always muck up the stride. Or you could try and pass the library buffer out, but in a real client like mplayer, that just means the client will immediately do a copy after the decode() because it can't guarantee how long the buffer is available for, and if you're doing accelerated rendering, it has to be in an acceleratable buffer anyway. So (re)get_buffer is obtaining an acceleratable buffer and we end up in the same place. Finally, interlaced content means you need the separate buffer to merge the fields.
reget_buffer() unlikely to return a buffer in video ram because we have to copy it on the next reget_buffer() call and that means read and read from video ram is slow The reason for the copy is that the decoder can need it again before it has been displayed also there can be a filter afterwards (deinterlace, deblock, ...) in that case outputting the frame as is means 1 copy less [...]
So, if I move this to another thread, the functional difference would be that during the waiting period, decode() would return immediately and not consume any input bytes or return any frames, rather than blocking inside decode(). Does that make any difference in practice?
As for why it needs to wait: If you don't wait and just keep feeding packets in, you will build up a huge pipeline (100+ packets) and the client is likely to freak out in some way. As you know, mplayer maintains its own pts queue and after 24 pts are queued, it just starts dropping them and sync goes out the window.
a fix of the waiting will need changes to the client too i guess [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Many that live deserve death. And some that die deserve life. Can you give it to them? Then do not be too eager to deal out death in judgement. For even the very wise cannot see all ends. -- Gandalf
On Tue, 8 Mar 2011 19:21:19 +0100 Michael Niedermayer <michaelni at gmx.at> wrote:
Yeah, but doing that will require introducing a lot more knowledge of the bitstream, which I desperately want to avoid - hence why I'm hoping I can get firmware fixes out of Broadcom so that it reports picture characteristics correctly.
why not just open the parser for the current codec and parse the frame?
But what if one packet != one frame (as I understand it, mpeg ps/ts can do this) - then I've got to worry about accumulating bytes and maintaining a lot of state. Certainly I see the h264 decoder has logic for this. The hardware is supposed to do the heavy lifting :-)
reget_buffer() is expensive (memcpy of whole frame) in many cases
Ok. I'll change it to use get/release (actually how it was originally). As a side effect of being stateful, the issue of field order becomes moot.
a fix of the waiting will need changes to the client too i guess
Yes. I experimented with returning 0 for bytes consumed and mplayer completely fell over - it can't comprehend a decode() call returning no frame and no bytes consumed without being an error. So I have to use a blocking delay in the absence of client changes. Assuming I make the get/release change, and we ignore interlaced h.264 for now, how do you feel about merging this? Thanks, --phil
On Tue, 8 Mar 2011 19:21:19 +0100 Michael Niedermayer <michaelni at gmx.at> wrote:
why not just open the parser for the current codec and parse the frame?
So, I've been investigating this and the parser is partially useful. It allows me to distinguish between MBAFF and PAFF content - which is great, but it can't help me deal with these different PAFF cases where the hardware returns a field-pair even when the input fields are in separate packets. I end up in a situation where the packet's picture_structure indicated it was a field, but when I match that up with the output picture, the hardware really gave me a field pair and the information is wrong. Is there any justification in the standard for this behaviour or is the hardware being weird and I have to look to the hardware for clues? Right now, the only evidence I can find is in the next picture after the ambiguous one. if the opaque 'timestamp' is the same as the first picture, then we have two separate fields - if it's a different value, then we're receiving field pairs. However, in the field pair case, I now have two received field pairs to pass out, and while I think I can do the juggling correctly, things are becoming ridiculous at this point. The hardware does actually claim to publish the timestamp of the next picture in the queue but it's usually not available because it hasn't been decoded yet. *sigh* --phil
On Sun, 13 Mar 2011 15:44:10 -0700 Philip Langdale <philipl at overt.org> wrote:
*sigh*
And of course, now I've found an MBAFF clip that the hardware returns individual fields for. FML. --phil
On Sun, Mar 13, 2011 at 04:28:38PM -0700, Philip Langdale wrote:
On Sun, 13 Mar 2011 15:44:10 -0700 Philip Langdale <philipl at overt.org> wrote:
*sigh*
And of course, now I've found an MBAFF clip that the hardware returns individual fields for. FML.
dont forget MBAFF + PAFF can be mixed in the same clip [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Breaking DRM is a little like attempting to break through a door even though the window is wide open and the only thing in the house is a bunch of things you dont want and which you would get tomorrow for free anyway
participants (2)
-
michaelni@gmx.at -
philipl@overt.org