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>
41 /*****************************************************************************
43 *****************************************************************************/
44 #define FASTSTART_TEXT N_("Create \"Fast start\" files")
45 #define FASTSTART_LONGTEXT N_( \
46 "When this option is turned on, \"Fast start\" files will be created. " \
47 "(\"Fast start\" files are optimized for download, allowing the user " \
48 "to start previewing the file while it is downloading).")
49 static int Open ( vlc_object_t * );
50 static void Close ( vlc_object_t * );
53 set_description( _("MP4/MOV muxer") );
55 add_bool( "mp4-faststart", 1, NULL, FASTSTART_TEXT, FASTSTART_LONGTEXT,
58 set_capability( "sout mux", 5 );
59 add_shortcut( "mp4" );
60 add_shortcut( "mov" );
61 set_callbacks( Open, Close );
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Capability(sout_mux_t *, int, void *, void * );
68 static int AddStream( sout_mux_t *, sout_input_t * );
69 static int DelStream( sout_mux_t *, sout_input_t * );
70 static int Mux ( sout_mux_t * );
72 /*****************************************************************************
74 *****************************************************************************/
92 unsigned int i_entry_count;
93 unsigned int i_entry_max;
101 /* for later stco fix-up (fast start files) */
107 struct sout_mux_sys_t
111 vlc_bool_t b_fast_start;
119 mp4_stream_t **pp_streams;
132 static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
133 static void bo_add_8 ( bo_t *, uint8_t );
134 static void bo_add_16be ( bo_t *, uint16_t );
135 static void bo_add_24be ( bo_t *, uint32_t );
136 static void bo_add_32be ( bo_t *, uint32_t );
137 static void bo_add_64be ( bo_t *, uint64_t );
138 static void bo_add_fourcc(bo_t *, char * );
139 static void bo_add_bo ( bo_t *, bo_t * );
140 static void bo_add_mem ( bo_t *, int , uint8_t * );
141 static void bo_add_descr( bo_t *, uint8_t , uint32_t );
143 static void bo_fix_32be ( bo_t *, int , uint32_t );
145 static bo_t *box_new ( char *fcc );
146 static bo_t *box_full_new( char *fcc, uint8_t v, uint32_t f );
147 static void box_fix ( bo_t *box );
148 static void box_free ( bo_t *box );
149 static void box_gather ( bo_t *box, bo_t *box2 );
151 static void box_send( sout_mux_t *p_mux, bo_t *box );
153 static sout_buffer_t *bo_to_sout( sout_instance_t *p_sout, bo_t *box );
155 static bo_t *GetMoovBox( sout_mux_t *p_mux );
157 /*****************************************************************************
159 *****************************************************************************/
160 static int Open( vlc_object_t *p_this )
162 sout_mux_t *p_mux = (sout_mux_t*)p_this;
163 sout_mux_sys_t *p_sys;
166 p_sys = malloc( sizeof( sout_mux_sys_t ) );
168 p_sys->i_nb_streams = 0;
169 p_sys->pp_streams = NULL;
170 p_sys->i_mdat_pos = 0;
171 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
172 p_sys->i_dts_start = 0;
174 msg_Dbg( p_mux, "Open" );
176 p_mux->pf_capacity = Capability;
177 p_mux->pf_addstream = AddStream;
178 p_mux->pf_delstream = DelStream;
180 p_mux->p_sys = p_sys;
184 /* Now add ftyp header */
185 box = box_new( "ftyp" );
186 bo_add_fourcc( box, "isom" );
187 bo_add_32be ( box, 0 );
188 bo_add_fourcc( box, "mp41" );
191 p_sys->i_pos += box->i_buffer;
192 p_sys->i_mdat_pos = p_sys->i_pos;
194 box_send( p_mux, box );
198 * Quicktime actually doesn't like the 64 bits extensions !!! */
199 p_sys->b_64_ext = VLC_FALSE;
201 /* Now add mdat header */
202 box = box_new( "mdat" );
203 bo_add_64be ( box, 0 ); // enough to store an extended size
205 p_sys->i_pos += box->i_buffer;
207 box_send( p_mux, box );
212 /*****************************************************************************
214 *****************************************************************************/
215 static void Close( vlc_object_t * p_this )
217 sout_mux_t *p_mux = (sout_mux_t*)p_this;
218 sout_mux_sys_t *p_sys = p_mux->p_sys;
219 sout_buffer_t *p_hdr;
226 msg_Dbg( p_mux, "Close" );
228 /* Update mdat size */
229 bo_init( &bo, 0, NULL, VLC_TRUE );
230 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
233 bo_add_32be ( &bo, 1 );
234 bo_add_fourcc( &bo, "mdat" );
235 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
239 bo_add_32be ( &bo, 8 );
240 bo_add_fourcc( &bo, "wide" );
241 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
242 bo_add_fourcc( &bo, "mdat" );
244 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
247 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
248 sout_AccessOutWrite( p_mux->p_access, p_hdr );
250 /* Create MOOV header */
251 i_moov_pos = p_sys->i_pos;
252 moov = GetMoovBox( p_mux );
254 /* Check we need to create "fast start" files */
255 var_Create( p_this, "mp4-faststart", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
256 var_Get( p_this, "mp4-faststart", &val );
257 p_sys->b_fast_start = val.b_bool;
258 while( p_sys->b_fast_start )
260 /* Move data to the end of the file so we can fit the moov header
262 sout_buffer_t *p_buf;
263 int64_t i_chunk, i_size = p_sys->i_pos - p_sys->i_mdat_pos;
264 int i_moov_size = moov->i_buffer;
268 i_chunk = __MIN( 32768, i_size );
269 p_buf = sout_BufferNew( p_mux->p_sout, i_chunk );
270 sout_AccessOutSeek( p_mux->p_access,
271 p_sys->i_mdat_pos + i_size - i_chunk );
272 if( sout_AccessOutRead( p_mux->p_access, p_buf ) < i_chunk )
274 msg_Warn( p_this, "read() not supported by acces output, "
275 "won't create a fast start file" );
276 p_sys->b_fast_start = VLC_FALSE;
279 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos + i_size +
280 i_moov_size - i_chunk );
281 sout_AccessOutWrite( p_mux->p_access, p_buf );
285 if( !p_sys->b_fast_start ) break;
287 /* Fix-up samples to chunks table in MOOV header */
288 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
290 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
294 moov->i_buffer = p_stream->i_stco_pos;
295 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
297 if( p_stream->b_stco64 )
298 bo_add_64be( moov, p_stream->entry[i].i_pos + i_moov_size);
300 bo_add_32be( moov, p_stream->entry[i].i_pos + i_moov_size);
302 while( i < p_stream->i_entry_count )
304 if( i + 1 < p_stream->i_entry_count &&
305 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
306 != p_stream->entry[i + 1].i_pos )
317 moov->i_buffer = i_moov_size;
318 i_moov_pos = p_sys->i_mdat_pos;
319 p_sys->b_fast_start = VLC_FALSE;
322 /* Write MOOV header */
323 sout_AccessOutSeek( p_mux->p_access, i_moov_pos );
324 box_send( p_mux, moov );
327 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
329 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
331 es_format_Clean( &p_stream->fmt );
332 free( p_stream->entry );
335 if( p_sys->i_nb_streams ) free( p_sys->pp_streams );
339 /*****************************************************************************
341 *****************************************************************************/
342 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
347 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
348 *(vlc_bool_t*)p_answer = VLC_TRUE;
349 return SOUT_MUX_CAP_ERR_OK;
351 case SOUT_MUX_CAP_GET_ADD_STREAM_WAIT:
352 *(vlc_bool_t*)p_answer = VLC_TRUE;
353 return( SOUT_MUX_CAP_ERR_OK );
356 return SOUT_MUX_CAP_ERR_UNIMPLEMENTED;
360 /*****************************************************************************
362 *****************************************************************************/
363 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
365 sout_mux_sys_t *p_sys = p_mux->p_sys;
366 mp4_stream_t *p_stream;
368 switch( p_input->p_fmt->i_codec )
370 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
371 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
372 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
373 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
374 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
375 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
376 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
377 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
380 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
381 (char*)&p_input->p_fmt->i_codec );
385 p_stream = malloc( sizeof( mp4_stream_t ) );
386 es_format_Copy( &p_stream->fmt, p_input->p_fmt );
387 p_stream->i_track_id = p_sys->i_nb_streams + 1;
388 p_stream->i_length_neg = 0;
389 p_stream->i_entry_count = 0;
390 p_stream->i_entry_max = 1000;
392 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
393 p_stream->i_dts_start = 0;
394 p_stream->i_duration = 0;
396 p_input->p_sys = p_stream;
398 msg_Dbg( p_mux, "adding input" );
400 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
404 /*****************************************************************************
406 *****************************************************************************/
407 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
409 msg_Dbg( p_mux, "removing input" );
413 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
419 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
421 sout_fifo_t *p_fifo = p_mux->pp_inputs[i]->p_fifo;
422 sout_buffer_t *p_buf;
424 if( p_fifo->i_depth <= 1 )
426 return -1; // wait that all fifo have at least 2 packets
429 p_buf = sout_FifoShow( p_fifo );
430 if( i_stream < 0 || p_buf->i_dts < i_dts )
432 i_dts = p_buf->i_dts;
438 *pi_stream = i_stream;
447 /*****************************************************************************
449 *****************************************************************************/
450 static int Mux( sout_mux_t *p_mux )
452 sout_mux_sys_t *p_sys = p_mux->p_sys;
456 sout_input_t *p_input;
458 mp4_stream_t *p_stream;
459 sout_buffer_t *p_data;
462 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
464 return( VLC_SUCCESS );
467 p_input = p_mux->pp_inputs[i_stream];
468 p_stream = (mp4_stream_t*)p_input->p_sys;
470 p_data = sout_FifoGet( p_input->p_fifo );
471 if( p_input->p_fifo->i_depth > 0 )
473 sout_buffer_t *p_next = sout_FifoShow( p_input->p_fifo );
474 int64_t i_diff = p_next->i_dts - p_data->i_dts;
476 if( i_diff < I64C(1000000 ) ) /* protection */
478 p_data->i_length = i_diff;
481 if( p_data->i_length <= 0 )
483 msg_Warn( p_mux, "i_length <= 0" );
484 p_stream->i_length_neg += p_data->i_length - 1;
485 p_data->i_length = 1;
487 else if( p_stream->i_length_neg < 0 )
489 int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
491 p_data->i_length -= i_recover;
492 p_stream->i_length_neg += i_recover;
495 /* Save starting time */
496 if( p_stream->i_entry_count == 0 )
498 p_stream->i_dts_start = p_data->i_dts;
500 /* Update global dts_start */
501 if( p_sys->i_dts_start <= 0 ||
502 p_stream->i_dts_start < p_sys->i_dts_start )
504 p_sys->i_dts_start = p_stream->i_dts_start;
508 /* add index entry */
509 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
510 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_size;
511 p_stream->entry[p_stream->i_entry_count].i_pts_dts= __MAX( p_data->i_pts - p_data->i_dts, 0 );
512 p_stream->entry[p_stream->i_entry_count].i_length = p_data->i_length;
513 p_stream->entry[p_stream->i_entry_count].i_flags = p_data->i_flags;
515 p_stream->i_entry_count++;
516 if( p_stream->i_entry_count >= p_stream->i_entry_max )
518 p_stream->i_entry_max += 1000;
520 realloc( p_stream->entry,
521 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
525 p_stream->i_duration += p_data->i_length;
526 p_sys->i_pos += p_data->i_size;
529 sout_AccessOutWrite( p_mux->p_access, p_data );
532 return( VLC_SUCCESS );
535 /*****************************************************************************
537 *****************************************************************************/
538 static int GetDescrLength( int i_size )
540 if( i_size < 0x00000080 )
542 else if( i_size < 0x00004000 )
544 else if( i_size < 0x00200000 )
550 static bo_t *GetESDS( mp4_stream_t *p_stream )
554 int i_object_type_indication;
555 int i_decoder_specific_info_size;
557 if( p_stream->fmt.i_extra > 0 )
559 i_decoder_specific_info_size =
560 GetDescrLength( p_stream->fmt.i_extra );
564 i_decoder_specific_info_size = 0;
567 esds = box_full_new( "esds", 0, 0 );
570 bo_add_descr( esds, 0x03, 3 +
571 GetDescrLength( 13 + i_decoder_specific_info_size ) +
572 GetDescrLength( 1 ) );
573 bo_add_16be( esds, p_stream->i_track_id );
574 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
576 /* DecoderConfigDescr */
577 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
579 switch( p_stream->fmt.i_codec )
581 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
582 i_object_type_indication = 0x20;
584 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
585 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
586 i_object_type_indication = 0x60;
588 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
589 /* FIXME for mpeg2-aac == 0x66->0x68 */
590 i_object_type_indication = 0x40;
592 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
593 i_object_type_indication =
594 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
597 i_object_type_indication = 0x00;
600 i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
602 bo_add_8 ( esds, i_object_type_indication );
603 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
604 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
605 bo_add_32be( esds, 0x7fffffff ); // maxBitrate
606 bo_add_32be( esds, 0 ); // avgBitrate
608 if( p_stream->fmt.i_extra > 0 )
612 /* DecoderSpecificInfo */
613 bo_add_descr( esds, 0x05, p_stream->fmt.i_extra );
615 for( i = 0; i < p_stream->fmt.i_extra; i++ )
617 bo_add_8( esds, ((uint8_t*)p_stream->fmt.p_extra)[i] );
621 /* SL_Descr mandatory */
622 bo_add_descr( esds, 0x06, 1 );
623 bo_add_8 ( esds, 0x02 ); // sl_predefined
630 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
635 wave = box_new( "wave" );
637 box = box_new( "frma" );
638 bo_add_fourcc( box, "mp4a" );
640 box_gather( wave, box );
642 box = box_new( "mp4a" );
643 bo_add_32be( box, 0 );
645 box_gather( wave, box );
647 box = GetESDS( p_stream );
649 box_gather( wave, box );
651 box = box_new( "srcq" );
652 bo_add_32be( box, 0x40 );
654 box_gather( wave, box );
657 bo_add_32be( wave, 8 ); /* new empty box */
658 bo_add_32be( wave, 0 ); /* box label */
665 /* TODO: No idea about these values */
666 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
668 bo_t *smi = box_new( "SMI " );
670 if( p_stream->fmt.i_extra > 0x4e )
672 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
673 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
675 while( p + 8 < p_end )
677 int i_size = GetDWBE( p );
680 /* FIXME handle 1 as long size */
683 if( !strncmp( &p[4], "SMI ", 4 ) )
685 bo_add_mem( smi, p_end - p - 8, &p[8] );
692 /* Create a dummy one in fallback */
693 bo_add_fourcc( smi, "SEQH" );
694 bo_add_32be( smi, 0x5 );
695 bo_add_32be( smi, 0xe2c0211d );
696 bo_add_8( smi, 0xc0 );
702 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
704 sout_mux_sys_t *p_sys = p_mux->p_sys;
705 bo_t *udta = box_new( "udta" );
709 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
711 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
713 if( p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','v') ||
714 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
716 bo_t *box = box_new( "\251req" );
718 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
719 bo_add_16be( box, 0 );
720 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
721 "QuickTime 6.0 or greater" );
723 box_gather( udta, box );
730 bo_t *box = box_new( "\251enc" );
732 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
733 bo_add_16be( box, 0 );
734 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
735 PACKAGE_STRING " stream output" );
737 box_gather( udta, box );
744 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
746 sout_mux_sys_t *p_sys = p_mux->p_sys;
747 vlc_bool_t b_descr = VLC_FALSE;
752 switch( p_stream->fmt.i_codec )
754 case VLC_FOURCC('m','p','4','a'):
755 memcpy( fcc, "mp4a", 4 );
759 case VLC_FOURCC('m','p','g','a'):
761 memcpy( fcc, ".mp3", 4 );
764 memcpy( fcc, "mp4a", 4 );
770 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
774 soun = box_new( fcc );
775 for( i = 0; i < 6; i++ )
777 bo_add_8( soun, 0 ); // reserved;
779 bo_add_16be( soun, 1 ); // data-reference-index
781 /* SoundDescription */
783 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
785 bo_add_16be( soun, 1 ); // version 1;
789 bo_add_16be( soun, 0 ); // version 0;
791 bo_add_16be( soun, 0 ); // revision level (0)
792 bo_add_32be( soun, 0 ); // vendor
794 bo_add_16be( soun, p_stream->fmt.audio.i_channels );
796 bo_add_16be( soun, p_stream->fmt.audio.i_bitspersample ?
797 p_stream->fmt.audio.i_bitspersample : 16 );
798 bo_add_16be( soun, -2 ); // compression id
799 bo_add_16be( soun, 0 ); // packet size (0)
800 bo_add_16be( soun, p_stream->fmt.audio.i_rate ); // sampleratehi
801 bo_add_16be( soun, 0 ); // sampleratelo
803 /* Extended data for SoundDescription V1 */
805 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
807 /* samples per packet */
808 bo_add_32be( soun, p_stream->fmt.audio.i_frame_length );
809 bo_add_32be( soun, 1536 ); /* bytes per packet */
810 bo_add_32be( soun, 2 ); /* bytes per frame */
811 /* bytes per sample */
812 bo_add_32be( soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
815 /* Add an ES Descriptor */
821 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
823 box = GetWaveTag( p_stream );
827 box = GetESDS( p_stream );
830 box_gather( soun, box );
838 static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
845 switch( p_stream->fmt.i_codec )
847 case VLC_FOURCC('m','p','4','v'):
848 case VLC_FOURCC('m','p','g','v'):
849 memcpy( fcc, "mp4v", 4 );
852 case VLC_FOURCC('M','J','P','G'):
853 memcpy( fcc, "mjpa", 4 );
856 case VLC_FOURCC('S','V','Q','1'):
857 memcpy( fcc, "SVQ1", 4 );
860 case VLC_FOURCC('S','V','Q','3'):
861 memcpy( fcc, "SVQ3", 4 );
865 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
869 vide = box_new( fcc );
870 for( i = 0; i < 6; i++ )
872 bo_add_8( vide, 0 ); // reserved;
874 bo_add_16be( vide, 1 ); // data-reference-index
876 bo_add_16be( vide, 0 ); // predefined;
877 bo_add_16be( vide, 0 ); // reserved;
878 for( i = 0; i < 3; i++ )
880 bo_add_32be( vide, 0 ); // predefined;
883 bo_add_16be( vide, p_stream->fmt.video.i_width ); // i_width
884 bo_add_16be( vide, p_stream->fmt.video.i_height ); // i_height
886 bo_add_32be( vide, 0x00480000 ); // h 72dpi
887 bo_add_32be( vide, 0x00480000 ); // v 72dpi
889 bo_add_32be( vide, 0 ); // data size, always 0
890 bo_add_16be( vide, 1 ); // frames count per sample
893 for( i = 0; i < 32; i++ )
898 bo_add_16be( vide, 0x18 ); // depth
899 bo_add_16be( vide, 0xffff ); // predefined
901 /* add an ES Descriptor */
902 switch( p_stream->fmt.i_codec )
904 case VLC_FOURCC('m','p','4','v'):
905 case VLC_FOURCC('m','p','g','v'):
907 bo_t *esds = GetESDS( p_stream );
910 box_gather( vide, esds );
914 case VLC_FOURCC('S','V','Q','3'):
916 bo_t *esds = GetSVQ3Tag( p_stream );
919 box_gather( vide, esds );
932 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
934 sout_mux_sys_t *p_sys = p_mux->p_sys;
935 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
936 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz, *stss;
937 uint32_t i_timescale;
938 int64_t i_dts, i_dts_q;
940 stbl = box_new( "stbl" );
942 /* sample description */
943 stsd = box_full_new( "stsd", 0, 0 );
944 bo_add_32be( stsd, 1 );
945 if( p_stream->fmt.i_cat == AUDIO_ES )
947 bo_t *soun = GetSounBox( p_mux, p_stream );
948 box_gather( stsd, soun );
950 else if( p_stream->fmt.i_cat == VIDEO_ES )
952 bo_t *vide = GetVideBox( p_mux, p_stream );
953 box_gather( stsd, vide );
957 /* chunk offset table */
958 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
960 /* 64 bits version */
961 p_stream->b_stco64 = VLC_TRUE;
962 stco = box_full_new( "co64", 0, 0 );
966 /* 32 bits version */
967 p_stream->b_stco64 = VLC_FALSE;
968 stco = box_full_new( "stco", 0, 0 );
970 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
972 /* sample to chunk table */
973 stsc = box_full_new( "stsc", 0, 0 );
974 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
976 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
977 i < p_stream->i_entry_count; i_chunk++ )
981 if( p_stream->b_stco64 )
982 bo_add_64be( stco, p_stream->entry[i].i_pos );
984 bo_add_32be( stco, p_stream->entry[i].i_pos );
986 while( i < p_stream->i_entry_count )
988 if( i + 1 < p_stream->i_entry_count &&
989 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
990 != p_stream->entry[i + 1].i_pos )
999 /* Add entry to the stsc table */
1000 if( i_stsc_last_val != i - i_first )
1002 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
1003 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
1004 bo_add_32be( stsc, 1 ); // sample-descr-index
1005 i_stsc_last_val = i - i_first;
1010 /* Fix stco entry count */
1011 bo_fix_32be( stco, 12, i_chunk );
1012 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
1015 /* Fix stsc entry count */
1016 bo_fix_32be( stsc, 12, i_stsc_entries );
1020 stts = box_full_new( "stts", 0, 0 );
1021 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
1023 if( p_stream->fmt.i_cat == AUDIO_ES )
1024 i_timescale = p_stream->fmt.audio.i_rate;
1028 /* first, create quantified length */
1029 for( i = 0, i_dts = 0, i_dts_q = 0; i < p_stream->i_entry_count; i++ )
1031 int64_t i_dts_deq = i_dts_q * I64C(1000000) / (int64_t)i_timescale;
1032 int64_t i_delta = p_stream->entry[i].i_length + i_dts - i_dts_deq;
1034 i_dts += p_stream->entry[i].i_length;
1036 p_stream->entry[i].i_length = i_delta * (int64_t)i_timescale / I64C(1000000);;
1038 i_dts_q += p_stream->entry[i].i_length;
1040 /* then write encoded table */
1041 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
1044 int64_t i_delta = p_stream->entry[i].i_length;
1046 while( i < p_stream->i_entry_count )
1049 if( i >= p_stream->i_entry_count ||
1050 p_stream->entry[i].i_length != i_delta )
1056 bo_add_32be( stts, i - i_first ); // sample-count
1057 bo_add_32be( stts, i_delta ); // sample-delta
1059 bo_fix_32be( stts, 12, i_index );
1062 /* FIXME add ctts ?? FIXME */
1064 stsz = box_full_new( "stsz", 0, 0 );
1065 bo_add_32be( stsz, 0 ); // sample-size
1066 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
1067 for( i = 0; i < p_stream->i_entry_count; i++ )
1069 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
1073 /* create stss table */
1075 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i++ )
1077 if( p_stream->entry[i].i_flags & (BLOCK_FLAG_TYPE_I<<SOUT_BUFFER_FLAGS_BLOCK_SHIFT) )
1081 stss = box_full_new( "stss", 0, 0 );
1082 bo_add_32be( stss, 0 ); /* fixed later */
1084 bo_add_32be( stss, 1 + i );
1090 bo_fix_32be( stss, 12, i_index );
1094 /* Now gather all boxes into stbl */
1095 box_gather( stbl, stsd );
1096 box_gather( stbl, stts );
1099 box_gather( stbl, stss );
1101 box_gather( stbl, stsc );
1102 box_gather( stbl, stsz );
1103 p_stream->i_stco_pos = stbl->i_buffer + 16;
1104 box_gather( stbl, stco );
1112 static int64_t get_timestamp();
1114 static uint32_t mvhd_matrix[9] =
1115 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1117 static bo_t *GetMoovBox( sout_mux_t *p_mux )
1119 sout_mux_sys_t *p_sys = p_mux->p_sys;
1124 uint32_t i_movie_timescale = 90000;
1125 int64_t i_movie_duration = 0;
1127 moov = box_new( "moov" );
1129 /* Create general info */
1130 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1132 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1133 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
1135 msg_Dbg( p_mux, "movie duration %ds",
1136 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
1138 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
1140 /* *** add /moov/mvhd *** */
1141 if( !p_sys->b_64_ext )
1143 mvhd = box_full_new( "mvhd", 0, 0 );
1144 bo_add_32be( mvhd, get_timestamp() ); // creation time
1145 bo_add_32be( mvhd, get_timestamp() ); // modification time
1146 bo_add_32be( mvhd, i_movie_timescale); // timescale
1147 bo_add_32be( mvhd, i_movie_duration ); // duration
1151 mvhd = box_full_new( "mvhd", 1, 0 );
1152 bo_add_64be( mvhd, get_timestamp() ); // creation time
1153 bo_add_64be( mvhd, get_timestamp() ); // modification time
1154 bo_add_32be( mvhd, i_movie_timescale); // timescale
1155 bo_add_64be( mvhd, i_movie_duration ); // duration
1157 bo_add_32be( mvhd, 0x10000 ); // rate
1158 bo_add_16be( mvhd, 0x100 ); // volume
1159 bo_add_16be( mvhd, 0 ); // reserved
1160 for( i = 0; i < 2; i++ )
1162 bo_add_32be( mvhd, 0 ); // reserved
1164 for( i = 0; i < 9; i++ )
1166 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
1168 for( i = 0; i < 6; i++ )
1170 bo_add_32be( mvhd, 0 ); // pre-defined
1173 /* Next available track id */
1174 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
1177 box_gather( moov, mvhd );
1179 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1181 mp4_stream_t *p_stream;
1182 uint32_t i_timescale;
1184 bo_t *trak, *tkhd, *edts, *elst, *mdia, *mdhd, *hdlr;
1185 bo_t *minf, *dinf, *dref, *url, *stbl;
1187 p_stream = p_sys->pp_streams[i_trak];
1189 if( p_stream->fmt.i_cat != AUDIO_ES &&
1190 p_stream->fmt.i_cat != VIDEO_ES )
1192 msg_Err( p_mux, "FIXME ignoring trak (noaudio&&novideo)" );
1196 if( p_stream->fmt.i_cat == AUDIO_ES )
1197 i_timescale = p_stream->fmt.audio.i_rate;
1201 /* *** add /moov/trak *** */
1202 trak = box_new( "trak" );
1204 /* *** add /moov/trak/tkhd *** */
1205 if( !p_sys->b_64_ext )
1208 tkhd = box_full_new( "tkhd", 0, 0x0f );
1210 tkhd = box_full_new( "tkhd", 0, 1 );
1212 bo_add_32be( tkhd, get_timestamp() ); // creation time
1213 bo_add_32be( tkhd, get_timestamp() ); // modification time
1214 bo_add_32be( tkhd, p_stream->i_track_id );
1215 bo_add_32be( tkhd, 0 ); // reserved 0
1216 bo_add_32be( tkhd, p_stream->i_duration *
1217 (int64_t)i_movie_timescale /
1218 (mtime_t)1000000 ); // duration
1223 tkhd = box_full_new( "tkhd", 1, 0x0f );
1225 tkhd = box_full_new( "tkhd", 1, 1 );
1227 bo_add_64be( tkhd, get_timestamp() ); // creation time
1228 bo_add_64be( tkhd, get_timestamp() ); // modification time
1229 bo_add_32be( tkhd, p_stream->i_track_id );
1230 bo_add_32be( tkhd, 0 ); // reserved 0
1231 bo_add_64be( tkhd, p_stream->i_duration *
1232 (int64_t)i_movie_timescale /
1233 (mtime_t)1000000 ); // duration
1236 for( i = 0; i < 2; i++ )
1238 bo_add_32be( tkhd, 0 ); // reserved
1240 bo_add_16be( tkhd, 0 ); // layer
1241 bo_add_16be( tkhd, 0 ); // pre-defined
1243 bo_add_16be( tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0 );
1244 bo_add_16be( tkhd, 0 ); // reserved
1245 for( i = 0; i < 9; i++ )
1247 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
1249 if( p_stream->fmt.i_cat == AUDIO_ES )
1251 bo_add_32be( tkhd, 0 ); // width (presentation)
1252 bo_add_32be( tkhd, 0 ); // height(presentation)
1256 // width (presentation)
1257 bo_add_32be( tkhd, p_stream->fmt.video.i_aspect *
1258 p_stream->fmt.video.i_height /
1259 VOUT_ASPECT_FACTOR << 16 );
1260 // height(presentation)
1261 bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
1264 box_gather( trak, tkhd );
1266 /* *** add /moov/trak/edts and elst */
1267 edts = box_new( "edts" );
1268 elst = box_full_new( "elst", p_sys->b_64_ext ? 1 : 0, 0 );
1269 if( p_stream->i_dts_start > p_sys->i_dts_start )
1271 bo_add_32be( elst, 2 );
1273 if( p_sys->b_64_ext )
1275 bo_add_64be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) * i_movie_timescale / I64C(1000000) );
1276 bo_add_64be( elst, -1 );
1280 bo_add_32be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) * i_movie_timescale / I64C(1000000) );
1281 bo_add_32be( elst, -1 );
1283 bo_add_16be( elst, 1 );
1284 bo_add_16be( elst, 0 );
1288 bo_add_32be( elst, 1 );
1290 if( p_sys->b_64_ext )
1292 bo_add_64be( elst, p_stream->i_duration * i_movie_timescale / I64C(1000000) );
1293 bo_add_64be( elst, 0 );
1297 bo_add_32be( elst, p_stream->i_duration * i_movie_timescale / I64C(1000000) );
1298 bo_add_32be( elst, 0 );
1300 bo_add_16be( elst, 1 );
1301 bo_add_16be( elst, 0 );
1304 box_gather( edts, elst );
1306 box_gather( trak, edts );
1308 /* *** add /moov/trak/mdia *** */
1309 mdia = box_new( "mdia" );
1312 if( !p_sys->b_64_ext )
1314 mdhd = box_full_new( "mdhd", 0, 0 );
1315 bo_add_32be( mdhd, get_timestamp() ); // creation time
1316 bo_add_32be( mdhd, get_timestamp() ); // modification time
1317 bo_add_32be( mdhd, i_timescale); // timescale
1318 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1319 (mtime_t)1000000 ); // duration
1323 mdhd = box_full_new( "mdhd", 1, 0 );
1324 bo_add_64be( mdhd, get_timestamp() ); // creation time
1325 bo_add_64be( mdhd, get_timestamp() ); // modification time
1326 bo_add_32be( mdhd, i_timescale); // timescale
1327 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1328 (mtime_t)1000000 ); // duration
1331 if( p_stream->fmt.psz_language )
1333 char *psz = p_stream->fmt.psz_language;
1334 const iso639_lang_t *pl = NULL;
1335 uint16_t lang = 0x0;
1337 if( strlen( psz ) == 2 )
1339 pl = GetLang_1( psz );
1341 else if( strlen( psz ) == 3 )
1343 pl = GetLang_2B( psz );
1344 if( !strcmp( pl->psz_iso639_1, "??" ) )
1346 pl = GetLang_2T( psz );
1349 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1351 lang = ( ( pl->psz_iso639_2T[0] - 0x60 ) << 10 ) |
1352 ( ( pl->psz_iso639_2T[1] - 0x60 ) << 5 ) |
1353 ( ( pl->psz_iso639_2T[2] - 0x60 ) );
1355 bo_add_16be( mdhd, lang ); // language
1359 bo_add_16be( mdhd, 0 ); // language
1361 bo_add_16be( mdhd, 0 ); // predefined
1363 box_gather( mdia, mdhd );
1365 /* handler reference */
1366 hdlr = box_full_new( "hdlr", 0, 0 );
1368 bo_add_fourcc( hdlr, "mhlr" ); // media handler
1369 if( p_stream->fmt.i_cat == AUDIO_ES )
1371 bo_add_fourcc( hdlr, "soun" );
1375 bo_add_fourcc( hdlr, "vide" );
1378 bo_add_32be( hdlr, 0 ); // reserved
1379 bo_add_32be( hdlr, 0 ); // reserved
1380 bo_add_32be( hdlr, 0 ); // reserved
1382 bo_add_8( hdlr, 12 );
1383 if( p_stream->fmt.i_cat == AUDIO_ES )
1384 bo_add_mem( hdlr, 12, "SoundHandler" );
1386 bo_add_mem( hdlr, 12, "VideoHandler" );
1389 box_gather( mdia, hdlr );
1392 minf = box_new( "minf" );
1395 if( p_stream->fmt.i_cat == AUDIO_ES )
1399 smhd = box_full_new( "smhd", 0, 0 );
1400 bo_add_16be( smhd, 0 ); // balance
1401 bo_add_16be( smhd, 0 ); // reserved
1404 box_gather( minf, smhd );
1406 else if( p_stream->fmt.i_cat == VIDEO_ES )
1410 vmhd = box_full_new( "vmhd", 0, 1 );
1411 bo_add_16be( vmhd, 0 ); // graphicsmode
1412 for( i = 0; i < 3; i++ )
1414 bo_add_16be( vmhd, 0 ); // opcolor
1418 box_gather( minf, vmhd );
1422 dinf = box_new( "dinf" );
1423 dref = box_full_new( "dref", 0, 0 );
1424 bo_add_32be( dref, 1 );
1425 url = box_full_new( "url ", 0, 0x01 );
1427 box_gather( dref, url );
1429 box_gather( dinf, dref );
1431 /* append dinf to mdia */
1433 box_gather( minf, dinf );
1436 stbl = GetStblBox( p_mux, p_stream );
1438 /* append stbl to minf */
1439 p_stream->i_stco_pos += minf->i_buffer;
1440 box_gather( minf, stbl );
1442 /* append minf to mdia */
1444 p_stream->i_stco_pos += mdia->i_buffer;
1445 box_gather( mdia, minf );
1447 /* append mdia to trak */
1449 p_stream->i_stco_pos += trak->i_buffer;
1450 box_gather( trak, mdia );
1452 /* append trak to moov */
1454 p_stream->i_stco_pos += moov->i_buffer;
1455 box_gather( moov, trak );
1458 /* Add user data tags */
1459 box_gather( moov, GetUdtaTag( p_mux ) );
1467 /****************************************************************************/
1469 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1474 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1475 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1479 p_bo->i_buffer_size = i_size;
1480 p_bo->p_buffer = p_buffer;
1483 p_bo->b_grow = b_grow;
1487 static void bo_add_8( bo_t *p_bo, uint8_t i )
1489 if( p_bo->i_buffer < p_bo->i_buffer_size )
1491 p_bo->p_buffer[p_bo->i_buffer] = i;
1493 else if( p_bo->b_grow )
1495 p_bo->i_buffer_size += 1024;
1496 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1498 p_bo->p_buffer[p_bo->i_buffer] = i;
1504 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1506 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1507 bo_add_8( p_bo, i &0xff );
1510 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1512 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1513 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1514 bo_add_8( p_bo, ( i &0xff ) );
1516 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1518 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1519 bo_add_16be( p_bo, i &0xffff );
1522 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1524 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1525 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1526 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1527 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1530 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1532 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1533 bo_add_32be( p_bo, i &0xffffffff );
1536 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1538 bo_add_8( p_bo, fcc[0] );
1539 bo_add_8( p_bo, fcc[1] );
1540 bo_add_8( p_bo, fcc[2] );
1541 bo_add_8( p_bo, fcc[3] );
1544 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1548 for( i = 0; i < i_size; i++ )
1550 bo_add_8( p_bo, p_mem[i] );
1554 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
1560 vals[3] = (unsigned char)(i_length & 0x7f);
1562 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
1564 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
1566 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
1568 bo_add_8( p_bo, tag );
1570 if( i_size < 0x00000080 )
1572 bo_add_8( p_bo, vals[3] );
1574 else if( i_size < 0x00004000 )
1576 bo_add_8( p_bo, vals[2] );
1577 bo_add_8( p_bo, vals[3] );
1579 else if( i_size < 0x00200000 )
1581 bo_add_8( p_bo, vals[1] );
1582 bo_add_8( p_bo, vals[2] );
1583 bo_add_8( p_bo, vals[3] );
1585 else if( i_size < 0x10000000 )
1587 bo_add_8( p_bo, vals[0] );
1588 bo_add_8( p_bo, vals[1] );
1589 bo_add_8( p_bo, vals[2] );
1590 bo_add_8( p_bo, vals[3] );
1594 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1598 for( i = 0; i < p_bo2->i_buffer; i++ )
1600 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1604 static bo_t * box_new( char *fcc )
1608 if( ( box = malloc( sizeof( bo_t ) ) ) )
1610 bo_init( box, 0, NULL, VLC_TRUE );
1612 bo_add_32be ( box, 0 );
1613 bo_add_fourcc( box, fcc );
1619 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
1623 if( ( box = malloc( sizeof( bo_t ) ) ) )
1625 bo_init( box, 0, NULL, VLC_TRUE );
1627 bo_add_32be ( box, 0 );
1628 bo_add_fourcc( box, fcc );
1629 bo_add_8 ( box, v );
1630 bo_add_24be ( box, f );
1636 static void box_fix( bo_t *box )
1640 memcpy( &box_tmp, box, sizeof( bo_t ) );
1642 box_tmp.i_buffer = 0;
1643 bo_add_32be( &box_tmp, box->i_buffer );
1646 static void box_free( bo_t *box )
1650 free( box->p_buffer );
1656 static void box_gather ( bo_t *box, bo_t *box2 )
1658 bo_add_bo( box, box2 );
1662 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
1664 sout_buffer_t *p_buf;
1666 p_buf = sout_BufferNew( p_sout, box->i_buffer );
1667 if( box->i_buffer > 0 )
1669 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
1672 p_buf->i_size = box->i_buffer;
1677 static void box_send( sout_mux_t *p_mux, bo_t *box )
1679 sout_buffer_t *p_buf;
1681 p_buf = bo_to_sout( p_mux->p_sout, box );
1684 sout_AccessOutWrite( p_mux->p_access, p_buf );
1687 static int64_t get_timestamp()
1689 int64_t i_timestamp = 0;
1692 i_timestamp = time(NULL);
1693 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
1694 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60