]> git.sesse.net Git - vlc/blob - modules/codec/cdg.c
cdg: clean up preprocessor constants
[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_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
34
35 /*****************************************************************************
36  * decoder_sys_t : decoder descriptor
37  *****************************************************************************/
38
39 /* The screen size */
40 #define CDG_SCREEN_WIDTH 300u
41 #define CDG_SCREEN_HEIGHT 216u
42
43 /* The border of the screen size */
44 #define CDG_SCREEN_BORDER_WIDTH 6u
45 #define CDG_SCREEN_BORDER_HEIGHT 12u
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     int     i_packet;
62 };
63
64 #define CDG_PACKET_SIZE 24u
65
66 #define CDG_COLOR_R_SHIFT  0
67 #define CDG_COLOR_G_SHIFT  8
68 #define CDG_COLOR_B_SHIFT 16
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static int  Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
75
76 static picture_t *Decode( decoder_t *, block_t ** );
77
78 static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer );
79 static int Render( decoder_sys_t *p_cdg, picture_t *p_picture );
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 vlc_module_begin ()
85     set_category( CAT_INPUT )
86     set_subcategory( SUBCAT_INPUT_VCODEC )
87     set_description( N_("CDG video decoder") )
88     set_capability( "decoder", 1000 )
89     set_callbacks( Open, Close )
90     add_shortcut( "cdg" )
91 vlc_module_end ()
92
93 /*****************************************************************************
94  * Open: probe the decoder and return score
95  *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
97 {
98     decoder_t *p_dec = (decoder_t*)p_this;
99     decoder_sys_t *p_sys;
100
101     if( p_dec->fmt_in.i_codec != VLC_CODEC_CDG )
102         return VLC_EGENERIC;
103
104     /* Allocate the memory needed to store the decoder's structure */
105     p_dec->p_sys = p_sys = calloc( 1, sizeof(decoder_sys_t) );
106     if( !p_sys )
107         return VLC_ENOMEM;
108
109     /* Init */
110     p_sys->p_screen = p_sys->screen;
111     p_sys->i_packet = 0;
112
113     /* Set output properties
114      * TODO maybe it would be better to use RV16 or RV24 ? */
115     p_dec->fmt_out.i_cat = VIDEO_ES;
116     p_dec->fmt_out.i_codec = VLC_CODEC_RGB32;
117     p_dec->fmt_out.video.i_width = CDG_DISPLAY_WIDTH;
118     p_dec->fmt_out.video.i_height = CDG_DISPLAY_HEIGHT;
119     p_dec->fmt_out.video.i_sar_num = 1;
120     p_dec->fmt_out.video.i_sar_den = 1;
121     p_dec->fmt_out.video.i_rmask = 0xff << CDG_COLOR_R_SHIFT;
122     p_dec->fmt_out.video.i_gmask = 0xff << CDG_COLOR_G_SHIFT;
123     p_dec->fmt_out.video.i_bmask = 0xff << CDG_COLOR_B_SHIFT;
124
125     /* Set callbacks */
126     p_dec->pf_decode_video = Decode;
127
128     return VLC_SUCCESS;
129 }
130
131 /****************************************************************************
132  * Decode: the whole thing
133  ****************************************************************************
134  * This function must be fed with a complete compressed frame.
135  ****************************************************************************/
136 static picture_t *Decode( decoder_t *p_dec, block_t **pp_block )
137 {
138     decoder_sys_t *p_sys = p_dec->p_sys;
139     block_t *p_block;
140     picture_t *p_pic = NULL;
141
142     if( !pp_block || !*pp_block )
143         return NULL;
144     p_block = *pp_block;
145
146     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
147     {
148         p_sys->i_packet = 0;
149         goto exit;
150     }
151
152     /* Decode packet */
153     while( p_block->i_buffer >= CDG_PACKET_SIZE )
154     {
155         DecodePacket( p_sys, p_block->p_buffer, CDG_PACKET_SIZE );
156         p_block->i_buffer -= CDG_PACKET_SIZE;
157         p_block->p_buffer += CDG_PACKET_SIZE;
158     }
159
160     /* Only display 25 frame per second (there is 75 packets per second) */
161     if( (p_sys->i_packet%3) == 1 )
162     {
163         /* Get a new picture */
164         p_pic = decoder_NewPicture( p_dec );
165         if( !p_pic )
166             goto exit;
167
168         Render( p_sys, p_pic );
169         p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
170     }
171
172 exit:
173     block_Release( p_block ); *pp_block = NULL;
174     return p_pic;
175 }
176
177 /*****************************************************************************
178  * Close: decoder destruction
179  *****************************************************************************/
180 static void Close( vlc_object_t *p_this )
181 {
182     decoder_t *p_dec = (decoder_t *)p_this;
183     decoder_sys_t *p_sys = p_dec->p_sys;
184
185     free( p_sys );
186 }
187
188 /*****************************************************************************
189  * Decoder
190  *****************************************************************************/
191 static void ScreenFill( decoder_sys_t *p_cdg, int sx, int sy, int dx, int dy, int c )
192 {
193     int x, y;
194     for( y = sy; y < sy+dy; y++ )
195         for( x = sx; x < sx+dx; x++ )
196             p_cdg->p_screen[y*CDG_SCREEN_PITCH+x] = c;
197 }
198 static int DecodeMemoryPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
199 {
200     const int i_color = p_data[0]&0x0f;
201 #if 0
202     const int i_repeat= p_data[1]&0x0f;
203 #endif
204     /* if i_repeat > 0 we could ignore it if we have a reliable stream */
205     ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, i_color );
206     return 0;
207 }
208 static int DecodeBorderPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
209 {
210     const int i_color = p_data[0]&0x0f;
211
212     /* */
213     ScreenFill( p_cdg,   0,   0,
214                 CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
215     ScreenFill( p_cdg,   0, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT,
216                 CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
217     ScreenFill( p_cdg,   0, CDG_SCREEN_BORDER_HEIGHT,
218                 CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
219     ScreenFill( p_cdg,   CDG_SCREEN_WIDTH-CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_BORDER_HEIGHT,
220                 CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
221     return 0;
222 }
223
224 static int DecodeLoadColorTable( decoder_sys_t *p_cdg, const uint8_t *p_data, int i_base )
225 {
226     int n;
227     for( n = 0; n < 8; n++ )
228     {
229         const int c = ((p_data[2*n+0] << 8) | p_data[2*n+1]);
230         const int r = (c >> 10)&0x0f;
231         const int g = ((c >> 6)&0x0c) | ((c >> 4)&0x03);
232         const int b = c &0x0f;
233
234         p_cdg->color[i_base+n][0] = r << 4;
235         p_cdg->color[i_base+n][1] = g << 4;
236         p_cdg->color[i_base+n][2] = b << 4;
237     }
238     return 0;
239 }
240 static int DecodeTileBlock( decoder_sys_t *p_cdg, const uint8_t *p_data, int doXor )
241 {
242     int p_color[2];
243     int sx, sy;
244     int x, y;
245
246     p_color[0] = p_data[0] & 0x0f;
247     p_color[1] = p_data[1] & 0x0f;
248
249     sy = (p_data[2] & 0x1f)*12;
250     sx = (p_data[3] & 0x3f)*6;
251
252     for( y = 0; y < 12; y++ )
253     {
254         for( x = 0; x < 6; x++ )
255         {
256             const int idx = ( p_data[4+y] >> (5-x) ) & 0x01;
257
258             int index = (sy+y)*CDG_SCREEN_PITCH+(sx+x);
259             if( index >= CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT )
260                 return 0;
261
262             uint8_t *p = &p_cdg->p_screen[index];
263
264             if( doXor )
265                 *p ^= p_color[idx];
266             else
267                 *p = p_color[idx];
268         }
269     }
270     return 0;
271 }
272
273 static int DecodeScroll( decoder_sys_t *p_cdg, const uint8_t *p_data, int b_copy )
274 {
275     uint8_t copy[CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT];
276
277     uint8_t color = p_data[0]&0x0f;
278     int i_shifth;
279     int i_shiftv;
280     int x, y;
281
282     /* */
283     p_cdg->i_offseth = p_data[1]&0x7;
284     if( p_cdg->i_offseth >= CDG_SCREEN_BORDER_WIDTH )
285         p_cdg->i_offseth = CDG_SCREEN_BORDER_WIDTH-1;
286
287     p_cdg->i_offsetv = p_data[2]&0xf;
288     if( p_cdg->i_offsetv >= CDG_SCREEN_BORDER_HEIGHT )
289         p_cdg->i_offsetv = CDG_SCREEN_BORDER_HEIGHT-1;
290
291     /* */
292     switch( (p_data[1] >> 4)&0x3 )
293     {
294     case 0x01: i_shifth =  6; break;
295     case 0x02: i_shifth = -6; break;
296     default:
297         i_shifth = 0;
298         break;
299     }
300     switch( (p_data[2] >> 4)&0x3 )
301     {
302     case 0x01: i_shiftv = 12; break;
303     case 0x02: i_shiftv =-12; break;
304     default:
305         i_shiftv = 0;
306         break;
307     }
308
309     if( i_shifth == 0 && i_shiftv == 0 )
310         return 0;
311
312     /* Make a copy of the screen */
313     memcpy( copy, p_cdg->screen, sizeof(p_cdg->screen) );
314
315     /* Fill the uncovered part XXX way too much */
316     ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, color );
317
318     /* Copy back */
319     for( y = 0; y < CDG_SCREEN_HEIGHT; y++ )
320     {
321         int dy = i_shiftv + y;
322         for( x = 0; x < CDG_SCREEN_WIDTH; x++ )
323         {
324             int dx = i_shifth + x;
325
326             if( b_copy )
327             {
328                 dy %= CDG_SCREEN_HEIGHT;
329                 dx %= CDG_SCREEN_WIDTH;
330             }
331             else
332             {
333                 if( dy < 0 || dy >= CDG_SCREEN_HEIGHT ||
334                     dx < 0 || dx >= CDG_SCREEN_WIDTH )
335                     continue;
336             }
337             p_cdg->screen[dy*CDG_SCREEN_PITCH+dx] = copy[y*CDG_SCREEN_PITCH+x];
338         }
339     }
340     /* */
341     //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 );
342     return 0;
343 }
344
345 static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer )
346 {
347     const int i_cmd = p_buffer[0] & 0x3f;
348     const int i_instruction = p_buffer[1] & 0x3f;
349     const uint8_t *p_data = &p_buffer[4];
350
351     if( i_buffer != CDG_PACKET_SIZE )
352         return -1;
353
354     p_cdg->i_packet++;
355
356     /* Handle CDG command only */
357     if( i_cmd != 0x09 )
358         return 0;
359
360     switch( i_instruction )
361     {
362         case 1:
363             DecodeMemoryPreset( p_cdg, p_data );
364             break;
365         case 2:
366             DecodeBorderPreset( p_cdg, p_data );
367             break;
368         case 6:
369             DecodeTileBlock( p_cdg, p_data, 0 );
370             break;
371         case 20:
372             DecodeScroll( p_cdg, p_data, 0 );
373             break;
374         case 24:
375             DecodeScroll( p_cdg, p_data, 1 );
376             break;
377         case 28:
378             /* FIXME what to do about that one ? */
379             //CdgDebug( CDG_LOG_WARNING, "DecodeDefineTransparentColor not implemented\n" );
380             //DecodeDefineTransparentColor( p_cdg, p_data );
381             break;
382         case 30:
383             DecodeLoadColorTable( p_cdg, p_data, 0 );
384             break;
385         case 31:
386             DecodeLoadColorTable( p_cdg, p_data, 8 );
387             break;
388         case 38:
389             DecodeTileBlock( p_cdg, p_data, 1 );
390             break;
391         default:
392             break;
393     }
394     return 0;
395 }
396
397 static void RenderPixel( picture_t *p_picture, int x, int y, uint32_t c )
398 {
399     const int i_plane = 0;
400     const int i_pitch = p_picture->p[i_plane].i_pitch;
401     uint32_t *s = (uint32_t*)&p_picture->p[i_plane].p_pixels[y*i_pitch + x*sizeof(uint32_t)];
402     *s = c;
403 }
404 static uint32_t RenderRGB( int r, int g, int b )
405 {
406     return ( r << CDG_COLOR_R_SHIFT ) | ( g << CDG_COLOR_G_SHIFT ) | ( b << CDG_COLOR_B_SHIFT );
407 }
408
409 static int Render( decoder_sys_t *p_cdg, picture_t *p_picture )
410 {
411     int x, y;
412
413     for( y = 0; y < CDG_DISPLAY_HEIGHT; y++ )
414     {
415         for( x = 0; x < CDG_DISPLAY_WIDTH; x++ )
416         {
417             const int sx = x + p_cdg->i_offseth + CDG_SCREEN_BORDER_WIDTH;
418             const int sy = y + p_cdg->i_offsetv + CDG_SCREEN_BORDER_HEIGHT;
419             uint8_t cidx = p_cdg->p_screen[sy*CDG_SCREEN_PITCH +sx];
420             uint8_t r = p_cdg->color[cidx][0];
421             uint8_t g = p_cdg->color[cidx][1];
422             uint8_t b = p_cdg->color[cidx][2];
423
424             RenderPixel( p_picture, x, y, RenderRGB( r, g, b ) );
425         }
426     }
427     return 0;
428 }
429