4

I have been searching high and low for an option to force avcodec to use unaligned memory for its AVFrame data.

Depending on the pixel format, the horizontal planes of an AVFrame->data may be padded with extra data to be aligned to memory for performance.

eg: a 1920 * 1080 video with 4 bytes per pixel will have 1920 * 4 = 7680 bytes per plane.

With avcodec if you are decoding this video it will create 7808 bytes per plane.

This adds 7808 - 7680 = 128 bytes of extra padding.

For my purposes I would like to force avcodec to use unaligned data so I can copy an entire continuous chunk of frame data instead of copying and formatting smaller pieces one at a time to a continuous chunk.

The following flag found in the headers:

/* encoding support
   These flags can be passed in AVCodecContext.flags before initialization.
   Note: Not everything is supported yet.
*/

/**
 * Allow decoders to produce frames with data planes that are not aligned
 * to CPU requirements (e.g. due to cropping).
 */
#define CODEC_FLAG_UNALIGNED 0x0001

Setting this AVCodecContext.flags to be CODEC_FLAG_UNALIGNED, the assumption is that the AVFrame->data is now unaligned, this is not the case.

I'm not sure if I am looking at the right place or using this flag correctly.

Regards,

Curious George

2 Answers2

2

After decoding, you can use swrescale to format the pixeles into an memory buffer you supply. Just set the input and output resolution to same, allocate a chunk of memory, and set your line stride equal to frame width.

szatmary
  • 29,969
  • 8
  • 44
  • 57
  • Indeed this is one of the work arounds to this, it unfortunately in my case with a full HD video this swsrescale adds 3-4 milliseconds of overhead which is unfortunate, I am seeking to skip that – user3244284 Jan 29 '14 at 09:23
  • 1
    The decoders just do not do what you are asking for. You cant get something for nothing. It writes to aligned memory, because aligned memory access is the fastest. so even if the decoder did do it, those 3ms would just be spent in the decoder instead of swscale. – szatmary Jan 29 '14 at 17:19
0

An important thing with ffmpeg is to always check anything actually cares about the defined option. Doesn't look like a lot of codecs actually use this flag:

~/sources/ffmpeg-2.1.1> find libavcodec/ -name \*.c | xargs grep CODEC_FLAG_UNALIGNED
libavcodec/hevc_ps.c:        !(s->avctx->flags & CODEC_FLAG_UNALIGNED)) {
libavcodec/h264_ps.c:                !(h->avctx->flags & CODEC_FLAG_UNALIGNED)) {
~/sources/ffmpeg-2.1.1>
George Y.
  • 11,307
  • 3
  • 24
  • 25
  • You are correct, this flag is used very rarely, which answers the question that I'm using the flag incorrectly for my purpose. The only question now is which FLAG/Option/Setting is it – user3244284 Jan 29 '14 at 09:30
  • You can ask in ffmpeg mailing list, but my guess is there is no such flag. Note that even the original description allows, but not requires the decoder to produce unaligned frames. – George Y. Jan 29 '14 at 09:34