1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 #define FASTSTART_TEXT N_("Create \"Fast start\" files")
46 #define FASTSTART_LONGTEXT N_( \
47 "When this option is turned on, \"Fast start\" files will be created. " \
48 "(\"Fast start\" files are optimized for download, allowing the user " \
49 "to start previewing the file while it is downloading).")
50 static int Open ( vlc_object_t * );
51 static void Close ( vlc_object_t * );
54 set_description( _("MP4/MOV muxer") );
56 add_bool( "mp4-faststart", 1, NULL, FASTSTART_TEXT, FASTSTART_LONGTEXT,
59 set_capability( "sout mux", 5 );
60 add_shortcut( "mp4" );
61 add_shortcut( "mov" );
62 set_callbacks( Open, Close );
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Capability(sout_mux_t *, int, void *, void * );
69 static int AddStream( sout_mux_t *, sout_input_t * );
70 static int DelStream( sout_mux_t *, sout_input_t * );
71 static int Mux ( sout_mux_t * );
73 /*****************************************************************************
75 *****************************************************************************/
93 unsigned int i_entry_count;
94 unsigned int i_entry_max;
102 /* for later stco fix-up (fast start files) */
108 struct sout_mux_sys_t
112 vlc_bool_t b_fast_start;
120 mp4_stream_t **pp_streams;
133 static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
134 static void bo_add_8 ( bo_t *, uint8_t );
135 static void bo_add_16be ( bo_t *, uint16_t );
136 static void bo_add_24be ( bo_t *, uint32_t );
137 static void bo_add_32be ( bo_t *, uint32_t );
138 static void bo_add_64be ( bo_t *, uint64_t );
139 static void bo_add_fourcc(bo_t *, char * );
140 static void bo_add_bo ( bo_t *, bo_t * );
141 static void bo_add_mem ( bo_t *, int , uint8_t * );
142 static void bo_add_descr( bo_t *, uint8_t , uint32_t );
144 static void bo_fix_32be ( bo_t *, int , uint32_t );
146 static bo_t *box_new ( char *fcc );
147 static bo_t *box_full_new( char *fcc, uint8_t v, uint32_t f );
148 static void box_fix ( bo_t *box );
149 static void box_free ( bo_t *box );
150 static void box_gather ( bo_t *box, bo_t *box2 );
152 static void box_send( sout_mux_t *p_mux, bo_t *box );
154 static sout_buffer_t *bo_to_sout( sout_instance_t *p_sout, bo_t *box );
156 static bo_t *GetMoovBox( sout_mux_t *p_mux );
158 /*****************************************************************************
160 *****************************************************************************/
161 static int Open( vlc_object_t *p_this )
163 sout_mux_t *p_mux = (sout_mux_t*)p_this;
164 sout_mux_sys_t *p_sys;
167 p_sys = malloc( sizeof( sout_mux_sys_t ) );
169 p_sys->i_nb_streams = 0;
170 p_sys->pp_streams = NULL;
171 p_sys->i_mdat_pos = 0;
172 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
173 p_sys->i_dts_start = 0;
175 msg_Dbg( p_mux, "Open" );
177 p_mux->pf_capacity = Capability;
178 p_mux->pf_addstream = AddStream;
179 p_mux->pf_delstream = DelStream;
181 p_mux->p_sys = p_sys;
185 /* Now add ftyp header */
186 box = box_new( "ftyp" );
187 bo_add_fourcc( box, "isom" );
188 bo_add_32be ( box, 0 );
189 bo_add_fourcc( box, "mp41" );
192 p_sys->i_pos += box->i_buffer;
193 p_sys->i_mdat_pos = p_sys->i_pos;
195 box_send( p_mux, box );
199 * Quicktime actually doesn't like the 64 bits extensions !!! */
200 p_sys->b_64_ext = VLC_FALSE;
202 /* Now add mdat header */
203 box = box_new( "mdat" );
204 bo_add_64be ( box, 0 ); // enough to store an extended size
206 p_sys->i_pos += box->i_buffer;
208 box_send( p_mux, box );
213 /*****************************************************************************
215 *****************************************************************************/
216 static void Close( vlc_object_t * p_this )
218 sout_mux_t *p_mux = (sout_mux_t*)p_this;
219 sout_mux_sys_t *p_sys = p_mux->p_sys;
220 sout_buffer_t *p_hdr;
227 msg_Dbg( p_mux, "Close" );
229 /* Update mdat size */
230 bo_init( &bo, 0, NULL, VLC_TRUE );
231 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
234 bo_add_32be ( &bo, 1 );
235 bo_add_fourcc( &bo, "mdat" );
236 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
240 bo_add_32be ( &bo, 8 );
241 bo_add_fourcc( &bo, "wide" );
242 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
243 bo_add_fourcc( &bo, "mdat" );
245 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
248 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
249 sout_AccessOutWrite( p_mux->p_access, p_hdr );
251 /* Create MOOV header */
252 i_moov_pos = p_sys->i_pos;
253 moov = GetMoovBox( p_mux );
255 /* Check we need to create "fast start" files */
256 var_Create( p_this, "mp4-faststart", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
257 var_Get( p_this, "mp4-faststart", &val );
258 p_sys->b_fast_start = val.b_bool;
259 while( p_sys->b_fast_start )
261 /* Move data to the end of the file so we can fit the moov header
263 sout_buffer_t *p_buf;
264 int64_t i_chunk, i_size = p_sys->i_pos - p_sys->i_mdat_pos;
265 int i_moov_size = moov->i_buffer;
269 i_chunk = __MIN( 32768, i_size );
270 p_buf = sout_BufferNew( p_mux->p_sout, i_chunk );
271 sout_AccessOutSeek( p_mux->p_access,
272 p_sys->i_mdat_pos + i_size - i_chunk );
273 if( sout_AccessOutRead( p_mux->p_access, p_buf ) < i_chunk )
275 msg_Warn( p_this, "read() not supported by acces output, "
276 "won't create a fast start file" );
277 p_sys->b_fast_start = VLC_FALSE;
280 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos + i_size +
281 i_moov_size - i_chunk );
282 sout_AccessOutWrite( p_mux->p_access, p_buf );
286 if( !p_sys->b_fast_start ) break;
288 /* Fix-up samples to chunks table in MOOV header */
289 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
291 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
295 moov->i_buffer = p_stream->i_stco_pos;
296 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
298 if( p_stream->b_stco64 )
299 bo_add_64be( moov, p_stream->entry[i].i_pos + i_moov_size);
301 bo_add_32be( moov, p_stream->entry[i].i_pos + i_moov_size);
303 while( i < p_stream->i_entry_count )
305 if( i + 1 < p_stream->i_entry_count &&
306 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
307 != p_stream->entry[i + 1].i_pos )
318 moov->i_buffer = i_moov_size;
319 i_moov_pos = p_sys->i_mdat_pos;
320 p_sys->b_fast_start = VLC_FALSE;
323 /* Write MOOV header */
324 sout_AccessOutSeek( p_mux->p_access, i_moov_pos );
325 box_send( p_mux, moov );
328 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
330 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
332 es_format_Clean( &p_stream->fmt );
333 free( p_stream->entry );
336 if( p_sys->i_nb_streams ) free( p_sys->pp_streams );
340 /*****************************************************************************
342 *****************************************************************************/
343 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
348 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
349 *(vlc_bool_t*)p_answer = VLC_TRUE;
350 return SOUT_MUX_CAP_ERR_OK;
352 case SOUT_MUX_CAP_GET_ADD_STREAM_WAIT:
353 *(vlc_bool_t*)p_answer = VLC_TRUE;
354 return( SOUT_MUX_CAP_ERR_OK );
357 return SOUT_MUX_CAP_ERR_UNIMPLEMENTED;
361 /*****************************************************************************
363 *****************************************************************************/
364 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
366 sout_mux_sys_t *p_sys = p_mux->p_sys;
367 mp4_stream_t *p_stream;
369 switch( p_input->p_fmt->i_codec )
371 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
372 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
373 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
374 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
375 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
376 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
377 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
378 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
379 case VLC_FOURCC( 'h', '2', '6', '4' ):
382 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
383 (char*)&p_input->p_fmt->i_codec );
387 p_stream = malloc( sizeof( mp4_stream_t ) );
388 es_format_Copy( &p_stream->fmt, p_input->p_fmt );
389 p_stream->i_track_id = p_sys->i_nb_streams + 1;
390 p_stream->i_length_neg = 0;
391 p_stream->i_entry_count = 0;
392 p_stream->i_entry_max = 1000;
394 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
395 p_stream->i_dts_start = 0;
396 p_stream->i_duration = 0;
398 p_input->p_sys = p_stream;
400 msg_Dbg( p_mux, "adding input" );
402 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
406 /*****************************************************************************
408 *****************************************************************************/
409 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
411 msg_Dbg( p_mux, "removing input" );
415 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
421 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
423 sout_fifo_t *p_fifo = p_mux->pp_inputs[i]->p_fifo;
424 sout_buffer_t *p_buf;
426 if( p_fifo->i_depth <= 1 )
428 return -1; // wait that all fifo have at least 2 packets
431 p_buf = sout_FifoShow( p_fifo );
432 if( i_stream < 0 || p_buf->i_dts < i_dts )
434 i_dts = p_buf->i_dts;
440 *pi_stream = i_stream;
449 /*****************************************************************************
451 *****************************************************************************/
452 static int Mux( sout_mux_t *p_mux )
454 sout_mux_sys_t *p_sys = p_mux->p_sys;
458 sout_input_t *p_input;
460 mp4_stream_t *p_stream;
461 sout_buffer_t *p_data;
464 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
466 return( VLC_SUCCESS );
469 p_input = p_mux->pp_inputs[i_stream];
470 p_stream = (mp4_stream_t*)p_input->p_sys;
472 p_data = sout_FifoGet( p_input->p_fifo );
473 if( p_input->p_fifo->i_depth > 0 )
475 sout_buffer_t *p_next = sout_FifoShow( p_input->p_fifo );
476 int64_t i_diff = p_next->i_dts - p_data->i_dts;
478 if( i_diff < I64C(1000000 ) ) /* protection */
480 p_data->i_length = i_diff;
483 if( p_data->i_length <= 0 )
485 msg_Warn( p_mux, "i_length <= 0" );
486 p_stream->i_length_neg += p_data->i_length - 1;
487 p_data->i_length = 1;
489 else if( p_stream->i_length_neg < 0 )
491 int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
493 p_data->i_length -= i_recover;
494 p_stream->i_length_neg += i_recover;
497 /* Save starting time */
498 if( p_stream->i_entry_count == 0 )
500 p_stream->i_dts_start = p_data->i_dts;
502 /* Update global dts_start */
503 if( p_sys->i_dts_start <= 0 ||
504 p_stream->i_dts_start < p_sys->i_dts_start )
506 p_sys->i_dts_start = p_stream->i_dts_start;
510 /* add index entry */
511 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
512 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_size;
513 p_stream->entry[p_stream->i_entry_count].i_pts_dts=
514 __MAX( p_data->i_pts - p_data->i_dts, 0 );
515 p_stream->entry[p_stream->i_entry_count].i_length = p_data->i_length;
516 p_stream->entry[p_stream->i_entry_count].i_flags = p_data->i_flags;
518 p_stream->i_entry_count++;
519 if( p_stream->i_entry_count >= p_stream->i_entry_max )
521 p_stream->i_entry_max += 1000;
523 realloc( p_stream->entry,
524 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
528 p_stream->i_duration += p_data->i_length;
529 p_sys->i_pos += p_data->i_size;
532 sout_AccessOutWrite( p_mux->p_access, p_data );
535 return( VLC_SUCCESS );
538 /*****************************************************************************
540 *****************************************************************************/
541 static int GetDescrLength( int i_size )
543 if( i_size < 0x00000080 )
545 else if( i_size < 0x00004000 )
547 else if( i_size < 0x00200000 )
553 static bo_t *GetESDS( mp4_stream_t *p_stream )
557 int i_object_type_indication;
558 int i_decoder_specific_info_size;
560 if( p_stream->fmt.i_extra > 0 )
562 i_decoder_specific_info_size =
563 GetDescrLength( p_stream->fmt.i_extra );
567 i_decoder_specific_info_size = 0;
570 esds = box_full_new( "esds", 0, 0 );
573 bo_add_descr( esds, 0x03, 3 +
574 GetDescrLength( 13 + i_decoder_specific_info_size ) +
575 GetDescrLength( 1 ) );
576 bo_add_16be( esds, p_stream->i_track_id );
577 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
579 /* DecoderConfigDescr */
580 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
582 switch( p_stream->fmt.i_codec )
584 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
585 i_object_type_indication = 0x20;
587 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
588 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
589 i_object_type_indication = 0x60;
591 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
592 /* FIXME for mpeg2-aac == 0x66->0x68 */
593 i_object_type_indication = 0x40;
595 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
596 i_object_type_indication =
597 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
600 i_object_type_indication = 0x00;
603 i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
605 bo_add_8 ( esds, i_object_type_indication );
606 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
607 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
608 bo_add_32be( esds, 0x7fffffff ); // maxBitrate
609 bo_add_32be( esds, 0 ); // avgBitrate
611 if( p_stream->fmt.i_extra > 0 )
615 /* DecoderSpecificInfo */
616 bo_add_descr( esds, 0x05, p_stream->fmt.i_extra );
618 for( i = 0; i < p_stream->fmt.i_extra; i++ )
620 bo_add_8( esds, ((uint8_t*)p_stream->fmt.p_extra)[i] );
624 /* SL_Descr mandatory */
625 bo_add_descr( esds, 0x06, 1 );
626 bo_add_8 ( esds, 0x02 ); // sl_predefined
633 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
638 wave = box_new( "wave" );
640 box = box_new( "frma" );
641 bo_add_fourcc( box, "mp4a" );
643 box_gather( wave, box );
645 box = box_new( "mp4a" );
646 bo_add_32be( box, 0 );
648 box_gather( wave, box );
650 box = GetESDS( p_stream );
652 box_gather( wave, box );
654 box = box_new( "srcq" );
655 bo_add_32be( box, 0x40 );
657 box_gather( wave, box );
660 bo_add_32be( wave, 8 ); /* new empty box */
661 bo_add_32be( wave, 0 ); /* box label */
668 /* TODO: No idea about these values */
669 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
671 bo_t *smi = box_new( "SMI " );
673 if( p_stream->fmt.i_extra > 0x4e )
675 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
676 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
678 while( p + 8 < p_end )
680 int i_size = GetDWBE( p );
683 /* FIXME handle 1 as long size */
686 if( !strncmp( &p[4], "SMI ", 4 ) )
688 bo_add_mem( smi, p_end - p - 8, &p[8] );
695 /* Create a dummy one in fallback */
696 bo_add_fourcc( smi, "SEQH" );
697 bo_add_32be( smi, 0x5 );
698 bo_add_32be( smi, 0xe2c0211d );
699 bo_add_8( smi, 0xc0 );
705 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
707 sout_mux_sys_t *p_sys = p_mux->p_sys;
708 bo_t *udta = box_new( "udta" );
709 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
713 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
715 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
717 if( p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','v') ||
718 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
720 bo_t *box = box_new( "\251req" );
722 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
723 bo_add_16be( box, 0 );
724 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
725 "QuickTime 6.0 or greater" );
727 box_gather( udta, box );
734 bo_t *box = box_new( "\251enc" );
736 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
737 bo_add_16be( box, 0 );
738 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
739 PACKAGE_STRING " stream output" );
741 box_gather( udta, box );
748 for( i = 0; i < p_meta->i_meta; i++ )
752 if( !strcmp( p_meta->name[i], VLC_META_TITLE ) )
753 box = box_new( "\251nam" );
754 else if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
755 box = box_new( "\251aut" );
756 else if( !strcmp( p_meta->name[i], VLC_META_ARTIST ) )
757 box = box_new( "\251ART" );
758 else if( !strcmp( p_meta->name[i], VLC_META_GENRE ) )
759 box = box_new( "\251gen" );
760 else if( !strcmp( p_meta->name[i], VLC_META_COPYRIGHT ) )
761 box = box_new( "\251cpy" );
762 else if( !strcmp( p_meta->name[i], VLC_META_DESCRIPTION ) )
763 box = box_new( "\251des" );
764 else if( !strcmp( p_meta->name[i], VLC_META_DATE ) )
765 box = box_new( "\251day" );
766 else if( !strcmp( p_meta->name[i], VLC_META_URL ) )
767 box = box_new( "\251url" );
771 bo_add_16be( box, strlen( p_meta->value[i] ) );
772 bo_add_16be( box, 0 );
773 bo_add_mem( box, strlen( p_meta->value[i] ),
776 box_gather( udta, box );
785 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
787 sout_mux_sys_t *p_sys = p_mux->p_sys;
788 vlc_bool_t b_descr = VLC_FALSE;
793 switch( p_stream->fmt.i_codec )
795 case VLC_FOURCC('m','p','4','a'):
796 memcpy( fcc, "mp4a", 4 );
800 case VLC_FOURCC('m','p','g','a'):
802 memcpy( fcc, ".mp3", 4 );
805 memcpy( fcc, "mp4a", 4 );
811 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
815 soun = box_new( fcc );
816 for( i = 0; i < 6; i++ )
818 bo_add_8( soun, 0 ); // reserved;
820 bo_add_16be( soun, 1 ); // data-reference-index
822 /* SoundDescription */
824 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
826 bo_add_16be( soun, 1 ); // version 1;
830 bo_add_16be( soun, 0 ); // version 0;
832 bo_add_16be( soun, 0 ); // revision level (0)
833 bo_add_32be( soun, 0 ); // vendor
835 bo_add_16be( soun, p_stream->fmt.audio.i_channels );
837 bo_add_16be( soun, p_stream->fmt.audio.i_bitspersample ?
838 p_stream->fmt.audio.i_bitspersample : 16 );
839 bo_add_16be( soun, -2 ); // compression id
840 bo_add_16be( soun, 0 ); // packet size (0)
841 bo_add_16be( soun, p_stream->fmt.audio.i_rate ); // sampleratehi
842 bo_add_16be( soun, 0 ); // sampleratelo
844 /* Extended data for SoundDescription V1 */
846 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
848 /* samples per packet */
849 bo_add_32be( soun, p_stream->fmt.audio.i_frame_length );
850 bo_add_32be( soun, 1536 ); /* bytes per packet */
851 bo_add_32be( soun, 2 ); /* bytes per frame */
852 /* bytes per sample */
853 bo_add_32be( soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
856 /* Add an ES Descriptor */
862 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
864 box = GetWaveTag( p_stream );
868 box = GetESDS( p_stream );
871 box_gather( soun, box );
879 static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
886 switch( p_stream->fmt.i_codec )
888 case VLC_FOURCC('m','p','4','v'):
889 case VLC_FOURCC('m','p','g','v'):
890 memcpy( fcc, "mp4v", 4 );
893 case VLC_FOURCC('M','J','P','G'):
894 memcpy( fcc, "mjpa", 4 );
897 case VLC_FOURCC('S','V','Q','1'):
898 memcpy( fcc, "SVQ1", 4 );
901 case VLC_FOURCC('S','V','Q','3'):
902 memcpy( fcc, "SVQ3", 4 );
906 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
910 vide = box_new( fcc );
911 for( i = 0; i < 6; i++ )
913 bo_add_8( vide, 0 ); // reserved;
915 bo_add_16be( vide, 1 ); // data-reference-index
917 bo_add_16be( vide, 0 ); // predefined;
918 bo_add_16be( vide, 0 ); // reserved;
919 for( i = 0; i < 3; i++ )
921 bo_add_32be( vide, 0 ); // predefined;
924 bo_add_16be( vide, p_stream->fmt.video.i_width ); // i_width
925 bo_add_16be( vide, p_stream->fmt.video.i_height ); // i_height
927 bo_add_32be( vide, 0x00480000 ); // h 72dpi
928 bo_add_32be( vide, 0x00480000 ); // v 72dpi
930 bo_add_32be( vide, 0 ); // data size, always 0
931 bo_add_16be( vide, 1 ); // frames count per sample
934 for( i = 0; i < 32; i++ )
939 bo_add_16be( vide, 0x18 ); // depth
940 bo_add_16be( vide, 0xffff ); // predefined
942 /* add an ES Descriptor */
943 switch( p_stream->fmt.i_codec )
945 case VLC_FOURCC('m','p','4','v'):
946 case VLC_FOURCC('m','p','g','v'):
948 bo_t *esds = GetESDS( p_stream );
951 box_gather( vide, esds );
955 case VLC_FOURCC('S','V','Q','3'):
957 bo_t *esds = GetSVQ3Tag( p_stream );
960 box_gather( vide, esds );
973 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
975 sout_mux_sys_t *p_sys = p_mux->p_sys;
976 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
977 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz, *stss;
978 uint32_t i_timescale;
979 int64_t i_dts, i_dts_q;
981 stbl = box_new( "stbl" );
983 /* sample description */
984 stsd = box_full_new( "stsd", 0, 0 );
985 bo_add_32be( stsd, 1 );
986 if( p_stream->fmt.i_cat == AUDIO_ES )
988 bo_t *soun = GetSounBox( p_mux, p_stream );
989 box_gather( stsd, soun );
991 else if( p_stream->fmt.i_cat == VIDEO_ES )
993 bo_t *vide = GetVideBox( p_mux, p_stream );
994 box_gather( stsd, vide );
998 /* chunk offset table */
999 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
1001 /* 64 bits version */
1002 p_stream->b_stco64 = VLC_TRUE;
1003 stco = box_full_new( "co64", 0, 0 );
1007 /* 32 bits version */
1008 p_stream->b_stco64 = VLC_FALSE;
1009 stco = box_full_new( "stco", 0, 0 );
1011 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
1013 /* sample to chunk table */
1014 stsc = box_full_new( "stsc", 0, 0 );
1015 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
1017 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
1018 i < p_stream->i_entry_count; i_chunk++ )
1022 if( p_stream->b_stco64 )
1023 bo_add_64be( stco, p_stream->entry[i].i_pos );
1025 bo_add_32be( stco, p_stream->entry[i].i_pos );
1027 while( i < p_stream->i_entry_count )
1029 if( i + 1 < p_stream->i_entry_count &&
1030 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
1031 != p_stream->entry[i + 1].i_pos )
1040 /* Add entry to the stsc table */
1041 if( i_stsc_last_val != i - i_first )
1043 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
1044 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
1045 bo_add_32be( stsc, 1 ); // sample-descr-index
1046 i_stsc_last_val = i - i_first;
1051 /* Fix stco entry count */
1052 bo_fix_32be( stco, 12, i_chunk );
1053 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
1056 /* Fix stsc entry count */
1057 bo_fix_32be( stsc, 12, i_stsc_entries );
1061 stts = box_full_new( "stts", 0, 0 );
1062 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
1064 if( p_stream->fmt.i_cat == AUDIO_ES )
1065 i_timescale = p_stream->fmt.audio.i_rate;
1069 /* first, create quantified length */
1070 for( i = 0, i_dts = 0, i_dts_q = 0; i < p_stream->i_entry_count; i++ )
1072 int64_t i_dts_deq = i_dts_q * I64C(1000000) / (int64_t)i_timescale;
1073 int64_t i_delta = p_stream->entry[i].i_length + i_dts - i_dts_deq;
1075 i_dts += p_stream->entry[i].i_length;
1077 p_stream->entry[i].i_length =
1078 i_delta * (int64_t)i_timescale / I64C(1000000);
1080 i_dts_q += p_stream->entry[i].i_length;
1082 /* then write encoded table */
1083 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
1086 int64_t i_delta = p_stream->entry[i].i_length;
1088 while( i < p_stream->i_entry_count )
1091 if( i >= p_stream->i_entry_count ||
1092 p_stream->entry[i].i_length != i_delta )
1098 bo_add_32be( stts, i - i_first ); // sample-count
1099 bo_add_32be( stts, i_delta ); // sample-delta
1101 bo_fix_32be( stts, 12, i_index );
1104 /* FIXME add ctts ?? FIXME */
1106 stsz = box_full_new( "stsz", 0, 0 );
1107 bo_add_32be( stsz, 0 ); // sample-size
1108 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
1109 for( i = 0; i < p_stream->i_entry_count; i++ )
1111 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
1115 /* create stss table */
1117 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i++ )
1119 if( p_stream->entry[i].i_flags &
1120 (BLOCK_FLAG_TYPE_I << SOUT_BUFFER_FLAGS_BLOCK_SHIFT) )
1124 stss = box_full_new( "stss", 0, 0 );
1125 bo_add_32be( stss, 0 ); /* fixed later */
1127 bo_add_32be( stss, 1 + i );
1133 bo_fix_32be( stss, 12, i_index );
1137 /* Now gather all boxes into stbl */
1138 box_gather( stbl, stsd );
1139 box_gather( stbl, stts );
1142 box_gather( stbl, stss );
1144 box_gather( stbl, stsc );
1145 box_gather( stbl, stsz );
1146 p_stream->i_stco_pos = stbl->i_buffer + 16;
1147 box_gather( stbl, stco );
1155 static int64_t get_timestamp();
1157 static uint32_t mvhd_matrix[9] =
1158 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1160 static bo_t *GetMoovBox( sout_mux_t *p_mux )
1162 sout_mux_sys_t *p_sys = p_mux->p_sys;
1167 uint32_t i_movie_timescale = 90000;
1168 int64_t i_movie_duration = 0;
1170 moov = box_new( "moov" );
1172 /* Create general info */
1173 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1175 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1176 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
1178 msg_Dbg( p_mux, "movie duration %ds",
1179 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
1181 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
1183 /* *** add /moov/mvhd *** */
1184 if( !p_sys->b_64_ext )
1186 mvhd = box_full_new( "mvhd", 0, 0 );
1187 bo_add_32be( mvhd, get_timestamp() ); // creation time
1188 bo_add_32be( mvhd, get_timestamp() ); // modification time
1189 bo_add_32be( mvhd, i_movie_timescale); // timescale
1190 bo_add_32be( mvhd, i_movie_duration ); // duration
1194 mvhd = box_full_new( "mvhd", 1, 0 );
1195 bo_add_64be( mvhd, get_timestamp() ); // creation time
1196 bo_add_64be( mvhd, get_timestamp() ); // modification time
1197 bo_add_32be( mvhd, i_movie_timescale); // timescale
1198 bo_add_64be( mvhd, i_movie_duration ); // duration
1200 bo_add_32be( mvhd, 0x10000 ); // rate
1201 bo_add_16be( mvhd, 0x100 ); // volume
1202 bo_add_16be( mvhd, 0 ); // reserved
1203 for( i = 0; i < 2; i++ )
1205 bo_add_32be( mvhd, 0 ); // reserved
1207 for( i = 0; i < 9; i++ )
1209 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
1211 for( i = 0; i < 6; i++ )
1213 bo_add_32be( mvhd, 0 ); // pre-defined
1216 /* Next available track id */
1217 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
1220 box_gather( moov, mvhd );
1222 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1224 mp4_stream_t *p_stream;
1225 uint32_t i_timescale;
1227 bo_t *trak, *tkhd, *edts, *elst, *mdia, *mdhd, *hdlr;
1228 bo_t *minf, *dinf, *dref, *url, *stbl;
1230 p_stream = p_sys->pp_streams[i_trak];
1232 if( p_stream->fmt.i_cat != AUDIO_ES &&
1233 p_stream->fmt.i_cat != VIDEO_ES )
1235 msg_Err( p_mux, "FIXME ignoring trak (noaudio&&novideo)" );
1239 if( p_stream->fmt.i_cat == AUDIO_ES )
1240 i_timescale = p_stream->fmt.audio.i_rate;
1244 /* *** add /moov/trak *** */
1245 trak = box_new( "trak" );
1247 /* *** add /moov/trak/tkhd *** */
1248 if( !p_sys->b_64_ext )
1251 tkhd = box_full_new( "tkhd", 0, 0x0f );
1253 tkhd = box_full_new( "tkhd", 0, 1 );
1255 bo_add_32be( tkhd, get_timestamp() ); // creation time
1256 bo_add_32be( tkhd, get_timestamp() ); // modification time
1257 bo_add_32be( tkhd, p_stream->i_track_id );
1258 bo_add_32be( tkhd, 0 ); // reserved 0
1259 bo_add_32be( tkhd, p_stream->i_duration *
1260 (int64_t)i_movie_timescale /
1261 (mtime_t)1000000 ); // duration
1266 tkhd = box_full_new( "tkhd", 1, 0x0f );
1268 tkhd = box_full_new( "tkhd", 1, 1 );
1270 bo_add_64be( tkhd, get_timestamp() ); // creation time
1271 bo_add_64be( tkhd, get_timestamp() ); // modification time
1272 bo_add_32be( tkhd, p_stream->i_track_id );
1273 bo_add_32be( tkhd, 0 ); // reserved 0
1274 bo_add_64be( tkhd, p_stream->i_duration *
1275 (int64_t)i_movie_timescale /
1276 (mtime_t)1000000 ); // duration
1279 for( i = 0; i < 2; i++ )
1281 bo_add_32be( tkhd, 0 ); // reserved
1283 bo_add_16be( tkhd, 0 ); // layer
1284 bo_add_16be( tkhd, 0 ); // pre-defined
1286 bo_add_16be( tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0 );
1287 bo_add_16be( tkhd, 0 ); // reserved
1288 for( i = 0; i < 9; i++ )
1290 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
1292 if( p_stream->fmt.i_cat == AUDIO_ES )
1294 bo_add_32be( tkhd, 0 ); // width (presentation)
1295 bo_add_32be( tkhd, 0 ); // height(presentation)
1299 // width (presentation)
1300 bo_add_32be( tkhd, p_stream->fmt.video.i_aspect *
1301 p_stream->fmt.video.i_height /
1302 VOUT_ASPECT_FACTOR << 16 );
1303 // height(presentation)
1304 bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
1307 box_gather( trak, tkhd );
1309 /* *** add /moov/trak/edts and elst */
1310 edts = box_new( "edts" );
1311 elst = box_full_new( "elst", p_sys->b_64_ext ? 1 : 0, 0 );
1312 if( p_stream->i_dts_start > p_sys->i_dts_start )
1314 bo_add_32be( elst, 2 );
1316 if( p_sys->b_64_ext )
1318 bo_add_64be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1319 i_movie_timescale / I64C(1000000) );
1320 bo_add_64be( elst, -1 );
1324 bo_add_32be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1325 i_movie_timescale / I64C(1000000) );
1326 bo_add_32be( elst, -1 );
1328 bo_add_16be( elst, 1 );
1329 bo_add_16be( elst, 0 );
1333 bo_add_32be( elst, 1 );
1335 if( p_sys->b_64_ext )
1337 bo_add_64be( elst, p_stream->i_duration *
1338 i_movie_timescale / I64C(1000000) );
1339 bo_add_64be( elst, 0 );
1343 bo_add_32be( elst, p_stream->i_duration *
1344 i_movie_timescale / I64C(1000000) );
1345 bo_add_32be( elst, 0 );
1347 bo_add_16be( elst, 1 );
1348 bo_add_16be( elst, 0 );
1351 box_gather( edts, elst );
1353 box_gather( trak, edts );
1355 /* *** add /moov/trak/mdia *** */
1356 mdia = box_new( "mdia" );
1359 if( !p_sys->b_64_ext )
1361 mdhd = box_full_new( "mdhd", 0, 0 );
1362 bo_add_32be( mdhd, get_timestamp() ); // creation time
1363 bo_add_32be( mdhd, get_timestamp() ); // modification time
1364 bo_add_32be( mdhd, i_timescale); // timescale
1365 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1366 (mtime_t)1000000 ); // duration
1370 mdhd = box_full_new( "mdhd", 1, 0 );
1371 bo_add_64be( mdhd, get_timestamp() ); // creation time
1372 bo_add_64be( mdhd, get_timestamp() ); // modification time
1373 bo_add_32be( mdhd, i_timescale); // timescale
1374 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1375 (mtime_t)1000000 ); // duration
1378 if( p_stream->fmt.psz_language )
1380 char *psz = p_stream->fmt.psz_language;
1381 const iso639_lang_t *pl = NULL;
1382 uint16_t lang = 0x0;
1384 if( strlen( psz ) == 2 )
1386 pl = GetLang_1( psz );
1388 else if( strlen( psz ) == 3 )
1390 pl = GetLang_2B( psz );
1391 if( !strcmp( pl->psz_iso639_1, "??" ) )
1393 pl = GetLang_2T( psz );
1396 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1398 lang = ( ( pl->psz_iso639_2T[0] - 0x60 ) << 10 ) |
1399 ( ( pl->psz_iso639_2T[1] - 0x60 ) << 5 ) |
1400 ( ( pl->psz_iso639_2T[2] - 0x60 ) );
1402 bo_add_16be( mdhd, lang ); // language
1406 bo_add_16be( mdhd, 0 ); // language
1408 bo_add_16be( mdhd, 0 ); // predefined
1410 box_gather( mdia, mdhd );
1412 /* handler reference */
1413 hdlr = box_full_new( "hdlr", 0, 0 );
1415 bo_add_fourcc( hdlr, "mhlr" ); // media handler
1416 if( p_stream->fmt.i_cat == AUDIO_ES )
1418 bo_add_fourcc( hdlr, "soun" );
1422 bo_add_fourcc( hdlr, "vide" );
1425 bo_add_32be( hdlr, 0 ); // reserved
1426 bo_add_32be( hdlr, 0 ); // reserved
1427 bo_add_32be( hdlr, 0 ); // reserved
1429 bo_add_8( hdlr, 12 );
1430 if( p_stream->fmt.i_cat == AUDIO_ES )
1431 bo_add_mem( hdlr, 12, "SoundHandler" );
1433 bo_add_mem( hdlr, 12, "VideoHandler" );
1436 box_gather( mdia, hdlr );
1439 minf = box_new( "minf" );
1442 if( p_stream->fmt.i_cat == AUDIO_ES )
1446 smhd = box_full_new( "smhd", 0, 0 );
1447 bo_add_16be( smhd, 0 ); // balance
1448 bo_add_16be( smhd, 0 ); // reserved
1451 box_gather( minf, smhd );
1453 else if( p_stream->fmt.i_cat == VIDEO_ES )
1457 vmhd = box_full_new( "vmhd", 0, 1 );
1458 bo_add_16be( vmhd, 0 ); // graphicsmode
1459 for( i = 0; i < 3; i++ )
1461 bo_add_16be( vmhd, 0 ); // opcolor
1465 box_gather( minf, vmhd );
1469 dinf = box_new( "dinf" );
1470 dref = box_full_new( "dref", 0, 0 );
1471 bo_add_32be( dref, 1 );
1472 url = box_full_new( "url ", 0, 0x01 );
1474 box_gather( dref, url );
1476 box_gather( dinf, dref );
1478 /* append dinf to mdia */
1480 box_gather( minf, dinf );
1483 stbl = GetStblBox( p_mux, p_stream );
1485 /* append stbl to minf */
1486 p_stream->i_stco_pos += minf->i_buffer;
1487 box_gather( minf, stbl );
1489 /* append minf to mdia */
1491 p_stream->i_stco_pos += mdia->i_buffer;
1492 box_gather( mdia, minf );
1494 /* append mdia to trak */
1496 p_stream->i_stco_pos += trak->i_buffer;
1497 box_gather( trak, mdia );
1499 /* append trak to moov */
1501 p_stream->i_stco_pos += moov->i_buffer;
1502 box_gather( moov, trak );
1505 /* Add user data tags */
1506 box_gather( moov, GetUdtaTag( p_mux ) );
1512 /****************************************************************************/
1514 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1519 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1520 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1524 p_bo->i_buffer_size = i_size;
1525 p_bo->p_buffer = p_buffer;
1528 p_bo->b_grow = b_grow;
1532 static void bo_add_8( bo_t *p_bo, uint8_t i )
1534 if( p_bo->i_buffer < p_bo->i_buffer_size )
1536 p_bo->p_buffer[p_bo->i_buffer] = i;
1538 else if( p_bo->b_grow )
1540 p_bo->i_buffer_size += 1024;
1541 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1543 p_bo->p_buffer[p_bo->i_buffer] = i;
1549 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1551 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1552 bo_add_8( p_bo, i &0xff );
1555 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1557 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1558 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1559 bo_add_8( p_bo, ( i &0xff ) );
1561 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1563 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1564 bo_add_16be( p_bo, i &0xffff );
1567 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1569 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1570 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1571 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1572 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1575 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1577 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1578 bo_add_32be( p_bo, i &0xffffffff );
1581 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1583 bo_add_8( p_bo, fcc[0] );
1584 bo_add_8( p_bo, fcc[1] );
1585 bo_add_8( p_bo, fcc[2] );
1586 bo_add_8( p_bo, fcc[3] );
1589 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1593 for( i = 0; i < i_size; i++ )
1595 bo_add_8( p_bo, p_mem[i] );
1599 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
1605 vals[3] = (unsigned char)(i_length & 0x7f);
1607 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
1609 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
1611 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
1613 bo_add_8( p_bo, tag );
1615 if( i_size < 0x00000080 )
1617 bo_add_8( p_bo, vals[3] );
1619 else if( i_size < 0x00004000 )
1621 bo_add_8( p_bo, vals[2] );
1622 bo_add_8( p_bo, vals[3] );
1624 else if( i_size < 0x00200000 )
1626 bo_add_8( p_bo, vals[1] );
1627 bo_add_8( p_bo, vals[2] );
1628 bo_add_8( p_bo, vals[3] );
1630 else if( i_size < 0x10000000 )
1632 bo_add_8( p_bo, vals[0] );
1633 bo_add_8( p_bo, vals[1] );
1634 bo_add_8( p_bo, vals[2] );
1635 bo_add_8( p_bo, vals[3] );
1639 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1643 for( i = 0; i < p_bo2->i_buffer; i++ )
1645 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1649 static bo_t * box_new( char *fcc )
1653 if( ( box = malloc( sizeof( bo_t ) ) ) )
1655 bo_init( box, 0, NULL, VLC_TRUE );
1657 bo_add_32be ( box, 0 );
1658 bo_add_fourcc( box, fcc );
1664 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
1668 if( ( box = malloc( sizeof( bo_t ) ) ) )
1670 bo_init( box, 0, NULL, VLC_TRUE );
1672 bo_add_32be ( box, 0 );
1673 bo_add_fourcc( box, fcc );
1674 bo_add_8 ( box, v );
1675 bo_add_24be ( box, f );
1681 static void box_fix( bo_t *box )
1685 memcpy( &box_tmp, box, sizeof( bo_t ) );
1687 box_tmp.i_buffer = 0;
1688 bo_add_32be( &box_tmp, box->i_buffer );
1691 static void box_free( bo_t *box )
1695 free( box->p_buffer );
1701 static void box_gather ( bo_t *box, bo_t *box2 )
1703 bo_add_bo( box, box2 );
1707 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
1709 sout_buffer_t *p_buf;
1711 p_buf = sout_BufferNew( p_sout, box->i_buffer );
1712 if( box->i_buffer > 0 )
1714 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
1717 p_buf->i_size = box->i_buffer;
1722 static void box_send( sout_mux_t *p_mux, bo_t *box )
1724 sout_buffer_t *p_buf;
1726 p_buf = bo_to_sout( p_mux->p_sout, box );
1729 sout_AccessOutWrite( p_mux->p_access, p_buf );
1732 static int64_t get_timestamp()
1734 int64_t i_timestamp = 0;
1737 i_timestamp = time(NULL);
1738 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
1739 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60