]> git.sesse.net Git - vlc/blob - modules/codec/subtitles/subsdec.c
Subsdec: Split the decoder for subsdec / SSA / USF in three files, since the code...
[vlc] / modules / codec / subtitles / subsdec.c
1 /*****************************************************************************
2  * subsdec.c : text subtitles decoder
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id: subsdec.c 20996 2007-08-05 20:01:21Z jb $
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Derk-Jan Hartman <hartman at videolan dot org>
10  *          Bernie Purcell <b dot purcell at adbglobal dot com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #include "subsdec.h"
32
33 /*****************************************************************************
34  * Local prototypes
35  *****************************************************************************/
36 static int  OpenDecoder   ( vlc_object_t * );
37 static void CloseDecoder  ( vlc_object_t * );
38
39 static subpicture_t   *DecodeBlock   ( decoder_t *, block_t ** );
40 static subpicture_t   *ParseText     ( decoder_t *, block_t * );
41 static char           *StripTags      ( char * );
42 static char           *CreateHtmlSubtitle ( char * );
43 static int            ParseImageAttachments( decoder_t *p_dec );
44
45 static int            ParsePositionAttributeList( char *, int *, int *, int * );
46
47 /*****************************************************************************
48  * Module descriptor.
49  *****************************************************************************/
50 static const char *ppsz_encodings[] = { DEFAULT_NAME, "ASCII", "UTF-8", "",
51     "ISO-8859-1", "CP1252", "MacRoman", "MacIceland","ISO-8859-15", "",
52     "ISO-8859-2", "CP1250", "MacCentralEurope", "MacCroatian", "MacRomania", "",
53     "ISO-8859-5", "CP1251", "MacCyrillic", "MacUkraine", "KOI8-R", "KOI8-U", "KOI8-RU", "",
54     "ISO-8859-6", "CP1256", "MacArabic", "",
55     "ISO-8859-7", "CP1253", "MacGreek", "",
56     "ISO-8859-8", "CP1255", "MacHebrew", "",
57     "ISO-8859-9", "CP1254", "MacTurkish", "",
58     "ISO-8859-13", "CP1257", "",
59     "ISO-2022-JP", "ISO-2022-JP-1", "ISO-2022-JP-2", "EUC-JP", "SHIFT_JIS", "",
60     "ISO-2022-CN", "ISO-2022-CN-EXT", "EUC-CN", "EUC-TW", "BIG5", "BIG5-HKSCS", "",
61     "ISO-2022-KR", "EUC-KR", "",
62     "MacThai", "KOI8-T", "",
63     "ISO-8859-3", "ISO-8859-4", "ISO-8859-10", "ISO-8859-14", "ISO-8859-16", "",
64     "CP850", "CP862", "CP866", "CP874", "CP932", "CP949", "CP950", "CP1133", "CP1258", "",
65     "Macintosh", "",
66     "UTF-7", "UTF-16", "UTF-16BE", "UTF-16LE", "UTF-32", "UTF-32BE", "UTF-32LE",
67     "C99", "JAVA", "UCS-2", "UCS-2BE", "UCS-2LE", "UCS-4", "UCS-4BE", "UCS-4LE", "",
68     "HZ", "GBK", "GB18030", "JOHAB", "ARMSCII-8",
69     "Georgian-Academy", "Georgian-PS", "TIS-620", "MuleLao-1", "VISCII", "TCVN",
70     "HPROMAN8", "NEXTSTEP" };
71 /*
72 SSA supports charset selection.
73 The following known charsets are used:
74
75 0 = Ansi - Western European
76 1 = default
77 2 = symbol
78 3 = invalid
79 77 = Mac
80 128 = Japanese (Shift JIS)
81 129 = Hangul
82 130 = Johab
83 134 = GB2312 Simplified Chinese
84 136 = Big5 Traditional Chinese
85 161 = Greek
86 162 = Turkish
87 163 = Vietnamese
88 177 = Hebrew
89 178 = Arabic
90 186 = Baltic
91 204 = Russian (Cyrillic)
92 222 = Thai
93 238 = Eastern European
94 254 = PC 437
95 */
96
97 static int  pi_justification[] = { 0, 1, 2 };
98 static const char *ppsz_justification_text[] = {N_("Center"),N_("Left"),N_("Right")};
99
100 #define ENCODING_TEXT N_("Subtitles text encoding")
101 #define ENCODING_LONGTEXT N_("Set the encoding used in text subtitles")
102 #define ALIGN_TEXT N_("Subtitles justification")
103 #define ALIGN_LONGTEXT N_("Set the justification of subtitles")
104 #define AUTODETECT_UTF8_TEXT N_("UTF-8 subtitles autodetection")
105 #define AUTODETECT_UTF8_LONGTEXT N_("This enables automatic detection of " \
106             "UTF-8 encoding within subtitles files.")
107 #define FORMAT_TEXT N_("Formatted Subtitles")
108 #define FORMAT_LONGTEXT N_("Some subtitle formats allow for text formatting. " \
109  "VLC partly implements this, but you can choose to disable all formatting.")
110
111
112 vlc_module_begin();
113     set_shortname( _("Subtitles"));
114     set_description( _("Text subtitles decoder") );
115     set_capability( "decoder", 50 );
116     set_callbacks( OpenDecoder, CloseDecoder );
117     set_category( CAT_INPUT );
118     set_subcategory( SUBCAT_INPUT_SCODEC );
119
120     add_integer( "subsdec-align", 0, NULL, ALIGN_TEXT, ALIGN_LONGTEXT,
121                  VLC_FALSE );
122         change_integer_list( pi_justification, ppsz_justification_text, 0 );
123     add_string( "subsdec-encoding", DEFAULT_NAME, NULL,
124                 ENCODING_TEXT, ENCODING_LONGTEXT, VLC_FALSE );
125         change_string_list( ppsz_encodings, 0, 0 );
126     add_bool( "subsdec-autodetect-utf8", VLC_TRUE, NULL,
127               AUTODETECT_UTF8_TEXT, AUTODETECT_UTF8_LONGTEXT, VLC_FALSE );
128     add_bool( "subsdec-formatted", VLC_TRUE, NULL, FORMAT_TEXT, FORMAT_LONGTEXT,
129                  VLC_FALSE );
130 vlc_module_end();
131
132 /*****************************************************************************
133  * OpenDecoder: probe the decoder and return score
134  *****************************************************************************
135  * Tries to launch a decoder and return score so that the interface is able
136  * to chose.
137  *****************************************************************************/
138 static int OpenDecoder( vlc_object_t *p_this )
139 {
140     decoder_t     *p_dec = (decoder_t*)p_this;
141     decoder_sys_t *p_sys;
142     vlc_value_t    val;
143
144     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','u','b','t') &&
145         p_dec->fmt_in.i_codec != VLC_FOURCC('u','s','f',' ') &&
146         p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
147     {
148         return VLC_EGENERIC;
149     }
150
151     p_dec->pf_decode_sub = DecodeBlock;
152
153     /* Allocate the memory needed to store the decoder's structure */
154     if( ( p_dec->p_sys = p_sys =
155           (decoder_sys_t *)calloc(1, sizeof(decoder_sys_t)) ) == NULL )
156     {
157         msg_Err( p_dec, "out of memory" );
158         return VLC_ENOMEM;
159     }
160
161     /* init of p_sys */
162     p_sys->i_align = 0;
163     p_sys->iconv_handle = (vlc_iconv_t)-1;
164     p_sys->b_autodetect_utf8 = VLC_FALSE;
165     p_sys->b_ass = VLC_FALSE;
166     p_sys->i_original_height = -1;
167     p_sys->i_original_width = -1;
168     TAB_INIT( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
169     TAB_INIT( p_sys->i_images, p_sys->pp_images );
170
171     char *psz_charset = NULL;
172     /* First try demux-specified encoding */
173     if( p_dec->fmt_in.subs.psz_encoding && *p_dec->fmt_in.subs.psz_encoding )
174     {
175         psz_charset = strdup (p_dec->fmt_in.subs.psz_encoding);
176         msg_Dbg (p_dec, "trying demuxer-specified character encoding: %s",
177                  p_dec->fmt_in.subs.psz_encoding ?: "not specified");
178     }
179
180     /* Second, try configured encoding */
181     if (psz_charset == NULL)
182     {
183         psz_charset = var_CreateGetNonEmptyString (p_dec, "subsdec-encoding");
184         if ((psz_charset != NULL) && !strcasecmp (psz_charset, DEFAULT_NAME))
185         {
186             free (psz_charset);
187             psz_charset = NULL;
188         }
189
190         msg_Dbg (p_dec, "trying configured character encoding: %s",
191                  psz_charset ?: "not specified");
192     }
193
194     /* Third, try "local" encoding with optional UTF-8 autodetection */
195     if (psz_charset == NULL)
196     {
197         psz_charset = strdup (GetFallbackEncoding ());
198         msg_Dbg (p_dec, "trying default character encoding: %s",
199                  psz_charset ?: "not specified");
200
201         if (var_CreateGetBool (p_dec, "subsdec-autodetect-utf8"))
202         {
203             msg_Dbg (p_dec, "using automatic UTF-8 detection");
204             p_sys->b_autodetect_utf8 = VLC_TRUE;
205         }
206     }
207
208     if (psz_charset == NULL)
209     {
210         psz_charset = strdup ("UTF-8");
211         msg_Dbg (p_dec, "trying hard-coded character encoding: %s",
212                  psz_charset ?: "error");
213     }
214
215     if (psz_charset == NULL)
216     {
217         free (p_sys);
218         return VLC_ENOMEM;
219     }
220
221     if (strcasecmp (psz_charset, "UTF-8") && strcasecmp (psz_charset, "utf8"))
222     {
223         p_sys->iconv_handle = vlc_iconv_open ("UTF-8", psz_charset);
224         if (p_sys->iconv_handle == (vlc_iconv_t)(-1))
225             msg_Err (p_dec, "cannot convert from %s: %s", psz_charset,
226                      strerror (errno));
227     }
228     free (psz_charset);
229
230     var_Create( p_dec, "subsdec-align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
231     var_Get( p_dec, "subsdec-align", &val );
232     p_sys->i_align = val.i_int;
233
234     ParseImageAttachments( p_dec );
235
236     if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
237     {
238         if( p_dec->fmt_in.i_extra > 0 )
239             ParseSSAHeader( p_dec );
240     }
241     else if( p_dec->fmt_in.i_codec == VLC_FOURCC('u','s','f',' ') && var_CreateGetBool( p_dec, "subsdec-formatted" ) )
242     {
243         if( p_dec->fmt_in.i_extra > 0 )
244             ParseUSFHeader( p_dec );
245     }
246
247     return VLC_SUCCESS;
248 }
249
250 /****************************************************************************
251  * DecodeBlock: the whole thing
252  ****************************************************************************
253  * This function must be fed with complete subtitles units.
254  ****************************************************************************/
255 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
256 {
257     subpicture_t *p_spu = NULL;
258
259     if( !pp_block || *pp_block == NULL ) return NULL;
260
261     p_spu = ParseText( p_dec, *pp_block );
262
263     block_Release( *pp_block );
264     *pp_block = NULL;
265
266     return p_spu;
267 }
268
269 /*****************************************************************************
270  * CloseDecoder: clean up the decoder
271  *****************************************************************************/
272 static void CloseDecoder( vlc_object_t *p_this )
273 {
274     decoder_t *p_dec = (decoder_t *)p_this;
275     decoder_sys_t *p_sys = p_dec->p_sys;
276
277     if( p_sys->iconv_handle != (vlc_iconv_t)-1 )
278         vlc_iconv_close( p_sys->iconv_handle );
279
280     if( p_sys->pp_ssa_styles )
281     {
282         int i;
283         for( i = 0; i < p_sys->i_ssa_styles; i++ )
284         {
285             if( !p_sys->pp_ssa_styles[i] )
286                 continue;
287
288             if( p_sys->pp_ssa_styles[i]->psz_stylename )
289                 free( p_sys->pp_ssa_styles[i]->psz_stylename );
290             if( p_sys->pp_ssa_styles[i]->font_style.psz_fontname )
291                 free( p_sys->pp_ssa_styles[i]->font_style.psz_fontname );
292             if( p_sys->pp_ssa_styles[i] )
293                 free( p_sys->pp_ssa_styles[i] );
294         }
295         TAB_CLEAN( p_sys->i_ssa_styles, p_sys->pp_ssa_styles );
296     }
297     if( p_sys->pp_images )
298     {
299         int i;
300         for( i = 0; i < p_sys->i_images; i++ )
301         {
302             if( !p_sys->pp_images[i] )
303                 continue;
304
305             if( p_sys->pp_images[i]->p_pic )
306                 p_sys->pp_images[i]->p_pic->pf_release( p_sys->pp_images[i]->p_pic );
307             if( p_sys->pp_images[i]->psz_filename )
308                 free( p_sys->pp_images[i]->psz_filename );
309
310             free( p_sys->pp_images[i] );
311         }
312         TAB_CLEAN( p_sys->i_images, p_sys->pp_images );
313     }
314
315     free( p_sys );
316 }
317
318 /*****************************************************************************
319  * ParseText: parse an text subtitle packet and send it to the video output
320  *****************************************************************************/
321 static subpicture_t *ParseText( decoder_t *p_dec, block_t *p_block )
322 {
323     decoder_sys_t *p_sys = p_dec->p_sys;
324     subpicture_t *p_spu = NULL;
325     char *psz_subtitle = NULL;
326     video_format_t fmt;
327
328     /* We cannot display a subpicture with no date */
329     if( p_block->i_pts == 0 )
330     {
331         msg_Warn( p_dec, "subtitle without a date" );
332         return NULL;
333     }
334
335     /* Check validity of packet data */
336     /* An "empty" line containing only \0 can be used to force
337        and ephemer picture from the screen */
338     if( p_block->i_buffer < 1 )
339     {
340         msg_Warn( p_dec, "no subtitle data" );
341         return NULL;
342     }
343
344     /* Should be resiliant against bad subtitles */
345     psz_subtitle = strndup( (const char *)p_block->p_buffer,
346                             p_block->i_buffer );
347     if( psz_subtitle == NULL )
348         return NULL;
349
350     if( p_sys->iconv_handle == (vlc_iconv_t)-1 )
351     {
352         if (EnsureUTF8( psz_subtitle ) == NULL)
353         {
354             msg_Err( p_dec, _("failed to convert subtitle encoding.\n"
355                      "Try manually setting a character-encoding "
356                      "before you open the file.") );
357         }
358     }
359     else
360     {
361
362         if( p_sys->b_autodetect_utf8 )
363         {
364             if( IsUTF8( psz_subtitle ) == NULL )
365             {
366                 msg_Dbg( p_dec, "invalid UTF-8 sequence: "
367                          "disabling UTF-8 subtitles autodetection" );
368                 p_sys->b_autodetect_utf8 = VLC_FALSE;
369             }
370         }
371
372         if( !p_sys->b_autodetect_utf8 )
373         {
374             size_t inbytes_left = strlen( psz_subtitle );
375             size_t outbytes_left = 6 * inbytes_left;
376             char *psz_new_subtitle = malloc( outbytes_left + 1 );
377             char *psz_convert_buffer_out = psz_new_subtitle;
378             const char *psz_convert_buffer_in = psz_subtitle;
379
380             size_t ret = vlc_iconv( p_sys->iconv_handle,
381                                     &psz_convert_buffer_in, &inbytes_left,
382                                     &psz_convert_buffer_out, &outbytes_left );
383
384             *psz_convert_buffer_out++ = '\0';
385             free( psz_subtitle );
386
387             if( ( ret == (size_t)(-1) ) || inbytes_left )
388             {
389                 free( psz_new_subtitle );
390                 msg_Err( p_dec, _("failed to convert subtitle encoding.\n"
391                         "Try manually setting a character-encoding "
392                                 "before you open the file.") );
393                 return NULL;
394             }
395
396             psz_subtitle = realloc( psz_new_subtitle,
397                                     psz_convert_buffer_out - psz_new_subtitle );
398         }
399     }
400
401     /* Create the subpicture unit */
402     p_spu = p_dec->pf_spu_buffer_new( p_dec );
403     if( !p_spu )
404     {
405         msg_Warn( p_dec, "can't get spu buffer" );
406         if( psz_subtitle ) free( psz_subtitle );
407         return NULL;
408     }
409
410     p_spu->b_pausable = VLC_TRUE;
411
412     /* Create a new subpicture region */
413     memset( &fmt, 0, sizeof(video_format_t) );
414     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
415     fmt.i_aspect = 0;
416     fmt.i_width = fmt.i_height = 0;
417     fmt.i_x_offset = fmt.i_y_offset = 0;
418     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
419     if( !p_spu->p_region )
420     {
421         msg_Err( p_dec, "cannot allocate SPU region" );
422         if( psz_subtitle ) free( psz_subtitle );
423         p_dec->pf_spu_buffer_del( p_dec, p_spu );
424         return NULL;
425     }
426
427     /* Decode and format the subpicture unit */
428     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') &&
429         p_dec->fmt_in.i_codec != VLC_FOURCC('u','s','f',' ') )
430     {
431         /* Normal text subs, easy markup */
432         p_spu->p_region->i_align = SUBPICTURE_ALIGN_BOTTOM | p_sys->i_align;
433         p_spu->i_x = p_sys->i_align ? 20 : 0;
434         p_spu->i_y = 10;
435
436         /* Remove formatting from string */
437
438         p_spu->p_region->psz_text = StripTags( psz_subtitle );
439         if( var_CreateGetBool( p_dec, "subsdec-formatted" ) )
440         {
441             p_spu->p_region->psz_html = CreateHtmlSubtitle( psz_subtitle );
442         }
443
444         p_spu->i_start = p_block->i_pts;
445         p_spu->i_stop = p_block->i_pts + p_block->i_length;
446         p_spu->b_ephemer = (p_block->i_length == 0);
447         p_spu->b_absolute = VLC_FALSE;
448     }
449     else
450     {
451         /* Decode SSA/USF strings */
452         if( p_dec->fmt_in.i_codec == VLC_FOURCC('s','s','a',' ') )
453             ParseSSAString( p_dec, psz_subtitle, p_spu );
454         else
455         {
456             p_spu->pf_destroy_region( VLC_OBJECT(p_dec), p_spu->p_region );
457             p_spu->p_region = ParseUSFString( p_dec, psz_subtitle, p_spu );
458         }
459
460         p_spu->i_start = p_block->i_pts;
461         p_spu->i_stop = p_block->i_pts + p_block->i_length;
462         p_spu->b_ephemer = (p_block->i_length == 0);
463         p_spu->b_absolute = VLC_FALSE;
464         p_spu->i_original_picture_width = p_sys->i_original_width;
465         p_spu->i_original_picture_height = p_sys->i_original_height;
466     }
467     if( psz_subtitle ) free( psz_subtitle );
468
469     return p_spu;
470 }
471
472 char *GrabAttributeValue( const char *psz_attribute,
473                                  const char *psz_tag_start )
474 {
475     if( psz_attribute && psz_tag_start )
476     {
477         char *psz_tag_end = strchr( psz_tag_start, '>' );
478         char *psz_found   = strcasestr( psz_tag_start, psz_attribute );
479
480         if( psz_found )
481         {
482             psz_found += strlen( psz_attribute );
483
484             if(( *(psz_found++) == '=' ) &&
485                ( *(psz_found++) == '\"' ))
486             {
487                 if( psz_found < psz_tag_end )
488                 {
489                     int   i_len = strcspn( psz_found, "\"" );
490                     return strndup( psz_found, i_len );
491                 }
492             }
493         }
494     }
495     return NULL;
496 }
497
498 static ssa_style_t *ParseStyle( decoder_sys_t *p_sys, char *psz_subtitle )
499 {
500     ssa_style_t *p_style   = NULL;
501     char        *psz_style = GrabAttributeValue( "style", psz_subtitle );
502
503     if( psz_style )
504     {
505         int i;
506
507         for( i = 0; i < p_sys->i_ssa_styles; i++ )
508         {
509             if( !strcmp( p_sys->pp_ssa_styles[i]->psz_stylename, psz_style ) )
510                 p_style = p_sys->pp_ssa_styles[i];
511         }
512         free( psz_style );
513     }
514     return p_style;
515 }
516
517 static int ParsePositionAttributeList( char *psz_subtitle, int *i_align,
518                                        int *i_x, int *i_y )
519 {
520     int   i_mask = 0;
521
522     char *psz_align    = GrabAttributeValue( "alignment", psz_subtitle );
523     char *psz_margin_x = GrabAttributeValue( "horizontal-margin", psz_subtitle );
524     char *psz_margin_y = GrabAttributeValue( "vertical-margin", psz_subtitle );
525     /* -- UNSUPPORTED
526     char *psz_relative = GrabAttributeValue( "relative-to", psz_subtitle );
527     char *psz_rotate_x = GrabAttributeValue( "rotate-x", psz_subtitle );
528     char *psz_rotate_y = GrabAttributeValue( "rotate-y", psz_subtitle );
529     char *psz_rotate_z = GrabAttributeValue( "rotate-z", psz_subtitle );
530     */
531
532     *i_align = SUBPICTURE_ALIGN_BOTTOM;
533     *i_x = 0;
534     *i_y = 0;
535
536     if( psz_align )
537     {
538         if( !strcasecmp( "TopLeft", psz_align ) )
539             *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
540         else if( !strcasecmp( "TopCenter", psz_align ) )
541             *i_align = SUBPICTURE_ALIGN_TOP;
542         else if( !strcasecmp( "TopRight", psz_align ) )
543             *i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_RIGHT;
544         else if( !strcasecmp( "MiddleLeft", psz_align ) )
545             *i_align = SUBPICTURE_ALIGN_LEFT;
546         else if( !strcasecmp( "MiddleCenter", psz_align ) )
547             *i_align = 0;
548         else if( !strcasecmp( "MiddleRight", psz_align ) )
549             *i_align = SUBPICTURE_ALIGN_RIGHT;
550         else if( !strcasecmp( "BottomLeft", psz_align ) )
551             *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_LEFT;
552         else if( !strcasecmp( "BottomCenter", psz_align ) )
553             *i_align = SUBPICTURE_ALIGN_BOTTOM;
554         else if( !strcasecmp( "BottomRight", psz_align ) )
555             *i_align = SUBPICTURE_ALIGN_BOTTOM | SUBPICTURE_ALIGN_RIGHT;
556
557         i_mask |= ATTRIBUTE_ALIGNMENT;
558         free( psz_align );
559     }
560     if( psz_margin_x )
561     {
562         *i_x = atoi( psz_margin_x );
563         if( strchr( psz_margin_x, '%' ) )
564             i_mask |= ATTRIBUTE_X_PERCENT;
565         else
566             i_mask |= ATTRIBUTE_X;
567
568         free( psz_margin_x );
569     }
570     if( psz_margin_y )
571     {
572         *i_y = atoi( psz_margin_y );
573         if( strchr( psz_margin_y, '%' ) )
574             i_mask |= ATTRIBUTE_Y_PERCENT;
575         else
576             i_mask |= ATTRIBUTE_Y;
577
578         free( psz_margin_y );
579     }
580     return i_mask;
581 }
582
583 void SetupPositions( subpicture_region_t *p_region, char *psz_subtitle )
584 {
585     int           i_mask = 0;
586     int           i_align;
587     int           i_x, i_y;
588
589     i_mask = ParsePositionAttributeList( psz_subtitle, &i_align, &i_x, &i_y );
590
591     if( i_mask & ATTRIBUTE_ALIGNMENT )
592         p_region->i_align = i_align;
593
594     /* TODO: Setup % based offsets properly, without adversely affecting
595      *       everything else in vlc. Will address with separate patch, to
596      *       prevent this one being any more complicated.
597      */
598     if( i_mask & ATTRIBUTE_X )
599         p_region->i_x = i_x;
600     else if( i_mask & ATTRIBUTE_X_PERCENT )
601         p_region->i_x = 0;
602
603     if( i_mask & ATTRIBUTE_Y )
604         p_region->i_y = i_y;
605     else if( i_mask & ATTRIBUTE_Y_PERCENT )
606         p_region->i_y = 0;
607 }
608
609 subpicture_region_t *CreateTextRegion( decoder_t *p_dec,
610                                               subpicture_t *p_spu,
611                                               char *psz_subtitle,
612                                               int i_len,
613                                               int i_sys_align )
614 {
615     decoder_sys_t        *p_sys = p_dec->p_sys;
616     subpicture_region_t  *p_text_region;
617     video_format_t        fmt;
618
619     /* Create a new subpicture region */
620     memset( &fmt, 0, sizeof(video_format_t) );
621     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
622     fmt.i_aspect = 0;
623     fmt.i_width = fmt.i_height = 0;
624     fmt.i_x_offset = fmt.i_y_offset = 0;
625     p_text_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
626
627     if( p_text_region != NULL )
628     {
629         ssa_style_t  *p_style = NULL;
630
631         p_text_region->psz_text = NULL;
632         p_text_region->psz_html = strndup( psz_subtitle, i_len );
633         if( ! p_text_region->psz_html )
634         {
635             msg_Err( p_dec, "out of memory" );
636             p_spu->pf_destroy_region( VLC_OBJECT(p_dec), p_text_region );
637             return NULL;
638         }
639
640         p_style = ParseStyle( p_sys, p_text_region->psz_html );
641         if( !p_style )
642         {
643             int i;
644
645             for( i = 0; i < p_sys->i_ssa_styles; i++ )
646             {
647                 if( !strcasecmp( p_sys->pp_ssa_styles[i]->psz_stylename, "Default" ) )
648                     p_style = p_sys->pp_ssa_styles[i];
649             }
650         }
651
652         if( p_style )
653         {
654             msg_Dbg( p_dec, "style is: %s", p_style->psz_stylename );
655
656             p_text_region->p_style = &p_style->font_style;
657             p_text_region->i_align = p_style->i_align;
658
659             /* TODO: Setup % based offsets properly, without adversely affecting
660              *       everything else in vlc. Will address with separate patch,
661              *       to prevent this one being any more complicated.
662
663                      * p_style->i_margin_percent_h;
664                      * p_style->i_margin_percent_v;
665              */
666             p_text_region->i_x         = p_style->i_margin_h;
667             p_text_region->i_y         = p_style->i_margin_v;
668
669         }
670         else
671         {
672             p_text_region->i_align = SUBPICTURE_ALIGN_BOTTOM | i_sys_align;
673             p_text_region->i_x = i_sys_align ? 20 : 0;
674             p_text_region->i_y = 10;
675         }
676         /* Look for position arguments which may override the style-based
677          * defaults.
678          */
679         SetupPositions( p_text_region, psz_subtitle );
680
681         p_text_region->p_next = NULL;
682     }
683     return p_text_region;
684 }
685
686 static int ParseImageAttachments( decoder_t *p_dec )
687 {
688     decoder_sys_t        *p_sys = p_dec->p_sys;
689     input_attachment_t  **pp_attachments;
690     int                   i_attachments_cnt;
691     int                   k = 0;
692
693     if( VLC_SUCCESS != decoder_GetInputAttachments( p_dec, &pp_attachments, &i_attachments_cnt ))
694         return VLC_EGENERIC;
695
696     for( k = 0; k < i_attachments_cnt; k++ )
697     {
698         input_attachment_t *p_attach = pp_attachments[k];
699
700         vlc_fourcc_t  type  = 0;
701
702         if( ( !strcmp( p_attach->psz_mime, "image/bmp" ) )      || /* BMP */
703             ( !strcmp( p_attach->psz_mime, "image/x-bmp" ) )    ||
704             ( !strcmp( p_attach->psz_mime, "image/x-bitmap" ) ) ||
705             ( !strcmp( p_attach->psz_mime, "image/x-ms-bmp" ) ) )
706         {
707              type = VLC_FOURCC('b','m','p',' ');
708         }
709         else if( ( !strcmp( p_attach->psz_mime, "image/x-portable-anymap" ) )  || /* PNM */
710                  ( !strcmp( p_attach->psz_mime, "image/x-portable-bitmap" ) )  || /* PBM */
711                  ( !strcmp( p_attach->psz_mime, "image/x-portable-graymap" ) ) || /* PGM */
712                  ( !strcmp( p_attach->psz_mime, "image/x-portable-pixmap" ) ) )   /* PPM */
713         {
714             type = VLC_FOURCC('p','n','m',' ');
715         }
716         else if ( !strcmp( p_attach->psz_mime, "image/gif" ) )         /* GIF */
717             type = VLC_FOURCC('g','i','f',' ');
718         else if ( !strcmp( p_attach->psz_mime, "image/jpeg" ) )        /* JPG, JPEG */
719             type = VLC_FOURCC('j','p','e','g');
720         else if ( !strcmp( p_attach->psz_mime, "image/pcx" ) )         /* PCX */
721             type = VLC_FOURCC('p','c','x',' ');
722         else if ( !strcmp( p_attach->psz_mime, "image/png" ) )         /* PNG */
723             type = VLC_FOURCC('p','n','g',' ');
724         else if ( !strcmp( p_attach->psz_mime, "image/tiff" ) )        /* TIF, TIFF */
725             type = VLC_FOURCC('t','i','f','f');
726         else if ( !strcmp( p_attach->psz_mime, "image/x-tga" ) )       /* TGA */
727             type = VLC_FOURCC('t','g','a',' ');
728         else if ( !strcmp( p_attach->psz_mime, "image/x-xpixmap") )    /* XPM */
729             type = VLC_FOURCC('x','p','m',' ');
730
731         if( ( type != 0 ) &&
732             ( p_attach->i_data > 0 ) &&
733             ( p_attach->p_data != NULL ) )
734         {
735             picture_t         *p_pic = NULL;
736             image_handler_t   *p_image;
737
738             p_image = image_HandlerCreate( p_dec );
739             if( p_image != NULL )
740             {
741                 block_t   *p_block;
742
743                 p_block = block_New( p_image->p_parent, p_attach->i_data );
744
745                 if( p_block != NULL )
746                 {
747                     video_format_t     fmt_in;
748                     video_format_t     fmt_out;
749
750                     memcpy( p_block->p_buffer, p_attach->p_data, p_attach->i_data );
751
752                     memset( &fmt_in,  0, sizeof( video_format_t));
753                     memset( &fmt_out, 0, sizeof( video_format_t));
754
755                     fmt_in.i_chroma  = type;
756                     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
757
758                     /* Find a suitable decoder module */
759                     if( module_Exists( p_dec, "SDL Image decoder" ) )
760                     {
761                         /* ffmpeg thinks it can handle bmp properly but it can't (at least
762                          * not all of them), so use sdl_image if it is available */
763
764                         vlc_value_t val;
765
766                         var_Create( p_dec, "codec", VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
767                         val.psz_string = (char*) "sdl_image";
768                         var_Set( p_dec, "codec", val );
769                     }
770
771                     p_pic = image_Read( p_image, p_block, &fmt_in, &fmt_out );
772                     var_Destroy( p_dec, "codec" );
773                 }
774
775                 image_HandlerDelete( p_image );
776             }
777             if( p_pic )
778             {
779                 image_attach_t *p_picture = malloc( sizeof(image_attach_t) );
780
781                 if( p_picture )
782                 {
783                     p_picture->psz_filename = strdup( p_attach->psz_name );
784                     p_picture->p_pic = p_pic;
785
786                     TAB_APPEND( p_sys->i_images, p_sys->pp_images, p_picture );
787                 }
788             }
789         }
790         vlc_input_attachment_Delete( pp_attachments[ k ] );
791     }
792     free( pp_attachments );
793
794     return VLC_SUCCESS;
795 }
796
797 char* GotoNextLine( char *psz_text )
798 {
799     char *p_newline = psz_text;
800
801     while( p_newline[0] != '\0' )
802     {
803         if( p_newline[0] == '\n' || p_newline[0] == '\r' )
804         {
805             p_newline++;
806             while( p_newline[0] == '\n' || p_newline[0] == '\r' )
807                 p_newline++;
808             break;
809         }
810         else p_newline++;
811     }
812     return p_newline;
813 }
814
815 /* Function now handles tags which has attribute values, and tries
816  * to deal with &' commands too. It no longer modifies the string
817  * in place, so that the original text can be reused
818  */
819 static char *StripTags( char *psz_subtitle )
820 {
821     char *psz_text_start;
822     char *psz_text;
823
824     psz_text = psz_text_start = malloc( strlen( psz_subtitle ) + 1 );
825     if( !psz_text_start )
826         return NULL;
827
828     while( *psz_subtitle )
829     {
830         if( *psz_subtitle == '<' )
831         {
832             if( strncasecmp( psz_subtitle, "<br/>", 5 ) == 0 )
833                 *psz_text++ = '\n';
834
835             psz_subtitle += strcspn( psz_subtitle, ">" );
836         }
837         else if( *psz_subtitle == '&' )
838         {
839             if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
840             {
841                 *psz_text++ = '<';
842                 psz_subtitle += strcspn( psz_subtitle, ";" );
843             }
844             else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
845             {
846                 *psz_text++ = '>';
847                 psz_subtitle += strcspn( psz_subtitle, ";" );
848             }
849             else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
850             {
851                 *psz_text++ = '&';
852                 psz_subtitle += strcspn( psz_subtitle, ";" );
853             }
854             else if( !strncasecmp( psz_subtitle, "&quot;", 6 ))
855             {
856                 *psz_text++ = '\"';
857                 psz_subtitle += strcspn( psz_subtitle, ";" );
858             }
859             else
860             {
861                 /* Assume it is just a normal ampersand */
862                 *psz_text++ = '&';
863             }
864         }
865         else
866         {
867             *psz_text++ = *psz_subtitle;
868         }
869
870         psz_subtitle++;
871     }
872     *psz_text = '\0';
873     psz_text_start = realloc( psz_text_start, strlen( psz_text_start ) + 1 );
874
875     return psz_text_start;
876 }
877
878 /* Try to respect any style tags present in the subtitle string. The main
879  * problem here is a lack of adequate specs for the subtitle formats.
880  * SSA/ASS and USF are both detail spec'ed -- but they are handled elsewhere.
881  * SAMI has a detailed spec, but extensive rework is needed in the demux
882  * code to prevent all this style information being excised, as it presently
883  * does.
884  * That leaves the others - none of which were (I guess) originally intended
885  * to be carrying style information. Over time people have used them that way.
886  * In the absence of specifications from which to work, the tags supported
887  * have been restricted to the simple set permitted by the USF DTD, ie. :
888  *  Basic: <br>, <i>, <b>, <u>
889  *  Extended: <font>
890  *    Attributes: face
891  *                family
892  *                size
893  *                color
894  *                outline-color
895  *                shadow-color
896  *                outline-level
897  *                shadow-level
898  *                back-color
899  *                alpha
900  * There is also the further restriction that the subtitle be well-formed
901  * as an XML entity, ie. the HTML sentence:
902  *        <b><i>Bold and Italics</b></i>
903  * doesn't qualify because the tags aren't nested one inside the other.
904  * <text> tags are automatically added to the output to ensure
905  * well-formedness.
906  * If the text doesn't qualify for any reason, a NULL string is
907  * returned, and the rendering engine will fall back to the
908  * plain text version of the subtitle.
909  */
910 static char *CreateHtmlSubtitle( char *psz_subtitle )
911 {
912     char    psz_tagStack[ 100 ];
913     size_t  i_buf_size     = strlen( psz_subtitle ) + 100;
914     char   *psz_html_start = malloc( i_buf_size );
915
916     psz_tagStack[ 0 ] = '\0';
917
918     if( psz_html_start != NULL )
919     {
920         char *psz_html = psz_html_start;
921
922         strcpy( psz_html, "<text>" );
923         psz_html += 6;
924
925         while( *psz_subtitle )
926         {
927             if( *psz_subtitle == '\n' )
928             {
929                 strcpy( psz_html, "<br/>" );
930                 psz_html += 5;
931                 psz_subtitle++;
932             }
933             else if( *psz_subtitle == '<' )
934             {
935                 if( !strncasecmp( psz_subtitle, "<br/>", 5 ))
936                 {
937                     strcpy( psz_html, "<br/>" );
938                     psz_html += 5;
939                     psz_subtitle += 5;
940                 }
941                 else if( !strncasecmp( psz_subtitle, "<b>", 3 ) )
942                 {
943                     strcpy( psz_html, "<b>" );
944                     strcat( psz_tagStack, "b" );
945                     psz_html += 3;
946                     psz_subtitle += 3;
947                 }
948                 else if( !strncasecmp( psz_subtitle, "<i>", 3 ) )
949                 {
950                     strcpy( psz_html, "<i>" );
951                     strcat( psz_tagStack, "i" );
952                     psz_html += 3;
953                     psz_subtitle += 3;
954                 }
955                 else if( !strncasecmp( psz_subtitle, "<u>", 3 ) )
956                 {
957                     strcpy( psz_html, "<u>" );
958                     strcat( psz_tagStack, "u" );
959                     psz_html += 3;
960                     psz_subtitle += 3;
961                 }
962                 else if( !strncasecmp( psz_subtitle, "<font ", 6 ))
963                 {
964                     const char *psz_attribs[] = { "face=\"", "family=\"", "size=\"",
965                             "color=\"", "outline-color=\"", "shadow-color=\"",
966                             "outline-level=\"", "shadow-level=\"", "back-color=\"",
967                             "alpha=\"", NULL };
968
969                     strcpy( psz_html, "<font " );
970                     strcat( psz_tagStack, "f" );
971                     psz_html += 6;
972                     psz_subtitle += 6;
973
974                     while( *psz_subtitle != '>' )
975                     {
976                         int  k;
977
978                         for( k=0; psz_attribs[ k ]; k++ )
979                         {
980                             int i_len = strlen( psz_attribs[ k ] );
981
982                             if( !strncasecmp( psz_subtitle, psz_attribs[ k ], i_len ))
983                             {
984                                 i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1;
985
986                                 strncpy( psz_html, psz_subtitle, i_len );
987                                 psz_html += i_len;
988                                 psz_subtitle += i_len;
989                                 break;
990                             }
991                         }
992                         if( psz_attribs[ k ] == NULL )
993                         {
994                             /* Jump over unrecognised tag */
995                             int i_len = strcspn( psz_subtitle, "\"" ) + 1;
996
997                             i_len += strcspn( psz_subtitle + i_len, "\"" ) + 1;
998                             psz_subtitle += i_len;
999                         }
1000                         while (*psz_subtitle == ' ')
1001                             *psz_html++ = *psz_subtitle++;
1002                     }
1003                     *psz_html++ = *psz_subtitle++;
1004                 }
1005                 else if( !strncmp( psz_subtitle, "</", 2 ))
1006                 {
1007                     vlc_bool_t  b_match     = VLC_FALSE;
1008                     int         i_len       = strlen( psz_tagStack ) - 1;
1009                     char       *psz_lastTag = NULL;
1010
1011                     if( i_len >= 0 )
1012                     {
1013                         psz_lastTag = psz_tagStack + i_len;
1014                         i_len = 0;
1015
1016                         switch( *psz_lastTag )
1017                         {
1018                             case 'b':
1019                                 b_match = !strncasecmp( psz_subtitle, "</b>", 4 );
1020                                 i_len   = 4;
1021                                 break;
1022                             case 'i':
1023                                 b_match = !strncasecmp( psz_subtitle, "</i>", 4 );
1024                                 i_len   = 4;
1025                                 break;
1026                             case 'u':
1027                                 b_match = !strncasecmp( psz_subtitle, "</u>", 4 );
1028                                 i_len   = 4;
1029                                 break;
1030                             case 'f':
1031                                 b_match = !strncasecmp( psz_subtitle, "</font>", 7 );
1032                                 i_len   = 7;
1033                                 break;
1034                         }
1035                     }
1036                     if( ! b_match )
1037                     {
1038                         /* Not well formed -- kill everything */
1039                         free( psz_html_start );
1040                         psz_html_start = NULL;
1041                         break;
1042                     }
1043                     *psz_lastTag = '\0';
1044                     strncpy( psz_html, psz_subtitle, i_len );
1045                     psz_html += i_len;
1046                     psz_subtitle += i_len;
1047                 }
1048                 else
1049                 {
1050                     psz_subtitle += strcspn( psz_subtitle, ">" );
1051                 }
1052             }
1053             else if( *psz_subtitle == '&' )
1054             {
1055                 if( !strncasecmp( psz_subtitle, "&lt;", 4 ))
1056                 {
1057                     strcpy( psz_html, "&lt;" );
1058                     psz_html += 4;
1059                     psz_subtitle += 4;
1060                 }
1061                 else if( !strncasecmp( psz_subtitle, "&gt;", 4 ))
1062                 {
1063                     strcpy( psz_html, "&gt;" );
1064                     psz_html += 4;
1065                     psz_subtitle += 4;
1066                 }
1067                 else if( !strncasecmp( psz_subtitle, "&amp;", 5 ))
1068                 {
1069                     strcpy( psz_html, "&amp;" );
1070                     psz_html += 5;
1071                     psz_subtitle += 5;
1072                 }
1073                 else
1074                 {
1075                     strcpy( psz_html, "&amp;" );
1076                     psz_html += 5;
1077                     psz_subtitle++;
1078                 }
1079             }
1080             else
1081             {
1082                 *psz_html = *psz_subtitle;
1083                 if( psz_html > psz_html_start )
1084                 {
1085                     /* Check for double whitespace */
1086                     if((( *psz_html == ' ' ) ||
1087                         ( *psz_html == '\t' )) &&
1088                        (( *(psz_html-1) == ' ' ) ||
1089                         ( *(psz_html-1) == '\t' )))
1090                     {
1091                         strcpy( psz_html, NO_BREAKING_SPACE );
1092                         psz_html += strlen( NO_BREAKING_SPACE ) - 1;
1093                     }
1094                 }
1095                 psz_html++;
1096                 psz_subtitle++;
1097             }
1098
1099             if( ( size_t )( psz_html - psz_html_start ) > i_buf_size - 10 )
1100             {
1101                 int i_len = psz_html - psz_html_start;
1102
1103                 i_buf_size += 100;
1104                 psz_html_start = realloc( psz_html_start, i_buf_size );
1105                 psz_html = psz_html_start + i_len;
1106                 *psz_html = '\0';
1107             }
1108         }
1109         strcpy( psz_html, "</text>" );
1110         psz_html += 7;
1111
1112         if( psz_tagStack[ 0 ] != '\0' )
1113         {
1114             /* Not well formed -- kill everything */
1115             free( psz_html_start );
1116             psz_html_start = NULL;
1117         }
1118         else if( psz_html_start )
1119         {
1120             /* Shrink the memory requirements */
1121             psz_html_start = realloc( psz_html_start,  psz_html - psz_html_start + 1 );
1122         }
1123     }
1124     return psz_html_start;
1125 }
1126
1127 /* The reverse of the above function - given a HTML subtitle, turn it
1128  * into a plain-text version, complete with sensible whitespace compaction
1129  */
1130
1131 char *CreatePlainText( char *psz_subtitle )
1132 {
1133     char *psz_text = StripTags( psz_subtitle );
1134     char *s;
1135
1136     if( !psz_text )
1137         return NULL;
1138
1139     s = strpbrk( psz_text, "\t\r\n " );
1140     while( s )
1141     {
1142         int   k;
1143         char  spc = ' ';
1144         int   i_whitespace = strspn( s, "\t\r\n " );
1145
1146         /* Favour '\n' over other whitespaces - if one of these
1147          * occurs in the whitespace use a '\n' as our value,
1148          * otherwise just use a ' '
1149          */
1150         for( k = 0; k < i_whitespace; k++ )
1151             if( s[k] == '\n' ) spc = '\n';
1152
1153         if( i_whitespace > 1 )
1154         {
1155             memmove( &s[1],
1156                      &s[i_whitespace],
1157                      strlen( s ) - i_whitespace + 1 );
1158         }
1159         *s++ = spc;
1160
1161         s = strpbrk( s, "\t\r\n " );
1162     }
1163     return psz_text;
1164 }
1165
1166 /****************************************************************************
1167  * download and resize image located at psz_url
1168  ***************************************************************************/
1169 subpicture_region_t *LoadEmbeddedImage( decoder_t *p_dec,
1170                                         subpicture_t *p_spu,
1171                                         const char *psz_filename,
1172                                         int i_transparent_color )
1173 {
1174     decoder_sys_t         *p_sys = p_dec->p_sys;
1175     subpicture_region_t   *p_region;
1176     video_format_t         fmt_out;
1177     int                    k;
1178     picture_t             *p_pic = NULL;
1179
1180     for( k = 0; k < p_sys->i_images; k++ )
1181     {
1182         if( p_sys->pp_images &&
1183             !strcmp( p_sys->pp_images[k]->psz_filename, psz_filename ) )
1184         {
1185             p_pic = p_sys->pp_images[k]->p_pic;
1186             break;
1187         }
1188     }
1189
1190     if( !p_pic )
1191     {
1192         msg_Err( p_dec, "Unable to read image %s", psz_filename );
1193         return NULL;
1194     }
1195
1196     /* Display the feed's image */
1197     memset( &fmt_out, 0, sizeof( video_format_t));
1198
1199     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
1200     fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
1201     fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
1202     fmt_out.i_width =
1203         fmt_out.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
1204     fmt_out.i_height =
1205         fmt_out.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
1206
1207     p_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt_out );
1208     if( !p_region )
1209     {
1210         msg_Err( p_dec, "cannot allocate SPU region" );
1211         return NULL;
1212     }
1213     vout_CopyPicture( p_dec, &p_region->picture, p_pic );
1214
1215     /* This isn't the best way to do this - if you really want transparency, then
1216      * you're much better off using an image type that supports it like PNG. The
1217      * spec requires this support though.
1218      */
1219     if( i_transparent_color > 0 )
1220     {
1221         uint8_t i_r = ( i_transparent_color >> 16 ) & 0xff;
1222         uint8_t i_g = ( i_transparent_color >>  8 ) & 0xff;
1223         uint8_t i_b = ( i_transparent_color       ) & 0xff;
1224         uint8_t i_y = ( ( (  66 * i_r + 129 * i_g +  25 * i_b + 128 ) >> 8 ) + 16 );
1225         uint8_t i_u =   ( ( -38 * i_r -  74 * i_g + 112 * i_b + 128 ) >> 8 ) + 128 ;
1226         uint8_t i_v =   ( ( 112 * i_r -  94 * i_g -  18 * i_b + 128 ) >> 8 ) + 128 ;
1227
1228         if( ( p_region->picture.Y_PITCH == p_region->picture.U_PITCH ) &&
1229             ( p_region->picture.Y_PITCH == p_region->picture.V_PITCH ) &&
1230             ( p_region->picture.Y_PITCH == p_region->picture.A_PITCH ) )
1231         {
1232             int i_lines = p_region->picture.p[ Y_PLANE ].i_lines;
1233             if( i_lines > p_region->picture.p[ U_PLANE ].i_lines )
1234                 i_lines = p_region->picture.p[ U_PLANE ].i_lines;
1235             if( i_lines > p_region->picture.p[ V_PLANE ].i_lines )
1236                 i_lines = p_region->picture.p[ V_PLANE ].i_lines;
1237             if( i_lines > p_region->picture.p[ A_PLANE ].i_lines )
1238                 i_lines = p_region->picture.p[ A_PLANE ].i_lines;
1239
1240             int   i;
1241
1242             for( i = 0; i < p_region->picture.A_PITCH * i_lines; i++ )
1243             {
1244                 if(( p_region->picture.Y_PIXELS[ i ] == i_y ) &&
1245                    ( p_region->picture.U_PIXELS[ i ] == i_u ) &&
1246                    ( p_region->picture.V_PIXELS[ i ] == i_v ) )
1247                 {
1248                     p_region->picture.A_PIXELS[ i ] = 1;
1249                 }
1250             }
1251         }
1252     }
1253     return p_region;
1254 }