527
|
1 /*
|
12857
|
2 * vo_png.c, Portable Network Graphics Renderer for MPlayer
|
527
|
3 *
|
530
|
4 * Copyright 2001 by Felix Buenemann <atmosfear@users.sourceforge.net>
|
527
|
5 *
|
530
|
6 * Uses libpng (which uses zlib), so see according licenses.
|
527
|
7 *
|
|
8 */
|
|
9
|
|
10 #include <stdio.h>
|
|
11 #include <stdlib.h>
|
|
12 #include <string.h>
|
612
|
13 #include <errno.h>
|
527
|
14
|
|
15 #include <png.h>
|
|
16
|
|
17 #include "config.h"
|
|
18 #include "video_out.h"
|
|
19 #include "video_out_internal.h"
|
14451
|
20 #include "subopt-helper.h"
|
527
|
21
|
8148
|
22 static vo_info_t info =
|
527
|
23 {
|
|
24 "PNG file",
|
|
25 "png",
|
|
26 "Felix Buenemann <atmosfear@users.sourceforge.net>",
|
|
27 ""
|
|
28 };
|
|
29
|
8148
|
30 LIBVO_EXTERN (png)
|
|
31
|
527
|
32 extern int verbose;
|
|
33 int z_compression = Z_NO_COMPRESSION;
|
|
34 static int framenum = 0;
|
|
35
|
|
36 struct pngdata {
|
|
37 FILE * fp;
|
|
38 png_structp png_ptr;
|
|
39 png_infop info_ptr;
|
|
40 enum {OK,ERROR} status;
|
|
41 };
|
3950
|
42
|
16171
|
43 static int
|
15212
|
44 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
|
527
|
45 {
|
|
46
|
|
47 if(z_compression == 0) {
|
|
48 printf("PNG Warning: compression level set to 0, compression disabled!\n");
|
14451
|
49 printf("PNG Info: Use -vo png:z=<n> to set compression level from 0 to 9.\n");
|
527
|
50 printf("PNG Info: (0 = no compression, 1 = fastest, lowest - 9 best, slowest compression)\n");
|
|
51 }
|
|
52
|
|
53 if(verbose) printf("PNG Compression level %i\n", z_compression);
|
|
54
|
|
55 return 0;
|
|
56 }
|
|
57
|
|
58
|
7926
|
59 struct pngdata create_png (char * fname, int image_width, int image_height, int swapped)
|
527
|
60 {
|
|
61 struct pngdata png;
|
7926
|
62
|
527
|
63 /*png_structp png_ptr = png_create_write_struct
|
|
64 (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
|
|
65 user_error_fn, user_warning_fn);*/
|
|
66 //png_byte *row_pointers[image_height];
|
|
67 png.png_ptr = png_create_write_struct
|
|
68 (PNG_LIBPNG_VER_STRING, NULL,
|
|
69 NULL, NULL);
|
|
70 png.info_ptr = png_create_info_struct(png.png_ptr);
|
|
71
|
|
72 if (!png.png_ptr) {
|
|
73 if(verbose > 1) printf("PNG Failed to init png pointer\n");
|
|
74 png.status = ERROR;
|
|
75 return png;
|
|
76 }
|
|
77
|
|
78 if (!png.info_ptr) {
|
|
79 if(verbose > 1) printf("PNG Failed to init png infopointer\n");
|
|
80 png_destroy_write_struct(&png.png_ptr,
|
|
81 (png_infopp)NULL);
|
|
82 png.status = ERROR;
|
|
83 return png;
|
|
84 }
|
|
85
|
|
86 if (setjmp(png.png_ptr->jmpbuf)) {
|
|
87 if(verbose > 1) printf("PNG Internal error!\n");
|
|
88 png_destroy_write_struct(&png.png_ptr, &png.info_ptr);
|
|
89 fclose(png.fp);
|
|
90 png.status = ERROR;
|
|
91 return png;
|
|
92 }
|
|
93
|
|
94 png.fp = fopen (fname, "wb");
|
|
95 if (png.fp == NULL) {
|
612
|
96 printf("\nPNG Error opening %s for writing!\n", strerror(errno));
|
527
|
97 png.status = ERROR;
|
|
98 return png;
|
|
99 }
|
|
100
|
|
101 if(verbose > 1) printf("PNG Init IO\n");
|
|
102 png_init_io(png.png_ptr, png.fp);
|
|
103
|
|
104 /* set the zlib compression level */
|
|
105 png_set_compression_level(png.png_ptr, z_compression);
|
|
106
|
|
107
|
|
108 /*png_set_IHDR(png_ptr, info_ptr, width, height,
|
|
109 bit_depth, color_type, interlace_type,
|
|
110 compression_type, filter_type)*/
|
|
111 png_set_IHDR(png.png_ptr, png.info_ptr, image_width, image_height,
|
|
112 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
|
|
113 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
|
114
|
|
115 if(verbose > 1) printf("PNG Write Info\n");
|
|
116 png_write_info(png.png_ptr, png.info_ptr);
|
|
117
|
7926
|
118 if(swapped) {
|
527
|
119 if(verbose > 1) printf("PNG Set BGR Conversion\n");
|
|
120 png_set_bgr(png.png_ptr);
|
|
121 }
|
|
122
|
|
123 png.status = OK;
|
|
124 return png;
|
|
125 }
|
|
126
|
|
127 static uint8_t destroy_png(struct pngdata png) {
|
|
128
|
|
129 if(verbose > 1) printf("PNG Write End\n");
|
|
130 png_write_end(png.png_ptr, png.info_ptr);
|
|
131
|
|
132 if(verbose > 1) printf("PNG Destroy Write Struct\n");
|
|
133 png_destroy_write_struct(&png.png_ptr, &png.info_ptr);
|
|
134
|
|
135 fclose (png.fp);
|
|
136
|
|
137 return 0;
|
|
138 }
|
|
139
|
7693
|
140 static uint32_t draw_image(mp_image_t* mpi){
|
527
|
141 char buf[100];
|
7926
|
142 int k;
|
527
|
143 struct pngdata png;
|
7926
|
144 png_byte *row_pointers[mpi->h];
|
7693
|
145
|
|
146 // if -dr or -slices then do nothing:
|
|
147 if(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK)) return VO_TRUE;
|
527
|
148
|
1078
|
149 snprintf (buf, 100, "%08d.png", ++framenum);
|
527
|
150
|
7926
|
151 png = create_png(buf, mpi->w, mpi->h, mpi->flags&MP_IMGFLAG_SWAPPED);
|
527
|
152
|
|
153 if(png.status){
|
|
154 printf("PNG Error in create_png\n");
|
|
155 return 1;
|
|
156 }
|
|
157
|
|
158 if(verbose > 1) printf("PNG Creating Row Pointers\n");
|
7926
|
159 for ( k = 0; k < mpi->h; k++ )
|
7693
|
160 row_pointers[k] = mpi->planes[0]+mpi->stride[0]*k;
|
|
161
|
527
|
162 //png_write_flush(png.png_ptr);
|
|
163 //png_set_flush(png.png_ptr, nrows);
|
|
164
|
|
165 if(verbose > 1) printf("PNG Writing Image Data\n");
|
|
166 png_write_image(png.png_ptr, row_pointers);
|
|
167
|
7693
|
168 destroy_png(png);
|
527
|
169
|
7693
|
170 return VO_TRUE;
|
527
|
171 }
|
|
172
|
7926
|
173 static void draw_osd(void){}
|
1501
|
174
|
7926
|
175 static void flip_page (void){}
|
527
|
176
|
16171
|
177 static int draw_frame(uint8_t * src[])
|
7926
|
178 {
|
|
179 return -1;
|
527
|
180 }
|
|
181
|
16171
|
182 static int draw_slice( uint8_t *src[],int stride[],int w,int h,int x,int y )
|
527
|
183 {
|
7926
|
184 return -1;
|
527
|
185 }
|
|
186
|
16171
|
187 static int
|
527
|
188 query_format(uint32_t format)
|
|
189 {
|
|
190 switch(format){
|
|
191 case IMGFMT_RGB|24:
|
|
192 case IMGFMT_BGR|24:
|
7694
|
193 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_ACCEPT_STRIDE;
|
527
|
194 }
|
|
195 return 0;
|
|
196 }
|
|
197
|
7926
|
198 static void uninit(void){}
|
527
|
199
|
7926
|
200 static void check_events(void){}
|
4352
|
201
|
14451
|
202 static int int_zero_to_nine(int *sh)
|
|
203 {
|
|
204 if ( (*sh < 0) || (*sh > 9) )
|
|
205 return 0;
|
|
206 return 1;
|
|
207 }
|
|
208
|
|
209 static opt_t subopts[] = {
|
|
210 {"z", OPT_ARG_INT, &z_compression, (opt_test_f)int_zero_to_nine},
|
|
211 {NULL}
|
|
212 };
|
|
213
|
16171
|
214 static int preinit(const char *arg)
|
4352
|
215 {
|
14451
|
216 z_compression = 0;
|
|
217 if (subopt_parse(arg, subopts) != 0) {
|
|
218 return -1;
|
4737
|
219 }
|
|
220 return 0;
|
4352
|
221 }
|
|
222
|
16171
|
223 static int control(uint32_t request, void *data, ...)
|
4352
|
224 {
|
4592
|
225 switch (request) {
|
7693
|
226 case VOCTRL_DRAW_IMAGE:
|
|
227 return draw_image(data);
|
4592
|
228 case VOCTRL_QUERY_FORMAT:
|
|
229 return query_format(*((uint32_t*)data));
|
|
230 }
|
|
231 return VO_NOTIMPL;
|
4352
|
232 }
|