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 Capability(sout_mux_t *, int, void *, void * );
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_ParseCfg( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
193 p_mux->pf_capacity = Capability;
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 Capability( sout_mux_t *p_mux, int i_query, void *p_args,
373 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
374 *(vlc_bool_t*)p_answer = VLC_TRUE;
375 return SOUT_MUX_CAP_ERR_OK;
377 case SOUT_MUX_CAP_GET_ADD_STREAM_WAIT:
378 *(vlc_bool_t*)p_answer = VLC_TRUE;
379 return( SOUT_MUX_CAP_ERR_OK );
382 return SOUT_MUX_CAP_ERR_UNIMPLEMENTED;
386 /*****************************************************************************
388 *****************************************************************************/
389 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
391 sout_mux_sys_t *p_sys = p_mux->p_sys;
392 mp4_stream_t *p_stream;
394 switch( p_input->p_fmt->i_codec )
396 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
397 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
398 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
399 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
400 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
401 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
402 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
403 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
404 case VLC_FOURCC( 'h', '2', '6', '4' ):
406 case VLC_FOURCC( 's', 'u', 'b', 't' ):
407 msg_Warn( p_mux, "subtitle track added like in .mov (even when creating .mp4)" );
410 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
411 (char*)&p_input->p_fmt->i_codec );
415 p_stream = malloc( sizeof( mp4_stream_t ) );
416 es_format_Copy( &p_stream->fmt, p_input->p_fmt );
417 p_stream->i_track_id = p_sys->i_nb_streams + 1;
418 p_stream->i_length_neg = 0;
419 p_stream->i_entry_count = 0;
420 p_stream->i_entry_max = 1000;
422 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
423 p_stream->i_dts_start = 0;
424 p_stream->i_duration = 0;
425 p_stream->avc.i_profile = 77;
426 p_stream->avc.i_level = 51;
427 p_stream->avc.i_sps = 0;
428 p_stream->avc.sps = NULL;
429 p_stream->avc.i_pps = 0;
430 p_stream->avc.pps = NULL;
432 p_input->p_sys = p_stream;
434 msg_Dbg( p_mux, "adding input" );
436 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
440 /*****************************************************************************
442 *****************************************************************************/
443 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
445 msg_Dbg( p_mux, "removing input" );
449 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
455 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
457 block_fifo_t *p_fifo = p_mux->pp_inputs[i]->p_fifo;
460 if( p_fifo->i_depth <= 1 )
462 if( p_mux->pp_inputs[i]->p_fmt->i_cat != SPU_ES )
464 return -1; // wait that all fifo have at least 2 packets
466 /* For SPU, we wait only 1 packet */
470 p_buf = block_FifoShow( p_fifo );
471 if( i_stream < 0 || p_buf->i_dts < i_dts )
473 i_dts = p_buf->i_dts;
479 *pi_stream = i_stream;
488 /*****************************************************************************
490 *****************************************************************************/
491 static int Mux( sout_mux_t *p_mux )
493 sout_mux_sys_t *p_sys = p_mux->p_sys;
497 sout_input_t *p_input;
499 mp4_stream_t *p_stream;
503 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
505 return( VLC_SUCCESS );
508 p_input = p_mux->pp_inputs[i_stream];
509 p_stream = (mp4_stream_t*)p_input->p_sys;
511 p_data = block_FifoGet( p_input->p_fifo );
512 if( p_stream->fmt.i_codec == VLC_FOURCC( 'h', '2', '6', '4' ) )
514 ConvertAVC1( p_mux, p_stream, p_data );
516 else if( p_stream->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
518 p_data = ConvertSUBT( p_mux, p_stream, p_data );
521 if( p_stream->fmt.i_cat != SPU_ES )
523 /* Fix length of the sample */
524 if( p_input->p_fifo->i_depth > 0 )
526 block_t *p_next = block_FifoShow( p_input->p_fifo );
527 int64_t i_diff = p_next->i_dts - p_data->i_dts;
529 if( i_diff < I64C(1000000 ) ) /* protection */
531 p_data->i_length = i_diff;
534 if( p_data->i_length <= 0 )
536 msg_Warn( p_mux, "i_length <= 0" );
537 p_stream->i_length_neg += p_data->i_length - 1;
538 p_data->i_length = 1;
540 else if( p_stream->i_length_neg < 0 )
542 int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
544 p_data->i_length -= i_recover;
545 p_stream->i_length_neg += i_recover;
549 /* Save starting time */
550 if( p_stream->i_entry_count == 0 )
552 p_stream->i_dts_start = p_data->i_dts;
554 /* Update global dts_start */
555 if( p_sys->i_dts_start <= 0 ||
556 p_stream->i_dts_start < p_sys->i_dts_start )
558 p_sys->i_dts_start = p_stream->i_dts_start;
562 if( p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0 )
564 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
568 /* FIXME handle this broken case */
573 if( p_stream->entry[p_stream->i_entry_count-1].i_length <= 0 )
575 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
580 /* add index entry */
581 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
582 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_buffer;
583 p_stream->entry[p_stream->i_entry_count].i_pts_dts=
584 __MAX( p_data->i_pts - p_data->i_dts, 0 );
585 p_stream->entry[p_stream->i_entry_count].i_length = p_data->i_length;
586 p_stream->entry[p_stream->i_entry_count].i_flags = p_data->i_flags;
588 p_stream->i_entry_count++;
589 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
590 if( p_stream->i_entry_count >= p_stream->i_entry_max - 1 )
592 p_stream->i_entry_max += 1000;
594 realloc( p_stream->entry,
595 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
599 p_stream->i_duration += p_data->i_length;
600 p_sys->i_pos += p_data->i_buffer;
603 p_stream->i_last_dts = p_data->i_dts;
606 sout_AccessOutWrite( p_mux->p_access, p_data );
608 if( p_stream->fmt.i_cat == SPU_ES )
610 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
615 msg_Dbg( p_mux, "writing a empty subs" ) ;
617 /* Append a idx entry */
618 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
619 p_stream->entry[p_stream->i_entry_count].i_size = 3;
620 p_stream->entry[p_stream->i_entry_count].i_pts_dts= 0;
621 p_stream->entry[p_stream->i_entry_count].i_length = 0;
622 p_stream->entry[p_stream->i_entry_count].i_flags = 0;
624 /* XXX: No need to grow the entry here */
625 p_stream->i_entry_count++;
628 p_stream->i_last_dts += i_length;
631 p_data = block_New( p_mux, 3 );
632 p_data->p_buffer[0] = 0;
633 p_data->p_buffer[1] = 1;
634 p_data->p_buffer[2] = ' ';
636 p_sys->i_pos += p_data->i_buffer;
638 sout_AccessOutWrite( p_mux->p_access, p_data );
642 p_stream->i_duration = p_stream->i_last_dts - p_stream->i_dts_start;
646 return( VLC_SUCCESS );
649 /*****************************************************************************
651 *****************************************************************************/
652 static block_t *ConvertSUBT( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
654 p_block = block_Realloc( p_block, 2, p_block->i_buffer );
656 /* No trailling '\0' */
657 if( p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0' )
660 p_block->p_buffer[0] = ( (p_block->i_buffer - 2) >> 8 )&0xff;
661 p_block->p_buffer[1] = ( (p_block->i_buffer - 2) )&0xff;
666 static void ConvertAVC1( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
668 uint8_t *src = p_block->p_buffer;
669 int i_src = p_block->i_buffer;
670 uint8_t *end = &p_block->p_buffer[i_src];
672 uint8_t *dst = p_block->p_buffer;
680 if( src[0] != 0 || src[1] != 0 || src[2] != 0 || src[3] != 1 )
683 /* skip start code and room for size */
688 i_type = (*dst++ = *src++)&0x1f;
693 if( src < end - 4 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x00 && src[3] == 0x01 )
698 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 )
708 i_size = dst - &fix[4];
709 fix[0] = (i_size >> 24)&0xff;
710 fix[1] = (i_size >> 16)&0xff;
711 fix[2] = (i_size >> 8)&0xff;
712 fix[3] = (i_size )&0xff;
714 if( i_type == 7 && tk->avc.i_sps <= 0 ) /* SPS */
716 tk->avc.i_sps = i_size;
717 tk->avc.sps = malloc( i_size );
718 memcpy( tk->avc.sps, &fix[4], i_size );
720 tk->avc.i_profile = tk->avc.sps[1];
721 tk->avc.i_level = tk->avc.sps[3];
723 else if( i_type == 8 && tk->avc.i_pps <= 0) /* PPS */
725 tk->avc.i_pps = i_size;
726 tk->avc.pps = malloc( i_size );
727 memcpy( tk->avc.pps, &fix[4], i_size );
731 p_block->i_buffer = dst - p_block->p_buffer;
735 static int GetDescrLength( int i_size )
737 if( i_size < 0x00000080 )
739 else if( i_size < 0x00004000 )
741 else if( i_size < 0x00200000 )
747 static bo_t *GetESDS( mp4_stream_t *p_stream )
751 int i_object_type_indication;
752 int i_decoder_specific_info_size;
754 if( p_stream->fmt.i_extra > 0 )
756 i_decoder_specific_info_size =
757 GetDescrLength( p_stream->fmt.i_extra );
761 i_decoder_specific_info_size = 0;
764 esds = box_full_new( "esds", 0, 0 );
767 bo_add_descr( esds, 0x03, 3 +
768 GetDescrLength( 13 + i_decoder_specific_info_size ) +
769 GetDescrLength( 1 ) );
770 bo_add_16be( esds, p_stream->i_track_id );
771 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
773 /* DecoderConfigDescr */
774 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
776 switch( p_stream->fmt.i_codec )
778 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
779 i_object_type_indication = 0x20;
781 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
782 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
783 i_object_type_indication = 0x60;
785 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
786 /* FIXME for mpeg2-aac == 0x66->0x68 */
787 i_object_type_indication = 0x40;
789 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
790 i_object_type_indication =
791 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
794 i_object_type_indication = 0x00;
797 i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
799 bo_add_8 ( esds, i_object_type_indication );
800 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
801 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
802 bo_add_32be( esds, 0x7fffffff ); // maxBitrate
803 bo_add_32be( esds, 0 ); // avgBitrate
805 if( p_stream->fmt.i_extra > 0 )
809 /* DecoderSpecificInfo */
810 bo_add_descr( esds, 0x05, p_stream->fmt.i_extra );
812 for( i = 0; i < p_stream->fmt.i_extra; i++ )
814 bo_add_8( esds, ((uint8_t*)p_stream->fmt.p_extra)[i] );
818 /* SL_Descr mandatory */
819 bo_add_descr( esds, 0x06, 1 );
820 bo_add_8 ( esds, 0x02 ); // sl_predefined
827 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
832 wave = box_new( "wave" );
834 box = box_new( "frma" );
835 bo_add_fourcc( box, "mp4a" );
837 box_gather( wave, box );
839 box = box_new( "mp4a" );
840 bo_add_32be( box, 0 );
842 box_gather( wave, box );
844 box = GetESDS( p_stream );
846 box_gather( wave, box );
848 box = box_new( "srcq" );
849 bo_add_32be( box, 0x40 );
851 box_gather( wave, box );
854 bo_add_32be( wave, 8 ); /* new empty box */
855 bo_add_32be( wave, 0 ); /* box label */
862 static bo_t *GetAvcCTag( mp4_stream_t *p_stream )
866 /* FIXME use better value */
867 avcC = box_new( "avcC" );
868 bo_add_8( avcC, 1 ); /* configuration version */
869 bo_add_8( avcC, p_stream->avc.i_profile );
870 bo_add_8( avcC, p_stream->avc.i_profile ); /* profile compatible ??? */
871 bo_add_8( avcC, p_stream->avc.i_level ); /* level, 5.1 */
872 bo_add_8( avcC, 0xff ); /* 0b11111100 | lengthsize = 0x11 */
874 bo_add_8( avcC, 0xe0 | (p_stream->avc.i_sps > 0 ? 1 : 0) ); /* 0b11100000 | sps_count */
875 if( p_stream->avc.i_sps > 0 )
877 bo_add_16be( avcC, p_stream->avc.i_sps );
878 bo_add_mem( avcC, p_stream->avc.i_sps, p_stream->avc.sps );
881 bo_add_8( avcC, (p_stream->avc.i_pps > 0 ? 1 : 0) ); /* pps_count */
882 if( p_stream->avc.i_pps > 0 )
884 bo_add_16be( avcC, p_stream->avc.i_pps );
885 bo_add_mem( avcC, p_stream->avc.i_pps, p_stream->avc.pps );
892 /* TODO: No idea about these values */
893 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
895 bo_t *smi = box_new( "SMI " );
897 if( p_stream->fmt.i_extra > 0x4e )
899 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
900 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
902 while( p + 8 < p_end )
904 int i_size = GetDWBE( p );
907 /* FIXME handle 1 as long size */
910 if( !strncmp( &p[4], "SMI ", 4 ) )
912 bo_add_mem( smi, p_end - p - 8, &p[8] );
919 /* Create a dummy one in fallback */
920 bo_add_fourcc( smi, "SEQH" );
921 bo_add_32be( smi, 0x5 );
922 bo_add_32be( smi, 0xe2c0211d );
923 bo_add_8( smi, 0xc0 );
929 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
931 sout_mux_sys_t *p_sys = p_mux->p_sys;
932 bo_t *udta = box_new( "udta" );
933 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
937 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
939 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
941 if( p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','v') ||
942 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
944 bo_t *box = box_new( "\251req" );
946 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
947 bo_add_16be( box, 0 );
948 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
949 "QuickTime 6.0 or greater" );
951 box_gather( udta, box );
958 bo_t *box = box_new( "\251enc" );
960 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
961 bo_add_16be( box, 0 );
962 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
963 PACKAGE_STRING " stream output" );
965 box_gather( udta, box );
972 for( i = 0; i < p_meta->i_meta; i++ )
976 if( !strcmp( p_meta->name[i], VLC_META_TITLE ) )
977 box = box_new( "\251nam" );
978 else if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
979 box = box_new( "\251aut" );
980 else if( !strcmp( p_meta->name[i], VLC_META_ARTIST ) )
981 box = box_new( "\251ART" );
982 else if( !strcmp( p_meta->name[i], VLC_META_GENRE ) )
983 box = box_new( "\251gen" );
984 else if( !strcmp( p_meta->name[i], VLC_META_COPYRIGHT ) )
985 box = box_new( "\251cpy" );
986 else if( !strcmp( p_meta->name[i], VLC_META_DESCRIPTION ) )
987 box = box_new( "\251des" );
988 else if( !strcmp( p_meta->name[i], VLC_META_DATE ) )
989 box = box_new( "\251day" );
990 else if( !strcmp( p_meta->name[i], VLC_META_URL ) )
991 box = box_new( "\251url" );
995 bo_add_16be( box, strlen( p_meta->value[i] ) );
996 bo_add_16be( box, 0 );
997 bo_add_mem( box, strlen( p_meta->value[i] ),
1000 box_gather( udta, box );
1009 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1011 sout_mux_sys_t *p_sys = p_mux->p_sys;
1012 vlc_bool_t b_descr = VLC_FALSE;
1017 switch( p_stream->fmt.i_codec )
1019 case VLC_FOURCC('m','p','4','a'):
1020 memcpy( fcc, "mp4a", 4 );
1024 case VLC_FOURCC('m','p','g','a'):
1026 memcpy( fcc, ".mp3", 4 );
1029 memcpy( fcc, "mp4a", 4 );
1035 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1039 soun = box_new( fcc );
1040 for( i = 0; i < 6; i++ )
1042 bo_add_8( soun, 0 ); // reserved;
1044 bo_add_16be( soun, 1 ); // data-reference-index
1046 /* SoundDescription */
1048 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1050 bo_add_16be( soun, 1 ); // version 1;
1054 bo_add_16be( soun, 0 ); // version 0;
1056 bo_add_16be( soun, 0 ); // revision level (0)
1057 bo_add_32be( soun, 0 ); // vendor
1059 bo_add_16be( soun, p_stream->fmt.audio.i_channels );
1061 bo_add_16be( soun, p_stream->fmt.audio.i_bitspersample ?
1062 p_stream->fmt.audio.i_bitspersample : 16 );
1063 bo_add_16be( soun, -2 ); // compression id
1064 bo_add_16be( soun, 0 ); // packet size (0)
1065 bo_add_16be( soun, p_stream->fmt.audio.i_rate ); // sampleratehi
1066 bo_add_16be( soun, 0 ); // sampleratelo
1068 /* Extended data for SoundDescription V1 */
1070 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1072 /* samples per packet */
1073 bo_add_32be( soun, p_stream->fmt.audio.i_frame_length );
1074 bo_add_32be( soun, 1536 ); /* bytes per packet */
1075 bo_add_32be( soun, 2 ); /* bytes per frame */
1076 /* bytes per sample */
1077 bo_add_32be( soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1080 /* Add an ES Descriptor */
1086 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1088 box = GetWaveTag( p_stream );
1092 box = GetESDS( p_stream );
1095 box_gather( soun, box );
1103 static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1110 switch( p_stream->fmt.i_codec )
1112 case VLC_FOURCC('m','p','4','v'):
1113 case VLC_FOURCC('m','p','g','v'):
1114 memcpy( fcc, "mp4v", 4 );
1117 case VLC_FOURCC('M','J','P','G'):
1118 memcpy( fcc, "mjpa", 4 );
1121 case VLC_FOURCC('S','V','Q','1'):
1122 memcpy( fcc, "SVQ1", 4 );
1125 case VLC_FOURCC('S','V','Q','3'):
1126 memcpy( fcc, "SVQ3", 4 );
1129 case VLC_FOURCC('h','2','6','4'):
1130 memcpy( fcc, "avc1", 4 );
1134 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1138 vide = box_new( fcc );
1139 for( i = 0; i < 6; i++ )
1141 bo_add_8( vide, 0 ); // reserved;
1143 bo_add_16be( vide, 1 ); // data-reference-index
1145 bo_add_16be( vide, 0 ); // predefined;
1146 bo_add_16be( vide, 0 ); // reserved;
1147 for( i = 0; i < 3; i++ )
1149 bo_add_32be( vide, 0 ); // predefined;
1152 bo_add_16be( vide, p_stream->fmt.video.i_width ); // i_width
1153 bo_add_16be( vide, p_stream->fmt.video.i_height ); // i_height
1155 bo_add_32be( vide, 0x00480000 ); // h 72dpi
1156 bo_add_32be( vide, 0x00480000 ); // v 72dpi
1158 bo_add_32be( vide, 0 ); // data size, always 0
1159 bo_add_16be( vide, 1 ); // frames count per sample
1162 for( i = 0; i < 32; i++ )
1164 bo_add_8( vide, 0 );
1167 bo_add_16be( vide, 0x18 ); // depth
1168 bo_add_16be( vide, 0xffff ); // predefined
1170 /* add an ES Descriptor */
1171 switch( p_stream->fmt.i_codec )
1173 case VLC_FOURCC('m','p','4','v'):
1174 case VLC_FOURCC('m','p','g','v'):
1176 bo_t *esds = GetESDS( p_stream );
1179 box_gather( vide, esds );
1183 case VLC_FOURCC('S','V','Q','3'):
1185 bo_t *esds = GetSVQ3Tag( p_stream );
1188 box_gather( vide, esds );
1192 case VLC_FOURCC('h','2','6','4'):
1193 box_gather( vide, GetAvcCTag( p_stream ) );
1205 static bo_t *GetTextBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1208 bo_t *text = box_new( "text" );
1211 for( i = 0; i < 6; i++ )
1213 bo_add_8( text, 0 ); // reserved;
1215 bo_add_16be( text, 1 ); // data-reference-index
1217 bo_add_32be( text, 0 ); // display flags
1218 bo_add_32be( text, 0 ); // justification
1219 for( i = 0; i < 3; i++ )
1221 bo_add_16be( text, 0 ); // back ground color
1224 bo_add_16be( text, 0 ); // box text
1225 bo_add_16be( text, 0 ); // box text
1226 bo_add_16be( text, 0 ); // box text
1227 bo_add_16be( text, 0 ); // box text
1229 bo_add_64be( text, 0 ); // reserved
1230 for( i = 0; i < 3; i++ )
1232 bo_add_16be( text, 0xff ); // foreground color
1235 bo_add_8 ( text, 9 );
1236 bo_add_mem( text, 9, "Helvetica" );
1243 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1245 sout_mux_sys_t *p_sys = p_mux->p_sys;
1246 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
1247 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz, *stss;
1248 uint32_t i_timescale;
1249 int64_t i_dts, i_dts_q;
1251 stbl = box_new( "stbl" );
1253 /* sample description */
1254 stsd = box_full_new( "stsd", 0, 0 );
1255 bo_add_32be( stsd, 1 );
1256 if( p_stream->fmt.i_cat == AUDIO_ES )
1258 bo_t *soun = GetSounBox( p_mux, p_stream );
1259 box_gather( stsd, soun );
1261 else if( p_stream->fmt.i_cat == VIDEO_ES )
1263 bo_t *vide = GetVideBox( p_mux, p_stream );
1264 box_gather( stsd, vide );
1266 else if( p_stream->fmt.i_cat == SPU_ES )
1268 box_gather( stsd, GetTextBox( p_mux, p_stream ) );
1272 /* chunk offset table */
1273 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
1275 /* 64 bits version */
1276 p_stream->b_stco64 = VLC_TRUE;
1277 stco = box_full_new( "co64", 0, 0 );
1281 /* 32 bits version */
1282 p_stream->b_stco64 = VLC_FALSE;
1283 stco = box_full_new( "stco", 0, 0 );
1285 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
1287 /* sample to chunk table */
1288 stsc = box_full_new( "stsc", 0, 0 );
1289 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
1291 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
1292 i < p_stream->i_entry_count; i_chunk++ )
1296 if( p_stream->b_stco64 )
1297 bo_add_64be( stco, p_stream->entry[i].i_pos );
1299 bo_add_32be( stco, p_stream->entry[i].i_pos );
1301 while( i < p_stream->i_entry_count )
1303 if( i + 1 < p_stream->i_entry_count &&
1304 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
1305 != p_stream->entry[i + 1].i_pos )
1314 /* Add entry to the stsc table */
1315 if( i_stsc_last_val != i - i_first )
1317 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
1318 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
1319 bo_add_32be( stsc, 1 ); // sample-descr-index
1320 i_stsc_last_val = i - i_first;
1325 /* Fix stco entry count */
1326 bo_fix_32be( stco, 12, i_chunk );
1327 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
1330 /* Fix stsc entry count */
1331 bo_fix_32be( stsc, 12, i_stsc_entries );
1335 stts = box_full_new( "stts", 0, 0 );
1336 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
1338 if( p_stream->fmt.i_cat == AUDIO_ES )
1339 i_timescale = p_stream->fmt.audio.i_rate;
1343 /* first, create quantified length */
1344 for( i = 0, i_dts = 0, i_dts_q = 0; i < p_stream->i_entry_count; i++ )
1346 int64_t i_dts_deq = i_dts_q * I64C(1000000) / (int64_t)i_timescale;
1347 int64_t i_delta = p_stream->entry[i].i_length + i_dts - i_dts_deq;
1349 i_dts += p_stream->entry[i].i_length;
1351 p_stream->entry[i].i_length =
1352 i_delta * (int64_t)i_timescale / I64C(1000000);
1354 i_dts_q += p_stream->entry[i].i_length;
1356 /* then write encoded table */
1357 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
1360 int64_t i_delta = p_stream->entry[i].i_length;
1362 while( i < p_stream->i_entry_count )
1365 if( i >= p_stream->i_entry_count ||
1366 p_stream->entry[i].i_length != i_delta )
1372 bo_add_32be( stts, i - i_first ); // sample-count
1373 bo_add_32be( stts, i_delta ); // sample-delta
1375 bo_fix_32be( stts, 12, i_index );
1378 /* FIXME add ctts ?? FIXME */
1380 stsz = box_full_new( "stsz", 0, 0 );
1381 bo_add_32be( stsz, 0 ); // sample-size
1382 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
1383 for( i = 0; i < p_stream->i_entry_count; i++ )
1385 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
1389 /* create stss table */
1391 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i++ )
1393 if( p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I )
1397 stss = box_full_new( "stss", 0, 0 );
1398 bo_add_32be( stss, 0 ); /* fixed later */
1400 bo_add_32be( stss, 1 + i );
1406 bo_fix_32be( stss, 12, i_index );
1410 /* Now gather all boxes into stbl */
1411 box_gather( stbl, stsd );
1412 box_gather( stbl, stts );
1415 box_gather( stbl, stss );
1417 box_gather( stbl, stsc );
1418 box_gather( stbl, stsz );
1419 p_stream->i_stco_pos = stbl->i_buffer + 16;
1420 box_gather( stbl, stco );
1428 static int64_t get_timestamp();
1430 static uint32_t mvhd_matrix[9] =
1431 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1433 static bo_t *GetMoovBox( sout_mux_t *p_mux )
1435 sout_mux_sys_t *p_sys = p_mux->p_sys;
1440 uint32_t i_movie_timescale = 90000;
1441 int64_t i_movie_duration = 0;
1443 moov = box_new( "moov" );
1445 /* Create general info */
1446 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1448 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1449 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
1451 msg_Dbg( p_mux, "movie duration %ds",
1452 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
1454 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
1456 /* *** add /moov/mvhd *** */
1457 if( !p_sys->b_64_ext )
1459 mvhd = box_full_new( "mvhd", 0, 0 );
1460 bo_add_32be( mvhd, get_timestamp() ); // creation time
1461 bo_add_32be( mvhd, get_timestamp() ); // modification time
1462 bo_add_32be( mvhd, i_movie_timescale); // timescale
1463 bo_add_32be( mvhd, i_movie_duration ); // duration
1467 mvhd = box_full_new( "mvhd", 1, 0 );
1468 bo_add_64be( mvhd, get_timestamp() ); // creation time
1469 bo_add_64be( mvhd, get_timestamp() ); // modification time
1470 bo_add_32be( mvhd, i_movie_timescale); // timescale
1471 bo_add_64be( mvhd, i_movie_duration ); // duration
1473 bo_add_32be( mvhd, 0x10000 ); // rate
1474 bo_add_16be( mvhd, 0x100 ); // volume
1475 bo_add_16be( mvhd, 0 ); // reserved
1476 for( i = 0; i < 2; i++ )
1478 bo_add_32be( mvhd, 0 ); // reserved
1480 for( i = 0; i < 9; i++ )
1482 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
1484 for( i = 0; i < 6; i++ )
1486 bo_add_32be( mvhd, 0 ); // pre-defined
1489 /* Next available track id */
1490 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
1493 box_gather( moov, mvhd );
1495 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1497 mp4_stream_t *p_stream;
1498 uint32_t i_timescale;
1500 bo_t *trak, *tkhd, *edts, *elst, *mdia, *mdhd, *hdlr;
1501 bo_t *minf, *dinf, *dref, *url, *stbl;
1503 p_stream = p_sys->pp_streams[i_trak];
1505 if( p_stream->fmt.i_cat == AUDIO_ES )
1506 i_timescale = p_stream->fmt.audio.i_rate;
1510 /* *** add /moov/trak *** */
1511 trak = box_new( "trak" );
1513 /* *** add /moov/trak/tkhd *** */
1514 if( !p_sys->b_64_ext )
1517 tkhd = box_full_new( "tkhd", 0, 0x0f );
1519 tkhd = box_full_new( "tkhd", 0, 1 );
1521 bo_add_32be( tkhd, get_timestamp() ); // creation time
1522 bo_add_32be( tkhd, get_timestamp() ); // modification time
1523 bo_add_32be( tkhd, p_stream->i_track_id );
1524 bo_add_32be( tkhd, 0 ); // reserved 0
1525 bo_add_32be( tkhd, p_stream->i_duration *
1526 (int64_t)i_movie_timescale /
1527 (mtime_t)1000000 ); // duration
1532 tkhd = box_full_new( "tkhd", 1, 0x0f );
1534 tkhd = box_full_new( "tkhd", 1, 1 );
1536 bo_add_64be( tkhd, get_timestamp() ); // creation time
1537 bo_add_64be( tkhd, get_timestamp() ); // modification time
1538 bo_add_32be( tkhd, p_stream->i_track_id );
1539 bo_add_32be( tkhd, 0 ); // reserved 0
1540 bo_add_64be( tkhd, p_stream->i_duration *
1541 (int64_t)i_movie_timescale /
1542 (mtime_t)1000000 ); // duration
1545 for( i = 0; i < 2; i++ )
1547 bo_add_32be( tkhd, 0 ); // reserved
1549 bo_add_16be( tkhd, 0 ); // layer
1550 bo_add_16be( tkhd, 0 ); // pre-defined
1552 bo_add_16be( tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0 );
1553 bo_add_16be( tkhd, 0 ); // reserved
1554 for( i = 0; i < 9; i++ )
1556 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
1558 if( p_stream->fmt.i_cat == AUDIO_ES )
1560 bo_add_32be( tkhd, 0 ); // width (presentation)
1561 bo_add_32be( tkhd, 0 ); // height(presentation)
1563 else if( p_stream->fmt.i_cat == VIDEO_ES )
1565 int i_width = p_stream->fmt.video.i_width;
1566 if( p_stream->fmt.video.i_aspect > 0 )
1568 i_width = tkhd, p_stream->fmt.video.i_aspect *
1569 p_stream->fmt.video.i_height /
1570 VOUT_ASPECT_FACTOR << 16;
1572 // width (presentation)
1573 bo_add_32be( tkhd, i_width );
1574 // height(presentation)
1575 bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
1582 for( i = 0; i < p_sys->i_nb_streams; i++ )
1584 mp4_stream_t *tk = p_sys->pp_streams[i];
1585 if( tk->fmt.i_cat == VIDEO_ES )
1587 if( p_stream->fmt.video.i_aspect )
1588 i_width = p_stream->fmt.video.i_aspect *
1589 p_stream->fmt.video.i_height / VOUT_ASPECT_FACTOR;
1591 i_width = p_stream->fmt.video.i_width;
1592 i_height = p_stream->fmt.video.i_height;
1596 bo_add_32be( tkhd, i_width << 16 ); // width (presentation)
1597 bo_add_32be( tkhd, i_height << 16 ); // height(presentation)
1601 box_gather( trak, tkhd );
1603 /* *** add /moov/trak/edts and elst */
1604 edts = box_new( "edts" );
1605 elst = box_full_new( "elst", p_sys->b_64_ext ? 1 : 0, 0 );
1606 if( p_stream->i_dts_start > p_sys->i_dts_start )
1608 bo_add_32be( elst, 2 );
1610 if( p_sys->b_64_ext )
1612 bo_add_64be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1613 i_movie_timescale / I64C(1000000) );
1614 bo_add_64be( elst, -1 );
1618 bo_add_32be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1619 i_movie_timescale / I64C(1000000) );
1620 bo_add_32be( elst, -1 );
1622 bo_add_16be( elst, 1 );
1623 bo_add_16be( elst, 0 );
1627 bo_add_32be( elst, 1 );
1629 if( p_sys->b_64_ext )
1631 bo_add_64be( elst, p_stream->i_duration *
1632 i_movie_timescale / I64C(1000000) );
1633 bo_add_64be( elst, 0 );
1637 bo_add_32be( elst, p_stream->i_duration *
1638 i_movie_timescale / I64C(1000000) );
1639 bo_add_32be( elst, 0 );
1641 bo_add_16be( elst, 1 );
1642 bo_add_16be( elst, 0 );
1645 box_gather( edts, elst );
1647 box_gather( trak, edts );
1649 /* *** add /moov/trak/mdia *** */
1650 mdia = box_new( "mdia" );
1653 if( !p_sys->b_64_ext )
1655 mdhd = box_full_new( "mdhd", 0, 0 );
1656 bo_add_32be( mdhd, get_timestamp() ); // creation time
1657 bo_add_32be( mdhd, get_timestamp() ); // modification time
1658 bo_add_32be( mdhd, i_timescale); // timescale
1659 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1660 (mtime_t)1000000 ); // duration
1664 mdhd = box_full_new( "mdhd", 1, 0 );
1665 bo_add_64be( mdhd, get_timestamp() ); // creation time
1666 bo_add_64be( mdhd, get_timestamp() ); // modification time
1667 bo_add_32be( mdhd, i_timescale); // timescale
1668 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1669 (mtime_t)1000000 ); // duration
1672 if( p_stream->fmt.psz_language )
1674 char *psz = p_stream->fmt.psz_language;
1675 const iso639_lang_t *pl = NULL;
1676 uint16_t lang = 0x0;
1678 if( strlen( psz ) == 2 )
1680 pl = GetLang_1( psz );
1682 else if( strlen( psz ) == 3 )
1684 pl = GetLang_2B( psz );
1685 if( !strcmp( pl->psz_iso639_1, "??" ) )
1687 pl = GetLang_2T( psz );
1690 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1692 lang = ( ( pl->psz_iso639_2T[0] - 0x60 ) << 10 ) |
1693 ( ( pl->psz_iso639_2T[1] - 0x60 ) << 5 ) |
1694 ( ( pl->psz_iso639_2T[2] - 0x60 ) );
1696 bo_add_16be( mdhd, lang ); // language
1700 bo_add_16be( mdhd, 0 ); // language
1702 bo_add_16be( mdhd, 0 ); // predefined
1704 box_gather( mdia, mdhd );
1706 /* handler reference */
1707 hdlr = box_full_new( "hdlr", 0, 0 );
1709 bo_add_fourcc( hdlr, "mhlr" ); // media handler
1710 if( p_stream->fmt.i_cat == AUDIO_ES )
1712 bo_add_fourcc( hdlr, "soun" );
1714 else if( p_stream->fmt.i_cat == VIDEO_ES )
1716 bo_add_fourcc( hdlr, "vide" );
1718 else if( p_stream->fmt.i_cat == SPU_ES )
1720 bo_add_fourcc( hdlr, "text" );
1723 bo_add_32be( hdlr, 0 ); // reserved
1724 bo_add_32be( hdlr, 0 ); // reserved
1725 bo_add_32be( hdlr, 0 ); // reserved
1727 bo_add_8( hdlr, 12 );
1728 if( p_stream->fmt.i_cat == AUDIO_ES )
1729 bo_add_mem( hdlr, 12, "SoundHandler" );
1730 else if( p_stream->fmt.i_cat == VIDEO_ES )
1731 bo_add_mem( hdlr, 12, "VideoHandler" );
1733 bo_add_mem( hdlr, 12, "Text Handler" );
1736 box_gather( mdia, hdlr );
1739 minf = box_new( "minf" );
1742 if( p_stream->fmt.i_cat == AUDIO_ES )
1746 smhd = box_full_new( "smhd", 0, 0 );
1747 bo_add_16be( smhd, 0 ); // balance
1748 bo_add_16be( smhd, 0 ); // reserved
1751 box_gather( minf, smhd );
1753 else if( p_stream->fmt.i_cat == VIDEO_ES )
1757 vmhd = box_full_new( "vmhd", 0, 1 );
1758 bo_add_16be( vmhd, 0 ); // graphicsmode
1759 for( i = 0; i < 3; i++ )
1761 bo_add_16be( vmhd, 0 ); // opcolor
1765 box_gather( minf, vmhd );
1767 else if( p_stream->fmt.i_cat == SPU_ES )
1769 bo_t *gmhd = box_new( "gmhd" );
1770 bo_t *gmin = box_full_new( "gmin", 0, 1 );
1772 bo_add_16be( gmin, 0 ); // graphicsmode
1773 for( i = 0; i < 3; i++ )
1775 bo_add_16be( gmin, 0 ); // opcolor
1777 bo_add_16be( gmin, 0 ); // balance
1778 bo_add_16be( gmin, 0 ); // reserved
1781 box_gather( gmhd, gmin );
1784 box_gather( minf, gmhd );
1788 dinf = box_new( "dinf" );
1789 dref = box_full_new( "dref", 0, 0 );
1790 bo_add_32be( dref, 1 );
1791 url = box_full_new( "url ", 0, 0x01 );
1793 box_gather( dref, url );
1795 box_gather( dinf, dref );
1797 /* append dinf to mdia */
1799 box_gather( minf, dinf );
1802 stbl = GetStblBox( p_mux, p_stream );
1804 /* append stbl to minf */
1805 p_stream->i_stco_pos += minf->i_buffer;
1806 box_gather( minf, stbl );
1808 /* append minf to mdia */
1810 p_stream->i_stco_pos += mdia->i_buffer;
1811 box_gather( mdia, minf );
1813 /* append mdia to trak */
1815 p_stream->i_stco_pos += trak->i_buffer;
1816 box_gather( trak, mdia );
1818 /* append trak to moov */
1820 p_stream->i_stco_pos += moov->i_buffer;
1821 box_gather( moov, trak );
1824 /* Add user data tags */
1825 box_gather( moov, GetUdtaTag( p_mux ) );
1831 /****************************************************************************/
1833 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1838 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1839 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1843 p_bo->i_buffer_size = i_size;
1844 p_bo->p_buffer = p_buffer;
1847 p_bo->b_grow = b_grow;
1851 static void bo_add_8( bo_t *p_bo, uint8_t i )
1853 if( p_bo->i_buffer < p_bo->i_buffer_size )
1855 p_bo->p_buffer[p_bo->i_buffer] = i;
1857 else if( p_bo->b_grow )
1859 p_bo->i_buffer_size += 1024;
1860 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1862 p_bo->p_buffer[p_bo->i_buffer] = i;
1868 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1870 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1871 bo_add_8( p_bo, i &0xff );
1874 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1876 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1877 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1878 bo_add_8( p_bo, ( i &0xff ) );
1880 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1882 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1883 bo_add_16be( p_bo, i &0xffff );
1886 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1888 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1889 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1890 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1891 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1894 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1896 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1897 bo_add_32be( p_bo, i &0xffffffff );
1900 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1902 bo_add_8( p_bo, fcc[0] );
1903 bo_add_8( p_bo, fcc[1] );
1904 bo_add_8( p_bo, fcc[2] );
1905 bo_add_8( p_bo, fcc[3] );
1908 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1912 for( i = 0; i < i_size; i++ )
1914 bo_add_8( p_bo, p_mem[i] );
1918 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
1924 vals[3] = (unsigned char)(i_length & 0x7f);
1926 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
1928 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
1930 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
1932 bo_add_8( p_bo, tag );
1934 if( i_size < 0x00000080 )
1936 bo_add_8( p_bo, vals[3] );
1938 else if( i_size < 0x00004000 )
1940 bo_add_8( p_bo, vals[2] );
1941 bo_add_8( p_bo, vals[3] );
1943 else if( i_size < 0x00200000 )
1945 bo_add_8( p_bo, vals[1] );
1946 bo_add_8( p_bo, vals[2] );
1947 bo_add_8( p_bo, vals[3] );
1949 else if( i_size < 0x10000000 )
1951 bo_add_8( p_bo, vals[0] );
1952 bo_add_8( p_bo, vals[1] );
1953 bo_add_8( p_bo, vals[2] );
1954 bo_add_8( p_bo, vals[3] );
1958 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
1962 for( i = 0; i < p_bo2->i_buffer; i++ )
1964 bo_add_8( p_bo, p_bo2->p_buffer[i] );
1968 static bo_t * box_new( char *fcc )
1972 if( ( box = malloc( sizeof( bo_t ) ) ) )
1974 bo_init( box, 0, NULL, VLC_TRUE );
1976 bo_add_32be ( box, 0 );
1977 bo_add_fourcc( box, fcc );
1983 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
1987 if( ( box = malloc( sizeof( bo_t ) ) ) )
1989 bo_init( box, 0, NULL, VLC_TRUE );
1991 bo_add_32be ( box, 0 );
1992 bo_add_fourcc( box, fcc );
1993 bo_add_8 ( box, v );
1994 bo_add_24be ( box, f );
2000 static void box_fix( bo_t *box )
2004 memcpy( &box_tmp, box, sizeof( bo_t ) );
2006 box_tmp.i_buffer = 0;
2007 bo_add_32be( &box_tmp, box->i_buffer );
2010 static void box_free( bo_t *box )
2014 free( box->p_buffer );
2020 static void box_gather ( bo_t *box, bo_t *box2 )
2022 bo_add_bo( box, box2 );
2026 static block_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
2030 p_buf = block_New( p_sout, box->i_buffer );
2031 if( box->i_buffer > 0 )
2033 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
2039 static void box_send( sout_mux_t *p_mux, bo_t *box )
2043 p_buf = bo_to_sout( p_mux->p_sout, box );
2046 sout_AccessOutWrite( p_mux->p_access, p_buf );
2049 static int64_t get_timestamp()
2051 int64_t i_timestamp = 0;
2054 i_timestamp = time(NULL);
2055 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2056 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60