input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / codec / cdg.c
blob0b829399a0e8ec81ec7e5e63f57c2b1e23848d74
1 /*****************************************************************************
2 * cdg.c: CDG decoder module
3 *****************************************************************************
4 * Copyright (C) 2007 Laurent Aimar
5 * $Id$
7 * Authors: Laurent Aimar <fenrir # via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
35 /*****************************************************************************
36 * decoder_sys_t : decoder descriptor
37 *****************************************************************************/
39 /* The screen size */
40 #define CDG_SCREEN_WIDTH 300u
41 #define CDG_SCREEN_HEIGHT 216u
43 /* The border of the screen size */
44 #define CDG_SCREEN_BORDER_WIDTH 6u
45 #define CDG_SCREEN_BORDER_HEIGHT 12u
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)
51 #define CDG_SCREEN_PITCH CDG_SCREEN_WIDTH
53 typedef struct
55 uint8_t color[16][3];
56 unsigned i_offseth;
57 unsigned i_offsetv;
58 uint8_t screen[CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT];
59 uint8_t *p_screen;
61 int i_packet;
62 } decoder_sys_t;
64 #define CDG_PACKET_SIZE 24u
66 #define CDG_COLOR_R_SHIFT 0
67 #define CDG_COLOR_G_SHIFT 8
68 #define CDG_COLOR_B_SHIFT 16
70 /*****************************************************************************
71 * Local prototypes
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Close( vlc_object_t * );
76 static int Decode( decoder_t *, block_t * );
78 static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer );
79 static void Flush( decoder_t * );
80 static int Render( decoder_sys_t *p_cdg, picture_t *p_picture );
82 /*****************************************************************************
83 * Module descriptor
84 *****************************************************************************/
85 vlc_module_begin ()
86 set_category( CAT_INPUT )
87 set_subcategory( SUBCAT_INPUT_VCODEC )
88 set_description( N_("CDG video decoder") )
89 set_capability( "video decoder", 1000 )
90 set_callbacks( Open, Close )
91 add_shortcut( "cdg" )
92 vlc_module_end ()
94 /*****************************************************************************
95 * Open: probe the decoder and return score
96 *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
99 decoder_t *p_dec = (decoder_t*)p_this;
100 decoder_sys_t *p_sys;
102 if( p_dec->fmt_in.i_codec != VLC_CODEC_CDG )
103 return VLC_EGENERIC;
105 /* Allocate the memory needed to store the decoder's structure */
106 p_dec->p_sys = p_sys = calloc( 1, sizeof(decoder_sys_t) );
107 if( !p_sys )
108 return VLC_ENOMEM;
110 /* Init */
111 p_sys->p_screen = p_sys->screen;
112 p_sys->i_packet = 0;
114 /* Set output properties
115 * TODO maybe it would be better to use RV16 or RV24 ? */
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;
125 /* Set callbacks */
126 p_dec->pf_decode = Decode;
127 p_dec->pf_flush = Flush;
129 return VLC_SUCCESS;
132 /*****************************************************************************
133 * Flush:
134 *****************************************************************************/
135 static void Flush( decoder_t *p_dec )
137 decoder_sys_t *p_sys = p_dec->p_sys;
139 p_sys->i_packet = 0;
142 /****************************************************************************
143 * Decode: the whole thing
144 ****************************************************************************
145 * This function must be fed with a complete compressed frame.
146 ****************************************************************************/
147 static int Decode( decoder_t *p_dec, block_t *p_block )
149 decoder_sys_t *p_sys = p_dec->p_sys;
150 picture_t *p_pic = NULL;
152 if( !p_block ) /* No Drain */
153 return VLCDEC_SUCCESS;
155 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
157 Flush( p_dec );
158 goto exit;
161 /* Decode packet */
162 while( p_block->i_buffer >= CDG_PACKET_SIZE )
164 DecodePacket( p_sys, p_block->p_buffer, CDG_PACKET_SIZE );
165 p_block->i_buffer -= CDG_PACKET_SIZE;
166 p_block->p_buffer += CDG_PACKET_SIZE;
169 /* Only display 25 frame per second (there is 75 packets per second) */
170 if( (p_sys->i_packet%3) == 1 && p_block->i_pts == p_block->i_dts )
172 /* Get a new picture */
173 if( decoder_UpdateVideoFormat( p_dec ) )
174 goto exit;
175 p_pic = decoder_NewPicture( p_dec );
176 if( !p_pic )
177 goto exit;
179 Render( p_sys, p_pic );
180 p_pic->date = p_block->i_pts != VLC_TICK_INVALID ? p_block->i_pts : p_block->i_dts;
183 exit:
184 block_Release( p_block );
185 if( p_pic != NULL )
186 decoder_QueueVideo( p_dec, p_pic );
187 return VLCDEC_SUCCESS;
190 /*****************************************************************************
191 * Close: decoder destruction
192 *****************************************************************************/
193 static void Close( vlc_object_t *p_this )
195 decoder_t *p_dec = (decoder_t *)p_this;
196 decoder_sys_t *p_sys = p_dec->p_sys;
198 free( p_sys );
201 /*****************************************************************************
202 * Decoder
203 *****************************************************************************/
204 static void ScreenFill( decoder_sys_t *p_cdg, int sx, int sy, int dx, int dy, int c )
206 int x, y;
207 for( y = sy; y < sy+dy; y++ )
208 for( x = sx; x < sx+dx; x++ )
209 p_cdg->p_screen[y*CDG_SCREEN_PITCH+x] = c;
211 static int DecodeMemoryPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
213 const int i_color = p_data[0]&0x0f;
214 #if 0
215 const int i_repeat= p_data[1]&0x0f;
216 #endif
217 /* if i_repeat > 0 we could ignore it if we have a reliable stream */
218 ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, i_color );
219 return 0;
221 static int DecodeBorderPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
223 const int i_color = p_data[0]&0x0f;
225 /* */
226 ScreenFill( p_cdg, 0, 0,
227 CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
228 ScreenFill( p_cdg, 0, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT,
229 CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
230 ScreenFill( p_cdg, 0, CDG_SCREEN_BORDER_HEIGHT,
231 CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
232 ScreenFill( p_cdg, CDG_SCREEN_WIDTH-CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_BORDER_HEIGHT,
233 CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
234 return 0;
237 static int DecodeLoadColorTable( decoder_sys_t *p_cdg, const uint8_t *p_data, int i_base )
239 int n;
240 for( n = 0; n < 8; n++ )
242 const int c = ((p_data[2*n+0] << 8) | p_data[2*n+1]);
243 const int r = (c >> 10)&0x0f;
244 const int g = ((c >> 6)&0x0c) | ((c >> 4)&0x03);
245 const int b = c &0x0f;
247 p_cdg->color[i_base+n][0] = r << 4;
248 p_cdg->color[i_base+n][1] = g << 4;
249 p_cdg->color[i_base+n][2] = b << 4;
251 return 0;
253 static int DecodeTileBlock( decoder_sys_t *p_cdg, const uint8_t *p_data, int doXor )
255 int p_color[2];
256 int sx, sy;
257 int x, y;
259 p_color[0] = p_data[0] & 0x0f;
260 p_color[1] = p_data[1] & 0x0f;
262 sy = (p_data[2] & 0x1f)*12;
263 sx = (p_data[3] & 0x3f)*6;
265 for( y = 0; y < 12; y++ )
267 for( x = 0; x < 6; x++ )
269 const int idx = ( p_data[4+y] >> (5-x) ) & 0x01;
271 unsigned index = (sy+y)*CDG_SCREEN_PITCH+(sx+x);
272 if( index >= CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT )
273 return 0;
275 uint8_t *p = &p_cdg->p_screen[index];
277 if( doXor )
278 *p ^= p_color[idx];
279 else
280 *p = p_color[idx];
283 return 0;
286 static int DecodeScroll( decoder_sys_t *p_cdg, const uint8_t *p_data, int b_copy )
288 uint8_t copy[CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT];
290 uint8_t color = p_data[0]&0x0f;
291 int i_shifth;
292 int i_shiftv;
294 /* */
295 p_cdg->i_offseth = p_data[1]&0x7;
296 if( p_cdg->i_offseth >= CDG_SCREEN_BORDER_WIDTH )
297 p_cdg->i_offseth = CDG_SCREEN_BORDER_WIDTH-1;
299 p_cdg->i_offsetv = p_data[2]&0xf;
300 if( p_cdg->i_offsetv >= CDG_SCREEN_BORDER_HEIGHT )
301 p_cdg->i_offsetv = CDG_SCREEN_BORDER_HEIGHT-1;
303 /* */
304 switch( (p_data[1] >> 4)&0x3 )
306 case 0x01: i_shifth = 6; break;
307 case 0x02: i_shifth = -6; break;
308 default:
309 i_shifth = 0;
310 break;
312 switch( (p_data[2] >> 4)&0x3 )
314 case 0x01: i_shiftv = 12; break;
315 case 0x02: i_shiftv =-12; break;
316 default:
317 i_shiftv = 0;
318 break;
321 if( i_shifth == 0 && i_shiftv == 0 )
322 return 0;
324 /* Make a copy of the screen */
325 memcpy( copy, p_cdg->screen, sizeof(p_cdg->screen) );
327 /* Fill the uncovered part XXX way too much */
328 ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, color );
330 /* Copy back */
331 for( unsigned y = 0; y < CDG_SCREEN_HEIGHT; y++ )
333 int dy = i_shiftv + y;
334 for( unsigned x = 0; x < CDG_SCREEN_WIDTH; x++ )
336 int dx = i_shifth + x;
338 if( b_copy )
340 dy = (dy + CDG_SCREEN_HEIGHT) % CDG_SCREEN_HEIGHT;
341 dx = (dx + CDG_SCREEN_WIDTH ) % CDG_SCREEN_WIDTH;
343 else
345 if( dy < 0 || (unsigned)dy >= CDG_SCREEN_HEIGHT ||
346 dx < 0 || (unsigned)dx >= CDG_SCREEN_WIDTH )
347 continue;
349 p_cdg->screen[dy*CDG_SCREEN_PITCH+dx] = copy[y*CDG_SCREEN_PITCH+x];
352 /* */
353 //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 );
354 return 0;
357 static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer )
359 const int i_cmd = p_buffer[0] & 0x3f;
360 const int i_instruction = p_buffer[1] & 0x3f;
361 const uint8_t *p_data = &p_buffer[4];
363 if( i_buffer != CDG_PACKET_SIZE )
364 return -1;
366 p_cdg->i_packet++;
368 /* Handle CDG command only */
369 if( i_cmd != 0x09 )
370 return 0;
372 switch( i_instruction )
374 case 1:
375 DecodeMemoryPreset( p_cdg, p_data );
376 break;
377 case 2:
378 DecodeBorderPreset( p_cdg, p_data );
379 break;
380 case 6:
381 DecodeTileBlock( p_cdg, p_data, 0 );
382 break;
383 case 20:
384 DecodeScroll( p_cdg, p_data, 0 );
385 break;
386 case 24:
387 DecodeScroll( p_cdg, p_data, 1 );
388 break;
389 case 28:
390 /* FIXME what to do about that one ? */
391 //CdgDebug( CDG_LOG_WARNING, "DecodeDefineTransparentColor not implemented\n" );
392 //DecodeDefineTransparentColor( p_cdg, p_data );
393 break;
394 case 30:
395 DecodeLoadColorTable( p_cdg, p_data, 0 );
396 break;
397 case 31:
398 DecodeLoadColorTable( p_cdg, p_data, 8 );
399 break;
400 case 38:
401 DecodeTileBlock( p_cdg, p_data, 1 );
402 break;
403 default:
404 break;
406 return 0;
409 static void RenderPixel( picture_t *p_picture, int x, int y, uint32_t c )
411 const int i_plane = 0;
412 const int i_pitch = p_picture->p[i_plane].i_pitch;
413 uint32_t *s = (uint32_t*)&p_picture->p[i_plane].p_pixels[y*i_pitch + x*sizeof(uint32_t)];
414 *s = c;
416 static uint32_t RenderRGB( int r, int g, int b )
418 return ( r << CDG_COLOR_R_SHIFT ) | ( g << CDG_COLOR_G_SHIFT ) | ( b << CDG_COLOR_B_SHIFT );
421 static int Render( decoder_sys_t *p_cdg, picture_t *p_picture )
423 for( unsigned y = 0; y < CDG_DISPLAY_HEIGHT; y++ )
425 for( unsigned x = 0; x < CDG_DISPLAY_WIDTH; x++ )
427 const int sx = x + p_cdg->i_offseth + CDG_SCREEN_BORDER_WIDTH;
428 const int sy = y + p_cdg->i_offsetv + CDG_SCREEN_BORDER_HEIGHT;
429 uint8_t cidx = p_cdg->p_screen[sy*CDG_SCREEN_PITCH +sx];
430 uint8_t r = p_cdg->color[cidx][0];
431 uint8_t g = p_cdg->color[cidx][1];
432 uint8_t b = p_cdg->color[cidx][2];
434 RenderPixel( p_picture, x, y, RenderRGB( r, g, b ) );
437 return 0;