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>
40 #include <vlc_iso_lang.h>
43 #include "../demux/mpeg/mpeg_parser_helpers.h"
44 #include "../demux/mp4/libmp4.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 #define FASTSTART_TEXT N_("Create \"Fast Start\" files")
51 #define FASTSTART_LONGTEXT N_(\
52 "Create \"Fast Start\" files. " \
53 "\"Fast Start\" files are optimized for downloads and allow the user " \
54 "to start previewing the file while it is downloading.")
56 static int Open (vlc_object_t *);
57 static void Close (vlc_object_t *);
58 static int OpenFrag (vlc_object_t *);
59 static void CloseFrag (vlc_object_t *);
61 #define SOUT_CFG_PREFIX "sout-mp4-"
64 set_description(N_("MP4/MOV muxer"))
65 set_category(CAT_SOUT)
66 set_subcategory(SUBCAT_SOUT_MUX)
69 add_bool(SOUT_CFG_PREFIX "faststart", true,
70 FASTSTART_TEXT, FASTSTART_LONGTEXT,
72 set_capability("sout mux", 5)
73 add_shortcut("mp4", "mov", "3gp")
74 set_callbacks(Open, Close)
77 set_description(N_("Fragmented and streamable MP4 muxer"))
78 set_category(CAT_SOUT)
79 set_subcategory(SUBCAT_SOUT_MUX)
80 set_shortname("MP4 Frag")
81 add_shortcut("mp4frag", "mp4stream")
82 set_capability("sout mux", 0)
83 set_callbacks(OpenFrag, CloseFrag)
87 /*****************************************************************************
89 *****************************************************************************/
90 static const char *const ppsz_sout_options[] = {
94 static int Control(sout_mux_t *, int, va_list);
95 static int AddStream(sout_mux_t *, sout_input_t *);
96 static int DelStream(sout_mux_t *, sout_input_t *);
97 static int Mux (sout_mux_t *);
98 static int MuxFrag (sout_mux_t *);
100 /*****************************************************************************
102 *****************************************************************************/
110 unsigned int i_flags;
113 typedef struct mp4_fragentry_t mp4_fragentry_t;
115 struct mp4_fragentry_t
119 mp4_fragentry_t *p_next;
122 typedef struct mp4_fragindex_t
124 uint64_t i_moofoffset;
131 typedef struct mp4_fragqueue_t
133 mp4_fragentry_t *p_first;
134 mp4_fragentry_t *p_last;
140 unsigned int i_track_id;
143 unsigned int i_entry_count;
144 unsigned int i_entry_max;
146 int64_t i_length_neg;
149 int64_t i_dts_start; /* applies to current segment only */
150 int64_t i_read_duration;
151 uint32_t i_timescale;
152 mtime_t i_starttime; /* the really first packet */
155 /* for later stco fix-up (fast start files) */
160 int64_t i_last_dts; /* applies to current segment only */
161 int64_t i_last_length;
165 uint32_t i_trex_length;
166 uint32_t i_trex_size;
167 uint32_t i_tfhd_flags;
169 uint32_t i_current_run;
170 mp4_fragentry_t *p_held_entry;
171 mp4_fragqueue_t read;
172 mp4_fragqueue_t towrite;
173 mtime_t i_last_iframe_time;
174 mtime_t i_written_duration;
175 mp4_fragindex_t *p_indexentries;
176 uint32_t i_indexentriesmax;
177 uint32_t i_indexentries;
180 struct sout_mux_sys_t
189 mtime_t i_read_duration;
191 unsigned int i_nb_streams;
192 mp4_stream_t **pp_streams;
197 mtime_t i_written_duration;
198 uint32_t i_mfhd_sequence;
207 static void bo_init (bo_t *);
208 static void bo_add_8 (bo_t *, uint8_t);
209 static void bo_add_16be (bo_t *, uint16_t);
210 static void bo_add_24be (bo_t *, uint32_t);
211 static void bo_add_32be (bo_t *, uint32_t);
212 static void bo_add_64be (bo_t *, uint64_t);
213 static void bo_add_fourcc(bo_t *, const char *);
214 static void bo_add_mem (bo_t *, int , uint8_t *);
215 static void bo_add_descr(bo_t *, uint8_t , uint32_t);
217 static void bo_fix_32be (bo_t *, int , uint32_t);
219 static bo_t *box_new (const char *fcc);
220 static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f);
221 static void box_fix (bo_t *box);
222 static void box_gather (bo_t *box, bo_t *box2);
224 static void box_send(sout_mux_t *p_mux, bo_t *box);
226 static bo_t *GetMoovBox(sout_mux_t *p_mux);
228 static block_t *ConvertSUBT(block_t *);
229 static block_t *ConvertFromAnnexB(block_t *);
231 static const char avc1_start_code[4] = { 0, 0, 0, 1 };
233 /*****************************************************************************
235 *****************************************************************************/
236 static int Open(vlc_object_t *p_this)
238 sout_mux_t *p_mux = (sout_mux_t*)p_this;
239 sout_mux_sys_t *p_sys;
242 msg_Dbg(p_mux, "Mp4 muxer opened");
243 config_ChainParse(p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg);
245 p_mux->pf_control = Control;
246 p_mux->pf_addstream = AddStream;
247 p_mux->pf_delstream = DelStream;
249 p_mux->p_sys = p_sys = malloc(sizeof(sout_mux_sys_t));
253 p_sys->i_nb_streams = 0;
254 p_sys->pp_streams = NULL;
255 p_sys->i_mdat_pos = 0;
256 p_sys->b_mov = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "mov");
257 p_sys->b_3gp = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "3gp");
258 p_sys->i_read_duration = 0;
259 p_sys->b_fragmented = false;
262 /* Now add ftyp header */
263 box = box_new("ftyp");
265 bo_add_fourcc(box, "3gp6");
267 bo_add_fourcc(box, "isom");
268 bo_add_32be (box, 0);
270 bo_add_fourcc(box, "3gp4");
272 bo_add_fourcc(box, "mp41");
273 bo_add_fourcc(box, "avc1");
274 bo_add_fourcc(box, "qt ");
277 p_sys->i_pos += box->len;
278 p_sys->i_mdat_pos = p_sys->i_pos;
280 box_send(p_mux, box);
284 * Quicktime actually doesn't like the 64 bits extensions !!! */
285 p_sys->b_64_ext = false;
287 /* Now add mdat header */
288 box = box_new("mdat");
289 bo_add_64be (box, 0); // enough to store an extended size
291 p_sys->i_pos += box->len;
293 box_send(p_mux, box);
298 /*****************************************************************************
300 *****************************************************************************/
301 static void Close(vlc_object_t *p_this)
303 sout_mux_t *p_mux = (sout_mux_t*)p_this;
304 sout_mux_sys_t *p_sys = p_mux->p_sys;
306 msg_Dbg(p_mux, "Close");
308 /* Update mdat size */
311 if (p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32)) {
313 bo_add_32be (&bo, 1);
314 bo_add_fourcc(&bo, "mdat");
315 bo_add_64be (&bo, p_sys->i_pos - p_sys->i_mdat_pos);
317 bo_add_32be (&bo, 8);
318 bo_add_fourcc(&bo, "wide");
319 bo_add_32be (&bo, p_sys->i_pos - p_sys->i_mdat_pos - 8);
320 bo_add_fourcc(&bo, "mdat");
323 bo.b->i_buffer = bo.len;
324 sout_AccessOutSeek(p_mux->p_access, p_sys->i_mdat_pos);
325 sout_AccessOutWrite(p_mux->p_access, bo.b);
327 /* Create MOOV header */
328 uint64_t i_moov_pos = p_sys->i_pos;
329 bo_t *moov = GetMoovBox(p_mux);
331 /* Check we need to create "fast start" files */
332 p_sys->b_fast_start = var_GetBool(p_this, SOUT_CFG_PREFIX "faststart");
333 while (p_sys->b_fast_start) {
334 /* Move data to the end of the file so we can fit the moov header
336 int64_t i_size = p_sys->i_pos - p_sys->i_mdat_pos;
337 int i_moov_size = moov->len;
340 int64_t i_chunk = __MIN(32768, i_size);
341 block_t *p_buf = block_Alloc(i_chunk);
342 sout_AccessOutSeek(p_mux->p_access,
343 p_sys->i_mdat_pos + i_size - i_chunk);
344 if (sout_AccessOutRead(p_mux->p_access, p_buf) < i_chunk) {
345 msg_Warn(p_this, "read() not supported by access output, "
346 "won't create a fast start file");
347 p_sys->b_fast_start = false;
348 block_Release(p_buf);
351 sout_AccessOutSeek(p_mux->p_access, p_sys->i_mdat_pos + i_size +
352 i_moov_size - i_chunk);
353 sout_AccessOutWrite(p_mux->p_access, p_buf);
357 if (!p_sys->b_fast_start)
360 /* Fix-up samples to chunks table in MOOV header */
361 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
362 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
364 moov->len = p_stream->i_stco_pos;
365 for (unsigned i = 0; i < p_stream->i_entry_count; ) {
366 mp4_entry_t *entry = p_stream->entry;
367 if (p_stream->b_stco64)
368 bo_add_64be(moov, entry[i].i_pos + i_moov_size);
370 bo_add_32be(moov, entry[i].i_pos + i_moov_size);
372 for (; i < p_stream->i_entry_count; i++)
373 if (i >= p_stream->i_entry_count - 1 ||
374 entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
381 moov->len = i_moov_size;
382 i_moov_pos = p_sys->i_mdat_pos;
383 p_sys->b_fast_start = false;
386 /* Write MOOV header */
387 sout_AccessOutSeek(p_mux->p_access, i_moov_pos);
388 box_send(p_mux, moov);
391 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
392 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
394 es_format_Clean(&p_stream->fmt);
395 free(p_stream->entry);
398 if (p_sys->i_nb_streams)
399 free(p_sys->pp_streams);
403 /*****************************************************************************
405 *****************************************************************************/
406 static int Control(sout_mux_t *p_mux, int i_query, va_list args)
413 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
414 pb_bool = (bool*)va_arg(args, bool *);
418 case MUX_GET_ADD_STREAM_WAIT:
419 pb_bool = (bool*)va_arg(args, bool *);
423 case MUX_GET_MIME: /* Not needed, as not streamable */
429 /*****************************************************************************
431 *****************************************************************************/
432 static int AddStream(sout_mux_t *p_mux, sout_input_t *p_input)
434 sout_mux_sys_t *p_sys = p_mux->p_sys;
435 mp4_stream_t *p_stream;
437 switch(p_input->p_fmt->i_codec)
446 case VLC_CODEC_MJPGB:
452 case VLC_CODEC_AMR_NB:
453 case VLC_CODEC_AMR_WB:
458 msg_Warn(p_mux, "subtitle track added like in .mov (even when creating .mp4)");
461 msg_Err(p_mux, "unsupported codec %4.4s in mp4",
462 (char*)&p_input->p_fmt->i_codec);
466 p_stream = malloc(sizeof(mp4_stream_t));
469 es_format_Copy(&p_stream->fmt, p_input->p_fmt);
470 p_stream->i_track_id = p_sys->i_nb_streams + 1;
471 p_stream->i_length_neg = 0;
472 p_stream->i_entry_count = 0;
473 p_stream->i_entry_max = 1000;
475 calloc(p_stream->i_entry_max, sizeof(mp4_entry_t));
476 p_stream->i_dts_start = 0;
477 p_stream->i_read_duration = 0;
478 switch( p_stream->fmt.i_cat )
481 p_stream->i_timescale = p_stream->fmt.audio.i_rate;
484 p_stream->i_timescale = p_stream->fmt.video.i_frame_rate * 1000 /
485 p_stream->fmt.video.i_frame_rate_base;
488 p_stream->i_timescale = CLOCK_FREQ;
492 p_stream->i_starttime = p_sys->i_read_duration;
493 p_stream->b_hasbframes = false;
495 p_stream->i_last_dts = 0;
496 p_stream->i_last_length = 0;
498 p_stream->b_hasiframes = false;
499 p_stream->i_trex_length = 0;
500 p_stream->i_trex_size = 0;
502 p_stream->i_current_run = 0;
503 p_stream->read.p_first = NULL;
504 p_stream->read.p_last = NULL;
505 p_stream->towrite.p_first = NULL;
506 p_stream->towrite.p_last = NULL;
507 p_stream->p_held_entry = NULL;
508 p_stream->i_last_iframe_time = 0;
509 p_stream->i_written_duration = 0;
510 p_stream->p_indexentries = NULL;
511 p_stream->i_indexentriesmax = 0;
512 p_stream->i_indexentries = 0;
514 p_input->p_sys = p_stream;
516 msg_Dbg(p_mux, "adding input");
518 TAB_APPEND(p_sys->i_nb_streams, p_sys->pp_streams, p_stream);
522 /*****************************************************************************
524 *****************************************************************************/
525 static int DelStream(sout_mux_t *p_mux, sout_input_t *p_input)
528 msg_Dbg(p_mux, "removing input");
532 /*****************************************************************************
534 *****************************************************************************/
535 static int Mux(sout_mux_t *p_mux)
537 sout_mux_sys_t *p_sys = p_mux->p_sys;
540 int i_stream = sout_MuxGetStream(p_mux, 2, NULL);
544 sout_input_t *p_input = p_mux->pp_inputs[i_stream];
545 mp4_stream_t *p_stream = (mp4_stream_t*)p_input->p_sys;
549 p_data = block_FifoGet(p_input->p_fifo);
550 if (p_stream->fmt.i_codec == VLC_CODEC_H264 ||
551 p_stream->fmt.i_codec == VLC_CODEC_HEVC)
552 p_data = ConvertFromAnnexB(p_data);
553 else if (p_stream->fmt.i_codec == VLC_CODEC_SUBT)
554 p_data = ConvertSUBT(p_data);
557 /* Reset reference dts in case of discontinuity (ex: gather sout) */
558 if ( p_stream->i_entry_count == 0 || p_data->i_flags & BLOCK_FLAG_DISCONTINUITY )
560 p_stream->i_dts_start = p_data->i_dts;
561 p_stream->i_last_dts = p_data->i_dts;
562 p_stream->i_length_neg = 0;
565 if (p_stream->fmt.i_cat != SPU_ES) {
566 /* Fix length of the sample */
567 if (block_FifoCount(p_input->p_fifo) > 0) {
568 block_t *p_next = block_FifoShow(p_input->p_fifo);
569 if ( p_next->i_flags & BLOCK_FLAG_DISCONTINUITY )
570 { /* we have no way to know real length except by decoding */
571 if ( p_stream->fmt.i_cat == VIDEO_ES )
573 p_data->i_length = CLOCK_FREQ *
574 p_stream->fmt.video.i_frame_rate_base /
575 p_stream->fmt.video.i_frame_rate;
576 msg_Dbg( p_mux, "video track %u fixup to %"PRId64" for sample %u",
577 p_stream->i_track_id, p_data->i_length, p_stream->i_entry_count );
579 else if ( p_stream->fmt.i_cat == AUDIO_ES &&
580 p_stream->fmt.audio.i_rate &&
581 p_data->i_nb_samples )
583 p_data->i_length = CLOCK_FREQ * p_data->i_nb_samples /
584 p_stream->fmt.audio.i_rate;
585 msg_Dbg( p_mux, "audio 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_data->i_length <= 0 )
590 msg_Warn( p_mux, "unknown length for track %u sample %u",
591 p_stream->i_track_id, p_stream->i_entry_count );
592 p_data->i_length = 1;
597 int64_t i_diff = p_next->i_dts - p_data->i_dts;
598 if (i_diff < CLOCK_FREQ) /* protection */
599 p_data->i_length = i_diff;
602 if (p_data->i_length <= 0) {
603 msg_Warn(p_mux, "i_length <= 0");
604 p_stream->i_length_neg += p_data->i_length - 1;
605 p_data->i_length = 1;
606 } else if (p_stream->i_length_neg < 0) {
607 int64_t i_recover = __MIN(p_data->i_length / 4, - p_stream->i_length_neg);
609 p_data->i_length -= i_recover;
610 p_stream->i_length_neg += i_recover;
614 if (p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0) {
615 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
617 if (i_length <= 0) /* FIXME handle this broken case */
621 if (p_stream->entry[p_stream->i_entry_count-1].i_length <= 0)
622 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
625 /* add index entry */
626 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
627 e->i_pos = p_sys->i_pos;
628 e->i_size = p_data->i_buffer;
630 if ( p_data->i_dts > VLC_TS_INVALID && p_data->i_pts > p_data->i_dts )
632 e->i_pts_dts = p_data->i_pts - p_data->i_dts;
633 if ( !p_stream->b_hasbframes )
634 p_stream->b_hasbframes = true;
636 else e->i_pts_dts = 0;
638 e->i_length = p_data->i_length;
639 e->i_flags = p_data->i_flags;
641 p_stream->i_entry_count++;
642 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
643 if (p_stream->i_entry_count >= p_stream->i_entry_max - 1) {
644 p_stream->i_entry_max += 1000;
645 p_stream->entry = xrealloc(p_stream->entry,
646 p_stream->i_entry_max * sizeof(mp4_entry_t));
650 p_stream->i_read_duration += __MAX( 0, p_data->i_length );
651 p_stream->i_last_length = p_data->i_length;
652 p_sys->i_pos += p_data->i_buffer;
654 /* Save the DTS for SPU */
655 p_stream->i_last_dts = p_data->i_dts;
658 sout_AccessOutWrite(p_mux->p_access, p_data);
660 /* close subtitle with empty frame */
661 if (p_stream->fmt.i_cat == SPU_ES) {
662 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
664 if ( i_length != 0 && (p_data = block_Alloc(3)) ) {
666 msg_Dbg(p_mux, "writing an empty sub") ;
668 /* Append a idx entry */
669 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
670 e->i_pos = p_sys->i_pos;
676 /* XXX: No need to grow the entry here */
677 p_stream->i_entry_count++;
680 p_stream->i_last_dts += i_length;
683 p_data->i_dts = p_stream->i_last_dts;
684 p_data->i_dts = p_data->i_pts;
685 p_data->p_buffer[0] = 0;
686 p_data->p_buffer[1] = 1;
687 p_data->p_buffer[2] = ' ';
689 p_sys->i_pos += p_data->i_buffer;
691 sout_AccessOutWrite(p_mux->p_access, p_data);
694 /* Fix duration = current segment starttime + duration within */
695 p_stream->i_read_duration = p_stream->i_starttime + ( p_stream->i_last_dts - p_stream->i_dts_start );
699 /* Update the global segment/media duration */
700 for ( unsigned int i=0; i<p_sys->i_nb_streams; i++ )
702 if ( p_sys->pp_streams[i]->i_read_duration > p_sys->i_read_duration )
703 p_sys->i_read_duration = p_sys->pp_streams[i]->i_read_duration;
709 /*****************************************************************************
711 *****************************************************************************/
712 static block_t *ConvertSUBT(block_t *p_block)
714 p_block = block_Realloc(p_block, 2, p_block->i_buffer);
716 /* No trailling '\0' */
717 if (p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0')
720 p_block->p_buffer[0] = ((p_block->i_buffer - 2) >> 8)&0xff;
721 p_block->p_buffer[1] = ((p_block->i_buffer - 2) )&0xff;
726 static block_t *ConvertFromAnnexB(block_t *p_block)
728 uint8_t *last = p_block->p_buffer; /* Assume it starts with 0x00000001 */
729 uint8_t *dat = &p_block->p_buffer[4];
730 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
733 /* Replace the 4 bytes start code with 4 bytes size,
734 * FIXME are all startcodes 4 bytes ? (I don't think :(*/
736 while (dat < end - 4) {
737 if (!memcmp(dat, avc1_start_code, 4))
745 int i_size = dat - &last[4];
746 last[0] = (i_size >> 24)&0xff;
747 last[1] = (i_size >> 16)&0xff;
748 last[2] = (i_size >> 8)&0xff;
749 last[3] = (i_size )&0xff;
751 /* Skip blocks with SPS/PPS */
752 //if ((last[4]&0x1f) == 7 || (last[4]&0x1f) == 8)
753 // ; // FIXME Find a way to skip dat without frelling everything
760 static bo_t *GetESDS(mp4_stream_t *p_stream)
763 int64_t i_bitrate_avg = 0;
764 int64_t i_bitrate_max = 0;
766 /* Compute avg/max bitrate */
767 for (unsigned i = 0; i < p_stream->i_entry_count; i++) {
768 i_bitrate_avg += p_stream->entry[i].i_size;
769 if (p_stream->entry[i].i_length > 0) {
770 int64_t i_bitrate = INT64_C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
771 if (i_bitrate > i_bitrate_max)
772 i_bitrate_max = i_bitrate;
776 if (p_stream->i_read_duration > 0)
777 i_bitrate_avg = INT64_C(8000000) * i_bitrate_avg / p_stream->i_read_duration;
780 if (i_bitrate_max <= 1)
781 i_bitrate_max = 0x7fffffff;
784 int i_decoder_specific_info_size = (p_stream->fmt.i_extra > 0) ? 5 + p_stream->fmt.i_extra : 0;
786 esds = box_full_new("esds", 0, 0);
789 bo_add_descr(esds, 0x03, 3 + 5 + 13 + i_decoder_specific_info_size + 5 + 1);
790 bo_add_16be(esds, p_stream->i_track_id);
791 bo_add_8 (esds, 0x1f); // flags=0|streamPriority=0x1f
793 /* DecoderConfigDescr */
794 bo_add_descr(esds, 0x04, 13 + i_decoder_specific_info_size);
796 int i_object_type_indication;
797 switch(p_stream->fmt.i_codec)
800 i_object_type_indication = 0x20;
803 /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
804 i_object_type_indication = 0x65;
807 /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
808 i_object_type_indication = 0x6b;
811 /* FIXME for mpeg2-aac == 0x66->0x68 */
812 i_object_type_indication = 0x40;
815 i_object_type_indication =
816 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
819 i_object_type_indication = 0x00;
822 int i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
824 bo_add_8 (esds, i_object_type_indication);
825 bo_add_8 (esds, (i_stream_type << 2) | 1);
826 bo_add_24be(esds, 1024 * 1024); // bufferSizeDB
827 bo_add_32be(esds, i_bitrate_max); // maxBitrate
828 bo_add_32be(esds, i_bitrate_avg); // avgBitrate
830 if (p_stream->fmt.i_extra > 0) {
831 /* DecoderSpecificInfo */
832 bo_add_descr(esds, 0x05, p_stream->fmt.i_extra);
834 for (int i = 0; i < p_stream->fmt.i_extra; i++)
835 bo_add_8(esds, ((uint8_t*)p_stream->fmt.p_extra)[i]);
838 /* SL_Descr mandatory */
839 bo_add_descr(esds, 0x06, 1);
840 bo_add_8 (esds, 0x02); // sl_predefined
845 static bo_t *GetWaveTag(mp4_stream_t *p_stream)
850 wave = box_new("wave");
852 box = box_new("frma");
853 bo_add_fourcc(box, "mp4a");
854 box_gather(wave, box);
856 box = box_new("mp4a");
858 box_gather(wave, box);
860 box = GetESDS(p_stream);
861 box_gather(wave, box);
863 box = box_new("srcq");
864 bo_add_32be(box, 0x40);
865 box_gather(wave, box);
868 bo_add_32be(wave, 8); /* new empty box */
869 bo_add_32be(wave, 0); /* box label */
874 static bo_t *GetDamrTag(mp4_stream_t *p_stream)
878 damr = box_new("damr");
880 bo_add_fourcc(damr, "REFC");
883 if (p_stream->fmt.i_codec == VLC_CODEC_AMR_NB)
884 bo_add_16be(damr, 0x81ff); /* Mode set (all modes for AMR_NB) */
886 bo_add_16be(damr, 0x83ff); /* Mode set (all modes for AMR_WB) */
887 bo_add_16be(damr, 0x1); /* Mode change period (no restriction) */
892 static bo_t *GetD263Tag(void)
896 d263 = box_new("d263");
898 bo_add_fourcc(d263, "VLC ");
899 bo_add_16be(d263, 0xa);
905 static void hevcParseVPS(uint8_t * p_buffer, size_t i_buffer, uint8_t *general,
906 uint8_t * numTemporalLayer, bool * temporalIdNested)
908 const size_t i_decoded_nal_size = 512;
909 uint8_t p_dec_nal[i_decoded_nal_size];
910 size_t i_size = (i_buffer < i_decoded_nal_size)?i_buffer:i_decoded_nal_size;
911 nal_decode(p_buffer, p_dec_nal, i_size);
913 /* first two bytes are the NAL header, 3rd and 4th are:
914 vps_video_parameter_set_id(4)
915 vps_reserved_3_2bis(2)
916 vps_max_layers_minus1(6)
917 vps_max_sub_layers_minus1(3)
918 vps_temporal_id_nesting_flags
920 *numTemporalLayer = ((p_dec_nal[3] & 0x0E) >> 1) + 1;
921 *temporalIdNested = (bool)(p_dec_nal[3] & 0x01);
923 /* 5th & 6th are reserved 0xffff */
924 /* copy the first 12 bytes of profile tier */
925 memcpy(general, &p_dec_nal[6], 12);
928 static void hevcParseSPS(uint8_t * p_buffer, size_t i_buffer, uint8_t * chroma_idc,
929 uint8_t *bit_depth_luma_minus8, uint8_t *bit_depth_chroma_minus8)
931 const size_t i_decoded_nal_size = 512;
932 uint8_t p_dec_nal[i_decoded_nal_size];
933 size_t i_size = (i_buffer < i_decoded_nal_size)?i_buffer-2:i_decoded_nal_size;
934 nal_decode(p_buffer+2, p_dec_nal, i_size);
936 bs_init(&bs, p_dec_nal, i_size);
940 uint32_t sps_max_sublayer_minus1 = bs_read(&bs, 3);
942 /* skip nesting flag */
945 hevc_skip_profile_tiers_level(&bs, sps_max_sublayer_minus1);
948 (void) bs_read_ue( &bs );
950 *chroma_idc = bs_read_ue(&bs);
951 if (*chroma_idc == 3)
954 /* skip width and heigh */
955 (void) bs_read_ue( &bs );
956 (void) bs_read_ue( &bs );
958 uint32_t conformance_window_flag = bs_read1(&bs);
959 if (conformance_window_flag) {
961 (void) bs_read_ue(&bs);
962 (void) bs_read_ue(&bs);
963 (void) bs_read_ue(&bs);
964 (void) bs_read_ue(&bs);
966 *bit_depth_luma_minus8 = bs_read_ue(&bs);
967 *bit_depth_chroma_minus8 = bs_read_ue(&bs);
970 static bo_t *GetHvcCTag(mp4_stream_t *p_stream)
972 /* Generate hvcC box matching iso/iec 14496-15 3rd edition */
973 bo_t *hvcC = box_new("hvcC");
974 if(!p_stream->fmt.i_extra)
982 /* According to the specification HEVC stream can have
983 * 16 vps id and an "unlimited" number of sps and pps id using ue(v) id*/
984 struct nal p_vps[16], *p_sps = NULL, *p_pps = NULL, *p_sei = NULL,
986 size_t i_vps = 0, i_sps = 0, i_pps = 0, i_sei = 0;
987 uint8_t i_num_arrays = 0;
989 uint8_t * p_buffer = p_stream->fmt.p_extra;
990 size_t i_buffer = p_stream->fmt.i_extra;
992 uint8_t general_configuration[12] = {0};
993 uint8_t i_numTemporalLayer = 0;
994 uint8_t i_chroma_idc = 1;
995 uint8_t i_bit_depth_luma_minus8 = 0;
996 uint8_t i_bit_depth_chroma_minus8 = 0;
997 bool b_temporalIdNested = false;
999 uint32_t cmp = 0xFFFFFFFF;
1001 /* look for start code 0X0000001 */
1003 cmp = (cmp << 8) | *p_buffer;
1004 if((cmp ^ UINT32_C(0x100)) <= UINT32_C(0xFF))
1010 p_nal->i_buffer = p_buffer - p_nal->p_buffer - ((i_buffer)?3:0);
1012 switch (*p_buffer & 0x72) {
1015 p_nal = &p_vps[i_vps++];
1016 p_nal->p_buffer = p_buffer;
1017 /* Only keep the general profile from the first VPS
1018 * if there are several (this shouldn't happen so soon) */
1020 hevcParseVPS(p_buffer, i_buffer, general_configuration,
1021 &i_numTemporalLayer, &b_temporalIdNested);
1027 struct nal * p_tmp = realloc(p_sps, sizeof(struct nal) * (i_sps + 1));
1031 p_nal = &p_sps[i_sps++];
1032 p_nal->p_buffer = p_buffer;
1033 if (i_sps == 1 && i_buffer > 15) {
1034 /* Get Chroma_idc and bitdepths */
1035 hevcParseSPS(p_buffer, i_buffer, &i_chroma_idc,
1036 &i_bit_depth_luma_minus8, &i_bit_depth_chroma_minus8);
1043 struct nal * p_tmp = realloc(p_pps, sizeof(struct nal) * (i_pps + 1));
1047 p_nal = &p_pps[i_pps++];
1048 p_nal->p_buffer = p_buffer;
1056 struct nal * p_tmp = realloc(p_sei, sizeof(struct nal) * (i_sei + 1));
1060 p_nal = &p_sei[i_sei++];
1061 p_nal->p_buffer = p_buffer;
1071 bo_add_8(hvcC, 0x01);
1072 bo_add_mem(hvcC, 12, general_configuration);
1073 /* Don't set min spatial segmentation */
1074 bo_add_16be(hvcC, 0xF000);
1075 /* Don't set parallelism type since segmentation isn't set */
1076 bo_add_8(hvcC, 0xFC);
1077 bo_add_8(hvcC, (0xFC | (i_chroma_idc & 0x03)));
1078 bo_add_8(hvcC, (0xF8 | (i_bit_depth_luma_minus8 & 0x07)));
1079 bo_add_8(hvcC, (0xF8 | (i_bit_depth_chroma_minus8 & 0x07)));
1081 /* Don't set framerate */
1082 bo_add_16be(hvcC, 0x0000);
1083 /* Force NAL size of 4 bytes that replace the startcode */
1084 bo_add_8(hvcC, (((i_numTemporalLayer & 0x07) << 3) |
1085 (b_temporalIdNested << 2) | 0x03));
1086 bo_add_8(hvcC, i_num_arrays);
1090 /* Write VPS without forcing array_completeness */
1092 bo_add_16be(hvcC, i_vps);
1093 for (size_t i = 0; i < i_vps; i++) {
1095 bo_add_16be(hvcC, p_nal->i_buffer);
1096 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1101 /* Write SPS without forcing array_completeness */
1103 bo_add_16be(hvcC, i_sps);
1104 for (size_t i = 0; i < i_sps; i++) {
1106 bo_add_16be(hvcC, p_nal->i_buffer);
1107 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1112 /* Write PPS without forcing array_completeness */
1114 bo_add_16be(hvcC, i_pps);
1115 for (size_t i = 0; i < i_pps; i++) {
1117 bo_add_16be(hvcC, p_nal->i_buffer);
1118 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1123 /* Write SEI without forcing array_completeness */
1125 bo_add_16be(hvcC, i_sei);
1126 for (size_t i = 0; i < i_sei; i++) {
1128 bo_add_16be(hvcC, p_nal->i_buffer);
1129 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1135 static bo_t *GetAvcCTag(mp4_stream_t *p_stream)
1138 uint8_t *p_sps = NULL;
1139 uint8_t *p_pps = NULL;
1143 if (p_stream->fmt.i_extra > 0) {
1144 /* FIXME: take into account multiple sps/pps */
1145 uint8_t *p_buffer = p_stream->fmt.p_extra;
1146 int i_buffer = p_stream->fmt.i_extra;
1148 while (i_buffer > 3) {
1149 while (memcmp(p_buffer, &avc1_start_code[1], 3)) {
1153 const int i_nal_type = p_buffer[3]&0x1f;
1154 int i_startcode = 0;
1156 for (int i_offset = 1; i_offset+2 < i_buffer ; i_offset++)
1157 if (!memcmp(&p_buffer[i_offset], &avc1_start_code[1], 3)) {
1158 /* we found another startcode */
1159 i_startcode = i_offset;
1160 while (p_buffer[i_startcode-1] == 0 && i_startcode > 0)
1165 int i_size = i_startcode ? i_startcode : i_buffer;
1167 if (i_nal_type == 7) {
1168 p_sps = &p_buffer[3];
1169 i_sps_size = i_size - 3;
1171 if (i_nal_type == 8) {
1172 p_pps = &p_buffer[3];
1173 i_pps_size = i_size - 3;
1180 /* FIXME use better value */
1181 avcC = box_new("avcC");
1182 bo_add_8(avcC, 1); /* configuration version */
1183 bo_add_8(avcC, i_sps_size ? p_sps[1] : 77);
1184 bo_add_8(avcC, i_sps_size ? p_sps[2] : 64);
1185 bo_add_8(avcC, i_sps_size ? p_sps[3] : 30); /* level, 5.1 */
1186 bo_add_8(avcC, 0xff); /* 0b11111100 | lengthsize = 0x11 */
1188 bo_add_8(avcC, 0xe0 | (i_sps_size > 0 ? 1 : 0)); /* 0b11100000 | sps_count */
1189 if (i_sps_size > 0) {
1190 bo_add_16be(avcC, i_sps_size);
1191 bo_add_mem(avcC, i_sps_size, p_sps);
1194 bo_add_8(avcC, (i_pps_size > 0 ? 1 : 0)); /* pps_count */
1195 if (i_pps_size > 0) {
1196 bo_add_16be(avcC, i_pps_size);
1197 bo_add_mem(avcC, i_pps_size, p_pps);
1203 /* TODO: No idea about these values */
1204 static bo_t *GetSVQ3Tag(mp4_stream_t *p_stream)
1206 bo_t *smi = box_new("SMI ");
1208 if (p_stream->fmt.i_extra > 0x4e) {
1209 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
1210 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
1212 while (p + 8 < p_end) {
1213 int i_size = GetDWBE(p);
1214 if (i_size <= 1) /* FIXME handle 1 as long size */
1216 if (!strncmp((const char *)&p[4], "SMI ", 4)) {
1217 bo_add_mem(smi, p_end - p - 8, &p[8]);
1224 /* Create a dummy one in fallback */
1225 bo_add_fourcc(smi, "SEQH");
1226 bo_add_32be(smi, 0x5);
1227 bo_add_32be(smi, 0xe2c0211d);
1228 bo_add_8(smi, 0xc0);
1233 static bo_t *GetUdtaTag(sout_mux_t *p_mux)
1235 sout_mux_sys_t *p_sys = p_mux->p_sys;
1236 bo_t *udta = box_new("udta");
1239 for (unsigned int i_track = 0; i_track < p_sys->i_nb_streams; i_track++) {
1240 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
1241 vlc_fourcc_t codec = p_stream->fmt.i_codec;
1243 if (codec == VLC_CODEC_MP4V || codec == VLC_CODEC_MP4A) {
1244 bo_t *box = box_new("\251req");
1246 bo_add_16be(box, sizeof("QuickTime 6.0 or greater") - 1);
1247 bo_add_16be(box, 0);
1248 bo_add_mem(box, sizeof("QuickTime 6.0 or greater") - 1,
1249 (uint8_t *)"QuickTime 6.0 or greater");
1250 box_gather(udta, box);
1257 bo_t *box = box_new("\251enc");
1259 bo_add_16be(box, sizeof(PACKAGE_STRING " stream output") - 1);
1260 bo_add_16be(box, 0);
1261 bo_add_mem(box, sizeof(PACKAGE_STRING " stream output") - 1,
1262 (uint8_t*)PACKAGE_STRING " stream output");
1263 box_gather(udta, box);
1267 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
1269 #define ADD_META_BOX(type, box_string) { \
1271 if (vlc_meta_Get(p_meta, vlc_meta_##type)) \
1272 box = box_new("\251" box_string); \
1274 bo_add_16be(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type))); \
1275 bo_add_16be(box, 0); \
1276 bo_add_mem(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type)), \
1277 (uint8_t*)(vlc_meta_Get(p_meta, vlc_meta_##type))); \
1278 box_gather(udta, box); \
1281 ADD_META_BOX(Title, "nam");
1282 ADD_META_BOX(Artist, "ART");
1283 ADD_META_BOX(Genre, "gen");
1284 ADD_META_BOX(Copyright, "cpy");
1285 ADD_META_BOX(Description, "des");
1286 ADD_META_BOX(Date, "day");
1287 ADD_META_BOX(URL, "url");
1294 static bo_t *GetSounBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
1296 sout_mux_sys_t *p_sys = p_mux->p_sys;
1297 bool b_descr = true;
1298 vlc_fourcc_t codec = p_stream->fmt.i_codec;
1300 vlc_fourcc_to_char(codec, fcc);
1302 if (codec == VLC_CODEC_MPGA) {
1305 memcpy(fcc, ".mp3", 4);
1307 memcpy(fcc, "mp4a", 4);
1310 bo_t *soun = box_new(fcc);
1311 for (int i = 0; i < 6; i++)
1312 bo_add_8(soun, 0); // reserved;
1313 bo_add_16be(soun, 1); // data-reference-index
1315 /* SoundDescription */
1316 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
1317 bo_add_16be(soun, 1); // version 1;
1319 bo_add_16be(soun, 0); // version 0;
1320 bo_add_16be(soun, 0); // revision level (0)
1321 bo_add_32be(soun, 0); // vendor
1323 bo_add_16be(soun, p_stream->fmt.audio.i_channels);
1325 bo_add_16be(soun, p_stream->fmt.audio.i_bitspersample ?
1326 p_stream->fmt.audio.i_bitspersample : 16);
1327 bo_add_16be(soun, -2); // compression id
1328 bo_add_16be(soun, 0); // packet size (0)
1329 bo_add_16be(soun, p_stream->fmt.audio.i_rate); // sampleratehi
1330 bo_add_16be(soun, 0); // sampleratelo
1332 /* Extended data for SoundDescription V1 */
1333 if (p_sys->b_mov && p_stream->fmt.i_codec == VLC_CODEC_MP4A) {
1334 /* samples per packet */
1335 bo_add_32be(soun, p_stream->fmt.audio.i_frame_length);
1336 bo_add_32be(soun, 1536); /* bytes per packet */
1337 bo_add_32be(soun, 2); /* bytes per frame */
1338 /* bytes per sample */
1339 bo_add_32be(soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1342 /* Add an ES Descriptor */
1346 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
1347 box = GetWaveTag(p_stream);
1348 else if (codec == VLC_CODEC_AMR_NB)
1349 box = GetDamrTag(p_stream);
1351 box = GetESDS(p_stream);
1352 box_gather(soun, box);
1358 static bo_t *GetVideBox(mp4_stream_t *p_stream)
1362 switch(p_stream->fmt.i_codec)
1364 case VLC_CODEC_MP4V:
1365 case VLC_CODEC_MPGV: memcpy(fcc, "mp4v", 4); break;
1366 case VLC_CODEC_MJPG: memcpy(fcc, "mjpa", 4); break;
1367 case VLC_CODEC_SVQ1: memcpy(fcc, "SVQ1", 4); break;
1368 case VLC_CODEC_SVQ3: memcpy(fcc, "SVQ3", 4); break;
1369 case VLC_CODEC_H263: memcpy(fcc, "s263", 4); break;
1370 case VLC_CODEC_H264: memcpy(fcc, "avc1", 4); break;
1371 case VLC_CODEC_HEVC: memcpy(fcc, "hvc1", 4); break;
1372 case VLC_CODEC_YV12: memcpy(fcc, "yv12", 4); break;
1373 case VLC_CODEC_YUYV: memcpy(fcc, "yuy2", 4); break;
1375 vlc_fourcc_to_char(p_stream->fmt.i_codec, fcc);
1379 bo_t *vide = box_new(fcc);
1380 for (int i = 0; i < 6; i++)
1381 bo_add_8(vide, 0); // reserved;
1382 bo_add_16be(vide, 1); // data-reference-index
1384 bo_add_16be(vide, 0); // predefined;
1385 bo_add_16be(vide, 0); // reserved;
1386 for (int i = 0; i < 3; i++)
1387 bo_add_32be(vide, 0); // predefined;
1389 bo_add_16be(vide, p_stream->fmt.video.i_width); // i_width
1390 bo_add_16be(vide, p_stream->fmt.video.i_height); // i_height
1392 bo_add_32be(vide, 0x00480000); // h 72dpi
1393 bo_add_32be(vide, 0x00480000); // v 72dpi
1395 bo_add_32be(vide, 0); // data size, always 0
1396 bo_add_16be(vide, 1); // frames count per sample
1399 for (int i = 0; i < 32; i++)
1402 bo_add_16be(vide, 0x18); // depth
1403 bo_add_16be(vide, 0xffff); // predefined
1405 /* add an ES Descriptor */
1406 switch(p_stream->fmt.i_codec)
1408 case VLC_CODEC_MP4V:
1409 case VLC_CODEC_MPGV:
1410 box_gather(vide, GetESDS(p_stream));
1413 case VLC_CODEC_H263:
1414 box_gather(vide, GetD263Tag());
1417 case VLC_CODEC_SVQ3:
1418 box_gather(vide, GetSVQ3Tag(p_stream));
1421 case VLC_CODEC_H264:
1422 box_gather(vide, GetAvcCTag(p_stream));
1425 case VLC_CODEC_HEVC:
1426 box_gather(vide, GetHvcCTag(p_stream));
1433 static bo_t *GetTextBox(void)
1435 bo_t *text = box_new("text");
1437 for (int i = 0; i < 6; i++)
1438 bo_add_8(text, 0); // reserved;
1439 bo_add_16be(text, 1); // data-reference-index
1441 bo_add_32be(text, 0); // display flags
1442 bo_add_32be(text, 0); // justification
1443 for (int i = 0; i < 3; i++)
1444 bo_add_16be(text, 0); // back ground color
1446 bo_add_16be(text, 0); // box text
1447 bo_add_16be(text, 0); // box text
1448 bo_add_16be(text, 0); // box text
1449 bo_add_16be(text, 0); // box text
1451 bo_add_64be(text, 0); // reserved
1452 for (int i = 0; i < 3; i++)
1453 bo_add_16be(text, 0xff); // foreground color
1456 bo_add_mem(text, 9, (uint8_t*)"Helvetica");
1461 static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
1463 sout_mux_sys_t *p_sys = p_mux->p_sys;
1465 /* sample description */
1466 bo_t *stsd = box_full_new("stsd", 0, 0);
1467 bo_add_32be(stsd, 1);
1468 if (p_stream->fmt.i_cat == AUDIO_ES)
1469 box_gather(stsd, GetSounBox(p_mux, p_stream));
1470 else if (p_stream->fmt.i_cat == VIDEO_ES)
1471 box_gather(stsd, GetVideBox(p_stream));
1472 else if (p_stream->fmt.i_cat == SPU_ES)
1473 box_gather(stsd, GetTextBox());
1475 /* chunk offset table */
1477 if (p_sys->i_pos >= (((uint64_t)0x1) << 32)) {
1478 /* 64 bits version */
1479 p_stream->b_stco64 = true;
1480 stco = box_full_new("co64", 0, 0);
1482 /* 32 bits version */
1483 p_stream->b_stco64 = false;
1484 stco = box_full_new("stco", 0, 0);
1486 bo_add_32be(stco, 0); // entry-count (fixed latter)
1488 /* sample to chunk table */
1489 bo_t *stsc = box_full_new("stsc", 0, 0);
1490 bo_add_32be(stsc, 0); // entry-count (fixed latter)
1492 unsigned i_chunk = 0;
1493 unsigned i_stsc_last_val = 0, i_stsc_entries = 0;
1494 for (unsigned i = 0; i < p_stream->i_entry_count; i_chunk++) {
1495 mp4_entry_t *entry = p_stream->entry;
1498 if (p_stream->b_stco64)
1499 bo_add_64be(stco, entry[i].i_pos);
1501 bo_add_32be(stco, entry[i].i_pos);
1503 for (; i < p_stream->i_entry_count; i++)
1504 if (i >= p_stream->i_entry_count - 1 ||
1505 entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
1510 /* Add entry to the stsc table */
1511 if (i_stsc_last_val != i - i_first) {
1512 bo_add_32be(stsc, 1 + i_chunk); // first-chunk
1513 bo_add_32be(stsc, i - i_first) ; // samples-per-chunk
1514 bo_add_32be(stsc, 1); // sample-descr-index
1515 i_stsc_last_val = i - i_first;
1520 /* Fix stco entry count */
1521 bo_fix_32be(stco, 12, i_chunk);
1522 msg_Dbg(p_mux, "created %d chunks (stco)", i_chunk);
1524 /* Fix stsc entry count */
1525 bo_fix_32be(stsc, 12, i_stsc_entries );
1528 bo_t *stts = box_full_new("stts", 0, 0);
1529 bo_add_32be(stts, 0); // entry-count (fixed latter)
1531 unsigned i_index = 0;
1532 for (unsigned i = 0; i < p_stream->i_entry_count; i_index++) {
1534 mtime_t i_delta = p_stream->entry[i].i_length;
1536 for (; i < p_stream->i_entry_count; ++i)
1537 if (i == p_stream->i_entry_count || p_stream->entry[i].i_length != i_delta)
1540 bo_add_32be(stts, i - i_first); // sample-count
1541 bo_add_32be(stts, (uint64_t)i_delta * p_stream->i_timescale / CLOCK_FREQ); // sample-delta
1543 bo_fix_32be(stts, 12, i_index);
1545 /* composition time handling */
1547 if ( p_stream->b_hasbframes && (ctts = box_full_new("ctts", 0, 0)) )
1549 bo_add_32be(ctts, 0);
1551 for (unsigned i = 0; i < p_stream->i_entry_count; i_index++)
1554 mtime_t i_offset = p_stream->entry[i].i_pts_dts;
1556 for (; i < p_stream->i_entry_count; ++i)
1557 if (i == p_stream->i_entry_count || p_stream->entry[i].i_pts_dts != i_offset)
1560 bo_add_32be(ctts, i - i_first); // sample-count
1561 bo_add_32be(ctts, i_offset * p_stream->i_timescale / CLOCK_FREQ ); // sample-offset
1563 bo_fix_32be(ctts, 12, i_index);
1566 bo_t *stsz = box_full_new("stsz", 0, 0);
1568 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1571 i_size = p_stream->entry[i].i_size;
1572 else if ( p_stream->entry[i].i_size != i_size )
1578 bo_add_32be(stsz, i_size); // sample-size
1579 bo_add_32be(stsz, p_stream->i_entry_count); // sample-count
1580 if ( i_size == 0 ) // all samples have different size
1582 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1583 bo_add_32be(stsz, p_stream->entry[i].i_size); // sample-size
1586 /* create stss table */
1589 if ( p_stream->fmt.i_cat == VIDEO_ES || p_stream->fmt.i_cat == AUDIO_ES )
1591 mtime_t i_interval = -1;
1592 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1594 if ( i_interval != -1 )
1596 i_interval += p_stream->entry[i].i_length + p_stream->entry[i].i_pts_dts;
1597 if ( i_interval < CLOCK_FREQ * 2 )
1601 if (p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I) {
1603 stss = box_full_new("stss", 0, 0);
1604 bo_add_32be(stss, 0); /* fixed later */
1606 bo_add_32be(stss, 1 + i);
1614 bo_fix_32be(stss, 12, i_index);
1616 /* Now gather all boxes into stbl */
1617 bo_t *stbl = box_new("stbl");
1619 box_gather(stbl, stsd);
1620 box_gather(stbl, stts);
1622 box_gather(stbl, stss);
1624 box_gather(stbl, ctts);
1625 box_gather(stbl, stsc);
1626 box_gather(stbl, stsz);
1627 p_stream->i_stco_pos = stbl->len + 16;
1628 box_gather(stbl, stco);
1633 static int64_t get_timestamp(void);
1635 static void matrix_apply_rotation(es_format_t *fmt, uint32_t mvhd_matrix[9])
1637 enum video_orientation_t orientation = ORIENT_NORMAL;
1638 if (fmt->i_cat == VIDEO_ES)
1639 orientation = fmt->video.orientation;
1641 #define ATAN(a, b) do { mvhd_matrix[1] = (a) << 16; \
1642 mvhd_matrix[0] = (b) << 16; \
1645 switch (orientation) {
1646 case ORIENT_ROTATED_90: ATAN( 1, 0); break;
1647 case ORIENT_ROTATED_180: ATAN( 0, -1); break;
1648 case ORIENT_ROTATED_270: ATAN( -1, 0); break;
1649 default: ATAN( 0, 1); break;
1652 mvhd_matrix[3] = mvhd_matrix[0] ? 0 : 0x10000;
1653 mvhd_matrix[4] = mvhd_matrix[1] ? 0 : 0x10000;
1656 static bo_t *GetMoovBox(sout_mux_t *p_mux)
1658 sout_mux_sys_t *p_sys = p_mux->p_sys;
1662 uint32_t i_movie_timescale = 90000;
1663 int64_t i_movie_duration = 0;
1664 int64_t i_timestamp = get_timestamp();
1666 moov = box_new("moov");
1668 /* Create general info */
1669 if ( !p_sys->b_fragmented )
1671 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
1672 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1673 i_movie_duration = __MAX(i_movie_duration, p_stream->i_read_duration);
1675 msg_Dbg(p_mux, "movie duration %"PRId64"s", i_movie_duration / CLOCK_FREQ);
1677 i_movie_duration = i_movie_duration * i_movie_timescale / CLOCK_FREQ;
1680 i_movie_duration = 0;
1682 /* *** add /moov/mvhd *** */
1683 if (!p_sys->b_64_ext) {
1684 mvhd = box_full_new("mvhd", 0, 0);
1685 bo_add_32be(mvhd, i_timestamp); // creation time
1686 bo_add_32be(mvhd, i_timestamp); // modification time
1687 bo_add_32be(mvhd, i_movie_timescale); // timescale
1688 bo_add_32be(mvhd, i_movie_duration); // duration
1690 mvhd = box_full_new("mvhd", 1, 0);
1691 bo_add_64be(mvhd, i_timestamp); // creation time
1692 bo_add_64be(mvhd, i_timestamp); // modification time
1693 bo_add_32be(mvhd, i_movie_timescale); // timescale
1694 bo_add_64be(mvhd, i_movie_duration); // duration
1696 bo_add_32be(mvhd, 0x10000); // rate
1697 bo_add_16be(mvhd, 0x100); // volume
1698 bo_add_16be(mvhd, 0); // reserved
1699 for (int i = 0; i < 2; i++)
1700 bo_add_32be(mvhd, 0); // reserved
1702 uint32_t mvhd_matrix[9] = { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1704 for (int i = 0; i < 9; i++)
1705 bo_add_32be(mvhd, mvhd_matrix[i]);// matrix
1706 for (int i = 0; i < 6; i++)
1707 bo_add_32be(mvhd, 0); // pre-defined
1709 /* Next available track id */
1710 bo_add_32be(mvhd, p_sys->i_nb_streams + 1); // next-track-id
1712 box_gather(moov, mvhd);
1714 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
1715 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1717 mtime_t i_stream_duration;
1718 if ( !p_sys->b_fragmented )
1719 i_stream_duration = p_stream->i_read_duration * i_movie_timescale / CLOCK_FREQ;
1721 i_stream_duration = 0;
1723 /* *** add /moov/trak *** */
1724 bo_t *trak = box_new("trak");
1726 /* *** add /moov/trak/tkhd *** */
1728 if (!p_sys->b_64_ext) {
1730 tkhd = box_full_new("tkhd", 0, 0x0f);
1732 tkhd = box_full_new("tkhd", 0, 1);
1734 bo_add_32be(tkhd, i_timestamp); // creation time
1735 bo_add_32be(tkhd, i_timestamp); // modification time
1736 bo_add_32be(tkhd, p_stream->i_track_id);
1737 bo_add_32be(tkhd, 0); // reserved 0
1738 bo_add_32be(tkhd, i_stream_duration); // duration
1741 tkhd = box_full_new("tkhd", 1, 0x0f);
1743 tkhd = box_full_new("tkhd", 1, 1);
1745 bo_add_64be(tkhd, i_timestamp); // creation time
1746 bo_add_64be(tkhd, i_timestamp); // modification time
1747 bo_add_32be(tkhd, p_stream->i_track_id);
1748 bo_add_32be(tkhd, 0); // reserved 0
1749 bo_add_64be(tkhd, i_stream_duration); // duration
1752 for (int i = 0; i < 2; i++)
1753 bo_add_32be(tkhd, 0); // reserved
1754 bo_add_16be(tkhd, 0); // layer
1755 bo_add_16be(tkhd, 0); // pre-defined
1757 bo_add_16be(tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0);
1758 bo_add_16be(tkhd, 0); // reserved
1759 matrix_apply_rotation(&p_stream->fmt, mvhd_matrix);
1760 for (int i = 0; i < 9; i++)
1761 bo_add_32be(tkhd, mvhd_matrix[i]); // matrix
1762 if (p_stream->fmt.i_cat == AUDIO_ES) {
1763 bo_add_32be(tkhd, 0); // width (presentation)
1764 bo_add_32be(tkhd, 0); // height(presentation)
1765 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
1766 int i_width = p_stream->fmt.video.i_width << 16;
1767 if (p_stream->fmt.video.i_sar_num > 0 && p_stream->fmt.video.i_sar_den > 0) {
1768 i_width = (int64_t)p_stream->fmt.video.i_sar_num *
1769 ((int64_t)p_stream->fmt.video.i_width << 16) /
1770 p_stream->fmt.video.i_sar_den;
1772 // width (presentation)
1773 bo_add_32be(tkhd, i_width);
1774 // height(presentation)
1775 bo_add_32be(tkhd, p_stream->fmt.video.i_height << 16);
1777 int i_width = 320 << 16;
1779 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++) {
1780 mp4_stream_t *tk = p_sys->pp_streams[i];
1781 if (tk->fmt.i_cat == VIDEO_ES) {
1782 if (tk->fmt.video.i_sar_num > 0 &&
1783 tk->fmt.video.i_sar_den > 0)
1784 i_width = (int64_t)tk->fmt.video.i_sar_num *
1785 ((int64_t)tk->fmt.video.i_width << 16) /
1786 tk->fmt.video.i_sar_den;
1788 i_width = tk->fmt.video.i_width << 16;
1789 i_height = tk->fmt.video.i_height;
1793 bo_add_32be(tkhd, i_width); // width (presentation)
1794 bo_add_32be(tkhd, i_height << 16); // height(presentation)
1797 box_gather(trak, tkhd);
1799 /* *** add /moov/trak/edts and elst */
1800 if ( !p_sys->b_fragmented )
1802 bo_t *edts = box_new("edts");
1803 bo_t *elst = box_full_new("elst", p_sys->b_64_ext ? 1 : 0, 0);
1804 if (p_stream->i_starttime > 0) {
1805 bo_add_32be(elst, 2);
1807 if (p_sys->b_64_ext) {
1808 bo_add_64be(elst, p_stream->i_starttime *
1809 i_movie_timescale / CLOCK_FREQ);
1810 bo_add_64be(elst, -1);
1812 bo_add_32be(elst, p_stream->i_starttime *
1813 i_movie_timescale / CLOCK_FREQ);
1814 bo_add_32be(elst, -1);
1816 bo_add_16be(elst, 1);
1817 bo_add_16be(elst, 0);
1819 bo_add_32be(elst, 1);
1821 if (p_sys->b_64_ext) {
1822 bo_add_64be(elst, p_stream->i_read_duration *
1823 i_movie_timescale / CLOCK_FREQ);
1824 bo_add_64be(elst, 0);
1826 bo_add_32be(elst, p_stream->i_read_duration *
1827 i_movie_timescale / CLOCK_FREQ);
1828 bo_add_32be(elst, 0);
1830 bo_add_16be(elst, 1);
1831 bo_add_16be(elst, 0);
1833 box_gather(edts, elst);
1834 box_gather(trak, edts);
1837 /* *** add /moov/trak/mdia *** */
1838 bo_t *mdia = box_new("mdia");
1842 if (!p_sys->b_64_ext) {
1843 mdhd = box_full_new("mdhd", 0, 0);
1844 bo_add_32be(mdhd, i_timestamp); // creation time
1845 bo_add_32be(mdhd, i_timestamp); // modification time
1846 bo_add_32be(mdhd, p_stream->i_timescale); // timescale
1847 bo_add_32be(mdhd, i_stream_duration); // duration
1849 mdhd = box_full_new("mdhd", 1, 0);
1850 bo_add_64be(mdhd, i_timestamp); // creation time
1851 bo_add_64be(mdhd, i_timestamp); // modification time
1852 bo_add_32be(mdhd, p_stream->i_timescale); // timescale
1853 bo_add_64be(mdhd, i_stream_duration); // duration
1856 if (p_stream->fmt.psz_language) {
1857 char *psz = p_stream->fmt.psz_language;
1858 const iso639_lang_t *pl = NULL;
1859 uint16_t lang = 0x0;
1861 if (strlen(psz) == 2)
1862 pl = GetLang_1(psz);
1863 else if (strlen(psz) == 3) {
1864 pl = GetLang_2B(psz);
1865 if (!strcmp(pl->psz_iso639_1, "??"))
1866 pl = GetLang_2T(psz);
1869 if (pl && strcmp(pl->psz_iso639_1, "??"))
1870 lang = ((pl->psz_iso639_2T[0] - 0x60) << 10) |
1871 ((pl->psz_iso639_2T[1] - 0x60) << 5) |
1872 ((pl->psz_iso639_2T[2] - 0x60));
1873 bo_add_16be(mdhd, lang); // language
1875 bo_add_16be(mdhd, 0 ); // language
1876 bo_add_16be(mdhd, 0 ); // predefined
1877 box_gather(mdia, mdhd);
1879 /* handler reference */
1880 bo_t *hdlr = box_full_new("hdlr", 0, 0);
1883 bo_add_fourcc(hdlr, "mhlr"); // media handler
1885 bo_add_32be(hdlr, 0);
1887 if (p_stream->fmt.i_cat == AUDIO_ES)
1888 bo_add_fourcc(hdlr, "soun");
1889 else if (p_stream->fmt.i_cat == VIDEO_ES)
1890 bo_add_fourcc(hdlr, "vide");
1891 else if (p_stream->fmt.i_cat == SPU_ES)
1892 bo_add_fourcc(hdlr, "text");
1894 bo_add_32be(hdlr, 0); // reserved
1895 bo_add_32be(hdlr, 0); // reserved
1896 bo_add_32be(hdlr, 0); // reserved
1899 bo_add_8(hdlr, 12); /* Pascal string for .mov */
1901 if (p_stream->fmt.i_cat == AUDIO_ES)
1902 bo_add_mem(hdlr, 12, (uint8_t*)"SoundHandler");
1903 else if (p_stream->fmt.i_cat == VIDEO_ES)
1904 bo_add_mem(hdlr, 12, (uint8_t*)"VideoHandler");
1906 bo_add_mem(hdlr, 12, (uint8_t*)"Text Handler");
1909 bo_add_8(hdlr, 0); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
1911 box_gather(mdia, hdlr);
1914 bo_t *minf = box_new("minf");
1917 if (p_stream->fmt.i_cat == AUDIO_ES) {
1920 smhd = box_full_new("smhd", 0, 0);
1921 bo_add_16be(smhd, 0); // balance
1922 bo_add_16be(smhd, 0); // reserved
1924 box_gather(minf, smhd);
1925 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
1928 vmhd = box_full_new("vmhd", 0, 1);
1929 bo_add_16be(vmhd, 0); // graphicsmode
1930 for (int i = 0; i < 3; i++)
1931 bo_add_16be(vmhd, 0); // opcolor
1933 box_gather(minf, vmhd);
1934 } else if (p_stream->fmt.i_cat == SPU_ES) {
1935 bo_t *gmhd = box_new("gmhd");
1936 bo_t *gmin = box_full_new("gmin", 0, 1);
1938 bo_add_16be(gmin, 0); // graphicsmode
1939 for (int i = 0; i < 3; i++)
1940 bo_add_16be(gmin, 0); // opcolor
1941 bo_add_16be(gmin, 0); // balance
1942 bo_add_16be(gmin, 0); // reserved
1944 box_gather(gmhd, gmin);
1946 box_gather(minf, gmhd);
1950 bo_t *dinf = box_new("dinf");
1951 bo_t *dref = box_full_new("dref", 0, 0);
1952 bo_add_32be(dref, 1);
1953 bo_t *url = box_full_new("url ", 0, 0x01);
1954 box_gather(dref, url);
1955 box_gather(dinf, dref);
1957 /* append dinf to mdia */
1958 box_gather(minf, dinf);
1962 if ( p_sys->b_fragmented )
1964 uint32_t i_backup = p_stream->i_entry_count;
1965 p_stream->i_entry_count = 0;
1966 stbl = GetStblBox(p_mux, p_stream);
1967 p_stream->i_entry_count = i_backup;
1970 stbl = GetStblBox(p_mux, p_stream);
1972 /* append stbl to minf */
1973 p_stream->i_stco_pos += minf->len;
1974 box_gather(minf, stbl);
1976 /* append minf to mdia */
1977 p_stream->i_stco_pos += mdia->len;
1978 box_gather(mdia, minf);
1980 /* append mdia to trak */
1981 p_stream->i_stco_pos += trak->len;
1982 box_gather(trak, mdia);
1984 /* append trak to moov */
1985 p_stream->i_stco_pos += moov->len;
1986 box_gather(moov, trak);
1989 /* Add user data tags */
1990 box_gather(moov, GetUdtaTag(p_mux));
1992 if ( p_sys->b_fragmented )
1994 bo_t *mvex = box_new("mvex");
1995 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++)
1997 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1999 /* Try to find some defaults */
2000 if ( p_stream->i_entry_count )
2002 // FIXME: find highest occurence
2003 p_stream->i_trex_length = p_stream->entry[0].i_length;
2004 p_stream->i_trex_size = p_stream->entry[0].i_size;
2007 /* *** add /mvex/trex *** */
2008 bo_t *trex = box_full_new("trex", 0, 0);
2009 bo_add_32be(trex, p_stream->i_track_id);
2010 bo_add_32be(trex, 1); // sample desc index
2011 bo_add_32be(trex, (uint64_t)p_stream->i_trex_length * p_stream->i_timescale / CLOCK_FREQ); // sample duration
2012 bo_add_32be(trex, p_stream->i_trex_size); // sample size
2013 bo_add_32be(trex, 0); // sample flags
2014 box_gather(mvex, trex);
2016 box_gather(moov, mvex);
2023 /****************************************************************************/
2025 static void bo_init(bo_t *p_bo)
2028 p_bo->b = block_Alloc(1024);
2031 static void bo_add_8(bo_t *p_bo, uint8_t i)
2033 if (p_bo->len >= p_bo->b->i_buffer)
2034 p_bo->b = block_Realloc(p_bo->b, 0, p_bo->b->i_buffer + 1024);
2036 p_bo->b->p_buffer[p_bo->len++] = i;
2039 static void bo_add_16be(bo_t *p_bo, uint16_t i)
2041 bo_add_8(p_bo, ((i >> 8) &0xff));
2042 bo_add_8(p_bo, i &0xff);
2045 static void bo_add_24be(bo_t *p_bo, uint32_t i)
2047 bo_add_8(p_bo, ((i >> 16) &0xff));
2048 bo_add_8(p_bo, ((i >> 8) &0xff));
2049 bo_add_8(p_bo, ( i &0xff));
2051 static void bo_add_32be(bo_t *p_bo, uint32_t i)
2053 bo_add_16be(p_bo, ((i >> 16) &0xffff));
2054 bo_add_16be(p_bo, i &0xffff);
2057 static void bo_fix_32be (bo_t *p_bo, int i_pos, uint32_t i)
2059 p_bo->b->p_buffer[i_pos ] = (i >> 24)&0xff;
2060 p_bo->b->p_buffer[i_pos + 1] = (i >> 16)&0xff;
2061 p_bo->b->p_buffer[i_pos + 2] = (i >> 8)&0xff;
2062 p_bo->b->p_buffer[i_pos + 3] = (i )&0xff;
2065 static void bo_add_64be(bo_t *p_bo, uint64_t i)
2067 bo_add_32be(p_bo, ((i >> 32) &0xffffffff));
2068 bo_add_32be(p_bo, i &0xffffffff);
2071 static void bo_add_fourcc(bo_t *p_bo, const char *fcc)
2073 bo_add_8(p_bo, fcc[0]);
2074 bo_add_8(p_bo, fcc[1]);
2075 bo_add_8(p_bo, fcc[2]);
2076 bo_add_8(p_bo, fcc[3]);
2079 static void bo_add_mem(bo_t *p_bo, int i_size, uint8_t *p_mem)
2081 for (int i = 0; i < i_size; i++)
2082 bo_add_8(p_bo, p_mem[i]);
2085 static void bo_add_descr(bo_t *p_bo, uint8_t tag, uint32_t size)
2087 bo_add_8(p_bo, tag);
2088 for (int i = 3; i>0; i--)
2089 bo_add_8(p_bo, (size>>(7*i)) | 0x80);
2090 bo_add_8(p_bo, size & 0x7F);
2093 static bo_t *box_new(const char *fcc)
2095 bo_t *box = malloc(sizeof(*box));
2101 bo_add_32be (box, 0);
2102 bo_add_fourcc(box, fcc);
2107 static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f)
2109 bo_t *box = box_new(fcc);
2114 bo_add_24be (box, f);
2119 static void box_free(bo_t *box)
2121 block_Release(box->b);
2125 static void box_fix(bo_t *box)
2127 box->b->p_buffer[0] = box->len >> 24;
2128 box->b->p_buffer[1] = box->len >> 16;
2129 box->b->p_buffer[2] = box->len >> 8;
2130 box->b->p_buffer[3] = box->len;
2133 static void box_gather (bo_t *box, bo_t *box2)
2136 box->b = block_Realloc(box->b, 0, box->len + box2->len);
2137 memcpy(&box->b->p_buffer[box->len], box2->b->p_buffer, box2->len);
2138 box->len += box2->len;
2142 static void box_send(sout_mux_t *p_mux, bo_t *box)
2144 box->b->i_buffer = box->len;
2145 sout_AccessOutWrite(p_mux->p_access, box->b);
2149 static int64_t get_timestamp(void)
2151 int64_t i_timestamp = time(NULL);
2153 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2154 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60
2159 /***************************************************************************
2161 ****************************************************************************/
2162 #define FRAGMENT_LENGTH (CLOCK_FREQ * 3/2)
2164 #define ENQUEUE_ENTRY(object, entry) \
2167 object.p_last->p_next = entry;\
2168 object.p_last = entry;\
2169 if (!object.p_first)\
2170 object.p_first = entry;\
2173 #define DEQUEUE_ENTRY(object, entry) \
2175 entry = object.p_first;\
2176 if (object.p_last == entry)\
2177 object.p_last = NULL;\
2178 object.p_first = object.p_first->p_next;\
2179 entry->p_next = NULL;\
2182 /* Creates mfra/traf index entries */
2183 static void AddKeyframeEntry(mp4_stream_t *p_stream, const uint64_t i_moof_pos,
2184 const uint8_t i_traf, const uint32_t i_sample,
2185 const mtime_t i_time)
2187 /* alloc or realloc */
2188 mp4_fragindex_t *p_entries = p_stream->p_indexentries;
2189 if (p_stream->i_indexentries >= p_stream->i_indexentriesmax)
2191 p_stream->i_indexentriesmax += 256;
2192 p_entries = xrealloc(p_stream->p_indexentries,
2193 p_stream->i_indexentriesmax * sizeof(mp4_fragindex_t));
2194 if (p_entries) /* realloc can fail */
2195 p_stream->p_indexentries = p_entries;
2198 mtime_t i_last_entry_time;
2199 if (p_stream->i_indexentries)
2200 i_last_entry_time = p_stream->p_indexentries[p_stream->i_indexentries - 1].i_time;
2202 i_last_entry_time = 0;
2204 if (p_entries && i_time - i_last_entry_time >= CLOCK_FREQ * 2)
2206 mp4_fragindex_t *p_indexentry = &p_stream->p_indexentries[p_stream->i_indexentries];
2207 p_indexentry->i_time = i_time;
2208 p_indexentry->i_moofoffset = i_moof_pos;
2209 p_indexentry->i_sample = i_sample;
2210 p_indexentry->i_traf = i_traf;
2211 p_indexentry->i_trun = 1;
2212 p_stream->i_indexentries++;
2216 /* Creates moof box and traf/trun information.
2217 * Single run per traf is absolutely not optimal as interleaving should be done
2218 * using runs and not limiting moof size, but creating an relative offset only
2219 * requires base_offset_is_moof and then comply to late iso brand spec which
2220 * breaks clients. */
2221 static bo_t *GetMoofBox(sout_mux_t *p_mux, size_t *pi_mdat_total_size,
2222 mtime_t i_barrier_time, const uint64_t i_write_pos)
2224 sout_mux_sys_t *p_sys = p_mux->p_sys;
2227 size_t i_fixupoffset = 0;
2229 *pi_mdat_total_size = 0;
2231 moof = box_new("moof");
2233 /* *** add /moof/mfhd *** */
2235 mfhd = box_full_new("mfhd", 0, 0);
2236 bo_add_32be(mfhd, p_sys->i_mfhd_sequence++); // sequence number
2238 box_gather(moof, mfhd);
2240 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++)
2242 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
2244 /* *** add /moof/traf *** */
2245 bo_t *traf = box_new("traf");
2247 uint32_t i_sample = 0;
2248 mtime_t i_time = p_stream->i_written_duration;
2249 bool b_allsamesize = true;
2250 bool b_allsamelength = true;
2251 if ( p_stream->read.p_first )
2253 mp4_fragentry_t *p_entry = p_stream->read.p_first->p_next;
2254 while (p_entry && (b_allsamelength || b_allsamesize))
2256 /* compare against queue head */
2257 b_allsamelength &= ( p_entry->p_block->i_length == p_stream->read.p_first->p_block->i_length );
2258 b_allsamesize &= ( p_entry->p_block->i_buffer == p_stream->read.p_first->p_block->i_buffer );
2259 p_entry = p_entry->p_next;
2263 uint16_t i_tfhd_flags = 0x0;
2264 if (p_stream->read.p_first)
2266 /* Current segment have all same duration value, different than trex's default */
2267 if (b_allsamelength &&
2268 p_stream->read.p_first->p_block->i_length != p_stream->i_trex_length &&
2269 p_stream->read.p_first->p_block->i_length)
2270 i_tfhd_flags |= MP4_TFHD_DFLT_SAMPLE_DURATION;
2272 /* Current segment have all same size value, different than trex's default */
2273 if (b_allsamesize &&
2274 p_stream->read.p_first->p_block->i_buffer != p_stream->i_trex_size &&
2275 p_stream->read.p_first->p_block->i_buffer)
2276 i_tfhd_flags |= MP4_TFHD_DFLT_SAMPLE_SIZE;
2280 /* We have no samples */
2281 i_tfhd_flags |= MP4_TFHD_DURATION_IS_EMPTY;
2284 /* *** add /moof/traf/tfhd *** */
2285 bo_t *tfhd = box_full_new("tfhd", 0, i_tfhd_flags);
2286 bo_add_32be(tfhd, p_stream->i_track_id);
2288 /* set the local sample duration default */
2289 if (i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_DURATION)
2290 bo_add_32be(tfhd, p_stream->read.p_first->p_block->i_length * p_stream->i_timescale / CLOCK_FREQ);
2292 /* set the local sample size default */
2293 if (i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_SIZE)
2294 bo_add_32be(tfhd, p_stream->read.p_first->p_block->i_buffer);
2296 box_gather(traf, tfhd);
2298 /* *** add /moof/traf/trun *** */
2299 if (p_stream->read.p_first)
2301 uint16_t i_trun_flags = 0x0;
2303 if (p_stream->b_hasiframes && !(p_stream->read.p_first->p_block->i_flags & BLOCK_FLAG_TYPE_I))
2304 i_trun_flags |= MP4_TRUN_FIRST_FLAGS;
2306 if (!b_allsamelength ||
2307 ( !(i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_DURATION) && p_stream->i_trex_length == 0 ))
2308 i_trun_flags |= MP4_TRUN_SAMPLE_DURATION;
2310 if (!b_allsamesize ||
2311 ( !(i_tfhd_flags & MP4_TFHD_DFLT_SAMPLE_SIZE) && p_stream->i_trex_size == 0 ))
2312 i_trun_flags |= MP4_TRUN_SAMPLE_SIZE;
2314 if (p_stream->b_hasbframes)
2315 i_trun_flags |= MP4_TRUN_SAMPLE_TIME_OFFSET;
2317 if (i_fixupoffset == 0)
2318 i_trun_flags |= MP4_TRUN_DATA_OFFSET;
2320 bo_t *trun = box_full_new("trun", 0, i_trun_flags);
2323 uint32_t i_entry_count = 0;
2324 mtime_t i_run_time = p_stream->i_written_duration;
2325 mp4_fragentry_t *p_entry = p_stream->read.p_first;
2328 if ( i_barrier_time && i_run_time + p_entry->p_block->i_length > i_barrier_time )
2331 i_run_time += p_entry->p_block->i_length;
2332 p_entry = p_entry->p_next;
2334 bo_add_32be(trun, i_entry_count); // sample count
2336 if (i_trun_flags & MP4_TRUN_DATA_OFFSET)
2338 i_fixupoffset = moof->len + traf->len + trun->len;
2339 bo_add_32be(trun, 0xdeadbeef); // data offset
2342 if (i_trun_flags & MP4_TRUN_FIRST_FLAGS)
2343 bo_add_32be(trun, 1<<16); // flag as non keyframe
2345 while(p_stream->read.p_first && i_entry_count)
2347 DEQUEUE_ENTRY(p_stream->read, p_entry);
2349 if (i_trun_flags & MP4_TRUN_SAMPLE_DURATION)
2350 bo_add_32be(trun, p_entry->p_block->i_length * p_stream->i_timescale / CLOCK_FREQ); // sample duration
2352 if (i_trun_flags & MP4_TRUN_SAMPLE_SIZE)
2353 bo_add_32be(trun, p_entry->p_block->i_buffer); // sample size
2355 if (i_trun_flags & MP4_TRUN_SAMPLE_TIME_OFFSET)
2357 uint32_t i_diff = 0;
2358 if ( p_entry->p_block->i_dts > VLC_TS_INVALID &&
2359 p_entry->p_block->i_pts > p_entry->p_block->i_dts )
2361 i_diff = p_entry->p_block->i_pts - p_entry->p_block->i_dts;
2363 bo_add_32be(trun, i_diff * p_stream->i_timescale / CLOCK_FREQ); // ctts
2366 *pi_mdat_total_size += p_entry->p_block->i_buffer;
2368 ENQUEUE_ENTRY(p_stream->towrite, p_entry);
2372 /* Add keyframe entry if needed */
2373 if (p_stream->b_hasiframes && (p_entry->p_block->i_flags & BLOCK_FLAG_TYPE_I) &&
2374 (p_stream->fmt.i_cat == VIDEO_ES || p_stream->fmt.i_cat == AUDIO_ES))
2376 AddKeyframeEntry(p_stream, i_write_pos, i_trak, i_sample, i_time);
2379 i_time += p_entry->p_block->i_length;
2382 box_gather(traf, trun);
2385 box_gather(moof, traf);
2390 /* do tfhd base data offset fixup */
2393 /* mdat will follow moof */
2394 SetDWBE(moof->b->p_buffer + i_fixupoffset, moof->len + 8);
2397 /* set iframe flag, so the streaming server always starts from moof */
2398 moof->b->i_flags |= BLOCK_FLAG_TYPE_I;
2403 static void WriteFragmentMDAT(sout_mux_t *p_mux, size_t i_total_size)
2405 sout_mux_sys_t *p_sys = p_mux->p_sys;
2407 /* Now add mdat header */
2408 bo_t *mdat = box_new("mdat");
2409 /* force update of real size */
2410 mdat->b->i_buffer = mdat->len;
2411 assert(mdat->len==8);
2412 mdat->len += i_total_size;
2414 p_sys->i_pos += mdat->b->i_buffer;
2415 /* only write header */
2416 sout_AccessOutWrite(p_mux->p_access, mdat->b);
2418 /* Header and its size are written and good, now write content */
2419 for (unsigned int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++)
2421 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
2423 while(p_stream->towrite.p_first)
2425 mp4_fragentry_t *p_entry = p_stream->towrite.p_first;
2426 p_sys->i_pos += p_entry->p_block->i_buffer;
2427 p_stream->i_written_duration += p_entry->p_block->i_length;
2429 p_entry->p_block->i_flags &= ~BLOCK_FLAG_TYPE_I; // clear flag for http stream
2430 sout_AccessOutWrite(p_mux->p_access, p_entry->p_block);
2432 p_stream->towrite.p_first = p_entry->p_next;
2434 if (!p_stream->towrite.p_first)
2435 p_stream->towrite.p_last = NULL;
2440 static bo_t *GetMfraBox(sout_mux_t *p_mux)
2442 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2444 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2446 mp4_stream_t *p_stream = p_sys->pp_streams[i];
2447 if (p_stream->i_indexentries)
2449 bo_t *tfra = box_full_new("tfra", 0, 0x0);
2450 if (!tfra) continue;
2451 bo_add_32be(tfra, p_stream->i_track_id);
2452 bo_add_32be(tfra, 0x3); // reserved + lengths (1,1,4)=>(0,0,3)
2453 bo_add_32be(tfra, p_stream->i_indexentries);
2454 for(uint32_t i_index=0; i_index<p_stream->i_indexentries; i_index++)
2456 const mp4_fragindex_t *p_indexentry = &p_stream->p_indexentries[i_index];
2457 bo_add_32be(tfra, p_indexentry->i_time);
2458 bo_add_32be(tfra, p_indexentry->i_moofoffset);
2459 assert(sizeof(p_indexentry->i_traf==1)); /* guard against sys changes */
2460 assert(sizeof(p_indexentry->i_trun==1));
2461 assert(sizeof(p_indexentry->i_sample==4));
2462 bo_add_8(tfra, p_indexentry->i_traf);
2463 bo_add_8(tfra, p_indexentry->i_trun);
2464 bo_add_32be(tfra, p_indexentry->i_sample);
2467 if (!mfra && !(mfra = box_new("mfra")))
2473 box_gather(mfra,tfra);
2479 static void FlushHeader(sout_mux_t *p_mux)
2481 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2483 /* Now add ftyp header */
2484 bo_t *ftyp = box_new("ftyp");
2485 bo_add_fourcc(ftyp, "isom");
2486 bo_add_32be (ftyp, 0); // minor version
2489 bo_t *moov = GetMoovBox(p_mux);
2491 /* merge into a single block */
2492 box_gather(ftyp, moov);
2494 /* add header flag for streaming server */
2495 ftyp->b->i_flags |= BLOCK_FLAG_HEADER;
2496 p_sys->i_pos += ftyp->len;
2497 box_send(p_mux, ftyp);
2498 p_sys->b_header_sent = true;
2501 static int OpenFrag(vlc_object_t *p_this)
2503 sout_mux_t *p_mux = (sout_mux_t*) p_this;
2504 sout_mux_sys_t *p_sys = malloc(sizeof(sout_mux_sys_t));
2508 p_mux->p_sys = (sout_mux_sys_t *) p_sys;
2509 p_mux->pf_control = Control;
2510 p_mux->pf_addstream = AddStream;
2511 p_mux->pf_delstream = DelStream;
2512 p_mux->pf_mux = MuxFrag;
2515 p_sys->b_mov = false;
2516 p_sys->b_3gp = false;
2517 p_sys->b_64_ext = false;
2521 p_sys->i_nb_streams = 0;
2522 p_sys->pp_streams = NULL;
2523 p_sys->i_mdat_pos = 0;
2524 p_sys->i_read_duration = 0;
2525 p_sys->i_written_duration= 0;
2527 p_sys->b_header_sent = false;
2528 p_sys->b_fragmented = true;
2529 p_sys->i_mfhd_sequence = 1;
2534 static void WriteFragments(sout_mux_t *p_mux, bool b_flush)
2536 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2538 mtime_t i_barrier_time = p_sys->i_written_duration + FRAGMENT_LENGTH;
2539 size_t i_mdat_size = 0;
2540 bool b_has_samples = false;
2542 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2544 const mp4_stream_t *p_stream = p_sys->pp_streams[i];
2545 if (p_stream->read.p_first)
2547 b_has_samples = true;
2549 /* set a barrier so we try to align to keyframe */
2550 if (p_stream->b_hasiframes &&
2551 p_stream->i_last_iframe_time > p_stream->i_written_duration &&
2552 (p_stream->fmt.i_cat == VIDEO_ES ||
2553 p_stream->fmt.i_cat == AUDIO_ES) )
2555 i_barrier_time = __MIN(i_barrier_time, p_stream->i_last_iframe_time);
2560 if (!p_sys->b_header_sent)
2564 moof = GetMoofBox(p_mux, &i_mdat_size, (b_flush)?0:i_barrier_time, p_sys->i_pos);
2566 if (moof && i_mdat_size == 0)
2568 block_Release(moof->b);
2574 msg_Dbg(p_mux, "writing moof @ %"PRId64, p_sys->i_pos);
2575 p_sys->i_pos += moof->len;
2576 assert(moof->b->i_flags & BLOCK_FLAG_TYPE_I); /* http sout */
2577 box_send(p_mux, moof);
2578 msg_Dbg(p_mux, "writing mdat @ %"PRId64, p_sys->i_pos);
2579 WriteFragmentMDAT(p_mux, i_mdat_size);
2581 /* update iframe point */
2582 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2584 mp4_stream_t *p_stream = p_sys->pp_streams[i];
2585 p_stream->i_last_iframe_time = 0;
2590 /* Do an entry length fixup using only its own info.
2591 * This is the end boundary case. */
2592 static void LengthLocalFixup(sout_mux_t *p_mux, const mp4_stream_t *p_stream, block_t *p_entrydata)
2594 if ( p_stream->fmt.i_cat == VIDEO_ES )
2596 p_entrydata->i_length = CLOCK_FREQ *
2597 p_stream->fmt.video.i_frame_rate_base /
2598 p_stream->fmt.video.i_frame_rate;
2599 msg_Dbg(p_mux, "video track %d fixup to %"PRId64" for sample %u",
2600 p_stream->i_track_id, p_entrydata->i_length, p_stream->i_entry_count - 1);
2602 else if (p_stream->fmt.i_cat == AUDIO_ES &&
2603 p_stream->fmt.audio.i_rate &&
2604 p_entrydata->i_nb_samples)
2606 p_entrydata->i_length = CLOCK_FREQ * p_entrydata->i_nb_samples /
2607 p_stream->fmt.audio.i_rate;
2608 msg_Dbg(p_mux, "audio track %d fixup to %"PRId64" for sample %u",
2609 p_stream->i_track_id, p_entrydata->i_length, p_stream->i_entry_count - 1);
2613 msg_Warn(p_mux, "unknown length for track %d sample %u",
2614 p_stream->i_track_id, p_stream->i_entry_count - 1);
2615 p_entrydata->i_length = 1;
2619 static void CleanupFrag(sout_mux_sys_t *p_sys)
2621 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2623 mp4_stream_t *p_stream = p_sys->pp_streams[i];
2624 if (p_stream->p_held_entry)
2626 block_Release(p_stream->p_held_entry->p_block);
2627 free(p_stream->p_held_entry);
2629 while(p_stream->read.p_first)
2631 mp4_fragentry_t *p_next = p_stream->read.p_first->p_next;
2632 block_Release(p_stream->read.p_first->p_block);
2633 free(p_stream->read.p_first);
2634 p_stream->read.p_first = p_next;
2636 while(p_stream->towrite.p_first)
2638 mp4_fragentry_t *p_next = p_stream->towrite.p_first->p_next;
2639 block_Release(p_stream->towrite.p_first->p_block);
2640 free(p_stream->towrite.p_first);
2641 p_stream->towrite.p_first = p_next;
2643 free(p_stream->p_indexentries);
2648 static void CloseFrag(vlc_object_t *p_this)
2650 sout_mux_t *p_mux = (sout_mux_t *) p_this;
2651 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2653 /* Flush remaining entries */
2654 for (unsigned int i = 0; i < p_sys->i_nb_streams; i++)
2656 mp4_stream_t *p_stream = p_sys->pp_streams[i];
2657 if (p_stream->p_held_entry)
2659 if (p_stream->p_held_entry->p_block->i_length < 1)
2660 LengthLocalFixup(p_mux, p_stream, p_stream->p_held_entry->p_block);
2661 ENQUEUE_ENTRY(p_stream->read, p_stream->p_held_entry);
2662 p_stream->p_held_entry = NULL;
2666 /* and force creating a fragment from it */
2667 WriteFragments(p_mux, true);
2669 /* Write indexes, but only for non streamed content
2670 as they refer to moof by absolute position */
2671 if (!strcmp(p_mux->psz_mux, "mp4frag"))
2673 bo_t *mfra = GetMfraBox(p_mux);
2676 bo_t *mfro = box_full_new("mfro", 0, 0x0);
2680 bo_add_32be(mfro, mfra->len + MP4_MFRO_BOXSIZE);
2681 box_gather(mfra, mfro);
2683 box_send(p_mux, mfra);
2690 static int MuxFrag(sout_mux_t *p_mux)
2692 sout_mux_sys_t *p_sys = (sout_mux_sys_t*) p_mux->p_sys;
2694 int i_stream = sout_MuxGetStream(p_mux, 1, NULL);
2697 sout_input_t *p_input = p_mux->pp_inputs[i_stream];
2698 mp4_stream_t *p_stream = (mp4_stream_t*) p_input->p_sys;
2699 block_t *p_currentblock = block_FifoGet(p_input->p_fifo);
2701 /* do block conversion */
2702 switch(p_stream->fmt.i_codec)
2704 case VLC_CODEC_H264:
2705 case VLC_CODEC_HEVC:
2706 p_currentblock = ConvertFromAnnexB(p_currentblock);
2708 case VLC_CODEC_SUBT:
2709 p_currentblock = ConvertSUBT(p_currentblock);
2715 /* If we have a previous entry for outgoing queue */
2716 if (p_stream->p_held_entry)
2718 block_t *p_heldblock = p_stream->p_held_entry->p_block;
2720 /* Fix previous block length from current */
2721 if (p_heldblock->i_length < 1)
2724 /* Fix using dts if not on a boundary */
2725 if ((p_currentblock->i_flags & BLOCK_FLAG_DISCONTINUITY) == 0)
2726 p_heldblock->i_length = p_currentblock->i_dts - p_heldblock->i_dts;
2728 if (p_heldblock->i_length < 1)
2729 LengthLocalFixup(p_mux, p_stream, p_heldblock);
2733 ENQUEUE_ENTRY(p_stream->read, p_stream->p_held_entry);
2734 p_stream->p_held_entry = NULL;
2736 if (p_stream->b_hasiframes && (p_heldblock->i_flags & BLOCK_FLAG_TYPE_I) &&
2737 p_stream->i_read_duration - p_sys->i_written_duration < FRAGMENT_LENGTH)
2739 /* Flag the last iframe time, we'll use it as boundary so it will start
2741 p_stream->i_last_iframe_time = p_stream->i_read_duration;
2744 /* update buffered time */
2745 p_stream->i_read_duration += __MAX(0, p_heldblock->i_length);
2749 /* set temp entry */
2750 p_stream->p_held_entry = malloc(sizeof(mp4_fragentry_t));
2751 if (unlikely(!p_stream->p_held_entry))
2754 p_stream->p_held_entry->p_block = p_currentblock;
2755 p_stream->p_held_entry->i_run = p_stream->i_current_run;
2756 p_stream->p_held_entry->p_next = NULL;
2758 if (p_stream->fmt.i_cat == VIDEO_ES )
2760 if (!p_stream->b_hasiframes && (p_currentblock->i_flags & BLOCK_FLAG_TYPE_I))
2761 p_stream->b_hasiframes = true;
2763 if (!p_stream->b_hasbframes && p_currentblock->i_dts > VLC_TS_INVALID &&
2764 p_currentblock->i_pts > p_currentblock->i_dts)
2765 p_stream->b_hasbframes = true;
2768 /* Update the global fragment/media duration */
2769 mtime_t i_min_read_duration = p_stream->i_read_duration;
2770 mtime_t i_min_written_duration = p_stream->i_written_duration;
2771 for (unsigned int i=0; i<p_sys->i_nb_streams; i++)
2773 const mp4_stream_t *p_s = p_sys->pp_streams[i];
2774 if (p_s->fmt.i_cat != VIDEO_ES && p_s->fmt.i_cat != AUDIO_ES)
2776 if (p_s->i_read_duration < i_min_read_duration)
2777 i_min_read_duration = p_s->i_read_duration;
2779 if (p_s->i_written_duration < i_min_written_duration)
2780 i_min_written_duration = p_s->i_written_duration;
2782 p_sys->i_read_duration = i_min_read_duration;
2783 p_sys->i_written_duration = i_min_written_duration;
2785 /* we have prerolled enough to know all streams, and have enough date to create a fragment */
2786 if (p_stream->read.p_first && p_sys->i_read_duration - p_sys->i_written_duration >= FRAGMENT_LENGTH)
2787 WriteFragments(p_mux, false);