]> git.sesse.net Git - vlc/blob - modules/codec/cdg.c
codecs & packetizers: fix warnings
[vlc] / modules / codec / cdg.c
1 /*****************************************************************************
2  * cdg.c: CDG decoder module
3  *****************************************************************************
4  * Copyright (C) 2007 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir # via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc/vlc.h>
32 #include <vlc_codec.h>
33 #include <vlc_vout.h>
34
35 /*****************************************************************************
36  * decoder_sys_t : decoder descriptor
37  *****************************************************************************/
38
39 /* The screen size */
40 #define CDG_SCREEN_WIDTH (300)
41 #define CDG_SCREEN_HEIGHT (216)
42
43 /* The border of the screen size */
44 #define CDG_SCREEN_BORDER_WIDTH (6)
45 #define CDG_SCREEN_BORDER_HEIGHT (12)
46
47 /* The display part */
48 #define CDG_DISPLAY_WIDTH  (CDG_SCREEN_WIDTH-2*CDG_SCREEN_BORDER_WIDTH)
49 #define CDG_DISPLAY_HEIGHT (CDG_SCREEN_HEIGHT-2*CDG_SCREEN_BORDER_HEIGHT)
50
51 #define CDG_SCREEN_PITCH (CDG_SCREEN_WIDTH)
52
53 struct decoder_sys_t
54 {
55     uint8_t color[16][3];
56     int     i_offseth;
57     int     i_offsetv;
58     uint8_t screen[CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT];
59     uint8_t *p_screen;
60 };
61
62 #define CDG_PACKET_SIZE (24)
63
64 #define CDG_COLOR_R_SHIFT ( 0)
65 #define CDG_COLOR_G_SHIFT ( 8)
66 #define CDG_COLOR_B_SHIFT (16)
67
68 /*****************************************************************************
69  * Local prototypes
70  *****************************************************************************/
71 static int  Open ( vlc_object_t * );
72 static void Close( vlc_object_t * );
73
74 static picture_t *Decode( decoder_t *, block_t ** );
75
76 static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer );
77 static int Render( decoder_sys_t *p_cdg, picture_t *p_picture );
78
79 /*****************************************************************************
80  * Module descriptor
81  *****************************************************************************/
82 vlc_module_begin();
83     set_category( CAT_INPUT );
84     set_subcategory( SUBCAT_INPUT_VCODEC );
85     set_description( _("CDG video decoder") );
86     set_capability( "decoder", 1000 );
87     set_callbacks( Open, Close );
88     add_shortcut( "cdg" );
89 vlc_module_end();
90
91 /*****************************************************************************
92  * Open: probe the decoder and return score
93  *****************************************************************************/
94 static int Open( vlc_object_t *p_this )
95 {
96     decoder_t *p_dec = (decoder_t*)p_this;
97     decoder_sys_t *p_sys;
98
99     if( p_dec->fmt_in.i_codec != VLC_FOURCC('C','D','G',' ') )
100         return VLC_EGENERIC;
101
102     /* Allocate the memory needed to store the decoder's structure */
103     p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t));
104     if( !p_sys )
105     {
106         msg_Err( p_dec, "out of memory" );
107         return VLC_EGENERIC;
108     }
109
110     /* Init */
111     memset( p_sys, 0, sizeof(*p_sys) );
112     p_sys->i_offseth = 0;
113     p_sys->i_offsetv = 0;
114     p_sys->p_screen = p_sys->screen;
115
116     /* Set output properties
117      * TODO maybe it would be better to use RV16 or RV24 ? */
118     p_dec->fmt_out.i_cat = VIDEO_ES;
119     p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
120     p_dec->fmt_out.video.i_width = CDG_DISPLAY_WIDTH;
121     p_dec->fmt_out.video.i_height = CDG_DISPLAY_HEIGHT;
122     p_dec->fmt_out.video.i_aspect =
123         VOUT_ASPECT_FACTOR * p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
124     p_dec->fmt_out.video.i_rmask = 0xff << CDG_COLOR_R_SHIFT;
125     p_dec->fmt_out.video.i_gmask = 0xff << CDG_COLOR_G_SHIFT;
126     p_dec->fmt_out.video.i_bmask = 0xff << CDG_COLOR_B_SHIFT;
127
128     /* Set callbacks */
129     p_dec->pf_decode_video = Decode;
130
131     return VLC_SUCCESS;
132 }
133
134 /****************************************************************************
135  * Decode: the whole thing
136  ****************************************************************************
137  * This function must be fed with a complete compressed frame.
138  ****************************************************************************/
139 static picture_t *Decode( decoder_t *p_dec, block_t **pp_block )
140 {
141     decoder_sys_t *p_sys = p_dec->p_sys;
142     block_t *p_block;
143     picture_t *p_pic = NULL;
144
145     if( !pp_block || !*pp_block )
146         return NULL;
147     p_block = *pp_block;
148
149     /* Decode packet */
150     while( p_block->i_buffer >= CDG_PACKET_SIZE )
151     {
152         DecodePacket( p_sys, p_block->p_buffer, CDG_PACKET_SIZE );
153         p_block->i_buffer -= CDG_PACKET_SIZE;
154         p_block->p_buffer += CDG_PACKET_SIZE;
155     }
156
157     /* Get a new picture */
158     p_pic = p_dec->pf_vout_buffer_new( p_dec );
159     if( !p_pic )
160         goto error;
161
162     Render( p_sys, p_pic );
163     p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
164
165     block_Release( p_block ); *pp_block = NULL;
166     return p_pic;
167
168 error:
169     block_Release( p_block ); *pp_block = NULL;
170     return NULL;
171 }
172
173 /*****************************************************************************
174  * Close: decoder destruction
175  *****************************************************************************/
176 static void Close( vlc_object_t *p_this )
177 {
178     decoder_t *p_dec = (decoder_t *)p_this;
179     decoder_sys_t *p_sys = p_dec->p_sys;
180
181     free( p_sys );
182 }
183
184 /*****************************************************************************
185  * Decoder
186  *****************************************************************************/
187 static void ScreenFill( decoder_sys_t *p_cdg, int sx, int sy, int dx, int dy, int c )
188 {
189     int x, y;
190     for( y = sy; y < sy+dy; y++ )
191         for( x = sx; x < sx+dx; x++ )
192             p_cdg->p_screen[y*CDG_SCREEN_PITCH+x] = c;
193 }
194 static int DecodeMemoryPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
195 {
196     const int i_color = p_data[0]&0x0f;
197 #if 0
198     const int i_repeat= p_data[1]&0x0f;
199 #endif
200     /* if i_repeat > 0 we could ignore it if we have a reliable stream */
201     ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, i_color );
202     return 0;
203 }
204 static int DecodeBorderPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
205 {
206     const int i_color = p_data[0]&0x0f;
207
208     /* */
209     ScreenFill( p_cdg,   0,   0,
210                 CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
211     ScreenFill( p_cdg,   0, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT,
212                 CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
213     ScreenFill( p_cdg,   0, CDG_SCREEN_BORDER_HEIGHT,
214                 CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
215     ScreenFill( p_cdg,   CDG_SCREEN_WIDTH-CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_BORDER_HEIGHT,
216                 CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
217     return 0;
218 }
219
220 static int DecodeLoadColorTable( decoder_sys_t *p_cdg, const uint8_t *p_data, int i_base )
221 {
222     int n;
223     for( n = 0; n < 8; n++ )
224     {
225         const int c = ((p_data[2*n+0] << 8) | p_data[2*n+1]);
226         const int r = (c >> 10)&0x0f;
227         const int g = ((c >> 6)&0x0c) | ((c >> 4)&0x03);
228         const int b = c &0x0f;
229
230         p_cdg->color[i_base+n][0] = r << 4;
231         p_cdg->color[i_base+n][1] = g << 4;
232         p_cdg->color[i_base+n][2] = b << 4;
233     }
234     return 0;
235 }
236 static int DecodeTileBlock( decoder_sys_t *p_cdg, const uint8_t *p_data, int doXor )
237 {
238     int p_color[2];
239     int sx, sy;
240     int x, y;
241
242     p_color[0] = p_data[0] & 0x0f;
243     p_color[1] = p_data[1] & 0x0f;
244
245     sy = (p_data[2] & 0x1f)*12;
246     sx = (p_data[3] & 0x3f)*6;
247
248     for( y = 0; y < 12; y++ )
249     {
250         for( x = 0; x < 6; x++ )
251         {
252             const int idx = ( p_data[4+y] >> (5-x) ) & 0x01;
253             uint8_t *p = &p_cdg->p_screen[(sy+y)*CDG_SCREEN_PITCH+(sx+x)];
254             if( doXor )
255                 *p ^= p_color[idx];
256             else
257                 *p = p_color[idx];
258         }
259     }
260     return 0;
261 }
262
263 static int DecodeScroll( decoder_sys_t *p_cdg, const uint8_t *p_data, int b_copy )
264 {
265     uint8_t copy[CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT];
266
267     uint8_t color = p_data[0]&0x0f;
268     int i_shifth;
269     int i_shiftv;
270     int x, y;
271
272     /* */
273     p_cdg->i_offseth = p_data[1]&0x7;
274     if( p_cdg->i_offseth >= CDG_SCREEN_BORDER_WIDTH )
275         p_cdg->i_offseth = CDG_SCREEN_BORDER_WIDTH-1;
276
277     p_cdg->i_offsetv = p_data[2]&0xf;
278     if( p_cdg->i_offsetv >= CDG_SCREEN_BORDER_HEIGHT )
279         p_cdg->i_offsetv = CDG_SCREEN_BORDER_HEIGHT-1;
280
281     /* */
282     switch( (p_data[1] >> 4)&0x3 )
283     {
284     case 0x01: i_shifth =  6; break;
285     case 0x02: i_shifth = -6; break;
286     default:
287         i_shifth = 0;
288         break;
289     }
290     switch( (p_data[2] >> 4)&0x3 )
291     {
292     case 0x01: i_shiftv = 12; break;
293     case 0x02: i_shiftv =-12; break;
294     default:
295         i_shiftv = 0;
296         break;
297     }
298
299     if( i_shifth == 0 && i_shiftv == 0 )
300         return 0;
301
302     /* Make a copy of the screen */
303     memcpy( copy, p_cdg->screen, sizeof(p_cdg->screen) );
304
305     /* Fill the uncovered part XXX way too much */
306     ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, color );
307
308     /* Copy back */
309     for( y = 0; y < CDG_SCREEN_HEIGHT; y++ )
310     {
311         int dy = i_shiftv + y;
312         for( x = 0; x < CDG_SCREEN_WIDTH; x++ )
313         {
314             int dx = i_shifth + x;
315
316             if( b_copy )
317             {
318                 dy = ( dy + CDG_SCREEN_HEIGHT ) % CDG_SCREEN_HEIGHT;
319                 dy = ( dy + CDG_SCREEN_WIDTH  ) % CDG_SCREEN_WIDTH;
320             }
321             else
322             {
323                 if( dy < 0 || dy >= CDG_SCREEN_HEIGHT ||
324                     dx < 0 || dx >= CDG_SCREEN_WIDTH )
325                     continue;
326             }
327             p_cdg->screen[dy*CDG_SCREEN_PITCH+dx] = copy[y*CDG_SCREEN_PITCH+x];
328         }
329     }
330     /* */
331     //CdgDebug( CDG_LOG_WARNING, "DecodeScroll: color=%d ch=%d oh=%d cv=%d ov=%d\n copy=%d\n", color, i_shifth, p_cdg->i_offseth, i_shiftv, p_cdg->i_offsetv, b_copy );
332     return 0;
333 }
334
335 static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer )
336 {
337     const int i_cmd = p_buffer[0] & 0x3f;
338     const int i_instruction = p_buffer[1] & 0x3f;
339     const uint8_t *p_data = &p_buffer[4];
340
341     if( i_buffer != CDG_PACKET_SIZE )
342         return -1;
343
344     /* Handle CDG command only */
345     if( i_cmd != 0x09 )
346         return 0;
347
348     switch( i_instruction )
349     {
350         case 1:
351             DecodeMemoryPreset( p_cdg, p_data );
352             break;
353         case 2:
354             DecodeBorderPreset( p_cdg, p_data );
355             break;
356         case 6:
357             DecodeTileBlock( p_cdg, p_data, 0 );
358             break;
359         case 20:
360             DecodeScroll( p_cdg, p_data, 0 );
361             break;
362         case 24:
363             DecodeScroll( p_cdg, p_data, 1 );
364             break;
365         case 28:
366             /* FIXME what to do about that one ? */
367             //CdgDebug( CDG_LOG_WARNING, "DecodeDefineTransparentColor not implemented\n" );
368             //DecodeDefineTransparentColor( p_cdg, p_data );
369             break;
370         case 30:
371             DecodeLoadColorTable( p_cdg, p_data, 0 );
372             break;
373         case 31:
374             DecodeLoadColorTable( p_cdg, p_data, 8 );
375             break;
376         case 38:
377             DecodeTileBlock( p_cdg, p_data, 1 );
378             break;
379         default:
380             break;
381     }
382     return 0;
383 }
384
385 static void RenderPixel( picture_t *p_picture, int x, int y, uint32_t c )
386 {
387     const int i_plane = 0;
388     const int i_pitch = p_picture->p[i_plane].i_pitch;
389     uint32_t *s = (uint32_t*)&p_picture->p[i_plane].p_pixels[y*i_pitch + x*sizeof(uint32_t)];
390     *s = c;
391 }
392 static uint32_t RenderRGB( int r, int g, int b )
393 {
394     return ( r << CDG_COLOR_R_SHIFT ) | ( g << CDG_COLOR_G_SHIFT ) | ( b << CDG_COLOR_B_SHIFT );
395 }
396
397 static int Render( decoder_sys_t *p_cdg, picture_t *p_picture )
398 {
399     int x, y;
400
401     for( y = 0; y < CDG_DISPLAY_HEIGHT; y++ )
402     {
403         for( x = 0; x < CDG_DISPLAY_WIDTH; x++ )
404         {
405             const int sx = x + p_cdg->i_offseth + CDG_SCREEN_BORDER_WIDTH;
406             const int sy = y + p_cdg->i_offsetv + CDG_SCREEN_BORDER_HEIGHT;
407             uint8_t cidx = p_cdg->p_screen[sy*CDG_SCREEN_PITCH +sx];
408             uint8_t r = p_cdg->color[cidx][0];
409             uint8_t g = p_cdg->color[cidx][1];
410             uint8_t b = p_cdg->color[cidx][2];
411
412             RenderPixel( p_picture, x, y, RenderRGB( r, g, b ) );
413         }
414     }
415     return 0;
416 }
417