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 )
70 set_callbacks( Open, Close )
73 /*****************************************************************************
75 *****************************************************************************/
76 static const char *const ppsz_sout_options[] = {
80 static int Control( sout_mux_t *, int, va_list );
81 static int AddStream( sout_mux_t *, sout_input_t * );
82 static int DelStream( sout_mux_t *, sout_input_t * );
83 static int Mux ( sout_mux_t * );
85 /*****************************************************************************
87 *****************************************************************************/
105 unsigned int i_entry_count;
106 unsigned int i_entry_max;
108 int64_t i_length_neg;
114 /* for later stco fix-up (fast start files) */
123 struct sout_mux_sys_t
136 mp4_stream_t **pp_streams;
149 static void bo_init ( bo_t *, int , uint8_t *, bool );
150 static void bo_add_8 ( bo_t *, uint8_t );
151 static void bo_add_16be ( bo_t *, uint16_t );
152 static void bo_add_24be ( bo_t *, uint32_t );
153 static void bo_add_32be ( bo_t *, uint32_t );
154 static void bo_add_64be ( bo_t *, uint64_t );
155 static void bo_add_fourcc(bo_t *, const char * );
156 static void bo_add_bo ( bo_t *, bo_t * );
157 static void bo_add_mem ( bo_t *, int , uint8_t * );
158 static void bo_add_descr( bo_t *, uint8_t , uint32_t );
160 static void bo_fix_32be ( bo_t *, int , uint32_t );
162 static bo_t *box_new ( const char *fcc );
163 static bo_t *box_full_new( const char *fcc, uint8_t v, uint32_t f );
164 static void box_fix ( bo_t *box );
165 static void box_free ( bo_t *box );
166 static void box_gather ( bo_t *box, bo_t *box2 );
168 static void box_send( sout_mux_t *p_mux, bo_t *box );
170 static block_t *bo_to_sout( sout_instance_t *p_sout, bo_t *box );
172 static bo_t *GetMoovBox( sout_mux_t *p_mux );
174 static block_t *ConvertSUBT( block_t *);
175 static block_t *ConvertAVC1( block_t * );
177 /*****************************************************************************
179 *****************************************************************************/
180 static int Open( vlc_object_t *p_this )
182 sout_mux_t *p_mux = (sout_mux_t*)p_this;
183 sout_mux_sys_t *p_sys;
186 msg_Dbg( p_mux, "Mp4 muxer opened" );
187 config_ChainParse( p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg );
189 p_mux->pf_control = Control;
190 p_mux->pf_addstream = AddStream;
191 p_mux->pf_delstream = DelStream;
193 p_mux->p_sys = p_sys = malloc( sizeof( sout_mux_sys_t ) );
197 p_sys->i_nb_streams = 0;
198 p_sys->pp_streams = NULL;
199 p_sys->i_mdat_pos = 0;
200 p_sys->b_mov = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "mov" );
201 p_sys->b_3gp = p_mux->psz_mux && !strcmp( p_mux->psz_mux, "3gp" );
202 p_sys->i_dts_start = 0;
207 /* Now add ftyp header */
208 box = box_new( "ftyp" );
209 if( p_sys->b_3gp ) bo_add_fourcc( box, "3gp4" );
210 else bo_add_fourcc( box, "isom" );
211 bo_add_32be ( box, 0 );
212 if( p_sys->b_3gp ) bo_add_fourcc( box, "3gp4" );
213 else bo_add_fourcc( box, "mp41" );
216 p_sys->i_pos += box->i_buffer;
217 p_sys->i_mdat_pos = p_sys->i_pos;
219 box_send( p_mux, box );
223 * Quicktime actually doesn't like the 64 bits extensions !!! */
224 p_sys->b_64_ext = false;
226 /* Now add mdat header */
227 box = box_new( "mdat" );
228 bo_add_64be ( box, 0 ); // enough to store an extended size
230 p_sys->i_pos += box->i_buffer;
232 box_send( p_mux, box );
237 /*****************************************************************************
239 *****************************************************************************/
240 static void Close( vlc_object_t * p_this )
242 sout_mux_t *p_mux = (sout_mux_t*)p_this;
243 sout_mux_sys_t *p_sys = p_mux->p_sys;
251 msg_Dbg( p_mux, "Close" );
253 /* Update mdat size */
254 bo_init( &bo, 0, NULL, true );
255 if( p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32) )
258 bo_add_32be ( &bo, 1 );
259 bo_add_fourcc( &bo, "mdat" );
260 bo_add_64be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos );
264 bo_add_32be ( &bo, 8 );
265 bo_add_fourcc( &bo, "wide" );
266 bo_add_32be ( &bo, p_sys->i_pos - p_sys->i_mdat_pos - 8 );
267 bo_add_fourcc( &bo, "mdat" );
269 p_hdr = bo_to_sout( p_mux->p_sout, &bo );
272 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos );
273 sout_AccessOutWrite( p_mux->p_access, p_hdr );
275 /* Create MOOV header */
276 i_moov_pos = p_sys->i_pos;
277 moov = GetMoovBox( p_mux );
279 /* Check we need to create "fast start" files */
280 var_Get( p_this, SOUT_CFG_PREFIX "faststart", &val );
281 p_sys->b_fast_start = val.b_bool;
282 while( p_sys->b_fast_start )
284 /* Move data to the end of the file so we can fit the moov header
287 int64_t i_chunk, i_size = p_sys->i_pos - p_sys->i_mdat_pos;
288 int i_moov_size = moov->i_buffer;
292 i_chunk = __MIN( 32768, i_size );
293 p_buf = block_New( p_mux, i_chunk );
294 sout_AccessOutSeek( p_mux->p_access,
295 p_sys->i_mdat_pos + i_size - i_chunk );
296 if( sout_AccessOutRead( p_mux->p_access, p_buf ) < i_chunk )
298 msg_Warn( p_this, "read() not supported by access output, "
299 "won't create a fast start file" );
300 p_sys->b_fast_start = false;
301 block_Release( p_buf );
304 sout_AccessOutSeek( p_mux->p_access, p_sys->i_mdat_pos + i_size +
305 i_moov_size - i_chunk );
306 sout_AccessOutWrite( p_mux->p_access, p_buf );
310 if( !p_sys->b_fast_start ) break;
312 /* Fix-up samples to chunks table in MOOV header */
313 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
315 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
319 moov->i_buffer = p_stream->i_stco_pos;
320 for( i_chunk = 0, i = 0; i < p_stream->i_entry_count; i_chunk++ )
322 if( p_stream->b_stco64 )
323 bo_add_64be( moov, p_stream->entry[i].i_pos + i_moov_size);
325 bo_add_32be( moov, p_stream->entry[i].i_pos + i_moov_size);
327 while( i < p_stream->i_entry_count )
329 if( i + 1 < p_stream->i_entry_count &&
330 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
331 != p_stream->entry[i + 1].i_pos )
342 moov->i_buffer = i_moov_size;
343 i_moov_pos = p_sys->i_mdat_pos;
344 p_sys->b_fast_start = false;
347 /* Write MOOV header */
348 sout_AccessOutSeek( p_mux->p_access, i_moov_pos );
349 box_send( p_mux, moov );
352 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
354 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
356 es_format_Clean( &p_stream->fmt );
357 free( p_stream->entry );
360 if( p_sys->i_nb_streams ) free( p_sys->pp_streams );
364 /*****************************************************************************
366 *****************************************************************************/
367 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 = (bool*)va_arg( args, bool * );
379 case MUX_GET_ADD_STREAM_WAIT:
380 pb_bool = (bool*)va_arg( args, bool * );
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 )
405 case VLC_CODEC_MJPGB:
410 case VLC_CODEC_AMR_NB:
411 case VLC_CODEC_AMR_WB:
416 msg_Warn( p_mux, "subtitle track added like in .mov (even when creating .mp4)" );
419 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
420 (char*)&p_input->p_fmt->i_codec );
424 p_stream = malloc( sizeof( mp4_stream_t ) );
427 es_format_Copy( &p_stream->fmt, p_input->p_fmt );
428 p_stream->i_track_id = p_sys->i_nb_streams + 1;
429 p_stream->i_length_neg = 0;
430 p_stream->i_entry_count = 0;
431 p_stream->i_entry_max = 1000;
433 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
434 p_stream->i_dts_start = 0;
435 p_stream->i_duration = 0;
437 p_input->p_sys = p_stream;
439 msg_Dbg( p_mux, "adding input" );
441 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
445 /*****************************************************************************
447 *****************************************************************************/
448 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
451 msg_Dbg( p_mux, "removing input" );
455 /*****************************************************************************
457 *****************************************************************************/
458 static int Mux( sout_mux_t *p_mux )
460 sout_mux_sys_t *p_sys = p_mux->p_sys;
464 sout_input_t *p_input;
465 mp4_stream_t *p_stream;
469 int i_stream = sout_MuxGetStream( p_mux, 2, &i_dts);
472 return( VLC_SUCCESS );
475 p_input = p_mux->pp_inputs[i_stream];
476 p_stream = (mp4_stream_t*)p_input->p_sys;
479 p_data = block_FifoGet( p_input->p_fifo );
480 if( p_stream->fmt.i_codec == VLC_CODEC_H264 )
482 p_data = ConvertAVC1( p_data );
484 else if( p_stream->fmt.i_codec == VLC_CODEC_SUBT )
486 p_data = ConvertSUBT( p_data );
488 if( p_data == NULL ) goto again;
490 if( p_stream->fmt.i_cat != SPU_ES )
492 /* Fix length of the sample */
493 if( block_FifoCount( p_input->p_fifo ) > 0 )
495 block_t *p_next = block_FifoShow( p_input->p_fifo );
496 int64_t i_diff = p_next->i_dts - p_data->i_dts;
498 if( i_diff < INT64_C(1000000 ) ) /* protection */
500 p_data->i_length = i_diff;
503 if( p_data->i_length <= 0 )
505 msg_Warn( p_mux, "i_length <= 0" );
506 p_stream->i_length_neg += p_data->i_length - 1;
507 p_data->i_length = 1;
509 else if( p_stream->i_length_neg < 0 )
511 int64_t i_recover = __MIN( p_data->i_length / 4, - p_stream->i_length_neg );
513 p_data->i_length -= i_recover;
514 p_stream->i_length_neg += i_recover;
518 /* Save starting time */
519 if( p_stream->i_entry_count == 0 )
521 p_stream->i_dts_start = p_data->i_dts;
523 /* Update global dts_start */
524 if( p_sys->i_dts_start <= 0 ||
525 p_stream->i_dts_start < p_sys->i_dts_start )
527 p_sys->i_dts_start = p_stream->i_dts_start;
531 if( p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0 )
533 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
537 /* FIXME handle this broken case */
542 if( p_stream->entry[p_stream->i_entry_count-1].i_length <= 0 )
544 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
549 /* add index entry */
550 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
551 p_stream->entry[p_stream->i_entry_count].i_size = p_data->i_buffer;
552 p_stream->entry[p_stream->i_entry_count].i_pts_dts=
553 __MAX( p_data->i_pts - p_data->i_dts, 0 );
554 p_stream->entry[p_stream->i_entry_count].i_length = p_data->i_length;
555 p_stream->entry[p_stream->i_entry_count].i_flags = p_data->i_flags;
557 p_stream->i_entry_count++;
558 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
559 if( p_stream->i_entry_count >= p_stream->i_entry_max - 1 )
561 p_stream->i_entry_max += 1000;
562 p_stream->entry = xrealloc( p_stream->entry,
563 p_stream->i_entry_max * sizeof( mp4_entry_t ) );
567 p_stream->i_duration += p_data->i_length;
568 p_sys->i_pos += p_data->i_buffer;
571 p_stream->i_last_dts = p_data->i_dts;
574 sout_AccessOutWrite( p_mux->p_access, p_data );
576 if( p_stream->fmt.i_cat == SPU_ES )
578 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
583 msg_Dbg( p_mux, "writing an empty sub" ) ;
585 /* Append a idx entry */
586 p_stream->entry[p_stream->i_entry_count].i_pos = p_sys->i_pos;
587 p_stream->entry[p_stream->i_entry_count].i_size = 3;
588 p_stream->entry[p_stream->i_entry_count].i_pts_dts= 0;
589 p_stream->entry[p_stream->i_entry_count].i_length = 0;
590 p_stream->entry[p_stream->i_entry_count].i_flags = 0;
592 /* XXX: No need to grow the entry here */
593 p_stream->i_entry_count++;
596 p_stream->i_last_dts += i_length;
599 p_data = block_New( p_mux, 3 );
600 p_data->p_buffer[0] = 0;
601 p_data->p_buffer[1] = 1;
602 p_data->p_buffer[2] = ' ';
604 p_sys->i_pos += p_data->i_buffer;
606 sout_AccessOutWrite( p_mux->p_access, p_data );
610 p_stream->i_duration = p_stream->i_last_dts - p_stream->i_dts_start;
614 return( VLC_SUCCESS );
617 /*****************************************************************************
619 *****************************************************************************/
620 static block_t *ConvertSUBT( block_t *p_block )
622 p_block = block_Realloc( p_block, 2, p_block->i_buffer );
624 /* No trailling '\0' */
625 if( p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0' )
628 p_block->p_buffer[0] = ( (p_block->i_buffer - 2) >> 8 )&0xff;
629 p_block->p_buffer[1] = ( (p_block->i_buffer - 2) )&0xff;
634 static block_t *ConvertAVC1( block_t *p_block )
636 uint8_t *last = p_block->p_buffer; /* Assume it starts with 0x00000001 */
637 uint8_t *dat = &p_block->p_buffer[4];
638 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
641 /* Replace the 4 bytes start code with 4 bytes size,
642 * FIXME are all startcodes 4 bytes ? (I don't think :( */
647 while( dat < end - 4 )
649 if( dat[0] == 0x00 && dat[1] == 0x00 &&
650 dat[2] == 0x00 && dat[3] == 0x01 )
662 i_size = dat - &last[4];
663 last[0] = ( i_size >> 24 )&0xff;
664 last[1] = ( i_size >> 16 )&0xff;
665 last[2] = ( i_size >> 8 )&0xff;
666 last[3] = ( i_size )&0xff;
668 /* Skip blocks with SPS/PPS */
669 if( (last[4]&0x1f) == 7 || (last[4]&0x1f) == 8 )
671 ; // FIXME Find a way to skip dat without frelling everything
679 static int GetDescrLength( int i_size )
681 if( i_size < 0x00000080 )
683 else if( i_size < 0x00004000 )
685 else if( i_size < 0x00200000 )
691 static bo_t *GetESDS( mp4_stream_t *p_stream )
695 int i_object_type_indication;
696 int i_decoder_specific_info_size;
698 int64_t i_bitrate_avg = 0;
699 int64_t i_bitrate_max = 0;
701 /* Compute avg/max bitrate */
702 for( i = 0; i < p_stream->i_entry_count; i++ )
704 i_bitrate_avg += p_stream->entry[i].i_size;
705 if( p_stream->entry[i].i_length > 0)
707 int64_t i_bitrate = INT64_C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
708 if( i_bitrate > i_bitrate_max )
709 i_bitrate_max = i_bitrate;
713 if( p_stream->i_duration > 0 )
714 i_bitrate_avg = INT64_C(8000000) * i_bitrate_avg / p_stream->i_duration;
717 if( i_bitrate_max <= 1 )
718 i_bitrate_max = 0x7fffffff;
721 if( p_stream->fmt.i_extra > 0 )
723 i_decoder_specific_info_size =
724 GetDescrLength( p_stream->fmt.i_extra );
728 i_decoder_specific_info_size = 0;
731 esds = box_full_new( "esds", 0, 0 );
734 bo_add_descr( esds, 0x03, 3 +
735 GetDescrLength( 13 + i_decoder_specific_info_size ) +
736 GetDescrLength( 1 ) );
737 bo_add_16be( esds, p_stream->i_track_id );
738 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
740 /* DecoderConfigDescr */
741 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
743 switch( p_stream->fmt.i_codec )
746 i_object_type_indication = 0x20;
749 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
750 i_object_type_indication = 0x60;
753 /* FIXME for mpeg2-aac == 0x66->0x68 */
754 i_object_type_indication = 0x40;
757 i_object_type_indication =
758 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
761 i_object_type_indication = 0x00;
764 i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
766 bo_add_8 ( esds, i_object_type_indication );
767 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
768 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
769 bo_add_32be( esds, i_bitrate_max ); // maxBitrate
770 bo_add_32be( esds, i_bitrate_avg ); // avgBitrate
772 if( p_stream->fmt.i_extra > 0 )
776 /* DecoderSpecificInfo */
777 bo_add_descr( esds, 0x05, p_stream->fmt.i_extra );
779 for( i = 0; i < p_stream->fmt.i_extra; i++ )
781 bo_add_8( esds, ((uint8_t*)p_stream->fmt.p_extra)[i] );
785 /* SL_Descr mandatory */
786 bo_add_descr( esds, 0x06, 1 );
787 bo_add_8 ( esds, 0x02 ); // sl_predefined
794 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
799 wave = box_new( "wave" );
801 box = box_new( "frma" );
802 bo_add_fourcc( box, "mp4a" );
804 box_gather( wave, box );
806 box = box_new( "mp4a" );
807 bo_add_32be( box, 0 );
809 box_gather( wave, box );
811 box = GetESDS( p_stream );
813 box_gather( wave, box );
815 box = box_new( "srcq" );
816 bo_add_32be( box, 0x40 );
818 box_gather( wave, box );
821 bo_add_32be( wave, 8 ); /* new empty box */
822 bo_add_32be( wave, 0 ); /* box label */
829 static bo_t *GetDamrTag( mp4_stream_t *p_stream )
833 damr = box_new( "damr" );
835 bo_add_fourcc( damr, "REFC" );
838 if( p_stream->fmt.i_codec == VLC_CODEC_AMR_NB )
839 bo_add_16be( damr, 0x81ff ); /* Mode set (all modes for AMR_NB) */
841 bo_add_16be( damr, 0x83ff ); /* Mode set (all modes for AMR_WB) */
842 bo_add_16be( damr, 0x1 ); /* Mode change period (no restriction) */
849 static bo_t *GetD263Tag( void )
853 d263 = box_new( "d263" );
855 bo_add_fourcc( d263, "VLC " );
856 bo_add_16be( d263, 0xa );
864 static bo_t *GetAvcCTag( mp4_stream_t *p_stream )
867 uint8_t *p_sps = NULL;
868 uint8_t *p_pps = NULL;
872 if( p_stream->fmt.i_extra > 0 )
874 /* FIXME: take into account multiple sps/pps */
875 uint8_t *p_buffer = p_stream->fmt.p_extra;
876 int i_buffer = p_stream->fmt.i_extra;
878 while( i_buffer > 4 &&
879 p_buffer[0] == 0 && p_buffer[1] == 0 &&
880 p_buffer[2] == 0 && p_buffer[3] == 1 )
882 const int i_nal_type = p_buffer[4]&0x1f;
887 //msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
889 for( i_offset = 1; i_offset+3 < i_buffer ; i_offset++)
891 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 &&
892 p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
894 /* we found another startcode */
895 i_startcode = i_offset;
899 i_size = i_startcode ? i_startcode : i_buffer;
900 if( i_nal_type == 7 )
902 p_sps = &p_buffer[4];
903 i_sps_size = i_size - 4;
905 if( i_nal_type == 8 )
907 p_pps = &p_buffer[4];
908 i_pps_size = i_size - 4;
915 /* FIXME use better value */
916 avcC = box_new( "avcC" );
917 bo_add_8( avcC, 1 ); /* configuration version */
918 bo_add_8( avcC, i_sps_size ? p_sps[1] : 77 );
919 bo_add_8( avcC, i_sps_size ? p_sps[2] : 64 );
920 bo_add_8( avcC, i_sps_size ? p_sps[3] : 30 ); /* level, 5.1 */
921 bo_add_8( avcC, 0xff ); /* 0b11111100 | lengthsize = 0x11 */
923 bo_add_8( avcC, 0xe0 | (i_sps_size > 0 ? 1 : 0) ); /* 0b11100000 | sps_count */
926 bo_add_16be( avcC, i_sps_size );
927 bo_add_mem( avcC, i_sps_size, p_sps );
930 bo_add_8( avcC, (i_pps_size > 0 ? 1 : 0) ); /* pps_count */
933 bo_add_16be( avcC, i_pps_size );
934 bo_add_mem( avcC, i_pps_size, p_pps );
941 /* TODO: No idea about these values */
942 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
944 bo_t *smi = box_new( "SMI " );
946 if( p_stream->fmt.i_extra > 0x4e )
948 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
949 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
951 while( p + 8 < p_end )
953 int i_size = GetDWBE( p );
956 /* FIXME handle 1 as long size */
959 if( !strncmp( (const char *)&p[4], "SMI ", 4 ) )
961 bo_add_mem( smi, p_end - p - 8, &p[8] );
968 /* Create a dummy one in fallback */
969 bo_add_fourcc( smi, "SEQH" );
970 bo_add_32be( smi, 0x5 );
971 bo_add_32be( smi, 0xe2c0211d );
972 bo_add_8( smi, 0xc0 );
978 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
980 sout_mux_sys_t *p_sys = p_mux->p_sys;
981 bo_t *udta = box_new( "udta" );
982 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
986 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
988 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
990 if( p_stream->fmt.i_codec == VLC_CODEC_MP4V ||
991 p_stream->fmt.i_codec == VLC_CODEC_MP4A )
993 bo_t *box = box_new( "\251req" );
995 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
996 bo_add_16be( box, 0 );
997 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
998 (uint8_t *)"QuickTime 6.0 or greater" );
1000 box_gather( udta, box );
1007 bo_t *box = box_new( "\251enc" );
1009 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
1010 bo_add_16be( box, 0 );
1011 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
1012 (uint8_t*)PACKAGE_STRING " stream output" );
1014 box_gather( udta, box );
1020 #define ADD_META_BOX( type, box_string ) { \
1022 if( vlc_meta_Get( p_meta, vlc_meta_##type ) ) box = box_new( "\251" box_string ); \
1025 bo_add_16be( box, strlen( vlc_meta_Get( p_meta, vlc_meta_##type ) )); \
1026 bo_add_16be( box, 0 ); \
1027 bo_add_mem( box, strlen( vlc_meta_Get( p_meta, vlc_meta_##type ) ), \
1028 (uint8_t*)(vlc_meta_Get( p_meta, vlc_meta_##type ) ) ); \
1030 box_gather( udta, box ); \
1033 ADD_META_BOX( Title, "nam" );
1034 ADD_META_BOX( Artist, "ART" );
1035 ADD_META_BOX( Genre, "gen" );
1036 ADD_META_BOX( Copyright, "cpy" );
1037 ADD_META_BOX( Description, "des" );
1038 ADD_META_BOX( Date, "day" );
1039 ADD_META_BOX( URL, "url" );
1047 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1049 sout_mux_sys_t *p_sys = p_mux->p_sys;
1050 bool b_descr = false;
1055 switch( p_stream->fmt.i_codec )
1057 case VLC_CODEC_MP4A:
1058 memcpy( fcc, "mp4a", 4 );
1062 case VLC_CODEC_AMR_NB:
1063 case VLC_CODEC_AMR_WB:
1064 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1068 case VLC_CODEC_MPGA:
1070 memcpy( fcc, ".mp3", 4 );
1073 memcpy( fcc, "mp4a", 4 );
1079 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1083 soun = box_new( fcc );
1084 for( i = 0; i < 6; i++ )
1086 bo_add_8( soun, 0 ); // reserved;
1088 bo_add_16be( soun, 1 ); // data-reference-index
1090 /* SoundDescription */
1092 p_stream->fmt.i_codec == VLC_CODEC_MP4A )
1094 bo_add_16be( soun, 1 ); // version 1;
1098 bo_add_16be( soun, 0 ); // version 0;
1100 bo_add_16be( soun, 0 ); // revision level (0)
1101 bo_add_32be( soun, 0 ); // vendor
1103 bo_add_16be( soun, p_stream->fmt.audio.i_channels );
1105 bo_add_16be( soun, p_stream->fmt.audio.i_bitspersample ?
1106 p_stream->fmt.audio.i_bitspersample : 16 );
1107 bo_add_16be( soun, -2 ); // compression id
1108 bo_add_16be( soun, 0 ); // packet size (0)
1109 bo_add_16be( soun, p_stream->fmt.audio.i_rate ); // sampleratehi
1110 bo_add_16be( soun, 0 ); // sampleratelo
1112 /* Extended data for SoundDescription V1 */
1114 p_stream->fmt.i_codec == VLC_CODEC_MP4A )
1116 /* samples per packet */
1117 bo_add_32be( soun, p_stream->fmt.audio.i_frame_length );
1118 bo_add_32be( soun, 1536 ); /* bytes per packet */
1119 bo_add_32be( soun, 2 ); /* bytes per frame */
1120 /* bytes per sample */
1121 bo_add_32be( soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1124 /* Add an ES Descriptor */
1130 p_stream->fmt.i_codec == VLC_CODEC_MP4A )
1132 box = GetWaveTag( p_stream );
1134 else if( p_stream->fmt.i_codec == VLC_CODEC_AMR_NB )
1136 box = GetDamrTag( p_stream );
1140 box = GetESDS( p_stream );
1143 box_gather( soun, box );
1151 static bo_t *GetVideBox( mp4_stream_t *p_stream )
1158 switch( p_stream->fmt.i_codec )
1160 case VLC_CODEC_MP4V:
1161 case VLC_CODEC_MPGV:
1162 memcpy( fcc, "mp4v", 4 );
1165 case VLC_CODEC_MJPG:
1166 memcpy( fcc, "mjpa", 4 );
1169 case VLC_CODEC_SVQ1:
1170 memcpy( fcc, "SVQ1", 4 );
1173 case VLC_CODEC_SVQ3:
1174 memcpy( fcc, "SVQ3", 4 );
1177 case VLC_CODEC_H263:
1178 memcpy( fcc, "s263", 4 );
1181 case VLC_CODEC_H264:
1182 memcpy( fcc, "avc1", 4 );
1185 case VLC_CODEC_YV12:
1186 memcpy( fcc, "yv12", 4 );
1189 case VLC_CODEC_YUYV:
1190 memcpy( fcc, "yuy2", 4 );
1194 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1198 vide = box_new( fcc );
1199 for( i = 0; i < 6; i++ )
1201 bo_add_8( vide, 0 ); // reserved;
1203 bo_add_16be( vide, 1 ); // data-reference-index
1205 bo_add_16be( vide, 0 ); // predefined;
1206 bo_add_16be( vide, 0 ); // reserved;
1207 for( i = 0; i < 3; i++ )
1209 bo_add_32be( vide, 0 ); // predefined;
1212 bo_add_16be( vide, p_stream->fmt.video.i_width ); // i_width
1213 bo_add_16be( vide, p_stream->fmt.video.i_height ); // i_height
1215 bo_add_32be( vide, 0x00480000 ); // h 72dpi
1216 bo_add_32be( vide, 0x00480000 ); // v 72dpi
1218 bo_add_32be( vide, 0 ); // data size, always 0
1219 bo_add_16be( vide, 1 ); // frames count per sample
1222 for( i = 0; i < 32; i++ )
1224 bo_add_8( vide, 0 );
1227 bo_add_16be( vide, 0x18 ); // depth
1228 bo_add_16be( vide, 0xffff ); // predefined
1230 /* add an ES Descriptor */
1231 switch( p_stream->fmt.i_codec )
1233 case VLC_CODEC_MP4V:
1234 case VLC_CODEC_MPGV:
1236 bo_t *esds = GetESDS( p_stream );
1239 box_gather( vide, esds );
1243 case VLC_CODEC_H263:
1245 bo_t *d263 = GetD263Tag();
1248 box_gather( vide, d263 );
1252 case VLC_CODEC_SVQ3:
1254 bo_t *esds = GetSVQ3Tag( p_stream );
1257 box_gather( vide, esds );
1261 case VLC_CODEC_H264:
1262 box_gather( vide, GetAvcCTag( p_stream ) );
1274 static bo_t *GetTextBox( void )
1277 bo_t *text = box_new( "text" );
1280 for( i = 0; i < 6; i++ )
1282 bo_add_8( text, 0 ); // reserved;
1284 bo_add_16be( text, 1 ); // data-reference-index
1286 bo_add_32be( text, 0 ); // display flags
1287 bo_add_32be( text, 0 ); // justification
1288 for( i = 0; i < 3; i++ )
1290 bo_add_16be( text, 0 ); // back ground color
1293 bo_add_16be( text, 0 ); // box text
1294 bo_add_16be( text, 0 ); // box text
1295 bo_add_16be( text, 0 ); // box text
1296 bo_add_16be( text, 0 ); // box text
1298 bo_add_64be( text, 0 ); // reserved
1299 for( i = 0; i < 3; i++ )
1301 bo_add_16be( text, 0xff ); // foreground color
1304 bo_add_8 ( text, 9 );
1305 bo_add_mem( text, 9, (uint8_t*)"Helvetica" );
1312 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1314 sout_mux_sys_t *p_sys = p_mux->p_sys;
1315 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
1316 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz, *stss;
1317 uint32_t i_timescale;
1318 int64_t i_dts, i_dts_q;
1320 stbl = box_new( "stbl" );
1322 /* sample description */
1323 stsd = box_full_new( "stsd", 0, 0 );
1324 bo_add_32be( stsd, 1 );
1325 if( p_stream->fmt.i_cat == AUDIO_ES )
1327 bo_t *soun = GetSounBox( p_mux, p_stream );
1328 box_gather( stsd, soun );
1330 else if( p_stream->fmt.i_cat == VIDEO_ES )
1332 bo_t *vide = GetVideBox( p_stream );
1333 box_gather( stsd, vide );
1335 else if( p_stream->fmt.i_cat == SPU_ES )
1337 box_gather( stsd, GetTextBox() );
1341 /* chunk offset table */
1342 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
1344 /* 64 bits version */
1345 p_stream->b_stco64 = true;
1346 stco = box_full_new( "co64", 0, 0 );
1350 /* 32 bits version */
1351 p_stream->b_stco64 = false;
1352 stco = box_full_new( "stco", 0, 0 );
1354 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
1356 /* sample to chunk table */
1357 stsc = box_full_new( "stsc", 0, 0 );
1358 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
1360 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
1361 i < p_stream->i_entry_count; i_chunk++ )
1365 if( p_stream->b_stco64 )
1366 bo_add_64be( stco, p_stream->entry[i].i_pos );
1368 bo_add_32be( stco, p_stream->entry[i].i_pos );
1370 while( i < p_stream->i_entry_count )
1372 if( i + 1 < p_stream->i_entry_count &&
1373 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
1374 != p_stream->entry[i + 1].i_pos )
1383 /* Add entry to the stsc table */
1384 if( i_stsc_last_val != i - i_first )
1386 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
1387 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
1388 bo_add_32be( stsc, 1 ); // sample-descr-index
1389 i_stsc_last_val = i - i_first;
1394 /* Fix stco entry count */
1395 bo_fix_32be( stco, 12, i_chunk );
1396 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
1399 /* Fix stsc entry count */
1400 bo_fix_32be( stsc, 12, i_stsc_entries );
1404 stts = box_full_new( "stts", 0, 0 );
1405 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
1407 if( p_stream->fmt.i_cat == AUDIO_ES )
1408 i_timescale = p_stream->fmt.audio.i_rate;
1412 /* first, create quantified length */
1413 for( i = 0, i_dts = 0, i_dts_q = 0; i < p_stream->i_entry_count; i++ )
1415 int64_t i_dts_deq = i_dts_q * INT64_C(1000000) / (int64_t)i_timescale;
1416 int64_t i_delta = p_stream->entry[i].i_length + i_dts - i_dts_deq;
1418 i_dts += p_stream->entry[i].i_length;
1420 p_stream->entry[i].i_length =
1421 i_delta * (int64_t)i_timescale / INT64_C(1000000);
1423 i_dts_q += p_stream->entry[i].i_length;
1425 /* then write encoded table */
1426 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
1429 int64_t i_delta = p_stream->entry[i].i_length;
1431 while( i < p_stream->i_entry_count )
1434 if( i >= p_stream->i_entry_count ||
1435 p_stream->entry[i].i_length != i_delta )
1441 bo_add_32be( stts, i - i_first ); // sample-count
1442 bo_add_32be( stts, i_delta ); // sample-delta
1444 bo_fix_32be( stts, 12, i_index );
1447 /* FIXME add ctts ?? FIXME */
1449 stsz = box_full_new( "stsz", 0, 0 );
1450 bo_add_32be( stsz, 0 ); // sample-size
1451 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
1452 for( i = 0; i < p_stream->i_entry_count; i++ )
1454 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
1458 /* create stss table */
1460 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i++ )
1462 if( p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I )
1466 stss = box_full_new( "stss", 0, 0 );
1467 bo_add_32be( stss, 0 ); /* fixed later */
1469 bo_add_32be( stss, 1 + i );
1475 bo_fix_32be( stss, 12, i_index );
1479 /* Now gather all boxes into stbl */
1480 box_gather( stbl, stsd );
1481 box_gather( stbl, stts );
1484 box_gather( stbl, stss );
1486 box_gather( stbl, stsc );
1487 box_gather( stbl, stsz );
1488 p_stream->i_stco_pos = stbl->i_buffer + 16;
1489 box_gather( stbl, stco );
1497 static int64_t get_timestamp(void);
1499 static const uint32_t mvhd_matrix[9] =
1500 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1502 static bo_t *GetMoovBox( sout_mux_t *p_mux )
1504 sout_mux_sys_t *p_sys = p_mux->p_sys;
1509 uint32_t i_movie_timescale = 90000;
1510 int64_t i_movie_duration = 0;
1512 moov = box_new( "moov" );
1514 /* Create general info */
1515 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1517 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1518 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
1520 msg_Dbg( p_mux, "movie duration %ds",
1521 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
1523 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
1525 /* *** add /moov/mvhd *** */
1526 if( !p_sys->b_64_ext )
1528 mvhd = box_full_new( "mvhd", 0, 0 );
1529 bo_add_32be( mvhd, get_timestamp() ); // creation time
1530 bo_add_32be( mvhd, get_timestamp() ); // modification time
1531 bo_add_32be( mvhd, i_movie_timescale); // timescale
1532 bo_add_32be( mvhd, i_movie_duration ); // duration
1536 mvhd = box_full_new( "mvhd", 1, 0 );
1537 bo_add_64be( mvhd, get_timestamp() ); // creation time
1538 bo_add_64be( mvhd, get_timestamp() ); // modification time
1539 bo_add_32be( mvhd, i_movie_timescale); // timescale
1540 bo_add_64be( mvhd, i_movie_duration ); // duration
1542 bo_add_32be( mvhd, 0x10000 ); // rate
1543 bo_add_16be( mvhd, 0x100 ); // volume
1544 bo_add_16be( mvhd, 0 ); // reserved
1545 for( i = 0; i < 2; i++ )
1547 bo_add_32be( mvhd, 0 ); // reserved
1549 for( i = 0; i < 9; i++ )
1551 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
1553 for( i = 0; i < 6; i++ )
1555 bo_add_32be( mvhd, 0 ); // pre-defined
1558 /* Next available track id */
1559 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
1562 box_gather( moov, mvhd );
1564 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1566 mp4_stream_t *p_stream;
1567 uint32_t i_timescale;
1569 bo_t *trak, *tkhd, *edts, *elst, *mdia, *mdhd, *hdlr;
1570 bo_t *minf, *dinf, *dref, *url, *stbl;
1572 p_stream = p_sys->pp_streams[i_trak];
1574 if( p_stream->fmt.i_cat == AUDIO_ES )
1575 i_timescale = p_stream->fmt.audio.i_rate;
1579 /* *** add /moov/trak *** */
1580 trak = box_new( "trak" );
1582 /* *** add /moov/trak/tkhd *** */
1583 if( !p_sys->b_64_ext )
1586 tkhd = box_full_new( "tkhd", 0, 0x0f );
1588 tkhd = box_full_new( "tkhd", 0, 1 );
1590 bo_add_32be( tkhd, get_timestamp() ); // creation time
1591 bo_add_32be( tkhd, get_timestamp() ); // modification time
1592 bo_add_32be( tkhd, p_stream->i_track_id );
1593 bo_add_32be( tkhd, 0 ); // reserved 0
1594 bo_add_32be( tkhd, p_stream->i_duration *
1595 (int64_t)i_movie_timescale /
1596 (mtime_t)1000000 ); // duration
1601 tkhd = box_full_new( "tkhd", 1, 0x0f );
1603 tkhd = box_full_new( "tkhd", 1, 1 );
1605 bo_add_64be( tkhd, get_timestamp() ); // creation time
1606 bo_add_64be( tkhd, get_timestamp() ); // modification time
1607 bo_add_32be( tkhd, p_stream->i_track_id );
1608 bo_add_32be( tkhd, 0 ); // reserved 0
1609 bo_add_64be( tkhd, p_stream->i_duration *
1610 (int64_t)i_movie_timescale /
1611 (mtime_t)1000000 ); // duration
1614 for( i = 0; i < 2; i++ )
1616 bo_add_32be( tkhd, 0 ); // reserved
1618 bo_add_16be( tkhd, 0 ); // layer
1619 bo_add_16be( tkhd, 0 ); // pre-defined
1621 bo_add_16be( tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0 );
1622 bo_add_16be( tkhd, 0 ); // reserved
1623 for( i = 0; i < 9; i++ )
1625 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
1627 if( p_stream->fmt.i_cat == AUDIO_ES )
1629 bo_add_32be( tkhd, 0 ); // width (presentation)
1630 bo_add_32be( tkhd, 0 ); // height(presentation)
1632 else if( p_stream->fmt.i_cat == VIDEO_ES )
1634 int i_width = p_stream->fmt.video.i_width << 16;
1635 if( p_stream->fmt.video.i_sar_num > 0 &&
1636 p_stream->fmt.video.i_sar_den > 0 )
1638 i_width = (int64_t)p_stream->fmt.video.i_sar_num *
1639 ((int64_t)p_stream->fmt.video.i_width << 16) /
1640 p_stream->fmt.video.i_sar_den;
1642 // width (presentation)
1643 bo_add_32be( tkhd, i_width );
1644 // height(presentation)
1645 bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
1649 int i_width = 320 << 16;
1652 for( i = 0; i < p_sys->i_nb_streams; i++ )
1654 mp4_stream_t *tk = p_sys->pp_streams[i];
1655 if( tk->fmt.i_cat == VIDEO_ES )
1657 if( tk->fmt.video.i_sar_num > 0 &&
1658 tk->fmt.video.i_sar_den > 0 )
1659 i_width = (int64_t)tk->fmt.video.i_sar_num *
1660 ((int64_t)tk->fmt.video.i_width << 16) /
1661 tk->fmt.video.i_sar_den;
1663 i_width = tk->fmt.video.i_width << 16;
1664 i_height = tk->fmt.video.i_height;
1668 bo_add_32be( tkhd, i_width ); // width (presentation)
1669 bo_add_32be( tkhd, i_height << 16 ); // height(presentation)
1673 box_gather( trak, tkhd );
1675 /* *** add /moov/trak/edts and elst */
1676 edts = box_new( "edts" );
1677 elst = box_full_new( "elst", p_sys->b_64_ext ? 1 : 0, 0 );
1678 if( p_stream->i_dts_start > p_sys->i_dts_start )
1680 bo_add_32be( elst, 2 );
1682 if( p_sys->b_64_ext )
1684 bo_add_64be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1685 i_movie_timescale / INT64_C(1000000) );
1686 bo_add_64be( elst, -1 );
1690 bo_add_32be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1691 i_movie_timescale / INT64_C(1000000) );
1692 bo_add_32be( elst, -1 );
1694 bo_add_16be( elst, 1 );
1695 bo_add_16be( elst, 0 );
1699 bo_add_32be( elst, 1 );
1701 if( p_sys->b_64_ext )
1703 bo_add_64be( elst, p_stream->i_duration *
1704 i_movie_timescale / INT64_C(1000000) );
1705 bo_add_64be( elst, 0 );
1709 bo_add_32be( elst, p_stream->i_duration *
1710 i_movie_timescale / INT64_C(1000000) );
1711 bo_add_32be( elst, 0 );
1713 bo_add_16be( elst, 1 );
1714 bo_add_16be( elst, 0 );
1717 box_gather( edts, elst );
1719 box_gather( trak, edts );
1721 /* *** add /moov/trak/mdia *** */
1722 mdia = box_new( "mdia" );
1725 if( !p_sys->b_64_ext )
1727 mdhd = box_full_new( "mdhd", 0, 0 );
1728 bo_add_32be( mdhd, get_timestamp() ); // creation time
1729 bo_add_32be( mdhd, get_timestamp() ); // modification time
1730 bo_add_32be( mdhd, i_timescale); // timescale
1731 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1732 (mtime_t)1000000 ); // duration
1736 mdhd = box_full_new( "mdhd", 1, 0 );
1737 bo_add_64be( mdhd, get_timestamp() ); // creation time
1738 bo_add_64be( mdhd, get_timestamp() ); // modification time
1739 bo_add_32be( mdhd, i_timescale); // timescale
1740 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1741 (mtime_t)1000000 ); // duration
1744 if( p_stream->fmt.psz_language )
1746 char *psz = p_stream->fmt.psz_language;
1747 const iso639_lang_t *pl = NULL;
1748 uint16_t lang = 0x0;
1750 if( strlen( psz ) == 2 )
1752 pl = GetLang_1( psz );
1754 else if( strlen( psz ) == 3 )
1756 pl = GetLang_2B( psz );
1757 if( !strcmp( pl->psz_iso639_1, "??" ) )
1759 pl = GetLang_2T( psz );
1762 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1764 lang = ( ( pl->psz_iso639_2T[0] - 0x60 ) << 10 ) |
1765 ( ( pl->psz_iso639_2T[1] - 0x60 ) << 5 ) |
1766 ( ( pl->psz_iso639_2T[2] - 0x60 ) );
1768 bo_add_16be( mdhd, lang ); // language
1772 bo_add_16be( mdhd, 0 ); // language
1774 bo_add_16be( mdhd, 0 ); // predefined
1776 box_gather( mdia, mdhd );
1778 /* handler reference */
1779 hdlr = box_full_new( "hdlr", 0, 0 );
1782 bo_add_fourcc( hdlr, "mhlr" ); // media handler
1784 bo_add_32be( hdlr, 0 );
1786 if( p_stream->fmt.i_cat == AUDIO_ES )
1787 bo_add_fourcc( hdlr, "soun" );
1788 else if( p_stream->fmt.i_cat == VIDEO_ES )
1789 bo_add_fourcc( hdlr, "vide" );
1790 else if( p_stream->fmt.i_cat == SPU_ES )
1791 bo_add_fourcc( hdlr, "text" );
1793 bo_add_32be( hdlr, 0 ); // reserved
1794 bo_add_32be( hdlr, 0 ); // reserved
1795 bo_add_32be( hdlr, 0 ); // reserved
1798 bo_add_8( hdlr, 12 ); /* Pascal string for .mov */
1800 if( p_stream->fmt.i_cat == AUDIO_ES )
1801 bo_add_mem( hdlr, 12, (uint8_t*)"SoundHandler" );
1802 else if( p_stream->fmt.i_cat == VIDEO_ES )
1803 bo_add_mem( hdlr, 12, (uint8_t*)"VideoHandler" );
1805 bo_add_mem( hdlr, 12, (uint8_t*)"Text Handler" );
1808 bo_add_8( hdlr, 0 ); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
1811 box_gather( mdia, hdlr );
1814 minf = box_new( "minf" );
1817 if( p_stream->fmt.i_cat == AUDIO_ES )
1821 smhd = box_full_new( "smhd", 0, 0 );
1822 bo_add_16be( smhd, 0 ); // balance
1823 bo_add_16be( smhd, 0 ); // reserved
1826 box_gather( minf, smhd );
1828 else if( p_stream->fmt.i_cat == VIDEO_ES )
1832 vmhd = box_full_new( "vmhd", 0, 1 );
1833 bo_add_16be( vmhd, 0 ); // graphicsmode
1834 for( i = 0; i < 3; i++ )
1836 bo_add_16be( vmhd, 0 ); // opcolor
1840 box_gather( minf, vmhd );
1842 else if( p_stream->fmt.i_cat == SPU_ES )
1844 bo_t *gmhd = box_new( "gmhd" );
1845 bo_t *gmin = box_full_new( "gmin", 0, 1 );
1847 bo_add_16be( gmin, 0 ); // graphicsmode
1848 for( i = 0; i < 3; i++ )
1850 bo_add_16be( gmin, 0 ); // opcolor
1852 bo_add_16be( gmin, 0 ); // balance
1853 bo_add_16be( gmin, 0 ); // reserved
1856 box_gather( gmhd, gmin );
1859 box_gather( minf, gmhd );
1863 dinf = box_new( "dinf" );
1864 dref = box_full_new( "dref", 0, 0 );
1865 bo_add_32be( dref, 1 );
1866 url = box_full_new( "url ", 0, 0x01 );
1868 box_gather( dref, url );
1870 box_gather( dinf, dref );
1872 /* append dinf to mdia */
1874 box_gather( minf, dinf );
1877 stbl = GetStblBox( p_mux, p_stream );
1879 /* append stbl to minf */
1880 p_stream->i_stco_pos += minf->i_buffer;
1881 box_gather( minf, stbl );
1883 /* append minf to mdia */
1885 p_stream->i_stco_pos += mdia->i_buffer;
1886 box_gather( mdia, minf );
1888 /* append mdia to trak */
1890 p_stream->i_stco_pos += trak->i_buffer;
1891 box_gather( trak, mdia );
1893 /* append trak to moov */
1895 p_stream->i_stco_pos += moov->i_buffer;
1896 box_gather( moov, trak );
1899 /* Add user data tags */
1900 box_gather( moov, GetUdtaTag( p_mux ) );
1906 /****************************************************************************/
1908 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1913 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1914 p_bo->p_buffer = xmalloc( p_bo->i_buffer_size );
1918 p_bo->i_buffer_size = i_size;
1919 p_bo->p_buffer = p_buffer;
1922 p_bo->b_grow = b_grow;
1926 static void bo_add_8( bo_t *p_bo, uint8_t i )
1928 if( p_bo->i_buffer < p_bo->i_buffer_size )
1930 p_bo->p_buffer[p_bo->i_buffer] = i;
1932 else if( p_bo->b_grow )
1934 p_bo->i_buffer_size += 1024;
1935 p_bo->p_buffer = xrealloc( p_bo->p_buffer, p_bo->i_buffer_size );
1936 p_bo->p_buffer[p_bo->i_buffer] = i;
1942 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1944 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1945 bo_add_8( p_bo, i &0xff );
1948 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1950 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1951 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1952 bo_add_8( p_bo, ( i &0xff ) );
1954 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1956 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1957 bo_add_16be( p_bo, i &0xffff );
1960 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1962 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1963 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1964 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1965 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1968 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1970 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1971 bo_add_32be( p_bo, i &0xffffffff );
1974 static void bo_add_fourcc( bo_t *p_bo, const char *fcc )
1976 bo_add_8( p_bo, fcc[0] );
1977 bo_add_8( p_bo, fcc[1] );
1978 bo_add_8( p_bo, fcc[2] );
1979 bo_add_8( p_bo, fcc[3] );
1982 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
1986 for( i = 0; i < i_size; i++ )
1988 bo_add_8( p_bo, p_mem[i] );
1992 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
1998 vals[3] = (unsigned char)(i_length & 0x7f);
2000 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
2002 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
2004 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
2006 bo_add_8( p_bo, tag );
2008 if( i_size < 0x00000080 )
2010 bo_add_8( p_bo, vals[3] );
2012 else if( i_size < 0x00004000 )
2014 bo_add_8( p_bo, vals[2] );
2015 bo_add_8( p_bo, vals[3] );
2017 else if( i_size < 0x00200000 )
2019 bo_add_8( p_bo, vals[1] );
2020 bo_add_8( p_bo, vals[2] );
2021 bo_add_8( p_bo, vals[3] );
2023 else if( i_size < 0x10000000 )
2025 bo_add_8( p_bo, vals[0] );
2026 bo_add_8( p_bo, vals[1] );
2027 bo_add_8( p_bo, vals[2] );
2028 bo_add_8( p_bo, vals[3] );
2032 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
2036 for( i = 0; i < p_bo2->i_buffer; i++ )
2038 bo_add_8( p_bo, p_bo2->p_buffer[i] );
2042 static bo_t * box_new( const char *fcc )
2046 if( ( box = malloc( sizeof( bo_t ) ) ) )
2048 bo_init( box, 0, NULL, true );
2050 bo_add_32be ( box, 0 );
2051 bo_add_fourcc( box, fcc );
2057 static bo_t * box_full_new( const char *fcc, uint8_t v, uint32_t f )
2061 if( ( box = malloc( sizeof( bo_t ) ) ) )
2063 bo_init( box, 0, NULL, true );
2065 bo_add_32be ( box, 0 );
2066 bo_add_fourcc( box, fcc );
2067 bo_add_8 ( box, v );
2068 bo_add_24be ( box, f );
2074 static void box_fix( bo_t *box )
2078 memcpy( &box_tmp, box, sizeof( bo_t ) );
2080 box_tmp.i_buffer = 0;
2081 bo_add_32be( &box_tmp, box->i_buffer );
2084 static void box_free( bo_t *box )
2086 free( box->p_buffer );
2090 static void box_gather ( bo_t *box, bo_t *box2 )
2092 bo_add_bo( box, box2 );
2096 static block_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
2101 p_buf = block_New( p_sout, box->i_buffer );
2102 if( box->i_buffer > 0 )
2104 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
2110 static void box_send( sout_mux_t *p_mux, bo_t *box )
2114 p_buf = bo_to_sout( p_mux->p_sout, box );
2117 sout_AccessOutWrite( p_mux->p_access, p_buf );
2120 static int64_t get_timestamp(void)
2122 int64_t i_timestamp = time(NULL);
2124 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2125 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60