1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2003, 2006 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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>
42 #include <vlc_iso_lang.h>
45 #include "../demux/mpeg/mpeg_parser_helpers.h"
46 #include "../demux/mp4/libmp4.h"
49 /*****************************************************************************
51 *****************************************************************************/
52 #define FASTSTART_TEXT N_("Create \"Fast Start\" files")
53 #define FASTSTART_LONGTEXT N_(\
54 "Create \"Fast Start\" files. " \
55 "\"Fast Start\" files are optimized for downloads and allow the user " \
56 "to start previewing the file while it is downloading.")
58 static int Open (vlc_object_t *);
59 static void Close (vlc_object_t *);
60 static int OpenFrag (vlc_object_t *);
61 static void CloseFrag (vlc_object_t *);
63 #define SOUT_CFG_PREFIX "sout-mp4-"
66 set_description(N_("MP4/MOV muxer"))
67 set_category(CAT_SOUT)
68 set_subcategory(SUBCAT_SOUT_MUX)
71 add_bool(SOUT_CFG_PREFIX "faststart", true,
72 FASTSTART_TEXT, FASTSTART_LONGTEXT,
74 set_capability("sout mux", 5)
75 add_shortcut("mp4", "mov", "3gp")
76 set_callbacks(Open, Close)
79 set_description(N_("Fragmented and streamable MP4 muxer"))
80 set_category(CAT_SOUT)
81 set_subcategory(SUBCAT_SOUT_MUX)
82 set_shortname("MP4 Frag")
83 add_shortcut("mp4frag", "mp4stream")
84 set_capability("sout mux", 0)
85 set_callbacks(OpenFrag, CloseFrag)
89 /*****************************************************************************
91 *****************************************************************************/
92 static const char *const ppsz_sout_options[] = {
96 static int Control(sout_mux_t *, int, va_list);
97 static int AddStream(sout_mux_t *, sout_input_t *);
98 static void DelStream(sout_mux_t *, sout_input_t *);
99 static int Mux (sout_mux_t *);
100 static int MuxFrag (sout_mux_t *);
102 /*****************************************************************************
104 *****************************************************************************/
112 unsigned int i_flags;
115 typedef struct mp4_fragentry_t mp4_fragentry_t;
117 struct mp4_fragentry_t
121 mp4_fragentry_t *p_next;
124 typedef struct mp4_fragindex_t
126 uint64_t i_moofoffset;
133 typedef struct mp4_fragqueue_t
135 mp4_fragentry_t *p_first;
136 mp4_fragentry_t *p_last;
142 unsigned int i_track_id;
145 unsigned int i_entry_count;
146 unsigned int i_entry_max;
148 int64_t i_length_neg;
151 int64_t i_dts_start; /* applies to current segment only */
152 int64_t i_read_duration;
153 uint32_t i_timescale;
154 mtime_t i_starttime; /* the really first packet */
157 /* XXX: needed for other codecs too, see lavf */
160 /* for later stco fix-up (fast start files) */
165 int64_t i_last_dts; /* applies to current segment only */
166 int64_t i_last_length;
170 uint32_t i_trex_length;
171 uint32_t i_trex_size;
172 uint32_t i_tfhd_flags;
174 uint32_t i_current_run;
175 mp4_fragentry_t *p_held_entry;
176 mp4_fragqueue_t read;
177 mp4_fragqueue_t towrite;
178 mtime_t i_last_iframe_time;
179 mtime_t i_written_duration;
180 mp4_fragindex_t *p_indexentries;
181 uint32_t i_indexentriesmax;
182 uint32_t i_indexentries;
185 struct sout_mux_sys_t
194 mtime_t i_read_duration;
196 unsigned int i_nb_streams;
197 mp4_stream_t **pp_streams;
202 mtime_t i_written_duration;
203 uint32_t i_mfhd_sequence;
206 static bo_t *box_new (const char *fcc);
207 static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f);
208 static void box_fix (bo_t *box, uint32_t);
209 static void box_gather (bo_t *box, bo_t *box2);
211 static inline void bo_add_mp4_tag_descr(bo_t *box, uint8_t tag, uint32_t size)
214 for (int i = 3; i>0; i--)
215 bo_add_8(box, (size>>(7*i)) | 0x80);
216 bo_add_8(box, size & 0x7F);
219 static void box_send(sout_mux_t *p_mux, bo_t *box);
221 static bo_t *GetMoovBox(sout_mux_t *p_mux);
223 static block_t *ConvertSUBT(block_t *);
224 static block_t *ConvertFromAnnexB(block_t *);
226 static const char avc1_short_start_code[3] = { 0, 0, 1 };
227 static const char avc1_start_code[4] = { 0, 0, 0, 1 };
229 /*****************************************************************************
231 *****************************************************************************/
232 static int Open(vlc_object_t *p_this)
234 sout_mux_t *p_mux = (sout_mux_t*)p_this;
235 sout_mux_sys_t *p_sys;
238 msg_Dbg(p_mux, "Mp4 muxer opened");
239 config_ChainParse(p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg);
241 p_mux->pf_control = Control;
242 p_mux->pf_addstream = AddStream;
243 p_mux->pf_delstream = DelStream;
245 p_mux->p_sys = p_sys = malloc(sizeof(sout_mux_sys_t));
249 p_sys->i_nb_streams = 0;
250 p_sys->pp_streams = NULL;
251 p_sys->i_mdat_pos = 0;
252 p_sys->b_mov = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "mov");
253 p_sys->b_3gp = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "3gp");
254 p_sys->i_read_duration = 0;
255 p_sys->b_fragmented = false;
258 /* Now add ftyp header */
259 box = box_new("ftyp");
266 bo_add_fourcc(box, "3gp6");
268 bo_add_fourcc(box, "isom");
269 bo_add_32be (box, 0);
271 bo_add_fourcc(box, "3gp4");
273 bo_add_fourcc(box, "mp41");
274 bo_add_fourcc(box, "avc1");
277 box_fix(box, box->b->i_buffer);
278 p_sys->i_pos += box->b->i_buffer;
279 p_sys->i_mdat_pos = p_sys->i_pos;
281 box_send(p_mux, box);
286 * Quicktime actually doesn't like the 64 bits extensions !!! */
287 p_sys->b_64_ext = false;
289 /* Now add mdat header */
290 box = box_new("mdat");
296 bo_add_64be (box, 0); // enough to store an extended size
299 p_sys->i_pos += box->b->i_buffer;
301 box_send(p_mux, box);
306 /*****************************************************************************
308 *****************************************************************************/
309 static void Close(vlc_object_t *p_this)
311 sout_mux_t *p_mux = (sout_mux_t*)p_this;
312 sout_mux_sys_t *p_sys = p_mux->p_sys;
314 msg_Dbg(p_mux, "Close");
316 /* Update mdat size */
318 if (!bo_init(&bo, 16))
320 if (p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32)) {
322 bo_add_32be (&bo, 1);
323 bo_add_fourcc(&bo, "mdat");
324 bo_add_64be (&bo, p_sys->i_pos - p_sys->i_mdat_pos);
326 bo_add_32be (&bo, 8);
327 bo_add_fourcc(&bo, "wide");
328 bo_add_32be (&bo, p_sys->i_pos - p_sys->i_mdat_pos - 8);
329 bo_add_fourcc(&bo, "mdat");
332 sout_AccessOutSeek(p_mux->p_access, p_sys->i_mdat_pos);
333 sout_AccessOutWrite(p_mux->p_access, bo.b);
335 /* Create MOOV header */
336 uint64_t i_moov_pos = p_sys->i_pos;
337 bo_t *moov = GetMoovBox(p_mux);
339 /* Check we need to create "fast start" files */
340 p_sys->b_fast_start = var_GetBool(p_this, SOUT_CFG_PREFIX "faststart");
341 while (p_sys->b_fast_start && moov && moov->b) {
342 /* Move data to the end of the file so we can fit the moov header
344 int64_t i_size = p_sys->i_pos - p_sys->i_mdat_pos;
345 int i_moov_size = moov->b->i_buffer;
348 int64_t i_chunk = __MIN(32768, i_size);
349 block_t *p_buf = block_Alloc(i_chunk);
350 sout_AccessOutSeek(p_mux->p_access,
351 p_sys->i_mdat_pos + i_size - i_chunk);
352 if (sout_AccessOutRead(p_mux->p_access, p_buf) < i_chunk) {
353 msg_Warn(p_this, "read() not supported by access output, "
354 "won't create a fast start file");
355 p_sys->b_fast_start = false;
356 block_Release(p_buf);
359 sout_AccessOutSeek(p_mux->p_access, p_sys->i_mdat_pos + i_size +
360 i_moov_size - i_chunk);
361 sout_AccessOutWrite(p_mux->p_access, p_buf);
365 if (!p_sys->b_fast_start)
368 /* Update pos pointers */
369 i_moov_pos = p_sys->i_mdat_pos;
370 p_sys->i_mdat_pos += moov->b->i_buffer;
372 /* Fix-up samples to chunks table in MOOV header */
373 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
374 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
375 unsigned i_written = 0;
376 for (unsigned i = 0; i < p_stream->i_entry_count; ) {
377 mp4_entry_t *entry = p_stream->entry;
378 if (p_stream->b_stco64)
379 bo_set_64be(moov, p_stream->i_stco_pos + i_written++ * 8, entry[i].i_pos + p_sys->i_mdat_pos - i_moov_pos);
381 bo_set_32be(moov, p_stream->i_stco_pos + i_written++ * 4, entry[i].i_pos + p_sys->i_mdat_pos - i_moov_pos);
383 for (; i < p_stream->i_entry_count; i++)
384 if (i >= p_stream->i_entry_count - 1 ||
385 entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
392 p_sys->b_fast_start = false;
395 /* Write MOOV header */
396 sout_AccessOutSeek(p_mux->p_access, i_moov_pos);
397 box_send(p_mux, moov);
401 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
402 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
404 es_format_Clean(&p_stream->fmt);
405 if (p_stream->a52_frame)
406 block_Release(p_stream->a52_frame);
407 free(p_stream->entry);
410 if (p_sys->i_nb_streams)
411 free(p_sys->pp_streams);
415 /*****************************************************************************
417 *****************************************************************************/
418 static int Control(sout_mux_t *p_mux, int i_query, va_list args)
425 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
426 pb_bool = (bool*)va_arg(args, bool *);
430 case MUX_GET_ADD_STREAM_WAIT:
431 pb_bool = (bool*)va_arg(args, bool *);
435 case MUX_GET_MIME: /* Not needed, as not streamable */
441 /*****************************************************************************
443 *****************************************************************************/
444 static int AddStream(sout_mux_t *p_mux, sout_input_t *p_input)
446 sout_mux_sys_t *p_sys = p_mux->p_sys;
447 mp4_stream_t *p_stream;
449 switch(p_input->p_fmt->i_codec)
460 case VLC_CODEC_MJPGB:
466 case VLC_CODEC_AMR_NB:
467 case VLC_CODEC_AMR_WB:
472 msg_Warn(p_mux, "subtitle track added like in .mov (even when creating .mp4)");
475 msg_Err(p_mux, "unsupported codec %4.4s in mp4",
476 (char*)&p_input->p_fmt->i_codec);
480 p_stream = malloc(sizeof(mp4_stream_t));
483 es_format_Copy(&p_stream->fmt, p_input->p_fmt);
484 p_stream->i_track_id = p_sys->i_nb_streams + 1;
485 p_stream->i_length_neg = 0;
486 p_stream->i_entry_count = 0;
487 p_stream->i_entry_max = 1000;
489 calloc(p_stream->i_entry_max, sizeof(mp4_entry_t));
490 p_stream->i_dts_start = 0;
491 p_stream->i_read_duration = 0;
492 p_stream->a52_frame = NULL;
493 switch( p_stream->fmt.i_cat )
496 if(!p_stream->fmt.audio.i_rate)
498 msg_Warn( p_mux, "no audio rate given for stream %d, assuming 48KHz",
499 p_sys->i_nb_streams );
500 p_stream->fmt.audio.i_rate = 48000;
502 p_stream->i_timescale = p_stream->fmt.audio.i_rate;
505 if( !p_stream->fmt.video.i_frame_rate ||
506 !p_stream->fmt.video.i_frame_rate_base )
508 msg_Warn( p_mux, "Missing frame rate for stream %d, assuming 25fps",
509 p_sys->i_nb_streams );
510 p_stream->fmt.video.i_frame_rate = 25;
511 p_stream->fmt.video.i_frame_rate_base = 1;
513 p_stream->i_timescale = p_stream->fmt.video.i_frame_rate * 1000 /
514 p_stream->fmt.video.i_frame_rate_base;
517 p_stream->i_timescale = CLOCK_FREQ;
521 p_stream->i_starttime = p_sys->i_read_duration;
522 p_stream->b_hasbframes = false;
524 p_stream->i_last_dts = 0;
525 p_stream->i_last_length = 0;
527 p_stream->b_hasiframes = false;
528 p_stream->i_trex_length = 0;
529 p_stream->i_trex_size = 0;
531 p_stream->i_current_run = 0;
532 p_stream->read.p_first = NULL;
533 p_stream->read.p_last = NULL;
534 p_stream->towrite.p_first = NULL;
535 p_stream->towrite.p_last = NULL;
536 p_stream->p_held_entry = NULL;
537 p_stream->i_last_iframe_time = 0;
538 p_stream->i_written_duration = 0;
539 p_stream->p_indexentries = NULL;
540 p_stream->i_indexentriesmax = 0;
541 p_stream->i_indexentries = 0;
543 p_input->p_sys = p_stream;
545 msg_Dbg(p_mux, "adding input");
547 TAB_APPEND(p_sys->i_nb_streams, p_sys->pp_streams, p_stream);
551 /*****************************************************************************
553 *****************************************************************************/
554 static void DelStream(sout_mux_t *p_mux, sout_input_t *p_input)
557 msg_Dbg(p_mux, "removing input");
560 /*****************************************************************************
562 *****************************************************************************/
563 static int Mux(sout_mux_t *p_mux)
565 sout_mux_sys_t *p_sys = p_mux->p_sys;
568 int i_stream = sout_MuxGetStream(p_mux, 2, NULL);
572 sout_input_t *p_input = p_mux->pp_inputs[i_stream];
573 mp4_stream_t *p_stream = (mp4_stream_t*)p_input->p_sys;
577 p_data = block_FifoGet(p_input->p_fifo);
578 if (p_stream->fmt.i_codec == VLC_CODEC_H264 ||
579 p_stream->fmt.i_codec == VLC_CODEC_HEVC)
580 p_data = ConvertFromAnnexB(p_data);
581 else if (p_stream->fmt.i_codec == VLC_CODEC_SUBT)
582 p_data = ConvertSUBT(p_data);
583 else if (p_stream->fmt.i_codec == VLC_CODEC_A52 ||
584 p_stream->fmt.i_codec == VLC_CODEC_EAC3) {
585 if (p_stream->a52_frame == NULL && p_data->i_buffer >= 8)
586 p_stream->a52_frame = block_Duplicate(p_data);
590 /* Reset reference dts in case of discontinuity (ex: gather sout) */
591 if ( p_stream->i_entry_count == 0 || p_data->i_flags & BLOCK_FLAG_DISCONTINUITY )
593 p_stream->i_dts_start = p_data->i_dts;
594 p_stream->i_last_dts = p_data->i_dts;
595 p_stream->i_length_neg = 0;
598 if (p_stream->fmt.i_cat != SPU_ES) {
599 /* Fix length of the sample */
600 if (block_FifoCount(p_input->p_fifo) > 0) {
601 block_t *p_next = block_FifoShow(p_input->p_fifo);
602 if ( p_next->i_flags & BLOCK_FLAG_DISCONTINUITY )
603 { /* we have no way to know real length except by decoding */
604 if ( p_stream->fmt.i_cat == VIDEO_ES )
606 p_data->i_length = CLOCK_FREQ *
607 p_stream->fmt.video.i_frame_rate_base /
608 p_stream->fmt.video.i_frame_rate;
609 msg_Dbg( p_mux, "video track %u fixup to %"PRId64" for sample %u",
610 p_stream->i_track_id, p_data->i_length, p_stream->i_entry_count );
612 else if ( p_stream->fmt.i_cat == AUDIO_ES &&
613 p_stream->fmt.audio.i_rate &&
614 p_data->i_nb_samples )
616 p_data->i_length = CLOCK_FREQ * p_data->i_nb_samples /
617 p_stream->fmt.audio.i_rate;
618 msg_Dbg( p_mux, "audio track %u fixup to %"PRId64" for sample %u",
619 p_stream->i_track_id, p_data->i_length, p_stream->i_entry_count );
621 else if ( p_data->i_length <= 0 )
623 msg_Warn( p_mux, "unknown length for track %u sample %u",
624 p_stream->i_track_id, p_stream->i_entry_count );
625 p_data->i_length = 1;
630 int64_t i_diff = p_next->i_dts - p_data->i_dts;
631 if (i_diff < CLOCK_FREQ) /* protection */
632 p_data->i_length = i_diff;
635 if (p_data->i_length <= 0) {
636 msg_Warn(p_mux, "i_length <= 0");
637 p_stream->i_length_neg += p_data->i_length - 1;
638 p_data->i_length = 1;
639 } else if (p_stream->i_length_neg < 0) {
640 int64_t i_recover = __MIN(p_data->i_length / 4, - p_stream->i_length_neg);
642 p_data->i_length -= i_recover;
643 p_stream->i_length_neg += i_recover;
647 if (p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0) {
648 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
650 if (i_length <= 0) /* FIXME handle this broken case */
654 if (p_stream->entry[p_stream->i_entry_count-1].i_length <= 0)
655 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
658 /* add index entry */
659 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
660 e->i_pos = p_sys->i_pos;
661 e->i_size = p_data->i_buffer;
663 if ( p_data->i_dts > VLC_TS_INVALID && p_data->i_pts > p_data->i_dts )
665 e->i_pts_dts = p_data->i_pts - p_data->i_dts;
666 if ( !p_stream->b_hasbframes )
667 p_stream->b_hasbframes = true;
669 else e->i_pts_dts = 0;
671 e->i_length = p_data->i_length;
672 e->i_flags = p_data->i_flags;
674 p_stream->i_entry_count++;
675 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
676 if (p_stream->i_entry_count >= p_stream->i_entry_max - 1) {
677 p_stream->i_entry_max += 1000;
678 p_stream->entry = xrealloc(p_stream->entry,
679 p_stream->i_entry_max * sizeof(mp4_entry_t));
683 p_stream->i_read_duration += __MAX( 0, p_data->i_length );
684 p_stream->i_last_length = p_data->i_length;
685 p_sys->i_pos += p_data->i_buffer;
687 /* Save the DTS for SPU */
688 p_stream->i_last_dts = p_data->i_dts;
691 sout_AccessOutWrite(p_mux->p_access, p_data);
693 /* close subtitle with empty frame */
694 if (p_stream->fmt.i_cat == SPU_ES) {
695 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
697 if ( i_length != 0 && (p_data = block_Alloc(3)) ) {
699 msg_Dbg(p_mux, "writing an empty sub") ;
701 /* Append a idx entry */
702 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
703 e->i_pos = p_sys->i_pos;
709 /* XXX: No need to grow the entry here */
710 p_stream->i_entry_count++;
713 p_stream->i_last_dts += i_length;
716 p_data->i_dts = p_stream->i_last_dts;
717 p_data->i_dts = p_data->i_pts;
718 p_data->p_buffer[0] = 0;
719 p_data->p_buffer[1] = 1;
720 p_data->p_buffer[2] = ' ';
722 p_sys->i_pos += p_data->i_buffer;
724 sout_AccessOutWrite(p_mux->p_access, p_data);
727 /* Fix duration = current segment starttime + duration within */
728 p_stream->i_read_duration = p_stream->i_starttime + ( p_stream->i_last_dts - p_stream->i_dts_start );
732 /* Update the global segment/media duration */
733 for ( unsigned int i=0; i<p_sys->i_nb_streams; i++ )
735 if ( p_sys->pp_streams[i]->i_read_duration > p_sys->i_read_duration )
736 p_sys->i_read_duration = p_sys->pp_streams[i]->i_read_duration;
742 /*****************************************************************************
744 *****************************************************************************/
745 static block_t *ConvertSUBT(block_t *p_block)
747 p_block = block_Realloc(p_block, 2, p_block->i_buffer);
750 /* No trailling '\0' */
751 if (p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0')
754 p_block->p_buffer[0] = ((p_block->i_buffer - 2) >> 8)&0xff;
755 p_block->p_buffer[1] = ((p_block->i_buffer - 2) )&0xff;
760 static block_t *ConvertFromAnnexB(block_t *p_block)
762 if(p_block->i_buffer < 4)
764 block_Release(p_block);
768 if(memcmp(p_block->p_buffer, avc1_start_code, 4))
770 if(!memcmp(p_block->p_buffer, avc1_short_start_code, 3))
772 p_block = block_Realloc(p_block, 1, p_block->i_buffer);
776 else /* No startcode on start */
778 block_Release(p_block);
783 uint8_t *last = p_block->p_buffer;
784 uint8_t *dat = &p_block->p_buffer[4];
785 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
787 /* Replace the 4 bytes start code with 4 bytes size */
789 while (dat < end - 4) {
790 if (!memcmp(dat, avc1_start_code, 4))
794 else if(!memcmp(dat, avc1_short_start_code, 3))
796 /* save offsets as we don't know if realloc will replace buffer */
797 size_t i_last = last - p_block->p_buffer;
798 size_t i_dat = dat - p_block->p_buffer;
799 size_t i_end = end - p_block->p_buffer;
801 p_block = block_Realloc(p_block, 0, p_block->i_buffer + 1);
805 /* restore offsets */
806 last = &p_block->p_buffer[i_last];
807 dat = &p_block->p_buffer[i_dat];
808 end = &p_block->p_buffer[i_end];
811 memmove(&dat[4], &dat[3], end - &dat[3]);
821 SetDWBE(last, dat - &last[4]);
823 /* Skip blocks with SPS/PPS */
824 //if ((last[4]&0x1f) == 7 || (last[4]&0x1f) == 8)
825 // ; // FIXME Find a way to skip dat without frelling everything
832 static bo_t *GetESDS(mp4_stream_t *p_stream)
835 int64_t i_bitrate_avg = 0;
836 int64_t i_bitrate_max = 0;
838 /* Compute avg/max bitrate */
839 for (unsigned i = 0; i < p_stream->i_entry_count; i++) {
840 i_bitrate_avg += p_stream->entry[i].i_size;
841 if (p_stream->entry[i].i_length > 0) {
842 int64_t i_bitrate = INT64_C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
843 if (i_bitrate > i_bitrate_max)
844 i_bitrate_max = i_bitrate;
848 if (p_stream->i_read_duration > 0)
849 i_bitrate_avg = INT64_C(8000000) * i_bitrate_avg / p_stream->i_read_duration;
852 if (i_bitrate_max <= 1)
853 i_bitrate_max = 0x7fffffff;
856 int i_decoder_specific_info_size = (p_stream->fmt.i_extra > 0) ? 5 + p_stream->fmt.i_extra : 0;
858 esds = box_full_new("esds", 0, 0);
863 bo_add_mp4_tag_descr(esds, 0x03, 3 + 5 + 13 + i_decoder_specific_info_size + 5 + 1);
864 bo_add_16be(esds, p_stream->i_track_id);
865 bo_add_8 (esds, 0x1f); // flags=0|streamPriority=0x1f
867 /* DecoderConfigDescr */
868 bo_add_mp4_tag_descr(esds, 0x04, 13 + i_decoder_specific_info_size);
870 int i_object_type_indication;
871 switch(p_stream->fmt.i_codec)
874 i_object_type_indication = 0x20;
877 /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
878 i_object_type_indication = 0x65;
881 /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
882 i_object_type_indication = 0x6b;
885 /* FIXME for mpeg2-aac == 0x66->0x68 */
886 i_object_type_indication = 0x40;
889 i_object_type_indication =
890 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
893 i_object_type_indication = 0x00;
896 int i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
898 bo_add_8 (esds, i_object_type_indication);
899 bo_add_8 (esds, (i_stream_type << 2) | 1);
900 bo_add_24be(esds, 1024 * 1024); // bufferSizeDB
901 bo_add_32be(esds, i_bitrate_max); // maxBitrate
902 bo_add_32be(esds, i_bitrate_avg); // avgBitrate
904 if (p_stream->fmt.i_extra > 0) {
905 /* DecoderSpecificInfo */
906 bo_add_mp4_tag_descr(esds, 0x05, p_stream->fmt.i_extra);
908 for (int i = 0; i < p_stream->fmt.i_extra; i++)
909 bo_add_8(esds, ((uint8_t*)p_stream->fmt.p_extra)[i]);
912 /* SL_Descr mandatory */
913 bo_add_mp4_tag_descr(esds, 0x06, 1);
914 bo_add_8 (esds, 0x02); // sl_predefined
919 static bo_t *GetWaveTag(mp4_stream_t *p_stream)
924 wave = box_new("wave");
927 box = box_new("frma");
930 bo_add_fourcc(box, "mp4a");
931 box_gather(wave, box);
934 box = box_new("mp4a");
938 box_gather(wave, box);
941 box = GetESDS(p_stream);
942 box_gather(wave, box);
944 box = box_new("srcq");
947 bo_add_32be(box, 0x40);
948 box_gather(wave, box);
952 bo_add_32be(wave, 8); /* new empty box */
953 bo_add_32be(wave, 0); /* box label */
958 static bo_t *GetDec3Tag(mp4_stream_t *p_stream)
960 if (!p_stream->a52_frame)
964 bs_init(&s, p_stream->a52_frame->p_buffer, sizeof(p_stream->a52_frame->i_buffer));
965 bs_skip(&s, 16); // syncword
967 uint8_t fscod, bsid, bsmod, acmod, lfeon, strmtyp;
971 strmtyp = bs_read(&s, 2);
973 if (strmtyp & 0x1) // dependant or reserved stream
976 if (bs_read(&s, 3) != 0x0) // substreamid: we don't support more than 1 stream
980 bs_skip(&s, 11); // frmsizecod
981 fscod = bs_read(&s, 2);
983 bs_skip(&s, 2); // fscod2
986 numblkscod = bs_read(&s, 2);
989 acmod = bs_read(&s, 3);
990 lfeon = bs_read1(&s);
992 bsid = bs_read(&s, 5);
994 bs_skip(&s, 5); // dialnorm
995 if (bs_read1(&s)) // compre
996 bs_skip(&s, 5); // compr
999 bs_skip(&s, 5); // dialnorm2
1000 if (bs_read1(&s)) // compr2e
1001 bs_skip(&s, 8); // compr2
1004 if (strmtyp == 0x1) // dependant stream XXX: unsupported
1005 if (bs_read1(&s)) // chanmape
1006 bs_skip(&s, 16); // chanmap
1008 /* we have to skip mixing info to read bsmod */
1009 if (bs_read1(&s)) { // mixmdate
1010 if (acmod > 0x2) // 2+ channels
1011 bs_skip(&s, 2); // dmixmod
1012 if ((acmod & 0x1) && (acmod > 0x2)) // 3 front channels
1013 bs_skip(&s, 3 + 3); // ltrtcmixlev + lorocmixlev
1014 if (acmod & 0x4) // surround channel
1015 bs_skip(&s, 3 + 3); // ltrsurmixlev + lorosurmixlev
1018 bs_skip(&s, 5); // lfemixlevcod
1019 if (strmtyp == 0) { // independant stream
1020 if (bs_read1(&s)) // pgmscle
1021 bs_skip(&s, 6); // pgmscl
1022 if (acmod == 0x0) // dual mono
1023 if (bs_read1(&s)) // pgmscl2e
1024 bs_skip(&s, 6); // pgmscl2
1025 if (bs_read1(&s)) // extpgmscle
1026 bs_skip(&s, 6); // extpgmscl
1027 uint8_t mixdef = bs_read(&s, 2);
1030 else if (mixdef == 0x2)
1032 else if (mixdef == 0x3) {
1033 uint8_t mixdeflen = bs_read(&s, 5);
1034 bs_skip(&s, 8 * (mixdeflen + 2));
1036 if (acmod < 0x2) { // mono or dual mono
1037 if (bs_read1(&s)) // paninfoe
1038 bs_skip(&s, 14); // paninfo
1039 if (acmod == 0) // dual mono
1040 if (bs_read1(&s)) // paninfo2e
1041 bs_skip(&s, 14); // paninfo2
1043 if (bs_read1(&s)) { // frmmixcfginfoe
1044 static const int blocks[4] = { 1, 2, 3, 6 };
1045 int number_of_blocks = blocks[numblkscod];
1046 if (number_of_blocks == 1)
1047 bs_skip(&s, 5); // blkmixcfginfo[0]
1048 else for (int i = 0; i < number_of_blocks; i++)
1049 if (bs_read1(&s)) // blkmixcfginfoe
1050 bs_skip(&s, 5); // blkmixcfginfo[i]
1055 if (bs_read1(&s)) // infomdate
1056 bsmod = bs_read(&s, 3);
1058 uint8_t mp4_eac3_header[5];
1059 bs_init(&s, mp4_eac3_header, sizeof(mp4_eac3_header));
1061 int data_rate = p_stream->fmt.i_bitrate / 1000;
1062 bs_write(&s, 13, data_rate);
1063 bs_write(&s, 3, 0); // num_ind_sub - 1
1064 bs_write(&s, 2, fscod);
1065 bs_write(&s, 5, bsid);
1066 bs_write(&s, 5, bsmod);
1067 bs_write(&s, 3, acmod);
1068 bs_write(&s, 1, lfeon);
1069 bs_write(&s, 3, 0); // reserved
1070 bs_write(&s, 4, 0); // num_dep_sub
1071 bs_write(&s, 1, 0); // reserved
1073 bo_t *dec3 = box_new("dec3");
1075 bo_add_mem(dec3, sizeof(mp4_eac3_header), mp4_eac3_header);
1080 static bo_t *GetDac3Tag(mp4_stream_t *p_stream)
1082 if (!p_stream->a52_frame)
1085 bo_t *dac3 = box_new("dac3");
1090 bs_init(&s, p_stream->a52_frame->p_buffer, sizeof(p_stream->a52_frame->i_buffer));
1092 uint8_t fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
1094 bs_skip(&s, 16 + 16); // syncword + crc
1096 fscod = bs_read(&s, 2);
1097 frmsizecod = bs_read(&s, 6);
1098 bsid = bs_read(&s, 5);
1099 bsmod = bs_read(&s, 3);
1100 acmod = bs_read(&s, 3);
1102 bs_skip(&s, 2); // dsurmod
1104 if ((acmod & 1) && acmod != 1)
1105 bs_skip(&s, 2); // cmixlev
1107 bs_skip(&s, 2); // surmixlev
1110 lfeon = bs_read1(&s);
1112 uint8_t mp4_a52_header[3];
1113 bs_init(&s, mp4_a52_header, sizeof(mp4_a52_header));
1115 bs_write(&s, 2, fscod);
1116 bs_write(&s, 5, bsid);
1117 bs_write(&s, 3, bsmod);
1118 bs_write(&s, 3, acmod);
1119 bs_write(&s, 1, lfeon);
1120 bs_write(&s, 5, frmsizecod >> 1); // bit_rate_code
1121 bs_write(&s, 5, 0); // reserved
1123 bo_add_mem(dac3, sizeof(mp4_a52_header), mp4_a52_header);
1128 static bo_t *GetDamrTag(mp4_stream_t *p_stream)
1130 bo_t *damr = box_new("damr");
1134 bo_add_fourcc(damr, "REFC");
1137 if (p_stream->fmt.i_codec == VLC_CODEC_AMR_NB)
1138 bo_add_16be(damr, 0x81ff); /* Mode set (all modes for AMR_NB) */
1140 bo_add_16be(damr, 0x83ff); /* Mode set (all modes for AMR_WB) */
1141 bo_add_16be(damr, 0x1); /* Mode change period (no restriction) */
1146 static bo_t *GetD263Tag(void)
1148 bo_t *d263 = box_new("d263");
1152 bo_add_fourcc(d263, "VLC ");
1153 bo_add_16be(d263, 0xa);
1159 static void hevcParseVPS(uint8_t * p_buffer, size_t i_buffer, uint8_t *general,
1160 uint8_t * numTemporalLayer, bool * temporalIdNested)
1162 const size_t i_decoded_nal_size = 512;
1163 uint8_t p_dec_nal[i_decoded_nal_size];
1164 size_t i_size = (i_buffer < i_decoded_nal_size)?i_buffer:i_decoded_nal_size;
1165 nal_decode(p_buffer, p_dec_nal, i_size);
1167 /* first two bytes are the NAL header, 3rd and 4th are:
1168 vps_video_parameter_set_id(4)
1169 vps_reserved_3_2bis(2)
1170 vps_max_layers_minus1(6)
1171 vps_max_sub_layers_minus1(3)
1172 vps_temporal_id_nesting_flags
1174 *numTemporalLayer = ((p_dec_nal[3] & 0x0E) >> 1) + 1;
1175 *temporalIdNested = (bool)(p_dec_nal[3] & 0x01);
1177 /* 5th & 6th are reserved 0xffff */
1178 /* copy the first 12 bytes of profile tier */
1179 memcpy(general, &p_dec_nal[6], 12);
1182 static void hevcParseSPS(uint8_t * p_buffer, size_t i_buffer, uint8_t * chroma_idc,
1183 uint8_t *bit_depth_luma_minus8, uint8_t *bit_depth_chroma_minus8)
1185 const size_t i_decoded_nal_size = 512;
1186 uint8_t p_dec_nal[i_decoded_nal_size];
1187 size_t i_size = (i_buffer < i_decoded_nal_size)?i_buffer-2:i_decoded_nal_size;
1188 nal_decode(p_buffer+2, p_dec_nal, i_size);
1190 bs_init(&bs, p_dec_nal, i_size);
1194 uint32_t sps_max_sublayer_minus1 = bs_read(&bs, 3);
1196 /* skip nesting flag */
1199 hevc_skip_profile_tiers_level(&bs, sps_max_sublayer_minus1);
1202 (void) bs_read_ue( &bs );
1204 *chroma_idc = bs_read_ue(&bs);
1205 if (*chroma_idc == 3)
1208 /* skip width and heigh */
1209 (void) bs_read_ue( &bs );
1210 (void) bs_read_ue( &bs );
1212 uint32_t conformance_window_flag = bs_read1(&bs);
1213 if (conformance_window_flag) {
1215 (void) bs_read_ue(&bs);
1216 (void) bs_read_ue(&bs);
1217 (void) bs_read_ue(&bs);
1218 (void) bs_read_ue(&bs);
1220 *bit_depth_luma_minus8 = bs_read_ue(&bs);
1221 *bit_depth_chroma_minus8 = bs_read_ue(&bs);
1224 static bo_t *GetHvcCTag(mp4_stream_t *p_stream)
1226 /* Generate hvcC box matching iso/iec 14496-15 3rd edition */
1227 bo_t *hvcC = box_new("hvcC");
1228 if(!hvcC || !p_stream->fmt.i_extra)
1236 /* According to the specification HEVC stream can have
1237 * 16 vps id and an "unlimited" number of sps and pps id using ue(v) id*/
1238 struct nal p_vps[16], *p_sps = NULL, *p_pps = NULL, *p_sei = NULL,
1240 size_t i_vps = 0, i_sps = 0, i_pps = 0, i_sei = 0;
1241 uint8_t i_num_arrays = 0;
1243 uint8_t * p_buffer = p_stream->fmt.p_extra;
1244 size_t i_buffer = p_stream->fmt.i_extra;
1246 uint8_t general_configuration[12] = {0};
1247 uint8_t i_numTemporalLayer = 0;
1248 uint8_t i_chroma_idc = 1;
1249 uint8_t i_bit_depth_luma_minus8 = 0;
1250 uint8_t i_bit_depth_chroma_minus8 = 0;
1251 bool b_temporalIdNested = false;
1253 uint32_t cmp = 0xFFFFFFFF;
1255 /* look for start code 0X0000001 */
1257 cmp = (cmp << 8) | *p_buffer;
1258 if((cmp ^ UINT32_C(0x100)) <= UINT32_C(0xFF))
1264 p_nal->i_buffer = p_buffer - p_nal->p_buffer - ((i_buffer)?3:0);
1266 switch (*p_buffer & 0x72) {
1269 p_nal = &p_vps[i_vps++];
1270 p_nal->p_buffer = p_buffer;
1271 /* Only keep the general profile from the first VPS
1272 * if there are several (this shouldn't happen so soon) */
1274 hevcParseVPS(p_buffer, i_buffer, general_configuration,
1275 &i_numTemporalLayer, &b_temporalIdNested);
1281 struct nal * p_tmp = realloc(p_sps, sizeof(struct nal) * (i_sps + 1));
1285 p_nal = &p_sps[i_sps++];
1286 p_nal->p_buffer = p_buffer;
1287 if (i_sps == 1 && i_buffer > 15) {
1288 /* Get Chroma_idc and bitdepths */
1289 hevcParseSPS(p_buffer, i_buffer, &i_chroma_idc,
1290 &i_bit_depth_luma_minus8, &i_bit_depth_chroma_minus8);
1297 struct nal * p_tmp = realloc(p_pps, sizeof(struct nal) * (i_pps + 1));
1301 p_nal = &p_pps[i_pps++];
1302 p_nal->p_buffer = p_buffer;
1310 struct nal * p_tmp = realloc(p_sei, sizeof(struct nal) * (i_sei + 1));
1314 p_nal = &p_sei[i_sei++];
1315 p_nal->p_buffer = p_buffer;
1325 bo_add_8(hvcC, 0x01);
1326 bo_add_mem(hvcC, 12, general_configuration);
1327 /* Don't set min spatial segmentation */
1328 bo_add_16be(hvcC, 0xF000);
1329 /* Don't set parallelism type since segmentation isn't set */
1330 bo_add_8(hvcC, 0xFC);
1331 bo_add_8(hvcC, (0xFC | (i_chroma_idc & 0x03)));
1332 bo_add_8(hvcC, (0xF8 | (i_bit_depth_luma_minus8 & 0x07)));
1333 bo_add_8(hvcC, (0xF8 | (i_bit_depth_chroma_minus8 & 0x07)));
1335 /* Don't set framerate */
1336 bo_add_16be(hvcC, 0x0000);
1337 /* Force NAL size of 4 bytes that replace the startcode */
1338 bo_add_8(hvcC, (((i_numTemporalLayer & 0x07) << 3) |
1339 (b_temporalIdNested << 2) | 0x03));
1340 bo_add_8(hvcC, i_num_arrays);
1344 /* Write VPS without forcing array_completeness */
1346 bo_add_16be(hvcC, i_vps);
1347 for (size_t i = 0; i < i_vps; i++) {
1349 bo_add_16be(hvcC, p_nal->i_buffer);
1350 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1355 /* Write SPS without forcing array_completeness */
1357 bo_add_16be(hvcC, i_sps);
1358 for (size_t i = 0; i < i_sps; i++) {
1360 bo_add_16be(hvcC, p_nal->i_buffer);
1361 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1366 /* Write PPS without forcing array_completeness */
1368 bo_add_16be(hvcC, i_pps);
1369 for (size_t i = 0; i < i_pps; i++) {
1371 bo_add_16be(hvcC, p_nal->i_buffer);
1372 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1377 /* Write SEI without forcing array_completeness */
1379 bo_add_16be(hvcC, i_sei);
1380 for (size_t i = 0; i < i_sei; i++) {
1382 bo_add_16be(hvcC, p_nal->i_buffer);
1383 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1389 static bo_t *GetAvcCTag(mp4_stream_t *p_stream)
1391 bo_t *avcC = box_new("avcC");/* FIXME use better value */
1394 uint8_t *p_sps = NULL;
1395 uint8_t *p_pps = NULL;
1399 if (p_stream->fmt.i_extra > 0) {
1400 /* FIXME: take into account multiple sps/pps */
1401 uint8_t *p_buffer = p_stream->fmt.p_extra;
1402 int i_buffer = p_stream->fmt.i_extra;
1404 while (i_buffer > 3) {
1405 while (memcmp(p_buffer, &avc1_start_code[1], 3)) {
1409 const int i_nal_type = p_buffer[3]&0x1f;
1410 int i_startcode = 0;
1412 for (int i_offset = 1; i_offset+2 < i_buffer ; i_offset++)
1413 if (!memcmp(&p_buffer[i_offset], &avc1_start_code[1], 3)) {
1414 /* we found another startcode */
1415 i_startcode = i_offset;
1416 while (p_buffer[i_startcode-1] == 0 && i_startcode > 0)
1421 int i_size = i_startcode ? i_startcode : i_buffer;
1423 if (i_nal_type == 7) {
1424 p_sps = &p_buffer[3];
1425 i_sps_size = i_size - 3;
1427 if (i_nal_type == 8) {
1428 p_pps = &p_buffer[3];
1429 i_pps_size = i_size - 3;
1436 bo_add_8(avcC, 1); /* configuration version */
1437 bo_add_8(avcC, i_sps_size ? p_sps[1] : 77);
1438 bo_add_8(avcC, i_sps_size ? p_sps[2] : 64);
1439 bo_add_8(avcC, i_sps_size ? p_sps[3] : 30); /* level, 5.1 */
1440 bo_add_8(avcC, 0xff); /* 0b11111100 | lengthsize = 0x11 */
1442 bo_add_8(avcC, 0xe0 | (i_sps_size > 0 ? 1 : 0)); /* 0b11100000 | sps_count */
1443 if (i_sps_size > 0) {
1444 bo_add_16be(avcC, i_sps_size);
1445 bo_add_mem(avcC, i_sps_size, p_sps);
1448 bo_add_8(avcC, (i_pps_size > 0 ? 1 : 0)); /* pps_count */
1449 if (i_pps_size > 0) {
1450 bo_add_16be(avcC, i_pps_size);
1451 bo_add_mem(avcC, i_pps_size, p_pps);
1457 /* TODO: No idea about these values */
1458 static bo_t *GetSVQ3Tag(mp4_stream_t *p_stream)
1460 bo_t *smi = box_new("SMI ");
1464 if (p_stream->fmt.i_extra > 0x4e) {
1465 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
1466 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
1468 while (p + 8 < p_end) {
1469 int i_size = GetDWBE(p);
1470 if (i_size <= 1) /* FIXME handle 1 as long size */
1472 if (!strncmp((const char *)&p[4], "SMI ", 4)) {
1473 bo_add_mem(smi, p_end - p - 8, &p[8]);
1480 /* Create a dummy one in fallback */
1481 bo_add_fourcc(smi, "SEQH");
1482 bo_add_32be(smi, 0x5);
1483 bo_add_32be(smi, 0xe2c0211d);
1484 bo_add_8(smi, 0xc0);
1489 static bo_t *GetUdtaTag(sout_mux_t *p_mux)
1491 sout_mux_sys_t *p_sys = p_mux->p_sys;
1492 bo_t *udta = box_new("udta");
1497 for (unsigned int i_track = 0; i_track < p_sys->i_nb_streams; i_track++) {
1498 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
1499 vlc_fourcc_t codec = p_stream->fmt.i_codec;
1501 if (codec == VLC_CODEC_MP4V || codec == VLC_CODEC_MP4A) {
1502 bo_t *box = box_new("\251req");
1506 bo_add_16be(box, sizeof("QuickTime 6.0 or greater") - 1);
1507 bo_add_16be(box, 0);
1508 bo_add_mem(box, sizeof("QuickTime 6.0 or greater") - 1,
1509 (uint8_t *)"QuickTime 6.0 or greater");
1510 box_gather(udta, box);
1517 bo_t *box = box_new("\251enc");
1521 bo_add_16be(box, sizeof(PACKAGE_STRING " stream output") - 1);
1522 bo_add_16be(box, 0);
1523 bo_add_mem(box, sizeof(PACKAGE_STRING " stream output") - 1,
1524 (uint8_t*)PACKAGE_STRING " stream output");
1525 box_gather(udta, box);
1530 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
1532 #define ADD_META_BOX(type, box_string) { \
1534 if (vlc_meta_Get(p_meta, vlc_meta_##type)) \
1535 box = box_new("\251" box_string); \
1537 bo_add_16be(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type))); \
1538 bo_add_16be(box, 0); \
1539 bo_add_mem(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type)), \
1540 (uint8_t*)(vlc_meta_Get(p_meta, vlc_meta_##type))); \
1541 box_gather(udta, box); \
1544 ADD_META_BOX(Title, "nam");
1545 ADD_META_BOX(Artist, "ART");
1546 ADD_META_BOX(Genre, "gen");
1547 ADD_META_BOX(Copyright, "cpy");
1548 ADD_META_BOX(Description, "des");
1549 ADD_META_BOX(Date, "day");
1550 ADD_META_BOX(URL, "url");
1557 static bo_t *GetSounBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
1559 sout_mux_sys_t *p_sys = p_mux->p_sys;
1560 bool b_descr = true;
1561 vlc_fourcc_t codec = p_stream->fmt.i_codec;
1564 if (codec == VLC_CODEC_MPGA) {
1567 memcpy(fcc, ".mp3", 4);
1569 memcpy(fcc, "mp4a", 4);
1570 } else if (codec == VLC_CODEC_A52) {
1571 memcpy(fcc, "ac-3", 4);
1572 } else if (codec == VLC_CODEC_EAC3) {
1573 memcpy(fcc, "ec-3", 4);
1575 vlc_fourcc_to_char(codec, fcc);
1577 bo_t *soun = box_new(fcc);
1580 for (int i = 0; i < 6; i++)
1581 bo_add_8(soun, 0); // reserved;
1582 bo_add_16be(soun, 1); // data-reference-index
1584 /* SoundDescription */
1585 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
1586 bo_add_16be(soun, 1); // version 1;
1588 bo_add_16be(soun, 0); // version 0;
1589 bo_add_16be(soun, 0); // revision level (0)
1590 bo_add_32be(soun, 0); // vendor
1592 bo_add_16be(soun, p_stream->fmt.audio.i_channels);
1594 bo_add_16be(soun, p_stream->fmt.audio.i_bitspersample ?
1595 p_stream->fmt.audio.i_bitspersample : 16);
1596 bo_add_16be(soun, -2); // compression id
1597 bo_add_16be(soun, 0); // packet size (0)
1598 bo_add_16be(soun, p_stream->fmt.audio.i_rate); // sampleratehi
1599 bo_add_16be(soun, 0); // sampleratelo
1601 /* Extended data for SoundDescription V1 */
1602 if (p_sys->b_mov && p_stream->fmt.i_codec == VLC_CODEC_MP4A) {
1603 /* samples per packet */
1604 bo_add_32be(soun, p_stream->fmt.audio.i_frame_length);
1605 bo_add_32be(soun, 1536); /* bytes per packet */
1606 bo_add_32be(soun, 2); /* bytes per frame */
1607 /* bytes per sample */
1608 bo_add_32be(soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1611 /* Add an ES Descriptor */
1615 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
1616 box = GetWaveTag(p_stream);
1617 else if (codec == VLC_CODEC_AMR_NB)
1618 box = GetDamrTag(p_stream);
1619 else if (codec == VLC_CODEC_A52)
1620 box = GetDac3Tag(p_stream);
1621 else if (codec == VLC_CODEC_EAC3)
1622 box = GetDec3Tag(p_stream);
1624 box = GetESDS(p_stream);
1627 box_gather(soun, box);
1633 static bo_t *GetVideBox(mp4_stream_t *p_stream)
1637 switch(p_stream->fmt.i_codec)
1639 case VLC_CODEC_MP4V:
1640 case VLC_CODEC_MPGV: memcpy(fcc, "mp4v", 4); break;
1641 case VLC_CODEC_MJPG: memcpy(fcc, "mjpa", 4); break;
1642 case VLC_CODEC_SVQ1: memcpy(fcc, "SVQ1", 4); break;
1643 case VLC_CODEC_SVQ3: memcpy(fcc, "SVQ3", 4); break;
1644 case VLC_CODEC_H263: memcpy(fcc, "s263", 4); break;
1645 case VLC_CODEC_H264: memcpy(fcc, "avc1", 4); break;
1646 case VLC_CODEC_HEVC: memcpy(fcc, "hvc1", 4); break;
1647 case VLC_CODEC_YV12: memcpy(fcc, "yv12", 4); break;
1648 case VLC_CODEC_YUYV: memcpy(fcc, "yuy2", 4); break;
1650 vlc_fourcc_to_char(p_stream->fmt.i_codec, fcc);
1654 bo_t *vide = box_new(fcc);
1657 for (int i = 0; i < 6; i++)
1658 bo_add_8(vide, 0); // reserved;
1659 bo_add_16be(vide, 1); // data-reference-index
1661 bo_add_16be(vide, 0); // predefined;
1662 bo_add_16be(vide, 0); // reserved;
1663 for (int i = 0; i < 3; i++)
1664 bo_add_32be(vide, 0); // predefined;
1666 bo_add_16be(vide, p_stream->fmt.video.i_width); // i_width
1667 bo_add_16be(vide, p_stream->fmt.video.i_height); // i_height
1669 bo_add_32be(vide, 0x00480000); // h 72dpi
1670 bo_add_32be(vide, 0x00480000); // v 72dpi
1672 bo_add_32be(vide, 0); // data size, always 0
1673 bo_add_16be(vide, 1); // frames count per sample
1676 for (int i = 0; i < 32; i++)
1679 bo_add_16be(vide, 0x18); // depth
1680 bo_add_16be(vide, 0xffff); // predefined
1682 /* add an ES Descriptor */
1683 switch(p_stream->fmt.i_codec)
1685 case VLC_CODEC_MP4V:
1686 case VLC_CODEC_MPGV:
1687 box_gather(vide, GetESDS(p_stream));
1690 case VLC_CODEC_H263:
1691 box_gather(vide, GetD263Tag());
1694 case VLC_CODEC_SVQ3:
1695 box_gather(vide, GetSVQ3Tag(p_stream));
1698 case VLC_CODEC_H264:
1699 box_gather(vide, GetAvcCTag(p_stream));
1702 case VLC_CODEC_HEVC:
1703 box_gather(vide, GetHvcCTag(p_stream));
1710 static bo_t *GetTextBox(void)
1712 bo_t *text = box_new("text");
1716 for (int i = 0; i < 6; i++)
1717 bo_add_8(text, 0); // reserved;
1718 bo_add_16be(text, 1); // data-reference-index
1720 bo_add_32be(text, 0); // display flags
1721 bo_add_32be(text, 0); // justification
1722 for (int i = 0; i < 3; i++)
1723 bo_add_16be(text, 0); // back ground color
1725 bo_add_16be(text, 0); // box text
1726 bo_add_16be(text, 0); // box text
1727 bo_add_16be(text, 0); // box text
1728 bo_add_16be(text, 0); // box text
1730 bo_add_64be(text, 0); // reserved
1731 for (int i = 0; i < 3; i++)
1732 bo_add_16be(text, 0xff); // foreground color
1735 bo_add_mem(text, 9, (uint8_t*)"Helvetica");
1740 static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
1742 sout_mux_sys_t *p_sys = p_mux->p_sys;
1744 /* sample description */
1745 bo_t *stsd = box_full_new("stsd", 0, 0);
1748 bo_add_32be(stsd, 1);
1749 if (p_stream->fmt.i_cat == AUDIO_ES)
1750 box_gather(stsd, GetSounBox(p_mux, p_stream));
1751 else if (p_stream->fmt.i_cat == VIDEO_ES)
1752 box_gather(stsd, GetVideBox(p_stream));
1753 else if (p_stream->fmt.i_cat == SPU_ES)
1754 box_gather(stsd, GetTextBox());
1756 /* chunk offset table */
1758 if (p_sys->i_pos >= (((uint64_t)0x1) << 32)) {
1759 /* 64 bits version */
1760 p_stream->b_stco64 = true;
1761 stco = box_full_new("co64", 0, 0);
1763 /* 32 bits version */
1764 p_stream->b_stco64 = false;
1765 stco = box_full_new("stco", 0, 0);
1772 bo_add_32be(stco, 0); // entry-count (fixed latter)
1774 /* sample to chunk table */
1775 bo_t *stsc = box_full_new("stsc", 0, 0);
1776 bo_add_32be(stsc, 0); // entry-count (fixed latter)
1778 unsigned i_chunk = 0;
1779 unsigned i_stsc_last_val = 0, i_stsc_entries = 0;
1780 for (unsigned i = 0; i < p_stream->i_entry_count; i_chunk++) {
1781 mp4_entry_t *entry = p_stream->entry;
1784 if (p_stream->b_stco64)
1785 bo_add_64be(stco, entry[i].i_pos);
1787 bo_add_32be(stco, entry[i].i_pos);
1789 for (; i < p_stream->i_entry_count; i++)
1790 if (i >= p_stream->i_entry_count - 1 ||
1791 entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
1796 /* Add entry to the stsc table */
1797 if (i_stsc_last_val != i - i_first) {
1798 bo_add_32be(stsc, 1 + i_chunk); // first-chunk
1799 bo_add_32be(stsc, i - i_first) ; // samples-per-chunk
1800 bo_add_32be(stsc, 1); // sample-descr-index
1801 i_stsc_last_val = i - i_first;
1806 /* Fix stco entry count */
1807 bo_swap_32be(stco, 12, i_chunk);
1808 msg_Dbg(p_mux, "created %d chunks (stco)", i_chunk);
1810 /* Fix stsc entry count */
1811 bo_swap_32be(stsc, 12, i_stsc_entries );
1814 bo_t *stts = box_full_new("stts", 0, 0);
1821 bo_add_32be(stts, 0); // entry-count (fixed latter)
1823 unsigned i_index = 0;
1824 for (unsigned i = 0; i < p_stream->i_entry_count; i_index++) {
1826 mtime_t i_delta = p_stream->entry[i].i_length;
1828 for (; i < p_stream->i_entry_count; ++i)
1829 if (i == p_stream->i_entry_count || p_stream->entry[i].i_length != i_delta)
1832 bo_add_32be(stts, i - i_first); // sample-count
1833 bo_add_32be(stts, (uint64_t)i_delta * p_stream->i_timescale / CLOCK_FREQ); // sample-delta
1835 bo_swap_32be(stts, 12, i_index);
1837 /* composition time handling */
1839 if ( p_stream->b_hasbframes && (ctts = box_full_new("ctts", 0, 0)) )
1841 bo_add_32be(ctts, 0);
1843 for (unsigned i = 0; i < p_stream->i_entry_count; i_index++)
1846 mtime_t i_offset = p_stream->entry[i].i_pts_dts;
1848 for (; i < p_stream->i_entry_count; ++i)
1849 if (i == p_stream->i_entry_count || p_stream->entry[i].i_pts_dts != i_offset)
1852 bo_add_32be(ctts, i - i_first); // sample-count
1853 bo_add_32be(ctts, i_offset * p_stream->i_timescale / CLOCK_FREQ ); // sample-offset
1855 bo_swap_32be(ctts, 12, i_index);
1858 bo_t *stsz = box_full_new("stsz", 0, 0);
1867 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1870 i_size = p_stream->entry[i].i_size;
1871 else if ( p_stream->entry[i].i_size != i_size )
1877 bo_add_32be(stsz, i_size); // sample-size
1878 bo_add_32be(stsz, p_stream->i_entry_count); // sample-count
1879 if ( i_size == 0 ) // all samples have different size
1881 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1882 bo_add_32be(stsz, p_stream->entry[i].i_size); // sample-size
1885 /* create stss table */
1888 if ( p_stream->fmt.i_cat == VIDEO_ES || p_stream->fmt.i_cat == AUDIO_ES )
1890 mtime_t i_interval = -1;
1891 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1893 if ( i_interval != -1 )
1895 i_interval += p_stream->entry[i].i_length + p_stream->entry[i].i_pts_dts;
1896 if ( i_interval < CLOCK_FREQ * 2 )
1900 if (p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I) {
1902 stss = box_full_new("stss", 0, 0);
1905 bo_add_32be(stss, 0); /* fixed later */
1907 bo_add_32be(stss, 1 + i);
1915 bo_swap_32be(stss, 12, i_index);
1917 /* Now gather all boxes into stbl */
1918 bo_t *stbl = box_new("stbl");
1928 box_gather(stbl, stsd);
1929 box_gather(stbl, stts);
1931 box_gather(stbl, stss);
1933 box_gather(stbl, ctts);
1934 box_gather(stbl, stsc);
1935 box_gather(stbl, stsz);
1936 p_stream->i_stco_pos = stbl->b->i_buffer + 16;
1937 box_gather(stbl, stco);
1942 static int64_t get_timestamp(void);
1944 static void matrix_apply_rotation(es_format_t *fmt, uint32_t mvhd_matrix[9])
1946 enum video_orientation_t orientation = ORIENT_NORMAL;
1947 if (fmt->i_cat == VIDEO_ES)
1948 orientation = fmt->video.orientation;
1950 #define ATAN(a, b) do { mvhd_matrix[1] = (a) << 16; \
1951 mvhd_matrix[0] = (b) << 16; \
1954 switch (orientation) {
1955 case ORIENT_ROTATED_90: ATAN( 1, 0); break;
1956 case ORIENT_ROTATED_180: ATAN( 0, -1); break;
1957 case ORIENT_ROTATED_270: ATAN( -1, 0); break;
1958 default: ATAN( 0, 1); break;
1961 mvhd_matrix[3] = mvhd_matrix[0] ? 0 : 0x10000;
1962 mvhd_matrix[4] = mvhd_matrix[1] ? 0 : 0x10000;
1965 static bo_t *GetMoovBox(sout_mux_t *p_mux)
1967 sout_mux_sys_t *p_sys = p_mux->p_sys;
1971 uint32_t i_movie_timescale = 90000;
1972 int64_t i_movie_duration = 0;
1973 int64_t i_timestamp = get_timestamp();
1975 moov = box_new("moov");
1978 /* Create general info */
1979 if ( !p_sys->b_fragmented )
1981 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
1982 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1983 i_movie_duration = __MAX(i_movie_duration, p_stream->i_read_duration);
1985 msg_Dbg(p_mux, "movie duration %"PRId64"s", i_movie_duration / CLOCK_FREQ);
1987 i_movie_duration = i_movie_duration * i_movie_timescale / CLOCK_FREQ;
1990 i_movie_duration = 0;
1992 /* *** add /moov/mvhd *** */
1993 if (!p_sys->b_64_ext) {
1994 mvhd = box_full_new("mvhd", 0, 0);
2000 bo_add_32be(mvhd, i_timestamp); // creation time
2001 bo_add_32be(mvhd, i_timestamp); // modification time
2002 bo_add_32be(mvhd, i_movie_timescale); // timescale
2003 bo_add_32be(mvhd, i_movie_duration); // duration
2005 mvhd = box_full_new("mvhd", 1, 0);
2011 bo_add_64be(mvhd, i_timestamp); // creation time
2012 bo_add_64be(mvhd, i_timestamp); // modification time
2013 bo_add_32be(mvhd, i_movie_timescale); // timescale
2014 bo_add_64be(mvhd, i_movie_duration); // duration
2016 bo_add_32be(mvhd, 0x10000); // rate
2017 bo_add_16be(mvhd, 0x100); // volume
2018 bo_add_16be(mvhd, 0); // reserved
2019 for (int i = 0; i < 2; i++)
2020 bo_add_32be(mvhd, 0); // reserved
2022 uint32_t mvhd_matrix[9] = { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
2024 for (int i = 0; i < 9; i++)
2025 bo_add_32be(mvhd, mvhd_matrix[i]);// matrix
2026 for (int i = 0; i < 6; i++)
2027 bo_add_32be(mvhd, 0); // pre-defined
2029 /* Next available track id */
2030 bo_add_32be(mvhd, p_sys->i_nb_streams + 1); // next-track-id
2032 box_gather(moov, mvhd);
2034 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
2035 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
2037 mtime_t i_stream_duration;
2038 if ( !p_sys->b_fragmented )
2039 i_stream_duration = p_stream->i_read_duration * i_movie_timescale / CLOCK_FREQ;
2041 i_stream_duration = 0;
2043 /* *** add /moov/trak *** */
2044 bo_t *trak = box_new("trak");
2048 /* *** add /moov/trak/tkhd *** */
2050 if (!p_sys->b_64_ext) {
2052 tkhd = box_full_new("tkhd", 0, 0x0f);
2054 tkhd = box_full_new("tkhd", 0, 1);
2060 bo_add_32be(tkhd, i_timestamp); // creation time
2061 bo_add_32be(tkhd, i_timestamp); // modification time
2062 bo_add_32be(tkhd, p_stream->i_track_id);
2063 bo_add_32be(tkhd, 0); // reserved 0
2064 bo_add_32be(tkhd, i_stream_duration); // duration
2067 tkhd = box_full_new("tkhd", 1, 0x0f);
2069 tkhd = box_full_new("tkhd", 1, 1);
2075 bo_add_64be(tkhd, i_timestamp); // creation time
2076 bo_add_64be(tkhd, i_timestamp); // modification time
2077 bo_add_32be(tkhd, p_stream->i_track_id);
2078 bo_add_32be(tkhd, 0); // reserved 0
2079 bo_add_64be(tkhd, i_stream_duration); // duration
2082 for (int i = 0; i < 2; i++)
2083 bo_add_32be(tkhd, 0); // reserved
2084 bo_add_16be(tkhd, 0); // layer
2085 bo_add_16be(tkhd, 0); // pre-defined
2087 bo_add_16be(tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0);
2088 bo_add_16be(tkhd, 0); // reserved
2089 matrix_apply_rotation(&p_stream->fmt, mvhd_matrix);
2090 for (int i = 0; i < 9; i++)
2091 bo_add_32be(tkhd, mvhd_matrix[i]); // matrix
2092 if (p_stream->fmt.i_cat == AUDIO_ES) {
2093 bo_add_32be(tkhd, 0); // width (presentation)
2094 bo_add_32be(tkhd, 0); // height(presentation)
2095 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
2096 int i_width = p_stream->fmt.video.i_width << 16;
2097 if (p_stream->fmt.video.i_sar_num > 0 && p_stream->fmt.video.i_sar_den > 0) {
2098 i_width = (int64_t)p_stream->fmt.video.i_sar_num *
2099 ((int64_t)p_stream->fmt.video.i_width << 16) /
2100 p_stream->fmt.video.i_sar_den;
2102 // width (presentation)
2103 bo_add_32be(tkhd, i_width);
2104 // height(presentation)
2105 bo_add_32be(tkhd, p_stream->fmt.video.i_height << 16);
2107 int i_width = 320 << 16;
2109 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++) {
2110 mp4_stream_t *tk = p_sys->pp_streams[i];
2111 if (tk->fmt.i_cat == VIDEO_ES) {
2112 if (tk->fmt.video.i_sar_num > 0 &&
2113 tk->fmt.video.i_sar_den > 0)
2114 i_width = (int64_t)tk->fmt.video.i_sar_num *
2115 ((int64_t)tk->fmt.video.i_width << 16) /
2116 tk->fmt.video.i_sar_den;
2118 i_width = tk->fmt.video.i_width << 16;
2119 i_height = tk->fmt.video.i_height;
2123 bo_add_32be(tkhd, i_width); // width (presentation)
2124 bo_add_32be(tkhd, i_height << 16); // height(presentation)
2127 box_gather(trak, tkhd);
2129 /* *** add /moov/trak/edts and elst */
2130 if ( !p_sys->b_fragmented )
2132 bo_t *elst = box_full_new("elst", p_sys->b_64_ext ? 1 : 0, 0);
2135 if (p_stream->i_starttime > 0) {
2136 bo_add_32be(elst, 2);
2138 if (p_sys->b_64_ext) {
2139 bo_add_64be(elst, p_stream->i_starttime *
2140 i_movie_timescale / CLOCK_FREQ);
2141 bo_add_64be(elst, -1);
2143 bo_add_32be(elst, p_stream->i_starttime *
2144 i_movie_timescale / CLOCK_FREQ);
2145 bo_add_32be(elst, -1);
2147 bo_add_16be(elst, 1);
2148 bo_add_16be(elst, 0);
2150 bo_add_32be(elst, 1);
2152 if (p_sys->b_64_ext) {
2153 bo_add_64be(elst, p_stream->i_read_duration *
2154 i_movie_timescale / CLOCK_FREQ);
2155 bo_add_64be(elst, 0);
2157 bo_add_32be(elst, p_stream->i_read_duration *
2158 i_movie_timescale / CLOCK_FREQ);
2159 bo_add_32be(elst, 0);
2161 bo_add_16be(elst, 1);
2162 bo_add_16be(elst, 0);
2164 bo_t *edts = box_new("edts");
2167 box_gather(edts, elst);
2168 box_gather(trak, edts);
2174 /* *** add /moov/trak/mdia *** */
2175 bo_t *mdia = box_new("mdia");
2184 if (!p_sys->b_64_ext) {
2185 mdhd = box_full_new("mdhd", 0, 0);
2192 bo_add_32be(mdhd, i_timestamp); // creation time
2193 bo_add_32be(mdhd, i_timestamp); // modification time
2194 bo_add_32be(mdhd, p_stream->i_timescale); // timescale
2195 bo_add_32be(mdhd, i_stream_duration); // duration
2197 mdhd = box_full_new("mdhd", 1, 0);
2204 bo_add_64be(mdhd, i_timestamp); // creation time
2205 bo_add_64be(mdhd, i_timestamp); // modification time
2206 bo_add_32be(mdhd, p_stream->i_timescale); // timescale
2207 bo_add_64be(mdhd, i_stream_duration); // duration
2210 if (p_stream->fmt.psz_language) {
2211 char *psz = p_stream->fmt.psz_language;
2212 const iso639_lang_t *pl = NULL;
2213 uint16_t lang = 0x0;
2215 if (strlen(psz) == 2)
2216 pl = GetLang_1(psz);
2217 else if (strlen(psz) == 3) {
2218 pl = GetLang_2B(psz);
2219 if (!strcmp(pl->psz_iso639_1, "??"))
2220 pl = GetLang_2T(psz);
2223 if (pl && strcmp(pl->psz_iso639_1, "??"))
2224 lang = ((pl->psz_iso639_2T[0] - 0x60) << 10) |
2225 ((pl->psz_iso639_2T[1] - 0x60) << 5) |
2226 ((pl->psz_iso639_2T[2] - 0x60));
2227 bo_add_16be(mdhd, lang); // language
2229 bo_add_16be(mdhd, 0 ); // language
2230 bo_add_16be(mdhd, 0 ); // predefined
2231 box_gather(mdia, mdhd);
2233 /* handler reference */
2234 bo_t *hdlr = box_full_new("hdlr", 0, 0);
2243 bo_add_fourcc(hdlr, "mhlr"); // media handler
2245 bo_add_32be(hdlr, 0);
2247 if (p_stream->fmt.i_cat == AUDIO_ES)
2248 bo_add_fourcc(hdlr, "soun");
2249 else if (p_stream->fmt.i_cat == VIDEO_ES)
2250 bo_add_fourcc(hdlr, "vide");
2251 else if (p_stream->fmt.i_cat == SPU_ES)
2252 bo_add_fourcc(hdlr, "text");
2254 bo_add_32be(hdlr, 0); // reserved
2255 bo_add_32be(hdlr, 0); // reserved
2256 bo_add_32be(hdlr, 0); // reserved
2259 bo_add_8(hdlr, 12); /* Pascal string for .mov */
2261 if (p_stream->fmt.i_cat == AUDIO_ES)
2262 bo_add_mem(hdlr, 12, (uint8_t*)"SoundHandler");
2263 else if (p_stream->fmt.i_cat == VIDEO_ES)
2264 bo_add_mem(hdlr, 12, (uint8_t*)"VideoHandler");
2266 bo_add_mem(hdlr, 12, (uint8_t*)"Text Handler");
2269 bo_add_8(hdlr, 0); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
2271 box_gather(mdia, hdlr);
2274 bo_t *minf = box_new("minf");
2283 if (p_stream->fmt.i_cat == AUDIO_ES) {
2284 bo_t *smhd = box_full_new("smhd", 0, 0);
2287 bo_add_16be(smhd, 0); // balance
2288 bo_add_16be(smhd, 0); // reserved
2290 box_gather(minf, smhd);
2292 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
2293 bo_t *vmhd = box_full_new("vmhd", 0, 1);
2296 bo_add_16be(vmhd, 0); // graphicsmode
2297 for (int i = 0; i < 3; i++)
2298 bo_add_16be(vmhd, 0); // opcolor
2299 box_gather(minf, vmhd);
2301 } else if (p_stream->fmt.i_cat == SPU_ES) {
2302 bo_t *gmin = box_full_new("gmin", 0, 1);
2305 bo_add_16be(gmin, 0); // graphicsmode
2306 for (int i = 0; i < 3; i++)
2307 bo_add_16be(gmin, 0); // opcolor
2308 bo_add_16be(gmin, 0); // balance
2309 bo_add_16be(gmin, 0); // reserved
2311 bo_t *gmhd = box_new("gmhd");
2314 box_gather(gmhd, gmin);
2315 box_gather(minf, gmhd);
2322 bo_t *dref = box_full_new("dref", 0, 0);
2325 bo_add_32be(dref, 1);
2327 bo_t *url = box_full_new("url ", 0, 0x01);
2329 box_gather(dref, url);
2331 bo_t *dinf = box_new("dinf");
2334 box_gather(dinf, dref);
2336 /* append dinf to mdia */
2337 box_gather(minf, dinf);
2344 if ( p_sys->b_fragmented )
2346 uint32_t i_backup = p_stream->i_entry_count;
2347 p_stream->i_entry_count = 0;
2348 stbl = GetStblBox(p_mux, p_stream);
2349 p_stream->i_entry_count = i_backup;
2352 stbl = GetStblBox(p_mux, p_stream);
2354 /* append stbl to minf */
2355 p_stream->i_stco_pos += minf->b->i_buffer;
2356 box_gather(minf, stbl);
2358 /* append minf to mdia */
2359 p_stream->i_stco_pos += mdia->b->i_buffer;
2360 box_gather(mdia, minf);
2362 /* append mdia to trak */
2363 p_stream->i_stco_pos += trak->b->i_buffer;
2364 box_gather(trak, mdia);
2366 /* append trak to moov */
2367 p_stream->i_stco_pos += moov->b->i_buffer;
2368 box_gather(moov, trak);
2371 /* Add user data tags */
2372 box_gather(moov, GetUdtaTag(p_mux));
2374 if ( p_sys->b_fragmented )
2376 bo_t *mvex = box_new("mvex");
2377 for (unsigned int i_trak = 0; mvex && i_trak < p_sys->i_nb_streams; i_trak++)
2379 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
2381 /* Try to find some defaults */
2382 if ( p_stream->i_entry_count )
2384 // FIXME: find highest occurence
2385 p_stream->i_trex_length = p_stream->entry[0].i_length;
2386 p_stream->i_trex_size = p_stream->entry[0].i_size;
2389 /* *** add /mvex/trex *** */
2390 bo_t *trex = box_full_new("trex", 0, 0);
2391 bo_add_32be(trex, p_stream->i_track_id);
2392 bo_add_32be(trex, 1); // sample desc index
2393 bo_add_32be(trex, (uint64_t)p_stream->i_trex_length * p_stream->i_timescale / CLOCK_FREQ); // sample duration
2394 bo_add_32be(trex, p_stream->i_trex_size); // sample size
2395 bo_add_32be(trex, 0); // sample flags
2396 box_gather(mvex, trex);
2398 box_gather(moov, mvex);
2402 box_fix(moov, moov->b->i_buffer);
2406 /****************************************************************************/
2408 static bo_t *box_new(const char *fcc)
2410 bo_t *box = malloc(sizeof(*box));
2416 bo_add_32be (box, 0);
2417 bo_add_fourcc(box, fcc);
2422 static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f)
2424 bo_t *box = box_new(fcc);
2429 bo_add_24be (box, f);
2434 static void box_fix(bo_t *box, uint32_t i_size)
2436 bo_set_32be(box, 0, i_size);
2439 static void box_gather (bo_t *box, bo_t *box2)
2441 if(box2 && box2->b && box && box->b)
2443 box_fix(box2, box2->b->i_buffer);
2444 size_t i_offset = box->b->i_buffer;
2445 box->b = block_Realloc(box->b, 0, box->b->i_buffer + box2->b->i_buffer);
2446 memcpy(&box->b->p_buffer[i_offset], box2->b->p_buffer, box2->b->i_buffer);
2451 static void box_send(sout_mux_t *p_mux, bo_t *box)
2454 sout_AccessOutWrite(p_mux->p_access, box->b);
2458 static int64_t get_timestamp(void)
2460 int64_t i_timestamp = time(NULL);
2462 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2463 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60
2468 /***************************************************************************
2470 ****************************************************************************/
2471 #define FRAGMENT_LENGTH (CLOCK_FREQ * 3/2)
2473 #define ENQUEUE_ENTRY(object, entry) \
2476 object.p_last->p_next = entry;\
2477 object.p_last = entry;\
2478 if (!object.p_first)\
2479 object.p_first = entry;\
2482 #define DEQUEUE_ENTRY(object, entry) \
2484 entry = object.p_first;\
2485 if (object.p_last == entry)\
2486 object.p_last = NULL;\
2487 object.p_first = object.p_first->p_next;\
2488 entry->p_next = NULL;\
2491 /* Creates mfra/traf index entries */
2492 static void AddKeyframeEntry(mp4_stream_t *p_stream, const uint64_t i_moof_pos,
2493 const uint8_t i_traf, const uint32_t i_sample,
2494 const mtime_t i_time)
2496 /* alloc or realloc */
2497 mp4_fragindex_t *p_entries = p_stream->p_indexentries;
2498 if (p_stream->i_indexentries >= p_stream->i_indexentriesmax)
2500 p_stream->i_indexentriesmax += 256;
2501 p_entries = xrealloc(p_stream->p_indexentries,
2502 p_stream->i_indexentriesmax * sizeof(mp4_fragindex_t));
2503 if (p_entries) /* realloc can fail */
2504 p_stream->p_indexentries = p_entries;
2507 mtime_t i_last_entry_time;
2508 if (p_stream->i_indexentries)
2509 i_last_entry_time = p_stream->p_indexentries[p_stream->i_indexentries - 1].i_time;
2511 i_last_entry_time = 0;
2513 if (p_entries && i_time - i_last_entry_time >= CLOCK_FREQ * 2)
2515 mp4_fragindex_t *p_indexentry = &p_stream->p_indexentries[p_stream->i_indexentries];
2516 p_indexentry->i_time = i_time;
2517 p_indexentry->i_moofoffset = i_moof_pos;
2518 p_indexentry->i_sample = i_sample;
2519 p_indexentry->i_traf = i_traf;
2520 p_indexentry->i_trun = 1;
2521 p_stream->i_indexentries++;
2525 /* Creates moof box and traf/trun information.
2526 * Single run per traf is absolutely not optimal as interleaving should be done
2527 * using runs and not limiting moof size, but creating an relative offset only
2528 * requires base_offset_is_moof and then comply to late iso brand spec which
2529 * breaks clients. */
2530 static bo_t *GetMoofBox(sout_mux_t *p_mux, size_t *pi_mdat_total_size,
2531 mtime_t i_barrier_time, const uint64_t i_write_pos)
2533 sout_mux_sys_t *p_sys = p_mux->p_sys;
2536 size_t i_fixupoffset = 0;
2538 *pi_mdat_total_size = 0;
2540 moof = box_new("moof");
2544 /* *** add /moof/mfhd *** */
2546 mfhd = box_full_new("mfhd", 0, 0);
2552 bo_add_32be(mfhd, p_sys->i_mfhd_sequence++); // sequence number
2554 box_gather(moof, mfhd);
2556 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++)
2558 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
2560 /* *** add /moof/traf *** */
2561 bo_t *traf = box_new("traf");
2564 uint32_t i_sample = 0;
2565 mtime_t i_time = p_stream->i_written_duration;
2566 bool b_allsamesize = true;
2567 bool b_allsamelength = true;
2568 if ( p_stream->read.p_first )
2570 mp4_fragentry_t *p_entry = p_stream->read.p_first->p_next;
2571 while (p_entry && (b_allsamelength || b_allsamesize))
2573 /* compare against queue head */
2574 b_allsamelength &= ( p_entry->p_block->i_length == p_stream->read.p_first->p_block->i_length );
2575 b_allsamesize &= ( p_entry->p_block->i_buffer == p_stream->read.p_first->p_block->i_buffer );
2576 p_entry = p_entry->p_next;
2580 uint32_t i_tfhd_flags = 0x0;
2581 if (p_stream->read.p_first)
2583 /* Current segment have all same duration value, different than trex's default */
2584 if (b_allsamelength &&
2585 p_stream->read.p_first->p_block->i_length != p_stream->i_trex_length &&
2586 p_stream->read.p_first->p_block->i_length)
2587 i_tfhd_flags |= MP4_TFHD_DFLT_SAMPLE_DURATION;
2589 /* Current segment have all same size value, different than trex's default */
2590 if (b_allsamesize &&
2591 p_stream->read.p_first->p_block->i_buffer != p_stream->i_trex_size &&
2592 p_stream->read.p_first->p_block->i_buffer)
2593 i_tfhd_flags |= MP4_TFHD_DFLT_SAMPLE_SIZE;
2597 /* We have no samples */
2598 i_tfhd_flags |= MP4_TFHD_DURATION_IS_EMPTY;
2601 /* *** add /moof/traf/tfhd *** */
2602 bo_t *tfhd = box_full_new("tfhd", 0, i_tfhd_flags);
2608 bo_add_32be(tfhd, p_stream->i_track_id);
2610 /* set the local sample duration default */
2611 if (i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_DURATION)
2612 bo_add_32be(tfhd, p_stream->read.p_first->p_block->i_length * p_stream->i_timescale / CLOCK_FREQ);
2614 /* set the local sample size default */
2615 if (i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_SIZE)
2616 bo_add_32be(tfhd, p_stream->read.p_first->p_block->i_buffer);
2618 box_gather(traf, tfhd);
2620 /* *** add /moof/traf/tfdt *** */
2621 bo_t *tfdt = box_full_new("tfdt", 1, 0);
2627 bo_add_64be(tfdt, p_stream->i_written_duration * p_stream->i_timescale / CLOCK_FREQ );
2628 box_gather(traf, tfdt);
2630 /* *** add /moof/traf/trun *** */
2631 if (p_stream->read.p_first)
2633 uint32_t i_trun_flags = 0x0;
2635 if (p_stream->b_hasiframes && !(p_stream->read.p_first->p_block->i_flags & BLOCK_FLAG_TYPE_I))
2636 i_trun_flags |= MP4_TRUN_FIRST_FLAGS;
2638 if (!b_allsamelength ||
2639 ( !(i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_DURATION) && p_stream->i_trex_length == 0 ))
2640 i_trun_flags |= MP4_TRUN_SAMPLE_DURATION;
2642 if (!b_allsamesize ||
2643 ( !(i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_SIZE) && p_stream->i_trex_size == 0 ))
2644 i_trun_flags |= MP4_TRUN_SAMPLE_SIZE;
2646 if (p_stream->b_hasbframes)
2647 i_trun_flags |= MP4_TRUN_SAMPLE_TIME_OFFSET;
2649 if (i_fixupoffset == 0)
2650 i_trun_flags |= MP4_TRUN_DATA_OFFSET;
2652 bo_t *trun = box_full_new("trun", 0, i_trun_flags);
2660 uint32_t i_entry_count = 0;
2661 mtime_t i_run_time = p_stream->i_written_duration;
2662 mp4_fragentry_t *p_entry = p_stream->read.p_first;
2665 if ( i_barrier_time && i_run_time + p_entry->p_block->i_length > i_barrier_time )
2668 i_run_time += p_entry->p_block->i_length;
2669 p_entry = p_entry->p_next;
2671 bo_add_32be(trun, i_entry_count); // sample count
2673 if (i_trun_flags & MP4_TRUN_DATA_OFFSET)
2675 i_fixupoffset = moof->b->i_buffer + traf->b->i_buffer + trun->b->i_buffer;
2676 bo_add_32be(trun, 0xdeadbeef); // data offset
2679 if (i_trun_flags & MP4_TRUN_FIRST_FLAGS)
2680 bo_add_32be(trun, 1<<16); // flag as non keyframe
2682 while(p_stream->read.p_first && i_entry_count)
2684 DEQUEUE_ENTRY(p_stream->read, p_entry);
2686 if (i_trun_flags & MP4_TRUN_SAMPLE_DURATION)
2687 bo_add_32be(trun, p_entry->p_block->i_length * p_stream->i_timescale / CLOCK_FREQ); // sample duration
2689 if (i_trun_flags & MP4_TRUN_SAMPLE_SIZE)
2690 bo_add_32be(trun, p_entry->p_block->i_buffer); // sample size
2692 if (i_trun_flags & MP4_TRUN_SAMPLE_TIME_OFFSET)
2694 uint32_t i_diff = 0;
2695 if ( p_entry->p_block->i_dts > VLC_TS_INVALID &&
2696 p_entry->p_block->i_pts > p_entry->p_block->i_dts )
2698 i_diff = p_entry->p_block->i_pts - p_entry->p_block->i_dts;
2700 bo_add_32be(trun, i_diff * p_stream->i_timescale / CLOCK_FREQ); // ctts
2703 *pi_mdat_total_size += p_entry->p_block->i_buffer;
2705 ENQUEUE_ENTRY(p_stream->towrite, p_entry);
2709 /* Add keyframe entry if needed */
2710 if (p_stream->b_hasiframes && (p_entry->p_block->i_flags & BLOCK_FLAG_TYPE_I) &&
2711 (p_stream->fmt.i_cat == VIDEO_ES || p_stream->fmt.i_cat == AUDIO_ES))
2713 AddKeyframeEntry(p_stream, i_write_pos, i_trak, i_sample, i_time);
2716 i_time += p_entry->p_block->i_length;
2719 box_gather(traf, trun);
2722 box_gather(moof, traf);
2731 box_fix(moof, moof->b->i_buffer);
2733 /* do tfhd base data offset fixup */
2736 /* mdat will follow moof */
2737 bo_set_32be(moof, i_fixupoffset, moof->b->i_buffer + 8);
2740 /* set iframe flag, so the streaming server always starts from moof */
2741 moof->b->i_flags |= BLOCK_FLAG_TYPE_I;
2746 static void WriteFragmentMDAT(sout_mux_t *p_mux, size_t i_total_size)
2748 sout_mux_sys_t *p_sys = p_mux->p_sys;
2750 /* Now add mdat header */
2751 bo_t *mdat = box_new("mdat");
2754 /* force update of real size */
2755 assert(mdat->b->i_buffer==8);
2756 box_fix(mdat, mdat->b->i_buffer + i_total_size);
2757 p_sys->i_pos += mdat->b->i_buffer;
2758 /* only write header */
2759 sout_AccessOutWrite(p_mux->p_access, mdat->b);
2761 /* Header and its size are written and good, now write content */
2762 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++)
2764 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
2766 while(p_stream->towrite.p_first)
2768 mp4_fragentry_t *p_entry = p_stream->towrite.p_first;
2769 p_sys->i_pos += p_entry->p_block->i_buffer;
2770 p_stream->i_written_duration += p_entry->p_block->i_length;
2772 p_entry->p_block->i_flags &= ~BLOCK_FLAG_TYPE_I; // clear flag for http stream
2773 sout_AccessOutWrite(p_mux->p_access, p_entry->p_block);
2775 p_stream->towrite.p_first = p_entry->p_next;
2777 if (!p_stream->towrite.p_first)
2778 p_stream->towrite.p_last = NULL;
2783 static bo_t *GetMfraBox(sout_mux_t *p_mux)
2785 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2787 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2789 mp4_stream_t *p_stream = p_sys->pp_streams[i];
2790 if (p_stream->i_indexentries)
2792 bo_t *tfra = box_full_new("tfra", 0, 0x0);
2793 if (!tfra) continue;
2794 bo_add_32be(tfra, p_stream->i_track_id);
2795 bo_add_32be(tfra, 0x3); // reserved + lengths (1,1,4)=>(0,0,3)
2796 bo_add_32be(tfra, p_stream->i_indexentries);
2797 for(uint32_t i_index=0; i_index<p_stream->i_indexentries; i_index++)
2799 const mp4_fragindex_t *p_indexentry = &p_stream->p_indexentries[i_index];
2800 bo_add_32be(tfra, p_indexentry->i_time);
2801 bo_add_32be(tfra, p_indexentry->i_moofoffset);
2802 assert(sizeof(p_indexentry->i_traf)==1); /* guard against sys changes */
2803 assert(sizeof(p_indexentry->i_trun)==1);
2804 assert(sizeof(p_indexentry->i_sample)==4);
2805 bo_add_8(tfra, p_indexentry->i_traf);
2806 bo_add_8(tfra, p_indexentry->i_trun);
2807 bo_add_32be(tfra, p_indexentry->i_sample);
2810 if (!mfra && !(mfra = box_new("mfra")))
2816 box_gather(mfra,tfra);
2822 static void FlushHeader(sout_mux_t *p_mux)
2824 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2826 /* Now add ftyp header */
2827 bo_t *ftyp = box_new("ftyp");
2830 bo_add_fourcc(ftyp, "isom");
2831 bo_add_32be (ftyp, 0); // minor version
2833 box_fix(ftyp, ftyp->b->i_buffer);
2835 bo_t *moov = GetMoovBox(p_mux);
2837 /* merge into a single block */
2838 box_gather(ftyp, moov);
2840 /* add header flag for streaming server */
2841 ftyp->b->i_flags |= BLOCK_FLAG_HEADER;
2842 p_sys->i_pos += ftyp->b->i_buffer;
2843 box_send(p_mux, ftyp);
2844 p_sys->b_header_sent = true;
2847 static int OpenFrag(vlc_object_t *p_this)
2849 sout_mux_t *p_mux = (sout_mux_t*) p_this;
2850 sout_mux_sys_t *p_sys = malloc(sizeof(sout_mux_sys_t));
2854 p_mux->p_sys = (sout_mux_sys_t *) p_sys;
2855 p_mux->pf_control = Control;
2856 p_mux->pf_addstream = AddStream;
2857 p_mux->pf_delstream = DelStream;
2858 p_mux->pf_mux = MuxFrag;
2861 p_sys->b_mov = false;
2862 p_sys->b_3gp = false;
2863 p_sys->b_64_ext = false;
2867 p_sys->i_nb_streams = 0;
2868 p_sys->pp_streams = NULL;
2869 p_sys->i_mdat_pos = 0;
2870 p_sys->i_read_duration = 0;
2871 p_sys->i_written_duration= 0;
2873 p_sys->b_header_sent = false;
2874 p_sys->b_fragmented = true;
2875 p_sys->i_mfhd_sequence = 1;
2880 static void WriteFragments(sout_mux_t *p_mux, bool b_flush)
2882 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2884 mtime_t i_barrier_time = p_sys->i_written_duration + FRAGMENT_LENGTH;
2885 size_t i_mdat_size = 0;
2886 bool b_has_samples = false;
2888 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2890 const mp4_stream_t *p_stream = p_sys->pp_streams[i];
2891 if (p_stream->read.p_first)
2893 b_has_samples = true;
2895 /* set a barrier so we try to align to keyframe */
2896 if (p_stream->b_hasiframes &&
2897 p_stream->i_last_iframe_time > p_stream->i_written_duration &&
2898 (p_stream->fmt.i_cat == VIDEO_ES ||
2899 p_stream->fmt.i_cat == AUDIO_ES) )
2901 i_barrier_time = __MIN(i_barrier_time, p_stream->i_last_iframe_time);
2906 if (!p_sys->b_header_sent)
2910 moof = GetMoofBox(p_mux, &i_mdat_size, (b_flush)?0:i_barrier_time, p_sys->i_pos);
2912 if (moof && i_mdat_size == 0)
2914 block_Release(moof->b);
2920 msg_Dbg(p_mux, "writing moof @ %"PRId64, p_sys->i_pos);
2921 p_sys->i_pos += moof->b->i_buffer;
2922 assert(moof->b->i_flags & BLOCK_FLAG_TYPE_I); /* http sout */
2923 box_send(p_mux, moof);
2924 msg_Dbg(p_mux, "writing mdat @ %"PRId64, p_sys->i_pos);
2925 WriteFragmentMDAT(p_mux, i_mdat_size);
2927 /* update iframe point */
2928 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2930 mp4_stream_t *p_stream = p_sys->pp_streams[i];
2931 p_stream->i_last_iframe_time = 0;
2936 /* Do an entry length fixup using only its own info.
2937 * This is the end boundary case. */
2938 static void LengthLocalFixup(sout_mux_t *p_mux, const mp4_stream_t *p_stream, block_t *p_entrydata)
2940 if ( p_stream->fmt.i_cat == VIDEO_ES )
2942 p_entrydata->i_length = CLOCK_FREQ *
2943 p_stream->fmt.video.i_frame_rate_base /
2944 p_stream->fmt.video.i_frame_rate;
2945 msg_Dbg(p_mux, "video track %d fixup to %"PRId64" for sample %u",
2946 p_stream->i_track_id, p_entrydata->i_length, p_stream->i_entry_count - 1);
2948 else if (p_stream->fmt.i_cat == AUDIO_ES &&
2949 p_stream->fmt.audio.i_rate &&
2950 p_entrydata->i_nb_samples)
2952 p_entrydata->i_length = CLOCK_FREQ * p_entrydata->i_nb_samples /
2953 p_stream->fmt.audio.i_rate;
2954 msg_Dbg(p_mux, "audio track %d fixup to %"PRId64" for sample %u",
2955 p_stream->i_track_id, p_entrydata->i_length, p_stream->i_entry_count - 1);
2959 msg_Warn(p_mux, "unknown length for track %d sample %u",
2960 p_stream->i_track_id, p_stream->i_entry_count - 1);
2961 p_entrydata->i_length = 1;
2965 static void CleanupFrag(sout_mux_sys_t *p_sys)
2967 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2969 mp4_stream_t *p_stream = p_sys->pp_streams[i];
2970 if (p_stream->p_held_entry)
2972 block_Release(p_stream->p_held_entry->p_block);
2973 free(p_stream->p_held_entry);
2975 while(p_stream->read.p_first)
2977 mp4_fragentry_t *p_next = p_stream->read.p_first->p_next;
2978 block_Release(p_stream->read.p_first->p_block);
2979 free(p_stream->read.p_first);
2980 p_stream->read.p_first = p_next;
2982 while(p_stream->towrite.p_first)
2984 mp4_fragentry_t *p_next = p_stream->towrite.p_first->p_next;
2985 block_Release(p_stream->towrite.p_first->p_block);
2986 free(p_stream->towrite.p_first);
2987 p_stream->towrite.p_first = p_next;
2989 free(p_stream->p_indexentries);
2994 static void CloseFrag(vlc_object_t *p_this)
2996 sout_mux_t *p_mux = (sout_mux_t *) p_this;
2997 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2999 /* Flush remaining entries */
3000 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
3002 mp4_stream_t *p_stream = p_sys->pp_streams[i];
3003 if (p_stream->p_held_entry)
3005 if (p_stream->p_held_entry->p_block->i_length < 1)
3006 LengthLocalFixup(p_mux, p_stream, p_stream->p_held_entry->p_block);
3007 ENQUEUE_ENTRY(p_stream->read, p_stream->p_held_entry);
3008 p_stream->p_held_entry = NULL;
3012 /* and force creating a fragment from it */
3013 WriteFragments(p_mux, true);
3015 /* Write indexes, but only for non streamed content
3016 as they refer to moof by absolute position */
3017 if (!strcmp(p_mux->psz_mux, "mp4frag"))
3019 bo_t *mfra = GetMfraBox(p_mux);
3022 bo_t *mfro = box_full_new("mfro", 0, 0x0);
3023 if (mfro && mfra->b)
3025 box_fix(mfra, mfra->b->i_buffer);
3026 bo_add_32be(mfro, mfra->b->i_buffer + MP4_MFRO_BOXSIZE);
3027 box_gather(mfra, mfro);
3029 box_send(p_mux, mfra);
3036 static int MuxFrag(sout_mux_t *p_mux)
3038 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
3040 int i_stream = sout_MuxGetStream(p_mux, 1, NULL);
3043 sout_input_t *p_input = p_mux->pp_inputs[i_stream];
3044 mp4_stream_t *p_stream = (mp4_stream_t*) p_input->p_sys;
3045 block_t *p_currentblock = block_FifoGet(p_input->p_fifo);
3047 /* do block conversion */
3048 switch(p_stream->fmt.i_codec)
3050 case VLC_CODEC_H264:
3051 case VLC_CODEC_HEVC:
3052 p_currentblock = ConvertFromAnnexB(p_currentblock);
3054 case VLC_CODEC_SUBT:
3055 p_currentblock = ConvertSUBT(p_currentblock);
3061 if( !p_currentblock )
3064 /* If we have a previous entry for outgoing queue */
3065 if (p_stream->p_held_entry)
3067 block_t *p_heldblock = p_stream->p_held_entry->p_block;
3069 /* Fix previous block length from current */
3070 if (p_heldblock->i_length < 1)
3073 /* Fix using dts if not on a boundary */
3074 if ((p_currentblock->i_flags & BLOCK_FLAG_DISCONTINUITY) == 0)
3075 p_heldblock->i_length = p_currentblock->i_dts - p_heldblock->i_dts;
3077 if (p_heldblock->i_length < 1)
3078 LengthLocalFixup(p_mux, p_stream, p_heldblock);
3082 ENQUEUE_ENTRY(p_stream->read, p_stream->p_held_entry);
3083 p_stream->p_held_entry = NULL;
3085 if (p_stream->b_hasiframes && (p_heldblock->i_flags & BLOCK_FLAG_TYPE_I) &&
3086 p_stream->i_read_duration - p_sys->i_written_duration < FRAGMENT_LENGTH)
3088 /* Flag the last iframe time, we'll use it as boundary so it will start
3090 p_stream->i_last_iframe_time = p_stream->i_read_duration;
3093 /* update buffered time */
3094 p_stream->i_read_duration += __MAX(0, p_heldblock->i_length);
3098 /* set temp entry */
3099 p_stream->p_held_entry = malloc(sizeof(mp4_fragentry_t));
3100 if (unlikely(!p_stream->p_held_entry))
3103 p_stream->p_held_entry->p_block = p_currentblock;
3104 p_stream->p_held_entry->i_run = p_stream->i_current_run;
3105 p_stream->p_held_entry->p_next = NULL;
3107 if (p_stream->fmt.i_cat == VIDEO_ES )
3109 if (!p_stream->b_hasiframes && (p_currentblock->i_flags & BLOCK_FLAG_TYPE_I))
3110 p_stream->b_hasiframes = true;
3112 if (!p_stream->b_hasbframes && p_currentblock->i_dts > VLC_TS_INVALID &&
3113 p_currentblock->i_pts > p_currentblock->i_dts)
3114 p_stream->b_hasbframes = true;
3117 /* Update the global fragment/media duration */
3118 mtime_t i_min_read_duration = p_stream->i_read_duration;
3119 mtime_t i_min_written_duration = p_stream->i_written_duration;
3120 for (unsigned int i=0; i<p_sys->i_nb_streams; i++)
3122 const mp4_stream_t *p_s = p_sys->pp_streams[i];
3123 if (p_s->fmt.i_cat != VIDEO_ES && p_s->fmt.i_cat != AUDIO_ES)
3125 if (p_s->i_read_duration < i_min_read_duration)
3126 i_min_read_duration = p_s->i_read_duration;
3128 if (p_s->i_written_duration < i_min_written_duration)
3129 i_min_written_duration = p_s->i_written_duration;
3131 p_sys->i_read_duration = i_min_read_duration;
3132 p_sys->i_written_duration = i_min_written_duration;
3134 /* we have prerolled enough to know all streams, and have enough date to create a fragment */
3135 if (p_stream->read.p_first && p_sys->i_read_duration - p_sys->i_written_duration >= FRAGMENT_LENGTH)
3136 WriteFragments(p_mux, false);