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 set_callbacks( Open, Close );
66 /*****************************************************************************
68 *****************************************************************************/
69 static const char *ppsz_sout_options[] = {
73 static int Control( sout_mux_t *, int, va_list );
74 static int AddStream( sout_mux_t *, sout_input_t * );
75 static int DelStream( sout_mux_t *, sout_input_t * );
76 static int Mux ( sout_mux_t * );
78 /*****************************************************************************
80 *****************************************************************************/
98 unsigned int i_entry_count;
99 unsigned int i_entry_max;
101 int64_t i_length_neg;
107 /* for later stco fix-up (fast start files) */
128 struct sout_mux_sys_t
132 vlc_bool_t b_fast_start;
140 mp4_stream_t **pp_streams;
153 static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
154 static void bo_add_8 ( bo_t *, uint8_t );
155 static void bo_add_16be ( bo_t *, uint16_t );
156 static void bo_add_24be ( bo_t *, uint32_t );
157 static void bo_add_32be ( bo_t *, uint32_t );
158 static void bo_add_64be ( bo_t *, uint64_t );
159 static void bo_add_fourcc(bo_t *, char * );
160 static void bo_add_bo ( bo_t *, bo_t * );
161 static void bo_add_mem ( bo_t *, int , uint8_t * );
162 static void bo_add_descr( bo_t *, uint8_t , uint32_t );
164 static void bo_fix_32be ( bo_t *, int , uint32_t );
166 static bo_t *box_new ( char *fcc );
167 static bo_t *box_full_new( char *fcc, uint8_t v, uint32_t f );
168 static void box_fix ( bo_t *box );
169 static void box_free ( bo_t *box );
170 static void box_gather ( bo_t *box, bo_t *box2 );
172 static void box_send( sout_mux_t *p_mux, bo_t *box );
174 static block_t *bo_to_sout( sout_instance_t *p_sout, bo_t *box );
176 static bo_t *GetMoovBox( sout_mux_t *p_mux );
178 static block_t *ConvertSUBT( sout_mux_t *, mp4_stream_t *, block_t *);
179 static void ConvertAVC1( sout_mux_t *, mp4_stream_t *, block_t * );
181 /*****************************************************************************
183 *****************************************************************************/
184 static int Open( vlc_object_t *p_this )
186 sout_mux_t *p_mux = (sout_mux_t*)p_this;
187 sout_mux_sys_t *p_sys;
190 msg_Dbg( p_mux, "Mp4 muxer opend" );
191 sout_CfgParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
193 p_mux->pf_control = Control;
194 p_mux->pf_addstream = AddStream;
195 p_mux->pf_delstream = DelStream;
197 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
199 p_sys->i_nb_streams = 0;
200 p_sys->pp_streams = NULL;
201 p_sys->i_mdat_pos = 0;
202 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
203 p_sys->i_dts_start = 0;
208 /* Now add ftyp header */
209 box = box_new( "ftyp" );
210 bo_add_fourcc( box, "isom" );
211 bo_add_32be ( box, 0 );
212 bo_add_fourcc( box, "mp41" );
215 p_sys->i_pos += box->i_buffer;
216 p_sys->i_mdat_pos = p_sys->i_pos;
218 box_send( p_mux, box );
222 * Quicktime actually doesn't like the 64 bits extensions !!! */
223 p_sys->b_64_ext = VLC_FALSE;
225 /* Now add mdat header */
226 box = box_new( "mdat" );
227 bo_add_64be ( box, 0 ); // enough to store an extended size
229 p_sys->i_pos += box->i_buffer;
231 box_send( p_mux, box );
236 /*****************************************************************************
238 *****************************************************************************/
239 static void Close( vlc_object_t * p_this )
241 sout_mux_t *p_mux = (sout_mux_t*)p_this;
242 sout_mux_sys_t *p_sys = p_mux->p_sys;
250 msg_Dbg( p_mux, "Close" );
252 /* Update mdat size */
253 bo_init( &bo, 0, NULL, VLC_TRUE );
254 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
257 bo_add_32be ( &bo, 1 );
258 bo_add_fourcc( &bo, "mdat" );
259 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
263 bo_add_32be ( &bo, 8 );
264 bo_add_fourcc( &bo, "wide" );
265 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
266 bo_add_fourcc( &bo, "mdat" );
268 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
271 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
272 sout_AccessOutWrite( p_mux->p_access, p_hdr );
274 /* Create MOOV header */
275 i_moov_pos = p_sys->i_pos;
276 moov = GetMoovBox( p_mux );
278 /* Check we need to create "fast start" files */
279 var_Get( p_this, SOUT_CFG_PREFIX "faststart", &val );
280 p_sys->b_fast_start = val.b_bool;
281 while( p_sys->b_fast_start )
283 /* Move data to the end of the file so we can fit the moov header
286 int64_t i_chunk, i_size = p_sys->i_pos - p_sys->i_mdat_pos;
287 int i_moov_size = moov->i_buffer;
291 i_chunk = __MIN( 32768, i_size );
292 p_buf = block_New( p_mux, i_chunk );
293 sout_AccessOutSeek( p_mux->p_access,
294 p_sys->i_mdat_pos + i_size - i_chunk );
295 if( sout_AccessOutRead( p_mux->p_access, p_buf ) < i_chunk )
297 msg_Warn( p_this, "read() not supported by acces output, "
298 "won't create a fast start file" );
299 p_sys->b_fast_start = VLC_FALSE;
300 block_Release( p_buf );
303 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos + i_size +
304 i_moov_size - i_chunk );
305 sout_AccessOutWrite( p_mux->p_access, p_buf );
309 if( !p_sys->b_fast_start ) break;
311 /* Fix-up samples to chunks table in MOOV header */
312 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
314 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
318 moov->i_buffer = p_stream->i_stco_pos;
319 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
321 if( p_stream->b_stco64 )
322 bo_add_64be( moov, p_stream->entry[i].i_pos + i_moov_size);
324 bo_add_32be( moov, p_stream->entry[i].i_pos + i_moov_size);
326 while( i < p_stream->i_entry_count )
328 if( i + 1 < p_stream->i_entry_count &&
329 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
330 != p_stream->entry[i + 1].i_pos )
341 moov->i_buffer = i_moov_size;
342 i_moov_pos = p_sys->i_mdat_pos;
343 p_sys->b_fast_start = VLC_FALSE;
346 /* Write MOOV header */
347 sout_AccessOutSeek( p_mux->p_access, i_moov_pos );
348 box_send( p_mux, moov );
351 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
353 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
355 es_format_Clean( &p_stream->fmt );
356 if( p_stream->avc.i_sps ) free( p_stream->avc.sps );
357 if( p_stream->avc.i_pps ) free( p_stream->avc.pps );
358 free( p_stream->entry );
361 if( p_sys->i_nb_streams ) free( p_sys->pp_streams );
365 /*****************************************************************************
367 *****************************************************************************/
368 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
374 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
375 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
376 *pb_bool = VLC_FALSE;
379 case MUX_GET_ADD_STREAM_WAIT:
380 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
384 case MUX_GET_MIME: /* Not needed, as not streamable */
390 /*****************************************************************************
392 *****************************************************************************/
393 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
395 sout_mux_sys_t *p_sys = p_mux->p_sys;
396 mp4_stream_t *p_stream;
398 switch( p_input->p_fmt->i_codec )
400 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
401 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
402 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
403 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
404 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
405 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
406 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
407 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
408 case VLC_FOURCC( 'h', '2', '6', '4' ):
410 case VLC_FOURCC( 's', 'u', 'b', 't' ):
411 msg_Warn( p_mux, "subtitle track added like in .mov (even when creating .mp4)" );
414 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
415 (char*)&p_input->p_fmt->i_codec );
419 p_stream = malloc( sizeof( mp4_stream_t ) );
420 es_format_Copy( &p_stream->fmt, p_input->p_fmt );
421 p_stream->i_track_id = p_sys->i_nb_streams + 1;
422 p_stream->i_length_neg = 0;
423 p_stream->i_entry_count = 0;
424 p_stream->i_entry_max = 1000;
426 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
427 p_stream->i_dts_start = 0;
428 p_stream->i_duration = 0;
429 p_stream->avc.i_profile = 77;
430 p_stream->avc.i_level = 51;
431 p_stream->avc.i_sps = 0;
432 p_stream->avc.sps = NULL;
433 p_stream->avc.i_pps = 0;
434 p_stream->avc.pps = NULL;
436 p_input->p_sys = p_stream;
438 msg_Dbg( p_mux, "adding input" );
440 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
444 /*****************************************************************************
446 *****************************************************************************/
447 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
449 msg_Dbg( p_mux, "removing input" );
453 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
459 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
461 block_fifo_t *p_fifo = p_mux->pp_inputs[i]->p_fifo;
464 if( p_fifo->i_depth <= 1 )
466 if( p_mux->pp_inputs[i]->p_fmt->i_cat != SPU_ES )
468 return -1; // wait that all fifo have at least 2 packets
470 /* For SPU, we wait only 1 packet */
474 p_buf = block_FifoShow( p_fifo );
475 if( i_stream < 0 || p_buf->i_dts < i_dts )
477 i_dts = p_buf->i_dts;
483 *pi_stream = i_stream;
492 /*****************************************************************************
494 *****************************************************************************/
495 static int Mux( sout_mux_t *p_mux )
497 sout_mux_sys_t *p_sys = p_mux->p_sys;
501 sout_input_t *p_input;
503 mp4_stream_t *p_stream;
507 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
509 return( VLC_SUCCESS );
512 p_input = p_mux->pp_inputs[i_stream];
513 p_stream = (mp4_stream_t*)p_input->p_sys;
515 p_data = block_FifoGet( p_input->p_fifo );
516 if( p_stream->fmt.i_codec == VLC_FOURCC( 'h', '2', '6', '4' ) )
518 ConvertAVC1( p_mux, p_stream, p_data );
520 else if( p_stream->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
522 p_data = ConvertSUBT( p_mux, p_stream, p_data );
525 if( p_stream->fmt.i_cat != SPU_ES )
527 /* Fix length of the sample */
528 if( p_input->p_fifo->i_depth > 0 )
530 block_t *p_next = block_FifoShow( p_input->p_fifo );
531 int64_t i_diff = p_next->i_dts - p_data->i_dts;
533 if( i_diff < I64C(1000000 ) ) /* protection */
535 p_data->i_length = i_diff;
538 if( p_data->i_length <= 0 )
540 msg_Warn( p_mux, "i_length <= 0" );
541 p_stream->i_length_neg += p_data->i_length - 1;
542 p_data->i_length = 1;
544 else if( p_stream->i_length_neg < 0 )
546 int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
548 p_data->i_length -= i_recover;
549 p_stream->i_length_neg += i_recover;
553 /* Save starting time */
554 if( p_stream->i_entry_count == 0 )
556 p_stream->i_dts_start = p_data->i_dts;
558 /* Update global dts_start */
559 if( p_sys->i_dts_start <= 0 ||
560 p_stream->i_dts_start < p_sys->i_dts_start )
562 p_sys->i_dts_start = p_stream->i_dts_start;
566 if( p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0 )
568 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
572 /* FIXME handle this broken case */
577 if( p_stream->entry[p_stream->i_entry_count-1].i_length <= 0 )
579 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
584 /* add index entry */
585 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
586 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_buffer;
587 p_stream->entry[p_stream->i_entry_count].i_pts_dts=
588 __MAX( p_data->i_pts - p_data->i_dts, 0 );
589 p_stream->entry[p_stream->i_entry_count].i_length = p_data->i_length;
590 p_stream->entry[p_stream->i_entry_count].i_flags = p_data->i_flags;
592 p_stream->i_entry_count++;
593 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
594 if( p_stream->i_entry_count >= p_stream->i_entry_max - 1 )
596 p_stream->i_entry_max += 1000;
598 realloc( p_stream->entry,
599 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
603 p_stream->i_duration += p_data->i_length;
604 p_sys->i_pos += p_data->i_buffer;
607 p_stream->i_last_dts = p_data->i_dts;
610 sout_AccessOutWrite( p_mux->p_access, p_data );
612 if( p_stream->fmt.i_cat == SPU_ES )
614 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
619 msg_Dbg( p_mux, "writing a empty subs" ) ;
621 /* Append a idx entry */
622 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
623 p_stream->entry[p_stream->i_entry_count].i_size = 3;
624 p_stream->entry[p_stream->i_entry_count].i_pts_dts= 0;
625 p_stream->entry[p_stream->i_entry_count].i_length = 0;
626 p_stream->entry[p_stream->i_entry_count].i_flags = 0;
628 /* XXX: No need to grow the entry here */
629 p_stream->i_entry_count++;
632 p_stream->i_last_dts += i_length;
635 p_data = block_New( p_mux, 3 );
636 p_data->p_buffer[0] = 0;
637 p_data->p_buffer[1] = 1;
638 p_data->p_buffer[2] = ' ';
640 p_sys->i_pos += p_data->i_buffer;
642 sout_AccessOutWrite( p_mux->p_access, p_data );
646 p_stream->i_duration = p_stream->i_last_dts - p_stream->i_dts_start;
650 return( VLC_SUCCESS );
653 /*****************************************************************************
655 *****************************************************************************/
656 static block_t *ConvertSUBT( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
658 p_block = block_Realloc( p_block, 2, p_block->i_buffer );
660 /* No trailling '\0' */
661 if( p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0' )
664 p_block->p_buffer[0] = ( (p_block->i_buffer - 2) >> 8 )&0xff;
665 p_block->p_buffer[1] = ( (p_block->i_buffer - 2) )&0xff;
670 static void ConvertAVC1( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
672 uint8_t *last = p_block->p_buffer; /* Assume it starts with 0x00000001 */
673 uint8_t *dat = &p_block->p_buffer[4];
674 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
677 /* Replace the 4 bytes start code with 4 bytes size,
678 * FIXME are all startcode 4 bytes ? (I don't think :( */
683 while( dat < end - 4 )
685 if( dat[0] == 0x00 && dat[1] == 0x00 &&
686 dat[2] == 0x00 && dat[3] == 0x01 )
698 i_size = dat - &last[4];
699 last[0] = ( i_size >> 24 )&0xff;
700 last[1] = ( i_size >> 16 )&0xff;
701 last[2] = ( i_size >> 8 )&0xff;
702 last[3] = ( i_size )&0xff;
704 if( (last[4]&0x1f) == 7 && tk->avc.i_sps <= 0 ) /* SPS */
706 tk->avc.i_sps = i_size;
707 tk->avc.sps = malloc( i_size );
708 memcpy( tk->avc.sps, &last[4], i_size );
710 tk->avc.i_profile = tk->avc.sps[1];
711 tk->avc.i_level = tk->avc.sps[3];
713 else if( (last[4]&0x1f) == 8 && tk->avc.i_pps <= 0 ) /* PPS */
715 tk->avc.i_pps = i_size;
716 tk->avc.pps = malloc( i_size );
717 memcpy( tk->avc.pps, &last[4], i_size );
727 static int GetDescrLength( int i_size )
729 if( i_size < 0x00000080 )
731 else if( i_size < 0x00004000 )
733 else if( i_size < 0x00200000 )
739 static bo_t *GetESDS( mp4_stream_t *p_stream )
743 int i_object_type_indication;
744 int i_decoder_specific_info_size;
746 if( p_stream->fmt.i_extra > 0 )
748 i_decoder_specific_info_size =
749 GetDescrLength( p_stream->fmt.i_extra );
753 i_decoder_specific_info_size = 0;
756 esds = box_full_new( "esds", 0, 0 );
759 bo_add_descr( esds, 0x03, 3 +
760 GetDescrLength( 13 + i_decoder_specific_info_size ) +
761 GetDescrLength( 1 ) );
762 bo_add_16be( esds, p_stream->i_track_id );
763 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
765 /* DecoderConfigDescr */
766 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
768 switch( p_stream->fmt.i_codec )
770 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
771 i_object_type_indication = 0x20;
773 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
774 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
775 i_object_type_indication = 0x60;
777 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
778 /* FIXME for mpeg2-aac == 0x66->0x68 */
779 i_object_type_indication = 0x40;
781 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
782 i_object_type_indication =
783 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
786 i_object_type_indication = 0x00;
789 i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
791 bo_add_8 ( esds, i_object_type_indication );
792 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
793 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
794 bo_add_32be( esds, 0x7fffffff ); // maxBitrate
795 bo_add_32be( esds, 0 ); // avgBitrate
797 if( p_stream->fmt.i_extra > 0 )
801 /* DecoderSpecificInfo */
802 bo_add_descr( esds, 0x05, p_stream->fmt.i_extra );
804 for( i = 0; i < p_stream->fmt.i_extra; i++ )
806 bo_add_8( esds, ((uint8_t*)p_stream->fmt.p_extra)[i] );
810 /* SL_Descr mandatory */
811 bo_add_descr( esds, 0x06, 1 );
812 bo_add_8 ( esds, 0x02 ); // sl_predefined
819 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
824 wave = box_new( "wave" );
826 box = box_new( "frma" );
827 bo_add_fourcc( box, "mp4a" );
829 box_gather( wave, box );
831 box = box_new( "mp4a" );
832 bo_add_32be( box, 0 );
834 box_gather( wave, box );
836 box = GetESDS( p_stream );
838 box_gather( wave, box );
840 box = box_new( "srcq" );
841 bo_add_32be( box, 0x40 );
843 box_gather( wave, box );
846 bo_add_32be( wave, 8 ); /* new empty box */
847 bo_add_32be( wave, 0 ); /* box label */
854 static bo_t *GetAvcCTag( mp4_stream_t *p_stream )
858 /* FIXME use better value */
859 avcC = box_new( "avcC" );
860 bo_add_8( avcC, 1 ); /* configuration version */
861 bo_add_8( avcC, p_stream->avc.i_profile );
862 bo_add_8( avcC, p_stream->avc.i_profile ); /* profile compatible ??? */
863 bo_add_8( avcC, p_stream->avc.i_level ); /* level, 5.1 */
864 bo_add_8( avcC, 0xff ); /* 0b11111100 | lengthsize = 0x11 */
866 bo_add_8( avcC, 0xe0 | (p_stream->avc.i_sps > 0 ? 1 : 0) ); /* 0b11100000 | sps_count */
867 if( p_stream->avc.i_sps > 0 )
869 bo_add_16be( avcC, p_stream->avc.i_sps );
870 bo_add_mem( avcC, p_stream->avc.i_sps, p_stream->avc.sps );
873 bo_add_8( avcC, (p_stream->avc.i_pps > 0 ? 1 : 0) ); /* pps_count */
874 if( p_stream->avc.i_pps > 0 )
876 bo_add_16be( avcC, p_stream->avc.i_pps );
877 bo_add_mem( avcC, p_stream->avc.i_pps, p_stream->avc.pps );
884 /* TODO: No idea about these values */
885 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
887 bo_t *smi = box_new( "SMI " );
889 if( p_stream->fmt.i_extra > 0x4e )
891 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
892 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
894 while( p + 8 < p_end )
896 int i_size = GetDWBE( p );
899 /* FIXME handle 1 as long size */
902 if( !strncmp( &p[4], "SMI ", 4 ) )
904 bo_add_mem( smi, p_end - p - 8, &p[8] );
911 /* Create a dummy one in fallback */
912 bo_add_fourcc( smi, "SEQH" );
913 bo_add_32be( smi, 0x5 );
914 bo_add_32be( smi, 0xe2c0211d );
915 bo_add_8( smi, 0xc0 );
921 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
923 sout_mux_sys_t *p_sys = p_mux->p_sys;
924 bo_t *udta = box_new( "udta" );
925 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
929 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
931 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
933 if( p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','v') ||
934 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
936 bo_t *box = box_new( "\251req" );
938 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
939 bo_add_16be( box, 0 );
940 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
941 "QuickTime 6.0 or greater" );
943 box_gather( udta, box );
950 bo_t *box = box_new( "\251enc" );
952 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
953 bo_add_16be( box, 0 );
954 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
955 PACKAGE_STRING " stream output" );
957 box_gather( udta, box );
964 for( i = 0; i < p_meta->i_meta; i++ )
968 if( !strcmp( p_meta->name[i], VLC_META_TITLE ) )
969 box = box_new( "\251nam" );
970 else if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
971 box = box_new( "\251aut" );
972 else if( !strcmp( p_meta->name[i], VLC_META_ARTIST ) )
973 box = box_new( "\251ART" );
974 else if( !strcmp( p_meta->name[i], VLC_META_GENRE ) )
975 box = box_new( "\251gen" );
976 else if( !strcmp( p_meta->name[i], VLC_META_COPYRIGHT ) )
977 box = box_new( "\251cpy" );
978 else if( !strcmp( p_meta->name[i], VLC_META_DESCRIPTION ) )
979 box = box_new( "\251des" );
980 else if( !strcmp( p_meta->name[i], VLC_META_DATE ) )
981 box = box_new( "\251day" );
982 else if( !strcmp( p_meta->name[i], VLC_META_URL ) )
983 box = box_new( "\251url" );
987 bo_add_16be( box, strlen( p_meta->value[i] ) );
988 bo_add_16be( box, 0 );
989 bo_add_mem( box, strlen( p_meta->value[i] ),
992 box_gather( udta, box );
1001 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1003 sout_mux_sys_t *p_sys = p_mux->p_sys;
1004 vlc_bool_t b_descr = VLC_FALSE;
1009 switch( p_stream->fmt.i_codec )
1011 case VLC_FOURCC('m','p','4','a'):
1012 memcpy( fcc, "mp4a", 4 );
1016 case VLC_FOURCC('m','p','g','a'):
1018 memcpy( fcc, ".mp3", 4 );
1021 memcpy( fcc, "mp4a", 4 );
1027 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1031 soun = box_new( fcc );
1032 for( i = 0; i < 6; i++ )
1034 bo_add_8( soun, 0 ); // reserved;
1036 bo_add_16be( soun, 1 ); // data-reference-index
1038 /* SoundDescription */
1040 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1042 bo_add_16be( soun, 1 ); // version 1;
1046 bo_add_16be( soun, 0 ); // version 0;
1048 bo_add_16be( soun, 0 ); // revision level (0)
1049 bo_add_32be( soun, 0 ); // vendor
1051 bo_add_16be( soun, p_stream->fmt.audio.i_channels );
1053 bo_add_16be( soun, p_stream->fmt.audio.i_bitspersample ?
1054 p_stream->fmt.audio.i_bitspersample : 16 );
1055 bo_add_16be( soun, -2 ); // compression id
1056 bo_add_16be( soun, 0 ); // packet size (0)
1057 bo_add_16be( soun, p_stream->fmt.audio.i_rate ); // sampleratehi
1058 bo_add_16be( soun, 0 ); // sampleratelo
1060 /* Extended data for SoundDescription V1 */
1062 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1064 /* samples per packet */
1065 bo_add_32be( soun, p_stream->fmt.audio.i_frame_length );
1066 bo_add_32be( soun, 1536 ); /* bytes per packet */
1067 bo_add_32be( soun, 2 ); /* bytes per frame */
1068 /* bytes per sample */
1069 bo_add_32be( soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1072 /* Add an ES Descriptor */
1078 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1080 box = GetWaveTag( p_stream );
1084 box = GetESDS( p_stream );
1087 box_gather( soun, box );
1095 static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1102 switch( p_stream->fmt.i_codec )
1104 case VLC_FOURCC('m','p','4','v'):
1105 case VLC_FOURCC('m','p','g','v'):
1106 memcpy( fcc, "mp4v", 4 );
1109 case VLC_FOURCC('M','J','P','G'):
1110 memcpy( fcc, "mjpa", 4 );
1113 case VLC_FOURCC('S','V','Q','1'):
1114 memcpy( fcc, "SVQ1", 4 );
1117 case VLC_FOURCC('S','V','Q','3'):
1118 memcpy( fcc, "SVQ3", 4 );
1121 case VLC_FOURCC('h','2','6','4'):
1122 memcpy( fcc, "avc1", 4 );
1126 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1130 vide = box_new( fcc );
1131 for( i = 0; i < 6; i++ )
1133 bo_add_8( vide, 0 ); // reserved;
1135 bo_add_16be( vide, 1 ); // data-reference-index
1137 bo_add_16be( vide, 0 ); // predefined;
1138 bo_add_16be( vide, 0 ); // reserved;
1139 for( i = 0; i < 3; i++ )
1141 bo_add_32be( vide, 0 ); // predefined;
1144 bo_add_16be( vide, p_stream->fmt.video.i_width ); // i_width
1145 bo_add_16be( vide, p_stream->fmt.video.i_height ); // i_height
1147 bo_add_32be( vide, 0x00480000 ); // h 72dpi
1148 bo_add_32be( vide, 0x00480000 ); // v 72dpi
1150 bo_add_32be( vide, 0 ); // data size, always 0
1151 bo_add_16be( vide, 1 ); // frames count per sample
1154 for( i = 0; i < 32; i++ )
1156 bo_add_8( vide, 0 );
1159 bo_add_16be( vide, 0x18 ); // depth
1160 bo_add_16be( vide, 0xffff ); // predefined
1162 /* add an ES Descriptor */
1163 switch( p_stream->fmt.i_codec )
1165 case VLC_FOURCC('m','p','4','v'):
1166 case VLC_FOURCC('m','p','g','v'):
1168 bo_t *esds = GetESDS( p_stream );
1171 box_gather( vide, esds );
1175 case VLC_FOURCC('S','V','Q','3'):
1177 bo_t *esds = GetSVQ3Tag( p_stream );
1180 box_gather( vide, esds );
1184 case VLC_FOURCC('h','2','6','4'):
1185 box_gather( vide, GetAvcCTag( p_stream ) );
1197 static bo_t *GetTextBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1200 bo_t *text = box_new( "text" );
1203 for( i = 0; i < 6; i++ )
1205 bo_add_8( text, 0 ); // reserved;
1207 bo_add_16be( text, 1 ); // data-reference-index
1209 bo_add_32be( text, 0 ); // display flags
1210 bo_add_32be( text, 0 ); // justification
1211 for( i = 0; i < 3; i++ )
1213 bo_add_16be( text, 0 ); // back ground color
1216 bo_add_16be( text, 0 ); // box text
1217 bo_add_16be( text, 0 ); // box text
1218 bo_add_16be( text, 0 ); // box text
1219 bo_add_16be( text, 0 ); // box text
1221 bo_add_64be( text, 0 ); // reserved
1222 for( i = 0; i < 3; i++ )
1224 bo_add_16be( text, 0xff ); // foreground color
1227 bo_add_8 ( text, 9 );
1228 bo_add_mem( text, 9, "Helvetica" );
1235 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1237 sout_mux_sys_t *p_sys = p_mux->p_sys;
1238 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
1239 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz, *stss;
1240 uint32_t i_timescale;
1241 int64_t i_dts, i_dts_q;
1243 stbl = box_new( "stbl" );
1245 /* sample description */
1246 stsd = box_full_new( "stsd", 0, 0 );
1247 bo_add_32be( stsd, 1 );
1248 if( p_stream->fmt.i_cat == AUDIO_ES )
1250 bo_t *soun = GetSounBox( p_mux, p_stream );
1251 box_gather( stsd, soun );
1253 else if( p_stream->fmt.i_cat == VIDEO_ES )
1255 bo_t *vide = GetVideBox( p_mux, p_stream );
1256 box_gather( stsd, vide );
1258 else if( p_stream->fmt.i_cat == SPU_ES )
1260 box_gather( stsd, GetTextBox( p_mux, p_stream ) );
1264 /* chunk offset table */
1265 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
1267 /* 64 bits version */
1268 p_stream->b_stco64 = VLC_TRUE;
1269 stco = box_full_new( "co64", 0, 0 );
1273 /* 32 bits version */
1274 p_stream->b_stco64 = VLC_FALSE;
1275 stco = box_full_new( "stco", 0, 0 );
1277 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
1279 /* sample to chunk table */
1280 stsc = box_full_new( "stsc", 0, 0 );
1281 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
1283 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
1284 i < p_stream->i_entry_count; i_chunk++ )
1288 if( p_stream->b_stco64 )
1289 bo_add_64be( stco, p_stream->entry[i].i_pos );
1291 bo_add_32be( stco, p_stream->entry[i].i_pos );
1293 while( i < p_stream->i_entry_count )
1295 if( i + 1 < p_stream->i_entry_count &&
1296 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
1297 != p_stream->entry[i + 1].i_pos )
1306 /* Add entry to the stsc table */
1307 if( i_stsc_last_val != i - i_first )
1309 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
1310 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
1311 bo_add_32be( stsc, 1 ); // sample-descr-index
1312 i_stsc_last_val = i - i_first;
1317 /* Fix stco entry count */
1318 bo_fix_32be( stco, 12, i_chunk );
1319 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
1322 /* Fix stsc entry count */
1323 bo_fix_32be( stsc, 12, i_stsc_entries );
1327 stts = box_full_new( "stts", 0, 0 );
1328 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
1330 if( p_stream->fmt.i_cat == AUDIO_ES )
1331 i_timescale = p_stream->fmt.audio.i_rate;
1335 /* first, create quantified length */
1336 for( i = 0, i_dts = 0, i_dts_q = 0; i < p_stream->i_entry_count; i++ )
1338 int64_t i_dts_deq = i_dts_q * I64C(1000000) / (int64_t)i_timescale;
1339 int64_t i_delta = p_stream->entry[i].i_length + i_dts - i_dts_deq;
1341 i_dts += p_stream->entry[i].i_length;
1343 p_stream->entry[i].i_length =
1344 i_delta * (int64_t)i_timescale / I64C(1000000);
1346 i_dts_q += p_stream->entry[i].i_length;
1348 /* then write encoded table */
1349 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
1352 int64_t i_delta = p_stream->entry[i].i_length;
1354 while( i < p_stream->i_entry_count )
1357 if( i >= p_stream->i_entry_count ||
1358 p_stream->entry[i].i_length != i_delta )
1364 bo_add_32be( stts, i - i_first ); // sample-count
1365 bo_add_32be( stts, i_delta ); // sample-delta
1367 bo_fix_32be( stts, 12, i_index );
1370 /* FIXME add ctts ?? FIXME */
1372 stsz = box_full_new( "stsz", 0, 0 );
1373 bo_add_32be( stsz, 0 ); // sample-size
1374 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
1375 for( i = 0; i < p_stream->i_entry_count; i++ )
1377 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
1381 /* create stss table */
1383 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i++ )
1385 if( p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I )
1389 stss = box_full_new( "stss", 0, 0 );
1390 bo_add_32be( stss, 0 ); /* fixed later */
1392 bo_add_32be( stss, 1 + i );
1398 bo_fix_32be( stss, 12, i_index );
1402 /* Now gather all boxes into stbl */
1403 box_gather( stbl, stsd );
1404 box_gather( stbl, stts );
1407 box_gather( stbl, stss );
1409 box_gather( stbl, stsc );
1410 box_gather( stbl, stsz );
1411 p_stream->i_stco_pos = stbl->i_buffer + 16;
1412 box_gather( stbl, stco );
1420 static int64_t get_timestamp();
1422 static uint32_t mvhd_matrix[9] =
1423 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1425 static bo_t *GetMoovBox( sout_mux_t *p_mux )
1427 sout_mux_sys_t *p_sys = p_mux->p_sys;
1432 uint32_t i_movie_timescale = 90000;
1433 int64_t i_movie_duration = 0;
1435 moov = box_new( "moov" );
1437 /* Create general info */
1438 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1440 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1441 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
1443 msg_Dbg( p_mux, "movie duration %ds",
1444 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
1446 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
1448 /* *** add /moov/mvhd *** */
1449 if( !p_sys->b_64_ext )
1451 mvhd = box_full_new( "mvhd", 0, 0 );
1452 bo_add_32be( mvhd, get_timestamp() ); // creation time
1453 bo_add_32be( mvhd, get_timestamp() ); // modification time
1454 bo_add_32be( mvhd, i_movie_timescale); // timescale
1455 bo_add_32be( mvhd, i_movie_duration ); // duration
1459 mvhd = box_full_new( "mvhd", 1, 0 );
1460 bo_add_64be( mvhd, get_timestamp() ); // creation time
1461 bo_add_64be( mvhd, get_timestamp() ); // modification time
1462 bo_add_32be( mvhd, i_movie_timescale); // timescale
1463 bo_add_64be( mvhd, i_movie_duration ); // duration
1465 bo_add_32be( mvhd, 0x10000 ); // rate
1466 bo_add_16be( mvhd, 0x100 ); // volume
1467 bo_add_16be( mvhd, 0 ); // reserved
1468 for( i = 0; i < 2; i++ )
1470 bo_add_32be( mvhd, 0 ); // reserved
1472 for( i = 0; i < 9; i++ )
1474 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
1476 for( i = 0; i < 6; i++ )
1478 bo_add_32be( mvhd, 0 ); // pre-defined
1481 /* Next available track id */
1482 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
1485 box_gather( moov, mvhd );
1487 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1489 mp4_stream_t *p_stream;
1490 uint32_t i_timescale;
1492 bo_t *trak, *tkhd, *edts, *elst, *mdia, *mdhd, *hdlr;
1493 bo_t *minf, *dinf, *dref, *url, *stbl;
1495 p_stream = p_sys->pp_streams[i_trak];
1497 if( p_stream->fmt.i_cat == AUDIO_ES )
1498 i_timescale = p_stream->fmt.audio.i_rate;
1502 /* *** add /moov/trak *** */
1503 trak = box_new( "trak" );
1505 /* *** add /moov/trak/tkhd *** */
1506 if( !p_sys->b_64_ext )
1509 tkhd = box_full_new( "tkhd", 0, 0x0f );
1511 tkhd = box_full_new( "tkhd", 0, 1 );
1513 bo_add_32be( tkhd, get_timestamp() ); // creation time
1514 bo_add_32be( tkhd, get_timestamp() ); // modification time
1515 bo_add_32be( tkhd, p_stream->i_track_id );
1516 bo_add_32be( tkhd, 0 ); // reserved 0
1517 bo_add_32be( tkhd, p_stream->i_duration *
1518 (int64_t)i_movie_timescale /
1519 (mtime_t)1000000 ); // duration
1524 tkhd = box_full_new( "tkhd", 1, 0x0f );
1526 tkhd = box_full_new( "tkhd", 1, 1 );
1528 bo_add_64be( tkhd, get_timestamp() ); // creation time
1529 bo_add_64be( tkhd, get_timestamp() ); // modification time
1530 bo_add_32be( tkhd, p_stream->i_track_id );
1531 bo_add_32be( tkhd, 0 ); // reserved 0
1532 bo_add_64be( tkhd, p_stream->i_duration *
1533 (int64_t)i_movie_timescale /
1534 (mtime_t)1000000 ); // duration
1537 for( i = 0; i < 2; i++ )
1539 bo_add_32be( tkhd, 0 ); // reserved
1541 bo_add_16be( tkhd, 0 ); // layer
1542 bo_add_16be( tkhd, 0 ); // pre-defined
1544 bo_add_16be( tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0 );
1545 bo_add_16be( tkhd, 0 ); // reserved
1546 for( i = 0; i < 9; i++ )
1548 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
1550 if( p_stream->fmt.i_cat == AUDIO_ES )
1552 bo_add_32be( tkhd, 0 ); // width (presentation)
1553 bo_add_32be( tkhd, 0 ); // height(presentation)
1555 else if( p_stream->fmt.i_cat == VIDEO_ES )
1557 int i_width = p_stream->fmt.video.i_width;
1558 if( p_stream->fmt.video.i_aspect > 0 )
1560 i_width = p_stream->fmt.video.i_aspect *
1561 (p_stream->fmt.video.i_height << 16) / VOUT_ASPECT_FACTOR;
1563 // width (presentation)
1564 bo_add_32be( tkhd, i_width );
1565 // height(presentation)
1566 bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
1573 for( i = 0; i < p_sys->i_nb_streams; i++ )
1575 mp4_stream_t *tk = p_sys->pp_streams[i];
1576 if( tk->fmt.i_cat == VIDEO_ES )
1578 if( p_stream->fmt.video.i_aspect )
1579 i_width = p_stream->fmt.video.i_aspect *
1580 p_stream->fmt.video.i_height / VOUT_ASPECT_FACTOR;
1582 i_width = p_stream->fmt.video.i_width;
1583 i_height = p_stream->fmt.video.i_height;
1587 bo_add_32be( tkhd, i_width << 16 ); // width (presentation)
1588 bo_add_32be( tkhd, i_height << 16 ); // height(presentation)
1592 box_gather( trak, tkhd );
1594 /* *** add /moov/trak/edts and elst */
1595 edts = box_new( "edts" );
1596 elst = box_full_new( "elst", p_sys->b_64_ext ? 1 : 0, 0 );
1597 if( p_stream->i_dts_start > p_sys->i_dts_start )
1599 bo_add_32be( elst, 2 );
1601 if( p_sys->b_64_ext )
1603 bo_add_64be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1604 i_movie_timescale / I64C(1000000) );
1605 bo_add_64be( elst, -1 );
1609 bo_add_32be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1610 i_movie_timescale / I64C(1000000) );
1611 bo_add_32be( elst, -1 );
1613 bo_add_16be( elst, 1 );
1614 bo_add_16be( elst, 0 );
1618 bo_add_32be( elst, 1 );
1620 if( p_sys->b_64_ext )
1622 bo_add_64be( elst, p_stream->i_duration *
1623 i_movie_timescale / I64C(1000000) );
1624 bo_add_64be( elst, 0 );
1628 bo_add_32be( elst, p_stream->i_duration *
1629 i_movie_timescale / I64C(1000000) );
1630 bo_add_32be( elst, 0 );
1632 bo_add_16be( elst, 1 );
1633 bo_add_16be( elst, 0 );
1636 box_gather( edts, elst );
1638 box_gather( trak, edts );
1640 /* *** add /moov/trak/mdia *** */
1641 mdia = box_new( "mdia" );
1644 if( !p_sys->b_64_ext )
1646 mdhd = box_full_new( "mdhd", 0, 0 );
1647 bo_add_32be( mdhd, get_timestamp() ); // creation time
1648 bo_add_32be( mdhd, get_timestamp() ); // modification time
1649 bo_add_32be( mdhd, i_timescale); // timescale
1650 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1651 (mtime_t)1000000 ); // duration
1655 mdhd = box_full_new( "mdhd", 1, 0 );
1656 bo_add_64be( mdhd, get_timestamp() ); // creation time
1657 bo_add_64be( mdhd, get_timestamp() ); // modification time
1658 bo_add_32be( mdhd, i_timescale); // timescale
1659 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1660 (mtime_t)1000000 ); // duration
1663 if( p_stream->fmt.psz_language )
1665 char *psz = p_stream->fmt.psz_language;
1666 const iso639_lang_t *pl = NULL;
1667 uint16_t lang = 0x0;
1669 if( strlen( psz ) == 2 )
1671 pl = GetLang_1( psz );
1673 else if( strlen( psz ) == 3 )
1675 pl = GetLang_2B( psz );
1676 if( !strcmp( pl->psz_iso639_1, "??" ) )
1678 pl = GetLang_2T( psz );
1681 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1683 lang = ( ( pl->psz_iso639_2T[0] - 0x60 ) << 10 ) |
1684 ( ( pl->psz_iso639_2T[1] - 0x60 ) << 5 ) |
1685 ( ( pl->psz_iso639_2T[2] - 0x60 ) );
1687 bo_add_16be( mdhd, lang ); // language
1691 bo_add_16be( mdhd, 0 ); // language
1693 bo_add_16be( mdhd, 0 ); // predefined
1695 box_gather( mdia, mdhd );
1697 /* handler reference */
1698 hdlr = box_full_new( "hdlr", 0, 0 );
1701 bo_add_fourcc( hdlr, "mhlr" ); // media handler
1703 bo_add_32be( hdlr, 0 );
1705 if( p_stream->fmt.i_cat == AUDIO_ES )
1706 bo_add_fourcc( hdlr, "soun" );
1707 else if( p_stream->fmt.i_cat == VIDEO_ES )
1708 bo_add_fourcc( hdlr, "vide" );
1709 else if( p_stream->fmt.i_cat == SPU_ES )
1710 bo_add_fourcc( hdlr, "text" );
1712 bo_add_32be( hdlr, 0 ); // reserved
1713 bo_add_32be( hdlr, 0 ); // reserved
1714 bo_add_32be( hdlr, 0 ); // reserved
1717 bo_add_8( hdlr, 12 ); /* Pascal string for .mov */
1719 if( p_stream->fmt.i_cat == AUDIO_ES )
1720 bo_add_mem( hdlr, 12, "SoundHandler" );
1721 else if( p_stream->fmt.i_cat == VIDEO_ES )
1722 bo_add_mem( hdlr, 12, "VideoHandler" );
1724 bo_add_mem( hdlr, 12, "Text Handler" );
1727 bo_add_8( hdlr, 0 ); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
1730 box_gather( mdia, hdlr );
1733 minf = box_new( "minf" );
1736 if( p_stream->fmt.i_cat == AUDIO_ES )
1740 smhd = box_full_new( "smhd", 0, 0 );
1741 bo_add_16be( smhd, 0 ); // balance
1742 bo_add_16be( smhd, 0 ); // reserved
1745 box_gather( minf, smhd );
1747 else if( p_stream->fmt.i_cat == VIDEO_ES )
1751 vmhd = box_full_new( "vmhd", 0, 1 );
1752 bo_add_16be( vmhd, 0 ); // graphicsmode
1753 for( i = 0; i < 3; i++ )
1755 bo_add_16be( vmhd, 0 ); // opcolor
1759 box_gather( minf, vmhd );
1761 else if( p_stream->fmt.i_cat == SPU_ES )
1763 bo_t *gmhd = box_new( "gmhd" );
1764 bo_t *gmin = box_full_new( "gmin", 0, 1 );
1766 bo_add_16be( gmin, 0 ); // graphicsmode
1767 for( i = 0; i < 3; i++ )
1769 bo_add_16be( gmin, 0 ); // opcolor
1771 bo_add_16be( gmin, 0 ); // balance
1772 bo_add_16be( gmin, 0 ); // reserved
1775 box_gather( gmhd, gmin );
1778 box_gather( minf, gmhd );
1782 dinf = box_new( "dinf" );
1783 dref = box_full_new( "dref", 0, 0 );
1784 bo_add_32be( dref, 1 );
1785 url = box_full_new( "url ", 0, 0x01 );
1787 box_gather( dref, url );
1789 box_gather( dinf, dref );
1791 /* append dinf to mdia */
1793 box_gather( minf, dinf );
1796 stbl = GetStblBox( p_mux, p_stream );
1798 /* append stbl to minf */
1799 p_stream->i_stco_pos += minf->i_buffer;
1800 box_gather( minf, stbl );
1802 /* append minf to mdia */
1804 p_stream->i_stco_pos += mdia->i_buffer;
1805 box_gather( mdia, minf );
1807 /* append mdia to trak */
1809 p_stream->i_stco_pos += trak->i_buffer;
1810 box_gather( trak, mdia );
1812 /* append trak to moov */
1814 p_stream->i_stco_pos += moov->i_buffer;
1815 box_gather( moov, trak );
1818 /* Add user data tags */
1819 box_gather( moov, GetUdtaTag( p_mux ) );
1825 /****************************************************************************/
1827 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1832 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1833 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1837 p_bo->i_buffer_size = i_size;
1838 p_bo->p_buffer = p_buffer;
1841 p_bo->b_grow = b_grow;
1845 static void bo_add_8( bo_t *p_bo, uint8_t i )
1847 if( p_bo->i_buffer < p_bo->i_buffer_size )
1849 p_bo->p_buffer[p_bo->i_buffer] = i;
1851 else if( p_bo->b_grow )
1853 p_bo->i_buffer_size += 1024;
1854 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1856 p_bo->p_buffer[p_bo->i_buffer] = i;
1862 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1864 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1865 bo_add_8( p_bo, i &0xff );
1868 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1870 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1871 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1872 bo_add_8( p_bo, ( i &0xff ) );
1874 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1876 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1877 bo_add_16be( p_bo, i &0xffff );
1880 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1882 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1883 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1884 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1885 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1888 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1890 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1891 bo_add_32be( p_bo, i &0xffffffff );
1894 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1896 bo_add_8( p_bo, fcc[0] );
1897 bo_add_8( p_bo, fcc[1] );
1898 bo_add_8( p_bo, fcc[2] );
1899 bo_add_8( p_bo, fcc[3] );
1902 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1906 for( i = 0; i < i_size; i++ )
1908 bo_add_8( p_bo, p_mem[i] );
1912 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
1918 vals[3] = (unsigned char)(i_length & 0x7f);
1920 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
1922 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
1924 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
1926 bo_add_8( p_bo, tag );
1928 if( i_size < 0x00000080 )
1930 bo_add_8( p_bo, vals[3] );
1932 else if( i_size < 0x00004000 )
1934 bo_add_8( p_bo, vals[2] );
1935 bo_add_8( p_bo, vals[3] );
1937 else if( i_size < 0x00200000 )
1939 bo_add_8( p_bo, vals[1] );
1940 bo_add_8( p_bo, vals[2] );
1941 bo_add_8( p_bo, vals[3] );
1943 else if( i_size < 0x10000000 )
1945 bo_add_8( p_bo, vals[0] );
1946 bo_add_8( p_bo, vals[1] );
1947 bo_add_8( p_bo, vals[2] );
1948 bo_add_8( p_bo, vals[3] );
1952 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1956 for( i = 0; i < p_bo2->i_buffer; i++ )
1958 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1962 static bo_t * box_new( char *fcc )
1966 if( ( box = malloc( sizeof( bo_t ) ) ) )
1968 bo_init( box, 0, NULL, VLC_TRUE );
1970 bo_add_32be ( box, 0 );
1971 bo_add_fourcc( box, fcc );
1977 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
1981 if( ( box = malloc( sizeof( bo_t ) ) ) )
1983 bo_init( box, 0, NULL, VLC_TRUE );
1985 bo_add_32be ( box, 0 );
1986 bo_add_fourcc( box, fcc );
1987 bo_add_8 ( box, v );
1988 bo_add_24be ( box, f );
1994 static void box_fix( bo_t *box )
1998 memcpy( &box_tmp, box, sizeof( bo_t ) );
2000 box_tmp.i_buffer = 0;
2001 bo_add_32be( &box_tmp, box->i_buffer );
2004 static void box_free( bo_t *box )
2008 free( box->p_buffer );
2014 static void box_gather ( bo_t *box, bo_t *box2 )
2016 bo_add_bo( box, box2 );
2020 static block_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
2024 p_buf = block_New( p_sout, box->i_buffer );
2025 if( box->i_buffer > 0 )
2027 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
2033 static void box_send( sout_mux_t *p_mux, bo_t *box )
2037 p_buf = bo_to_sout( p_mux->p_sout, box );
2040 sout_AccessOutWrite( p_mux->p_access, p_buf );
2043 static int64_t get_timestamp()
2045 int64_t i_timestamp = 0;
2048 i_timestamp = time(NULL);
2049 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2050 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60