vf_palette.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * This file is part of MPlayer.
  3. *
  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.
  8. *
  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.
  13. *
  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.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <inttypes.h>
  21. #include "config.h"
  22. #include "mp_msg.h"
  23. #include "help_mp.h"
  24. #include "img_format.h"
  25. #include "mp_image.h"
  26. #include "vf.h"
  27. #include "mpbswap.h"
  28. #include "libswscale/swscale.h"
  29. #include "libavutil/avstring.h"
  30. //===========================================================================//
  31. // commented out 16 and 15 bit output support, because the conversion
  32. // routines are incorrrect. they assume the palette to be of the same
  33. // depth as the output, which is incorrect. --Joey
  34. static const unsigned int bgr_list[]={
  35. IMGFMT_BGR32,
  36. IMGFMT_BGR24,
  37. // IMGFMT_BGR16,
  38. // IMGFMT_BGR15,
  39. 0
  40. };
  41. static const unsigned int rgb_list[]={
  42. IMGFMT_RGB32,
  43. IMGFMT_RGB24,
  44. // IMGFMT_RGB16,
  45. // IMGFMT_RGB15,
  46. 0
  47. };
  48. /**
  49. * Palette is assumed to contain BGR16, see rgb32to16 to convert the palette.
  50. */
  51. static void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  52. {
  53. long i;
  54. for (i=0; i<num_pixels; i++)
  55. ((uint16_t *)dst)[i] = ((const uint16_t *)palette)[src[i]];
  56. }
  57. static void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  58. {
  59. long i;
  60. for (i=0; i<num_pixels; i++)
  61. ((uint16_t *)dst)[i] = bswap_16(((const uint16_t *)palette)[src[i]]);
  62. }
  63. static unsigned int gray_pal[256];
  64. static unsigned int find_best(struct vf_instance *vf, unsigned int fmt){
  65. unsigned int best=0;
  66. int ret;
  67. const unsigned int* p;
  68. if(fmt==IMGFMT_BGR8) p=bgr_list;
  69. else if(fmt==IMGFMT_RGB8) p=rgb_list;
  70. else return 0;
  71. while(*p){
  72. ret=vf->next->query_format(vf->next,*p);
  73. mp_msg(MSGT_VFILTER,MSGL_DBG2,"[%s] query(%s) -> %d\n",vf->info->name,vo_format_name(*p),ret&3);
  74. if(ret&VFCAP_CSP_SUPPORTED_BY_HW){ best=*p; break;} // no conversion -> bingo!
  75. if(ret&VFCAP_CSP_SUPPORTED && !best) best=*p; // best with conversion
  76. ++p;
  77. }
  78. return best;
  79. }
  80. //===========================================================================//
  81. struct vf_priv_s {
  82. unsigned int fmt;
  83. int pal_msg;
  84. };
  85. static int config(struct vf_instance *vf,
  86. int width, int height, int d_width, int d_height,
  87. unsigned int flags, unsigned int outfmt){
  88. if (!vf->priv->fmt)
  89. vf->priv->fmt=find_best(vf,outfmt);
  90. if(!vf->priv->fmt){
  91. // no matching fmt, so force one...
  92. if(outfmt==IMGFMT_RGB8) vf->priv->fmt=IMGFMT_RGB32;
  93. else if(outfmt==IMGFMT_BGR8) vf->priv->fmt=IMGFMT_BGR32;
  94. else return 0;
  95. }
  96. return vf_next_config(vf,width,height,d_width,d_height,flags,vf->priv->fmt);
  97. }
  98. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  99. mp_image_t *dmpi;
  100. uint8_t *old_palette = mpi->planes[1];
  101. // hope we'll get DR buffer:
  102. dmpi=vf_get_image(vf->next,vf->priv->fmt,
  103. MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
  104. mpi->w, mpi->h);
  105. if (!mpi->planes[1])
  106. {
  107. if(!vf->priv->pal_msg){
  108. mp_msg(MSGT_VFILTER,MSGL_V,"[%s] no palette given, assuming builtin grayscale one\n",vf->info->name);
  109. vf->priv->pal_msg=1;
  110. }
  111. mpi->planes[1] = (unsigned char*)gray_pal;
  112. }
  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. case 16:
  118. if (IMGFMT_IS_BGR(dmpi->imgfmt))
  119. palette8tobgr16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
  120. else
  121. palette8torgb16(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
  122. break;
  123. case 24:
  124. if (IMGFMT_IS_BGR(dmpi->imgfmt))
  125. sws_convertPalette8ToPacked24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
  126. else
  127. sws_convertPalette8ToPacked24(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
  128. break;
  129. case 32:
  130. if (IMGFMT_IS_BGR(dmpi->imgfmt))
  131. sws_convertPalette8ToPacked32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
  132. else
  133. sws_convertPalette8ToPacked32(mpi->planes[0],dmpi->planes[0],mpi->h*mpi->w,mpi->planes[1]);
  134. break;
  135. }
  136. } else {
  137. int y;
  138. for(y=0;y<mpi->h;y++){
  139. unsigned char* src=mpi->planes[0]+y*mpi->stride[0];
  140. unsigned char* dst=dmpi->planes[0]+y*dmpi->stride[0];
  141. switch(IMGFMT_RGB_DEPTH(dmpi->imgfmt)){
  142. case 15:
  143. case 16:
  144. if (IMGFMT_IS_BGR(dmpi->imgfmt))
  145. palette8tobgr16(src,dst,mpi->w,mpi->planes[1]);
  146. else
  147. palette8torgb16(src,dst,mpi->w,mpi->planes[1]);
  148. break;
  149. case 24:
  150. if (IMGFMT_IS_BGR(dmpi->imgfmt))
  151. sws_convertPalette8ToPacked24(src,dst,mpi->w,mpi->planes[1]);
  152. else
  153. sws_convertPalette8ToPacked24(src,dst,mpi->w,mpi->planes[1]);
  154. break;
  155. case 32:
  156. if (IMGFMT_IS_BGR(dmpi->imgfmt))
  157. sws_convertPalette8ToPacked32(src,dst,mpi->w,mpi->planes[1]);
  158. else
  159. sws_convertPalette8ToPacked32(src,dst,mpi->w,mpi->planes[1]);
  160. break;
  161. }
  162. }
  163. }
  164. mpi->planes[1] = old_palette;
  165. return vf_next_put_image(vf,dmpi, pts);
  166. }
  167. //===========================================================================//
  168. static int query_format(struct vf_instance *vf, unsigned int fmt){
  169. int best=find_best(vf,fmt);
  170. if(!best) return 0; // no match
  171. return vf->next->query_format(vf->next,best);
  172. }
  173. static void uninit(vf_instance_t *vf) {
  174. free(vf->priv);
  175. }
  176. static int vf_open(vf_instance_t *vf, char *args){
  177. unsigned int i;
  178. vf->config=config;
  179. vf->uninit=uninit;
  180. vf->put_image=put_image;
  181. vf->query_format=query_format;
  182. vf->priv=malloc(sizeof(struct vf_priv_s));
  183. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  184. for(i=0;i<256;i++) gray_pal[i]=0x01010101*i;
  185. if (args)
  186. {
  187. if (!av_strcasecmp(args,"rgb15")) vf->priv->fmt=IMGFMT_RGB15; else
  188. if (!av_strcasecmp(args,"rgb16")) vf->priv->fmt=IMGFMT_RGB16; else
  189. if (!av_strcasecmp(args,"rgb24")) vf->priv->fmt=IMGFMT_RGB24; else
  190. if (!av_strcasecmp(args,"rgb32")) vf->priv->fmt=IMGFMT_RGB32; else
  191. if (!av_strcasecmp(args,"bgr15")) vf->priv->fmt=IMGFMT_BGR15; else
  192. if (!av_strcasecmp(args,"bgr16")) vf->priv->fmt=IMGFMT_BGR16; else
  193. if (!av_strcasecmp(args,"bgr24")) vf->priv->fmt=IMGFMT_BGR24; else
  194. if (!av_strcasecmp(args,"bgr32")) vf->priv->fmt=IMGFMT_BGR32; else
  195. {
  196. mp_msg(MSGT_VFILTER, MSGL_WARN, MSGTR_MPCODECS_UnknownFormatName, args);
  197. return 0;
  198. }
  199. }
  200. return 1;
  201. }
  202. const vf_info_t vf_info_palette = {
  203. "8bpp indexed (using palette) -> BGR 15/16/24/32 conversion",
  204. "palette",
  205. "A'rpi & Alex",
  206. "",
  207. vf_open,
  208. NULL
  209. };
  210. //===========================================================================//