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_block.h>
34 #include <vlc_codecs.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( _("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", 1, NULL,
64 FASTSTART_TEXT, FASTSTART_LONGTEXT,
66 set_capability( "sout mux", 5 );
67 add_shortcut( "mp4" );
68 add_shortcut( "mov" );
69 add_shortcut( "3gp" );
70 set_callbacks( Open, Close );
73 /*****************************************************************************
75 *****************************************************************************/
76 static const char *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
128 vlc_bool_t b_fast_start;
136 mp4_stream_t **pp_streams;
149 static void bo_init ( bo_t *, int , uint8_t *, vlc_bool_t );
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 *, 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 ( char *fcc );
163 static bo_t *box_full_new( 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( sout_mux_t *, mp4_stream_t *, block_t *);
175 static block_t *ConvertAVC1( sout_mux_t *, mp4_stream_t *, 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 opend" );
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 ) );
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 = VLC_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, VLC_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 = VLC_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 = VLC_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 )
371 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
372 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
373 *pb_bool = VLC_FALSE;
376 case MUX_GET_ADD_STREAM_WAIT:
377 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
381 case MUX_GET_MIME: /* Not needed, as not streamable */
387 /*****************************************************************************
389 *****************************************************************************/
390 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
392 sout_mux_sys_t *p_sys = p_mux->p_sys;
393 mp4_stream_t *p_stream;
395 switch( p_input->p_fmt->i_codec )
397 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
398 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
399 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
400 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
401 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
402 case VLC_FOURCC( 'm', 'j', 'p', 'b' ):
403 case VLC_FOURCC( 'S', 'V', 'Q', '1' ):
404 case VLC_FOURCC( 'S', 'V', 'Q', '3' ):
405 case VLC_FOURCC( 'H', '2', '6', '3' ):
406 case VLC_FOURCC( 'h', '2', '6', '4' ):
407 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
408 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
410 case VLC_FOURCC( 's', 'u', 'b', 't' ):
411 msg_Warn( p_mux, "subtitle track added like in .mov (even when creating .mp4)" );
414 msg_Err( p_mux, "unsupported codec %4.4s in mp4",
415 (char*)&p_input->p_fmt->i_codec );
419 p_stream = malloc( sizeof( mp4_stream_t ) );
420 es_format_Copy( &p_stream->fmt, p_input->p_fmt );
421 p_stream->i_track_id = p_sys->i_nb_streams + 1;
422 p_stream->i_length_neg = 0;
423 p_stream->i_entry_count = 0;
424 p_stream->i_entry_max = 1000;
426 calloc( p_stream->i_entry_max, sizeof( mp4_entry_t ) );
427 p_stream->i_dts_start = 0;
428 p_stream->i_duration = 0;
430 p_input->p_sys = p_stream;
432 msg_Dbg( p_mux, "adding input" );
434 TAB_APPEND( p_sys->i_nb_streams, p_sys->pp_streams, p_stream );
438 /*****************************************************************************
440 *****************************************************************************/
441 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
443 msg_Dbg( p_mux, "removing input" );
447 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
453 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
455 block_fifo_t *p_fifo = p_mux->pp_inputs[i]->p_fifo;
458 if( p_fifo->i_depth <= 1 )
460 if( p_mux->pp_inputs[i]->p_fmt->i_cat != SPU_ES )
462 return -1; // wait that all fifo have at least 2 packets
464 /* For SPU, we wait only 1 packet */
468 p_buf = block_FifoShow( p_fifo );
469 if( i_stream < 0 || p_buf->i_dts < i_dts )
471 i_dts = p_buf->i_dts;
477 *pi_stream = i_stream;
486 /*****************************************************************************
488 *****************************************************************************/
489 static int Mux( sout_mux_t *p_mux )
491 sout_mux_sys_t *p_sys = p_mux->p_sys;
495 sout_input_t *p_input;
497 mp4_stream_t *p_stream;
501 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
503 return( VLC_SUCCESS );
506 p_input = p_mux->pp_inputs[i_stream];
507 p_stream = (mp4_stream_t*)p_input->p_sys;
510 p_data = block_FifoGet( p_input->p_fifo );
511 if( p_stream->fmt.i_codec == VLC_FOURCC( 'h', '2', '6', '4' ) )
513 p_data = ConvertAVC1( p_mux, p_stream, p_data );
515 else if( p_stream->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
517 p_data = ConvertSUBT( p_mux, p_stream, p_data );
519 if( p_data == NULL ) goto again;
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 an empty sub" ) ;
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 block_t *ConvertAVC1( sout_mux_t *p_mux, mp4_stream_t *tk, block_t *p_block )
668 uint8_t *last = p_block->p_buffer; /* Assume it starts with 0x00000001 */
669 uint8_t *dat = &p_block->p_buffer[4];
670 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
673 /* Replace the 4 bytes start code with 4 bytes size,
674 * FIXME are all startcodes 4 bytes ? (I don't think :( */
679 while( dat < end - 4 )
681 if( dat[0] == 0x00 && dat[1] == 0x00 &&
682 dat[2] == 0x00 && dat[3] == 0x01 )
694 i_size = dat - &last[4];
695 last[0] = ( i_size >> 24 )&0xff;
696 last[1] = ( i_size >> 16 )&0xff;
697 last[2] = ( i_size >> 8 )&0xff;
698 last[3] = ( i_size )&0xff;
700 /* Skip blocks with SPS/PPS */
701 if( (last[4]&0x1f) == 7 || (last[4]&0x1f) == 8 )
703 ; // FIXME Find a way to skip dat without frelling everything
711 static int GetDescrLength( int i_size )
713 if( i_size < 0x00000080 )
715 else if( i_size < 0x00004000 )
717 else if( i_size < 0x00200000 )
723 static bo_t *GetESDS( mp4_stream_t *p_stream )
727 int i_object_type_indication;
728 int i_decoder_specific_info_size;
730 int64_t i_bitrate_avg = 0;
731 int64_t i_bitrate_max = 0;
733 /* Compute avg/max bitrate */
734 for( i = 0; i < p_stream->i_entry_count; i++ )
736 i_bitrate_avg += p_stream->entry[i].i_size;
737 if( p_stream->entry[i].i_length > 0)
739 int64_t i_bitrate = I64C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
740 if( i_bitrate > i_bitrate_max )
741 i_bitrate_max = i_bitrate;
745 if( p_stream->i_duration > 0 )
746 i_bitrate_avg = I64C(8000000) * i_bitrate_avg / p_stream->i_duration;
749 if( i_bitrate_max <= 1 )
750 i_bitrate_max = 0x7fffffff;
753 if( p_stream->fmt.i_extra > 0 )
755 i_decoder_specific_info_size =
756 GetDescrLength( p_stream->fmt.i_extra );
760 i_decoder_specific_info_size = 0;
763 esds = box_full_new( "esds", 0, 0 );
766 bo_add_descr( esds, 0x03, 3 +
767 GetDescrLength( 13 + i_decoder_specific_info_size ) +
768 GetDescrLength( 1 ) );
769 bo_add_16be( esds, p_stream->i_track_id );
770 bo_add_8 ( esds, 0x1f ); // flags=0|streamPriority=0x1f
772 /* DecoderConfigDescr */
773 bo_add_descr( esds, 0x04, 13 + i_decoder_specific_info_size );
775 switch( p_stream->fmt.i_codec )
777 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
778 i_object_type_indication = 0x20;
780 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
781 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
782 i_object_type_indication = 0x60;
784 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
785 /* FIXME for mpeg2-aac == 0x66->0x68 */
786 i_object_type_indication = 0x40;
788 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
789 i_object_type_indication =
790 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
793 i_object_type_indication = 0x00;
796 i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
798 bo_add_8 ( esds, i_object_type_indication );
799 bo_add_8 ( esds, ( i_stream_type << 2 ) | 1 );
800 bo_add_24be( esds, 1024 * 1024 ); // bufferSizeDB
801 bo_add_32be( esds, i_bitrate_max ); // maxBitrate
802 bo_add_32be( esds, i_bitrate_avg ); // avgBitrate
804 if( p_stream->fmt.i_extra > 0 )
808 /* DecoderSpecificInfo */
809 bo_add_descr( esds, 0x05, p_stream->fmt.i_extra );
811 for( i = 0; i < p_stream->fmt.i_extra; i++ )
813 bo_add_8( esds, ((uint8_t*)p_stream->fmt.p_extra)[i] );
817 /* SL_Descr mandatory */
818 bo_add_descr( esds, 0x06, 1 );
819 bo_add_8 ( esds, 0x02 ); // sl_predefined
826 static bo_t *GetWaveTag( mp4_stream_t *p_stream )
831 wave = box_new( "wave" );
833 box = box_new( "frma" );
834 bo_add_fourcc( box, "mp4a" );
836 box_gather( wave, box );
838 box = box_new( "mp4a" );
839 bo_add_32be( box, 0 );
841 box_gather( wave, box );
843 box = GetESDS( p_stream );
845 box_gather( wave, box );
847 box = box_new( "srcq" );
848 bo_add_32be( box, 0x40 );
850 box_gather( wave, box );
853 bo_add_32be( wave, 8 ); /* new empty box */
854 bo_add_32be( wave, 0 ); /* box label */
861 static bo_t *GetDamrTag( mp4_stream_t *p_stream )
865 damr = box_new( "damr" );
867 bo_add_fourcc( damr, "REFC" );
870 if( p_stream->fmt.i_codec == VLC_FOURCC( 's', 'a', 'm', 'r' ) )
871 bo_add_16be( damr, 0x81ff ); /* Mode set (all modes for AMR_NB) */
873 bo_add_16be( damr, 0x83ff ); /* Mode set (all modes for AMR_WB) */
874 bo_add_16be( damr, 0x1 ); /* Mode change period (no restriction) */
881 static bo_t *GetD263Tag( mp4_stream_t *p_stream )
885 d263 = box_new( "d263" );
887 bo_add_fourcc( d263, "VLC " );
888 bo_add_16be( d263, 0xa );
896 static bo_t *GetAvcCTag( mp4_stream_t *p_stream )
899 uint8_t *p_sps = NULL;
900 uint8_t *p_pps = NULL;
904 if( p_stream->fmt.i_extra > 0 )
906 /* FIXME: take into account multiple sps/pps */
907 uint8_t *p_buffer = p_stream->fmt.p_extra;
908 int i_buffer = p_stream->fmt.i_extra;
910 while( i_buffer > 4 &&
911 p_buffer[0] == 0 && p_buffer[1] == 0 &&
912 p_buffer[2] == 0 && p_buffer[3] == 1 )
914 const int i_nal_type = p_buffer[4]&0x1f;
919 //msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
921 for( i_offset = 1; i_offset+3 < i_buffer ; i_offset++)
923 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 &&
924 p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
926 /* we found another startcode */
927 i_startcode = i_offset;
931 i_size = i_startcode ? i_startcode : i_buffer;
932 if( i_nal_type == 7 )
934 p_sps = &p_buffer[4];
935 i_sps_size = i_size - 4;
937 if( i_nal_type == 8 )
939 p_pps = &p_buffer[4];
940 i_pps_size = i_size - 4;
947 /* FIXME use better value */
948 avcC = box_new( "avcC" );
949 bo_add_8( avcC, 1 ); /* configuration version */
950 bo_add_8( avcC, i_sps_size ? p_sps[1] : 77 );
951 bo_add_8( avcC, i_sps_size ? p_sps[2] : 64 );
952 bo_add_8( avcC, i_sps_size ? p_sps[3] : 30 ); /* level, 5.1 */
953 bo_add_8( avcC, 0xff ); /* 0b11111100 | lengthsize = 0x11 */
955 bo_add_8( avcC, 0xe0 | (i_sps_size > 0 ? 1 : 0) ); /* 0b11100000 | sps_count */
958 bo_add_16be( avcC, i_sps_size );
959 bo_add_mem( avcC, i_sps_size, p_sps );
962 bo_add_8( avcC, (i_pps_size > 0 ? 1 : 0) ); /* pps_count */
965 bo_add_16be( avcC, i_pps_size );
966 bo_add_mem( avcC, i_pps_size, p_pps );
973 /* TODO: No idea about these values */
974 static bo_t *GetSVQ3Tag( mp4_stream_t *p_stream )
976 bo_t *smi = box_new( "SMI " );
978 if( p_stream->fmt.i_extra > 0x4e )
980 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
981 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
983 while( p + 8 < p_end )
985 int i_size = GetDWBE( p );
988 /* FIXME handle 1 as long size */
991 if( !strncmp( (const char *)&p[4], "SMI ", 4 ) )
993 bo_add_mem( smi, p_end - p - 8, &p[8] );
1000 /* Create a dummy one in fallback */
1001 bo_add_fourcc( smi, "SEQH" );
1002 bo_add_32be( smi, 0x5 );
1003 bo_add_32be( smi, 0xe2c0211d );
1004 bo_add_8( smi, 0xc0 );
1010 static bo_t *GetUdtaTag( sout_mux_t *p_mux )
1012 sout_mux_sys_t *p_sys = p_mux->p_sys;
1013 bo_t *udta = box_new( "udta" );
1014 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
1018 for( i_track = 0; i_track < p_sys->i_nb_streams; i_track++ )
1020 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
1022 if( p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','v') ||
1023 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1025 bo_t *box = box_new( "\251req" );
1027 bo_add_16be( box, sizeof("QuickTime 6.0 or greater") - 1);
1028 bo_add_16be( box, 0 );
1029 bo_add_mem( box, sizeof("QuickTime 6.0 or greater") - 1,
1030 (uint8_t *)"QuickTime 6.0 or greater" );
1032 box_gather( udta, box );
1039 bo_t *box = box_new( "\251enc" );
1041 bo_add_16be( box, sizeof(PACKAGE_STRING " stream output") - 1);
1042 bo_add_16be( box, 0 );
1043 bo_add_mem( box, sizeof(PACKAGE_STRING " stream output") - 1,
1044 (uint8_t*)PACKAGE_STRING " stream output" );
1046 box_gather( udta, box );
1052 #define ADD_META_BOX( type, box_string ) { \
1054 if( p_meta->psz_##type ) box = box_new( "\251" box_string ); \
1057 bo_add_16be( box, strlen( p_meta->psz_##type ) ); \
1058 bo_add_16be( box, 0 ); \
1059 bo_add_mem( box, strlen( p_meta->psz_##type ), \
1060 (uint8_t*)(p_meta->psz_##type ) ); \
1062 box_gather( udta, box ); \
1065 ADD_META_BOX( title, "nam" );
1066 ADD_META_BOX( artist, "ART" );
1067 ADD_META_BOX( genre, "gen" );
1068 ADD_META_BOX( copyright, "cpy" );
1069 ADD_META_BOX( description, "des" );
1070 ADD_META_BOX( date, "day" );
1071 ADD_META_BOX( url, "url" );
1079 static bo_t *GetSounBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1081 sout_mux_sys_t *p_sys = p_mux->p_sys;
1082 vlc_bool_t b_descr = VLC_FALSE;
1087 switch( p_stream->fmt.i_codec )
1089 case VLC_FOURCC('m','p','4','a'):
1090 memcpy( fcc, "mp4a", 4 );
1094 case VLC_FOURCC('s','a','m','r'):
1095 case VLC_FOURCC('s','a','w','b'):
1096 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1100 case VLC_FOURCC('m','p','g','a'):
1102 memcpy( fcc, ".mp3", 4 );
1105 memcpy( fcc, "mp4a", 4 );
1111 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1115 soun = box_new( fcc );
1116 for( i = 0; i < 6; i++ )
1118 bo_add_8( soun, 0 ); // reserved;
1120 bo_add_16be( soun, 1 ); // data-reference-index
1122 /* SoundDescription */
1124 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1126 bo_add_16be( soun, 1 ); // version 1;
1130 bo_add_16be( soun, 0 ); // version 0;
1132 bo_add_16be( soun, 0 ); // revision level (0)
1133 bo_add_32be( soun, 0 ); // vendor
1135 bo_add_16be( soun, p_stream->fmt.audio.i_channels );
1137 bo_add_16be( soun, p_stream->fmt.audio.i_bitspersample ?
1138 p_stream->fmt.audio.i_bitspersample : 16 );
1139 bo_add_16be( soun, -2 ); // compression id
1140 bo_add_16be( soun, 0 ); // packet size (0)
1141 bo_add_16be( soun, p_stream->fmt.audio.i_rate ); // sampleratehi
1142 bo_add_16be( soun, 0 ); // sampleratelo
1144 /* Extended data for SoundDescription V1 */
1146 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1148 /* samples per packet */
1149 bo_add_32be( soun, p_stream->fmt.audio.i_frame_length );
1150 bo_add_32be( soun, 1536 ); /* bytes per packet */
1151 bo_add_32be( soun, 2 ); /* bytes per frame */
1152 /* bytes per sample */
1153 bo_add_32be( soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1156 /* Add an ES Descriptor */
1162 p_stream->fmt.i_codec == VLC_FOURCC('m','p','4','a') )
1164 box = GetWaveTag( p_stream );
1166 else if( p_stream->fmt.i_codec == VLC_FOURCC('s','a','m','r') )
1168 box = GetDamrTag( p_stream );
1172 box = GetESDS( p_stream );
1175 box_gather( soun, box );
1183 static bo_t *GetVideBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1190 switch( p_stream->fmt.i_codec )
1192 case VLC_FOURCC('m','p','4','v'):
1193 case VLC_FOURCC('m','p','g','v'):
1194 memcpy( fcc, "mp4v", 4 );
1197 case VLC_FOURCC('M','J','P','G'):
1198 memcpy( fcc, "mjpa", 4 );
1201 case VLC_FOURCC('S','V','Q','1'):
1202 memcpy( fcc, "SVQ1", 4 );
1205 case VLC_FOURCC('S','V','Q','3'):
1206 memcpy( fcc, "SVQ3", 4 );
1209 case VLC_FOURCC('H','2','6','3'):
1210 memcpy( fcc, "s263", 4 );
1213 case VLC_FOURCC('h','2','6','4'):
1214 memcpy( fcc, "avc1", 4 );
1218 memcpy( fcc, (char*)&p_stream->fmt.i_codec, 4 );
1222 vide = box_new( fcc );
1223 for( i = 0; i < 6; i++ )
1225 bo_add_8( vide, 0 ); // reserved;
1227 bo_add_16be( vide, 1 ); // data-reference-index
1229 bo_add_16be( vide, 0 ); // predefined;
1230 bo_add_16be( vide, 0 ); // reserved;
1231 for( i = 0; i < 3; i++ )
1233 bo_add_32be( vide, 0 ); // predefined;
1236 bo_add_16be( vide, p_stream->fmt.video.i_width ); // i_width
1237 bo_add_16be( vide, p_stream->fmt.video.i_height ); // i_height
1239 bo_add_32be( vide, 0x00480000 ); // h 72dpi
1240 bo_add_32be( vide, 0x00480000 ); // v 72dpi
1242 bo_add_32be( vide, 0 ); // data size, always 0
1243 bo_add_16be( vide, 1 ); // frames count per sample
1246 for( i = 0; i < 32; i++ )
1248 bo_add_8( vide, 0 );
1251 bo_add_16be( vide, 0x18 ); // depth
1252 bo_add_16be( vide, 0xffff ); // predefined
1254 /* add an ES Descriptor */
1255 switch( p_stream->fmt.i_codec )
1257 case VLC_FOURCC('m','p','4','v'):
1258 case VLC_FOURCC('m','p','g','v'):
1260 bo_t *esds = GetESDS( p_stream );
1263 box_gather( vide, esds );
1267 case VLC_FOURCC('H','2','6','3'):
1269 bo_t *d263 = GetD263Tag( p_stream );
1272 box_gather( vide, d263 );
1276 case VLC_FOURCC('S','V','Q','3'):
1278 bo_t *esds = GetSVQ3Tag( p_stream );
1281 box_gather( vide, esds );
1285 case VLC_FOURCC('h','2','6','4'):
1286 box_gather( vide, GetAvcCTag( p_stream ) );
1298 static bo_t *GetTextBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1301 bo_t *text = box_new( "text" );
1304 for( i = 0; i < 6; i++ )
1306 bo_add_8( text, 0 ); // reserved;
1308 bo_add_16be( text, 1 ); // data-reference-index
1310 bo_add_32be( text, 0 ); // display flags
1311 bo_add_32be( text, 0 ); // justification
1312 for( i = 0; i < 3; i++ )
1314 bo_add_16be( text, 0 ); // back ground color
1317 bo_add_16be( text, 0 ); // box text
1318 bo_add_16be( text, 0 ); // box text
1319 bo_add_16be( text, 0 ); // box text
1320 bo_add_16be( text, 0 ); // box text
1322 bo_add_64be( text, 0 ); // reserved
1323 for( i = 0; i < 3; i++ )
1325 bo_add_16be( text, 0xff ); // foreground color
1328 bo_add_8 ( text, 9 );
1329 bo_add_mem( text, 9, (uint8_t*)"Helvetica" );
1336 static bo_t *GetStblBox( sout_mux_t *p_mux, mp4_stream_t *p_stream )
1338 sout_mux_sys_t *p_sys = p_mux->p_sys;
1339 unsigned int i_chunk, i_stsc_last_val, i_stsc_entries, i, i_index;
1340 bo_t *stbl, *stsd, *stts, *stco, *stsc, *stsz, *stss;
1341 uint32_t i_timescale;
1342 int64_t i_dts, i_dts_q;
1344 stbl = box_new( "stbl" );
1346 /* sample description */
1347 stsd = box_full_new( "stsd", 0, 0 );
1348 bo_add_32be( stsd, 1 );
1349 if( p_stream->fmt.i_cat == AUDIO_ES )
1351 bo_t *soun = GetSounBox( p_mux, p_stream );
1352 box_gather( stsd, soun );
1354 else if( p_stream->fmt.i_cat == VIDEO_ES )
1356 bo_t *vide = GetVideBox( p_mux, p_stream );
1357 box_gather( stsd, vide );
1359 else if( p_stream->fmt.i_cat == SPU_ES )
1361 box_gather( stsd, GetTextBox( p_mux, p_stream ) );
1365 /* chunk offset table */
1366 if( p_sys->i_pos >= (((uint64_t)0x1) << 32) )
1368 /* 64 bits version */
1369 p_stream->b_stco64 = VLC_TRUE;
1370 stco = box_full_new( "co64", 0, 0 );
1374 /* 32 bits version */
1375 p_stream->b_stco64 = VLC_FALSE;
1376 stco = box_full_new( "stco", 0, 0 );
1378 bo_add_32be( stco, 0 ); // entry-count (fixed latter)
1380 /* sample to chunk table */
1381 stsc = box_full_new( "stsc", 0, 0 );
1382 bo_add_32be( stsc, 0 ); // entry-count (fixed latter)
1384 for( i_chunk = 0, i_stsc_last_val = 0, i_stsc_entries = 0, i = 0;
1385 i < p_stream->i_entry_count; i_chunk++ )
1389 if( p_stream->b_stco64 )
1390 bo_add_64be( stco, p_stream->entry[i].i_pos );
1392 bo_add_32be( stco, p_stream->entry[i].i_pos );
1394 while( i < p_stream->i_entry_count )
1396 if( i + 1 < p_stream->i_entry_count &&
1397 p_stream->entry[i].i_pos + p_stream->entry[i].i_size
1398 != p_stream->entry[i + 1].i_pos )
1407 /* Add entry to the stsc table */
1408 if( i_stsc_last_val != i - i_first )
1410 bo_add_32be( stsc, 1 + i_chunk ); // first-chunk
1411 bo_add_32be( stsc, i - i_first ) ; // samples-per-chunk
1412 bo_add_32be( stsc, 1 ); // sample-descr-index
1413 i_stsc_last_val = i - i_first;
1418 /* Fix stco entry count */
1419 bo_fix_32be( stco, 12, i_chunk );
1420 msg_Dbg( p_mux, "created %d chunks (stco)", i_chunk );
1423 /* Fix stsc entry count */
1424 bo_fix_32be( stsc, 12, i_stsc_entries );
1428 stts = box_full_new( "stts", 0, 0 );
1429 bo_add_32be( stts, 0 ); // entry-count (fixed latter)
1431 if( p_stream->fmt.i_cat == AUDIO_ES )
1432 i_timescale = p_stream->fmt.audio.i_rate;
1436 /* first, create quantified length */
1437 for( i = 0, i_dts = 0, i_dts_q = 0; i < p_stream->i_entry_count; i++ )
1439 int64_t i_dts_deq = i_dts_q * I64C(1000000) / (int64_t)i_timescale;
1440 int64_t i_delta = p_stream->entry[i].i_length + i_dts - i_dts_deq;
1442 i_dts += p_stream->entry[i].i_length;
1444 p_stream->entry[i].i_length =
1445 i_delta * (int64_t)i_timescale / I64C(1000000);
1447 i_dts_q += p_stream->entry[i].i_length;
1449 /* then write encoded table */
1450 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i_index++)
1453 int64_t i_delta = p_stream->entry[i].i_length;
1455 while( i < p_stream->i_entry_count )
1458 if( i >= p_stream->i_entry_count ||
1459 p_stream->entry[i].i_length != i_delta )
1465 bo_add_32be( stts, i - i_first ); // sample-count
1466 bo_add_32be( stts, i_delta ); // sample-delta
1468 bo_fix_32be( stts, 12, i_index );
1471 /* FIXME add ctts ?? FIXME */
1473 stsz = box_full_new( "stsz", 0, 0 );
1474 bo_add_32be( stsz, 0 ); // sample-size
1475 bo_add_32be( stsz, p_stream->i_entry_count ); // sample-count
1476 for( i = 0; i < p_stream->i_entry_count; i++ )
1478 bo_add_32be( stsz, p_stream->entry[i].i_size ); // sample-size
1482 /* create stss table */
1484 for( i = 0, i_index = 0; i < p_stream->i_entry_count; i++ )
1486 if( p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I )
1490 stss = box_full_new( "stss", 0, 0 );
1491 bo_add_32be( stss, 0 ); /* fixed later */
1493 bo_add_32be( stss, 1 + i );
1499 bo_fix_32be( stss, 12, i_index );
1503 /* Now gather all boxes into stbl */
1504 box_gather( stbl, stsd );
1505 box_gather( stbl, stts );
1508 box_gather( stbl, stss );
1510 box_gather( stbl, stsc );
1511 box_gather( stbl, stsz );
1512 p_stream->i_stco_pos = stbl->i_buffer + 16;
1513 box_gather( stbl, stco );
1521 static int64_t get_timestamp();
1523 static uint32_t mvhd_matrix[9] =
1524 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1526 static bo_t *GetMoovBox( sout_mux_t *p_mux )
1528 sout_mux_sys_t *p_sys = p_mux->p_sys;
1533 uint32_t i_movie_timescale = 90000;
1534 int64_t i_movie_duration = 0;
1536 moov = box_new( "moov" );
1538 /* Create general info */
1539 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1541 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1542 i_movie_duration = __MAX( i_movie_duration, p_stream->i_duration );
1544 msg_Dbg( p_mux, "movie duration %ds",
1545 (uint32_t)( i_movie_duration / (mtime_t)1000000 ) );
1547 i_movie_duration = i_movie_duration * i_movie_timescale / 1000000;
1549 /* *** add /moov/mvhd *** */
1550 if( !p_sys->b_64_ext )
1552 mvhd = box_full_new( "mvhd", 0, 0 );
1553 bo_add_32be( mvhd, get_timestamp() ); // creation time
1554 bo_add_32be( mvhd, get_timestamp() ); // modification time
1555 bo_add_32be( mvhd, i_movie_timescale); // timescale
1556 bo_add_32be( mvhd, i_movie_duration ); // duration
1560 mvhd = box_full_new( "mvhd", 1, 0 );
1561 bo_add_64be( mvhd, get_timestamp() ); // creation time
1562 bo_add_64be( mvhd, get_timestamp() ); // modification time
1563 bo_add_32be( mvhd, i_movie_timescale); // timescale
1564 bo_add_64be( mvhd, i_movie_duration ); // duration
1566 bo_add_32be( mvhd, 0x10000 ); // rate
1567 bo_add_16be( mvhd, 0x100 ); // volume
1568 bo_add_16be( mvhd, 0 ); // reserved
1569 for( i = 0; i < 2; i++ )
1571 bo_add_32be( mvhd, 0 ); // reserved
1573 for( i = 0; i < 9; i++ )
1575 bo_add_32be( mvhd, mvhd_matrix[i] );// matrix
1577 for( i = 0; i < 6; i++ )
1579 bo_add_32be( mvhd, 0 ); // pre-defined
1582 /* Next available track id */
1583 bo_add_32be( mvhd, p_sys->i_nb_streams + 1 ); // next-track-id
1586 box_gather( moov, mvhd );
1588 for( i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++ )
1590 mp4_stream_t *p_stream;
1591 uint32_t i_timescale;
1593 bo_t *trak, *tkhd, *edts, *elst, *mdia, *mdhd, *hdlr;
1594 bo_t *minf, *dinf, *dref, *url, *stbl;
1596 p_stream = p_sys->pp_streams[i_trak];
1598 if( p_stream->fmt.i_cat == AUDIO_ES )
1599 i_timescale = p_stream->fmt.audio.i_rate;
1603 /* *** add /moov/trak *** */
1604 trak = box_new( "trak" );
1606 /* *** add /moov/trak/tkhd *** */
1607 if( !p_sys->b_64_ext )
1610 tkhd = box_full_new( "tkhd", 0, 0x0f );
1612 tkhd = box_full_new( "tkhd", 0, 1 );
1614 bo_add_32be( tkhd, get_timestamp() ); // creation time
1615 bo_add_32be( tkhd, get_timestamp() ); // modification time
1616 bo_add_32be( tkhd, p_stream->i_track_id );
1617 bo_add_32be( tkhd, 0 ); // reserved 0
1618 bo_add_32be( tkhd, p_stream->i_duration *
1619 (int64_t)i_movie_timescale /
1620 (mtime_t)1000000 ); // duration
1625 tkhd = box_full_new( "tkhd", 1, 0x0f );
1627 tkhd = box_full_new( "tkhd", 1, 1 );
1629 bo_add_64be( tkhd, get_timestamp() ); // creation time
1630 bo_add_64be( tkhd, get_timestamp() ); // modification time
1631 bo_add_32be( tkhd, p_stream->i_track_id );
1632 bo_add_32be( tkhd, 0 ); // reserved 0
1633 bo_add_64be( tkhd, p_stream->i_duration *
1634 (int64_t)i_movie_timescale /
1635 (mtime_t)1000000 ); // duration
1638 for( i = 0; i < 2; i++ )
1640 bo_add_32be( tkhd, 0 ); // reserved
1642 bo_add_16be( tkhd, 0 ); // layer
1643 bo_add_16be( tkhd, 0 ); // pre-defined
1645 bo_add_16be( tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0 );
1646 bo_add_16be( tkhd, 0 ); // reserved
1647 for( i = 0; i < 9; i++ )
1649 bo_add_32be( tkhd, mvhd_matrix[i] ); // matrix
1651 if( p_stream->fmt.i_cat == AUDIO_ES )
1653 bo_add_32be( tkhd, 0 ); // width (presentation)
1654 bo_add_32be( tkhd, 0 ); // height(presentation)
1656 else if( p_stream->fmt.i_cat == VIDEO_ES )
1658 int i_width = p_stream->fmt.video.i_width << 16;
1659 if( p_stream->fmt.video.i_aspect > 0 )
1661 i_width = (int64_t)p_stream->fmt.video.i_aspect *
1662 ((int64_t)p_stream->fmt.video.i_height << 16) /
1665 // width (presentation)
1666 bo_add_32be( tkhd, i_width );
1667 // height(presentation)
1668 bo_add_32be( tkhd, p_stream->fmt.video.i_height << 16 );
1672 int i_width = 320 << 16;
1675 for( i = 0; i < p_sys->i_nb_streams; i++ )
1677 mp4_stream_t *tk = p_sys->pp_streams[i];
1678 if( tk->fmt.i_cat == VIDEO_ES )
1680 if( p_stream->fmt.video.i_aspect )
1681 i_width = (int64_t)p_stream->fmt.video.i_aspect *
1682 ((int64_t)p_stream->fmt.video.i_height<<16) / VOUT_ASPECT_FACTOR;
1684 i_width = p_stream->fmt.video.i_width << 16;
1685 i_height = p_stream->fmt.video.i_height;
1689 bo_add_32be( tkhd, i_width ); // width (presentation)
1690 bo_add_32be( tkhd, i_height << 16 ); // height(presentation)
1694 box_gather( trak, tkhd );
1696 /* *** add /moov/trak/edts and elst */
1697 edts = box_new( "edts" );
1698 elst = box_full_new( "elst", p_sys->b_64_ext ? 1 : 0, 0 );
1699 if( p_stream->i_dts_start > p_sys->i_dts_start )
1701 bo_add_32be( elst, 2 );
1703 if( p_sys->b_64_ext )
1705 bo_add_64be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1706 i_movie_timescale / I64C(1000000) );
1707 bo_add_64be( elst, -1 );
1711 bo_add_32be( elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1712 i_movie_timescale / I64C(1000000) );
1713 bo_add_32be( elst, -1 );
1715 bo_add_16be( elst, 1 );
1716 bo_add_16be( elst, 0 );
1720 bo_add_32be( elst, 1 );
1722 if( p_sys->b_64_ext )
1724 bo_add_64be( elst, p_stream->i_duration *
1725 i_movie_timescale / I64C(1000000) );
1726 bo_add_64be( elst, 0 );
1730 bo_add_32be( elst, p_stream->i_duration *
1731 i_movie_timescale / I64C(1000000) );
1732 bo_add_32be( elst, 0 );
1734 bo_add_16be( elst, 1 );
1735 bo_add_16be( elst, 0 );
1738 box_gather( edts, elst );
1740 box_gather( trak, edts );
1742 /* *** add /moov/trak/mdia *** */
1743 mdia = box_new( "mdia" );
1746 if( !p_sys->b_64_ext )
1748 mdhd = box_full_new( "mdhd", 0, 0 );
1749 bo_add_32be( mdhd, get_timestamp() ); // creation time
1750 bo_add_32be( mdhd, get_timestamp() ); // modification time
1751 bo_add_32be( mdhd, i_timescale); // timescale
1752 bo_add_32be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1753 (mtime_t)1000000 ); // duration
1757 mdhd = box_full_new( "mdhd", 1, 0 );
1758 bo_add_64be( mdhd, get_timestamp() ); // creation time
1759 bo_add_64be( mdhd, get_timestamp() ); // modification time
1760 bo_add_32be( mdhd, i_timescale); // timescale
1761 bo_add_64be( mdhd, p_stream->i_duration * (int64_t)i_timescale /
1762 (mtime_t)1000000 ); // duration
1765 if( p_stream->fmt.psz_language )
1767 char *psz = p_stream->fmt.psz_language;
1768 const iso639_lang_t *pl = NULL;
1769 uint16_t lang = 0x0;
1771 if( strlen( psz ) == 2 )
1773 pl = GetLang_1( psz );
1775 else if( strlen( psz ) == 3 )
1777 pl = GetLang_2B( psz );
1778 if( !strcmp( pl->psz_iso639_1, "??" ) )
1780 pl = GetLang_2T( psz );
1783 if( pl && strcmp( pl->psz_iso639_1, "??" ) )
1785 lang = ( ( pl->psz_iso639_2T[0] - 0x60 ) << 10 ) |
1786 ( ( pl->psz_iso639_2T[1] - 0x60 ) << 5 ) |
1787 ( ( pl->psz_iso639_2T[2] - 0x60 ) );
1789 bo_add_16be( mdhd, lang ); // language
1793 bo_add_16be( mdhd, 0 ); // language
1795 bo_add_16be( mdhd, 0 ); // predefined
1797 box_gather( mdia, mdhd );
1799 /* handler reference */
1800 hdlr = box_full_new( "hdlr", 0, 0 );
1803 bo_add_fourcc( hdlr, "mhlr" ); // media handler
1805 bo_add_32be( hdlr, 0 );
1807 if( p_stream->fmt.i_cat == AUDIO_ES )
1808 bo_add_fourcc( hdlr, "soun" );
1809 else if( p_stream->fmt.i_cat == VIDEO_ES )
1810 bo_add_fourcc( hdlr, "vide" );
1811 else if( p_stream->fmt.i_cat == SPU_ES )
1812 bo_add_fourcc( hdlr, "text" );
1814 bo_add_32be( hdlr, 0 ); // reserved
1815 bo_add_32be( hdlr, 0 ); // reserved
1816 bo_add_32be( hdlr, 0 ); // reserved
1819 bo_add_8( hdlr, 12 ); /* Pascal string for .mov */
1821 if( p_stream->fmt.i_cat == AUDIO_ES )
1822 bo_add_mem( hdlr, 12, (uint8_t*)"SoundHandler" );
1823 else if( p_stream->fmt.i_cat == VIDEO_ES )
1824 bo_add_mem( hdlr, 12, (uint8_t*)"VideoHandler" );
1826 bo_add_mem( hdlr, 12, (uint8_t*)"Text Handler" );
1829 bo_add_8( hdlr, 0 ); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
1832 box_gather( mdia, hdlr );
1835 minf = box_new( "minf" );
1838 if( p_stream->fmt.i_cat == AUDIO_ES )
1842 smhd = box_full_new( "smhd", 0, 0 );
1843 bo_add_16be( smhd, 0 ); // balance
1844 bo_add_16be( smhd, 0 ); // reserved
1847 box_gather( minf, smhd );
1849 else if( p_stream->fmt.i_cat == VIDEO_ES )
1853 vmhd = box_full_new( "vmhd", 0, 1 );
1854 bo_add_16be( vmhd, 0 ); // graphicsmode
1855 for( i = 0; i < 3; i++ )
1857 bo_add_16be( vmhd, 0 ); // opcolor
1861 box_gather( minf, vmhd );
1863 else if( p_stream->fmt.i_cat == SPU_ES )
1865 bo_t *gmhd = box_new( "gmhd" );
1866 bo_t *gmin = box_full_new( "gmin", 0, 1 );
1868 bo_add_16be( gmin, 0 ); // graphicsmode
1869 for( i = 0; i < 3; i++ )
1871 bo_add_16be( gmin, 0 ); // opcolor
1873 bo_add_16be( gmin, 0 ); // balance
1874 bo_add_16be( gmin, 0 ); // reserved
1877 box_gather( gmhd, gmin );
1880 box_gather( minf, gmhd );
1884 dinf = box_new( "dinf" );
1885 dref = box_full_new( "dref", 0, 0 );
1886 bo_add_32be( dref, 1 );
1887 url = box_full_new( "url ", 0, 0x01 );
1889 box_gather( dref, url );
1891 box_gather( dinf, dref );
1893 /* append dinf to mdia */
1895 box_gather( minf, dinf );
1898 stbl = GetStblBox( p_mux, p_stream );
1900 /* append stbl to minf */
1901 p_stream->i_stco_pos += minf->i_buffer;
1902 box_gather( minf, stbl );
1904 /* append minf to mdia */
1906 p_stream->i_stco_pos += mdia->i_buffer;
1907 box_gather( mdia, minf );
1909 /* append mdia to trak */
1911 p_stream->i_stco_pos += trak->i_buffer;
1912 box_gather( trak, mdia );
1914 /* append trak to moov */
1916 p_stream->i_stco_pos += moov->i_buffer;
1917 box_gather( moov, trak );
1920 /* Add user data tags */
1921 box_gather( moov, GetUdtaTag( p_mux ) );
1927 /****************************************************************************/
1929 static void bo_init( bo_t *p_bo, int i_size, uint8_t *p_buffer,
1934 p_bo->i_buffer_size = __MAX( i_size, 1024 );
1935 p_bo->p_buffer = malloc( p_bo->i_buffer_size );
1939 p_bo->i_buffer_size = i_size;
1940 p_bo->p_buffer = p_buffer;
1943 p_bo->b_grow = b_grow;
1947 static void bo_add_8( bo_t *p_bo, uint8_t i )
1949 if( p_bo->i_buffer < p_bo->i_buffer_size )
1951 p_bo->p_buffer[p_bo->i_buffer] = i;
1953 else if( p_bo->b_grow )
1955 p_bo->i_buffer_size += 1024;
1956 p_bo->p_buffer = realloc( p_bo->p_buffer, p_bo->i_buffer_size );
1958 p_bo->p_buffer[p_bo->i_buffer] = i;
1964 static void bo_add_16be( bo_t *p_bo, uint16_t i )
1966 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1967 bo_add_8( p_bo, i &0xff );
1970 static void bo_add_24be( bo_t *p_bo, uint32_t i )
1972 bo_add_8( p_bo, ( ( i >> 16) &0xff ) );
1973 bo_add_8( p_bo, ( ( i >> 8) &0xff ) );
1974 bo_add_8( p_bo, ( i &0xff ) );
1976 static void bo_add_32be( bo_t *p_bo, uint32_t i )
1978 bo_add_16be( p_bo, ( ( i >> 16) &0xffff ) );
1979 bo_add_16be( p_bo, i &0xffff );
1982 static void bo_fix_32be ( bo_t *p_bo, int i_pos, uint32_t i)
1984 p_bo->p_buffer[i_pos ] = ( i >> 24 )&0xff;
1985 p_bo->p_buffer[i_pos + 1] = ( i >> 16 )&0xff;
1986 p_bo->p_buffer[i_pos + 2] = ( i >> 8 )&0xff;
1987 p_bo->p_buffer[i_pos + 3] = ( i )&0xff;
1990 static void bo_add_64be( bo_t *p_bo, uint64_t i )
1992 bo_add_32be( p_bo, ( ( i >> 32) &0xffffffff ) );
1993 bo_add_32be( p_bo, i &0xffffffff );
1996 static void bo_add_fourcc( bo_t *p_bo, char *fcc )
1998 bo_add_8( p_bo, fcc[0] );
1999 bo_add_8( p_bo, fcc[1] );
2000 bo_add_8( p_bo, fcc[2] );
2001 bo_add_8( p_bo, fcc[3] );
2004 static void bo_add_mem( bo_t *p_bo, int i_size, uint8_t *p_mem )
2008 for( i = 0; i < i_size; i++ )
2010 bo_add_8( p_bo, p_mem[i] );
2014 static void bo_add_descr( bo_t *p_bo, uint8_t tag, uint32_t i_size )
2020 vals[3] = (unsigned char)(i_length & 0x7f);
2022 vals[2] = (unsigned char)((i_length & 0x7f) | 0x80);
2024 vals[1] = (unsigned char)((i_length & 0x7f) | 0x80);
2026 vals[0] = (unsigned char)((i_length & 0x7f) | 0x80);
2028 bo_add_8( p_bo, tag );
2030 if( i_size < 0x00000080 )
2032 bo_add_8( p_bo, vals[3] );
2034 else if( i_size < 0x00004000 )
2036 bo_add_8( p_bo, vals[2] );
2037 bo_add_8( p_bo, vals[3] );
2039 else if( i_size < 0x00200000 )
2041 bo_add_8( p_bo, vals[1] );
2042 bo_add_8( p_bo, vals[2] );
2043 bo_add_8( p_bo, vals[3] );
2045 else if( i_size < 0x10000000 )
2047 bo_add_8( p_bo, vals[0] );
2048 bo_add_8( p_bo, vals[1] );
2049 bo_add_8( p_bo, vals[2] );
2050 bo_add_8( p_bo, vals[3] );
2054 static void bo_add_bo( bo_t *p_bo, bo_t *p_bo2 )
2058 for( i = 0; i < p_bo2->i_buffer; i++ )
2060 bo_add_8( p_bo, p_bo2->p_buffer[i] );
2064 static bo_t * box_new( char *fcc )
2068 if( ( box = malloc( sizeof( bo_t ) ) ) )
2070 bo_init( box, 0, NULL, VLC_TRUE );
2072 bo_add_32be ( box, 0 );
2073 bo_add_fourcc( box, fcc );
2079 static bo_t * box_full_new( char *fcc, uint8_t v, uint32_t f )
2083 if( ( box = malloc( sizeof( bo_t ) ) ) )
2085 bo_init( box, 0, NULL, VLC_TRUE );
2087 bo_add_32be ( box, 0 );
2088 bo_add_fourcc( box, fcc );
2089 bo_add_8 ( box, v );
2090 bo_add_24be ( box, f );
2096 static void box_fix( bo_t *box )
2100 memcpy( &box_tmp, box, sizeof( bo_t ) );
2102 box_tmp.i_buffer = 0;
2103 bo_add_32be( &box_tmp, box->i_buffer );
2106 static void box_free( bo_t *box )
2110 free( box->p_buffer );
2116 static void box_gather ( bo_t *box, bo_t *box2 )
2118 bo_add_bo( box, box2 );
2122 static block_t * bo_to_sout( sout_instance_t *p_sout, bo_t *box )
2126 p_buf = block_New( p_sout, box->i_buffer );
2127 if( box->i_buffer > 0 )
2129 memcpy( p_buf->p_buffer, box->p_buffer, box->i_buffer );
2135 static void box_send( sout_mux_t *p_mux, bo_t *box )
2139 p_buf = bo_to_sout( p_mux->p_sout, box );
2142 sout_AccessOutWrite( p_mux->p_access, p_buf );
2145 static int64_t get_timestamp()
2147 int64_t i_timestamp = 0;
2150 i_timestamp = time(NULL);
2151 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2152 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60