cdg.c 13.5 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
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
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
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

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

76
static int Decode( decoder_t *, block_t * );
Laurent Aimar's avatar
Laurent Aimar committed
77 78

static int DecodePacket( decoder_sys_t *p_cdg, uint8_t *p_buffer, int i_buffer );
79
static void Flush( decoder_t * );
Laurent Aimar's avatar
Laurent Aimar committed
80 81 82 83 84
static int Render( decoder_sys_t *p_cdg, picture_t *p_picture );

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

/*****************************************************************************
 * 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;

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

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

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

    /* Set output properties
     * TODO maybe it would be better to use RV16 or RV24 ? */
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;
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
    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 */
126 127
    p_dec->pf_decode = Decode;
    p_dec->pf_flush  = Flush;
Laurent Aimar's avatar
Laurent Aimar committed
128 129 130 131

    return VLC_SUCCESS;
}

132 133 134 135 136 137 138 139 140 141
/*****************************************************************************
 * Flush:
 *****************************************************************************/
static void Flush( decoder_t *p_dec )
{
    decoder_sys_t *p_sys = p_dec->p_sys;

    p_sys->i_packet = 0;
}

Laurent Aimar's avatar
Laurent Aimar committed
142 143 144 145 146
/****************************************************************************
 * Decode: the whole thing
 ****************************************************************************
 * This function must be fed with a complete compressed frame.
 ****************************************************************************/
147
static int Decode( decoder_t *p_dec, block_t *p_block )
Laurent Aimar's avatar
Laurent Aimar committed
148 149 150 151
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    picture_t *p_pic = NULL;

152 153
    if( !p_block ) /* No Drain */
        return VLCDEC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
154

155
    if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
156
    {
157
        Flush( p_dec );
158 159 160
        goto exit;
    }

Laurent Aimar's avatar
Laurent Aimar committed
161 162 163 164 165 166 167 168
    /* 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;
    }

169
    /* Only display 25 frame per second (there is 75 packets per second) */
Zhao Zhili's avatar
Zhao Zhili committed
170
    if( (p_sys->i_packet%3) == 1 && p_block->i_pts == p_block->i_dts )
171 172
    {
        /* Get a new picture */
173 174
        if( decoder_UpdateVideoFormat( p_dec ) )
            goto exit;
175 176 177
        p_pic = decoder_NewPicture( p_dec );
        if( !p_pic )
            goto exit;
Laurent Aimar's avatar
Laurent Aimar committed
178

179
        Render( p_sys, p_pic );
180
        p_pic->date = p_block->i_pts > VLC_TS_INVALID ? p_block->i_pts : p_block->i_dts;
181
    }
Laurent Aimar's avatar
Laurent Aimar committed
182

183
exit:
184
    block_Release( p_block );
185 186 187
    if( p_pic != NULL )
        decoder_QueueVideo( p_dec, p_pic );
    return VLCDEC_SUCCESS;
Laurent Aimar's avatar
Laurent Aimar committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
}

/*****************************************************************************
 * 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;
214
#if 0
Laurent Aimar's avatar
Laurent Aimar committed
215
    const int i_repeat= p_data[1]&0x0f;
216
#endif
Laurent Aimar's avatar
Laurent Aimar committed
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 257 258 259 260 261 262 263 264 265 266 267 268 269
    /* 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;
270

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
271
            unsigned index = (sy+y)*CDG_SCREEN_PITCH+(sx+x);
272 273 274 275 276
            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
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 318 319 320 321 322 323 324 325 326 327 328 329 330
            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
331
    for( unsigned y = 0; y < CDG_SCREEN_HEIGHT; y++ )
Laurent Aimar's avatar
Laurent Aimar committed
332 333
    {
        int dy = i_shiftv + y;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
334
        for( unsigned x = 0; x < CDG_SCREEN_WIDTH; x++ )
Laurent Aimar's avatar
Laurent Aimar committed
335 336 337 338 339
        {
            int dx = i_shifth + x;

            if( b_copy )
            {
Laurent Aimar's avatar
Laurent Aimar committed
340 341
                dy = (dy + CDG_SCREEN_HEIGHT) % CDG_SCREEN_HEIGHT;
                dx = (dx + CDG_SCREEN_WIDTH ) % CDG_SCREEN_WIDTH;
Laurent Aimar's avatar
Laurent Aimar committed
342 343 344
            }
            else
            {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
345 346
                if( dy < 0 || (unsigned)dy >= CDG_SCREEN_HEIGHT ||
                    dx < 0 || (unsigned)dx >= CDG_SCREEN_WIDTH )
Laurent Aimar's avatar
Laurent Aimar committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
                    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;

366 367
    p_cdg->i_packet++;

Laurent Aimar's avatar
Laurent Aimar committed
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 410 411 412 413 414 415 416 417 418 419 420 421 422
    /* 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
423
    for( unsigned y = 0; y < CDG_DISPLAY_HEIGHT; y++ )
Laurent Aimar's avatar
Laurent Aimar committed
424
    {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
425
        for( unsigned x = 0; x < CDG_DISPLAY_WIDTH; x++ )
Laurent Aimar's avatar
Laurent Aimar committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439
        {
            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;
}