Commit 16d54c3d authored by Laurent Aimar's avatar Laurent Aimar

No functionnal changes.

parent 9c5ebdcd
......@@ -48,6 +48,12 @@
#define VLC_FOURCC_RGBA VLC_FOURCC('R','G','B','A')
#define VLC_FOURCC_TEXT VLC_FOURCC('T','E','X','T')
/* TODO export */
static subpicture_t *subpicture_New( void );
static void subpicture_Delete( subpicture_t *p_subpic );
static void SubpictureChain( subpicture_t **pp_head, subpicture_t *p_subpic );
/* */
typedef struct
{
subpicture_t *p_subpicture;
......@@ -60,6 +66,12 @@ typedef struct
} spu_heap_t;
static void SpuHeapInit( spu_heap_t * );
static int SpuHeapPush( spu_heap_t *, subpicture_t * );
static void SpuHeapDeleteAt( spu_heap_t *, int i_index );
static int SpuHeapDeleteSubpicture( spu_heap_t *, subpicture_t * );
static void SpuHeapClean( spu_heap_t *p_heap );
struct spu_private_t
{
vlc_mutex_t lock; /* lock to protect all followings fields */
......@@ -67,8 +79,6 @@ struct spu_private_t
spu_heap_t heap;
int i_channel; /**< number of subpicture channels registered */
int64_t i_subpicture_order; /**< number of created subpicture since spu creation */
filter_t *p_blend; /**< alpha blending module */
filter_t *p_text; /**< text renderer module */
filter_t *p_scale_yuvp; /**< scaling module for YUVP */
......@@ -90,15 +100,56 @@ struct subpicture_region_private_t
video_format_t fmt;
picture_t *p_picture;
};
static subpicture_region_private_t *SpuRegionPrivateNew( video_format_t * );
static void SpuRegionPrivateDelete( subpicture_region_private_t * );
/* */
typedef struct
{
int w;
int h;
} spu_scale_t;
static spu_scale_t spu_scale_create( int w, int h );
static spu_scale_t spu_scale_unit(void );
static spu_scale_t spu_scale_createq( int wn, int wd, int hn, int hd );
static int spu_scale_w( int v, const spu_scale_t s );
static int spu_scale_h( int v, const spu_scale_t s );
static int spu_invscale_w( int v, const spu_scale_t s );
static int spu_invscale_h( int v, const spu_scale_t s );
typedef struct
{
int i_x;
int i_y;
int i_width;
int i_height;
spu_scale_t scale;
} spu_area_t;
static spu_area_t spu_area_create( int x, int y, int w, int h, spu_scale_t );
static spu_area_t spu_area_scaled( spu_area_t );
static spu_area_t spu_area_unscaled( spu_area_t, spu_scale_t );
static bool spu_area_overlap( spu_area_t, spu_area_t );
/* Subpicture rendered flag
* FIXME ? it could be moved to private ? */
#define SUBPICTURE_RENDERED (0x1000)
#ifdef SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
#if SUBPICTURE_RENDERED < SUBPICTURE_ALIGN_MASK
# error SUBPICTURE_RENDERED too low
#endif
static void SpuRegionPrivateDestroy( subpicture_region_private_t *p_private );
#define SCALE_UNIT (1000)
static int SubpictureCmp( const void *s0, const void *s1 );
static void SpuRenderRegion( spu_t *,
picture_t *p_pic_dst, spu_area_t *,
subpicture_t *, subpicture_region_t *,
const spu_scale_t scale_size,
const video_format_t *p_fmt,
const spu_area_t *p_subtitle_area, int i_subtitle_area );
static void UpdateSPU ( spu_t *, vlc_object_t * );
static int CropCallback( vlc_object_t *, char const *,
......@@ -123,205 +174,15 @@ static int SubFilterCallback( vlc_object_t *, char const *,
static int SubFilterAllocationInit( filter_t *, void * );
static void SubFilterAllocationClean( filter_t * );
#define SCALE_UNIT (1000)
/* */
subpicture_region_t *subpicture_region_New( const video_format_t *p_fmt )
{
subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
if( !p_region )
return NULL;
p_region->fmt = *p_fmt;
p_region->fmt.p_palette = NULL;
if( p_fmt->i_chroma == VLC_FOURCC_YUVP )
{
p_region->fmt.p_palette = calloc( 1, sizeof(*p_region->fmt.p_palette) );
if( p_fmt->p_palette )
*p_region->fmt.p_palette = *p_fmt->p_palette;
}
p_region->i_alpha = 0xff;
p_region->p_next = NULL;
p_region->p_private = NULL;
p_region->psz_text = NULL;
p_region->p_style = NULL;
p_region->p_picture = NULL;
if( p_fmt->i_chroma == VLC_FOURCC_TEXT )
return p_region;
p_region->p_picture = picture_New( p_fmt->i_chroma, p_fmt->i_width, p_fmt->i_height,
p_fmt->i_aspect );
if( !p_region->p_picture )
{
free( p_fmt->p_palette );
free( p_region );
return NULL;
}
return p_region;
}
/* */
void subpicture_region_Delete( subpicture_region_t *p_region )
{
if( !p_region )
return;
if( p_region->p_private )
SpuRegionPrivateDestroy( p_region->p_private );
if( p_region->p_picture )
picture_Release( p_region->p_picture );
free( p_region->fmt.p_palette );
free( p_region->psz_text );
free( p_region->psz_html );
//free( p_region->p_style ); FIXME --fenrir plugin does not allocate the memory for it. I think it might lead to segfault, video renderer can live longer than the decoder
free( p_region );
}
/* */
void subpicture_region_ChainDelete( subpicture_region_t *p_head )
{
while( p_head )
{
subpicture_region_t *p_next = p_head->p_next;
subpicture_region_Delete( p_head );
p_head = p_next;
}
}
/**
* This function create a new empty subpicture.
*/
static subpicture_t *subpicture_New( void )
{
subpicture_t *p_subpic = calloc( 1, sizeof(*p_subpic) );
if( !p_subpic )
return NULL;
p_subpic->i_order = 0;
p_subpic->b_absolute = true;
p_subpic->b_fade = false;
p_subpic->b_subtitle = false;
p_subpic->i_alpha = 0xFF;
p_subpic->p_region = NULL;
p_subpic->pf_render = NULL;
p_subpic->pf_destroy = NULL;
p_subpic->p_sys = NULL;
return p_subpic;
}
static void subpicture_Delete( subpicture_t *p_subpic )
{
subpicture_region_ChainDelete( p_subpic->p_region );
p_subpic->p_region = NULL;
if( p_subpic->pf_destroy )
{
p_subpic->pf_destroy( p_subpic );
}
free( p_subpic );
}
static void SpuHeapInit( spu_heap_t *p_heap )
{
for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
{
spu_heap_entry_t *e = &p_heap->p_entry[i];
e->p_subpicture = NULL;
e->b_reject = false;
}
}
static int SpuHeapPush( spu_heap_t *p_heap, subpicture_t *p_subpic )
{
for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
{
spu_heap_entry_t *e = &p_heap->p_entry[i];
if( e->p_subpicture )
continue;
e->p_subpicture = p_subpic;
e->b_reject = false;
return VLC_SUCCESS;
}
return VLC_EGENERIC;
}
static void SpuHeapDeleteAt( spu_heap_t *p_heap, int i_index )
{
spu_heap_entry_t *e = &p_heap->p_entry[i_index];
if( e->p_subpicture )
subpicture_Delete( e->p_subpicture );
e->p_subpicture = NULL;
}
static int SpuHeapDeleteSubpicture( spu_heap_t *p_heap, subpicture_t *p_subpic )
{
for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
{
spu_heap_entry_t *e = &p_heap->p_entry[i];
if( e->p_subpicture != p_subpic )
continue;
SpuHeapDeleteAt( p_heap, i );
return VLC_SUCCESS;
}
return VLC_EGENERIC;
}
static void SpuHeapClean( spu_heap_t *p_heap )
{
for( int i = 0; i < VOUT_MAX_SUBPICTURES; i++ )
{
spu_heap_entry_t *e = &p_heap->p_entry[i];
if( e->p_subpicture )
subpicture_Delete( e->p_subpicture );
}
}
static subpicture_region_private_t *SpuRegionPrivateCreate( video_format_t *p_fmt )
{
subpicture_region_private_t *p_private = malloc( sizeof(*p_private) );
if( !p_private )
return NULL;
p_private->fmt = *p_fmt;
if( p_fmt->p_palette )
{
p_private->fmt.p_palette = malloc( sizeof(*p_private->fmt.p_palette) );
if( p_private->fmt.p_palette )
*p_private->fmt.p_palette = *p_fmt->p_palette;
}
p_private->p_picture = NULL;
return p_private;
}
static void SpuRegionPrivateDestroy( subpicture_region_private_t *p_private )
{
if( p_private->p_picture )
picture_Release( p_private->p_picture );
free( p_private->fmt.p_palette );
free( p_private );
}
/* */
static void SpuRenderCreateAndLoadText( spu_t *p_spu );
static void SpuRenderCreateAndLoadScale( spu_t *p_spu );
static void SpuRenderCreateAndLoadText( spu_t * );
static void SpuRenderCreateAndLoadScale( spu_t * );
static void SpuRenderCreateBlend( spu_t *, vlc_fourcc_t i_chroma, int i_aspect );
static void FilterRelease( filter_t *p_filter );
/*****************************************************************************
* Public API
*****************************************************************************/
/**
* Creates the subpicture unit
*
......@@ -347,8 +208,6 @@ spu_t *__spu_Create( vlc_object_t *p_this )
SpuHeapInit( &p_sys->heap );
p_sys->i_subpicture_order = 1;
p_sys->p_blend = NULL;
p_sys->p_text = NULL;
p_sys->p_scale = NULL;
......@@ -528,1072 +387,1260 @@ void spu_DestroySubpicture( spu_t *p_spu, subpicture_t *p_subpic )
subpicture_Delete( p_subpic );
}
static void FilterRelease( filter_t *p_filter )
/**
* This function renders all sub picture units in the list.
*/
void spu_RenderSubpictures( spu_t *p_spu,
picture_t *p_pic_dst, const video_format_t *p_fmt_dst,
subpicture_t *p_subpic_list,
const video_format_t *p_fmt_src )
{
if( p_filter->p_module )
module_unneed( p_filter, p_filter->p_module );
spu_private_t *p_sys = p_spu->p;
vlc_object_detach( p_filter );
vlc_object_release( p_filter );
}
const int i_source_video_width = p_fmt_src->i_width;
const int i_source_video_height = p_fmt_src->i_height;
const mtime_t i_current_date = mdate();
static void SpuRenderCreateBlend( spu_t *p_spu, vlc_fourcc_t i_chroma, int i_aspect )
{
filter_t *p_blend;
unsigned int i_subpicture;
subpicture_t *pp_subpicture[VOUT_MAX_SUBPICTURES];
assert( !p_spu->p->p_blend );
unsigned int i_subtitle_region_count;
spu_area_t p_subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
spu_area_t *p_subtitle_area;
int i_subtitle_area;
p_spu->p->p_blend =
p_blend = vlc_custom_create( p_spu, sizeof(filter_t),
VLC_OBJECT_GENERIC, "blend" );
if( !p_blend )
return;
vlc_mutex_lock( &p_sys->lock );
es_format_Init( &p_blend->fmt_in, VIDEO_ES, 0 );
/* Preprocess subpictures */
i_subpicture = 0;
i_subtitle_region_count = 0;
for( subpicture_t * p_subpic = p_subpic_list;
p_subpic != NULL;
p_subpic = p_subpic->p_next )
{
/* */
if( p_subpic->pf_pre_render )
p_subpic->pf_pre_render( p_spu, p_subpic, p_fmt_dst );
es_format_Init( &p_blend->fmt_out, VIDEO_ES, 0 );
p_blend->fmt_out.video.i_x_offset = 0;
p_blend->fmt_out.video.i_y_offset = 0;
p_blend->fmt_out.video.i_chroma = i_chroma;
p_blend->fmt_out.video.i_aspect = i_aspect;
if( p_subpic->pf_update_regions )
{
video_format_t fmt_org = *p_fmt_dst;
fmt_org.i_width =
fmt_org.i_visible_width = i_source_video_width;
fmt_org.i_height =
fmt_org.i_visible_height = i_source_video_height;
/* The blend module will be loaded when needed with the real
* input format */
p_blend->p_module = NULL;
p_subpic->pf_update_regions( p_spu, p_subpic, &fmt_org, i_current_date );
}
/* */
vlc_object_attach( p_blend, p_spu );
}
static void SpuRenderUpdateBlend( spu_t *p_spu, int i_out_width, int i_out_height,
const video_format_t *p_in_fmt )
{
filter_t *p_blend = p_spu->p->p_blend;
/* */
if( p_subpic->b_subtitle )
{
for( subpicture_region_t *r = p_subpic->p_region; r != NULL; r = r->p_next )
i_subtitle_region_count++;
}
assert( p_blend );
/* */
pp_subpicture[i_subpicture++] = p_subpic;
}
/* */
if( p_blend->p_module && p_blend->fmt_in.video.i_chroma != p_in_fmt->i_chroma )
/* Be sure we have at least 1 picture to process */
if( i_subpicture <= 0 )
{
/* The chroma is not the same, we need to reload the blend module
* XXX to match the old behaviour just test !p_blend->fmt_in.video.i_chroma */
module_unneed( p_blend, p_blend->p_module );
p_blend->p_module = NULL;
vlc_mutex_unlock( &p_sys->lock );
return;
}
/* */
p_blend->fmt_in.video = *p_in_fmt;
/* Now order subpicture array
* XXX The order is *really* important for overlap subtitles positionning */
qsort( pp_subpicture, i_subpicture, sizeof(*pp_subpicture), SubpictureCmp );
/* */
p_blend->fmt_out.video.i_width =
p_blend->fmt_out.video.i_visible_width = i_out_width;
p_blend->fmt_out.video.i_height =
p_blend->fmt_out.video.i_visible_height = i_out_height;
/* Allocate area array for subtitle overlap */
i_subtitle_area = 0;
p_subtitle_area = p_subtitle_area_buffer;
if( i_subtitle_region_count > sizeof(p_subtitle_area_buffer)/sizeof(*p_subtitle_area_buffer) )
p_subtitle_area = calloc( i_subtitle_region_count, sizeof(*p_subtitle_area) );
/* */
if( !p_blend->p_module )
p_blend->p_module = module_need( p_blend, "video blending", 0, 0 );
}
static void SpuRenderCreateAndLoadText( spu_t *p_spu )
{
filter_t *p_text;
/* Create the blending module */
if( !p_sys->p_blend )
SpuRenderCreateBlend( p_spu, p_fmt_dst->i_chroma, p_fmt_dst->i_aspect );
assert( !p_spu->p->p_text );
/* Process all subpictures and regions (in the right order) */
for( unsigned int i_index = 0; i_index < i_subpicture; i_index++ )
{
subpicture_t *p_subpic = pp_subpicture[i_index];
subpicture_region_t *p_region;
p_spu->p->p_text =
p_text = vlc_custom_create( p_spu, sizeof(filter_t),
VLC_OBJECT_GENERIC, "spu text" );
if( !p_text )
return;
if( !p_subpic->p_region )
continue;
es_format_Init( &p_text->fmt_in, VIDEO_ES, 0 );
/* FIXME when possible use a better rendering size than source size
* (max of display size and source size for example) FIXME */
int i_render_width = p_subpic->i_original_picture_width;
int i_render_height = p_subpic->i_original_picture_height;
if( !i_render_width || !i_render_height )
{
if( i_render_width != 0 || i_render_height != 0 )
msg_Err( p_spu, "unsupported original picture size %dx%d",
i_render_width, i_render_height );
es_format_Init( &p_text->fmt_out, VIDEO_ES, 0 );
p_text->fmt_out.video.i_width =
p_text->fmt_out.video.i_visible_width = 32;
p_text->fmt_out.video.i_height =
p_text->fmt_out.video.i_visible_height = 32;
p_subpic->i_original_picture_width = i_render_width = i_source_video_width;
p_subpic->i_original_picture_height = i_render_height = i_source_video_height;
}
p_text->pf_sub_buffer_new = spu_new_buffer;
p_text->pf_sub_buffer_del = spu_del_buffer;
if( p_sys->p_text )
{
p_sys->p_text->fmt_out.video.i_width =
p_sys->p_text->fmt_out.video.i_visible_width = i_render_width;
vlc_object_attach( p_text, p_spu );
p_sys->p_text->fmt_out.video.i_height =
p_sys->p_text->fmt_out.video.i_visible_height = i_render_height;
}
/* FIXME TOCHECK shouldn't module_need( , , psz_modulename, false ) do the
* same than these 2 calls ? */
char *psz_modulename = var_CreateGetString( p_spu, "text-renderer" );
if( psz_modulename && *psz_modulename )
{
p_text->p_module = module_need( p_text, "text renderer",
psz_modulename, true );
}
free( psz_modulename );
/* Compute scaling from picture to source size */
spu_scale_t scale = spu_scale_createq( i_source_video_width, i_render_width,
i_source_video_height, i_render_height );
if( !p_text->p_module )
p_text->p_module = module_need( p_text, "text renderer", NULL, false );
/* Update scaling from source size to display size(p_fmt_dst) */
scale.w = scale.w * p_fmt_dst->i_width / i_source_video_width;
scale.h = scale.h * p_fmt_dst->i_height / i_source_video_height;
/* Create a few variables used for enhanced text rendering */
var_Create( p_text, "spu-duration", VLC_VAR_TIME );
var_Create( p_text, "spu-elapsed", VLC_VAR_TIME );
var_Create( p_text, "text-rerender", VLC_VAR_BOOL );
var_Create( p_text, "scale", VLC_VAR_INTEGER );
}
/* Set default subpicture aspect ratio
* FIXME if we only handle 1 aspect ratio per picture, why is it set per
* region ? */
p_region = p_subpic->p_region;
if( !p_region->fmt.i_sar_num || !p_region->fmt.i_sar_den )
{
if( p_region->fmt.i_aspect != 0 )
{
p_region->fmt.i_sar_den = p_region->fmt.i_aspect;
p_region->fmt.i_sar_num = VOUT_ASPECT_FACTOR;
}
else
{
p_region->fmt.i_sar_den = p_fmt_dst->i_sar_den;
p_region->fmt.i_sar_num = p_fmt_dst->i_sar_num;
}
}
static filter_t *CreateAndLoadScale( vlc_object_t *p_obj,
vlc_fourcc_t i_src_chroma, vlc_fourcc_t i_dst_chroma,
bool b_resize )
{
filter_t *p_scale;
/* Take care of the aspect ratio */
if( p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den !=
p_region->fmt.i_sar_den * p_fmt_dst->i_sar_num )
{
/* FIXME FIXME what about region->i_x/i_y ? */
scale.w = scale.w *
(int64_t)p_region->fmt.i_sar_num * p_fmt_dst->i_sar_den /
p_region->fmt.i_sar_den / p_fmt_dst->i_sar_num;
}
p_scale = vlc_custom_create( p_obj, sizeof(filter_t),
VLC_OBJECT_GENERIC, "scale" );
if( !p_scale )
return NULL;
/* Render all regions
* We always transform non absolute subtitle into absolute one on the
* first rendering to allow good subtitle overlap support.
*/
for( p_region = p_subpic->p_region; p_region != NULL; p_region = p_region->p_next )
{
spu_area_t area;
es_format_Init( &p_scale->fmt_in, VIDEO_ES, 0 );
p_scale->fmt_in.video.i_chroma = i_src_chroma;
p_scale->fmt_in.video.i_width =
p_scale->fmt_in.video.i_height = 32;
/* Check scale validity */
if( scale.w <= 0 || scale.h <= 0 )
continue;
es_format_Init( &p_scale->fmt_out, VIDEO_ES, 0 );
p_scale->fmt_out.video.i_chroma = i_dst_chroma;
p_scale->fmt_out.video.i_width =
p_scale->fmt_out.video.i_height = b_resize ? 16 : 32;
/* */
SpuRenderRegion( p_spu, p_pic_dst, &area,
p_subpic, p_region, scale, p_fmt_dst,
p_subtitle_area, i_subtitle_area );
p_scale->pf_vout_buffer_new = spu_new_video_buffer;
p_scale->pf_vout_buffer_del = spu_del_video_buffer;
if( p_subpic->b_subtitle )
{
area = spu_area_unscaled( area, scale );
if( !p_subpic->b_absolute && area.i_width > 0 && area.i_height > 0 )
{
p_region->i_x = area.i_x;
p_region->i_y = area.i_y;
}
if( p_subtitle_area )
p_subtitle_area[i_subtitle_area++] = area;
}
}
if( p_subpic->b_subtitle )
p_subpic->b_absolute = true;
}
vlc_object_attach( p_scale, p_obj );
p_scale->p_module = module_need( p_scale, "video filter2", 0, 0 );
/* */
if( p_subtitle_area != p_subtitle_area_buffer )
free( p_subtitle_area );
return p_scale;
}
static void SpuRenderCreateAndLoadScale( spu_t *p_spu )
{
assert( !p_spu->p->p_scale );
assert( !p_spu->p->p_scale_yuvp );
/* XXX p_spu->p_scale is used for all conversion/scaling except yuvp to
* yuva/rgba */
p_spu->p->p_scale = CreateAndLoadScale( VLC_OBJECT(p_spu),
VLC_FOURCC_YUVA, VLC_FOURCC_YUVA, true );
/* This one is used for YUVP to YUVA/RGBA without scaling
* FIXME rename it */
p_spu->p->p_scale_yuvp = CreateAndLoadScale( VLC_OBJECT(p_spu),
VLC_FOURCC_YUVP, VLC_FOURCC_YUVA, false );
vlc_mutex_unlock( &p_sys->lock );
}
static void SpuRenderText( spu_t *p_spu, bool *pb_rerender_text,
subpicture_t *p_subpic, subpicture_region_t *p_region,
int i_min_scale_ratio )
/*****************************************************************************
* spu_SortSubpictures: find the subpictures to display
*****************************************************************************