Add support for VDPAU video out, including hardware decoding.
[mplayer/glamo.git] / libmpcodecs / vd_mpng.c
blobf4af973e8d0dbad4bc2bd126b86002347dde4882
1 #include <stdio.h>
2 #include <stdlib.h>
4 #include "config.h"
5 #include "mp_msg.h"
7 #include <png.h>
9 #include "libavutil/common.h"
10 #include "mpbswap.h"
11 #include "libvo/fastmemcpy.h"
13 #include "vd_internal.h"
15 static vd_info_t info = {
16 "PNG Images decoder",
17 "mpng",
18 "A'rpi",
19 ".so, based on mpng.c",
20 "uses libpng, 8bpp modes not supported yet"
23 LIBVD_EXTERN(mpng)
25 static unsigned int out_fmt=0;
27 static int last_w=-1;
28 static int last_h=-1;
29 static int last_c=-1;
31 // to set/get/query special features/parameters
32 static int control(sh_video_t *sh,int cmd,void* arg,...){
33 switch (cmd)
35 case VDCTRL_QUERY_FORMAT:
36 if (*((int *) arg) == out_fmt) return CONTROL_TRUE;
37 return CONTROL_FALSE;
39 return CONTROL_UNKNOWN;
42 // init driver
43 static int init(sh_video_t *sh){
44 last_w=-1;
45 return 1;
48 // uninit driver
49 static void uninit(sh_video_t *sh){
52 //mp_image_t* mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag, int w, int h);
54 static int pngPointer;
55 static int pngLength;
57 static void pngReadFN( png_structp pngstr,png_bytep buffer,png_size_t size )
59 char * p = pngstr->io_ptr;
60 if(size>pngLength-pngPointer && pngLength>=pngPointer) size=pngLength-pngPointer;
61 fast_memcpy( buffer,(char *)&p[pngPointer],size );
62 pngPointer+=size;
65 // decode a frame
66 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
67 png_structp png;
68 png_infop info;
69 png_infop endinfo;
70 // png_bytep data;
71 png_bytep * row_p;
72 png_uint_32 png_width=0,png_height=0;
73 int depth,color;
74 png_uint_32 i;
75 mp_image_t* mpi;
77 int cols;
78 png_colorp pal;
79 unsigned char *p;
81 if(len<=0) return NULL; // skipped frame
83 png=png_create_read_struct( PNG_LIBPNG_VER_STRING,NULL,NULL,NULL );
84 info=png_create_info_struct( png );
85 endinfo=png_create_info_struct( png );
87 pngPointer=8;
88 pngLength=len;
89 png_set_read_fn( png,data,pngReadFN );
90 png_set_strip_16( png );
91 png_set_sig_bytes( png,8 );
92 png_read_info( png,info );
93 png_get_IHDR( png,info,&png_width,&png_height,&depth,&color,NULL,NULL,NULL );
94 png_set_bgr( png );
96 switch( info->color_type ) {
97 case PNG_COLOR_TYPE_GRAY_ALPHA:
98 mp_msg( MSGT_DECVIDEO,MSGL_INFO,"Sorry gray scaled png with alpha channel not supported at moment.\n" );
99 break;
100 case PNG_COLOR_TYPE_GRAY:
101 out_fmt=IMGFMT_Y800;
102 break;
103 case PNG_COLOR_TYPE_PALETTE:
104 out_fmt=IMGFMT_BGR8;
105 break;
106 case PNG_COLOR_TYPE_RGB_ALPHA:
107 out_fmt=IMGFMT_BGR32;
108 break;
109 case PNG_COLOR_TYPE_RGB:
110 out_fmt=IMGFMT_BGR24;
111 break;
112 default:
113 mp_msg( MSGT_DECVIDEO,MSGL_INFO,"Sorry, unsupported PNG colorspace: %d.\n" ,info->color_type);
116 // (re)init libvo if image parameters changed (width/height/colorspace)
117 if(last_w!=png_width || last_h!=png_height || last_c!=out_fmt){
118 last_w=png_width; last_h=png_height; last_c=out_fmt;
119 if(!out_fmt) return NULL;
120 if(!mpcodecs_config_vo(sh,png_width,png_height,out_fmt)) return NULL;
123 #if 0
124 switch( info->color_type )
126 case PNG_COLOR_TYPE_GRAY_ALPHA: printf( "[png] used GrayA -> stripping alpha channel\n" ); break;
127 case PNG_COLOR_TYPE_GRAY: printf( "[png] used Gray -> rgb\n" ); break;
128 case PNG_COLOR_TYPE_PALETTE: printf( "[png] used palette -> rgb\n" ); break;
129 case PNG_COLOR_TYPE_RGB_ALPHA: printf( "[png] used RGBA -> stripping alpha channel\n" ); break;
130 case PNG_COLOR_TYPE_RGB: printf( "[png] read rgb datas.\n" ); break;
132 #endif
134 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
135 png_width,png_height);
136 if(!mpi) return NULL;
138 // Let's DECODE!
139 row_p=malloc( sizeof( png_bytep ) * png_height );
140 //png_get_rowbytes( png,info )
141 for ( i=0; i < png_height; i++ ) row_p[i]=mpi->planes[0] + mpi->stride[0]*i;
142 png_read_image( png,row_p );
143 free( row_p );
145 if (out_fmt==IMGFMT_BGR8) {
146 png_get_PLTE( png,info,&pal,&cols );
147 mpi->planes[1] = (char*)realloc(mpi->planes[1], 4*cols);
148 p = mpi->planes[1];
149 for (i = 0; i < cols; i++) {
150 *p++ = pal[i].blue;
151 *p++ = pal[i].green;
152 *p++ = pal[i].red;
153 *p++ = 0;
157 png_read_end( png,endinfo );
158 png_destroy_read_struct( &png,&info,&endinfo );
160 return mpi;