ao_pulse: support native mute control
[mplayer.git] / libvo / vo_png.c
blob6e23bafd5faf2d77699df268e7f0e8a092a67aea
1 /*
2 * Portable Network Graphics renderer
4 * Copyright 2001 by Felix Buenemann <atmosfear@users.sourceforge.net>
6 * Uses libpng (which uses zlib), so see according licenses.
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
30 #include "config.h"
31 #include "mp_msg.h"
32 #include "mp_msg.h"
33 #include "video_out.h"
34 #include "video_out_internal.h"
35 #include "subopt-helper.h"
36 #include "libavcodec/avcodec.h"
37 #include "fmt-conversion.h"
39 static const vo_info_t info =
41 "PNG file",
42 "png",
43 "Felix Buenemann <atmosfear@users.sourceforge.net>",
47 const LIBVO_EXTERN (png)
49 static int z_compression;
50 static int framenum;
51 static int use_alpha;
52 static AVCodecContext *avctx;
53 static uint8_t *outbuffer;
54 int outbuffer_size;
56 static int
57 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
59 if(z_compression == 0) {
60 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_PNG] Warning: compression level set to 0, compression disabled!\n");
61 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_PNG] Info: Use -vo png:z=<n> to set compression level from 0 to 9.\n");
62 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_PNG] Info: (0 = no compression, 1 = fastest, lowest - 9 best, slowest compression)\n");
65 mp_msg(MSGT_VO,MSGL_DBG2, "PNG Compression level %i\n", z_compression);
66 uninit();
67 struct AVCodec *png_codec = avcodec_find_encoder(CODEC_ID_PNG);
68 if (!png_codec)
69 goto error;
70 avctx = avcodec_alloc_context3(png_codec);
71 if (!avctx)
72 goto error;
73 avctx->width = width;
74 avctx->height = height;
75 avctx->pix_fmt = imgfmt2pixfmt(format);
76 avctx->compression_level = z_compression;
77 if (avcodec_open2(avctx, png_codec, NULL) < 0)
78 goto error;
79 return 0;
81 error:
82 uninit();
83 return -1;
87 static uint32_t draw_image(mp_image_t* mpi){
88 AVFrame pic;
89 int buffersize;
90 int res;
91 char buf[100];
92 FILE *outfile;
94 // if -dr or -slices then do nothing:
95 if(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK)) return VO_TRUE;
97 snprintf (buf, 100, "%08d.png", ++framenum);
98 outfile = fopen(buf, "wb");
99 if (!outfile) {
100 mp_msg(MSGT_VO,MSGL_WARN, "\n[VO_PNG] Error opening '%s' for writing!\n", strerror(errno));
101 return 1;
104 pic.data[0] = mpi->planes[0];
105 pic.linesize[0] = mpi->stride[0];
106 buffersize = mpi->w * mpi->h * 8;
107 if (outbuffer_size < buffersize) {
108 av_freep(&outbuffer);
109 outbuffer = av_malloc(buffersize);
110 outbuffer_size = buffersize;
112 res = avcodec_encode_video(avctx, outbuffer, outbuffer_size, &pic);
114 if(res < 0){
115 mp_msg(MSGT_VO,MSGL_WARN, "[VO_PNG] Error in create_png.\n");
116 fclose(outfile);
117 return 1;
120 fwrite(outbuffer, res, 1, outfile);
121 fclose(outfile);
123 return VO_TRUE;
126 static void draw_osd(void){}
128 static void flip_page (void){}
130 static int draw_frame(uint8_t * src[])
132 return -1;
135 static int draw_slice( uint8_t *src[],int stride[],int w,int h,int x,int y )
137 return -1;
140 static int
141 query_format(uint32_t format)
143 const int supported_flags = VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_ACCEPT_STRIDE;
144 switch(format){
145 case IMGFMT_RGB24:
146 return use_alpha ? 0 : supported_flags;
147 case IMGFMT_BGR32:
148 return use_alpha ? supported_flags : 0;
150 return 0;
153 static void uninit(void)
155 if (avctx)
156 avcodec_close(avctx);
157 av_freep(&avctx);
158 av_freep(&outbuffer);
159 outbuffer_size = 0;
162 static void check_events(void){}
164 static int int_zero_to_nine(void *value)
166 int *sh = value;
167 return *sh >= 0 && *sh <= 9;
170 static const opt_t subopts[] = {
171 {"alpha", OPT_ARG_BOOL, &use_alpha, NULL},
172 {"z", OPT_ARG_INT, &z_compression, int_zero_to_nine},
173 {NULL}
176 static int preinit(const char *arg)
178 z_compression = 0;
179 use_alpha = 0;
180 if (subopt_parse(arg, subopts) != 0) {
181 return -1;
183 return 0;
186 static int control(uint32_t request, void *data)
188 switch (request) {
189 case VOCTRL_DRAW_IMAGE:
190 return draw_image(data);
191 case VOCTRL_QUERY_FORMAT:
192 return query_format(*((uint32_t*)data));
194 return VO_NOTIMPL;