1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 #define FASTSTART_TEXT N_("Create \"Fast start\" files")
46 #define FASTSTART_LONGTEXT N_( \
47 "When this option is turned on, \"Fast start\" files will be created. " \
48 "(\"Fast start\" files are optimized for download, allowing the user " \
49 "to start previewing the file while it is downloading).")
50 static int Open ( vlc_object_t * );
51 static void Close ( vlc_object_t * );
53 #define SOUT_CFG_PREFIX "sout-mp4-"
56 set_description( _("MP4/MOV muxer") );
58 add_bool( SOUT_CFG_PREFIX "faststart", 1, NULL, FASTSTART_TEXT, FASTSTART_LONGTEXT,
60 set_capability( "sout mux", 5 );
61 add_shortcut( "mp4" );
62 add_shortcut( "mov" );
63 add_shortcut( "3gp" );
64 set_callbacks( Open, Close );
67 /*****************************************************************************
69 *****************************************************************************/
70 static const char *ppsz_sout_options[] = {
74 static int Control( sout_mux_t *, int, va_list );
75 static int AddStream( sout_mux_t *, sout_input_t * );
76 static int DelStream( sout_mux_t *, sout_input_t * );
77 static int Mux ( sout_mux_t * );
79 /*****************************************************************************
81 *****************************************************************************/
99 unsigned int i_entry_count;
100 unsigned int i_entry_max;
102 int64_t i_length_neg;
108 /* for later stco fix-up (fast start files) */
129 struct sout_mux_sys_t
134 vlc_bool_t b_fast_start;
142 mp4_stream_t **pp_streams;
155 static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
156 static void bo_add_8 ( bo_t *, uint8_t );
157 static void bo_add_16be ( bo_t *, uint16_t );
158 static void bo_add_24be ( bo_t *, uint32_t );
159 static void bo_add_32be ( bo_t *, uint32_t );
160 static void bo_add_64be ( bo_t *, uint64_t );
161 static void bo_add_fourcc(bo_t *, char * );
162 static void bo_add_bo ( bo_t *, bo_t * );
163 static void bo_add_mem ( bo_t *, int , uint8_t * );
164 static void bo_add_descr( bo_t *, uint8_t , uint32_t );
166 static void bo_fix_32be ( bo_t *, int , uint32_t );
168 static bo_t *box_new ( char *fcc );
169 static bo_t *box_full_new( char *fcc, uint8_t v, uint32_t f );
170 static void box_fix ( bo_t *box );
171 static void box_free ( bo_t *box );
172 static void box_gather ( bo_t *box, bo_t *box2 );
174 static void box_send( sout_mux_t *p_mux, bo_t *box );
176 static block_t *bo_to_sout( sout_instance_t *p_sout, bo_t *box );
178 static bo_t *GetMoovBox( sout_mux_t *p_mux );
180 static block_t *ConvertSUBT( sout_mux_t *, mp4_stream_t *, block_t *);
181 static void ConvertAVC1( sout_mux_t *, mp4_stream_t *, block_t * );
183 /*****************************************************************************
185 *****************************************************************************/
186 static int Open( vlc_object_t *p_this )
188 sout_mux_t *p_mux = (sout_mux_t*)p_this;
189 sout_mux_sys_t *p_sys;
192 msg_Dbg( p_mux, "Mp4 muxer opend" );
193 sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
195 p_mux->pf_control = Control;
196 p_mux->pf_addstream = AddStream;
197 p_mux->pf_delstream = DelStream;
199 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
201 p_sys->i_nb_streams = 0;
202 p_sys->pp_streams = NULL;
203 p_sys->i_mdat_pos = 0;
204 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
205 p_sys->b_3gp = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "3gp" );
206 p_sys->i_dts_start = 0;
211 /* Now add ftyp header */
212 box = box_new( "ftyp" );
213 if( p_sys->b_3gp ) bo_add_fourcc( box, "3gp4" );
214 else bo_add_fourcc( box, "isom" );
215 bo_add_32be ( box, 0 );
216 if( p_sys->b_3gp ) bo_add_fourcc( box, "3gp4" );
217 else bo_add_fourcc( box, "mp41" );
220 p_sys->i_pos += box->i_buffer;
221 p_sys->i_mdat_pos = p_sys->i_pos;
223 box_send( p_mux, box );
227 * Quicktime actually doesn't like the 64 bits extensions !!! */
228 p_sys->b_64_ext = VLC_FALSE;
230 /* Now add mdat header */
231 box = box_new( "mdat" );
232 bo_add_64be ( box, 0 ); // enough to store an extended size
234 p_sys->i_pos += box->i_buffer;
236 box_send( p_mux, box );
241 /*****************************************************************************
243 *****************************************************************************/
244 static void Close( vlc_object_t * p_this )
246 sout_mux_t *p_mux = (sout_mux_t*)p_this;
247 sout_mux_sys_t *p_sys = p_mux->p_sys;
255 msg_Dbg( p_mux, "Close" );
257 /* Update mdat size */
258 bo_init( &bo, 0, NULL, VLC_TRUE );
259 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
262 bo_add_32be ( &bo, 1 );
263 bo_add_fourcc( &bo, "mdat" );
264 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
268 bo_add_32be ( &bo, 8 );
269 bo_add_fourcc( &bo, "wide" );
270 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
271 bo_add_fourcc( &bo, "mdat" );
273 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
276 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
277 sout_AccessOutWrite( p_mux->p_access, p_hdr );
279 /* Create MOOV header */
280 i_moov_pos = p_sys->i_pos;
281 moov = GetMoovBox( p_mux );
283 /* Check we need to create "fast start" files */
284 var_Get( p_this, SOUT_CFG_PREFIX "faststart", &val );
285 p_sys->b_fast_start = val.b_bool;
286 while( p_sys->b_fast_start )
288 /* Move data to the end of the file so we can fit the moov header
291 int64_t i_chunk, i_size = p_sys->i_pos - p_sys->i_mdat_pos;
292 int i_moov_size = moov->i_buffer;
296 i_chunk = __MIN( 32768, i_size );
297 p_buf = block_New( p_mux, i_chunk );
298 sout_AccessOutSeek( p_mux->p_access,
299 p_sys->i_mdat_pos + i_size - i_chunk );
300 if( sout_AccessOutRead( p_mux->p_access, p_buf ) < i_chunk )
302 msg_Warn( p_this, "read() not supported by acces output, "
303 "won't create a fast start file" );
304 p_sys->b_fast_start = VLC_FALSE;
305 block_Release( p_buf );
308 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos + i_size +
309 i_moov_size - i_chunk );
310 sout_AccessOutWrite( p_mux->p_access, p_buf );
314 if( !p_sys->b_fast_start ) break;
316 /* Fix-up samples to chunks table in MOOV header */
317 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
319 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
323 moov->i_buffer = p_stream->i_stco_pos;
324 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
326 if( p_stream->b_stco64 )
327 bo_add_64be( moov, p_stream->entry[i].i_pos + i_moov_size);
329 bo_add_32be( moov, p_stream->entry[i].i_pos + i_moov_size);
331 while( i < p_stream->i_entry_count )
333 if( i + 1 < p_stream->i_entry_count &&
334 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
335 != p_stream->entry[i + 1].i_pos )
346 moov->i_buffer = i_moov_size;
347 i_moov_pos = p_sys->i_mdat_pos;
348 p_sys->b_fast_start = VLC_FALSE;
351 /* Write MOOV header */
352 sout_AccessOutSeek( p_mux->p_access, i_moov_pos );
353 box_send( p_mux, moov );
356 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
358 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
360 es_format_Clean( &p_stream->fmt );
361 if( p_stream->avc.i_sps ) free( p_stream->avc.sps );
362 if( p_stream->avc.i_pps ) free( p_stream->avc.pps );
363 free( p_stream->entry );
366 if( p_sys->i_nb_streams ) free( p_sys->pp_streams );
370 /*****************************************************************************
372 *****************************************************************************/
373 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
379 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
380 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
381 *pb_bool = VLC_FALSE;
384 case MUX_GET_ADD_STREAM_WAIT:
385 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
389 case MUX_GET_MIME: /* Not needed, as not streamable */
395 /*****************************************************************************
397 *****************************************************************************/
398 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
400 sout_mux_sys_t *p_sys = p_mux->p_sys;
401 mp4_stream_t *p_stream;
403 switch( p_input->p_fmt->i_codec )
405 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
406 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
407 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
408 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
409 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
410 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
411 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
412 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
413 case VLC_FOURCC( 'h', '2', '6', '4' ):
415 case VLC_FOURCC( 's', 'u', 'b', 't' ):
416 msg_Warn( p_mux, "subtitle track added like in .mov (even when creating .mp4)" );
419 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
420 (char*)&p_input->p_fmt->i_codec );
424 p_stream = malloc( sizeof( mp4_stream_t ) );
425 es_format_Copy( &p_stream->fmt, p_input->p_fmt );
426 p_stream->i_track_id = p_sys->i_nb_streams + 1;
427 p_stream->i_length_neg = 0;
428 p_stream->i_entry_count = 0;
429 p_stream->i_entry_max = 1000;
431 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
432 p_stream->i_dts_start = 0;
433 p_stream->i_duration = 0;
434 p_stream->avc.i_profile = 77;
435 p_stream->avc.i_level = 51;
436 p_stream->avc.i_sps = 0;
437 p_stream->avc.sps = NULL;
438 p_stream->avc.i_pps = 0;
439 p_stream->avc.pps = NULL;
441 p_input->p_sys = p_stream;
443 msg_Dbg( p_mux, "adding input" );
445 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
449 /*****************************************************************************
451 *****************************************************************************/
452 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
454 msg_Dbg( p_mux, "removing input" );
458 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
464 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
466 block_fifo_t *p_fifo = p_mux->pp_inputs[i]->p_fifo;
469 if( p_fifo->i_depth <= 1 )
471 if( p_mux->pp_inputs[i]->p_fmt->i_cat != SPU_ES )
473 return -1; // wait that all fifo have at least 2 packets
475 /* For SPU, we wait only 1 packet */
479 p_buf = block_FifoShow( p_fifo );
480 if( i_stream < 0 || p_buf->i_dts < i_dts )
482 i_dts = p_buf->i_dts;
488 *pi_stream = i_stream;
497 /*****************************************************************************
499 *****************************************************************************/
500 static int Mux( sout_mux_t *p_mux )
502 sout_mux_sys_t *p_sys = p_mux->p_sys;
506 sout_input_t *p_input;
508 mp4_stream_t *p_stream;
512 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
514 return( VLC_SUCCESS );
517 p_input = p_mux->pp_inputs[i_stream];
518 p_stream = (mp4_stream_t*)p_input->p_sys;
520 p_data = block_FifoGet( p_input->p_fifo );
521 if( p_stream->fmt.i_codec == VLC_FOURCC( 'h', '2', '6', '4' ) )
523 ConvertAVC1( p_mux, p_stream, p_data );
525 else if( p_stream->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
527 p_data = ConvertSUBT( p_mux, p_stream, p_data );
530 if( p_stream->fmt.i_cat != SPU_ES )
532 /* Fix length of the sample */
533 if( p_input->p_fifo->i_depth > 0 )
535 block_t *p_next = block_FifoShow( p_input->p_fifo );
536 int64_t i_diff = p_next->i_dts - p_data->i_dts;
538 if( i_diff < I64C(1000000 ) ) /* protection */
540 p_data->i_length = i_diff;
543 if( p_data->i_length <= 0 )
545 msg_Warn( p_mux, "i_length <= 0" );
546 p_stream->i_length_neg += p_data->i_length - 1;
547 p_data->i_length = 1;
549 else if( p_stream->i_length_neg < 0 )
551 int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
553 p_data->i_length -= i_recover;
554 p_stream->i_length_neg += i_recover;
558 /* Save starting time */
559 if( p_stream->i_entry_count == 0 )
561 p_stream->i_dts_start = p_data->i_dts;
563 /* Update global dts_start */
564 if( p_sys->i_dts_start <= 0 ||
565 p_stream->i_dts_start < p_sys->i_dts_start )
567 p_sys->i_dts_start = p_stream->i_dts_start;
571 if( p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0 )
573 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
577 /* FIXME handle this broken case */
582 if( p_stream->entry[p_stream->i_entry_count-1].i_length <= 0 )
584 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
589 /* add index entry */
590 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
591 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_buffer;
592 p_stream->entry[p_stream->i_entry_count].i_pts_dts=
593 __MAX( p_data->i_pts - p_data->i_dts, 0 );
594 p_stream->entry[p_stream->i_entry_count].i_length = p_data->i_length;
595 p_stream->entry[p_stream->i_entry_count].i_flags = p_data->i_flags;
597 p_stream->i_entry_count++;
598 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
599 if( p_stream->i_entry_count >= p_stream->i_entry_max - 1 )
601 p_stream->i_entry_max += 1000;
603 realloc( p_stream->entry,
604 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
608 p_stream->i_duration += p_data->i_length;
609 p_sys->i_pos += p_data->i_buffer;
612 p_stream->i_last_dts = p_data->i_dts;
615 sout_AccessOutWrite( p_mux->p_access, p_data );
617 if( p_stream->fmt.i_cat == SPU_ES )
619 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
624 msg_Dbg( p_mux, "writing a empty subs" ) ;
626 /* Append a idx entry */
627 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
628 p_stream->entry[p_stream->i_entry_count].i_size = 3;
629 p_stream->entry[p_stream->i_entry_count].i_pts_dts= 0;
630 p_stream->entry[p_stream->i_entry_count].i_length = 0;
631 p_stream->entry[p_stream->i_entry_count].i_flags = 0;
633 /* XXX: No need to grow the entry here */
634 p_stream->i_entry_count++;
637 p_stream->i_last_dts += i_length;
640 p_data = block_New( p_mux, 3 );
641 p_data->p_buffer[0] = 0;
642 p_data->p_buffer[1] = 1;
643 p_data->p_buffer[2] = ' ';
645 p_sys->i_pos += p_data->i_buffer;
647 sout_AccessOutWrite( p_mux->p_access, p_data );
651 p_stream->i_duration = p_stream->i_last_dts - p_stream->i_dts_start;
655 return( VLC_SUCCESS );
658 /*****************************************************************************
660 *****************************************************************************/
661 static block_t *ConvertSUBT( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
663 p_block = block_Realloc( p_block, 2, p_block->i_buffer );
665 /* No trailling '\0' */
666 if( p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0' )
669 p_block->p_buffer[0] = ( (p_block->i_buffer - 2) >> 8 )&0xff;
670 p_block->p_buffer[1] = ( (p_block->i_buffer - 2) )&0xff;
675 static void ConvertAVC1( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
677 uint8_t *last = p_block->p_buffer; /* Assume it starts with 0x00000001 */
678 uint8_t *dat = &p_block->p_buffer[4];
679 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
682 /* Replace the 4 bytes start code with 4 bytes size,
683 * FIXME are all startcode 4 bytes ? (I don't think :( */
688 while( dat < end - 4 )
690 if( dat[0] == 0x00 && dat[1] == 0x00 &&
691 dat[2] == 0x00 && dat[3] == 0x01 )
703 i_size = dat - &last[4];
704 last[0] = ( i_size >> 24 )&0xff;
705 last[1] = ( i_size >> 16 )&0xff;
706 last[2] = ( i_size >> 8 )&0xff;
707 last[3] = ( i_size )&0xff;
709 if( (last[4]&0x1f) == 7 && tk->avc.i_sps <= 0 ) /* SPS */
711 tk->avc.i_sps = i_size;
712 tk->avc.sps = malloc( i_size );
713 memcpy( tk->avc.sps, &last[4], i_size );
715 tk->avc.i_profile = tk->avc.sps[1];
716 tk->avc.i_level = tk->avc.sps[3];
718 else if( (last[4]&0x1f) == 8 && tk->avc.i_pps <= 0 ) /* PPS */
720 tk->avc.i_pps = i_size;
721 tk->avc.pps = malloc( i_size );
722 memcpy( tk->avc.pps, &last[4], i_size );
732 static int GetDescrLength( int i_size )
734 if( i_size < 0x00000080 )
736 else if( i_size < 0x00004000 )
738 else if( i_size < 0x00200000 )
744 static bo_t *GetESDS( mp4_stream_t *p_stream )
748 int i_object_type_indication;
749 int i_decoder_specific_info_size;
751 int64_t i_bitrate_avg = 0;
752 int64_t i_bitrate_max = 0;
754 /* Compute avg/max bitrate */
755 for( i = 0; i < p_stream->i_entry_count; i++ )
757 i_bitrate_avg += p_stream->entry[i].i_size;
758 if( p_stream->entry[i].i_length > 0)
760 int64_t i_bitrate = I64C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
761 if( i_bitrate > i_bitrate_max )
762 i_bitrate_max = i_bitrate;
766 if( p_stream->i_duration > 0 )
767 i_bitrate_avg = I64C(8000000) * i_bitrate_avg / p_stream->i_duration;
770 if( i_bitrate_max <= 1 )
771 i_bitrate_max = 0x7fffffff;
774 if( p_stream->fmt.i_extra > 0 )
776 i_decoder_specific_info_size =
777 GetDescrLength( p_stream->fmt.i_extra );
781 i_decoder_specific_info_size = 0;
784 esds = box_full_new( "esds", 0, 0 );
787 bo_add_descr( esds, 0x03, 3 +
788 GetDescrLength( 13 + i_decoder_specific_info_size ) +
789 GetDescrLength( 1 ) );
790 bo_add_16be( esds, p_stream->i_track_id );
791 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
793 /* DecoderConfigDescr */
794 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
796 switch( p_stream->fmt.i_codec )
798 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
799 i_object_type_indication = 0x20;
801 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
802 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
803 i_object_type_indication = 0x60;
805 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
806 /* FIXME for mpeg2-aac == 0x66->0x68 */
807 i_object_type_indication = 0x40;
809 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
810 i_object_type_indication =
811 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
814 i_object_type_indication = 0x00;
817 i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
819 bo_add_8 ( esds, i_object_type_indication );
820 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
821 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
822 bo_add_32be( esds, i_bitrate_max ); // maxBitrate
823 bo_add_32be( esds, i_bitrate_avg ); // avgBitrate
825 if( p_stream->fmt.i_extra > 0 )
829 /* DecoderSpecificInfo */
830 bo_add_descr( esds, 0x05, p_stream->fmt.i_extra );
832 for( i = 0; i < p_stream->fmt.i_extra; i++ )
834 bo_add_8( esds, ((uint8_t*)p_stream->fmt.p_extra)[i] );
838 /* SL_Descr mandatory */
839 bo_add_descr( esds, 0x06, 1 );
840 bo_add_8 ( esds, 0x02 ); // sl_predefined
847 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
852 wave = box_new( "wave" );
854 box = box_new( "frma" );
855 bo_add_fourcc( box, "mp4a" );
857 box_gather( wave, box );
859 box = box_new( "mp4a" );
860 bo_add_32be( box, 0 );
862 box_gather( wave, box );
864 box = GetESDS( p_stream );
866 box_gather( wave, box );
868 box = box_new( "srcq" );
869 bo_add_32be( box, 0x40 );
871 box_gather( wave, box );
874 bo_add_32be( wave, 8 ); /* new empty box */
875 bo_add_32be( wave, 0 ); /* box label */
882 static bo_t *GetAvcCTag( mp4_stream_t *p_stream )
886 /* FIXME use better value */
887 avcC = box_new( "avcC" );
888 bo_add_8( avcC, 1 ); /* configuration version */
889 bo_add_8( avcC, p_stream->avc.i_profile );
890 bo_add_8( avcC, p_stream->avc.i_profile ); /* profile compatible ??? */
891 bo_add_8( avcC, p_stream->avc.i_level ); /* level, 5.1 */
892 bo_add_8( avcC, 0xff ); /* 0b11111100 | lengthsize = 0x11 */
894 bo_add_8( avcC, 0xe0 | (p_stream->avc.i_sps > 0 ? 1 : 0) ); /* 0b11100000 | sps_count */
895 if( p_stream->avc.i_sps > 0 )
897 bo_add_16be( avcC, p_stream->avc.i_sps );
898 bo_add_mem( avcC, p_stream->avc.i_sps, p_stream->avc.sps );
901 bo_add_8( avcC, (p_stream->avc.i_pps > 0 ? 1 : 0) ); /* pps_count */
902 if( p_stream->avc.i_pps > 0 )
904 bo_add_16be( avcC, p_stream->avc.i_pps );
905 bo_add_mem( avcC, p_stream->avc.i_pps, p_stream->avc.pps );
912 /* TODO: No idea about these values */
913 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
915 bo_t *smi = box_new( "SMI " );
917 if( p_stream->fmt.i_extra > 0x4e )
919 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
920 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
922 while( p + 8 < p_end )
924 int i_size = GetDWBE( p );
927 /* FIXME handle 1 as long size */
930 if( !strncmp( &p[4], "SMI ", 4 ) )
932 bo_add_mem( smi, p_end - p - 8, &p[8] );
939 /* Create a dummy one in fallback */
940 bo_add_fourcc( smi, "SEQH" );
941 bo_add_32be( smi, 0x5 );
942 bo_add_32be( smi, 0xe2c0211d );
943 bo_add_8( smi, 0xc0 );
949 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
951 sout_mux_sys_t *p_sys = p_mux->p_sys;
952 bo_t *udta = box_new( "udta" );
953 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
957 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
959 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
961 if( p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','v') ||
962 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
964 bo_t *box = box_new( "\251req" );
966 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
967 bo_add_16be( box, 0 );
968 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
969 "QuickTime 6.0 or greater" );
971 box_gather( udta, box );
978 bo_t *box = box_new( "\251enc" );
980 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
981 bo_add_16be( box, 0 );
982 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
983 PACKAGE_STRING " stream output" );
985 box_gather( udta, box );
992 for( i = 0; i < p_meta->i_meta; i++ )
996 if( !strcmp( p_meta->name[i], VLC_META_TITLE ) )
997 box = box_new( "\251nam" );
998 else if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
999 box = box_new( "\251aut" );
1000 else if( !strcmp( p_meta->name[i], VLC_META_ARTIST ) )
1001 box = box_new( "\251ART" );
1002 else if( !strcmp( p_meta->name[i], VLC_META_GENRE ) )
1003 box = box_new( "\251gen" );
1004 else if( !strcmp( p_meta->name[i], VLC_META_COPYRIGHT ) )
1005 box = box_new( "\251cpy" );
1006 else if( !strcmp( p_meta->name[i], VLC_META_DESCRIPTION ) )
1007 box = box_new( "\251des" );
1008 else if( !strcmp( p_meta->name[i], VLC_META_DATE ) )
1009 box = box_new( "\251day" );
1010 else if( !strcmp( p_meta->name[i], VLC_META_URL ) )
1011 box = box_new( "\251url" );
1015 bo_add_16be( box, strlen( p_meta->value[i] ) );
1016 bo_add_16be( box, 0 );
1017 bo_add_mem( box, strlen( p_meta->value[i] ),
1020 box_gather( udta, box );
1029 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1031 sout_mux_sys_t *p_sys = p_mux->p_sys;
1032 vlc_bool_t b_descr = VLC_FALSE;
1037 switch( p_stream->fmt.i_codec )
1039 case VLC_FOURCC('m','p','4','a'):
1040 memcpy( fcc, "mp4a", 4 );
1044 case VLC_FOURCC('m','p','g','a'):
1046 memcpy( fcc, ".mp3", 4 );
1049 memcpy( fcc, "mp4a", 4 );
1055 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1059 soun = box_new( fcc );
1060 for( i = 0; i < 6; i++ )
1062 bo_add_8( soun, 0 ); // reserved;
1064 bo_add_16be( soun, 1 ); // data-reference-index
1066 /* SoundDescription */
1068 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1070 bo_add_16be( soun, 1 ); // version 1;
1074 bo_add_16be( soun, 0 ); // version 0;
1076 bo_add_16be( soun, 0 ); // revision level (0)
1077 bo_add_32be( soun, 0 ); // vendor
1079 bo_add_16be( soun, p_stream->fmt.audio.i_channels );
1081 bo_add_16be( soun, p_stream->fmt.audio.i_bitspersample ?
1082 p_stream->fmt.audio.i_bitspersample : 16 );
1083 bo_add_16be( soun, -2 ); // compression id
1084 bo_add_16be( soun, 0 ); // packet size (0)
1085 bo_add_16be( soun, p_stream->fmt.audio.i_rate ); // sampleratehi
1086 bo_add_16be( soun, 0 ); // sampleratelo
1088 /* Extended data for SoundDescription V1 */
1090 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1092 /* samples per packet */
1093 bo_add_32be( soun, p_stream->fmt.audio.i_frame_length );
1094 bo_add_32be( soun, 1536 ); /* bytes per packet */
1095 bo_add_32be( soun, 2 ); /* bytes per frame */
1096 /* bytes per sample */
1097 bo_add_32be( soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1100 /* Add an ES Descriptor */
1106 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1108 box = GetWaveTag( p_stream );
1112 box = GetESDS( p_stream );
1115 box_gather( soun, box );
1123 static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1130 switch( p_stream->fmt.i_codec )
1132 case VLC_FOURCC('m','p','4','v'):
1133 case VLC_FOURCC('m','p','g','v'):
1134 memcpy( fcc, "mp4v", 4 );
1137 case VLC_FOURCC('M','J','P','G'):
1138 memcpy( fcc, "mjpa", 4 );
1141 case VLC_FOURCC('S','V','Q','1'):
1142 memcpy( fcc, "SVQ1", 4 );
1145 case VLC_FOURCC('S','V','Q','3'):
1146 memcpy( fcc, "SVQ3", 4 );
1149 case VLC_FOURCC('h','2','6','4'):
1150 memcpy( fcc, "avc1", 4 );
1154 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1158 vide = box_new( fcc );
1159 for( i = 0; i < 6; i++ )
1161 bo_add_8( vide, 0 ); // reserved;
1163 bo_add_16be( vide, 1 ); // data-reference-index
1165 bo_add_16be( vide, 0 ); // predefined;
1166 bo_add_16be( vide, 0 ); // reserved;
1167 for( i = 0; i < 3; i++ )
1169 bo_add_32be( vide, 0 ); // predefined;
1172 bo_add_16be( vide, p_stream->fmt.video.i_width ); // i_width
1173 bo_add_16be( vide, p_stream->fmt.video.i_height ); // i_height
1175 bo_add_32be( vide, 0x00480000 ); // h 72dpi
1176 bo_add_32be( vide, 0x00480000 ); // v 72dpi
1178 bo_add_32be( vide, 0 ); // data size, always 0
1179 bo_add_16be( vide, 1 ); // frames count per sample
1182 for( i = 0; i < 32; i++ )
1184 bo_add_8( vide, 0 );
1187 bo_add_16be( vide, 0x18 ); // depth
1188 bo_add_16be( vide, 0xffff ); // predefined
1190 /* add an ES Descriptor */
1191 switch( p_stream->fmt.i_codec )
1193 case VLC_FOURCC('m','p','4','v'):
1194 case VLC_FOURCC('m','p','g','v'):
1196 bo_t *esds = GetESDS( p_stream );
1199 box_gather( vide, esds );
1203 case VLC_FOURCC('S','V','Q','3'):
1205 bo_t *esds = GetSVQ3Tag( p_stream );
1208 box_gather( vide, esds );
1212 case VLC_FOURCC('h','2','6','4'):
1213 box_gather( vide, GetAvcCTag( p_stream ) );
1225 static bo_t *GetTextBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1228 bo_t *text = box_new( "text" );
1231 for( i = 0; i < 6; i++ )
1233 bo_add_8( text, 0 ); // reserved;
1235 bo_add_16be( text, 1 ); // data-reference-index
1237 bo_add_32be( text, 0 ); // display flags
1238 bo_add_32be( text, 0 ); // justification
1239 for( i = 0; i < 3; i++ )
1241 bo_add_16be( text, 0 ); // back ground color
1244 bo_add_16be( text, 0 ); // box text
1245 bo_add_16be( text, 0 ); // box text
1246 bo_add_16be( text, 0 ); // box text
1247 bo_add_16be( text, 0 ); // box text
1249 bo_add_64be( text, 0 ); // reserved
1250 for( i = 0; i < 3; i++ )
1252 bo_add_16be( text, 0xff ); // foreground color
1255 bo_add_8 ( text, 9 );
1256 bo_add_mem( text, 9, "Helvetica" );
1263 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1265 sout_mux_sys_t *p_sys = p_mux->p_sys;
1266 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
1267 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz, *stss;
1268 uint32_t i_timescale;
1269 int64_t i_dts, i_dts_q;
1271 stbl = box_new( "stbl" );
1273 /* sample description */
1274 stsd = box_full_new( "stsd", 0, 0 );
1275 bo_add_32be( stsd, 1 );
1276 if( p_stream->fmt.i_cat == AUDIO_ES )
1278 bo_t *soun = GetSounBox( p_mux, p_stream );
1279 box_gather( stsd, soun );
1281 else if( p_stream->fmt.i_cat == VIDEO_ES )
1283 bo_t *vide = GetVideBox( p_mux, p_stream );
1284 box_gather( stsd, vide );
1286 else if( p_stream->fmt.i_cat == SPU_ES )
1288 box_gather( stsd, GetTextBox( p_mux, p_stream ) );
1292 /* chunk offset table */
1293 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
1295 /* 64 bits version */
1296 p_stream->b_stco64 = VLC_TRUE;
1297 stco = box_full_new( "co64", 0, 0 );
1301 /* 32 bits version */
1302 p_stream->b_stco64 = VLC_FALSE;
1303 stco = box_full_new( "stco", 0, 0 );
1305 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
1307 /* sample to chunk table */
1308 stsc = box_full_new( "stsc", 0, 0 );
1309 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
1311 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
1312 i < p_stream->i_entry_count; i_chunk++ )
1316 if( p_stream->b_stco64 )
1317 bo_add_64be( stco, p_stream->entry[i].i_pos );
1319 bo_add_32be( stco, p_stream->entry[i].i_pos );
1321 while( i < p_stream->i_entry_count )
1323 if( i + 1 < p_stream->i_entry_count &&
1324 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
1325 != p_stream->entry[i + 1].i_pos )
1334 /* Add entry to the stsc table */
1335 if( i_stsc_last_val != i - i_first )
1337 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
1338 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
1339 bo_add_32be( stsc, 1 ); // sample-descr-index
1340 i_stsc_last_val = i - i_first;
1345 /* Fix stco entry count */
1346 bo_fix_32be( stco, 12, i_chunk );
1347 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
1350 /* Fix stsc entry count */
1351 bo_fix_32be( stsc, 12, i_stsc_entries );
1355 stts = box_full_new( "stts", 0, 0 );
1356 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
1358 if( p_stream->fmt.i_cat == AUDIO_ES )
1359 i_timescale = p_stream->fmt.audio.i_rate;
1363 /* first, create quantified length */
1364 for( i = 0, i_dts = 0, i_dts_q = 0; i < p_stream->i_entry_count; i++ )
1366 int64_t i_dts_deq = i_dts_q * I64C(1000000) / (int64_t)i_timescale;
1367 int64_t i_delta = p_stream->entry[i].i_length + i_dts - i_dts_deq;
1369 i_dts += p_stream->entry[i].i_length;
1371 p_stream->entry[i].i_length =
1372 i_delta * (int64_t)i_timescale / I64C(1000000);
1374 i_dts_q += p_stream->entry[i].i_length;
1376 /* then write encoded table */
1377 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
1380 int64_t i_delta = p_stream->entry[i].i_length;
1382 while( i < p_stream->i_entry_count )
1385 if( i >= p_stream->i_entry_count ||
1386 p_stream->entry[i].i_length != i_delta )
1392 bo_add_32be( stts, i - i_first ); // sample-count
1393 bo_add_32be( stts, i_delta ); // sample-delta
1395 bo_fix_32be( stts, 12, i_index );
1398 /* FIXME add ctts ?? FIXME */
1400 stsz = box_full_new( "stsz", 0, 0 );
1401 bo_add_32be( stsz, 0 ); // sample-size
1402 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
1403 for( i = 0; i < p_stream->i_entry_count; i++ )
1405 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
1409 /* create stss table */
1411 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i++ )
1413 if( p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I )
1417 stss = box_full_new( "stss", 0, 0 );
1418 bo_add_32be( stss, 0 ); /* fixed later */
1420 bo_add_32be( stss, 1 + i );
1426 bo_fix_32be( stss, 12, i_index );
1430 /* Now gather all boxes into stbl */
1431 box_gather( stbl, stsd );
1432 box_gather( stbl, stts );
1435 box_gather( stbl, stss );
1437 box_gather( stbl, stsc );
1438 box_gather( stbl, stsz );
1439 p_stream->i_stco_pos = stbl->i_buffer + 16;
1440 box_gather( stbl, stco );
1448 static int64_t get_timestamp();
1450 static uint32_t mvhd_matrix[9] =
1451 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1453 static bo_t *GetMoovBox( sout_mux_t *p_mux )
1455 sout_mux_sys_t *p_sys = p_mux->p_sys;
1460 uint32_t i_movie_timescale = 90000;
1461 int64_t i_movie_duration = 0;
1463 moov = box_new( "moov" );
1465 /* Create general info */
1466 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1468 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1469 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
1471 msg_Dbg( p_mux, "movie duration %ds",
1472 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
1474 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
1476 /* *** add /moov/mvhd *** */
1477 if( !p_sys->b_64_ext )
1479 mvhd = box_full_new( "mvhd", 0, 0 );
1480 bo_add_32be( mvhd, get_timestamp() ); // creation time
1481 bo_add_32be( mvhd, get_timestamp() ); // modification time
1482 bo_add_32be( mvhd, i_movie_timescale); // timescale
1483 bo_add_32be( mvhd, i_movie_duration ); // duration
1487 mvhd = box_full_new( "mvhd", 1, 0 );
1488 bo_add_64be( mvhd, get_timestamp() ); // creation time
1489 bo_add_64be( mvhd, get_timestamp() ); // modification time
1490 bo_add_32be( mvhd, i_movie_timescale); // timescale
1491 bo_add_64be( mvhd, i_movie_duration ); // duration
1493 bo_add_32be( mvhd, 0x10000 ); // rate
1494 bo_add_16be( mvhd, 0x100 ); // volume
1495 bo_add_16be( mvhd, 0 ); // reserved
1496 for( i = 0; i < 2; i++ )
1498 bo_add_32be( mvhd, 0 ); // reserved
1500 for( i = 0; i < 9; i++ )
1502 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
1504 for( i = 0; i < 6; i++ )
1506 bo_add_32be( mvhd, 0 ); // pre-defined
1509 /* Next available track id */
1510 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
1513 box_gather( moov, mvhd );
1515 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1517 mp4_stream_t *p_stream;
1518 uint32_t i_timescale;
1520 bo_t *trak, *tkhd, *edts, *elst, *mdia, *mdhd, *hdlr;
1521 bo_t *minf, *dinf, *dref, *url, *stbl;
1523 p_stream = p_sys->pp_streams[i_trak];
1525 if( p_stream->fmt.i_cat == AUDIO_ES )
1526 i_timescale = p_stream->fmt.audio.i_rate;
1530 /* *** add /moov/trak *** */
1531 trak = box_new( "trak" );
1533 /* *** add /moov/trak/tkhd *** */
1534 if( !p_sys->b_64_ext )
1537 tkhd = box_full_new( "tkhd", 0, 0x0f );
1539 tkhd = box_full_new( "tkhd", 0, 1 );
1541 bo_add_32be( tkhd, get_timestamp() ); // creation time
1542 bo_add_32be( tkhd, get_timestamp() ); // modification time
1543 bo_add_32be( tkhd, p_stream->i_track_id );
1544 bo_add_32be( tkhd, 0 ); // reserved 0
1545 bo_add_32be( tkhd, p_stream->i_duration *
1546 (int64_t)i_movie_timescale /
1547 (mtime_t)1000000 ); // duration
1552 tkhd = box_full_new( "tkhd", 1, 0x0f );
1554 tkhd = box_full_new( "tkhd", 1, 1 );
1556 bo_add_64be( tkhd, get_timestamp() ); // creation time
1557 bo_add_64be( tkhd, get_timestamp() ); // modification time
1558 bo_add_32be( tkhd, p_stream->i_track_id );
1559 bo_add_32be( tkhd, 0 ); // reserved 0
1560 bo_add_64be( tkhd, p_stream->i_duration *
1561 (int64_t)i_movie_timescale /
1562 (mtime_t)1000000 ); // duration
1565 for( i = 0; i < 2; i++ )
1567 bo_add_32be( tkhd, 0 ); // reserved
1569 bo_add_16be( tkhd, 0 ); // layer
1570 bo_add_16be( tkhd, 0 ); // pre-defined
1572 bo_add_16be( tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0 );
1573 bo_add_16be( tkhd, 0 ); // reserved
1574 for( i = 0; i < 9; i++ )
1576 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
1578 if( p_stream->fmt.i_cat == AUDIO_ES )
1580 bo_add_32be( tkhd, 0 ); // width (presentation)
1581 bo_add_32be( tkhd, 0 ); // height(presentation)
1583 else if( p_stream->fmt.i_cat == VIDEO_ES )
1585 int i_width = p_stream->fmt.video.i_width << 16;
1586 if( p_stream->fmt.video.i_aspect > 0 )
1588 i_width = (int64_t)p_stream->fmt.video.i_aspect *
1589 ((int64_t)p_stream->fmt.video.i_height << 16) /
1592 // width (presentation)
1593 bo_add_32be( tkhd, i_width );
1594 // height(presentation)
1595 bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
1599 int i_width = 320 << 16;
1602 for( i = 0; i < p_sys->i_nb_streams; i++ )
1604 mp4_stream_t *tk = p_sys->pp_streams[i];
1605 if( tk->fmt.i_cat == VIDEO_ES )
1607 if( p_stream->fmt.video.i_aspect )
1608 i_width = (int64_t)p_stream->fmt.video.i_aspect *
1609 ((int64_t)p_stream->fmt.video.i_height<<16) / VOUT_ASPECT_FACTOR;
1611 i_width = p_stream->fmt.video.i_width << 16;
1612 i_height = p_stream->fmt.video.i_height;
1616 bo_add_32be( tkhd, i_width ); // width (presentation)
1617 bo_add_32be( tkhd, i_height << 16 ); // height(presentation)
1621 box_gather( trak, tkhd );
1623 /* *** add /moov/trak/edts and elst */
1624 edts = box_new( "edts" );
1625 elst = box_full_new( "elst", p_sys->b_64_ext ? 1 : 0, 0 );
1626 if( p_stream->i_dts_start > p_sys->i_dts_start )
1628 bo_add_32be( elst, 2 );
1630 if( p_sys->b_64_ext )
1632 bo_add_64be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1633 i_movie_timescale / I64C(1000000) );
1634 bo_add_64be( elst, -1 );
1638 bo_add_32be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1639 i_movie_timescale / I64C(1000000) );
1640 bo_add_32be( elst, -1 );
1642 bo_add_16be( elst, 1 );
1643 bo_add_16be( elst, 0 );
1647 bo_add_32be( elst, 1 );
1649 if( p_sys->b_64_ext )
1651 bo_add_64be( elst, p_stream->i_duration *
1652 i_movie_timescale / I64C(1000000) );
1653 bo_add_64be( elst, 0 );
1657 bo_add_32be( elst, p_stream->i_duration *
1658 i_movie_timescale / I64C(1000000) );
1659 bo_add_32be( elst, 0 );
1661 bo_add_16be( elst, 1 );
1662 bo_add_16be( elst, 0 );
1665 box_gather( edts, elst );
1667 box_gather( trak, edts );
1669 /* *** add /moov/trak/mdia *** */
1670 mdia = box_new( "mdia" );
1673 if( !p_sys->b_64_ext )
1675 mdhd = box_full_new( "mdhd", 0, 0 );
1676 bo_add_32be( mdhd, get_timestamp() ); // creation time
1677 bo_add_32be( mdhd, get_timestamp() ); // modification time
1678 bo_add_32be( mdhd, i_timescale); // timescale
1679 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1680 (mtime_t)1000000 ); // duration
1684 mdhd = box_full_new( "mdhd", 1, 0 );
1685 bo_add_64be( mdhd, get_timestamp() ); // creation time
1686 bo_add_64be( mdhd, get_timestamp() ); // modification time
1687 bo_add_32be( mdhd, i_timescale); // timescale
1688 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1689 (mtime_t)1000000 ); // duration
1692 if( p_stream->fmt.psz_language )
1694 char *psz = p_stream->fmt.psz_language;
1695 const iso639_lang_t *pl = NULL;
1696 uint16_t lang = 0x0;
1698 if( strlen( psz ) == 2 )
1700 pl = GetLang_1( psz );
1702 else if( strlen( psz ) == 3 )
1704 pl = GetLang_2B( psz );
1705 if( !strcmp( pl->psz_iso639_1, "??" ) )
1707 pl = GetLang_2T( psz );
1710 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1712 lang = ( ( pl->psz_iso639_2T[0] - 0x60 ) << 10 ) |
1713 ( ( pl->psz_iso639_2T[1] - 0x60 ) << 5 ) |
1714 ( ( pl->psz_iso639_2T[2] - 0x60 ) );
1716 bo_add_16be( mdhd, lang ); // language
1720 bo_add_16be( mdhd, 0 ); // language
1722 bo_add_16be( mdhd, 0 ); // predefined
1724 box_gather( mdia, mdhd );
1726 /* handler reference */
1727 hdlr = box_full_new( "hdlr", 0, 0 );
1730 bo_add_fourcc( hdlr, "mhlr" ); // media handler
1732 bo_add_32be( hdlr, 0 );
1734 if( p_stream->fmt.i_cat == AUDIO_ES )
1735 bo_add_fourcc( hdlr, "soun" );
1736 else if( p_stream->fmt.i_cat == VIDEO_ES )
1737 bo_add_fourcc( hdlr, "vide" );
1738 else if( p_stream->fmt.i_cat == SPU_ES )
1739 bo_add_fourcc( hdlr, "text" );
1741 bo_add_32be( hdlr, 0 ); // reserved
1742 bo_add_32be( hdlr, 0 ); // reserved
1743 bo_add_32be( hdlr, 0 ); // reserved
1746 bo_add_8( hdlr, 12 ); /* Pascal string for .mov */
1748 if( p_stream->fmt.i_cat == AUDIO_ES )
1749 bo_add_mem( hdlr, 12, "SoundHandler" );
1750 else if( p_stream->fmt.i_cat == VIDEO_ES )
1751 bo_add_mem( hdlr, 12, "VideoHandler" );
1753 bo_add_mem( hdlr, 12, "Text Handler" );
1756 bo_add_8( hdlr, 0 ); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
1759 box_gather( mdia, hdlr );
1762 minf = box_new( "minf" );
1765 if( p_stream->fmt.i_cat == AUDIO_ES )
1769 smhd = box_full_new( "smhd", 0, 0 );
1770 bo_add_16be( smhd, 0 ); // balance
1771 bo_add_16be( smhd, 0 ); // reserved
1774 box_gather( minf, smhd );
1776 else if( p_stream->fmt.i_cat == VIDEO_ES )
1780 vmhd = box_full_new( "vmhd", 0, 1 );
1781 bo_add_16be( vmhd, 0 ); // graphicsmode
1782 for( i = 0; i < 3; i++ )
1784 bo_add_16be( vmhd, 0 ); // opcolor
1788 box_gather( minf, vmhd );
1790 else if( p_stream->fmt.i_cat == SPU_ES )
1792 bo_t *gmhd = box_new( "gmhd" );
1793 bo_t *gmin = box_full_new( "gmin", 0, 1 );
1795 bo_add_16be( gmin, 0 ); // graphicsmode
1796 for( i = 0; i < 3; i++ )
1798 bo_add_16be( gmin, 0 ); // opcolor
1800 bo_add_16be( gmin, 0 ); // balance
1801 bo_add_16be( gmin, 0 ); // reserved
1804 box_gather( gmhd, gmin );
1807 box_gather( minf, gmhd );
1811 dinf = box_new( "dinf" );
1812 dref = box_full_new( "dref", 0, 0 );
1813 bo_add_32be( dref, 1 );
1814 url = box_full_new( "url ", 0, 0x01 );
1816 box_gather( dref, url );
1818 box_gather( dinf, dref );
1820 /* append dinf to mdia */
1822 box_gather( minf, dinf );
1825 stbl = GetStblBox( p_mux, p_stream );
1827 /* append stbl to minf */
1828 p_stream->i_stco_pos += minf->i_buffer;
1829 box_gather( minf, stbl );
1831 /* append minf to mdia */
1833 p_stream->i_stco_pos += mdia->i_buffer;
1834 box_gather( mdia, minf );
1836 /* append mdia to trak */
1838 p_stream->i_stco_pos += trak->i_buffer;
1839 box_gather( trak, mdia );
1841 /* append trak to moov */
1843 p_stream->i_stco_pos += moov->i_buffer;
1844 box_gather( moov, trak );
1847 /* Add user data tags */
1848 box_gather( moov, GetUdtaTag( p_mux ) );
1854 /****************************************************************************/
1856 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1861 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1862 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1866 p_bo->i_buffer_size = i_size;
1867 p_bo->p_buffer = p_buffer;
1870 p_bo->b_grow = b_grow;
1874 static void bo_add_8( bo_t *p_bo, uint8_t i )
1876 if( p_bo->i_buffer < p_bo->i_buffer_size )
1878 p_bo->p_buffer[p_bo->i_buffer] = i;
1880 else if( p_bo->b_grow )
1882 p_bo->i_buffer_size += 1024;
1883 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1885 p_bo->p_buffer[p_bo->i_buffer] = i;
1891 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1893 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1894 bo_add_8( p_bo, i &0xff );
1897 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1899 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1900 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1901 bo_add_8( p_bo, ( i &0xff ) );
1903 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1905 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1906 bo_add_16be( p_bo, i &0xffff );
1909 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1911 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1912 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1913 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1914 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1917 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1919 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1920 bo_add_32be( p_bo, i &0xffffffff );
1923 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1925 bo_add_8( p_bo, fcc[0] );
1926 bo_add_8( p_bo, fcc[1] );
1927 bo_add_8( p_bo, fcc[2] );
1928 bo_add_8( p_bo, fcc[3] );
1931 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1935 for( i = 0; i < i_size; i++ )
1937 bo_add_8( p_bo, p_mem[i] );
1941 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
1947 vals[3] = (unsigned char)(i_length & 0x7f);
1949 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
1951 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
1953 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
1955 bo_add_8( p_bo, tag );
1957 if( i_size < 0x00000080 )
1959 bo_add_8( p_bo, vals[3] );
1961 else if( i_size < 0x00004000 )
1963 bo_add_8( p_bo, vals[2] );
1964 bo_add_8( p_bo, vals[3] );
1966 else if( i_size < 0x00200000 )
1968 bo_add_8( p_bo, vals[1] );
1969 bo_add_8( p_bo, vals[2] );
1970 bo_add_8( p_bo, vals[3] );
1972 else if( i_size < 0x10000000 )
1974 bo_add_8( p_bo, vals[0] );
1975 bo_add_8( p_bo, vals[1] );
1976 bo_add_8( p_bo, vals[2] );
1977 bo_add_8( p_bo, vals[3] );
1981 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1985 for( i = 0; i < p_bo2->i_buffer; i++ )
1987 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1991 static bo_t * box_new( char *fcc )
1995 if( ( box = malloc( sizeof( bo_t ) ) ) )
1997 bo_init( box, 0, NULL, VLC_TRUE );
1999 bo_add_32be ( box, 0 );
2000 bo_add_fourcc( box, fcc );
2006 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
2010 if( ( box = malloc( sizeof( bo_t ) ) ) )
2012 bo_init( box, 0, NULL, VLC_TRUE );
2014 bo_add_32be ( box, 0 );
2015 bo_add_fourcc( box, fcc );
2016 bo_add_8 ( box, v );
2017 bo_add_24be ( box, f );
2023 static void box_fix( bo_t *box )
2027 memcpy( &box_tmp, box, sizeof( bo_t ) );
2029 box_tmp.i_buffer = 0;
2030 bo_add_32be( &box_tmp, box->i_buffer );
2033 static void box_free( bo_t *box )
2037 free( box->p_buffer );
2043 static void box_gather ( bo_t *box, bo_t *box2 )
2045 bo_add_bo( box, box2 );
2049 static block_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
2053 p_buf = block_New( p_sout, box->i_buffer );
2054 if( box->i_buffer > 0 )
2056 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
2062 static void box_send( sout_mux_t *p_mux, bo_t *box )
2066 p_buf = bo_to_sout( p_mux->p_sout, box );
2069 sout_AccessOutWrite( p_mux->p_access, p_buf );
2072 static int64_t get_timestamp()
2074 int64_t i_timestamp = 0;
2077 i_timestamp = time(NULL);
2078 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2079 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60