1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003, 2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
40 #include <vlc_iso_lang.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 #define FASTSTART_TEXT N_("Create \"Fast Start\" files")
47 #define FASTSTART_LONGTEXT N_( \
48 "Create \"Fast Start\" files. " \
49 "\"Fast Start\" files are optimized for downloads and allow the user " \
50 "to start previewing the file while it is downloading.")
52 static int Open ( vlc_object_t * );
53 static void Close ( vlc_object_t * );
55 #define SOUT_CFG_PREFIX "sout-mp4-"
58 set_description( N_("MP4/MOV muxer") )
59 set_category( CAT_SOUT )
60 set_subcategory( SUBCAT_SOUT_MUX )
61 set_shortname( "MP4" )
63 add_bool( SOUT_CFG_PREFIX "faststart", true, NULL,
64 FASTSTART_TEXT, FASTSTART_LONGTEXT,
66 set_capability( "sout mux", 5 )
67 add_shortcut( "mp4", "mov", "3gp" )
68 set_callbacks( Open, Close )
71 /*****************************************************************************
73 *****************************************************************************/
74 static const char *const ppsz_sout_options[] = {
78 static int Control( sout_mux_t *, int, va_list );
79 static int AddStream( sout_mux_t *, sout_input_t * );
80 static int DelStream( sout_mux_t *, sout_input_t * );
81 static int Mux ( sout_mux_t * );
83 /*****************************************************************************
85 *****************************************************************************/
103 unsigned int i_entry_count;
104 unsigned int i_entry_max;
106 int64_t i_length_neg;
112 /* for later stco fix-up (fast start files) */
121 struct sout_mux_sys_t
134 mp4_stream_t **pp_streams;
147 static void bo_init ( bo_t *, int , uint8_t *, bool );
148 static void bo_add_8 ( bo_t *, uint8_t );
149 static void bo_add_16be ( bo_t *, uint16_t );
150 static void bo_add_24be ( bo_t *, uint32_t );
151 static void bo_add_32be ( bo_t *, uint32_t );
152 static void bo_add_64be ( bo_t *, uint64_t );
153 static void bo_add_fourcc(bo_t *, const char * );
154 static void bo_add_bo ( bo_t *, bo_t * );
155 static void bo_add_mem ( bo_t *, int , uint8_t * );
156 static void bo_add_descr( bo_t *, uint8_t , uint32_t );
158 static void bo_fix_32be ( bo_t *, int , uint32_t );
160 static bo_t *box_new ( const char *fcc );
161 static bo_t *box_full_new( const char *fcc, uint8_t v, uint32_t f );
162 static void box_fix ( bo_t *box );
163 static void box_free ( bo_t *box );
164 static void box_gather ( bo_t *box, bo_t *box2 );
166 static void box_send( sout_mux_t *p_mux, bo_t *box );
168 static block_t *bo_to_sout( sout_instance_t *p_sout, bo_t *box );
170 static bo_t *GetMoovBox( sout_mux_t *p_mux );
172 static block_t *ConvertSUBT( block_t *);
173 static block_t *ConvertAVC1( block_t * );
175 /*****************************************************************************
177 *****************************************************************************/
178 static int Open( vlc_object_t *p_this )
180 sout_mux_t *p_mux = (sout_mux_t*)p_this;
181 sout_mux_sys_t *p_sys;
184 msg_Dbg( p_mux, "Mp4 muxer opened" );
185 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
187 p_mux->pf_control = Control;
188 p_mux->pf_addstream = AddStream;
189 p_mux->pf_delstream = DelStream;
191 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
195 p_sys->i_nb_streams = 0;
196 p_sys->pp_streams = NULL;
197 p_sys->i_mdat_pos = 0;
198 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
199 p_sys->b_3gp = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "3gp" );
200 p_sys->i_dts_start = 0;
205 /* Now add ftyp header */
206 box = box_new( "ftyp" );
207 if( p_sys->b_3gp ) bo_add_fourcc( box, "3gp4" );
208 else bo_add_fourcc( box, "isom" );
209 bo_add_32be ( box, 0 );
210 if( p_sys->b_3gp ) bo_add_fourcc( box, "3gp4" );
211 else bo_add_fourcc( box, "mp41" );
214 p_sys->i_pos += box->i_buffer;
215 p_sys->i_mdat_pos = p_sys->i_pos;
217 box_send( p_mux, box );
221 * Quicktime actually doesn't like the 64 bits extensions !!! */
222 p_sys->b_64_ext = false;
224 /* Now add mdat header */
225 box = box_new( "mdat" );
226 bo_add_64be ( box, 0 ); // enough to store an extended size
228 p_sys->i_pos += box->i_buffer;
230 box_send( p_mux, box );
235 /*****************************************************************************
237 *****************************************************************************/
238 static void Close( vlc_object_t * p_this )
240 sout_mux_t *p_mux = (sout_mux_t*)p_this;
241 sout_mux_sys_t *p_sys = p_mux->p_sys;
249 msg_Dbg( p_mux, "Close" );
251 /* Update mdat size */
252 bo_init( &bo, 0, NULL, true );
253 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
256 bo_add_32be ( &bo, 1 );
257 bo_add_fourcc( &bo, "mdat" );
258 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
262 bo_add_32be ( &bo, 8 );
263 bo_add_fourcc( &bo, "wide" );
264 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
265 bo_add_fourcc( &bo, "mdat" );
267 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
270 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
271 sout_AccessOutWrite( p_mux->p_access, p_hdr );
273 /* Create MOOV header */
274 i_moov_pos = p_sys->i_pos;
275 moov = GetMoovBox( p_mux );
277 /* Check we need to create "fast start" files */
278 var_Get( p_this, SOUT_CFG_PREFIX "faststart", &val );
279 p_sys->b_fast_start = val.b_bool;
280 while( p_sys->b_fast_start )
282 /* Move data to the end of the file so we can fit the moov header
285 int64_t i_chunk, i_size = p_sys->i_pos - p_sys->i_mdat_pos;
286 int i_moov_size = moov->i_buffer;
290 i_chunk = __MIN( 32768, i_size );
291 p_buf = block_New( p_mux, i_chunk );
292 sout_AccessOutSeek( p_mux->p_access,
293 p_sys->i_mdat_pos + i_size - i_chunk );
294 if( sout_AccessOutRead( p_mux->p_access, p_buf ) < i_chunk )
296 msg_Warn( p_this, "read() not supported by access output, "
297 "won't create a fast start file" );
298 p_sys->b_fast_start = false;
299 block_Release( p_buf );
302 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos + i_size +
303 i_moov_size - i_chunk );
304 sout_AccessOutWrite( p_mux->p_access, p_buf );
308 if( !p_sys->b_fast_start ) break;
310 /* Fix-up samples to chunks table in MOOV header */
311 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
313 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
317 moov->i_buffer = p_stream->i_stco_pos;
318 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
320 if( p_stream->b_stco64 )
321 bo_add_64be( moov, p_stream->entry[i].i_pos + i_moov_size);
323 bo_add_32be( moov, p_stream->entry[i].i_pos + i_moov_size);
325 while( i < p_stream->i_entry_count )
327 if( i + 1 < p_stream->i_entry_count &&
328 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
329 != p_stream->entry[i + 1].i_pos )
340 moov->i_buffer = i_moov_size;
341 i_moov_pos = p_sys->i_mdat_pos;
342 p_sys->b_fast_start = false;
345 /* Write MOOV header */
346 sout_AccessOutSeek( p_mux->p_access, i_moov_pos );
347 box_send( p_mux, moov );
350 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
352 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
354 es_format_Clean( &p_stream->fmt );
355 free( p_stream->entry );
358 if( p_sys->i_nb_streams ) free( p_sys->pp_streams );
362 /*****************************************************************************
364 *****************************************************************************/
365 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
372 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
373 pb_bool = (bool*)va_arg( args, bool * );
377 case MUX_GET_ADD_STREAM_WAIT:
378 pb_bool = (bool*)va_arg( args, bool * );
382 case MUX_GET_MIME: /* Not needed, as not streamable */
388 /*****************************************************************************
390 *****************************************************************************/
391 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
393 sout_mux_sys_t *p_sys = p_mux->p_sys;
394 mp4_stream_t *p_stream;
396 switch( p_input->p_fmt->i_codec )
403 case VLC_CODEC_MJPGB:
408 case VLC_CODEC_AMR_NB:
409 case VLC_CODEC_AMR_WB:
414 msg_Warn( p_mux, "subtitle track added like in .mov (even when creating .mp4)" );
417 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
418 (char*)&p_input->p_fmt->i_codec );
422 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;
435 p_input->p_sys = p_stream;
437 msg_Dbg( p_mux, "adding input" );
439 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
443 /*****************************************************************************
445 *****************************************************************************/
446 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
449 msg_Dbg( p_mux, "removing input" );
453 /*****************************************************************************
455 *****************************************************************************/
456 static int Mux( sout_mux_t *p_mux )
458 sout_mux_sys_t *p_sys = p_mux->p_sys;
462 sout_input_t *p_input;
463 mp4_stream_t *p_stream;
467 int i_stream = sout_MuxGetStream( p_mux, 2, &i_dts);
470 return( VLC_SUCCESS );
473 p_input = p_mux->pp_inputs[i_stream];
474 p_stream = (mp4_stream_t*)p_input->p_sys;
477 p_data = block_FifoGet( p_input->p_fifo );
478 if( p_stream->fmt.i_codec == VLC_CODEC_H264 )
480 p_data = ConvertAVC1( p_data );
482 else if( p_stream->fmt.i_codec == VLC_CODEC_SUBT )
484 p_data = ConvertSUBT( p_data );
486 if( p_data == NULL ) goto again;
488 if( p_stream->fmt.i_cat != SPU_ES )
490 /* Fix length of the sample */
491 if( block_FifoCount( p_input->p_fifo ) > 0 )
493 block_t *p_next = block_FifoShow( p_input->p_fifo );
494 int64_t i_diff = p_next->i_dts - p_data->i_dts;
496 if( i_diff < INT64_C(1000000 ) ) /* protection */
498 p_data->i_length = i_diff;
501 if( p_data->i_length <= 0 )
503 msg_Warn( p_mux, "i_length <= 0" );
504 p_stream->i_length_neg += p_data->i_length - 1;
505 p_data->i_length = 1;
507 else if( p_stream->i_length_neg < 0 )
509 int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
511 p_data->i_length -= i_recover;
512 p_stream->i_length_neg += i_recover;
516 /* Save starting time */
517 if( p_stream->i_entry_count == 0 )
519 p_stream->i_dts_start = p_data->i_dts;
521 /* Update global dts_start */
522 if( p_sys->i_dts_start <= 0 ||
523 p_stream->i_dts_start < p_sys->i_dts_start )
525 p_sys->i_dts_start = p_stream->i_dts_start;
529 if( p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0 )
531 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
535 /* FIXME handle this broken case */
540 if( p_stream->entry[p_stream->i_entry_count-1].i_length <= 0 )
542 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
547 /* add index entry */
548 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
549 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_buffer;
550 p_stream->entry[p_stream->i_entry_count].i_pts_dts=
551 __MAX( p_data->i_pts - p_data->i_dts, 0 );
552 p_stream->entry[p_stream->i_entry_count].i_length = p_data->i_length;
553 p_stream->entry[p_stream->i_entry_count].i_flags = p_data->i_flags;
555 p_stream->i_entry_count++;
556 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
557 if( p_stream->i_entry_count >= p_stream->i_entry_max - 1 )
559 p_stream->i_entry_max += 1000;
560 p_stream->entry = xrealloc( p_stream->entry,
561 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
565 p_stream->i_duration += p_data->i_length;
566 p_sys->i_pos += p_data->i_buffer;
569 p_stream->i_last_dts = p_data->i_dts;
572 sout_AccessOutWrite( p_mux->p_access, p_data );
574 if( p_stream->fmt.i_cat == SPU_ES )
576 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
581 msg_Dbg( p_mux, "writing an empty sub" ) ;
583 /* Append a idx entry */
584 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
585 p_stream->entry[p_stream->i_entry_count].i_size = 3;
586 p_stream->entry[p_stream->i_entry_count].i_pts_dts= 0;
587 p_stream->entry[p_stream->i_entry_count].i_length = 0;
588 p_stream->entry[p_stream->i_entry_count].i_flags = 0;
590 /* XXX: No need to grow the entry here */
591 p_stream->i_entry_count++;
594 p_stream->i_last_dts += i_length;
597 p_data = block_New( p_mux, 3 );
598 p_data->p_buffer[0] = 0;
599 p_data->p_buffer[1] = 1;
600 p_data->p_buffer[2] = ' ';
602 p_sys->i_pos += p_data->i_buffer;
604 sout_AccessOutWrite( p_mux->p_access, p_data );
608 p_stream->i_duration = p_stream->i_last_dts - p_stream->i_dts_start;
612 return( VLC_SUCCESS );
615 /*****************************************************************************
617 *****************************************************************************/
618 static block_t *ConvertSUBT( block_t *p_block )
620 p_block = block_Realloc( p_block, 2, p_block->i_buffer );
622 /* No trailling '\0' */
623 if( p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0' )
626 p_block->p_buffer[0] = ( (p_block->i_buffer - 2) >> 8 )&0xff;
627 p_block->p_buffer[1] = ( (p_block->i_buffer - 2) )&0xff;
632 static block_t *ConvertAVC1( block_t *p_block )
634 uint8_t *last = p_block->p_buffer; /* Assume it starts with 0x00000001 */
635 uint8_t *dat = &p_block->p_buffer[4];
636 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
639 /* Replace the 4 bytes start code with 4 bytes size,
640 * FIXME are all startcodes 4 bytes ? (I don't think :( */
645 while( dat < end - 4 )
647 if( dat[0] == 0x00 && dat[1] == 0x00 &&
648 dat[2] == 0x00 && dat[3] == 0x01 )
660 i_size = dat - &last[4];
661 last[0] = ( i_size >> 24 )&0xff;
662 last[1] = ( i_size >> 16 )&0xff;
663 last[2] = ( i_size >> 8 )&0xff;
664 last[3] = ( i_size )&0xff;
666 /* Skip blocks with SPS/PPS */
667 if( (last[4]&0x1f) == 7 || (last[4]&0x1f) == 8 )
669 ; // FIXME Find a way to skip dat without frelling everything
677 static int GetDescrLength( int i_size )
679 if( i_size < 0x00000080 )
681 else if( i_size < 0x00004000 )
683 else if( i_size < 0x00200000 )
689 static bo_t *GetESDS( mp4_stream_t *p_stream )
693 int i_object_type_indication;
694 int i_decoder_specific_info_size;
696 int64_t i_bitrate_avg = 0;
697 int64_t i_bitrate_max = 0;
699 /* Compute avg/max bitrate */
700 for( i = 0; i < p_stream->i_entry_count; i++ )
702 i_bitrate_avg += p_stream->entry[i].i_size;
703 if( p_stream->entry[i].i_length > 0)
705 int64_t i_bitrate = INT64_C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
706 if( i_bitrate > i_bitrate_max )
707 i_bitrate_max = i_bitrate;
711 if( p_stream->i_duration > 0 )
712 i_bitrate_avg = INT64_C(8000000) * i_bitrate_avg / p_stream->i_duration;
715 if( i_bitrate_max <= 1 )
716 i_bitrate_max = 0x7fffffff;
719 if( p_stream->fmt.i_extra > 0 )
721 i_decoder_specific_info_size =
722 GetDescrLength( p_stream->fmt.i_extra );
726 i_decoder_specific_info_size = 0;
729 esds = box_full_new( "esds", 0, 0 );
732 bo_add_descr( esds, 0x03, 3 +
733 GetDescrLength( 13 + i_decoder_specific_info_size ) +
734 GetDescrLength( 1 ) );
735 bo_add_16be( esds, p_stream->i_track_id );
736 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
738 /* DecoderConfigDescr */
739 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
741 switch( p_stream->fmt.i_codec )
744 i_object_type_indication = 0x20;
747 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
748 i_object_type_indication = 0x60;
751 /* FIXME for mpeg2-aac == 0x66->0x68 */
752 i_object_type_indication = 0x40;
755 i_object_type_indication =
756 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
759 i_object_type_indication = 0x00;
762 i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
764 bo_add_8 ( esds, i_object_type_indication );
765 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
766 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
767 bo_add_32be( esds, i_bitrate_max ); // maxBitrate
768 bo_add_32be( esds, i_bitrate_avg ); // avgBitrate
770 if( p_stream->fmt.i_extra > 0 )
774 /* DecoderSpecificInfo */
775 bo_add_descr( esds, 0x05, p_stream->fmt.i_extra );
777 for( i = 0; i < p_stream->fmt.i_extra; i++ )
779 bo_add_8( esds, ((uint8_t*)p_stream->fmt.p_extra)[i] );
783 /* SL_Descr mandatory */
784 bo_add_descr( esds, 0x06, 1 );
785 bo_add_8 ( esds, 0x02 ); // sl_predefined
792 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
797 wave = box_new( "wave" );
799 box = box_new( "frma" );
800 bo_add_fourcc( box, "mp4a" );
802 box_gather( wave, box );
804 box = box_new( "mp4a" );
805 bo_add_32be( box, 0 );
807 box_gather( wave, box );
809 box = GetESDS( p_stream );
811 box_gather( wave, box );
813 box = box_new( "srcq" );
814 bo_add_32be( box, 0x40 );
816 box_gather( wave, box );
819 bo_add_32be( wave, 8 ); /* new empty box */
820 bo_add_32be( wave, 0 ); /* box label */
827 static bo_t *GetDamrTag( mp4_stream_t *p_stream )
831 damr = box_new( "damr" );
833 bo_add_fourcc( damr, "REFC" );
836 if( p_stream->fmt.i_codec == VLC_CODEC_AMR_NB )
837 bo_add_16be( damr, 0x81ff ); /* Mode set (all modes for AMR_NB) */
839 bo_add_16be( damr, 0x83ff ); /* Mode set (all modes for AMR_WB) */
840 bo_add_16be( damr, 0x1 ); /* Mode change period (no restriction) */
847 static bo_t *GetD263Tag( void )
851 d263 = box_new( "d263" );
853 bo_add_fourcc( d263, "VLC " );
854 bo_add_16be( d263, 0xa );
862 static bo_t *GetAvcCTag( mp4_stream_t *p_stream )
865 uint8_t *p_sps = NULL;
866 uint8_t *p_pps = NULL;
870 if( p_stream->fmt.i_extra > 0 )
872 /* FIXME: take into account multiple sps/pps */
873 uint8_t *p_buffer = p_stream->fmt.p_extra;
874 int i_buffer = p_stream->fmt.i_extra;
876 while( i_buffer > 3 )
879 while( p_buffer[0] != 0 || p_buffer[1] != 0 ||
885 const int i_nal_type = p_buffer[3]&0x1f;
891 for( i_offset = 1; i_offset+2 < i_buffer ; i_offset++)
893 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 &&
894 p_buffer[i_offset+2] == 1 )
896 /* we found another startcode */
897 i_startcode = i_offset;
898 while( p_buffer[i_startcode-1] == 0 && i_startcode > 0 )
903 i_size = i_startcode ? i_startcode : i_buffer;
905 if( i_nal_type == 7 )
907 p_sps = &p_buffer[3];
908 i_sps_size = i_size - 3;
910 if( i_nal_type == 8 )
912 p_pps = &p_buffer[3];
913 i_pps_size = i_size - 3;
920 /* FIXME use better value */
921 avcC = box_new( "avcC" );
922 bo_add_8( avcC, 1 ); /* configuration version */
923 bo_add_8( avcC, i_sps_size ? p_sps[1] : 77 );
924 bo_add_8( avcC, i_sps_size ? p_sps[2] : 64 );
925 bo_add_8( avcC, i_sps_size ? p_sps[3] : 30 ); /* level, 5.1 */
926 bo_add_8( avcC, 0xff ); /* 0b11111100 | lengthsize = 0x11 */
928 bo_add_8( avcC, 0xe0 | (i_sps_size > 0 ? 1 : 0) ); /* 0b11100000 | sps_count */
931 bo_add_16be( avcC, i_sps_size );
932 bo_add_mem( avcC, i_sps_size, p_sps );
935 bo_add_8( avcC, (i_pps_size > 0 ? 1 : 0) ); /* pps_count */
938 bo_add_16be( avcC, i_pps_size );
939 bo_add_mem( avcC, i_pps_size, p_pps );
946 /* TODO: No idea about these values */
947 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
949 bo_t *smi = box_new( "SMI " );
951 if( p_stream->fmt.i_extra > 0x4e )
953 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
954 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
956 while( p + 8 < p_end )
958 int i_size = GetDWBE( p );
961 /* FIXME handle 1 as long size */
964 if( !strncmp( (const char *)&p[4], "SMI ", 4 ) )
966 bo_add_mem( smi, p_end - p - 8, &p[8] );
973 /* Create a dummy one in fallback */
974 bo_add_fourcc( smi, "SEQH" );
975 bo_add_32be( smi, 0x5 );
976 bo_add_32be( smi, 0xe2c0211d );
977 bo_add_8( smi, 0xc0 );
983 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
985 sout_mux_sys_t *p_sys = p_mux->p_sys;
986 bo_t *udta = box_new( "udta" );
987 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
991 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
993 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
995 if( p_stream->fmt.i_codec == VLC_CODEC_MP4V ||
996 p_stream->fmt.i_codec == VLC_CODEC_MP4A )
998 bo_t *box = box_new( "\251req" );
1000 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
1001 bo_add_16be( box, 0 );
1002 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
1003 (uint8_t *)"QuickTime 6.0 or greater" );
1005 box_gather( udta, box );
1012 bo_t *box = box_new( "\251enc" );
1014 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
1015 bo_add_16be( box, 0 );
1016 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
1017 (uint8_t*)PACKAGE_STRING " stream output" );
1019 box_gather( udta, box );
1025 #define ADD_META_BOX( type, box_string ) { \
1027 if( vlc_meta_Get( p_meta, vlc_meta_##type ) ) box = box_new( "\251" box_string ); \
1030 bo_add_16be( box, strlen( vlc_meta_Get( p_meta, vlc_meta_##type ) )); \
1031 bo_add_16be( box, 0 ); \
1032 bo_add_mem( box, strlen( vlc_meta_Get( p_meta, vlc_meta_##type ) ), \
1033 (uint8_t*)(vlc_meta_Get( p_meta, vlc_meta_##type ) ) ); \
1035 box_gather( udta, box ); \
1038 ADD_META_BOX( Title, "nam" );
1039 ADD_META_BOX( Artist, "ART" );
1040 ADD_META_BOX( Genre, "gen" );
1041 ADD_META_BOX( Copyright, "cpy" );
1042 ADD_META_BOX( Description, "des" );
1043 ADD_META_BOX( Date, "day" );
1044 ADD_META_BOX( URL, "url" );
1052 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1054 sout_mux_sys_t *p_sys = p_mux->p_sys;
1055 bool b_descr = false;
1060 switch( p_stream->fmt.i_codec )
1062 case VLC_CODEC_MP4A:
1063 memcpy( fcc, "mp4a", 4 );
1067 case VLC_CODEC_AMR_NB:
1068 case VLC_CODEC_AMR_WB:
1069 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1073 case VLC_CODEC_MPGA:
1075 memcpy( fcc, ".mp3", 4 );
1078 memcpy( fcc, "mp4a", 4 );
1084 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1088 soun = box_new( fcc );
1089 for( i = 0; i < 6; i++ )
1091 bo_add_8( soun, 0 ); // reserved;
1093 bo_add_16be( soun, 1 ); // data-reference-index
1095 /* SoundDescription */
1097 p_stream->fmt.i_codec == VLC_CODEC_MP4A )
1099 bo_add_16be( soun, 1 ); // version 1;
1103 bo_add_16be( soun, 0 ); // version 0;
1105 bo_add_16be( soun, 0 ); // revision level (0)
1106 bo_add_32be( soun, 0 ); // vendor
1108 bo_add_16be( soun, p_stream->fmt.audio.i_channels );
1110 bo_add_16be( soun, p_stream->fmt.audio.i_bitspersample ?
1111 p_stream->fmt.audio.i_bitspersample : 16 );
1112 bo_add_16be( soun, -2 ); // compression id
1113 bo_add_16be( soun, 0 ); // packet size (0)
1114 bo_add_16be( soun, p_stream->fmt.audio.i_rate ); // sampleratehi
1115 bo_add_16be( soun, 0 ); // sampleratelo
1117 /* Extended data for SoundDescription V1 */
1119 p_stream->fmt.i_codec == VLC_CODEC_MP4A )
1121 /* samples per packet */
1122 bo_add_32be( soun, p_stream->fmt.audio.i_frame_length );
1123 bo_add_32be( soun, 1536 ); /* bytes per packet */
1124 bo_add_32be( soun, 2 ); /* bytes per frame */
1125 /* bytes per sample */
1126 bo_add_32be( soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1129 /* Add an ES Descriptor */
1135 p_stream->fmt.i_codec == VLC_CODEC_MP4A )
1137 box = GetWaveTag( p_stream );
1139 else if( p_stream->fmt.i_codec == VLC_CODEC_AMR_NB )
1141 box = GetDamrTag( p_stream );
1145 box = GetESDS( p_stream );
1148 box_gather( soun, box );
1156 static bo_t *GetVideBox( mp4_stream_t *p_stream )
1163 switch( p_stream->fmt.i_codec )
1165 case VLC_CODEC_MP4V:
1166 case VLC_CODEC_MPGV:
1167 memcpy( fcc, "mp4v", 4 );
1170 case VLC_CODEC_MJPG:
1171 memcpy( fcc, "mjpa", 4 );
1174 case VLC_CODEC_SVQ1:
1175 memcpy( fcc, "SVQ1", 4 );
1178 case VLC_CODEC_SVQ3:
1179 memcpy( fcc, "SVQ3", 4 );
1182 case VLC_CODEC_H263:
1183 memcpy( fcc, "s263", 4 );
1186 case VLC_CODEC_H264:
1187 memcpy( fcc, "avc1", 4 );
1190 case VLC_CODEC_YV12:
1191 memcpy( fcc, "yv12", 4 );
1194 case VLC_CODEC_YUYV:
1195 memcpy( fcc, "yuy2", 4 );
1199 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1203 vide = box_new( fcc );
1204 for( i = 0; i < 6; i++ )
1206 bo_add_8( vide, 0 ); // reserved;
1208 bo_add_16be( vide, 1 ); // data-reference-index
1210 bo_add_16be( vide, 0 ); // predefined;
1211 bo_add_16be( vide, 0 ); // reserved;
1212 for( i = 0; i < 3; i++ )
1214 bo_add_32be( vide, 0 ); // predefined;
1217 bo_add_16be( vide, p_stream->fmt.video.i_width ); // i_width
1218 bo_add_16be( vide, p_stream->fmt.video.i_height ); // i_height
1220 bo_add_32be( vide, 0x00480000 ); // h 72dpi
1221 bo_add_32be( vide, 0x00480000 ); // v 72dpi
1223 bo_add_32be( vide, 0 ); // data size, always 0
1224 bo_add_16be( vide, 1 ); // frames count per sample
1227 for( i = 0; i < 32; i++ )
1229 bo_add_8( vide, 0 );
1232 bo_add_16be( vide, 0x18 ); // depth
1233 bo_add_16be( vide, 0xffff ); // predefined
1235 /* add an ES Descriptor */
1236 switch( p_stream->fmt.i_codec )
1238 case VLC_CODEC_MP4V:
1239 case VLC_CODEC_MPGV:
1241 bo_t *esds = GetESDS( p_stream );
1244 box_gather( vide, esds );
1248 case VLC_CODEC_H263:
1250 bo_t *d263 = GetD263Tag();
1253 box_gather( vide, d263 );
1257 case VLC_CODEC_SVQ3:
1259 bo_t *esds = GetSVQ3Tag( p_stream );
1262 box_gather( vide, esds );
1266 case VLC_CODEC_H264:
1267 box_gather( vide, GetAvcCTag( p_stream ) );
1279 static bo_t *GetTextBox( void )
1282 bo_t *text = box_new( "text" );
1285 for( i = 0; i < 6; i++ )
1287 bo_add_8( text, 0 ); // reserved;
1289 bo_add_16be( text, 1 ); // data-reference-index
1291 bo_add_32be( text, 0 ); // display flags
1292 bo_add_32be( text, 0 ); // justification
1293 for( i = 0; i < 3; i++ )
1295 bo_add_16be( text, 0 ); // back ground color
1298 bo_add_16be( text, 0 ); // box text
1299 bo_add_16be( text, 0 ); // box text
1300 bo_add_16be( text, 0 ); // box text
1301 bo_add_16be( text, 0 ); // box text
1303 bo_add_64be( text, 0 ); // reserved
1304 for( i = 0; i < 3; i++ )
1306 bo_add_16be( text, 0xff ); // foreground color
1309 bo_add_8 ( text, 9 );
1310 bo_add_mem( text, 9, (uint8_t*)"Helvetica" );
1317 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1319 sout_mux_sys_t *p_sys = p_mux->p_sys;
1320 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
1321 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz, *stss;
1322 uint32_t i_timescale;
1323 int64_t i_dts, i_dts_q;
1325 stbl = box_new( "stbl" );
1327 /* sample description */
1328 stsd = box_full_new( "stsd", 0, 0 );
1329 bo_add_32be( stsd, 1 );
1330 if( p_stream->fmt.i_cat == AUDIO_ES )
1332 bo_t *soun = GetSounBox( p_mux, p_stream );
1333 box_gather( stsd, soun );
1335 else if( p_stream->fmt.i_cat == VIDEO_ES )
1337 bo_t *vide = GetVideBox( p_stream );
1338 box_gather( stsd, vide );
1340 else if( p_stream->fmt.i_cat == SPU_ES )
1342 box_gather( stsd, GetTextBox() );
1346 /* chunk offset table */
1347 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
1349 /* 64 bits version */
1350 p_stream->b_stco64 = true;
1351 stco = box_full_new( "co64", 0, 0 );
1355 /* 32 bits version */
1356 p_stream->b_stco64 = false;
1357 stco = box_full_new( "stco", 0, 0 );
1359 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
1361 /* sample to chunk table */
1362 stsc = box_full_new( "stsc", 0, 0 );
1363 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
1365 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
1366 i < p_stream->i_entry_count; i_chunk++ )
1370 if( p_stream->b_stco64 )
1371 bo_add_64be( stco, p_stream->entry[i].i_pos );
1373 bo_add_32be( stco, p_stream->entry[i].i_pos );
1375 while( i < p_stream->i_entry_count )
1377 if( i + 1 < p_stream->i_entry_count &&
1378 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
1379 != p_stream->entry[i + 1].i_pos )
1388 /* Add entry to the stsc table */
1389 if( i_stsc_last_val != i - i_first )
1391 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
1392 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
1393 bo_add_32be( stsc, 1 ); // sample-descr-index
1394 i_stsc_last_val = i - i_first;
1399 /* Fix stco entry count */
1400 bo_fix_32be( stco, 12, i_chunk );
1401 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
1404 /* Fix stsc entry count */
1405 bo_fix_32be( stsc, 12, i_stsc_entries );
1409 stts = box_full_new( "stts", 0, 0 );
1410 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
1412 if( p_stream->fmt.i_cat == AUDIO_ES )
1413 i_timescale = p_stream->fmt.audio.i_rate;
1417 /* first, create quantified length */
1418 for( i = 0, i_dts = 0, i_dts_q = 0; i < p_stream->i_entry_count; i++ )
1420 int64_t i_dts_deq = i_dts_q * INT64_C(1000000) / (int64_t)i_timescale;
1421 int64_t i_delta = p_stream->entry[i].i_length + i_dts - i_dts_deq;
1423 i_dts += p_stream->entry[i].i_length;
1425 p_stream->entry[i].i_length =
1426 i_delta * (int64_t)i_timescale / INT64_C(1000000);
1428 i_dts_q += p_stream->entry[i].i_length;
1430 /* then write encoded table */
1431 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
1434 int64_t i_delta = p_stream->entry[i].i_length;
1436 while( i < p_stream->i_entry_count )
1439 if( i >= p_stream->i_entry_count ||
1440 p_stream->entry[i].i_length != i_delta )
1446 bo_add_32be( stts, i - i_first ); // sample-count
1447 bo_add_32be( stts, i_delta ); // sample-delta
1449 bo_fix_32be( stts, 12, i_index );
1452 /* FIXME add ctts ?? FIXME */
1454 stsz = box_full_new( "stsz", 0, 0 );
1455 bo_add_32be( stsz, 0 ); // sample-size
1456 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
1457 for( i = 0; i < p_stream->i_entry_count; i++ )
1459 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
1463 /* create stss table */
1465 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i++ )
1467 if( p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I )
1471 stss = box_full_new( "stss", 0, 0 );
1472 bo_add_32be( stss, 0 ); /* fixed later */
1474 bo_add_32be( stss, 1 + i );
1480 bo_fix_32be( stss, 12, i_index );
1484 /* Now gather all boxes into stbl */
1485 box_gather( stbl, stsd );
1486 box_gather( stbl, stts );
1489 box_gather( stbl, stss );
1491 box_gather( stbl, stsc );
1492 box_gather( stbl, stsz );
1493 p_stream->i_stco_pos = stbl->i_buffer + 16;
1494 box_gather( stbl, stco );
1502 static int64_t get_timestamp(void);
1504 static const uint32_t mvhd_matrix[9] =
1505 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1507 static bo_t *GetMoovBox( sout_mux_t *p_mux )
1509 sout_mux_sys_t *p_sys = p_mux->p_sys;
1514 uint32_t i_movie_timescale = 90000;
1515 int64_t i_movie_duration = 0;
1517 moov = box_new( "moov" );
1519 /* Create general info */
1520 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1522 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1523 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
1525 msg_Dbg( p_mux, "movie duration %ds",
1526 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
1528 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
1530 /* *** add /moov/mvhd *** */
1531 if( !p_sys->b_64_ext )
1533 mvhd = box_full_new( "mvhd", 0, 0 );
1534 bo_add_32be( mvhd, get_timestamp() ); // creation time
1535 bo_add_32be( mvhd, get_timestamp() ); // modification time
1536 bo_add_32be( mvhd, i_movie_timescale); // timescale
1537 bo_add_32be( mvhd, i_movie_duration ); // duration
1541 mvhd = box_full_new( "mvhd", 1, 0 );
1542 bo_add_64be( mvhd, get_timestamp() ); // creation time
1543 bo_add_64be( mvhd, get_timestamp() ); // modification time
1544 bo_add_32be( mvhd, i_movie_timescale); // timescale
1545 bo_add_64be( mvhd, i_movie_duration ); // duration
1547 bo_add_32be( mvhd, 0x10000 ); // rate
1548 bo_add_16be( mvhd, 0x100 ); // volume
1549 bo_add_16be( mvhd, 0 ); // reserved
1550 for( i = 0; i < 2; i++ )
1552 bo_add_32be( mvhd, 0 ); // reserved
1554 for( i = 0; i < 9; i++ )
1556 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
1558 for( i = 0; i < 6; i++ )
1560 bo_add_32be( mvhd, 0 ); // pre-defined
1563 /* Next available track id */
1564 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
1567 box_gather( moov, mvhd );
1569 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1571 mp4_stream_t *p_stream;
1572 uint32_t i_timescale;
1574 bo_t *trak, *tkhd, *edts, *elst, *mdia, *mdhd, *hdlr;
1575 bo_t *minf, *dinf, *dref, *url, *stbl;
1577 p_stream = p_sys->pp_streams[i_trak];
1579 if( p_stream->fmt.i_cat == AUDIO_ES )
1580 i_timescale = p_stream->fmt.audio.i_rate;
1584 /* *** add /moov/trak *** */
1585 trak = box_new( "trak" );
1587 /* *** add /moov/trak/tkhd *** */
1588 if( !p_sys->b_64_ext )
1591 tkhd = box_full_new( "tkhd", 0, 0x0f );
1593 tkhd = box_full_new( "tkhd", 0, 1 );
1595 bo_add_32be( tkhd, get_timestamp() ); // creation time
1596 bo_add_32be( tkhd, get_timestamp() ); // modification time
1597 bo_add_32be( tkhd, p_stream->i_track_id );
1598 bo_add_32be( tkhd, 0 ); // reserved 0
1599 bo_add_32be( tkhd, p_stream->i_duration *
1600 (int64_t)i_movie_timescale /
1601 (mtime_t)1000000 ); // duration
1606 tkhd = box_full_new( "tkhd", 1, 0x0f );
1608 tkhd = box_full_new( "tkhd", 1, 1 );
1610 bo_add_64be( tkhd, get_timestamp() ); // creation time
1611 bo_add_64be( tkhd, get_timestamp() ); // modification time
1612 bo_add_32be( tkhd, p_stream->i_track_id );
1613 bo_add_32be( tkhd, 0 ); // reserved 0
1614 bo_add_64be( tkhd, p_stream->i_duration *
1615 (int64_t)i_movie_timescale /
1616 (mtime_t)1000000 ); // duration
1619 for( i = 0; i < 2; i++ )
1621 bo_add_32be( tkhd, 0 ); // reserved
1623 bo_add_16be( tkhd, 0 ); // layer
1624 bo_add_16be( tkhd, 0 ); // pre-defined
1626 bo_add_16be( tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0 );
1627 bo_add_16be( tkhd, 0 ); // reserved
1628 for( i = 0; i < 9; i++ )
1630 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
1632 if( p_stream->fmt.i_cat == AUDIO_ES )
1634 bo_add_32be( tkhd, 0 ); // width (presentation)
1635 bo_add_32be( tkhd, 0 ); // height(presentation)
1637 else if( p_stream->fmt.i_cat == VIDEO_ES )
1639 int i_width = p_stream->fmt.video.i_width << 16;
1640 if( p_stream->fmt.video.i_sar_num > 0 &&
1641 p_stream->fmt.video.i_sar_den > 0 )
1643 i_width = (int64_t)p_stream->fmt.video.i_sar_num *
1644 ((int64_t)p_stream->fmt.video.i_width << 16) /
1645 p_stream->fmt.video.i_sar_den;
1647 // width (presentation)
1648 bo_add_32be( tkhd, i_width );
1649 // height(presentation)
1650 bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
1654 int i_width = 320 << 16;
1657 for( i = 0; i < p_sys->i_nb_streams; i++ )
1659 mp4_stream_t *tk = p_sys->pp_streams[i];
1660 if( tk->fmt.i_cat == VIDEO_ES )
1662 if( tk->fmt.video.i_sar_num > 0 &&
1663 tk->fmt.video.i_sar_den > 0 )
1664 i_width = (int64_t)tk->fmt.video.i_sar_num *
1665 ((int64_t)tk->fmt.video.i_width << 16) /
1666 tk->fmt.video.i_sar_den;
1668 i_width = tk->fmt.video.i_width << 16;
1669 i_height = tk->fmt.video.i_height;
1673 bo_add_32be( tkhd, i_width ); // width (presentation)
1674 bo_add_32be( tkhd, i_height << 16 ); // height(presentation)
1678 box_gather( trak, tkhd );
1680 /* *** add /moov/trak/edts and elst */
1681 edts = box_new( "edts" );
1682 elst = box_full_new( "elst", p_sys->b_64_ext ? 1 : 0, 0 );
1683 if( p_stream->i_dts_start > p_sys->i_dts_start )
1685 bo_add_32be( elst, 2 );
1687 if( p_sys->b_64_ext )
1689 bo_add_64be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1690 i_movie_timescale / INT64_C(1000000) );
1691 bo_add_64be( elst, -1 );
1695 bo_add_32be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1696 i_movie_timescale / INT64_C(1000000) );
1697 bo_add_32be( elst, -1 );
1699 bo_add_16be( elst, 1 );
1700 bo_add_16be( elst, 0 );
1704 bo_add_32be( elst, 1 );
1706 if( p_sys->b_64_ext )
1708 bo_add_64be( elst, p_stream->i_duration *
1709 i_movie_timescale / INT64_C(1000000) );
1710 bo_add_64be( elst, 0 );
1714 bo_add_32be( elst, p_stream->i_duration *
1715 i_movie_timescale / INT64_C(1000000) );
1716 bo_add_32be( elst, 0 );
1718 bo_add_16be( elst, 1 );
1719 bo_add_16be( elst, 0 );
1722 box_gather( edts, elst );
1724 box_gather( trak, edts );
1726 /* *** add /moov/trak/mdia *** */
1727 mdia = box_new( "mdia" );
1730 if( !p_sys->b_64_ext )
1732 mdhd = box_full_new( "mdhd", 0, 0 );
1733 bo_add_32be( mdhd, get_timestamp() ); // creation time
1734 bo_add_32be( mdhd, get_timestamp() ); // modification time
1735 bo_add_32be( mdhd, i_timescale); // timescale
1736 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1737 (mtime_t)1000000 ); // duration
1741 mdhd = box_full_new( "mdhd", 1, 0 );
1742 bo_add_64be( mdhd, get_timestamp() ); // creation time
1743 bo_add_64be( mdhd, get_timestamp() ); // modification time
1744 bo_add_32be( mdhd, i_timescale); // timescale
1745 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1746 (mtime_t)1000000 ); // duration
1749 if( p_stream->fmt.psz_language )
1751 char *psz = p_stream->fmt.psz_language;
1752 const iso639_lang_t *pl = NULL;
1753 uint16_t lang = 0x0;
1755 if( strlen( psz ) == 2 )
1757 pl = GetLang_1( psz );
1759 else if( strlen( psz ) == 3 )
1761 pl = GetLang_2B( psz );
1762 if( !strcmp( pl->psz_iso639_1, "??" ) )
1764 pl = GetLang_2T( psz );
1767 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1769 lang = ( ( pl->psz_iso639_2T[0] - 0x60 ) << 10 ) |
1770 ( ( pl->psz_iso639_2T[1] - 0x60 ) << 5 ) |
1771 ( ( pl->psz_iso639_2T[2] - 0x60 ) );
1773 bo_add_16be( mdhd, lang ); // language
1777 bo_add_16be( mdhd, 0 ); // language
1779 bo_add_16be( mdhd, 0 ); // predefined
1781 box_gather( mdia, mdhd );
1783 /* handler reference */
1784 hdlr = box_full_new( "hdlr", 0, 0 );
1787 bo_add_fourcc( hdlr, "mhlr" ); // media handler
1789 bo_add_32be( hdlr, 0 );
1791 if( p_stream->fmt.i_cat == AUDIO_ES )
1792 bo_add_fourcc( hdlr, "soun" );
1793 else if( p_stream->fmt.i_cat == VIDEO_ES )
1794 bo_add_fourcc( hdlr, "vide" );
1795 else if( p_stream->fmt.i_cat == SPU_ES )
1796 bo_add_fourcc( hdlr, "text" );
1798 bo_add_32be( hdlr, 0 ); // reserved
1799 bo_add_32be( hdlr, 0 ); // reserved
1800 bo_add_32be( hdlr, 0 ); // reserved
1803 bo_add_8( hdlr, 12 ); /* Pascal string for .mov */
1805 if( p_stream->fmt.i_cat == AUDIO_ES )
1806 bo_add_mem( hdlr, 12, (uint8_t*)"SoundHandler" );
1807 else if( p_stream->fmt.i_cat == VIDEO_ES )
1808 bo_add_mem( hdlr, 12, (uint8_t*)"VideoHandler" );
1810 bo_add_mem( hdlr, 12, (uint8_t*)"Text Handler" );
1813 bo_add_8( hdlr, 0 ); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
1816 box_gather( mdia, hdlr );
1819 minf = box_new( "minf" );
1822 if( p_stream->fmt.i_cat == AUDIO_ES )
1826 smhd = box_full_new( "smhd", 0, 0 );
1827 bo_add_16be( smhd, 0 ); // balance
1828 bo_add_16be( smhd, 0 ); // reserved
1831 box_gather( minf, smhd );
1833 else if( p_stream->fmt.i_cat == VIDEO_ES )
1837 vmhd = box_full_new( "vmhd", 0, 1 );
1838 bo_add_16be( vmhd, 0 ); // graphicsmode
1839 for( i = 0; i < 3; i++ )
1841 bo_add_16be( vmhd, 0 ); // opcolor
1845 box_gather( minf, vmhd );
1847 else if( p_stream->fmt.i_cat == SPU_ES )
1849 bo_t *gmhd = box_new( "gmhd" );
1850 bo_t *gmin = box_full_new( "gmin", 0, 1 );
1852 bo_add_16be( gmin, 0 ); // graphicsmode
1853 for( i = 0; i < 3; i++ )
1855 bo_add_16be( gmin, 0 ); // opcolor
1857 bo_add_16be( gmin, 0 ); // balance
1858 bo_add_16be( gmin, 0 ); // reserved
1861 box_gather( gmhd, gmin );
1864 box_gather( minf, gmhd );
1868 dinf = box_new( "dinf" );
1869 dref = box_full_new( "dref", 0, 0 );
1870 bo_add_32be( dref, 1 );
1871 url = box_full_new( "url ", 0, 0x01 );
1873 box_gather( dref, url );
1875 box_gather( dinf, dref );
1877 /* append dinf to mdia */
1879 box_gather( minf, dinf );
1882 stbl = GetStblBox( p_mux, p_stream );
1884 /* append stbl to minf */
1885 p_stream->i_stco_pos += minf->i_buffer;
1886 box_gather( minf, stbl );
1888 /* append minf to mdia */
1890 p_stream->i_stco_pos += mdia->i_buffer;
1891 box_gather( mdia, minf );
1893 /* append mdia to trak */
1895 p_stream->i_stco_pos += trak->i_buffer;
1896 box_gather( trak, mdia );
1898 /* append trak to moov */
1900 p_stream->i_stco_pos += moov->i_buffer;
1901 box_gather( moov, trak );
1904 /* Add user data tags */
1905 box_gather( moov, GetUdtaTag( p_mux ) );
1911 /****************************************************************************/
1913 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1918 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1919 p_bo->p_buffer = xmalloc( p_bo->i_buffer_size );
1923 p_bo->i_buffer_size = i_size;
1924 p_bo->p_buffer = p_buffer;
1927 p_bo->b_grow = b_grow;
1931 static void bo_add_8( bo_t *p_bo, uint8_t i )
1933 if( p_bo->i_buffer < p_bo->i_buffer_size )
1935 p_bo->p_buffer[p_bo->i_buffer] = i;
1937 else if( p_bo->b_grow )
1939 p_bo->i_buffer_size += 1024;
1940 p_bo->p_buffer = xrealloc( p_bo->p_buffer, p_bo->i_buffer_size );
1941 p_bo->p_buffer[p_bo->i_buffer] = i;
1947 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1949 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1950 bo_add_8( p_bo, i &0xff );
1953 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1955 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1956 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1957 bo_add_8( p_bo, ( i &0xff ) );
1959 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1961 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1962 bo_add_16be( p_bo, i &0xffff );
1965 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1967 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1968 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1969 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1970 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1973 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1975 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1976 bo_add_32be( p_bo, i &0xffffffff );
1979 static void bo_add_fourcc( bo_t *p_bo, const char *fcc )
1981 bo_add_8( p_bo, fcc[0] );
1982 bo_add_8( p_bo, fcc[1] );
1983 bo_add_8( p_bo, fcc[2] );
1984 bo_add_8( p_bo, fcc[3] );
1987 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1991 for( i = 0; i < i_size; i++ )
1993 bo_add_8( p_bo, p_mem[i] );
1997 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
2003 vals[3] = (unsigned char)(i_length & 0x7f);
2005 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
2007 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
2009 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
2011 bo_add_8( p_bo, tag );
2013 if( i_size < 0x00000080 )
2015 bo_add_8( p_bo, vals[3] );
2017 else if( i_size < 0x00004000 )
2019 bo_add_8( p_bo, vals[2] );
2020 bo_add_8( p_bo, vals[3] );
2022 else if( i_size < 0x00200000 )
2024 bo_add_8( p_bo, vals[1] );
2025 bo_add_8( p_bo, vals[2] );
2026 bo_add_8( p_bo, vals[3] );
2028 else if( i_size < 0x10000000 )
2030 bo_add_8( p_bo, vals[0] );
2031 bo_add_8( p_bo, vals[1] );
2032 bo_add_8( p_bo, vals[2] );
2033 bo_add_8( p_bo, vals[3] );
2037 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
2041 for( i = 0; i < p_bo2->i_buffer; i++ )
2043 bo_add_8( p_bo, p_bo2->p_buffer[i] );
2047 static bo_t * box_new( const char *fcc )
2051 if( ( box = malloc( sizeof( bo_t ) ) ) )
2053 bo_init( box, 0, NULL, true );
2055 bo_add_32be ( box, 0 );
2056 bo_add_fourcc( box, fcc );
2062 static bo_t * box_full_new( const char *fcc, uint8_t v, uint32_t f )
2066 if( ( box = malloc( sizeof( bo_t ) ) ) )
2068 bo_init( box, 0, NULL, true );
2070 bo_add_32be ( box, 0 );
2071 bo_add_fourcc( box, fcc );
2072 bo_add_8 ( box, v );
2073 bo_add_24be ( box, f );
2079 static void box_fix( bo_t *box )
2083 memcpy( &box_tmp, box, sizeof( bo_t ) );
2085 box_tmp.i_buffer = 0;
2086 bo_add_32be( &box_tmp, box->i_buffer );
2089 static void box_free( bo_t *box )
2091 free( box->p_buffer );
2095 static void box_gather ( bo_t *box, bo_t *box2 )
2097 bo_add_bo( box, box2 );
2101 static block_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
2106 p_buf = block_New( p_sout, box->i_buffer );
2107 if( box->i_buffer > 0 )
2109 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
2115 static void box_send( sout_mux_t *p_mux, bo_t *box )
2119 p_buf = bo_to_sout( p_mux->p_sout, box );
2122 sout_AccessOutWrite( p_mux->p_access, p_buf );
2125 static int64_t get_timestamp(void)
2127 int64_t i_timestamp = time(NULL);
2129 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2130 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60