Merge svn changes up to r30529
[mplayer/kovensky.git] / libvo / vo_png.c
blobbce8d05390cec45de173762f6dabf943fab5c160
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 "help_mp.h"
34 #include "video_out.h"
35 #include "video_out_internal.h"
36 #include "subopt-helper.h"
37 #include "libavcodec/avcodec.h"
38 #include "fmt-conversion.h"
40 static const vo_info_t info =
42 "PNG file",
43 "png",
44 "Felix Buenemann <atmosfear@users.sourceforge.net>",
48 const LIBVO_EXTERN (png)
50 static int z_compression;
51 static int framenum;
52 static int use_alpha;
53 static AVCodecContext *avctx;
54 static uint8_t *outbuffer;
55 int outbuffer_size;
57 static int
58 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
60 if(z_compression == 0) {
61 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_PNG] Warning: compression level set to 0, compression disabled!\n");
62 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_PNG] Info: Use -vo png:z=<n> to set compression level from 0 to 9.\n");
63 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_PNG] Info: (0 = no compression, 1 = fastest, lowest - 9 best, slowest compression)\n");
66 mp_msg(MSGT_VO,MSGL_DBG2, "PNG Compression level %i\n", z_compression);
68 return 0;
72 static uint32_t draw_image(mp_image_t* mpi){
73 AVFrame pic;
74 int buffersize;
75 int res;
76 char buf[100];
77 FILE *outfile;
79 // if -dr or -slices then do nothing:
80 if(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK)) return VO_TRUE;
82 snprintf (buf, 100, "%08d.png", ++framenum);
83 outfile = fopen(buf, "wb");
84 if (!outfile) {
85 mp_msg(MSGT_VO,MSGL_WARN, "\n[VO_PNG] Error opening '%s' for writing!\n", strerror(errno));
86 return 1;
89 avctx->width = mpi->w;
90 avctx->height = mpi->h;
91 avctx->pix_fmt = imgfmt2pixfmt(mpi->imgfmt);
92 pic.data[0] = mpi->planes[0];
93 pic.linesize[0] = mpi->stride[0];
94 buffersize = mpi->w * mpi->h * 8;
95 if (outbuffer_size < buffersize) {
96 av_freep(&outbuffer);
97 outbuffer = av_malloc(buffersize);
98 outbuffer_size = buffersize;
100 res = avcodec_encode_video(avctx, outbuffer, outbuffer_size, &pic);
102 if(res < 0){
103 mp_msg(MSGT_VO,MSGL_WARN, "[VO_PNG] Error in create_png.\n");
104 fclose(outfile);
105 return 1;
108 fwrite(outbuffer, res, 1, outfile);
109 fclose(outfile);
111 return VO_TRUE;
114 static void draw_osd(void){}
116 static void flip_page (void){}
118 static int draw_frame(uint8_t * src[])
120 return -1;
123 static int draw_slice( uint8_t *src[],int stride[],int w,int h,int x,int y )
125 return -1;
128 static int
129 query_format(uint32_t format)
131 const int supported_flags = VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_ACCEPT_STRIDE;
132 switch(format){
133 case IMGFMT_RGB24:
134 return use_alpha ? 0 : supported_flags;
135 case IMGFMT_BGR32:
136 return use_alpha ? supported_flags : 0;
138 return 0;
141 static void uninit(void){
142 avcodec_close(avctx);
143 av_freep(&avctx);
144 av_freep(&outbuffer);
145 outbuffer_size = 0;
148 static void check_events(void){}
150 static int int_zero_to_nine(void *value)
152 int *sh = value;
153 return *sh >= 0 && *sh <= 9;
156 static const opt_t subopts[] = {
157 {"alpha", OPT_ARG_BOOL, &use_alpha, NULL},
158 {"z", OPT_ARG_INT, &z_compression, int_zero_to_nine},
159 {NULL}
162 static int preinit(const char *arg)
164 z_compression = 0;
165 use_alpha = 0;
166 if (subopt_parse(arg, subopts) != 0) {
167 return -1;
169 avcodec_register_all();
170 avctx = avcodec_alloc_context();
171 if (avcodec_open(avctx, avcodec_find_encoder(CODEC_ID_PNG)) < 0) {
172 uninit();
173 return -1;
175 avctx->compression_level = z_compression;
176 return 0;
179 static int control(uint32_t request, void *data)
181 switch (request) {
182 case VOCTRL_DRAW_IMAGE:
183 return draw_image(data);
184 case VOCTRL_QUERY_FORMAT:
185 return query_format(*((uint32_t*)data));
187 return VO_NOTIMPL;