qml: remove context indicator
[vlc.git] / modules / codec / cdg.c
blob0fdfc56f9dc431aa37feb67797d079cdbfcfaf69
1 /*****************************************************************************
2 * cdg.c: CDG decoder module
3 *****************************************************************************
4 * Copyright (C) 2007 Laurent Aimar
6 * Authors: Laurent Aimar <fenrir # via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_codec.h>
34 /*****************************************************************************
35 * decoder_sys_t : decoder descriptor
36 *****************************************************************************/
38 /* The screen size */
39 #define CDG_SCREEN_WIDTH 300u
40 #define CDG_SCREEN_HEIGHT 216u
42 /* The border of the screen size */
43 #define CDG_SCREEN_BORDER_WIDTH 6u
44 #define CDG_SCREEN_BORDER_HEIGHT 12u
46 /* The display part */
47 #define CDG_DISPLAY_WIDTH (CDG_SCREEN_WIDTH-2*CDG_SCREEN_BORDER_WIDTH)
48 #define CDG_DISPLAY_HEIGHT (CDG_SCREEN_HEIGHT-2*CDG_SCREEN_BORDER_HEIGHT)
50 #define CDG_SCREEN_PITCH CDG_SCREEN_WIDTH
52 typedef struct
54 uint8_t color[16][3];
55 unsigned i_offseth;
56 unsigned i_offsetv;
57 uint8_t screen[CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT];
58 uint8_t *p_screen;
60 int i_packet;
61 } decoder_sys_t;
63 #define CDG_PACKET_SIZE 24u
65 #define CDG_COLOR_R_SHIFT 0
66 #define CDG_COLOR_G_SHIFT 8
67 #define CDG_COLOR_B_SHIFT 16
69 /*****************************************************************************
70 * Local prototypes
71 *****************************************************************************/
72 static int Open ( vlc_object_t * );
73 static void Close( vlc_object_t * );
75 static int Decode( decoder_t *, block_t * );
77 static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer );
78 static void Flush( decoder_t * );
79 static int Render( decoder_sys_t *p_cdg, picture_t *p_picture );
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( "video decoder", 1000 )
89 set_callbacks( Open, Close )
90 add_shortcut( "cdg" )
91 vlc_module_end ()
93 /*****************************************************************************
94 * Open: probe the decoder and return score
95 *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
98 decoder_t *p_dec = (decoder_t*)p_this;
99 decoder_sys_t *p_sys;
101 if( p_dec->fmt_in.i_codec != VLC_CODEC_CDG )
102 return VLC_EGENERIC;
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;
109 /* Init */
110 p_sys->p_screen = p_sys->screen;
111 p_sys->i_packet = 0;
113 /* Set output properties
114 * TODO maybe it would be better to use RV16 or RV24 ? */
115 p_dec->fmt_out.i_codec = VLC_CODEC_RGB32;
116 p_dec->fmt_out.video.i_width = CDG_DISPLAY_WIDTH;
117 p_dec->fmt_out.video.i_height = CDG_DISPLAY_HEIGHT;
118 p_dec->fmt_out.video.i_sar_num = 1;
119 p_dec->fmt_out.video.i_sar_den = 1;
120 p_dec->fmt_out.video.i_rmask = 0xff << CDG_COLOR_R_SHIFT;
121 p_dec->fmt_out.video.i_gmask = 0xff << CDG_COLOR_G_SHIFT;
122 p_dec->fmt_out.video.i_bmask = 0xff << CDG_COLOR_B_SHIFT;
124 /* Set callbacks */
125 p_dec->pf_decode = Decode;
126 p_dec->pf_flush = Flush;
128 return VLC_SUCCESS;
131 /*****************************************************************************
132 * Flush:
133 *****************************************************************************/
134 static void Flush( decoder_t *p_dec )
136 decoder_sys_t *p_sys = p_dec->p_sys;
138 p_sys->i_packet = 0;
141 /****************************************************************************
142 * Decode: the whole thing
143 ****************************************************************************
144 * This function must be fed with a complete compressed frame.
145 ****************************************************************************/
146 static int Decode( decoder_t *p_dec, block_t *p_block )
148 decoder_sys_t *p_sys = p_dec->p_sys;
149 picture_t *p_pic = NULL;
151 if( !p_block ) /* No Drain */
152 return VLCDEC_SUCCESS;
154 if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
156 Flush( p_dec );
157 goto exit;
160 /* Decode packet */
161 while( p_block->i_buffer >= CDG_PACKET_SIZE )
163 DecodePacket( p_sys, p_block->p_buffer, CDG_PACKET_SIZE );
164 p_block->i_buffer -= CDG_PACKET_SIZE;
165 p_block->p_buffer += CDG_PACKET_SIZE;
168 /* Only display 25 frame per second (there is 75 packets per second) */
169 if( (p_sys->i_packet%3) == 1 && p_block->i_pts == p_block->i_dts )
171 /* Get a new picture */
172 if( decoder_UpdateVideoFormat( p_dec ) )
173 goto exit;
174 p_pic = decoder_NewPicture( p_dec );
175 if( !p_pic )
176 goto exit;
178 Render( p_sys, p_pic );
179 p_pic->date = p_block->i_pts != VLC_TICK_INVALID ? p_block->i_pts : p_block->i_dts;
182 exit:
183 block_Release( p_block );
184 if( p_pic != NULL )
185 decoder_QueueVideo( p_dec, p_pic );
186 return VLCDEC_SUCCESS;
189 /*****************************************************************************
190 * Close: decoder destruction
191 *****************************************************************************/
192 static void Close( vlc_object_t *p_this )
194 decoder_t *p_dec = (decoder_t *)p_this;
195 decoder_sys_t *p_sys = p_dec->p_sys;
197 free( p_sys );
200 /*****************************************************************************
201 * Decoder
202 *****************************************************************************/
203 static void ScreenFill( decoder_sys_t *p_cdg, int sx, int sy, int dx, int dy, int c )
205 int x, y;
206 for( y = sy; y < sy+dy; y++ )
207 for( x = sx; x < sx+dx; x++ )
208 p_cdg->p_screen[y*CDG_SCREEN_PITCH+x] = c;
210 static int DecodeMemoryPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
212 const int i_color = p_data[0]&0x0f;
213 #if 0
214 const int i_repeat= p_data[1]&0x0f;
215 #endif
216 /* if i_repeat > 0 we could ignore it if we have a reliable stream */
217 ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, i_color );
218 return 0;
220 static int DecodeBorderPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
222 const int i_color = p_data[0]&0x0f;
224 /* */
225 ScreenFill( p_cdg, 0, 0,
226 CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
227 ScreenFill( p_cdg, 0, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT,
228 CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
229 ScreenFill( p_cdg, 0, CDG_SCREEN_BORDER_HEIGHT,
230 CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
231 ScreenFill( p_cdg, CDG_SCREEN_WIDTH-CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_BORDER_HEIGHT,
232 CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
233 return 0;
236 static int DecodeLoadColorTable( decoder_sys_t *p_cdg, const uint8_t *p_data, int i_base )
238 int n;
239 for( n = 0; n < 8; n++ )
241 const int c = ((p_data[2*n+0] << 8) | p_data[2*n+1]);
242 const int r = (c >> 10)&0x0f;
243 const int g = ((c >> 6)&0x0c) | ((c >> 4)&0x03);
244 const int b = c &0x0f;
246 p_cdg->color[i_base+n][0] = r << 4;
247 p_cdg->color[i_base+n][1] = g << 4;
248 p_cdg->color[i_base+n][2] = b << 4;
250 return 0;
252 static int DecodeTileBlock( decoder_sys_t *p_cdg, const uint8_t *p_data, int doXor )
254 int p_color[2];
255 int sx, sy;
256 int x, y;
258 p_color[0] = p_data[0] & 0x0f;
259 p_color[1] = p_data[1] & 0x0f;
261 sy = (p_data[2] & 0x1f)*12;
262 sx = (p_data[3] & 0x3f)*6;
264 for( y = 0; y < 12; y++ )
266 for( x = 0; x < 6; x++ )
268 const int idx = ( p_data[4+y] >> (5-x) ) & 0x01;
270 unsigned index = (sy+y)*CDG_SCREEN_PITCH+(sx+x);
271 if( index >= CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT )
272 return 0;
274 uint8_t *p = &p_cdg->p_screen[index];
276 if( doXor )
277 *p ^= p_color[idx];
278 else
279 *p = p_color[idx];
282 return 0;
285 static int DecodeScroll( decoder_sys_t *p_cdg, const uint8_t *p_data, int b_copy )
287 uint8_t copy[CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT];
289 uint8_t color = p_data[0]&0x0f;
290 int i_shifth;
291 int i_shiftv;
293 /* */
294 p_cdg->i_offseth = p_data[1]&0x7;
295 if( p_cdg->i_offseth >= CDG_SCREEN_BORDER_WIDTH )
296 p_cdg->i_offseth = CDG_SCREEN_BORDER_WIDTH-1;
298 p_cdg->i_offsetv = p_data[2]&0xf;
299 if( p_cdg->i_offsetv >= CDG_SCREEN_BORDER_HEIGHT )
300 p_cdg->i_offsetv = CDG_SCREEN_BORDER_HEIGHT-1;
302 /* */
303 switch( (p_data[1] >> 4)&0x3 )
305 case 0x01: i_shifth = 6; break;
306 case 0x02: i_shifth = -6; break;
307 default:
308 i_shifth = 0;
309 break;
311 switch( (p_data[2] >> 4)&0x3 )
313 case 0x01: i_shiftv = 12; break;
314 case 0x02: i_shiftv =-12; break;
315 default:
316 i_shiftv = 0;
317 break;
320 if( i_shifth == 0 && i_shiftv == 0 )
321 return 0;
323 /* Make a copy of the screen */
324 memcpy( copy, p_cdg->screen, sizeof(p_cdg->screen) );
326 /* Fill the uncovered part XXX way too much */
327 ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, color );
329 /* Copy back */
330 for( unsigned y = 0; y < CDG_SCREEN_HEIGHT; y++ )
332 int dy = i_shiftv + y;
333 for( unsigned x = 0; x < CDG_SCREEN_WIDTH; x++ )
335 int dx = i_shifth + x;
337 if( b_copy )
339 dy = (dy + CDG_SCREEN_HEIGHT) % CDG_SCREEN_HEIGHT;
340 dx = (dx + CDG_SCREEN_WIDTH ) % CDG_SCREEN_WIDTH;
342 else
344 if( dy < 0 || (unsigned)dy >= CDG_SCREEN_HEIGHT ||
345 dx < 0 || (unsigned)dx >= CDG_SCREEN_WIDTH )
346 continue;
348 p_cdg->screen[dy*CDG_SCREEN_PITCH+dx] = copy[y*CDG_SCREEN_PITCH+x];
351 /* */
352 //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 );
353 return 0;
356 static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer )
358 const int i_cmd = p_buffer[0] & 0x3f;
359 const int i_instruction = p_buffer[1] & 0x3f;
360 const uint8_t *p_data = &p_buffer[4];
362 if( i_buffer != CDG_PACKET_SIZE )
363 return -1;
365 p_cdg->i_packet++;
367 /* Handle CDG command only */
368 if( i_cmd != 0x09 )
369 return 0;
371 switch( i_instruction )
373 case 1:
374 DecodeMemoryPreset( p_cdg, p_data );
375 break;
376 case 2:
377 DecodeBorderPreset( p_cdg, p_data );
378 break;
379 case 6:
380 DecodeTileBlock( p_cdg, p_data, 0 );
381 break;
382 case 20:
383 DecodeScroll( p_cdg, p_data, 0 );
384 break;
385 case 24:
386 DecodeScroll( p_cdg, p_data, 1 );
387 break;
388 case 28:
389 /* FIXME what to do about that one ? */
390 //CdgDebug( CDG_LOG_WARNING, "DecodeDefineTransparentColor not implemented\n" );
391 //DecodeDefineTransparentColor( p_cdg, p_data );
392 break;
393 case 30:
394 DecodeLoadColorTable( p_cdg, p_data, 0 );
395 break;
396 case 31:
397 DecodeLoadColorTable( p_cdg, p_data, 8 );
398 break;
399 case 38:
400 DecodeTileBlock( p_cdg, p_data, 1 );
401 break;
402 default:
403 break;
405 return 0;
408 static void RenderPixel( picture_t *p_picture, int x, int y, uint32_t c )
410 const int i_plane = 0;
411 const int i_pitch = p_picture->p[i_plane].i_pitch;
412 uint32_t *s = (uint32_t*)&p_picture->p[i_plane].p_pixels[y*i_pitch + x*sizeof(uint32_t)];
413 *s = c;
415 static uint32_t RenderRGB( int r, int g, int b )
417 return ( r << CDG_COLOR_R_SHIFT ) | ( g << CDG_COLOR_G_SHIFT ) | ( b << CDG_COLOR_B_SHIFT );
420 static int Render( decoder_sys_t *p_cdg, picture_t *p_picture )
422 for( unsigned y = 0; y < CDG_DISPLAY_HEIGHT; y++ )
424 for( unsigned x = 0; x < CDG_DISPLAY_WIDTH; x++ )
426 const int sx = x + p_cdg->i_offseth + CDG_SCREEN_BORDER_WIDTH;
427 const int sy = y + p_cdg->i_offsetv + CDG_SCREEN_BORDER_HEIGHT;
428 uint8_t cidx = p_cdg->p_screen[sy*CDG_SCREEN_PITCH +sx];
429 uint8_t r = p_cdg->color[cidx][0];
430 uint8_t g = p_cdg->color[cidx][1];
431 uint8_t b = p_cdg->color[cidx][2];
433 RenderPixel( p_picture, x, y, RenderRGB( r, g, b ) );
436 return 0;