1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003 VideoLAN
5 * $Id: mp4.c,v 1.8 2004/01/12 20:19:55 gbazin Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc/input.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
47 static int Capability(sout_mux_t *, int, void *, void * );
48 static int AddStream( sout_mux_t *, sout_input_t * );
49 static int DelStream( sout_mux_t *, sout_input_t * );
50 static int Mux ( sout_mux_t * );
52 /*****************************************************************************
54 *****************************************************************************/
56 set_description( _("MP4/MOV muxer") );
57 set_capability( "sout mux", 5 );
58 add_shortcut( "mp4" );
59 add_shortcut( "mov" );
60 set_callbacks( Open, Close );
80 unsigned int i_entry_count;
81 unsigned int i_entry_max;
98 mp4_stream_t **pp_streams;
102 typedef struct bo_t bo_t;
113 static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
114 static void bo_add_8 ( bo_t *, uint8_t );
115 static void bo_add_16be ( bo_t *, uint16_t );
116 static void bo_add_24be ( bo_t *, uint32_t );
117 static void bo_add_32be ( bo_t *, uint32_t );
118 static void bo_add_64be ( bo_t *, uint64_t );
119 static void bo_add_fourcc(bo_t *, char * );
120 static void bo_add_bo ( bo_t *, bo_t * );
121 static void bo_add_mem ( bo_t *, int , uint8_t * );
122 static void bo_add_descr ( bo_t *, uint8_t , uint32_t );
124 static void bo_fix_32be ( bo_t *, int , uint32_t );
126 static bo_t *box_new ( char *fcc );
127 static bo_t *box_full_new( char *fcc, uint8_t v, uint32_t f );
128 static void box_fix ( bo_t *box );
129 static void box_free ( bo_t *box );
130 static void box_gather ( bo_t *box, bo_t *box2 );
132 static void box_send( sout_mux_t *p_mux, bo_t *box );
134 static int64_t get_timestamp();
136 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box );
138 /*****************************************************************************
140 *****************************************************************************/
141 static int Open( vlc_object_t *p_this )
143 sout_mux_t *p_mux = (sout_mux_t*)p_this;
144 sout_mux_sys_t *p_sys;
147 p_sys = malloc( sizeof( sout_mux_sys_t ) );
149 p_sys->i_nb_streams = 0;
150 p_sys->pp_streams = NULL;
151 p_sys->i_mdat_pos = 0;
152 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
153 p_sys->i_start_dts = 0;
155 msg_Info( p_mux, "Open" );
157 p_mux->pf_capacity = Capability;
158 p_mux->pf_addstream = AddStream;
159 p_mux->pf_delstream = DelStream;
161 p_mux->p_sys = p_sys;
165 /* Now add ftyp header */
166 box = box_new( "ftyp" );
167 bo_add_fourcc( box, "isom" );
168 bo_add_32be ( box, 0 );
169 bo_add_fourcc( box, "mp41" );
172 p_sys->i_pos += box->i_buffer;
173 p_sys->i_mdat_pos = p_sys->i_pos;
175 box_send( p_mux, box );
178 * Quicktime actually doesn't like the 64 bits extensions !!! */
179 p_sys->b_mov = VLC_TRUE;
182 /* Now add mdat header */
183 box = box_new( "mdat" );
184 bo_add_64be ( box, 0 ); // enough to store an extended size
186 p_sys->i_pos += box->i_buffer;
188 box_send( p_mux, box );
193 static int GetDescrLength( int i_size )
195 if( i_size < 0x00000080 )
197 else if( i_size < 0x00004000 )
199 else if( i_size < 0x00200000 )
205 static bo_t *GetESDS( mp4_stream_t *p_stream )
209 int i_object_type_indication;
210 int i_decoder_specific_info_size;
212 if( p_stream->p_fmt->i_extra > 0 )
214 i_decoder_specific_info_size =
215 GetDescrLength( p_stream->p_fmt->i_extra );
219 i_decoder_specific_info_size = 0;
222 esds = box_full_new( "esds", 0, 0 );
225 bo_add_descr( esds, 0x03, 3 +
226 GetDescrLength( 13 + i_decoder_specific_info_size ) +
227 GetDescrLength( 1 ) );
228 bo_add_16be( esds, p_stream->i_track_id );
229 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
231 /* DecoderConfigDescr */
232 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
234 switch( p_stream->p_fmt->i_codec )
236 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
237 i_object_type_indication = 0x20;
239 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
240 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
241 i_object_type_indication = 0x60;
243 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
244 /* FIXME for mpeg2-aac == 0x66->0x68 */
245 i_object_type_indication = 0x40;
247 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
248 i_object_type_indication =
249 p_stream->p_fmt->audio.i_rate < 32000 ? 0x69 : 0x6b;
252 i_object_type_indication = 0x00;
255 i_stream_type = p_stream->p_fmt->i_cat == VIDEO_ES ? 0x04 : 0x05;
257 bo_add_8 ( esds, i_object_type_indication );
258 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
259 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
260 bo_add_32be( esds, 0x7fffffff ); // maxBitrate
261 bo_add_32be( esds, 0 ); // avgBitrate
263 if( p_stream->p_fmt->i_extra > 0 )
267 /* DecoderSpecificInfo */
268 bo_add_descr( esds, 0x05, p_stream->p_fmt->i_extra );
270 for( i = 0; i < p_stream->p_fmt->i_extra; i++ )
272 bo_add_8( esds, ((uint8_t*)p_stream->p_fmt->p_extra)[i] );
276 /* SL_Descr mandatory */
277 bo_add_descr( esds, 0x06, 1 );
278 bo_add_8 ( esds, 0x02 ); // sl_predefined
285 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
290 wave = box_new( "wave" );
292 box = box_new( "frma" );
293 bo_add_fourcc( box, "mp4a" );
295 box_gather( wave, box );
297 box = box_new( "mp4a" );
298 bo_add_32be( box, 0 );
300 box_gather( wave, box );
302 box = GetESDS( p_stream );
304 box_gather( wave, box );
306 box = box_new( "srcq" );
307 bo_add_32be( box, 0x40 );
309 box_gather( wave, box );
311 box = box_new( " " );
313 box_gather( wave, box );
320 /* TODO: No idea about these values */
321 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
323 bo_t *smi = box_new( "SMI " );
325 bo_add_fourcc( smi, "SEQH" );
326 bo_add_32be( smi, 0x5 );
327 bo_add_32be( smi, 0xe2c0211d );
328 bo_add_8( smi, 0xc0 );
334 /*****************************************************************************
336 *****************************************************************************/
337 static uint32_t mvhd_matrix[9] =
338 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
340 static void Close( vlc_object_t * p_this )
342 sout_mux_t *p_mux = (sout_mux_t*)p_this;
343 sout_mux_sys_t *p_sys = p_mux->p_sys;
344 sout_buffer_t *p_hdr;
350 uint32_t i_movie_timescale = 90000;
351 int64_t i_movie_duration = 0;
352 msg_Info( p_mux, "Close" );
354 /* create general info */
355 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
357 mp4_stream_t *p_stream;
359 p_stream = p_sys->pp_streams[i_trak];
361 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
363 msg_Dbg( p_mux, "movie duration %ds",
364 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
366 i_movie_duration = i_movie_duration *
367 (int64_t)i_movie_timescale / (int64_t)1000000;
369 /* *** update mdat size *** */
370 bo_init ( &bo, 0, NULL, VLC_TRUE );
371 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
374 bo_add_32be ( &bo, 1 );
375 bo_add_fourcc( &bo, "mdat" );
376 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
380 bo_add_32be ( &bo, 8 );
381 bo_add_fourcc( &bo, "wide" );
382 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
383 bo_add_fourcc( &bo, "mdat" );
386 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
390 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
391 sout_AccessOutWrite( p_mux->p_access, p_hdr );
393 /* Now create header */
394 sout_AccessOutSeek( p_mux->p_access, p_sys->i_pos );
396 moov = box_new( "moov" );
398 /* *** add /moov/mvhd *** */
401 mvhd = box_full_new( "mvhd", 0, 0 );
402 bo_add_32be( mvhd, get_timestamp() ); // creation time
403 bo_add_32be( mvhd, get_timestamp() ); // modification time
404 bo_add_32be( mvhd, i_movie_timescale); // timescale
405 bo_add_32be( mvhd, i_movie_duration ); // duration
409 mvhd = box_full_new( "mvhd", 1, 0 );
410 bo_add_64be( mvhd, get_timestamp() ); // creation time
411 bo_add_64be( mvhd, get_timestamp() ); // modification time
412 bo_add_32be( mvhd, i_movie_timescale); // timescale
413 bo_add_64be( mvhd, i_movie_duration ); // duration
415 bo_add_32be( mvhd, 0x10000 ); // rate
416 bo_add_16be( mvhd, 0x100 ); // volume
417 bo_add_16be( mvhd, 0 ); // reserved
418 for( i = 0; i < 2; i++ )
420 bo_add_32be( mvhd, 0 ); // reserved
422 for( i = 0; i < 9; i++ )
424 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
426 for( i = 0; i < 6; i++ )
428 bo_add_32be( mvhd, 0 ); // pre-defined
431 /* Find the 1st track id */
432 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
434 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
436 if( p_stream->p_fmt->i_cat == AUDIO_ES ||
437 p_stream->p_fmt->i_cat == VIDEO_ES )
440 bo_add_32be( mvhd, p_stream->i_track_id ); // next-track-id
444 if( i_trak == p_sys->i_nb_streams ) /* Just for sanity reasons */
445 bo_add_32be( mvhd, 0xffffffff );
448 box_gather( moov, mvhd );
450 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
452 mp4_stream_t *p_stream;
453 uint32_t i_timescale;
454 uint32_t i_chunk_count;
469 p_stream = p_sys->pp_streams[i_trak];
471 if( p_stream->p_fmt->i_cat != AUDIO_ES &&
472 p_stream->p_fmt->i_cat != VIDEO_ES )
474 msg_Err( p_mux, "FIXME ignoring trak (noaudio&&novideo)" );
477 if( p_stream->p_fmt->i_cat == AUDIO_ES )
479 i_timescale = p_stream->p_fmt->audio.i_rate;
486 /* *** add /moov/trak *** */
487 trak = box_new( "trak" );
489 /* *** add /moov/trak/tkhd *** */
492 tkhd = box_full_new( "tkhd", 0, 1 );
493 bo_add_32be( tkhd, get_timestamp() ); // creation time
494 bo_add_32be( tkhd, get_timestamp() ); // modification time
495 bo_add_32be( tkhd, p_stream->i_track_id );
496 bo_add_32be( tkhd, 0 ); // reserved 0
497 bo_add_32be( tkhd, p_stream->i_duration *
498 (int64_t)i_movie_timescale /
499 (mtime_t)1000000 ); // duration
503 tkhd = box_full_new( "tkhd", 1, 1 );
504 bo_add_64be( tkhd, get_timestamp() ); // creation time
505 bo_add_64be( tkhd, get_timestamp() ); // modification time
506 bo_add_32be( tkhd, p_stream->i_track_id );
507 bo_add_32be( tkhd, 0 ); // reserved 0
508 bo_add_64be( tkhd, p_stream->i_duration *
509 (int64_t)i_movie_timescale /
510 (mtime_t)1000000 ); // duration
513 for( i = 0; i < 2; i++ )
515 bo_add_32be( tkhd, 0 ); // reserved
517 bo_add_16be( tkhd, 0 ); // layer
518 bo_add_16be( tkhd, 0 ); // pre-defined
519 bo_add_16be( tkhd, p_stream->p_fmt->i_cat == AUDIO_ES ? 0x100 : 0 ); // volume
520 bo_add_16be( tkhd, 0 ); // reserved
521 for( i = 0; i < 9; i++ )
523 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
525 if( p_stream->p_fmt->i_cat == AUDIO_ES )
527 bo_add_32be( tkhd, 0 ); // width (presentation)
528 bo_add_32be( tkhd, 0 ); // height(presentation)
532 bo_add_32be( tkhd, p_stream->p_fmt->video.i_width << 16 ); // width (presentation)
533 bo_add_32be( tkhd, p_stream->p_fmt->video.i_height << 16 ); // height(presentation)
536 box_gather( trak, tkhd );
538 /* *** add /moov/trak/mdia *** */
539 mdia = box_new( "mdia" );
544 mdhd = box_full_new( "mdhd", 0, 0 );
545 bo_add_32be( mdhd, get_timestamp() ); // creation time
546 bo_add_32be( mdhd, get_timestamp() ); // modification time
547 bo_add_32be( mdhd, i_timescale); // timescale
548 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
549 (mtime_t)1000000 ); // duration
553 mdhd = box_full_new( "mdhd", 1, 0 );
554 bo_add_64be( mdhd, get_timestamp() ); // creation time
555 bo_add_64be( mdhd, get_timestamp() ); // modification time
556 bo_add_32be( mdhd, i_timescale); // timescale
557 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
558 (mtime_t)1000000 ); // duration
561 bo_add_16be( mdhd, 0 ); // language FIXME
562 bo_add_16be( mdhd, 0 ); // predefined
564 box_gather( mdia, mdhd );
566 /* handler reference */
567 hdlr = box_full_new( "hdlr", 0, 0 );
569 bo_add_fourcc( hdlr, "mhlr" ); // media handler
570 if( p_stream->p_fmt->i_cat == AUDIO_ES )
572 bo_add_fourcc( hdlr, "soun" );
576 bo_add_fourcc( hdlr, "vide" );
579 bo_add_32be( hdlr, 0 ); // reserved
580 bo_add_32be( hdlr, 0 ); // reserved
581 bo_add_32be( hdlr, 0 ); // reserved
583 if( p_stream->p_fmt->i_cat == AUDIO_ES )
585 bo_add_mem( hdlr, 13, "SoundHandler" );
589 bo_add_mem( hdlr, 13, "VideoHandler" );
593 box_gather( mdia, hdlr );
596 minf = box_new( "minf" );
599 if( p_stream->p_fmt->i_cat == AUDIO_ES )
603 smhd = box_full_new( "smhd", 0, 0 );
604 bo_add_16be( smhd, 0 ); // balance
605 bo_add_16be( smhd, 0 ); // reserved
608 box_gather( minf, smhd );
610 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
614 vmhd = box_full_new( "vmhd", 0, 1 );
615 bo_add_16be( vmhd, 0 ); // graphicsmode
616 for( i = 0; i < 3; i++ )
618 bo_add_16be( vmhd, 0 ); // opcolor
622 box_gather( minf, vmhd );
626 dinf = box_new( "dinf" );
627 dref = box_full_new( "dref", 0, 0 );
628 bo_add_32be( dref, 1 );
629 url = box_full_new( "url ", 0, 0x01 );
631 box_gather( dref, url );
633 box_gather( dinf, dref );
635 /* append dinf to mdia */
637 box_gather( minf, dinf );
640 stbl = box_new( "stbl" );
641 stsd = box_full_new( "stsd", 0, 0 );
642 bo_add_32be( stsd, 1 );
644 if( p_stream->p_fmt->i_cat == AUDIO_ES )
649 vlc_bool_t b_mpeg4_hdr;
651 switch( p_stream->p_fmt->i_codec )
653 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
654 memcpy( fcc, "mp4a", 4 );
655 b_mpeg4_hdr = VLC_TRUE;
658 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
660 memcpy( fcc, ".mp3 ", 4 );
662 memcpy( fcc, "mp4a", 4 );
663 b_mpeg4_hdr = VLC_FALSE;
667 memcpy( fcc, (char*)&p_stream->p_fmt->i_codec, 4 );
668 b_mpeg4_hdr = VLC_FALSE;
672 soun = box_new( fcc );
673 for( i = 0; i < 6; i++ )
675 bo_add_8( soun, 0 ); // reserved;
677 bo_add_16be( soun, 1 ); // data-reference-index
679 /* SoundDescription */
681 p_stream->p_fmt->i_codec == VLC_FOURCC( 'm', 'p', '4', 'a' ) )
683 bo_add_16be( soun, 1 ); // version 1;
687 bo_add_16be( soun, 0 ); // version 0;
689 bo_add_16be( soun, 0 ); // revision level (0)
690 bo_add_32be( soun, 0 ); // vendor
691 bo_add_16be( soun, p_stream->p_fmt->audio.i_channels ); // channel-count
692 bo_add_16be( soun, 16); // FIXME sample size
693 bo_add_16be( soun, -2 ); // compression id
694 bo_add_16be( soun, 0 ); // packet size (0)
695 bo_add_16be( soun, p_stream->p_fmt->audio.i_rate ); // sampleratehi
696 bo_add_16be( soun, 0 ); // sampleratelo
698 /* Extended data for SoundDescription V1 */
700 p_stream->p_fmt->i_codec == VLC_FOURCC( 'm', 'p', '4', 'a' ) )
702 bo_add_32be( soun, p_stream->p_fmt->audio.i_frame_length ); /* samples per packet */
703 bo_add_32be( soun, 1536 ); /* bytes per packet */
704 bo_add_32be( soun, 2 ); /* bytes per frame */
705 /* bytes per sample */
706 bo_add_32be( soun, 2 /*p_stream->p_fmt->audio.i_bitspersample/8 */);
709 /* add an ES Descriptor */
715 p_stream->p_fmt->i_codec == VLC_FOURCC('m','p','4','a') )
717 box = GetWaveTag( p_stream );
721 box = GetESDS( p_stream );
724 box_gather( soun, box );
728 box_gather( stsd, soun );
730 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
736 switch( p_stream->p_fmt->i_codec )
738 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
739 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
740 memcpy( fcc, "mp4v", 4 );
743 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
744 memcpy( fcc, "mjpa", 4 );
747 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
748 memcpy( fcc, "SVQ3", 4 );
752 memcpy( fcc, (char*)&p_stream->p_fmt->i_codec, 4 );
756 vide = box_new( fcc );
757 for( i = 0; i < 6; i++ )
759 bo_add_8( vide, 0 ); // reserved;
761 bo_add_16be( vide, 1 ); // data-reference-index
763 bo_add_16be( vide, 0 ); // predefined;
764 bo_add_16be( vide, 0 ); // reserved;
765 for( i = 0; i < 3; i++ )
767 bo_add_32be( vide, 0 ); // predefined;
770 bo_add_16be( vide, p_stream->p_fmt->video.i_width ); // i_width
771 bo_add_16be( vide, p_stream->p_fmt->video.i_height ); // i_height
773 bo_add_32be( vide, 0x00480000 ); // h 72dpi
774 bo_add_32be( vide, 0x00480000 ); // v 72dpi
776 bo_add_32be( vide, 0 ); // data size, always 0
777 bo_add_16be( vide, 1 ); // frames count per sample
780 for( i = 0; i < 32; i++ )
785 bo_add_16be( vide, 0x18 ); // depth
786 bo_add_16be( vide, 0xffff ); // predefined
788 /* add an ES Descriptor */
789 switch( p_stream->p_fmt->i_codec )
791 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
792 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
794 bo_t *esds = GetESDS( p_stream );
797 box_gather( vide, esds );
801 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
803 bo_t *esds = GetSVQ3Tag( p_stream );
806 box_gather( vide, esds );
815 box_gather( stsd, vide );
818 /* append stsd to stbl */
820 box_gather( stbl, stsd );
822 /* we will create chunk table and stsc table FIXME optim stsc table FIXME */
824 for( i = 0; i < p_stream->i_entry_count; )
826 while( i < p_stream->i_entry_count )
828 if( i + 1 < p_stream->i_entry_count &&
829 p_stream->entry[i].i_pos +
830 p_stream->entry[i].i_size != p_stream->entry[i + 1].i_pos )
841 /* chunk offset table */
842 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
844 /* 64 bits version */
848 unsigned int i_chunk;
850 msg_Dbg( p_mux, "creating %d chunk (co64)", i_chunk_count );
852 co64 = box_full_new( "co64", 0, 0 );
853 bo_add_32be( co64, i_chunk_count );
855 stsc = box_full_new( "stsc", 0, 0 );
856 bo_add_32be( stsc, i_chunk_count ); // entry-count
857 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
860 bo_add_64be( co64, p_stream->entry[i].i_pos );
864 while( i < p_stream->i_entry_count )
866 if( i + 1 < p_stream->i_entry_count &&
867 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
868 != p_stream->entry[i + 1].i_pos )
876 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
877 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
878 bo_add_32be( stsc, 1 ); // sample-descr-index
880 /* append co64 to stbl */
882 box_gather( stbl, co64 );
884 /* append stsc to stbl */
886 box_gather( stbl, stsc );
890 /* 32 bits version */
894 unsigned int i_chunk;
896 msg_Dbg( p_mux, "creating %d chunk (stco)", i_chunk_count );
898 stco = box_full_new( "stco", 0, 0 );
899 bo_add_32be( stco, i_chunk_count );
901 stsc = box_full_new( "stsc", 0, 0 );
902 bo_add_32be( stsc, i_chunk_count ); // entry-count
903 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
906 bo_add_32be( stco, p_stream->entry[i].i_pos );
910 while( i < p_stream->i_entry_count )
912 if( i + 1 < p_stream->i_entry_count &&
913 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
914 != p_stream->entry[i + 1].i_pos )
922 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
923 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
924 bo_add_32be( stsc, 1 ); // sample-descr-index
926 /* append stco to stbl */
928 box_gather( stbl, stco );
930 /* append stsc to stbl */
932 box_gather( stbl, stsc );
937 stts = box_full_new( "stts", 0, 0 );
938 bo_add_32be( stts, 0 ); // fixed latter
939 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
945 i_delta = p_stream->entry[i].i_length;
947 while( i < p_stream->i_entry_count )
949 if( i + 1 < p_stream->i_entry_count &&
950 p_stream->entry[i + 1].i_length != i_delta )
959 bo_add_32be( stts, i - i_first ); // sample-count
960 bo_add_32be( stts, i_delta * (int64_t)i_timescale /
961 (int64_t)1000000 ); // sample-delta
963 bo_fix_32be( stts, 12, i_index );
965 /* append stts to stbl */
967 box_gather( stbl, stts );
969 /* FIXME add ctts ?? FIXME */
971 stsz = box_full_new( "stsz", 0, 0 );
972 bo_add_32be( stsz, 0 ); // sample-size
973 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
974 for( i = 0; i < p_stream->i_entry_count; i++ )
976 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
978 /* append stsz to stbl */
980 box_gather( stbl, stsz );
982 /* append stbl to minf */
984 box_gather( minf, stbl );
986 /* append minf to mdia */
988 box_gather( mdia, minf );
990 /* append mdia to trak */
992 box_gather( trak, mdia );
994 /* append trak to moov */
996 box_gather( moov, trak );
1000 box_send( p_mux, moov );
1002 /* *** release memory *** */
1003 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1005 mp4_stream_t *p_stream;
1007 p_stream = p_sys->pp_streams[i_trak];
1009 if( p_stream->p_fmt->p_extra )
1011 free( p_stream->p_fmt->p_extra );
1013 free( p_stream->p_fmt );
1014 free( p_stream->entry );
1020 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
1025 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
1026 *(vlc_bool_t*)p_answer = VLC_FALSE;
1027 return( SOUT_MUX_CAP_ERR_OK );
1029 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
1033 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
1035 sout_mux_sys_t *p_sys = p_mux->p_sys;
1036 mp4_stream_t *p_stream;
1038 switch( p_input->p_fmt->i_codec )
1040 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1041 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1042 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1043 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
1044 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
1045 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
1046 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1049 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
1050 (char*)&p_input->p_fmt->i_codec );
1051 return VLC_EGENERIC;
1054 p_stream = malloc( sizeof( mp4_stream_t ) );
1055 p_stream->p_fmt = malloc( sizeof( es_format_t ) );
1056 memcpy( p_stream->p_fmt, p_input->p_fmt, sizeof( es_format_t ) );
1057 if( p_stream->p_fmt->i_extra )
1059 p_stream->p_fmt->p_extra =
1060 malloc( p_stream->p_fmt->i_extra );
1061 memcpy( p_stream->p_fmt->p_extra,
1062 p_input->p_fmt->p_extra,
1063 p_input->p_fmt->i_extra );
1065 p_stream->i_track_id = p_sys->i_nb_streams + 1;
1066 p_stream->i_entry_count = 0;
1067 p_stream->i_entry_max = 1000;
1069 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
1070 p_stream->i_duration = 0;
1072 p_input->p_sys = p_stream;
1074 msg_Dbg( p_mux, "adding input" );
1076 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
1077 return( VLC_SUCCESS );
1080 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
1082 msg_Dbg( p_mux, "removing input" );
1086 /****************************************************************************/
1088 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
1094 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
1096 sout_fifo_t *p_fifo;
1098 p_fifo = p_mux->pp_inputs[i]->p_fifo;
1100 if( p_fifo->i_depth > 1 )
1102 sout_buffer_t *p_buf;
1104 p_buf = sout_FifoShow( p_fifo );
1105 if( i_stream < 0 || p_buf->i_dts < i_dts )
1107 i_dts = p_buf->i_dts;
1113 return( -1 ); // wait that all fifo have at least 2 packets
1118 *pi_stream = i_stream;
1127 static int Mux( sout_mux_t *p_mux )
1129 sout_mux_sys_t *p_sys = p_mux->p_sys;
1133 sout_input_t *p_input;
1135 mp4_stream_t *p_stream;
1136 sout_buffer_t *p_data;
1139 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
1141 return( VLC_SUCCESS );
1144 if( !p_sys->i_start_dts )
1145 p_sys->i_start_dts = i_dts;
1147 p_input = p_mux->pp_inputs[i_stream];
1148 p_stream = (mp4_stream_t*)p_input->p_sys;
1150 p_data = sout_FifoGet( p_input->p_fifo );
1151 //msg_Dbg( p_mux, "stream=%d size=%6d pos=%8lld", i_stream, p_data->i_size, p_sys->i_pos );
1153 /* add index entry */
1154 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
1155 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_size;
1156 p_stream->entry[p_stream->i_entry_count].i_pts = p_data->i_pts;
1157 p_stream->entry[p_stream->i_entry_count].i_dts = p_data->i_dts;
1158 p_stream->entry[p_stream->i_entry_count].i_length=
1159 __MAX( p_data->i_length, 0 );
1161 if( p_stream->i_entry_count == 0 )
1163 /* Here is another bad hack.
1164 * To make sure audio/video are in sync, we report a corrected
1165 * length for the 1st sample. */
1166 p_stream->entry[p_stream->i_entry_count].i_length =
1167 __MAX( p_data->i_length, 0 ) +
1168 p_data->i_pts - p_sys->i_start_dts;
1171 p_stream->i_entry_count++;
1172 if( p_stream->i_entry_count >= p_stream->i_entry_max )
1174 p_stream->i_entry_max += 1000;
1176 realloc( p_stream->entry,
1177 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
1181 p_stream->i_duration += __MAX( p_data->i_length, 0 );
1182 p_sys->i_pos += p_data->i_size;
1185 sout_AccessOutWrite( p_mux->p_access, p_data );
1188 return( VLC_SUCCESS );
1192 /****************************************************************************/
1194 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1199 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1200 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1204 p_bo->i_buffer_size = i_size;
1205 p_bo->p_buffer = p_buffer;
1208 p_bo->b_grow = b_grow;
1212 static void bo_add_8( bo_t *p_bo, uint8_t i )
1214 if( p_bo->i_buffer < p_bo->i_buffer_size )
1216 p_bo->p_buffer[p_bo->i_buffer] = i;
1218 else if( p_bo->b_grow )
1220 p_bo->i_buffer_size += 1024;
1221 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1223 p_bo->p_buffer[p_bo->i_buffer] = i;
1229 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1231 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1232 bo_add_8( p_bo, i &0xff );
1235 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1237 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1238 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1239 bo_add_8( p_bo, ( i &0xff ) );
1241 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1243 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1244 bo_add_16be( p_bo, i &0xffff );
1247 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1249 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1250 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1251 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1252 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1255 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1257 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1258 bo_add_32be( p_bo, i &0xffffffff );
1261 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1263 bo_add_8( p_bo, fcc[0] );
1264 bo_add_8( p_bo, fcc[1] );
1265 bo_add_8( p_bo, fcc[2] );
1266 bo_add_8( p_bo, fcc[3] );
1269 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1273 for( i = 0; i < i_size; i++ )
1275 bo_add_8( p_bo, p_mem[i] );
1279 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
1285 vals[3] = (unsigned char)(i_length & 0x7f);
1287 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
1289 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
1291 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
1293 bo_add_8( p_bo, tag );
1295 if( i_size < 0x00000080 )
1297 bo_add_8( p_bo, vals[3] );
1299 else if( i_size < 0x00004000 )
1301 bo_add_8( p_bo, vals[2] );
1302 bo_add_8( p_bo, vals[3] );
1304 else if( i_size < 0x00200000 )
1306 bo_add_8( p_bo, vals[1] );
1307 bo_add_8( p_bo, vals[2] );
1308 bo_add_8( p_bo, vals[3] );
1310 else if( i_size < 0x10000000 )
1312 bo_add_8( p_bo, vals[0] );
1313 bo_add_8( p_bo, vals[1] );
1314 bo_add_8( p_bo, vals[2] );
1315 bo_add_8( p_bo, vals[3] );
1319 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1323 for( i = 0; i < p_bo2->i_buffer; i++ )
1325 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1329 static bo_t * box_new( char *fcc )
1333 if( ( box = malloc( sizeof( bo_t ) ) ) )
1335 bo_init( box, 0, NULL, VLC_TRUE );
1337 bo_add_32be ( box, 0 );
1338 bo_add_fourcc( box, fcc );
1344 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
1348 if( ( box = malloc( sizeof( bo_t ) ) ) )
1350 bo_init( box, 0, NULL, VLC_TRUE );
1352 bo_add_32be ( box, 0 );
1353 bo_add_fourcc( box, fcc );
1354 bo_add_8 ( box, v );
1355 bo_add_24be ( box, f );
1361 static void box_fix( bo_t *box )
1365 memcpy( &box_tmp, box, sizeof( bo_t ) );
1367 box_tmp.i_buffer = 0;
1368 bo_add_32be( &box_tmp, box->i_buffer );
1371 static void box_free( bo_t *box )
1375 free( box->p_buffer );
1381 static void box_gather ( bo_t *box, bo_t *box2 )
1383 bo_add_bo( box, box2 );
1388 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
1390 sout_buffer_t *p_buf;
1392 p_buf = sout_BufferNew( p_sout, box->i_buffer );
1393 if( box->i_buffer > 0 )
1395 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
1398 p_buf->i_size = box->i_buffer;
1403 static void box_send( sout_mux_t *p_mux, bo_t *box )
1405 sout_buffer_t *p_buf;
1407 p_buf = bo_to_sout( p_mux->p_sout, box );
1410 sout_AccessOutWrite( p_mux->p_access, p_buf );
1413 static int64_t get_timestamp()
1415 int64_t i_timestamp = 0;
1418 i_timestamp = time(NULL);
1419 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
1420 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60