/*****************************************************************************
* parse.c: SPU parser
*****************************************************************************
- * Copyright (C) 2000-2001 VideoLAN
- * $Id: parse.c,v 1.2 2002/10/17 08:24:12 sam Exp $
+ * Copyright (C) 2000-2001, 2005, 2006 the VideoLAN team
+ * $Id$
*
- * Authors: Samuel Hocevar <sam@zoy.org>
+ * Authors: Sam Hocevar <sam@zoy.org>
+ * Laurent Aimar <fenrir@via.ecp.fr>
+ * Gildas Bazin <gbazin@videolan.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <stdlib.h> /* malloc(), free() */
-#include <string.h> /* memcpy(), memset() */
-
-#include <vlc/vlc.h>
-#include <vlc/vout.h>
-#include <vlc/decoder.h>
-
-#ifdef HAVE_UNISTD_H
-# include <unistd.h> /* getpid() */
+#ifdef HAVE_CONFIG_H
+# include "config.h"
#endif
-#ifdef WIN32 /* getpid() for win32 is located in process.h */
-# include <process.h>
-#endif
+#include <vlc_common.h>
+#include <vlc_codec.h>
+#include <vlc_input.h>
#include "spudec.h"
/*****************************************************************************
* Local prototypes.
*****************************************************************************/
-static int ParseControlSequences( spudec_thread_t *, subpicture_t * );
-static int ParseRLE ( spudec_thread_t *, subpicture_t *, u8 * );
+typedef struct
+{
+ int i_width;
+ int i_height;
+ int i_x;
+ int i_y;
+} spu_properties_t;
+
+typedef struct
+{
+ int pi_offset[2]; /* byte offsets to data */
+ uint16_t *p_data;
+
+ /* Color information */
+ bool b_palette;
+ uint8_t pi_alpha[4];
+ uint8_t pi_yuv[4][3];
+
+ /* Auto crop fullscreen subtitles */
+ bool b_auto_crop;
+ int i_y_top_offset;
+ int i_y_bottom_offset;
+
+} subpicture_data_t;
+
+static int ParseControlSeq( decoder_t *, subpicture_t *, subpicture_data_t *,
+ spu_properties_t *, mtime_t i_pts );
+static int ParseRLE ( decoder_t *, subpicture_data_t *,
+ const spu_properties_t * );
+static void Render ( decoder_t *, subpicture_t *, subpicture_data_t *,
+ const spu_properties_t * );
/*****************************************************************************
* AddNibble: read a nibble from a source packet and add it to our integer.
*****************************************************************************/
static inline unsigned int AddNibble( unsigned int i_code,
- u8 *p_src, int *pi_index )
+ const uint8_t *p_src, unsigned int *pi_index )
{
if( *pi_index & 0x1 )
{
}
}
-/*****************************************************************************
- * SyncPacket: get in sync with the stream
- *****************************************************************************
- * This function makes a few sanity checks and returns 0 if it looks like we
- * are at the beginning of a subpicture packet.
- *****************************************************************************/
-int E_(SyncPacket)( spudec_thread_t *p_spudec )
-{
- /* Re-align the buffer on an 8-bit boundary */
- RealignBits( &p_spudec->bit_stream );
-
- /* The total SPU packet size, often bigger than a PS packet */
- p_spudec->i_spu_size = GetBits( &p_spudec->bit_stream, 16 );
-
- /* The RLE stuff size (remove 4 because we just read 32 bits) */
- p_spudec->i_rle_size = ShowBits( &p_spudec->bit_stream, 16 ) - 4;
-
- /* If the values we got are a bit strange, skip packet */
- if( !p_spudec->i_spu_size
- || ( p_spudec->i_rle_size >= p_spudec->i_spu_size ) )
- {
- return VLC_EGENERIC;
- }
-
- RemoveBits( &p_spudec->bit_stream, 16 );
-
- return VLC_SUCCESS;
-}
-
/*****************************************************************************
* ParsePacket: parse an SPU packet and send it to the video output
*****************************************************************************
* This function parses the SPU packet and, if valid, sends it to the
* video output.
*****************************************************************************/
-void E_(ParsePacket)( spudec_thread_t *p_spudec )
+subpicture_t * ParsePacket( decoder_t *p_dec )
{
- subpicture_t * p_spu;
- u8 * p_src;
- unsigned int i_offset;
-
- msg_Dbg( p_spudec->p_fifo, "trying to gather a 0x%.2x long subtitle",
- p_spudec->i_spu_size );
-
- /* We cannot display a subpicture with no date */
- if( p_spudec->p_fifo->p_first->i_pts == 0 )
- {
- msg_Warn( p_spudec->p_fifo, "subtitle without a date" );
- return;
- }
+ decoder_sys_t *p_sys = p_dec->p_sys;
+ subpicture_t *p_spu;
+ subpicture_data_t spu_data;
+ spu_properties_t spu_properties;
/* Allocate the subpicture internal data. */
- p_spu = vout_CreateSubPicture( p_spudec->p_vout, MEMORY_SUBPICTURE,
- sizeof( subpicture_sys_t )
- + p_spudec->i_rle_size * 4 );
- /* Rationale for the "p_spudec->i_rle_size * 4": we are going to
- * expand the RLE stuff so that we won't need to read nibbles later
- * on. This will speed things up a lot. Plus, we'll only need to do
- * this stupid interlacing stuff once. */
-
- if( p_spu == NULL )
- {
- return;
- }
-
- /* Fill the p_spu structure */
- p_spu->pf_render = E_(RenderSPU);
- p_spu->p_sys->p_data = (u8*)p_spu->p_sys + sizeof( subpicture_sys_t );
- p_spu->p_sys->b_palette = VLC_FALSE;
-
- p_spu->p_sys->pi_alpha[0] = 0x00;
- p_spu->p_sys->pi_alpha[1] = 0x0f;
- p_spu->p_sys->pi_alpha[2] = 0x0f;
- p_spu->p_sys->pi_alpha[3] = 0x0f;
+ p_spu = decoder_NewSubpicture( p_dec );
+ if( !p_spu ) return NULL;
- /* Get display time now. If we do it later, we may miss the PTS. */
- p_spu->p_sys->i_pts = p_spudec->p_fifo->p_first->i_pts;
-
- /* Allocate the temporary buffer we will parse */
- p_src = malloc( p_spudec->i_rle_size );
-
- if( p_src == NULL )
- {
- msg_Err( p_spudec->p_fifo, "out of memory" );
- vout_DestroySubPicture( p_spudec->p_vout, p_spu );
- return;
- }
-
- /* Get RLE data */
- for( i_offset = 0; i_offset < p_spudec->i_rle_size;
- i_offset += SPU_CHUNK_SIZE )
- {
- GetChunk( &p_spudec->bit_stream, p_src + i_offset,
- ( i_offset + SPU_CHUNK_SIZE < p_spudec->i_rle_size ) ?
- SPU_CHUNK_SIZE : p_spudec->i_rle_size - i_offset );
-
- /* Abort subtitle parsing if we were requested to stop */
- if( p_spudec->p_fifo->b_die )
- {
- free( p_src );
- vout_DestroySubPicture( p_spudec->p_vout, p_spu );
- return;
- }
- }
-
-#if 0
- /* Dump the subtitle info */
- intf_WarnHexDump( 5, p_spu->p_sys->p_data, p_spudec->i_rle_size );
-#endif
+ p_spu->i_original_picture_width =
+ p_dec->fmt_in.subs.spu.i_original_frame_width;
+ p_spu->i_original_picture_height =
+ p_dec->fmt_in.subs.spu.i_original_frame_height;
/* Getting the control part */
- if( ParseControlSequences( p_spudec, p_spu ) )
+ if( ParseControlSeq( p_dec, p_spu, &spu_data, &spu_properties, p_sys->i_pts ) )
{
/* There was a parse error, delete the subpicture */
- free( p_src );
- vout_DestroySubPicture( p_spudec->p_vout, p_spu );
- return;
+ decoder_DeleteSubpicture( p_dec, p_spu );
+ return NULL;
}
- /* At this point, no more GetBit() command is needed, so we have all
- * the data we need to tell whether the subtitle is valid. Thus we
- * try to display it and we ignore b_die. */
-
- if( ParseRLE( p_spudec, p_spu, p_src ) )
+ /* we are going to expand the RLE stuff so that we won't need to read
+ * nibbles later on. This will speed things up a lot. Plus, we'll only
+ * need to do this stupid interlacing stuff once.
+ *
+ * Rationale for the "p_spudec->i_rle_size * 4*sizeof(*spu_data.p_data)":
+ * one byte gaves two nibbles and may be used twice (once per field)
+ * generating 4 codes.
+ */
+ spu_data.p_data = malloc( sizeof(*spu_data.p_data) * 2 * 2 * p_sys->i_rle_size );
+
+ /* We try to display it */
+ if( ParseRLE( p_dec, &spu_data, &spu_properties ) )
{
/* There was a parse error, delete the subpicture */
- free( p_src );
- vout_DestroySubPicture( p_spudec->p_vout, p_spu );
- return;
+ decoder_DeleteSubpicture( p_dec, p_spu );
+ free( spu_data.p_data );
+ return NULL;
}
- msg_Dbg( p_spudec->p_fifo, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
- p_spudec->i_spu_size,
- p_spu->p_sys->pi_offset[0], p_spu->p_sys->pi_offset[1] );
+#ifdef DEBUG_SPUDEC
+ msg_Dbg( p_dec, "total size: 0x%x, RLE offsets: 0x%x 0x%x",
+ p_sys->i_spu_size,
+ spu_data.pi_offset[0], spu_data.pi_offset[1] );
+#endif
- /* SPU is finished - we can ask the video output to display it */
- vout_DisplaySubPicture( p_spudec->p_vout, p_spu );
+ Render( p_dec, p_spu, &spu_data, &spu_properties );
+ free( spu_data.p_data );
- /* Clean up */
- free( p_src );
+ return p_spu;
}
/*****************************************************************************
- * ParseControlSequences: parse all SPU control sequences
+ * ParseControlSeq: parse all SPU control sequences
*****************************************************************************
* This is the most important part in SPU decoding. We get dates, palette
* information, coordinates, and so on. For more information on the
* subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
*****************************************************************************/
-static int ParseControlSequences( spudec_thread_t *p_spudec,
- subpicture_t * p_spu )
+static int ParseControlSeq( decoder_t *p_dec, subpicture_t *p_spu,
+ subpicture_data_t *p_spu_data, spu_properties_t *p_spu_properties, mtime_t i_pts )
{
+ decoder_sys_t *p_sys = p_dec->p_sys;
+
/* Our current index in the SPU packet */
- int i_index = p_spudec->i_rle_size + 4;
+ unsigned int i_index;
/* The next start-of-control-sequence index and the previous one */
- int i_next_seq = 0, i_cur_seq = 0;
+ unsigned int i_next_seq = 0, i_cur_seq = 0;
/* Command and date */
- u8 i_command = SPU_CMD_END;
+ uint8_t i_command = SPU_CMD_END;
mtime_t date = 0;
+ bool b_cmd_offset = false;
+ bool b_cmd_alpha = false;
+ subpicture_data_t spu_data_cmd;
+
+ if( !p_spu || !p_spu_data )
+ return VLC_EGENERIC;
- int i, pi_alpha[4];
+ /* Create working space for spu data */
+ memset( &spu_data_cmd, 0, sizeof(spu_data_cmd) );
+ spu_data_cmd.pi_offset[0] = -1;
+ spu_data_cmd.pi_offset[1] = -1;
+ spu_data_cmd.p_data = NULL;
+ spu_data_cmd.b_palette = false;
+ spu_data_cmd.b_auto_crop = false;
+ spu_data_cmd.i_y_top_offset = 0;
+ spu_data_cmd.i_y_bottom_offset = 0;
+ spu_data_cmd.pi_alpha[0] = 0x00;
+ spu_data_cmd.pi_alpha[1] = 0x0f;
+ spu_data_cmd.pi_alpha[2] = 0x0f;
+ spu_data_cmd.pi_alpha[3] = 0x0f;
/* Initialize the structure */
p_spu->i_start = p_spu->i_stop = 0;
- p_spu->b_ephemer = VLC_FALSE;
+ p_spu->b_ephemer = false;
- do
+ memset( p_spu_properties, 0, sizeof(*p_spu_properties) );
+
+ /* */
+ *p_spu_data = spu_data_cmd;
+
+ for( i_index = 4 + p_sys->i_rle_size; i_index < p_sys->i_spu_size ; )
{
/* If we just read a command sequence, read the next one;
* otherwise, go on with the commands of the current sequence. */
if( i_command == SPU_CMD_END )
{
+ if( i_index + 4 > p_sys->i_spu_size )
+ {
+ msg_Err( p_dec, "overflow in SPU command sequence" );
+ return VLC_EGENERIC;
+ }
+
+ /* */
+ b_cmd_offset = false;
+ b_cmd_alpha = false;
/* Get the control sequence date */
- date = GetBits( &p_spudec->bit_stream, 16 );
-
+ date = (mtime_t)GetWBE( &p_sys->buffer[i_index] ) * 11000;
+
/* Next offset */
i_cur_seq = i_index;
- i_next_seq = GetBits( &p_spudec->bit_stream, 16 );
-
+ i_next_seq = GetWBE( &p_sys->buffer[i_index+2] );
+
+ if( i_next_seq > p_sys->i_spu_size )
+ {
+ msg_Err( p_dec, "overflow in SPU next command sequence" );
+ return VLC_EGENERIC;
+ }
+
/* Skip what we just read */
i_index += 4;
}
-
- i_command = GetBits( &p_spudec->bit_stream, 8 );
- i_index++;
-
+
+ i_command = p_sys->buffer[i_index];
+
switch( i_command )
{
case SPU_CMD_FORCE_DISPLAY: /* 00 (force displaying) */
- p_spu->i_start = p_spu->p_sys->i_pts + ( date * 11000 );
- p_spu->b_ephemer = VLC_TRUE;
+ p_spu->i_start = i_pts + date;
+ p_spu->b_ephemer = true;
+ i_index += 1;
break;
/* Convert the dates in seconds to PTS values */
case SPU_CMD_START_DISPLAY: /* 01 (start displaying) */
- p_spu->i_start = p_spu->p_sys->i_pts + ( date * 11000 );
+ p_spu->i_start = i_pts + date;
+ i_index += 1;
break;
case SPU_CMD_STOP_DISPLAY: /* 02 (stop displaying) */
- p_spu->i_stop = p_spu->p_sys->i_pts + ( date * 11000 );
+ p_spu->i_stop = i_pts + date;
+ i_index += 1;
break;
case SPU_CMD_SET_PALETTE:
-
/* 03xxxx (palette) */
- if( p_spudec->p_fifo->p_demux_data
- && *(int*)p_spudec->p_fifo->p_demux_data == 0xBeeF )
+ if( i_index + 3 > p_sys->i_spu_size )
{
- u32 i_color;
+ msg_Err( p_dec, "overflow in SPU command" );
+ return VLC_EGENERIC;
+ }
+
+ if( p_dec->fmt_in.subs.spu.palette[0] == 0xBeeF )
+ {
+ unsigned int idx[4];
+ int i;
+
+ spu_data_cmd.b_palette = true;
+
+ idx[0] = (p_sys->buffer[i_index+1]>>4)&0x0f;
+ idx[1] = (p_sys->buffer[i_index+1])&0x0f;
+ idx[2] = (p_sys->buffer[i_index+2]>>4)&0x0f;
+ idx[3] = (p_sys->buffer[i_index+2])&0x0f;
- p_spu->p_sys->b_palette = VLC_TRUE;
for( i = 0; i < 4 ; i++ )
{
- i_color = ((u32*)((char*)p_spudec->p_fifo->
- p_demux_data + sizeof(int)))[
- GetBits(&p_spudec->bit_stream, 4) ];
+ uint32_t i_color = p_dec->fmt_in.subs.spu.palette[1+idx[i]];
/* FIXME: this job should be done sooner */
-#ifndef WORDS_BIGENDIAN
- p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>16) & 0xff;
- p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>0) & 0xff;
- p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>8) & 0xff;
-#else
- p_spu->p_sys->pi_yuv[3-i][0] = (i_color>>8) & 0xff;
- p_spu->p_sys->pi_yuv[3-i][1] = (i_color>>24) & 0xff;
- p_spu->p_sys->pi_yuv[3-i][2] = (i_color>>16) & 0xff;
-#endif
+ spu_data_cmd.pi_yuv[3-i][0] = (i_color>>16) & 0xff;
+ spu_data_cmd.pi_yuv[3-i][1] = (i_color>>0) & 0xff;
+ spu_data_cmd.pi_yuv[3-i][2] = (i_color>>8) & 0xff;
}
}
- else
- {
- RemoveBits( &p_spudec->bit_stream, 16 );
- }
- i_index += 2;
+ i_index += 3;
break;
case SPU_CMD_SET_ALPHACHANNEL: /* 04xxxx (alpha channel) */
- pi_alpha[3] = GetBits( &p_spudec->bit_stream, 4 );
- pi_alpha[2] = GetBits( &p_spudec->bit_stream, 4 );
- pi_alpha[1] = GetBits( &p_spudec->bit_stream, 4 );
- pi_alpha[0] = GetBits( &p_spudec->bit_stream, 4 );
-
- /* Ignore blank alpha palette. Sometimes spurious blank
- * alpha palettes are present - dunno why. */
- if( pi_alpha[0] | pi_alpha[1] | pi_alpha[2] | pi_alpha[3] )
+ if( i_index + 3 > p_sys->i_spu_size )
{
- p_spu->p_sys->pi_alpha[0] = pi_alpha[0];
- p_spu->p_sys->pi_alpha[1] = pi_alpha[1];
- p_spu->p_sys->pi_alpha[2] = pi_alpha[2];
- p_spu->p_sys->pi_alpha[3] = pi_alpha[3];
+ msg_Err( p_dec, "overflow in SPU command" );
+ return VLC_EGENERIC;
}
- else
- {
- msg_Warn( p_spudec->p_fifo, "ignoring blank alpha palette" );
+
+ if(!p_sys->b_disabletrans)
+ { /* If we want to use original transparency values */
+ b_cmd_alpha = true;
+ spu_data_cmd.pi_alpha[3] = (p_sys->buffer[i_index+1]>>4)&0x0f;
+ spu_data_cmd.pi_alpha[2] = (p_sys->buffer[i_index+1])&0x0f;
+ spu_data_cmd.pi_alpha[1] = (p_sys->buffer[i_index+2]>>4)&0x0f;
+ spu_data_cmd.pi_alpha[0] = (p_sys->buffer[i_index+2])&0x0f;
}
- i_index += 2;
+ i_index += 3;
break;
case SPU_CMD_SET_COORDINATES: /* 05xxxyyyxxxyyy (coordinates) */
- p_spu->i_x = GetBits( &p_spudec->bit_stream, 12 );
- p_spu->i_width = GetBits( &p_spudec->bit_stream, 12 )
- - p_spu->i_x + 1;
+ if( i_index + 7 > p_sys->i_spu_size )
+ {
+ msg_Err( p_dec, "overflow in SPU command" );
+ return VLC_EGENERIC;
+ }
+
+ p_spu_properties->i_x = (p_sys->buffer[i_index+1]<<4)|
+ ((p_sys->buffer[i_index+2]>>4)&0x0f);
+ p_spu_properties->i_width = (((p_sys->buffer[i_index+2]&0x0f)<<8)|
+ p_sys->buffer[i_index+3]) - p_spu_properties->i_x + 1;
+
+ p_spu_properties->i_y = (p_sys->buffer[i_index+4]<<4)|
+ ((p_sys->buffer[i_index+5]>>4)&0x0f);
+ p_spu_properties->i_height = (((p_sys->buffer[i_index+5]&0x0f)<<8)|
+ p_sys->buffer[i_index+6]) - p_spu_properties->i_y + 1;
- p_spu->i_y = GetBits( &p_spudec->bit_stream, 12 );
- p_spu->i_height = GetBits( &p_spudec->bit_stream, 12 )
- - p_spu->i_y + 1;
+ /* Auto crop fullscreen subtitles */
+ if( p_spu_properties->i_height > 250 )
+ p_spu_data->b_auto_crop = true;
- i_index += 6;
+ i_index += 7;
break;
case SPU_CMD_SET_OFFSETS: /* 06xxxxyyyy (byte offsets) */
- p_spu->p_sys->pi_offset[0] =
- GetBits( &p_spudec->bit_stream, 16 ) - 4;
-
- p_spu->p_sys->pi_offset[1] =
- GetBits( &p_spudec->bit_stream, 16 ) - 4;
+ if( i_index + 5 > p_sys->i_spu_size )
+ {
+ msg_Err( p_dec, "overflow in SPU command" );
+ return VLC_EGENERIC;
+ }
- i_index += 4;
+ b_cmd_offset = true;
+ p_spu_data->pi_offset[0] = GetWBE(&p_sys->buffer[i_index+1]) - 4;
+ p_spu_data->pi_offset[1] = GetWBE(&p_sys->buffer[i_index+3]) - 4;
+ i_index += 5;
break;
case SPU_CMD_END: /* ff (end) */
+ if( b_cmd_offset )
+ {
+ /* It seems that palette and alpha from the block having
+ * the cmd offset have to be used
+ * XXX is it all ? */
+ p_spu_data->b_palette = spu_data_cmd.b_palette;
+ if( spu_data_cmd.b_palette )
+ memcpy( p_spu_data->pi_yuv, spu_data_cmd.pi_yuv, sizeof(spu_data_cmd.pi_yuv) );
+ if( b_cmd_alpha )
+ memcpy( p_spu_data->pi_alpha, spu_data_cmd.pi_alpha, sizeof(spu_data_cmd.pi_alpha) );
+ }
+
+ i_index += 1;
break;
default: /* xx (unknown command) */
- msg_Err( p_spudec->p_fifo, "unknown command 0x%.2x",
- i_command );
- return VLC_EGENERIC;
- }
-
- /* We need to check for quit commands here */
- if( p_spudec->p_fifo->b_die )
- {
- return VLC_EGENERIC;
+ msg_Warn( p_dec, "unknown SPU command 0x%.2x", i_command );
+ if( i_index + 1 < i_next_seq )
+ {
+ /* There is at least one other command sequence */
+ if( p_sys->buffer[i_next_seq - 1] == SPU_CMD_END )
+ {
+ /* This is consistent. Skip to that command sequence. */
+ i_index = i_next_seq;
+ }
+ else
+ {
+ /* There were other commands. */
+ msg_Warn( p_dec, "cannot recover, dropping subtitle" );
+ return VLC_EGENERIC;
+ }
+ }
+ else
+ {
+ /* We were in the last command sequence. Stop parsing by
+ * pretending we met an SPU_CMD_END command. */
+ i_command = SPU_CMD_END;
+ i_index++;
+ }
}
- } while( i_command != SPU_CMD_END || i_index == i_next_seq );
+ /* */
+ if( i_command == SPU_CMD_END && i_index != i_next_seq )
+ break;
+ }
/* Check that the next sequence index matches the current one */
if( i_next_seq != i_cur_seq )
{
- msg_Err( p_spudec->p_fifo, "index mismatch (0x%.4x != 0x%.4x)",
- i_next_seq, i_cur_seq );
+ msg_Err( p_dec, "index mismatch (0x%.4x != 0x%.4x)",
+ i_next_seq, i_cur_seq );
+ return VLC_EGENERIC;
+ }
+
+ if( i_index > p_sys->i_spu_size )
+ {
+ msg_Err( p_dec, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
+ i_index, p_sys->i_spu_size );
return VLC_EGENERIC;
}
- if( i_index > p_spudec->i_spu_size )
+ const int i_spu_size = p_sys->i_spu - 4;
+ if( p_spu_data->pi_offset[0] < 0 || p_spu_data->pi_offset[0] >= i_spu_size ||
+ p_spu_data->pi_offset[1] < 0 || p_spu_data->pi_offset[1] >= i_spu_size )
{
- msg_Err( p_spudec->p_fifo, "uh-oh, we went too far (0x%.4x > 0x%.4x)",
- i_index, p_spudec->i_spu_size );
+ msg_Err( p_dec, "invalid offset values" );
return VLC_EGENERIC;
}
if( !p_spu->i_start )
{
- msg_Err( p_spudec->p_fifo, "no `start display' command" );
+ msg_Err( p_dec, "no `start display' command" );
+ return VLC_EGENERIC;
}
- if( !p_spu->i_stop && !p_spu->b_ephemer )
+ if( p_spu->i_stop <= p_spu->i_start && !p_spu->b_ephemer )
{
/* This subtitle will live for 5 seconds or until the next subtitle */
- p_spu->i_stop = p_spu->i_start + 500 * 11000;
- p_spu->b_ephemer = VLC_TRUE;
+ p_spu->i_stop = p_spu->i_start + (mtime_t)500 * 11000;
+ p_spu->b_ephemer = true;
}
/* Get rid of padding bytes */
- switch( p_spudec->i_spu_size - i_index )
+ if( p_sys->i_spu_size > i_index + 1 )
{
- /* Zero or one padding byte, quite usual */
- case 1:
- RemoveBits( &p_spudec->bit_stream, 8 );
- i_index++;
- case 0:
- break;
-
- /* More than one padding byte - this is very strange, but
- * we can deal with it */
- default:
- msg_Warn( p_spudec->p_fifo,
- "%i padding bytes, we usually get 0 or 1 of them",
- p_spudec->i_spu_size - i_index );
-
- while( i_index < p_spudec->i_spu_size )
- {
- RemoveBits( &p_spudec->bit_stream, 8 );
- i_index++;
- }
-
- break;
+ /* Zero or one padding byte are quite usual
+ * More than one padding byte - this is very strange, but
+ * we can ignore them. */
+ msg_Warn( p_dec, "%i padding bytes, we usually get 0 or 1 of them",
+ p_sys->i_spu_size - i_index );
}
/* Successfully parsed ! */
* convenient structure for later decoding. For more information on the
* subtitles format, see http://sam.zoy.org/doc/dvd/subtitles/index.html
*****************************************************************************/
-static int ParseRLE( spudec_thread_t *p_spudec,
- subpicture_t * p_spu, u8 * p_src )
+static int ParseRLE( decoder_t *p_dec,
+ subpicture_data_t *p_spu_data,
+ const spu_properties_t *p_spu_properties )
{
- unsigned int i_code;
+ decoder_sys_t *p_sys = p_dec->p_sys;
- unsigned int i_width = p_spu->i_width;
- unsigned int i_height = p_spu->i_height;
+ const unsigned int i_width = p_spu_properties->i_width;
+ const unsigned int i_height = p_spu_properties->i_height;
unsigned int i_x, i_y;
- u16 *p_dest = (u16 *)p_spu->p_sys->p_data;
+ uint16_t *p_dest = p_spu_data->p_data;
/* The subtitles are interlaced, we need two offsets */
unsigned int i_id = 0; /* Start on the even SPU layer */
unsigned int pi_table[ 2 ];
unsigned int *pi_offset;
- vlc_bool_t b_empty_top = VLC_TRUE,
- b_empty_bottom = VLC_FALSE;
- unsigned int i_skipped_top = 0,
- i_skipped_bottom = 0;
+ /* Cropping */
+ bool b_empty_top = true;
+ unsigned int i_skipped_top = 0, i_skipped_bottom = 0;
+ unsigned int i_transparent_code = 0;
/* Colormap statistics */
int i_border = -1;
int stats[4]; stats[0] = stats[1] = stats[2] = stats[3] = 0;
- pi_table[ 0 ] = p_spu->p_sys->pi_offset[ 0 ] << 1;
- pi_table[ 1 ] = p_spu->p_sys->pi_offset[ 1 ] << 1;
+ pi_table[ 0 ] = p_spu_data->pi_offset[ 0 ] << 1;
+ pi_table[ 1 ] = p_spu_data->pi_offset[ 1 ] << 1;
for( i_y = 0 ; i_y < i_height ; i_y++ )
{
+ unsigned int i_code;
pi_offset = pi_table + i_id;
for( i_x = 0 ; i_x < i_width ; i_x += i_code >> 2 )
{
- i_code = AddNibble( 0, p_src, pi_offset );
-
- if( i_code < 0x04 )
+ i_code = 0;
+ for( unsigned int i_min = 1; i_min <= 0x40 && i_code < i_min; i_min <<= 2 )
{
- i_code = AddNibble( i_code, p_src, pi_offset );
-
- if( i_code < 0x10 )
+ if( (*pi_offset >> 1) >= p_sys->i_spu_size )
{
- i_code = AddNibble( i_code, p_src, pi_offset );
-
- if( i_code < 0x040 )
- {
- i_code = AddNibble( i_code, p_src, pi_offset );
-
- if( i_code < 0x0100 )
- {
- /* If the 14 first bits are set to 0, then it's a
- * new line. We emulate it. */
- if( i_code < 0x0004 )
- {
- i_code |= ( i_width - i_x ) << 2;
- }
- else
- {
- /* We have a boo boo ! */
- msg_Err( p_spudec->p_fifo, "unknown RLE code "
- "0x%.4x", i_code );
- return VLC_EGENERIC;
- }
- }
- }
+ msg_Err( p_dec, "out of bounds while reading rle" );
+ return VLC_EGENERIC;
}
+ i_code = AddNibble( i_code, &p_sys->buffer[4], pi_offset );
+ }
+ if( i_code < 0x0004 )
+ {
+ /* If the 14 first bits are set to 0, then it's a
+ * new line. We emulate it. */
+ i_code |= ( i_width - i_x ) << 2;
}
if( ( (i_code >> 2) + i_x + i_y * i_width ) > i_height * i_width )
{
- msg_Err( p_spudec->p_fifo,
- "out of bounds, %i at (%i,%i) is out of %ix%i",
+ msg_Err( p_dec, "out of bounds, %i at (%i,%i) is out of %ix%i",
i_code >> 2, i_x, i_y, i_width, i_height );
return VLC_EGENERIC;
}
/* Try to find the border color */
- if( p_spu->p_sys->pi_alpha[ i_code & 0x3 ] != 0x00 )
+ if( p_spu_data->pi_alpha[ i_code & 0x3 ] != 0x00 )
{
i_border = i_code & 0x3;
stats[i_border] += i_code >> 2;
}
- if( (i_code >> 2) == i_width
- && p_spu->p_sys->pi_alpha[ i_code & 0x3 ] == 0x00 )
+ /* Auto crop subtitles (a lot more optimized) */
+ if( p_spu_data->b_auto_crop )
{
- if( b_empty_top )
+ if( !i_y )
+ {
+ /* We assume that if the first line is transparent, then
+ * it is using the palette index for the
+ * (background) transparent color */
+ if( (i_code >> 2) == i_width &&
+ p_spu_data->pi_alpha[ i_code & 0x3 ] == 0x00 )
+ {
+ i_transparent_code = i_code;
+ }
+ else
+ {
+ p_spu_data->b_auto_crop = false;
+ }
+ }
+
+ if( i_code == i_transparent_code )
{
- /* This is a blank top line, we skip it */
- i_skipped_top++;
+ if( b_empty_top )
+ {
+ /* This is a blank top line, we skip it */
+ i_skipped_top++;
+ }
+ else
+ {
+ /* We can't be sure the current lines will be skipped,
+ * so we store the code just in case. */
+ *p_dest++ = i_code;
+ i_skipped_bottom++;
+ }
}
else
{
- /* We can't be sure the current lines will be skipped,
- * so we store the code just in case. */
+ /* We got a valid code, store it */
*p_dest++ = i_code;
- b_empty_bottom = VLC_TRUE;
- i_skipped_bottom++;
+ /* Valid code means no blank line */
+ b_empty_top = false;
+ i_skipped_bottom = 0;
}
}
else
{
- /* We got a valid code, store it */
*p_dest++ = i_code;
-
- /* Valid code means no blank line */
- b_empty_top = VLC_FALSE;
- b_empty_bottom = VLC_FALSE;
- i_skipped_bottom = 0;
}
}
/* Check that we didn't go too far */
if( i_x > i_width )
{
- msg_Err( p_spudec->p_fifo, "i_x overflowed, %i > %i",
- i_x, i_width );
+ msg_Err( p_dec, "i_x overflowed, %i > %i", i_x, i_width );
return VLC_EGENERIC;
}
/* We shouldn't get any padding bytes */
if( i_y < i_height )
{
- msg_Err( p_spudec->p_fifo, "padding bytes found in RLE sequence" );
- msg_Err( p_spudec->p_fifo, "send mail to <sam@zoy.org> if you "
- "want to help debugging this" );
+ msg_Err( p_dec, "padding bytes found in RLE sequence" );
+ msg_Err( p_dec, "send mail to <sam@zoy.org> if you "
+ "want to help debugging this" );
/* Skip them just in case */
while( i_y < i_height )
return VLC_EGENERIC;
}
- msg_Dbg( p_spudec->p_fifo, "valid subtitle, size: %ix%i, position: %i,%i",
+#ifdef DEBUG_SPUDEC
+ msg_Dbg( p_dec, "valid subtitle, size: %ix%i, position: %i,%i",
p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
+#endif
/* Crop if necessary */
if( i_skipped_top || i_skipped_bottom )
{
- p_spu->i_y += i_skipped_top;
- p_spu->i_height -= i_skipped_top + i_skipped_bottom;
-
- msg_Dbg( p_spudec->p_fifo, "cropped to: %ix%i, position: %i,%i",
- p_spu->i_width, p_spu->i_height, p_spu->i_x, p_spu->i_y );
+#ifdef DEBUG_SPUDEC
+ int i_y = p_spu->i_y + i_skipped_top;
+ int i_height = p_spu->i_height - (i_skipped_top + i_skipped_bottom);
+#endif
+ p_spu_data->i_y_top_offset = i_skipped_top;
+ p_spu_data->i_y_bottom_offset = i_skipped_bottom;
+#ifdef DEBUG_SPUDEC
+ msg_Dbg( p_dec, "cropped to: %ix%i, position: %i,%i",
+ p_spu->i_width, i_height, p_spu->i_x, i_y );
+#endif
}
/* Handle color if no palette was found */
- if( !p_spu->p_sys->b_palette )
+ if( !p_spu_data->b_palette )
{
int i, i_inner = -1, i_shade = -1;
/* Set the border color */
- p_spu->p_sys->pi_yuv[i_border][0] = 0x00;
- p_spu->p_sys->pi_yuv[i_border][1] = 0x80;
- p_spu->p_sys->pi_yuv[i_border][2] = 0x80;
+ p_spu_data->pi_yuv[i_border][0] = 0x00;
+ p_spu_data->pi_yuv[i_border][1] = 0x80;
+ p_spu_data->pi_yuv[i_border][2] = 0x80;
stats[i_border] = 0;
/* Find the inner colors */
/* Set the inner color */
if( i_inner != -1 )
{
- p_spu->p_sys->pi_yuv[i_inner][0] = 0xff;
- p_spu->p_sys->pi_yuv[i_inner][1] = 0x80;
- p_spu->p_sys->pi_yuv[i_inner][2] = 0x80;
+ p_spu_data->pi_yuv[i_inner][0] = 0xff;
+ p_spu_data->pi_yuv[i_inner][1] = 0x80;
+ p_spu_data->pi_yuv[i_inner][2] = 0x80;
}
/* Set the anti-aliasing color */
if( i_shade != -1 )
{
- p_spu->p_sys->pi_yuv[i_shade][0] = 0x80;
- p_spu->p_sys->pi_yuv[i_shade][1] = 0x80;
- p_spu->p_sys->pi_yuv[i_shade][2] = 0x80;
+ p_spu_data->pi_yuv[i_shade][0] = 0x80;
+ p_spu_data->pi_yuv[i_shade][1] = 0x80;
+ p_spu_data->pi_yuv[i_shade][2] = 0x80;
}
- msg_Dbg( p_spudec->p_fifo,
- "using custom palette (border %i, inner %i, shade %i)",
+#ifdef DEBUG_SPUDEC
+ msg_Dbg( p_dec, "using custom palette (border %i, inner %i, shade %i)",
i_border, i_inner, i_shade );
+#endif
}
return VLC_SUCCESS;
}
+static void Render( decoder_t *p_dec, subpicture_t *p_spu,
+ subpicture_data_t *p_spu_data,
+ const spu_properties_t *p_spu_properties )
+{
+ uint8_t *p_p;
+ int i_x, i_y, i_len, i_color, i_pitch;
+ const uint16_t *p_source = p_spu_data->p_data;
+ video_format_t fmt;
+ video_palette_t palette;
+
+ /* Create a new subpicture region */
+ memset( &fmt, 0, sizeof(video_format_t) );
+ fmt.i_chroma = VLC_CODEC_YUVP;
+ fmt.i_sar_num = 0; /* 0 means use aspect ratio of background video */
+ fmt.i_sar_den = 1;
+ fmt.i_width = fmt.i_visible_width = p_spu_properties->i_width;
+ fmt.i_height = fmt.i_visible_height = p_spu_properties->i_height -
+ p_spu_data->i_y_top_offset - p_spu_data->i_y_bottom_offset;
+ fmt.i_x_offset = fmt.i_y_offset = 0;
+ fmt.p_palette = &palette;
+ fmt.p_palette->i_entries = 4;
+ for( i_x = 0; i_x < fmt.p_palette->i_entries; i_x++ )
+ {
+ fmt.p_palette->palette[i_x][0] = p_spu_data->pi_yuv[i_x][0];
+ fmt.p_palette->palette[i_x][1] = p_spu_data->pi_yuv[i_x][1];
+ fmt.p_palette->palette[i_x][2] = p_spu_data->pi_yuv[i_x][2];
+ fmt.p_palette->palette[i_x][3] = p_spu_data->pi_alpha[i_x] * 0x11;
+ }
+
+ p_spu->p_region = subpicture_region_New( &fmt );
+ if( !p_spu->p_region )
+ {
+ msg_Err( p_dec, "cannot allocate SPU region" );
+ return;
+ }
+
+ p_spu->p_region->i_x = p_spu_properties->i_x;
+ p_spu->p_region->i_y = p_spu_properties->i_y + p_spu_data->i_y_top_offset;
+ p_p = p_spu->p_region->p_picture->p->p_pixels;
+ i_pitch = p_spu->p_region->p_picture->p->i_pitch;
+
+ /* Draw until we reach the bottom of the subtitle */
+ for( i_y = 0; i_y < (int)fmt.i_height * i_pitch; i_y += i_pitch )
+ {
+ /* Draw until we reach the end of the line */
+ for( i_x = 0 ; i_x < (int)fmt.i_width; i_x += i_len )
+ {
+ /* Get the RLE part, then draw the line */
+ i_color = *p_source & 0x3;
+ i_len = *p_source++ >> 2;
+ memset( p_p + i_x + i_y, i_color, i_len );
+ }
+ }
+}