adding the code documentation guide lines
[mplayer/glamo.git] / libmpcodecs / vf_palette.c
blobd0cb8fffb35bd9193e20252d2077ba55deb6db12
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
6 #include "../config.h"
7 #include "../mp_msg.h"
9 #include "img_format.h"
10 #include "mp_image.h"
11 #include "vf.h"
13 #include "../postproc/rgb2rgb.h"
15 //===========================================================================//
17 // commented out 16 and 15 bit output support, because the conversion
18 // routines are incorrrect. they assume the palette to be of the same
19 // depth as the output, which is incorrect. --Joey
21 static unsigned int bgr_list[]={
22 IMGFMT_BGR32,
23 IMGFMT_BGR24,
24 // IMGFMT_BGR16,
25 // IMGFMT_BGR15,
28 static unsigned int rgb_list[]={
29 IMGFMT_RGB32,
30 IMGFMT_RGB24,
31 // IMGFMT_RGB16,
32 // IMGFMT_RGB15,
36 static unsigned int gray_pal[256];
38 static unsigned int find_best(struct vf_instance_s* vf, unsigned int fmt){
39 unsigned int best=0;
40 int ret;
41 unsigned int* p;
42 if(fmt==IMGFMT_BGR8) p=bgr_list;
43 else if(fmt==IMGFMT_RGB8) p=rgb_list;
44 else return 0;
45 while(*p){
46 ret=vf->next->query_format(vf->next,*p);
47 mp_msg(MSGT_VFILTER,MSGL_DBG2,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
48 if(ret&VFCAP_CSP_SUPPORTED_BY_HW){ best=*p; break;} // no conversion -> bingo!
49 if(ret&VFCAP_CSP_SUPPORTED && !best) best=*p; // best with conversion
50 ++p;
52 return best;
55 //===========================================================================//
57 struct vf_priv_s {
58 unsigned int fmt;
59 int pal_msg;
62 static int config(struct vf_instance_s* vf,
63 int width, int height, int d_width, int d_height,
64 unsigned int flags, unsigned int outfmt){
65 if (!vf->priv->fmt)
66 vf->priv->fmt=find_best(vf,outfmt);
67 if(!vf->priv->fmt){
68 // no matching fmt, so force one...
69 if(outfmt==IMGFMT_RGB8) vf->priv->fmt=IMGFMT_RGB32;
70 else if(outfmt==IMGFMT_BGR8) vf->priv->fmt=IMGFMT_BGR32;
71 else return 0;
73 return vf_next_config(vf,width,height,d_width,d_height,flags,vf->priv->fmt);
76 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
77 mp_image_t *dmpi;
79 // hope we'll get DR buffer:
80 dmpi=vf_get_image(vf->next,vf->priv->fmt,
81 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
82 mpi->w, mpi->h);
84 if (!mpi->planes[1])
86 if(!vf->priv->pal_msg){
87 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] no palette given, assuming builtin grayscale one\n",vf->info->name);
88 vf->priv->pal_msg=1;
90 mpi->planes[1] = (unsigned char*)gray_pal;
93 if(mpi->w==mpi->stride[0] && dmpi->w*(dmpi->bpp>>3)==dmpi->stride[0]){
94 // no stride conversion needed
95 switch(dmpi->imgfmt&255){
96 case 15:
97 if (dmpi->flags & MP_IMGFLAG_SWAPPED)
98 palette8tobgr15(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
99 else
100 palette8torgb15(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
101 break;
102 case 16:
103 if (dmpi->flags & MP_IMGFLAG_SWAPPED)
104 palette8tobgr16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
105 else
106 palette8torgb16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
107 break;
108 case 24:
109 if (dmpi->flags & MP_IMGFLAG_SWAPPED)
110 palette8tobgr24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
111 else
112 palette8torgb24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
113 break;
114 case 32:
115 if (dmpi->flags & MP_IMGFLAG_SWAPPED)
116 palette8tobgr32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
117 else
118 palette8torgb32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
119 break;
121 } else {
122 int y;
123 for(y=0;y<mpi->h;y++){
124 unsigned char* src=mpi->planes[0]+y*mpi->stride[0];
125 unsigned char* dst=dmpi->planes[0]+y*dmpi->stride[0];
126 switch(dmpi->imgfmt&255){
127 case 15:
128 if (dmpi->flags & MP_IMGFLAG_SWAPPED)
129 palette8tobgr15(src,dst,mpi->w,mpi->planes[1]);
130 else
131 palette8torgb15(src,dst,mpi->w,mpi->planes[1]);
132 break;
133 case 16:
134 if (dmpi->flags & MP_IMGFLAG_SWAPPED)
135 palette8tobgr16(src,dst,mpi->w,mpi->planes[1]);
136 else
137 palette8torgb16(src,dst,mpi->w,mpi->planes[1]);
138 break;
139 case 24:
140 if (dmpi->flags & MP_IMGFLAG_SWAPPED)
141 palette8tobgr24(src,dst,mpi->w,mpi->planes[1]);
142 else
143 palette8torgb24(src,dst,mpi->w,mpi->planes[1]);
144 break;
145 case 32:
146 if (dmpi->flags & MP_IMGFLAG_SWAPPED)
147 palette8tobgr32(src,dst,mpi->w,mpi->planes[1]);
148 else
149 palette8torgb32(src,dst,mpi->w,mpi->planes[1]);
150 break;
155 return vf_next_put_image(vf,dmpi);
158 //===========================================================================//
160 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
161 int best=find_best(vf,fmt);
162 if(!best) return 0; // no match
163 return vf->next->query_format(vf->next,best);
166 static int open(vf_instance_t *vf, char* args){
167 unsigned int i;
168 vf->config=config;
169 vf->put_image=put_image;
170 vf->query_format=query_format;
171 vf->priv=malloc(sizeof(struct vf_priv_s));
172 memset(vf->priv, 0, sizeof(struct vf_priv_s));
173 for(i=0;i<256;i++) gray_pal[i]=0x01010101*i;
174 if (args)
176 if (!strcasecmp(args,"rgb15")) vf->priv->fmt=IMGFMT_RGB15; else
177 if (!strcasecmp(args,"rgb16")) vf->priv->fmt=IMGFMT_RGB16; else
178 if (!strcasecmp(args,"rgb24")) vf->priv->fmt=IMGFMT_RGB24; else
179 if (!strcasecmp(args,"rgb32")) vf->priv->fmt=IMGFMT_RGB32; else
180 if (!strcasecmp(args,"bgr15")) vf->priv->fmt=IMGFMT_BGR15; else
181 if (!strcasecmp(args,"bgr16")) vf->priv->fmt=IMGFMT_BGR16; else
182 if (!strcasecmp(args,"bgr24")) vf->priv->fmt=IMGFMT_BGR24; else
183 if (!strcasecmp(args,"bgr32")) vf->priv->fmt=IMGFMT_BGR32; else
185 printf("Unknown forced format name: '%s'\n", args);
186 return(0);
189 return 1;
192 vf_info_t vf_info_palette = {
193 "8bpp indexed (using palette) -> BGR 15/16/24/32 conversion",
194 "palette",
195 "A'rpi & Alex",
197 open,
198 NULL
201 //===========================================================================//