[FFmpeg-devel] [PATCH v5] lavc/rawdec: Use AV_PIX_FMT_PAL8 for 1-bit raw AVI video, and more

Mats Peterson matsp888 at yahoo.com
Mon Jan 25 01:23:00 CET 2016


On 01/25/2016 01:14 AM, Michael Niedermayer wrote:
> On Fri, Jan 22, 2016 at 03:44:30PM +0100, Mats Peterson wrote:
>> You'll have to excuse me, Michael, but the changes I've made,
>> including the line alignment changes, are dependent on each other,
>> and not easy or very logical to split. Here's a new version that
>> displays the Matrix video correctly.
>>
>> Mats
>>
>> --
>> Mats Peterson
>> http://matsp888.no-ip.org/~mats/
>
>>   raw.c    |    2 -
>>   rawdec.c |   92 +++++++++++++++++++++++++++++++++++++++++----------------------
>>   2 files changed, 62 insertions(+), 32 deletions(-)
>> 7e3ead59067fc542c5ad6c3231ab5783bad57c0b  0001-lavc-rawdec-Use-AV_PIX_FMT_PAL8-for-1-bit-raw-AVI-vi.patch
>>  From 0f99c5bdcf39230556eef82fa849dd6342208539 Mon Sep 17 00:00:00 2001
>> From: Mats Peterson <matsp888 at yahoo.com>
>> Date: Fri, 22 Jan 2016 15:38:26 +0100
>> Subject: [PATCH v5] lavc/rawdec: Use AV_PIX_FMT_PAL8 for 1-bit raw AVI video, and more
>>
>> The stuff about 1-bit video not necessarily being black & white in
>> QuickTime goes for AVI as well. Being 1 bit per pixel only means that
>> the data is bi-level. The two colors can be any color. Since many
>> 1 bpp AVI files don't have a palette following the BITMAPINFOHEADER,
>> I'm setting a "default" black & white palette in raw_init_decoder().
>>
>> Also, the line alignment for 4 bpp and 8 bpp depths generated warning
>> messages. The line alignment is now 16 bytes for 1, 2, 4, and 8 bpp depth.
>>
>> Mats
>>
>> ---
>>   libavcodec/raw.c    |    2 +-
>>   libavcodec/rawdec.c |   92 ++++++++++++++++++++++++++++++++++-----------------
>>   2 files changed, 62 insertions(+), 32 deletions(-)
>>
>> diff --git a/libavcodec/raw.c b/libavcodec/raw.c
>> index 3f2cc11..3979c5c 100644
>> --- a/libavcodec/raw.c
>> +++ b/libavcodec/raw.c
>> @@ -242,7 +242,7 @@ unsigned int avcodec_pix_fmt_to_codec_tag(enum AVPixelFormat fmt)
>>   }
>>
>>   const PixelFormatTag avpriv_pix_fmt_bps_avi[] = {
>> -    { AV_PIX_FMT_MONOWHITE, 1 },
>> +    { AV_PIX_FMT_PAL8,    1 },
>>       { AV_PIX_FMT_PAL8,    2 },
>>       { AV_PIX_FMT_PAL8,    4 },
>>       { AV_PIX_FMT_PAL8,    8 },
>> diff --git a/libavcodec/rawdec.c b/libavcodec/rawdec.c
>> index cb0364c..9c4e69a 100644
>> --- a/libavcodec/rawdec.c
>> +++ b/libavcodec/rawdec.c
>> @@ -41,7 +41,7 @@ typedef struct RawVideoContext {
>>       AVBufferRef *palette;
>>       int frame_size;  /* size of the frame in bytes */
>>       int flip;
>> -    int is_1_2_4_bpp; // 1 bpp raw in mov, and 2 or 4 bpp raw in avi/mov
>> +    int is_1_2_4_8_bpp; // 1, 2, 4 and 8 bpp raw in avi/mov
>>       int is_yuv2;
>>       int is_lt_16bpp; // 16bpp pixfmt and bits_per_coded_sample < 16
>>       int tff;
>> @@ -94,8 +94,11 @@ static av_cold int raw_init_decoder(AVCodecContext *avctx)
>>               return AVERROR(ENOMEM);
>>           if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
>>               avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
>> -        else
>> +        else {
>>               memset(context->palette->data, 0, AVPALETTE_SIZE);
>> +            if (avctx->bits_per_coded_sample == 1)
>> +                memset(context->palette->data, 0xff, 4);
>> +        }
>>       }
>>
>>       if ((avctx->extradata_size >= 9 &&
>> @@ -153,19 +156,20 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
>>       RawVideoContext *context       = avctx->priv_data;
>>       const uint8_t *buf             = avpkt->data;
>>       int buf_size                   = avpkt->size;
>> +    int avpkt_stride               = avpkt->size / avctx->height;
>>       int linesize_align             = 4;
>>       int res, len;
>>       int need_copy;
>>
>>       AVFrame   *frame   = data;
>>
>> -    if ((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2
>> -            || avctx->bits_per_coded_sample == 1) &&
>> +    if ((avctx->bits_per_coded_sample == 8 || avctx->bits_per_coded_sample == 4
>> +            || avctx->bits_per_coded_sample == 2 || avctx->bits_per_coded_sample == 1) &&
>>           avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
>>          (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))) {
>> -        context->is_1_2_4_bpp = 1;
>> +        context->is_1_2_4_8_bpp = 1;
>>           context->frame_size = av_image_get_buffer_size(avctx->pix_fmt,
>> -                                                       FFALIGN(avctx->width, 32),
>> +                                                       FFALIGN(avctx->width, 16),
>>                                                          avctx->height, 1);
>>       } else {
>>           context->is_lt_16bpp = av_get_bits_per_pixel(desc) == 16 && avctx->bits_per_coded_sample && avctx->bits_per_coded_sample < 16;
>> @@ -175,7 +179,7 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
>>       if (context->frame_size < 0)
>>           return context->frame_size;
>>
>> -    need_copy = !avpkt->buf || context->is_1_2_4_bpp || context->is_yuv2 || context->is_lt_16bpp;
>> +    need_copy = !avpkt->buf || context->is_1_2_4_8_bpp || context->is_yuv2 || context->is_lt_16bpp;
>>
>>       frame->pict_type        = AV_PICTURE_TYPE_I;
>>       frame->key_frame        = 1;
>> @@ -202,39 +206,65 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
>>       if (!frame->buf[0])
>>           return AVERROR(ENOMEM);
>>
>> -    // 1 bpp raw in mov, and 2 or 4 bpp raw in avi/mov
>> -    if (context->is_1_2_4_bpp) {
>> -        int i;
>> +    // 1, 2, 4 and 8 bpp raw in avi/mov
>> +    if (context->is_1_2_4_8_bpp) {
>> +        int i, j, row_pix = 0;
>>           uint8_t *dst = frame->buf[0]->data;
>>           buf_size = context->frame_size - AVPALETTE_SIZE;
>> -        if (avctx->bits_per_coded_sample == 4) {
>> -            for (i = 0; 2 * i + 1 < buf_size && i<avpkt->size; i++) {
>> -                dst[2 * i + 0] = buf[i] >> 4;
>> -                dst[2 * i + 1] = buf[i] & 15;
>> +        if (avctx->bits_per_coded_sample == 8) {
>> +            for (i = 0, j = 0; j < buf_size && i<avpkt->size; i++, j++) {
>> +                dst[j] = buf[i];
>> +                row_pix++;
>> +                if (row_pix == avctx->width) {
>> +                    i += avpkt_stride - (i % avpkt_stride) - 1;
>> +                    j += 16 - (j % 16) - 1;
>> +                    row_pix = 0;
>> +                }
>> +            }
>> +        } else if (avctx->bits_per_coded_sample == 4) {
>> +            for (i = 0, j = 0; 2 * j + 1 < buf_size && i<avpkt->size; i++, j++) {
>> +                dst[2 * j + 0] = buf[i] >> 4;
>> +                dst[2 * j + 1] = buf[i] & 15;
>> +                row_pix += 2;
>> +                if (row_pix >= avctx->width) {
>> +                    i += avpkt_stride - (i % avpkt_stride) - 1;
>> +                    j += 8 - (j % 8) - 1;
>> +                    row_pix = 0;
>> +                }
>>               }
>> -            linesize_align = 8;
>
> decreasing linesize_align below the SIMD register size is undesirable
> as it could lead to unaligned accesses in subsequent code using
> the frames which would cause speedloss
>
>

I haven't decreased anything, rather increased it from previous 4 and 
8-byte line alignments to 16 bytes. Or do you want me to use 32-byte 
alignment? That's a lot of overhead. And please read your mail.

Mats



More information about the ffmpeg-devel mailing list