How to force avcodec to use unaligned frame data planes?

2k views Asked by At

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

There are 2 answers

2
George Y. On BEST ANSWER

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>
2
szatmary On

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.