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 );
740 static int GetDescrLength( int i_size )
742 if( i_size < 0x00000080 )
744 else if( i_size < 0x00004000 )
746 else if( i_size < 0x00200000 )
752 static bo_t *GetESDS( mp4_stream_t *p_stream )
756 int i_object_type_indication;
757 int i_decoder_specific_info_size;
759 int64_t i_bitrate_avg = 0;
760 int64_t i_bitrate_max = 0;
762 /* Compute avg/max bitrate */
763 for( i = 0; i < p_stream->i_entry_count; i++ )
765 i_bitrate_avg += p_stream->entry[i].i_size;
766 if( p_stream->entry[i].i_length > 0)
768 int64_t i_bitrate = I64C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
769 if( i_bitrate > i_bitrate_max )
770 i_bitrate_max = i_bitrate;
774 if( p_stream->i_duration > 0 )
775 i_bitrate_avg = I64C(8000000) * i_bitrate_avg / p_stream->i_duration;
778 if( i_bitrate_max <= 1 )
779 i_bitrate_max = 0x7fffffff;
782 if( p_stream->fmt.i_extra > 0 )
784 i_decoder_specific_info_size =
785 GetDescrLength( p_stream->fmt.i_extra );
789 i_decoder_specific_info_size = 0;
792 esds = box_full_new( "esds", 0, 0 );
795 bo_add_descr( esds, 0x03, 3 +
796 GetDescrLength( 13 + i_decoder_specific_info_size ) +
797 GetDescrLength( 1 ) );
798 bo_add_16be( esds, p_stream->i_track_id );
799 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
801 /* DecoderConfigDescr */
802 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
804 switch( p_stream->fmt.i_codec )
806 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
807 i_object_type_indication = 0x20;
809 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
810 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
811 i_object_type_indication = 0x60;
813 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
814 /* FIXME for mpeg2-aac == 0x66->0x68 */
815 i_object_type_indication = 0x40;
817 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
818 i_object_type_indication =
819 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
822 i_object_type_indication = 0x00;
825 i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
827 bo_add_8 ( esds, i_object_type_indication );
828 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
829 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
830 bo_add_32be( esds, i_bitrate_max ); // maxBitrate
831 bo_add_32be( esds, i_bitrate_avg ); // avgBitrate
833 if( p_stream->fmt.i_extra > 0 )
837 /* DecoderSpecificInfo */
838 bo_add_descr( esds, 0x05, p_stream->fmt.i_extra );
840 for( i = 0; i < p_stream->fmt.i_extra; i++ )
842 bo_add_8( esds, ((uint8_t*)p_stream->fmt.p_extra)[i] );
846 /* SL_Descr mandatory */
847 bo_add_descr( esds, 0x06, 1 );
848 bo_add_8 ( esds, 0x02 ); // sl_predefined
855 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
860 wave = box_new( "wave" );
862 box = box_new( "frma" );
863 bo_add_fourcc( box, "mp4a" );
865 box_gather( wave, box );
867 box = box_new( "mp4a" );
868 bo_add_32be( box, 0 );
870 box_gather( wave, box );
872 box = GetESDS( p_stream );
874 box_gather( wave, box );
876 box = box_new( "srcq" );
877 bo_add_32be( box, 0x40 );
879 box_gather( wave, box );
882 bo_add_32be( wave, 8 ); /* new empty box */
883 bo_add_32be( wave, 0 ); /* box label */
890 static bo_t *GetDamrTag( mp4_stream_t *p_stream )
894 damr = box_new( "damr" );
896 bo_add_fourcc( damr, "REFC" );
899 if( p_stream->fmt.i_codec == VLC_FOURCC( 's', 'a', 'm', 'r' ) )
900 bo_add_16be( damr, 0x81ff ); /* Mode set (all modes for AMR_NB) */
902 bo_add_16be( damr, 0x83ff ); /* Mode set (all modes for AMR_WB) */
903 bo_add_16be( damr, 0x1 ); /* Mode change period (no restriction) */
910 static bo_t *GetD263Tag( mp4_stream_t *p_stream )
914 d263 = box_new( "d263" );
916 bo_add_fourcc( d263, "VLC " );
917 bo_add_16be( d263, 0xa );
925 static bo_t *GetAvcCTag( mp4_stream_t *p_stream )
929 /* FIXME use better value */
930 avcC = box_new( "avcC" );
931 bo_add_8( avcC, 1 ); /* configuration version */
932 bo_add_8( avcC, p_stream->avc.i_profile );
933 bo_add_8( avcC, p_stream->avc.i_profile ); /* profile compatible ??? */
934 bo_add_8( avcC, p_stream->avc.i_level ); /* level, 5.1 */
935 bo_add_8( avcC, 0xff ); /* 0b11111100 | lengthsize = 0x11 */
937 bo_add_8( avcC, 0xe0 | (p_stream->avc.i_sps > 0 ? 1 : 0) ); /* 0b11100000 | sps_count */
938 if( p_stream->avc.i_sps > 0 )
940 bo_add_16be( avcC, p_stream->avc.i_sps );
941 bo_add_mem( avcC, p_stream->avc.i_sps, p_stream->avc.sps );
944 bo_add_8( avcC, (p_stream->avc.i_pps > 0 ? 1 : 0) ); /* pps_count */
945 if( p_stream->avc.i_pps > 0 )
947 bo_add_16be( avcC, p_stream->avc.i_pps );
948 bo_add_mem( avcC, p_stream->avc.i_pps, p_stream->avc.pps );
955 /* TODO: No idea about these values */
956 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
958 bo_t *smi = box_new( "SMI " );
960 if( p_stream->fmt.i_extra > 0x4e )
962 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
963 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
965 while( p + 8 < p_end )
967 int i_size = GetDWBE( p );
970 /* FIXME handle 1 as long size */
973 if( !strncmp( (const char *)&p[4], "SMI ", 4 ) )
975 bo_add_mem( smi, p_end - p - 8, &p[8] );
982 /* Create a dummy one in fallback */
983 bo_add_fourcc( smi, "SEQH" );
984 bo_add_32be( smi, 0x5 );
985 bo_add_32be( smi, 0xe2c0211d );
986 bo_add_8( smi, 0xc0 );
992 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
994 sout_mux_sys_t *p_sys = p_mux->p_sys;
995 bo_t *udta = box_new( "udta" );
996 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
1000 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
1002 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
1004 if( p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','v') ||
1005 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1007 bo_t *box = box_new( "\251req" );
1009 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
1010 bo_add_16be( box, 0 );
1011 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
1012 (uint8_t *)"QuickTime 6.0 or greater" );
1014 box_gather( udta, box );
1021 bo_t *box = box_new( "\251enc" );
1023 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
1024 bo_add_16be( box, 0 );
1025 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
1026 (uint8_t*)PACKAGE_STRING " stream output" );
1028 box_gather( udta, box );
1034 #define ADD_META_BOX( type, box_string ) { \
1036 if( p_meta->psz_##type ) box = box_new( "\251" box_string ); \
1039 bo_add_16be( box, strlen( p_meta->psz_##type ) ); \
1040 bo_add_16be( box, 0 ); \
1041 bo_add_mem( box, strlen( p_meta->psz_##type ), \
1042 (uint8_t*)(p_meta->psz_##type ) ); \
1044 box_gather( udta, box ); \
1047 ADD_META_BOX( title, "nam" );
1048 ADD_META_BOX( author, "aut" );
1049 ADD_META_BOX( artist, "ART" );
1050 ADD_META_BOX( genre, "gen" );
1051 ADD_META_BOX( copyright, "cpy" );
1052 ADD_META_BOX( description, "des" );
1053 ADD_META_BOX( date, "day" );
1054 ADD_META_BOX( url, "url" );
1062 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1064 sout_mux_sys_t *p_sys = p_mux->p_sys;
1065 vlc_bool_t b_descr = VLC_FALSE;
1070 switch( p_stream->fmt.i_codec )
1072 case VLC_FOURCC('m','p','4','a'):
1073 memcpy( fcc, "mp4a", 4 );
1077 case VLC_FOURCC('s','a','m','r'):
1078 case VLC_FOURCC('s','a','w','b'):
1079 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1083 case VLC_FOURCC('m','p','g','a'):
1085 memcpy( fcc, ".mp3", 4 );
1088 memcpy( fcc, "mp4a", 4 );
1094 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1098 soun = box_new( fcc );
1099 for( i = 0; i < 6; i++ )
1101 bo_add_8( soun, 0 ); // reserved;
1103 bo_add_16be( soun, 1 ); // data-reference-index
1105 /* SoundDescription */
1107 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1109 bo_add_16be( soun, 1 ); // version 1;
1113 bo_add_16be( soun, 0 ); // version 0;
1115 bo_add_16be( soun, 0 ); // revision level (0)
1116 bo_add_32be( soun, 0 ); // vendor
1118 bo_add_16be( soun, p_stream->fmt.audio.i_channels );
1120 bo_add_16be( soun, p_stream->fmt.audio.i_bitspersample ?
1121 p_stream->fmt.audio.i_bitspersample : 16 );
1122 bo_add_16be( soun, -2 ); // compression id
1123 bo_add_16be( soun, 0 ); // packet size (0)
1124 bo_add_16be( soun, p_stream->fmt.audio.i_rate ); // sampleratehi
1125 bo_add_16be( soun, 0 ); // sampleratelo
1127 /* Extended data for SoundDescription V1 */
1129 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1131 /* samples per packet */
1132 bo_add_32be( soun, p_stream->fmt.audio.i_frame_length );
1133 bo_add_32be( soun, 1536 ); /* bytes per packet */
1134 bo_add_32be( soun, 2 ); /* bytes per frame */
1135 /* bytes per sample */
1136 bo_add_32be( soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1139 /* Add an ES Descriptor */
1145 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1147 box = GetWaveTag( p_stream );
1149 else if( p_stream->fmt.i_codec == VLC_FOURCC('s','a','m','r') )
1151 box = GetDamrTag( p_stream );
1155 box = GetESDS( p_stream );
1158 box_gather( soun, box );
1166 static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1173 switch( p_stream->fmt.i_codec )
1175 case VLC_FOURCC('m','p','4','v'):
1176 case VLC_FOURCC('m','p','g','v'):
1177 memcpy( fcc, "mp4v", 4 );
1180 case VLC_FOURCC('M','J','P','G'):
1181 memcpy( fcc, "mjpa", 4 );
1184 case VLC_FOURCC('S','V','Q','1'):
1185 memcpy( fcc, "SVQ1", 4 );
1188 case VLC_FOURCC('S','V','Q','3'):
1189 memcpy( fcc, "SVQ3", 4 );
1192 case VLC_FOURCC('H','2','6','3'):
1193 memcpy( fcc, "s263", 4 );
1196 case VLC_FOURCC('h','2','6','4'):
1197 memcpy( fcc, "avc1", 4 );
1201 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1205 vide = box_new( fcc );
1206 for( i = 0; i < 6; i++ )
1208 bo_add_8( vide, 0 ); // reserved;
1210 bo_add_16be( vide, 1 ); // data-reference-index
1212 bo_add_16be( vide, 0 ); // predefined;
1213 bo_add_16be( vide, 0 ); // reserved;
1214 for( i = 0; i < 3; i++ )
1216 bo_add_32be( vide, 0 ); // predefined;
1219 bo_add_16be( vide, p_stream->fmt.video.i_width ); // i_width
1220 bo_add_16be( vide, p_stream->fmt.video.i_height ); // i_height
1222 bo_add_32be( vide, 0x00480000 ); // h 72dpi
1223 bo_add_32be( vide, 0x00480000 ); // v 72dpi
1225 bo_add_32be( vide, 0 ); // data size, always 0
1226 bo_add_16be( vide, 1 ); // frames count per sample
1229 for( i = 0; i < 32; i++ )
1231 bo_add_8( vide, 0 );
1234 bo_add_16be( vide, 0x18 ); // depth
1235 bo_add_16be( vide, 0xffff ); // predefined
1237 /* add an ES Descriptor */
1238 switch( p_stream->fmt.i_codec )
1240 case VLC_FOURCC('m','p','4','v'):
1241 case VLC_FOURCC('m','p','g','v'):
1243 bo_t *esds = GetESDS( p_stream );
1246 box_gather( vide, esds );
1250 case VLC_FOURCC('H','2','6','3'):
1252 bo_t *d263 = GetD263Tag( p_stream );
1255 box_gather( vide, d263 );
1259 case VLC_FOURCC('S','V','Q','3'):
1261 bo_t *esds = GetSVQ3Tag( p_stream );
1264 box_gather( vide, esds );
1268 case VLC_FOURCC('h','2','6','4'):
1269 box_gather( vide, GetAvcCTag( p_stream ) );
1281 static bo_t *GetTextBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1284 bo_t *text = box_new( "text" );
1287 for( i = 0; i < 6; i++ )
1289 bo_add_8( text, 0 ); // reserved;
1291 bo_add_16be( text, 1 ); // data-reference-index
1293 bo_add_32be( text, 0 ); // display flags
1294 bo_add_32be( text, 0 ); // justification
1295 for( i = 0; i < 3; i++ )
1297 bo_add_16be( text, 0 ); // back ground color
1300 bo_add_16be( text, 0 ); // box text
1301 bo_add_16be( text, 0 ); // box text
1302 bo_add_16be( text, 0 ); // box text
1303 bo_add_16be( text, 0 ); // box text
1305 bo_add_64be( text, 0 ); // reserved
1306 for( i = 0; i < 3; i++ )
1308 bo_add_16be( text, 0xff ); // foreground color
1311 bo_add_8 ( text, 9 );
1312 bo_add_mem( text, 9, (uint8_t*)"Helvetica" );
1319 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1321 sout_mux_sys_t *p_sys = p_mux->p_sys;
1322 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
1323 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz, *stss;
1324 uint32_t i_timescale;
1325 int64_t i_dts, i_dts_q;
1327 stbl = box_new( "stbl" );
1329 /* sample description */
1330 stsd = box_full_new( "stsd", 0, 0 );
1331 bo_add_32be( stsd, 1 );
1332 if( p_stream->fmt.i_cat == AUDIO_ES )
1334 bo_t *soun = GetSounBox( p_mux, p_stream );
1335 box_gather( stsd, soun );
1337 else if( p_stream->fmt.i_cat == VIDEO_ES )
1339 bo_t *vide = GetVideBox( p_mux, p_stream );
1340 box_gather( stsd, vide );
1342 else if( p_stream->fmt.i_cat == SPU_ES )
1344 box_gather( stsd, GetTextBox( p_mux, p_stream ) );
1348 /* chunk offset table */
1349 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
1351 /* 64 bits version */
1352 p_stream->b_stco64 = VLC_TRUE;
1353 stco = box_full_new( "co64", 0, 0 );
1357 /* 32 bits version */
1358 p_stream->b_stco64 = VLC_FALSE;
1359 stco = box_full_new( "stco", 0, 0 );
1361 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
1363 /* sample to chunk table */
1364 stsc = box_full_new( "stsc", 0, 0 );
1365 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
1367 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
1368 i < p_stream->i_entry_count; i_chunk++ )
1372 if( p_stream->b_stco64 )
1373 bo_add_64be( stco, p_stream->entry[i].i_pos );
1375 bo_add_32be( stco, p_stream->entry[i].i_pos );
1377 while( i < p_stream->i_entry_count )
1379 if( i + 1 < p_stream->i_entry_count &&
1380 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
1381 != p_stream->entry[i + 1].i_pos )
1390 /* Add entry to the stsc table */
1391 if( i_stsc_last_val != i - i_first )
1393 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
1394 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
1395 bo_add_32be( stsc, 1 ); // sample-descr-index
1396 i_stsc_last_val = i - i_first;
1401 /* Fix stco entry count */
1402 bo_fix_32be( stco, 12, i_chunk );
1403 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
1406 /* Fix stsc entry count */
1407 bo_fix_32be( stsc, 12, i_stsc_entries );
1411 stts = box_full_new( "stts", 0, 0 );
1412 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
1414 if( p_stream->fmt.i_cat == AUDIO_ES )
1415 i_timescale = p_stream->fmt.audio.i_rate;
1419 /* first, create quantified length */
1420 for( i = 0, i_dts = 0, i_dts_q = 0; i < p_stream->i_entry_count; i++ )
1422 int64_t i_dts_deq = i_dts_q * I64C(1000000) / (int64_t)i_timescale;
1423 int64_t i_delta = p_stream->entry[i].i_length + i_dts - i_dts_deq;
1425 i_dts += p_stream->entry[i].i_length;
1427 p_stream->entry[i].i_length =
1428 i_delta * (int64_t)i_timescale / I64C(1000000);
1430 i_dts_q += p_stream->entry[i].i_length;
1432 /* then write encoded table */
1433 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
1436 int64_t i_delta = p_stream->entry[i].i_length;
1438 while( i < p_stream->i_entry_count )
1441 if( i >= p_stream->i_entry_count ||
1442 p_stream->entry[i].i_length != i_delta )
1448 bo_add_32be( stts, i - i_first ); // sample-count
1449 bo_add_32be( stts, i_delta ); // sample-delta
1451 bo_fix_32be( stts, 12, i_index );
1454 /* FIXME add ctts ?? FIXME */
1456 stsz = box_full_new( "stsz", 0, 0 );
1457 bo_add_32be( stsz, 0 ); // sample-size
1458 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
1459 for( i = 0; i < p_stream->i_entry_count; i++ )
1461 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
1465 /* create stss table */
1467 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i++ )
1469 if( p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I )
1473 stss = box_full_new( "stss", 0, 0 );
1474 bo_add_32be( stss, 0 ); /* fixed later */
1476 bo_add_32be( stss, 1 + i );
1482 bo_fix_32be( stss, 12, i_index );
1486 /* Now gather all boxes into stbl */
1487 box_gather( stbl, stsd );
1488 box_gather( stbl, stts );
1491 box_gather( stbl, stss );
1493 box_gather( stbl, stsc );
1494 box_gather( stbl, stsz );
1495 p_stream->i_stco_pos = stbl->i_buffer + 16;
1496 box_gather( stbl, stco );
1504 static int64_t get_timestamp();
1506 static uint32_t mvhd_matrix[9] =
1507 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1509 static bo_t *GetMoovBox( sout_mux_t *p_mux )
1511 sout_mux_sys_t *p_sys = p_mux->p_sys;
1516 uint32_t i_movie_timescale = 90000;
1517 int64_t i_movie_duration = 0;
1519 moov = box_new( "moov" );
1521 /* Create general info */
1522 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1524 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1525 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
1527 msg_Dbg( p_mux, "movie duration %ds",
1528 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
1530 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
1532 /* *** add /moov/mvhd *** */
1533 if( !p_sys->b_64_ext )
1535 mvhd = box_full_new( "mvhd", 0, 0 );
1536 bo_add_32be( mvhd, get_timestamp() ); // creation time
1537 bo_add_32be( mvhd, get_timestamp() ); // modification time
1538 bo_add_32be( mvhd, i_movie_timescale); // timescale
1539 bo_add_32be( mvhd, i_movie_duration ); // duration
1543 mvhd = box_full_new( "mvhd", 1, 0 );
1544 bo_add_64be( mvhd, get_timestamp() ); // creation time
1545 bo_add_64be( mvhd, get_timestamp() ); // modification time
1546 bo_add_32be( mvhd, i_movie_timescale); // timescale
1547 bo_add_64be( mvhd, i_movie_duration ); // duration
1549 bo_add_32be( mvhd, 0x10000 ); // rate
1550 bo_add_16be( mvhd, 0x100 ); // volume
1551 bo_add_16be( mvhd, 0 ); // reserved
1552 for( i = 0; i < 2; i++ )
1554 bo_add_32be( mvhd, 0 ); // reserved
1556 for( i = 0; i < 9; i++ )
1558 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
1560 for( i = 0; i < 6; i++ )
1562 bo_add_32be( mvhd, 0 ); // pre-defined
1565 /* Next available track id */
1566 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
1569 box_gather( moov, mvhd );
1571 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1573 mp4_stream_t *p_stream;
1574 uint32_t i_timescale;
1576 bo_t *trak, *tkhd, *edts, *elst, *mdia, *mdhd, *hdlr;
1577 bo_t *minf, *dinf, *dref, *url, *stbl;
1579 p_stream = p_sys->pp_streams[i_trak];
1581 if( p_stream->fmt.i_cat == AUDIO_ES )
1582 i_timescale = p_stream->fmt.audio.i_rate;
1586 /* *** add /moov/trak *** */
1587 trak = box_new( "trak" );
1589 /* *** add /moov/trak/tkhd *** */
1590 if( !p_sys->b_64_ext )
1593 tkhd = box_full_new( "tkhd", 0, 0x0f );
1595 tkhd = box_full_new( "tkhd", 0, 1 );
1597 bo_add_32be( tkhd, get_timestamp() ); // creation time
1598 bo_add_32be( tkhd, get_timestamp() ); // modification time
1599 bo_add_32be( tkhd, p_stream->i_track_id );
1600 bo_add_32be( tkhd, 0 ); // reserved 0
1601 bo_add_32be( tkhd, p_stream->i_duration *
1602 (int64_t)i_movie_timescale /
1603 (mtime_t)1000000 ); // duration
1608 tkhd = box_full_new( "tkhd", 1, 0x0f );
1610 tkhd = box_full_new( "tkhd", 1, 1 );
1612 bo_add_64be( tkhd, get_timestamp() ); // creation time
1613 bo_add_64be( tkhd, get_timestamp() ); // modification time
1614 bo_add_32be( tkhd, p_stream->i_track_id );
1615 bo_add_32be( tkhd, 0 ); // reserved 0
1616 bo_add_64be( tkhd, p_stream->i_duration *
1617 (int64_t)i_movie_timescale /
1618 (mtime_t)1000000 ); // duration
1621 for( i = 0; i < 2; i++ )
1623 bo_add_32be( tkhd, 0 ); // reserved
1625 bo_add_16be( tkhd, 0 ); // layer
1626 bo_add_16be( tkhd, 0 ); // pre-defined
1628 bo_add_16be( tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0 );
1629 bo_add_16be( tkhd, 0 ); // reserved
1630 for( i = 0; i < 9; i++ )
1632 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
1634 if( p_stream->fmt.i_cat == AUDIO_ES )
1636 bo_add_32be( tkhd, 0 ); // width (presentation)
1637 bo_add_32be( tkhd, 0 ); // height(presentation)
1639 else if( p_stream->fmt.i_cat == VIDEO_ES )
1641 int i_width = p_stream->fmt.video.i_width << 16;
1642 if( p_stream->fmt.video.i_aspect > 0 )
1644 i_width = (int64_t)p_stream->fmt.video.i_aspect *
1645 ((int64_t)p_stream->fmt.video.i_height << 16) /
1648 // width (presentation)
1649 bo_add_32be( tkhd, i_width );
1650 // height(presentation)
1651 bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
1655 int i_width = 320 << 16;
1658 for( i = 0; i < p_sys->i_nb_streams; i++ )
1660 mp4_stream_t *tk = p_sys->pp_streams[i];
1661 if( tk->fmt.i_cat == VIDEO_ES )
1663 if( p_stream->fmt.video.i_aspect )
1664 i_width = (int64_t)p_stream->fmt.video.i_aspect *
1665 ((int64_t)p_stream->fmt.video.i_height<<16) / VOUT_ASPECT_FACTOR;
1667 i_width = p_stream->fmt.video.i_width << 16;
1668 i_height = p_stream->fmt.video.i_height;
1672 bo_add_32be( tkhd, i_width ); // width (presentation)
1673 bo_add_32be( tkhd, i_height << 16 ); // height(presentation)
1677 box_gather( trak, tkhd );
1679 /* *** add /moov/trak/edts and elst */
1680 edts = box_new( "edts" );
1681 elst = box_full_new( "elst", p_sys->b_64_ext ? 1 : 0, 0 );
1682 if( p_stream->i_dts_start > p_sys->i_dts_start )
1684 bo_add_32be( elst, 2 );
1686 if( p_sys->b_64_ext )
1688 bo_add_64be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1689 i_movie_timescale / I64C(1000000) );
1690 bo_add_64be( elst, -1 );
1694 bo_add_32be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1695 i_movie_timescale / I64C(1000000) );
1696 bo_add_32be( elst, -1 );
1698 bo_add_16be( elst, 1 );
1699 bo_add_16be( elst, 0 );
1703 bo_add_32be( elst, 1 );
1705 if( p_sys->b_64_ext )
1707 bo_add_64be( elst, p_stream->i_duration *
1708 i_movie_timescale / I64C(1000000) );
1709 bo_add_64be( elst, 0 );
1713 bo_add_32be( elst, p_stream->i_duration *
1714 i_movie_timescale / I64C(1000000) );
1715 bo_add_32be( elst, 0 );
1717 bo_add_16be( elst, 1 );
1718 bo_add_16be( elst, 0 );
1721 box_gather( edts, elst );
1723 box_gather( trak, edts );
1725 /* *** add /moov/trak/mdia *** */
1726 mdia = box_new( "mdia" );
1729 if( !p_sys->b_64_ext )
1731 mdhd = box_full_new( "mdhd", 0, 0 );
1732 bo_add_32be( mdhd, get_timestamp() ); // creation time
1733 bo_add_32be( mdhd, get_timestamp() ); // modification time
1734 bo_add_32be( mdhd, i_timescale); // timescale
1735 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1736 (mtime_t)1000000 ); // duration
1740 mdhd = box_full_new( "mdhd", 1, 0 );
1741 bo_add_64be( mdhd, get_timestamp() ); // creation time
1742 bo_add_64be( mdhd, get_timestamp() ); // modification time
1743 bo_add_32be( mdhd, i_timescale); // timescale
1744 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1745 (mtime_t)1000000 ); // duration
1748 if( p_stream->fmt.psz_language )
1750 char *psz = p_stream->fmt.psz_language;
1751 const iso639_lang_t *pl = NULL;
1752 uint16_t lang = 0x0;
1754 if( strlen( psz ) == 2 )
1756 pl = GetLang_1( psz );
1758 else if( strlen( psz ) == 3 )
1760 pl = GetLang_2B( psz );
1761 if( !strcmp( pl->psz_iso639_1, "??" ) )
1763 pl = GetLang_2T( psz );
1766 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1768 lang = ( ( pl->psz_iso639_2T[0] - 0x60 ) << 10 ) |
1769 ( ( pl->psz_iso639_2T[1] - 0x60 ) << 5 ) |
1770 ( ( pl->psz_iso639_2T[2] - 0x60 ) );
1772 bo_add_16be( mdhd, lang ); // language
1776 bo_add_16be( mdhd, 0 ); // language
1778 bo_add_16be( mdhd, 0 ); // predefined
1780 box_gather( mdia, mdhd );
1782 /* handler reference */
1783 hdlr = box_full_new( "hdlr", 0, 0 );
1786 bo_add_fourcc( hdlr, "mhlr" ); // media handler
1788 bo_add_32be( hdlr, 0 );
1790 if( p_stream->fmt.i_cat == AUDIO_ES )
1791 bo_add_fourcc( hdlr, "soun" );
1792 else if( p_stream->fmt.i_cat == VIDEO_ES )
1793 bo_add_fourcc( hdlr, "vide" );
1794 else if( p_stream->fmt.i_cat == SPU_ES )
1795 bo_add_fourcc( hdlr, "text" );
1797 bo_add_32be( hdlr, 0 ); // reserved
1798 bo_add_32be( hdlr, 0 ); // reserved
1799 bo_add_32be( hdlr, 0 ); // reserved
1802 bo_add_8( hdlr, 12 ); /* Pascal string for .mov */
1804 if( p_stream->fmt.i_cat == AUDIO_ES )
1805 bo_add_mem( hdlr, 12, (uint8_t*)"SoundHandler" );
1806 else if( p_stream->fmt.i_cat == VIDEO_ES )
1807 bo_add_mem( hdlr, 12, (uint8_t*)"VideoHandler" );
1809 bo_add_mem( hdlr, 12, (uint8_t*)"Text Handler" );
1812 bo_add_8( hdlr, 0 ); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
1815 box_gather( mdia, hdlr );
1818 minf = box_new( "minf" );
1821 if( p_stream->fmt.i_cat == AUDIO_ES )
1825 smhd = box_full_new( "smhd", 0, 0 );
1826 bo_add_16be( smhd, 0 ); // balance
1827 bo_add_16be( smhd, 0 ); // reserved
1830 box_gather( minf, smhd );
1832 else if( p_stream->fmt.i_cat == VIDEO_ES )
1836 vmhd = box_full_new( "vmhd", 0, 1 );
1837 bo_add_16be( vmhd, 0 ); // graphicsmode
1838 for( i = 0; i < 3; i++ )
1840 bo_add_16be( vmhd, 0 ); // opcolor
1844 box_gather( minf, vmhd );
1846 else if( p_stream->fmt.i_cat == SPU_ES )
1848 bo_t *gmhd = box_new( "gmhd" );
1849 bo_t *gmin = box_full_new( "gmin", 0, 1 );
1851 bo_add_16be( gmin, 0 ); // graphicsmode
1852 for( i = 0; i < 3; i++ )
1854 bo_add_16be( gmin, 0 ); // opcolor
1856 bo_add_16be( gmin, 0 ); // balance
1857 bo_add_16be( gmin, 0 ); // reserved
1860 box_gather( gmhd, gmin );
1863 box_gather( minf, gmhd );
1867 dinf = box_new( "dinf" );
1868 dref = box_full_new( "dref", 0, 0 );
1869 bo_add_32be( dref, 1 );
1870 url = box_full_new( "url ", 0, 0x01 );
1872 box_gather( dref, url );
1874 box_gather( dinf, dref );
1876 /* append dinf to mdia */
1878 box_gather( minf, dinf );
1881 stbl = GetStblBox( p_mux, p_stream );
1883 /* append stbl to minf */
1884 p_stream->i_stco_pos += minf->i_buffer;
1885 box_gather( minf, stbl );
1887 /* append minf to mdia */
1889 p_stream->i_stco_pos += mdia->i_buffer;
1890 box_gather( mdia, minf );
1892 /* append mdia to trak */
1894 p_stream->i_stco_pos += trak->i_buffer;
1895 box_gather( trak, mdia );
1897 /* append trak to moov */
1899 p_stream->i_stco_pos += moov->i_buffer;
1900 box_gather( moov, trak );
1903 /* Add user data tags */
1904 box_gather( moov, GetUdtaTag( p_mux ) );
1910 /****************************************************************************/
1912 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1917 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1918 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1922 p_bo->i_buffer_size = i_size;
1923 p_bo->p_buffer = p_buffer;
1926 p_bo->b_grow = b_grow;
1930 static void bo_add_8( bo_t *p_bo, uint8_t i )
1932 if( p_bo->i_buffer < p_bo->i_buffer_size )
1934 p_bo->p_buffer[p_bo->i_buffer] = i;
1936 else if( p_bo->b_grow )
1938 p_bo->i_buffer_size += 1024;
1939 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1941 p_bo->p_buffer[p_bo->i_buffer] = i;
1947 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1949 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1950 bo_add_8( p_bo, i &0xff );
1953 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1955 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1956 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1957 bo_add_8( p_bo, ( i &0xff ) );
1959 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1961 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1962 bo_add_16be( p_bo, i &0xffff );
1965 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1967 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1968 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1969 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1970 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1973 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1975 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1976 bo_add_32be( p_bo, i &0xffffffff );
1979 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1981 bo_add_8( p_bo, fcc[0] );
1982 bo_add_8( p_bo, fcc[1] );
1983 bo_add_8( p_bo, fcc[2] );
1984 bo_add_8( p_bo, fcc[3] );
1987 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1991 for( i = 0; i < i_size; i++ )
1993 bo_add_8( p_bo, p_mem[i] );
1997 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
2003 vals[3] = (unsigned char)(i_length & 0x7f);
2005 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
2007 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
2009 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
2011 bo_add_8( p_bo, tag );
2013 if( i_size < 0x00000080 )
2015 bo_add_8( p_bo, vals[3] );
2017 else if( i_size < 0x00004000 )
2019 bo_add_8( p_bo, vals[2] );
2020 bo_add_8( p_bo, vals[3] );
2022 else if( i_size < 0x00200000 )
2024 bo_add_8( p_bo, vals[1] );
2025 bo_add_8( p_bo, vals[2] );
2026 bo_add_8( p_bo, vals[3] );
2028 else if( i_size < 0x10000000 )
2030 bo_add_8( p_bo, vals[0] );
2031 bo_add_8( p_bo, vals[1] );
2032 bo_add_8( p_bo, vals[2] );
2033 bo_add_8( p_bo, vals[3] );
2037 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
2041 for( i = 0; i < p_bo2->i_buffer; i++ )
2043 bo_add_8( p_bo, p_bo2->p_buffer[i] );
2047 static bo_t * box_new( char *fcc )
2051 if( ( box = malloc( sizeof( bo_t ) ) ) )
2053 bo_init( box, 0, NULL, VLC_TRUE );
2055 bo_add_32be ( box, 0 );
2056 bo_add_fourcc( box, fcc );
2062 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
2066 if( ( box = malloc( sizeof( bo_t ) ) ) )
2068 bo_init( box, 0, NULL, VLC_TRUE );
2070 bo_add_32be ( box, 0 );
2071 bo_add_fourcc( box, fcc );
2072 bo_add_8 ( box, v );
2073 bo_add_24be ( box, f );
2079 static void box_fix( bo_t *box )
2083 memcpy( &box_tmp, box, sizeof( bo_t ) );
2085 box_tmp.i_buffer = 0;
2086 bo_add_32be( &box_tmp, box->i_buffer );
2089 static void box_free( bo_t *box )
2093 free( box->p_buffer );
2099 static void box_gather ( bo_t *box, bo_t *box2 )
2101 bo_add_bo( box, box2 );
2105 static block_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
2109 p_buf = block_New( p_sout, box->i_buffer );
2110 if( box->i_buffer > 0 )
2112 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
2118 static void box_send( sout_mux_t *p_mux, bo_t *box )
2122 p_buf = bo_to_sout( p_mux->p_sout, box );
2125 sout_AccessOutWrite( p_mux->p_access, p_buf );
2128 static int64_t get_timestamp()
2130 int64_t i_timestamp = 0;
2133 i_timestamp = time(NULL);
2134 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2135 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60