Use MSGT_DECVIDEO in a video decoder.
[mplayer/glamo.git] / libmpcodecs / vf_palette.c
blobcef47df788ebf1fe1c82274dc97e7f34f43bbe72
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
24 #include "config.h"
25 #include "mp_msg.h"
26 #include "help_mp.h"
28 #include "img_format.h"
29 #include "mp_image.h"
30 #include "vf.h"
32 #include "libswscale/rgb2rgb.h"
34 //===========================================================================//
36 // commented out 16 and 15 bit output support, because the conversion
37 // routines are incorrrect. they assume the palette to be of the same
38 // depth as the output, which is incorrect. --Joey
40 static const unsigned int bgr_list[]={
41 IMGFMT_BGR32,
42 IMGFMT_BGR24,
43 // IMGFMT_BGR16,
44 // IMGFMT_BGR15,
47 static const unsigned int rgb_list[]={
48 IMGFMT_RGB32,
49 IMGFMT_RGB24,
50 // IMGFMT_RGB16,
51 // IMGFMT_RGB15,
55 static unsigned int gray_pal[256];
57 static unsigned int find_best(struct vf_instance *vf, unsigned int fmt){
58 unsigned int best=0;
59 int ret;
60 const unsigned int* p;
61 if(fmt==IMGFMT_BGR8) p=bgr_list;
62 else if(fmt==IMGFMT_RGB8) p=rgb_list;
63 else return 0;
64 while(*p){
65 ret=vf->next->query_format(vf->next,*p);
66 mp_msg(MSGT_VFILTER,MSGL_DBG2,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
67 if(ret&VFCAP_CSP_SUPPORTED_BY_HW){ best=*p; break;} // no conversion -> bingo!
68 if(ret&VFCAP_CSP_SUPPORTED && !best) best=*p; // best with conversion
69 ++p;
71 return best;
74 //===========================================================================//
76 struct vf_priv_s {
77 unsigned int fmt;
78 int pal_msg;
81 static int config(struct vf_instance *vf,
82 int width, int height, int d_width, int d_height,
83 unsigned int flags, unsigned int outfmt){
84 if (!vf->priv->fmt)
85 vf->priv->fmt=find_best(vf,outfmt);
86 if(!vf->priv->fmt){
87 // no matching fmt, so force one...
88 if(outfmt==IMGFMT_RGB8) vf->priv->fmt=IMGFMT_RGB32;
89 else if(outfmt==IMGFMT_BGR8) vf->priv->fmt=IMGFMT_BGR32;
90 else return 0;
92 return vf_next_config(vf,width,height,d_width,d_height,flags,vf->priv->fmt);
95 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
96 mp_image_t *dmpi;
97 uint8_t *old_palette = mpi->planes[1];
99 // hope we'll get DR buffer:
100 dmpi=vf_get_image(vf->next,vf->priv->fmt,
101 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
102 mpi->w, mpi->h);
104 if (!mpi->planes[1])
106 if(!vf->priv->pal_msg){
107 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] no palette given, assuming builtin grayscale one\n",vf->info->name);
108 vf->priv->pal_msg=1;
110 mpi->planes[1] = (unsigned char*)gray_pal;
113 if(mpi->w==mpi->stride[0] && dmpi->w*(dmpi->bpp>>3)==dmpi->stride[0]){
114 // no stride conversion needed
115 switch(IMGFMT_RGB_DEPTH(dmpi->imgfmt)){
116 case 15:
117 if (IMGFMT_IS_BGR(dmpi->imgfmt))
118 palette8tobgr15(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
119 else
120 palette8torgb15(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
121 break;
122 case 16:
123 if (IMGFMT_IS_BGR(dmpi->imgfmt))
124 palette8tobgr16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
125 else
126 palette8torgb16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
127 break;
128 case 24:
129 if (IMGFMT_IS_BGR(dmpi->imgfmt))
130 palette8topacked24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
131 else
132 palette8topacked24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
133 break;
134 case 32:
135 if (IMGFMT_IS_BGR(dmpi->imgfmt))
136 palette8topacked32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
137 else
138 palette8topacked32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
139 break;
141 } else {
142 int y;
143 for(y=0;y<mpi->h;y++){
144 unsigned char* src=mpi->planes[0]+y*mpi->stride[0];
145 unsigned char* dst=dmpi->planes[0]+y*dmpi->stride[0];
146 switch(IMGFMT_RGB_DEPTH(dmpi->imgfmt)){
147 case 15:
148 if (IMGFMT_IS_BGR(dmpi->imgfmt))
149 palette8tobgr15(src,dst,mpi->w,mpi->planes[1]);
150 else
151 palette8torgb15(src,dst,mpi->w,mpi->planes[1]);
152 break;
153 case 16:
154 if (IMGFMT_IS_BGR(dmpi->imgfmt))
155 palette8tobgr16(src,dst,mpi->w,mpi->planes[1]);
156 else
157 palette8torgb16(src,dst,mpi->w,mpi->planes[1]);
158 break;
159 case 24:
160 if (IMGFMT_IS_BGR(dmpi->imgfmt))
161 palette8topacked24(src,dst,mpi->w,mpi->planes[1]);
162 else
163 palette8topacked24(src,dst,mpi->w,mpi->planes[1]);
164 break;
165 case 32:
166 if (IMGFMT_IS_BGR(dmpi->imgfmt))
167 palette8topacked32(src,dst,mpi->w,mpi->planes[1]);
168 else
169 palette8topacked32(src,dst,mpi->w,mpi->planes[1]);
170 break;
174 mpi->planes[1] = old_palette;
176 return vf_next_put_image(vf,dmpi, pts);
179 //===========================================================================//
181 static int query_format(struct vf_instance *vf, unsigned int fmt){
182 int best=find_best(vf,fmt);
183 if(!best) return 0; // no match
184 return vf->next->query_format(vf->next,best);
187 static void uninit(vf_instance_t *vf) {
188 free(vf->priv);
191 static int vf_open(vf_instance_t *vf, char *args){
192 unsigned int i;
193 vf->config=config;
194 vf->uninit=uninit;
195 vf->put_image=put_image;
196 vf->query_format=query_format;
197 vf->priv=malloc(sizeof(struct vf_priv_s));
198 memset(vf->priv, 0, sizeof(struct vf_priv_s));
199 for(i=0;i<256;i++) gray_pal[i]=0x01010101*i;
200 if (args)
202 if (!strcasecmp(args,"rgb15")) vf->priv->fmt=IMGFMT_RGB15; else
203 if (!strcasecmp(args,"rgb16")) vf->priv->fmt=IMGFMT_RGB16; else
204 if (!strcasecmp(args,"rgb24")) vf->priv->fmt=IMGFMT_RGB24; else
205 if (!strcasecmp(args,"rgb32")) vf->priv->fmt=IMGFMT_RGB32; else
206 if (!strcasecmp(args,"bgr15")) vf->priv->fmt=IMGFMT_BGR15; else
207 if (!strcasecmp(args,"bgr16")) vf->priv->fmt=IMGFMT_BGR16; else
208 if (!strcasecmp(args,"bgr24")) vf->priv->fmt=IMGFMT_BGR24; else
209 if (!strcasecmp(args,"bgr32")) vf->priv->fmt=IMGFMT_BGR32; else
211 mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_UnknownFormatName, args);
212 return 0;
215 return 1;
218 const vf_info_t vf_info_palette = {
219 "8bpp indexed (using palette) -> BGR 15/16/24/32 conversion",
220 "palette",
221 "A'rpi & Alex",
223 vf_open,
224 NULL
227 //===========================================================================//