Commit a9e48057 authored by Laurent Aimar's avatar Laurent Aimar
Browse files

* mp4.c : begining of a mp4/mov muxer.

 It's far from stable, produce file unplayable by mplayer at least,
and doesn't yet take care of video/audio synchro.
Becarefull that the header is written only when closing, and there is
no way to repair a mp4 file without its header.
parent f4e8bac1
/*****************************************************************************
* mp4.c
*****************************************************************************
* Copyright (C) 2001, 2002, 2003 VideoLAN
* $Id: mp4.c,v 1.1 2003/04/18 22:43:08 fenrir Exp $
*
* Authors: Laurent Aimar <fenrir@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <vlc/vlc.h>
#include <vlc/input.h>
#include <vlc/sout.h>
#include "codecs.h"
/*****************************************************************************
* Exported prototypes
*****************************************************************************/
static int Open ( vlc_object_t * );
static void Close ( vlc_object_t * );
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 * );
static int Mux ( sout_mux_t * );
/*****************************************************************************
* Module descriptor
*****************************************************************************/
vlc_module_begin();
set_description( _("MP4/MOV muxer") );
set_capability( "sout mux", 5 );
add_shortcut( "mp4" );
add_shortcut( "mov" );
set_callbacks( Open, Close );
vlc_module_end();
typedef struct
{
uint64_t i_pos;
int i_size;
mtime_t i_pts;
mtime_t i_dts;
mtime_t i_length;
} mp4_entry_t;
typedef struct
{
sout_format_t *p_fmt;
int i_track_id;
/* index */
unsigned int i_entry_count;
unsigned int i_entry_max;
mp4_entry_t *entry;
/* stats */
mtime_t i_duration;
} mp4_stream_t;
struct sout_mux_sys_t
{
uint64_t i_mdat_pos;
uint64_t i_pos;
int i_nb_streams;
mp4_stream_t **pp_streams;
};
typedef struct bo_t bo_t;
struct bo_t
{
vlc_bool_t b_grow;
int i_buffer_size;
int i_buffer;
uint8_t *p_buffer;
};
static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
static void bo_add_8 ( bo_t *, uint8_t );
static void bo_add_16be ( bo_t *, uint16_t );
static void bo_add_24be ( bo_t *, uint32_t );
static void bo_add_32be ( bo_t *, uint32_t );
static void bo_add_64be ( bo_t *, uint64_t );
static void bo_add_fourcc(bo_t *, char * );
static void bo_add_bo ( bo_t *, bo_t * );
static void bo_add_mem ( bo_t *, int , uint8_t * );
static void bo_fix_32be ( bo_t *, int , uint32_t );
static bo_t * box_new ( char *fcc );
static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f );
static void box_fix ( bo_t *box );
static void box_free( bo_t *box );
static void box_gather ( bo_t *box, bo_t *box2 );
static void box_send( sout_mux_t *p_mux, bo_t *box );
static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box );
/*****************************************************************************
* Open:
*****************************************************************************/
static int Open( vlc_object_t *p_this )
{
sout_mux_t *p_mux = (sout_mux_t*)p_this;
sout_mux_sys_t *p_sys;
bo_t *box;
p_sys = malloc( sizeof( sout_mux_sys_t ) );
p_sys->i_pos = 0;
p_sys->i_nb_streams = 0;
p_sys->pp_streams = NULL;
msg_Info( 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;
//p_mux->i_preheader = 0;
/* Now add ftyp header */
box = box_new( "ftyp" );
bo_add_fourcc( box, "isom" );
bo_add_32be ( box, 0 );
bo_add_fourcc( box, "mp41" );
box_fix( box );
p_sys->i_pos += box->i_buffer;
p_sys->i_mdat_pos = p_sys->i_pos;
box_send( p_mux, box );
/* Now add mdat header */
box = box_new( "mdat" );
bo_add_64be ( box, 0 ); // XXX large size
p_sys->i_pos += box->i_buffer;
box_send( p_mux, box );
return VLC_SUCCESS;
}
static uint32_t GetDescriptorLength24b( int i_length )
{
uint32_t i_l1, i_l2, i_l3;
i_l1 = i_length&0x7f;
i_l2 = ( i_length >> 7 )&0x7f;
i_l3 = ( i_length >> 14 )&0x7f;
return( 0x808000 | ( i_l3 << 16 ) | ( i_l2 << 8 ) | i_l1 );
}
static bo_t *GetESDS( mp4_stream_t *p_stream )
{
bo_t *esds;
int i_stream_type;
int i_object_type_indication;
int i_decoder_specific_info_size;
if( p_stream->p_fmt->i_extra_data > 0 )
{
i_decoder_specific_info_size = p_stream->p_fmt->i_extra_data + 4;
}
else
{
i_decoder_specific_info_size = 0;
}
esds = box_full_new( "esds", 0, 0 );
bo_add_8 ( esds, 0x03 ); // ES_DescrTag
bo_add_24be( esds, GetDescriptorLength24b( 25 + i_decoder_specific_info_size ) );
bo_add_16be( esds, p_stream->i_track_id );
bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
bo_add_8 ( esds, 0x04 ); // DecoderConfigDescrTag
bo_add_24be( esds, GetDescriptorLength24b( 13 + i_decoder_specific_info_size ) );
switch( p_stream->p_fmt->i_fourcc )
{
case VLC_FOURCC( 'm', 'p', '4', 'v' ):
i_object_type_indication = 0x20;
break;
case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
i_object_type_indication = 0x60; /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
break;
case VLC_FOURCC( 'm', 'p', '4', 'a' ):
i_object_type_indication = 0x40; /* FIXME for mpeg2-aac == 0x66->0x68 */
break;
case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
i_object_type_indication = p_stream->p_fmt->i_sample_rate < 32000 ? 0x69 : 0x6b;
break;
default:
i_object_type_indication = 0x00;
break;
}
i_stream_type = p_stream->p_fmt->i_cat == VIDEO_ES ? 0x04 : 0x05;
bo_add_8 ( esds, i_object_type_indication );
bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
bo_add_32be( esds, 0x7fffffff ); // maxBitrate
bo_add_32be( esds, 0 ); // avgBitrate
if( p_stream->p_fmt->i_extra_data > 0 )
{
int i;
bo_add_8 ( esds, 0x05 ); // DecoderSpecificInfo
bo_add_24be( esds, GetDescriptorLength24b( p_stream->p_fmt->i_extra_data ) );
for( i = 0; i < p_stream->p_fmt->i_extra_data; i++ )
{
bo_add_8 ( esds, p_stream->p_fmt->p_extra_data[i] );
}
}
/* SL_Descr mandatory */
bo_add_8 ( esds, 0x06 ); // SLConfigDescriptorTag
bo_add_24be( esds, GetDescriptorLength24b( 1 ) );
bo_add_8 ( esds, 0x02 ); // sl_predefined
box_fix( esds );
return esds;
}
/*****************************************************************************
* Close:
*****************************************************************************/
static uint32_t mvhd_matrix[9] = { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
static void Close( vlc_object_t * p_this )
{
sout_mux_t *p_mux = (sout_mux_t*)p_this;
sout_mux_sys_t *p_sys = p_mux->p_sys;
sout_buffer_t *p_hdr;
bo_t bo;
bo_t *moov, *mvhd;
unsigned int i;
int i_trak, i_index;
uint32_t i_movie_timescale = 90000;
int64_t i_movie_duration = 0;
msg_Info( p_mux, "Close" );
/* create general info */
for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
{
mp4_stream_t *p_stream;
p_stream = p_sys->pp_streams[i_trak];
i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
}
msg_Dbg( p_mux, "movie duration %ds", (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
i_movie_duration = i_movie_duration * (int64_t)i_movie_timescale / (int64_t)1000000;
/* *** update mdat size *** */
bo_init ( &bo, 0, NULL, VLC_TRUE );
bo_add_32be ( &bo, 1 );
bo_add_fourcc( &bo, "mdat" );
bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
p_hdr = bo_to_sout( p_mux->p_sout, &bo );
free( bo.p_buffer );
/* seek to mdat */
sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
sout_AccessOutWrite( p_mux->p_access, p_hdr );
/* Now create header */
sout_AccessOutSeek( p_mux->p_access, p_sys->i_pos );
moov = box_new( "moov" );
/* *** add /moov/mvhd *** */
mvhd = box_full_new( "mvhd", 1, 0 );
bo_add_64be( mvhd, 0 ); // creation time FIXME
bo_add_64be( mvhd, 0 ); // modification time FIXME
bo_add_32be( mvhd, i_movie_timescale); // timescale
bo_add_64be( mvhd, i_movie_duration ); // duration
bo_add_32be( mvhd, 0x10000 ); // rate
bo_add_16be( mvhd, 0x100 ); // volume
bo_add_16be( mvhd, 0 ); // reserved
for( i = 0; i < 2; i++ )
{
bo_add_32be( mvhd, 0 ); // reserved
}
for( i = 0; i < 9; i++ )
{
bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
}
for( i = 0; i < 6; i++ )
{
bo_add_32be( mvhd, 0 ); // pre-defined
}
bo_add_32be( mvhd, 0xffffffff ); // next-track-id FIXME
box_fix( mvhd );
box_gather( moov, mvhd );
for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
{
mp4_stream_t *p_stream;
uint32_t i_timescale;
uint32_t i_chunk_count;
bo_t *trak;
bo_t *tkhd;
bo_t *mdia;
bo_t *mdhd, *hdlr;
bo_t *minf;
bo_t *dinf;
bo_t *dref;
bo_t *url;
bo_t *stbl;
bo_t *stsd;
bo_t *stts;
bo_t *stsz;
p_stream = p_sys->pp_streams[i_trak];
if( p_stream->p_fmt->i_cat != AUDIO_ES && p_stream->p_fmt->i_cat != VIDEO_ES )
{
msg_Err( p_mux, "FIXME ignoring trak (noaudio&&novideo)" );
continue;
}
if( p_stream->p_fmt->i_cat == AUDIO_ES )
{
i_timescale = p_stream->p_fmt->i_sample_rate;
}
else
{
i_timescale = 1001;
}
/* *** add /moov/trak *** */
trak = box_new( "trak" );
/* *** add /moov/trak/tkhd *** */
tkhd = box_full_new( "tkhd", 1, 1 );
bo_add_64be( tkhd, 0 ); // creation time
bo_add_64be( tkhd, 0 ); // modification time
bo_add_32be( tkhd, p_stream->i_track_id );
bo_add_32be( tkhd, 0 ); // reserved 0
bo_add_64be( tkhd, p_stream->i_duration *
(int64_t)i_movie_timescale /
(mtime_t)1000000 ); // duration
for( i = 0; i < 2; i++ )
{
bo_add_32be( tkhd, 0 ); // reserved
}
bo_add_16be( tkhd, 0 ); // layer
bo_add_16be( tkhd, 0 ); // pre-defined
bo_add_16be( tkhd, p_stream->p_fmt->i_cat == AUDIO_ES ? 0x100 : 0 ); // volume
bo_add_16be( tkhd, 0 ); // reserved
for( i = 0; i < 9; i++ )
{
bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
}
if( p_stream->p_fmt->i_cat == AUDIO_ES )
{
bo_add_32be( tkhd, 0 ); // width (presentation)
bo_add_32be( tkhd, 0 ); // height(presentation)
}
else
{
bo_add_32be( tkhd, p_stream->p_fmt->i_width << 16 ); // width (presentation)
bo_add_32be( tkhd, p_stream->p_fmt->i_height << 16 ); // height(presentation)
}
box_fix( tkhd );
box_gather( trak, tkhd );
/* *** add /moov/trak/mdia *** */
mdia = box_new( "mdia" );
/* */
mdhd = box_full_new( "mdhd", 1, 0 );
bo_add_64be( mdhd, 0 ); // creation time
bo_add_64be( mdhd, 0 ); // modification time
bo_add_32be( mdhd, i_timescale); // timescale
bo_add_64be( mdhd, p_stream->i_duration *
(int64_t)i_timescale /
(mtime_t)1000000 ); // duration
bo_add_16be( mdhd, 0 ); // language FIXME
bo_add_16be( mdhd, 0 ); // predefined
box_fix( mdhd );
box_gather( mdia, mdhd );
/* */
hdlr = box_full_new( "hdlr", 0, 0 );
bo_add_32be( hdlr, 0 ); // predefined
if( p_stream->p_fmt->i_cat == AUDIO_ES )
{
bo_add_fourcc( hdlr, "soun" );
}
else
{
bo_add_fourcc( hdlr, "vide" );
}
for( i = 0; i < 3; i++ )
{
bo_add_32be( hdlr, 0 ); // reserved
}
bo_add_mem( hdlr, 2, "?" );
box_fix( hdlr );
box_gather( mdia, hdlr );
/* minf*/
minf = box_new( "minf" );
/* add smhd|vmhd */
if( p_stream->p_fmt->i_cat == AUDIO_ES )
{
bo_t *smhd;
smhd = box_full_new( "smhd", 0, 0 );
bo_add_16be( smhd, 0 ); // balance
bo_add_16be( smhd, 0 ); // reserved
box_fix( smhd );
box_gather( minf, smhd );
}
else if( p_stream->p_fmt->i_cat == VIDEO_ES )
{
bo_t *vmhd;
vmhd = box_full_new( "vmhd", 0, 1 );
bo_add_16be( vmhd, 0 ); // graphicsmode
for( i = 0; i < 3; i++ )
{
bo_add_16be( vmhd, 0 ); // opcolor
}
box_fix( vmhd );
box_gather( minf, vmhd );
}
/* dinf */
dinf = box_new( "dinf" );
dref = box_full_new( "dref", 0, 0 );
bo_add_32be( dref, 1 );
url = box_full_new( "url ", 0, 0x01 );
box_fix( url );
box_gather( dref, url );
box_fix( dref );
box_gather( dinf, dref );
/* append dinf to mdia */
box_fix( dinf );
box_gather( minf, dinf );
/* add stbl */
stbl = box_new( "stbl" );
stsd = box_full_new( "stsd", 0, 0 );
bo_add_32be( stsd, 1 );
if( p_stream->p_fmt->i_cat == AUDIO_ES )
{
bo_t *soun;
char fcc[4] = " ";
int i;
vlc_bool_t b_mpeg4_hdr;
switch( p_stream->p_fmt->i_fourcc )
{
case VLC_FOURCC( 'm', 'p', '4', 'a' ):
case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
memcpy( fcc, "mp4a", 4 );
b_mpeg4_hdr = VLC_TRUE;
break;
default:
memcpy( fcc, (char*)&p_stream->p_fmt->i_fourcc, 4 );
b_mpeg4_hdr = VLC_FALSE;
break;
}
soun = box_new( fcc );
for( i = 0; i < 6; i++ )
{
bo_add_8( soun, 0 ); // reserved;
}
bo_add_16be( soun, 1 ); // data-reference-index
bo_add_32be( soun, 0 ); // reserved;
bo_add_32be( soun, 0 ); // reserved;
bo_add_16be( soun, p_stream->p_fmt->i_channels ); // channel-count
bo_add_16be( soun, 16); // FIXME sample size
bo_add_16be( soun, 0 ); // predefined
bo_add_16be( soun, 0 ); // reserved
bo_add_16be( soun, p_stream->p_fmt->i_sample_rate ); // sampleratehi
bo_add_16be( soun, 0 ); // sampleratelo
/* add an ES Descriptor */
if( b_mpeg4_hdr )
{
bo_t *esds;
esds = GetESDS( p_stream );
box_fix( esds );
box_gather( soun, esds );
}
box_fix( soun );
box_gather( stsd, soun );
}
else if( p_stream->p_fmt->i_cat == VIDEO_ES )
{
bo_t *vide;
char fcc[4] = " ";
int i;
vlc_bool_t b_mpeg4_hdr;
switch( p_stream->p_fmt->i_fourcc )
{
case VLC_FOURCC( 'm', 'p', '4', 'v' ):
case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
memcpy( fcc, "mp4v", 4 );
b_mpeg4_hdr = VLC_TRUE;
break;
default:
memcpy( fcc, (char*)&p_stream->p_fmt->i_fourcc, 4 );
b_mpeg4_hdr = VLC_FALSE;
break;
}
vide = box_new( fcc );
for( i = 0; i < 6; i++ )
{
bo_add_8( vide, 0 ); // reserved;
}
bo_add_16be( vide, 1 ); // data-reference-index
bo_add_16be( vide, 0 ); // predefined;
bo_add_16be( vide, 0 ); // reserved;
for( i = 0; i < 3; i++ )
{
bo_add_32be( vide, 0 ); // predefined;
}
bo_add_16be( vide, p_stream->p_fmt->i_width ); // i_width
bo_add_16be( vide, p_stream->p_fmt->i_height ); // i_height
bo_add_32be( vide, 0x00480000 ); // h 72dpi
bo_add_32be( vide, 0x00480000 ); // v 72dpi
bo_add_32be( vide, 0 ); // reserved
bo_add_16be( vide, 1 ); // predefined
for( i = 0; i < 32; i++ )
{
bo_add_8( vide, 0 ); // compressor name;
}
bo_add_16be( vide, 0x18 ); // depth
bo_add_16be( vide, 0xffff ); // predefined
/* add an ES Descriptor */
if( b_mpeg4_hdr )
{
bo_t *esds;
esds = GetESDS( p_stream );
box_fix( esds );
box_gather( vide, esds );
}
box_fix( vide );
box_gather( stsd, vide );
}
/* append stsd to stbl */
box_fix( stsd );
box_gather( stbl, stsd );
/* we will create chunk table and stsc table FIXME optim stsc table FIXME */
i_chunk_count = 0;
for( i = 0; i < p_stream->i_entry_count; )
{
while( i < p_stream->i_entry_count )
{
if( i + 1 < p_stream->i_entry_count && p_stream->entry[i].i_pos + p_stream->entry[i].i_size != p_stream->entry[i + 1].i_pos )
{
i++;
break;
}
i++;
}
i_chunk_count++;