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);
209 static void box_gather (bo_t *box, bo_t *box2);
211 static void box_send(sout_mux_t *p_mux, bo_t *box);
213 static bo_t *GetMoovBox(sout_mux_t *p_mux);
215 static block_t *ConvertSUBT(block_t *);
216 static block_t *ConvertFromAnnexB(block_t *);
218 static const char avc1_start_code[4] = { 0, 0, 0, 1 };
220 /*****************************************************************************
222 *****************************************************************************/
223 static int Open(vlc_object_t *p_this)
225 sout_mux_t *p_mux = (sout_mux_t*)p_this;
226 sout_mux_sys_t *p_sys;
229 msg_Dbg(p_mux, "Mp4 muxer opened");
230 config_ChainParse(p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg);
232 p_mux->pf_control = Control;
233 p_mux->pf_addstream = AddStream;
234 p_mux->pf_delstream = DelStream;
236 p_mux->p_sys = p_sys = malloc(sizeof(sout_mux_sys_t));
240 p_sys->i_nb_streams = 0;
241 p_sys->pp_streams = NULL;
242 p_sys->i_mdat_pos = 0;
243 p_sys->b_mov = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "mov");
244 p_sys->b_3gp = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "3gp");
245 p_sys->i_read_duration = 0;
246 p_sys->b_fragmented = false;
249 /* Now add ftyp header */
250 box = box_new("ftyp");
252 bo_add_fourcc(box, "3gp6");
254 bo_add_fourcc(box, "isom");
255 bo_add_32be (box, 0);
257 bo_add_fourcc(box, "3gp4");
259 bo_add_fourcc(box, "mp41");
260 bo_add_fourcc(box, "avc1");
263 p_sys->i_pos += box->len;
264 p_sys->i_mdat_pos = p_sys->i_pos;
266 box_send(p_mux, box);
270 * Quicktime actually doesn't like the 64 bits extensions !!! */
271 p_sys->b_64_ext = false;
273 /* Now add mdat header */
274 box = box_new("mdat");
275 bo_add_64be (box, 0); // enough to store an extended size
277 p_sys->i_pos += box->len;
279 box_send(p_mux, box);
284 /*****************************************************************************
286 *****************************************************************************/
287 static void Close(vlc_object_t *p_this)
289 sout_mux_t *p_mux = (sout_mux_t*)p_this;
290 sout_mux_sys_t *p_sys = p_mux->p_sys;
292 msg_Dbg(p_mux, "Close");
294 /* Update mdat size */
297 if (p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32)) {
299 bo_add_32be (&bo, 1);
300 bo_add_fourcc(&bo, "mdat");
301 bo_add_64be (&bo, p_sys->i_pos - p_sys->i_mdat_pos);
303 bo_add_32be (&bo, 8);
304 bo_add_fourcc(&bo, "wide");
305 bo_add_32be (&bo, p_sys->i_pos - p_sys->i_mdat_pos - 8);
306 bo_add_fourcc(&bo, "mdat");
309 bo.b->i_buffer = bo.len;
310 sout_AccessOutSeek(p_mux->p_access, p_sys->i_mdat_pos);
311 sout_AccessOutWrite(p_mux->p_access, bo.b);
313 /* Create MOOV header */
314 uint64_t i_moov_pos = p_sys->i_pos;
315 bo_t *moov = GetMoovBox(p_mux);
317 /* Check we need to create "fast start" files */
318 p_sys->b_fast_start = var_GetBool(p_this, SOUT_CFG_PREFIX "faststart");
319 while (p_sys->b_fast_start) {
320 /* Move data to the end of the file so we can fit the moov header
322 int64_t i_size = p_sys->i_pos - p_sys->i_mdat_pos;
323 int i_moov_size = moov->len;
326 int64_t i_chunk = __MIN(32768, i_size);
327 block_t *p_buf = block_Alloc(i_chunk);
328 sout_AccessOutSeek(p_mux->p_access,
329 p_sys->i_mdat_pos + i_size - i_chunk);
330 if (sout_AccessOutRead(p_mux->p_access, p_buf) < i_chunk) {
331 msg_Warn(p_this, "read() not supported by access output, "
332 "won't create a fast start file");
333 p_sys->b_fast_start = false;
334 block_Release(p_buf);
337 sout_AccessOutSeek(p_mux->p_access, p_sys->i_mdat_pos + i_size +
338 i_moov_size - i_chunk);
339 sout_AccessOutWrite(p_mux->p_access, p_buf);
343 if (!p_sys->b_fast_start)
346 /* Fix-up samples to chunks table in MOOV header */
347 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
348 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
350 moov->len = p_stream->i_stco_pos;
351 for (unsigned i = 0; i < p_stream->i_entry_count; ) {
352 mp4_entry_t *entry = p_stream->entry;
353 if (p_stream->b_stco64)
354 bo_add_64be(moov, entry[i].i_pos + i_moov_size);
356 bo_add_32be(moov, entry[i].i_pos + i_moov_size);
358 for (; i < p_stream->i_entry_count; i++)
359 if (i >= p_stream->i_entry_count - 1 ||
360 entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
367 moov->len = i_moov_size;
368 i_moov_pos = p_sys->i_mdat_pos;
369 p_sys->b_fast_start = false;
372 /* Write MOOV header */
373 sout_AccessOutSeek(p_mux->p_access, i_moov_pos);
374 box_send(p_mux, moov);
377 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
378 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
380 es_format_Clean(&p_stream->fmt);
381 if (p_stream->a52_frame)
382 block_Release(p_stream->a52_frame);
383 free(p_stream->entry);
386 if (p_sys->i_nb_streams)
387 free(p_sys->pp_streams);
391 /*****************************************************************************
393 *****************************************************************************/
394 static int Control(sout_mux_t *p_mux, int i_query, va_list args)
401 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
402 pb_bool = (bool*)va_arg(args, bool *);
406 case MUX_GET_ADD_STREAM_WAIT:
407 pb_bool = (bool*)va_arg(args, bool *);
411 case MUX_GET_MIME: /* Not needed, as not streamable */
417 /*****************************************************************************
419 *****************************************************************************/
420 static int AddStream(sout_mux_t *p_mux, sout_input_t *p_input)
422 sout_mux_sys_t *p_sys = p_mux->p_sys;
423 mp4_stream_t *p_stream;
425 switch(p_input->p_fmt->i_codec)
436 case VLC_CODEC_MJPGB:
442 case VLC_CODEC_AMR_NB:
443 case VLC_CODEC_AMR_WB:
448 msg_Warn(p_mux, "subtitle track added like in .mov (even when creating .mp4)");
451 msg_Err(p_mux, "unsupported codec %4.4s in mp4",
452 (char*)&p_input->p_fmt->i_codec);
456 p_stream = malloc(sizeof(mp4_stream_t));
459 es_format_Copy(&p_stream->fmt, p_input->p_fmt);
460 p_stream->i_track_id = p_sys->i_nb_streams + 1;
461 p_stream->i_length_neg = 0;
462 p_stream->i_entry_count = 0;
463 p_stream->i_entry_max = 1000;
465 calloc(p_stream->i_entry_max, sizeof(mp4_entry_t));
466 p_stream->i_dts_start = 0;
467 p_stream->i_read_duration = 0;
468 p_stream->a52_frame = NULL;
469 switch( p_stream->fmt.i_cat )
472 if(!p_stream->fmt.audio.i_rate)
474 msg_Warn( p_mux, "no audio rate given for stream %d, assuming 48KHz",
475 p_sys->i_nb_streams );
476 p_stream->fmt.audio.i_rate = 48000;
478 p_stream->i_timescale = p_stream->fmt.audio.i_rate;
481 if( !p_stream->fmt.video.i_frame_rate ||
482 !p_stream->fmt.video.i_frame_rate_base )
484 msg_Warn( p_mux, "Missing frame rate for stream %d, assuming 25fps",
485 p_sys->i_nb_streams );
486 p_stream->fmt.video.i_frame_rate = 25;
487 p_stream->fmt.video.i_frame_rate_base = 1;
489 p_stream->i_timescale = p_stream->fmt.video.i_frame_rate * 1000 /
490 p_stream->fmt.video.i_frame_rate_base;
493 p_stream->i_timescale = CLOCK_FREQ;
497 p_stream->i_starttime = p_sys->i_read_duration;
498 p_stream->b_hasbframes = false;
500 p_stream->i_last_dts = 0;
501 p_stream->i_last_length = 0;
503 p_stream->b_hasiframes = false;
504 p_stream->i_trex_length = 0;
505 p_stream->i_trex_size = 0;
507 p_stream->i_current_run = 0;
508 p_stream->read.p_first = NULL;
509 p_stream->read.p_last = NULL;
510 p_stream->towrite.p_first = NULL;
511 p_stream->towrite.p_last = NULL;
512 p_stream->p_held_entry = NULL;
513 p_stream->i_last_iframe_time = 0;
514 p_stream->i_written_duration = 0;
515 p_stream->p_indexentries = NULL;
516 p_stream->i_indexentriesmax = 0;
517 p_stream->i_indexentries = 0;
519 p_input->p_sys = p_stream;
521 msg_Dbg(p_mux, "adding input");
523 TAB_APPEND(p_sys->i_nb_streams, p_sys->pp_streams, p_stream);
527 /*****************************************************************************
529 *****************************************************************************/
530 static void DelStream(sout_mux_t *p_mux, sout_input_t *p_input)
533 msg_Dbg(p_mux, "removing input");
536 /*****************************************************************************
538 *****************************************************************************/
539 static int Mux(sout_mux_t *p_mux)
541 sout_mux_sys_t *p_sys = p_mux->p_sys;
544 int i_stream = sout_MuxGetStream(p_mux, 2, NULL);
548 sout_input_t *p_input = p_mux->pp_inputs[i_stream];
549 mp4_stream_t *p_stream = (mp4_stream_t*)p_input->p_sys;
553 p_data = block_FifoGet(p_input->p_fifo);
554 if (p_stream->fmt.i_codec == VLC_CODEC_H264 ||
555 p_stream->fmt.i_codec == VLC_CODEC_HEVC)
556 p_data = ConvertFromAnnexB(p_data);
557 else if (p_stream->fmt.i_codec == VLC_CODEC_SUBT)
558 p_data = ConvertSUBT(p_data);
559 else if (p_stream->fmt.i_codec == VLC_CODEC_A52 ||
560 p_stream->fmt.i_codec == VLC_CODEC_EAC3) {
561 if (p_stream->a52_frame == NULL && p_data->i_buffer >= 8)
562 p_stream->a52_frame = block_Duplicate(p_data);
566 /* Reset reference dts in case of discontinuity (ex: gather sout) */
567 if ( p_stream->i_entry_count == 0 || p_data->i_flags & BLOCK_FLAG_DISCONTINUITY )
569 p_stream->i_dts_start = p_data->i_dts;
570 p_stream->i_last_dts = p_data->i_dts;
571 p_stream->i_length_neg = 0;
574 if (p_stream->fmt.i_cat != SPU_ES) {
575 /* Fix length of the sample */
576 if (block_FifoCount(p_input->p_fifo) > 0) {
577 block_t *p_next = block_FifoShow(p_input->p_fifo);
578 if ( p_next->i_flags & BLOCK_FLAG_DISCONTINUITY )
579 { /* we have no way to know real length except by decoding */
580 if ( p_stream->fmt.i_cat == VIDEO_ES )
582 p_data->i_length = CLOCK_FREQ *
583 p_stream->fmt.video.i_frame_rate_base /
584 p_stream->fmt.video.i_frame_rate;
585 msg_Dbg( p_mux, "video track %u fixup to %"PRId64" for sample %u",
586 p_stream->i_track_id, p_data->i_length, p_stream->i_entry_count );
588 else if ( p_stream->fmt.i_cat == AUDIO_ES &&
589 p_stream->fmt.audio.i_rate &&
590 p_data->i_nb_samples )
592 p_data->i_length = CLOCK_FREQ * p_data->i_nb_samples /
593 p_stream->fmt.audio.i_rate;
594 msg_Dbg( p_mux, "audio track %u fixup to %"PRId64" for sample %u",
595 p_stream->i_track_id, p_data->i_length, p_stream->i_entry_count );
597 else if ( p_data->i_length <= 0 )
599 msg_Warn( p_mux, "unknown length for track %u sample %u",
600 p_stream->i_track_id, p_stream->i_entry_count );
601 p_data->i_length = 1;
606 int64_t i_diff = p_next->i_dts - p_data->i_dts;
607 if (i_diff < CLOCK_FREQ) /* protection */
608 p_data->i_length = i_diff;
611 if (p_data->i_length <= 0) {
612 msg_Warn(p_mux, "i_length <= 0");
613 p_stream->i_length_neg += p_data->i_length - 1;
614 p_data->i_length = 1;
615 } else if (p_stream->i_length_neg < 0) {
616 int64_t i_recover = __MIN(p_data->i_length / 4, - p_stream->i_length_neg);
618 p_data->i_length -= i_recover;
619 p_stream->i_length_neg += i_recover;
623 if (p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0) {
624 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
626 if (i_length <= 0) /* FIXME handle this broken case */
630 if (p_stream->entry[p_stream->i_entry_count-1].i_length <= 0)
631 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
634 /* add index entry */
635 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
636 e->i_pos = p_sys->i_pos;
637 e->i_size = p_data->i_buffer;
639 if ( p_data->i_dts > VLC_TS_INVALID && p_data->i_pts > p_data->i_dts )
641 e->i_pts_dts = p_data->i_pts - p_data->i_dts;
642 if ( !p_stream->b_hasbframes )
643 p_stream->b_hasbframes = true;
645 else e->i_pts_dts = 0;
647 e->i_length = p_data->i_length;
648 e->i_flags = p_data->i_flags;
650 p_stream->i_entry_count++;
651 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
652 if (p_stream->i_entry_count >= p_stream->i_entry_max - 1) {
653 p_stream->i_entry_max += 1000;
654 p_stream->entry = xrealloc(p_stream->entry,
655 p_stream->i_entry_max * sizeof(mp4_entry_t));
659 p_stream->i_read_duration += __MAX( 0, p_data->i_length );
660 p_stream->i_last_length = p_data->i_length;
661 p_sys->i_pos += p_data->i_buffer;
663 /* Save the DTS for SPU */
664 p_stream->i_last_dts = p_data->i_dts;
667 sout_AccessOutWrite(p_mux->p_access, p_data);
669 /* close subtitle with empty frame */
670 if (p_stream->fmt.i_cat == SPU_ES) {
671 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
673 if ( i_length != 0 && (p_data = block_Alloc(3)) ) {
675 msg_Dbg(p_mux, "writing an empty sub") ;
677 /* Append a idx entry */
678 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
679 e->i_pos = p_sys->i_pos;
685 /* XXX: No need to grow the entry here */
686 p_stream->i_entry_count++;
689 p_stream->i_last_dts += i_length;
692 p_data->i_dts = p_stream->i_last_dts;
693 p_data->i_dts = p_data->i_pts;
694 p_data->p_buffer[0] = 0;
695 p_data->p_buffer[1] = 1;
696 p_data->p_buffer[2] = ' ';
698 p_sys->i_pos += p_data->i_buffer;
700 sout_AccessOutWrite(p_mux->p_access, p_data);
703 /* Fix duration = current segment starttime + duration within */
704 p_stream->i_read_duration = p_stream->i_starttime + ( p_stream->i_last_dts - p_stream->i_dts_start );
708 /* Update the global segment/media duration */
709 for ( unsigned int i=0; i<p_sys->i_nb_streams; i++ )
711 if ( p_sys->pp_streams[i]->i_read_duration > p_sys->i_read_duration )
712 p_sys->i_read_duration = p_sys->pp_streams[i]->i_read_duration;
718 /*****************************************************************************
720 *****************************************************************************/
721 static block_t *ConvertSUBT(block_t *p_block)
723 p_block = block_Realloc(p_block, 2, p_block->i_buffer);
726 /* No trailling '\0' */
727 if (p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0')
730 p_block->p_buffer[0] = ((p_block->i_buffer - 2) >> 8)&0xff;
731 p_block->p_buffer[1] = ((p_block->i_buffer - 2) )&0xff;
736 static block_t *ConvertFromAnnexB(block_t *p_block)
738 uint8_t *last = p_block->p_buffer; /* Assume it starts with 0x00000001 */
739 uint8_t *dat = &p_block->p_buffer[4];
740 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
743 /* Replace the 4 bytes start code with 4 bytes size,
744 * FIXME are all startcodes 4 bytes ? (I don't think :(*/
746 while (dat < end - 4) {
747 if (!memcmp(dat, avc1_start_code, 4))
755 int i_size = dat - &last[4];
756 SetDWBE((uint32_t*) last, i_size);
758 /* Skip blocks with SPS/PPS */
759 //if ((last[4]&0x1f) == 7 || (last[4]&0x1f) == 8)
760 // ; // FIXME Find a way to skip dat without frelling everything
767 static bo_t *GetESDS(mp4_stream_t *p_stream)
770 int64_t i_bitrate_avg = 0;
771 int64_t i_bitrate_max = 0;
773 /* Compute avg/max bitrate */
774 for (unsigned i = 0; i < p_stream->i_entry_count; i++) {
775 i_bitrate_avg += p_stream->entry[i].i_size;
776 if (p_stream->entry[i].i_length > 0) {
777 int64_t i_bitrate = INT64_C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
778 if (i_bitrate > i_bitrate_max)
779 i_bitrate_max = i_bitrate;
783 if (p_stream->i_read_duration > 0)
784 i_bitrate_avg = INT64_C(8000000) * i_bitrate_avg / p_stream->i_read_duration;
787 if (i_bitrate_max <= 1)
788 i_bitrate_max = 0x7fffffff;
791 int i_decoder_specific_info_size = (p_stream->fmt.i_extra > 0) ? 5 + p_stream->fmt.i_extra : 0;
793 esds = box_full_new("esds", 0, 0);
796 bo_add_mp4_tag_descr(esds, 0x03, 3 + 5 + 13 + i_decoder_specific_info_size + 5 + 1);
797 bo_add_16be(esds, p_stream->i_track_id);
798 bo_add_8 (esds, 0x1f); // flags=0|streamPriority=0x1f
800 /* DecoderConfigDescr */
801 bo_add_mp4_tag_descr(esds, 0x04, 13 + i_decoder_specific_info_size);
803 int i_object_type_indication;
804 switch(p_stream->fmt.i_codec)
807 i_object_type_indication = 0x20;
810 /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
811 i_object_type_indication = 0x65;
814 /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
815 i_object_type_indication = 0x6b;
818 /* FIXME for mpeg2-aac == 0x66->0x68 */
819 i_object_type_indication = 0x40;
822 i_object_type_indication =
823 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
826 i_object_type_indication = 0x00;
829 int i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
831 bo_add_8 (esds, i_object_type_indication);
832 bo_add_8 (esds, (i_stream_type << 2) | 1);
833 bo_add_24be(esds, 1024 * 1024); // bufferSizeDB
834 bo_add_32be(esds, i_bitrate_max); // maxBitrate
835 bo_add_32be(esds, i_bitrate_avg); // avgBitrate
837 if (p_stream->fmt.i_extra > 0) {
838 /* DecoderSpecificInfo */
839 bo_add_mp4_tag_descr(esds, 0x05, p_stream->fmt.i_extra);
841 for (int i = 0; i < p_stream->fmt.i_extra; i++)
842 bo_add_8(esds, ((uint8_t*)p_stream->fmt.p_extra)[i]);
845 /* SL_Descr mandatory */
846 bo_add_mp4_tag_descr(esds, 0x06, 1);
847 bo_add_8 (esds, 0x02); // sl_predefined
852 static bo_t *GetWaveTag(mp4_stream_t *p_stream)
857 wave = box_new("wave");
859 box = box_new("frma");
860 bo_add_fourcc(box, "mp4a");
861 box_gather(wave, box);
863 box = box_new("mp4a");
865 box_gather(wave, box);
867 box = GetESDS(p_stream);
868 box_gather(wave, box);
870 box = box_new("srcq");
871 bo_add_32be(box, 0x40);
872 box_gather(wave, box);
875 bo_add_32be(wave, 8); /* new empty box */
876 bo_add_32be(wave, 0); /* box label */
881 static bo_t *GetDec3Tag(mp4_stream_t *p_stream)
883 if (!p_stream->a52_frame)
887 bs_init(&s, p_stream->a52_frame->p_buffer, sizeof(p_stream->a52_frame->i_buffer));
888 bs_skip(&s, 16); // syncword
890 uint8_t fscod, bsid, bsmod, acmod, lfeon, strmtyp;
894 strmtyp = bs_read(&s, 2);
896 if (strmtyp & 0x1) // dependant or reserved stream
899 if (bs_read(&s, 3) != 0x0) // substreamid: we don't support more than 1 stream
903 bs_skip(&s, 11); // frmsizecod
904 fscod = bs_read(&s, 2);
906 bs_skip(&s, 2); // fscod2
909 numblkscod = bs_read(&s, 2);
912 acmod = bs_read(&s, 3);
913 lfeon = bs_read1(&s);
915 bsid = bs_read(&s, 5);
917 bs_skip(&s, 5); // dialnorm
918 if (bs_read1(&s)) // compre
919 bs_skip(&s, 5); // compr
922 bs_skip(&s, 5); // dialnorm2
923 if (bs_read1(&s)) // compr2e
924 bs_skip(&s, 8); // compr2
927 if (strmtyp == 0x1) // dependant stream XXX: unsupported
928 if (bs_read1(&s)) // chanmape
929 bs_skip(&s, 16); // chanmap
931 /* we have to skip mixing info to read bsmod */
932 if (bs_read1(&s)) { // mixmdate
933 if (acmod > 0x2) // 2+ channels
934 bs_skip(&s, 2); // dmixmod
935 if ((acmod & 0x1) && (acmod > 0x2)) // 3 front channels
936 bs_skip(&s, 3 + 3); // ltrtcmixlev + lorocmixlev
937 if (acmod & 0x4) // surround channel
938 bs_skip(&s, 3 + 3); // ltrsurmixlev + lorosurmixlev
941 bs_skip(&s, 5); // lfemixlevcod
942 if (strmtyp == 0) { // independant stream
943 if (bs_read1(&s)) // pgmscle
944 bs_skip(&s, 6); // pgmscl
945 if (acmod == 0x0) // dual mono
946 if (bs_read1(&s)) // pgmscl2e
947 bs_skip(&s, 6); // pgmscl2
948 if (bs_read1(&s)) // extpgmscle
949 bs_skip(&s, 6); // extpgmscl
950 uint8_t mixdef = bs_read(&s, 2);
953 else if (mixdef == 0x2)
955 else if (mixdef == 0x3) {
956 uint8_t mixdeflen = bs_read(&s, 5);
957 bs_skip(&s, 8 * (mixdeflen + 2));
959 if (acmod < 0x2) { // mono or dual mono
960 if (bs_read1(&s)) // paninfoe
961 bs_skip(&s, 14); // paninfo
962 if (acmod == 0) // dual mono
963 if (bs_read1(&s)) // paninfo2e
964 bs_skip(&s, 14); // paninfo2
966 if (bs_read1(&s)) { // frmmixcfginfoe
967 static const int blocks[4] = { 1, 2, 3, 6 };
968 int number_of_blocks = blocks[numblkscod];
969 if (number_of_blocks == 1)
970 bs_skip(&s, 5); // blkmixcfginfo[0]
971 else for (int i = 0; i < number_of_blocks; i++)
972 if (bs_read1(&s)) // blkmixcfginfoe
973 bs_skip(&s, 5); // blkmixcfginfo[i]
978 if (bs_read1(&s)) // infomdate
979 bsmod = bs_read(&s, 3);
981 uint8_t mp4_eac3_header[5];
982 bs_init(&s, mp4_eac3_header, sizeof(mp4_eac3_header));
984 int data_rate = p_stream->fmt.i_bitrate / 1000;
985 bs_write(&s, 13, data_rate);
986 bs_write(&s, 3, 0); // num_ind_sub - 1
987 bs_write(&s, 2, fscod);
988 bs_write(&s, 5, bsid);
989 bs_write(&s, 5, bsmod);
990 bs_write(&s, 3, acmod);
991 bs_write(&s, 1, lfeon);
992 bs_write(&s, 3, 0); // reserved
993 bs_write(&s, 4, 0); // num_dep_sub
994 bs_write(&s, 1, 0); // reserved
996 bo_t *dec3 = box_new("dec3");
998 bo_add_mem(dec3, sizeof(mp4_eac3_header), mp4_eac3_header);
1003 static bo_t *GetDac3Tag(mp4_stream_t *p_stream)
1005 if (!p_stream->a52_frame)
1008 bo_t *dac3 = box_new("dac3");
1011 bs_init(&s, p_stream->a52_frame->p_buffer, sizeof(p_stream->a52_frame->i_buffer));
1013 uint8_t fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
1015 bs_skip(&s, 16 + 16); // syncword + crc
1017 fscod = bs_read(&s, 2);
1018 frmsizecod = bs_read(&s, 6);
1019 bsid = bs_read(&s, 5);
1020 bsmod = bs_read(&s, 3);
1021 acmod = bs_read(&s, 3);
1023 bs_skip(&s, 2); // dsurmod
1025 if ((acmod & 1) && acmod != 1)
1026 bs_skip(&s, 2); // cmixlev
1028 bs_skip(&s, 2); // surmixlev
1031 lfeon = bs_read1(&s);
1033 uint8_t mp4_a52_header[3];
1034 bs_init(&s, mp4_a52_header, sizeof(mp4_a52_header));
1036 bs_write(&s, 2, fscod);
1037 bs_write(&s, 5, bsid);
1038 bs_write(&s, 3, bsmod);
1039 bs_write(&s, 3, acmod);
1040 bs_write(&s, 1, lfeon);
1041 bs_write(&s, 5, frmsizecod >> 1); // bit_rate_code
1042 bs_write(&s, 5, 0); // reserved
1044 bo_add_mem(dac3, sizeof(mp4_a52_header), mp4_a52_header);
1049 static bo_t *GetDamrTag(mp4_stream_t *p_stream)
1053 damr = box_new("damr");
1055 bo_add_fourcc(damr, "REFC");
1058 if (p_stream->fmt.i_codec == VLC_CODEC_AMR_NB)
1059 bo_add_16be(damr, 0x81ff); /* Mode set (all modes for AMR_NB) */
1061 bo_add_16be(damr, 0x83ff); /* Mode set (all modes for AMR_WB) */
1062 bo_add_16be(damr, 0x1); /* Mode change period (no restriction) */
1067 static bo_t *GetD263Tag(void)
1071 d263 = box_new("d263");
1073 bo_add_fourcc(d263, "VLC ");
1074 bo_add_16be(d263, 0xa);
1080 static void hevcParseVPS(uint8_t * p_buffer, size_t i_buffer, uint8_t *general,
1081 uint8_t * numTemporalLayer, bool * temporalIdNested)
1083 const size_t i_decoded_nal_size = 512;
1084 uint8_t p_dec_nal[i_decoded_nal_size];
1085 size_t i_size = (i_buffer < i_decoded_nal_size)?i_buffer:i_decoded_nal_size;
1086 nal_decode(p_buffer, p_dec_nal, i_size);
1088 /* first two bytes are the NAL header, 3rd and 4th are:
1089 vps_video_parameter_set_id(4)
1090 vps_reserved_3_2bis(2)
1091 vps_max_layers_minus1(6)
1092 vps_max_sub_layers_minus1(3)
1093 vps_temporal_id_nesting_flags
1095 *numTemporalLayer = ((p_dec_nal[3] & 0x0E) >> 1) + 1;
1096 *temporalIdNested = (bool)(p_dec_nal[3] & 0x01);
1098 /* 5th & 6th are reserved 0xffff */
1099 /* copy the first 12 bytes of profile tier */
1100 memcpy(general, &p_dec_nal[6], 12);
1103 static void hevcParseSPS(uint8_t * p_buffer, size_t i_buffer, uint8_t * chroma_idc,
1104 uint8_t *bit_depth_luma_minus8, uint8_t *bit_depth_chroma_minus8)
1106 const size_t i_decoded_nal_size = 512;
1107 uint8_t p_dec_nal[i_decoded_nal_size];
1108 size_t i_size = (i_buffer < i_decoded_nal_size)?i_buffer-2:i_decoded_nal_size;
1109 nal_decode(p_buffer+2, p_dec_nal, i_size);
1111 bs_init(&bs, p_dec_nal, i_size);
1115 uint32_t sps_max_sublayer_minus1 = bs_read(&bs, 3);
1117 /* skip nesting flag */
1120 hevc_skip_profile_tiers_level(&bs, sps_max_sublayer_minus1);
1123 (void) bs_read_ue( &bs );
1125 *chroma_idc = bs_read_ue(&bs);
1126 if (*chroma_idc == 3)
1129 /* skip width and heigh */
1130 (void) bs_read_ue( &bs );
1131 (void) bs_read_ue( &bs );
1133 uint32_t conformance_window_flag = bs_read1(&bs);
1134 if (conformance_window_flag) {
1136 (void) bs_read_ue(&bs);
1137 (void) bs_read_ue(&bs);
1138 (void) bs_read_ue(&bs);
1139 (void) bs_read_ue(&bs);
1141 *bit_depth_luma_minus8 = bs_read_ue(&bs);
1142 *bit_depth_chroma_minus8 = bs_read_ue(&bs);
1145 static bo_t *GetHvcCTag(mp4_stream_t *p_stream)
1147 /* Generate hvcC box matching iso/iec 14496-15 3rd edition */
1148 bo_t *hvcC = box_new("hvcC");
1149 if(!p_stream->fmt.i_extra)
1157 /* According to the specification HEVC stream can have
1158 * 16 vps id and an "unlimited" number of sps and pps id using ue(v) id*/
1159 struct nal p_vps[16], *p_sps = NULL, *p_pps = NULL, *p_sei = NULL,
1161 size_t i_vps = 0, i_sps = 0, i_pps = 0, i_sei = 0;
1162 uint8_t i_num_arrays = 0;
1164 uint8_t * p_buffer = p_stream->fmt.p_extra;
1165 size_t i_buffer = p_stream->fmt.i_extra;
1167 uint8_t general_configuration[12] = {0};
1168 uint8_t i_numTemporalLayer = 0;
1169 uint8_t i_chroma_idc = 1;
1170 uint8_t i_bit_depth_luma_minus8 = 0;
1171 uint8_t i_bit_depth_chroma_minus8 = 0;
1172 bool b_temporalIdNested = false;
1174 uint32_t cmp = 0xFFFFFFFF;
1176 /* look for start code 0X0000001 */
1178 cmp = (cmp << 8) | *p_buffer;
1179 if((cmp ^ UINT32_C(0x100)) <= UINT32_C(0xFF))
1185 p_nal->i_buffer = p_buffer - p_nal->p_buffer - ((i_buffer)?3:0);
1187 switch (*p_buffer & 0x72) {
1190 p_nal = &p_vps[i_vps++];
1191 p_nal->p_buffer = p_buffer;
1192 /* Only keep the general profile from the first VPS
1193 * if there are several (this shouldn't happen so soon) */
1195 hevcParseVPS(p_buffer, i_buffer, general_configuration,
1196 &i_numTemporalLayer, &b_temporalIdNested);
1202 struct nal * p_tmp = realloc(p_sps, sizeof(struct nal) * (i_sps + 1));
1206 p_nal = &p_sps[i_sps++];
1207 p_nal->p_buffer = p_buffer;
1208 if (i_sps == 1 && i_buffer > 15) {
1209 /* Get Chroma_idc and bitdepths */
1210 hevcParseSPS(p_buffer, i_buffer, &i_chroma_idc,
1211 &i_bit_depth_luma_minus8, &i_bit_depth_chroma_minus8);
1218 struct nal * p_tmp = realloc(p_pps, sizeof(struct nal) * (i_pps + 1));
1222 p_nal = &p_pps[i_pps++];
1223 p_nal->p_buffer = p_buffer;
1231 struct nal * p_tmp = realloc(p_sei, sizeof(struct nal) * (i_sei + 1));
1235 p_nal = &p_sei[i_sei++];
1236 p_nal->p_buffer = p_buffer;
1246 bo_add_8(hvcC, 0x01);
1247 bo_add_mem(hvcC, 12, general_configuration);
1248 /* Don't set min spatial segmentation */
1249 bo_add_16be(hvcC, 0xF000);
1250 /* Don't set parallelism type since segmentation isn't set */
1251 bo_add_8(hvcC, 0xFC);
1252 bo_add_8(hvcC, (0xFC | (i_chroma_idc & 0x03)));
1253 bo_add_8(hvcC, (0xF8 | (i_bit_depth_luma_minus8 & 0x07)));
1254 bo_add_8(hvcC, (0xF8 | (i_bit_depth_chroma_minus8 & 0x07)));
1256 /* Don't set framerate */
1257 bo_add_16be(hvcC, 0x0000);
1258 /* Force NAL size of 4 bytes that replace the startcode */
1259 bo_add_8(hvcC, (((i_numTemporalLayer & 0x07) << 3) |
1260 (b_temporalIdNested << 2) | 0x03));
1261 bo_add_8(hvcC, i_num_arrays);
1265 /* Write VPS without forcing array_completeness */
1267 bo_add_16be(hvcC, i_vps);
1268 for (size_t i = 0; i < i_vps; i++) {
1270 bo_add_16be(hvcC, p_nal->i_buffer);
1271 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1276 /* Write SPS without forcing array_completeness */
1278 bo_add_16be(hvcC, i_sps);
1279 for (size_t i = 0; i < i_sps; i++) {
1281 bo_add_16be(hvcC, p_nal->i_buffer);
1282 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1287 /* Write PPS without forcing array_completeness */
1289 bo_add_16be(hvcC, i_pps);
1290 for (size_t i = 0; i < i_pps; i++) {
1292 bo_add_16be(hvcC, p_nal->i_buffer);
1293 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1298 /* Write SEI without forcing array_completeness */
1300 bo_add_16be(hvcC, i_sei);
1301 for (size_t i = 0; i < i_sei; i++) {
1303 bo_add_16be(hvcC, p_nal->i_buffer);
1304 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1310 static bo_t *GetAvcCTag(mp4_stream_t *p_stream)
1313 uint8_t *p_sps = NULL;
1314 uint8_t *p_pps = NULL;
1318 if (p_stream->fmt.i_extra > 0) {
1319 /* FIXME: take into account multiple sps/pps */
1320 uint8_t *p_buffer = p_stream->fmt.p_extra;
1321 int i_buffer = p_stream->fmt.i_extra;
1323 while (i_buffer > 3) {
1324 while (memcmp(p_buffer, &avc1_start_code[1], 3)) {
1328 const int i_nal_type = p_buffer[3]&0x1f;
1329 int i_startcode = 0;
1331 for (int i_offset = 1; i_offset+2 < i_buffer ; i_offset++)
1332 if (!memcmp(&p_buffer[i_offset], &avc1_start_code[1], 3)) {
1333 /* we found another startcode */
1334 i_startcode = i_offset;
1335 while (p_buffer[i_startcode-1] == 0 && i_startcode > 0)
1340 int i_size = i_startcode ? i_startcode : i_buffer;
1342 if (i_nal_type == 7) {
1343 p_sps = &p_buffer[3];
1344 i_sps_size = i_size - 3;
1346 if (i_nal_type == 8) {
1347 p_pps = &p_buffer[3];
1348 i_pps_size = i_size - 3;
1355 /* FIXME use better value */
1356 avcC = box_new("avcC");
1357 bo_add_8(avcC, 1); /* configuration version */
1358 bo_add_8(avcC, i_sps_size ? p_sps[1] : 77);
1359 bo_add_8(avcC, i_sps_size ? p_sps[2] : 64);
1360 bo_add_8(avcC, i_sps_size ? p_sps[3] : 30); /* level, 5.1 */
1361 bo_add_8(avcC, 0xff); /* 0b11111100 | lengthsize = 0x11 */
1363 bo_add_8(avcC, 0xe0 | (i_sps_size > 0 ? 1 : 0)); /* 0b11100000 | sps_count */
1364 if (i_sps_size > 0) {
1365 bo_add_16be(avcC, i_sps_size);
1366 bo_add_mem(avcC, i_sps_size, p_sps);
1369 bo_add_8(avcC, (i_pps_size > 0 ? 1 : 0)); /* pps_count */
1370 if (i_pps_size > 0) {
1371 bo_add_16be(avcC, i_pps_size);
1372 bo_add_mem(avcC, i_pps_size, p_pps);
1378 /* TODO: No idea about these values */
1379 static bo_t *GetSVQ3Tag(mp4_stream_t *p_stream)
1381 bo_t *smi = box_new("SMI ");
1383 if (p_stream->fmt.i_extra > 0x4e) {
1384 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
1385 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
1387 while (p + 8 < p_end) {
1388 int i_size = GetDWBE(p);
1389 if (i_size <= 1) /* FIXME handle 1 as long size */
1391 if (!strncmp((const char *)&p[4], "SMI ", 4)) {
1392 bo_add_mem(smi, p_end - p - 8, &p[8]);
1399 /* Create a dummy one in fallback */
1400 bo_add_fourcc(smi, "SEQH");
1401 bo_add_32be(smi, 0x5);
1402 bo_add_32be(smi, 0xe2c0211d);
1403 bo_add_8(smi, 0xc0);
1408 static bo_t *GetUdtaTag(sout_mux_t *p_mux)
1410 sout_mux_sys_t *p_sys = p_mux->p_sys;
1411 bo_t *udta = box_new("udta");
1414 for (unsigned int i_track = 0; i_track < p_sys->i_nb_streams; i_track++) {
1415 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
1416 vlc_fourcc_t codec = p_stream->fmt.i_codec;
1418 if (codec == VLC_CODEC_MP4V || codec == VLC_CODEC_MP4A) {
1419 bo_t *box = box_new("\251req");
1421 bo_add_16be(box, sizeof("QuickTime 6.0 or greater") - 1);
1422 bo_add_16be(box, 0);
1423 bo_add_mem(box, sizeof("QuickTime 6.0 or greater") - 1,
1424 (uint8_t *)"QuickTime 6.0 or greater");
1425 box_gather(udta, box);
1432 bo_t *box = box_new("\251enc");
1434 bo_add_16be(box, sizeof(PACKAGE_STRING " stream output") - 1);
1435 bo_add_16be(box, 0);
1436 bo_add_mem(box, sizeof(PACKAGE_STRING " stream output") - 1,
1437 (uint8_t*)PACKAGE_STRING " stream output");
1438 box_gather(udta, box);
1442 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
1444 #define ADD_META_BOX(type, box_string) { \
1446 if (vlc_meta_Get(p_meta, vlc_meta_##type)) \
1447 box = box_new("\251" box_string); \
1449 bo_add_16be(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type))); \
1450 bo_add_16be(box, 0); \
1451 bo_add_mem(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type)), \
1452 (uint8_t*)(vlc_meta_Get(p_meta, vlc_meta_##type))); \
1453 box_gather(udta, box); \
1456 ADD_META_BOX(Title, "nam");
1457 ADD_META_BOX(Artist, "ART");
1458 ADD_META_BOX(Genre, "gen");
1459 ADD_META_BOX(Copyright, "cpy");
1460 ADD_META_BOX(Description, "des");
1461 ADD_META_BOX(Date, "day");
1462 ADD_META_BOX(URL, "url");
1469 static bo_t *GetSounBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
1471 sout_mux_sys_t *p_sys = p_mux->p_sys;
1472 bool b_descr = true;
1473 vlc_fourcc_t codec = p_stream->fmt.i_codec;
1476 if (codec == VLC_CODEC_MPGA) {
1479 memcpy(fcc, ".mp3", 4);
1481 memcpy(fcc, "mp4a", 4);
1482 } else if (codec == VLC_CODEC_A52) {
1483 memcpy(fcc, "ac-3", 4);
1484 } else if (codec == VLC_CODEC_EAC3) {
1485 memcpy(fcc, "ec-3", 4);
1487 vlc_fourcc_to_char(codec, fcc);
1489 bo_t *soun = box_new(fcc);
1490 for (int i = 0; i < 6; i++)
1491 bo_add_8(soun, 0); // reserved;
1492 bo_add_16be(soun, 1); // data-reference-index
1494 /* SoundDescription */
1495 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
1496 bo_add_16be(soun, 1); // version 1;
1498 bo_add_16be(soun, 0); // version 0;
1499 bo_add_16be(soun, 0); // revision level (0)
1500 bo_add_32be(soun, 0); // vendor
1502 bo_add_16be(soun, p_stream->fmt.audio.i_channels);
1504 bo_add_16be(soun, p_stream->fmt.audio.i_bitspersample ?
1505 p_stream->fmt.audio.i_bitspersample : 16);
1506 bo_add_16be(soun, -2); // compression id
1507 bo_add_16be(soun, 0); // packet size (0)
1508 bo_add_16be(soun, p_stream->fmt.audio.i_rate); // sampleratehi
1509 bo_add_16be(soun, 0); // sampleratelo
1511 /* Extended data for SoundDescription V1 */
1512 if (p_sys->b_mov && p_stream->fmt.i_codec == VLC_CODEC_MP4A) {
1513 /* samples per packet */
1514 bo_add_32be(soun, p_stream->fmt.audio.i_frame_length);
1515 bo_add_32be(soun, 1536); /* bytes per packet */
1516 bo_add_32be(soun, 2); /* bytes per frame */
1517 /* bytes per sample */
1518 bo_add_32be(soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1521 /* Add an ES Descriptor */
1525 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
1526 box = GetWaveTag(p_stream);
1527 else if (codec == VLC_CODEC_AMR_NB)
1528 box = GetDamrTag(p_stream);
1529 else if (codec == VLC_CODEC_A52)
1530 box = GetDac3Tag(p_stream);
1531 else if (codec == VLC_CODEC_EAC3)
1532 box = GetDec3Tag(p_stream);
1534 box = GetESDS(p_stream);
1537 box_gather(soun, box);
1543 static bo_t *GetVideBox(mp4_stream_t *p_stream)
1547 switch(p_stream->fmt.i_codec)
1549 case VLC_CODEC_MP4V:
1550 case VLC_CODEC_MPGV: memcpy(fcc, "mp4v", 4); break;
1551 case VLC_CODEC_MJPG: memcpy(fcc, "mjpa", 4); break;
1552 case VLC_CODEC_SVQ1: memcpy(fcc, "SVQ1", 4); break;
1553 case VLC_CODEC_SVQ3: memcpy(fcc, "SVQ3", 4); break;
1554 case VLC_CODEC_H263: memcpy(fcc, "s263", 4); break;
1555 case VLC_CODEC_H264: memcpy(fcc, "avc1", 4); break;
1556 case VLC_CODEC_HEVC: memcpy(fcc, "hvc1", 4); break;
1557 case VLC_CODEC_YV12: memcpy(fcc, "yv12", 4); break;
1558 case VLC_CODEC_YUYV: memcpy(fcc, "yuy2", 4); break;
1560 vlc_fourcc_to_char(p_stream->fmt.i_codec, fcc);
1564 bo_t *vide = box_new(fcc);
1565 for (int i = 0; i < 6; i++)
1566 bo_add_8(vide, 0); // reserved;
1567 bo_add_16be(vide, 1); // data-reference-index
1569 bo_add_16be(vide, 0); // predefined;
1570 bo_add_16be(vide, 0); // reserved;
1571 for (int i = 0; i < 3; i++)
1572 bo_add_32be(vide, 0); // predefined;
1574 bo_add_16be(vide, p_stream->fmt.video.i_width); // i_width
1575 bo_add_16be(vide, p_stream->fmt.video.i_height); // i_height
1577 bo_add_32be(vide, 0x00480000); // h 72dpi
1578 bo_add_32be(vide, 0x00480000); // v 72dpi
1580 bo_add_32be(vide, 0); // data size, always 0
1581 bo_add_16be(vide, 1); // frames count per sample
1584 for (int i = 0; i < 32; i++)
1587 bo_add_16be(vide, 0x18); // depth
1588 bo_add_16be(vide, 0xffff); // predefined
1590 /* add an ES Descriptor */
1591 switch(p_stream->fmt.i_codec)
1593 case VLC_CODEC_MP4V:
1594 case VLC_CODEC_MPGV:
1595 box_gather(vide, GetESDS(p_stream));
1598 case VLC_CODEC_H263:
1599 box_gather(vide, GetD263Tag());
1602 case VLC_CODEC_SVQ3:
1603 box_gather(vide, GetSVQ3Tag(p_stream));
1606 case VLC_CODEC_H264:
1607 box_gather(vide, GetAvcCTag(p_stream));
1610 case VLC_CODEC_HEVC:
1611 box_gather(vide, GetHvcCTag(p_stream));
1618 static bo_t *GetTextBox(void)
1620 bo_t *text = box_new("text");
1622 for (int i = 0; i < 6; i++)
1623 bo_add_8(text, 0); // reserved;
1624 bo_add_16be(text, 1); // data-reference-index
1626 bo_add_32be(text, 0); // display flags
1627 bo_add_32be(text, 0); // justification
1628 for (int i = 0; i < 3; i++)
1629 bo_add_16be(text, 0); // back ground color
1631 bo_add_16be(text, 0); // box text
1632 bo_add_16be(text, 0); // box text
1633 bo_add_16be(text, 0); // box text
1634 bo_add_16be(text, 0); // box text
1636 bo_add_64be(text, 0); // reserved
1637 for (int i = 0; i < 3; i++)
1638 bo_add_16be(text, 0xff); // foreground color
1641 bo_add_mem(text, 9, (uint8_t*)"Helvetica");
1646 static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
1648 sout_mux_sys_t *p_sys = p_mux->p_sys;
1650 /* sample description */
1651 bo_t *stsd = box_full_new("stsd", 0, 0);
1652 bo_add_32be(stsd, 1);
1653 if (p_stream->fmt.i_cat == AUDIO_ES)
1654 box_gather(stsd, GetSounBox(p_mux, p_stream));
1655 else if (p_stream->fmt.i_cat == VIDEO_ES)
1656 box_gather(stsd, GetVideBox(p_stream));
1657 else if (p_stream->fmt.i_cat == SPU_ES)
1658 box_gather(stsd, GetTextBox());
1660 /* chunk offset table */
1662 if (p_sys->i_pos >= (((uint64_t)0x1) << 32)) {
1663 /* 64 bits version */
1664 p_stream->b_stco64 = true;
1665 stco = box_full_new("co64", 0, 0);
1667 /* 32 bits version */
1668 p_stream->b_stco64 = false;
1669 stco = box_full_new("stco", 0, 0);
1671 bo_add_32be(stco, 0); // entry-count (fixed latter)
1673 /* sample to chunk table */
1674 bo_t *stsc = box_full_new("stsc", 0, 0);
1675 bo_add_32be(stsc, 0); // entry-count (fixed latter)
1677 unsigned i_chunk = 0;
1678 unsigned i_stsc_last_val = 0, i_stsc_entries = 0;
1679 for (unsigned i = 0; i < p_stream->i_entry_count; i_chunk++) {
1680 mp4_entry_t *entry = p_stream->entry;
1683 if (p_stream->b_stco64)
1684 bo_add_64be(stco, entry[i].i_pos);
1686 bo_add_32be(stco, entry[i].i_pos);
1688 for (; i < p_stream->i_entry_count; i++)
1689 if (i >= p_stream->i_entry_count - 1 ||
1690 entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
1695 /* Add entry to the stsc table */
1696 if (i_stsc_last_val != i - i_first) {
1697 bo_add_32be(stsc, 1 + i_chunk); // first-chunk
1698 bo_add_32be(stsc, i - i_first) ; // samples-per-chunk
1699 bo_add_32be(stsc, 1); // sample-descr-index
1700 i_stsc_last_val = i - i_first;
1705 /* Fix stco entry count */
1706 bo_swap_32be(stco, 12, i_chunk);
1707 msg_Dbg(p_mux, "created %d chunks (stco)", i_chunk);
1709 /* Fix stsc entry count */
1710 bo_swap_32be(stsc, 12, i_stsc_entries );
1713 bo_t *stts = box_full_new("stts", 0, 0);
1714 bo_add_32be(stts, 0); // entry-count (fixed latter)
1716 unsigned i_index = 0;
1717 for (unsigned i = 0; i < p_stream->i_entry_count; i_index++) {
1719 mtime_t i_delta = p_stream->entry[i].i_length;
1721 for (; i < p_stream->i_entry_count; ++i)
1722 if (i == p_stream->i_entry_count || p_stream->entry[i].i_length != i_delta)
1725 bo_add_32be(stts, i - i_first); // sample-count
1726 bo_add_32be(stts, (uint64_t)i_delta * p_stream->i_timescale / CLOCK_FREQ); // sample-delta
1728 bo_swap_32be(stts, 12, i_index);
1730 /* composition time handling */
1732 if ( p_stream->b_hasbframes && (ctts = box_full_new("ctts", 0, 0)) )
1734 bo_add_32be(ctts, 0);
1736 for (unsigned i = 0; i < p_stream->i_entry_count; i_index++)
1739 mtime_t i_offset = p_stream->entry[i].i_pts_dts;
1741 for (; i < p_stream->i_entry_count; ++i)
1742 if (i == p_stream->i_entry_count || p_stream->entry[i].i_pts_dts != i_offset)
1745 bo_add_32be(ctts, i - i_first); // sample-count
1746 bo_add_32be(ctts, i_offset * p_stream->i_timescale / CLOCK_FREQ ); // sample-offset
1748 bo_swap_32be(ctts, 12, i_index);
1751 bo_t *stsz = box_full_new("stsz", 0, 0);
1753 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1756 i_size = p_stream->entry[i].i_size;
1757 else if ( p_stream->entry[i].i_size != i_size )
1763 bo_add_32be(stsz, i_size); // sample-size
1764 bo_add_32be(stsz, p_stream->i_entry_count); // sample-count
1765 if ( i_size == 0 ) // all samples have different size
1767 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1768 bo_add_32be(stsz, p_stream->entry[i].i_size); // sample-size
1771 /* create stss table */
1774 if ( p_stream->fmt.i_cat == VIDEO_ES || p_stream->fmt.i_cat == AUDIO_ES )
1776 mtime_t i_interval = -1;
1777 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1779 if ( i_interval != -1 )
1781 i_interval += p_stream->entry[i].i_length + p_stream->entry[i].i_pts_dts;
1782 if ( i_interval < CLOCK_FREQ * 2 )
1786 if (p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I) {
1788 stss = box_full_new("stss", 0, 0);
1789 bo_add_32be(stss, 0); /* fixed later */
1791 bo_add_32be(stss, 1 + i);
1799 bo_swap_32be(stss, 12, i_index);
1801 /* Now gather all boxes into stbl */
1802 bo_t *stbl = box_new("stbl");
1804 box_gather(stbl, stsd);
1805 box_gather(stbl, stts);
1807 box_gather(stbl, stss);
1809 box_gather(stbl, ctts);
1810 box_gather(stbl, stsc);
1811 box_gather(stbl, stsz);
1812 p_stream->i_stco_pos = stbl->len + 16;
1813 box_gather(stbl, stco);
1818 static int64_t get_timestamp(void);
1820 static void matrix_apply_rotation(es_format_t *fmt, uint32_t mvhd_matrix[9])
1822 enum video_orientation_t orientation = ORIENT_NORMAL;
1823 if (fmt->i_cat == VIDEO_ES)
1824 orientation = fmt->video.orientation;
1826 #define ATAN(a, b) do { mvhd_matrix[1] = (a) << 16; \
1827 mvhd_matrix[0] = (b) << 16; \
1830 switch (orientation) {
1831 case ORIENT_ROTATED_90: ATAN( 1, 0); break;
1832 case ORIENT_ROTATED_180: ATAN( 0, -1); break;
1833 case ORIENT_ROTATED_270: ATAN( -1, 0); break;
1834 default: ATAN( 0, 1); break;
1837 mvhd_matrix[3] = mvhd_matrix[0] ? 0 : 0x10000;
1838 mvhd_matrix[4] = mvhd_matrix[1] ? 0 : 0x10000;
1841 static bo_t *GetMoovBox(sout_mux_t *p_mux)
1843 sout_mux_sys_t *p_sys = p_mux->p_sys;
1847 uint32_t i_movie_timescale = 90000;
1848 int64_t i_movie_duration = 0;
1849 int64_t i_timestamp = get_timestamp();
1851 moov = box_new("moov");
1853 /* Create general info */
1854 if ( !p_sys->b_fragmented )
1856 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
1857 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1858 i_movie_duration = __MAX(i_movie_duration, p_stream->i_read_duration);
1860 msg_Dbg(p_mux, "movie duration %"PRId64"s", i_movie_duration / CLOCK_FREQ);
1862 i_movie_duration = i_movie_duration * i_movie_timescale / CLOCK_FREQ;
1865 i_movie_duration = 0;
1867 /* *** add /moov/mvhd *** */
1868 if (!p_sys->b_64_ext) {
1869 mvhd = box_full_new("mvhd", 0, 0);
1870 bo_add_32be(mvhd, i_timestamp); // creation time
1871 bo_add_32be(mvhd, i_timestamp); // modification time
1872 bo_add_32be(mvhd, i_movie_timescale); // timescale
1873 bo_add_32be(mvhd, i_movie_duration); // duration
1875 mvhd = box_full_new("mvhd", 1, 0);
1876 bo_add_64be(mvhd, i_timestamp); // creation time
1877 bo_add_64be(mvhd, i_timestamp); // modification time
1878 bo_add_32be(mvhd, i_movie_timescale); // timescale
1879 bo_add_64be(mvhd, i_movie_duration); // duration
1881 bo_add_32be(mvhd, 0x10000); // rate
1882 bo_add_16be(mvhd, 0x100); // volume
1883 bo_add_16be(mvhd, 0); // reserved
1884 for (int i = 0; i < 2; i++)
1885 bo_add_32be(mvhd, 0); // reserved
1887 uint32_t mvhd_matrix[9] = { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1889 for (int i = 0; i < 9; i++)
1890 bo_add_32be(mvhd, mvhd_matrix[i]);// matrix
1891 for (int i = 0; i < 6; i++)
1892 bo_add_32be(mvhd, 0); // pre-defined
1894 /* Next available track id */
1895 bo_add_32be(mvhd, p_sys->i_nb_streams + 1); // next-track-id
1897 box_gather(moov, mvhd);
1899 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
1900 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1902 mtime_t i_stream_duration;
1903 if ( !p_sys->b_fragmented )
1904 i_stream_duration = p_stream->i_read_duration * i_movie_timescale / CLOCK_FREQ;
1906 i_stream_duration = 0;
1908 /* *** add /moov/trak *** */
1909 bo_t *trak = box_new("trak");
1911 /* *** add /moov/trak/tkhd *** */
1913 if (!p_sys->b_64_ext) {
1915 tkhd = box_full_new("tkhd", 0, 0x0f);
1917 tkhd = box_full_new("tkhd", 0, 1);
1919 bo_add_32be(tkhd, i_timestamp); // creation time
1920 bo_add_32be(tkhd, i_timestamp); // modification time
1921 bo_add_32be(tkhd, p_stream->i_track_id);
1922 bo_add_32be(tkhd, 0); // reserved 0
1923 bo_add_32be(tkhd, i_stream_duration); // duration
1926 tkhd = box_full_new("tkhd", 1, 0x0f);
1928 tkhd = box_full_new("tkhd", 1, 1);
1930 bo_add_64be(tkhd, i_timestamp); // creation time
1931 bo_add_64be(tkhd, i_timestamp); // modification time
1932 bo_add_32be(tkhd, p_stream->i_track_id);
1933 bo_add_32be(tkhd, 0); // reserved 0
1934 bo_add_64be(tkhd, i_stream_duration); // duration
1937 for (int i = 0; i < 2; i++)
1938 bo_add_32be(tkhd, 0); // reserved
1939 bo_add_16be(tkhd, 0); // layer
1940 bo_add_16be(tkhd, 0); // pre-defined
1942 bo_add_16be(tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0);
1943 bo_add_16be(tkhd, 0); // reserved
1944 matrix_apply_rotation(&p_stream->fmt, mvhd_matrix);
1945 for (int i = 0; i < 9; i++)
1946 bo_add_32be(tkhd, mvhd_matrix[i]); // matrix
1947 if (p_stream->fmt.i_cat == AUDIO_ES) {
1948 bo_add_32be(tkhd, 0); // width (presentation)
1949 bo_add_32be(tkhd, 0); // height(presentation)
1950 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
1951 int i_width = p_stream->fmt.video.i_width << 16;
1952 if (p_stream->fmt.video.i_sar_num > 0 && p_stream->fmt.video.i_sar_den > 0) {
1953 i_width = (int64_t)p_stream->fmt.video.i_sar_num *
1954 ((int64_t)p_stream->fmt.video.i_width << 16) /
1955 p_stream->fmt.video.i_sar_den;
1957 // width (presentation)
1958 bo_add_32be(tkhd, i_width);
1959 // height(presentation)
1960 bo_add_32be(tkhd, p_stream->fmt.video.i_height << 16);
1962 int i_width = 320 << 16;
1964 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++) {
1965 mp4_stream_t *tk = p_sys->pp_streams[i];
1966 if (tk->fmt.i_cat == VIDEO_ES) {
1967 if (tk->fmt.video.i_sar_num > 0 &&
1968 tk->fmt.video.i_sar_den > 0)
1969 i_width = (int64_t)tk->fmt.video.i_sar_num *
1970 ((int64_t)tk->fmt.video.i_width << 16) /
1971 tk->fmt.video.i_sar_den;
1973 i_width = tk->fmt.video.i_width << 16;
1974 i_height = tk->fmt.video.i_height;
1978 bo_add_32be(tkhd, i_width); // width (presentation)
1979 bo_add_32be(tkhd, i_height << 16); // height(presentation)
1982 box_gather(trak, tkhd);
1984 /* *** add /moov/trak/edts and elst */
1985 if ( !p_sys->b_fragmented )
1987 bo_t *edts = box_new("edts");
1988 bo_t *elst = box_full_new("elst", p_sys->b_64_ext ? 1 : 0, 0);
1989 if (p_stream->i_starttime > 0) {
1990 bo_add_32be(elst, 2);
1992 if (p_sys->b_64_ext) {
1993 bo_add_64be(elst, p_stream->i_starttime *
1994 i_movie_timescale / CLOCK_FREQ);
1995 bo_add_64be(elst, -1);
1997 bo_add_32be(elst, p_stream->i_starttime *
1998 i_movie_timescale / CLOCK_FREQ);
1999 bo_add_32be(elst, -1);
2001 bo_add_16be(elst, 1);
2002 bo_add_16be(elst, 0);
2004 bo_add_32be(elst, 1);
2006 if (p_sys->b_64_ext) {
2007 bo_add_64be(elst, p_stream->i_read_duration *
2008 i_movie_timescale / CLOCK_FREQ);
2009 bo_add_64be(elst, 0);
2011 bo_add_32be(elst, p_stream->i_read_duration *
2012 i_movie_timescale / CLOCK_FREQ);
2013 bo_add_32be(elst, 0);
2015 bo_add_16be(elst, 1);
2016 bo_add_16be(elst, 0);
2018 box_gather(edts, elst);
2019 box_gather(trak, edts);
2022 /* *** add /moov/trak/mdia *** */
2023 bo_t *mdia = box_new("mdia");
2027 if (!p_sys->b_64_ext) {
2028 mdhd = box_full_new("mdhd", 0, 0);
2029 bo_add_32be(mdhd, i_timestamp); // creation time
2030 bo_add_32be(mdhd, i_timestamp); // modification time
2031 bo_add_32be(mdhd, p_stream->i_timescale); // timescale
2032 bo_add_32be(mdhd, i_stream_duration); // duration
2034 mdhd = box_full_new("mdhd", 1, 0);
2035 bo_add_64be(mdhd, i_timestamp); // creation time
2036 bo_add_64be(mdhd, i_timestamp); // modification time
2037 bo_add_32be(mdhd, p_stream->i_timescale); // timescale
2038 bo_add_64be(mdhd, i_stream_duration); // duration
2041 if (p_stream->fmt.psz_language) {
2042 char *psz = p_stream->fmt.psz_language;
2043 const iso639_lang_t *pl = NULL;
2044 uint16_t lang = 0x0;
2046 if (strlen(psz) == 2)
2047 pl = GetLang_1(psz);
2048 else if (strlen(psz) == 3) {
2049 pl = GetLang_2B(psz);
2050 if (!strcmp(pl->psz_iso639_1, "??"))
2051 pl = GetLang_2T(psz);
2054 if (pl && strcmp(pl->psz_iso639_1, "??"))
2055 lang = ((pl->psz_iso639_2T[0] - 0x60) << 10) |
2056 ((pl->psz_iso639_2T[1] - 0x60) << 5) |
2057 ((pl->psz_iso639_2T[2] - 0x60));
2058 bo_add_16be(mdhd, lang); // language
2060 bo_add_16be(mdhd, 0 ); // language
2061 bo_add_16be(mdhd, 0 ); // predefined
2062 box_gather(mdia, mdhd);
2064 /* handler reference */
2065 bo_t *hdlr = box_full_new("hdlr", 0, 0);
2068 bo_add_fourcc(hdlr, "mhlr"); // media handler
2070 bo_add_32be(hdlr, 0);
2072 if (p_stream->fmt.i_cat == AUDIO_ES)
2073 bo_add_fourcc(hdlr, "soun");
2074 else if (p_stream->fmt.i_cat == VIDEO_ES)
2075 bo_add_fourcc(hdlr, "vide");
2076 else if (p_stream->fmt.i_cat == SPU_ES)
2077 bo_add_fourcc(hdlr, "text");
2079 bo_add_32be(hdlr, 0); // reserved
2080 bo_add_32be(hdlr, 0); // reserved
2081 bo_add_32be(hdlr, 0); // reserved
2084 bo_add_8(hdlr, 12); /* Pascal string for .mov */
2086 if (p_stream->fmt.i_cat == AUDIO_ES)
2087 bo_add_mem(hdlr, 12, (uint8_t*)"SoundHandler");
2088 else if (p_stream->fmt.i_cat == VIDEO_ES)
2089 bo_add_mem(hdlr, 12, (uint8_t*)"VideoHandler");
2091 bo_add_mem(hdlr, 12, (uint8_t*)"Text Handler");
2094 bo_add_8(hdlr, 0); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
2096 box_gather(mdia, hdlr);
2099 bo_t *minf = box_new("minf");
2102 if (p_stream->fmt.i_cat == AUDIO_ES) {
2105 smhd = box_full_new("smhd", 0, 0);
2106 bo_add_16be(smhd, 0); // balance
2107 bo_add_16be(smhd, 0); // reserved
2109 box_gather(minf, smhd);
2110 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
2113 vmhd = box_full_new("vmhd", 0, 1);
2114 bo_add_16be(vmhd, 0); // graphicsmode
2115 for (int i = 0; i < 3; i++)
2116 bo_add_16be(vmhd, 0); // opcolor
2118 box_gather(minf, vmhd);
2119 } else if (p_stream->fmt.i_cat == SPU_ES) {
2120 bo_t *gmhd = box_new("gmhd");
2121 bo_t *gmin = box_full_new("gmin", 0, 1);
2123 bo_add_16be(gmin, 0); // graphicsmode
2124 for (int i = 0; i < 3; i++)
2125 bo_add_16be(gmin, 0); // opcolor
2126 bo_add_16be(gmin, 0); // balance
2127 bo_add_16be(gmin, 0); // reserved
2129 box_gather(gmhd, gmin);
2131 box_gather(minf, gmhd);
2135 bo_t *dinf = box_new("dinf");
2136 bo_t *dref = box_full_new("dref", 0, 0);
2137 bo_add_32be(dref, 1);
2138 bo_t *url = box_full_new("url ", 0, 0x01);
2139 box_gather(dref, url);
2140 box_gather(dinf, dref);
2142 /* append dinf to mdia */
2143 box_gather(minf, dinf);
2147 if ( p_sys->b_fragmented )
2149 uint32_t i_backup = p_stream->i_entry_count;
2150 p_stream->i_entry_count = 0;
2151 stbl = GetStblBox(p_mux, p_stream);
2152 p_stream->i_entry_count = i_backup;
2155 stbl = GetStblBox(p_mux, p_stream);
2157 /* append stbl to minf */
2158 p_stream->i_stco_pos += minf->len;
2159 box_gather(minf, stbl);
2161 /* append minf to mdia */
2162 p_stream->i_stco_pos += mdia->len;
2163 box_gather(mdia, minf);
2165 /* append mdia to trak */
2166 p_stream->i_stco_pos += trak->len;
2167 box_gather(trak, mdia);
2169 /* append trak to moov */
2170 p_stream->i_stco_pos += moov->len;
2171 box_gather(moov, trak);
2174 /* Add user data tags */
2175 box_gather(moov, GetUdtaTag(p_mux));
2177 if ( p_sys->b_fragmented )
2179 bo_t *mvex = box_new("mvex");
2180 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++)
2182 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
2184 /* Try to find some defaults */
2185 if ( p_stream->i_entry_count )
2187 // FIXME: find highest occurence
2188 p_stream->i_trex_length = p_stream->entry[0].i_length;
2189 p_stream->i_trex_size = p_stream->entry[0].i_size;
2192 /* *** add /mvex/trex *** */
2193 bo_t *trex = box_full_new("trex", 0, 0);
2194 bo_add_32be(trex, p_stream->i_track_id);
2195 bo_add_32be(trex, 1); // sample desc index
2196 bo_add_32be(trex, (uint64_t)p_stream->i_trex_length * p_stream->i_timescale / CLOCK_FREQ); // sample duration
2197 bo_add_32be(trex, p_stream->i_trex_size); // sample size
2198 bo_add_32be(trex, 0); // sample flags
2199 box_gather(mvex, trex);
2201 box_gather(moov, mvex);
2208 /****************************************************************************/
2210 static bo_t *box_new(const char *fcc)
2212 bo_t *box = malloc(sizeof(*box));
2218 bo_add_32be (box, 0);
2219 bo_add_fourcc(box, fcc);
2224 static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f)
2226 bo_t *box = box_new(fcc);
2231 bo_add_24be (box, f);
2236 static void box_free(bo_t *box)
2238 block_Release(box->b);
2242 static void box_fix(bo_t *box)
2244 box->b->p_buffer[0] = box->len >> 24;
2245 box->b->p_buffer[1] = box->len >> 16;
2246 box->b->p_buffer[2] = box->len >> 8;
2247 box->b->p_buffer[3] = box->len;
2250 static void box_gather (bo_t *box, bo_t *box2)
2253 box->b = block_Realloc(box->b, 0, box->len + box2->len);
2254 memcpy(&box->b->p_buffer[box->len], box2->b->p_buffer, box2->len);
2255 box->len += box2->len;
2259 static void box_send(sout_mux_t *p_mux, bo_t *box)
2261 box->b->i_buffer = box->len;
2262 sout_AccessOutWrite(p_mux->p_access, box->b);
2266 static int64_t get_timestamp(void)
2268 int64_t i_timestamp = time(NULL);
2270 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2271 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60
2276 /***************************************************************************
2278 ****************************************************************************/
2279 #define FRAGMENT_LENGTH (CLOCK_FREQ * 3/2)
2281 #define ENQUEUE_ENTRY(object, entry) \
2284 object.p_last->p_next = entry;\
2285 object.p_last = entry;\
2286 if (!object.p_first)\
2287 object.p_first = entry;\
2290 #define DEQUEUE_ENTRY(object, entry) \
2292 entry = object.p_first;\
2293 if (object.p_last == entry)\
2294 object.p_last = NULL;\
2295 object.p_first = object.p_first->p_next;\
2296 entry->p_next = NULL;\
2299 /* Creates mfra/traf index entries */
2300 static void AddKeyframeEntry(mp4_stream_t *p_stream, const uint64_t i_moof_pos,
2301 const uint8_t i_traf, const uint32_t i_sample,
2302 const mtime_t i_time)
2304 /* alloc or realloc */
2305 mp4_fragindex_t *p_entries = p_stream->p_indexentries;
2306 if (p_stream->i_indexentries >= p_stream->i_indexentriesmax)
2308 p_stream->i_indexentriesmax += 256;
2309 p_entries = xrealloc(p_stream->p_indexentries,
2310 p_stream->i_indexentriesmax * sizeof(mp4_fragindex_t));
2311 if (p_entries) /* realloc can fail */
2312 p_stream->p_indexentries = p_entries;
2315 mtime_t i_last_entry_time;
2316 if (p_stream->i_indexentries)
2317 i_last_entry_time = p_stream->p_indexentries[p_stream->i_indexentries - 1].i_time;
2319 i_last_entry_time = 0;
2321 if (p_entries && i_time - i_last_entry_time >= CLOCK_FREQ * 2)
2323 mp4_fragindex_t *p_indexentry = &p_stream->p_indexentries[p_stream->i_indexentries];
2324 p_indexentry->i_time = i_time;
2325 p_indexentry->i_moofoffset = i_moof_pos;
2326 p_indexentry->i_sample = i_sample;
2327 p_indexentry->i_traf = i_traf;
2328 p_indexentry->i_trun = 1;
2329 p_stream->i_indexentries++;
2333 /* Creates moof box and traf/trun information.
2334 * Single run per traf is absolutely not optimal as interleaving should be done
2335 * using runs and not limiting moof size, but creating an relative offset only
2336 * requires base_offset_is_moof and then comply to late iso brand spec which
2337 * breaks clients. */
2338 static bo_t *GetMoofBox(sout_mux_t *p_mux, size_t *pi_mdat_total_size,
2339 mtime_t i_barrier_time, const uint64_t i_write_pos)
2341 sout_mux_sys_t *p_sys = p_mux->p_sys;
2344 size_t i_fixupoffset = 0;
2346 *pi_mdat_total_size = 0;
2348 moof = box_new("moof");
2350 /* *** add /moof/mfhd *** */
2352 mfhd = box_full_new("mfhd", 0, 0);
2353 bo_add_32be(mfhd, p_sys->i_mfhd_sequence++); // sequence number
2355 box_gather(moof, mfhd);
2357 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++)
2359 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
2361 /* *** add /moof/traf *** */
2362 bo_t *traf = box_new("traf");
2364 uint32_t i_sample = 0;
2365 mtime_t i_time = p_stream->i_written_duration;
2366 bool b_allsamesize = true;
2367 bool b_allsamelength = true;
2368 if ( p_stream->read.p_first )
2370 mp4_fragentry_t *p_entry = p_stream->read.p_first->p_next;
2371 while (p_entry && (b_allsamelength || b_allsamesize))
2373 /* compare against queue head */
2374 b_allsamelength &= ( p_entry->p_block->i_length == p_stream->read.p_first->p_block->i_length );
2375 b_allsamesize &= ( p_entry->p_block->i_buffer == p_stream->read.p_first->p_block->i_buffer );
2376 p_entry = p_entry->p_next;
2380 uint32_t i_tfhd_flags = 0x0;
2381 if (p_stream->read.p_first)
2383 /* Current segment have all same duration value, different than trex's default */
2384 if (b_allsamelength &&
2385 p_stream->read.p_first->p_block->i_length != p_stream->i_trex_length &&
2386 p_stream->read.p_first->p_block->i_length)
2387 i_tfhd_flags |= MP4_TFHD_DFLT_SAMPLE_DURATION;
2389 /* Current segment have all same size value, different than trex's default */
2390 if (b_allsamesize &&
2391 p_stream->read.p_first->p_block->i_buffer != p_stream->i_trex_size &&
2392 p_stream->read.p_first->p_block->i_buffer)
2393 i_tfhd_flags |= MP4_TFHD_DFLT_SAMPLE_SIZE;
2397 /* We have no samples */
2398 i_tfhd_flags |= MP4_TFHD_DURATION_IS_EMPTY;
2401 /* *** add /moof/traf/tfhd *** */
2402 bo_t *tfhd = box_full_new("tfhd", 0, i_tfhd_flags);
2403 bo_add_32be(tfhd, p_stream->i_track_id);
2405 /* set the local sample duration default */
2406 if (i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_DURATION)
2407 bo_add_32be(tfhd, p_stream->read.p_first->p_block->i_length * p_stream->i_timescale / CLOCK_FREQ);
2409 /* set the local sample size default */
2410 if (i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_SIZE)
2411 bo_add_32be(tfhd, p_stream->read.p_first->p_block->i_buffer);
2413 box_gather(traf, tfhd);
2415 /* *** add /moof/traf/tfdt *** */
2416 bo_t *tfdt = box_full_new("tfdt", 1, 0);
2417 bo_add_64be(tfdt, p_stream->i_written_duration * p_stream->i_timescale / CLOCK_FREQ );
2418 box_gather(traf, tfdt);
2420 /* *** add /moof/traf/trun *** */
2421 if (p_stream->read.p_first)
2423 uint32_t i_trun_flags = 0x0;
2425 if (p_stream->b_hasiframes && !(p_stream->read.p_first->p_block->i_flags & BLOCK_FLAG_TYPE_I))
2426 i_trun_flags |= MP4_TRUN_FIRST_FLAGS;
2428 if (!b_allsamelength ||
2429 ( !(i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_DURATION) && p_stream->i_trex_length == 0 ))
2430 i_trun_flags |= MP4_TRUN_SAMPLE_DURATION;
2432 if (!b_allsamesize ||
2433 ( !(i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_SIZE) && p_stream->i_trex_size == 0 ))
2434 i_trun_flags |= MP4_TRUN_SAMPLE_SIZE;
2436 if (p_stream->b_hasbframes)
2437 i_trun_flags |= MP4_TRUN_SAMPLE_TIME_OFFSET;
2439 if (i_fixupoffset == 0)
2440 i_trun_flags |= MP4_TRUN_DATA_OFFSET;
2442 bo_t *trun = box_full_new("trun", 0, i_trun_flags);
2445 uint32_t i_entry_count = 0;
2446 mtime_t i_run_time = p_stream->i_written_duration;
2447 mp4_fragentry_t *p_entry = p_stream->read.p_first;
2450 if ( i_barrier_time && i_run_time + p_entry->p_block->i_length > i_barrier_time )
2453 i_run_time += p_entry->p_block->i_length;
2454 p_entry = p_entry->p_next;
2456 bo_add_32be(trun, i_entry_count); // sample count
2458 if (i_trun_flags & MP4_TRUN_DATA_OFFSET)
2460 i_fixupoffset = moof->len + traf->len + trun->len;
2461 bo_add_32be(trun, 0xdeadbeef); // data offset
2464 if (i_trun_flags & MP4_TRUN_FIRST_FLAGS)
2465 bo_add_32be(trun, 1<<16); // flag as non keyframe
2467 while(p_stream->read.p_first && i_entry_count)
2469 DEQUEUE_ENTRY(p_stream->read, p_entry);
2471 if (i_trun_flags & MP4_TRUN_SAMPLE_DURATION)
2472 bo_add_32be(trun, p_entry->p_block->i_length * p_stream->i_timescale / CLOCK_FREQ); // sample duration
2474 if (i_trun_flags & MP4_TRUN_SAMPLE_SIZE)
2475 bo_add_32be(trun, p_entry->p_block->i_buffer); // sample size
2477 if (i_trun_flags & MP4_TRUN_SAMPLE_TIME_OFFSET)
2479 uint32_t i_diff = 0;
2480 if ( p_entry->p_block->i_dts > VLC_TS_INVALID &&
2481 p_entry->p_block->i_pts > p_entry->p_block->i_dts )
2483 i_diff = p_entry->p_block->i_pts - p_entry->p_block->i_dts;
2485 bo_add_32be(trun, i_diff * p_stream->i_timescale / CLOCK_FREQ); // ctts
2488 *pi_mdat_total_size += p_entry->p_block->i_buffer;
2490 ENQUEUE_ENTRY(p_stream->towrite, p_entry);
2494 /* Add keyframe entry if needed */
2495 if (p_stream->b_hasiframes && (p_entry->p_block->i_flags & BLOCK_FLAG_TYPE_I) &&
2496 (p_stream->fmt.i_cat == VIDEO_ES || p_stream->fmt.i_cat == AUDIO_ES))
2498 AddKeyframeEntry(p_stream, i_write_pos, i_trak, i_sample, i_time);
2501 i_time += p_entry->p_block->i_length;
2504 box_gather(traf, trun);
2507 box_gather(moof, traf);
2512 /* do tfhd base data offset fixup */
2515 /* mdat will follow moof */
2516 SetDWBE(moof->b->p_buffer + i_fixupoffset, moof->len + 8);
2519 /* set iframe flag, so the streaming server always starts from moof */
2520 moof->b->i_flags |= BLOCK_FLAG_TYPE_I;
2525 static void WriteFragmentMDAT(sout_mux_t *p_mux, size_t i_total_size)
2527 sout_mux_sys_t *p_sys = p_mux->p_sys;
2529 /* Now add mdat header */
2530 bo_t *mdat = box_new("mdat");
2531 /* force update of real size */
2532 mdat->b->i_buffer = mdat->len;
2533 assert(mdat->len==8);
2534 mdat->len += i_total_size;
2536 p_sys->i_pos += mdat->b->i_buffer;
2537 /* only write header */
2538 sout_AccessOutWrite(p_mux->p_access, mdat->b);
2540 /* Header and its size are written and good, now write content */
2541 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++)
2543 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
2545 while(p_stream->towrite.p_first)
2547 mp4_fragentry_t *p_entry = p_stream->towrite.p_first;
2548 p_sys->i_pos += p_entry->p_block->i_buffer;
2549 p_stream->i_written_duration += p_entry->p_block->i_length;
2551 p_entry->p_block->i_flags &= ~BLOCK_FLAG_TYPE_I; // clear flag for http stream
2552 sout_AccessOutWrite(p_mux->p_access, p_entry->p_block);
2554 p_stream->towrite.p_first = p_entry->p_next;
2556 if (!p_stream->towrite.p_first)
2557 p_stream->towrite.p_last = NULL;
2562 static bo_t *GetMfraBox(sout_mux_t *p_mux)
2564 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2566 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2568 mp4_stream_t *p_stream = p_sys->pp_streams[i];
2569 if (p_stream->i_indexentries)
2571 bo_t *tfra = box_full_new("tfra", 0, 0x0);
2572 if (!tfra) continue;
2573 bo_add_32be(tfra, p_stream->i_track_id);
2574 bo_add_32be(tfra, 0x3); // reserved + lengths (1,1,4)=>(0,0,3)
2575 bo_add_32be(tfra, p_stream->i_indexentries);
2576 for(uint32_t i_index=0; i_index<p_stream->i_indexentries; i_index++)
2578 const mp4_fragindex_t *p_indexentry = &p_stream->p_indexentries[i_index];
2579 bo_add_32be(tfra, p_indexentry->i_time);
2580 bo_add_32be(tfra, p_indexentry->i_moofoffset);
2581 assert(sizeof(p_indexentry->i_traf)==1); /* guard against sys changes */
2582 assert(sizeof(p_indexentry->i_trun)==1);
2583 assert(sizeof(p_indexentry->i_sample)==4);
2584 bo_add_8(tfra, p_indexentry->i_traf);
2585 bo_add_8(tfra, p_indexentry->i_trun);
2586 bo_add_32be(tfra, p_indexentry->i_sample);
2589 if (!mfra && !(mfra = box_new("mfra")))
2595 box_gather(mfra,tfra);
2601 static void FlushHeader(sout_mux_t *p_mux)
2603 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2605 /* Now add ftyp header */
2606 bo_t *ftyp = box_new("ftyp");
2607 bo_add_fourcc(ftyp, "isom");
2608 bo_add_32be (ftyp, 0); // minor version
2611 bo_t *moov = GetMoovBox(p_mux);
2613 /* merge into a single block */
2614 box_gather(ftyp, moov);
2616 /* add header flag for streaming server */
2617 ftyp->b->i_flags |= BLOCK_FLAG_HEADER;
2618 p_sys->i_pos += ftyp->len;
2619 box_send(p_mux, ftyp);
2620 p_sys->b_header_sent = true;
2623 static int OpenFrag(vlc_object_t *p_this)
2625 sout_mux_t *p_mux = (sout_mux_t*) p_this;
2626 sout_mux_sys_t *p_sys = malloc(sizeof(sout_mux_sys_t));
2630 p_mux->p_sys = (sout_mux_sys_t *) p_sys;
2631 p_mux->pf_control = Control;
2632 p_mux->pf_addstream = AddStream;
2633 p_mux->pf_delstream = DelStream;
2634 p_mux->pf_mux = MuxFrag;
2637 p_sys->b_mov = false;
2638 p_sys->b_3gp = false;
2639 p_sys->b_64_ext = false;
2643 p_sys->i_nb_streams = 0;
2644 p_sys->pp_streams = NULL;
2645 p_sys->i_mdat_pos = 0;
2646 p_sys->i_read_duration = 0;
2647 p_sys->i_written_duration= 0;
2649 p_sys->b_header_sent = false;
2650 p_sys->b_fragmented = true;
2651 p_sys->i_mfhd_sequence = 1;
2656 static void WriteFragments(sout_mux_t *p_mux, bool b_flush)
2658 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2660 mtime_t i_barrier_time = p_sys->i_written_duration + FRAGMENT_LENGTH;
2661 size_t i_mdat_size = 0;
2662 bool b_has_samples = false;
2664 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2666 const mp4_stream_t *p_stream = p_sys->pp_streams[i];
2667 if (p_stream->read.p_first)
2669 b_has_samples = true;
2671 /* set a barrier so we try to align to keyframe */
2672 if (p_stream->b_hasiframes &&
2673 p_stream->i_last_iframe_time > p_stream->i_written_duration &&
2674 (p_stream->fmt.i_cat == VIDEO_ES ||
2675 p_stream->fmt.i_cat == AUDIO_ES) )
2677 i_barrier_time = __MIN(i_barrier_time, p_stream->i_last_iframe_time);
2682 if (!p_sys->b_header_sent)
2686 moof = GetMoofBox(p_mux, &i_mdat_size, (b_flush)?0:i_barrier_time, p_sys->i_pos);
2688 if (moof && i_mdat_size == 0)
2690 block_Release(moof->b);
2696 msg_Dbg(p_mux, "writing moof @ %"PRId64, p_sys->i_pos);
2697 p_sys->i_pos += moof->len;
2698 assert(moof->b->i_flags & BLOCK_FLAG_TYPE_I); /* http sout */
2699 box_send(p_mux, moof);
2700 msg_Dbg(p_mux, "writing mdat @ %"PRId64, p_sys->i_pos);
2701 WriteFragmentMDAT(p_mux, i_mdat_size);
2703 /* update iframe point */
2704 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2706 mp4_stream_t *p_stream = p_sys->pp_streams[i];
2707 p_stream->i_last_iframe_time = 0;
2712 /* Do an entry length fixup using only its own info.
2713 * This is the end boundary case. */
2714 static void LengthLocalFixup(sout_mux_t *p_mux, const mp4_stream_t *p_stream, block_t *p_entrydata)
2716 if ( p_stream->fmt.i_cat == VIDEO_ES )
2718 p_entrydata->i_length = CLOCK_FREQ *
2719 p_stream->fmt.video.i_frame_rate_base /
2720 p_stream->fmt.video.i_frame_rate;
2721 msg_Dbg(p_mux, "video track %d fixup to %"PRId64" for sample %u",
2722 p_stream->i_track_id, p_entrydata->i_length, p_stream->i_entry_count - 1);
2724 else if (p_stream->fmt.i_cat == AUDIO_ES &&
2725 p_stream->fmt.audio.i_rate &&
2726 p_entrydata->i_nb_samples)
2728 p_entrydata->i_length = CLOCK_FREQ * p_entrydata->i_nb_samples /
2729 p_stream->fmt.audio.i_rate;
2730 msg_Dbg(p_mux, "audio track %d fixup to %"PRId64" for sample %u",
2731 p_stream->i_track_id, p_entrydata->i_length, p_stream->i_entry_count - 1);
2735 msg_Warn(p_mux, "unknown length for track %d sample %u",
2736 p_stream->i_track_id, p_stream->i_entry_count - 1);
2737 p_entrydata->i_length = 1;
2741 static void CleanupFrag(sout_mux_sys_t *p_sys)
2743 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2745 mp4_stream_t *p_stream = p_sys->pp_streams[i];
2746 if (p_stream->p_held_entry)
2748 block_Release(p_stream->p_held_entry->p_block);
2749 free(p_stream->p_held_entry);
2751 while(p_stream->read.p_first)
2753 mp4_fragentry_t *p_next = p_stream->read.p_first->p_next;
2754 block_Release(p_stream->read.p_first->p_block);
2755 free(p_stream->read.p_first);
2756 p_stream->read.p_first = p_next;
2758 while(p_stream->towrite.p_first)
2760 mp4_fragentry_t *p_next = p_stream->towrite.p_first->p_next;
2761 block_Release(p_stream->towrite.p_first->p_block);
2762 free(p_stream->towrite.p_first);
2763 p_stream->towrite.p_first = p_next;
2765 free(p_stream->p_indexentries);
2770 static void CloseFrag(vlc_object_t *p_this)
2772 sout_mux_t *p_mux = (sout_mux_t *) p_this;
2773 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2775 /* Flush remaining entries */
2776 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2778 mp4_stream_t *p_stream = p_sys->pp_streams[i];
2779 if (p_stream->p_held_entry)
2781 if (p_stream->p_held_entry->p_block->i_length < 1)
2782 LengthLocalFixup(p_mux, p_stream, p_stream->p_held_entry->p_block);
2783 ENQUEUE_ENTRY(p_stream->read, p_stream->p_held_entry);
2784 p_stream->p_held_entry = NULL;
2788 /* and force creating a fragment from it */
2789 WriteFragments(p_mux, true);
2791 /* Write indexes, but only for non streamed content
2792 as they refer to moof by absolute position */
2793 if (!strcmp(p_mux->psz_mux, "mp4frag"))
2795 bo_t *mfra = GetMfraBox(p_mux);
2798 bo_t *mfro = box_full_new("mfro", 0, 0x0);
2802 bo_add_32be(mfro, mfra->len + MP4_MFRO_BOXSIZE);
2803 box_gather(mfra, mfro);
2805 box_send(p_mux, mfra);
2812 static int MuxFrag(sout_mux_t *p_mux)
2814 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2816 int i_stream = sout_MuxGetStream(p_mux, 1, NULL);
2819 sout_input_t *p_input = p_mux->pp_inputs[i_stream];
2820 mp4_stream_t *p_stream = (mp4_stream_t*) p_input->p_sys;
2821 block_t *p_currentblock = block_FifoGet(p_input->p_fifo);
2823 /* do block conversion */
2824 switch(p_stream->fmt.i_codec)
2826 case VLC_CODEC_H264:
2827 case VLC_CODEC_HEVC:
2828 p_currentblock = ConvertFromAnnexB(p_currentblock);
2830 case VLC_CODEC_SUBT:
2831 p_currentblock = ConvertSUBT(p_currentblock);
2837 if( !p_currentblock )
2840 /* If we have a previous entry for outgoing queue */
2841 if (p_stream->p_held_entry)
2843 block_t *p_heldblock = p_stream->p_held_entry->p_block;
2845 /* Fix previous block length from current */
2846 if (p_heldblock->i_length < 1)
2849 /* Fix using dts if not on a boundary */
2850 if ((p_currentblock->i_flags & BLOCK_FLAG_DISCONTINUITY) == 0)
2851 p_heldblock->i_length = p_currentblock->i_dts - p_heldblock->i_dts;
2853 if (p_heldblock->i_length < 1)
2854 LengthLocalFixup(p_mux, p_stream, p_heldblock);
2858 ENQUEUE_ENTRY(p_stream->read, p_stream->p_held_entry);
2859 p_stream->p_held_entry = NULL;
2861 if (p_stream->b_hasiframes && (p_heldblock->i_flags & BLOCK_FLAG_TYPE_I) &&
2862 p_stream->i_read_duration - p_sys->i_written_duration < FRAGMENT_LENGTH)
2864 /* Flag the last iframe time, we'll use it as boundary so it will start
2866 p_stream->i_last_iframe_time = p_stream->i_read_duration;
2869 /* update buffered time */
2870 p_stream->i_read_duration += __MAX(0, p_heldblock->i_length);
2874 /* set temp entry */
2875 p_stream->p_held_entry = malloc(sizeof(mp4_fragentry_t));
2876 if (unlikely(!p_stream->p_held_entry))
2879 p_stream->p_held_entry->p_block = p_currentblock;
2880 p_stream->p_held_entry->i_run = p_stream->i_current_run;
2881 p_stream->p_held_entry->p_next = NULL;
2883 if (p_stream->fmt.i_cat == VIDEO_ES )
2885 if (!p_stream->b_hasiframes && (p_currentblock->i_flags & BLOCK_FLAG_TYPE_I))
2886 p_stream->b_hasiframes = true;
2888 if (!p_stream->b_hasbframes && p_currentblock->i_dts > VLC_TS_INVALID &&
2889 p_currentblock->i_pts > p_currentblock->i_dts)
2890 p_stream->b_hasbframes = true;
2893 /* Update the global fragment/media duration */
2894 mtime_t i_min_read_duration = p_stream->i_read_duration;
2895 mtime_t i_min_written_duration = p_stream->i_written_duration;
2896 for (unsigned int i=0; i<p_sys->i_nb_streams; i++)
2898 const mp4_stream_t *p_s = p_sys->pp_streams[i];
2899 if (p_s->fmt.i_cat != VIDEO_ES && p_s->fmt.i_cat != AUDIO_ES)
2901 if (p_s->i_read_duration < i_min_read_duration)
2902 i_min_read_duration = p_s->i_read_duration;
2904 if (p_s->i_written_duration < i_min_written_duration)
2905 i_min_written_duration = p_s->i_written_duration;
2907 p_sys->i_read_duration = i_min_read_duration;
2908 p_sys->i_written_duration = i_min_written_duration;
2910 /* we have prerolled enough to know all streams, and have enough date to create a fragment */
2911 if (p_stream->read.p_first && p_sys->i_read_duration - p_sys->i_written_duration >= FRAGMENT_LENGTH)
2912 WriteFragments(p_mux, false);