1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc/input.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
48 static int Capability(sout_mux_t *, int, void *, void * );
49 static int AddStream( sout_mux_t *, sout_input_t * );
50 static int DelStream( sout_mux_t *, sout_input_t * );
51 static int Mux ( sout_mux_t * );
53 /*****************************************************************************
55 *****************************************************************************/
56 #define FASTSTART_TEXT N_("Create \"Fast start\" files")
57 #define FASTSTART_LONGTEXT N_( \
58 "When this option is turned on, \"Fast start\" files will be created. " \
59 "(\"Fast start\" files are optimized for download, allowing the user " \
60 "to start previewing the file while it is downloading).")
63 set_description( _("MP4/MOV muxer") );
65 add_bool( "mp4-faststart", 1, NULL, FASTSTART_TEXT, FASTSTART_LONGTEXT,
68 set_capability( "sout mux", 5 );
69 add_shortcut( "mp4" );
70 add_shortcut( "mov" );
71 set_callbacks( Open, Close );
74 /*****************************************************************************
76 *****************************************************************************/
96 unsigned int i_entry_count;
97 unsigned int i_entry_max;
103 /* for later stco fix-up (fast start files) */
109 struct sout_mux_sys_t
113 vlc_bool_t b_fast_start;
121 mp4_stream_t **pp_streams;
134 static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
135 static void bo_add_8 ( bo_t *, uint8_t );
136 static void bo_add_16be ( bo_t *, uint16_t );
137 static void bo_add_24be ( bo_t *, uint32_t );
138 static void bo_add_32be ( bo_t *, uint32_t );
139 static void bo_add_64be ( bo_t *, uint64_t );
140 static void bo_add_fourcc(bo_t *, char * );
141 static void bo_add_bo ( bo_t *, bo_t * );
142 static void bo_add_mem ( bo_t *, int , uint8_t * );
143 static void bo_add_descr( bo_t *, uint8_t , uint32_t );
145 static void bo_fix_32be ( bo_t *, int , uint32_t );
147 static bo_t *box_new ( char *fcc );
148 static bo_t *box_full_new( char *fcc, uint8_t v, uint32_t f );
149 static void box_fix ( bo_t *box );
150 static void box_free ( bo_t *box );
151 static void box_gather ( bo_t *box, bo_t *box2 );
153 static void box_send( sout_mux_t *p_mux, bo_t *box );
155 static int64_t get_timestamp();
157 static sout_buffer_t *bo_to_sout( sout_instance_t *p_sout, bo_t *box );
159 /*****************************************************************************
161 *****************************************************************************/
162 static int Open( vlc_object_t *p_this )
164 sout_mux_t *p_mux = (sout_mux_t*)p_this;
165 sout_mux_sys_t *p_sys;
168 p_sys = malloc( sizeof( sout_mux_sys_t ) );
170 p_sys->i_nb_streams = 0;
171 p_sys->pp_streams = NULL;
172 p_sys->i_mdat_pos = 0;
173 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
174 p_sys->i_start_dts = 0;
176 msg_Dbg( p_mux, "Open" );
178 p_mux->pf_capacity = Capability;
179 p_mux->pf_addstream = AddStream;
180 p_mux->pf_delstream = DelStream;
182 p_mux->p_sys = p_sys;
186 /* Now add ftyp header */
187 box = box_new( "ftyp" );
188 bo_add_fourcc( box, "isom" );
189 bo_add_32be ( box, 0 );
190 bo_add_fourcc( box, "mp41" );
193 p_sys->i_pos += box->i_buffer;
194 p_sys->i_mdat_pos = p_sys->i_pos;
196 box_send( p_mux, box );
200 * Quicktime actually doesn't like the 64 bits extensions !!! */
201 p_sys->b_64_ext = VLC_FALSE;
203 /* Now add mdat header */
204 box = box_new( "mdat" );
205 bo_add_64be ( box, 0 ); // enough to store an extended size
207 p_sys->i_pos += box->i_buffer;
209 box_send( p_mux, box );
214 static int GetDescrLength( int i_size )
216 if( i_size < 0x00000080 )
218 else if( i_size < 0x00004000 )
220 else if( i_size < 0x00200000 )
226 static bo_t *GetESDS( mp4_stream_t *p_stream )
230 int i_object_type_indication;
231 int i_decoder_specific_info_size;
233 if( p_stream->p_fmt->i_extra > 0 )
235 i_decoder_specific_info_size =
236 GetDescrLength( p_stream->p_fmt->i_extra );
240 i_decoder_specific_info_size = 0;
243 esds = box_full_new( "esds", 0, 0 );
246 bo_add_descr( esds, 0x03, 3 +
247 GetDescrLength( 13 + i_decoder_specific_info_size ) +
248 GetDescrLength( 1 ) );
249 bo_add_16be( esds, p_stream->i_track_id );
250 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
252 /* DecoderConfigDescr */
253 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
255 switch( p_stream->p_fmt->i_codec )
257 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
258 i_object_type_indication = 0x20;
260 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
261 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
262 i_object_type_indication = 0x60;
264 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
265 /* FIXME for mpeg2-aac == 0x66->0x68 */
266 i_object_type_indication = 0x40;
268 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
269 i_object_type_indication =
270 p_stream->p_fmt->audio.i_rate < 32000 ? 0x69 : 0x6b;
273 i_object_type_indication = 0x00;
276 i_stream_type = p_stream->p_fmt->i_cat == VIDEO_ES ? 0x04 : 0x05;
278 bo_add_8 ( esds, i_object_type_indication );
279 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
280 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
281 bo_add_32be( esds, 0x7fffffff ); // maxBitrate
282 bo_add_32be( esds, 0 ); // avgBitrate
284 if( p_stream->p_fmt->i_extra > 0 )
288 /* DecoderSpecificInfo */
289 bo_add_descr( esds, 0x05, p_stream->p_fmt->i_extra );
291 for( i = 0; i < p_stream->p_fmt->i_extra; i++ )
293 bo_add_8( esds, ((uint8_t*)p_stream->p_fmt->p_extra)[i] );
297 /* SL_Descr mandatory */
298 bo_add_descr( esds, 0x06, 1 );
299 bo_add_8 ( esds, 0x02 ); // sl_predefined
306 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
311 wave = box_new( "wave" );
313 box = box_new( "frma" );
314 bo_add_fourcc( box, "mp4a" );
316 box_gather( wave, box );
318 box = box_new( "mp4a" );
319 bo_add_32be( box, 0 );
321 box_gather( wave, box );
323 box = GetESDS( p_stream );
325 box_gather( wave, box );
327 box = box_new( "srcq" );
328 bo_add_32be( box, 0x40 );
330 box_gather( wave, box );
333 bo_add_32be( wave, 8 ); /* new empty box */
334 bo_add_32be( wave, 0 ); /* box label */
341 /* TODO: No idea about these values */
342 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
344 bo_t *smi = box_new( "SMI " );
346 if( p_stream->p_fmt->i_extra > 0x4e )
348 uint8_t *p_end = &((uint8_t*)p_stream->p_fmt->p_extra)[p_stream->p_fmt->i_extra];
349 uint8_t *p = &((uint8_t*)p_stream->p_fmt->p_extra)[0x46];
351 while( p + 8 < p_end )
353 int i_size = GetDWBE( p );
356 /* FIXME handle 1 as long size */
359 if( !strncmp( &p[4], "SMI ", 4 ) )
361 bo_add_mem( smi, p_end - p - 8, &p[8] );
368 /* Create a dummy one in fallback */
369 bo_add_fourcc( smi, "SEQH" );
370 bo_add_32be( smi, 0x5 );
371 bo_add_32be( smi, 0xe2c0211d );
372 bo_add_8( smi, 0xc0 );
378 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
380 sout_mux_sys_t *p_sys = p_mux->p_sys;
381 bo_t *udta = box_new( "udta" );
385 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
387 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
389 if( p_stream->p_fmt->i_codec == VLC_FOURCC('m','p','4','v') ||
390 p_stream->p_fmt->i_codec == VLC_FOURCC('m','p','4','a') )
392 bo_t *box = box_new( "\251req" );
394 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
395 bo_add_16be( box, 0 );
396 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
397 "QuickTime 6.0 or greater" );
399 box_gather( udta, box );
406 bo_t *box = box_new( "\251enc" );
408 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
409 bo_add_16be( box, 0 );
410 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
411 PACKAGE_STRING " stream output" );
413 box_gather( udta, box );
420 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
422 sout_mux_sys_t *p_sys = p_mux->p_sys;
423 vlc_bool_t b_descr = VLC_FALSE;
428 switch( p_stream->p_fmt->i_codec )
430 case VLC_FOURCC('m','p','4','a'):
431 memcpy( fcc, "mp4a", 4 );
435 case VLC_FOURCC('m','p','g','a'):
437 memcpy( fcc, ".mp3", 4 );
440 memcpy( fcc, "mp4a", 4 );
446 memcpy( fcc, (char*)&p_stream->p_fmt->i_codec, 4 );
450 soun = box_new( fcc );
451 for( i = 0; i < 6; i++ )
453 bo_add_8( soun, 0 ); // reserved;
455 bo_add_16be( soun, 1 ); // data-reference-index
457 /* SoundDescription */
459 p_stream->p_fmt->i_codec == VLC_FOURCC('m','p','4','a') )
461 bo_add_16be( soun, 1 ); // version 1;
465 bo_add_16be( soun, 0 ); // version 0;
467 bo_add_16be( soun, 0 ); // revision level (0)
468 bo_add_32be( soun, 0 ); // vendor
470 bo_add_16be( soun, p_stream->p_fmt->audio.i_channels );
472 bo_add_16be( soun, p_stream->p_fmt->audio.i_bitspersample ?
473 p_stream->p_fmt->audio.i_bitspersample : 16 );
474 bo_add_16be( soun, -2 ); // compression id
475 bo_add_16be( soun, 0 ); // packet size (0)
476 bo_add_16be( soun, p_stream->p_fmt->audio.i_rate ); // sampleratehi
477 bo_add_16be( soun, 0 ); // sampleratelo
479 /* Extended data for SoundDescription V1 */
481 p_stream->p_fmt->i_codec == VLC_FOURCC('m','p','4','a') )
483 /* samples per packet */
484 bo_add_32be( soun, p_stream->p_fmt->audio.i_frame_length );
485 bo_add_32be( soun, 1536 ); /* bytes per packet */
486 bo_add_32be( soun, 2 ); /* bytes per frame */
487 /* bytes per sample */
488 bo_add_32be( soun, 2 /*p_stream->p_fmt->audio.i_bitspersample/8 */);
491 /* Add an ES Descriptor */
497 p_stream->p_fmt->i_codec == VLC_FOURCC('m','p','4','a') )
499 box = GetWaveTag( p_stream );
503 box = GetESDS( p_stream );
506 box_gather( soun, box );
514 static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
521 switch( p_stream->p_fmt->i_codec )
523 case VLC_FOURCC('m','p','4','v'):
524 case VLC_FOURCC('m','p','g','v'):
525 memcpy( fcc, "mp4v", 4 );
528 case VLC_FOURCC('M','J','P','G'):
529 memcpy( fcc, "mjpa", 4 );
532 case VLC_FOURCC('S','V','Q','3'):
533 memcpy( fcc, "SVQ3", 4 );
537 memcpy( fcc, (char*)&p_stream->p_fmt->i_codec, 4 );
541 vide = box_new( fcc );
542 for( i = 0; i < 6; i++ )
544 bo_add_8( vide, 0 ); // reserved;
546 bo_add_16be( vide, 1 ); // data-reference-index
548 bo_add_16be( vide, 0 ); // predefined;
549 bo_add_16be( vide, 0 ); // reserved;
550 for( i = 0; i < 3; i++ )
552 bo_add_32be( vide, 0 ); // predefined;
555 bo_add_16be( vide, p_stream->p_fmt->video.i_width ); // i_width
556 bo_add_16be( vide, p_stream->p_fmt->video.i_height ); // i_height
558 bo_add_32be( vide, 0x00480000 ); // h 72dpi
559 bo_add_32be( vide, 0x00480000 ); // v 72dpi
561 bo_add_32be( vide, 0 ); // data size, always 0
562 bo_add_16be( vide, 1 ); // frames count per sample
565 for( i = 0; i < 32; i++ )
570 bo_add_16be( vide, 0x18 ); // depth
571 bo_add_16be( vide, 0xffff ); // predefined
573 /* add an ES Descriptor */
574 switch( p_stream->p_fmt->i_codec )
576 case VLC_FOURCC('m','p','4','v'):
577 case VLC_FOURCC('m','p','g','v'):
579 bo_t *esds = GetESDS( p_stream );
582 box_gather( vide, esds );
586 case VLC_FOURCC('S','V','Q','3'):
588 bo_t *esds = GetSVQ3Tag( p_stream );
591 box_gather( vide, esds );
604 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
606 sout_mux_sys_t *p_sys = p_mux->p_sys;
607 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
608 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz;
609 uint32_t i_timescale;
612 stbl = box_new( "stbl" );
613 stsd = box_full_new( "stsd", 0, 0 );
614 bo_add_32be( stsd, 1 );
616 if( p_stream->p_fmt->i_cat == AUDIO_ES )
618 bo_t *soun = GetSounBox( p_mux, p_stream );
619 box_gather( stsd, soun );
621 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
623 bo_t *vide = GetVideBox( p_mux, p_stream );
624 box_gather( stsd, vide );
627 /* append stsd to stbl */
629 box_gather( stbl, stsd );
631 /* chunk offset table */
632 p_stream->i_stco_pos = stbl->i_buffer + 16;
633 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
635 /* 64 bits version */
636 p_stream->b_stco64 = VLC_TRUE;
637 stco = box_full_new( "co64", 0, 0 );
641 /* 32 bits version */
642 p_stream->b_stco64 = VLC_FALSE;
643 stco = box_full_new( "stco", 0, 0 );
645 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
647 /* sample to chunk table */
648 stsc = box_full_new( "stsc", 0, 0 );
649 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
651 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
652 i < p_stream->i_entry_count; i_chunk++ )
656 if( p_stream->b_stco64 )
657 bo_add_64be( stco, p_stream->entry[i].i_pos );
659 bo_add_32be( stco, p_stream->entry[i].i_pos );
661 while( i < p_stream->i_entry_count )
663 if( i + 1 < p_stream->i_entry_count &&
664 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
665 != p_stream->entry[i + 1].i_pos )
674 /* Add entry to the stsc table */
675 if( i_stsc_last_val != i - i_first )
677 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
678 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
679 bo_add_32be( stsc, 1 ); // sample-descr-index
680 i_stsc_last_val = i - i_first;
685 /* Fix stco entry count */
686 bo_fix_32be( stco, 12, i_chunk );
687 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
689 /* append stco to stbl */
691 box_gather( stbl, stco );
693 /* Fix stsc entry count */
694 bo_fix_32be( stsc, 12, i_stsc_entries );
696 /* append stsc to stbl */
698 box_gather( stbl, stsc );
701 stts = box_full_new( "stts", 0, 0 );
702 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
704 if( p_stream->p_fmt->i_cat == AUDIO_ES )
705 i_timescale = p_stream->p_fmt->audio.i_rate;
709 for( i = 0, i_index = 0, i_residue = 0; i < p_stream->i_entry_count; i_index++)
716 i_delta = p_stream->entry[i].i_length;
718 while( i < p_stream->i_entry_count )
720 if( i + 1 < p_stream->i_entry_count &&
721 p_stream->entry[i + 1].i_length != i_delta )
730 i_tmp = i_delta * (int64_t)i_timescale + i_residue;
732 bo_add_32be( stts, i - i_first ); // sample-count
733 bo_add_32be( stts, i_tmp / I64C(1000000) ); // sample-delta
735 i_residue = i_tmp % I64C(1000000);
737 bo_fix_32be( stts, 12, i_index );
739 /* append stts to stbl */
741 box_gather( stbl, stts );
743 /* FIXME add ctts ?? FIXME */
745 stsz = box_full_new( "stsz", 0, 0 );
746 bo_add_32be( stsz, 0 ); // sample-size
747 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
748 for( i = 0; i < p_stream->i_entry_count; i++ )
750 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
752 /* append stsz to stbl */
754 box_gather( stbl, stsz );
761 static uint32_t mvhd_matrix[9] =
762 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
764 static bo_t *GetMoovBox( sout_mux_t *p_mux )
766 sout_mux_sys_t *p_sys = p_mux->p_sys;
771 uint32_t i_movie_timescale = 90000;
772 int64_t i_movie_duration = 0;
774 moov = box_new( "moov" );
776 /* Create general info */
777 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
779 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
780 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
782 msg_Dbg( p_mux, "movie duration %ds",
783 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
785 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
787 /* *** add /moov/mvhd *** */
788 if( !p_sys->b_64_ext )
790 mvhd = box_full_new( "mvhd", 0, 0 );
791 bo_add_32be( mvhd, get_timestamp() ); // creation time
792 bo_add_32be( mvhd, get_timestamp() ); // modification time
793 bo_add_32be( mvhd, i_movie_timescale); // timescale
794 bo_add_32be( mvhd, i_movie_duration ); // duration
798 mvhd = box_full_new( "mvhd", 1, 0 );
799 bo_add_64be( mvhd, get_timestamp() ); // creation time
800 bo_add_64be( mvhd, get_timestamp() ); // modification time
801 bo_add_32be( mvhd, i_movie_timescale); // timescale
802 bo_add_64be( mvhd, i_movie_duration ); // duration
804 bo_add_32be( mvhd, 0x10000 ); // rate
805 bo_add_16be( mvhd, 0x100 ); // volume
806 bo_add_16be( mvhd, 0 ); // reserved
807 for( i = 0; i < 2; i++ )
809 bo_add_32be( mvhd, 0 ); // reserved
811 for( i = 0; i < 9; i++ )
813 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
815 for( i = 0; i < 6; i++ )
817 bo_add_32be( mvhd, 0 ); // pre-defined
820 /* Next available track id */
821 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
824 box_gather( moov, mvhd );
826 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
828 mp4_stream_t *p_stream;
829 uint32_t i_timescale;
831 bo_t *trak, *tkhd, *mdia, *mdhd, *hdlr;
832 bo_t *minf, *dinf, *dref, *url, *stbl;
834 p_stream = p_sys->pp_streams[i_trak];
836 if( p_stream->p_fmt->i_cat != AUDIO_ES &&
837 p_stream->p_fmt->i_cat != VIDEO_ES )
839 msg_Err( p_mux, "FIXME ignoring trak (noaudio&&novideo)" );
843 if( p_stream->p_fmt->i_cat == AUDIO_ES )
844 i_timescale = p_stream->p_fmt->audio.i_rate;
848 /* *** add /moov/trak *** */
849 trak = box_new( "trak" );
851 /* *** add /moov/trak/tkhd *** */
852 if( !p_sys->b_64_ext )
855 tkhd = box_full_new( "tkhd", 0, 0x0f );
857 tkhd = box_full_new( "tkhd", 0, 1 );
859 bo_add_32be( tkhd, get_timestamp() ); // creation time
860 bo_add_32be( tkhd, get_timestamp() ); // modification time
861 bo_add_32be( tkhd, p_stream->i_track_id );
862 bo_add_32be( tkhd, 0 ); // reserved 0
863 bo_add_32be( tkhd, p_stream->i_duration *
864 (int64_t)i_movie_timescale /
865 (mtime_t)1000000 ); // duration
870 tkhd = box_full_new( "tkhd", 1, 0x0f );
872 tkhd = box_full_new( "tkhd", 1, 1 );
874 bo_add_64be( tkhd, get_timestamp() ); // creation time
875 bo_add_64be( tkhd, get_timestamp() ); // modification time
876 bo_add_32be( tkhd, p_stream->i_track_id );
877 bo_add_32be( tkhd, 0 ); // reserved 0
878 bo_add_64be( tkhd, p_stream->i_duration *
879 (int64_t)i_movie_timescale /
880 (mtime_t)1000000 ); // duration
883 for( i = 0; i < 2; i++ )
885 bo_add_32be( tkhd, 0 ); // reserved
887 bo_add_16be( tkhd, 0 ); // layer
888 bo_add_16be( tkhd, 0 ); // pre-defined
890 bo_add_16be( tkhd, p_stream->p_fmt->i_cat == AUDIO_ES ? 0x100 : 0 );
891 bo_add_16be( tkhd, 0 ); // reserved
892 for( i = 0; i < 9; i++ )
894 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
896 if( p_stream->p_fmt->i_cat == AUDIO_ES )
898 bo_add_32be( tkhd, 0 ); // width (presentation)
899 bo_add_32be( tkhd, 0 ); // height(presentation)
903 // width (presentation)
904 bo_add_32be( tkhd, p_stream->p_fmt->video.i_aspect *
905 p_stream->p_fmt->video.i_height /
906 VOUT_ASPECT_FACTOR << 16 );
907 // height(presentation)
908 bo_add_32be( tkhd, p_stream->p_fmt->video.i_height << 16 );
911 box_gather( trak, tkhd );
913 /* *** add /moov/trak/mdia *** */
914 mdia = box_new( "mdia" );
917 if( !p_sys->b_64_ext )
919 mdhd = box_full_new( "mdhd", 0, 0 );
920 bo_add_32be( mdhd, get_timestamp() ); // creation time
921 bo_add_32be( mdhd, get_timestamp() ); // modification time
922 bo_add_32be( mdhd, i_timescale); // timescale
923 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
924 (mtime_t)1000000 ); // duration
928 mdhd = box_full_new( "mdhd", 1, 0 );
929 bo_add_64be( mdhd, get_timestamp() ); // creation time
930 bo_add_64be( mdhd, get_timestamp() ); // modification time
931 bo_add_32be( mdhd, i_timescale); // timescale
932 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
933 (mtime_t)1000000 ); // duration
936 bo_add_16be( mdhd, 0 ); // language FIXME
937 bo_add_16be( mdhd, 0 ); // predefined
939 box_gather( mdia, mdhd );
941 /* handler reference */
942 hdlr = box_full_new( "hdlr", 0, 0 );
944 bo_add_fourcc( hdlr, "mhlr" ); // media handler
945 if( p_stream->p_fmt->i_cat == AUDIO_ES )
947 bo_add_fourcc( hdlr, "soun" );
951 bo_add_fourcc( hdlr, "vide" );
954 bo_add_32be( hdlr, 0 ); // reserved
955 bo_add_32be( hdlr, 0 ); // reserved
956 bo_add_32be( hdlr, 0 ); // reserved
958 bo_add_8( hdlr, 12 );
959 if( p_stream->p_fmt->i_cat == AUDIO_ES )
960 bo_add_mem( hdlr, 12, "SoundHandler" );
962 bo_add_mem( hdlr, 12, "VideoHandler" );
965 box_gather( mdia, hdlr );
968 minf = box_new( "minf" );
971 if( p_stream->p_fmt->i_cat == AUDIO_ES )
975 smhd = box_full_new( "smhd", 0, 0 );
976 bo_add_16be( smhd, 0 ); // balance
977 bo_add_16be( smhd, 0 ); // reserved
980 box_gather( minf, smhd );
982 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
986 vmhd = box_full_new( "vmhd", 0, 1 );
987 bo_add_16be( vmhd, 0 ); // graphicsmode
988 for( i = 0; i < 3; i++ )
990 bo_add_16be( vmhd, 0 ); // opcolor
994 box_gather( minf, vmhd );
998 dinf = box_new( "dinf" );
999 dref = box_full_new( "dref", 0, 0 );
1000 bo_add_32be( dref, 1 );
1001 url = box_full_new( "url ", 0, 0x01 );
1003 box_gather( dref, url );
1005 box_gather( dinf, dref );
1007 /* append dinf to mdia */
1009 box_gather( minf, dinf );
1012 stbl = GetStblBox( p_mux, p_stream );
1014 /* append stbl to minf */
1015 p_stream->i_stco_pos += minf->i_buffer;
1016 box_gather( minf, stbl );
1018 /* append minf to mdia */
1020 p_stream->i_stco_pos += mdia->i_buffer;
1021 box_gather( mdia, minf );
1023 /* append mdia to trak */
1025 p_stream->i_stco_pos += trak->i_buffer;
1026 box_gather( trak, mdia );
1028 /* append trak to moov */
1030 p_stream->i_stco_pos += moov->i_buffer;
1031 box_gather( moov, trak );
1034 /* Add user data tags */
1035 box_gather( moov, GetUdtaTag( p_mux ) );
1041 /*****************************************************************************
1043 *****************************************************************************/
1044 static void Close( vlc_object_t * p_this )
1046 sout_mux_t *p_mux = (sout_mux_t*)p_this;
1047 sout_mux_sys_t *p_sys = p_mux->p_sys;
1048 sout_buffer_t *p_hdr;
1053 uint64_t i_moov_pos;
1055 msg_Dbg( p_mux, "Close" );
1057 /* Update mdat size */
1058 bo_init( &bo, 0, NULL, VLC_TRUE );
1059 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
1062 bo_add_32be ( &bo, 1 );
1063 bo_add_fourcc( &bo, "mdat" );
1064 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
1068 bo_add_32be ( &bo, 8 );
1069 bo_add_fourcc( &bo, "wide" );
1070 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
1071 bo_add_fourcc( &bo, "mdat" );
1073 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
1074 free( bo.p_buffer );
1076 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
1077 sout_AccessOutWrite( p_mux->p_access, p_hdr );
1079 /* Create MOOV header */
1080 i_moov_pos = p_sys->i_pos;
1081 moov = GetMoovBox( p_mux );
1083 /* Check we need to create "fast start" files */
1084 var_Create( p_this, "mp4-faststart", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
1085 var_Get( p_this, "mp4-faststart", &val );
1086 p_sys->b_fast_start = val.b_bool;
1087 while( p_sys->b_fast_start )
1089 /* Move data to the end of the file so we can fit the moov header
1091 sout_buffer_t *p_buf;
1092 int64_t i_chunk, i_size = p_sys->i_pos - p_sys->i_mdat_pos;
1093 int i_moov_size = moov->i_buffer;
1097 i_chunk = __MIN( 32768, i_size );
1098 p_buf = sout_BufferNew( p_mux->p_sout, i_chunk );
1099 sout_AccessOutSeek( p_mux->p_access,
1100 p_sys->i_mdat_pos + i_size - i_chunk );
1101 if( sout_AccessOutRead( p_mux->p_access, p_buf ) < i_chunk )
1103 msg_Warn( p_this, "read() not supported by acces output, "
1104 "won't create a fast start file" );
1105 p_sys->b_fast_start = VLC_FALSE;
1108 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos + i_size +
1109 i_moov_size - i_chunk );
1110 sout_AccessOutWrite( p_mux->p_access, p_buf );
1114 if( !p_sys->b_fast_start ) break;
1116 /* Fix-up samples to chunks table in MOOV header */
1117 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1119 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1123 moov->i_buffer = p_stream->i_stco_pos;
1124 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
1126 if( p_stream->b_stco64 )
1127 bo_add_64be( moov, p_stream->entry[i].i_pos + i_moov_size);
1129 bo_add_32be( moov, p_stream->entry[i].i_pos + i_moov_size);
1131 while( i < p_stream->i_entry_count )
1133 if( i + 1 < p_stream->i_entry_count &&
1134 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
1135 != p_stream->entry[i + 1].i_pos )
1146 moov->i_buffer = i_moov_size;
1147 i_moov_pos = p_sys->i_mdat_pos;
1148 p_sys->b_fast_start = VLC_FALSE;
1151 /* Write MOOV header */
1152 sout_AccessOutSeek( p_mux->p_access, i_moov_pos );
1153 box_send( p_mux, moov );
1156 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1158 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1160 if( p_stream->p_fmt->p_extra )
1162 free( p_stream->p_fmt->p_extra );
1164 free( p_stream->p_fmt );
1165 free( p_stream->entry );
1168 if( p_sys->i_nb_streams ) free( p_sys->pp_streams );
1172 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
1177 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
1178 *(vlc_bool_t*)p_answer = VLC_FALSE;
1179 return( SOUT_MUX_CAP_ERR_OK );
1181 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
1185 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
1187 sout_mux_sys_t *p_sys = p_mux->p_sys;
1188 mp4_stream_t *p_stream;
1190 switch( p_input->p_fmt->i_codec )
1192 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1193 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1194 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1195 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
1196 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
1197 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
1198 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1201 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
1202 (char*)&p_input->p_fmt->i_codec );
1203 return VLC_EGENERIC;
1206 p_stream = malloc( sizeof( mp4_stream_t ) );
1207 p_stream->p_fmt = malloc( sizeof( es_format_t ) );
1208 memcpy( p_stream->p_fmt, p_input->p_fmt, sizeof( es_format_t ) );
1209 if( p_stream->p_fmt->i_extra )
1211 p_stream->p_fmt->p_extra =
1212 malloc( p_stream->p_fmt->i_extra );
1213 memcpy( p_stream->p_fmt->p_extra,
1214 p_input->p_fmt->p_extra,
1215 p_input->p_fmt->i_extra );
1217 p_stream->i_track_id = p_sys->i_nb_streams + 1;
1218 p_stream->i_length_neg = 0;
1219 p_stream->i_entry_count = 0;
1220 p_stream->i_entry_max = 1000;
1222 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
1223 p_stream->i_duration = 0;
1225 p_input->p_sys = p_stream;
1227 msg_Dbg( p_mux, "adding input" );
1229 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
1233 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
1235 msg_Dbg( p_mux, "removing input" );
1239 /****************************************************************************/
1241 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
1247 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
1249 sout_fifo_t *p_fifo;
1251 p_fifo = p_mux->pp_inputs[i]->p_fifo;
1253 if( p_fifo->i_depth > 1 )
1255 sout_buffer_t *p_buf;
1257 p_buf = sout_FifoShow( p_fifo );
1258 if( i_stream < 0 || p_buf->i_dts < i_dts )
1260 i_dts = p_buf->i_dts;
1266 return( -1 ); // wait that all fifo have at least 2 packets
1271 *pi_stream = i_stream;
1280 static int Mux( sout_mux_t *p_mux )
1282 sout_mux_sys_t *p_sys = p_mux->p_sys;
1286 sout_input_t *p_input;
1288 mp4_stream_t *p_stream;
1289 sout_buffer_t *p_data;
1292 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
1294 return( VLC_SUCCESS );
1297 if( !p_sys->i_start_dts )
1298 p_sys->i_start_dts = i_dts;
1300 p_input = p_mux->pp_inputs[i_stream];
1301 p_stream = (mp4_stream_t*)p_input->p_sys;
1303 p_data = sout_FifoGet( p_input->p_fifo );
1304 if( p_input->p_fifo->i_depth > 0 )
1306 sout_buffer_t *p_next = sout_FifoShow( p_input->p_fifo );
1307 int64_t i_diff = p_next->i_dts - p_data->i_dts;
1309 if( i_diff < I64C(1000000 ) ) /* protection */
1311 p_data->i_length = i_diff;
1314 if( p_data->i_length <= 0 )
1316 msg_Warn( p_mux, "i_length <= 0" );
1317 p_stream->i_length_neg += p_data->i_length - 1;
1318 p_data->i_length = 1;
1320 else if( p_stream->i_length_neg < 0 )
1322 int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
1324 p_data->i_length -= i_recover;
1325 p_stream->i_length_neg += i_recover;
1328 /* add index entry */
1329 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
1330 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_size;
1331 p_stream->entry[p_stream->i_entry_count].i_pts = p_data->i_pts;
1332 p_stream->entry[p_stream->i_entry_count].i_dts = p_data->i_dts;
1333 p_stream->entry[p_stream->i_entry_count].i_length= p_data->i_length;
1335 if( p_stream->i_entry_count == 0 )
1337 /* Here is another bad hack.
1338 * To make sure audio/video are in sync, we report a corrected
1339 * length for the 1st sample. */
1340 p_stream->entry[p_stream->i_entry_count].i_length =
1342 p_data->i_pts - p_sys->i_start_dts;
1345 p_stream->i_entry_count++;
1346 if( p_stream->i_entry_count >= p_stream->i_entry_max )
1348 p_stream->i_entry_max += 1000;
1350 realloc( p_stream->entry,
1351 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
1355 p_stream->i_duration += p_data->i_length;
1356 p_sys->i_pos += p_data->i_size;
1359 sout_AccessOutWrite( p_mux->p_access, p_data );
1362 return( VLC_SUCCESS );
1366 /****************************************************************************/
1368 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1373 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1374 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1378 p_bo->i_buffer_size = i_size;
1379 p_bo->p_buffer = p_buffer;
1382 p_bo->b_grow = b_grow;
1386 static void bo_add_8( bo_t *p_bo, uint8_t i )
1388 if( p_bo->i_buffer < p_bo->i_buffer_size )
1390 p_bo->p_buffer[p_bo->i_buffer] = i;
1392 else if( p_bo->b_grow )
1394 p_bo->i_buffer_size += 1024;
1395 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1397 p_bo->p_buffer[p_bo->i_buffer] = i;
1403 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1405 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1406 bo_add_8( p_bo, i &0xff );
1409 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1411 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1412 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1413 bo_add_8( p_bo, ( i &0xff ) );
1415 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1417 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1418 bo_add_16be( p_bo, i &0xffff );
1421 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1423 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1424 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1425 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1426 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1429 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1431 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1432 bo_add_32be( p_bo, i &0xffffffff );
1435 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1437 bo_add_8( p_bo, fcc[0] );
1438 bo_add_8( p_bo, fcc[1] );
1439 bo_add_8( p_bo, fcc[2] );
1440 bo_add_8( p_bo, fcc[3] );
1443 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1447 for( i = 0; i < i_size; i++ )
1449 bo_add_8( p_bo, p_mem[i] );
1453 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
1459 vals[3] = (unsigned char)(i_length & 0x7f);
1461 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
1463 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
1465 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
1467 bo_add_8( p_bo, tag );
1469 if( i_size < 0x00000080 )
1471 bo_add_8( p_bo, vals[3] );
1473 else if( i_size < 0x00004000 )
1475 bo_add_8( p_bo, vals[2] );
1476 bo_add_8( p_bo, vals[3] );
1478 else if( i_size < 0x00200000 )
1480 bo_add_8( p_bo, vals[1] );
1481 bo_add_8( p_bo, vals[2] );
1482 bo_add_8( p_bo, vals[3] );
1484 else if( i_size < 0x10000000 )
1486 bo_add_8( p_bo, vals[0] );
1487 bo_add_8( p_bo, vals[1] );
1488 bo_add_8( p_bo, vals[2] );
1489 bo_add_8( p_bo, vals[3] );
1493 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1497 for( i = 0; i < p_bo2->i_buffer; i++ )
1499 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1503 static bo_t * box_new( char *fcc )
1507 if( ( box = malloc( sizeof( bo_t ) ) ) )
1509 bo_init( box, 0, NULL, VLC_TRUE );
1511 bo_add_32be ( box, 0 );
1512 bo_add_fourcc( box, fcc );
1518 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
1522 if( ( box = malloc( sizeof( bo_t ) ) ) )
1524 bo_init( box, 0, NULL, VLC_TRUE );
1526 bo_add_32be ( box, 0 );
1527 bo_add_fourcc( box, fcc );
1528 bo_add_8 ( box, v );
1529 bo_add_24be ( box, f );
1535 static void box_fix( bo_t *box )
1539 memcpy( &box_tmp, box, sizeof( bo_t ) );
1541 box_tmp.i_buffer = 0;
1542 bo_add_32be( &box_tmp, box->i_buffer );
1545 static void box_free( bo_t *box )
1549 free( box->p_buffer );
1555 static void box_gather ( bo_t *box, bo_t *box2 )
1557 bo_add_bo( box, box2 );
1561 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
1563 sout_buffer_t *p_buf;
1565 p_buf = sout_BufferNew( p_sout, box->i_buffer );
1566 if( box->i_buffer > 0 )
1568 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
1571 p_buf->i_size = box->i_buffer;
1576 static void box_send( sout_mux_t *p_mux, bo_t *box )
1578 sout_buffer_t *p_buf;
1580 p_buf = bo_to_sout( p_mux->p_sout, box );
1583 sout_AccessOutWrite( p_mux->p_access, p_buf );
1586 static int64_t get_timestamp()
1588 int64_t i_timestamp = 0;
1591 i_timestamp = time(NULL);
1592 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
1593 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60