[Ffmpeg-devel] [PATCH] convert libswscale to PIX_FMT_*

Michael Niedermayer michaelni
Wed Sep 13 14:56:03 CEST 2006


Hi

On Mon, Sep 11, 2006 at 10:47:25AM +0200, Luca Abeni wrote:
> Hi all,
> 
> here is a patch for removing the dependency of libswscale on
> img_format.h (after applying the patch, the swscale interface will use
> PIX_FMT_* instead of IMGFMT_*).
> I tested it during the last week, and it does not seem to break anything
> in mplayer (but I do not know how to test advanced swscale usage in
> mplayer - suggestions are welcome).
> 
> I think the patch is now in an acceptable state for being reviewed
> (since the last version, I removed some duplicate macros by moving them
> in swscale_internal.h, I removed an fprintf, and I fixed some tabs vs
> spaces inconsistencies).

[...]

> @@ -47,6 +46,38 @@
>  
>  #define DITHER1XBPP // only for mmx
>  
> +int fmt_depth(int fmt)
> +{

shouldnt that either be static or have a prefix?


>  
>  #if defined(ARCH_X86) || defined(ARCH_X86_64)
> @@ -1509,15 +1543,73 @@
>  	return srcSliceH;
>  }
>  
> +/* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
> +static int fmt_id(int fmt)
> +{
> +	switch (fmt) {
> +	    case PIX_FMT_RGB32:
> +	    case PIX_FMT_RGB32_1:
> +	    case PIX_FMT_BGR32:
> +	    case PIX_FMT_BGR32_1:
> +		return 8;
> +	    case PIX_FMT_RGB24:
> +	    case PIX_FMT_BGR24:
> +		return 6;
> +	    case PIX_FMT_RGB565:
> +	    case PIX_FMT_BGR565:
> +		return 4;
> +	    case PIX_FMT_RGB555:
> +	    case PIX_FMT_BGR555:
> +		return 3;
> +	    case PIX_FMT_RGB8:
> +	    case PIX_FMT_BGR8:
> +		return 2;
> +	    case PIX_FMT_RGB4:
> +	    case PIX_FMT_BGR4:
> +		return 1;
> +	    case PIX_FMT_MONOBLACK:
> +		return 0;

isnt that the same as fmt_depth()>>2 ?

except these patch ok, (commit it after fixing these if it works and there
are no objectons from anyone else) (= no need to repost it with these 2
minor things fixed)


> +    switch(fmt) {
> +        case PIX_FMT_BGRA:
> +        case PIX_FMT_ABGR:
> +        case PIX_FMT_RGBA:
> +        case PIX_FMT_ARGB:
> +            return 32;
> +        case PIX_FMT_BGR24:
> +        case PIX_FMT_RGB24:
> +            return 24;
> +        case PIX_FMT_BGR565:
> +        case PIX_FMT_RGB565:
> +            return 16;
> +        case PIX_FMT_BGR555:
> +        case PIX_FMT_RGB555:
> +            return 15;
> +        case PIX_FMT_BGR8:
> +        case PIX_FMT_RGB8:
> +            return 8;
> +        case PIX_FMT_BGR4:
> +        case PIX_FMT_RGB4:
> +        case PIX_FMT_BGR4_BYTE:
> +        case PIX_FMT_RGB4_BYTE:
> +            return 4;
> +        case PIX_FMT_MONOBLACK:
> +            return 1;
> +        default:
> +            return 0;
> +    }
> +}
> +
>  const uint8_t  __attribute__((aligned(8))) dither_2x2_4[2][8]={
>  {  1,   3,   1,   3,   1,   3,   1,   3, },
>  {  2,   0,   2,   0,   2,   0,   2,   0, },
[...]
> +char *sws_format_name(enum PixelFormat format)
>  {
> -    static char fmt_name[64];
> -    char *res;
> -    static int buffer;
> -
> -    res = fmt_name + buffer * 32;
> -    buffer = 1 - buffer;
> -    snprintf(res, 32, "0x%x (%c%c%c%c)", format,
> -		    format >> 24, (format >> 16) & 0xFF,
> -		    (format >> 8) & 0xFF,
> -		    format & 0xFF);
> -
> -    return res;
> +    switch (format) {
> +        case PIX_FMT_YUV420P:
> +            return "yuv420p";
> +        case PIX_FMT_YUYV422:
> +            return "yuyv422";
> +        case PIX_FMT_RGB24:
> +            return "rgb24";
> +        case PIX_FMT_BGR24:
> +            return "bgr24";
> +        case PIX_FMT_YUV422P:
> +            return "yuv422p";
> +        case PIX_FMT_YUV444P:
> +            return "yuv444p";
> +        case PIX_FMT_RGB32:
> +            return "rgb32";
> +        case PIX_FMT_YUV410P:
> +            return "yuv410p";
> +        case PIX_FMT_YUV411P:
> +            return "yuv411p";
> +        case PIX_FMT_RGB565:
> +            return "rgb565";
> +        case PIX_FMT_RGB555:
> +            return "rgb555";
> +        case PIX_FMT_GRAY8:
> +            return "gray8";
> +        case PIX_FMT_MONOWHITE:
> +            return "mono white";
> +        case PIX_FMT_MONOBLACK:
> +            return "mono black";
> +        case PIX_FMT_PAL8:
> +            return "Palette";
> +        case PIX_FMT_YUVJ420P:
> +            return "yuvj420p";
> +        case PIX_FMT_YUVJ422P:
> +            return "yuvj422p";
> +        case PIX_FMT_YUVJ444P:
> +            return "yuvj444p";
> +        case PIX_FMT_XVMC_MPEG2_MC:
> +            return "xvmc_mpeg2_mc";
> +        case PIX_FMT_XVMC_MPEG2_IDCT:
> +            return "xvmc_mpeg2_idct";
> +        case PIX_FMT_UYVY422:
> +            return "uyvy422";
> +        case PIX_FMT_UYYVYY411:
> +            return "uyyvyy411";
> +        case PIX_FMT_RGB32_1:
> +            return "rgb32x";
> +        case PIX_FMT_BGR32_1:
> +            return "bgr32x";
> +        case PIX_FMT_BGR32:
> +            return "bgr32";
> +        case PIX_FMT_BGR565:
> +            return "bgr565";
> +        case PIX_FMT_BGR555:
> +            return "bgr555";
> +        case PIX_FMT_BGR8:
> +            return "bgr8";
> +        case PIX_FMT_BGR4:
> +            return "bgr4";
> +        case PIX_FMT_BGR4_BYTE:
> +            return "bgr4 byte";
> +        case PIX_FMT_RGB8:
> +            return "rgb8";
> +        case PIX_FMT_RGB4:
> +            return "rgb4";
> +        case PIX_FMT_RGB4_BYTE:
> +            return "rgb4 byte";
> +        case PIX_FMT_NV12:
> +            return "nv12";
> +        case PIX_FMT_NV21:
> +            return "nv21";
> +        default:
> +            return "Unknown format";
> +    }
>  }

we really should add a small table to libavutil which contains these at 
some point in the future maybe something like

typedef struct PixFmtInfo {
    const char *name;
    uint8_t flags;
#define PIX_FMT_FLAG_YUV    1
#define PIX_FMT_FLAG_PLANAR 2
#define PIX_FMT_FLAG_16BIT  4
#define PIX_FMT_FLAG_32BIT  8
#define PIX_FMT_FLAG_ELEM_IS_SAMPLE  16 ///< each 8/16/32 bit element matches a sample
#define PIX_FMT_FLAG_CHROMA_2NDX     32 ///< for packed 422 and NV12/NV21
    uint8_t log2_h_chroma_sub_sample;
    uint8_t log2_v_chroma_sub_sample;
    uint8_t sample_stride;
    uint8_t sample_pos[4];
    uint8_t mask[4]
}

this would also allow some generic (and slow) reading/writing code like:

int read_sample(void *v[4], int index, int c){
    if((flags&PIX_FMT_FLAG_CHROMA_2NDX) && c) //take care of packed 422 and NV12/21, alternatively we could have a sample_stride[4]
        index += index;
    index = index * sample_stride + sample_pos[c];
    if     (flags & PIX_FMT_FLAG_16BIT)
        return (((*uint16_t)v[c])[ index>>4 ] >> (index&15)) & mask[c];
    else if(flags & PIX_FMT_FLAG_32BIT)
        return (((*uint32_t)v[c])[ index>>5 ] >> (index&31)) & mask[c];
    else
        return (((*uint8_t )v[c])[ index>>3 ] >> (index& 7)) & mask[c];
}

or a little longer and faster

int read_sample(void *v[4], int index, int c){
    if(flags & PIX_FMT_FLAG_PLANAR){
        // planar is always 8bit currently
        return ((*uint8_t)v[c])[index];
    }
    if((flags&PIX_FMT_FLAG_CHROMA_2NDX) && c)
        index += index;
    if(flags & PIX_FMT_FLAG_ELEM_IS_SAMPLE){
        // this case is currently also always 8bit
        return ((*uint8_t)v[c])[ (index * sample_stride)>>3 ];
    }else{
        index = index * sample_stride + sample_pos[c];
        if     (flags & PIX_FMT_FLAG_16BIT)
            return (((*uint16_t)v[c])[ index>>4 ] >> (index&15)) & mask[c];
        else if(flags & PIX_FMT_FLAG_32BIT)
            return (((*uint32_t)v[c])[ index>>5 ] >> (index&31)) & mask[c];
        else
            return (((*uint8_t )v[c])[ index>>3 ] >> (index& 7)) & mask[c];
    }
}

ideally of course the table should be smaller then what ive come up with
here ...

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

In the past you could go to a library and read, borrow or copy any book
Today you'd get arrested for mere telling someone where the library is




More information about the ffmpeg-devel mailing list