configure: Fix compilation on Windows: MinGW unistd.h does not define NULL
[mplayer/glamo.git] / libmpcodecs / vd_lzo.c
blobfb926895cc0810d16453210c93e69ddfc0337c77
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
22 #include "config.h"
23 #include "mp_msg.h"
25 #include "vd_internal.h"
26 #include "libavutil/lzo.h"
28 #define MOD_NAME "DecLZO"
30 static const vd_info_t info = {
31 "LZO compressed Video",
32 "lzo",
33 "Tilmann Bitterberg",
34 "Transcode development team <http://www.theorie.physik.uni-goettingen.de/~ostreich/transcode/>",
35 "based on liblzo: http://www.oberhumer.com/opensource/lzo/"
38 LIBVD_EXTERN(lzo)
40 typedef struct {
41 uint8_t *buffer;
42 int bufsz;
43 int codec;
44 } lzo_context_t;
46 // to set/get/query special features/parameters
47 static int control (sh_video_t *sh, int cmd, void* arg, ...)
49 lzo_context_t *priv = sh->context;
50 switch(cmd){
51 case VDCTRL_QUERY_FORMAT:
52 if (*(int *)arg == priv->codec) return CONTROL_TRUE;
53 return CONTROL_FALSE;
55 return CONTROL_UNKNOWN;
59 // init driver
60 static int init(sh_video_t *sh)
62 lzo_context_t *priv;
64 if (sh->bih->biSizeImage <= 0) {
65 mp_msg (MSGT_DECVIDEO, MSGL_ERR, "[%s] Invalid frame size\n", MOD_NAME);
66 return 0;
69 priv = malloc(sizeof(lzo_context_t));
70 if (!priv)
72 mp_msg (MSGT_DECVIDEO, MSGL_ERR, "[%s] memory allocation failed\n", MOD_NAME);
73 return 0;
75 priv->bufsz = sh->bih->biSizeImage;
76 priv->buffer = malloc(priv->bufsz + AV_LZO_OUTPUT_PADDING);
77 priv->codec = -1;
78 sh->context = priv;
80 return 1;
83 // uninit driver
84 static void uninit(sh_video_t *sh)
86 lzo_context_t *priv = sh->context;
88 if (priv)
90 free(priv->buffer);
91 free(priv);
94 sh->context = NULL;
97 // decode a frame
98 static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags)
100 int r;
101 mp_image_t* mpi;
102 lzo_context_t *priv = sh->context;
103 int w = priv->bufsz;
105 if (len <= 0) {
106 return NULL; // skipped frame
109 r = av_lzo1x_decode(priv->buffer, &w, data, &len);
110 if (r) {
111 /* this should NEVER happen */
112 mp_msg (MSGT_DECVIDEO, MSGL_ERR,
113 "[%s] internal error - decompression failed: %d\n", MOD_NAME, r);
114 return NULL;
117 if (priv->codec == -1) {
118 // detect RGB24 vs. YV12 via decoded size
119 mp_msg (MSGT_DECVIDEO, MSGL_V, "[%s] 2 depth %d, format %d data %p len (%d) (%d)\n",
120 MOD_NAME, sh->bih->biBitCount, sh->format, data, len, sh->bih->biSizeImage
123 if (w == 0) {
124 priv->codec = IMGFMT_BGR24;
125 mp_msg (MSGT_DECVIDEO, MSGL_V, "[%s] codec chosen is BGR24\n", MOD_NAME);
126 } else if (w == (sh->bih->biSizeImage)/2) {
127 priv->codec = IMGFMT_YV12;
128 mp_msg (MSGT_DECVIDEO, MSGL_V, "[%s] codec chosen is YV12\n", MOD_NAME);
129 } else {
130 priv->codec = -1;
131 mp_msg(MSGT_DECVIDEO,MSGL_ERR,"[%s] Unsupported out_fmt\n", MOD_NAME);
132 return NULL;
135 if(!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,priv->codec)) {
136 priv->codec = -1;
137 return NULL;
141 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0,
142 sh->disp_w, sh->disp_h);
145 if (!mpi) {
146 mp_msg (MSGT_DECVIDEO, MSGL_ERR, "[%s] mpcodecs_get_image failed\n", MOD_NAME);
147 return NULL;
150 mpi->planes[0] = priv->buffer;
151 if (priv->codec == IMGFMT_BGR24)
152 mpi->stride[0] = 3 * sh->disp_w;
153 else {
154 mpi->stride[0] = sh->disp_w;
155 mpi->planes[2] = priv->buffer + sh->disp_w*sh->disp_h;
156 mpi->stride[2] = sh->disp_w / 2;
157 mpi->planes[1] = priv->buffer + sh->disp_w*sh->disp_h*5/4;
158 mpi->stride[1] = sh->disp_w / 2;
161 mp_msg (MSGT_DECVIDEO, MSGL_DBG2,
162 "[%s] decompressed %lu bytes into %lu bytes\n", MOD_NAME,
163 (long) len, (long)w);
165 return mpi;