1 /*****************************************************************************
2 * cdg.c: CDG decoder module
3 *****************************************************************************
4 * Copyright (C) 2007 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir # via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
36 /*****************************************************************************
37 * decoder_sys_t : decoder descriptor
38 *****************************************************************************/
41 #define CDG_SCREEN_WIDTH (300)
42 #define CDG_SCREEN_HEIGHT (216)
44 /* The border of the screen size */
45 #define CDG_SCREEN_BORDER_WIDTH (6)
46 #define CDG_SCREEN_BORDER_HEIGHT (12)
48 /* The display part */
49 #define CDG_DISPLAY_WIDTH (CDG_SCREEN_WIDTH-2*CDG_SCREEN_BORDER_WIDTH)
50 #define CDG_DISPLAY_HEIGHT (CDG_SCREEN_HEIGHT-2*CDG_SCREEN_BORDER_HEIGHT)
52 #define CDG_SCREEN_PITCH (CDG_SCREEN_WIDTH)
59 uint8_t screen[CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT];
63 #define CDG_PACKET_SIZE (24)
65 #define CDG_COLOR_R_SHIFT ( 0)
66 #define CDG_COLOR_G_SHIFT ( 8)
67 #define CDG_COLOR_B_SHIFT (16)
69 /*****************************************************************************
71 *****************************************************************************/
72 static int Open ( vlc_object_t * );
73 static void Close( vlc_object_t * );
75 static picture_t *Decode( decoder_t *, block_t ** );
77 static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer );
78 static int Render( decoder_sys_t *p_cdg, picture_t *p_picture );
80 /*****************************************************************************
82 *****************************************************************************/
84 set_category( CAT_INPUT );
85 set_subcategory( SUBCAT_INPUT_VCODEC );
86 set_description( _("CDG video decoder") );
87 set_capability( "decoder", 1000 );
88 set_callbacks( Open, Close );
89 add_shortcut( "cdg" );
92 /*****************************************************************************
93 * Open: probe the decoder and return score
94 *****************************************************************************/
95 static int Open( vlc_object_t *p_this )
97 decoder_t *p_dec = (decoder_t*)p_this;
100 if( p_dec->fmt_in.i_codec != VLC_FOURCC('C','D','G',' ') )
103 /* Allocate the memory needed to store the decoder's structure */
104 p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t));
107 msg_Err( p_dec, "out of memory" );
112 memset( p_sys, 0, sizeof(*p_sys) );
113 p_sys->i_offseth = 0;
114 p_sys->i_offsetv = 0;
115 p_sys->p_screen = p_sys->screen;
117 /* Set output properties
118 * TODO maybe it would be better to use RV16 or RV24 ? */
119 p_dec->fmt_out.i_cat = VIDEO_ES;
120 p_dec->fmt_out.i_codec = VLC_FOURCC('R','V','3','2');
121 p_dec->fmt_out.video.i_width = CDG_DISPLAY_WIDTH;
122 p_dec->fmt_out.video.i_height = CDG_DISPLAY_HEIGHT;
123 p_dec->fmt_out.video.i_aspect =
124 VOUT_ASPECT_FACTOR * p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
125 p_dec->fmt_out.video.i_rmask = 0xff << CDG_COLOR_R_SHIFT;
126 p_dec->fmt_out.video.i_gmask = 0xff << CDG_COLOR_G_SHIFT;
127 p_dec->fmt_out.video.i_bmask = 0xff << CDG_COLOR_B_SHIFT;
130 p_dec->pf_decode_video = Decode;
135 /****************************************************************************
136 * Decode: the whole thing
137 ****************************************************************************
138 * This function must be fed with a complete compressed frame.
139 ****************************************************************************/
140 static picture_t *Decode( decoder_t *p_dec, block_t **pp_block )
142 decoder_sys_t *p_sys = p_dec->p_sys;
144 picture_t *p_pic = NULL;
146 if( !pp_block || !*pp_block )
151 while( p_block->i_buffer >= CDG_PACKET_SIZE )
153 DecodePacket( p_sys, p_block->p_buffer, CDG_PACKET_SIZE );
154 p_block->i_buffer -= CDG_PACKET_SIZE;
155 p_block->p_buffer += CDG_PACKET_SIZE;
158 /* Get a new picture */
159 p_pic = p_dec->pf_vout_buffer_new( p_dec );
163 Render( p_sys, p_pic );
164 p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts;
166 block_Release( p_block ); *pp_block = NULL;
170 block_Release( p_block ); *pp_block = NULL;
174 /*****************************************************************************
175 * Close: decoder destruction
176 *****************************************************************************/
177 static void Close( vlc_object_t *p_this )
179 decoder_t *p_dec = (decoder_t *)p_this;
180 decoder_sys_t *p_sys = p_dec->p_sys;
185 /*****************************************************************************
187 *****************************************************************************/
188 static void ScreenFill( decoder_sys_t *p_cdg, int sx, int sy, int dx, int dy, int c )
191 for( y = sy; y < sy+dy; y++ )
192 for( x = sx; x < sx+dx; x++ )
193 p_cdg->p_screen[y*CDG_SCREEN_PITCH+x] = c;
195 static int DecodeMemoryPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
197 const int i_color = p_data[0]&0x0f;
199 const int i_repeat= p_data[1]&0x0f;
201 /* if i_repeat > 0 we could ignore it if we have a reliable stream */
202 ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, i_color );
205 static int DecodeBorderPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
207 const int i_color = p_data[0]&0x0f;
210 ScreenFill( p_cdg, 0, 0,
211 CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
212 ScreenFill( p_cdg, 0, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT,
213 CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
214 ScreenFill( p_cdg, 0, CDG_SCREEN_BORDER_HEIGHT,
215 CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
216 ScreenFill( p_cdg, CDG_SCREEN_WIDTH-CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_BORDER_HEIGHT,
217 CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
221 static int DecodeLoadColorTable( decoder_sys_t *p_cdg, const uint8_t *p_data, int i_base )
224 for( n = 0; n < 8; n++ )
226 const int c = ((p_data[2*n+0] << 8) | p_data[2*n+1]);
227 const int r = (c >> 10)&0x0f;
228 const int g = ((c >> 6)&0x0c) | ((c >> 4)&0x03);
229 const int b = c &0x0f;
231 p_cdg->color[i_base+n][0] = r << 4;
232 p_cdg->color[i_base+n][1] = g << 4;
233 p_cdg->color[i_base+n][2] = b << 4;
237 static int DecodeTileBlock( decoder_sys_t *p_cdg, const uint8_t *p_data, int doXor )
243 p_color[0] = p_data[0] & 0x0f;
244 p_color[1] = p_data[1] & 0x0f;
246 sy = (p_data[2] & 0x1f)*12;
247 sx = (p_data[3] & 0x3f)*6;
249 for( y = 0; y < 12; y++ )
251 for( x = 0; x < 6; x++ )
253 const int idx = ( p_data[4+y] >> (5-x) ) & 0x01;
254 uint8_t *p = &p_cdg->p_screen[(sy+y)*CDG_SCREEN_PITCH+(sx+x)];
264 static int DecodeScroll( decoder_sys_t *p_cdg, const uint8_t *p_data, int b_copy )
266 uint8_t copy[CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT];
268 uint8_t color = p_data[0]&0x0f;
274 p_cdg->i_offseth = p_data[1]&0x7;
275 if( p_cdg->i_offseth >= CDG_SCREEN_BORDER_WIDTH )
276 p_cdg->i_offseth = CDG_SCREEN_BORDER_WIDTH-1;
278 p_cdg->i_offsetv = p_data[2]&0xf;
279 if( p_cdg->i_offsetv >= CDG_SCREEN_BORDER_HEIGHT )
280 p_cdg->i_offsetv = CDG_SCREEN_BORDER_HEIGHT-1;
283 switch( (p_data[1] >> 4)&0x3 )
285 case 0x01: i_shifth = 6; break;
286 case 0x02: i_shifth = -6; break;
291 switch( (p_data[2] >> 4)&0x3 )
293 case 0x01: i_shiftv = 12; break;
294 case 0x02: i_shiftv =-12; break;
300 if( i_shifth == 0 && i_shiftv == 0 )
303 /* Make a copy of the screen */
304 memcpy( copy, p_cdg->screen, sizeof(p_cdg->screen) );
306 /* Fill the uncovered part XXX way too much */
307 ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, color );
310 for( y = 0; y < CDG_SCREEN_HEIGHT; y++ )
312 int dy = i_shiftv + y;
313 for( x = 0; x < CDG_SCREEN_WIDTH; x++ )
315 int dx = i_shifth + x;
319 dy = ( dy + CDG_SCREEN_HEIGHT ) % CDG_SCREEN_HEIGHT;
320 dy = ( dy + CDG_SCREEN_WIDTH ) % CDG_SCREEN_WIDTH;
324 if( dy < 0 || dy >= CDG_SCREEN_HEIGHT ||
325 dx < 0 || dx >= CDG_SCREEN_WIDTH )
328 p_cdg->screen[dy*CDG_SCREEN_PITCH+dx] = copy[y*CDG_SCREEN_PITCH+x];
332 //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 );
336 static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer )
338 const int i_cmd = p_buffer[0] & 0x3f;
339 const int i_instruction = p_buffer[1] & 0x3f;
340 const uint8_t *p_data = &p_buffer[4];
342 if( i_buffer != CDG_PACKET_SIZE )
345 /* Handle CDG command only */
349 switch( i_instruction )
352 DecodeMemoryPreset( p_cdg, p_data );
355 DecodeBorderPreset( p_cdg, p_data );
358 DecodeTileBlock( p_cdg, p_data, 0 );
361 DecodeScroll( p_cdg, p_data, 0 );
364 DecodeScroll( p_cdg, p_data, 1 );
367 /* FIXME what to do about that one ? */
368 //CdgDebug( CDG_LOG_WARNING, "DecodeDefineTransparentColor not implemented\n" );
369 //DecodeDefineTransparentColor( p_cdg, p_data );
372 DecodeLoadColorTable( p_cdg, p_data, 0 );
375 DecodeLoadColorTable( p_cdg, p_data, 8 );
378 DecodeTileBlock( p_cdg, p_data, 1 );
386 static void RenderPixel( picture_t *p_picture, int x, int y, uint32_t c )
388 const int i_plane = 0;
389 const int i_pitch = p_picture->p[i_plane].i_pitch;
390 uint32_t *s = (uint32_t*)&p_picture->p[i_plane].p_pixels[y*i_pitch + x*sizeof(uint32_t)];
393 static uint32_t RenderRGB( int r, int g, int b )
395 return ( r << CDG_COLOR_R_SHIFT ) | ( g << CDG_COLOR_G_SHIFT ) | ( b << CDG_COLOR_B_SHIFT );
398 static int Render( decoder_sys_t *p_cdg, picture_t *p_picture )
402 for( y = 0; y < CDG_DISPLAY_HEIGHT; y++ )
404 for( x = 0; x < CDG_DISPLAY_WIDTH; x++ )
406 const int sx = x + p_cdg->i_offseth + CDG_SCREEN_BORDER_WIDTH;
407 const int sy = y + p_cdg->i_offsetv + CDG_SCREEN_BORDER_HEIGHT;
408 uint8_t cidx = p_cdg->p_screen[sy*CDG_SCREEN_PITCH +sx];
409 uint8_t r = p_cdg->color[cidx][0];
410 uint8_t g = p_cdg->color[cidx][1];
411 uint8_t b = p_cdg->color[cidx][2];
413 RenderPixel( p_picture, x, y, RenderRGB( r, g, b ) );