1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003 VideoLAN
5 * $Id: mp4.c,v 1.14 2004/01/26 18:30:01 gbazin Exp $
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 *****************************************************************************/
94 unsigned int i_entry_count;
95 unsigned int i_entry_max;
101 /* for later stco fix-up (fast start files) */
107 struct sout_mux_sys_t
111 vlc_bool_t b_fast_start;
119 mp4_stream_t **pp_streams;
132 static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
133 static void bo_add_8 ( bo_t *, uint8_t );
134 static void bo_add_16be ( bo_t *, uint16_t );
135 static void bo_add_24be ( bo_t *, uint32_t );
136 static void bo_add_32be ( bo_t *, uint32_t );
137 static void bo_add_64be ( bo_t *, uint64_t );
138 static void bo_add_fourcc(bo_t *, char * );
139 static void bo_add_bo ( bo_t *, bo_t * );
140 static void bo_add_mem ( bo_t *, int , uint8_t * );
141 static void bo_add_descr( bo_t *, uint8_t , uint32_t );
143 static void bo_fix_32be ( bo_t *, int , uint32_t );
145 static bo_t *box_new ( char *fcc );
146 static bo_t *box_full_new( char *fcc, uint8_t v, uint32_t f );
147 static void box_fix ( bo_t *box );
148 static void box_free ( bo_t *box );
149 static void box_gather ( bo_t *box, bo_t *box2 );
151 static void box_send( sout_mux_t *p_mux, bo_t *box );
153 static int64_t get_timestamp();
155 static sout_buffer_t *bo_to_sout( sout_instance_t *p_sout, bo_t *box );
157 /*****************************************************************************
159 *****************************************************************************/
160 static int Open( vlc_object_t *p_this )
162 sout_mux_t *p_mux = (sout_mux_t*)p_this;
163 sout_mux_sys_t *p_sys;
166 p_sys = malloc( sizeof( sout_mux_sys_t ) );
168 p_sys->i_nb_streams = 0;
169 p_sys->pp_streams = NULL;
170 p_sys->i_mdat_pos = 0;
171 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
172 p_sys->i_start_dts = 0;
174 msg_Dbg( p_mux, "Open" );
176 p_mux->pf_capacity = Capability;
177 p_mux->pf_addstream = AddStream;
178 p_mux->pf_delstream = DelStream;
180 p_mux->p_sys = p_sys;
184 /* Now add ftyp header */
185 box = box_new( "ftyp" );
186 bo_add_fourcc( box, "isom" );
187 bo_add_32be ( box, 0 );
188 bo_add_fourcc( box, "mp41" );
191 p_sys->i_pos += box->i_buffer;
192 p_sys->i_mdat_pos = p_sys->i_pos;
194 box_send( p_mux, box );
198 * Quicktime actually doesn't like the 64 bits extensions !!! */
199 p_sys->b_64_ext = VLC_FALSE;
201 /* Now add mdat header */
202 box = box_new( "mdat" );
203 bo_add_64be ( box, 0 ); // enough to store an extended size
205 p_sys->i_pos += box->i_buffer;
207 box_send( p_mux, box );
212 static int GetDescrLength( int i_size )
214 if( i_size < 0x00000080 )
216 else if( i_size < 0x00004000 )
218 else if( i_size < 0x00200000 )
224 static bo_t *GetESDS( mp4_stream_t *p_stream )
228 int i_object_type_indication;
229 int i_decoder_specific_info_size;
231 if( p_stream->p_fmt->i_extra > 0 )
233 i_decoder_specific_info_size =
234 GetDescrLength( p_stream->p_fmt->i_extra );
238 i_decoder_specific_info_size = 0;
241 esds = box_full_new( "esds", 0, 0 );
244 bo_add_descr( esds, 0x03, 3 +
245 GetDescrLength( 13 + i_decoder_specific_info_size ) +
246 GetDescrLength( 1 ) );
247 bo_add_16be( esds, p_stream->i_track_id );
248 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
250 /* DecoderConfigDescr */
251 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
253 switch( p_stream->p_fmt->i_codec )
255 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
256 i_object_type_indication = 0x20;
258 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
259 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
260 i_object_type_indication = 0x60;
262 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
263 /* FIXME for mpeg2-aac == 0x66->0x68 */
264 i_object_type_indication = 0x40;
266 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
267 i_object_type_indication =
268 p_stream->p_fmt->audio.i_rate < 32000 ? 0x69 : 0x6b;
271 i_object_type_indication = 0x00;
274 i_stream_type = p_stream->p_fmt->i_cat == VIDEO_ES ? 0x04 : 0x05;
276 bo_add_8 ( esds, i_object_type_indication );
277 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
278 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
279 bo_add_32be( esds, 0x7fffffff ); // maxBitrate
280 bo_add_32be( esds, 0 ); // avgBitrate
282 if( p_stream->p_fmt->i_extra > 0 )
286 /* DecoderSpecificInfo */
287 bo_add_descr( esds, 0x05, p_stream->p_fmt->i_extra );
289 for( i = 0; i < p_stream->p_fmt->i_extra; i++ )
291 bo_add_8( esds, ((uint8_t*)p_stream->p_fmt->p_extra)[i] );
295 /* SL_Descr mandatory */
296 bo_add_descr( esds, 0x06, 1 );
297 bo_add_8 ( esds, 0x02 ); // sl_predefined
304 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
309 wave = box_new( "wave" );
311 box = box_new( "frma" );
312 bo_add_fourcc( box, "mp4a" );
314 box_gather( wave, box );
316 box = box_new( "mp4a" );
317 bo_add_32be( box, 0 );
319 box_gather( wave, box );
321 box = GetESDS( p_stream );
323 box_gather( wave, box );
325 box = box_new( "srcq" );
326 bo_add_32be( box, 0x40 );
328 box_gather( wave, box );
331 bo_add_32be( wave, 8 ); /* new empty box */
332 bo_add_32be( wave, 0 ); /* box label */
339 /* TODO: No idea about these values */
340 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
342 bo_t *smi = box_new( "SMI " );
344 bo_add_fourcc( smi, "SEQH" );
345 bo_add_32be( smi, 0x5 );
346 bo_add_32be( smi, 0xe2c0211d );
347 bo_add_8( smi, 0xc0 );
353 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
355 sout_mux_sys_t *p_sys = p_mux->p_sys;
356 bo_t *udta = box_new( "udta" );
360 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
362 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
364 if( p_stream->p_fmt->i_codec == VLC_FOURCC('m','p','4','v') ||
365 p_stream->p_fmt->i_codec == VLC_FOURCC('m','p','4','a') )
367 bo_t *box = box_new( "\251req" );
369 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
370 bo_add_16be( box, 0 );
371 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
372 "QuickTime 6.0 or greater" );
374 box_gather( udta, box );
381 bo_t *box = box_new( "\251enc" );
383 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
384 bo_add_16be( box, 0 );
385 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
386 PACKAGE_STRING " stream output" );
388 box_gather( udta, box );
395 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
397 sout_mux_sys_t *p_sys = p_mux->p_sys;
398 vlc_bool_t b_descr = VLC_FALSE;
403 switch( p_stream->p_fmt->i_codec )
405 case VLC_FOURCC('m','p','4','a'):
406 memcpy( fcc, "mp4a", 4 );
410 case VLC_FOURCC('m','p','g','a'):
412 memcpy( fcc, ".mp3", 4 );
414 memcpy( fcc, "mp4a", 4 );
418 memcpy( fcc, (char*)&p_stream->p_fmt->i_codec, 4 );
422 soun = box_new( fcc );
423 for( i = 0; i < 6; i++ )
425 bo_add_8( soun, 0 ); // reserved;
427 bo_add_16be( soun, 1 ); // data-reference-index
429 /* SoundDescription */
431 p_stream->p_fmt->i_codec == VLC_FOURCC('m','p','4','a') )
433 bo_add_16be( soun, 1 ); // version 1;
437 bo_add_16be( soun, 0 ); // version 0;
439 bo_add_16be( soun, 0 ); // revision level (0)
440 bo_add_32be( soun, 0 ); // vendor
442 bo_add_16be( soun, p_stream->p_fmt->audio.i_channels );
444 bo_add_16be( soun, p_stream->p_fmt->audio.i_bitspersample ?
445 p_stream->p_fmt->audio.i_bitspersample : 16 );
446 bo_add_16be( soun, -2 ); // compression id
447 bo_add_16be( soun, 0 ); // packet size (0)
448 bo_add_16be( soun, p_stream->p_fmt->audio.i_rate ); // sampleratehi
449 bo_add_16be( soun, 0 ); // sampleratelo
451 /* Extended data for SoundDescription V1 */
453 p_stream->p_fmt->i_codec == VLC_FOURCC('m','p','4','a') )
455 /* samples per packet */
456 bo_add_32be( soun, p_stream->p_fmt->audio.i_frame_length );
457 bo_add_32be( soun, 1536 ); /* bytes per packet */
458 bo_add_32be( soun, 2 ); /* bytes per frame */
459 /* bytes per sample */
460 bo_add_32be( soun, 2 /*p_stream->p_fmt->audio.i_bitspersample/8 */);
463 /* Add an ES Descriptor */
469 p_stream->p_fmt->i_codec == VLC_FOURCC('m','p','4','a') )
471 box = GetWaveTag( p_stream );
475 box = GetESDS( p_stream );
478 box_gather( soun, box );
486 static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
493 switch( p_stream->p_fmt->i_codec )
495 case VLC_FOURCC('m','p','4','v'):
496 case VLC_FOURCC('m','p','g','v'):
497 memcpy( fcc, "mp4v", 4 );
500 case VLC_FOURCC('M','J','P','G'):
501 memcpy( fcc, "mjpa", 4 );
504 case VLC_FOURCC('S','V','Q','3'):
505 memcpy( fcc, "SVQ3", 4 );
509 memcpy( fcc, (char*)&p_stream->p_fmt->i_codec, 4 );
513 vide = box_new( fcc );
514 for( i = 0; i < 6; i++ )
516 bo_add_8( vide, 0 ); // reserved;
518 bo_add_16be( vide, 1 ); // data-reference-index
520 bo_add_16be( vide, 0 ); // predefined;
521 bo_add_16be( vide, 0 ); // reserved;
522 for( i = 0; i < 3; i++ )
524 bo_add_32be( vide, 0 ); // predefined;
527 bo_add_16be( vide, p_stream->p_fmt->video.i_width ); // i_width
528 bo_add_16be( vide, p_stream->p_fmt->video.i_height ); // i_height
530 bo_add_32be( vide, 0x00480000 ); // h 72dpi
531 bo_add_32be( vide, 0x00480000 ); // v 72dpi
533 bo_add_32be( vide, 0 ); // data size, always 0
534 bo_add_16be( vide, 1 ); // frames count per sample
537 for( i = 0; i < 32; i++ )
542 bo_add_16be( vide, 0x18 ); // depth
543 bo_add_16be( vide, 0xffff ); // predefined
545 /* add an ES Descriptor */
546 switch( p_stream->p_fmt->i_codec )
548 case VLC_FOURCC('m','p','4','v'):
549 case VLC_FOURCC('m','p','g','v'):
551 bo_t *esds = GetESDS( p_stream );
554 box_gather( vide, esds );
558 case VLC_FOURCC('S','V','Q','3'):
560 bo_t *esds = GetSVQ3Tag( p_stream );
563 box_gather( vide, esds );
576 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
578 sout_mux_sys_t *p_sys = p_mux->p_sys;
579 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
580 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz;
581 uint32_t i_timescale;
583 stbl = box_new( "stbl" );
584 stsd = box_full_new( "stsd", 0, 0 );
585 bo_add_32be( stsd, 1 );
587 if( p_stream->p_fmt->i_cat == AUDIO_ES )
589 bo_t *soun = GetSounBox( p_mux, p_stream );
590 box_gather( stsd, soun );
592 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
594 bo_t *vide = GetVideBox( p_mux, p_stream );
595 box_gather( stsd, vide );
598 /* append stsd to stbl */
600 box_gather( stbl, stsd );
602 /* chunk offset table */
603 p_stream->i_stco_pos = stbl->i_buffer + 16;
604 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
606 /* 64 bits version */
607 p_stream->b_stco64 = VLC_TRUE;
608 stco = box_full_new( "co64", 0, 0 );
612 /* 32 bits version */
613 p_stream->b_stco64 = VLC_FALSE;
614 stco = box_full_new( "stco", 0, 0 );
616 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
618 /* sample to chunk table */
619 stsc = box_full_new( "stsc", 0, 0 );
620 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
622 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
623 i < p_stream->i_entry_count; i_chunk++ )
627 if( p_stream->b_stco64 )
628 bo_add_64be( stco, p_stream->entry[i].i_pos );
630 bo_add_32be( stco, p_stream->entry[i].i_pos );
632 while( i < p_stream->i_entry_count )
634 if( i + 1 < p_stream->i_entry_count &&
635 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
636 != p_stream->entry[i + 1].i_pos )
645 /* Add entry to the stsc table */
646 if( i_stsc_last_val != i - i_first )
648 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
649 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
650 bo_add_32be( stsc, 1 ); // sample-descr-index
651 i_stsc_last_val = i - i_first;
656 /* Fix stco entry count */
657 bo_fix_32be( stco, 12, i_chunk );
658 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
660 /* append stco to stbl */
662 box_gather( stbl, stco );
664 /* Fix stsc entry count */
665 bo_fix_32be( stsc, 12, i_stsc_entries );
667 /* append stsc to stbl */
669 box_gather( stbl, stsc );
672 stts = box_full_new( "stts", 0, 0 );
673 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
675 if( p_stream->p_fmt->i_cat == AUDIO_ES )
676 i_timescale = p_stream->p_fmt->audio.i_rate;
680 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
686 i_delta = p_stream->entry[i].i_length;
688 while( i < p_stream->i_entry_count )
690 if( i + 1 < p_stream->i_entry_count &&
691 p_stream->entry[i + 1].i_length != i_delta )
700 bo_add_32be( stts, i - i_first ); // sample-count
701 bo_add_32be( stts, i_delta * (int64_t)i_timescale /
702 (int64_t)1000000 ); // sample-delta
704 bo_fix_32be( stts, 12, i_index );
706 /* append stts to stbl */
708 box_gather( stbl, stts );
710 /* FIXME add ctts ?? FIXME */
712 stsz = box_full_new( "stsz", 0, 0 );
713 bo_add_32be( stsz, 0 ); // sample-size
714 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
715 for( i = 0; i < p_stream->i_entry_count; i++ )
717 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
719 /* append stsz to stbl */
721 box_gather( stbl, stsz );
728 static uint32_t mvhd_matrix[9] =
729 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
731 static bo_t *GetMoovBox( sout_mux_t *p_mux )
733 sout_mux_sys_t *p_sys = p_mux->p_sys;
738 uint32_t i_movie_timescale = 90000;
739 int64_t i_movie_duration = 0;
741 moov = box_new( "moov" );
743 /* Create general info */
744 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
746 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
747 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
749 msg_Dbg( p_mux, "movie duration %ds",
750 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
752 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
754 /* *** add /moov/mvhd *** */
755 if( !p_sys->b_64_ext )
757 mvhd = box_full_new( "mvhd", 0, 0 );
758 bo_add_32be( mvhd, get_timestamp() ); // creation time
759 bo_add_32be( mvhd, get_timestamp() ); // modification time
760 bo_add_32be( mvhd, i_movie_timescale); // timescale
761 bo_add_32be( mvhd, i_movie_duration ); // duration
765 mvhd = box_full_new( "mvhd", 1, 0 );
766 bo_add_64be( mvhd, get_timestamp() ); // creation time
767 bo_add_64be( mvhd, get_timestamp() ); // modification time
768 bo_add_32be( mvhd, i_movie_timescale); // timescale
769 bo_add_64be( mvhd, i_movie_duration ); // duration
771 bo_add_32be( mvhd, 0x10000 ); // rate
772 bo_add_16be( mvhd, 0x100 ); // volume
773 bo_add_16be( mvhd, 0 ); // reserved
774 for( i = 0; i < 2; i++ )
776 bo_add_32be( mvhd, 0 ); // reserved
778 for( i = 0; i < 9; i++ )
780 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
782 for( i = 0; i < 6; i++ )
784 bo_add_32be( mvhd, 0 ); // pre-defined
787 /* Next available track id */
788 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
791 box_gather( moov, mvhd );
793 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
795 mp4_stream_t *p_stream;
796 uint32_t i_timescale;
798 bo_t *trak, *tkhd, *mdia, *mdhd, *hdlr;
799 bo_t *minf, *dinf, *dref, *url, *stbl;
801 p_stream = p_sys->pp_streams[i_trak];
803 if( p_stream->p_fmt->i_cat != AUDIO_ES &&
804 p_stream->p_fmt->i_cat != VIDEO_ES )
806 msg_Err( p_mux, "FIXME ignoring trak (noaudio&&novideo)" );
810 if( p_stream->p_fmt->i_cat == AUDIO_ES )
811 i_timescale = p_stream->p_fmt->audio.i_rate;
815 /* *** add /moov/trak *** */
816 trak = box_new( "trak" );
818 /* *** add /moov/trak/tkhd *** */
819 if( !p_sys->b_64_ext )
822 tkhd = box_full_new( "tkhd", 0, 0x0f );
824 tkhd = box_full_new( "tkhd", 0, 1 );
826 bo_add_32be( tkhd, get_timestamp() ); // creation time
827 bo_add_32be( tkhd, get_timestamp() ); // modification time
828 bo_add_32be( tkhd, p_stream->i_track_id );
829 bo_add_32be( tkhd, 0 ); // reserved 0
830 bo_add_32be( tkhd, p_stream->i_duration *
831 (int64_t)i_movie_timescale /
832 (mtime_t)1000000 ); // duration
837 tkhd = box_full_new( "tkhd", 1, 0x0f );
839 tkhd = box_full_new( "tkhd", 1, 1 );
841 bo_add_64be( tkhd, get_timestamp() ); // creation time
842 bo_add_64be( tkhd, get_timestamp() ); // modification time
843 bo_add_32be( tkhd, p_stream->i_track_id );
844 bo_add_32be( tkhd, 0 ); // reserved 0
845 bo_add_64be( tkhd, p_stream->i_duration *
846 (int64_t)i_movie_timescale /
847 (mtime_t)1000000 ); // duration
850 for( i = 0; i < 2; i++ )
852 bo_add_32be( tkhd, 0 ); // reserved
854 bo_add_16be( tkhd, 0 ); // layer
855 bo_add_16be( tkhd, 0 ); // pre-defined
857 bo_add_16be( tkhd, p_stream->p_fmt->i_cat == AUDIO_ES ? 0x100 : 0 );
858 bo_add_16be( tkhd, 0 ); // reserved
859 for( i = 0; i < 9; i++ )
861 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
863 if( p_stream->p_fmt->i_cat == AUDIO_ES )
865 bo_add_32be( tkhd, 0 ); // width (presentation)
866 bo_add_32be( tkhd, 0 ); // height(presentation)
870 // width (presentation)
871 bo_add_32be( tkhd, p_stream->p_fmt->video.i_aspect *
872 p_stream->p_fmt->video.i_height /
873 VOUT_ASPECT_FACTOR << 16 );
874 // height(presentation)
875 bo_add_32be( tkhd, p_stream->p_fmt->video.i_height << 16 );
878 box_gather( trak, tkhd );
880 /* *** add /moov/trak/mdia *** */
881 mdia = box_new( "mdia" );
884 if( !p_sys->b_64_ext )
886 mdhd = box_full_new( "mdhd", 0, 0 );
887 bo_add_32be( mdhd, get_timestamp() ); // creation time
888 bo_add_32be( mdhd, get_timestamp() ); // modification time
889 bo_add_32be( mdhd, i_timescale); // timescale
890 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
891 (mtime_t)1000000 ); // duration
895 mdhd = box_full_new( "mdhd", 1, 0 );
896 bo_add_64be( mdhd, get_timestamp() ); // creation time
897 bo_add_64be( mdhd, get_timestamp() ); // modification time
898 bo_add_32be( mdhd, i_timescale); // timescale
899 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
900 (mtime_t)1000000 ); // duration
903 bo_add_16be( mdhd, 0 ); // language FIXME
904 bo_add_16be( mdhd, 0 ); // predefined
906 box_gather( mdia, mdhd );
908 /* handler reference */
909 hdlr = box_full_new( "hdlr", 0, 0 );
911 bo_add_fourcc( hdlr, "mhlr" ); // media handler
912 if( p_stream->p_fmt->i_cat == AUDIO_ES )
914 bo_add_fourcc( hdlr, "soun" );
918 bo_add_fourcc( hdlr, "vide" );
921 bo_add_32be( hdlr, 0 ); // reserved
922 bo_add_32be( hdlr, 0 ); // reserved
923 bo_add_32be( hdlr, 0 ); // reserved
925 bo_add_8( hdlr, 12 );
926 if( p_stream->p_fmt->i_cat == AUDIO_ES )
927 bo_add_mem( hdlr, 12, "SoundHandler" );
929 bo_add_mem( hdlr, 12, "VideoHandler" );
932 box_gather( mdia, hdlr );
935 minf = box_new( "minf" );
938 if( p_stream->p_fmt->i_cat == AUDIO_ES )
942 smhd = box_full_new( "smhd", 0, 0 );
943 bo_add_16be( smhd, 0 ); // balance
944 bo_add_16be( smhd, 0 ); // reserved
947 box_gather( minf, smhd );
949 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
953 vmhd = box_full_new( "vmhd", 0, 1 );
954 bo_add_16be( vmhd, 0 ); // graphicsmode
955 for( i = 0; i < 3; i++ )
957 bo_add_16be( vmhd, 0 ); // opcolor
961 box_gather( minf, vmhd );
965 dinf = box_new( "dinf" );
966 dref = box_full_new( "dref", 0, 0 );
967 bo_add_32be( dref, 1 );
968 url = box_full_new( "url ", 0, 0x01 );
970 box_gather( dref, url );
972 box_gather( dinf, dref );
974 /* append dinf to mdia */
976 box_gather( minf, dinf );
979 stbl = GetStblBox( p_mux, p_stream );
981 /* append stbl to minf */
982 p_stream->i_stco_pos += minf->i_buffer;
983 box_gather( minf, stbl );
985 /* append minf to mdia */
987 p_stream->i_stco_pos += mdia->i_buffer;
988 box_gather( mdia, minf );
990 /* append mdia to trak */
992 p_stream->i_stco_pos += trak->i_buffer;
993 box_gather( trak, mdia );
995 /* append trak to moov */
997 p_stream->i_stco_pos += moov->i_buffer;
998 box_gather( moov, trak );
1001 /* Add user data tags */
1002 box_gather( moov, GetUdtaTag( p_mux ) );
1008 /*****************************************************************************
1010 *****************************************************************************/
1011 static void Close( vlc_object_t * p_this )
1013 sout_mux_t *p_mux = (sout_mux_t*)p_this;
1014 sout_mux_sys_t *p_sys = p_mux->p_sys;
1015 sout_buffer_t *p_hdr;
1020 uint64_t i_moov_pos;
1022 msg_Dbg( p_mux, "Close" );
1024 /* Update mdat size */
1025 bo_init( &bo, 0, NULL, VLC_TRUE );
1026 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
1029 bo_add_32be ( &bo, 1 );
1030 bo_add_fourcc( &bo, "mdat" );
1031 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
1035 bo_add_32be ( &bo, 8 );
1036 bo_add_fourcc( &bo, "wide" );
1037 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
1038 bo_add_fourcc( &bo, "mdat" );
1040 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
1041 free( bo.p_buffer );
1043 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
1044 sout_AccessOutWrite( p_mux->p_access, p_hdr );
1046 /* Create MOOV header */
1047 i_moov_pos = p_sys->i_pos;
1048 moov = GetMoovBox( p_mux );
1050 /* Check we need to create "fast start" files */
1051 var_Create( p_this, "mp4-faststart", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
1052 var_Get( p_this, "mp4-faststart", &val );
1053 p_sys->b_fast_start = val.b_bool;
1054 while( p_sys->b_fast_start )
1056 /* Move data to the end of the file so we can fit the moov header
1058 sout_buffer_t *p_buf;
1059 int64_t i_chunk, i_size = p_sys->i_pos - p_sys->i_mdat_pos;
1060 int i_moov_size = moov->i_buffer;
1064 i_chunk = __MIN( 32768, i_size );
1065 p_buf = sout_BufferNew( p_mux->p_sout, i_chunk );
1066 sout_AccessOutSeek( p_mux->p_access,
1067 p_sys->i_mdat_pos + i_size - i_chunk );
1068 if( sout_AccessOutRead( p_mux->p_access, p_buf ) < i_chunk )
1070 msg_Warn( p_this, "read() not supported by acces output, "
1071 "won't create a fast start file" );
1072 p_sys->b_fast_start = VLC_FALSE;
1075 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos + i_size +
1076 i_moov_size - i_chunk );
1077 sout_AccessOutWrite( p_mux->p_access, p_buf );
1081 if( !p_sys->b_fast_start ) break;
1083 /* Fix-up samples to chunks table in MOOV header */
1084 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1086 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1090 moov->i_buffer = p_stream->i_stco_pos;
1091 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
1093 if( p_stream->b_stco64 )
1094 bo_add_64be( moov, p_stream->entry[i].i_pos + i_moov_size);
1096 bo_add_32be( moov, p_stream->entry[i].i_pos + i_moov_size);
1098 while( i < p_stream->i_entry_count )
1100 if( i + 1 < p_stream->i_entry_count &&
1101 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
1102 != p_stream->entry[i + 1].i_pos )
1113 moov->i_buffer = i_moov_size;
1114 i_moov_pos = p_sys->i_mdat_pos;
1115 p_sys->b_fast_start = VLC_FALSE;
1118 /* Write MOOV header */
1119 sout_AccessOutSeek( p_mux->p_access, i_moov_pos );
1120 box_send( p_mux, moov );
1123 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1125 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1127 if( p_stream->p_fmt->p_extra )
1129 free( p_stream->p_fmt->p_extra );
1131 free( p_stream->p_fmt );
1132 free( p_stream->entry );
1138 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
1143 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
1144 *(vlc_bool_t*)p_answer = VLC_FALSE;
1145 return( SOUT_MUX_CAP_ERR_OK );
1147 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
1151 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
1153 sout_mux_sys_t *p_sys = p_mux->p_sys;
1154 mp4_stream_t *p_stream;
1156 switch( p_input->p_fmt->i_codec )
1158 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1159 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1160 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1161 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
1162 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
1163 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
1164 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
1167 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
1168 (char*)&p_input->p_fmt->i_codec );
1169 return VLC_EGENERIC;
1172 p_stream = malloc( sizeof( mp4_stream_t ) );
1173 p_stream->p_fmt = malloc( sizeof( es_format_t ) );
1174 memcpy( p_stream->p_fmt, p_input->p_fmt, sizeof( es_format_t ) );
1175 if( p_stream->p_fmt->i_extra )
1177 p_stream->p_fmt->p_extra =
1178 malloc( p_stream->p_fmt->i_extra );
1179 memcpy( p_stream->p_fmt->p_extra,
1180 p_input->p_fmt->p_extra,
1181 p_input->p_fmt->i_extra );
1183 p_stream->i_track_id = p_sys->i_nb_streams + 1;
1184 p_stream->i_entry_count = 0;
1185 p_stream->i_entry_max = 1000;
1187 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
1188 p_stream->i_duration = 0;
1190 p_input->p_sys = p_stream;
1192 msg_Dbg( p_mux, "adding input" );
1194 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
1195 return( VLC_SUCCESS );
1198 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
1200 msg_Dbg( p_mux, "removing input" );
1204 /****************************************************************************/
1206 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
1212 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
1214 sout_fifo_t *p_fifo;
1216 p_fifo = p_mux->pp_inputs[i]->p_fifo;
1218 if( p_fifo->i_depth > 1 )
1220 sout_buffer_t *p_buf;
1222 p_buf = sout_FifoShow( p_fifo );
1223 if( i_stream < 0 || p_buf->i_dts < i_dts )
1225 i_dts = p_buf->i_dts;
1231 return( -1 ); // wait that all fifo have at least 2 packets
1236 *pi_stream = i_stream;
1245 static int Mux( sout_mux_t *p_mux )
1247 sout_mux_sys_t *p_sys = p_mux->p_sys;
1251 sout_input_t *p_input;
1253 mp4_stream_t *p_stream;
1254 sout_buffer_t *p_data;
1257 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
1259 return( VLC_SUCCESS );
1262 if( !p_sys->i_start_dts )
1263 p_sys->i_start_dts = i_dts;
1265 p_input = p_mux->pp_inputs[i_stream];
1266 p_stream = (mp4_stream_t*)p_input->p_sys;
1268 p_data = sout_FifoGet( p_input->p_fifo );
1270 /* add index entry */
1271 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
1272 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_size;
1273 p_stream->entry[p_stream->i_entry_count].i_pts = p_data->i_pts;
1274 p_stream->entry[p_stream->i_entry_count].i_dts = p_data->i_dts;
1275 p_stream->entry[p_stream->i_entry_count].i_length=
1276 __MAX( p_data->i_length, 0 );
1278 if( p_stream->i_entry_count == 0 )
1280 /* Here is another bad hack.
1281 * To make sure audio/video are in sync, we report a corrected
1282 * length for the 1st sample. */
1283 p_stream->entry[p_stream->i_entry_count].i_length =
1284 __MAX( p_data->i_length, 0 ) +
1285 p_data->i_pts - p_sys->i_start_dts;
1288 p_stream->i_entry_count++;
1289 if( p_stream->i_entry_count >= p_stream->i_entry_max )
1291 p_stream->i_entry_max += 1000;
1293 realloc( p_stream->entry,
1294 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
1298 p_stream->i_duration += __MAX( p_data->i_length, 0 );
1299 p_sys->i_pos += p_data->i_size;
1302 sout_AccessOutWrite( p_mux->p_access, p_data );
1305 return( VLC_SUCCESS );
1309 /****************************************************************************/
1311 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1316 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1317 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1321 p_bo->i_buffer_size = i_size;
1322 p_bo->p_buffer = p_buffer;
1325 p_bo->b_grow = b_grow;
1329 static void bo_add_8( bo_t *p_bo, uint8_t i )
1331 if( p_bo->i_buffer < p_bo->i_buffer_size )
1333 p_bo->p_buffer[p_bo->i_buffer] = i;
1335 else if( p_bo->b_grow )
1337 p_bo->i_buffer_size += 1024;
1338 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1340 p_bo->p_buffer[p_bo->i_buffer] = i;
1346 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1348 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1349 bo_add_8( p_bo, i &0xff );
1352 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1354 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1355 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1356 bo_add_8( p_bo, ( i &0xff ) );
1358 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1360 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1361 bo_add_16be( p_bo, i &0xffff );
1364 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1366 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1367 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1368 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1369 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1372 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1374 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1375 bo_add_32be( p_bo, i &0xffffffff );
1378 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1380 bo_add_8( p_bo, fcc[0] );
1381 bo_add_8( p_bo, fcc[1] );
1382 bo_add_8( p_bo, fcc[2] );
1383 bo_add_8( p_bo, fcc[3] );
1386 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1390 for( i = 0; i < i_size; i++ )
1392 bo_add_8( p_bo, p_mem[i] );
1396 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
1402 vals[3] = (unsigned char)(i_length & 0x7f);
1404 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
1406 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
1408 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
1410 bo_add_8( p_bo, tag );
1412 if( i_size < 0x00000080 )
1414 bo_add_8( p_bo, vals[3] );
1416 else if( i_size < 0x00004000 )
1418 bo_add_8( p_bo, vals[2] );
1419 bo_add_8( p_bo, vals[3] );
1421 else if( i_size < 0x00200000 )
1423 bo_add_8( p_bo, vals[1] );
1424 bo_add_8( p_bo, vals[2] );
1425 bo_add_8( p_bo, vals[3] );
1427 else if( i_size < 0x10000000 )
1429 bo_add_8( p_bo, vals[0] );
1430 bo_add_8( p_bo, vals[1] );
1431 bo_add_8( p_bo, vals[2] );
1432 bo_add_8( p_bo, vals[3] );
1436 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1440 for( i = 0; i < p_bo2->i_buffer; i++ )
1442 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1446 static bo_t * box_new( char *fcc )
1450 if( ( box = malloc( sizeof( bo_t ) ) ) )
1452 bo_init( box, 0, NULL, VLC_TRUE );
1454 bo_add_32be ( box, 0 );
1455 bo_add_fourcc( box, fcc );
1461 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
1465 if( ( box = malloc( sizeof( bo_t ) ) ) )
1467 bo_init( box, 0, NULL, VLC_TRUE );
1469 bo_add_32be ( box, 0 );
1470 bo_add_fourcc( box, fcc );
1471 bo_add_8 ( box, v );
1472 bo_add_24be ( box, f );
1478 static void box_fix( bo_t *box )
1482 memcpy( &box_tmp, box, sizeof( bo_t ) );
1484 box_tmp.i_buffer = 0;
1485 bo_add_32be( &box_tmp, box->i_buffer );
1488 static void box_free( bo_t *box )
1492 free( box->p_buffer );
1498 static void box_gather ( bo_t *box, bo_t *box2 )
1500 bo_add_bo( box, box2 );
1504 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
1506 sout_buffer_t *p_buf;
1508 p_buf = sout_BufferNew( p_sout, box->i_buffer );
1509 if( box->i_buffer > 0 )
1511 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
1514 p_buf->i_size = box->i_buffer;
1519 static void box_send( sout_mux_t *p_mux, bo_t *box )
1521 sout_buffer_t *p_buf;
1523 p_buf = bo_to_sout( p_mux->p_sout, box );
1526 sout_AccessOutWrite( p_mux->p_access, p_buf );
1529 static int64_t get_timestamp()
1531 int64_t i_timestamp = 0;
1534 i_timestamp = time(NULL);
1535 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
1536 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60