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