+ /* Sanity check */
+ if( !p_region ) return NULL;
+
+ if( ( p_region->fmt.i_chroma != VLC_FOURCC('T','E','X','T') ) &&
+ ( p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) )
+ {
+ char psz_fourcc[5];
+ memset( &psz_fourcc, 0, sizeof( psz_fourcc ) );
+ vlc_fourcc_to_char( p_region->fmt.i_chroma, &psz_fourcc );
+ msg_Err( p_enc, "chroma %4s not supported", psz_fourcc );
+ return NULL;
+ }
+
+ if( p_region->fmt.p_palette )
+ {
+ switch( p_region->fmt.p_palette->i_entries )
+ {
+ case 0:
+ case 4:
+ case 16:
+ case 256:
+ break;
+ default:
+ msg_Err( p_enc, "subpicture palette (%d) not handled",
+ p_region->fmt.p_palette->i_entries );
+ return NULL;
+ }
+ }
+ /* End of hack */
+
+#ifdef DEBUG_DVBSUB
+ msg_Dbg( p_enc, "encoding subpicture" );
+#endif
+ p_block = block_New( p_enc, 64000 );
+ bs_init( s, p_block->p_buffer, p_block->i_buffer );
+
+ bs_write( s, 8, 0x20 ); /* Data identifier */
+ bs_write( s, 8, 0x0 ); /* Subtitle stream id */
+
+ encode_page_composition( p_enc, s, p_subpic );
+ encode_region_composition( p_enc, s, p_subpic );
+ encode_clut( p_enc, s, p_subpic );
+ encode_object( p_enc, s, p_subpic );
+
+ /* End of display */
+ bs_write( s, 8, 0x0f ); /* Sync byte */
+ bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
+ bs_write( s, 16, 1 ); /* Page id */
+ bs_write( s, 16, 0 ); /* Segment length */
+
+ bs_write( s, 8, 0xff );/* End marker */
+ p_block->i_buffer = bs_pos( s ) / 8;
+ p_block->i_pts = p_block->i_dts = p_subpic->i_start;
+ if( !p_subpic->b_ephemer && ( p_subpic->i_stop > p_subpic->i_start ) )
+ {
+ block_t *p_block_stop;
+
+ p_block->i_length = p_subpic->i_stop - p_subpic->i_start;
+
+ /* Send another (empty) subtitle to signal the end of display */
+ p_block_stop = block_New( p_enc, 64000 );
+ bs_init( s, p_block_stop->p_buffer, p_block_stop->i_buffer );
+ bs_write( s, 8, 0x20 ); /* Data identifier */
+ bs_write( s, 8, 0x0 ); /* Subtitle stream id */
+ encode_page_composition( p_enc, s, 0 );
+ bs_write( s, 8, 0x0f ); /* Sync byte */
+ bs_write( s, 8, DVBSUB_ST_ENDOFDISPLAY ); /* Segment type */
+ bs_write( s, 16, 1 ); /* Page id */
+ bs_write( s, 16, 0 ); /* Segment length */
+ bs_write( s, 8, 0xff );/* End marker */
+ p_block_stop->i_buffer = bs_pos( s ) / 8;
+ p_block_stop->i_pts = p_block_stop->i_dts = p_subpic->i_stop;
+ block_ChainAppend( &p_block, p_block_stop );
+ p_block_stop->i_length = 100000; /* p_subpic->i_stop - p_subpic->i_start; */
+ }
+#ifdef DEBUG_DVBSUB
+ msg_Dbg( p_enc, "subpicture encoded properly" );
+#endif
+ return p_block;
+}
+
+/*****************************************************************************
+ * CloseEncoder: encoder destruction
+ *****************************************************************************/
+static void CloseEncoder( vlc_object_t *p_this )
+{
+ encoder_t *p_enc = (encoder_t *)p_this;
+ encoder_sys_t *p_sys = p_enc->p_sys;
+
+ var_Destroy( p_this , ENC_CFG_PREFIX "x" );
+ var_Destroy( p_this , ENC_CFG_PREFIX "y" );
+ var_Destroy( p_this , ENC_CFG_PREFIX "timeout" );
+
+ if( p_sys->i_regions ) free( p_sys->p_regions );
+ free( p_sys );
+}
+
+static void encode_page_composition( encoder_t *p_enc, bs_t *s,
+ subpicture_t *p_subpic )
+{
+ encoder_sys_t *p_sys = p_enc->p_sys;
+ subpicture_region_t *p_region;
+ bool b_mode_change = false;
+ int i_regions, i_timeout;
+
+ bs_write( s, 8, 0x0f ); /* Sync byte */
+ bs_write( s, 8, DVBSUB_ST_PAGE_COMPOSITION ); /* Segment type */
+ bs_write( s, 16, 1 ); /* Page id */
+
+ for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
+ p_region; p_region = p_region->p_next, i_regions++ )
+ {
+ if( i_regions >= p_sys->i_regions )
+ {
+ encoder_region_t region;
+ region.i_width = region.i_height = 0;
+ p_sys->p_regions =
+ realloc( p_sys->p_regions, sizeof(encoder_region_t) *
+ (p_sys->i_regions + 1) );
+ p_sys->p_regions[p_sys->i_regions++] = region;
+ }
+
+ if( ( p_sys->p_regions[i_regions].i_width <
+ (int)p_region->fmt.i_visible_width ) ||
+ ( p_sys->p_regions[i_regions].i_width >
+ (int)p_region->fmt.i_visible_width ) )
+ {
+ b_mode_change = true;
+ msg_Dbg( p_enc, "region %i width change: %i -> %i",
+ i_regions, p_sys->p_regions[i_regions].i_width,
+ p_region->fmt.i_visible_width );
+ p_sys->p_regions[i_regions].i_width =
+ p_region->fmt.i_visible_width;
+ }
+ if( p_sys->p_regions[i_regions].i_height <
+ (int)p_region->fmt.i_visible_height )
+ {
+ b_mode_change = true;
+ msg_Dbg( p_enc, "region %i height change: %i -> %i",
+ i_regions, p_sys->p_regions[i_regions].i_height,
+ p_region->fmt.i_visible_height );
+ p_sys->p_regions[i_regions].i_height =
+ p_region->fmt.i_visible_height;
+ }
+ }
+
+ bs_write( s, 16, i_regions * 6 + 2 ); /* Segment length */
+
+ i_timeout = 0;
+ if( p_subpic && !p_subpic->b_ephemer &&
+ ( p_subpic->i_stop > p_subpic->i_start ) )
+ {
+ i_timeout = (p_subpic->i_stop - p_subpic->i_start) / 1000000;
+ }
+
+ bs_write( s, 8, i_timeout ); /* Timeout */
+ bs_write( s, 4, p_sys->i_page_ver++ );
+ bs_write( s, 2, b_mode_change ?
+ DVBSUB_PCS_STATE_CHANGE : DVBSUB_PCS_STATE_ACQUISITION );
+ bs_write( s, 2, 0 ); /* Reserved */
+
+ for( i_regions = 0, p_region = p_subpic ? p_subpic->p_region : 0;
+ p_region; p_region = p_region->p_next, i_regions++ )
+ {
+ bs_write( s, 8, i_regions );
+ bs_write( s, 8, 0 ); /* Reserved */
+ if( (p_sys->i_offset_x > 0) && (p_sys->i_offset_y > 0) )
+ {
+ bs_write( s, 16, p_sys->i_offset_x ); /* override x position */
+ bs_write( s, 16, p_sys->i_offset_y ); /* override y position */
+ }
+ else
+ {
+ bs_write( s, 16, p_region->i_x );
+ bs_write( s, 16, p_region->i_y );
+ }
+ }
+}
+
+static void encode_clut( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
+{
+ encoder_sys_t *p_sys = p_enc->p_sys;
+ subpicture_region_t *p_region = p_subpic->p_region;
+ video_palette_t *p_pal, pal;
+ int i;
+
+ /* Sanity check */
+ if( !p_region ) return;
+
+ if( p_region->fmt.i_chroma == VLC_FOURCC('Y','U','V','P') )
+ {
+ p_pal = p_region->fmt.p_palette;
+ }
+ else
+ {
+ pal.i_entries = 4;
+ for( i = 0; i < 4; i++ )
+ {
+ pal.palette[i][0] = 0;
+ pal.palette[i][1] = 0;
+ pal.palette[i][2] = 0;
+ pal.palette[i][3] = 0;
+ }
+ p_pal = &pal;
+ }
+
+ bs_write( s, 8, 0x0f ); /* Sync byte */
+ bs_write( s, 8, DVBSUB_ST_CLUT_DEFINITION ); /* Segment type */
+ bs_write( s, 16, 1 ); /* Page id */
+
+ bs_write( s, 16, p_pal->i_entries * 6 + 2 ); /* Segment length */
+ bs_write( s, 8, 1 ); /* Clut id */
+ bs_write( s, 4, p_sys->i_clut_ver++ );
+ bs_write( s, 4, 0 ); /* Reserved */
+
+ for( i = 0; i < p_pal->i_entries; i++ )
+ {
+ bs_write( s, 8, i ); /* Clut entry id */
+ bs_write( s, 1, p_pal->i_entries == 4 ); /* 2bit/entry flag */
+ bs_write( s, 1, p_pal->i_entries == 16 ); /* 4bit/entry flag */
+ bs_write( s, 1, p_pal->i_entries == 256 ); /* 8bit/entry flag */
+ bs_write( s, 4, 0 ); /* Reserved */
+ bs_write( s, 1, 1 ); /* Full range flag */
+ bs_write( s, 8, p_pal->palette[i][3] ? /* Y value */
+ (p_pal->palette[i][0] ? p_pal->palette[i][0] : 16) : 0 );
+ bs_write( s, 8, p_pal->palette[i][1] ); /* Cr value */
+ bs_write( s, 8, p_pal->palette[i][2] ); /* Cb value */
+ bs_write( s, 8, 0xff - p_pal->palette[i][3] ); /* T value */
+ }
+}
+
+static void encode_region_composition( encoder_t *p_enc, bs_t *s,
+ subpicture_t *p_subpic )
+{
+ encoder_sys_t *p_sys = p_enc->p_sys;
+ subpicture_region_t *p_region;
+ int i_region;
+
+ for( i_region = 0, p_region = p_subpic->p_region; p_region;
+ p_region = p_region->p_next, i_region++ )
+ {
+ int i_entries = 4, i_depth = 0x1, i_bg = 0;
+ bool b_text =
+ ( p_region->fmt.i_chroma == VLC_FOURCC('T','E','X','T') );
+
+ if( !b_text )
+ {
+ video_palette_t *p_pal = p_region->fmt.p_palette;
+
+ if( !p_pal )
+ {
+ msg_Err( p_enc, "subpicture has no palette - ignoring it" );
+ break;
+ }
+
+ i_entries = p_pal->i_entries;
+ i_depth = i_entries == 4 ? 0x1 : i_entries == 16 ? 0x2 : 0x3;
+
+ for( i_bg = 0; i_bg < p_pal->i_entries; i_bg++ )
+ {
+ if( !p_pal->palette[i_bg][3] ) break;
+ }
+ }
+
+ bs_write( s, 8, 0x0f ); /* Sync byte */
+ bs_write( s, 8, DVBSUB_ST_REGION_COMPOSITION ); /* Segment type */
+ bs_write( s, 16, 1 ); /* Page id */
+
+ bs_write( s, 16, 10 + 6 + (b_text ? 2 : 0) ); /* Segment length */
+ bs_write( s, 8, i_region );
+ bs_write( s, 4, p_sys->i_region_ver++ );
+
+ /* Region attributes */
+ bs_write( s, 1, i_bg < i_entries ); /* Fill */
+ bs_write( s, 3, 0 ); /* Reserved */
+ bs_write( s, 16, p_sys->p_regions[i_region].i_width );
+ bs_write( s, 16, p_sys->p_regions[i_region].i_height );
+ bs_write( s, 3, i_depth ); /* Region level of compatibility */
+ bs_write( s, 3, i_depth ); /* Region depth */
+ bs_write( s, 2, 0 ); /* Reserved */
+ bs_write( s, 8, 1 ); /* Clut id */
+ bs_write( s, 8, i_bg ); /* region 8bit pixel code */
+ bs_write( s, 4, i_bg ); /* region 4bit pixel code */
+ bs_write( s, 2, i_bg ); /* region 2bit pixel code */
+ bs_write( s, 2, 0 ); /* Reserved */
+
+ /* In our implementation we only have 1 object per region */
+ bs_write( s, 16, i_region );
+ bs_write( s, 2, b_text ? DVBSUB_OT_BASIC_CHAR:DVBSUB_OT_BASIC_BITMAP );
+ bs_write( s, 2, 0 ); /* object provider flag */
+ bs_write( s, 12, 0 );/* object horizontal position */
+ bs_write( s, 4, 0 ); /* Reserved */
+ bs_write( s, 12, 0 );/* object vertical position */
+
+ if( b_text )
+ {
+ bs_write( s, 8, 1 ); /* foreground pixel code */
+ bs_write( s, 8, 0 ); /* background pixel code */
+ }
+ }
+}
+
+static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
+ subpicture_region_t *p_region,
+ bool b_top );
+
+static void encode_object( encoder_t *p_enc, bs_t *s, subpicture_t *p_subpic )
+{
+ encoder_sys_t *p_sys = p_enc->p_sys;
+ subpicture_region_t *p_region;
+ int i_region;
+
+ int i_length_pos, i_update_pos, i_pixel_data_pos;
+
+ for( i_region = 0, p_region = p_subpic->p_region; p_region;
+ p_region = p_region->p_next, i_region++ )
+ {
+ bs_write( s, 8, 0x0f ); /* Sync byte */
+ bs_write( s, 8, DVBSUB_ST_OBJECT_DATA ); /* Segment type */
+ bs_write( s, 16, 1 ); /* Page id */
+
+ i_length_pos = bs_pos( s );
+ bs_write( s, 16, 0 ); /* Segment length */
+ bs_write( s, 16, i_region ); /* Object id */
+ bs_write( s, 4, p_sys->i_region_ver++ );
+
+ /* object coding method */
+ switch( p_region->fmt.i_chroma )
+ {
+ case VLC_FOURCC( 'Y','U','V','P' ):
+ bs_write( s, 2, 0 );
+ break;
+ case VLC_FOURCC( 'T','E','X','T' ):
+ bs_write( s, 2, 1 );
+ break;
+ default:
+ msg_Err( p_enc, "FOURCC %d not supported by encoder.", p_region->fmt.i_chroma );
+ continue;
+ }
+
+ bs_write( s, 1, 0 ); /* non modifying color flag */
+ bs_write( s, 1, 0 ); /* Reserved */
+
+ if( p_region->fmt.i_chroma == VLC_FOURCC( 'T','E','X','T' ) )
+ {
+ int i_size, i;
+
+ if( !p_region->psz_text ) continue;
+
+ i_size = __MIN( strlen( p_region->psz_text ), 256 );
+
+ bs_write( s, 8, i_size ); /* number of characters in string */
+ for( i = 0; i < i_size; i++ )
+ {
+ bs_write( s, 16, p_region->psz_text[i] );
+ }
+
+ /* Update segment length */
+ SetWBE( &s->p_start[i_length_pos/8],
+ (bs_pos(s) - i_length_pos)/8 -2 );
+ continue;
+ }
+
+ /* Coding of a bitmap object */
+ i_update_pos = bs_pos( s );
+ bs_write( s, 16, 0 ); /* topfield data block length */
+ bs_write( s, 16, 0 ); /* bottomfield data block length */
+
+ /* Top field */
+ i_pixel_data_pos = bs_pos( s );
+ encode_pixel_data( p_enc, s, p_region, true );
+ i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
+ SetWBE( &s->p_start[i_update_pos/8], i_pixel_data_pos );
+
+ /* Bottom field */
+ i_pixel_data_pos = bs_pos( s );
+ encode_pixel_data( p_enc, s, p_region, false );
+ i_pixel_data_pos = ( bs_pos( s ) - i_pixel_data_pos ) / 8;
+ SetWBE( &s->p_start[i_update_pos/8+2], i_pixel_data_pos );
+
+ /* Stuffing for word alignment */
+ bs_align_0( s );
+ if( bs_pos( s ) % 16 ) bs_write( s, 8, 0 );
+
+ /* Update segment length */
+ SetWBE( &s->p_start[i_length_pos/8], (bs_pos(s) - i_length_pos)/8 -2 );
+ }
+}
+
+static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
+ int i_line );
+static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
+ int i_line );
+static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
+ int i_line );
+static void encode_pixel_data( encoder_t *p_enc, bs_t *s,
+ subpicture_region_t *p_region,
+ bool b_top )
+{
+ unsigned int i_line;
+
+ /* Sanity check */
+ if( p_region->fmt.i_chroma != VLC_FOURCC('Y','U','V','P') ) return;
+
+ /* Encode line by line */
+ for( i_line = !b_top; i_line < p_region->fmt.i_visible_height;
+ i_line += 2 )
+ {
+ switch( p_region->fmt.p_palette->i_entries )
+ {
+ case 0:
+ break;
+
+ case 4:
+ bs_write( s, 8, 0x10 ); /* 2 bit/pixel code string */
+ encode_pixel_line_2bp( s, p_region, i_line );
+ break;
+
+ case 16:
+ bs_write( s, 8, 0x11 ); /* 4 bit/pixel code string */
+ encode_pixel_line_4bp( s, p_region, i_line );
+ break;
+
+ case 256:
+ bs_write( s, 8, 0x12 ); /* 8 bit/pixel code string */
+ encode_pixel_line_8bp( s, p_region, i_line );
+ break;
+
+ default:
+ msg_Err( p_enc, "subpicture palette (%i) not handled",
+ p_region->fmt.p_palette->i_entries );
+ break;
+ }
+
+ bs_write( s, 8, 0xf0 ); /* End of object line code */
+ }
+}
+
+static void encode_pixel_line_2bp( bs_t *s, subpicture_region_t *p_region,
+ int i_line )
+{
+ unsigned int i, i_length = 0;
+ int i_pitch = p_region->p_picture->p->i_pitch;
+ uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
+ int i_last_pixel = p_data[0];
+
+ for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
+ {
+ if( ( i != p_region->fmt.i_visible_width ) &&
+ ( p_data[i] == i_last_pixel ) && ( i_length != 284 ) )
+ {
+ i_length++;
+ continue;
+ }
+
+ if( ( i_length == 1 ) || ( i_length == 11 ) || ( i_length == 28 ) )
+ {
+ /* 2bit/pixel code */
+ if( i_last_pixel )
+ bs_write( s, 2, i_last_pixel );
+ else
+ {
+ bs_write( s, 2, 0 );
+ bs_write( s, 1, 0 );
+ bs_write( s, 1, 1 ); /* pseudo color 0 */
+ }
+ i_length--;
+ }
+
+ if( i_length == 2 )
+ {
+ if( i_last_pixel )
+ {
+ bs_write( s, 2, i_last_pixel );
+ bs_write( s, 2, i_last_pixel );
+ }
+ else
+ {
+ bs_write( s, 2, 0 );
+ bs_write( s, 1, 0 );
+ bs_write( s, 1, 0 );
+ bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
+ }
+ }
+ else if( i_length > 2 )
+ {
+ bs_write( s, 2, 0 );
+ if( i_length <= 10 )
+ {
+ bs_write( s, 1, 1 );
+ bs_write( s, 3, i_length - 3 );
+ bs_write( s, 2, i_last_pixel );
+ }
+ else
+ {
+ bs_write( s, 1, 0 );
+ bs_write( s, 1, 0 );
+
+ if( i_length <= 27 )
+ {
+ bs_write( s, 2, 2 );
+ bs_write( s, 4, i_length - 12 );
+ bs_write( s, 2, i_last_pixel );
+ }
+ else
+ {
+ bs_write( s, 2, 3 );
+ bs_write( s, 8, i_length - 29 );
+ bs_write( s, 2, i_last_pixel );
+ }
+ }
+ }
+
+ if( i == p_region->fmt.i_visible_width ) break;
+
+ i_last_pixel = p_data[i];
+ i_length = 1;
+ }
+
+ /* Stop */
+ bs_write( s, 2, 0 );
+ bs_write( s, 1, 0 );
+ bs_write( s, 1, 0 );
+ bs_write( s, 2, 0 );
+
+ /* Stuffing */
+ bs_align_0( s );
+}
+
+static void encode_pixel_line_4bp( bs_t *s, subpicture_region_t *p_region,
+ int i_line )
+{
+ unsigned int i, i_length = 0;
+ int i_pitch = p_region->p_picture->p->i_pitch;
+ uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
+ int i_last_pixel = p_data[0];
+
+ for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
+ {
+ if( i != p_region->fmt.i_visible_width &&
+ p_data[i] == i_last_pixel && i_length != 280 )
+ {
+ i_length++;
+ continue;
+ }
+
+ if( ( i_length == 1 ) ||
+ ( ( i_length == 3 ) && i_last_pixel ) ||
+ ( i_length == 8 ) )
+ {
+ /* 4bit/pixel code */
+ if( i_last_pixel )
+ bs_write( s, 4, i_last_pixel );
+ else
+ {
+ bs_write( s, 4, 0 );
+ bs_write( s, 1, 1 );
+ bs_write( s, 1, 1 );
+ bs_write( s, 2, 0 ); /* pseudo color 0 */
+ }
+ i_length--;
+ }
+
+ if( i_length == 2 )
+ {
+ if( i_last_pixel )
+ {
+ bs_write( s, 4, i_last_pixel );
+ bs_write( s, 4, i_last_pixel );
+ }
+ else
+ {
+ bs_write( s, 4, 0 );
+ bs_write( s, 1, 1 );
+ bs_write( s, 1, 1 );
+ bs_write( s, 2, 1 ); /* 2 * pseudo color 0 */
+ }
+ }
+ else if( !i_last_pixel && ( i_length >= 3 ) && ( i_length <= 9 ) )
+ {
+ bs_write( s, 4, 0 );
+ bs_write( s, 1, 0 );
+ bs_write( s, 3, i_length - 2 ); /* (i_length - 2) * color 0 */
+ }
+ else if( i_length > 2 )
+ {
+ bs_write( s, 4, 0 );
+ bs_write( s, 1, 1 );
+
+ if( i_length <= 7 )
+ {
+ bs_write( s, 1, 0 );
+ bs_write( s, 2, i_length - 4 );
+ bs_write( s, 4, i_last_pixel );
+ }
+ else
+ {
+ bs_write( s, 1, 1 );
+
+ if( i_length <= 24 )
+ {
+ bs_write( s, 2, 2 );
+ bs_write( s, 4, i_length - 9 );
+ bs_write( s, 4, i_last_pixel );
+ }
+ else
+ {
+ bs_write( s, 2, 3 );
+ bs_write( s, 8, i_length - 25 );
+ bs_write( s, 4, i_last_pixel );
+ }
+ }
+ }
+
+ if( i == p_region->fmt.i_visible_width ) break;
+
+ i_last_pixel = p_data[i];
+ i_length = 1;
+ }
+
+ /* Stop */
+ bs_write( s, 8, 0 );
+
+ /* Stuffing */
+ bs_align_0( s );
+}
+
+static void encode_pixel_line_8bp( bs_t *s, subpicture_region_t *p_region,
+ int i_line )
+{
+ unsigned int i, i_length = 0;
+ int i_pitch = p_region->p_picture->p->i_pitch;
+ uint8_t *p_data = &p_region->p_picture->p->p_pixels[ i_pitch * i_line ];
+ int i_last_pixel = p_data[0];
+
+ for( i = 0; i <= p_region->fmt.i_visible_width; i++ )
+ {
+ if( ( i != p_region->fmt.i_visible_width ) &&
+ ( p_data[i] == i_last_pixel ) && ( i_length != 127 ) )
+ {
+ i_length++;
+ continue;
+ }
+
+ if( ( i_length == 1 ) && i_last_pixel )
+ {
+ /* 8bit/pixel code */
+ bs_write( s, 8, i_last_pixel );
+ }
+ else if( ( i_length == 2 ) && i_last_pixel )
+ {
+ /* 8bit/pixel code */
+ bs_write( s, 8, i_last_pixel );
+ bs_write( s, 8, i_last_pixel );
+ }
+ else if( i_length <= 127 )
+ {
+ bs_write( s, 8, 0 );
+
+ if( !i_last_pixel )
+ {
+ bs_write( s, 1, 0 );
+ bs_write( s, 7, i_length ); /* pseudo color 0 */
+ }
+ else
+ {
+ bs_write( s, 1, 1 );
+ bs_write( s, 7, i_length );
+ bs_write( s, 8, i_last_pixel );
+ }
+ }
+
+ if( i == p_region->fmt.i_visible_width ) break;
+
+ i_last_pixel = p_data[i];
+ i_length = 1;
+ }
+
+ /* Stop */
+ bs_write( s, 8, 0 );
+ bs_write( s, 8, 0 );
+
+ /* Stuffing */
+ bs_align_0( s );