4998
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3
|
|
4 #include "config.h"
|
|
5 #include "mp_msg.h"
|
|
6
|
|
7 #ifdef HAVE_PNG
|
|
8
|
|
9 #include <png.h>
|
|
10
|
|
11 #include "bswap.h"
|
|
12 #include "postproc/rgb2rgb.h"
|
|
13 #include "libvo/fastmemcpy.h"
|
|
14
|
|
15 #include "vd_internal.h"
|
|
16
|
|
17 static vd_info_t info = {
|
|
18 "PNG Images decoder",
|
|
19 "mpng",
|
|
20 "A'rpi",
|
|
21 ".so, based on mpng.c",
|
|
22 "uses libpng, 8bpp modes not supported yet"
|
|
23 };
|
|
24
|
|
25 LIBVD_EXTERN(mpng)
|
|
26
|
|
27 static unsigned int out_fmt=0;
|
|
28
|
|
29 static int last_w=-1;
|
|
30 static int last_h=-1;
|
|
31 static int last_c=-1;
|
|
32
|
|
33 // to set/get/query special features/parameters
|
|
34 static int control(sh_video_t *sh,int cmd,void* arg,...){
|
|
35 return CONTROL_UNKNOWN;
|
|
36 }
|
|
37
|
|
38 // init driver
|
|
39 static int init(sh_video_t *sh){
|
|
40 last_w=-1;
|
|
41 return 1;
|
|
42 }
|
|
43
|
|
44 // uninit driver
|
|
45 static void uninit(sh_video_t *sh){
|
|
46 }
|
|
47
|
|
48 //mp_image_t* mpcodecs_get_image(sh_video_t *sh, int mp_imgtype, int mp_imgflag, int w, int h);
|
|
49
|
|
50 static int pngPointer;
|
|
51 static int pngLength;
|
|
52
|
|
53 static void pngReadFN( png_structp pngstr,png_bytep buffer,png_size_t size )
|
|
54 {
|
|
55 char * p = pngstr->io_ptr;
|
|
56 if(size>pngLength-pngPointer && pngLength>=pngPointer) size=pngLength-pngPointer;
|
|
57 memcpy( buffer,(char *)&p[pngPointer],size );
|
|
58 pngPointer+=size;
|
|
59 }
|
|
60
|
|
61 // decode a frame
|
|
62 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags){
|
|
63 png_structp png;
|
|
64 png_infop info;
|
|
65 png_infop endinfo;
|
|
66 // png_bytep data;
|
|
67 png_bytep * row_p;
|
|
68 png_uint_32 png_width=0,png_height=0;
|
|
69 int depth,color;
|
|
70 png_uint_32 i;
|
|
71 mp_image_t* mpi;
|
|
72
|
|
73 if(len<=0) return NULL; // skipped frame
|
|
74
|
|
75 png=png_create_read_struct( PNG_LIBPNG_VER_STRING,NULL,NULL,NULL );
|
|
76 info=png_create_info_struct( png );
|
|
77 endinfo=png_create_info_struct( png );
|
|
78
|
|
79 pngPointer=8;
|
|
80 pngLength=len;
|
|
81 png_set_read_fn( png,data,pngReadFN );
|
|
82 png_set_sig_bytes( png,8 );
|
|
83 png_read_info( png,info );
|
|
84 png_get_IHDR( png,info,&png_width,&png_height,&depth,&color,NULL,NULL,NULL );
|
|
85
|
|
86 png_set_bgr( png );
|
|
87
|
|
88 switch( info->color_type ) {
|
|
89 case PNG_COLOR_TYPE_GRAY_ALPHA:
|
|
90 mp_msg( MSGT_DECVIDEO,MSGL_INFO,"Sorry gray scaled png with alpha channel not supported at moment.\n" );
|
|
91 break;
|
|
92 case PNG_COLOR_TYPE_GRAY:
|
|
93 case PNG_COLOR_TYPE_PALETTE:
|
|
94 out_fmt=IMGFMT_BGR8;
|
|
95 break;
|
|
96 case PNG_COLOR_TYPE_RGB_ALPHA:
|
|
97 out_fmt=IMGFMT_BGR32;
|
|
98 break;
|
|
99 case PNG_COLOR_TYPE_RGB:
|
|
100 out_fmt=IMGFMT_BGR24;
|
|
101 break;
|
|
102 default:
|
|
103 mp_msg( MSGT_DECVIDEO,MSGL_INFO,"Sorry, unsupported PNG colorspace: %d.\n" ,info->color_type);
|
|
104 }
|
|
105
|
|
106 // (re)init libvo if image parameters changed (width/height/colorspace)
|
|
107 if(last_w!=png_width || last_h!=png_height || last_c!=out_fmt){
|
|
108 last_w=png_width; last_h=png_height; last_c=out_fmt;
|
|
109 if(!out_fmt) return NULL;
|
5124
|
110 if(!mpcodecs_config_vo(sh,png_width,png_height,out_fmt)) return NULL;
|
4998
|
111 }
|
|
112
|
|
113 #if 0
|
|
114 switch( info->color_type )
|
|
115 {
|
|
116 case PNG_COLOR_TYPE_GRAY_ALPHA: printf( "[png] used GrayA -> stripping alpha channel\n" ); break;
|
|
117 case PNG_COLOR_TYPE_GRAY: printf( "[png] used Gray -> rgb\n" ); break;
|
|
118 case PNG_COLOR_TYPE_PALETTE: printf( "[png] used palette -> rgb\n" ); break;
|
|
119 case PNG_COLOR_TYPE_RGB_ALPHA: printf( "[png] used RGBA -> stripping alpha channel\n" ); break;
|
|
120 case PNG_COLOR_TYPE_RGB: printf( "[png] read rgb datas.\n" ); break;
|
|
121 }
|
|
122 #endif
|
|
123
|
|
124 mpi=mpcodecs_get_image(sh, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
|
|
125 png_width,png_height);
|
|
126 if(!mpi) return NULL;
|
|
127
|
|
128 // Let's DECODE!
|
|
129 row_p=(png_bytep*)malloc( sizeof( png_bytep ) * png_height );
|
|
130 //png_get_rowbytes( png,info )
|
|
131 for ( i=0; i < png_height; i++ ) row_p[i]=mpi->planes[0] + mpi->stride[0]*i;
|
|
132 png_read_image( png,row_p );
|
|
133 free( row_p );
|
|
134
|
|
135 //png_get_PLTE( png,info,(png_colorp*)&pal,&cols );
|
|
136
|
|
137 png_read_end( png,endinfo );
|
|
138 png_destroy_read_struct( &png,&info,&endinfo );
|
|
139
|
|
140 return mpi;
|
|
141 }
|
|
142
|
|
143 #endif
|