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"
45 /*****************************************************************************
47 *****************************************************************************/
48 #define FASTSTART_TEXT N_("Create \"Fast Start\" files")
49 #define FASTSTART_LONGTEXT N_(\
50 "Create \"Fast Start\" files. " \
51 "\"Fast Start\" files are optimized for downloads and allow the user " \
52 "to start previewing the file while it is downloading.")
54 static int Open (vlc_object_t *);
55 static void Close (vlc_object_t *);
57 #define SOUT_CFG_PREFIX "sout-mp4-"
60 set_description(N_("MP4/MOV muxer"))
61 set_category(CAT_SOUT)
62 set_subcategory(SUBCAT_SOUT_MUX)
65 add_bool(SOUT_CFG_PREFIX "faststart", true,
66 FASTSTART_TEXT, FASTSTART_LONGTEXT,
68 set_capability("sout mux", 5)
69 add_shortcut("mp4", "mov", "3gp")
70 set_callbacks(Open, Close)
73 /*****************************************************************************
75 *****************************************************************************/
76 static const char *const ppsz_sout_options[] = {
80 static int Control(sout_mux_t *, int, va_list);
81 static int AddStream(sout_mux_t *, sout_input_t *);
82 static int DelStream(sout_mux_t *, sout_input_t *);
83 static int Mux (sout_mux_t *);
85 /*****************************************************************************
87 *****************************************************************************/
105 unsigned int i_entry_count;
106 unsigned int i_entry_max;
108 int64_t i_length_neg;
111 int64_t i_dts_start; /* applies to current segment only */
113 mtime_t i_starttime; /* the really first packet */
116 /* for later stco fix-up (fast start files) */
121 int64_t i_last_dts; /* applies to current segment only */
125 struct sout_mux_sys_t
137 mp4_stream_t **pp_streams;
146 static void bo_init (bo_t *);
147 static void bo_add_8 (bo_t *, uint8_t);
148 static void bo_add_16be (bo_t *, uint16_t);
149 static void bo_add_24be (bo_t *, uint32_t);
150 static void bo_add_32be (bo_t *, uint32_t);
151 static void bo_add_64be (bo_t *, uint64_t);
152 static void bo_add_fourcc(bo_t *, const char *);
153 static void bo_add_mem (bo_t *, int , uint8_t *);
154 static void bo_add_descr(bo_t *, uint8_t , uint32_t);
156 static void bo_fix_32be (bo_t *, int , uint32_t);
158 static bo_t *box_new (const char *fcc);
159 static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f);
160 static void box_fix (bo_t *box);
161 static void box_gather (bo_t *box, bo_t *box2);
163 static void box_send(sout_mux_t *p_mux, bo_t *box);
165 static bo_t *GetMoovBox(sout_mux_t *p_mux);
167 static block_t *ConvertSUBT(block_t *);
168 static block_t *ConvertFromAnnexB(block_t *);
170 static const char avc1_start_code[4] = { 0, 0, 0, 1 };
172 /*****************************************************************************
174 *****************************************************************************/
175 static int Open(vlc_object_t *p_this)
177 sout_mux_t *p_mux = (sout_mux_t*)p_this;
178 sout_mux_sys_t *p_sys;
181 msg_Dbg(p_mux, "Mp4 muxer opened");
182 config_ChainParse(p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg);
184 p_mux->pf_control = Control;
185 p_mux->pf_addstream = AddStream;
186 p_mux->pf_delstream = DelStream;
188 p_mux->p_sys = p_sys = malloc(sizeof(sout_mux_sys_t));
192 p_sys->i_nb_streams = 0;
193 p_sys->pp_streams = NULL;
194 p_sys->i_mdat_pos = 0;
195 p_sys->b_mov = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "mov");
196 p_sys->b_3gp = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "3gp");
197 p_sys->i_duration = 0;
200 /* Now add ftyp header */
201 box = box_new("ftyp");
203 bo_add_fourcc(box, "3gp6");
205 bo_add_fourcc(box, "isom");
206 bo_add_32be (box, 0);
208 bo_add_fourcc(box, "3gp4");
210 bo_add_fourcc(box, "mp41");
211 bo_add_fourcc(box, "avc1");
212 bo_add_fourcc(box, "qt ");
215 p_sys->i_pos += box->len;
216 p_sys->i_mdat_pos = p_sys->i_pos;
218 box_send(p_mux, box);
222 * Quicktime actually doesn't like the 64 bits extensions !!! */
223 p_sys->b_64_ext = false;
225 /* Now add mdat header */
226 box = box_new("mdat");
227 bo_add_64be (box, 0); // enough to store an extended size
229 p_sys->i_pos += box->len;
231 box_send(p_mux, box);
236 /*****************************************************************************
238 *****************************************************************************/
239 static void Close(vlc_object_t *p_this)
241 sout_mux_t *p_mux = (sout_mux_t*)p_this;
242 sout_mux_sys_t *p_sys = p_mux->p_sys;
244 msg_Dbg(p_mux, "Close");
246 /* Update mdat size */
249 if (p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32)) {
251 bo_add_32be (&bo, 1);
252 bo_add_fourcc(&bo, "mdat");
253 bo_add_64be (&bo, p_sys->i_pos - p_sys->i_mdat_pos);
255 bo_add_32be (&bo, 8);
256 bo_add_fourcc(&bo, "wide");
257 bo_add_32be (&bo, p_sys->i_pos - p_sys->i_mdat_pos - 8);
258 bo_add_fourcc(&bo, "mdat");
261 bo.b->i_buffer = bo.len;
262 sout_AccessOutSeek(p_mux->p_access, p_sys->i_mdat_pos);
263 sout_AccessOutWrite(p_mux->p_access, bo.b);
265 /* Create MOOV header */
266 uint64_t i_moov_pos = p_sys->i_pos;
267 bo_t *moov = GetMoovBox(p_mux);
269 /* Check we need to create "fast start" files */
270 p_sys->b_fast_start = var_GetBool(p_this, SOUT_CFG_PREFIX "faststart");
271 while (p_sys->b_fast_start) {
272 /* Move data to the end of the file so we can fit the moov header
274 int64_t i_size = p_sys->i_pos - p_sys->i_mdat_pos;
275 int i_moov_size = moov->len;
278 int64_t i_chunk = __MIN(32768, i_size);
279 block_t *p_buf = block_Alloc(i_chunk);
280 sout_AccessOutSeek(p_mux->p_access,
281 p_sys->i_mdat_pos + i_size - i_chunk);
282 if (sout_AccessOutRead(p_mux->p_access, p_buf) < i_chunk) {
283 msg_Warn(p_this, "read() not supported by access output, "
284 "won't create a fast start file");
285 p_sys->b_fast_start = false;
286 block_Release(p_buf);
289 sout_AccessOutSeek(p_mux->p_access, p_sys->i_mdat_pos + i_size +
290 i_moov_size - i_chunk);
291 sout_AccessOutWrite(p_mux->p_access, p_buf);
295 if (!p_sys->b_fast_start)
298 /* Fix-up samples to chunks table in MOOV header */
299 for (int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
300 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
302 moov->len = p_stream->i_stco_pos;
303 for (unsigned i = 0; i < p_stream->i_entry_count; ) {
304 mp4_entry_t *entry = p_stream->entry;
305 if (p_stream->b_stco64)
306 bo_add_64be(moov, entry[i].i_pos + i_moov_size);
308 bo_add_32be(moov, entry[i].i_pos + i_moov_size);
310 for (; i < p_stream->i_entry_count; i++)
311 if (i >= p_stream->i_entry_count - 1 ||
312 entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
319 moov->len = i_moov_size;
320 i_moov_pos = p_sys->i_mdat_pos;
321 p_sys->b_fast_start = false;
324 /* Write MOOV header */
325 sout_AccessOutSeek(p_mux->p_access, i_moov_pos);
326 box_send(p_mux, moov);
329 for (int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
330 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
332 es_format_Clean(&p_stream->fmt);
333 free(p_stream->entry);
336 if (p_sys->i_nb_streams)
337 free(p_sys->pp_streams);
341 /*****************************************************************************
343 *****************************************************************************/
344 static int Control(sout_mux_t *p_mux, int i_query, va_list args)
351 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
352 pb_bool = (bool*)va_arg(args, bool *);
356 case MUX_GET_ADD_STREAM_WAIT:
357 pb_bool = (bool*)va_arg(args, bool *);
361 case MUX_GET_MIME: /* Not needed, as not streamable */
367 /*****************************************************************************
369 *****************************************************************************/
370 static int AddStream(sout_mux_t *p_mux, sout_input_t *p_input)
372 sout_mux_sys_t *p_sys = p_mux->p_sys;
373 mp4_stream_t *p_stream;
375 switch(p_input->p_fmt->i_codec)
384 case VLC_CODEC_MJPGB:
390 case VLC_CODEC_AMR_NB:
391 case VLC_CODEC_AMR_WB:
396 msg_Warn(p_mux, "subtitle track added like in .mov (even when creating .mp4)");
399 msg_Err(p_mux, "unsupported codec %4.4s in mp4",
400 (char*)&p_input->p_fmt->i_codec);
404 p_stream = malloc(sizeof(mp4_stream_t));
407 es_format_Copy(&p_stream->fmt, p_input->p_fmt);
408 p_stream->i_track_id = p_sys->i_nb_streams + 1;
409 p_stream->i_length_neg = 0;
410 p_stream->i_entry_count = 0;
411 p_stream->i_entry_max = 1000;
413 calloc(p_stream->i_entry_max, sizeof(mp4_entry_t));
414 p_stream->i_dts_start = 0;
415 p_stream->i_duration = 0;
416 p_stream->i_starttime = p_sys->i_duration;
417 p_stream->b_hasbframes = false;
419 p_stream->i_last_dts = 0;
421 p_input->p_sys = p_stream;
423 msg_Dbg(p_mux, "adding input");
425 TAB_APPEND(p_sys->i_nb_streams, p_sys->pp_streams, p_stream);
429 /*****************************************************************************
431 *****************************************************************************/
432 static int DelStream(sout_mux_t *p_mux, sout_input_t *p_input)
435 msg_Dbg(p_mux, "removing input");
439 /*****************************************************************************
441 *****************************************************************************/
442 static int Mux(sout_mux_t *p_mux)
444 sout_mux_sys_t *p_sys = p_mux->p_sys;
447 int i_stream = sout_MuxGetStream(p_mux, 2, NULL);
451 sout_input_t *p_input = p_mux->pp_inputs[i_stream];
452 mp4_stream_t *p_stream = (mp4_stream_t*)p_input->p_sys;
456 p_data = block_FifoGet(p_input->p_fifo);
457 if (p_stream->fmt.i_codec == VLC_CODEC_H264 ||
458 p_stream->fmt.i_codec == VLC_CODEC_HEVC)
459 p_data = ConvertFromAnnexB(p_data);
460 else if (p_stream->fmt.i_codec == VLC_CODEC_SUBT)
461 p_data = ConvertSUBT(p_data);
464 /* Reset reference dts in case of discontinuity (ex: gather sout) */
465 if ( p_stream->i_entry_count == 0 || p_data->i_flags & BLOCK_FLAG_DISCONTINUITY )
467 p_stream->i_dts_start = p_data->i_dts;
468 p_stream->i_last_dts = p_data->i_dts;
469 p_stream->i_length_neg = 0;
472 if (p_stream->fmt.i_cat != SPU_ES) {
473 /* Fix length of the sample */
474 if (block_FifoCount(p_input->p_fifo) > 0) {
475 block_t *p_next = block_FifoShow(p_input->p_fifo);
476 if ( p_next->i_flags & BLOCK_FLAG_DISCONTINUITY )
477 { /* we have no way to know real length except by decoding */
478 if ( p_stream->fmt.i_cat == VIDEO_ES )
480 p_data->i_length = CLOCK_FREQ *
481 p_stream->fmt.video.i_frame_rate_base /
482 p_stream->fmt.video.i_frame_rate;
483 msg_Dbg( p_mux, "video track %d fixup to %"PRId64" for sample %u",
484 p_stream->i_track_id, p_data->i_length, p_stream->i_entry_count );
486 else if ( p_stream->fmt.i_cat == AUDIO_ES &&
487 p_stream->fmt.audio.i_rate &&
488 p_data->i_nb_samples )
490 p_data->i_length = CLOCK_FREQ * p_data->i_nb_samples /
491 p_stream->fmt.audio.i_rate;
492 msg_Dbg( p_mux, "audio track %d fixup to %"PRId64" for sample %u",
493 p_stream->i_track_id, p_data->i_length, p_stream->i_entry_count );
495 else if ( p_data->i_length <= 0 )
497 msg_Warn( p_mux, "unknown length for track %d sample %u",
498 p_stream->i_track_id, p_stream->i_entry_count );
499 p_data->i_length = 1;
504 int64_t i_diff = p_next->i_dts - p_data->i_dts;
505 if (i_diff < CLOCK_FREQ) /* protection */
506 p_data->i_length = i_diff;
509 if (p_data->i_length <= 0) {
510 msg_Warn(p_mux, "i_length <= 0");
511 p_stream->i_length_neg += p_data->i_length - 1;
512 p_data->i_length = 1;
513 } else if (p_stream->i_length_neg < 0) {
514 int64_t i_recover = __MIN(p_data->i_length / 4, - p_stream->i_length_neg);
516 p_data->i_length -= i_recover;
517 p_stream->i_length_neg += i_recover;
521 if (p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0) {
522 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
524 if (i_length <= 0) /* FIXME handle this broken case */
528 if (p_stream->entry[p_stream->i_entry_count-1].i_length <= 0)
529 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
532 /* add index entry */
533 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
534 e->i_pos = p_sys->i_pos;
535 e->i_size = p_data->i_buffer;
537 if ( p_data->i_dts > VLC_TS_INVALID && p_data->i_pts > p_data->i_dts )
539 e->i_pts_dts = p_data->i_pts - p_data->i_dts;
540 if ( !p_stream->b_hasbframes )
541 p_stream->b_hasbframes = true;
543 else e->i_pts_dts = 0;
545 e->i_length = p_data->i_length;
546 e->i_flags = p_data->i_flags;
548 p_stream->i_entry_count++;
549 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
550 if (p_stream->i_entry_count >= p_stream->i_entry_max - 1) {
551 p_stream->i_entry_max += 1000;
552 p_stream->entry = xrealloc(p_stream->entry,
553 p_stream->i_entry_max * sizeof(mp4_entry_t));
557 p_stream->i_duration += __MAX( 0, p_data->i_length );
558 p_sys->i_pos += p_data->i_buffer;
560 /* Save the DTS for SPU */
561 p_stream->i_last_dts = p_data->i_dts;
564 sout_AccessOutWrite(p_mux->p_access, p_data);
566 /* close subtitle with empty frame */
567 if (p_stream->fmt.i_cat == SPU_ES) {
568 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
570 if ( i_length != 0 && (p_data = block_Alloc(3)) ) {
572 msg_Dbg(p_mux, "writing an empty sub") ;
574 /* Append a idx entry */
575 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
576 e->i_pos = p_sys->i_pos;
582 /* XXX: No need to grow the entry here */
583 p_stream->i_entry_count++;
586 p_stream->i_last_dts += i_length;
589 p_data->i_dts = p_stream->i_last_dts;
590 p_data->i_dts = p_data->i_pts;
591 p_data->p_buffer[0] = 0;
592 p_data->p_buffer[1] = 1;
593 p_data->p_buffer[2] = ' ';
595 p_sys->i_pos += p_data->i_buffer;
597 sout_AccessOutWrite(p_mux->p_access, p_data);
600 /* Fix duration = current segment starttime + duration within */
601 p_stream->i_duration = p_stream->i_starttime + ( p_stream->i_last_dts - p_stream->i_dts_start );
605 /* Update the global segment/media duration */
606 for ( int i=0; i<p_sys->i_nb_streams; i++ )
608 if ( p_sys->pp_streams[i]->i_duration > p_sys->i_duration )
609 p_sys->i_duration = p_sys->pp_streams[i]->i_duration;
615 /*****************************************************************************
617 *****************************************************************************/
618 static block_t *ConvertSUBT(block_t *p_block)
620 p_block = block_Realloc(p_block, 2, p_block->i_buffer);
622 /* No trailling '\0' */
623 if (p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0')
626 p_block->p_buffer[0] = ((p_block->i_buffer - 2) >> 8)&0xff;
627 p_block->p_buffer[1] = ((p_block->i_buffer - 2) )&0xff;
632 static block_t *ConvertFromAnnexB(block_t *p_block)
634 uint8_t *last = p_block->p_buffer; /* Assume it starts with 0x00000001 */
635 uint8_t *dat = &p_block->p_buffer[4];
636 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
639 /* Replace the 4 bytes start code with 4 bytes size,
640 * FIXME are all startcodes 4 bytes ? (I don't think :(*/
642 while (dat < end - 4) {
643 if (!memcmp(dat, avc1_start_code, 4))
651 int i_size = dat - &last[4];
652 last[0] = (i_size >> 24)&0xff;
653 last[1] = (i_size >> 16)&0xff;
654 last[2] = (i_size >> 8)&0xff;
655 last[3] = (i_size )&0xff;
657 /* Skip blocks with SPS/PPS */
658 //if ((last[4]&0x1f) == 7 || (last[4]&0x1f) == 8)
659 // ; // FIXME Find a way to skip dat without frelling everything
666 static bo_t *GetESDS(mp4_stream_t *p_stream)
669 int64_t i_bitrate_avg = 0;
670 int64_t i_bitrate_max = 0;
672 /* Compute avg/max bitrate */
673 for (unsigned i = 0; i < p_stream->i_entry_count; i++) {
674 i_bitrate_avg += p_stream->entry[i].i_size;
675 if (p_stream->entry[i].i_length > 0) {
676 int64_t i_bitrate = INT64_C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
677 if (i_bitrate > i_bitrate_max)
678 i_bitrate_max = i_bitrate;
682 if (p_stream->i_duration > 0)
683 i_bitrate_avg = INT64_C(8000000) * i_bitrate_avg / p_stream->i_duration;
686 if (i_bitrate_max <= 1)
687 i_bitrate_max = 0x7fffffff;
690 int i_decoder_specific_info_size = (p_stream->fmt.i_extra > 0) ? 5 + p_stream->fmt.i_extra : 0;
692 esds = box_full_new("esds", 0, 0);
695 bo_add_descr(esds, 0x03, 3 + 5 + 13 + i_decoder_specific_info_size + 5 + 1);
696 bo_add_16be(esds, p_stream->i_track_id);
697 bo_add_8 (esds, 0x1f); // flags=0|streamPriority=0x1f
699 /* DecoderConfigDescr */
700 bo_add_descr(esds, 0x04, 13 + i_decoder_specific_info_size);
702 int i_object_type_indication;
703 switch(p_stream->fmt.i_codec)
706 i_object_type_indication = 0x20;
709 /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
710 i_object_type_indication = 0x65;
713 /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
714 i_object_type_indication = 0x6b;
717 /* FIXME for mpeg2-aac == 0x66->0x68 */
718 i_object_type_indication = 0x40;
721 i_object_type_indication =
722 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
725 i_object_type_indication = 0x00;
728 int i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
730 bo_add_8 (esds, i_object_type_indication);
731 bo_add_8 (esds, (i_stream_type << 2) | 1);
732 bo_add_24be(esds, 1024 * 1024); // bufferSizeDB
733 bo_add_32be(esds, i_bitrate_max); // maxBitrate
734 bo_add_32be(esds, i_bitrate_avg); // avgBitrate
736 if (p_stream->fmt.i_extra > 0) {
737 /* DecoderSpecificInfo */
738 bo_add_descr(esds, 0x05, p_stream->fmt.i_extra);
740 for (int i = 0; i < p_stream->fmt.i_extra; i++)
741 bo_add_8(esds, ((uint8_t*)p_stream->fmt.p_extra)[i]);
744 /* SL_Descr mandatory */
745 bo_add_descr(esds, 0x06, 1);
746 bo_add_8 (esds, 0x02); // sl_predefined
751 static bo_t *GetWaveTag(mp4_stream_t *p_stream)
756 wave = box_new("wave");
758 box = box_new("frma");
759 bo_add_fourcc(box, "mp4a");
760 box_gather(wave, box);
762 box = box_new("mp4a");
764 box_gather(wave, box);
766 box = GetESDS(p_stream);
767 box_gather(wave, box);
769 box = box_new("srcq");
770 bo_add_32be(box, 0x40);
771 box_gather(wave, box);
774 bo_add_32be(wave, 8); /* new empty box */
775 bo_add_32be(wave, 0); /* box label */
780 static bo_t *GetDamrTag(mp4_stream_t *p_stream)
784 damr = box_new("damr");
786 bo_add_fourcc(damr, "REFC");
789 if (p_stream->fmt.i_codec == VLC_CODEC_AMR_NB)
790 bo_add_16be(damr, 0x81ff); /* Mode set (all modes for AMR_NB) */
792 bo_add_16be(damr, 0x83ff); /* Mode set (all modes for AMR_WB) */
793 bo_add_16be(damr, 0x1); /* Mode change period (no restriction) */
798 static bo_t *GetD263Tag(void)
802 d263 = box_new("d263");
804 bo_add_fourcc(d263, "VLC ");
805 bo_add_16be(d263, 0xa);
811 static void hevcParseVPS(uint8_t * p_buffer, size_t i_buffer, uint8_t *general,
812 uint8_t * numTemporalLayer, bool * temporalIdNested)
814 const size_t i_decoded_nal_size = 512;
815 uint8_t p_dec_nal[i_decoded_nal_size];
816 size_t i_size = (i_buffer < i_decoded_nal_size)?i_buffer:i_decoded_nal_size;
817 nal_decode(p_buffer, p_dec_nal, i_size);
819 /* first two bytes are the NAL header, 3rd and 4th are:
820 vps_video_parameter_set_id(4)
821 vps_reserved_3_2bis(2)
822 vps_max_layers_minus1(6)
823 vps_max_sub_layers_minus1(3)
824 vps_temporal_id_nesting_flags
826 *numTemporalLayer = ((p_dec_nal[3] & 0x0E) >> 1) + 1;
827 *temporalIdNested = (bool)(p_dec_nal[3] & 0x01);
829 /* 5th & 6th are reserved 0xffff */
830 /* copy the first 12 bytes of profile tier */
831 memcpy(general, &p_dec_nal[6], 12);
834 static void hevcParseSPS(uint8_t * p_buffer, size_t i_buffer, uint8_t * chroma_idc,
835 uint8_t *bit_depth_luma_minus8, uint8_t *bit_depth_chroma_minus8)
837 const size_t i_decoded_nal_size = 512;
838 uint8_t p_dec_nal[i_decoded_nal_size];
839 size_t i_size = (i_buffer < i_decoded_nal_size)?i_buffer-2:i_decoded_nal_size;
840 nal_decode(p_buffer+2, p_dec_nal, i_size);
842 bs_init(&bs, p_dec_nal, i_size);
846 uint32_t sps_max_sublayer_minus1 = bs_read(&bs, 3);
848 /* skip nesting flag */
851 hevc_skip_profile_tiers_level(&bs, sps_max_sublayer_minus1);
854 (void) read_ue( &bs );
856 *chroma_idc = read_ue(&bs);
857 if (*chroma_idc == 3)
860 /* skip width and heigh */
861 (void) read_ue( &bs );
862 (void) read_ue( &bs );
864 uint32_t conformance_window_flag = bs_read1(&bs);
865 if (conformance_window_flag) {
872 *bit_depth_luma_minus8 = read_ue(&bs);
873 *bit_depth_chroma_minus8 = read_ue(&bs);
876 static bo_t *GetHvcCTag(mp4_stream_t *p_stream)
878 /* Generate hvcC box matching iso/iec 14496-15 3rd edition */
879 bo_t *hvcC = box_new("hvcC");
880 if(!p_stream->fmt.i_extra)
888 /* According to the specification HEVC stream can have
889 * 16 vps id and an "unlimited" number of sps and pps id using ue(v) id*/
890 struct nal p_vps[16], *p_sps = NULL, *p_pps = NULL, *p_sei = NULL,
892 size_t i_vps = 0, i_sps = 0, i_pps = 0, i_sei = 0;
893 uint8_t i_num_arrays = 0;
895 uint8_t * p_buffer = p_stream->fmt.p_extra;
896 size_t i_buffer = p_stream->fmt.i_extra;
898 uint8_t general_configuration[12] = {0};
899 uint8_t i_numTemporalLayer = 0;
900 uint8_t i_chroma_idc = 1;
901 uint8_t i_bit_depth_luma_minus8 = 0;
902 uint8_t i_bit_depth_chroma_minus8 = 0;
903 bool b_temporalIdNested = false;
905 uint32_t cmp = 0xFFFFFFFF;
907 /* look for start code 0X0000001 */
909 cmp = (cmp << 8) | *p_buffer;
910 if((cmp ^ UINT32_C(0x100)) <= UINT32_C(0xFF))
916 p_nal->i_buffer = p_buffer - p_nal->p_buffer - ((i_buffer)?3:0);
918 switch (*p_buffer & 0x72) {
921 p_nal = &p_vps[i_vps++];
922 p_nal->p_buffer = p_buffer;
923 /* Only keep the general profile from the first VPS
924 * if there are several (this shouldn't happen so soon) */
926 hevcParseVPS(p_buffer, i_buffer, general_configuration,
927 &i_numTemporalLayer, &b_temporalIdNested);
933 struct nal * p_tmp = realloc(p_sps, sizeof(struct nal) * (i_sps + 1));
937 p_nal = &p_sps[i_sps++];
938 p_nal->p_buffer = p_buffer;
939 if (i_sps == 1 && i_buffer > 15) {
940 /* Get Chroma_idc and bitdepths */
941 hevcParseSPS(p_buffer, i_buffer, &i_chroma_idc,
942 &i_bit_depth_luma_minus8, &i_bit_depth_chroma_minus8);
949 struct nal * p_tmp = realloc(p_pps, sizeof(struct nal) * (i_pps + 1));
953 p_nal = &p_pps[i_pps++];
954 p_nal->p_buffer = p_buffer;
962 struct nal * p_tmp = realloc(p_sei, sizeof(struct nal) * (i_sei + 1));
966 p_nal = &p_sei[i_sei++];
967 p_nal->p_buffer = p_buffer;
977 bo_add_8(hvcC, 0x01);
978 bo_add_mem(hvcC, 12, general_configuration);
979 /* Don't set min spatial segmentation */
980 bo_add_16be(hvcC, 0xF000);
981 /* Don't set parallelism type since segmentation isn't set */
982 bo_add_8(hvcC, 0xFC);
983 bo_add_8(hvcC, (0xFC | (i_chroma_idc & 0x03)));
984 bo_add_8(hvcC, (0xF8 | (i_bit_depth_luma_minus8 & 0x07)));
985 bo_add_8(hvcC, (0xF8 | (i_bit_depth_chroma_minus8 & 0x07)));
987 /* Don't set framerate */
988 bo_add_16be(hvcC, 0x0000);
989 /* Force NAL size of 4 bytes that replace the startcode */
990 bo_add_8(hvcC, (((i_numTemporalLayer & 0x07) << 3) |
991 (b_temporalIdNested << 2) | 0x03));
992 bo_add_8(hvcC, i_num_arrays);
996 /* Write VPS without forcing array_completeness */
998 bo_add_16be(hvcC, i_vps);
999 for (size_t i = 0; i < i_vps; i++) {
1001 bo_add_16be(hvcC, p_nal->i_buffer);
1002 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1007 /* Write SPS without forcing array_completeness */
1009 bo_add_16be(hvcC, i_sps);
1010 for (size_t i = 0; i < i_sps; i++) {
1012 bo_add_16be(hvcC, p_nal->i_buffer);
1013 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1018 /* Write PPS without forcing array_completeness */
1020 bo_add_16be(hvcC, i_pps);
1021 for (size_t i = 0; i < i_pps; i++) {
1023 bo_add_16be(hvcC, p_nal->i_buffer);
1024 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1029 /* Write SEI without forcing array_completeness */
1031 bo_add_16be(hvcC, i_sei);
1032 for (size_t i = 0; i < i_sei; i++) {
1034 bo_add_16be(hvcC, p_nal->i_buffer);
1035 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
1041 static bo_t *GetAvcCTag(mp4_stream_t *p_stream)
1044 uint8_t *p_sps = NULL;
1045 uint8_t *p_pps = NULL;
1049 if (p_stream->fmt.i_extra > 0) {
1050 /* FIXME: take into account multiple sps/pps */
1051 uint8_t *p_buffer = p_stream->fmt.p_extra;
1052 int i_buffer = p_stream->fmt.i_extra;
1054 while (i_buffer > 3) {
1055 while (memcmp(p_buffer, &avc1_start_code[1], 3)) {
1059 const int i_nal_type = p_buffer[3]&0x1f;
1060 int i_startcode = 0;
1062 for (int i_offset = 1; i_offset+2 < i_buffer ; i_offset++)
1063 if (!memcmp(&p_buffer[i_offset], &avc1_start_code[1], 3)) {
1064 /* we found another startcode */
1065 i_startcode = i_offset;
1066 while (p_buffer[i_startcode-1] == 0 && i_startcode > 0)
1071 int i_size = i_startcode ? i_startcode : i_buffer;
1073 if (i_nal_type == 7) {
1074 p_sps = &p_buffer[3];
1075 i_sps_size = i_size - 3;
1077 if (i_nal_type == 8) {
1078 p_pps = &p_buffer[3];
1079 i_pps_size = i_size - 3;
1086 /* FIXME use better value */
1087 avcC = box_new("avcC");
1088 bo_add_8(avcC, 1); /* configuration version */
1089 bo_add_8(avcC, i_sps_size ? p_sps[1] : 77);
1090 bo_add_8(avcC, i_sps_size ? p_sps[2] : 64);
1091 bo_add_8(avcC, i_sps_size ? p_sps[3] : 30); /* level, 5.1 */
1092 bo_add_8(avcC, 0xff); /* 0b11111100 | lengthsize = 0x11 */
1094 bo_add_8(avcC, 0xe0 | (i_sps_size > 0 ? 1 : 0)); /* 0b11100000 | sps_count */
1095 if (i_sps_size > 0) {
1096 bo_add_16be(avcC, i_sps_size);
1097 bo_add_mem(avcC, i_sps_size, p_sps);
1100 bo_add_8(avcC, (i_pps_size > 0 ? 1 : 0)); /* pps_count */
1101 if (i_pps_size > 0) {
1102 bo_add_16be(avcC, i_pps_size);
1103 bo_add_mem(avcC, i_pps_size, p_pps);
1109 /* TODO: No idea about these values */
1110 static bo_t *GetSVQ3Tag(mp4_stream_t *p_stream)
1112 bo_t *smi = box_new("SMI ");
1114 if (p_stream->fmt.i_extra > 0x4e) {
1115 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
1116 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
1118 while (p + 8 < p_end) {
1119 int i_size = GetDWBE(p);
1120 if (i_size <= 1) /* FIXME handle 1 as long size */
1122 if (!strncmp((const char *)&p[4], "SMI ", 4)) {
1123 bo_add_mem(smi, p_end - p - 8, &p[8]);
1130 /* Create a dummy one in fallback */
1131 bo_add_fourcc(smi, "SEQH");
1132 bo_add_32be(smi, 0x5);
1133 bo_add_32be(smi, 0xe2c0211d);
1134 bo_add_8(smi, 0xc0);
1139 static bo_t *GetUdtaTag(sout_mux_t *p_mux)
1141 sout_mux_sys_t *p_sys = p_mux->p_sys;
1142 bo_t *udta = box_new("udta");
1145 for (int i_track = 0; i_track < p_sys->i_nb_streams; i_track++) {
1146 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
1147 vlc_fourcc_t codec = p_stream->fmt.i_codec;
1149 if (codec == VLC_CODEC_MP4V || codec == VLC_CODEC_MP4A) {
1150 bo_t *box = box_new("\251req");
1152 bo_add_16be(box, sizeof("QuickTime 6.0 or greater") - 1);
1153 bo_add_16be(box, 0);
1154 bo_add_mem(box, sizeof("QuickTime 6.0 or greater") - 1,
1155 (uint8_t *)"QuickTime 6.0 or greater");
1156 box_gather(udta, box);
1163 bo_t *box = box_new("\251enc");
1165 bo_add_16be(box, sizeof(PACKAGE_STRING " stream output") - 1);
1166 bo_add_16be(box, 0);
1167 bo_add_mem(box, sizeof(PACKAGE_STRING " stream output") - 1,
1168 (uint8_t*)PACKAGE_STRING " stream output");
1169 box_gather(udta, box);
1173 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
1175 #define ADD_META_BOX(type, box_string) { \
1177 if (vlc_meta_Get(p_meta, vlc_meta_##type)) \
1178 box = box_new("\251" box_string); \
1180 bo_add_16be(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type))); \
1181 bo_add_16be(box, 0); \
1182 bo_add_mem(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type)), \
1183 (uint8_t*)(vlc_meta_Get(p_meta, vlc_meta_##type))); \
1184 box_gather(udta, box); \
1187 ADD_META_BOX(Title, "nam");
1188 ADD_META_BOX(Artist, "ART");
1189 ADD_META_BOX(Genre, "gen");
1190 ADD_META_BOX(Copyright, "cpy");
1191 ADD_META_BOX(Description, "des");
1192 ADD_META_BOX(Date, "day");
1193 ADD_META_BOX(URL, "url");
1200 static bo_t *GetSounBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
1202 sout_mux_sys_t *p_sys = p_mux->p_sys;
1203 bool b_descr = true;
1204 vlc_fourcc_t codec = p_stream->fmt.i_codec;
1206 vlc_fourcc_to_char(codec, fcc);
1208 if (codec == VLC_CODEC_MPGA) {
1211 memcpy(fcc, ".mp3", 4);
1213 memcpy(fcc, "mp4a", 4);
1216 bo_t *soun = box_new(fcc);
1217 for (int i = 0; i < 6; i++)
1218 bo_add_8(soun, 0); // reserved;
1219 bo_add_16be(soun, 1); // data-reference-index
1221 /* SoundDescription */
1222 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
1223 bo_add_16be(soun, 1); // version 1;
1225 bo_add_16be(soun, 0); // version 0;
1226 bo_add_16be(soun, 0); // revision level (0)
1227 bo_add_32be(soun, 0); // vendor
1229 bo_add_16be(soun, p_stream->fmt.audio.i_channels);
1231 bo_add_16be(soun, p_stream->fmt.audio.i_bitspersample ?
1232 p_stream->fmt.audio.i_bitspersample : 16);
1233 bo_add_16be(soun, -2); // compression id
1234 bo_add_16be(soun, 0); // packet size (0)
1235 bo_add_16be(soun, p_stream->fmt.audio.i_rate); // sampleratehi
1236 bo_add_16be(soun, 0); // sampleratelo
1238 /* Extended data for SoundDescription V1 */
1239 if (p_sys->b_mov && p_stream->fmt.i_codec == VLC_CODEC_MP4A) {
1240 /* samples per packet */
1241 bo_add_32be(soun, p_stream->fmt.audio.i_frame_length);
1242 bo_add_32be(soun, 1536); /* bytes per packet */
1243 bo_add_32be(soun, 2); /* bytes per frame */
1244 /* bytes per sample */
1245 bo_add_32be(soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1248 /* Add an ES Descriptor */
1252 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
1253 box = GetWaveTag(p_stream);
1254 else if (codec == VLC_CODEC_AMR_NB)
1255 box = GetDamrTag(p_stream);
1257 box = GetESDS(p_stream);
1258 box_gather(soun, box);
1264 static bo_t *GetVideBox(mp4_stream_t *p_stream)
1268 switch(p_stream->fmt.i_codec)
1270 case VLC_CODEC_MP4V:
1271 case VLC_CODEC_MPGV: memcpy(fcc, "mp4v", 4); break;
1272 case VLC_CODEC_MJPG: memcpy(fcc, "mjpa", 4); break;
1273 case VLC_CODEC_SVQ1: memcpy(fcc, "SVQ1", 4); break;
1274 case VLC_CODEC_SVQ3: memcpy(fcc, "SVQ3", 4); break;
1275 case VLC_CODEC_H263: memcpy(fcc, "s263", 4); break;
1276 case VLC_CODEC_H264: memcpy(fcc, "avc1", 4); break;
1277 case VLC_CODEC_HEVC: memcpy(fcc, "hvc1", 4); break;
1278 case VLC_CODEC_YV12: memcpy(fcc, "yv12", 4); break;
1279 case VLC_CODEC_YUYV: memcpy(fcc, "yuy2", 4); break;
1281 vlc_fourcc_to_char(p_stream->fmt.i_codec, fcc);
1285 bo_t *vide = box_new(fcc);
1286 for (int i = 0; i < 6; i++)
1287 bo_add_8(vide, 0); // reserved;
1288 bo_add_16be(vide, 1); // data-reference-index
1290 bo_add_16be(vide, 0); // predefined;
1291 bo_add_16be(vide, 0); // reserved;
1292 for (int i = 0; i < 3; i++)
1293 bo_add_32be(vide, 0); // predefined;
1295 bo_add_16be(vide, p_stream->fmt.video.i_width); // i_width
1296 bo_add_16be(vide, p_stream->fmt.video.i_height); // i_height
1298 bo_add_32be(vide, 0x00480000); // h 72dpi
1299 bo_add_32be(vide, 0x00480000); // v 72dpi
1301 bo_add_32be(vide, 0); // data size, always 0
1302 bo_add_16be(vide, 1); // frames count per sample
1305 for (int i = 0; i < 32; i++)
1308 bo_add_16be(vide, 0x18); // depth
1309 bo_add_16be(vide, 0xffff); // predefined
1311 /* add an ES Descriptor */
1312 switch(p_stream->fmt.i_codec)
1314 case VLC_CODEC_MP4V:
1315 case VLC_CODEC_MPGV:
1316 box_gather(vide, GetESDS(p_stream));
1319 case VLC_CODEC_H263:
1320 box_gather(vide, GetD263Tag());
1323 case VLC_CODEC_SVQ3:
1324 box_gather(vide, GetSVQ3Tag(p_stream));
1327 case VLC_CODEC_H264:
1328 box_gather(vide, GetAvcCTag(p_stream));
1331 case VLC_CODEC_HEVC:
1332 box_gather(vide, GetHvcCTag(p_stream));
1339 static bo_t *GetTextBox(void)
1341 bo_t *text = box_new("text");
1343 for (int i = 0; i < 6; i++)
1344 bo_add_8(text, 0); // reserved;
1345 bo_add_16be(text, 1); // data-reference-index
1347 bo_add_32be(text, 0); // display flags
1348 bo_add_32be(text, 0); // justification
1349 for (int i = 0; i < 3; i++)
1350 bo_add_16be(text, 0); // back ground color
1352 bo_add_16be(text, 0); // box text
1353 bo_add_16be(text, 0); // box text
1354 bo_add_16be(text, 0); // box text
1355 bo_add_16be(text, 0); // box text
1357 bo_add_64be(text, 0); // reserved
1358 for (int i = 0; i < 3; i++)
1359 bo_add_16be(text, 0xff); // foreground color
1362 bo_add_mem(text, 9, (uint8_t*)"Helvetica");
1367 static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
1369 sout_mux_sys_t *p_sys = p_mux->p_sys;
1371 /* sample description */
1372 bo_t *stsd = box_full_new("stsd", 0, 0);
1373 bo_add_32be(stsd, 1);
1374 if (p_stream->fmt.i_cat == AUDIO_ES)
1375 box_gather(stsd, GetSounBox(p_mux, p_stream));
1376 else if (p_stream->fmt.i_cat == VIDEO_ES)
1377 box_gather(stsd, GetVideBox(p_stream));
1378 else if (p_stream->fmt.i_cat == SPU_ES)
1379 box_gather(stsd, GetTextBox());
1381 /* chunk offset table */
1383 if (p_sys->i_pos >= (((uint64_t)0x1) << 32)) {
1384 /* 64 bits version */
1385 p_stream->b_stco64 = true;
1386 stco = box_full_new("co64", 0, 0);
1388 /* 32 bits version */
1389 p_stream->b_stco64 = false;
1390 stco = box_full_new("stco", 0, 0);
1392 bo_add_32be(stco, 0); // entry-count (fixed latter)
1394 /* sample to chunk table */
1395 bo_t *stsc = box_full_new("stsc", 0, 0);
1396 bo_add_32be(stsc, 0); // entry-count (fixed latter)
1398 unsigned i_chunk = 0;
1399 unsigned i_stsc_last_val = 0, i_stsc_entries = 0;
1400 for (unsigned i = 0; i < p_stream->i_entry_count; i_chunk++) {
1401 mp4_entry_t *entry = p_stream->entry;
1404 if (p_stream->b_stco64)
1405 bo_add_64be(stco, entry[i].i_pos);
1407 bo_add_32be(stco, entry[i].i_pos);
1409 for (; i < p_stream->i_entry_count; i++)
1410 if (i >= p_stream->i_entry_count - 1 ||
1411 entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
1416 /* Add entry to the stsc table */
1417 if (i_stsc_last_val != i - i_first) {
1418 bo_add_32be(stsc, 1 + i_chunk); // first-chunk
1419 bo_add_32be(stsc, i - i_first) ; // samples-per-chunk
1420 bo_add_32be(stsc, 1); // sample-descr-index
1421 i_stsc_last_val = i - i_first;
1426 /* Fix stco entry count */
1427 bo_fix_32be(stco, 12, i_chunk);
1428 msg_Dbg(p_mux, "created %d chunks (stco)", i_chunk);
1430 /* Fix stsc entry count */
1431 bo_fix_32be(stsc, 12, i_stsc_entries );
1434 bo_t *stts = box_full_new("stts", 0, 0);
1435 bo_add_32be(stts, 0); // entry-count (fixed latter)
1437 uint32_t i_timescale;
1438 if (p_stream->fmt.i_cat == AUDIO_ES)
1439 i_timescale = p_stream->fmt.audio.i_rate;
1441 i_timescale = CLOCK_FREQ;
1443 unsigned i_index = 0;
1444 for (unsigned i = 0; i < p_stream->i_entry_count; i_index++) {
1446 mtime_t i_delta = p_stream->entry[i].i_length;
1448 for (; i < p_stream->i_entry_count; ++i)
1449 if (i == p_stream->i_entry_count || p_stream->entry[i].i_length != i_delta)
1452 bo_add_32be(stts, i - i_first); // sample-count
1453 bo_add_32be(stts, i_delta * i_timescale / CLOCK_FREQ ); // sample-delta
1455 bo_fix_32be(stts, 12, i_index);
1457 /* composition time handling */
1459 if ( p_stream->b_hasbframes && (ctts = box_full_new("ctts", 0, 0)) )
1461 bo_add_32be(ctts, 0);
1463 for (unsigned i = 0; i < p_stream->i_entry_count; i_index++)
1466 mtime_t i_offset = p_stream->entry[i].i_pts_dts;
1468 for (; i < p_stream->i_entry_count; ++i)
1469 if (i == p_stream->i_entry_count || p_stream->entry[i].i_pts_dts != i_offset)
1472 bo_add_32be(ctts, i - i_first); // sample-count
1473 bo_add_32be(ctts, i_offset * i_timescale / CLOCK_FREQ ); // sample-offset
1475 bo_fix_32be(ctts, 12, i_index);
1478 bo_t *stsz = box_full_new("stsz", 0, 0);
1480 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1483 i_size = p_stream->entry[i].i_size;
1484 else if ( p_stream->entry[i].i_size != i_size )
1490 bo_add_32be(stsz, i_size); // sample-size
1491 bo_add_32be(stsz, p_stream->i_entry_count); // sample-count
1492 if ( i_size == 0 ) // all samples have different size
1494 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1495 bo_add_32be(stsz, p_stream->entry[i].i_size); // sample-size
1498 /* create stss table */
1501 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1502 if (p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I) {
1504 stss = box_full_new("stss", 0, 0);
1505 bo_add_32be(stss, 0); /* fixed later */
1507 bo_add_32be(stss, 1 + i);
1512 bo_fix_32be(stss, 12, i_index);
1514 /* Now gather all boxes into stbl */
1515 bo_t *stbl = box_new("stbl");
1517 box_gather(stbl, stsd);
1518 box_gather(stbl, stts);
1520 box_gather(stbl, stss);
1522 box_gather(stbl, ctts);
1523 box_gather(stbl, stsc);
1524 box_gather(stbl, stsz);
1525 p_stream->i_stco_pos = stbl->len + 16;
1526 box_gather(stbl, stco);
1531 static int64_t get_timestamp(void);
1533 static void matrix_apply_rotation(es_format_t *fmt, uint32_t mvhd_matrix[9])
1535 enum video_orientation_t orientation = ORIENT_NORMAL;
1536 if (fmt->i_cat == VIDEO_ES)
1537 orientation = fmt->video.orientation;
1539 #define ATAN(a, b) do { mvhd_matrix[1] = (a) << 16; \
1540 mvhd_matrix[0] = (b) << 16; \
1543 switch (orientation) {
1544 case ORIENT_ROTATED_90: ATAN( 1, 0); break;
1545 case ORIENT_ROTATED_180: ATAN( 0, -1); break;
1546 case ORIENT_ROTATED_270: ATAN( -1, 0); break;
1547 default: ATAN( 0, 1); break;
1550 mvhd_matrix[3] = mvhd_matrix[0] ? 0 : 0x10000;
1551 mvhd_matrix[4] = mvhd_matrix[1] ? 0 : 0x10000;
1554 static bo_t *GetMoovBox(sout_mux_t *p_mux)
1556 sout_mux_sys_t *p_sys = p_mux->p_sys;
1560 uint32_t i_movie_timescale = 90000;
1561 int64_t i_movie_duration = 0;
1562 int64_t i_timestamp = get_timestamp();
1564 moov = box_new("moov");
1566 /* Create general info */
1567 for (int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
1568 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1569 i_movie_duration = __MAX(i_movie_duration, p_stream->i_duration);
1571 msg_Dbg(p_mux, "movie duration %"PRId64"s", i_movie_duration / CLOCK_FREQ);
1573 i_movie_duration = i_movie_duration * i_movie_timescale / CLOCK_FREQ;
1575 /* *** add /moov/mvhd *** */
1576 if (!p_sys->b_64_ext) {
1577 mvhd = box_full_new("mvhd", 0, 0);
1578 bo_add_32be(mvhd, i_timestamp); // creation time
1579 bo_add_32be(mvhd, i_timestamp); // modification time
1580 bo_add_32be(mvhd, i_movie_timescale); // timescale
1581 bo_add_32be(mvhd, i_movie_duration); // duration
1583 mvhd = box_full_new("mvhd", 1, 0);
1584 bo_add_64be(mvhd, i_timestamp); // creation time
1585 bo_add_64be(mvhd, i_timestamp); // modification time
1586 bo_add_32be(mvhd, i_movie_timescale); // timescale
1587 bo_add_64be(mvhd, i_movie_duration); // duration
1589 bo_add_32be(mvhd, 0x10000); // rate
1590 bo_add_16be(mvhd, 0x100); // volume
1591 bo_add_16be(mvhd, 0); // reserved
1592 for (int i = 0; i < 2; i++)
1593 bo_add_32be(mvhd, 0); // reserved
1595 uint32_t mvhd_matrix[9] = { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1597 for (int i = 0; i < 9; i++)
1598 bo_add_32be(mvhd, mvhd_matrix[i]);// matrix
1599 for (int i = 0; i < 6; i++)
1600 bo_add_32be(mvhd, 0); // pre-defined
1602 /* Next available track id */
1603 bo_add_32be(mvhd, p_sys->i_nb_streams + 1); // next-track-id
1605 box_gather(moov, mvhd);
1607 for (int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
1608 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1610 uint32_t i_timescale;
1611 if (p_stream->fmt.i_cat == AUDIO_ES)
1612 i_timescale = p_stream->fmt.audio.i_rate;
1614 i_timescale = CLOCK_FREQ;
1616 /* *** add /moov/trak *** */
1617 bo_t *trak = box_new("trak");
1619 /* *** add /moov/trak/tkhd *** */
1621 if (!p_sys->b_64_ext) {
1623 tkhd = box_full_new("tkhd", 0, 0x0f);
1625 tkhd = box_full_new("tkhd", 0, 1);
1627 bo_add_32be(tkhd, i_timestamp); // creation time
1628 bo_add_32be(tkhd, i_timestamp); // modification time
1629 bo_add_32be(tkhd, p_stream->i_track_id);
1630 bo_add_32be(tkhd, 0); // reserved 0
1631 bo_add_32be(tkhd, p_stream->i_duration *
1632 (int64_t)i_movie_timescale / CLOCK_FREQ); // duration
1635 tkhd = box_full_new("tkhd", 1, 0x0f);
1637 tkhd = box_full_new("tkhd", 1, 1);
1639 bo_add_64be(tkhd, i_timestamp); // creation time
1640 bo_add_64be(tkhd, i_timestamp); // modification time
1641 bo_add_32be(tkhd, p_stream->i_track_id);
1642 bo_add_32be(tkhd, 0); // reserved 0
1643 bo_add_64be(tkhd, p_stream->i_duration *
1644 (int64_t)i_movie_timescale / CLOCK_FREQ); // duration
1647 for (int i = 0; i < 2; i++)
1648 bo_add_32be(tkhd, 0); // reserved
1649 bo_add_16be(tkhd, 0); // layer
1650 bo_add_16be(tkhd, 0); // pre-defined
1652 bo_add_16be(tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0);
1653 bo_add_16be(tkhd, 0); // reserved
1654 matrix_apply_rotation(&p_stream->fmt, mvhd_matrix);
1655 for (int i = 0; i < 9; i++)
1656 bo_add_32be(tkhd, mvhd_matrix[i]); // matrix
1657 if (p_stream->fmt.i_cat == AUDIO_ES) {
1658 bo_add_32be(tkhd, 0); // width (presentation)
1659 bo_add_32be(tkhd, 0); // height(presentation)
1660 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
1661 int i_width = p_stream->fmt.video.i_width << 16;
1662 if (p_stream->fmt.video.i_sar_num > 0 && p_stream->fmt.video.i_sar_den > 0) {
1663 i_width = (int64_t)p_stream->fmt.video.i_sar_num *
1664 ((int64_t)p_stream->fmt.video.i_width << 16) /
1665 p_stream->fmt.video.i_sar_den;
1667 // width (presentation)
1668 bo_add_32be(tkhd, i_width);
1669 // height(presentation)
1670 bo_add_32be(tkhd, p_stream->fmt.video.i_height << 16);
1672 int i_width = 320 << 16;
1674 for (int i = 0; i < p_sys->i_nb_streams; i++) {
1675 mp4_stream_t *tk = p_sys->pp_streams[i];
1676 if (tk->fmt.i_cat == VIDEO_ES) {
1677 if (tk->fmt.video.i_sar_num > 0 &&
1678 tk->fmt.video.i_sar_den > 0)
1679 i_width = (int64_t)tk->fmt.video.i_sar_num *
1680 ((int64_t)tk->fmt.video.i_width << 16) /
1681 tk->fmt.video.i_sar_den;
1683 i_width = tk->fmt.video.i_width << 16;
1684 i_height = tk->fmt.video.i_height;
1688 bo_add_32be(tkhd, i_width); // width (presentation)
1689 bo_add_32be(tkhd, i_height << 16); // height(presentation)
1692 box_gather(trak, tkhd);
1694 /* *** add /moov/trak/edts and elst */
1695 bo_t *edts = box_new("edts");
1696 bo_t *elst = box_full_new("elst", p_sys->b_64_ext ? 1 : 0, 0);
1697 if (p_stream->i_starttime > 0) {
1698 bo_add_32be(elst, 2);
1700 if (p_sys->b_64_ext) {
1701 bo_add_64be(elst, p_stream->i_starttime *
1702 i_movie_timescale / CLOCK_FREQ);
1703 bo_add_64be(elst, -1);
1705 bo_add_32be(elst, p_stream->i_starttime *
1706 i_movie_timescale / CLOCK_FREQ);
1707 bo_add_32be(elst, -1);
1709 bo_add_16be(elst, 1);
1710 bo_add_16be(elst, 0);
1712 bo_add_32be(elst, 1);
1714 if (p_sys->b_64_ext) {
1715 bo_add_64be(elst, p_stream->i_duration *
1716 i_movie_timescale / CLOCK_FREQ);
1717 bo_add_64be(elst, 0);
1719 bo_add_32be(elst, p_stream->i_duration *
1720 i_movie_timescale / CLOCK_FREQ);
1721 bo_add_32be(elst, 0);
1723 bo_add_16be(elst, 1);
1724 bo_add_16be(elst, 0);
1726 box_gather(edts, elst);
1727 box_gather(trak, edts);
1729 /* *** add /moov/trak/mdia *** */
1730 bo_t *mdia = box_new("mdia");
1734 if (!p_sys->b_64_ext) {
1735 mdhd = box_full_new("mdhd", 0, 0);
1736 bo_add_32be(mdhd, i_timestamp); // creation time
1737 bo_add_32be(mdhd, i_timestamp); // modification time
1738 bo_add_32be(mdhd, i_timescale); // timescale
1739 bo_add_32be(mdhd, p_stream->i_duration * (int64_t)i_timescale /
1740 CLOCK_FREQ); // duration
1742 mdhd = box_full_new("mdhd", 1, 0);
1743 bo_add_64be(mdhd, i_timestamp); // creation time
1744 bo_add_64be(mdhd, i_timestamp); // modification time
1745 bo_add_32be(mdhd, i_timescale); // timescale
1746 bo_add_64be(mdhd, p_stream->i_duration * (int64_t)i_timescale /
1747 CLOCK_FREQ); // duration
1750 if (p_stream->fmt.psz_language) {
1751 char *psz = p_stream->fmt.psz_language;
1752 const iso639_lang_t *pl = NULL;
1753 uint16_t lang = 0x0;
1755 if (strlen(psz) == 2)
1756 pl = GetLang_1(psz);
1757 else if (strlen(psz) == 3) {
1758 pl = GetLang_2B(psz);
1759 if (!strcmp(pl->psz_iso639_1, "??"))
1760 pl = GetLang_2T(psz);
1763 if (pl && strcmp(pl->psz_iso639_1, "??"))
1764 lang = ((pl->psz_iso639_2T[0] - 0x60) << 10) |
1765 ((pl->psz_iso639_2T[1] - 0x60) << 5) |
1766 ((pl->psz_iso639_2T[2] - 0x60));
1767 bo_add_16be(mdhd, lang); // language
1769 bo_add_16be(mdhd, 0 ); // language
1770 bo_add_16be(mdhd, 0 ); // predefined
1771 box_gather(mdia, mdhd);
1773 /* handler reference */
1774 bo_t *hdlr = box_full_new("hdlr", 0, 0);
1777 bo_add_fourcc(hdlr, "mhlr"); // media handler
1779 bo_add_32be(hdlr, 0);
1781 if (p_stream->fmt.i_cat == AUDIO_ES)
1782 bo_add_fourcc(hdlr, "soun");
1783 else if (p_stream->fmt.i_cat == VIDEO_ES)
1784 bo_add_fourcc(hdlr, "vide");
1785 else if (p_stream->fmt.i_cat == SPU_ES)
1786 bo_add_fourcc(hdlr, "text");
1788 bo_add_32be(hdlr, 0); // reserved
1789 bo_add_32be(hdlr, 0); // reserved
1790 bo_add_32be(hdlr, 0); // reserved
1793 bo_add_8(hdlr, 12); /* Pascal string for .mov */
1795 if (p_stream->fmt.i_cat == AUDIO_ES)
1796 bo_add_mem(hdlr, 12, (uint8_t*)"SoundHandler");
1797 else if (p_stream->fmt.i_cat == VIDEO_ES)
1798 bo_add_mem(hdlr, 12, (uint8_t*)"VideoHandler");
1800 bo_add_mem(hdlr, 12, (uint8_t*)"Text Handler");
1803 bo_add_8(hdlr, 0); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
1805 box_gather(mdia, hdlr);
1808 bo_t *minf = box_new("minf");
1811 if (p_stream->fmt.i_cat == AUDIO_ES) {
1814 smhd = box_full_new("smhd", 0, 0);
1815 bo_add_16be(smhd, 0); // balance
1816 bo_add_16be(smhd, 0); // reserved
1818 box_gather(minf, smhd);
1819 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
1822 vmhd = box_full_new("vmhd", 0, 1);
1823 bo_add_16be(vmhd, 0); // graphicsmode
1824 for (int i = 0; i < 3; i++)
1825 bo_add_16be(vmhd, 0); // opcolor
1827 box_gather(minf, vmhd);
1828 } else if (p_stream->fmt.i_cat == SPU_ES) {
1829 bo_t *gmhd = box_new("gmhd");
1830 bo_t *gmin = box_full_new("gmin", 0, 1);
1832 bo_add_16be(gmin, 0); // graphicsmode
1833 for (int i = 0; i < 3; i++)
1834 bo_add_16be(gmin, 0); // opcolor
1835 bo_add_16be(gmin, 0); // balance
1836 bo_add_16be(gmin, 0); // reserved
1838 box_gather(gmhd, gmin);
1840 box_gather(minf, gmhd);
1844 bo_t *dinf = box_new("dinf");
1845 bo_t *dref = box_full_new("dref", 0, 0);
1846 bo_add_32be(dref, 1);
1847 bo_t *url = box_full_new("url ", 0, 0x01);
1848 box_gather(dref, url);
1849 box_gather(dinf, dref);
1851 /* append dinf to mdia */
1852 box_gather(minf, dinf);
1855 bo_t *stbl = GetStblBox(p_mux, p_stream);
1857 /* append stbl to minf */
1858 p_stream->i_stco_pos += minf->len;
1859 box_gather(minf, stbl);
1861 /* append minf to mdia */
1862 p_stream->i_stco_pos += mdia->len;
1863 box_gather(mdia, minf);
1865 /* append mdia to trak */
1866 p_stream->i_stco_pos += trak->len;
1867 box_gather(trak, mdia);
1869 /* append trak to moov */
1870 p_stream->i_stco_pos += moov->len;
1871 box_gather(moov, trak);
1874 /* Add user data tags */
1875 box_gather(moov, GetUdtaTag(p_mux));
1881 /****************************************************************************/
1883 static void bo_init(bo_t *p_bo)
1886 p_bo->b = block_Alloc(1024);
1889 static void bo_add_8(bo_t *p_bo, uint8_t i)
1891 if (p_bo->len >= p_bo->b->i_buffer)
1892 p_bo->b = block_Realloc(p_bo->b, 0, p_bo->b->i_buffer + 1024);
1894 p_bo->b->p_buffer[p_bo->len++] = i;
1897 static void bo_add_16be(bo_t *p_bo, uint16_t i)
1899 bo_add_8(p_bo, ((i >> 8) &0xff));
1900 bo_add_8(p_bo, i &0xff);
1903 static void bo_add_24be(bo_t *p_bo, uint32_t i)
1905 bo_add_8(p_bo, ((i >> 16) &0xff));
1906 bo_add_8(p_bo, ((i >> 8) &0xff));
1907 bo_add_8(p_bo, ( i &0xff));
1909 static void bo_add_32be(bo_t *p_bo, uint32_t i)
1911 bo_add_16be(p_bo, ((i >> 16) &0xffff));
1912 bo_add_16be(p_bo, i &0xffff);
1915 static void bo_fix_32be (bo_t *p_bo, int i_pos, uint32_t i)
1917 p_bo->b->p_buffer[i_pos ] = (i >> 24)&0xff;
1918 p_bo->b->p_buffer[i_pos + 1] = (i >> 16)&0xff;
1919 p_bo->b->p_buffer[i_pos + 2] = (i >> 8)&0xff;
1920 p_bo->b->p_buffer[i_pos + 3] = (i )&0xff;
1923 static void bo_add_64be(bo_t *p_bo, uint64_t i)
1925 bo_add_32be(p_bo, ((i >> 32) &0xffffffff));
1926 bo_add_32be(p_bo, i &0xffffffff);
1929 static void bo_add_fourcc(bo_t *p_bo, const char *fcc)
1931 bo_add_8(p_bo, fcc[0]);
1932 bo_add_8(p_bo, fcc[1]);
1933 bo_add_8(p_bo, fcc[2]);
1934 bo_add_8(p_bo, fcc[3]);
1937 static void bo_add_mem(bo_t *p_bo, int i_size, uint8_t *p_mem)
1939 for (int i = 0; i < i_size; i++)
1940 bo_add_8(p_bo, p_mem[i]);
1943 static void bo_add_descr(bo_t *p_bo, uint8_t tag, uint32_t size)
1945 bo_add_8(p_bo, tag);
1946 for (int i = 3; i>0; i--)
1947 bo_add_8(p_bo, (size>>(7*i)) | 0x80);
1948 bo_add_8(p_bo, size & 0x7F);
1951 static bo_t *box_new(const char *fcc)
1953 bo_t *box = malloc(sizeof(*box));
1959 bo_add_32be (box, 0);
1960 bo_add_fourcc(box, fcc);
1965 static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f)
1967 bo_t *box = box_new(fcc);
1972 bo_add_24be (box, f);
1977 static void box_fix(bo_t *box)
1979 box->b->p_buffer[0] = box->len >> 24;
1980 box->b->p_buffer[1] = box->len >> 16;
1981 box->b->p_buffer[2] = box->len >> 8;
1982 box->b->p_buffer[3] = box->len;
1985 static void box_gather (bo_t *box, bo_t *box2)
1988 box->b = block_Realloc(box->b, 0, box->len + box2->len);
1989 memcpy(&box->b->p_buffer[box->len], box2->b->p_buffer, box2->len);
1990 box->len += box2->len;
1991 block_Release(box2->b);
1995 static void box_send(sout_mux_t *p_mux, bo_t *box)
1997 box->b->i_buffer = box->len;
1998 sout_AccessOutWrite(p_mux->p_access, box->b);
2002 static int64_t get_timestamp(void)
2004 int64_t i_timestamp = time(NULL);
2006 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
2007 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60