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', '3' ):
379 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
380 (char*)&p_input->p_fmt->i_codec );
384 p_stream = malloc( sizeof( mp4_stream_t ) );
385 es_format_Copy( &p_stream->fmt, p_input->p_fmt );
386 p_stream->i_track_id = p_sys->i_nb_streams + 1;
387 p_stream->i_length_neg = 0;
388 p_stream->i_entry_count = 0;
389 p_stream->i_entry_max = 1000;
391 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
392 p_stream->i_dts_start = 0;
393 p_stream->i_duration = 0;
395 p_input->p_sys = p_stream;
397 msg_Dbg( p_mux, "adding input" );
399 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
403 /*****************************************************************************
405 *****************************************************************************/
406 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
408 msg_Dbg( p_mux, "removing input" );
412 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
418 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
420 sout_fifo_t *p_fifo = p_mux->pp_inputs[i]->p_fifo;
421 sout_buffer_t *p_buf;
423 if( p_fifo->i_depth <= 1 )
425 return -1; // wait that all fifo have at least 2 packets
428 p_buf = sout_FifoShow( p_fifo );
429 if( i_stream < 0 || p_buf->i_dts < i_dts )
431 i_dts = p_buf->i_dts;
437 *pi_stream = i_stream;
446 /*****************************************************************************
448 *****************************************************************************/
449 static int Mux( sout_mux_t *p_mux )
451 sout_mux_sys_t *p_sys = p_mux->p_sys;
455 sout_input_t *p_input;
457 mp4_stream_t *p_stream;
458 sout_buffer_t *p_data;
461 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
463 return( VLC_SUCCESS );
466 p_input = p_mux->pp_inputs[i_stream];
467 p_stream = (mp4_stream_t*)p_input->p_sys;
469 p_data = sout_FifoGet( p_input->p_fifo );
470 if( p_input->p_fifo->i_depth > 0 )
472 sout_buffer_t *p_next = sout_FifoShow( p_input->p_fifo );
473 int64_t i_diff = p_next->i_dts - p_data->i_dts;
475 if( i_diff < I64C(1000000 ) ) /* protection */
477 p_data->i_length = i_diff;
480 if( p_data->i_length <= 0 )
482 msg_Warn( p_mux, "i_length <= 0" );
483 p_stream->i_length_neg += p_data->i_length - 1;
484 p_data->i_length = 1;
486 else if( p_stream->i_length_neg < 0 )
488 int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
490 p_data->i_length -= i_recover;
491 p_stream->i_length_neg += i_recover;
494 /* Save starting time */
495 if( p_stream->i_entry_count == 0 )
497 p_stream->i_dts_start = p_data->i_dts;
499 /* Update global dts_start */
500 if( p_sys->i_dts_start <= 0 ||
501 p_stream->i_dts_start < p_sys->i_dts_start )
503 p_sys->i_dts_start = p_stream->i_dts_start;
507 /* add index entry */
508 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
509 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_size;
510 p_stream->entry[p_stream->i_entry_count].i_pts_dts= __MAX( p_data->i_pts - p_data->i_dts, 0 );
511 p_stream->entry[p_stream->i_entry_count].i_length = p_data->i_length;
512 p_stream->entry[p_stream->i_entry_count].i_flags = p_data->i_flags;
514 p_stream->i_entry_count++;
515 if( p_stream->i_entry_count >= p_stream->i_entry_max )
517 p_stream->i_entry_max += 1000;
519 realloc( p_stream->entry,
520 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
524 p_stream->i_duration += p_data->i_length;
525 p_sys->i_pos += p_data->i_size;
528 sout_AccessOutWrite( p_mux->p_access, p_data );
531 return( VLC_SUCCESS );
534 /*****************************************************************************
536 *****************************************************************************/
537 static int GetDescrLength( int i_size )
539 if( i_size < 0x00000080 )
541 else if( i_size < 0x00004000 )
543 else if( i_size < 0x00200000 )
549 static bo_t *GetESDS( mp4_stream_t *p_stream )
553 int i_object_type_indication;
554 int i_decoder_specific_info_size;
556 if( p_stream->fmt.i_extra > 0 )
558 i_decoder_specific_info_size =
559 GetDescrLength( p_stream->fmt.i_extra );
563 i_decoder_specific_info_size = 0;
566 esds = box_full_new( "esds", 0, 0 );
569 bo_add_descr( esds, 0x03, 3 +
570 GetDescrLength( 13 + i_decoder_specific_info_size ) +
571 GetDescrLength( 1 ) );
572 bo_add_16be( esds, p_stream->i_track_id );
573 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
575 /* DecoderConfigDescr */
576 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
578 switch( p_stream->fmt.i_codec )
580 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
581 i_object_type_indication = 0x20;
583 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
584 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
585 i_object_type_indication = 0x60;
587 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
588 /* FIXME for mpeg2-aac == 0x66->0x68 */
589 i_object_type_indication = 0x40;
591 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
592 i_object_type_indication =
593 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
596 i_object_type_indication = 0x00;
599 i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
601 bo_add_8 ( esds, i_object_type_indication );
602 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
603 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
604 bo_add_32be( esds, 0x7fffffff ); // maxBitrate
605 bo_add_32be( esds, 0 ); // avgBitrate
607 if( p_stream->fmt.i_extra > 0 )
611 /* DecoderSpecificInfo */
612 bo_add_descr( esds, 0x05, p_stream->fmt.i_extra );
614 for( i = 0; i < p_stream->fmt.i_extra; i++ )
616 bo_add_8( esds, ((uint8_t*)p_stream->fmt.p_extra)[i] );
620 /* SL_Descr mandatory */
621 bo_add_descr( esds, 0x06, 1 );
622 bo_add_8 ( esds, 0x02 ); // sl_predefined
629 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
634 wave = box_new( "wave" );
636 box = box_new( "frma" );
637 bo_add_fourcc( box, "mp4a" );
639 box_gather( wave, box );
641 box = box_new( "mp4a" );
642 bo_add_32be( box, 0 );
644 box_gather( wave, box );
646 box = GetESDS( p_stream );
648 box_gather( wave, box );
650 box = box_new( "srcq" );
651 bo_add_32be( box, 0x40 );
653 box_gather( wave, box );
656 bo_add_32be( wave, 8 ); /* new empty box */
657 bo_add_32be( wave, 0 ); /* box label */
664 /* TODO: No idea about these values */
665 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
667 bo_t *smi = box_new( "SMI " );
669 if( p_stream->fmt.i_extra > 0x4e )
671 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
672 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
674 while( p + 8 < p_end )
676 int i_size = GetDWBE( p );
679 /* FIXME handle 1 as long size */
682 if( !strncmp( &p[4], "SMI ", 4 ) )
684 bo_add_mem( smi, p_end - p - 8, &p[8] );
691 /* Create a dummy one in fallback */
692 bo_add_fourcc( smi, "SEQH" );
693 bo_add_32be( smi, 0x5 );
694 bo_add_32be( smi, 0xe2c0211d );
695 bo_add_8( smi, 0xc0 );
701 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
703 sout_mux_sys_t *p_sys = p_mux->p_sys;
704 bo_t *udta = box_new( "udta" );
708 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
710 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
712 if( p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','v') ||
713 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
715 bo_t *box = box_new( "\251req" );
717 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
718 bo_add_16be( box, 0 );
719 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
720 "QuickTime 6.0 or greater" );
722 box_gather( udta, box );
729 bo_t *box = box_new( "\251enc" );
731 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
732 bo_add_16be( box, 0 );
733 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
734 PACKAGE_STRING " stream output" );
736 box_gather( udta, box );
743 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
745 sout_mux_sys_t *p_sys = p_mux->p_sys;
746 vlc_bool_t b_descr = VLC_FALSE;
751 switch( p_stream->fmt.i_codec )
753 case VLC_FOURCC('m','p','4','a'):
754 memcpy( fcc, "mp4a", 4 );
758 case VLC_FOURCC('m','p','g','a'):
760 memcpy( fcc, ".mp3", 4 );
763 memcpy( fcc, "mp4a", 4 );
769 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
773 soun = box_new( fcc );
774 for( i = 0; i < 6; i++ )
776 bo_add_8( soun, 0 ); // reserved;
778 bo_add_16be( soun, 1 ); // data-reference-index
780 /* SoundDescription */
782 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
784 bo_add_16be( soun, 1 ); // version 1;
788 bo_add_16be( soun, 0 ); // version 0;
790 bo_add_16be( soun, 0 ); // revision level (0)
791 bo_add_32be( soun, 0 ); // vendor
793 bo_add_16be( soun, p_stream->fmt.audio.i_channels );
795 bo_add_16be( soun, p_stream->fmt.audio.i_bitspersample ?
796 p_stream->fmt.audio.i_bitspersample : 16 );
797 bo_add_16be( soun, -2 ); // compression id
798 bo_add_16be( soun, 0 ); // packet size (0)
799 bo_add_16be( soun, p_stream->fmt.audio.i_rate ); // sampleratehi
800 bo_add_16be( soun, 0 ); // sampleratelo
802 /* Extended data for SoundDescription V1 */
804 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
806 /* samples per packet */
807 bo_add_32be( soun, p_stream->fmt.audio.i_frame_length );
808 bo_add_32be( soun, 1536 ); /* bytes per packet */
809 bo_add_32be( soun, 2 ); /* bytes per frame */
810 /* bytes per sample */
811 bo_add_32be( soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
814 /* Add an ES Descriptor */
820 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
822 box = GetWaveTag( p_stream );
826 box = GetESDS( p_stream );
829 box_gather( soun, box );
837 static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
844 switch( p_stream->fmt.i_codec )
846 case VLC_FOURCC('m','p','4','v'):
847 case VLC_FOURCC('m','p','g','v'):
848 memcpy( fcc, "mp4v", 4 );
851 case VLC_FOURCC('M','J','P','G'):
852 memcpy( fcc, "mjpa", 4 );
855 case VLC_FOURCC('S','V','Q','3'):
856 memcpy( fcc, "SVQ3", 4 );
860 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
864 vide = box_new( fcc );
865 for( i = 0; i < 6; i++ )
867 bo_add_8( vide, 0 ); // reserved;
869 bo_add_16be( vide, 1 ); // data-reference-index
871 bo_add_16be( vide, 0 ); // predefined;
872 bo_add_16be( vide, 0 ); // reserved;
873 for( i = 0; i < 3; i++ )
875 bo_add_32be( vide, 0 ); // predefined;
878 bo_add_16be( vide, p_stream->fmt.video.i_width ); // i_width
879 bo_add_16be( vide, p_stream->fmt.video.i_height ); // i_height
881 bo_add_32be( vide, 0x00480000 ); // h 72dpi
882 bo_add_32be( vide, 0x00480000 ); // v 72dpi
884 bo_add_32be( vide, 0 ); // data size, always 0
885 bo_add_16be( vide, 1 ); // frames count per sample
888 for( i = 0; i < 32; i++ )
893 bo_add_16be( vide, 0x18 ); // depth
894 bo_add_16be( vide, 0xffff ); // predefined
896 /* add an ES Descriptor */
897 switch( p_stream->fmt.i_codec )
899 case VLC_FOURCC('m','p','4','v'):
900 case VLC_FOURCC('m','p','g','v'):
902 bo_t *esds = GetESDS( p_stream );
905 box_gather( vide, esds );
909 case VLC_FOURCC('S','V','Q','3'):
911 bo_t *esds = GetSVQ3Tag( p_stream );
914 box_gather( vide, esds );
927 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
929 sout_mux_sys_t *p_sys = p_mux->p_sys;
930 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
931 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz, *stss;
932 uint32_t i_timescale;
933 int64_t i_dts, i_dts_q;
935 stbl = box_new( "stbl" );
937 /* sample description */
938 stsd = box_full_new( "stsd", 0, 0 );
939 bo_add_32be( stsd, 1 );
940 if( p_stream->fmt.i_cat == AUDIO_ES )
942 bo_t *soun = GetSounBox( p_mux, p_stream );
943 box_gather( stsd, soun );
945 else if( p_stream->fmt.i_cat == VIDEO_ES )
947 bo_t *vide = GetVideBox( p_mux, p_stream );
948 box_gather( stsd, vide );
952 /* chunk offset table */
953 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
955 /* 64 bits version */
956 p_stream->b_stco64 = VLC_TRUE;
957 stco = box_full_new( "co64", 0, 0 );
961 /* 32 bits version */
962 p_stream->b_stco64 = VLC_FALSE;
963 stco = box_full_new( "stco", 0, 0 );
965 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
967 /* sample to chunk table */
968 stsc = box_full_new( "stsc", 0, 0 );
969 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
971 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
972 i < p_stream->i_entry_count; i_chunk++ )
976 if( p_stream->b_stco64 )
977 bo_add_64be( stco, p_stream->entry[i].i_pos );
979 bo_add_32be( stco, p_stream->entry[i].i_pos );
981 while( i < p_stream->i_entry_count )
983 if( i + 1 < p_stream->i_entry_count &&
984 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
985 != p_stream->entry[i + 1].i_pos )
994 /* Add entry to the stsc table */
995 if( i_stsc_last_val != i - i_first )
997 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
998 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
999 bo_add_32be( stsc, 1 ); // sample-descr-index
1000 i_stsc_last_val = i - i_first;
1005 /* Fix stco entry count */
1006 bo_fix_32be( stco, 12, i_chunk );
1007 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
1010 /* Fix stsc entry count */
1011 bo_fix_32be( stsc, 12, i_stsc_entries );
1015 stts = box_full_new( "stts", 0, 0 );
1016 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
1018 if( p_stream->fmt.i_cat == AUDIO_ES )
1019 i_timescale = p_stream->fmt.audio.i_rate;
1023 /* first, create quantified length */
1024 for( i = 0, i_dts = 0, i_dts_q = 0; i < p_stream->i_entry_count; i++ )
1026 int64_t i_dts_deq = i_dts_q * I64C(1000000) / (int64_t)i_timescale;
1027 int64_t i_delta = p_stream->entry[i].i_length + i_dts - i_dts_deq;
1029 i_dts += p_stream->entry[i].i_length;
1031 p_stream->entry[i].i_length = i_delta * (int64_t)i_timescale / I64C(1000000);;
1033 i_dts_q += p_stream->entry[i].i_length;
1035 /* then write encoded table */
1036 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
1039 int64_t i_delta = p_stream->entry[i].i_length;
1041 while( i < p_stream->i_entry_count )
1044 if( i >= p_stream->i_entry_count ||
1045 p_stream->entry[i].i_length != i_delta )
1051 bo_add_32be( stts, i - i_first ); // sample-count
1052 bo_add_32be( stts, i_delta ); // sample-delta
1054 bo_fix_32be( stts, 12, i_index );
1057 /* FIXME add ctts ?? FIXME */
1059 stsz = box_full_new( "stsz", 0, 0 );
1060 bo_add_32be( stsz, 0 ); // sample-size
1061 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
1062 for( i = 0; i < p_stream->i_entry_count; i++ )
1064 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
1068 /* create stss table */
1070 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i++ )
1072 if( p_stream->entry[i].i_flags & (BLOCK_FLAG_TYPE_I<<SOUT_BUFFER_FLAGS_BLOCK_SHIFT) )
1076 stss = box_full_new( "stss", 0, 0 );
1077 bo_add_32be( stss, 0 ); /* fixed later */
1079 bo_add_32be( stss, 1 + i );
1085 bo_fix_32be( stss, 12, i_index );
1089 /* Now gather all boxes into stbl */
1090 box_gather( stbl, stsd );
1091 box_gather( stbl, stts );
1094 box_gather( stbl, stss );
1096 box_gather( stbl, stsc );
1097 box_gather( stbl, stsz );
1098 p_stream->i_stco_pos = stbl->i_buffer + 16;
1099 box_gather( stbl, stco );
1107 static int64_t get_timestamp();
1109 static uint32_t mvhd_matrix[9] =
1110 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1112 static bo_t *GetMoovBox( sout_mux_t *p_mux )
1114 sout_mux_sys_t *p_sys = p_mux->p_sys;
1119 uint32_t i_movie_timescale = 90000;
1120 int64_t i_movie_duration = 0;
1122 moov = box_new( "moov" );
1124 /* Create general info */
1125 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1127 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1128 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
1130 msg_Dbg( p_mux, "movie duration %ds",
1131 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
1133 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
1135 /* *** add /moov/mvhd *** */
1136 if( !p_sys->b_64_ext )
1138 mvhd = box_full_new( "mvhd", 0, 0 );
1139 bo_add_32be( mvhd, get_timestamp() ); // creation time
1140 bo_add_32be( mvhd, get_timestamp() ); // modification time
1141 bo_add_32be( mvhd, i_movie_timescale); // timescale
1142 bo_add_32be( mvhd, i_movie_duration ); // duration
1146 mvhd = box_full_new( "mvhd", 1, 0 );
1147 bo_add_64be( mvhd, get_timestamp() ); // creation time
1148 bo_add_64be( mvhd, get_timestamp() ); // modification time
1149 bo_add_32be( mvhd, i_movie_timescale); // timescale
1150 bo_add_64be( mvhd, i_movie_duration ); // duration
1152 bo_add_32be( mvhd, 0x10000 ); // rate
1153 bo_add_16be( mvhd, 0x100 ); // volume
1154 bo_add_16be( mvhd, 0 ); // reserved
1155 for( i = 0; i < 2; i++ )
1157 bo_add_32be( mvhd, 0 ); // reserved
1159 for( i = 0; i < 9; i++ )
1161 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
1163 for( i = 0; i < 6; i++ )
1165 bo_add_32be( mvhd, 0 ); // pre-defined
1168 /* Next available track id */
1169 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
1172 box_gather( moov, mvhd );
1174 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1176 mp4_stream_t *p_stream;
1177 uint32_t i_timescale;
1179 bo_t *trak, *tkhd, *edts, *elst, *mdia, *mdhd, *hdlr;
1180 bo_t *minf, *dinf, *dref, *url, *stbl;
1182 p_stream = p_sys->pp_streams[i_trak];
1184 if( p_stream->fmt.i_cat != AUDIO_ES &&
1185 p_stream->fmt.i_cat != VIDEO_ES )
1187 msg_Err( p_mux, "FIXME ignoring trak (noaudio&&novideo)" );
1191 if( p_stream->fmt.i_cat == AUDIO_ES )
1192 i_timescale = p_stream->fmt.audio.i_rate;
1196 /* *** add /moov/trak *** */
1197 trak = box_new( "trak" );
1199 /* *** add /moov/trak/tkhd *** */
1200 if( !p_sys->b_64_ext )
1203 tkhd = box_full_new( "tkhd", 0, 0x0f );
1205 tkhd = box_full_new( "tkhd", 0, 1 );
1207 bo_add_32be( tkhd, get_timestamp() ); // creation time
1208 bo_add_32be( tkhd, get_timestamp() ); // modification time
1209 bo_add_32be( tkhd, p_stream->i_track_id );
1210 bo_add_32be( tkhd, 0 ); // reserved 0
1211 bo_add_32be( tkhd, p_stream->i_duration *
1212 (int64_t)i_movie_timescale /
1213 (mtime_t)1000000 ); // duration
1218 tkhd = box_full_new( "tkhd", 1, 0x0f );
1220 tkhd = box_full_new( "tkhd", 1, 1 );
1222 bo_add_64be( tkhd, get_timestamp() ); // creation time
1223 bo_add_64be( tkhd, get_timestamp() ); // modification time
1224 bo_add_32be( tkhd, p_stream->i_track_id );
1225 bo_add_32be( tkhd, 0 ); // reserved 0
1226 bo_add_64be( tkhd, p_stream->i_duration *
1227 (int64_t)i_movie_timescale /
1228 (mtime_t)1000000 ); // duration
1231 for( i = 0; i < 2; i++ )
1233 bo_add_32be( tkhd, 0 ); // reserved
1235 bo_add_16be( tkhd, 0 ); // layer
1236 bo_add_16be( tkhd, 0 ); // pre-defined
1238 bo_add_16be( tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0 );
1239 bo_add_16be( tkhd, 0 ); // reserved
1240 for( i = 0; i < 9; i++ )
1242 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
1244 if( p_stream->fmt.i_cat == AUDIO_ES )
1246 bo_add_32be( tkhd, 0 ); // width (presentation)
1247 bo_add_32be( tkhd, 0 ); // height(presentation)
1251 // width (presentation)
1252 bo_add_32be( tkhd, p_stream->fmt.video.i_aspect *
1253 p_stream->fmt.video.i_height /
1254 VOUT_ASPECT_FACTOR << 16 );
1255 // height(presentation)
1256 bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
1259 box_gather( trak, tkhd );
1261 /* *** add /moov/trak/edts and elst */
1262 edts = box_new( "edts" );
1263 elst = box_full_new( "elst", p_sys->b_64_ext ? 1 : 0, 0 );
1264 if( p_stream->i_dts_start > p_sys->i_dts_start )
1266 bo_add_32be( elst, 2 );
1268 if( p_sys->b_64_ext )
1270 bo_add_64be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) * i_movie_timescale / I64C(1000000) );
1271 bo_add_64be( elst, -1 );
1275 bo_add_32be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) * i_movie_timescale / I64C(1000000) );
1276 bo_add_32be( elst, -1 );
1278 bo_add_16be( elst, 1 );
1279 bo_add_16be( elst, 0 );
1283 bo_add_32be( elst, 1 );
1285 if( p_sys->b_64_ext )
1287 bo_add_64be( elst, p_stream->i_duration * i_movie_timescale / I64C(1000000) );
1288 bo_add_64be( elst, 0 );
1292 bo_add_32be( elst, p_stream->i_duration * i_movie_timescale / I64C(1000000) );
1293 bo_add_32be( elst, 0 );
1295 bo_add_16be( elst, 1 );
1296 bo_add_16be( elst, 0 );
1299 box_gather( edts, elst );
1301 box_gather( trak, edts );
1303 /* *** add /moov/trak/mdia *** */
1304 mdia = box_new( "mdia" );
1307 if( !p_sys->b_64_ext )
1309 mdhd = box_full_new( "mdhd", 0, 0 );
1310 bo_add_32be( mdhd, get_timestamp() ); // creation time
1311 bo_add_32be( mdhd, get_timestamp() ); // modification time
1312 bo_add_32be( mdhd, i_timescale); // timescale
1313 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1314 (mtime_t)1000000 ); // duration
1318 mdhd = box_full_new( "mdhd", 1, 0 );
1319 bo_add_64be( mdhd, get_timestamp() ); // creation time
1320 bo_add_64be( mdhd, get_timestamp() ); // modification time
1321 bo_add_32be( mdhd, i_timescale); // timescale
1322 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1323 (mtime_t)1000000 ); // duration
1326 if( p_stream->fmt.psz_language )
1328 char *psz = p_stream->fmt.psz_language;
1329 const iso639_lang_t *pl = NULL;
1330 uint16_t lang = 0x0;
1332 if( strlen( psz ) == 2 )
1334 pl = GetLang_1( psz );
1336 else if( strlen( psz ) == 3 )
1338 pl = GetLang_2B( psz );
1339 if( !strcmp( pl->psz_iso639_1, "??" ) )
1341 pl = GetLang_2T( psz );
1344 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1346 lang = ( ( pl->psz_iso639_2T[0] - 0x60 ) << 10 ) |
1347 ( ( pl->psz_iso639_2T[1] - 0x60 ) << 5 ) |
1348 ( ( pl->psz_iso639_2T[2] - 0x60 ) );
1350 bo_add_16be( mdhd, lang ); // language
1354 bo_add_16be( mdhd, 0 ); // language
1356 bo_add_16be( mdhd, 0 ); // predefined
1358 box_gather( mdia, mdhd );
1360 /* handler reference */
1361 hdlr = box_full_new( "hdlr", 0, 0 );
1363 bo_add_fourcc( hdlr, "mhlr" ); // media handler
1364 if( p_stream->fmt.i_cat == AUDIO_ES )
1366 bo_add_fourcc( hdlr, "soun" );
1370 bo_add_fourcc( hdlr, "vide" );
1373 bo_add_32be( hdlr, 0 ); // reserved
1374 bo_add_32be( hdlr, 0 ); // reserved
1375 bo_add_32be( hdlr, 0 ); // reserved
1377 bo_add_8( hdlr, 12 );
1378 if( p_stream->fmt.i_cat == AUDIO_ES )
1379 bo_add_mem( hdlr, 12, "SoundHandler" );
1381 bo_add_mem( hdlr, 12, "VideoHandler" );
1384 box_gather( mdia, hdlr );
1387 minf = box_new( "minf" );
1390 if( p_stream->fmt.i_cat == AUDIO_ES )
1394 smhd = box_full_new( "smhd", 0, 0 );
1395 bo_add_16be( smhd, 0 ); // balance
1396 bo_add_16be( smhd, 0 ); // reserved
1399 box_gather( minf, smhd );
1401 else if( p_stream->fmt.i_cat == VIDEO_ES )
1405 vmhd = box_full_new( "vmhd", 0, 1 );
1406 bo_add_16be( vmhd, 0 ); // graphicsmode
1407 for( i = 0; i < 3; i++ )
1409 bo_add_16be( vmhd, 0 ); // opcolor
1413 box_gather( minf, vmhd );
1417 dinf = box_new( "dinf" );
1418 dref = box_full_new( "dref", 0, 0 );
1419 bo_add_32be( dref, 1 );
1420 url = box_full_new( "url ", 0, 0x01 );
1422 box_gather( dref, url );
1424 box_gather( dinf, dref );
1426 /* append dinf to mdia */
1428 box_gather( minf, dinf );
1431 stbl = GetStblBox( p_mux, p_stream );
1433 /* append stbl to minf */
1434 p_stream->i_stco_pos += minf->i_buffer;
1435 box_gather( minf, stbl );
1437 /* append minf to mdia */
1439 p_stream->i_stco_pos += mdia->i_buffer;
1440 box_gather( mdia, minf );
1442 /* append mdia to trak */
1444 p_stream->i_stco_pos += trak->i_buffer;
1445 box_gather( trak, mdia );
1447 /* append trak to moov */
1449 p_stream->i_stco_pos += moov->i_buffer;
1450 box_gather( moov, trak );
1453 /* Add user data tags */
1454 box_gather( moov, GetUdtaTag( p_mux ) );
1462 /****************************************************************************/
1464 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1469 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1470 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1474 p_bo->i_buffer_size = i_size;
1475 p_bo->p_buffer = p_buffer;
1478 p_bo->b_grow = b_grow;
1482 static void bo_add_8( bo_t *p_bo, uint8_t i )
1484 if( p_bo->i_buffer < p_bo->i_buffer_size )
1486 p_bo->p_buffer[p_bo->i_buffer] = i;
1488 else if( p_bo->b_grow )
1490 p_bo->i_buffer_size += 1024;
1491 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1493 p_bo->p_buffer[p_bo->i_buffer] = i;
1499 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1501 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1502 bo_add_8( p_bo, i &0xff );
1505 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1507 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1508 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1509 bo_add_8( p_bo, ( i &0xff ) );
1511 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1513 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1514 bo_add_16be( p_bo, i &0xffff );
1517 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1519 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1520 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1521 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1522 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1525 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1527 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1528 bo_add_32be( p_bo, i &0xffffffff );
1531 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1533 bo_add_8( p_bo, fcc[0] );
1534 bo_add_8( p_bo, fcc[1] );
1535 bo_add_8( p_bo, fcc[2] );
1536 bo_add_8( p_bo, fcc[3] );
1539 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1543 for( i = 0; i < i_size; i++ )
1545 bo_add_8( p_bo, p_mem[i] );
1549 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
1555 vals[3] = (unsigned char)(i_length & 0x7f);
1557 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
1559 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
1561 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
1563 bo_add_8( p_bo, tag );
1565 if( i_size < 0x00000080 )
1567 bo_add_8( p_bo, vals[3] );
1569 else if( i_size < 0x00004000 )
1571 bo_add_8( p_bo, vals[2] );
1572 bo_add_8( p_bo, vals[3] );
1574 else if( i_size < 0x00200000 )
1576 bo_add_8( p_bo, vals[1] );
1577 bo_add_8( p_bo, vals[2] );
1578 bo_add_8( p_bo, vals[3] );
1580 else if( i_size < 0x10000000 )
1582 bo_add_8( p_bo, vals[0] );
1583 bo_add_8( p_bo, vals[1] );
1584 bo_add_8( p_bo, vals[2] );
1585 bo_add_8( p_bo, vals[3] );
1589 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1593 for( i = 0; i < p_bo2->i_buffer; i++ )
1595 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1599 static bo_t * box_new( char *fcc )
1603 if( ( box = malloc( sizeof( bo_t ) ) ) )
1605 bo_init( box, 0, NULL, VLC_TRUE );
1607 bo_add_32be ( box, 0 );
1608 bo_add_fourcc( box, fcc );
1614 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
1618 if( ( box = malloc( sizeof( bo_t ) ) ) )
1620 bo_init( box, 0, NULL, VLC_TRUE );
1622 bo_add_32be ( box, 0 );
1623 bo_add_fourcc( box, fcc );
1624 bo_add_8 ( box, v );
1625 bo_add_24be ( box, f );
1631 static void box_fix( bo_t *box )
1635 memcpy( &box_tmp, box, sizeof( bo_t ) );
1637 box_tmp.i_buffer = 0;
1638 bo_add_32be( &box_tmp, box->i_buffer );
1641 static void box_free( bo_t *box )
1645 free( box->p_buffer );
1651 static void box_gather ( bo_t *box, bo_t *box2 )
1653 bo_add_bo( box, box2 );
1657 static sout_buffer_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
1659 sout_buffer_t *p_buf;
1661 p_buf = sout_BufferNew( p_sout, box->i_buffer );
1662 if( box->i_buffer > 0 )
1664 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
1667 p_buf->i_size = box->i_buffer;
1672 static void box_send( sout_mux_t *p_mux, bo_t *box )
1674 sout_buffer_t *p_buf;
1676 p_buf = bo_to_sout( p_mux->p_sout, box );
1679 sout_AccessOutWrite( p_mux->p_access, p_buf );
1682 static int64_t get_timestamp()
1684 int64_t i_timestamp = 0;
1687 i_timestamp = time(NULL);
1688 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
1689 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60