libav: use avcodec_free_frame() with new Libav
[mplayer.git] / libvo / vo_png.c
blobea2b76307961260a86e6a24a6ecc75de10f69d9c
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 <libavcodec/avcodec.h>
31 #include <libavutil/common.h>
33 #include "config.h"
34 #include "mp_msg.h"
35 #include "mp_msg.h"
36 #include "video_out.h"
37 #include "video_out_internal.h"
38 #include "subopt-helper.h"
39 #include "fmt-conversion.h"
41 static const vo_info_t info =
43 "PNG file",
44 "png",
45 "Felix Buenemann <atmosfear@users.sourceforge.net>",
49 const LIBVO_EXTERN (png)
51 static int z_compression;
52 static int framenum;
53 static int use_alpha;
54 static AVCodecContext *avctx;
55 static uint8_t *outbuffer;
56 int outbuffer_size;
58 static int
59 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t flags, char *title, uint32_t format)
61 if(z_compression == 0) {
62 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_PNG] Warning: compression level set to 0, compression disabled!\n");
63 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_PNG] Info: Use -vo png:z=<n> to set compression level from 0 to 9.\n");
64 mp_tmsg(MSGT_VO,MSGL_INFO, "[VO_PNG] Info: (0 = no compression, 1 = fastest, lowest - 9 best, slowest compression)\n");
67 mp_msg(MSGT_VO,MSGL_DBG2, "PNG Compression level %i\n", z_compression);
68 uninit();
69 struct AVCodec *png_codec = avcodec_find_encoder(CODEC_ID_PNG);
70 if (!png_codec)
71 goto error;
72 avctx = avcodec_alloc_context3(png_codec);
73 if (!avctx)
74 goto error;
75 avctx->width = width;
76 avctx->height = height;
77 avctx->pix_fmt = imgfmt2pixfmt(format);
78 avctx->compression_level = z_compression;
79 if (avcodec_open2(avctx, png_codec, NULL) < 0)
80 goto error;
81 return 0;
83 error:
84 uninit();
85 return -1;
89 static uint32_t draw_image(mp_image_t* mpi){
90 AVFrame pic;
91 int buffersize;
92 int res;
93 char buf[100];
94 FILE *outfile;
96 // if -dr or -slices then do nothing:
97 if(mpi->flags&(MP_IMGFLAG_DIRECT|MP_IMGFLAG_DRAW_CALLBACK)) return VO_TRUE;
99 snprintf (buf, 100, "%08d.png", ++framenum);
100 outfile = fopen(buf, "wb");
101 if (!outfile) {
102 mp_msg(MSGT_VO,MSGL_WARN, "\n[VO_PNG] Error opening '%s' for writing!\n", strerror(errno));
103 return 1;
106 pic.data[0] = mpi->planes[0];
107 pic.linesize[0] = mpi->stride[0];
108 buffersize = mpi->w * mpi->h * 8;
109 if (outbuffer_size < buffersize) {
110 av_freep(&outbuffer);
111 outbuffer = av_malloc(buffersize);
112 outbuffer_size = buffersize;
114 res = avcodec_encode_video(avctx, outbuffer, outbuffer_size, &pic);
116 if(res < 0){
117 mp_msg(MSGT_VO,MSGL_WARN, "[VO_PNG] Error in create_png.\n");
118 fclose(outfile);
119 return 1;
122 fwrite(outbuffer, res, 1, outfile);
123 fclose(outfile);
125 return VO_TRUE;
128 static void draw_osd(void){}
130 static void flip_page (void){}
132 static int draw_frame(uint8_t * src[])
134 return -1;
137 static int draw_slice( uint8_t *src[],int stride[],int w,int h,int x,int y )
139 return -1;
142 static int
143 query_format(uint32_t format)
145 const int supported_flags = VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW|VFCAP_ACCEPT_STRIDE;
146 switch(format){
147 case IMGFMT_RGB24:
148 return use_alpha ? 0 : supported_flags;
149 case IMGFMT_BGR32:
150 return use_alpha ? supported_flags : 0;
152 return 0;
155 static void uninit(void)
157 if (avctx)
158 avcodec_close(avctx);
159 av_freep(&avctx);
160 av_freep(&outbuffer);
161 outbuffer_size = 0;
164 static void check_events(void){}
166 static int int_zero_to_nine(void *value)
168 int *sh = value;
169 return *sh >= 0 && *sh <= 9;
172 static const opt_t subopts[] = {
173 {"alpha", OPT_ARG_BOOL, &use_alpha, NULL},
174 {"z", OPT_ARG_INT, &z_compression, int_zero_to_nine},
175 {NULL}
178 static int preinit(const char *arg)
180 z_compression = 0;
181 use_alpha = 0;
182 if (subopt_parse(arg, subopts) != 0) {
183 return -1;
185 return 0;
188 static int control(uint32_t request, void *data)
190 switch (request) {
191 case VOCTRL_DRAW_IMAGE:
192 return draw_image(data);
193 case VOCTRL_QUERY_FORMAT:
194 return query_format(*((uint32_t*)data));
196 return VO_NOTIMPL;