1 /*****************************************************************************
2 * dvbsub.c : DVB subtitles decoder
3 * DVB subtitles encoder (developed for Anevia, www.anevia.com)
4 *****************************************************************************
5 * Copyright (C) 2003 ANEVIA
6 * Copyright (C) 2003-2004 VideoLAN
9 * Authors: Damien LUCAS <damien.lucas@anevia.com>
10 * Laurent Aimar <fenrir@via.ecp.fr>
11 * Gildas Bazin <gbazin@videolan.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
26 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <vlc/decoder.h>
37 //#define DEBUG_DVBSUB 1
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
44 static subpicture_t *Decode( decoder_t *, block_t ** );
46 static int OpenEncoder ( vlc_object_t * );
47 static void CloseEncoder( vlc_object_t * );
48 static block_t *Encode ( encoder_t *, subpicture_t * );
51 set_description( _("DVB subtitles decoder") );
52 set_capability( "decoder", 50 );
53 set_callbacks( Open, Close );
55 # define ENC_CFG_PREFIX "sout-dvbsub-"
57 set_description( _("DVB subtitles encoder") );
58 set_capability( "encoder", 100 );
59 set_callbacks( OpenEncoder, CloseEncoder );
62 static const char *ppsz_enc_options[] = { NULL };
64 /****************************************************************************
66 ****************************************************************************
67 * Those structures refer closely to the ETSI 300 743 Object model
68 ****************************************************************************/
70 /* Storage of a RLE entry */
71 typedef struct dvbsub_rle_s
80 struct dvbsub_rle_s *p_next;
84 /* A subpicture image is a list of codes
85 * We need to store the length of each line since nothing specify in
86 * the standard that all lines should have the same length
87 * WARNING: We assume here that a spu is less than 576 lines high */
93 dvbsub_rle_t *p_codes;
97 /* The object definition gives the position of the object in a region */
98 typedef struct dvbsub_objectdef_s
108 } dvbsub_objectdef_t;
110 /* An object is constituted of 2 images (for interleaving) */
111 typedef struct dvbsub_object_s
114 uint8_t i_version_number;
115 uint8_t i_coding_method;
116 vlc_bool_t b_non_modify_color;
117 dvbsub_image_t *topfield;
118 dvbsub_image_t *bottomfield;
119 struct dvbsub_object_s *p_next;
123 /* The object definition gives the position of the object in a region */
124 typedef struct dvbsub_regiondef_s
130 } dvbsub_regiondef_t;
132 /* The Region is an aera on the image
133 * with a list of the object definitions associated and a CLUT */
134 typedef struct dvbsub_region_s
137 uint8_t i_version_number;
143 uint8_t i_level_comp;
151 dvbsub_objectdef_t *p_object_defs;
153 struct dvbsub_region_s *p_next;
157 /* The page defines the list of regions */
163 uint8_t i_version_number;
165 uint8_t i_region_defs;
166 dvbsub_regiondef_t *p_region_defs;
170 /* The entry in the palette CLUT */
184 uint8_t i_version_number;
185 dvbsub_color_t c_2b[4];
186 dvbsub_color_t c_4b[16];
187 dvbsub_color_t c_8b[256];
195 /* Decoder internal data */
200 dvbsub_page_t *p_page;
201 dvbsub_region_t *p_regions;
202 dvbsub_object_t *p_objects;
204 dvbsub_clut_t *p_clut[256];
205 dvbsub_clut_t default_clut;
209 // List of different SEGMENT TYPES
210 // According to EN 300-743, table 2
211 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
212 #define DVBSUB_ST_REGION_COMPOSITION 0x11
213 #define DVBSUB_ST_CLUT_DEFINITION 0x12
214 #define DVBSUB_ST_OBJECT_DATA 0x13
215 #define DVBSUB_ST_ENDOFDISPLAY 0x80
216 #define DVBSUB_ST_STUFFING 0xff
217 // List of different OBJECT TYPES
218 // According to EN 300-743, table 6
219 #define DVBSUB_OT_BASIC_BITMAP 0x00
220 #define DVBSUB_OT_BASIC_CHAR 0x01
221 #define DVBSUB_OT_COMPOSITE_STRING 0x02
223 // According to EN 300-743, table 9
224 #define DVBSUB_DT_2BP_CODE_STRING 0x10
225 #define DVBSUB_DT_4BP_CODE_STRING 0x11
226 #define DVBSUB_DT_8BP_CODE_STRING 0x12
227 #define DVBSUB_DT_24_TABLE_DATA 0x20
228 #define DVBSUB_DT_28_TABLE_DATA 0x21
229 #define DVBSUB_DT_48_TABLE_DATA 0x22
230 #define DVBSUB_DT_END_LINE 0xf0
231 // List of different Page Composition Segment state
232 // According to EN 300-743, 7.2.1 table 3
233 #define DVBSUB_PCS_STATE_ACQUISITION 0x01
234 #define DVBSUB_PCS_STATE_CHANGE 0x10
236 /*****************************************************************************
238 *****************************************************************************/
239 static void decode_segment( decoder_t *, bs_t * );
240 static void decode_page_composition( decoder_t *, bs_t * );
241 static void decode_region_composition( decoder_t *, bs_t * );
242 static void decode_object( decoder_t *, bs_t * );
243 static void decode_clut( decoder_t *, bs_t * );
245 static void free_objects( decoder_t * );
246 static void free_all( decoder_t * );
248 static subpicture_t *render( decoder_t * );
250 static void default_clut_init( decoder_t * );
252 /*****************************************************************************
253 * Open: probe the decoder and return score
254 *****************************************************************************
255 * Tries to launch a decoder and return score so that the interface is able
257 *****************************************************************************/
258 static int Open( vlc_object_t *p_this )
260 decoder_t *p_dec = (decoder_t *) p_this;
261 decoder_sys_t *p_sys;
264 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
269 p_dec->pf_decode_sub = Decode;
270 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
273 p_sys->i_id = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
274 p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
275 p_sys->p_page = NULL;
276 p_sys->p_regions = NULL;
277 p_sys->p_objects = NULL;
278 for( i = 0; i < 256; i++ ) p_sys->p_clut[i] = NULL;
280 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
282 default_clut_init( p_dec );
287 /*****************************************************************************
289 *****************************************************************************/
290 static void Close( vlc_object_t *p_this )
292 decoder_t *p_dec = (decoder_t*) p_this;
293 decoder_sys_t *p_sys = p_dec->p_sys;
299 /*****************************************************************************
301 *****************************************************************************/
302 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
304 decoder_sys_t *p_sys = p_dec->p_sys;
306 subpicture_t *p_spu = NULL;
308 if( pp_block == NULL || *pp_block == NULL ) return NULL;
312 p_sys->i_pts = p_block->i_pts;
313 if( p_sys->i_pts <= 0 )
316 /* Some DVB channels send stuffing segments in non-dated packets so
317 * don't complain too loudly. */
318 msg_Warn( p_dec, "non dated subtitle" );
320 block_Release( p_block );
324 bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
326 if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
328 msg_Dbg( p_dec, "invalid data identifier" );
329 block_Release( p_block );
333 if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
335 msg_Dbg( p_dec, "invalid subtitle stream id" );
336 block_Release( p_block );
341 msg_Dbg( p_dec, "subtitle packet received: "I64Fd, p_sys->i_pts );
344 while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
346 decode_segment( p_dec, &p_sys->bs );
349 if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
351 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
352 block_Release( p_block );
356 /* Check if the page is to be displayed */
357 if( p_sys->p_page ) p_spu = render( p_dec );
359 block_Release( p_block );
364 /* following functions are local */
366 /*****************************************************************************
367 * default_clut_init: default clut as defined in EN 300-743 section 10
368 *****************************************************************************/
369 static void default_clut_init( decoder_t *p_dec )
371 decoder_sys_t *p_sys = p_dec->p_sys;
374 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
375 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
376 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
379 for( i = 0; i < 4; i++ )
381 uint8_t R = 0, G = 0, B = 0, T = 0;
383 if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
384 else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
385 else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
386 else R = G = B = 0x7F;
388 p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
389 p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
390 p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
391 p_sys->default_clut.c_2b[i].T = T;
394 /* 16 entries CLUT */
395 for( i = 0; i < 16; i++ )
397 uint8_t R = 0, G = 0, B = 0, T = 0;
401 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
407 R = (i & 0x1) ? 0xFF : 0;
408 G = (i & 0x2) ? 0xFF : 0;
409 B = (i & 0x4) ? 0xFF : 0;
414 R = (i & 0x1) ? 0x7F : 0;
415 G = (i & 0x2) ? 0x7F : 0;
416 B = (i & 0x4) ? 0x7F : 0;
419 p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
420 p_sys->default_clut.c_4b[i].Cr = RGB_TO_U(R,G,B);
421 p_sys->default_clut.c_4b[i].Cb = RGB_TO_V(R,G,B);
422 p_sys->default_clut.c_4b[i].T = T;
425 /* 256 entries CLUT (TODO) */
426 memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
429 static void decode_segment( decoder_t *p_dec, bs_t *s )
431 decoder_sys_t *p_sys = p_dec->p_sys;
436 /* sync_byte (already checked) */
440 i_type = bs_read( s, 8 );
443 i_page_id = bs_read( s, 16 );
446 i_size = bs_show( s, 16 );
448 if( i_page_id != p_sys->i_id && i_page_id != p_sys->i_ancillary_id )
451 msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
452 i_page_id, p_sys->i_id );
454 bs_skip( s, 8 * ( 2 + i_size ) );
459 if( i_page_id == p_sys->i_id )
460 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
462 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
467 case DVBSUB_ST_PAGE_COMPOSITION:
469 msg_Dbg( p_dec, "decode_page_composition" );
471 decode_page_composition( p_dec, s );
474 case DVBSUB_ST_REGION_COMPOSITION:
476 msg_Dbg( p_dec, "decode_region_composition" );
478 decode_region_composition( p_dec, s );
481 case DVBSUB_ST_CLUT_DEFINITION:
483 msg_Dbg( p_dec, "decode_clut" );
485 decode_clut( p_dec, s );
488 case DVBSUB_ST_OBJECT_DATA:
490 msg_Dbg( p_dec, "decode_object" );
492 decode_object( p_dec, s );
495 case DVBSUB_ST_ENDOFDISPLAY:
497 msg_Dbg( p_dec, "end of display" );
499 bs_skip( s, 8 * ( 2 + i_size ) );
502 case DVBSUB_ST_STUFFING:
504 msg_Dbg( p_dec, "skip stuffing" );
506 bs_skip( s, 8 * ( 2 + i_size ) );
510 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
511 bs_skip( s, 8 * ( 2 + i_size ) );
516 static void decode_clut( decoder_t *p_dec, bs_t *s )
518 decoder_sys_t *p_sys = p_dec->p_sys;
519 uint16_t i_segment_length;
520 uint16_t i_processed_length;
521 dvbsub_clut_t *p_clut;
523 uint8_t i_version_number;
525 i_segment_length = bs_read( s, 16 );
526 i_clut_id = bs_read( s, 8 );
527 i_version_number = bs_read( s, 4 );
529 /* Check that this id doesn't not already exist with the same version
530 * number and allocate memory if necessary */
531 if( p_sys->p_clut[i_clut_id] != NULL &&
532 p_sys->p_clut[i_clut_id]->i_version_number == i_version_number )
535 bs_skip( s, 8 * i_segment_length - 12 );
539 if( !p_sys->p_clut[i_clut_id] )
541 p_sys->p_clut[i_clut_id] = malloc( sizeof(dvbsub_clut_t) );
544 p_clut = p_sys->p_clut[i_clut_id];
546 /* We don't have this version of the CLUT: Parse it */
547 p_clut->i_version_number = i_version_number;
548 bs_skip( s, 4 ); /* Reserved bits */
549 i_processed_length = 2;
550 while( i_processed_length < i_segment_length )
552 uint8_t y, cb, cr, t;
556 i_id = bs_read( s, 8 );
557 i_type = bs_read( s, 3 );
561 if( bs_read( s, 1 ) )
564 cr = bs_read( s, 8 );
565 cb = bs_read( s, 8 );
567 i_processed_length += 6;
571 y = bs_read( s, 6 ) << 2;
572 cr = bs_read( s, 4 ) << 4;
573 cb = bs_read( s, 4 ) << 4;
574 t = bs_read( s, 2 ) << 6;
575 i_processed_length += 4;
578 /* We are not entirely compliant here as full transparency is indicated
579 * with a luma value of zero, not a transparency value of 0xff
580 * (full transparency would actually be 0xff + 1). */
588 /* According to EN 300-743 section 7.2.3 note 1, type should
589 * not have more than 1 bit set to one, but some streams don't
590 * respect this note. */
594 p_clut->c_2b[i_id].Y = y;
595 p_clut->c_2b[i_id].Cr = cr;
596 p_clut->c_2b[i_id].Cb = cb;
597 p_clut->c_2b[i_id].T = t;
601 p_clut->c_4b[i_id].Y = y;
602 p_clut->c_4b[i_id].Cr = cr;
603 p_clut->c_4b[i_id].Cb = cb;
604 p_clut->c_4b[i_id].T = t;
608 p_clut->c_8b[i_id].Y = y;
609 p_clut->c_8b[i_id].Cr = cr;
610 p_clut->c_8b[i_id].Cb = cb;
611 p_clut->c_8b[i_id].T = t;
616 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
618 decoder_sys_t *p_sys = p_dec->p_sys;
619 unsigned int i_version_number;
620 unsigned int i_state;
621 unsigned int i_segment_length;
625 /* A page is composed by one or more region */
627 i_segment_length = bs_read( s, 16 );
628 i_timeout = bs_read( s, 8 );
629 i_version_number = bs_read( s, 4 );
630 i_state = bs_read( s, 2 );
631 bs_skip( s, 2 ); /* Reserved */
633 if( i_state == DVBSUB_PCS_STATE_CHANGE )
635 /* End of an epoch, reset decoder buffer */
637 msg_Dbg( p_dec, "page composition mode change" );
641 else if( !p_sys->p_page && i_state != DVBSUB_PCS_STATE_ACQUISITION )
643 /* Not a full PCS, we need to wait for one */
644 bs_skip( s, 8 * (i_segment_length - 2) );
648 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
651 msg_Dbg( p_dec, "acquisition page composition" );
653 /* Make sure we clean up regularly our objects list.
654 * Is it the best place to do this ? */
655 free_objects( p_dec );
658 /* Check version number */
660 p_sys->p_page->i_version_number == i_version_number )
662 bs_skip( s, 8 * (i_segment_length - 2) );
665 else if( p_sys->p_page )
667 if( p_sys->p_page->i_region_defs )
668 free( p_sys->p_page->p_region_defs );
669 p_sys->p_page->i_region_defs = 0;
674 /* Allocate a new page */
675 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
678 p_sys->p_page->i_version_number = i_version_number;
679 p_sys->p_page->i_timeout = i_timeout;
681 /* Number of regions */
682 p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
684 if( p_sys->p_page->i_region_defs == 0 ) return;
686 p_sys->p_page->p_region_defs =
687 malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
688 for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
690 p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
691 bs_skip( s, 8 ); /* Reserved */
692 p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
693 p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
696 msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
697 i, p_sys->p_page->p_region_defs[i].i_x,
698 p_sys->p_page->p_region_defs[i].i_y );
703 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
705 decoder_sys_t *p_sys = p_dec->p_sys;
706 dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
707 int i_segment_length;
708 int i_processed_length;
710 int i_version_number;
712 i_segment_length = bs_read( s, 16 );
713 i_region_id = bs_read( s, 8 );
714 i_version_number = bs_read( s, 4 );
716 /* Check if we already have this region */
717 for( p_region = p_sys->p_regions; p_region != NULL;
718 p_region = p_region->p_next )
720 pp_region = &p_region->p_next;
721 if( p_region->i_id == i_region_id ) break;
724 /* Check version number */
726 p_region->i_version_number == i_version_number )
728 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
733 if( p_region->i_object_defs )
734 free( p_region->p_object_defs );
740 msg_Dbg( p_dec, "new region: %i", i_region_id );
742 p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
743 p_region->p_next = NULL;
746 /* Region attributes */
747 p_region->i_id = i_region_id;
748 p_region->i_version_number = i_version_number;
749 p_region->b_fill = bs_read( s, 1 );
750 bs_skip( s, 3 ); /* Reserved */
751 p_region->i_width = bs_read( s, 16 );
752 p_region->i_height = bs_read( s, 16 );
753 p_region->i_level_comp = bs_read( s, 3 );
754 p_region->i_depth = bs_read( s, 3 );
755 bs_skip( s, 2 ); /* Reserved */
756 p_region->i_clut = bs_read( s, 8 );
757 p_region->i_8bp_code = bs_read( s, 8 );
758 p_region->i_4bp_code = bs_read( s, 4 );
759 p_region->i_2bp_code = bs_read( s, 2 );
760 bs_skip( s, 2 ); /* Reserved */
761 p_region->p_object_defs = NULL;
762 p_region->i_object_defs = 0;
764 /* List of objects in the region */
765 i_processed_length = 10;
766 while( i_processed_length < i_segment_length )
768 dvbsub_objectdef_t *p_obj;
770 /* We create a new object */
771 p_region->i_object_defs++;
772 p_region->p_object_defs =
773 realloc( p_region->p_object_defs,
774 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
776 /* We parse object properties */
777 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
778 p_obj->i_id = bs_read( s, 16 );
779 p_obj->i_type = bs_read( s, 2 );
780 p_obj->i_provider = bs_read( s, 2 );
781 p_obj->i_x = bs_read( s, 12 );
782 bs_skip( s, 4 ); /* Reserved */
783 p_obj->i_y = bs_read( s, 12 );
785 i_processed_length += 6;
787 if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
788 p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
790 p_obj->i_fg_pc = bs_read( s, 8 );
791 p_obj->i_bg_pc = bs_read( s, 8 );
792 i_processed_length += 2;
797 static dvbsub_image_t *dvbsub_parse_pdata( decoder_t *, bs_t *, uint16_t );
798 static uint16_t dvbsub_pdata2bpp( bs_t *, uint16_t *, dvbsub_image_t *, int );
799 static uint16_t dvbsub_pdata4bpp( bs_t *, uint16_t *, dvbsub_image_t *, int );
800 static uint16_t dvbsub_pdata8bpp( bs_t *, uint16_t *, dvbsub_image_t *, int );
802 static void decode_object( decoder_t *p_dec, bs_t *s )
804 decoder_sys_t *p_sys = p_dec->p_sys;
805 dvbsub_object_t *p_obj, **pp_obj = &p_sys->p_objects;
806 int i_segment_length;
807 int i_version_number;
811 i_segment_length = bs_read( s, 16 );
812 i_obj_id = bs_read( s, 16 );
813 i_version_number = bs_read( s, 4 );
814 i_coding_method = bs_read( s, 2 );
816 if( i_coding_method )
818 /* TODO: DVB subtitling as characters */
819 msg_Dbg( p_dec, "DVB subtitling as characters is not handled!" );
820 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
824 /* Check if we already have this region */
825 for( p_obj = p_sys->p_objects; p_obj != NULL; p_obj = p_obj->p_next )
827 pp_obj = &p_obj->p_next;
828 if( p_obj->i_id == i_obj_id ) break;
831 /* Check version number */
832 if( p_obj && p_obj->i_version_number == i_version_number )
834 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
839 /* Clean structure */
845 msg_Dbg( p_dec, "new object: %i", i_obj_id );
847 p_obj = *pp_obj = malloc( sizeof(dvbsub_object_t) );
848 p_obj->p_next = NULL;
851 p_obj->i_id = i_obj_id;
852 p_obj->i_version_number = i_version_number;
853 p_obj->i_coding_method = i_coding_method;
854 p_obj->b_non_modify_color = bs_read( s, 1 );
855 bs_skip( s, 1 ); /* Reserved */
857 if( p_obj->i_coding_method == 0x00 )
859 uint16_t i_topfield_length;
860 uint16_t i_bottomfield_length;
862 i_topfield_length = bs_read( s, 16 );
863 i_bottomfield_length = bs_read( s, 16 );
866 dvbsub_parse_pdata( p_dec, s, i_topfield_length );
868 dvbsub_parse_pdata( p_dec, s, i_bottomfield_length );
870 /* Check word-alignement */
872 if( bs_pos( s ) % 16 ) bs_skip( s, 8 );
876 /* TODO: DVB subtitling as characters */
880 msg_Dbg( p_dec, "end object: %i", i_obj_id );
884 static dvbsub_image_t* dvbsub_parse_pdata( decoder_t *p_dec, bs_t *s,
887 dvbsub_image_t* p_image;
888 uint16_t i_processed_length = 0;
889 uint16_t i_lines = 0;
890 uint16_t i_cols_last = 0;
892 p_image = malloc( sizeof(dvbsub_image_t) );
893 p_image->p_last = p_image->p_codes = NULL;
895 memset( p_image->i_cols, 0, 576 * sizeof(uint16_t) );
897 /* Let's parse it a first time to determine the size of the buffer */
898 while( i_processed_length < length )
900 i_processed_length++;
902 switch( bs_read( s, 8 ) )
905 i_processed_length +=
906 dvbsub_pdata2bpp( s, &p_image->i_cols[i_lines],
907 p_image, length - i_processed_length );
910 i_processed_length +=
911 dvbsub_pdata4bpp( s, &p_image->i_cols[i_lines],
912 p_image, length - i_processed_length );
915 i_processed_length +=
916 dvbsub_pdata8bpp( s, &p_image->i_cols[i_lines],
917 p_image, length - i_processed_length );
922 /* We don't use map tables */
925 i_lines++; /* End of line code */
930 p_image->i_rows = i_lines;
931 p_image->i_cols[i_lines] = i_cols_last;
936 static void add_rle_code( dvbsub_image_t *p, uint16_t num, uint8_t color,
939 if( p->p_last != NULL )
941 p->p_last->p_next = malloc( sizeof(dvbsub_rle_t) );
942 p->p_last = p->p_last->p_next;
946 p->p_codes = malloc( sizeof(dvbsub_rle_t) );
947 p->p_last = p->p_codes;
949 p->p_last->i_num = num;
951 p->p_last->i_color_code = color;
952 p->p_last->i_bpp = i_bpp;
953 p->p_last->p_next = NULL;
956 static uint16_t dvbsub_pdata2bpp( bs_t *s, uint16_t* p,
957 dvbsub_image_t* p_image, int i_length )
959 uint16_t i_processed = 0;
960 vlc_bool_t b_stop = 0;
961 uint16_t i_count = 0;
964 while( !b_stop && i_processed/8 < i_length )
967 if( (i_color = bs_read( s, 2 )) != 0x00 )
972 add_rle_code( p_image, 1, i_color, 2 );
977 if( bs_read( s, 1 ) == 0x01 ) // Switch1
979 i_count = 3 + bs_read( s, 3 );
981 i_color = bs_read( s, 2 );
982 add_rle_code( p_image, i_count, i_color, 2 );
988 if( bs_read( s, 1 ) == 0x00 ) //Switch2
991 switch( bs_read( s, 2 ) ) //Switch3
998 add_rle_code( p_image, 2, 0, 2 );
1001 i_count = 12 + bs_read( s, 4 );
1002 i_color = bs_read( s, 2 );
1005 add_rle_code( p_image, i_count, i_color, 2 );
1008 i_count = 29 + bs_read( s, 8 );
1009 i_color = bs_read( s, 2 );
1012 add_rle_code( p_image, i_count, i_color, 2 );
1021 add_rle_code( p_image, 1, 0, 2 ); /* 1 pixel color 0 */
1029 return ( i_processed + 7 ) / 8;
1032 static uint16_t dvbsub_pdata4bpp( bs_t *s, uint16_t* p,
1033 dvbsub_image_t* p_image, int i_length )
1035 uint16_t i_processed = 0;
1036 vlc_bool_t b_stop = 0;
1037 uint16_t i_count = 0;
1038 uint8_t i_color = 0;
1040 while( !b_stop && i_processed/8 < i_length )
1042 if( (i_color = bs_read( s, 4 )) != 0x00 )
1048 add_rle_code( p_image, 1, i_color, 4 );
1052 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1054 if( bs_show( s, 3 ) != 0x00 )
1056 i_count = 2 + bs_read( s, 3 );
1058 add_rle_code( p_image, i_count, 0x00, 4 );
1069 if( bs_read( s, 1 ) == 0x00) //Switch2
1071 i_count = 4 + bs_read( s, 2 );
1072 i_color = bs_read( s, 4 );
1075 add_rle_code( p_image, i_count, i_color, 4 );
1079 switch ( bs_read( s, 2 ) ) //Switch3
1084 add_rle_code( p_image, 1, 0x00, 4 );
1089 add_rle_code( p_image, 2, 0x00, 4 );
1092 i_count = 9 + bs_read( s, 4 );
1093 i_color = bs_read( s, 4 );
1096 add_rle_code( p_image, i_count, i_color, 4 );
1099 i_count= 25 + bs_read( s, 8 );
1100 i_color = bs_read( s, 4 );
1103 add_rle_code( p_image, i_count, i_color, 4 );
1113 return ( i_processed + 7 ) / 8 ;
1116 static uint16_t dvbsub_pdata8bpp( bs_t *s, uint16_t* p,
1117 dvbsub_image_t* p_image, int i_length )
1119 uint16_t i_processed = 0;
1120 vlc_bool_t b_stop = 0;
1121 uint16_t i_count = 0;
1122 uint8_t i_color = 0;
1124 while( !b_stop && i_processed/8 < i_length )
1127 if( (i_color = bs_read( s, 8 )) != 0x00 )
1132 add_rle_code( p_image, 1, i_color, 8 );
1137 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1139 if( bs_show( s, 7 ) != 0x00 )
1141 i_count = bs_read( s, 7 );
1143 add_rle_code( p_image, i_count, 0x00, 8 );
1154 i_count = bs_read( s, 7 );
1156 i_color = bs_read( s, 8 );
1157 add_rle_code( p_image, i_count, i_color, 8 );
1165 return ( i_processed + 7 ) / 8 ;
1168 static void free_image( dvbsub_image_t *p_i )
1171 dvbsub_rle_t *p2 = NULL;
1173 for( p1 = p_i->p_codes; p1 != NULL; p1 = p2 )
1183 static void free_objects( decoder_t *p_dec )
1185 decoder_sys_t *p_sys = p_dec->p_sys;
1186 dvbsub_object_t *p_obj, *p_obj_next;
1188 for( p_obj = p_sys->p_objects; p_obj != NULL; p_obj = p_obj_next )
1190 p_obj_next = p_obj->p_next;
1191 free_image( p_obj->topfield );
1192 free_image( p_obj->bottomfield );
1195 p_sys->p_objects = NULL;
1198 static void free_all( decoder_t *p_dec )
1200 decoder_sys_t *p_sys = p_dec->p_sys;
1201 dvbsub_region_t *p_reg, *p_reg_next;
1204 for( i = 0; i < 256; i++ )
1206 if( p_sys->p_clut[i] ) free( p_sys->p_clut[i] );
1207 p_sys->p_clut[i] = NULL;
1212 if( p_sys->p_page->i_region_defs )
1213 free( p_sys->p_page->p_region_defs );
1214 free( p_sys->p_page );
1215 p_sys->p_page = NULL;
1218 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1220 p_reg_next = p_reg->p_next;
1221 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1224 p_sys->p_regions = NULL;
1226 free_objects( p_dec );
1229 static subpicture_t *render( decoder_t *p_dec )
1231 decoder_sys_t *p_sys = p_dec->p_sys;
1232 dvbsub_clut_t *p_clut;
1234 subpicture_t *p_spu;
1235 subpicture_region_t **pp_spu_region;
1236 int i, j = 0, i_timeout = 0;
1238 /* Allocate the subpicture internal data. */
1239 p_spu = p_dec->pf_spu_buffer_new( p_dec );
1240 if( !p_spu ) return NULL;
1242 pp_spu_region = &p_spu->p_region;
1244 /* Loop on region definitions */
1247 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1250 for( i = 0; p_sys->p_page && i < p_sys->p_page->i_region_defs; i++ )
1252 dvbsub_region_t *p_region;
1253 dvbsub_regiondef_t *p_regiondef;
1254 subpicture_region_t *p_spu_region;
1255 dvbsub_color_t *p_color;
1258 int i_pitch, i_background;
1260 i_timeout = p_sys->p_page->i_timeout;
1262 p_regiondef = &p_sys->p_page->p_region_defs[i];
1265 msg_Dbg( p_dec, "rendering region %i (%i,%i)", i,
1266 p_regiondef->i_x, p_regiondef->i_y );
1269 /* Find associated region */
1270 for( p_region = p_sys->p_regions; p_region != NULL;
1271 p_region = p_region->p_next )
1273 if( p_regiondef->i_id == p_region->i_id ) break;
1278 msg_Err( p_dec, "no region founddddd!!!" );
1282 /* Create new SPU region */
1283 memset( &fmt, 0, sizeof(video_format_t) );
1284 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
1285 fmt.i_aspect = VOUT_ASPECT_FACTOR;
1286 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1287 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1288 fmt.i_x_offset = fmt.i_y_offset = 0;
1289 p_spu_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
1292 msg_Err( p_dec, "cannot allocate SPU region" );
1295 p_spu_region->i_x = p_regiondef->i_x;
1296 p_spu_region->i_y = p_regiondef->i_y;
1297 *pp_spu_region = p_spu_region;
1298 pp_spu_region = &p_spu_region->p_next;
1301 p_clut = p_sys->p_clut[p_region->i_clut];
1302 if( !p_clut ) p_clut = &p_sys->default_clut;
1303 fmt.p_palette->i_entries = p_region->i_depth == 1 ? 4 :
1304 p_region->i_depth == 2 ? 16 : 256;
1305 p_color = (p_region->i_depth == 1) ? p_clut->c_2b :
1306 (p_region->i_depth == 2) ? p_clut->c_4b : p_clut->c_8b;
1307 for( j = 0; j < fmt.p_palette->i_entries; j++ )
1309 fmt.p_palette->palette[j][0] = p_color[j].Y;
1310 fmt.p_palette->palette[j][1] = p_color[j].Cr;
1311 fmt.p_palette->palette[j][2] = p_color[j].Cb;
1312 fmt.p_palette->palette[j][3] = 0xff - p_color[j].T;
1315 p_y = p_spu_region->picture.Y_PIXELS;
1316 i_pitch = p_spu_region->picture.Y_PITCH;
1319 i_background = (p_region->i_depth == 1) ? p_region->i_2bp_code :
1320 (p_region->i_depth == 2) ? p_region->i_4bp_code :
1321 p_region->i_8bp_code;
1322 memset( p_y, i_background, i_pitch * p_region->i_height );
1324 /* Loop on object definitions */
1325 for( j = 0; j < p_region->i_object_defs; j++ )
1327 dvbsub_object_t *p_object;
1328 dvbsub_objectdef_t *p_objectdef;
1329 uint16_t k, l, x, y;
1331 p_objectdef = &p_region->p_object_defs[j];
1334 msg_Dbg( p_dec, "rendering object %i (%i,%i)", p_objectdef->i_id,
1335 p_objectdef->i_x, p_objectdef->i_y );
1338 /* Look for the right object */
1339 for( p_object = p_sys->p_objects; p_object != NULL;
1340 p_object = p_object->p_next )
1342 if( p_objectdef->i_id == p_object->i_id ) break;
1347 msg_Err( p_dec, "no object founddddd!!!" );
1351 /* Draw SPU region */
1352 for( k = 0, l = 0, p_c = p_object->topfield->p_codes;
1353 p_c; p_c = p_c->p_next )
1355 x = l + p_objectdef->i_x;
1356 y = 2 * k + p_objectdef->i_y;
1357 memset( p_y + y * i_pitch + x, p_c->i_color_code, p_c->i_num );
1360 if( l >= p_object->topfield->i_cols[k] ) { k++; l = 0; }
1361 if( k >= p_object->topfield->i_rows) break;
1364 for( k = 0, l = 0, p_c = p_object->bottomfield->p_codes;
1365 p_c; p_c = p_c->p_next )
1367 x = l + p_objectdef->i_x;
1368 y = 2 * k + 1 + p_objectdef->i_y;
1369 memset( p_y + y * i_pitch + x, p_c->i_color_code, p_c->i_num );
1372 if( l >= p_object->bottomfield->i_cols[k] ) { k++; l = 0; }
1373 if( k >= p_object->bottomfield->i_rows) break;
1379 /* Set the pf_render callback */
1380 p_spu->i_start = p_sys->i_pts;
1381 p_spu->i_stop = p_spu->i_start + i_timeout * 1000000;
1382 p_spu->b_ephemer = VLC_TRUE;
1387 /*****************************************************************************
1388 * encoder_sys_t : encoder descriptor
1389 *****************************************************************************/
1390 struct encoder_sys_t
1392 unsigned int i_page_ver;
1393 unsigned int i_region_ver;
1394 unsigned int i_clut_ver;
1405 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1406 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1407 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1408 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1410 /*****************************************************************************
1411 * OpenEncoder: probe the encoder and return score
1412 *****************************************************************************/
1413 static int OpenEncoder( vlc_object_t *p_this )
1415 encoder_t *p_enc = (encoder_t *)p_this;
1416 encoder_sys_t *p_sys;
1418 if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','v','b','s') &&
1421 return VLC_EGENERIC;
1424 /* Allocate the memory needed to store the decoder's structure */
1425 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1427 msg_Err( p_enc, "out of memory" );
1428 return VLC_EGENERIC;
1430 p_enc->p_sys = p_sys;
1432 p_enc->pf_encode_sub = Encode;
1433 p_enc->fmt_out.i_codec = VLC_FOURCC('d','v','b','s');
1434 p_enc->fmt_out.subs.dvb.i_id = 1 << 16 | 1;
1436 sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1438 p_sys->i_page_ver = 0;
1439 p_sys->i_region_ver = 0;
1440 p_sys->i_clut_ver = 0;
1445 /****************************************************************************
1446 * Encode: the whole thing
1447 ****************************************************************************/
1448 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1450 bs_t bits, *s = &bits;
1453 if( !p_subpic || !p_subpic->p_region ) return 0;
1455 msg_Dbg( p_enc, "encoding subpicture" );
1457 p_block = block_New( p_enc, 64000 );
1458 bs_init( s, p_block->p_buffer, p_block->i_buffer );
1460 bs_write( s, 8, 0x20 ); /* Data identifier */
1461 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
1463 encode_page_composition( p_enc, s, p_subpic );
1464 encode_region_composition( p_enc, s, p_subpic );
1465 encode_clut( p_enc, s, p_subpic );
1466 encode_object( p_enc, s, p_subpic );
1468 /* End of display */
1469 bs_write( s, 8, 0x0f ); /* Sync byte */
1470 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
1471 bs_write( s, 16, 1 ); /* Page id */
1472 bs_write( s, 16, 0 ); /* Segment length */
1474 bs_write( s, 8, 0xff ); /* End marker */
1475 p_block->i_buffer = bs_pos( s ) / 8;
1476 p_block->i_pts = p_block->i_dts = p_subpic->i_start;
1477 if( !p_subpic->b_ephemer && p_subpic->i_stop )
1478 p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
1480 msg_Dbg( p_enc, "subpicture encoded properly" );
1485 /*****************************************************************************
1486 * CloseEncoder: encoder destruction
1487 *****************************************************************************/
1488 static void CloseEncoder( vlc_object_t *p_this )
1490 encoder_t *p_enc = (encoder_t *)p_this;
1491 encoder_sys_t *p_sys = p_enc->p_sys;
1496 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
1497 subpicture_t *p_subpic )
1499 encoder_sys_t *p_sys = p_enc->p_sys;
1500 subpicture_region_t *p_region;
1503 bs_write( s, 8, 0x0f ); /* Sync byte */
1504 bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
1505 bs_write( s, 16, 1 ); /* Page id */
1507 for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1508 p_region = p_region->p_next, i_regions++ );
1510 bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
1512 bs_write( s, 8, 5 ); /* Timeout */
1513 bs_write( s, 4, p_sys->i_page_ver++ );
1514 bs_write( s, 2, DVBSUB_PCS_STATE_ACQUISITION );
1515 bs_write( s, 2, 0 ); /* Reserved */
1517 for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1518 p_region = p_region->p_next, i_regions++ )
1520 bs_write( s, 8, i_regions );
1521 bs_write( s, 8, 0 ); /* Reserved */
1522 bs_write( s, 16, p_region->i_x );
1523 bs_write( s, 16, p_region->i_y );
1527 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1529 encoder_sys_t *p_sys = p_enc->p_sys;
1530 subpicture_region_t *p_region = p_subpic->p_region;
1531 video_palette_t *p_pal;
1535 if( !p_region || !p_region->fmt.p_palette ||
1536 p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
1538 bs_write( s, 8, 0x0f ); /* Sync byte */
1539 bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
1540 bs_write( s, 16, 1 ); /* Page id */
1542 p_pal = p_region->fmt.p_palette;
1544 bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
1545 bs_write( s, 8, 1 ); /* Clut id */
1546 bs_write( s, 4, p_sys->i_clut_ver++ );
1547 bs_write( s, 4, 0 ); /* Reserved */
1549 for( i = 0; i < p_pal->i_entries; i++ )
1551 bs_write( s, 8, i ); /* Clut entry id */
1552 bs_write( s, 1, p_pal->i_entries == 4 ); /* 2bit/entry flag */
1553 bs_write( s, 1, p_pal->i_entries == 16 ); /* 4bit/entry flag */
1554 bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
1555 bs_write( s, 4, 0 ); /* Reserved */
1556 bs_write( s, 1, 1 ); /* Full range flag */
1557 bs_write( s, 8, p_pal->palette[i][0] ); /* Y value */
1558 bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
1559 bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
1560 bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
1564 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
1565 subpicture_t *p_subpic )
1567 encoder_sys_t *p_sys = p_enc->p_sys;
1568 subpicture_region_t *p_region;
1571 for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1572 p_region = p_region->p_next, i_regions++ )
1574 video_palette_t *p_pal = p_region->fmt.p_palette;
1575 int i_depth = p_pal->i_entries == 4 ? 0x1 :
1576 p_pal->i_entries == 16 ? 0x2 : 0x3;
1578 bs_write( s, 8, 0x0f ); /* Sync byte */
1579 bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
1580 bs_write( s, 16, 1 ); /* Page id */
1582 bs_write( s, 16, 10 + 6 ); /* Segment length */
1583 bs_write( s, 8, i_regions );
1584 bs_write( s, 4, p_sys->i_region_ver++ );
1586 /* Region attributes */
1587 bs_write( s, 1, 0 ); /* Fill */
1588 bs_write( s, 3, 0 ); /* Reserved */
1589 bs_write( s, 16, p_region->fmt.i_visible_width );
1590 bs_write( s, 16, p_region->fmt.i_visible_height );
1591 bs_write( s, 3, i_depth ); /* Region level of compatibility */
1592 bs_write( s, 3, i_depth ); /* Region depth */
1593 bs_write( s, 2, 0 ); /* Reserved */
1594 bs_write( s, 8, 1 ); /* Clut id */
1595 bs_write( s, 8, 0 ); /* region 8bit pixel code */
1596 bs_write( s, 4, 0 ); /* region 4bit pixel code */
1597 bs_write( s, 2, 0 ); /* region 2bit pixel code */
1598 bs_write( s, 2, 0 ); /* Reserved */
1600 /* In our implementation we only have 1 object per region */
1601 bs_write( s, 16, i_regions );
1602 bs_write( s, 2, DVBSUB_OT_BASIC_BITMAP );
1603 bs_write( s, 2, 0 ); /* object provider flag */
1604 bs_write( s, 12, 0 ); /* object horizontal position */
1605 bs_write( s, 4, 0 ); /* Reserved */
1606 bs_write( s, 12, 0 ); /* object vertical position */
1610 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1611 subpicture_region_t *p_region,
1614 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1616 encoder_sys_t *p_sys = p_enc->p_sys;
1617 subpicture_region_t *p_region;
1620 int i_length_pos, i_update_pos, i_pixel_data_pos;
1622 for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1623 p_region = p_region->p_next, i_regions++ )
1625 bs_write( s, 8, 0x0f ); /* Sync byte */
1626 bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
1627 bs_write( s, 16, 1 ); /* Page id */
1629 i_length_pos = bs_pos( s );
1630 bs_write( s, 16, 0 ); /* Segment length */
1631 bs_write( s, 16, i_regions ); /* Object id */
1632 bs_write( s, 4, p_sys->i_region_ver++ );
1633 bs_write( s, 2, 0 ); /* object coding method */
1635 bs_write( s, 1, 0 ); /* non modifying color flag */
1636 bs_write( s, 1, 0 ); /* Reserved */
1638 i_update_pos = bs_pos( s );
1639 bs_write( s, 16, 0 ); /* topfield data block length */
1640 bs_write( s, 16, 0 ); /* bottomfield data block length */
1643 i_pixel_data_pos = bs_pos( s );
1644 encode_pixel_data( p_enc, s, p_region, VLC_TRUE );
1645 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1646 SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
1649 i_pixel_data_pos = bs_pos( s );
1650 encode_pixel_data( p_enc, s, p_region, VLC_FALSE );
1651 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1652 SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
1654 /* Stuffing for word alignment */
1656 if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
1658 /* Update segment length */
1659 SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos -2)/8 );
1663 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1664 subpicture_region_t *p_region,
1666 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
1667 subpicture_region_t *p_region,
1669 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1670 subpicture_region_t *p_region,
1673 unsigned int i_line;
1676 if( p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
1678 /* Encode line by line */
1679 for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
1682 switch( p_region->fmt.p_palette->i_entries )
1685 bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
1686 encode_pixel_line_2bp( p_enc, s, p_region, i_line );
1690 bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
1691 encode_pixel_line_4bp( p_enc, s, p_region, i_line );
1695 msg_Err( p_enc, "subpicture palette (%i) not handled",
1696 p_region->fmt.p_palette->i_entries );
1700 bs_write( s, 8, 0xf0 ); /* End of object line code */
1704 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1705 subpicture_region_t *p_region,
1708 unsigned int i, i_length = 0;
1709 int i_pitch = p_region->picture.p->i_pitch;
1710 uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1711 int i_last_pixel = p_data[0];
1713 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1715 if( i != p_region->fmt.i_visible_width &&
1716 p_data[i] == i_last_pixel && i_length != 284 )
1722 if( i_length == 1 || i_length == 11 || i_length == 28 )
1724 /* 2bit/pixel code */
1725 if( i_last_pixel ) bs_write( s, 2, i_last_pixel );
1728 bs_write( s, 2, 0 );
1729 bs_write( s, 1, 0 );
1730 bs_write( s, 1, 1 ); /* pseudo color 0 */
1739 bs_write( s, 2, i_last_pixel );
1740 bs_write( s, 2, i_last_pixel );
1744 bs_write( s, 2, 0 );
1745 bs_write( s, 1, 0 );
1746 bs_write( s, 1, 0 );
1747 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
1750 else if( i_length > 2 )
1752 bs_write( s, 2, 0 );
1753 if( i_length <= 10 )
1755 bs_write( s, 1, 1 );
1756 bs_write( s, 3, i_length - 3 );
1757 bs_write( s, 2, i_last_pixel );
1761 bs_write( s, 1, 0 );
1762 bs_write( s, 1, 0 );
1764 if( i_length <= 27 )
1766 bs_write( s, 2, 2 );
1767 bs_write( s, 4, i_length - 12 );
1768 bs_write( s, 2, i_last_pixel );
1772 bs_write( s, 2, 3 );
1773 bs_write( s, 8, i_length - 29 );
1774 bs_write( s, 2, i_last_pixel );
1779 if( i == p_region->fmt.i_visible_width ) break;
1781 i_last_pixel = p_data[i];
1786 bs_write( s, 2, 0 );
1787 bs_write( s, 1, 0 );
1788 bs_write( s, 1, 0 );
1789 bs_write( s, 2, 0 );
1795 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
1796 subpicture_region_t *p_region,
1799 unsigned int i, i_length = 0;
1800 int i_pitch = p_region->picture.p->i_pitch;
1801 uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1802 int i_last_pixel = p_data[0];
1804 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1806 if( i != p_region->fmt.i_visible_width &&
1807 p_data[i] == i_last_pixel && i_length != 1 )
1815 /* 4bit/pixel code */
1816 if( i_last_pixel ) bs_write( s, 4, i_last_pixel );
1819 bs_write( s, 4, 0 );
1820 bs_write( s, 1, 1 );
1821 bs_write( s, 1, 1 );
1822 bs_write( s, 2, 0 ); /* pseudo color 0 */
1826 if( i == p_region->fmt.i_visible_width ) break;
1828 i_last_pixel = p_data[i];
1833 bs_write( s, 8, 0 );