1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003 VideoLAN
5 * $Id: mp4.c,v 1.3 2003/08/01 20:06:43 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 * );
121 static void bo_fix_32be ( bo_t *, int , uint32_t );
123 static bo_t *box_new ( char *fcc );
124 static bo_t *box_full_new( char *fcc, uint8_t v, uint32_t f );
125 static void box_fix ( bo_t *box );
126 static void box_free ( bo_t *box );
127 static void box_gather ( bo_t *box, bo_t *box2 );
129 static void box_send( sout_mux_t *p_mux, bo_t *box );
131 static int64_t get_timestamp();
133 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box );
135 /*****************************************************************************
137 *****************************************************************************/
138 static int Open( vlc_object_t *p_this )
140 sout_mux_t *p_mux = (sout_mux_t*)p_this;
141 sout_mux_sys_t *p_sys;
144 p_sys = malloc( sizeof( sout_mux_sys_t ) );
146 p_sys->i_nb_streams = 0;
147 p_sys->pp_streams = NULL;
148 p_sys->i_mdat_pos = 0;
149 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
151 msg_Info( p_mux, "Open" );
153 p_mux->pf_capacity = Capability;
154 p_mux->pf_addstream = AddStream;
155 p_mux->pf_delstream = DelStream;
157 p_mux->p_sys = p_sys;
161 /* Now add ftyp header */
162 box = box_new( "ftyp" );
163 bo_add_fourcc( box, "isom" );
164 bo_add_32be ( box, 0 );
165 bo_add_fourcc( box, "mp41" );
168 p_sys->i_pos += box->i_buffer;
169 p_sys->i_mdat_pos = p_sys->i_pos;
171 box_send( p_mux, box );
174 /* Now add mdat header */
175 box = box_new( "mdat" );
176 bo_add_64be ( box, 0 ); // enough to store an extended size
178 p_sys->i_pos += box->i_buffer;
180 box_send( p_mux, box );
185 static uint32_t GetDescriptorLength24b( int i_length )
187 uint32_t i_l1, i_l2, i_l3;
189 i_l1 = i_length&0x7f;
190 i_l2 = ( i_length >> 7 )&0x7f;
191 i_l3 = ( i_length >> 14 )&0x7f;
193 return( 0x808000 | ( i_l3 << 16 ) | ( i_l2 << 8 ) | i_l1 );
196 static bo_t *GetESDS( mp4_stream_t *p_stream )
200 int i_object_type_indication;
201 int i_decoder_specific_info_size;
203 if( p_stream->p_fmt->i_extra_data > 0 )
205 i_decoder_specific_info_size = p_stream->p_fmt->i_extra_data + 4;
209 i_decoder_specific_info_size = 0;
212 esds = box_full_new( "esds", 0, 0 );
214 bo_add_8 ( esds, 0x03 ); // ES_DescrTag
216 GetDescriptorLength24b( 25 + i_decoder_specific_info_size ) );
217 bo_add_16be( esds, p_stream->i_track_id );
218 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
220 bo_add_8 ( esds, 0x04 ); // DecoderConfigDescrTag
222 GetDescriptorLength24b( 13 + i_decoder_specific_info_size ) );
224 switch( p_stream->p_fmt->i_fourcc )
226 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
227 i_object_type_indication = 0x20;
229 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
230 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
231 i_object_type_indication = 0x60;
233 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
234 /* FIXME for mpeg2-aac == 0x66->0x68 */
235 i_object_type_indication = 0x40;
237 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
238 i_object_type_indication =
239 p_stream->p_fmt->i_sample_rate < 32000 ? 0x69 : 0x6b;
242 i_object_type_indication = 0x00;
245 i_stream_type = p_stream->p_fmt->i_cat == VIDEO_ES ? 0x04 : 0x05;
247 bo_add_8 ( esds, i_object_type_indication );
248 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
249 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
250 bo_add_32be( esds, 0x7fffffff ); // maxBitrate
251 bo_add_32be( esds, 0 ); // avgBitrate
253 if( p_stream->p_fmt->i_extra_data > 0 )
256 bo_add_8 ( esds, 0x05 ); // DecoderSpecificInfo
258 GetDescriptorLength24b( p_stream->p_fmt->i_extra_data ) );
260 for( i = 0; i < p_stream->p_fmt->i_extra_data; i++ )
262 bo_add_8( esds, p_stream->p_fmt->p_extra_data[i] );
266 /* SL_Descr mandatory */
267 bo_add_8 ( esds, 0x06 ); // SLConfigDescriptorTag
268 bo_add_24be( esds, GetDescriptorLength24b( 1 ) );
269 bo_add_8 ( esds, 0x02 ); // sl_predefined
276 /*****************************************************************************
278 *****************************************************************************/
279 static uint32_t mvhd_matrix[9] =
280 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
282 static void Close( vlc_object_t * p_this )
284 sout_mux_t *p_mux = (sout_mux_t*)p_this;
285 sout_mux_sys_t *p_sys = p_mux->p_sys;
286 sout_buffer_t *p_hdr;
292 uint32_t i_movie_timescale = 90000;
293 int64_t i_movie_duration = 0;
294 msg_Info( p_mux, "Close" );
296 /* create general info */
297 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
299 mp4_stream_t *p_stream;
301 p_stream = p_sys->pp_streams[i_trak];
303 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
305 msg_Dbg( p_mux, "movie duration %ds",
306 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
308 i_movie_duration = i_movie_duration *
309 (int64_t)i_movie_timescale / (int64_t)1000000;
311 /* *** update mdat size *** */
312 bo_init ( &bo, 0, NULL, VLC_TRUE );
313 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
316 bo_add_32be ( &bo, 1 );
317 bo_add_fourcc( &bo, "mdat" );
318 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
322 bo_add_32be ( &bo, 8 );
323 bo_add_fourcc( &bo, "wide" );
324 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
325 bo_add_fourcc( &bo, "mdat" );
328 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
332 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
333 sout_AccessOutWrite( p_mux->p_access, p_hdr );
335 /* Now create header */
336 sout_AccessOutSeek( p_mux->p_access, p_sys->i_pos );
338 moov = box_new( "moov" );
340 /* *** add /moov/mvhd *** */
343 mvhd = box_full_new( "mvhd", 0, 0 );
344 bo_add_32be( mvhd, get_timestamp() ); // creation time
345 bo_add_32be( mvhd, get_timestamp() ); // modification time
346 bo_add_32be( mvhd, i_movie_timescale); // timescale
347 bo_add_32be( mvhd, i_movie_duration ); // duration
351 mvhd = box_full_new( "mvhd", 1, 0 );
352 bo_add_64be( mvhd, get_timestamp() ); // creation time
353 bo_add_64be( mvhd, get_timestamp() ); // modification time
354 bo_add_32be( mvhd, i_movie_timescale); // timescale
355 bo_add_64be( mvhd, i_movie_duration ); // duration
357 bo_add_32be( mvhd, 0x10000 ); // rate
358 bo_add_16be( mvhd, 0x100 ); // volume
359 bo_add_16be( mvhd, 0 ); // reserved
360 for( i = 0; i < 2; i++ )
362 bo_add_32be( mvhd, 0 ); // reserved
364 for( i = 0; i < 9; i++ )
366 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
368 for( i = 0; i < 6; i++ )
370 bo_add_32be( mvhd, 0 ); // pre-defined
373 /* Find the 1st track id */
374 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
376 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
378 if( p_stream->p_fmt->i_cat == AUDIO_ES ||
379 p_stream->p_fmt->i_cat == VIDEO_ES )
382 bo_add_32be( mvhd, p_stream->i_track_id ); // next-track-id
386 if( i_trak == p_sys->i_nb_streams ) /* Just for sanity reasons */
387 bo_add_32be( mvhd, 0xffffffff );
390 box_gather( moov, mvhd );
392 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
394 mp4_stream_t *p_stream;
395 uint32_t i_timescale;
396 uint32_t i_chunk_count;
411 p_stream = p_sys->pp_streams[i_trak];
413 if( p_stream->p_fmt->i_cat != AUDIO_ES &&
414 p_stream->p_fmt->i_cat != VIDEO_ES )
416 msg_Err( p_mux, "FIXME ignoring trak (noaudio&&novideo)" );
419 if( p_stream->p_fmt->i_cat == AUDIO_ES )
421 i_timescale = p_stream->p_fmt->i_sample_rate;
428 /* *** add /moov/trak *** */
429 trak = box_new( "trak" );
431 /* *** add /moov/trak/tkhd *** */
434 tkhd = box_full_new( "tkhd", 0, 1 );
435 bo_add_32be( tkhd, get_timestamp() ); // creation time
436 bo_add_32be( tkhd, get_timestamp() ); // modification time
437 bo_add_32be( tkhd, p_stream->i_track_id );
438 bo_add_32be( tkhd, 0 ); // reserved 0
439 bo_add_32be( tkhd, p_stream->i_duration *
440 (int64_t)i_movie_timescale /
441 (mtime_t)1000000 ); // duration
445 tkhd = box_full_new( "tkhd", 1, 1 );
446 bo_add_64be( tkhd, get_timestamp() ); // creation time
447 bo_add_64be( tkhd, get_timestamp() ); // modification time
448 bo_add_32be( tkhd, p_stream->i_track_id );
449 bo_add_32be( tkhd, 0 ); // reserved 0
450 bo_add_64be( tkhd, p_stream->i_duration *
451 (int64_t)i_movie_timescale /
452 (mtime_t)1000000 ); // duration
455 for( i = 0; i < 2; i++ )
457 bo_add_32be( tkhd, 0 ); // reserved
459 bo_add_16be( tkhd, 0 ); // layer
460 bo_add_16be( tkhd, 0 ); // pre-defined
461 bo_add_16be( tkhd, p_stream->p_fmt->i_cat == AUDIO_ES ? 0x100 : 0 ); // volume
462 bo_add_16be( tkhd, 0 ); // reserved
463 for( i = 0; i < 9; i++ )
465 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
467 if( p_stream->p_fmt->i_cat == AUDIO_ES )
469 bo_add_32be( tkhd, 0 ); // width (presentation)
470 bo_add_32be( tkhd, 0 ); // height(presentation)
474 bo_add_32be( tkhd, p_stream->p_fmt->i_width << 16 ); // width (presentation)
475 bo_add_32be( tkhd, p_stream->p_fmt->i_height << 16 ); // height(presentation)
478 box_gather( trak, tkhd );
480 /* *** add /moov/trak/mdia *** */
481 mdia = box_new( "mdia" );
486 mdhd = box_full_new( "mdhd", 0, 0 );
487 bo_add_32be( mdhd, get_timestamp() ); // creation time
488 bo_add_32be( mdhd, get_timestamp() ); // modification time
489 bo_add_32be( mdhd, i_timescale); // timescale
490 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
491 (mtime_t)1000000 ); // duration
495 mdhd = box_full_new( "mdhd", 1, 0 );
496 bo_add_64be( mdhd, get_timestamp() ); // creation time
497 bo_add_64be( mdhd, get_timestamp() ); // modification time
498 bo_add_32be( mdhd, i_timescale); // timescale
499 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
500 (mtime_t)1000000 ); // duration
503 bo_add_16be( mdhd, 0 ); // language FIXME
504 bo_add_16be( mdhd, 0 ); // predefined
506 box_gather( mdia, mdhd );
509 hdlr = box_full_new( "hdlr", 0, 0 );
510 bo_add_32be( hdlr, 0 ); // predefined
511 if( p_stream->p_fmt->i_cat == AUDIO_ES )
513 bo_add_fourcc( hdlr, "soun" );
517 bo_add_fourcc( hdlr, "vide" );
519 for( i = 0; i < 3; i++ )
521 bo_add_32be( hdlr, 0 ); // reserved
523 bo_add_mem( hdlr, 2, "?" );
526 box_gather( mdia, hdlr );
529 minf = box_new( "minf" );
532 if( p_stream->p_fmt->i_cat == AUDIO_ES )
536 smhd = box_full_new( "smhd", 0, 0 );
537 bo_add_16be( smhd, 0 ); // balance
538 bo_add_16be( smhd, 0 ); // reserved
541 box_gather( minf, smhd );
543 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
547 vmhd = box_full_new( "vmhd", 0, 1 );
548 bo_add_16be( vmhd, 0 ); // graphicsmode
549 for( i = 0; i < 3; i++ )
551 bo_add_16be( vmhd, 0 ); // opcolor
555 box_gather( minf, vmhd );
559 dinf = box_new( "dinf" );
560 dref = box_full_new( "dref", 0, 0 );
561 bo_add_32be( dref, 1 );
562 url = box_full_new( "url ", 0, 0x01 );
564 box_gather( dref, url );
566 box_gather( dinf, dref );
568 /* append dinf to mdia */
570 box_gather( minf, dinf );
573 stbl = box_new( "stbl" );
574 stsd = box_full_new( "stsd", 0, 0 );
575 bo_add_32be( stsd, 1 );
577 if( p_stream->p_fmt->i_cat == AUDIO_ES )
582 vlc_bool_t b_mpeg4_hdr;
584 switch( p_stream->p_fmt->i_fourcc )
586 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
587 memcpy( fcc, "mp4a", 4 );
588 b_mpeg4_hdr = VLC_TRUE;
591 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
593 memcpy( fcc, ".mp3 ", 4 );
595 memcpy( fcc, "mp4a", 4 );
596 b_mpeg4_hdr = VLC_FALSE;
600 memcpy( fcc, (char*)&p_stream->p_fmt->i_fourcc, 4 );
601 b_mpeg4_hdr = VLC_FALSE;
605 soun = box_new( fcc );
606 for( i = 0; i < 6; i++ )
608 bo_add_8( soun, 0 ); // reserved;
610 bo_add_16be( soun, 1 ); // data-reference-index
611 bo_add_32be( soun, 0 ); // reserved;
612 bo_add_32be( soun, 0 ); // reserved;
613 bo_add_16be( soun, p_stream->p_fmt->i_channels ); // channel-count
614 bo_add_16be( soun, 16); // FIXME sample size
615 bo_add_16be( soun, 0 ); // predefined
616 bo_add_16be( soun, 0 ); // reserved
617 bo_add_16be( soun, p_stream->p_fmt->i_sample_rate ); // sampleratehi
618 bo_add_16be( soun, 0 ); // sampleratelo
620 /* add an ES Descriptor */
625 esds = GetESDS( p_stream );
628 box_gather( soun, esds );
632 box_gather( stsd, soun );
634 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
639 vlc_bool_t b_mpeg4_hdr;
641 switch( p_stream->p_fmt->i_fourcc )
643 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
644 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
645 memcpy( fcc, "mp4v", 4 );
646 b_mpeg4_hdr = VLC_TRUE;
649 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
650 memcpy( fcc, "mjpa", 4 );
651 b_mpeg4_hdr = VLC_FALSE;
655 memcpy( fcc, (char*)&p_stream->p_fmt->i_fourcc, 4 );
656 b_mpeg4_hdr = VLC_FALSE;
660 vide = box_new( fcc );
661 for( i = 0; i < 6; i++ )
663 bo_add_8( vide, 0 ); // reserved;
665 bo_add_16be( vide, 1 ); // data-reference-index
667 bo_add_16be( vide, 0 ); // predefined;
668 bo_add_16be( vide, 0 ); // reserved;
669 for( i = 0; i < 3; i++ )
671 bo_add_32be( vide, 0 ); // predefined;
674 bo_add_16be( vide, p_stream->p_fmt->i_width ); // i_width
675 bo_add_16be( vide, p_stream->p_fmt->i_height ); // i_height
677 bo_add_32be( vide, 0x00480000 ); // h 72dpi
678 bo_add_32be( vide, 0x00480000 ); // v 72dpi
680 bo_add_32be( vide, 0 ); // reserved
681 bo_add_16be( vide, 1 ); // predefined
682 for( i = 0; i < 32; i++ )
684 bo_add_8( vide, 0 ); // compressor name;
686 bo_add_16be( vide, 0x18 ); // depth
687 bo_add_16be( vide, 0xffff ); // predefined
689 /* add an ES Descriptor */
694 esds = GetESDS( p_stream );
697 box_gather( vide, esds );
701 box_gather( stsd, vide );
704 /* append stsd to stbl */
706 box_gather( stbl, stsd );
708 /* we will create chunk table and stsc table FIXME optim stsc table FIXME */
710 for( i = 0; i < p_stream->i_entry_count; )
712 while( i < p_stream->i_entry_count )
714 if( i + 1 < p_stream->i_entry_count &&
715 p_stream->entry[i].i_pos +
716 p_stream->entry[i].i_size != p_stream->entry[i + 1].i_pos )
727 // if( p_sys->i_pos >= 0xffffffff )
732 unsigned int i_chunk;
734 msg_Dbg( p_mux, "creating %d chunk (co64)", i_chunk_count );
736 co64 = box_full_new( "co64", 0, 0 );
737 bo_add_32be( co64, i_chunk_count );
739 stsc = box_full_new( "stsc", 0, 0 );
740 bo_add_32be( stsc, i_chunk_count ); // entry-count
741 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
744 bo_add_64be( co64, p_stream->entry[i].i_pos );
748 while( i < p_stream->i_entry_count )
750 if( i + 1 < p_stream->i_entry_count && p_stream->entry[i].i_pos + p_stream->entry[i].i_size != p_stream->entry[i + 1].i_pos )
758 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
759 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
760 bo_add_32be( stsc, 1 ); // sample-descr-index
762 /* append co64 to stbl */
764 box_gather( stbl, co64 );
766 /* append stsc to stbl */
768 box_gather( stbl, stsc );
772 // FIXME implement it
777 stts = box_full_new( "stts", 0, 0 );
778 bo_add_32be( stts, 0 ); // fixed latter
779 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
785 i_delta = p_stream->entry[i].i_length;
787 while( i < p_stream->i_entry_count )
789 if( i + 1 < p_stream->i_entry_count &&
790 p_stream->entry[i + 1].i_length != i_delta )
799 bo_add_32be( stts, i - i_first ); // sample-count
800 bo_add_32be( stts, i_delta * (int64_t)i_timescale /
801 (int64_t)1000000 ); // sample-delta
803 bo_fix_32be( stts, 12, i_index );
805 /* append stts to stbl */
807 box_gather( stbl, stts );
809 /* FIXME add ctts ?? FIXME */
811 stsz = box_full_new( "stsz", 0, 0 );
812 bo_add_32be( stsz, 0 ); // sample-size
813 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
814 for( i = 0; i < p_stream->i_entry_count; i++ )
816 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
818 /* append stsz to stbl */
820 box_gather( stbl, stsz );
822 /* append stbl to minf */
824 box_gather( minf, stbl );
826 /* append minf to mdia */
828 box_gather( mdia, minf );
830 /* append mdia to trak */
832 box_gather( trak, mdia );
834 /* append trak to moov */
836 box_gather( moov, trak );
840 box_send( p_mux, moov );
842 /* *** release memory *** */
843 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
845 mp4_stream_t *p_stream;
847 p_stream = p_sys->pp_streams[i_trak];
849 if( p_stream->p_fmt->p_extra_data )
851 free( p_stream->p_fmt->p_extra_data );
853 free( p_stream->p_fmt );
854 free( p_stream->entry );
860 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
865 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
866 *(vlc_bool_t*)p_answer = VLC_FALSE;
867 return( SOUT_MUX_CAP_ERR_OK );
869 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
873 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
875 sout_mux_sys_t *p_sys = p_mux->p_sys;
876 mp4_stream_t *p_stream;
878 switch( p_input->p_fmt->i_fourcc )
880 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
881 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
882 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
883 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
884 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
885 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
888 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
889 (char*)&p_input->p_fmt->i_fourcc );
893 p_stream = malloc( sizeof( mp4_stream_t ) );
894 p_stream->p_fmt = malloc( sizeof( sout_format_t ) );
895 memcpy( p_stream->p_fmt, p_input->p_fmt, sizeof( sout_format_t ) );
896 if( p_stream->p_fmt->i_extra_data )
898 p_stream->p_fmt->p_extra_data =
899 malloc( p_stream->p_fmt->i_extra_data );
900 memcpy( p_stream->p_fmt->p_extra_data,
901 p_input->p_fmt->p_extra_data,
902 p_input->p_fmt->i_extra_data );
904 p_stream->i_track_id = p_sys->i_nb_streams + 1;
905 p_stream->i_entry_count = 0;
906 p_stream->i_entry_max = 1000;
908 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
909 p_stream->i_duration = 0;
911 p_input->p_sys = p_stream;
913 msg_Dbg( p_mux, "adding input" );
915 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
916 return( VLC_SUCCESS );
919 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
921 msg_Dbg( p_mux, "removing input" );
925 /****************************************************************************/
927 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
933 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
937 p_fifo = p_mux->pp_inputs[i]->p_fifo;
939 if( p_fifo->i_depth > 1 )
941 sout_buffer_t *p_buf;
943 p_buf = sout_FifoShow( p_fifo );
944 if( i_stream < 0 || p_buf->i_dts < i_dts )
946 i_dts = p_buf->i_dts;
952 return( -1 ); // wait that all fifo have at least 2 packets
957 *pi_stream = i_stream;
966 static int Mux( sout_mux_t *p_mux )
968 sout_mux_sys_t *p_sys = p_mux->p_sys;
972 sout_input_t *p_input;
974 mp4_stream_t *p_stream;
975 sout_buffer_t *p_data;
978 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
980 return( VLC_SUCCESS );
984 p_input = p_mux->pp_inputs[i_stream];
985 p_stream = (mp4_stream_t*)p_input->p_sys;
987 p_data = sout_FifoGet( p_input->p_fifo );
988 //msg_Dbg( p_mux, "stream=%d size=%6d pos=%8lld", i_stream, p_data->i_size, p_sys->i_pos );
990 /* add index entry */
991 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
992 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_size;
993 p_stream->entry[p_stream->i_entry_count].i_pts = p_data->i_pts;
994 p_stream->entry[p_stream->i_entry_count].i_dts = p_data->i_dts;
995 p_stream->entry[p_stream->i_entry_count].i_length=
996 __MAX( p_data->i_length, 0 );
998 p_stream->i_entry_count++;
999 if( p_stream->i_entry_count >= p_stream->i_entry_max )
1001 p_stream->i_entry_max += 1000;
1003 realloc( p_stream->entry,
1004 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
1008 p_stream->i_duration += __MAX( p_data->i_length, 0 );
1009 p_sys->i_pos += p_data->i_size;
1012 sout_AccessOutWrite( p_mux->p_access, p_data );
1015 return( VLC_SUCCESS );
1019 /****************************************************************************/
1021 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1026 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1027 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1031 p_bo->i_buffer_size = i_size;
1032 p_bo->p_buffer = p_buffer;
1035 p_bo->b_grow = b_grow;
1039 static void bo_add_8( bo_t *p_bo, uint8_t i )
1041 if( p_bo->i_buffer < p_bo->i_buffer_size )
1043 p_bo->p_buffer[p_bo->i_buffer] = i;
1045 else if( p_bo->b_grow )
1047 p_bo->i_buffer_size += 1024;
1048 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1050 p_bo->p_buffer[p_bo->i_buffer] = i;
1056 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1058 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1059 bo_add_8( p_bo, i &0xff );
1062 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1064 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1065 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1066 bo_add_8( p_bo, ( i &0xff ) );
1068 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1070 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1071 bo_add_16be( p_bo, i &0xffff );
1074 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1076 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1077 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1078 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1079 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1082 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1084 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1085 bo_add_32be( p_bo, i &0xffffffff );
1088 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1090 bo_add_8( p_bo, fcc[0] );
1091 bo_add_8( p_bo, fcc[1] );
1092 bo_add_8( p_bo, fcc[2] );
1093 bo_add_8( p_bo, fcc[3] );
1096 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1100 for( i = 0; i < i_size; i++ )
1102 bo_add_8( p_bo, p_mem[i] );
1105 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1109 for( i = 0; i < p_bo2->i_buffer; i++ )
1111 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1115 static bo_t * box_new( char *fcc )
1119 if( ( box = malloc( sizeof( bo_t ) ) ) )
1121 bo_init( box, 0, NULL, VLC_TRUE );
1123 bo_add_32be ( box, 0 );
1124 bo_add_fourcc( box, fcc );
1130 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
1134 if( ( box = malloc( sizeof( bo_t ) ) ) )
1136 bo_init( box, 0, NULL, VLC_TRUE );
1138 bo_add_32be ( box, 0 );
1139 bo_add_fourcc( box, fcc );
1140 bo_add_8 ( box, v );
1141 bo_add_24be ( box, f );
1147 static void box_fix( bo_t *box )
1151 memcpy( &box_tmp, box, sizeof( bo_t ) );
1153 box_tmp.i_buffer = 0;
1154 bo_add_32be( &box_tmp, box->i_buffer );
1157 static void box_free( bo_t *box )
1161 free( box->p_buffer );
1167 static void box_gather ( bo_t *box, bo_t *box2 )
1169 bo_add_bo( box, box2 );
1174 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
1176 sout_buffer_t *p_buf;
1178 p_buf = sout_BufferNew( p_sout, box->i_buffer );
1179 if( box->i_buffer > 0 )
1181 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
1184 p_buf->i_size = box->i_buffer;
1189 static void box_send( sout_mux_t *p_mux, bo_t *box )
1191 sout_buffer_t *p_buf;
1193 p_buf = bo_to_sout( p_mux->p_sout, box );
1196 sout_AccessOutWrite( p_mux->p_access, p_buf );
1199 static int64_t get_timestamp()
1201 int64_t i_timestamp = 0;
1204 i_timestamp = time(NULL);
1205 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
1206 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60