1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003, 2006 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 "Create \"Fast Start\" files. " \
48 "\"Fast Start\" files are optimized for downloads and allow the user " \
49 "to start previewing the file while it is downloading.")
51 static int Open ( vlc_object_t * );
52 static void Close ( vlc_object_t * );
54 #define SOUT_CFG_PREFIX "sout-mp4-"
57 set_description( _("MP4/MOV muxer") );
58 set_category( CAT_SOUT );
59 set_subcategory( SUBCAT_SOUT_MUX );
60 set_shortname( "MP4" );
62 add_bool( SOUT_CFG_PREFIX "faststart", 1, NULL,
63 FASTSTART_TEXT, FASTSTART_LONGTEXT,
65 set_capability( "sout mux", 5 );
66 add_shortcut( "mp4" );
67 add_shortcut( "mov" );
68 add_shortcut( "3gp" );
69 set_callbacks( Open, Close );
72 /*****************************************************************************
74 *****************************************************************************/
75 static const char *ppsz_sout_options[] = {
79 static int Control( sout_mux_t *, int, va_list );
80 static int AddStream( sout_mux_t *, sout_input_t * );
81 static int DelStream( sout_mux_t *, sout_input_t * );
82 static int Mux ( sout_mux_t * );
84 /*****************************************************************************
86 *****************************************************************************/
104 unsigned int i_entry_count;
105 unsigned int i_entry_max;
107 int64_t i_length_neg;
113 /* for later stco fix-up (fast start files) */
134 struct sout_mux_sys_t
139 vlc_bool_t b_fast_start;
147 mp4_stream_t **pp_streams;
160 static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
161 static void bo_add_8 ( bo_t *, uint8_t );
162 static void bo_add_16be ( bo_t *, uint16_t );
163 static void bo_add_24be ( bo_t *, uint32_t );
164 static void bo_add_32be ( bo_t *, uint32_t );
165 static void bo_add_64be ( bo_t *, uint64_t );
166 static void bo_add_fourcc(bo_t *, char * );
167 static void bo_add_bo ( bo_t *, bo_t * );
168 static void bo_add_mem ( bo_t *, int , uint8_t * );
169 static void bo_add_descr( bo_t *, uint8_t , uint32_t );
171 static void bo_fix_32be ( bo_t *, int , uint32_t );
173 static bo_t *box_new ( char *fcc );
174 static bo_t *box_full_new( char *fcc, uint8_t v, uint32_t f );
175 static void box_fix ( bo_t *box );
176 static void box_free ( bo_t *box );
177 static void box_gather ( bo_t *box, bo_t *box2 );
179 static void box_send( sout_mux_t *p_mux, bo_t *box );
181 static block_t *bo_to_sout( sout_instance_t *p_sout, bo_t *box );
183 static bo_t *GetMoovBox( sout_mux_t *p_mux );
185 static block_t *ConvertSUBT( sout_mux_t *, mp4_stream_t *, block_t *);
186 static void ConvertAVC1( sout_mux_t *, mp4_stream_t *, block_t * );
188 /*****************************************************************************
190 *****************************************************************************/
191 static int Open( vlc_object_t *p_this )
193 sout_mux_t *p_mux = (sout_mux_t*)p_this;
194 sout_mux_sys_t *p_sys;
197 msg_Dbg( p_mux, "Mp4 muxer opend" );
198 sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
200 p_mux->pf_control = Control;
201 p_mux->pf_addstream = AddStream;
202 p_mux->pf_delstream = DelStream;
204 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
206 p_sys->i_nb_streams = 0;
207 p_sys->pp_streams = NULL;
208 p_sys->i_mdat_pos = 0;
209 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
210 p_sys->b_3gp = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "3gp" );
211 p_sys->i_dts_start = 0;
216 /* Now add ftyp header */
217 box = box_new( "ftyp" );
218 if( p_sys->b_3gp ) bo_add_fourcc( box, "3gp4" );
219 else bo_add_fourcc( box, "isom" );
220 bo_add_32be ( box, 0 );
221 if( p_sys->b_3gp ) bo_add_fourcc( box, "3gp4" );
222 else bo_add_fourcc( box, "mp41" );
225 p_sys->i_pos += box->i_buffer;
226 p_sys->i_mdat_pos = p_sys->i_pos;
228 box_send( p_mux, box );
232 * Quicktime actually doesn't like the 64 bits extensions !!! */
233 p_sys->b_64_ext = VLC_FALSE;
235 /* Now add mdat header */
236 box = box_new( "mdat" );
237 bo_add_64be ( box, 0 ); // enough to store an extended size
239 p_sys->i_pos += box->i_buffer;
241 box_send( p_mux, box );
246 /*****************************************************************************
248 *****************************************************************************/
249 static void Close( vlc_object_t * p_this )
251 sout_mux_t *p_mux = (sout_mux_t*)p_this;
252 sout_mux_sys_t *p_sys = p_mux->p_sys;
260 msg_Dbg( p_mux, "Close" );
262 /* Update mdat size */
263 bo_init( &bo, 0, NULL, VLC_TRUE );
264 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
267 bo_add_32be ( &bo, 1 );
268 bo_add_fourcc( &bo, "mdat" );
269 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
273 bo_add_32be ( &bo, 8 );
274 bo_add_fourcc( &bo, "wide" );
275 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
276 bo_add_fourcc( &bo, "mdat" );
278 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
281 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
282 sout_AccessOutWrite( p_mux->p_access, p_hdr );
284 /* Create MOOV header */
285 i_moov_pos = p_sys->i_pos;
286 moov = GetMoovBox( p_mux );
288 /* Check we need to create "fast start" files */
289 var_Get( p_this, SOUT_CFG_PREFIX "faststart", &val );
290 p_sys->b_fast_start = val.b_bool;
291 while( p_sys->b_fast_start )
293 /* Move data to the end of the file so we can fit the moov header
296 int64_t i_chunk, i_size = p_sys->i_pos - p_sys->i_mdat_pos;
297 int i_moov_size = moov->i_buffer;
301 i_chunk = __MIN( 32768, i_size );
302 p_buf = block_New( p_mux, i_chunk );
303 sout_AccessOutSeek( p_mux->p_access,
304 p_sys->i_mdat_pos + i_size - i_chunk );
305 if( sout_AccessOutRead( p_mux->p_access, p_buf ) < i_chunk )
307 msg_Warn( p_this, "read() not supported by access output, "
308 "won't create a fast start file" );
309 p_sys->b_fast_start = VLC_FALSE;
310 block_Release( p_buf );
313 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos + i_size +
314 i_moov_size - i_chunk );
315 sout_AccessOutWrite( p_mux->p_access, p_buf );
319 if( !p_sys->b_fast_start ) break;
321 /* Fix-up samples to chunks table in MOOV header */
322 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
324 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
328 moov->i_buffer = p_stream->i_stco_pos;
329 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
331 if( p_stream->b_stco64 )
332 bo_add_64be( moov, p_stream->entry[i].i_pos + i_moov_size);
334 bo_add_32be( moov, p_stream->entry[i].i_pos + i_moov_size);
336 while( i < p_stream->i_entry_count )
338 if( i + 1 < p_stream->i_entry_count &&
339 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
340 != p_stream->entry[i + 1].i_pos )
351 moov->i_buffer = i_moov_size;
352 i_moov_pos = p_sys->i_mdat_pos;
353 p_sys->b_fast_start = VLC_FALSE;
356 /* Write MOOV header */
357 sout_AccessOutSeek( p_mux->p_access, i_moov_pos );
358 box_send( p_mux, moov );
361 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
363 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
365 es_format_Clean( &p_stream->fmt );
366 if( p_stream->avc.i_sps ) free( p_stream->avc.sps );
367 if( p_stream->avc.i_pps ) free( p_stream->avc.pps );
368 free( p_stream->entry );
371 if( p_sys->i_nb_streams ) free( p_sys->pp_streams );
375 /*****************************************************************************
377 *****************************************************************************/
378 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
384 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
385 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
386 *pb_bool = VLC_FALSE;
389 case MUX_GET_ADD_STREAM_WAIT:
390 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
394 case MUX_GET_MIME: /* Not needed, as not streamable */
400 /*****************************************************************************
402 *****************************************************************************/
403 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
405 sout_mux_sys_t *p_sys = p_mux->p_sys;
406 mp4_stream_t *p_stream;
408 switch( p_input->p_fmt->i_codec )
410 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
411 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
412 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
413 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
414 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
415 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
416 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
417 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
418 case VLC_FOURCC( 'H', '2', '6', '3' ):
419 case VLC_FOURCC( 'h', '2', '6', '4' ):
420 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
421 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
423 case VLC_FOURCC( 's', 'u', 'b', 't' ):
424 msg_Warn( p_mux, "subtitle track added like in .mov (even when creating .mp4)" );
427 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
428 (char*)&p_input->p_fmt->i_codec );
432 p_stream = malloc( sizeof( mp4_stream_t ) );
433 es_format_Copy( &p_stream->fmt, p_input->p_fmt );
434 p_stream->i_track_id = p_sys->i_nb_streams + 1;
435 p_stream->i_length_neg = 0;
436 p_stream->i_entry_count = 0;
437 p_stream->i_entry_max = 1000;
439 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
440 p_stream->i_dts_start = 0;
441 p_stream->i_duration = 0;
442 p_stream->avc.i_profile = 77;
443 p_stream->avc.i_level = 51;
444 p_stream->avc.i_sps = 0;
445 p_stream->avc.sps = NULL;
446 p_stream->avc.i_pps = 0;
447 p_stream->avc.pps = NULL;
449 p_input->p_sys = p_stream;
451 msg_Dbg( p_mux, "adding input" );
453 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
457 /*****************************************************************************
459 *****************************************************************************/
460 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
462 msg_Dbg( p_mux, "removing input" );
466 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
472 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
474 block_fifo_t *p_fifo = p_mux->pp_inputs[i]->p_fifo;
477 if( p_fifo->i_depth <= 1 )
479 if( p_mux->pp_inputs[i]->p_fmt->i_cat != SPU_ES )
481 return -1; // wait that all fifo have at least 2 packets
483 /* For SPU, we wait only 1 packet */
487 p_buf = block_FifoShow( p_fifo );
488 if( i_stream < 0 || p_buf->i_dts < i_dts )
490 i_dts = p_buf->i_dts;
496 *pi_stream = i_stream;
505 /*****************************************************************************
507 *****************************************************************************/
508 static int Mux( sout_mux_t *p_mux )
510 sout_mux_sys_t *p_sys = p_mux->p_sys;
514 sout_input_t *p_input;
516 mp4_stream_t *p_stream;
520 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
522 return( VLC_SUCCESS );
525 p_input = p_mux->pp_inputs[i_stream];
526 p_stream = (mp4_stream_t*)p_input->p_sys;
528 p_data = block_FifoGet( p_input->p_fifo );
529 if( p_stream->fmt.i_codec == VLC_FOURCC( 'h', '2', '6', '4' ) )
531 ConvertAVC1( p_mux, p_stream, p_data );
533 else if( p_stream->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
535 p_data = ConvertSUBT( p_mux, p_stream, p_data );
538 if( p_stream->fmt.i_cat != SPU_ES )
540 /* Fix length of the sample */
541 if( p_input->p_fifo->i_depth > 0 )
543 block_t *p_next = block_FifoShow( p_input->p_fifo );
544 int64_t i_diff = p_next->i_dts - p_data->i_dts;
546 if( i_diff < I64C(1000000 ) ) /* protection */
548 p_data->i_length = i_diff;
551 if( p_data->i_length <= 0 )
553 msg_Warn( p_mux, "i_length <= 0" );
554 p_stream->i_length_neg += p_data->i_length - 1;
555 p_data->i_length = 1;
557 else if( p_stream->i_length_neg < 0 )
559 int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
561 p_data->i_length -= i_recover;
562 p_stream->i_length_neg += i_recover;
566 /* Save starting time */
567 if( p_stream->i_entry_count == 0 )
569 p_stream->i_dts_start = p_data->i_dts;
571 /* Update global dts_start */
572 if( p_sys->i_dts_start <= 0 ||
573 p_stream->i_dts_start < p_sys->i_dts_start )
575 p_sys->i_dts_start = p_stream->i_dts_start;
579 if( p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0 )
581 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
585 /* FIXME handle this broken case */
590 if( p_stream->entry[p_stream->i_entry_count-1].i_length <= 0 )
592 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
597 /* add index entry */
598 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
599 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_buffer;
600 p_stream->entry[p_stream->i_entry_count].i_pts_dts=
601 __MAX( p_data->i_pts - p_data->i_dts, 0 );
602 p_stream->entry[p_stream->i_entry_count].i_length = p_data->i_length;
603 p_stream->entry[p_stream->i_entry_count].i_flags = p_data->i_flags;
605 p_stream->i_entry_count++;
606 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
607 if( p_stream->i_entry_count >= p_stream->i_entry_max - 1 )
609 p_stream->i_entry_max += 1000;
611 realloc( p_stream->entry,
612 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
616 p_stream->i_duration += p_data->i_length;
617 p_sys->i_pos += p_data->i_buffer;
620 p_stream->i_last_dts = p_data->i_dts;
623 sout_AccessOutWrite( p_mux->p_access, p_data );
625 if( p_stream->fmt.i_cat == SPU_ES )
627 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
632 msg_Dbg( p_mux, "writing an empty sub" ) ;
634 /* Append a idx entry */
635 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
636 p_stream->entry[p_stream->i_entry_count].i_size = 3;
637 p_stream->entry[p_stream->i_entry_count].i_pts_dts= 0;
638 p_stream->entry[p_stream->i_entry_count].i_length = 0;
639 p_stream->entry[p_stream->i_entry_count].i_flags = 0;
641 /* XXX: No need to grow the entry here */
642 p_stream->i_entry_count++;
645 p_stream->i_last_dts += i_length;
648 p_data = block_New( p_mux, 3 );
649 p_data->p_buffer[0] = 0;
650 p_data->p_buffer[1] = 1;
651 p_data->p_buffer[2] = ' ';
653 p_sys->i_pos += p_data->i_buffer;
655 sout_AccessOutWrite( p_mux->p_access, p_data );
659 p_stream->i_duration = p_stream->i_last_dts - p_stream->i_dts_start;
663 return( VLC_SUCCESS );
666 /*****************************************************************************
668 *****************************************************************************/
669 static block_t *ConvertSUBT( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
671 p_block = block_Realloc( p_block, 2, p_block->i_buffer );
673 /* No trailling '\0' */
674 if( p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0' )
677 p_block->p_buffer[0] = ( (p_block->i_buffer - 2) >> 8 )&0xff;
678 p_block->p_buffer[1] = ( (p_block->i_buffer - 2) )&0xff;
683 static void ConvertAVC1( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
685 uint8_t *last = p_block->p_buffer; /* Assume it starts with 0x00000001 */
686 uint8_t *dat = &p_block->p_buffer[4];
687 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
690 /* Replace the 4 bytes start code with 4 bytes size,
691 * FIXME are all startcodes 4 bytes ? (I don't think :( */
696 while( dat < end - 4 )
698 if( dat[0] == 0x00 && dat[1] == 0x00 &&
699 dat[2] == 0x00 && dat[3] == 0x01 )
711 i_size = dat - &last[4];
712 last[0] = ( i_size >> 24 )&0xff;
713 last[1] = ( i_size >> 16 )&0xff;
714 last[2] = ( i_size >> 8 )&0xff;
715 last[3] = ( i_size )&0xff;
717 if( (last[4]&0x1f) == 7 && tk->avc.i_sps <= 0 ) /* SPS */
719 tk->avc.i_sps = i_size;
720 tk->avc.sps = malloc( i_size );
721 memcpy( tk->avc.sps, &last[4], i_size );
723 tk->avc.i_profile = tk->avc.sps[1];
724 tk->avc.i_level = tk->avc.sps[3];
726 else if( (last[4]&0x1f) == 8 && tk->avc.i_pps <= 0 ) /* PPS */
728 tk->avc.i_pps = i_size;
729 tk->avc.pps = malloc( i_size );
730 memcpy( tk->avc.pps, &last[4], i_size );
739 static int GetDescrLength( int i_size )
741 if( i_size < 0x00000080 )
743 else if( i_size < 0x00004000 )
745 else if( i_size < 0x00200000 )
751 static bo_t *GetESDS( mp4_stream_t *p_stream )
755 int i_object_type_indication;
756 int i_decoder_specific_info_size;
758 int64_t i_bitrate_avg = 0;
759 int64_t i_bitrate_max = 0;
761 /* Compute avg/max bitrate */
762 for( i = 0; i < p_stream->i_entry_count; i++ )
764 i_bitrate_avg += p_stream->entry[i].i_size;
765 if( p_stream->entry[i].i_length > 0)
767 int64_t i_bitrate = I64C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
768 if( i_bitrate > i_bitrate_max )
769 i_bitrate_max = i_bitrate;
773 if( p_stream->i_duration > 0 )
774 i_bitrate_avg = I64C(8000000) * i_bitrate_avg / p_stream->i_duration;
777 if( i_bitrate_max <= 1 )
778 i_bitrate_max = 0x7fffffff;
781 if( p_stream->fmt.i_extra > 0 )
783 i_decoder_specific_info_size =
784 GetDescrLength( p_stream->fmt.i_extra );
788 i_decoder_specific_info_size = 0;
791 esds = box_full_new( "esds", 0, 0 );
794 bo_add_descr( esds, 0x03, 3 +
795 GetDescrLength( 13 + i_decoder_specific_info_size ) +
796 GetDescrLength( 1 ) );
797 bo_add_16be( esds, p_stream->i_track_id );
798 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
800 /* DecoderConfigDescr */
801 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
803 switch( p_stream->fmt.i_codec )
805 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
806 i_object_type_indication = 0x20;
808 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
809 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
810 i_object_type_indication = 0x60;
812 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
813 /* FIXME for mpeg2-aac == 0x66->0x68 */
814 i_object_type_indication = 0x40;
816 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
817 i_object_type_indication =
818 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
821 i_object_type_indication = 0x00;
824 i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
826 bo_add_8 ( esds, i_object_type_indication );
827 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
828 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
829 bo_add_32be( esds, i_bitrate_max ); // maxBitrate
830 bo_add_32be( esds, i_bitrate_avg ); // avgBitrate
832 if( p_stream->fmt.i_extra > 0 )
836 /* DecoderSpecificInfo */
837 bo_add_descr( esds, 0x05, p_stream->fmt.i_extra );
839 for( i = 0; i < p_stream->fmt.i_extra; i++ )
841 bo_add_8( esds, ((uint8_t*)p_stream->fmt.p_extra)[i] );
845 /* SL_Descr mandatory */
846 bo_add_descr( esds, 0x06, 1 );
847 bo_add_8 ( esds, 0x02 ); // sl_predefined
854 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
859 wave = box_new( "wave" );
861 box = box_new( "frma" );
862 bo_add_fourcc( box, "mp4a" );
864 box_gather( wave, box );
866 box = box_new( "mp4a" );
867 bo_add_32be( box, 0 );
869 box_gather( wave, box );
871 box = GetESDS( p_stream );
873 box_gather( wave, box );
875 box = box_new( "srcq" );
876 bo_add_32be( box, 0x40 );
878 box_gather( wave, box );
881 bo_add_32be( wave, 8 ); /* new empty box */
882 bo_add_32be( wave, 0 ); /* box label */
889 static bo_t *GetDamrTag( mp4_stream_t *p_stream )
893 damr = box_new( "damr" );
895 bo_add_fourcc( damr, "REFC" );
898 if( p_stream->fmt.i_codec == VLC_FOURCC( 's', 'a', 'm', 'r' ) )
899 bo_add_16be( damr, 0x81ff ); /* Mode set (all modes for AMR_NB) */
901 bo_add_16be( damr, 0x83ff ); /* Mode set (all modes for AMR_WB) */
902 bo_add_16be( damr, 0x1 ); /* Mode change period (no restriction) */
909 static bo_t *GetD263Tag( mp4_stream_t *p_stream )
913 d263 = box_new( "d263" );
915 bo_add_fourcc( d263, "VLC " );
916 bo_add_16be( d263, 0xa );
924 static bo_t *GetAvcCTag( mp4_stream_t *p_stream )
928 /* FIXME use better value */
929 avcC = box_new( "avcC" );
930 bo_add_8( avcC, 1 ); /* configuration version */
931 bo_add_8( avcC, p_stream->avc.i_profile );
932 bo_add_8( avcC, p_stream->avc.i_profile ); /* profile compatible ??? */
933 bo_add_8( avcC, p_stream->avc.i_level ); /* level, 5.1 */
934 bo_add_8( avcC, 0xff ); /* 0b11111100 | lengthsize = 0x11 */
936 bo_add_8( avcC, 0xe0 | (p_stream->avc.i_sps > 0 ? 1 : 0) ); /* 0b11100000 | sps_count */
937 if( p_stream->avc.i_sps > 0 )
939 bo_add_16be( avcC, p_stream->avc.i_sps );
940 bo_add_mem( avcC, p_stream->avc.i_sps, p_stream->avc.sps );
943 bo_add_8( avcC, (p_stream->avc.i_pps > 0 ? 1 : 0) ); /* pps_count */
944 if( p_stream->avc.i_pps > 0 )
946 bo_add_16be( avcC, p_stream->avc.i_pps );
947 bo_add_mem( avcC, p_stream->avc.i_pps, p_stream->avc.pps );
954 /* TODO: No idea about these values */
955 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
957 bo_t *smi = box_new( "SMI " );
959 if( p_stream->fmt.i_extra > 0x4e )
961 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
962 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
964 while( p + 8 < p_end )
966 int i_size = GetDWBE( p );
969 /* FIXME handle 1 as long size */
972 if( !strncmp( (const char *)&p[4], "SMI ", 4 ) )
974 bo_add_mem( smi, p_end - p - 8, &p[8] );
981 /* Create a dummy one in fallback */
982 bo_add_fourcc( smi, "SEQH" );
983 bo_add_32be( smi, 0x5 );
984 bo_add_32be( smi, 0xe2c0211d );
985 bo_add_8( smi, 0xc0 );
991 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
993 sout_mux_sys_t *p_sys = p_mux->p_sys;
994 bo_t *udta = box_new( "udta" );
995 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
999 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
1001 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
1003 if( p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','v') ||
1004 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1006 bo_t *box = box_new( "\251req" );
1008 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
1009 bo_add_16be( box, 0 );
1010 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
1011 (uint8_t *)"QuickTime 6.0 or greater" );
1013 box_gather( udta, box );
1020 bo_t *box = box_new( "\251enc" );
1022 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
1023 bo_add_16be( box, 0 );
1024 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
1025 (uint8_t*)PACKAGE_STRING " stream output" );
1027 box_gather( udta, box );
1033 #define ADD_META_BOX( type, box_string ) { \
1035 if( p_meta->psz_##type ) box = box_new( "\251" box_string ); \
1038 bo_add_16be( box, strlen( p_meta->psz_##type ) ); \
1039 bo_add_16be( box, 0 ); \
1040 bo_add_mem( box, strlen( p_meta->psz_##type ), \
1041 (uint8_t*)(p_meta->psz_##type ) ); \
1043 box_gather( udta, box ); \
1046 ADD_META_BOX( title, "nam" );
1047 ADD_META_BOX( author, "aut" );
1048 ADD_META_BOX( artist, "ART" );
1049 ADD_META_BOX( genre, "gen" );
1050 ADD_META_BOX( copyright, "cpy" );
1051 ADD_META_BOX( description, "des" );
1052 ADD_META_BOX( date, "day" );
1053 ADD_META_BOX( url, "url" );
1061 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1063 sout_mux_sys_t *p_sys = p_mux->p_sys;
1064 vlc_bool_t b_descr = VLC_FALSE;
1069 switch( p_stream->fmt.i_codec )
1071 case VLC_FOURCC('m','p','4','a'):
1072 memcpy( fcc, "mp4a", 4 );
1076 case VLC_FOURCC('s','a','m','r'):
1077 case VLC_FOURCC('s','a','w','b'):
1078 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1082 case VLC_FOURCC('m','p','g','a'):
1084 memcpy( fcc, ".mp3", 4 );
1087 memcpy( fcc, "mp4a", 4 );
1093 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1097 soun = box_new( fcc );
1098 for( i = 0; i < 6; i++ )
1100 bo_add_8( soun, 0 ); // reserved;
1102 bo_add_16be( soun, 1 ); // data-reference-index
1104 /* SoundDescription */
1106 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1108 bo_add_16be( soun, 1 ); // version 1;
1112 bo_add_16be( soun, 0 ); // version 0;
1114 bo_add_16be( soun, 0 ); // revision level (0)
1115 bo_add_32be( soun, 0 ); // vendor
1117 bo_add_16be( soun, p_stream->fmt.audio.i_channels );
1119 bo_add_16be( soun, p_stream->fmt.audio.i_bitspersample ?
1120 p_stream->fmt.audio.i_bitspersample : 16 );
1121 bo_add_16be( soun, -2 ); // compression id
1122 bo_add_16be( soun, 0 ); // packet size (0)
1123 bo_add_16be( soun, p_stream->fmt.audio.i_rate ); // sampleratehi
1124 bo_add_16be( soun, 0 ); // sampleratelo
1126 /* Extended data for SoundDescription V1 */
1128 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1130 /* samples per packet */
1131 bo_add_32be( soun, p_stream->fmt.audio.i_frame_length );
1132 bo_add_32be( soun, 1536 ); /* bytes per packet */
1133 bo_add_32be( soun, 2 ); /* bytes per frame */
1134 /* bytes per sample */
1135 bo_add_32be( soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1138 /* Add an ES Descriptor */
1144 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1146 box = GetWaveTag( p_stream );
1148 else if( p_stream->fmt.i_codec == VLC_FOURCC('s','a','m','r') )
1150 box = GetDamrTag( p_stream );
1154 box = GetESDS( p_stream );
1157 box_gather( soun, box );
1165 static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1172 switch( p_stream->fmt.i_codec )
1174 case VLC_FOURCC('m','p','4','v'):
1175 case VLC_FOURCC('m','p','g','v'):
1176 memcpy( fcc, "mp4v", 4 );
1179 case VLC_FOURCC('M','J','P','G'):
1180 memcpy( fcc, "mjpa", 4 );
1183 case VLC_FOURCC('S','V','Q','1'):
1184 memcpy( fcc, "SVQ1", 4 );
1187 case VLC_FOURCC('S','V','Q','3'):
1188 memcpy( fcc, "SVQ3", 4 );
1191 case VLC_FOURCC('H','2','6','3'):
1192 memcpy( fcc, "s263", 4 );
1195 case VLC_FOURCC('h','2','6','4'):
1196 memcpy( fcc, "avc1", 4 );
1200 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1204 vide = box_new( fcc );
1205 for( i = 0; i < 6; i++ )
1207 bo_add_8( vide, 0 ); // reserved;
1209 bo_add_16be( vide, 1 ); // data-reference-index
1211 bo_add_16be( vide, 0 ); // predefined;
1212 bo_add_16be( vide, 0 ); // reserved;
1213 for( i = 0; i < 3; i++ )
1215 bo_add_32be( vide, 0 ); // predefined;
1218 bo_add_16be( vide, p_stream->fmt.video.i_width ); // i_width
1219 bo_add_16be( vide, p_stream->fmt.video.i_height ); // i_height
1221 bo_add_32be( vide, 0x00480000 ); // h 72dpi
1222 bo_add_32be( vide, 0x00480000 ); // v 72dpi
1224 bo_add_32be( vide, 0 ); // data size, always 0
1225 bo_add_16be( vide, 1 ); // frames count per sample
1228 for( i = 0; i < 32; i++ )
1230 bo_add_8( vide, 0 );
1233 bo_add_16be( vide, 0x18 ); // depth
1234 bo_add_16be( vide, 0xffff ); // predefined
1236 /* add an ES Descriptor */
1237 switch( p_stream->fmt.i_codec )
1239 case VLC_FOURCC('m','p','4','v'):
1240 case VLC_FOURCC('m','p','g','v'):
1242 bo_t *esds = GetESDS( p_stream );
1245 box_gather( vide, esds );
1249 case VLC_FOURCC('H','2','6','3'):
1251 bo_t *d263 = GetD263Tag( p_stream );
1254 box_gather( vide, d263 );
1258 case VLC_FOURCC('S','V','Q','3'):
1260 bo_t *esds = GetSVQ3Tag( p_stream );
1263 box_gather( vide, esds );
1267 case VLC_FOURCC('h','2','6','4'):
1268 box_gather( vide, GetAvcCTag( p_stream ) );
1280 static bo_t *GetTextBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1283 bo_t *text = box_new( "text" );
1286 for( i = 0; i < 6; i++ )
1288 bo_add_8( text, 0 ); // reserved;
1290 bo_add_16be( text, 1 ); // data-reference-index
1292 bo_add_32be( text, 0 ); // display flags
1293 bo_add_32be( text, 0 ); // justification
1294 for( i = 0; i < 3; i++ )
1296 bo_add_16be( text, 0 ); // back ground color
1299 bo_add_16be( text, 0 ); // box text
1300 bo_add_16be( text, 0 ); // box text
1301 bo_add_16be( text, 0 ); // box text
1302 bo_add_16be( text, 0 ); // box text
1304 bo_add_64be( text, 0 ); // reserved
1305 for( i = 0; i < 3; i++ )
1307 bo_add_16be( text, 0xff ); // foreground color
1310 bo_add_8 ( text, 9 );
1311 bo_add_mem( text, 9, (uint8_t*)"Helvetica" );
1318 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1320 sout_mux_sys_t *p_sys = p_mux->p_sys;
1321 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
1322 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz, *stss;
1323 uint32_t i_timescale;
1324 int64_t i_dts, i_dts_q;
1326 stbl = box_new( "stbl" );
1328 /* sample description */
1329 stsd = box_full_new( "stsd", 0, 0 );
1330 bo_add_32be( stsd, 1 );
1331 if( p_stream->fmt.i_cat == AUDIO_ES )
1333 bo_t *soun = GetSounBox( p_mux, p_stream );
1334 box_gather( stsd, soun );
1336 else if( p_stream->fmt.i_cat == VIDEO_ES )
1338 bo_t *vide = GetVideBox( p_mux, p_stream );
1339 box_gather( stsd, vide );
1341 else if( p_stream->fmt.i_cat == SPU_ES )
1343 box_gather( stsd, GetTextBox( p_mux, p_stream ) );
1347 /* chunk offset table */
1348 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
1350 /* 64 bits version */
1351 p_stream->b_stco64 = VLC_TRUE;
1352 stco = box_full_new( "co64", 0, 0 );
1356 /* 32 bits version */
1357 p_stream->b_stco64 = VLC_FALSE;
1358 stco = box_full_new( "stco", 0, 0 );
1360 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
1362 /* sample to chunk table */
1363 stsc = box_full_new( "stsc", 0, 0 );
1364 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
1366 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
1367 i < p_stream->i_entry_count; i_chunk++ )
1371 if( p_stream->b_stco64 )
1372 bo_add_64be( stco, p_stream->entry[i].i_pos );
1374 bo_add_32be( stco, p_stream->entry[i].i_pos );
1376 while( i < p_stream->i_entry_count )
1378 if( i + 1 < p_stream->i_entry_count &&
1379 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
1380 != p_stream->entry[i + 1].i_pos )
1389 /* Add entry to the stsc table */
1390 if( i_stsc_last_val != i - i_first )
1392 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
1393 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
1394 bo_add_32be( stsc, 1 ); // sample-descr-index
1395 i_stsc_last_val = i - i_first;
1400 /* Fix stco entry count */
1401 bo_fix_32be( stco, 12, i_chunk );
1402 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
1405 /* Fix stsc entry count */
1406 bo_fix_32be( stsc, 12, i_stsc_entries );
1410 stts = box_full_new( "stts", 0, 0 );
1411 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
1413 if( p_stream->fmt.i_cat == AUDIO_ES )
1414 i_timescale = p_stream->fmt.audio.i_rate;
1418 /* first, create quantified length */
1419 for( i = 0, i_dts = 0, i_dts_q = 0; i < p_stream->i_entry_count; i++ )
1421 int64_t i_dts_deq = i_dts_q * I64C(1000000) / (int64_t)i_timescale;
1422 int64_t i_delta = p_stream->entry[i].i_length + i_dts - i_dts_deq;
1424 i_dts += p_stream->entry[i].i_length;
1426 p_stream->entry[i].i_length =
1427 i_delta * (int64_t)i_timescale / I64C(1000000);
1429 i_dts_q += p_stream->entry[i].i_length;
1431 /* then write encoded table */
1432 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
1435 int64_t i_delta = p_stream->entry[i].i_length;
1437 while( i < p_stream->i_entry_count )
1440 if( i >= p_stream->i_entry_count ||
1441 p_stream->entry[i].i_length != i_delta )
1447 bo_add_32be( stts, i - i_first ); // sample-count
1448 bo_add_32be( stts, i_delta ); // sample-delta
1450 bo_fix_32be( stts, 12, i_index );
1453 /* FIXME add ctts ?? FIXME */
1455 stsz = box_full_new( "stsz", 0, 0 );
1456 bo_add_32be( stsz, 0 ); // sample-size
1457 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
1458 for( i = 0; i < p_stream->i_entry_count; i++ )
1460 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
1464 /* create stss table */
1466 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i++ )
1468 if( p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I )
1472 stss = box_full_new( "stss", 0, 0 );
1473 bo_add_32be( stss, 0 ); /* fixed later */
1475 bo_add_32be( stss, 1 + i );
1481 bo_fix_32be( stss, 12, i_index );
1485 /* Now gather all boxes into stbl */
1486 box_gather( stbl, stsd );
1487 box_gather( stbl, stts );
1490 box_gather( stbl, stss );
1492 box_gather( stbl, stsc );
1493 box_gather( stbl, stsz );
1494 p_stream->i_stco_pos = stbl->i_buffer + 16;
1495 box_gather( stbl, stco );
1503 static int64_t get_timestamp();
1505 static uint32_t mvhd_matrix[9] =
1506 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1508 static bo_t *GetMoovBox( sout_mux_t *p_mux )
1510 sout_mux_sys_t *p_sys = p_mux->p_sys;
1515 uint32_t i_movie_timescale = 90000;
1516 int64_t i_movie_duration = 0;
1518 moov = box_new( "moov" );
1520 /* Create general info */
1521 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1523 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1524 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
1526 msg_Dbg( p_mux, "movie duration %ds",
1527 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
1529 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
1531 /* *** add /moov/mvhd *** */
1532 if( !p_sys->b_64_ext )
1534 mvhd = box_full_new( "mvhd", 0, 0 );
1535 bo_add_32be( mvhd, get_timestamp() ); // creation time
1536 bo_add_32be( mvhd, get_timestamp() ); // modification time
1537 bo_add_32be( mvhd, i_movie_timescale); // timescale
1538 bo_add_32be( mvhd, i_movie_duration ); // duration
1542 mvhd = box_full_new( "mvhd", 1, 0 );
1543 bo_add_64be( mvhd, get_timestamp() ); // creation time
1544 bo_add_64be( mvhd, get_timestamp() ); // modification time
1545 bo_add_32be( mvhd, i_movie_timescale); // timescale
1546 bo_add_64be( mvhd, i_movie_duration ); // duration
1548 bo_add_32be( mvhd, 0x10000 ); // rate
1549 bo_add_16be( mvhd, 0x100 ); // volume
1550 bo_add_16be( mvhd, 0 ); // reserved
1551 for( i = 0; i < 2; i++ )
1553 bo_add_32be( mvhd, 0 ); // reserved
1555 for( i = 0; i < 9; i++ )
1557 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
1559 for( i = 0; i < 6; i++ )
1561 bo_add_32be( mvhd, 0 ); // pre-defined
1564 /* Next available track id */
1565 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
1568 box_gather( moov, mvhd );
1570 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1572 mp4_stream_t *p_stream;
1573 uint32_t i_timescale;
1575 bo_t *trak, *tkhd, *edts, *elst, *mdia, *mdhd, *hdlr;
1576 bo_t *minf, *dinf, *dref, *url, *stbl;
1578 p_stream = p_sys->pp_streams[i_trak];
1580 if( p_stream->fmt.i_cat == AUDIO_ES )
1581 i_timescale = p_stream->fmt.audio.i_rate;
1585 /* *** add /moov/trak *** */
1586 trak = box_new( "trak" );
1588 /* *** add /moov/trak/tkhd *** */
1589 if( !p_sys->b_64_ext )
1592 tkhd = box_full_new( "tkhd", 0, 0x0f );
1594 tkhd = box_full_new( "tkhd", 0, 1 );
1596 bo_add_32be( tkhd, get_timestamp() ); // creation time
1597 bo_add_32be( tkhd, get_timestamp() ); // modification time
1598 bo_add_32be( tkhd, p_stream->i_track_id );
1599 bo_add_32be( tkhd, 0 ); // reserved 0
1600 bo_add_32be( tkhd, p_stream->i_duration *
1601 (int64_t)i_movie_timescale /
1602 (mtime_t)1000000 ); // duration
1607 tkhd = box_full_new( "tkhd", 1, 0x0f );
1609 tkhd = box_full_new( "tkhd", 1, 1 );
1611 bo_add_64be( tkhd, get_timestamp() ); // creation time
1612 bo_add_64be( tkhd, get_timestamp() ); // modification time
1613 bo_add_32be( tkhd, p_stream->i_track_id );
1614 bo_add_32be( tkhd, 0 ); // reserved 0
1615 bo_add_64be( tkhd, p_stream->i_duration *
1616 (int64_t)i_movie_timescale /
1617 (mtime_t)1000000 ); // duration
1620 for( i = 0; i < 2; i++ )
1622 bo_add_32be( tkhd, 0 ); // reserved
1624 bo_add_16be( tkhd, 0 ); // layer
1625 bo_add_16be( tkhd, 0 ); // pre-defined
1627 bo_add_16be( tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0 );
1628 bo_add_16be( tkhd, 0 ); // reserved
1629 for( i = 0; i < 9; i++ )
1631 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
1633 if( p_stream->fmt.i_cat == AUDIO_ES )
1635 bo_add_32be( tkhd, 0 ); // width (presentation)
1636 bo_add_32be( tkhd, 0 ); // height(presentation)
1638 else if( p_stream->fmt.i_cat == VIDEO_ES )
1640 int i_width = p_stream->fmt.video.i_width << 16;
1641 if( p_stream->fmt.video.i_aspect > 0 )
1643 i_width = (int64_t)p_stream->fmt.video.i_aspect *
1644 ((int64_t)p_stream->fmt.video.i_height << 16) /
1647 // width (presentation)
1648 bo_add_32be( tkhd, i_width );
1649 // height(presentation)
1650 bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
1654 int i_width = 320 << 16;
1657 for( i = 0; i < p_sys->i_nb_streams; i++ )
1659 mp4_stream_t *tk = p_sys->pp_streams[i];
1660 if( tk->fmt.i_cat == VIDEO_ES )
1662 if( p_stream->fmt.video.i_aspect )
1663 i_width = (int64_t)p_stream->fmt.video.i_aspect *
1664 ((int64_t)p_stream->fmt.video.i_height<<16) / VOUT_ASPECT_FACTOR;
1666 i_width = p_stream->fmt.video.i_width << 16;
1667 i_height = p_stream->fmt.video.i_height;
1671 bo_add_32be( tkhd, i_width ); // width (presentation)
1672 bo_add_32be( tkhd, i_height << 16 ); // height(presentation)
1676 box_gather( trak, tkhd );
1678 /* *** add /moov/trak/edts and elst */
1679 edts = box_new( "edts" );
1680 elst = box_full_new( "elst", p_sys->b_64_ext ? 1 : 0, 0 );
1681 if( p_stream->i_dts_start > p_sys->i_dts_start )
1683 bo_add_32be( elst, 2 );
1685 if( p_sys->b_64_ext )
1687 bo_add_64be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1688 i_movie_timescale / I64C(1000000) );
1689 bo_add_64be( elst, -1 );
1693 bo_add_32be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1694 i_movie_timescale / I64C(1000000) );
1695 bo_add_32be( elst, -1 );
1697 bo_add_16be( elst, 1 );
1698 bo_add_16be( elst, 0 );
1702 bo_add_32be( elst, 1 );
1704 if( p_sys->b_64_ext )
1706 bo_add_64be( elst, p_stream->i_duration *
1707 i_movie_timescale / I64C(1000000) );
1708 bo_add_64be( elst, 0 );
1712 bo_add_32be( elst, p_stream->i_duration *
1713 i_movie_timescale / I64C(1000000) );
1714 bo_add_32be( elst, 0 );
1716 bo_add_16be( elst, 1 );
1717 bo_add_16be( elst, 0 );
1720 box_gather( edts, elst );
1722 box_gather( trak, edts );
1724 /* *** add /moov/trak/mdia *** */
1725 mdia = box_new( "mdia" );
1728 if( !p_sys->b_64_ext )
1730 mdhd = box_full_new( "mdhd", 0, 0 );
1731 bo_add_32be( mdhd, get_timestamp() ); // creation time
1732 bo_add_32be( mdhd, get_timestamp() ); // modification time
1733 bo_add_32be( mdhd, i_timescale); // timescale
1734 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1735 (mtime_t)1000000 ); // duration
1739 mdhd = box_full_new( "mdhd", 1, 0 );
1740 bo_add_64be( mdhd, get_timestamp() ); // creation time
1741 bo_add_64be( mdhd, get_timestamp() ); // modification time
1742 bo_add_32be( mdhd, i_timescale); // timescale
1743 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1744 (mtime_t)1000000 ); // duration
1747 if( p_stream->fmt.psz_language )
1749 char *psz = p_stream->fmt.psz_language;
1750 const iso639_lang_t *pl = NULL;
1751 uint16_t lang = 0x0;
1753 if( strlen( psz ) == 2 )
1755 pl = GetLang_1( psz );
1757 else if( strlen( psz ) == 3 )
1759 pl = GetLang_2B( psz );
1760 if( !strcmp( pl->psz_iso639_1, "??" ) )
1762 pl = GetLang_2T( psz );
1765 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1767 lang = ( ( pl->psz_iso639_2T[0] - 0x60 ) << 10 ) |
1768 ( ( pl->psz_iso639_2T[1] - 0x60 ) << 5 ) |
1769 ( ( pl->psz_iso639_2T[2] - 0x60 ) );
1771 bo_add_16be( mdhd, lang ); // language
1775 bo_add_16be( mdhd, 0 ); // language
1777 bo_add_16be( mdhd, 0 ); // predefined
1779 box_gather( mdia, mdhd );
1781 /* handler reference */
1782 hdlr = box_full_new( "hdlr", 0, 0 );
1785 bo_add_fourcc( hdlr, "mhlr" ); // media handler
1787 bo_add_32be( hdlr, 0 );
1789 if( p_stream->fmt.i_cat == AUDIO_ES )
1790 bo_add_fourcc( hdlr, "soun" );
1791 else if( p_stream->fmt.i_cat == VIDEO_ES )
1792 bo_add_fourcc( hdlr, "vide" );
1793 else if( p_stream->fmt.i_cat == SPU_ES )
1794 bo_add_fourcc( hdlr, "text" );
1796 bo_add_32be( hdlr, 0 ); // reserved
1797 bo_add_32be( hdlr, 0 ); // reserved
1798 bo_add_32be( hdlr, 0 ); // reserved
1801 bo_add_8( hdlr, 12 ); /* Pascal string for .mov */
1803 if( p_stream->fmt.i_cat == AUDIO_ES )
1804 bo_add_mem( hdlr, 12, (uint8_t*)"SoundHandler" );
1805 else if( p_stream->fmt.i_cat == VIDEO_ES )
1806 bo_add_mem( hdlr, 12, (uint8_t*)"VideoHandler" );
1808 bo_add_mem( hdlr, 12, (uint8_t*)"Text Handler" );
1811 bo_add_8( hdlr, 0 ); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
1814 box_gather( mdia, hdlr );
1817 minf = box_new( "minf" );
1820 if( p_stream->fmt.i_cat == AUDIO_ES )
1824 smhd = box_full_new( "smhd", 0, 0 );
1825 bo_add_16be( smhd, 0 ); // balance
1826 bo_add_16be( smhd, 0 ); // reserved
1829 box_gather( minf, smhd );
1831 else if( p_stream->fmt.i_cat == VIDEO_ES )
1835 vmhd = box_full_new( "vmhd", 0, 1 );
1836 bo_add_16be( vmhd, 0 ); // graphicsmode
1837 for( i = 0; i < 3; i++ )
1839 bo_add_16be( vmhd, 0 ); // opcolor
1843 box_gather( minf, vmhd );
1845 else if( p_stream->fmt.i_cat == SPU_ES )
1847 bo_t *gmhd = box_new( "gmhd" );
1848 bo_t *gmin = box_full_new( "gmin", 0, 1 );
1850 bo_add_16be( gmin, 0 ); // graphicsmode
1851 for( i = 0; i < 3; i++ )
1853 bo_add_16be( gmin, 0 ); // opcolor
1855 bo_add_16be( gmin, 0 ); // balance
1856 bo_add_16be( gmin, 0 ); // reserved
1859 box_gather( gmhd, gmin );
1862 box_gather( minf, gmhd );
1866 dinf = box_new( "dinf" );
1867 dref = box_full_new( "dref", 0, 0 );
1868 bo_add_32be( dref, 1 );
1869 url = box_full_new( "url ", 0, 0x01 );
1871 box_gather( dref, url );
1873 box_gather( dinf, dref );
1875 /* append dinf to mdia */
1877 box_gather( minf, dinf );
1880 stbl = GetStblBox( p_mux, p_stream );
1882 /* append stbl to minf */
1883 p_stream->i_stco_pos += minf->i_buffer;
1884 box_gather( minf, stbl );
1886 /* append minf to mdia */
1888 p_stream->i_stco_pos += mdia->i_buffer;
1889 box_gather( mdia, minf );
1891 /* append mdia to trak */
1893 p_stream->i_stco_pos += trak->i_buffer;
1894 box_gather( trak, mdia );
1896 /* append trak to moov */
1898 p_stream->i_stco_pos += moov->i_buffer;
1899 box_gather( moov, trak );
1902 /* Add user data tags */
1903 box_gather( moov, GetUdtaTag( p_mux ) );
1909 /****************************************************************************/
1911 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1916 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1917 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1921 p_bo->i_buffer_size = i_size;
1922 p_bo->p_buffer = p_buffer;
1925 p_bo->b_grow = b_grow;
1929 static void bo_add_8( bo_t *p_bo, uint8_t i )
1931 if( p_bo->i_buffer < p_bo->i_buffer_size )
1933 p_bo->p_buffer[p_bo->i_buffer] = i;
1935 else if( p_bo->b_grow )
1937 p_bo->i_buffer_size += 1024;
1938 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1940 p_bo->p_buffer[p_bo->i_buffer] = i;
1946 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1948 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1949 bo_add_8( p_bo, i &0xff );
1952 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1954 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1955 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1956 bo_add_8( p_bo, ( i &0xff ) );
1958 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1960 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1961 bo_add_16be( p_bo, i &0xffff );
1964 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1966 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1967 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1968 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1969 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1972 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1974 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1975 bo_add_32be( p_bo, i &0xffffffff );
1978 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1980 bo_add_8( p_bo, fcc[0] );
1981 bo_add_8( p_bo, fcc[1] );
1982 bo_add_8( p_bo, fcc[2] );
1983 bo_add_8( p_bo, fcc[3] );
1986 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1990 for( i = 0; i < i_size; i++ )
1992 bo_add_8( p_bo, p_mem[i] );
1996 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
2002 vals[3] = (unsigned char)(i_length & 0x7f);
2004 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
2006 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
2008 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
2010 bo_add_8( p_bo, tag );
2012 if( i_size < 0x00000080 )
2014 bo_add_8( p_bo, vals[3] );
2016 else if( i_size < 0x00004000 )
2018 bo_add_8( p_bo, vals[2] );
2019 bo_add_8( p_bo, vals[3] );
2021 else if( i_size < 0x00200000 )
2023 bo_add_8( p_bo, vals[1] );
2024 bo_add_8( p_bo, vals[2] );
2025 bo_add_8( p_bo, vals[3] );
2027 else if( i_size < 0x10000000 )
2029 bo_add_8( p_bo, vals[0] );
2030 bo_add_8( p_bo, vals[1] );
2031 bo_add_8( p_bo, vals[2] );
2032 bo_add_8( p_bo, vals[3] );
2036 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
2040 for( i = 0; i < p_bo2->i_buffer; i++ )
2042 bo_add_8( p_bo, p_bo2->p_buffer[i] );
2046 static bo_t * box_new( char *fcc )
2050 if( ( box = malloc( sizeof( bo_t ) ) ) )
2052 bo_init( box, 0, NULL, VLC_TRUE );
2054 bo_add_32be ( box, 0 );
2055 bo_add_fourcc( box, fcc );
2061 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
2065 if( ( box = malloc( sizeof( bo_t ) ) ) )
2067 bo_init( box, 0, NULL, VLC_TRUE );
2069 bo_add_32be ( box, 0 );
2070 bo_add_fourcc( box, fcc );
2071 bo_add_8 ( box, v );
2072 bo_add_24be ( box, f );
2078 static void box_fix( bo_t *box )
2082 memcpy( &box_tmp, box, sizeof( bo_t ) );
2084 box_tmp.i_buffer = 0;
2085 bo_add_32be( &box_tmp, box->i_buffer );
2088 static void box_free( bo_t *box )
2092 free( box->p_buffer );
2098 static void box_gather ( bo_t *box, bo_t *box2 )
2100 bo_add_bo( box, box2 );
2104 static block_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
2108 p_buf = block_New( p_sout, box->i_buffer );
2109 if( box->i_buffer > 0 )
2111 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
2117 static void box_send( sout_mux_t *p_mux, bo_t *box )
2121 p_buf = bo_to_sout( p_mux->p_sout, box );
2124 sout_AccessOutWrite( p_mux->p_access, p_buf );
2127 static int64_t get_timestamp()
2129 int64_t i_timestamp = 0;
2132 i_timestamp = time(NULL);
2133 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2134 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60