Fix type of zlen, fixes crashes on 64 bit systems.
[mplayer/glamo.git] / libmpcodecs / ve_nuv.c
blob1a8e5c0c42c78498f4cab2e9739d7c16d212a21b
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "config.h"
6 #include "mp_msg.h"
8 #include "m_option.h"
10 #include "codec-cfg.h"
11 #include "stream/stream.h"
12 #include "libmpdemux/demuxer.h"
13 #include "libmpdemux/stheader.h"
15 #include "stream/stream.h"
16 #include "libmpdemux/muxer.h"
18 #include "img_format.h"
19 #include "mp_image.h"
20 #include "vf.h"
22 #include "libmpdemux/nuppelvideo.h"
23 #include <lzo/lzo1x.h>
24 #include "native/rtjpegn.h"
26 #define LZO_AL(size) (((size) + (sizeof(long) - 1)) / sizeof(long))
27 #define LZO_OUT_LEN(in) ((in) + (in) / 64 + 16 + 3)
29 //===========================================================================//
31 struct vf_priv_s {
32 int raw; // Do not use RTjpeg
33 int lzo; // Use lzo
34 unsigned int l,c,q; // Mjpeg param
35 muxer_stream_t* mux;
36 uint8_t* buffer;
38 int buf_size;
39 int tbl_wrote;
40 lzo_byte *zbuffer;
41 long __LZO_MMODEL *zmem;
43 #define mux_v (vf->priv->mux)
45 struct vf_priv_s nuv_priv_dflt = {
46 0, // raw
47 1, // lzo
48 1,1, // l,c
49 255, // q
50 NULL,
51 NULL,
52 0,0,
53 NULL,NULL
56 m_option_t nuvopts_conf[]={
57 {"raw", &nuv_priv_dflt.raw, CONF_TYPE_FLAG, 0, 0, 1, NULL},
58 {"rtjpeg", &nuv_priv_dflt.raw, CONF_TYPE_FLAG, 0, 1, 0, NULL},
59 {"lzo", &nuv_priv_dflt.lzo, CONF_TYPE_FLAG, 0, 0, 1, NULL},
60 {"nolzo", &nuv_priv_dflt.lzo, CONF_TYPE_FLAG, 0, 1, 0, NULL},
61 {"q", &nuv_priv_dflt.q, CONF_TYPE_INT, M_OPT_RANGE,3,255, NULL},
62 {"l", &nuv_priv_dflt.l, CONF_TYPE_INT, M_OPT_RANGE,0,20, NULL},
63 {"c", &nuv_priv_dflt.c, CONF_TYPE_INT, M_OPT_RANGE,0,20, NULL},
64 {NULL, NULL, 0, 0, 0, 0, NULL}
65 };
67 //===========================================================================//
70 #define COMPDATASIZE (128*4)
72 static int config(struct vf_instance_s* vf,
73 int width, int height, int d_width, int d_height,
74 unsigned int flags, unsigned int outfmt){
76 // We need a buffer wich can holda header and a whole YV12 picture
77 // or a RTJpeg table
78 vf->priv->buf_size = width*height*3/2+FRAMEHEADERSIZE;
79 if(vf->priv->buf_size < COMPDATASIZE + FRAMEHEADERSIZE)
80 vf->priv->buf_size = COMPDATASIZE + FRAMEHEADERSIZE;
82 mux_v->bih->biWidth=width;
83 mux_v->bih->biHeight=height;
84 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
85 mux_v->aspect = (float)d_width/d_height;
86 vf->priv->buffer = realloc(vf->priv->buffer,vf->priv->buf_size);
87 if (vf->priv->lzo)
88 vf->priv->zbuffer = realloc(vf->priv->zbuffer, FRAMEHEADERSIZE + LZO_OUT_LEN(vf->priv->buf_size));
89 vf->priv->tbl_wrote = 0;
91 return 1;
94 static int control(struct vf_instance_s* vf, int request, void* data){
96 return CONTROL_UNKNOWN;
99 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
100 if(fmt==IMGFMT_I420) return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
101 return 0;
104 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
105 struct rtframeheader* ench = (struct rtframeheader*)vf->priv->buffer;
106 uint8_t* data = vf->priv->buffer + FRAMEHEADERSIZE;
107 uint8_t* zdata = vf->priv->zbuffer + FRAMEHEADERSIZE;
108 int len = 0, r;
109 size_t zlen = 0;
111 memset(vf->priv->buffer,0,FRAMEHEADERSIZE); // Reset the header
112 if(vf->priv->lzo)
113 memset(vf->priv->zbuffer,0,FRAMEHEADERSIZE);
115 // This has to be don here otherwise tv with sound doesn't work
116 if(!vf->priv->tbl_wrote) {
117 RTjpeg_init_compress((uint32_t *)data,mpi->width,mpi->height,vf->priv->q);
118 RTjpeg_init_mcompress();
120 ench->frametype = 'D'; // compressor data
121 ench->comptype = 'R'; // compressor data for RTjpeg
122 ench->packetlength = COMPDATASIZE;
124 le2me_rtframeheader(ench);
125 mux_v->buffer=vf->priv->buffer;
126 muxer_write_chunk(mux_v,FRAMEHEADERSIZE + COMPDATASIZE, 0x10, MP_NOPTS_VALUE, MP_NOPTS_VALUE);
127 vf->priv->tbl_wrote = 1;
128 memset(ench,0,FRAMEHEADERSIZE); // Reset the header
131 // Raw picture
132 if(vf->priv->raw) {
133 len = mpi->width*mpi->height*3/2;
134 // Try lzo ???
135 if(vf->priv->lzo) {
136 r = lzo1x_1_compress(mpi->planes[0],len,
137 zdata,&zlen,vf->priv->zmem);
138 if(r != LZO_E_OK) {
139 mp_msg(MSGT_VFILTER,MSGL_ERR,"LZO compress error\n");
140 zlen = 0;
144 if(zlen <= 0 || zlen > len) {
145 memcpy(data,mpi->planes[0],len);
146 ench->comptype = '0';
147 } else { // Use lzo only if it's littler
148 ench = (struct rtframeheader*)vf->priv->zbuffer;
149 ench->comptype = '3';
150 len = zlen;
153 } else { // RTjpeg compression
154 len = RTjpeg_mcompressYUV420(data,mpi->planes[0],vf->priv->l,
155 vf->priv->c);
156 if(len <= 0) {
157 mp_msg(MSGT_VFILTER,MSGL_ERR,"RTjpeg_mcompressYUV420 error (%d)\n",len);
158 return 0;
161 if(vf->priv->lzo) {
162 r = lzo1x_1_compress(data,len,zdata,&zlen,vf->priv->zmem);
163 if(r != LZO_E_OK) {
164 mp_msg(MSGT_VFILTER,MSGL_ERR,"LZO compress error\n");
165 zlen = 0;
169 if(zlen <= 0 || zlen > len)
170 ench->comptype = '1';
171 else {
172 ench = (struct rtframeheader*)vf->priv->zbuffer;
173 ench->comptype = '2';
174 len = zlen;
179 ench->frametype = 'V'; // video frame
180 ench->packetlength = len;
181 le2me_rtframeheader(ench);
182 mux_v->buffer=(void*)ench;
183 muxer_write_chunk(mux_v, len + FRAMEHEADERSIZE, 0x10, pts, pts);
184 return 1;
187 static void uninit(struct vf_instance_s* vf) {
189 if(vf->priv->buffer)
190 free(vf->priv->buffer);
191 if(vf->priv->zbuffer)
192 free(vf->priv->zbuffer);
193 if(vf->priv->zmem)
194 free(vf->priv->zmem);
198 //===========================================================================//
200 static int vf_open(vf_instance_t *vf, char* args){
201 vf->config=config;
202 vf->default_caps=VFCAP_CONSTANT;
203 vf->control=control;
204 vf->query_format=query_format;
205 vf->put_image=put_image;
206 vf->uninit = uninit;
207 vf->priv=malloc(sizeof(struct vf_priv_s));
208 memcpy(vf->priv, &nuv_priv_dflt,sizeof(struct vf_priv_s));
209 vf->priv->mux=(muxer_stream_t*)args;
211 mux_v->bih=calloc(1, sizeof(BITMAPINFOHEADER));
212 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
213 mux_v->bih->biWidth=0;
214 mux_v->bih->biHeight=0;
215 mux_v->bih->biPlanes=1;
216 mux_v->bih->biBitCount=12;
217 mux_v->bih->biCompression = mmioFOURCC('N','U','V','1');
219 if(vf->priv->lzo) {
220 if(lzo_init() != LZO_E_OK) {
221 mp_msg(MSGT_VFILTER,MSGL_WARN,"LZO init failed: no lzo compression\n");
222 vf->priv->lzo = 0;
223 } else
224 vf->priv->zmem = malloc(sizeof(long)*LZO_AL(LZO1X_1_MEM_COMPRESS));
227 return 1;
230 vf_info_t ve_info_nuv = {
231 "nuv encoder",
232 "nuv",
233 "Albeu",
234 "for internal use by mencoder",
235 vf_open
238 //===========================================================================//