00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #define _BSD_SOURCE //strcasecmp
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <inttypes.h>
00024
00025 #include "config.h"
00026 #include "mp_msg.h"
00027 #include "help_mp.h"
00028
00029 #include "img_format.h"
00030 #include "mp_image.h"
00031 #include "vf.h"
00032 #include "mpbswap.h"
00033
00034 #include "libswscale/swscale.h"
00035
00036
00037
00038
00039
00040
00041
00042 static const unsigned int bgr_list[]={
00043 IMGFMT_BGR32,
00044 IMGFMT_BGR24,
00045
00046
00047 0
00048 };
00049 static const unsigned int rgb_list[]={
00050 IMGFMT_RGB32,
00051 IMGFMT_RGB24,
00052
00053
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;}
00087 if(ret&VFCAP_CSP_SUPPORTED && !best) best=*p;
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
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
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
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;
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 (!strcasecmp(args,"rgb15")) vf->priv->fmt=IMGFMT_RGB15; else
00212 if (!strcasecmp(args,"rgb16")) vf->priv->fmt=IMGFMT_RGB16; else
00213 if (!strcasecmp(args,"rgb24")) vf->priv->fmt=IMGFMT_RGB24; else
00214 if (!strcasecmp(args,"rgb32")) vf->priv->fmt=IMGFMT_RGB32; else
00215 if (!strcasecmp(args,"bgr15")) vf->priv->fmt=IMGFMT_BGR15; else
00216 if (!strcasecmp(args,"bgr16")) vf->priv->fmt=IMGFMT_BGR16; else
00217 if (!strcasecmp(args,"bgr24")) vf->priv->fmt=IMGFMT_BGR24; else
00218 if (!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