demux_mkv: read tags.
[mplayer/glamo.git] / libmpcodecs / vf_palette.c
blob8610f042d6a8cb30b4f48464d8331d94f61d0723
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"
27 #include "img_format.h"
28 #include "mp_image.h"
29 #include "vf.h"
31 #include "libswscale/rgb2rgb.h"
33 //===========================================================================//
35 // commented out 16 and 15 bit output support, because the conversion
36 // routines are incorrrect. they assume the palette to be of the same
37 // depth as the output, which is incorrect. --Joey
39 static const unsigned int bgr_list[]={
40 IMGFMT_BGR32,
41 IMGFMT_BGR24,
42 // IMGFMT_BGR16,
43 // IMGFMT_BGR15,
46 static const unsigned int rgb_list[]={
47 IMGFMT_RGB32,
48 IMGFMT_RGB24,
49 // IMGFMT_RGB16,
50 // IMGFMT_RGB15,
54 static unsigned int gray_pal[256];
56 static unsigned int find_best(struct vf_instance* vf, unsigned int fmt){
57 unsigned int best=0;
58 int ret;
59 const unsigned int* p;
60 if(fmt==IMGFMT_BGR8) p=bgr_list;
61 else if(fmt==IMGFMT_RGB8) p=rgb_list;
62 else return 0;
63 while(*p){
64 ret=vf->next->query_format(vf->next,*p);
65 mp_msg(MSGT_VFILTER,MSGL_DBG2,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
66 if(ret&VFCAP_CSP_SUPPORTED_BY_HW){ best=*p; break;} // no conversion -> bingo!
67 if(ret&VFCAP_CSP_SUPPORTED && !best) best=*p; // best with conversion
68 ++p;
70 return best;
73 //===========================================================================//
75 struct vf_priv_s {
76 unsigned int fmt;
77 int pal_msg;
80 static int config(struct vf_instance* vf,
81 int width, int height, int d_width, int d_height,
82 unsigned int flags, unsigned int outfmt){
83 if (!vf->priv->fmt)
84 vf->priv->fmt=find_best(vf,outfmt);
85 if(!vf->priv->fmt){
86 // no matching fmt, so force one...
87 if(outfmt==IMGFMT_RGB8) vf->priv->fmt=IMGFMT_RGB32;
88 else if(outfmt==IMGFMT_BGR8) vf->priv->fmt=IMGFMT_BGR32;
89 else return 0;
91 return vf_next_config(vf,width,height,d_width,d_height,flags,vf->priv->fmt);
94 static int put_image(struct vf_instance* vf, mp_image_t *mpi, double pts){
95 mp_image_t *dmpi;
96 uint8_t *old_palette = mpi->planes[1];
98 // hope we'll get DR buffer:
99 dmpi=vf_get_image(vf->next,vf->priv->fmt,
100 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
101 mpi->w, mpi->h);
103 if (!mpi->planes[1])
105 if(!vf->priv->pal_msg){
106 mp_msg(MSGT_VFILTER,MSGL_V,"[%s] no palette given, assuming builtin grayscale one\n",vf->info->name);
107 vf->priv->pal_msg=1;
109 mpi->planes[1] = (unsigned char*)gray_pal;
112 if(mpi->w==mpi->stride[0] && dmpi->w*(dmpi->bpp>>3)==dmpi->stride[0]){
113 // no stride conversion needed
114 switch(IMGFMT_RGB_DEPTH(dmpi->imgfmt)){
115 case 15:
116 if (IMGFMT_IS_BGR(dmpi->imgfmt))
117 palette8tobgr15(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
118 else
119 palette8torgb15(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
120 break;
121 case 16:
122 if (IMGFMT_IS_BGR(dmpi->imgfmt))
123 palette8tobgr16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
124 else
125 palette8torgb16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
126 break;
127 case 24:
128 if (IMGFMT_IS_BGR(dmpi->imgfmt))
129 palette8topacked24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
130 else
131 palette8topacked24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
132 break;
133 case 32:
134 if (IMGFMT_IS_BGR(dmpi->imgfmt))
135 palette8topacked32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
136 else
137 palette8topacked32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
138 break;
140 } else {
141 int y;
142 for(y=0;y<mpi->h;y++){
143 unsigned char* src=mpi->planes[0]+y*mpi->stride[0];
144 unsigned char* dst=dmpi->planes[0]+y*dmpi->stride[0];
145 switch(IMGFMT_RGB_DEPTH(dmpi->imgfmt)){
146 case 15:
147 if (IMGFMT_IS_BGR(dmpi->imgfmt))
148 palette8tobgr15(src,dst,mpi->w,mpi->planes[1]);
149 else
150 palette8torgb15(src,dst,mpi->w,mpi->planes[1]);
151 break;
152 case 16:
153 if (IMGFMT_IS_BGR(dmpi->imgfmt))
154 palette8tobgr16(src,dst,mpi->w,mpi->planes[1]);
155 else
156 palette8torgb16(src,dst,mpi->w,mpi->planes[1]);
157 break;
158 case 24:
159 if (IMGFMT_IS_BGR(dmpi->imgfmt))
160 palette8topacked24(src,dst,mpi->w,mpi->planes[1]);
161 else
162 palette8topacked24(src,dst,mpi->w,mpi->planes[1]);
163 break;
164 case 32:
165 if (IMGFMT_IS_BGR(dmpi->imgfmt))
166 palette8topacked32(src,dst,mpi->w,mpi->planes[1]);
167 else
168 palette8topacked32(src,dst,mpi->w,mpi->planes[1]);
169 break;
173 mpi->planes[1] = old_palette;
175 return vf_next_put_image(vf,dmpi, pts);
178 //===========================================================================//
180 static int query_format(struct vf_instance* vf, unsigned int fmt){
181 int best=find_best(vf,fmt);
182 if(!best) return 0; // no match
183 return vf->next->query_format(vf->next,best);
186 static void uninit(vf_instance_t *vf) {
187 free(vf->priv);
190 static int vf_open(vf_instance_t *vf, char *args){
191 unsigned int i;
192 vf->config=config;
193 vf->uninit=uninit;
194 vf->put_image=put_image;
195 vf->query_format=query_format;
196 vf->priv=malloc(sizeof(struct vf_priv_s));
197 memset(vf->priv, 0, sizeof(struct vf_priv_s));
198 for(i=0;i<256;i++) gray_pal[i]=0x01010101*i;
199 if (args)
201 if (!strcasecmp(args,"rgb15")) vf->priv->fmt=IMGFMT_RGB15; else
202 if (!strcasecmp(args,"rgb16")) vf->priv->fmt=IMGFMT_RGB16; else
203 if (!strcasecmp(args,"rgb24")) vf->priv->fmt=IMGFMT_RGB24; else
204 if (!strcasecmp(args,"rgb32")) vf->priv->fmt=IMGFMT_RGB32; else
205 if (!strcasecmp(args,"bgr15")) vf->priv->fmt=IMGFMT_BGR15; else
206 if (!strcasecmp(args,"bgr16")) vf->priv->fmt=IMGFMT_BGR16; else
207 if (!strcasecmp(args,"bgr24")) vf->priv->fmt=IMGFMT_BGR24; else
208 if (!strcasecmp(args,"bgr32")) vf->priv->fmt=IMGFMT_BGR32; else
210 mp_tmsg(MSGT_VFILTER, MSGL_WARN, "[VF_FORMAT] Unknown format name: '%s'.\n", args);
211 return 0;
214 return 1;
217 const vf_info_t vf_info_palette = {
218 "8bpp indexed (using palette) -> BGR 15/16/24/32 conversion",
219 "palette",
220 "A'rpi & Alex",
222 vf_open,
223 NULL
226 //===========================================================================//