libavfilter/libmpcodecs/vf_palette.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of MPlayer.
00003  *
00004  * MPlayer is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * MPlayer is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License along
00015  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
00016  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00017  */
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <inttypes.h>
00022 
00023 #include "config.h"
00024 #include "mp_msg.h"
00025 #include "help_mp.h"
00026 
00027 #include "img_format.h"
00028 #include "mp_image.h"
00029 #include "vf.h"
00030 #include "mpbswap.h"
00031 
00032 #include "libswscale/swscale.h"
00033 
00034 #include "libavutil/avstring.h"
00035 
00036 //===========================================================================//
00037 
00038 // commented out 16 and 15 bit output support, because the conversion
00039 // routines are incorrrect.  they assume the palette to be of the same
00040 // depth as the output, which is incorrect. --Joey
00041 
00042 static const unsigned int bgr_list[]={
00043     IMGFMT_BGR32,
00044     IMGFMT_BGR24,
00045 //    IMGFMT_BGR16,
00046 //    IMGFMT_BGR15,
00047     0
00048 };
00049 static const unsigned int rgb_list[]={
00050     IMGFMT_RGB32,
00051     IMGFMT_RGB24,
00052 //    IMGFMT_RGB16,
00053 //    IMGFMT_RGB15,
00054     0
00055 };
00056 
00060 static void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
00061 {
00062     long i;
00063     for (i=0; i<num_pixels; i++)
00064         ((uint16_t *)dst)[i] = ((const uint16_t *)palette)[src[i]];
00065 }
00066 
00067 static void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
00068 {
00069     long i;
00070     for (i=0; i<num_pixels; i++)
00071         ((uint16_t *)dst)[i] = bswap_16(((const uint16_t *)palette)[src[i]]);
00072 }
00073 
00074 static unsigned int gray_pal[256];
00075 
00076 static unsigned int find_best(struct vf_instance *vf, unsigned int fmt){
00077     unsigned int best=0;
00078     int ret;
00079     const unsigned int* p;
00080     if(fmt==IMGFMT_BGR8) p=bgr_list;
00081     else if(fmt==IMGFMT_RGB8) p=rgb_list;
00082     else return 0;
00083     while(*p){
00084         ret=vf->next->query_format(vf->next,*p);
00085         mp_msg(MSGT_VFILTER,MSGL_DBG2,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
00086         if(ret&VFCAP_CSP_SUPPORTED_BY_HW){ best=*p; break;} // no conversion -> bingo!
00087         if(ret&VFCAP_CSP_SUPPORTED && !best) best=*p; // best with conversion
00088         ++p;
00089     }
00090     return best;
00091 }
00092 
00093 //===========================================================================//
00094 
00095 struct vf_priv_s {
00096     unsigned int fmt;
00097     int pal_msg;
00098 };
00099 
00100 static int config(struct vf_instance *vf,
00101         int width, int height, int d_width, int d_height,
00102         unsigned int flags, unsigned int outfmt){
00103     if (!vf->priv->fmt)
00104         vf->priv->fmt=find_best(vf,outfmt);
00105     if(!vf->priv->fmt){
00106         // no matching fmt, so force one...
00107         if(outfmt==IMGFMT_RGB8) vf->priv->fmt=IMGFMT_RGB32;
00108         else if(outfmt==IMGFMT_BGR8) vf->priv->fmt=IMGFMT_BGR32;
00109         else return 0;
00110     }
00111     return vf_next_config(vf,width,height,d_width,d_height,flags,vf->priv->fmt);
00112 }
00113 
00114 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
00115     mp_image_t *dmpi;
00116     uint8_t *old_palette = mpi->planes[1];
00117 
00118     // hope we'll get DR buffer:
00119     dmpi=vf_get_image(vf->next,vf->priv->fmt,
00120         MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
00121         mpi->w, mpi->h);
00122 
00123     if (!mpi->planes[1])
00124     {
00125         if(!vf->priv->pal_msg){
00126             mp_msg(MSGT_VFILTER,MSGL_V,"[%s] no palette given, assuming builtin grayscale one\n",vf->info->name);
00127             vf->priv->pal_msg=1;
00128         }
00129         mpi->planes[1] = (unsigned char*)gray_pal;
00130     }
00131 
00132     if(mpi->w==mpi->stride[0] && dmpi->w*(dmpi->bpp>>3)==dmpi->stride[0]){
00133         // no stride conversion needed
00134         switch(IMGFMT_RGB_DEPTH(dmpi->imgfmt)){
00135         case 15:
00136         case 16:
00137             if (IMGFMT_IS_BGR(dmpi->imgfmt))
00138                 palette8tobgr16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
00139             else
00140                 palette8torgb16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
00141             break;
00142         case 24:
00143             if (IMGFMT_IS_BGR(dmpi->imgfmt))
00144                 sws_convertPalette8ToPacked24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
00145             else
00146                 sws_convertPalette8ToPacked24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
00147             break;
00148         case 32:
00149             if (IMGFMT_IS_BGR(dmpi->imgfmt))
00150                 sws_convertPalette8ToPacked32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
00151             else
00152                 sws_convertPalette8ToPacked32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
00153             break;
00154         }
00155     } else {
00156         int y;
00157         for(y=0;y<mpi->h;y++){
00158             unsigned char* src=mpi->planes[0]+y*mpi->stride[0];
00159             unsigned char* dst=dmpi->planes[0]+y*dmpi->stride[0];
00160             switch(IMGFMT_RGB_DEPTH(dmpi->imgfmt)){
00161             case 15:
00162             case 16:
00163                 if (IMGFMT_IS_BGR(dmpi->imgfmt))
00164                     palette8tobgr16(src,dst,mpi->w,mpi->planes[1]);
00165                 else
00166                     palette8torgb16(src,dst,mpi->w,mpi->planes[1]);
00167                 break;
00168             case 24:
00169                 if (IMGFMT_IS_BGR(dmpi->imgfmt))
00170                     sws_convertPalette8ToPacked24(src,dst,mpi->w,mpi->planes[1]);
00171                 else
00172                     sws_convertPalette8ToPacked24(src,dst,mpi->w,mpi->planes[1]);
00173                 break;
00174             case 32:
00175                 if (IMGFMT_IS_BGR(dmpi->imgfmt))
00176                     sws_convertPalette8ToPacked32(src,dst,mpi->w,mpi->planes[1]);
00177                 else
00178                     sws_convertPalette8ToPacked32(src,dst,mpi->w,mpi->planes[1]);
00179                 break;
00180             }
00181         }
00182     }
00183     mpi->planes[1] = old_palette;
00184 
00185     return vf_next_put_image(vf,dmpi, pts);
00186 }
00187 
00188 //===========================================================================//
00189 
00190 static int query_format(struct vf_instance *vf, unsigned int fmt){
00191     int best=find_best(vf,fmt);
00192     if(!best) return 0; // no match
00193     return vf->next->query_format(vf->next,best);
00194 }
00195 
00196 static void uninit(vf_instance_t *vf) {
00197   free(vf->priv);
00198 }
00199 
00200 static int vf_open(vf_instance_t *vf, char *args){
00201     unsigned int i;
00202     vf->config=config;
00203     vf->uninit=uninit;
00204     vf->put_image=put_image;
00205     vf->query_format=query_format;
00206     vf->priv=malloc(sizeof(struct vf_priv_s));
00207     memset(vf->priv, 0, sizeof(struct vf_priv_s));
00208     for(i=0;i<256;i++) gray_pal[i]=0x01010101*i;
00209     if (args)
00210     {
00211         if (!av_strcasecmp(args,"rgb15")) vf->priv->fmt=IMGFMT_RGB15; else
00212         if (!av_strcasecmp(args,"rgb16")) vf->priv->fmt=IMGFMT_RGB16; else
00213         if (!av_strcasecmp(args,"rgb24")) vf->priv->fmt=IMGFMT_RGB24; else
00214         if (!av_strcasecmp(args,"rgb32")) vf->priv->fmt=IMGFMT_RGB32; else
00215         if (!av_strcasecmp(args,"bgr15")) vf->priv->fmt=IMGFMT_BGR15; else
00216         if (!av_strcasecmp(args,"bgr16")) vf->priv->fmt=IMGFMT_BGR16; else
00217         if (!av_strcasecmp(args,"bgr24")) vf->priv->fmt=IMGFMT_BGR24; else
00218         if (!av_strcasecmp(args,"bgr32")) vf->priv->fmt=IMGFMT_BGR32; else
00219         {
00220             mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_UnknownFormatName, args);
00221             return 0;
00222         }
00223     }
00224     return 1;
00225 }
00226 
00227 const vf_info_t vf_info_palette = {
00228     "8bpp indexed (using palette) -> BGR 15/16/24/32 conversion",
00229     "palette",
00230     "A'rpi & Alex",
00231     "",
00232     vf_open,
00233     NULL
00234 };
00235 
00236 //===========================================================================//