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: Gildas Bazin <gbazin@videolan.org>
10 * Damien LUCAS <damien.lucas@anevia.com>
11 * Laurent Aimar <fenrir@via.ecp.fr>
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 /* The object definition gives the position of the object in a region */
71 typedef struct dvbsub_objectdef_s
82 /* The entry in the palette CLUT */
93 typedef struct dvbsub_clut_s
97 dvbsub_color_t c_2b[4];
98 dvbsub_color_t c_4b[16];
99 dvbsub_color_t c_8b[256];
101 struct dvbsub_clut_s *p_next;
105 /* The Region is an aera on the image
106 * with a list of the object definitions associated and a CLUT */
107 typedef struct dvbsub_region_s
122 dvbsub_objectdef_t *p_object_defs;
124 struct dvbsub_region_s *p_next;
128 /* The object definition gives the position of the object in a region */
129 typedef struct dvbsub_regiondef_s
135 } dvbsub_regiondef_t;
137 /* The page defines the list of regions */
146 dvbsub_regiondef_t *p_region_defs;
154 /* Decoder internal data */
159 dvbsub_page_t *p_page;
160 dvbsub_region_t *p_regions;
161 dvbsub_clut_t *p_cluts;
162 dvbsub_clut_t default_clut;
166 // List of different SEGMENT TYPES
167 // According to EN 300-743, table 2
168 #define DVBSUB_ST_PAGE_COMPOSITION 0x10
169 #define DVBSUB_ST_REGION_COMPOSITION 0x11
170 #define DVBSUB_ST_CLUT_DEFINITION 0x12
171 #define DVBSUB_ST_OBJECT_DATA 0x13
172 #define DVBSUB_ST_ENDOFDISPLAY 0x80
173 #define DVBSUB_ST_STUFFING 0xff
174 // List of different OBJECT TYPES
175 // According to EN 300-743, table 6
176 #define DVBSUB_OT_BASIC_BITMAP 0x00
177 #define DVBSUB_OT_BASIC_CHAR 0x01
178 #define DVBSUB_OT_COMPOSITE_STRING 0x02
180 // According to EN 300-743, table 9
181 #define DVBSUB_DT_2BP_CODE_STRING 0x10
182 #define DVBSUB_DT_4BP_CODE_STRING 0x11
183 #define DVBSUB_DT_8BP_CODE_STRING 0x12
184 #define DVBSUB_DT_24_TABLE_DATA 0x20
185 #define DVBSUB_DT_28_TABLE_DATA 0x21
186 #define DVBSUB_DT_48_TABLE_DATA 0x22
187 #define DVBSUB_DT_END_LINE 0xf0
188 // List of different Page Composition Segment state
189 // According to EN 300-743, 7.2.1 table 3
190 #define DVBSUB_PCS_STATE_ACQUISITION 0x01
191 #define DVBSUB_PCS_STATE_CHANGE 0x10
193 /*****************************************************************************
195 *****************************************************************************/
196 static void decode_segment( decoder_t *, bs_t * );
197 static void decode_page_composition( decoder_t *, bs_t * );
198 static void decode_region_composition( decoder_t *, bs_t * );
199 static void decode_object( decoder_t *, bs_t * );
200 static void decode_clut( decoder_t *, bs_t * );
201 static void free_all( decoder_t * );
203 static void default_clut_init( decoder_t * );
205 static subpicture_t *render( decoder_t * );
207 /*****************************************************************************
208 * Open: probe the decoder and return score
209 *****************************************************************************
210 * Tries to launch a decoder and return score so that the interface is able
212 *****************************************************************************/
213 static int Open( vlc_object_t *p_this )
215 decoder_t *p_dec = (decoder_t *) p_this;
216 decoder_sys_t *p_sys;
218 if( p_dec->fmt_in.i_codec != VLC_FOURCC('d','v','b','s') )
223 p_dec->pf_decode_sub = Decode;
224 p_sys = p_dec->p_sys = malloc( sizeof(decoder_sys_t) );
225 memset( p_sys, 0, sizeof(decoder_sys_t) );
228 p_sys->i_id = p_dec->fmt_in.subs.dvb.i_id & 0xFFFF;
229 p_sys->i_ancillary_id = p_dec->fmt_in.subs.dvb.i_id >> 16;
230 p_sys->p_regions = NULL;
231 p_sys->p_cluts = NULL;
232 p_sys->p_page = NULL;
234 es_format_Init( &p_dec->fmt_out, SPU_ES, VLC_FOURCC( 'd','v','b','s' ) );
236 default_clut_init( p_dec );
241 /*****************************************************************************
243 *****************************************************************************/
244 static void Close( vlc_object_t *p_this )
246 decoder_t *p_dec = (decoder_t*) p_this;
247 decoder_sys_t *p_sys = p_dec->p_sys;
253 /*****************************************************************************
255 *****************************************************************************/
256 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
258 decoder_sys_t *p_sys = p_dec->p_sys;
260 subpicture_t *p_spu = NULL;
262 if( pp_block == NULL || *pp_block == NULL ) return NULL;
266 p_sys->i_pts = p_block->i_pts;
267 if( p_sys->i_pts <= 0 )
270 /* Some DVB channels send stuffing segments in non-dated packets so
271 * don't complain too loudly. */
272 msg_Warn( p_dec, "non dated subtitle" );
274 block_Release( p_block );
278 bs_init( &p_sys->bs, p_block->p_buffer, p_block->i_buffer );
280 if( bs_read( &p_sys->bs, 8 ) != 0x20 ) /* Data identifier */
282 msg_Dbg( p_dec, "invalid data identifier" );
283 block_Release( p_block );
287 if( bs_read( &p_sys->bs, 8 ) ) /* Subtitle stream id */
289 msg_Dbg( p_dec, "invalid subtitle stream id" );
290 block_Release( p_block );
295 msg_Dbg( p_dec, "subtitle packet received: "I64Fd, p_sys->i_pts );
298 while( bs_show( &p_sys->bs, 8 ) == 0x0f ) /* Sync byte */
300 decode_segment( p_dec, &p_sys->bs );
303 if( bs_read( &p_sys->bs, 8 ) != 0xff ) /* End marker */
305 msg_Warn( p_dec, "end marker not found (corrupted subtitle ?)" );
306 block_Release( p_block );
310 /* Check if the page is to be displayed */
311 if( p_sys->p_page ) p_spu = render( p_dec );
313 block_Release( p_block );
318 /* following functions are local */
320 /*****************************************************************************
321 * default_clut_init: default clut as defined in EN 300-743 section 10
322 *****************************************************************************/
323 static void default_clut_init( decoder_t *p_dec )
325 decoder_sys_t *p_sys = p_dec->p_sys;
328 #define RGB_TO_Y(r, g, b) ((int16_t) 77 * r + 150 * g + 29 * b) / 256;
329 #define RGB_TO_U(r, g, b) ((int16_t) -44 * r - 87 * g + 131 * b) / 256;
330 #define RGB_TO_V(r, g, b) ((int16_t) 131 * r - 110 * g - 21 * b) / 256;
333 for( i = 0; i < 4; i++ )
335 uint8_t R = 0, G = 0, B = 0, T = 0;
337 if( !(i & 0x2) && !(i & 0x1) ) T = 0xFF;
338 else if( !(i & 0x2) && (i & 0x1) ) R = G = B = 0xFF;
339 else if( (i & 0x2) && !(i & 0x1) ) R = G = B = 0;
340 else R = G = B = 0x7F;
342 p_sys->default_clut.c_2b[i].Y = RGB_TO_Y(R,G,B);
343 p_sys->default_clut.c_2b[i].Cr = RGB_TO_U(R,G,B);
344 p_sys->default_clut.c_2b[i].Cb = RGB_TO_V(R,G,B);
345 p_sys->default_clut.c_2b[i].T = T;
348 /* 16 entries CLUT */
349 for( i = 0; i < 16; i++ )
351 uint8_t R = 0, G = 0, B = 0, T = 0;
355 if( !(i & 0x4) && !(i & 0x2) && !(i & 0x1) )
361 R = (i & 0x1) ? 0xFF : 0;
362 G = (i & 0x2) ? 0xFF : 0;
363 B = (i & 0x4) ? 0xFF : 0;
368 R = (i & 0x1) ? 0x7F : 0;
369 G = (i & 0x2) ? 0x7F : 0;
370 B = (i & 0x4) ? 0x7F : 0;
373 p_sys->default_clut.c_4b[i].Y = RGB_TO_Y(R,G,B);
374 p_sys->default_clut.c_4b[i].Cr = RGB_TO_U(R,G,B);
375 p_sys->default_clut.c_4b[i].Cb = RGB_TO_V(R,G,B);
376 p_sys->default_clut.c_4b[i].T = T;
379 /* 256 entries CLUT (TODO) */
380 memset( p_sys->default_clut.c_8b, 0xFF, 256 * sizeof(dvbsub_color_t) );
383 static void decode_segment( decoder_t *p_dec, bs_t *s )
385 decoder_sys_t *p_sys = p_dec->p_sys;
390 /* sync_byte (already checked) */
394 i_type = bs_read( s, 8 );
397 i_page_id = bs_read( s, 16 );
400 i_size = bs_show( s, 16 );
402 if( i_page_id != p_sys->i_id && i_page_id != p_sys->i_ancillary_id )
405 msg_Dbg( p_dec, "subtitle skipped (page id: %i, %i)",
406 i_page_id, p_sys->i_id );
408 bs_skip( s, 8 * ( 2 + i_size ) );
413 if( i_page_id == p_sys->i_id )
414 msg_Dbg( p_dec, "segment (id: %i)", i_page_id );
416 msg_Dbg( p_dec, "ancillary segment (id: %i)", i_page_id );
421 case DVBSUB_ST_PAGE_COMPOSITION:
423 msg_Dbg( p_dec, "decode_page_composition" );
425 decode_page_composition( p_dec, s );
428 case DVBSUB_ST_REGION_COMPOSITION:
430 msg_Dbg( p_dec, "decode_region_composition" );
432 decode_region_composition( p_dec, s );
435 case DVBSUB_ST_CLUT_DEFINITION:
437 msg_Dbg( p_dec, "decode_clut" );
439 decode_clut( p_dec, s );
442 case DVBSUB_ST_OBJECT_DATA:
444 msg_Dbg( p_dec, "decode_object" );
446 decode_object( p_dec, s );
449 case DVBSUB_ST_ENDOFDISPLAY:
451 msg_Dbg( p_dec, "end of display" );
453 bs_skip( s, 8 * ( 2 + i_size ) );
456 case DVBSUB_ST_STUFFING:
458 msg_Dbg( p_dec, "skip stuffing" );
460 bs_skip( s, 8 * ( 2 + i_size ) );
464 msg_Warn( p_dec, "unsupported segment type: (%04x)", i_type );
465 bs_skip( s, 8 * ( 2 + i_size ) );
470 static void decode_clut( decoder_t *p_dec, bs_t *s )
472 decoder_sys_t *p_sys = p_dec->p_sys;
473 uint16_t i_segment_length;
474 uint16_t i_processed_length;
475 dvbsub_clut_t *p_clut, *p_next;
478 i_segment_length = bs_read( s, 16 );
479 i_id = bs_read( s, 8 );
480 i_version = bs_read( s, 4 );
482 /* Check if we already have this clut */
483 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
485 if( p_clut->i_id == i_id ) break;
488 /* Check version number */
489 if( p_clut && p_clut->i_version == i_version )
492 bs_skip( s, 8 * i_segment_length - 12 );
499 msg_Dbg( p_dec, "new clut: %i", i_id );
501 p_clut = malloc( sizeof(dvbsub_clut_t) );
502 p_clut->p_next = p_sys->p_cluts;
503 p_sys->p_cluts = p_clut;
506 /* Initialize to default clut */
507 p_next = p_clut->p_next;
508 *p_clut = p_sys->default_clut;
509 p_clut->p_next = p_next;
511 /* We don't have this version of the CLUT: Parse it */
512 p_clut->i_version = i_version;
514 bs_skip( s, 4 ); /* Reserved bits */
515 i_processed_length = 2;
516 while( i_processed_length < i_segment_length )
518 uint8_t y, cb, cr, t;
522 i_id = bs_read( s, 8 );
523 i_type = bs_read( s, 3 );
527 if( bs_read( s, 1 ) )
530 cr = bs_read( s, 8 );
531 cb = bs_read( s, 8 );
533 i_processed_length += 6;
537 y = bs_read( s, 6 ) << 2;
538 cr = bs_read( s, 4 ) << 4;
539 cb = bs_read( s, 4 ) << 4;
540 t = bs_read( s, 2 ) << 6;
541 i_processed_length += 4;
544 /* We are not entirely compliant here as full transparency is indicated
545 * with a luma value of zero, not a transparency value of 0xff
546 * (full transparency would actually be 0xff + 1). */
554 /* According to EN 300-743 section 7.2.3 note 1, type should
555 * not have more than 1 bit set to one, but some streams don't
556 * respect this note. */
560 p_clut->c_2b[i_id].Y = y;
561 p_clut->c_2b[i_id].Cr = cr;
562 p_clut->c_2b[i_id].Cb = cb;
563 p_clut->c_2b[i_id].T = t;
567 p_clut->c_4b[i_id].Y = y;
568 p_clut->c_4b[i_id].Cr = cr;
569 p_clut->c_4b[i_id].Cb = cb;
570 p_clut->c_4b[i_id].T = t;
574 p_clut->c_8b[i_id].Y = y;
575 p_clut->c_8b[i_id].Cr = cr;
576 p_clut->c_8b[i_id].Cb = cb;
577 p_clut->c_8b[i_id].T = t;
582 static void decode_page_composition( decoder_t *p_dec, bs_t *s )
584 decoder_sys_t *p_sys = p_dec->p_sys;
585 int i_version, i_state, i_segment_length, i_timeout, i;
587 /* A page is composed by 0 or more region */
589 i_segment_length = bs_read( s, 16 );
590 i_timeout = bs_read( s, 8 );
591 i_version = bs_read( s, 4 );
592 i_state = bs_read( s, 2 );
593 bs_skip( s, 2 ); /* Reserved */
595 if( i_state == DVBSUB_PCS_STATE_CHANGE )
597 /* End of an epoch, reset decoder buffer */
599 msg_Dbg( p_dec, "page composition mode change" );
603 else if( !p_sys->p_page && i_state != DVBSUB_PCS_STATE_ACQUISITION )
605 /* Not a full PCS, we need to wait for one */
606 msg_Dbg( p_dec, "didn't receive an acquisition page yet" );
608 #if 0 /* Try to start decoding even without an acquisition page */
609 bs_skip( s, 8 * (i_segment_length - 2) );
615 if( i_state == DVBSUB_PCS_STATE_ACQUISITION )
616 msg_Dbg( p_dec, "acquisition page composition" );
619 /* Check version number */
620 if( p_sys->p_page && p_sys->p_page->i_version == i_version )
622 bs_skip( s, 8 * (i_segment_length - 2) );
625 else if( p_sys->p_page )
627 if( p_sys->p_page->i_region_defs )
628 free( p_sys->p_page->p_region_defs );
629 p_sys->p_page->i_region_defs = 0;
635 msg_Dbg( p_dec, "new page" );
637 /* Allocate a new page */
638 p_sys->p_page = malloc( sizeof(dvbsub_page_t) );
641 p_sys->p_page->i_version = i_version;
642 p_sys->p_page->i_timeout = i_timeout;
644 /* Number of regions */
645 p_sys->p_page->i_region_defs = (i_segment_length - 2) / 6;
647 if( p_sys->p_page->i_region_defs == 0 ) return;
649 p_sys->p_page->p_region_defs =
650 malloc( p_sys->p_page->i_region_defs * sizeof(dvbsub_region_t) );
651 for( i = 0; i < p_sys->p_page->i_region_defs; i++ )
653 p_sys->p_page->p_region_defs[i].i_id = bs_read( s, 8 );
654 bs_skip( s, 8 ); /* Reserved */
655 p_sys->p_page->p_region_defs[i].i_x = bs_read( s, 16 );
656 p_sys->p_page->p_region_defs[i].i_y = bs_read( s, 16 );
659 msg_Dbg( p_dec, "page_composition, region %i (%i,%i)",
660 i, p_sys->p_page->p_region_defs[i].i_x,
661 p_sys->p_page->p_region_defs[i].i_y );
666 static void decode_region_composition( decoder_t *p_dec, bs_t *s )
668 decoder_sys_t *p_sys = p_dec->p_sys;
669 dvbsub_region_t *p_region, **pp_region = &p_sys->p_regions;
670 int i_segment_length, i_processed_length, i_id, i_version;
671 int i_width, i_height, i_level_comp, i_depth, i_clut;
672 int i_8_bg, i_4_bg, i_2_bg;
675 i_segment_length = bs_read( s, 16 );
676 i_id = bs_read( s, 8 );
677 i_version = bs_read( s, 4 );
679 /* Check if we already have this region */
680 for( p_region = p_sys->p_regions; p_region != NULL;
681 p_region = p_region->p_next )
683 pp_region = &p_region->p_next;
684 if( p_region->i_id == i_id ) break;
687 /* Check version number */
688 if( p_region && p_region->i_version == i_version )
690 bs_skip( s, 8 * (i_segment_length - 1) - 4 );
697 msg_Dbg( p_dec, "new region: %i", i_id );
699 p_region = *pp_region = malloc( sizeof(dvbsub_region_t) );
700 memset( p_region, 0, sizeof(dvbsub_region_t) );
701 p_region->p_object_defs = NULL;
702 p_region->p_pixbuf = NULL;
703 p_region->p_next = NULL;
706 /* Region attributes */
707 p_region->i_id = i_id;
708 p_region->i_version = i_version;
709 b_fill = bs_read( s, 1 );
710 bs_skip( s, 3 ); /* Reserved */
712 i_width = bs_read( s, 16 );
713 i_height = bs_read( s, 16 );
714 i_level_comp = bs_read( s, 3 );
715 i_depth = bs_read( s, 3 );
716 bs_skip( s, 2 ); /* Reserved */
717 i_clut = bs_read( s, 8 );
719 i_8_bg = bs_read( s, 8 );
720 i_4_bg = bs_read( s, 4 );
721 i_2_bg = bs_read( s, 2 );
722 bs_skip( s, 2 ); /* Reserved */
723 p_region->i_object_defs = 0;
725 /* Extra sanity checks */
726 if( p_region->i_width != i_width || p_region->i_height != i_height )
728 if( p_region->p_pixbuf )
730 msg_Dbg( p_dec, "region size changed (not allowed)" );
731 free( p_region->p_pixbuf );
734 p_region->p_pixbuf = malloc( i_height * i_width );
735 p_region->i_depth = 0;
738 if( p_region->i_depth && (p_region->i_depth != i_depth ||
739 p_region->i_level_comp != i_level_comp || p_region->i_clut != i_clut) )
741 msg_Dbg( p_dec, "region parameters changed (not allowed)" );
744 /* Erase background of region */
747 int i_background = (p_region->i_depth == 1) ? i_2_bg :
748 (p_region->i_depth == 2) ? i_4_bg : i_8_bg;
749 memset( p_region->p_pixbuf, i_background, i_width * i_height );
752 p_region->i_width = i_width;
753 p_region->i_height = i_height;
754 p_region->i_level_comp = i_level_comp;
755 p_region->i_depth = i_depth;
756 p_region->i_clut = i_clut;
758 /* List of objects in the region */
759 i_processed_length = 10;
760 while( i_processed_length < i_segment_length )
762 dvbsub_objectdef_t *p_obj;
764 /* We create a new object */
765 p_region->i_object_defs++;
766 p_region->p_object_defs =
767 realloc( p_region->p_object_defs,
768 sizeof(dvbsub_objectdef_t) * p_region->i_object_defs );
770 /* We parse object properties */
771 p_obj = &p_region->p_object_defs[p_region->i_object_defs - 1];
772 p_obj->i_id = bs_read( s, 16 );
773 p_obj->i_type = bs_read( s, 2 );
774 bs_skip( s, 2 ); /* Provider */
775 p_obj->i_x = bs_read( s, 12 );
776 bs_skip( s, 4 ); /* Reserved */
777 p_obj->i_y = bs_read( s, 12 );
779 i_processed_length += 6;
781 if( p_obj->i_type == DVBSUB_OT_BASIC_CHAR ||
782 p_obj->i_type == DVBSUB_OT_COMPOSITE_STRING )
784 p_obj->i_fg_pc = bs_read( s, 8 );
785 p_obj->i_bg_pc = bs_read( s, 8 );
786 i_processed_length += 2;
791 static void dvbsub_render_pdata( decoder_t *, dvbsub_region_t *, int, int,
793 static void dvbsub_pdata2bpp( bs_t *, uint8_t *, int, int * );
794 static void dvbsub_pdata4bpp( bs_t *, uint8_t *, int, int * );
795 static void dvbsub_pdata8bpp( bs_t *, uint8_t *, int, int * );
797 static void decode_object( decoder_t *p_dec, bs_t *s )
799 decoder_sys_t *p_sys = p_dec->p_sys;
800 dvbsub_region_t *p_region;
801 int i_segment_length, i_coding_method, i_version, i_id, i;
802 vlc_bool_t b_non_modify_color;
804 i_segment_length = bs_read( s, 16 );
805 i_id = bs_read( s, 16 );
806 i_version = bs_read( s, 4 );
807 i_coding_method = bs_read( s, 2 );
809 if( i_coding_method )
811 /* TODO: DVB subtitling as characters */
812 msg_Dbg( p_dec, "DVB subtitling as characters is not handled!" );
813 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
817 /* Check if the object needs to be rendered in at least one
819 for( p_region = p_sys->p_regions; p_region != NULL;
820 p_region = p_region->p_next )
822 for( i = 0; i < p_region->i_object_defs; i++ )
823 if( p_region->p_object_defs[i].i_id == i_id ) break;
825 if( i != p_region->i_object_defs ) break;
829 bs_skip( s, 8 * (i_segment_length - 2) - 6 );
834 msg_Dbg( p_dec, "new object: %i", i_id );
837 b_non_modify_color = bs_read( s, 1 );
838 bs_skip( s, 1 ); /* Reserved */
840 if( i_coding_method == 0x00 )
842 int i_topfield, i_bottomfield;
843 uint8_t *p_topfield, *p_bottomfield;
845 i_topfield = bs_read( s, 16 );
846 i_bottomfield = bs_read( s, 16 );
847 p_topfield = s->p_start + bs_pos( s ) / 8;
848 p_bottomfield = p_topfield + i_topfield;
850 bs_skip( s, 8 * (i_segment_length - 7) );
853 if( i_segment_length < i_topfield + i_bottomfield + 7 ||
854 s->p_start + i_topfield + i_bottomfield > s->p_end )
856 msg_Dbg( p_dec, "corrupted object data" );
860 for( p_region = p_sys->p_regions; p_region != NULL;
861 p_region = p_region->p_next )
863 for( i = 0; i < p_region->i_object_defs; i++ )
865 if( p_region->p_object_defs[i].i_id != i_id ) continue;
867 dvbsub_render_pdata( p_dec, p_region,
868 p_region->p_object_defs[i].i_x,
869 p_region->p_object_defs[i].i_y,
870 p_topfield, i_topfield );
874 dvbsub_render_pdata( p_dec, p_region,
875 p_region->p_object_defs[i].i_x,
876 p_region->p_object_defs[i].i_y + 1,
877 p_bottomfield, i_bottomfield );
881 /* Duplicate the top field */
882 dvbsub_render_pdata( p_dec, p_region,
883 p_region->p_object_defs[i].i_x,
884 p_region->p_object_defs[i].i_y + 1,
885 p_topfield, i_topfield );
892 /* TODO: DVB subtitling as characters */
896 msg_Dbg( p_dec, "end object: %i", i_id );
900 static void dvbsub_render_pdata( decoder_t *p_dec, dvbsub_region_t *p_region,
902 uint8_t *p_field, int i_field )
909 if( !p_region->p_pixbuf )
911 msg_Err( p_dec, "region %i has no pixel buffer!", p_region->i_id );
914 if( i_y < 0 || i_x < 0 || i_y >= p_region->i_height ||
915 i_x >= p_region->i_width )
917 msg_Dbg( p_dec, "invalid offset (%i,%i)", i_x, i_y );
921 p_pixbuf = p_region->p_pixbuf + i_y * p_region->i_width;
922 bs_init( &bs, p_field, i_field );
924 while( !bs_eof( &bs ) )
927 if( i_y >= p_region->i_height ) return;
929 switch( bs_read( &bs, 8 ) )
932 dvbsub_pdata2bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
937 dvbsub_pdata4bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
942 dvbsub_pdata8bpp( &bs, p_pixbuf + i_x, p_region->i_width - i_x,
949 /* We don't use map tables */
952 case 0xf0: /* End of line code */
953 p_pixbuf += 2*p_region->i_width;
954 i_offset = 0; i_y += 2;
960 static void dvbsub_pdata2bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
962 vlc_bool_t b_stop = 0;
964 while( !b_stop && !bs_eof( s ) )
966 int i_count = 0, i_color = 0;
968 if( (i_color = bs_read( s, 2 )) != 0x00 )
974 if( bs_read( s, 1 ) == 0x01 ) // Switch1
976 i_count = 3 + bs_read( s, 3 );
977 i_color = bs_read( s, 2 );
981 if( bs_read( s, 1 ) == 0x00 ) //Switch2
983 switch( bs_read( s, 2 ) ) //Switch3
992 i_count = 12 + bs_read( s, 4 );
993 i_color = bs_read( s, 2 );
996 i_count = 29 + bs_read( s, 8 );
997 i_color = bs_read( s, 2 );
1005 /* 1 pixel color 0 */
1011 if( !i_count ) continue;
1014 if( i_count + *pi_off > i_width ) break;
1016 if( i_count == 1 ) p[*pi_off] = i_color;
1017 else memset( p + *pi_off, i_color, i_count );
1019 (*pi_off) += i_count;
1025 static void dvbsub_pdata4bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1027 vlc_bool_t b_stop = 0;
1029 while( !b_stop && !bs_eof( s ) )
1031 int i_count = 0, i_color = 0;
1033 if( (i_color = bs_read( s, 4 )) != 0x00 )
1040 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1042 if( bs_show( s, 3 ) != 0x00 )
1044 i_count = 2 + bs_read( s, 3 );
1054 if( bs_read( s, 1 ) == 0x00) //Switch2
1056 i_count = 4 + bs_read( s, 2 );
1057 i_color = bs_read( s, 4 );
1061 switch ( bs_read( s, 2 ) ) //Switch3
1070 i_count = 9 + bs_read( s, 4 );
1071 i_color = bs_read( s, 4 );
1074 i_count= 25 + bs_read( s, 8 );
1075 i_color = bs_read( s, 4 );
1082 if( !i_count ) continue;
1085 if( i_count + *pi_off > i_width ) break;
1087 if( i_count == 1 ) p[*pi_off] = i_color;
1088 else memset( p + *pi_off, i_color, i_count );
1090 (*pi_off) += i_count;
1096 static void dvbsub_pdata8bpp( bs_t *s, uint8_t *p, int i_width, int *pi_off )
1098 vlc_bool_t b_stop = 0;
1100 while( !b_stop && !bs_eof( s ) )
1102 int i_count = 0, i_color = 0;
1104 if( (i_color = bs_read( s, 8 )) != 0x00 )
1111 if( bs_read( s, 1 ) == 0x00 ) // Switch1
1113 if( bs_show( s, 7 ) != 0x00 )
1115 i_count = bs_read( s, 7 );
1125 i_count = bs_read( s, 7 );
1126 i_color = bs_read( s, 8 );
1130 if( !i_count ) continue;
1133 if( i_count + *pi_off > i_width ) break;
1135 if( i_count == 1 ) p[*pi_off] = i_color;
1136 else memset( p + *pi_off, i_color, i_count );
1138 (*pi_off) += i_count;
1144 static void free_all( decoder_t *p_dec )
1146 decoder_sys_t *p_sys = p_dec->p_sys;
1147 dvbsub_region_t *p_reg, *p_reg_next;
1148 dvbsub_clut_t *p_clut, *p_clut_next;
1150 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut_next )
1152 p_clut_next = p_clut->p_next;
1155 p_sys->p_cluts = NULL;
1157 for( p_reg = p_sys->p_regions; p_reg != NULL; p_reg = p_reg_next )
1159 p_reg_next = p_reg->p_next;
1160 if( p_reg->i_object_defs ) free( p_reg->p_object_defs );
1161 if( p_reg->p_pixbuf ) free( p_reg->p_pixbuf );
1164 p_sys->p_regions = NULL;
1168 if( p_sys->p_page->i_region_defs )
1169 free( p_sys->p_page->p_region_defs );
1170 free( p_sys->p_page );
1172 p_sys->p_page = NULL;
1175 static subpicture_t *render( decoder_t *p_dec )
1177 decoder_sys_t *p_sys = p_dec->p_sys;
1178 subpicture_t *p_spu;
1179 subpicture_region_t **pp_spu_region;
1180 int i, j, i_timeout = 0;
1182 /* Allocate the subpicture internal data. */
1183 p_spu = p_dec->pf_spu_buffer_new( p_dec );
1184 if( !p_spu ) return NULL;
1186 pp_spu_region = &p_spu->p_region;
1188 /* Loop on region definitions */
1191 msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );
1194 for( i = 0; p_sys->p_page && i < p_sys->p_page->i_region_defs; i++ )
1196 dvbsub_region_t *p_region;
1197 dvbsub_regiondef_t *p_regiondef;
1198 dvbsub_clut_t *p_clut;
1199 dvbsub_color_t *p_color;
1200 subpicture_region_t *p_spu_region;
1201 uint8_t *p_src, *p_dst;
1205 i_timeout = p_sys->p_page->i_timeout;
1207 p_regiondef = &p_sys->p_page->p_region_defs[i];
1210 msg_Dbg( p_dec, "rendering region %i (%i,%i)", i,
1211 p_regiondef->i_x, p_regiondef->i_y );
1214 /* Find associated region */
1215 for( p_region = p_sys->p_regions; p_region != NULL;
1216 p_region = p_region->p_next )
1218 if( p_regiondef->i_id == p_region->i_id ) break;
1223 msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id );
1227 /* Find associated CLUT */
1228 for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next )
1230 if( p_region->i_clut == p_clut->i_id ) break;
1234 msg_Dbg( p_dec, "clut %i not found", p_region->i_clut );
1238 /* Create new SPU region */
1239 memset( &fmt, 0, sizeof(video_format_t) );
1240 fmt.i_chroma = VLC_FOURCC('Y','U','V','P');
1241 fmt.i_aspect = VOUT_ASPECT_FACTOR;
1242 fmt.i_width = fmt.i_visible_width = p_region->i_width;
1243 fmt.i_height = fmt.i_visible_height = p_region->i_height;
1244 fmt.i_x_offset = fmt.i_y_offset = 0;
1245 p_spu_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt );
1248 msg_Err( p_dec, "cannot allocate SPU region" );
1251 p_spu_region->i_x = p_regiondef->i_x;
1252 p_spu_region->i_y = p_regiondef->i_y;
1253 *pp_spu_region = p_spu_region;
1254 pp_spu_region = &p_spu_region->p_next;
1257 fmt.p_palette->i_entries = p_region->i_depth == 1 ? 4 :
1258 p_region->i_depth == 2 ? 16 : 256;
1259 p_color = (p_region->i_depth == 1) ? p_clut->c_2b :
1260 (p_region->i_depth == 2) ? p_clut->c_4b : p_clut->c_8b;
1261 for( j = 0; j < fmt.p_palette->i_entries; j++ )
1263 fmt.p_palette->palette[j][0] = p_color[j].Y;
1264 fmt.p_palette->palette[j][1] = p_color[j].Cr;
1265 fmt.p_palette->palette[j][2] = p_color[j].Cb;
1266 fmt.p_palette->palette[j][3] = 0xff - p_color[j].T;
1269 p_src = p_region->p_pixbuf;
1270 p_dst = p_spu_region->picture.Y_PIXELS;
1271 i_pitch = p_spu_region->picture.Y_PITCH;
1273 /* Copy pixel buffer */
1274 for( j = 0; j < p_region->i_height; j++ )
1276 memcpy( p_dst, p_src, p_region->i_width );
1277 p_src += p_region->i_width;
1282 /* Set the pf_render callback */
1283 p_spu->i_start = p_sys->i_pts;
1284 p_spu->i_stop = p_spu->i_start + i_timeout * 1000000;
1285 p_spu->b_ephemer = VLC_TRUE;
1290 /*****************************************************************************
1291 * encoder_sys_t : encoder descriptor
1292 *****************************************************************************/
1293 struct encoder_sys_t
1295 unsigned int i_page_ver;
1296 unsigned int i_region_ver;
1297 unsigned int i_clut_ver;
1308 static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );
1309 static void encode_clut( encoder_t *, bs_t *, subpicture_t * );
1310 static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );
1311 static void encode_object( encoder_t *, bs_t *, subpicture_t * );
1313 /*****************************************************************************
1314 * OpenEncoder: probe the encoder and return score
1315 *****************************************************************************/
1316 static int OpenEncoder( vlc_object_t *p_this )
1318 encoder_t *p_enc = (encoder_t *)p_this;
1319 encoder_sys_t *p_sys;
1321 if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','v','b','s') &&
1324 return VLC_EGENERIC;
1327 /* Allocate the memory needed to store the decoder's structure */
1328 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1330 msg_Err( p_enc, "out of memory" );
1331 return VLC_EGENERIC;
1333 p_enc->p_sys = p_sys;
1335 p_enc->pf_encode_sub = Encode;
1336 p_enc->fmt_out.i_codec = VLC_FOURCC('d','v','b','s');
1337 p_enc->fmt_out.subs.dvb.i_id = 1 << 16 | 1;
1339 sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
1341 p_sys->i_page_ver = 0;
1342 p_sys->i_region_ver = 0;
1343 p_sys->i_clut_ver = 0;
1348 /****************************************************************************
1349 * Encode: the whole thing
1350 ****************************************************************************/
1351 static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic )
1353 bs_t bits, *s = &bits;
1356 if( !p_subpic || !p_subpic->p_region ) return 0;
1358 msg_Dbg( p_enc, "encoding subpicture" );
1360 p_block = block_New( p_enc, 64000 );
1361 bs_init( s, p_block->p_buffer, p_block->i_buffer );
1363 bs_write( s, 8, 0x20 ); /* Data identifier */
1364 bs_write( s, 8, 0x0 ); /* Subtitle stream id */
1366 encode_page_composition( p_enc, s, p_subpic );
1367 encode_region_composition( p_enc, s, p_subpic );
1368 encode_clut( p_enc, s, p_subpic );
1369 encode_object( p_enc, s, p_subpic );
1371 /* End of display */
1372 bs_write( s, 8, 0x0f ); /* Sync byte */
1373 bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
1374 bs_write( s, 16, 1 ); /* Page id */
1375 bs_write( s, 16, 0 ); /* Segment length */
1377 bs_write( s, 8, 0xff ); /* End marker */
1378 p_block->i_buffer = bs_pos( s ) / 8;
1379 p_block->i_pts = p_block->i_dts = p_subpic->i_start;
1380 if( !p_subpic->b_ephemer && p_subpic->i_stop )
1381 p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
1383 msg_Dbg( p_enc, "subpicture encoded properly" );
1388 /*****************************************************************************
1389 * CloseEncoder: encoder destruction
1390 *****************************************************************************/
1391 static void CloseEncoder( vlc_object_t *p_this )
1393 encoder_t *p_enc = (encoder_t *)p_this;
1394 encoder_sys_t *p_sys = p_enc->p_sys;
1399 static void encode_page_composition( encoder_t *p_enc, bs_t *s,
1400 subpicture_t *p_subpic )
1402 encoder_sys_t *p_sys = p_enc->p_sys;
1403 subpicture_region_t *p_region;
1406 bs_write( s, 8, 0x0f ); /* Sync byte */
1407 bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
1408 bs_write( s, 16, 1 ); /* Page id */
1410 for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1411 p_region = p_region->p_next, i_regions++ );
1413 bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
1415 bs_write( s, 8, 5 ); /* Timeout */
1416 bs_write( s, 4, p_sys->i_page_ver++ );
1417 bs_write( s, 2, DVBSUB_PCS_STATE_ACQUISITION );
1418 bs_write( s, 2, 0 ); /* Reserved */
1420 for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1421 p_region = p_region->p_next, i_regions++ )
1423 bs_write( s, 8, i_regions );
1424 bs_write( s, 8, 0 ); /* Reserved */
1425 bs_write( s, 16, p_region->i_x );
1426 bs_write( s, 16, p_region->i_y );
1430 static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1432 encoder_sys_t *p_sys = p_enc->p_sys;
1433 subpicture_region_t *p_region = p_subpic->p_region;
1434 video_palette_t *p_pal;
1438 if( !p_region || !p_region->fmt.p_palette ||
1439 p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
1441 bs_write( s, 8, 0x0f ); /* Sync byte */
1442 bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
1443 bs_write( s, 16, 1 ); /* Page id */
1445 p_pal = p_region->fmt.p_palette;
1447 bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
1448 bs_write( s, 8, 1 ); /* Clut id */
1449 bs_write( s, 4, p_sys->i_clut_ver++ );
1450 bs_write( s, 4, 0 ); /* Reserved */
1452 for( i = 0; i < p_pal->i_entries; i++ )
1454 bs_write( s, 8, i ); /* Clut entry id */
1455 bs_write( s, 1, p_pal->i_entries == 4 ); /* 2bit/entry flag */
1456 bs_write( s, 1, p_pal->i_entries == 16 ); /* 4bit/entry flag */
1457 bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
1458 bs_write( s, 4, 0 ); /* Reserved */
1459 bs_write( s, 1, 1 ); /* Full range flag */
1460 bs_write( s, 8, p_pal->palette[i][0] ); /* Y value */
1461 bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
1462 bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
1463 bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
1467 static void encode_region_composition( encoder_t *p_enc, bs_t *s,
1468 subpicture_t *p_subpic )
1470 encoder_sys_t *p_sys = p_enc->p_sys;
1471 subpicture_region_t *p_region;
1474 for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1475 p_region = p_region->p_next, i_regions++ )
1477 video_palette_t *p_pal = p_region->fmt.p_palette;
1478 int i_depth = p_pal->i_entries == 4 ? 0x1 :
1479 p_pal->i_entries == 16 ? 0x2 : 0x3;
1481 bs_write( s, 8, 0x0f ); /* Sync byte */
1482 bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
1483 bs_write( s, 16, 1 ); /* Page id */
1485 bs_write( s, 16, 10 + 6 ); /* Segment length */
1486 bs_write( s, 8, i_regions );
1487 bs_write( s, 4, p_sys->i_region_ver++ );
1489 /* Region attributes */
1490 bs_write( s, 1, 0 ); /* Fill */
1491 bs_write( s, 3, 0 ); /* Reserved */
1492 bs_write( s, 16, p_region->fmt.i_visible_width );
1493 bs_write( s, 16, p_region->fmt.i_visible_height );
1494 bs_write( s, 3, i_depth ); /* Region level of compatibility */
1495 bs_write( s, 3, i_depth ); /* Region depth */
1496 bs_write( s, 2, 0 ); /* Reserved */
1497 bs_write( s, 8, 1 ); /* Clut id */
1498 bs_write( s, 8, 0 ); /* region 8bit pixel code */
1499 bs_write( s, 4, 0 ); /* region 4bit pixel code */
1500 bs_write( s, 2, 0 ); /* region 2bit pixel code */
1501 bs_write( s, 2, 0 ); /* Reserved */
1503 /* In our implementation we only have 1 object per region */
1504 bs_write( s, 16, i_regions );
1505 bs_write( s, 2, DVBSUB_OT_BASIC_BITMAP );
1506 bs_write( s, 2, 0 ); /* object provider flag */
1507 bs_write( s, 12, 0 ); /* object horizontal position */
1508 bs_write( s, 4, 0 ); /* Reserved */
1509 bs_write( s, 12, 0 ); /* object vertical position */
1513 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1514 subpicture_region_t *p_region,
1517 static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
1519 encoder_sys_t *p_sys = p_enc->p_sys;
1520 subpicture_region_t *p_region;
1523 int i_length_pos, i_update_pos, i_pixel_data_pos;
1525 for( i_regions = 0, p_region = p_subpic->p_region; p_region;
1526 p_region = p_region->p_next, i_regions++ )
1528 bs_write( s, 8, 0x0f ); /* Sync byte */
1529 bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
1530 bs_write( s, 16, 1 ); /* Page id */
1532 i_length_pos = bs_pos( s );
1533 bs_write( s, 16, 0 ); /* Segment length */
1534 bs_write( s, 16, i_regions ); /* Object id */
1535 bs_write( s, 4, p_sys->i_region_ver++ );
1536 bs_write( s, 2, 0 ); /* object coding method */
1538 bs_write( s, 1, 0 ); /* non modifying color flag */
1539 bs_write( s, 1, 0 ); /* Reserved */
1541 i_update_pos = bs_pos( s );
1542 bs_write( s, 16, 0 ); /* topfield data block length */
1543 bs_write( s, 16, 0 ); /* bottomfield data block length */
1546 i_pixel_data_pos = bs_pos( s );
1547 encode_pixel_data( p_enc, s, p_region, VLC_TRUE );
1548 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1549 SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
1552 i_pixel_data_pos = bs_pos( s );
1553 encode_pixel_data( p_enc, s, p_region, VLC_FALSE );
1554 i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
1555 SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
1557 /* Stuffing for word alignment */
1559 if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
1561 /* Update segment length */
1562 SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
1566 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1567 subpicture_region_t *p_region,
1569 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
1570 subpicture_region_t *p_region,
1572 static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
1573 subpicture_region_t *p_region,
1576 unsigned int i_line;
1579 if( p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
1581 /* Encode line by line */
1582 for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
1585 switch( p_region->fmt.p_palette->i_entries )
1588 bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
1589 encode_pixel_line_2bp( p_enc, s, p_region, i_line );
1593 bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
1594 encode_pixel_line_4bp( p_enc, s, p_region, i_line );
1598 msg_Err( p_enc, "subpicture palette (%i) not handled",
1599 p_region->fmt.p_palette->i_entries );
1603 bs_write( s, 8, 0xf0 ); /* End of object line code */
1607 static void encode_pixel_line_2bp( encoder_t *p_enc, bs_t *s,
1608 subpicture_region_t *p_region,
1611 unsigned int i, i_length = 0;
1612 int i_pitch = p_region->picture.p->i_pitch;
1613 uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1614 int i_last_pixel = p_data[0];
1616 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1618 if( i != p_region->fmt.i_visible_width &&
1619 p_data[i] == i_last_pixel && i_length != 284 )
1625 if( i_length == 1 || i_length == 11 || i_length == 28 )
1627 /* 2bit/pixel code */
1628 if( i_last_pixel ) bs_write( s, 2, i_last_pixel );
1631 bs_write( s, 2, 0 );
1632 bs_write( s, 1, 0 );
1633 bs_write( s, 1, 1 ); /* pseudo color 0 */
1642 bs_write( s, 2, i_last_pixel );
1643 bs_write( s, 2, i_last_pixel );
1647 bs_write( s, 2, 0 );
1648 bs_write( s, 1, 0 );
1649 bs_write( s, 1, 0 );
1650 bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
1653 else if( i_length > 2 )
1655 bs_write( s, 2, 0 );
1656 if( i_length <= 10 )
1658 bs_write( s, 1, 1 );
1659 bs_write( s, 3, i_length - 3 );
1660 bs_write( s, 2, i_last_pixel );
1664 bs_write( s, 1, 0 );
1665 bs_write( s, 1, 0 );
1667 if( i_length <= 27 )
1669 bs_write( s, 2, 2 );
1670 bs_write( s, 4, i_length - 12 );
1671 bs_write( s, 2, i_last_pixel );
1675 bs_write( s, 2, 3 );
1676 bs_write( s, 8, i_length - 29 );
1677 bs_write( s, 2, i_last_pixel );
1682 if( i == p_region->fmt.i_visible_width ) break;
1684 i_last_pixel = p_data[i];
1689 bs_write( s, 2, 0 );
1690 bs_write( s, 1, 0 );
1691 bs_write( s, 1, 0 );
1692 bs_write( s, 2, 0 );
1698 static void encode_pixel_line_4bp( encoder_t *p_enc, bs_t *s,
1699 subpicture_region_t *p_region,
1702 unsigned int i, i_length = 0;
1703 int i_pitch = p_region->picture.p->i_pitch;
1704 uint8_t *p_data = &p_region->picture.p->p_pixels[ i_pitch * i_line ];
1705 int i_last_pixel = p_data[0];
1707 for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
1709 if( i != p_region->fmt.i_visible_width &&
1710 p_data[i] == i_last_pixel && i_length != 1 )
1718 /* 4bit/pixel code */
1719 if( i_last_pixel ) bs_write( s, 4, i_last_pixel );
1722 bs_write( s, 4, 0 );
1723 bs_write( s, 1, 1 );
1724 bs_write( s, 1, 1 );
1725 bs_write( s, 2, 0 ); /* pseudo color 0 */
1729 if( i == p_region->fmt.i_visible_width ) break;
1731 i_last_pixel = p_data[i];
1736 bs_write( s, 8, 0 );