📄 dvbsub.c
字号:
if( p_sys->p_page ) msg_Dbg( p_dec, "rendering %i regions", p_sys->p_page->i_region_defs );#endif for( i = 0; p_sys->p_page && i < p_sys->p_page->i_region_defs; i++ ) { dvbsub_region_t *p_region; dvbsub_regiondef_t *p_regiondef; dvbsub_clut_t *p_clut; dvbsub_color_t *p_color; subpicture_region_t *p_spu_region; uint8_t *p_src, *p_dst; video_format_t fmt; int i_pitch; i_timeout = p_sys->p_page->i_timeout; p_regiondef = &p_sys->p_page->p_region_defs[i];#ifdef DEBUG_DVBSUB msg_Dbg( p_dec, "rendering region %i (%i,%i)", i, p_regiondef->i_x, p_regiondef->i_y );#endif /* Find associated region */ for( p_region = p_sys->p_regions; p_region != NULL; p_region = p_region->p_next ) { if( p_regiondef->i_id == p_region->i_id ) break; } if( !p_region ) { msg_Dbg( p_dec, "region %i not found", p_regiondef->i_id ); continue; } /* Find associated CLUT */ for( p_clut = p_sys->p_cluts; p_clut != NULL; p_clut = p_clut->p_next ) { if( p_region->i_clut == p_clut->i_id ) break; } if( !p_clut ) { msg_Dbg( p_dec, "clut %i not found", p_region->i_clut ); continue; } /* Create new SPU region */ memset( &fmt, 0, sizeof(video_format_t) ); fmt.i_chroma = VLC_FOURCC('Y','U','V','P'); fmt.i_aspect = 0; /* 0 means use aspect ratio of background video */ fmt.i_width = fmt.i_visible_width = p_region->i_width; fmt.i_height = fmt.i_visible_height = p_region->i_height; fmt.i_x_offset = fmt.i_y_offset = 0; p_spu_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt ); if( !p_region ) { msg_Err( p_dec, "cannot allocate SPU region" ); continue; } p_spu_region->i_x = p_regiondef->i_x; p_spu_region->i_y = p_regiondef->i_y; *pp_spu_region = p_spu_region; pp_spu_region = &p_spu_region->p_next; /* Build palette */ fmt.p_palette->i_entries = p_region->i_depth == 1 ? 4 : p_region->i_depth == 2 ? 16 : 256; p_color = (p_region->i_depth == 1) ? p_clut->c_2b : (p_region->i_depth == 2) ? p_clut->c_4b : p_clut->c_8b; for( j = 0; j < fmt.p_palette->i_entries; j++ ) { fmt.p_palette->palette[j][0] = p_color[j].Y; fmt.p_palette->palette[j][1] = p_color[j].Cr; fmt.p_palette->palette[j][2] = p_color[j].Cb; fmt.p_palette->palette[j][3] = 0xff - p_color[j].T; } p_src = p_region->p_pixbuf; p_dst = p_spu_region->picture.Y_PIXELS; i_pitch = p_spu_region->picture.Y_PITCH; /* Copy pixel buffer */ for( j = 0; j < p_region->i_height; j++ ) { memcpy( p_dst, p_src, p_region->i_width ); p_src += p_region->i_width; p_dst += i_pitch; } /* Check subtitles encoded as strings of characters * (since there are not rendered in the pixbuffer) */ for( j = 0; j < p_region->i_object_defs; j++ ) { dvbsub_objectdef_t *p_object_def = &p_region->p_object_defs[i]; if( p_object_def->i_type != 1 || !p_object_def->psz_text ) continue; /* Create new SPU region */ memset( &fmt, 0, sizeof(video_format_t) ); fmt.i_chroma = VLC_FOURCC('T','E','X','T'); fmt.i_aspect = VOUT_ASPECT_FACTOR; fmt.i_width = fmt.i_visible_width = p_region->i_width; fmt.i_height = fmt.i_visible_height = p_region->i_height; fmt.i_x_offset = fmt.i_y_offset = 0; p_spu_region = p_spu->pf_create_region( VLC_OBJECT(p_dec), &fmt ); if( !p_region ) { msg_Err( p_dec, "cannot allocate SPU region" ); continue; } p_spu_region->psz_text = strdup( p_object_def->psz_text ); p_spu_region->i_x = p_regiondef->i_x + p_object_def->i_x; p_spu_region->i_y = p_regiondef->i_y + p_object_def->i_y; *pp_spu_region = p_spu_region; pp_spu_region = &p_spu_region->p_next; } } /* Set the pf_render callback */ p_spu->i_start = p_sys->i_pts; p_spu->i_stop = p_spu->i_start + i_timeout * 1000000; p_spu->b_ephemer = VLC_TRUE; return p_spu;}/***************************************************************************** * encoder_sys_t : encoder descriptor *****************************************************************************/typedef struct encoder_region_t{ int i_width; int i_height;} encoder_region_t;struct encoder_sys_t{ unsigned int i_page_ver; unsigned int i_region_ver; unsigned int i_clut_ver; int i_regions; encoder_region_t *p_regions; mtime_t i_pts;};static void encode_page_composition( encoder_t *, bs_t *, subpicture_t * );static void encode_clut( encoder_t *, bs_t *, subpicture_t * );static void encode_region_composition( encoder_t *, bs_t *, subpicture_t * );static void encode_object( encoder_t *, bs_t *, subpicture_t * );/***************************************************************************** * OpenEncoder: probe the encoder and return score *****************************************************************************/static int OpenEncoder( vlc_object_t *p_this ){ encoder_t *p_enc = (encoder_t *)p_this; encoder_sys_t *p_sys; if( p_enc->fmt_out.i_codec != VLC_FOURCC('d','v','b','s') && !p_enc->b_force ) { return VLC_EGENERIC; } /* Allocate the memory needed to store the decoder's structure */ if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL ) { msg_Err( p_enc, "out of memory" ); return VLC_EGENERIC; } p_enc->p_sys = p_sys; p_enc->pf_encode_sub = Encode; p_enc->fmt_out.i_codec = VLC_FOURCC('d','v','b','s'); p_enc->fmt_out.subs.dvb.i_id = 1 << 16 | 1; sout_CfgParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg ); p_sys->i_page_ver = 0; p_sys->i_region_ver = 0; p_sys->i_clut_ver = 0; p_sys->i_regions = 0; p_sys->p_regions = 0; return VLC_SUCCESS;}/**************************************************************************** * Encode: the whole thing ****************************************************************************/static block_t *Encode( encoder_t *p_enc, subpicture_t *p_subpic ){ bs_t bits, *s = &bits; block_t *p_block; if( !p_subpic || !p_subpic->p_region ) return 0; msg_Dbg( p_enc, "encoding subpicture" ); 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; } msg_Dbg( p_enc, "subpicture encoded properly" ); 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; 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; vlc_bool_t b_mode_change = VLC_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 ) { b_mode_change = VLC_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 = VLC_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 + 15 ); /* 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 */ 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; vlc_bool_t 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; 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -