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