]> git.sesse.net Git - vlc/blob - src/video_output/video_spu.c
* Additional optimizations to the subtitle decoder
[vlc] / src / video_output / video_spu.c
1 /*****************************************************************************
2  * video_spu.c : DVD subpicture units functions
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "defs.h"
27
28 #include <stdio.h>
29
30 #include "config.h"
31 #include "common.h"
32 #include "threads.h"
33 #include "mtime.h"
34
35 #include "video.h"
36 #include "video_output.h"
37 #include "video_spu.h"
38
39 /* FIXME: fake palette - the real one has to be sought in the .IFO */
40 static int p_palette[4] = { 0x0000, 0xffff, 0x5555, 0x8888 };
41
42 /*****************************************************************************
43  * vout_RenderSPU: draw an SPU on a picture
44  *****************************************************************************
45  * This is a fast implementation of the subpicture drawing code. The data
46  * has been preprocessed once in spu_decoder.c, so we don't need to parse the
47  * RLE buffer again and again. Most sanity checks are done in spu_decoder.c
48  * so that this routine can be as fast as possible.
49  *****************************************************************************/
50 void vout_RenderSPU( vout_buffer_t *p_buffer, subpicture_t *p_spu,
51                      int i_bytes_per_pixel, int i_bytes_per_line )
52 {
53     int  i_len, i_color;
54     u16 *p_source = (u16 *)p_spu->p_data;
55
56     /* FIXME: we need a way to get 720 and 576 from the stream */
57     int i_xscale = ( p_buffer->i_pic_width << 6 ) / 720;
58     int i_yscale = ( p_buffer->i_pic_height << 6 ) / 576;
59
60     int i_width  = p_spu->i_width  * i_xscale;
61     int i_height = p_spu->i_height * i_yscale;
62
63     int i_x = 0, i_y = 0, i_ytmp, i_yreal, i_ynext;
64
65     u8 *p_dest = p_buffer->p_data
66                   /* Add the picture coordinates and the SPU coordinates */
67                   + ( p_buffer->i_pic_x + ((p_spu->i_x * i_xscale) >> 6))
68                        * i_bytes_per_pixel
69                   + ( p_buffer->i_pic_y + ((p_spu->i_y * i_yscale) >> 6))
70                        * i_bytes_per_line;
71
72     /* Draw until we reach the bottom of the subtitle */
73     for( i_y = 0 ; i_y < i_height ; /* i_y incremented below */ )
74     {
75         i_ytmp = i_y >> 6;
76         i_y += i_yscale;
77
78         /* Check whether we need to draw one line or more than one */
79         if( i_ytmp + 1 >= ( i_y >> 6 ) )
80         {
81             /* Just one line : we precalculate i_y >> 6 */
82             i_yreal = i_bytes_per_line * i_ytmp;
83
84             /* Draw until we reach the end of the line */
85             for( i_x = 0 ; i_x < i_width ; i_x += i_len )
86             {
87                 /* Get RLE information */
88                 i_len = i_xscale * ( *p_source >> 2 );
89                 i_color = *p_source++ & 0x3;
90
91                 /* Draw the line */
92                 if( i_color )
93                 {
94                     memset( p_dest + i_bytes_per_pixel * ( i_x >> 6 )
95                                    + i_yreal,
96                             p_palette[ i_color ],
97                             i_bytes_per_pixel * ( ( i_len >> 6 ) + 1 ) );
98                 }
99             }
100         }
101         else
102         {
103             i_yreal = i_bytes_per_line * i_ytmp;
104             i_ynext = i_bytes_per_line * i_y >> 6;
105
106             /* Draw until we reach the end of the line */
107             for( i_x = 0 ; i_x < i_width ; i_x += i_len )
108             {
109                 /* Get RLE information */
110                 i_len = i_xscale * ( *p_source >> 2 );
111                 i_color = *p_source++ & 0x3;
112
113                 /* Draw as many lines as needed */
114                 if( i_color )
115                 {
116                     for( i_ytmp = i_yreal ;
117                          i_ytmp < i_ynext ;
118                          i_ytmp += i_bytes_per_line )
119                     {
120                         memset( p_dest + i_bytes_per_pixel * ( i_x >> 6 )
121                                        + i_ytmp,
122                                 p_palette[ i_color ],
123                                 i_bytes_per_pixel * ( ( i_len >> 6 ) + 1 ) );
124                     }
125                 }
126             }
127         }
128     }
129 }
130