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 /*****************************************************************************
25 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_codec.h>
34 /*****************************************************************************
35 * decoder_sys_t : decoder descriptor
36 *****************************************************************************/
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
57 uint8_t screen
[CDG_SCREEN_PITCH
*CDG_SCREEN_HEIGHT
];
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 /*****************************************************************************
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 /*****************************************************************************
83 *****************************************************************************/
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
)
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
;
101 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_CDG
)
104 /* Allocate the memory needed to store the decoder's structure */
105 p_dec
->p_sys
= p_sys
= calloc( 1, sizeof(decoder_sys_t
) );
110 p_sys
->p_screen
= p_sys
->screen
;
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
;
125 p_dec
->pf_decode
= Decode
;
126 p_dec
->pf_flush
= Flush
;
131 /*****************************************************************************
133 *****************************************************************************/
134 static void Flush( decoder_t
*p_dec
)
136 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
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
)
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
) )
174 p_pic
= decoder_NewPicture( p_dec
);
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
;
183 block_Release( p_block
);
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
;
200 /*****************************************************************************
202 *****************************************************************************/
203 static void ScreenFill( decoder_sys_t
*p_cdg
, int sx
, int sy
, int dx
, int dy
, int c
)
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;
214 const int i_repeat
= p_data
[1]&0x0f;
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
);
220 static int DecodeBorderPreset( decoder_sys_t
*p_cdg
, const uint8_t *p_data
)
222 const int i_color
= p_data
[0]&0x0f;
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
);
236 static int DecodeLoadColorTable( decoder_sys_t
*p_cdg
, const uint8_t *p_data
, int i_base
)
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;
252 static int DecodeTileBlock( decoder_sys_t
*p_cdg
, const uint8_t *p_data
, int doXor
)
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
)
274 uint8_t *p
= &p_cdg
->p_screen
[index
];
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;
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;
303 switch( (p_data
[1] >> 4)&0x3 )
305 case 0x01: i_shifth
= 6; break;
306 case 0x02: i_shifth
= -6; break;
311 switch( (p_data
[2] >> 4)&0x3 )
313 case 0x01: i_shiftv
= 12; break;
314 case 0x02: i_shiftv
=-12; break;
320 if( i_shifth
== 0 && i_shiftv
== 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
);
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
;
339 dy
= (dy
+ CDG_SCREEN_HEIGHT
) % CDG_SCREEN_HEIGHT
;
340 dx
= (dx
+ CDG_SCREEN_WIDTH
) % CDG_SCREEN_WIDTH
;
344 if( dy
< 0 || (unsigned)dy
>= CDG_SCREEN_HEIGHT
||
345 dx
< 0 || (unsigned)dx
>= CDG_SCREEN_WIDTH
)
348 p_cdg
->screen
[dy
*CDG_SCREEN_PITCH
+dx
] = copy
[y
*CDG_SCREEN_PITCH
+x
];
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 );
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
)
367 /* Handle CDG command only */
371 switch( i_instruction
)
374 DecodeMemoryPreset( p_cdg
, p_data
);
377 DecodeBorderPreset( p_cdg
, p_data
);
380 DecodeTileBlock( p_cdg
, p_data
, 0 );
383 DecodeScroll( p_cdg
, p_data
, 0 );
386 DecodeScroll( p_cdg
, p_data
, 1 );
389 /* FIXME what to do about that one ? */
390 //CdgDebug( CDG_LOG_WARNING, "DecodeDefineTransparentColor not implemented\n" );
391 //DecodeDefineTransparentColor( p_cdg, p_data );
394 DecodeLoadColorTable( p_cdg
, p_data
, 0 );
397 DecodeLoadColorTable( p_cdg
, p_data
, 8 );
400 DecodeTileBlock( p_cdg
, p_data
, 1 );
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)];
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
) );