Mercurial > mplayer.hg
annotate libvo/vo_png.c @ 5540:80d8fed6e683
new tdfxfb yuv driver by Mark Zealey <mark@zealos.org>
author | arpi |
---|---|
date | Tue, 09 Apr 2002 14:02:55 +0000 |
parents | 36aa273ca3ea |
children | 3f1c000cdde4 |
rev | line source |
---|---|
527 | 1 #define DISP |
2 | |
3 /* | |
530 | 4 * vo_png.c, Portable Network Graphics Renderer for Mplayer |
527 | 5 * |
530 | 6 * Copyright 2001 by Felix Buenemann <atmosfear@users.sourceforge.net> |
527 | 7 * |
530 | 8 * Uses libpng (which uses zlib), so see according licenses. |
527 | 9 * |
10 */ | |
11 | |
12 #include <stdio.h> | |
13 #include <stdlib.h> | |
14 #include <string.h> | |
612 | 15 #include <errno.h> |
527 | 16 |
17 #include <png.h> | |
18 //#include "/usr/include/png.h" | |
19 | |
20 | |
21 #include "config.h" | |
22 #include "video_out.h" | |
23 #include "video_out_internal.h" | |
24 | |
4060
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
25 #include "../postproc/swscale.h" |
2732 | 26 #include "../postproc/rgb2rgb.h" |
527 | 27 |
28 LIBVO_EXTERN (png) | |
29 | |
30 static vo_info_t vo_info = | |
31 { | |
32 "PNG file", | |
33 "png", | |
34 "Felix Buenemann <atmosfear@users.sourceforge.net>", | |
35 "" | |
36 }; | |
37 | |
38 #define RGB 0 | |
39 #define BGR 1 | |
40 | |
41 extern int verbose; | |
42 int z_compression = Z_NO_COMPRESSION; | |
43 static int image_width; | |
44 static int image_height; | |
45 static int image_format; | |
46 static uint8_t *image_data=NULL; | |
47 //static char *image_data; | |
4060
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
48 static unsigned int scale_srcW = 0, scale_srcH = 0; |
527 | 49 |
50 static int bpp = 24; | |
51 static int cspace = RGB; | |
52 static int framenum = 0; | |
53 | |
54 struct pngdata { | |
55 FILE * fp; | |
56 png_structp png_ptr; | |
57 png_infop info_ptr; | |
58 enum {OK,ERROR} status; | |
59 }; | |
60 | |
3950
e92653caf5d7
osd support, zlib range fix. by Kim Minh Kaplan <kmkaplan@selfoffice.com>
arpi
parents:
2732
diff
changeset
|
61 static void draw_alpha(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride){ |
e92653caf5d7
osd support, zlib range fix. by Kim Minh Kaplan <kmkaplan@selfoffice.com>
arpi
parents:
2732
diff
changeset
|
62 vo_draw_alpha_rgb24(w, h, src, srca, stride, image_data + 3 * (y0 * image_width + x0), 3 * image_width); |
e92653caf5d7
osd support, zlib range fix. by Kim Minh Kaplan <kmkaplan@selfoffice.com>
arpi
parents:
2732
diff
changeset
|
63 } |
e92653caf5d7
osd support, zlib range fix. by Kim Minh Kaplan <kmkaplan@selfoffice.com>
arpi
parents:
2732
diff
changeset
|
64 |
527 | 65 static uint32_t |
4433 | 66 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t fullscreen, char *title, uint32_t format,const vo_tune_info_t *info) |
527 | 67 { |
4353 | 68 if (fullscreen&0x04 && (width != d_width || height != d_height) && |
69 ((format==IMGFMT_YV12) /*|| (format == IMGFMT_I420) || (format == IMGFMT_IYUV)*/)) { | |
4060
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
70 /* software scaling */ |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
71 image_width = (d_width + 7) & ~7; |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
72 image_height = d_height; |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
73 scale_srcW = width; |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
74 scale_srcH = height; |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
75 SwScale_Init(); |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
76 } |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
77 else { |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
78 image_height = height; |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
79 image_width = width; |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
80 } |
527 | 81 image_format = format; |
82 //printf("Verbose level is %i\n", verbose); | |
83 | |
84 switch(format) { | |
4904
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
85 case IMGFMT_BGR32: |
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
86 bpp = 24; |
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
87 cspace = BGR; |
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
88 image_data = malloc(image_width*image_height*3); |
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
89 break; |
527 | 90 case IMGFMT_BGR24: |
91 bpp = 24; | |
92 cspace = BGR; | |
93 break; | |
94 case IMGFMT_RGB24: | |
95 bpp = 24; | |
96 cspace = RGB; | |
97 break; | |
4353 | 98 case IMGFMT_IYUV: |
99 case IMGFMT_I420: | |
527 | 100 case IMGFMT_YV12: |
101 bpp = 24; | |
4796 | 102 cspace = BGR; |
103 yuv2rgb_init(bpp,MODE_RGB); | |
527 | 104 image_data = malloc(image_width*image_height*3); |
105 break; | |
106 default: | |
107 return 1; | |
108 } | |
109 | |
3950
e92653caf5d7
osd support, zlib range fix. by Kim Minh Kaplan <kmkaplan@selfoffice.com>
arpi
parents:
2732
diff
changeset
|
110 if((z_compression >= 0) && (z_compression <= 9)) { |
527 | 111 if(z_compression == 0) { |
112 printf("PNG Warning: compression level set to 0, compression disabled!\n"); | |
113 printf("PNG Info: Use the -z <n> switch to set compression level from 0 to 9.\n"); | |
114 printf("PNG Info: (0 = no compression, 1 = fastest, lowest - 9 best, slowest compression)\n"); | |
115 } | |
116 } | |
117 else { | |
118 printf("PNG Warning: compression level out of range setting to 1!\n"); | |
119 printf("PNG Info: Use the -z <n> switch to set compression level from 0 to 9.\n"); | |
120 printf("PNG Info: (0 = no compression, 1 = fastest, lowest - 9 best, slowest compression)\n"); | |
121 z_compression = Z_BEST_SPEED; | |
122 } | |
123 | |
124 if(verbose) printf("PNG Compression level %i\n", z_compression); | |
125 | |
126 return 0; | |
127 } | |
128 | |
129 static const vo_info_t* | |
130 get_info(void) | |
131 { | |
132 return &vo_info; | |
133 } | |
134 | |
135 | |
136 struct pngdata create_png (char * fname) | |
137 { | |
138 struct pngdata png; | |
139 | |
140 /*png_structp png_ptr = png_create_write_struct | |
141 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, | |
142 user_error_fn, user_warning_fn);*/ | |
143 //png_byte *row_pointers[image_height]; | |
144 png.png_ptr = png_create_write_struct | |
145 (PNG_LIBPNG_VER_STRING, NULL, | |
146 NULL, NULL); | |
147 png.info_ptr = png_create_info_struct(png.png_ptr); | |
148 | |
149 if (!png.png_ptr) { | |
150 if(verbose > 1) printf("PNG Failed to init png pointer\n"); | |
151 png.status = ERROR; | |
152 return png; | |
153 } | |
154 | |
155 if (!png.info_ptr) { | |
156 if(verbose > 1) printf("PNG Failed to init png infopointer\n"); | |
157 png_destroy_write_struct(&png.png_ptr, | |
158 (png_infopp)NULL); | |
159 png.status = ERROR; | |
160 return png; | |
161 } | |
162 | |
163 if (setjmp(png.png_ptr->jmpbuf)) { | |
164 if(verbose > 1) printf("PNG Internal error!\n"); | |
165 png_destroy_write_struct(&png.png_ptr, &png.info_ptr); | |
166 fclose(png.fp); | |
167 png.status = ERROR; | |
168 return png; | |
169 } | |
170 | |
171 png.fp = fopen (fname, "wb"); | |
172 if (png.fp == NULL) { | |
612 | 173 printf("\nPNG Error opening %s for writing!\n", strerror(errno)); |
527 | 174 png.status = ERROR; |
175 return png; | |
176 } | |
177 | |
178 if(verbose > 1) printf("PNG Init IO\n"); | |
179 png_init_io(png.png_ptr, png.fp); | |
180 | |
181 /* set the zlib compression level */ | |
182 png_set_compression_level(png.png_ptr, z_compression); | |
183 | |
184 | |
185 /*png_set_IHDR(png_ptr, info_ptr, width, height, | |
186 bit_depth, color_type, interlace_type, | |
187 compression_type, filter_type)*/ | |
188 png_set_IHDR(png.png_ptr, png.info_ptr, image_width, image_height, | |
189 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, | |
190 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); | |
191 | |
192 if(verbose > 1) printf("PNG Write Info\n"); | |
193 png_write_info(png.png_ptr, png.info_ptr); | |
194 | |
195 if(cspace) { | |
196 if(verbose > 1) printf("PNG Set BGR Conversion\n"); | |
197 png_set_bgr(png.png_ptr); | |
198 } | |
199 | |
200 png.status = OK; | |
201 return png; | |
202 | |
203 } | |
204 | |
205 static uint8_t destroy_png(struct pngdata png) { | |
206 | |
207 if(verbose > 1) printf("PNG Write End\n"); | |
208 png_write_end(png.png_ptr, png.info_ptr); | |
209 | |
210 if(verbose > 1) printf("PNG Destroy Write Struct\n"); | |
211 png_destroy_write_struct(&png.png_ptr, &png.info_ptr); | |
212 | |
213 fclose (png.fp); | |
214 | |
215 return 0; | |
216 } | |
217 | |
218 static uint32_t draw_frame(uint8_t * src[]) | |
219 { | |
220 char buf[100]; | |
221 int k, bppmul = bpp/8; | |
222 struct pngdata png; | |
223 png_byte *row_pointers[image_height]; | |
224 | |
1078 | 225 snprintf (buf, 100, "%08d.png", ++framenum); |
527 | 226 |
4904
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
227 if (image_format == IMGFMT_BGR32) |
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
228 { |
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
229 rgb32to24(src[0], image_data, image_width * image_height * 4); |
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
230 src[0] = image_data; |
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
231 } |
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
232 |
527 | 233 png = create_png(buf); |
234 | |
235 if(png.status){ | |
236 printf("PNG Error in create_png\n"); | |
237 return 1; | |
238 } | |
239 | |
240 if(verbose > 1) printf("PNG Creating Row Pointers\n"); | |
241 for ( k = 0; k < image_height; k++ ) row_pointers[k] = &src[0][image_width*k*bppmul]; | |
242 | |
243 //png_write_flush(png.png_ptr); | |
244 //png_set_flush(png.png_ptr, nrows); | |
245 | |
246 if(verbose > 1) printf("PNG Writing Image Data\n"); | |
247 png_write_image(png.png_ptr, row_pointers); | |
248 | |
249 return destroy_png(png); | |
250 | |
251 } | |
252 | |
1501
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1078
diff
changeset
|
253 static void draw_osd(void) |
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1078
diff
changeset
|
254 { |
3950
e92653caf5d7
osd support, zlib range fix. by Kim Minh Kaplan <kmkaplan@selfoffice.com>
arpi
parents:
2732
diff
changeset
|
255 vo_draw_text(image_width, image_height, draw_alpha); |
1501
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1078
diff
changeset
|
256 } |
d40f2b686846
changes according to -utf8 option, draw_osd() function added
atlka
parents:
1078
diff
changeset
|
257 |
527 | 258 static void flip_page (void) |
259 { | |
260 char buf[100]; | |
261 int k, bppmul = bpp/8; | |
262 struct pngdata png; | |
263 png_byte *row_pointers[image_height]; | |
264 | |
4353 | 265 if((image_format == IMGFMT_YV12) || (image_format == IMGFMT_IYUV) || (image_format == IMGFMT_I420)) { |
527 | 266 |
1078 | 267 snprintf (buf, 100, "%08d.png", ++framenum); |
527 | 268 |
269 png = create_png(buf); | |
270 | |
271 if(png.status){ | |
272 printf("PNG Error in create_png\n"); | |
273 } | |
274 | |
275 if(verbose > 1) printf("PNG Creating Row Pointers\n"); | |
276 for ( k = 0; k < image_height; k++ ) row_pointers[k] = &image_data[image_width*k*bppmul]; | |
277 | |
278 //png_write_flush(png.png_ptr); | |
279 //png_set_flush(png.png_ptr, nrows); | |
280 | |
281 if(verbose > 1) printf("PNG Writing Image Data\n"); | |
282 png_write_image(png.png_ptr, row_pointers); | |
283 | |
284 destroy_png(png); | |
285 } | |
286 } | |
287 | |
288 static uint32_t draw_slice( uint8_t *src[],int stride[],int w,int h,int x,int y ) | |
289 { | |
4353 | 290 /* hack: swap planes for I420 ;) -- alex */ |
291 if ((image_format == IMGFMT_IYUV) || (image_format == IMGFMT_I420)) | |
292 { | |
293 uint8_t *src_i420[3]; | |
294 | |
295 src_i420[0] = src[0]; | |
296 src_i420[1] = src[2]; | |
297 src_i420[2] = src[1]; | |
298 src = src_i420; | |
299 } | |
300 | |
4060
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
301 if (scale_srcW) { |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
302 uint8_t *dst[3] = {image_data, NULL, NULL}; |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
303 SwScale_YV12slice(src,stride,y,h, |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
304 dst, image_width*((bpp+7)/8), bpp, |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
305 scale_srcW, scale_srcH, image_width, image_height); |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
306 } |
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
307 else { |
4353 | 308 uint8_t *dst = image_data + (image_width * y + x) * (bpp/8); |
527 | 309 yuv2rgb(dst,src[0],src[1],src[2],w,h,image_width*(bpp/8),stride[0],stride[1]); |
4060
974e028471a4
SwScaler support for vo_png by Kim Minh, SwScale w/aspecz for vo_dga by me
atmos4
parents:
3950
diff
changeset
|
310 } |
527 | 311 return 0; |
312 } | |
313 | |
314 static uint32_t | |
315 query_format(uint32_t format) | |
316 { | |
317 switch(format){ | |
4353 | 318 case IMGFMT_IYUV: |
319 case IMGFMT_I420: | |
527 | 320 case IMGFMT_YV12: |
321 case IMGFMT_RGB|24: | |
322 case IMGFMT_BGR|24: | |
4904
36aa273ca3ea
Added BGR32 output support to PNG vo target because, hey, why
melanson
parents:
4796
diff
changeset
|
323 case IMGFMT_BGR|32: |
527 | 324 return 1; |
325 } | |
326 return 0; | |
327 } | |
328 | |
329 static void | |
330 uninit(void) | |
331 { | |
332 if(image_data){ free(image_data);image_data=NULL;} | |
333 } | |
334 | |
335 | |
336 static void check_events(void) | |
337 { | |
338 } | |
4352 | 339 |
340 static uint32_t preinit(const char *arg) | |
341 { | |
4737
32e1f5042f65
I don't like such reports: '-vo dga:vidix or -vo x11:vidix works fine for me'
nick
parents:
4596
diff
changeset
|
342 if(arg) |
32e1f5042f65
I don't like such reports: '-vo dga:vidix or -vo x11:vidix works fine for me'
nick
parents:
4596
diff
changeset
|
343 { |
32e1f5042f65
I don't like such reports: '-vo dga:vidix or -vo x11:vidix works fine for me'
nick
parents:
4596
diff
changeset
|
344 printf("PNG Unknown subdevice: %s\n",arg); |
32e1f5042f65
I don't like such reports: '-vo dga:vidix or -vo x11:vidix works fine for me'
nick
parents:
4596
diff
changeset
|
345 return ENOSYS; |
32e1f5042f65
I don't like such reports: '-vo dga:vidix or -vo x11:vidix works fine for me'
nick
parents:
4596
diff
changeset
|
346 } |
32e1f5042f65
I don't like such reports: '-vo dga:vidix or -vo x11:vidix works fine for me'
nick
parents:
4596
diff
changeset
|
347 return 0; |
4352 | 348 } |
349 | |
4596 | 350 static uint32_t control(uint32_t request, void *data, ...) |
4352 | 351 { |
4592
5fbfd8545c3b
query_ stuff replaced by new control() - patch by David Holm
arpi
parents:
4433
diff
changeset
|
352 switch (request) { |
5fbfd8545c3b
query_ stuff replaced by new control() - patch by David Holm
arpi
parents:
4433
diff
changeset
|
353 case VOCTRL_QUERY_FORMAT: |
5fbfd8545c3b
query_ stuff replaced by new control() - patch by David Holm
arpi
parents:
4433
diff
changeset
|
354 return query_format(*((uint32_t*)data)); |
5fbfd8545c3b
query_ stuff replaced by new control() - patch by David Holm
arpi
parents:
4433
diff
changeset
|
355 } |
5fbfd8545c3b
query_ stuff replaced by new control() - patch by David Holm
arpi
parents:
4433
diff
changeset
|
356 return VO_NOTIMPL; |
4352 | 357 } |