1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003 VideoLAN
5 * $Id: mp4.c,v 1.2 2003/04/19 00:12:50 fenrir 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>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
43 static int Capability(sout_mux_t *, int, void *, void * );
44 static int AddStream( sout_mux_t *, sout_input_t * );
45 static int DelStream( sout_mux_t *, sout_input_t * );
46 static int Mux ( sout_mux_t * );
48 /*****************************************************************************
50 *****************************************************************************/
52 set_description( _("MP4/MOV muxer") );
53 set_capability( "sout mux", 5 );
54 add_shortcut( "mp4" );
55 add_shortcut( "mov" );
56 set_callbacks( Open, Close );
76 unsigned int i_entry_count;
77 unsigned int i_entry_max;
90 mp4_stream_t **pp_streams;
94 typedef struct bo_t bo_t;
105 static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
106 static void bo_add_8 ( bo_t *, uint8_t );
107 static void bo_add_16be ( bo_t *, uint16_t );
108 static void bo_add_24be ( bo_t *, uint32_t );
109 static void bo_add_32be ( bo_t *, uint32_t );
110 static void bo_add_64be ( bo_t *, uint64_t );
111 static void bo_add_fourcc(bo_t *, char * );
112 static void bo_add_bo ( bo_t *, bo_t * );
113 static void bo_add_mem ( bo_t *, int , uint8_t * );
115 static void bo_fix_32be ( bo_t *, int , uint32_t );
117 static bo_t * box_new ( char *fcc );
118 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f );
119 static void box_fix ( bo_t *box );
120 static void box_free( bo_t *box );
121 static void box_gather ( bo_t *box, bo_t *box2 );
123 static void box_send( sout_mux_t *p_mux, bo_t *box );
125 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box );
127 /*****************************************************************************
129 *****************************************************************************/
130 static int Open( vlc_object_t *p_this )
132 sout_mux_t *p_mux = (sout_mux_t*)p_this;
133 sout_mux_sys_t *p_sys;
136 p_sys = malloc( sizeof( sout_mux_sys_t ) );
138 p_sys->i_nb_streams = 0;
139 p_sys->pp_streams = NULL;
142 msg_Info( p_mux, "Open" );
144 p_mux->pf_capacity = Capability;
145 p_mux->pf_addstream = AddStream;
146 p_mux->pf_delstream = DelStream;
148 p_mux->p_sys = p_sys;
149 //p_mux->i_preheader = 0;
151 /* Now add ftyp header */
152 box = box_new( "ftyp" );
153 bo_add_fourcc( box, "isom" );
154 bo_add_32be ( box, 0 );
155 bo_add_fourcc( box, "mp41" );
158 p_sys->i_pos += box->i_buffer;
159 p_sys->i_mdat_pos = p_sys->i_pos;
161 box_send( p_mux, box );
163 /* Now add mdat header */
164 box = box_new( "mdat" );
165 bo_add_64be ( box, 0 ); // XXX large size
167 p_sys->i_pos += box->i_buffer;
169 box_send( p_mux, box );
174 static uint32_t GetDescriptorLength24b( int i_length )
176 uint32_t i_l1, i_l2, i_l3;
178 i_l1 = i_length&0x7f;
179 i_l2 = ( i_length >> 7 )&0x7f;
180 i_l3 = ( i_length >> 14 )&0x7f;
182 return( 0x808000 | ( i_l3 << 16 ) | ( i_l2 << 8 ) | i_l1 );
185 static bo_t *GetESDS( mp4_stream_t *p_stream )
189 int i_object_type_indication;
190 int i_decoder_specific_info_size;
192 if( p_stream->p_fmt->i_extra_data > 0 )
194 i_decoder_specific_info_size = p_stream->p_fmt->i_extra_data + 4;
198 i_decoder_specific_info_size = 0;
201 esds = box_full_new( "esds", 0, 0 );
203 bo_add_8 ( esds, 0x03 ); // ES_DescrTag
204 bo_add_24be( esds, GetDescriptorLength24b( 25 + i_decoder_specific_info_size ) );
205 bo_add_16be( esds, p_stream->i_track_id );
206 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
208 bo_add_8 ( esds, 0x04 ); // DecoderConfigDescrTag
209 bo_add_24be( esds, GetDescriptorLength24b( 13 + i_decoder_specific_info_size ) );
210 switch( p_stream->p_fmt->i_fourcc )
212 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
213 i_object_type_indication = 0x20;
215 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
216 i_object_type_indication = 0x60; /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
218 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
219 i_object_type_indication = 0x40; /* FIXME for mpeg2-aac == 0x66->0x68 */
221 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
222 i_object_type_indication = p_stream->p_fmt->i_sample_rate < 32000 ? 0x69 : 0x6b;
225 i_object_type_indication = 0x00;
228 i_stream_type = p_stream->p_fmt->i_cat == VIDEO_ES ? 0x04 : 0x05;
230 bo_add_8 ( esds, i_object_type_indication );
231 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
232 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
233 bo_add_32be( esds, 0x7fffffff ); // maxBitrate
234 bo_add_32be( esds, 0 ); // avgBitrate
235 if( p_stream->p_fmt->i_extra_data > 0 )
238 bo_add_8 ( esds, 0x05 ); // DecoderSpecificInfo
239 bo_add_24be( esds, GetDescriptorLength24b( p_stream->p_fmt->i_extra_data ) );
241 for( i = 0; i < p_stream->p_fmt->i_extra_data; i++ )
243 bo_add_8 ( esds, p_stream->p_fmt->p_extra_data[i] );
247 /* SL_Descr mandatory */
248 bo_add_8 ( esds, 0x06 ); // SLConfigDescriptorTag
249 bo_add_24be( esds, GetDescriptorLength24b( 1 ) );
250 bo_add_8 ( esds, 0x02 ); // sl_predefined
259 /*****************************************************************************
261 *****************************************************************************/
262 static uint32_t mvhd_matrix[9] = { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
264 static void Close( vlc_object_t * p_this )
266 sout_mux_t *p_mux = (sout_mux_t*)p_this;
267 sout_mux_sys_t *p_sys = p_mux->p_sys;
268 sout_buffer_t *p_hdr;
274 uint32_t i_movie_timescale = 90000;
275 int64_t i_movie_duration = 0;
276 msg_Info( p_mux, "Close" );
278 /* create general info */
279 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
281 mp4_stream_t *p_stream;
283 p_stream = p_sys->pp_streams[i_trak];
285 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
287 msg_Dbg( p_mux, "movie duration %ds", (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
289 i_movie_duration = i_movie_duration * (int64_t)i_movie_timescale / (int64_t)1000000;
291 /* *** update mdat size *** */
292 bo_init ( &bo, 0, NULL, VLC_TRUE );
293 bo_add_32be ( &bo, 1 );
294 bo_add_fourcc( &bo, "mdat" );
295 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
296 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
300 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
301 sout_AccessOutWrite( p_mux->p_access, p_hdr );
303 /* Now create header */
304 sout_AccessOutSeek( p_mux->p_access, p_sys->i_pos );
306 moov = box_new( "moov" );
308 /* *** add /moov/mvhd *** */
309 mvhd = box_full_new( "mvhd", 1, 0 );
310 bo_add_64be( mvhd, 0 ); // creation time FIXME
311 bo_add_64be( mvhd, 0 ); // modification time FIXME
312 bo_add_32be( mvhd, i_movie_timescale); // timescale
313 bo_add_64be( mvhd, i_movie_duration ); // duration
314 bo_add_32be( mvhd, 0x10000 ); // rate
315 bo_add_16be( mvhd, 0x100 ); // volume
316 bo_add_16be( mvhd, 0 ); // reserved
317 for( i = 0; i < 2; i++ )
319 bo_add_32be( mvhd, 0 ); // reserved
321 for( i = 0; i < 9; i++ )
323 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
325 for( i = 0; i < 6; i++ )
327 bo_add_32be( mvhd, 0 ); // pre-defined
329 bo_add_32be( mvhd, 0xffffffff ); // next-track-id FIXME
331 box_gather( moov, mvhd );
333 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
335 mp4_stream_t *p_stream;
336 uint32_t i_timescale;
337 uint32_t i_chunk_count;
352 p_stream = p_sys->pp_streams[i_trak];
354 if( p_stream->p_fmt->i_cat != AUDIO_ES && p_stream->p_fmt->i_cat != VIDEO_ES )
356 msg_Err( p_mux, "FIXME ignoring trak (noaudio&&novideo)" );
359 if( p_stream->p_fmt->i_cat == AUDIO_ES )
361 i_timescale = p_stream->p_fmt->i_sample_rate;
368 /* *** add /moov/trak *** */
369 trak = box_new( "trak" );
371 /* *** add /moov/trak/tkhd *** */
372 tkhd = box_full_new( "tkhd", 1, 1 );
373 bo_add_64be( tkhd, 0 ); // creation time
374 bo_add_64be( tkhd, 0 ); // modification time
375 bo_add_32be( tkhd, p_stream->i_track_id );
376 bo_add_32be( tkhd, 0 ); // reserved 0
377 bo_add_64be( tkhd, p_stream->i_duration *
378 (int64_t)i_movie_timescale /
379 (mtime_t)1000000 ); // duration
380 for( i = 0; i < 2; i++ )
382 bo_add_32be( tkhd, 0 ); // reserved
384 bo_add_16be( tkhd, 0 ); // layer
385 bo_add_16be( tkhd, 0 ); // pre-defined
386 bo_add_16be( tkhd, p_stream->p_fmt->i_cat == AUDIO_ES ? 0x100 : 0 ); // volume
387 bo_add_16be( tkhd, 0 ); // reserved
388 for( i = 0; i < 9; i++ )
390 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
392 if( p_stream->p_fmt->i_cat == AUDIO_ES )
394 bo_add_32be( tkhd, 0 ); // width (presentation)
395 bo_add_32be( tkhd, 0 ); // height(presentation)
399 bo_add_32be( tkhd, p_stream->p_fmt->i_width << 16 ); // width (presentation)
400 bo_add_32be( tkhd, p_stream->p_fmt->i_height << 16 ); // height(presentation)
403 box_gather( trak, tkhd );
405 /* *** add /moov/trak/mdia *** */
406 mdia = box_new( "mdia" );
409 mdhd = box_full_new( "mdhd", 1, 0 );
410 bo_add_64be( mdhd, 0 ); // creation time
411 bo_add_64be( mdhd, 0 ); // modification time
412 bo_add_32be( mdhd, i_timescale); // timescale
413 bo_add_64be( mdhd, p_stream->i_duration *
414 (int64_t)i_timescale /
415 (mtime_t)1000000 ); // duration
417 bo_add_16be( mdhd, 0 ); // language FIXME
418 bo_add_16be( mdhd, 0 ); // predefined
420 box_gather( mdia, mdhd );
423 hdlr = box_full_new( "hdlr", 0, 0 );
424 bo_add_32be( hdlr, 0 ); // predefined
425 if( p_stream->p_fmt->i_cat == AUDIO_ES )
427 bo_add_fourcc( hdlr, "soun" );
431 bo_add_fourcc( hdlr, "vide" );
433 for( i = 0; i < 3; i++ )
435 bo_add_32be( hdlr, 0 ); // reserved
437 bo_add_mem( hdlr, 2, "?" );
440 box_gather( mdia, hdlr );
443 minf = box_new( "minf" );
446 if( p_stream->p_fmt->i_cat == AUDIO_ES )
450 smhd = box_full_new( "smhd", 0, 0 );
451 bo_add_16be( smhd, 0 ); // balance
452 bo_add_16be( smhd, 0 ); // reserved
455 box_gather( minf, smhd );
457 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
461 vmhd = box_full_new( "vmhd", 0, 1 );
462 bo_add_16be( vmhd, 0 ); // graphicsmode
463 for( i = 0; i < 3; i++ )
465 bo_add_16be( vmhd, 0 ); // opcolor
469 box_gather( minf, vmhd );
473 dinf = box_new( "dinf" );
474 dref = box_full_new( "dref", 0, 0 );
475 bo_add_32be( dref, 1 );
476 url = box_full_new( "url ", 0, 0x01 );
478 box_gather( dref, url );
480 box_gather( dinf, dref );
482 /* append dinf to mdia */
484 box_gather( minf, dinf );
487 stbl = box_new( "stbl" );
488 stsd = box_full_new( "stsd", 0, 0 );
489 bo_add_32be( stsd, 1 );
491 if( p_stream->p_fmt->i_cat == AUDIO_ES )
496 vlc_bool_t b_mpeg4_hdr;
498 switch( p_stream->p_fmt->i_fourcc )
500 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
501 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
502 memcpy( fcc, "mp4a", 4 );
503 b_mpeg4_hdr = VLC_TRUE;
507 memcpy( fcc, (char*)&p_stream->p_fmt->i_fourcc, 4 );
508 b_mpeg4_hdr = VLC_FALSE;
512 soun = box_new( fcc );
513 for( i = 0; i < 6; i++ )
515 bo_add_8( soun, 0 ); // reserved;
517 bo_add_16be( soun, 1 ); // data-reference-index
518 bo_add_32be( soun, 0 ); // reserved;
519 bo_add_32be( soun, 0 ); // reserved;
520 bo_add_16be( soun, p_stream->p_fmt->i_channels ); // channel-count
521 bo_add_16be( soun, 16); // FIXME sample size
522 bo_add_16be( soun, 0 ); // predefined
523 bo_add_16be( soun, 0 ); // reserved
524 bo_add_16be( soun, p_stream->p_fmt->i_sample_rate ); // sampleratehi
525 bo_add_16be( soun, 0 ); // sampleratelo
527 /* add an ES Descriptor */
532 esds = GetESDS( p_stream );
535 box_gather( soun, esds );
539 box_gather( stsd, soun );
541 else if( p_stream->p_fmt->i_cat == VIDEO_ES )
546 vlc_bool_t b_mpeg4_hdr;
548 switch( p_stream->p_fmt->i_fourcc )
550 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
551 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
552 memcpy( fcc, "mp4v", 4 );
553 b_mpeg4_hdr = VLC_TRUE;
557 memcpy( fcc, (char*)&p_stream->p_fmt->i_fourcc, 4 );
558 b_mpeg4_hdr = VLC_FALSE;
562 vide = box_new( fcc );
563 for( i = 0; i < 6; i++ )
565 bo_add_8( vide, 0 ); // reserved;
567 bo_add_16be( vide, 1 ); // data-reference-index
569 bo_add_16be( vide, 0 ); // predefined;
570 bo_add_16be( vide, 0 ); // reserved;
571 for( i = 0; i < 3; i++ )
573 bo_add_32be( vide, 0 ); // predefined;
576 bo_add_16be( vide, p_stream->p_fmt->i_width ); // i_width
577 bo_add_16be( vide, p_stream->p_fmt->i_height ); // i_height
579 bo_add_32be( vide, 0x00480000 ); // h 72dpi
580 bo_add_32be( vide, 0x00480000 ); // v 72dpi
582 bo_add_32be( vide, 0 ); // reserved
583 bo_add_16be( vide, 1 ); // predefined
584 for( i = 0; i < 32; i++ )
586 bo_add_8( vide, 0 ); // compressor name;
588 bo_add_16be( vide, 0x18 ); // depth
589 bo_add_16be( vide, 0xffff ); // predefined
591 /* add an ES Descriptor */
596 esds = GetESDS( p_stream );
599 box_gather( vide, esds );
603 box_gather( stsd, vide );
606 /* append stsd to stbl */
608 box_gather( stbl, stsd );
610 /* we will create chunk table and stsc table FIXME optim stsc table FIXME */
612 for( i = 0; i < p_stream->i_entry_count; )
614 while( i < p_stream->i_entry_count )
616 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 )
627 // if( p_sys->i_pos >= 0xffffffff )
632 unsigned int i_chunk;
634 msg_Dbg( p_mux, "creating %d chunk (co64)", i_chunk_count );
636 co64 = box_full_new( "co64", 0, 0 );
637 bo_add_32be( co64, i_chunk_count );
639 stsc = box_full_new( "stsc", 0, 0 );
640 bo_add_32be( stsc, i_chunk_count ); // entry-count
641 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
644 bo_add_64be( co64, p_stream->entry[i].i_pos );
648 while( i < p_stream->i_entry_count )
650 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 )
658 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
659 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
660 bo_add_32be( stsc, 1 ); // sample-descr-index
662 /* append co64 to stbl */
664 box_gather( stbl, co64 );
666 /* append stsc to stbl */
668 box_gather( stbl, stsc );
672 // FIXME implement it
677 stts = box_full_new( "stts", 0, 0 );
678 bo_add_32be( stts, 0 ); // fixed latter
679 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
685 i_delta = p_stream->entry[i].i_length;
687 while( i < p_stream->i_entry_count )
689 if( i + 1 < p_stream->i_entry_count && p_stream->entry[i + 1].i_length != i_delta )
698 bo_add_32be( stts, i - i_first ); // sample-count
699 bo_add_32be( stts, i_delta *
700 (int64_t)i_timescale /
701 (int64_t)1000000 ); // sample-delta
703 bo_fix_32be( stts, 12, i_index );
705 /* append stts to stbl */
707 box_gather( stbl, stts );
709 /* FIXME add ctts ?? FIXME */
711 stsz = box_full_new( "stsz", 0, 0 );
712 bo_add_32be( stsz, 0 ); // sample-size
713 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
714 for( i = 0; i < p_stream->i_entry_count; i++ )
716 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
718 /* append stsz to stbl */
720 box_gather( stbl, stsz );
722 /* append stbl to minf */
724 box_gather( minf, stbl );
727 /* append minf to mdia */
729 box_gather( mdia, minf );
732 /* append mdia to trak */
734 box_gather( trak, mdia );
736 /* append trak to moov */
738 box_gather( moov, trak );
742 box_send( p_mux, moov );
744 /* *** release memory *** */
745 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
747 mp4_stream_t *p_stream;
749 p_stream = p_sys->pp_streams[i_trak];
751 if( p_stream->p_fmt->p_extra_data )
753 free( p_stream->p_fmt->p_extra_data );
755 free( p_stream->p_fmt );
756 free( p_stream->entry );
762 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args, void *p_answer )
766 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
767 *(vlc_bool_t*)p_answer = VLC_FALSE;
768 return( SOUT_MUX_CAP_ERR_OK );
770 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
774 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
776 sout_mux_sys_t *p_sys = p_mux->p_sys;
777 mp4_stream_t *p_stream;
779 switch( p_input->p_fmt->i_fourcc )
781 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
782 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
783 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
784 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
788 "unsupported codec %4.4s in mp4",
789 (char*)&p_input->p_fmt->i_fourcc );
793 p_stream = malloc( sizeof( mp4_stream_t ) );
794 p_stream->p_fmt = malloc( sizeof( sout_format_t ) );
795 memcpy( p_stream->p_fmt, p_input->p_fmt, sizeof( sout_format_t ) );
796 if( p_stream->p_fmt->i_extra_data )
798 p_stream->p_fmt->p_extra_data = malloc( p_stream->p_fmt->i_extra_data );
799 memcpy( p_stream->p_fmt->p_extra_data,
800 p_input->p_fmt->p_extra_data,
801 p_input->p_fmt->i_extra_data );
803 p_stream->i_track_id = p_sys->i_nb_streams + 1;
804 p_stream->i_entry_count = 0;
805 p_stream->i_entry_max = 1000;
806 p_stream->entry = calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
807 p_stream->i_duration = 0;
809 p_input->p_sys = p_stream;
811 msg_Dbg( p_mux, "adding input" );
813 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
814 return( VLC_SUCCESS );
817 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
820 msg_Dbg( p_mux, "removing input" );
825 /****************************************************************************/
827 static int MuxGetStream( sout_mux_t *p_mux,
835 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
839 p_fifo = p_mux->pp_inputs[i]->p_fifo;
841 if( p_fifo->i_depth > 1 )
843 sout_buffer_t *p_buf;
845 p_buf = sout_FifoShow( p_fifo );
846 if( i_stream < 0 || p_buf->i_dts < i_dts )
848 i_dts = p_buf->i_dts;
854 return( -1 ); // wait that all fifo have at least 2 packets
859 *pi_stream = i_stream;
869 static int Mux ( sout_mux_t *p_mux )
871 sout_mux_sys_t *p_sys = p_mux->p_sys;
875 sout_input_t *p_input;
877 mp4_stream_t *p_stream;
878 sout_buffer_t *p_data;
881 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
883 return( VLC_SUCCESS );
887 p_input = p_mux->pp_inputs[i_stream];
888 p_stream = (mp4_stream_t*)p_input->p_sys;
890 p_data = sout_FifoGet( p_input->p_fifo );
891 //msg_Dbg( p_mux, "stream=%d size=%6d pos=%8lld", i_stream, p_data->i_size, p_sys->i_pos );
893 /* add index entry */
894 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
895 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_size;
896 p_stream->entry[p_stream->i_entry_count].i_pts = p_data->i_pts;
897 p_stream->entry[p_stream->i_entry_count].i_dts = p_data->i_dts;
898 p_stream->entry[p_stream->i_entry_count].i_length= __MAX( p_data->i_length, 0 );
900 p_stream->i_entry_count++;
901 if( p_stream->i_entry_count >= p_stream->i_entry_max )
903 p_stream->i_entry_max += 1000;
904 p_stream->entry = realloc( p_stream->entry, p_stream->i_entry_max * sizeof( mp4_entry_t ) );
908 p_stream->i_duration += __MAX( p_data->i_length, 0 );
909 p_sys->i_pos += p_data->i_size;
912 sout_AccessOutWrite( p_mux->p_access, p_data );
915 return( VLC_SUCCESS );
919 /****************************************************************************/
922 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer, vlc_bool_t b_grow )
926 p_bo->i_buffer_size = __MAX( i_size, 1024 );
927 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
931 p_bo->i_buffer_size = i_size;
932 p_bo->p_buffer = p_buffer;
935 p_bo->b_grow = b_grow;
939 static void bo_add_8( bo_t *p_bo, uint8_t i )
941 if( p_bo->i_buffer < p_bo->i_buffer_size )
943 p_bo->p_buffer[p_bo->i_buffer] = i;
945 else if( p_bo->b_grow )
947 p_bo->i_buffer_size += 1024;
948 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
950 p_bo->p_buffer[p_bo->i_buffer] = i;
956 static void bo_add_16be( bo_t *p_bo, uint16_t i )
958 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
959 bo_add_8( p_bo, i &0xff );
962 static void bo_add_24be( bo_t *p_bo, uint32_t i )
964 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
965 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
966 bo_add_8( p_bo, ( i &0xff ) );
968 static void bo_add_32be( bo_t *p_bo, uint32_t i )
970 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
971 bo_add_16be( p_bo, i &0xffff );
974 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
976 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
977 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
978 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
979 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
982 static void bo_add_64be( bo_t *p_bo, uint64_t i )
984 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
985 bo_add_32be( p_bo, i &0xffffffff );
988 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
990 bo_add_8( p_bo, fcc[0] );
991 bo_add_8( p_bo, fcc[1] );
992 bo_add_8( p_bo, fcc[2] );
993 bo_add_8( p_bo, fcc[3] );
996 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1000 for( i = 0; i < i_size; i++ )
1002 bo_add_8( p_bo, p_mem[i] );
1005 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1009 for( i = 0; i < p_bo2->i_buffer; i++ )
1011 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1015 static bo_t * box_new( char *fcc )
1019 if( ( box = malloc( sizeof( bo_t ) ) ) )
1021 bo_init( box, 0, NULL, VLC_TRUE );
1023 bo_add_32be ( box, 0 );
1024 bo_add_fourcc( box, fcc );
1030 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
1034 if( ( box = malloc( sizeof( bo_t ) ) ) )
1036 bo_init( box, 0, NULL, VLC_TRUE );
1038 bo_add_32be ( box, 0 );
1039 bo_add_fourcc( box, fcc );
1040 bo_add_8 ( box, v );
1041 bo_add_24be ( box, f );
1047 static void box_fix( bo_t *box )
1051 memcpy( &box_tmp, box, sizeof( bo_t ) );
1053 box_tmp.i_buffer = 0;
1054 bo_add_32be( &box_tmp, box->i_buffer );
1057 static void box_free( bo_t *box )
1061 free( box->p_buffer );
1067 static void box_gather ( bo_t *box, bo_t *box2 )
1069 bo_add_bo( box, box2 );
1074 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
1076 sout_buffer_t *p_buf;
1078 p_buf = sout_BufferNew( p_sout, box->i_buffer );
1079 if( box->i_buffer > 0 )
1081 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
1084 p_buf->i_size = box->i_buffer;
1090 static void box_send( sout_mux_t *p_mux, bo_t *box )
1092 sout_buffer_t *p_buf;
1094 p_buf = bo_to_sout( p_mux->p_sout, box );
1097 sout_AccessOutWrite( p_mux->p_access, p_buf );