static int Open ( vlc_object_t * );
static void Close ( vlc_object_t * );
+#define SOUT_CFG_PREFIX "sout-mp4-"
+
vlc_module_begin();
set_description( _("MP4/MOV muxer") );
- add_bool( "mp4-faststart", 1, NULL, FASTSTART_TEXT, FASTSTART_LONGTEXT,
+ add_bool( SOUT_CFG_PREFIX "faststart", 1, NULL, FASTSTART_TEXT, FASTSTART_LONGTEXT,
VLC_TRUE );
-
set_capability( "sout mux", 5 );
add_shortcut( "mp4" );
add_shortcut( "mov" );
/*****************************************************************************
* Exported prototypes
*****************************************************************************/
+static const char *ppsz_sout_options[] = {
+ "faststart", NULL
+};
+
static int Capability(sout_mux_t *, int, void *, void * );
static int AddStream( sout_mux_t *, sout_input_t * );
static int DelStream( sout_mux_t *, sout_input_t * );
uint64_t i_stco_pos;
vlc_bool_t b_stco64;
+ /* for h264 */
+ struct
+ {
+ int i_profile;
+ int i_level;
+
+ int i_sps;
+ uint8_t *sps;
+ int i_pps;
+ uint8_t *pps;
+ } avc;
+
+ /* for spu */
+ int64_t i_last_dts;
+
} mp4_stream_t;
struct sout_mux_sys_t
static bo_t *GetMoovBox( sout_mux_t *p_mux );
+static block_t *ConvertSUBT( sout_mux_t *, mp4_stream_t *, block_t *);
+static void ConvertAVC1( sout_mux_t *, mp4_stream_t *, block_t * );
+
/*****************************************************************************
* Open:
*****************************************************************************/
sout_mux_sys_t *p_sys;
bo_t *box;
- p_sys = malloc( sizeof( sout_mux_sys_t ) );
+ msg_Dbg( p_mux, "Mp4 muxer opend" );
+ sout_ParseCfg( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
+
+ p_mux->pf_capacity = Capability;
+ p_mux->pf_addstream = AddStream;
+ p_mux->pf_delstream = DelStream;
+ p_mux->pf_mux = Mux;
+ p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
p_sys->i_pos = 0;
p_sys->i_nb_streams = 0;
p_sys->pp_streams = NULL;
p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
p_sys->i_dts_start = 0;
- msg_Dbg( p_mux, "Open" );
-
- p_mux->pf_capacity = Capability;
- p_mux->pf_addstream = AddStream;
- p_mux->pf_delstream = DelStream;
- p_mux->pf_mux = Mux;
- p_mux->p_sys = p_sys;
if( !p_sys->b_mov )
{
moov = GetMoovBox( p_mux );
/* Check we need to create "fast start" files */
- var_Create( p_this, "mp4-faststart", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
- var_Get( p_this, "mp4-faststart", &val );
+ var_Get( p_this, SOUT_CFG_PREFIX "faststart", &val );
p_sys->b_fast_start = val.b_bool;
while( p_sys->b_fast_start )
{
msg_Warn( p_this, "read() not supported by acces output, "
"won't create a fast start file" );
p_sys->b_fast_start = VLC_FALSE;
+ block_Release( p_buf );
break;
}
sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos + i_size +
mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
es_format_Clean( &p_stream->fmt );
+ if( p_stream->avc.i_sps ) free( p_stream->avc.sps );
+ if( p_stream->avc.i_pps ) free( p_stream->avc.pps );
free( p_stream->entry );
free( p_stream );
}
case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
case VLC_FOURCC( 'h', '2', '6', '4' ):
break;
+ case VLC_FOURCC( 's', 'u', 'b', 't' ):
+ msg_Warn( p_mux, "subtitle track added like in .mov (even when creating .mp4)" );
+ break;
default:
msg_Err( p_mux, "unsupported codec %4.4s in mp4",
(char*)&p_input->p_fmt->i_codec );
calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
p_stream->i_dts_start = 0;
p_stream->i_duration = 0;
+ p_stream->avc.i_profile = 77;
+ p_stream->avc.i_level = 51;
+ p_stream->avc.i_sps = 0;
+ p_stream->avc.sps = NULL;
+ p_stream->avc.i_pps = 0;
+ p_stream->avc.pps = NULL;
p_input->p_sys = p_stream;
if( p_fifo->i_depth <= 1 )
{
- return -1; // wait that all fifo have at least 2 packets
+ if( p_mux->pp_inputs[i]->p_fmt->i_cat != SPU_ES )
+ {
+ return -1; // wait that all fifo have at least 2 packets
+ }
+ /* For SPU, we wait only 1 packet */
+ continue;
}
p_buf = block_FifoShow( p_fifo );
sout_input_t *p_input;
int i_stream;
mp4_stream_t *p_stream;
- block_t *p_data;
+ block_t *p_data;
mtime_t i_dts;
if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
p_stream = (mp4_stream_t*)p_input->p_sys;
p_data = block_FifoGet( p_input->p_fifo );
- if( p_input->p_fifo->i_depth > 0 )
+ if( p_stream->fmt.i_codec == VLC_FOURCC( 'h', '2', '6', '4' ) )
{
- block_t *p_next = block_FifoShow( p_input->p_fifo );
- int64_t i_diff = p_next->i_dts - p_data->i_dts;
-
- if( i_diff < I64C(1000000 ) ) /* protection */
- {
- p_data->i_length = i_diff;
- }
+ ConvertAVC1( p_mux, p_stream, p_data );
}
- if( p_data->i_length <= 0 )
+ else if( p_stream->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
{
- msg_Warn( p_mux, "i_length <= 0" );
- p_stream->i_length_neg += p_data->i_length - 1;
- p_data->i_length = 1;
+ p_data = ConvertSUBT( p_mux, p_stream, p_data );
}
- else if( p_stream->i_length_neg < 0 )
+
+ if( p_stream->fmt.i_cat != SPU_ES )
{
- int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
+ /* Fix length of the sample */
+ if( p_input->p_fifo->i_depth > 0 )
+ {
+ block_t *p_next = block_FifoShow( p_input->p_fifo );
+ int64_t i_diff = p_next->i_dts - p_data->i_dts;
+
+ if( i_diff < I64C(1000000 ) ) /* protection */
+ {
+ p_data->i_length = i_diff;
+ }
+ }
+ if( p_data->i_length <= 0 )
+ {
+ msg_Warn( p_mux, "i_length <= 0" );
+ p_stream->i_length_neg += p_data->i_length - 1;
+ p_data->i_length = 1;
+ }
+ else if( p_stream->i_length_neg < 0 )
+ {
+ int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
- p_data->i_length -= i_recover;
- p_stream->i_length_neg += i_recover;
+ p_data->i_length -= i_recover;
+ p_stream->i_length_neg += i_recover;
+ }
}
/* Save starting time */
}
}
+ if( p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0 )
+ {
+ int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
+
+ if( i_length <= 0 )
+ {
+ /* FIXME handle this broken case */
+ i_length = 1;
+ }
+
+ /* Fix last entry */
+ if( p_stream->entry[p_stream->i_entry_count-1].i_length <= 0 )
+ {
+ p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
+ }
+ }
+
+
/* add index entry */
p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_buffer;
p_stream->entry[p_stream->i_entry_count].i_flags = p_data->i_flags;
p_stream->i_entry_count++;
- if( p_stream->i_entry_count >= p_stream->i_entry_max )
+ /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
+ if( p_stream->i_entry_count >= p_stream->i_entry_max - 1 )
{
p_stream->i_entry_max += 1000;
p_stream->entry =
p_stream->i_duration += p_data->i_length;
p_sys->i_pos += p_data->i_buffer;
+ /* Save the DTS */
+ p_stream->i_last_dts = p_data->i_dts;
+
/* write data */
sout_AccessOutWrite( p_mux->p_access, p_data );
+
+ if( p_stream->fmt.i_cat == SPU_ES )
+ {
+ int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
+
+ if( i_length != 0 )
+ {
+ /* TODO */
+ msg_Dbg( p_mux, "writing a empty subs" ) ;
+
+ /* Append a idx entry */
+ p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
+ p_stream->entry[p_stream->i_entry_count].i_size = 3;
+ p_stream->entry[p_stream->i_entry_count].i_pts_dts= 0;
+ p_stream->entry[p_stream->i_entry_count].i_length = 0;
+ p_stream->entry[p_stream->i_entry_count].i_flags = 0;
+
+ /* XXX: No need to grow the entry here */
+ p_stream->i_entry_count++;
+
+ /* Fix last dts */
+ p_stream->i_last_dts += i_length;
+
+ /* Write a " " */
+ p_data = block_New( p_mux, 3 );
+ p_data->p_buffer[0] = 0;
+ p_data->p_buffer[1] = 1;
+ p_data->p_buffer[2] = ' ';
+
+ p_sys->i_pos += p_data->i_buffer;
+
+ sout_AccessOutWrite( p_mux->p_access, p_data );
+ }
+
+ /* Fix duration */
+ p_stream->i_duration = p_stream->i_last_dts - p_stream->i_dts_start;
+ }
}
return( VLC_SUCCESS );
/*****************************************************************************
*
*****************************************************************************/
+static block_t *ConvertSUBT( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
+{
+ p_block = block_Realloc( p_block, 2, p_block->i_buffer );
+
+ /* No trailling '\0' */
+ if( p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0' )
+ p_block->i_buffer--;
+
+ p_block->p_buffer[0] = ( (p_block->i_buffer - 2) >> 8 )&0xff;
+ p_block->p_buffer[1] = ( (p_block->i_buffer - 2) )&0xff;
+
+ return p_block;
+}
+
+static void ConvertAVC1( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
+{
+ uint8_t *src = p_block->p_buffer;
+ int i_src = p_block->i_buffer;
+ uint8_t *end = &p_block->p_buffer[i_src];
+
+ uint8_t *dst = p_block->p_buffer;
+
+ while( src < end )
+ {
+ uint8_t *fix = dst;
+ int i_type;
+ int i_size;
+
+ if( src[0] != 0 || src[1] != 0 || src[2] != 0 || src[3] != 1 )
+ break;
+
+ /* skip start code and room for size */
+ src += 4;
+ dst += 4;
+
+ /* nal type */
+ i_type = (*dst++ = *src++)&0x1f;
+
+ /* nal content */
+ while( src < end )
+ {
+ if( src < end - 4 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x00 && src[3] == 0x01 )
+ {
+ break;
+ }
+
+ if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 )
+ {
+ *dst++ = 0x00;
+ *dst++ = 0x00;
+
+ src += 3;
+ continue;
+ }
+ *dst++ = *src++;
+ }
+ i_size = dst - &fix[4];
+ fix[0] = (i_size >> 24)&0xff;
+ fix[1] = (i_size >> 16)&0xff;
+ fix[2] = (i_size >> 8)&0xff;
+ fix[3] = (i_size )&0xff;
+
+ if( i_type == 7 && tk->avc.i_sps <= 0 ) /* SPS */
+ {
+ tk->avc.i_sps = i_size;
+ tk->avc.sps = malloc( i_size );
+ memcpy( tk->avc.sps, &fix[4], i_size );
+
+ tk->avc.i_profile = tk->avc.sps[1];
+ tk->avc.i_level = tk->avc.sps[3];
+ }
+ else if( i_type == 8 && tk->avc.i_pps <= 0) /* PPS */
+ {
+ tk->avc.i_pps = i_size;
+ tk->avc.pps = malloc( i_size );
+ memcpy( tk->avc.pps, &fix[4], i_size );
+ }
+ }
+
+ p_block->i_buffer = dst - p_block->p_buffer;
+}
+
+
static int GetDescrLength( int i_size )
{
if( i_size < 0x00000080 )
return wave;
}
+static bo_t *GetAvcCTag( mp4_stream_t *p_stream )
+{
+ bo_t *avcC;
+
+ /* FIXME use better value */
+ avcC = box_new( "avcC" );
+ bo_add_8( avcC, 1 ); /* configuration version */
+ bo_add_8( avcC, p_stream->avc.i_profile );
+ bo_add_8( avcC, p_stream->avc.i_profile ); /* profile compatible ??? */
+ bo_add_8( avcC, p_stream->avc.i_level ); /* level, 5.1 */
+ bo_add_8( avcC, 0xff ); /* 0b11111100 | lengthsize = 0x11 */
+
+ bo_add_8( avcC, 0xe0 | (p_stream->avc.i_sps > 0 ? 1 : 0) ); /* 0b11100000 | sps_count */
+ if( p_stream->avc.i_sps > 0 )
+ {
+ bo_add_16be( avcC, p_stream->avc.i_sps );
+ bo_add_mem( avcC, p_stream->avc.i_sps, p_stream->avc.sps );
+ }
+
+ bo_add_8( avcC, (p_stream->avc.i_pps > 0 ? 1 : 0) ); /* pps_count */
+ if( p_stream->avc.i_pps > 0 )
+ {
+ bo_add_16be( avcC, p_stream->avc.i_pps );
+ bo_add_mem( avcC, p_stream->avc.i_pps, p_stream->avc.pps );
+ }
+ box_fix( avcC );
+
+ return avcC;
+}
+
/* TODO: No idea about these values */
static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
{
memcpy( fcc, "SVQ3", 4 );
break;
+ case VLC_FOURCC('h','2','6','4'):
+ memcpy( fcc, "avc1", 4 );
+ break;
+
default:
memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
break;
}
break;
+ case VLC_FOURCC('h','2','6','4'):
+ box_gather( vide, GetAvcCTag( p_stream ) );
+ break;
+
default:
break;
}
return vide;
}
+static bo_t *GetTextBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
+{
+
+ bo_t *text = box_new( "text" );
+ int i;
+
+ for( i = 0; i < 6; i++ )
+ {
+ bo_add_8( text, 0 ); // reserved;
+ }
+ bo_add_16be( text, 1 ); // data-reference-index
+
+ bo_add_32be( text, 0 ); // display flags
+ bo_add_32be( text, 0 ); // justification
+ for( i = 0; i < 3; i++ )
+ {
+ bo_add_16be( text, 0 ); // back ground color
+ }
+
+ bo_add_16be( text, 0 ); // box text
+ bo_add_16be( text, 0 ); // box text
+ bo_add_16be( text, 0 ); // box text
+ bo_add_16be( text, 0 ); // box text
+
+ bo_add_64be( text, 0 ); // reserved
+ for( i = 0; i < 3; i++ )
+ {
+ bo_add_16be( text, 0xff ); // foreground color
+ }
+
+ bo_add_8 ( text, 9 );
+ bo_add_mem( text, 9, "Helvetica" );
+
+ box_fix( text );
+
+ return text;
+}
+
static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
{
sout_mux_sys_t *p_sys = p_mux->p_sys;
bo_t *vide = GetVideBox( p_mux, p_stream );
box_gather( stsd, vide );
}
+ else if( p_stream->fmt.i_cat == SPU_ES )
+ {
+ box_gather( stsd, GetTextBox( p_mux, p_stream ) );
+ }
box_fix( stsd );
/* chunk offset table */
p_stream = p_sys->pp_streams[i_trak];
- if( p_stream->fmt.i_cat != AUDIO_ES &&
- p_stream->fmt.i_cat != VIDEO_ES )
- {
- msg_Err( p_mux, "FIXME ignoring trak (noaudio&&novideo)" );
- continue;
- }
-
if( p_stream->fmt.i_cat == AUDIO_ES )
i_timescale = p_stream->fmt.audio.i_rate;
else
bo_add_32be( tkhd, 0 ); // width (presentation)
bo_add_32be( tkhd, 0 ); // height(presentation)
}
- else
+ else if( p_stream->fmt.i_cat == VIDEO_ES )
{
+ int i_width = p_stream->fmt.video.i_width;
+ if( p_stream->fmt.video.i_aspect > 0 )
+ {
+ i_width = p_stream->fmt.video.i_aspect *
+ p_stream->fmt.video.i_height /
+ VOUT_ASPECT_FACTOR << 16;
+ }
// width (presentation)
- bo_add_32be( tkhd, p_stream->fmt.video.i_aspect *
- p_stream->fmt.video.i_height /
- VOUT_ASPECT_FACTOR << 16 );
+ bo_add_32be( tkhd, i_width );
// height(presentation)
bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
}
+ else
+ {
+ int i_width = 320;
+ int i_height = 200;
+ int i;
+ for( i = 0; i < p_sys->i_nb_streams; i++ )
+ {
+ mp4_stream_t *tk = p_sys->pp_streams[i];
+ if( tk->fmt.i_cat == VIDEO_ES )
+ {
+ if( p_stream->fmt.video.i_aspect )
+ i_width = p_stream->fmt.video.i_aspect *
+ p_stream->fmt.video.i_height / VOUT_ASPECT_FACTOR;
+ else
+ i_width = p_stream->fmt.video.i_width;
+ i_height = p_stream->fmt.video.i_height;
+ break;
+ }
+ }
+ bo_add_32be( tkhd, i_width << 16 ); // width (presentation)
+ bo_add_32be( tkhd, i_height << 16 ); // height(presentation)
+ }
+
box_fix( tkhd );
box_gather( trak, tkhd );
{
bo_add_fourcc( hdlr, "soun" );
}
- else
+ else if( p_stream->fmt.i_cat == VIDEO_ES )
{
bo_add_fourcc( hdlr, "vide" );
}
+ else if( p_stream->fmt.i_cat == SPU_ES )
+ {
+ bo_add_fourcc( hdlr, "text" );
+ }
bo_add_32be( hdlr, 0 ); // reserved
bo_add_32be( hdlr, 0 ); // reserved
bo_add_8( hdlr, 12 );
if( p_stream->fmt.i_cat == AUDIO_ES )
bo_add_mem( hdlr, 12, "SoundHandler" );
- else
+ else if( p_stream->fmt.i_cat == VIDEO_ES )
bo_add_mem( hdlr, 12, "VideoHandler" );
+ else
+ bo_add_mem( hdlr, 12, "Text Handler" );
box_fix( hdlr );
box_gather( mdia, hdlr );
box_gather( minf, vmhd );
}
+ else if( p_stream->fmt.i_cat == SPU_ES )
+ {
+ bo_t *gmhd = box_new( "gmhd" );
+ bo_t *gmin = box_full_new( "gmin", 0, 1 );
+
+ bo_add_16be( gmin, 0 ); // graphicsmode
+ for( i = 0; i < 3; i++ )
+ {
+ bo_add_16be( gmin, 0 ); // opcolor
+ }
+ bo_add_16be( gmin, 0 ); // balance
+ bo_add_16be( gmin, 0 ); // reserved
+ box_fix( gmin );
+
+ box_gather( gmhd, gmin );
+ box_fix( gmhd );
+
+ box_gather( minf, gmhd );
+ }
/* dinf */
dinf = box_new( "dinf" );