cdg.c 13.2 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3 4
/*****************************************************************************
 * cdg.c: CDG decoder module
 *****************************************************************************
 * Copyright (C) 2007 Laurent Aimar
5
 * $Id$
Laurent Aimar's avatar
Laurent Aimar committed
6 7 8
 *
 * Authors: Laurent Aimar <fenrir # via.ecp.fr>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
Laurent Aimar's avatar
Laurent Aimar committed
12 13 14 15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
Laurent Aimar's avatar
Laurent Aimar committed
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Laurent Aimar's avatar
Laurent Aimar committed
22 23 24 25 26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
Laurent Aimar's avatar
Laurent Aimar committed
33 34 35 36 37 38 39
#include <vlc_codec.h>

/*****************************************************************************
 * decoder_sys_t : decoder descriptor
 *****************************************************************************/

/* The screen size */
40 41
#define CDG_SCREEN_WIDTH 300u
#define CDG_SCREEN_HEIGHT 216u
Laurent Aimar's avatar
Laurent Aimar committed
42 43

/* The border of the screen size */
44 45
#define CDG_SCREEN_BORDER_WIDTH 6u
#define CDG_SCREEN_BORDER_HEIGHT 12u
Laurent Aimar's avatar
Laurent Aimar committed
46 47 48 49 50

/* The display part */
#define CDG_DISPLAY_WIDTH  (CDG_SCREEN_WIDTH-2*CDG_SCREEN_BORDER_WIDTH)
#define CDG_DISPLAY_HEIGHT (CDG_SCREEN_HEIGHT-2*CDG_SCREEN_BORDER_HEIGHT)

51
#define CDG_SCREEN_PITCH CDG_SCREEN_WIDTH
Laurent Aimar's avatar
Laurent Aimar committed
52 53 54

struct decoder_sys_t
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
55 56 57 58 59
    uint8_t  color[16][3];
    unsigned i_offseth;
    unsigned i_offsetv;
    uint8_t  screen[CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT];
    uint8_t  *p_screen;
60

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
61
    int      i_packet;
Laurent Aimar's avatar
Laurent Aimar committed
62 63
};

64
#define CDG_PACKET_SIZE 24u
Laurent Aimar's avatar
Laurent Aimar committed
65

66 67 68
#define CDG_COLOR_R_SHIFT  0
#define CDG_COLOR_G_SHIFT  8
#define CDG_COLOR_B_SHIFT 16
Laurent Aimar's avatar
Laurent Aimar committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

static picture_t *Decode( decoder_t *, block_t ** );

static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer );
static int Render( decoder_sys_t *p_cdg, picture_t *p_picture );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
84 85 86 87 88 89 90 91
vlc_module_begin ()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_VCODEC )
    set_description( N_("CDG video decoder") )
    set_capability( "decoder", 1000 )
    set_callbacks( Open, Close )
    add_shortcut( "cdg" )
vlc_module_end ()
Laurent Aimar's avatar
Laurent Aimar committed
92 93 94 95 96 97 98 99 100

/*****************************************************************************
 * Open: probe the decoder and return score
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;

101
    if( p_dec->fmt_in.i_codec != VLC_CODEC_CDG )
Laurent Aimar's avatar
Laurent Aimar committed
102 103 104
        return VLC_EGENERIC;

    /* Allocate the memory needed to store the decoder's structure */
ivoire's avatar
ivoire committed
105
    p_dec->p_sys = p_sys = calloc( 1, sizeof(decoder_sys_t) );
Laurent Aimar's avatar
Laurent Aimar committed
106
    if( !p_sys )
107
        return VLC_ENOMEM;
Laurent Aimar's avatar
Laurent Aimar committed
108 109 110

    /* Init */
    p_sys->p_screen = p_sys->screen;
111
    p_sys->i_packet = 0;
Laurent Aimar's avatar
Laurent Aimar committed
112 113 114 115

    /* Set output properties
     * TODO maybe it would be better to use RV16 or RV24 ? */
    p_dec->fmt_out.i_cat = VIDEO_ES;
116
    p_dec->fmt_out.i_codec = VLC_CODEC_RGB32;
Laurent Aimar's avatar
Laurent Aimar committed
117 118
    p_dec->fmt_out.video.i_width = CDG_DISPLAY_WIDTH;
    p_dec->fmt_out.video.i_height = CDG_DISPLAY_HEIGHT;
Laurent Aimar's avatar
Laurent Aimar committed
119 120
    p_dec->fmt_out.video.i_sar_num = 1;
    p_dec->fmt_out.video.i_sar_den = 1;
Laurent Aimar's avatar
Laurent Aimar committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    p_dec->fmt_out.video.i_rmask = 0xff << CDG_COLOR_R_SHIFT;
    p_dec->fmt_out.video.i_gmask = 0xff << CDG_COLOR_G_SHIFT;
    p_dec->fmt_out.video.i_bmask = 0xff << CDG_COLOR_B_SHIFT;

    /* Set callbacks */
    p_dec->pf_decode_video = Decode;

    return VLC_SUCCESS;
}

/****************************************************************************
 * Decode: the whole thing
 ****************************************************************************
 * This function must be fed with a complete compressed frame.
 ****************************************************************************/
static picture_t *Decode( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    block_t *p_block;
    picture_t *p_pic = NULL;

    if( !pp_block || !*pp_block )
        return NULL;
    p_block = *pp_block;

146
    if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
147 148 149 150 151
    {
        p_sys->i_packet = 0;
        goto exit;
    }

Laurent Aimar's avatar
Laurent Aimar committed
152 153 154 155 156 157 158 159
    /* Decode packet */
    while( p_block->i_buffer >= CDG_PACKET_SIZE )
    {
        DecodePacket( p_sys, p_block->p_buffer, CDG_PACKET_SIZE );
        p_block->i_buffer -= CDG_PACKET_SIZE;
        p_block->p_buffer += CDG_PACKET_SIZE;
    }

160 161 162 163 164 165 166
    /* Only display 25 frame per second (there is 75 packets per second) */
    if( (p_sys->i_packet%3) == 1 )
    {
        /* Get a new picture */
        p_pic = decoder_NewPicture( p_dec );
        if( !p_pic )
            goto exit;
Laurent Aimar's avatar
Laurent Aimar committed
167

168
        Render( p_sys, p_pic );
169
        p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
170
    }
Laurent Aimar's avatar
Laurent Aimar committed
171

172
exit:
173
    block_Release( p_block ); *pp_block = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    return p_pic;
}

/*****************************************************************************
 * Close: decoder destruction
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    decoder_t *p_dec = (decoder_t *)p_this;
    decoder_sys_t *p_sys = p_dec->p_sys;

    free( p_sys );
}

/*****************************************************************************
 * Decoder
 *****************************************************************************/
static void ScreenFill( decoder_sys_t *p_cdg, int sx, int sy, int dx, int dy, int c )
{
    int x, y;
    for( y = sy; y < sy+dy; y++ )
        for( x = sx; x < sx+dx; x++ )
            p_cdg->p_screen[y*CDG_SCREEN_PITCH+x] = c;
}
static int DecodeMemoryPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
{
    const int i_color = p_data[0]&0x0f;
201
#if 0
Laurent Aimar's avatar
Laurent Aimar committed
202
    const int i_repeat= p_data[1]&0x0f;
203
#endif
Laurent Aimar's avatar
Laurent Aimar committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    /* if i_repeat > 0 we could ignore it if we have a reliable stream */
    ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, i_color );
    return 0;
}
static int DecodeBorderPreset( decoder_sys_t *p_cdg, const uint8_t *p_data )
{
    const int i_color = p_data[0]&0x0f;

    /* */
    ScreenFill( p_cdg,   0,   0,
                CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
    ScreenFill( p_cdg,   0, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT,
                CDG_SCREEN_WIDTH, CDG_SCREEN_BORDER_HEIGHT, i_color );
    ScreenFill( p_cdg,   0, CDG_SCREEN_BORDER_HEIGHT,
                CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
    ScreenFill( p_cdg,   CDG_SCREEN_WIDTH-CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_BORDER_HEIGHT,
                CDG_SCREEN_BORDER_WIDTH, CDG_SCREEN_HEIGHT-CDG_SCREEN_BORDER_HEIGHT, i_color );
    return 0;
}

static int DecodeLoadColorTable( decoder_sys_t *p_cdg, const uint8_t *p_data, int i_base )
{
    int n;
    for( n = 0; n < 8; n++ )
    {
        const int c = ((p_data[2*n+0] << 8) | p_data[2*n+1]);
        const int r = (c >> 10)&0x0f;
        const int g = ((c >> 6)&0x0c) | ((c >> 4)&0x03);
        const int b = c &0x0f;

        p_cdg->color[i_base+n][0] = r << 4;
        p_cdg->color[i_base+n][1] = g << 4;
        p_cdg->color[i_base+n][2] = b << 4;
    }
    return 0;
}
static int DecodeTileBlock( decoder_sys_t *p_cdg, const uint8_t *p_data, int doXor )
{
    int p_color[2];
    int sx, sy;
    int x, y;

    p_color[0] = p_data[0] & 0x0f;
    p_color[1] = p_data[1] & 0x0f;

    sy = (p_data[2] & 0x1f)*12;
    sx = (p_data[3] & 0x3f)*6;

    for( y = 0; y < 12; y++ )
    {
        for( x = 0; x < 6; x++ )
        {
            const int idx = ( p_data[4+y] >> (5-x) ) & 0x01;
257

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
258
            unsigned index = (sy+y)*CDG_SCREEN_PITCH+(sx+x);
259 260 261 262 263
            if( index >= CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT )
                return 0;

            uint8_t *p = &p_cdg->p_screen[index];

Laurent Aimar's avatar
Laurent Aimar committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
            if( doXor )
                *p ^= p_color[idx];
            else
                *p = p_color[idx];
        }
    }
    return 0;
}

static int DecodeScroll( decoder_sys_t *p_cdg, const uint8_t *p_data, int b_copy )
{
    uint8_t copy[CDG_SCREEN_PITCH*CDG_SCREEN_HEIGHT];

    uint8_t color = p_data[0]&0x0f;
    int i_shifth;
    int i_shiftv;

    /* */
    p_cdg->i_offseth = p_data[1]&0x7;
    if( p_cdg->i_offseth >= CDG_SCREEN_BORDER_WIDTH )
        p_cdg->i_offseth = CDG_SCREEN_BORDER_WIDTH-1;

    p_cdg->i_offsetv = p_data[2]&0xf;
    if( p_cdg->i_offsetv >= CDG_SCREEN_BORDER_HEIGHT )
        p_cdg->i_offsetv = CDG_SCREEN_BORDER_HEIGHT-1;

    /* */
    switch( (p_data[1] >> 4)&0x3 )
    {
    case 0x01: i_shifth =  6; break;
    case 0x02: i_shifth = -6; break;
    default:
        i_shifth = 0;
        break;
    }
    switch( (p_data[2] >> 4)&0x3 )
    {
    case 0x01: i_shiftv = 12; break;
    case 0x02: i_shiftv =-12; break;
    default:
        i_shiftv = 0;
        break;
    }

    if( i_shifth == 0 && i_shiftv == 0 )
        return 0;

    /* Make a copy of the screen */
    memcpy( copy, p_cdg->screen, sizeof(p_cdg->screen) );

    /* Fill the uncovered part XXX way too much */
    ScreenFill( p_cdg, 0, 0, CDG_SCREEN_WIDTH, CDG_SCREEN_HEIGHT, color );

    /* Copy back */
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
318
    for( unsigned y = 0; y < CDG_SCREEN_HEIGHT; y++ )
Laurent Aimar's avatar
Laurent Aimar committed
319 320
    {
        int dy = i_shiftv + y;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
321
        for( unsigned x = 0; x < CDG_SCREEN_WIDTH; x++ )
Laurent Aimar's avatar
Laurent Aimar committed
322 323 324 325 326
        {
            int dx = i_shifth + x;

            if( b_copy )
            {
Laurent Aimar's avatar
Laurent Aimar committed
327 328
                dy = (dy + CDG_SCREEN_HEIGHT) % CDG_SCREEN_HEIGHT;
                dx = (dx + CDG_SCREEN_WIDTH ) % CDG_SCREEN_WIDTH;
Laurent Aimar's avatar
Laurent Aimar committed
329 330 331
            }
            else
            {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
332 333
                if( dy < 0 || (unsigned)dy >= CDG_SCREEN_HEIGHT ||
                    dx < 0 || (unsigned)dx >= CDG_SCREEN_WIDTH )
Laurent Aimar's avatar
Laurent Aimar committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
                    continue;
            }
            p_cdg->screen[dy*CDG_SCREEN_PITCH+dx] = copy[y*CDG_SCREEN_PITCH+x];
        }
    }
    /* */
    //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 );
    return 0;
}

static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer )
{
    const int i_cmd = p_buffer[0] & 0x3f;
    const int i_instruction = p_buffer[1] & 0x3f;
    const uint8_t *p_data = &p_buffer[4];

    if( i_buffer != CDG_PACKET_SIZE )
        return -1;

353 354
    p_cdg->i_packet++;

Laurent Aimar's avatar
Laurent Aimar committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    /* Handle CDG command only */
    if( i_cmd != 0x09 )
        return 0;

    switch( i_instruction )
    {
        case 1:
            DecodeMemoryPreset( p_cdg, p_data );
            break;
        case 2:
            DecodeBorderPreset( p_cdg, p_data );
            break;
        case 6:
            DecodeTileBlock( p_cdg, p_data, 0 );
            break;
        case 20:
            DecodeScroll( p_cdg, p_data, 0 );
            break;
        case 24:
            DecodeScroll( p_cdg, p_data, 1 );
            break;
        case 28:
            /* FIXME what to do about that one ? */
            //CdgDebug( CDG_LOG_WARNING, "DecodeDefineTransparentColor not implemented\n" );
            //DecodeDefineTransparentColor( p_cdg, p_data );
            break;
        case 30:
            DecodeLoadColorTable( p_cdg, p_data, 0 );
            break;
        case 31:
            DecodeLoadColorTable( p_cdg, p_data, 8 );
            break;
        case 38:
            DecodeTileBlock( p_cdg, p_data, 1 );
            break;
        default:
            break;
    }
    return 0;
}

static void RenderPixel( picture_t *p_picture, int x, int y, uint32_t c )
{
    const int i_plane = 0;
    const int i_pitch = p_picture->p[i_plane].i_pitch;
    uint32_t *s = (uint32_t*)&p_picture->p[i_plane].p_pixels[y*i_pitch + x*sizeof(uint32_t)];
    *s = c;
}
static uint32_t RenderRGB( int r, int g, int b )
{
    return ( r << CDG_COLOR_R_SHIFT ) | ( g << CDG_COLOR_G_SHIFT ) | ( b << CDG_COLOR_B_SHIFT );
}

static int Render( decoder_sys_t *p_cdg, picture_t *p_picture )
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
410
    for( unsigned y = 0; y < CDG_DISPLAY_HEIGHT; y++ )
Laurent Aimar's avatar
Laurent Aimar committed
411
    {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
412
        for( unsigned x = 0; x < CDG_DISPLAY_WIDTH; x++ )
Laurent Aimar's avatar
Laurent Aimar committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426
        {
            const int sx = x + p_cdg->i_offseth + CDG_SCREEN_BORDER_WIDTH;
            const int sy = y + p_cdg->i_offsetv + CDG_SCREEN_BORDER_HEIGHT;
            uint8_t cidx = p_cdg->p_screen[sy*CDG_SCREEN_PITCH +sx];
            uint8_t r = p_cdg->color[cidx][0];
            uint8_t g = p_cdg->color[cidx][1];
            uint8_t b = p_cdg->color[cidx][2];

            RenderPixel( p_picture, x, y, RenderRGB( r, g, b ) );
        }
    }
    return 0;
}