1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003 VideoLAN
5 * $Id: mp4.c,v 1.4 2003/09/02 16:00:24 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;
96 mp4_stream_t **pp_streams;
100 typedef struct bo_t bo_t;
111 static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
112 static void bo_add_8 ( bo_t *, uint8_t );
113 static void bo_add_16be ( bo_t *, uint16_t );
114 static void bo_add_24be ( bo_t *, uint32_t );
115 static void bo_add_32be ( bo_t *, uint32_t );
116 static void bo_add_64be ( bo_t *, uint64_t );
117 static void bo_add_fourcc(bo_t *, char * );
118 static void bo_add_bo ( bo_t *, bo_t * );
119 static void bo_add_mem ( bo_t *, int , uint8_t * );
120 static void bo_add_descr ( bo_t *, uint8_t , uint32_t );
122 static void bo_fix_32be ( bo_t *, int , uint32_t );
124 static bo_t *box_new ( char *fcc );
125 static bo_t *box_full_new( char *fcc, uint8_t v, uint32_t f );
126 static void box_fix ( bo_t *box );
127 static void box_free ( bo_t *box );
128 static void box_gather ( bo_t *box, bo_t *box2 );
130 static void box_send( sout_mux_t *p_mux, bo_t *box );
132 static int64_t get_timestamp();
134 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box );
136 /*****************************************************************************
138 *****************************************************************************/
139 static int Open( vlc_object_t *p_this )
141 sout_mux_t *p_mux = (sout_mux_t*)p_this;
142 sout_mux_sys_t *p_sys;
145 p_sys = malloc( sizeof( sout_mux_sys_t ) );
147 p_sys->i_nb_streams = 0;
148 p_sys->pp_streams = NULL;
149 p_sys->i_mdat_pos = 0;
150 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
152 msg_Info( p_mux, "Open" );
154 p_mux->pf_capacity = Capability;
155 p_mux->pf_addstream = AddStream;
156 p_mux->pf_delstream = DelStream;
158 p_mux->p_sys = p_sys;
162 /* Now add ftyp header */
163 box = box_new( "ftyp" );
164 bo_add_fourcc( box, "isom" );
165 bo_add_32be ( box, 0 );
166 bo_add_fourcc( box, "mp41" );
169 p_sys->i_pos += box->i_buffer;
170 p_sys->i_mdat_pos = p_sys->i_pos;
172 box_send( p_mux, box );
175 * Quicktime actually doesn't like the 64 bits extensions !!! */
176 p_sys->b_mov = VLC_TRUE;
179 /* Now add mdat header */
180 box = box_new( "mdat" );
181 bo_add_64be ( box, 0 ); // enough to store an extended size
183 p_sys->i_pos += box->i_buffer;
185 box_send( p_mux, box );
190 static int GetDescrLength( int i_size )
192 if( i_size < 0x00000080 )
194 else if( i_size < 0x00004000 )
196 else if( i_size < 0x00200000 )
202 static bo_t *GetESDS( mp4_stream_t *p_stream )
206 int i_object_type_indication;
207 int i_decoder_specific_info_size;
209 if( p_stream->p_fmt->i_extra_data > 0 )
211 i_decoder_specific_info_size =
212 GetDescrLength( p_stream->p_fmt->i_extra_data );
216 i_decoder_specific_info_size = 0;
219 esds = box_full_new( "esds", 0, 0 );
222 bo_add_descr( esds, 0x03, 3 +
223 GetDescrLength( 13 + i_decoder_specific_info_size ) +
224 GetDescrLength( 1 ) );
225 bo_add_16be( esds, p_stream->i_track_id );
226 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
228 /* DecoderConfigDescr */
229 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
231 switch( p_stream->p_fmt->i_fourcc )
233 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
234 i_object_type_indication = 0x20;
236 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
237 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
238 i_object_type_indication = 0x60;
240 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
241 /* FIXME for mpeg2-aac == 0x66->0x68 */
242 i_object_type_indication = 0x40;
244 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
245 i_object_type_indication =
246 p_stream->p_fmt->i_sample_rate < 32000 ? 0x69 : 0x6b;
249 i_object_type_indication = 0x00;
252 i_stream_type = p_stream->p_fmt->i_cat == VIDEO_ES ? 0x04 : 0x05;
254 bo_add_8 ( esds, i_object_type_indication );
255 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
256 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
257 bo_add_32be( esds, 0x7fffffff ); // maxBitrate
258 bo_add_32be( esds, 0 ); // avgBitrate
260 if( p_stream->p_fmt->i_extra_data > 0 )
264 /* DecoderSpecificInfo */
265 bo_add_descr( esds, 0x05, p_stream->p_fmt->i_extra_data );
267 for( i = 0; i < p_stream->p_fmt->i_extra_data; i++ )
269 bo_add_8( esds, p_stream->p_fmt->p_extra_data[i] );
273 /* SL_Descr mandatory */
274 bo_add_descr( esds, 0x06, 1 );
275 bo_add_8 ( esds, 0x02 ); // sl_predefined
282 /*****************************************************************************
284 *****************************************************************************/
285 static uint32_t mvhd_matrix[9] =
286 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
288 static void Close( vlc_object_t * p_this )
290 sout_mux_t *p_mux = (sout_mux_t*)p_this;
291 sout_mux_sys_t *p_sys = p_mux->p_sys;
292 sout_buffer_t *p_hdr;
298 uint32_t i_movie_timescale = 90000;
299 int64_t i_movie_duration = 0;
300 msg_Info( p_mux, "Close" );
302 /* create general info */
303 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
305 mp4_stream_t *p_stream;
307 p_stream = p_sys->pp_streams[i_trak];
309 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
311 msg_Dbg( p_mux, "movie duration %ds",
312 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
314 i_movie_duration = i_movie_duration *
315 (int64_t)i_movie_timescale / (int64_t)1000000;
317 /* *** update mdat size *** */
318 bo_init ( &bo, 0, NULL, VLC_TRUE );
319 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
322 bo_add_32be ( &bo, 1 );
323 bo_add_fourcc( &bo, "mdat" );
324 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
328 bo_add_32be ( &bo, 8 );
329 bo_add_fourcc( &bo, "wide" );
330 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
331 bo_add_fourcc( &bo, "mdat" );
334 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
338 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
339 sout_AccessOutWrite( p_mux->p_access, p_hdr );
341 /* Now create header */
342 sout_AccessOutSeek( p_mux->p_access, p_sys->i_pos );
344 moov = box_new( "moov" );
346 /* *** add /moov/mvhd *** */
349 mvhd = box_full_new( "mvhd", 0, 0 );
350 bo_add_32be( mvhd, get_timestamp() ); // creation time
351 bo_add_32be( mvhd, get_timestamp() ); // modification time
352 bo_add_32be( mvhd, i_movie_timescale); // timescale
353 bo_add_32be( mvhd, i_movie_duration ); // duration
357 mvhd = box_full_new( "mvhd", 1, 0 );
358 bo_add_64be( mvhd, get_timestamp() ); // creation time
359 bo_add_64be( mvhd, get_timestamp() ); // modification time
360 bo_add_32be( mvhd, i_movie_timescale); // timescale
361 bo_add_64be( mvhd, i_movie_duration ); // duration
363 bo_add_32be( mvhd, 0x10000 ); // rate
364 bo_add_16be( mvhd, 0x100 ); // volume
365 bo_add_16be( mvhd, 0 ); // reserved
366 for( i = 0; i < 2; i++ )
368 bo_add_32be( mvhd, 0 ); // reserved
370 for( i = 0; i < 9; i++ )
372 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
374 for( i = 0; i < 6; i++ )
376 bo_add_32be( mvhd, 0 ); // pre-defined
379 /* Find the 1st track id */
380 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
382 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
384 if( p_stream->p_fmt->i_cat == AUDIO_ES ||
385 p_stream->p_fmt->i_cat == VIDEO_ES )
388 bo_add_32be( mvhd, p_stream->i_track_id ); // next-track-id
392 if( i_trak == p_sys->i_nb_streams ) /* Just for sanity reasons */
393 bo_add_32be( mvhd, 0xffffffff );
396 box_gather( moov, mvhd );
398 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
400 mp4_stream_t *p_stream;
401 uint32_t i_timescale;
402 uint32_t i_chunk_count;
417 p_stream = p_sys->pp_streams[i_trak];
419 if( p_stream->p_fmt->i_cat != AUDIO_ES &&
420 p_stream->p_fmt->i_cat != VIDEO_ES )
422 msg_Err( p_mux, "FIXME ignoring trak (noaudio&&novideo)" );
425 if( p_stream->p_fmt->i_cat == AUDIO_ES )
427 i_timescale = p_stream->p_fmt->i_sample_rate;
434 /* *** add /moov/trak *** */
435 trak = box_new( "trak" );
437 /* *** add /moov/trak/tkhd *** */
440 tkhd = box_full_new( "tkhd", 0, 1 );
441 bo_add_32be( tkhd, get_timestamp() ); // creation time
442 bo_add_32be( tkhd, get_timestamp() ); // modification time
443 bo_add_32be( tkhd, p_stream->i_track_id );
444 bo_add_32be( tkhd, 0 ); // reserved 0
445 bo_add_32be( tkhd, p_stream->i_duration *
446 (int64_t)i_movie_timescale /
447 (mtime_t)1000000 ); // duration
451 tkhd = box_full_new( "tkhd", 1, 1 );
452 bo_add_64be( tkhd, get_timestamp() ); // creation time
453 bo_add_64be( tkhd, get_timestamp() ); // modification time
454 bo_add_32be( tkhd, p_stream->i_track_id );
455 bo_add_32be( tkhd, 0 ); // reserved 0
456 bo_add_64be( tkhd, p_stream->i_duration *
457 (int64_t)i_movie_timescale /
458 (mtime_t)1000000 ); // duration
461 for( i = 0; i < 2; i++ )
463 bo_add_32be( tkhd, 0 ); // reserved
465 bo_add_16be( tkhd, 0 ); // layer
466 bo_add_16be( tkhd, 0 ); // pre-defined
467 bo_add_16be( tkhd, p_stream->p_fmt->i_cat == AUDIO_ES ? 0x100 : 0 ); // volume
468 bo_add_16be( tkhd, 0 ); // reserved
469 for( i = 0; i < 9; i++ )
471 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
473 if( p_stream->p_fmt->i_cat == AUDIO_ES )
475 bo_add_32be( tkhd, 0 ); // width (presentation)
476 bo_add_32be( tkhd, 0 ); // height(presentation)
480 bo_add_32be( tkhd, p_stream->p_fmt->i_width << 16 ); // width (presentation)
481 bo_add_32be( tkhd, p_stream->p_fmt->i_height << 16 ); // height(presentation)
484 box_gather( trak, tkhd );
486 /* *** add /moov/trak/mdia *** */
487 mdia = box_new( "mdia" );
492 mdhd = box_full_new( "mdhd", 0, 0 );
493 bo_add_32be( mdhd, get_timestamp() ); // creation time
494 bo_add_32be( mdhd, get_timestamp() ); // modification time
495 bo_add_32be( mdhd, i_timescale); // timescale
496 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
497 (mtime_t)1000000 ); // duration
501 mdhd = box_full_new( "mdhd", 1, 0 );
502 bo_add_64be( mdhd, get_timestamp() ); // creation time
503 bo_add_64be( mdhd, get_timestamp() ); // modification time
504 bo_add_32be( mdhd, i_timescale); // timescale
505 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
506 (mtime_t)1000000 ); // duration
509 bo_add_16be( mdhd, 0 ); // language FIXME
510 bo_add_16be( mdhd, 0 ); // predefined
512 box_gather( mdia, mdhd );
514 /* handler reference */
515 hdlr = box_full_new( "hdlr", 0, 0 );
517 bo_add_fourcc( hdlr, "mhlr" ); // media handler
518 if( p_stream->p_fmt->i_cat == AUDIO_ES )
520 bo_add_fourcc( hdlr, "soun" );
524 bo_add_fourcc( hdlr, "vide" );
527 bo_add_32be( hdlr, 0 ); // reserved
528 bo_add_32be( hdlr, 0 ); // reserved
529 bo_add_32be( hdlr, 0 ); // reserved
531 if( p_stream->p_fmt->i_cat == AUDIO_ES )
533 bo_add_mem( hdlr, 13, "SoundHandler" );
537 bo_add_mem( hdlr, 13, "VideoHandler" );
541 box_gather( mdia, hdlr );
544 minf = box_new( "minf" );
547 if( p_stream->p_fmt->i_cat == AUDIO_ES )
551 smhd = box_full_new( "smhd", 0, 0 );
552 bo_add_16be( smhd, 0 ); // balance
553 bo_add_16be( smhd, 0 ); // reserved
556 box_gather( minf, smhd );
558 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
562 vmhd = box_full_new( "vmhd", 0, 1 );
563 bo_add_16be( vmhd, 0 ); // graphicsmode
564 for( i = 0; i < 3; i++ )
566 bo_add_16be( vmhd, 0 ); // opcolor
570 box_gather( minf, vmhd );
574 dinf = box_new( "dinf" );
575 dref = box_full_new( "dref", 0, 0 );
576 bo_add_32be( dref, 1 );
577 url = box_full_new( "url ", 0, 0x01 );
579 box_gather( dref, url );
581 box_gather( dinf, dref );
583 /* append dinf to mdia */
585 box_gather( minf, dinf );
588 stbl = box_new( "stbl" );
589 stsd = box_full_new( "stsd", 0, 0 );
590 bo_add_32be( stsd, 1 );
592 if( p_stream->p_fmt->i_cat == AUDIO_ES )
597 vlc_bool_t b_mpeg4_hdr;
599 switch( p_stream->p_fmt->i_fourcc )
601 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
602 memcpy( fcc, "mp4a", 4 );
603 b_mpeg4_hdr = VLC_TRUE;
606 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
608 memcpy( fcc, ".mp3 ", 4 );
610 memcpy( fcc, "mp4a", 4 );
611 b_mpeg4_hdr = VLC_FALSE;
615 memcpy( fcc, (char*)&p_stream->p_fmt->i_fourcc, 4 );
616 b_mpeg4_hdr = VLC_FALSE;
620 soun = box_new( fcc );
621 for( i = 0; i < 6; i++ )
623 bo_add_8( soun, 0 ); // reserved;
625 bo_add_16be( soun, 1 ); // data-reference-index
627 /* SoundDescriptionV1 */
628 bo_add_16be( soun, 1 ); // version;
629 bo_add_16be( soun, 0 ); // revision level (0)
630 bo_add_32be( soun, 0 ); // vendor
631 bo_add_16be( soun, p_stream->p_fmt->i_channels ); // channel-count
632 bo_add_16be( soun, 16); // FIXME sample size
633 bo_add_16be( soun, -2 ); // compression id
634 bo_add_16be( soun, 0 ); // packet size (0)
635 bo_add_16be( soun, p_stream->p_fmt->i_sample_rate ); // sampleratehi
636 bo_add_16be( soun, 0 ); // sampleratelo
638 /* add an ES Descriptor */
643 esds = GetESDS( p_stream );
646 box_gather( soun, esds );
650 box_gather( stsd, soun );
652 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
657 vlc_bool_t b_mpeg4_hdr;
659 switch( p_stream->p_fmt->i_fourcc )
661 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
662 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
663 memcpy( fcc, "mp4v", 4 );
664 b_mpeg4_hdr = VLC_TRUE;
667 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
668 memcpy( fcc, "mjpa", 4 );
669 b_mpeg4_hdr = VLC_FALSE;
673 memcpy( fcc, (char*)&p_stream->p_fmt->i_fourcc, 4 );
674 b_mpeg4_hdr = VLC_FALSE;
678 vide = box_new( fcc );
679 for( i = 0; i < 6; i++ )
681 bo_add_8( vide, 0 ); // reserved;
683 bo_add_16be( vide, 1 ); // data-reference-index
685 bo_add_16be( vide, 0 ); // predefined;
686 bo_add_16be( vide, 0 ); // reserved;
687 for( i = 0; i < 3; i++ )
689 bo_add_32be( vide, 0 ); // predefined;
692 bo_add_16be( vide, p_stream->p_fmt->i_width ); // i_width
693 bo_add_16be( vide, p_stream->p_fmt->i_height ); // i_height
695 bo_add_32be( vide, 0x00480000 ); // h 72dpi
696 bo_add_32be( vide, 0x00480000 ); // v 72dpi
698 bo_add_32be( vide, 0 ); // data size, always 0
699 bo_add_16be( vide, 1 ); // frames count per sample
702 for( i = 0; i < 32; i++ )
707 bo_add_16be( vide, 0x18 ); // depth
708 bo_add_16be( vide, 0xffff ); // predefined
710 /* add an ES Descriptor */
715 esds = GetESDS( p_stream );
718 box_gather( vide, esds );
722 box_gather( stsd, vide );
725 /* append stsd to stbl */
727 box_gather( stbl, stsd );
729 /* we will create chunk table and stsc table FIXME optim stsc table FIXME */
731 for( i = 0; i < p_stream->i_entry_count; )
733 while( i < p_stream->i_entry_count )
735 if( i + 1 < p_stream->i_entry_count &&
736 p_stream->entry[i].i_pos +
737 p_stream->entry[i].i_size != p_stream->entry[i + 1].i_pos )
748 /* chunk offset table */
749 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
751 /* 64 bits version */
755 unsigned int i_chunk;
757 msg_Dbg( p_mux, "creating %d chunk (co64)", i_chunk_count );
759 co64 = box_full_new( "co64", 0, 0 );
760 bo_add_32be( co64, i_chunk_count );
762 stsc = box_full_new( "stsc", 0, 0 );
763 bo_add_32be( stsc, i_chunk_count ); // entry-count
764 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
767 bo_add_64be( co64, p_stream->entry[i].i_pos );
771 while( i < p_stream->i_entry_count )
773 if( i + 1 < p_stream->i_entry_count &&
774 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
775 != p_stream->entry[i + 1].i_pos )
783 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
784 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
785 bo_add_32be( stsc, 1 ); // sample-descr-index
787 /* append co64 to stbl */
789 box_gather( stbl, co64 );
791 /* append stsc to stbl */
793 box_gather( stbl, stsc );
797 /* 32 bits version */
801 unsigned int i_chunk;
803 msg_Dbg( p_mux, "creating %d chunk (stco)", i_chunk_count );
805 stco = box_full_new( "stco", 0, 0 );
806 bo_add_32be( stco, i_chunk_count );
808 stsc = box_full_new( "stsc", 0, 0 );
809 bo_add_32be( stsc, i_chunk_count ); // entry-count
810 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
813 bo_add_32be( stco, p_stream->entry[i].i_pos );
817 while( i < p_stream->i_entry_count )
819 if( i + 1 < p_stream->i_entry_count &&
820 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
821 != p_stream->entry[i + 1].i_pos )
829 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
830 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
831 bo_add_32be( stsc, 1 ); // sample-descr-index
833 /* append stco to stbl */
835 box_gather( stbl, stco );
837 /* append stsc to stbl */
839 box_gather( stbl, stsc );
844 stts = box_full_new( "stts", 0, 0 );
845 bo_add_32be( stts, 0 ); // fixed latter
846 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
852 i_delta = p_stream->entry[i].i_length;
854 while( i < p_stream->i_entry_count )
856 if( i + 1 < p_stream->i_entry_count &&
857 p_stream->entry[i + 1].i_length != i_delta )
866 bo_add_32be( stts, i - i_first ); // sample-count
867 bo_add_32be( stts, i_delta * (int64_t)i_timescale /
868 (int64_t)1000000 ); // sample-delta
870 bo_fix_32be( stts, 12, i_index );
872 /* append stts to stbl */
874 box_gather( stbl, stts );
876 /* FIXME add ctts ?? FIXME */
878 stsz = box_full_new( "stsz", 0, 0 );
879 bo_add_32be( stsz, 0 ); // sample-size
880 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
881 for( i = 0; i < p_stream->i_entry_count; i++ )
883 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
885 /* append stsz to stbl */
887 box_gather( stbl, stsz );
889 /* append stbl to minf */
891 box_gather( minf, stbl );
893 /* append minf to mdia */
895 box_gather( mdia, minf );
897 /* append mdia to trak */
899 box_gather( trak, mdia );
901 /* append trak to moov */
903 box_gather( moov, trak );
907 box_send( p_mux, moov );
909 /* *** release memory *** */
910 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
912 mp4_stream_t *p_stream;
914 p_stream = p_sys->pp_streams[i_trak];
916 if( p_stream->p_fmt->p_extra_data )
918 free( p_stream->p_fmt->p_extra_data );
920 free( p_stream->p_fmt );
921 free( p_stream->entry );
927 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
932 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
933 *(vlc_bool_t*)p_answer = VLC_FALSE;
934 return( SOUT_MUX_CAP_ERR_OK );
936 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
940 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
942 sout_mux_sys_t *p_sys = p_mux->p_sys;
943 mp4_stream_t *p_stream;
945 switch( p_input->p_fmt->i_fourcc )
947 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
948 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
949 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
950 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
951 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
952 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
955 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
956 (char*)&p_input->p_fmt->i_fourcc );
960 p_stream = malloc( sizeof( mp4_stream_t ) );
961 p_stream->p_fmt = malloc( sizeof( sout_format_t ) );
962 memcpy( p_stream->p_fmt, p_input->p_fmt, sizeof( sout_format_t ) );
963 if( p_stream->p_fmt->i_extra_data )
965 p_stream->p_fmt->p_extra_data =
966 malloc( p_stream->p_fmt->i_extra_data );
967 memcpy( p_stream->p_fmt->p_extra_data,
968 p_input->p_fmt->p_extra_data,
969 p_input->p_fmt->i_extra_data );
971 p_stream->i_track_id = p_sys->i_nb_streams + 1;
972 p_stream->i_entry_count = 0;
973 p_stream->i_entry_max = 1000;
975 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
976 p_stream->i_duration = 0;
978 p_input->p_sys = p_stream;
980 msg_Dbg( p_mux, "adding input" );
982 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
983 return( VLC_SUCCESS );
986 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
988 msg_Dbg( p_mux, "removing input" );
992 /****************************************************************************/
994 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
1000 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
1002 sout_fifo_t *p_fifo;
1004 p_fifo = p_mux->pp_inputs[i]->p_fifo;
1006 if( p_fifo->i_depth > 1 )
1008 sout_buffer_t *p_buf;
1010 p_buf = sout_FifoShow( p_fifo );
1011 if( i_stream < 0 || p_buf->i_dts < i_dts )
1013 i_dts = p_buf->i_dts;
1019 return( -1 ); // wait that all fifo have at least 2 packets
1024 *pi_stream = i_stream;
1033 static int Mux( sout_mux_t *p_mux )
1035 sout_mux_sys_t *p_sys = p_mux->p_sys;
1039 sout_input_t *p_input;
1041 mp4_stream_t *p_stream;
1042 sout_buffer_t *p_data;
1045 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
1047 return( VLC_SUCCESS );
1051 p_input = p_mux->pp_inputs[i_stream];
1052 p_stream = (mp4_stream_t*)p_input->p_sys;
1054 p_data = sout_FifoGet( p_input->p_fifo );
1055 //msg_Dbg( p_mux, "stream=%d size=%6d pos=%8lld", i_stream, p_data->i_size, p_sys->i_pos );
1057 /* add index entry */
1058 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
1059 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_size;
1060 p_stream->entry[p_stream->i_entry_count].i_pts = p_data->i_pts;
1061 p_stream->entry[p_stream->i_entry_count].i_dts = p_data->i_dts;
1062 p_stream->entry[p_stream->i_entry_count].i_length=
1063 __MAX( p_data->i_length, 0 );
1065 p_stream->i_entry_count++;
1066 if( p_stream->i_entry_count >= p_stream->i_entry_max )
1068 p_stream->i_entry_max += 1000;
1070 realloc( p_stream->entry,
1071 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
1075 p_stream->i_duration += __MAX( p_data->i_length, 0 );
1076 p_sys->i_pos += p_data->i_size;
1079 sout_AccessOutWrite( p_mux->p_access, p_data );
1082 return( VLC_SUCCESS );
1086 /****************************************************************************/
1088 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1093 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1094 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1098 p_bo->i_buffer_size = i_size;
1099 p_bo->p_buffer = p_buffer;
1102 p_bo->b_grow = b_grow;
1106 static void bo_add_8( bo_t *p_bo, uint8_t i )
1108 if( p_bo->i_buffer < p_bo->i_buffer_size )
1110 p_bo->p_buffer[p_bo->i_buffer] = i;
1112 else if( p_bo->b_grow )
1114 p_bo->i_buffer_size += 1024;
1115 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1117 p_bo->p_buffer[p_bo->i_buffer] = i;
1123 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1125 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1126 bo_add_8( p_bo, i &0xff );
1129 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1131 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1132 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1133 bo_add_8( p_bo, ( i &0xff ) );
1135 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1137 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1138 bo_add_16be( p_bo, i &0xffff );
1141 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1143 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1144 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1145 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1146 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1149 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1151 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1152 bo_add_32be( p_bo, i &0xffffffff );
1155 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1157 bo_add_8( p_bo, fcc[0] );
1158 bo_add_8( p_bo, fcc[1] );
1159 bo_add_8( p_bo, fcc[2] );
1160 bo_add_8( p_bo, fcc[3] );
1163 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1167 for( i = 0; i < i_size; i++ )
1169 bo_add_8( p_bo, p_mem[i] );
1173 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
1179 vals[3] = (unsigned char)(i_length & 0x7f);
1181 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
1183 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
1185 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
1187 bo_add_8( p_bo, tag );
1189 if( i_size < 0x00000080 )
1191 bo_add_8( p_bo, vals[3] );
1193 else if( i_size < 0x00004000 )
1195 bo_add_8( p_bo, vals[2] );
1196 bo_add_8( p_bo, vals[3] );
1198 else if( i_size < 0x00200000 )
1200 bo_add_8( p_bo, vals[1] );
1201 bo_add_8( p_bo, vals[2] );
1202 bo_add_8( p_bo, vals[3] );
1204 else if( i_size < 0x10000000 )
1206 bo_add_8( p_bo, vals[0] );
1207 bo_add_8( p_bo, vals[1] );
1208 bo_add_8( p_bo, vals[2] );
1209 bo_add_8( p_bo, vals[3] );
1213 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1217 for( i = 0; i < p_bo2->i_buffer; i++ )
1219 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1223 static bo_t * box_new( char *fcc )
1227 if( ( box = malloc( sizeof( bo_t ) ) ) )
1229 bo_init( box, 0, NULL, VLC_TRUE );
1231 bo_add_32be ( box, 0 );
1232 bo_add_fourcc( box, fcc );
1238 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
1242 if( ( box = malloc( sizeof( bo_t ) ) ) )
1244 bo_init( box, 0, NULL, VLC_TRUE );
1246 bo_add_32be ( box, 0 );
1247 bo_add_fourcc( box, fcc );
1248 bo_add_8 ( box, v );
1249 bo_add_24be ( box, f );
1255 static void box_fix( bo_t *box )
1259 memcpy( &box_tmp, box, sizeof( bo_t ) );
1261 box_tmp.i_buffer = 0;
1262 bo_add_32be( &box_tmp, box->i_buffer );
1265 static void box_free( bo_t *box )
1269 free( box->p_buffer );
1275 static void box_gather ( bo_t *box, bo_t *box2 )
1277 bo_add_bo( box, box2 );
1282 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
1284 sout_buffer_t *p_buf;
1286 p_buf = sout_BufferNew( p_sout, box->i_buffer );
1287 if( box->i_buffer > 0 )
1289 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
1292 p_buf->i_size = box->i_buffer;
1297 static void box_send( sout_mux_t *p_mux, bo_t *box )
1299 sout_buffer_t *p_buf;
1301 p_buf = bo_to_sout( p_mux->p_sout, box );
1304 sout_AccessOutWrite( p_mux->p_access, p_buf );
1307 static int64_t get_timestamp()
1309 int64_t i_timestamp = 0;
1312 i_timestamp = time(NULL);
1313 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
1314 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60