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;
114 /* for later stco fix-up (fast start files) */
123 struct sout_mux_sys_t
136 mp4_stream_t **pp_streams;
145 static void bo_init (bo_t *);
146 static void bo_add_8 (bo_t *, uint8_t);
147 static void bo_add_16be (bo_t *, uint16_t);
148 static void bo_add_24be (bo_t *, uint32_t);
149 static void bo_add_32be (bo_t *, uint32_t);
150 static void bo_add_64be (bo_t *, uint64_t);
151 static void bo_add_fourcc(bo_t *, const char *);
152 static void bo_add_mem (bo_t *, int , uint8_t *);
153 static void bo_add_descr(bo_t *, uint8_t , uint32_t);
155 static void bo_fix_32be (bo_t *, int , uint32_t);
157 static bo_t *box_new (const char *fcc);
158 static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f);
159 static void box_fix (bo_t *box);
160 static void box_gather (bo_t *box, bo_t *box2);
162 static void box_send(sout_mux_t *p_mux, bo_t *box);
164 static bo_t *GetMoovBox(sout_mux_t *p_mux);
166 static block_t *ConvertSUBT(block_t *);
167 static block_t *ConvertFromAnnexB(block_t *);
169 static const char avc1_start_code[4] = { 0, 0, 0, 1 };
171 /*****************************************************************************
173 *****************************************************************************/
174 static int Open(vlc_object_t *p_this)
176 sout_mux_t *p_mux = (sout_mux_t*)p_this;
177 sout_mux_sys_t *p_sys;
180 msg_Dbg(p_mux, "Mp4 muxer opened");
181 config_ChainParse(p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg);
183 p_mux->pf_control = Control;
184 p_mux->pf_addstream = AddStream;
185 p_mux->pf_delstream = DelStream;
187 p_mux->p_sys = p_sys = malloc(sizeof(sout_mux_sys_t));
191 p_sys->i_nb_streams = 0;
192 p_sys->pp_streams = NULL;
193 p_sys->i_mdat_pos = 0;
194 p_sys->b_mov = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "mov");
195 p_sys->b_3gp = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "3gp");
196 p_sys->i_dts_start = 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;
417 p_input->p_sys = p_stream;
419 msg_Dbg(p_mux, "adding input");
421 TAB_APPEND(p_sys->i_nb_streams, p_sys->pp_streams, p_stream);
425 /*****************************************************************************
427 *****************************************************************************/
428 static int DelStream(sout_mux_t *p_mux, sout_input_t *p_input)
431 msg_Dbg(p_mux, "removing input");
435 /*****************************************************************************
437 *****************************************************************************/
438 static int Mux(sout_mux_t *p_mux)
440 sout_mux_sys_t *p_sys = p_mux->p_sys;
443 int i_stream = sout_MuxGetStream(p_mux, 2, NULL);
447 sout_input_t *p_input = p_mux->pp_inputs[i_stream];
448 mp4_stream_t *p_stream = (mp4_stream_t*)p_input->p_sys;
452 p_data = block_FifoGet(p_input->p_fifo);
453 if (p_stream->fmt.i_codec == VLC_CODEC_H264 ||
454 p_stream->fmt.i_codec == VLC_CODEC_HEVC)
455 p_data = ConvertFromAnnexB(p_data);
456 else if (p_stream->fmt.i_codec == VLC_CODEC_SUBT)
457 p_data = ConvertSUBT(p_data);
460 if (p_stream->fmt.i_cat != SPU_ES) {
461 /* Fix length of the sample */
462 if (block_FifoCount(p_input->p_fifo) > 0) {
463 block_t *p_next = block_FifoShow(p_input->p_fifo);
464 int64_t i_diff = p_next->i_dts - p_data->i_dts;
466 if (i_diff < CLOCK_FREQ) /* protection */
467 p_data->i_length = i_diff;
469 if (p_data->i_length <= 0) {
470 msg_Warn(p_mux, "i_length <= 0");
471 p_stream->i_length_neg += p_data->i_length - 1;
472 p_data->i_length = 1;
473 } else if (p_stream->i_length_neg < 0) {
474 int64_t i_recover = __MIN(p_data->i_length / 4, - p_stream->i_length_neg);
476 p_data->i_length -= i_recover;
477 p_stream->i_length_neg += i_recover;
481 /* Save starting time */
482 if (p_stream->i_entry_count == 0) {
483 p_stream->i_dts_start = p_data->i_dts;
485 /* Update global dts_start */
486 if (p_sys->i_dts_start <= 0 || p_stream->i_dts_start < p_sys->i_dts_start)
487 p_sys->i_dts_start = p_stream->i_dts_start;
490 if (p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0) {
491 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
493 if (i_length <= 0) /* FIXME handle this broken case */
497 if (p_stream->entry[p_stream->i_entry_count-1].i_length <= 0)
498 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
501 /* add index entry */
502 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
503 e->i_pos = p_sys->i_pos;
504 e->i_size = p_data->i_buffer;
505 e->i_pts_dts = p_data->i_pts - p_data->i_dts;
506 if (e->i_pts_dts < 0)
508 e->i_length = p_data->i_length;
509 e->i_flags = p_data->i_flags;
511 p_stream->i_entry_count++;
512 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
513 if (p_stream->i_entry_count >= p_stream->i_entry_max - 1) {
514 p_stream->i_entry_max += 1000;
515 p_stream->entry = xrealloc(p_stream->entry,
516 p_stream->i_entry_max * sizeof(mp4_entry_t));
520 p_stream->i_duration = p_stream->i_last_dts - p_stream->i_dts_start + p_data->i_length;
521 p_sys->i_pos += p_data->i_buffer;
524 p_stream->i_last_dts = p_data->i_dts;
527 sout_AccessOutWrite(p_mux->p_access, p_data);
529 if (p_stream->fmt.i_cat == SPU_ES) {
530 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
534 msg_Dbg(p_mux, "writing an empty sub") ;
536 /* Append a idx entry */
537 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
538 e->i_pos = p_sys->i_pos;
544 /* XXX: No need to grow the entry here */
545 p_stream->i_entry_count++;
548 p_stream->i_last_dts += i_length;
551 p_data = block_Alloc(3);
552 p_data->p_buffer[0] = 0;
553 p_data->p_buffer[1] = 1;
554 p_data->p_buffer[2] = ' ';
556 p_sys->i_pos += p_data->i_buffer;
558 sout_AccessOutWrite(p_mux->p_access, p_data);
562 p_stream->i_duration = p_stream->i_last_dts - p_stream->i_dts_start;
569 /*****************************************************************************
571 *****************************************************************************/
572 static block_t *ConvertSUBT(block_t *p_block)
574 p_block = block_Realloc(p_block, 2, p_block->i_buffer);
576 /* No trailling '\0' */
577 if (p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0')
580 p_block->p_buffer[0] = ((p_block->i_buffer - 2) >> 8)&0xff;
581 p_block->p_buffer[1] = ((p_block->i_buffer - 2) )&0xff;
586 static block_t *ConvertFromAnnexB(block_t *p_block)
588 uint8_t *last = p_block->p_buffer; /* Assume it starts with 0x00000001 */
589 uint8_t *dat = &p_block->p_buffer[4];
590 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
593 /* Replace the 4 bytes start code with 4 bytes size,
594 * FIXME are all startcodes 4 bytes ? (I don't think :(*/
596 while (dat < end - 4) {
597 if (!memcmp(dat, avc1_start_code, 4))
605 int i_size = dat - &last[4];
606 last[0] = (i_size >> 24)&0xff;
607 last[1] = (i_size >> 16)&0xff;
608 last[2] = (i_size >> 8)&0xff;
609 last[3] = (i_size )&0xff;
611 /* Skip blocks with SPS/PPS */
612 //if ((last[4]&0x1f) == 7 || (last[4]&0x1f) == 8)
613 // ; // FIXME Find a way to skip dat without frelling everything
620 static bo_t *GetESDS(mp4_stream_t *p_stream)
623 int64_t i_bitrate_avg = 0;
624 int64_t i_bitrate_max = 0;
626 /* Compute avg/max bitrate */
627 for (unsigned i = 0; i < p_stream->i_entry_count; i++) {
628 i_bitrate_avg += p_stream->entry[i].i_size;
629 if (p_stream->entry[i].i_length > 0) {
630 int64_t i_bitrate = INT64_C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
631 if (i_bitrate > i_bitrate_max)
632 i_bitrate_max = i_bitrate;
636 if (p_stream->i_duration > 0)
637 i_bitrate_avg = INT64_C(8000000) * i_bitrate_avg / p_stream->i_duration;
640 if (i_bitrate_max <= 1)
641 i_bitrate_max = 0x7fffffff;
644 int i_decoder_specific_info_size = (p_stream->fmt.i_extra > 0) ? 5 + p_stream->fmt.i_extra : 0;
646 esds = box_full_new("esds", 0, 0);
649 bo_add_descr(esds, 0x03, 3 + 5 + 13 + i_decoder_specific_info_size + 5 + 1);
650 bo_add_16be(esds, p_stream->i_track_id);
651 bo_add_8 (esds, 0x1f); // flags=0|streamPriority=0x1f
653 /* DecoderConfigDescr */
654 bo_add_descr(esds, 0x04, 13 + i_decoder_specific_info_size);
656 int i_object_type_indication;
657 switch(p_stream->fmt.i_codec)
660 i_object_type_indication = 0x20;
663 /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
664 i_object_type_indication = 0x65;
667 /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
668 i_object_type_indication = 0x6b;
671 /* FIXME for mpeg2-aac == 0x66->0x68 */
672 i_object_type_indication = 0x40;
675 i_object_type_indication =
676 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
679 i_object_type_indication = 0x00;
682 int i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
684 bo_add_8 (esds, i_object_type_indication);
685 bo_add_8 (esds, (i_stream_type << 2) | 1);
686 bo_add_24be(esds, 1024 * 1024); // bufferSizeDB
687 bo_add_32be(esds, i_bitrate_max); // maxBitrate
688 bo_add_32be(esds, i_bitrate_avg); // avgBitrate
690 if (p_stream->fmt.i_extra > 0) {
691 /* DecoderSpecificInfo */
692 bo_add_descr(esds, 0x05, p_stream->fmt.i_extra);
694 for (int i = 0; i < p_stream->fmt.i_extra; i++)
695 bo_add_8(esds, ((uint8_t*)p_stream->fmt.p_extra)[i]);
698 /* SL_Descr mandatory */
699 bo_add_descr(esds, 0x06, 1);
700 bo_add_8 (esds, 0x02); // sl_predefined
705 static bo_t *GetWaveTag(mp4_stream_t *p_stream)
710 wave = box_new("wave");
712 box = box_new("frma");
713 bo_add_fourcc(box, "mp4a");
714 box_gather(wave, box);
716 box = box_new("mp4a");
718 box_gather(wave, box);
720 box = GetESDS(p_stream);
721 box_gather(wave, box);
723 box = box_new("srcq");
724 bo_add_32be(box, 0x40);
725 box_gather(wave, box);
728 bo_add_32be(wave, 8); /* new empty box */
729 bo_add_32be(wave, 0); /* box label */
734 static bo_t *GetDamrTag(mp4_stream_t *p_stream)
738 damr = box_new("damr");
740 bo_add_fourcc(damr, "REFC");
743 if (p_stream->fmt.i_codec == VLC_CODEC_AMR_NB)
744 bo_add_16be(damr, 0x81ff); /* Mode set (all modes for AMR_NB) */
746 bo_add_16be(damr, 0x83ff); /* Mode set (all modes for AMR_WB) */
747 bo_add_16be(damr, 0x1); /* Mode change period (no restriction) */
752 static bo_t *GetD263Tag(void)
756 d263 = box_new("d263");
758 bo_add_fourcc(d263, "VLC ");
759 bo_add_16be(d263, 0xa);
765 static void hevcParseVPS(uint8_t * p_buffer, size_t i_buffer, uint8_t *general,
766 uint8_t * numTemporalLayer, bool * temporalIdNested)
768 const size_t i_decoded_nal_size = 512;
769 uint8_t p_dec_nal[i_decoded_nal_size];
770 size_t i_size = (i_buffer < i_decoded_nal_size)?i_buffer:i_decoded_nal_size;
771 nal_decode(p_buffer, p_dec_nal, i_size);
773 /* first two bytes are the NAL header, 3rd and 4th are:
774 vps_video_parameter_set_id(4)
775 vps_reserved_3_2bis(2)
776 vps_max_layers_minus1(6)
777 vps_max_sub_layers_minus1(3)
778 vps_temporal_id_nesting_flags
780 *numTemporalLayer = ((p_dec_nal[3] & 0x0E) >> 1) + 1;
781 *temporalIdNested = (bool)(p_dec_nal[3] & 0x01);
783 /* 5th & 6th are reserved 0xffff */
784 /* copy the first 12 bytes of profile tier */
785 memcpy(general, &p_dec_nal[6], 12);
788 static void hevcParseSPS(uint8_t * p_buffer, size_t i_buffer, uint8_t * chroma_idc,
789 uint8_t *bit_depth_luma_minus8, uint8_t *bit_depth_chroma_minus8)
791 const size_t i_decoded_nal_size = 512;
792 uint8_t p_dec_nal[i_decoded_nal_size];
793 size_t i_size = (i_buffer < i_decoded_nal_size)?i_buffer-2:i_decoded_nal_size;
794 nal_decode(p_buffer+2, p_dec_nal, i_size);
796 bs_init(&bs, p_dec_nal, i_size);
800 uint32_t sps_max_sublayer_minus1 = bs_read(&bs, 3);
802 /* skip nesting flag */
805 hevc_skip_profile_tiers_level(&bs, sps_max_sublayer_minus1);
808 (void) read_ue( &bs );
810 *chroma_idc = read_ue(&bs);
811 if (*chroma_idc == 3)
814 /* skip width and heigh */
815 (void) read_ue( &bs );
816 (void) read_ue( &bs );
818 uint32_t conformance_window_flag = bs_read1(&bs);
819 if (conformance_window_flag) {
826 *bit_depth_luma_minus8 = read_ue(&bs);
827 *bit_depth_chroma_minus8 = read_ue(&bs);
830 static bo_t *GetHvcCTag(mp4_stream_t *p_stream)
832 /* Generate hvcC box matching iso/iec 14496-15 3rd edition */
833 bo_t *hvcC = box_new("hvcC");
834 if(!p_stream->fmt.i_extra)
842 /* According to the specification HEVC stream can have
843 * 16 vps id and an "unlimited" number of sps and pps id using ue(v) id*/
844 struct nal p_vps[16], *p_sps = NULL, *p_pps = NULL, *p_sei = NULL,
846 size_t i_vps = 0, i_sps = 0, i_pps = 0, i_sei = 0;
847 uint8_t i_num_arrays = 0;
849 uint8_t * p_buffer = p_stream->fmt.p_extra;
850 size_t i_buffer = p_stream->fmt.i_extra;
852 uint8_t general_configuration[12] = {0};
853 uint8_t i_numTemporalLayer;
854 uint8_t i_chroma_idc = 1;
855 uint8_t i_bit_depth_luma_minus8 = 0;
856 uint8_t i_bit_depth_chroma_minus8 = 0;
857 bool b_temporalIdNested;
859 uint32_t cmp = 0xFFFFFFFF;
861 /* look for start code 0X0000001 */
863 cmp = (cmp << 8) | *p_buffer;
864 if((cmp ^ UINT32_C(0x100)) <= UINT32_C(0xFF))
870 p_nal->i_buffer = p_buffer - p_nal->p_buffer - ((i_buffer)?3:0);
872 switch (*p_buffer & 0x72) {
875 p_nal = &p_vps[i_vps++];
876 p_nal->p_buffer = p_buffer;
877 /* Only keep the general profile from the first VPS
878 * if there are several (this shouldn't happen so soon) */
880 hevcParseVPS(p_buffer, i_buffer, general_configuration,
881 &i_numTemporalLayer, &b_temporalIdNested);
887 struct nal * p_tmp = realloc(p_sps, sizeof(struct nal) * (i_sps + 1));
891 p_nal = &p_sps[i_sps++];
892 p_nal->p_buffer = p_buffer;
893 if (i_sps == 1 && i_buffer > 15) {
894 /* Get Chroma_idc and bitdepths */
895 hevcParseSPS(p_buffer, i_buffer, &i_chroma_idc,
896 &i_bit_depth_luma_minus8, &i_bit_depth_chroma_minus8);
903 struct nal * p_tmp = realloc(p_pps, sizeof(struct nal) * (i_pps + 1));
907 p_nal = &p_pps[i_pps++];
908 p_nal->p_buffer = p_buffer;
916 struct nal * p_tmp = realloc(p_sei, sizeof(struct nal) * (i_sei + 1));
920 p_nal = &p_sei[i_sei++];
921 p_nal->p_buffer = p_buffer;
931 bo_add_8(hvcC, 0x01);
932 bo_add_mem(hvcC, 12, general_configuration);
933 /* Don't set min spatial segmentation */
934 bo_add_16be(hvcC, 0xF000);
935 /* Don't set parallelism type since segmentation isn't set */
936 bo_add_8(hvcC, 0xFC);
937 bo_add_8(hvcC, (0xFC | (i_chroma_idc & 0x03)));
938 bo_add_8(hvcC, (0xF8 | (i_bit_depth_luma_minus8 & 0x07)));
939 bo_add_8(hvcC, (0xF8 | (i_bit_depth_chroma_minus8 & 0x07)));
941 /* Don't set framerate */
942 bo_add_16be(hvcC, 0x0000);
943 /* Force NAL size of 4 bytes that replace the startcode */
944 bo_add_8(hvcC, (((i_numTemporalLayer & 0x07) << 3) |
945 (b_temporalIdNested << 2) | 0x03));
946 bo_add_8(hvcC, i_num_arrays);
950 /* Write VPS without forcing array_completeness */
952 bo_add_16be(hvcC, i_vps);
953 for (size_t i = 0; i < i_vps; i++) {
955 bo_add_16be(hvcC, p_nal->i_buffer);
956 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
961 /* Write SPS without forcing array_completeness */
963 bo_add_16be(hvcC, i_sps);
964 for (size_t i = 0; i < i_sps; i++) {
966 bo_add_16be(hvcC, p_nal->i_buffer);
967 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
972 /* Write PPS without forcing array_completeness */
974 bo_add_16be(hvcC, i_pps);
975 for (size_t i = 0; i < i_pps; i++) {
977 bo_add_16be(hvcC, p_nal->i_buffer);
978 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
983 /* Write SEI without forcing array_completeness */
985 bo_add_16be(hvcC, i_sei);
986 for (size_t i = 0; i < i_sei; i++) {
988 bo_add_16be(hvcC, p_nal->i_buffer);
989 bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
995 static bo_t *GetAvcCTag(mp4_stream_t *p_stream)
998 uint8_t *p_sps = NULL;
999 uint8_t *p_pps = NULL;
1003 if (p_stream->fmt.i_extra > 0) {
1004 /* FIXME: take into account multiple sps/pps */
1005 uint8_t *p_buffer = p_stream->fmt.p_extra;
1006 int i_buffer = p_stream->fmt.i_extra;
1008 while (i_buffer > 3) {
1009 while (memcmp(p_buffer, &avc1_start_code[1], 3)) {
1013 const int i_nal_type = p_buffer[3]&0x1f;
1014 int i_startcode = 0;
1016 for (int i_offset = 1; i_offset+2 < i_buffer ; i_offset++)
1017 if (!memcmp(&p_buffer[i_offset], &avc1_start_code[1], 3)) {
1018 /* we found another startcode */
1019 i_startcode = i_offset;
1020 while (p_buffer[i_startcode-1] == 0 && i_startcode > 0)
1025 int i_size = i_startcode ? i_startcode : i_buffer;
1027 if (i_nal_type == 7) {
1028 p_sps = &p_buffer[3];
1029 i_sps_size = i_size - 3;
1031 if (i_nal_type == 8) {
1032 p_pps = &p_buffer[3];
1033 i_pps_size = i_size - 3;
1040 /* FIXME use better value */
1041 avcC = box_new("avcC");
1042 bo_add_8(avcC, 1); /* configuration version */
1043 bo_add_8(avcC, i_sps_size ? p_sps[1] : 77);
1044 bo_add_8(avcC, i_sps_size ? p_sps[2] : 64);
1045 bo_add_8(avcC, i_sps_size ? p_sps[3] : 30); /* level, 5.1 */
1046 bo_add_8(avcC, 0xff); /* 0b11111100 | lengthsize = 0x11 */
1048 bo_add_8(avcC, 0xe0 | (i_sps_size > 0 ? 1 : 0)); /* 0b11100000 | sps_count */
1049 if (i_sps_size > 0) {
1050 bo_add_16be(avcC, i_sps_size);
1051 bo_add_mem(avcC, i_sps_size, p_sps);
1054 bo_add_8(avcC, (i_pps_size > 0 ? 1 : 0)); /* pps_count */
1055 if (i_pps_size > 0) {
1056 bo_add_16be(avcC, i_pps_size);
1057 bo_add_mem(avcC, i_pps_size, p_pps);
1063 /* TODO: No idea about these values */
1064 static bo_t *GetSVQ3Tag(mp4_stream_t *p_stream)
1066 bo_t *smi = box_new("SMI ");
1068 if (p_stream->fmt.i_extra > 0x4e) {
1069 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
1070 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
1072 while (p + 8 < p_end) {
1073 int i_size = GetDWBE(p);
1074 if (i_size <= 1) /* FIXME handle 1 as long size */
1076 if (!strncmp((const char *)&p[4], "SMI ", 4)) {
1077 bo_add_mem(smi, p_end - p - 8, &p[8]);
1084 /* Create a dummy one in fallback */
1085 bo_add_fourcc(smi, "SEQH");
1086 bo_add_32be(smi, 0x5);
1087 bo_add_32be(smi, 0xe2c0211d);
1088 bo_add_8(smi, 0xc0);
1093 static bo_t *GetUdtaTag(sout_mux_t *p_mux)
1095 sout_mux_sys_t *p_sys = p_mux->p_sys;
1096 bo_t *udta = box_new("udta");
1099 for (int i_track = 0; i_track < p_sys->i_nb_streams; i_track++) {
1100 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
1101 vlc_fourcc_t codec = p_stream->fmt.i_codec;
1103 if (codec == VLC_CODEC_MP4V || codec == VLC_CODEC_MP4A) {
1104 bo_t *box = box_new("\251req");
1106 bo_add_16be(box, sizeof("QuickTime 6.0 or greater") - 1);
1107 bo_add_16be(box, 0);
1108 bo_add_mem(box, sizeof("QuickTime 6.0 or greater") - 1,
1109 (uint8_t *)"QuickTime 6.0 or greater");
1110 box_gather(udta, box);
1117 bo_t *box = box_new("\251enc");
1119 bo_add_16be(box, sizeof(PACKAGE_STRING " stream output") - 1);
1120 bo_add_16be(box, 0);
1121 bo_add_mem(box, sizeof(PACKAGE_STRING " stream output") - 1,
1122 (uint8_t*)PACKAGE_STRING " stream output");
1123 box_gather(udta, box);
1127 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
1129 #define ADD_META_BOX(type, box_string) { \
1131 if (vlc_meta_Get(p_meta, vlc_meta_##type)) \
1132 box = box_new("\251" box_string); \
1134 bo_add_16be(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type))); \
1135 bo_add_16be(box, 0); \
1136 bo_add_mem(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type)), \
1137 (uint8_t*)(vlc_meta_Get(p_meta, vlc_meta_##type))); \
1138 box_gather(udta, box); \
1141 ADD_META_BOX(Title, "nam");
1142 ADD_META_BOX(Artist, "ART");
1143 ADD_META_BOX(Genre, "gen");
1144 ADD_META_BOX(Copyright, "cpy");
1145 ADD_META_BOX(Description, "des");
1146 ADD_META_BOX(Date, "day");
1147 ADD_META_BOX(URL, "url");
1154 static bo_t *GetSounBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
1156 sout_mux_sys_t *p_sys = p_mux->p_sys;
1157 bool b_descr = true;
1158 vlc_fourcc_t codec = p_stream->fmt.i_codec;
1160 vlc_fourcc_to_char(codec, fcc);
1162 if (codec == VLC_CODEC_MPGA) {
1165 memcpy(fcc, ".mp3", 4);
1167 memcpy(fcc, "mp4a", 4);
1170 bo_t *soun = box_new(fcc);
1171 for (int i = 0; i < 6; i++)
1172 bo_add_8(soun, 0); // reserved;
1173 bo_add_16be(soun, 1); // data-reference-index
1175 /* SoundDescription */
1176 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
1177 bo_add_16be(soun, 1); // version 1;
1179 bo_add_16be(soun, 0); // version 0;
1180 bo_add_16be(soun, 0); // revision level (0)
1181 bo_add_32be(soun, 0); // vendor
1183 bo_add_16be(soun, p_stream->fmt.audio.i_channels);
1185 bo_add_16be(soun, p_stream->fmt.audio.i_bitspersample ?
1186 p_stream->fmt.audio.i_bitspersample : 16);
1187 bo_add_16be(soun, -2); // compression id
1188 bo_add_16be(soun, 0); // packet size (0)
1189 bo_add_16be(soun, p_stream->fmt.audio.i_rate); // sampleratehi
1190 bo_add_16be(soun, 0); // sampleratelo
1192 /* Extended data for SoundDescription V1 */
1193 if (p_sys->b_mov && p_stream->fmt.i_codec == VLC_CODEC_MP4A) {
1194 /* samples per packet */
1195 bo_add_32be(soun, p_stream->fmt.audio.i_frame_length);
1196 bo_add_32be(soun, 1536); /* bytes per packet */
1197 bo_add_32be(soun, 2); /* bytes per frame */
1198 /* bytes per sample */
1199 bo_add_32be(soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
1202 /* Add an ES Descriptor */
1206 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
1207 box = GetWaveTag(p_stream);
1208 else if (codec == VLC_CODEC_AMR_NB)
1209 box = GetDamrTag(p_stream);
1211 box = GetESDS(p_stream);
1212 box_gather(soun, box);
1218 static bo_t *GetVideBox(mp4_stream_t *p_stream)
1222 switch(p_stream->fmt.i_codec)
1224 case VLC_CODEC_MP4V:
1225 case VLC_CODEC_MPGV: memcpy(fcc, "mp4v", 4); break;
1226 case VLC_CODEC_MJPG: memcpy(fcc, "mjpa", 4); break;
1227 case VLC_CODEC_SVQ1: memcpy(fcc, "SVQ1", 4); break;
1228 case VLC_CODEC_SVQ3: memcpy(fcc, "SVQ3", 4); break;
1229 case VLC_CODEC_H263: memcpy(fcc, "s263", 4); break;
1230 case VLC_CODEC_H264: memcpy(fcc, "avc1", 4); break;
1231 case VLC_CODEC_HEVC: memcpy(fcc, "hvc1", 4); break;
1232 case VLC_CODEC_YV12: memcpy(fcc, "yv12", 4); break;
1233 case VLC_CODEC_YUYV: memcpy(fcc, "yuy2", 4); break;
1235 vlc_fourcc_to_char(p_stream->fmt.i_codec, fcc);
1239 bo_t *vide = box_new(fcc);
1240 for (int i = 0; i < 6; i++)
1241 bo_add_8(vide, 0); // reserved;
1242 bo_add_16be(vide, 1); // data-reference-index
1244 bo_add_16be(vide, 0); // predefined;
1245 bo_add_16be(vide, 0); // reserved;
1246 for (int i = 0; i < 3; i++)
1247 bo_add_32be(vide, 0); // predefined;
1249 bo_add_16be(vide, p_stream->fmt.video.i_width); // i_width
1250 bo_add_16be(vide, p_stream->fmt.video.i_height); // i_height
1252 bo_add_32be(vide, 0x00480000); // h 72dpi
1253 bo_add_32be(vide, 0x00480000); // v 72dpi
1255 bo_add_32be(vide, 0); // data size, always 0
1256 bo_add_16be(vide, 1); // frames count per sample
1259 for (int i = 0; i < 32; i++)
1262 bo_add_16be(vide, 0x18); // depth
1263 bo_add_16be(vide, 0xffff); // predefined
1265 /* add an ES Descriptor */
1266 switch(p_stream->fmt.i_codec)
1268 case VLC_CODEC_MP4V:
1269 case VLC_CODEC_MPGV:
1270 box_gather(vide, GetESDS(p_stream));
1273 case VLC_CODEC_H263:
1274 box_gather(vide, GetD263Tag());
1277 case VLC_CODEC_SVQ3:
1278 box_gather(vide, GetSVQ3Tag(p_stream));
1281 case VLC_CODEC_H264:
1282 box_gather(vide, GetAvcCTag(p_stream));
1285 case VLC_CODEC_HEVC:
1286 box_gather(vide, GetHvcCTag(p_stream));
1293 static bo_t *GetTextBox(void)
1295 bo_t *text = box_new("text");
1297 for (int i = 0; i < 6; i++)
1298 bo_add_8(text, 0); // reserved;
1299 bo_add_16be(text, 1); // data-reference-index
1301 bo_add_32be(text, 0); // display flags
1302 bo_add_32be(text, 0); // justification
1303 for (int i = 0; i < 3; i++)
1304 bo_add_16be(text, 0); // back ground color
1306 bo_add_16be(text, 0); // box text
1307 bo_add_16be(text, 0); // box text
1308 bo_add_16be(text, 0); // box text
1309 bo_add_16be(text, 0); // box text
1311 bo_add_64be(text, 0); // reserved
1312 for (int i = 0; i < 3; i++)
1313 bo_add_16be(text, 0xff); // foreground color
1316 bo_add_mem(text, 9, (uint8_t*)"Helvetica");
1321 static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
1323 sout_mux_sys_t *p_sys = p_mux->p_sys;
1325 /* sample description */
1326 bo_t *stsd = box_full_new("stsd", 0, 0);
1327 bo_add_32be(stsd, 1);
1328 if (p_stream->fmt.i_cat == AUDIO_ES)
1329 box_gather(stsd, GetSounBox(p_mux, p_stream));
1330 else if (p_stream->fmt.i_cat == VIDEO_ES)
1331 box_gather(stsd, GetVideBox(p_stream));
1332 else if (p_stream->fmt.i_cat == SPU_ES)
1333 box_gather(stsd, GetTextBox());
1335 /* chunk offset table */
1337 if (p_sys->i_pos >= (((uint64_t)0x1) << 32)) {
1338 /* 64 bits version */
1339 p_stream->b_stco64 = true;
1340 stco = box_full_new("co64", 0, 0);
1342 /* 32 bits version */
1343 p_stream->b_stco64 = false;
1344 stco = box_full_new("stco", 0, 0);
1346 bo_add_32be(stco, 0); // entry-count (fixed latter)
1348 /* sample to chunk table */
1349 bo_t *stsc = box_full_new("stsc", 0, 0);
1350 bo_add_32be(stsc, 0); // entry-count (fixed latter)
1352 unsigned i_chunk = 0;
1353 unsigned i_stsc_last_val = 0, i_stsc_entries = 0;
1354 for (unsigned i = 0; i < p_stream->i_entry_count; i_chunk++) {
1355 mp4_entry_t *entry = p_stream->entry;
1358 if (p_stream->b_stco64)
1359 bo_add_64be(stco, entry[i].i_pos);
1361 bo_add_32be(stco, entry[i].i_pos);
1363 for (; i < p_stream->i_entry_count; i++)
1364 if (i >= p_stream->i_entry_count - 1 ||
1365 entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
1370 /* Add entry to the stsc table */
1371 if (i_stsc_last_val != i - i_first) {
1372 bo_add_32be(stsc, 1 + i_chunk); // first-chunk
1373 bo_add_32be(stsc, i - i_first) ; // samples-per-chunk
1374 bo_add_32be(stsc, 1); // sample-descr-index
1375 i_stsc_last_val = i - i_first;
1380 /* Fix stco entry count */
1381 bo_fix_32be(stco, 12, i_chunk);
1382 msg_Dbg(p_mux, "created %d chunks (stco)", i_chunk);
1384 /* Fix stsc entry count */
1385 bo_fix_32be(stsc, 12, i_stsc_entries );
1388 bo_t *stts = box_full_new("stts", 0, 0);
1389 bo_add_32be(stts, 0); // entry-count (fixed latter)
1391 uint32_t i_timescale;
1392 if (p_stream->fmt.i_cat == AUDIO_ES)
1393 i_timescale = p_stream->fmt.audio.i_rate;
1395 i_timescale = CLOCK_FREQ;
1397 unsigned i_index = 0;
1398 for (unsigned i = 0; i < p_stream->i_entry_count; i_index++) {
1399 p_stream->entry[i].i_length = p_stream->entry[i].i_length
1400 * (int64_t)i_timescale / CLOCK_FREQ;
1402 int64_t i_delta = p_stream->entry[i].i_length;
1404 for (; i < p_stream->i_entry_count; ++i)
1405 if (i == p_stream->i_entry_count || p_stream->entry[i].i_length != i_delta)
1408 bo_add_32be(stts, i - i_first); // sample-count
1409 bo_add_32be(stts, i_delta); // sample-delta
1411 bo_fix_32be(stts, 12, i_index);
1413 /* FIXME add ctts ?? FIXME */
1415 bo_t *stsz = box_full_new("stsz", 0, 0);
1416 bo_add_32be(stsz, 0); // sample-size
1417 bo_add_32be(stsz, p_stream->i_entry_count); // sample-count
1418 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1419 bo_add_32be(stsz, p_stream->entry[i].i_size); // sample-size
1421 /* create stss table */
1424 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1425 if (p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I) {
1427 stss = box_full_new("stss", 0, 0);
1428 bo_add_32be(stss, 0); /* fixed later */
1430 bo_add_32be(stss, 1 + i);
1435 bo_fix_32be(stss, 12, i_index);
1437 /* Now gather all boxes into stbl */
1438 bo_t *stbl = box_new("stbl");
1440 box_gather(stbl, stsd);
1441 box_gather(stbl, stts);
1443 box_gather(stbl, stss);
1444 box_gather(stbl, stsc);
1445 box_gather(stbl, stsz);
1446 p_stream->i_stco_pos = stbl->len + 16;
1447 box_gather(stbl, stco);
1452 static int64_t get_timestamp(void);
1454 static void matrix_apply_rotation(es_format_t *fmt, uint32_t mvhd_matrix[9])
1456 enum video_orientation_t orientation = ORIENT_NORMAL;
1457 if (fmt->i_cat == VIDEO_ES)
1458 orientation = fmt->video.orientation;
1460 #define ATAN(a, b) do { mvhd_matrix[1] = (a) << 16; \
1461 mvhd_matrix[0] = (b) << 16; \
1464 switch (orientation) {
1465 case ORIENT_ROTATED_90: ATAN( 1, 0); break;
1466 case ORIENT_ROTATED_180: ATAN( 0, -1); break;
1467 case ORIENT_ROTATED_270: ATAN( -1, 0); break;
1468 default: ATAN( 0, 1); break;
1471 mvhd_matrix[3] = mvhd_matrix[0] ? 0 : 0x10000;
1472 mvhd_matrix[4] = mvhd_matrix[1] ? 0 : 0x10000;
1475 static bo_t *GetMoovBox(sout_mux_t *p_mux)
1477 sout_mux_sys_t *p_sys = p_mux->p_sys;
1481 uint32_t i_movie_timescale = 90000;
1482 int64_t i_movie_duration = 0;
1483 int64_t i_timestamp = get_timestamp();
1485 moov = box_new("moov");
1487 /* Create general info */
1488 for (int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
1489 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1490 i_movie_duration = __MAX(i_movie_duration, p_stream->i_duration);
1492 msg_Dbg(p_mux, "movie duration %"PRId64"s", i_movie_duration / CLOCK_FREQ);
1494 i_movie_duration = i_movie_duration * i_movie_timescale / CLOCK_FREQ;
1496 /* *** add /moov/mvhd *** */
1497 if (!p_sys->b_64_ext) {
1498 mvhd = box_full_new("mvhd", 0, 0);
1499 bo_add_32be(mvhd, i_timestamp); // creation time
1500 bo_add_32be(mvhd, i_timestamp); // modification time
1501 bo_add_32be(mvhd, i_movie_timescale); // timescale
1502 bo_add_32be(mvhd, i_movie_duration); // duration
1504 mvhd = box_full_new("mvhd", 1, 0);
1505 bo_add_64be(mvhd, i_timestamp); // creation time
1506 bo_add_64be(mvhd, i_timestamp); // modification time
1507 bo_add_32be(mvhd, i_movie_timescale); // timescale
1508 bo_add_64be(mvhd, i_movie_duration); // duration
1510 bo_add_32be(mvhd, 0x10000); // rate
1511 bo_add_16be(mvhd, 0x100); // volume
1512 bo_add_16be(mvhd, 0); // reserved
1513 for (int i = 0; i < 2; i++)
1514 bo_add_32be(mvhd, 0); // reserved
1516 uint32_t mvhd_matrix[9] = { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1518 for (int i = 0; i < 9; i++)
1519 bo_add_32be(mvhd, mvhd_matrix[i]);// matrix
1520 for (int i = 0; i < 6; i++)
1521 bo_add_32be(mvhd, 0); // pre-defined
1523 /* Next available track id */
1524 bo_add_32be(mvhd, p_sys->i_nb_streams + 1); // next-track-id
1526 box_gather(moov, mvhd);
1528 for (int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
1529 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1531 uint32_t i_timescale;
1532 if (p_stream->fmt.i_cat == AUDIO_ES)
1533 i_timescale = p_stream->fmt.audio.i_rate;
1535 i_timescale = CLOCK_FREQ;
1537 /* *** add /moov/trak *** */
1538 bo_t *trak = box_new("trak");
1540 /* *** add /moov/trak/tkhd *** */
1542 if (!p_sys->b_64_ext) {
1544 tkhd = box_full_new("tkhd", 0, 0x0f);
1546 tkhd = box_full_new("tkhd", 0, 1);
1548 bo_add_32be(tkhd, i_timestamp); // creation time
1549 bo_add_32be(tkhd, i_timestamp); // modification time
1550 bo_add_32be(tkhd, p_stream->i_track_id);
1551 bo_add_32be(tkhd, 0); // reserved 0
1552 bo_add_32be(tkhd, p_stream->i_duration *
1553 (int64_t)i_movie_timescale / CLOCK_FREQ); // duration
1556 tkhd = box_full_new("tkhd", 1, 0x0f);
1558 tkhd = box_full_new("tkhd", 1, 1);
1560 bo_add_64be(tkhd, i_timestamp); // creation time
1561 bo_add_64be(tkhd, i_timestamp); // modification time
1562 bo_add_32be(tkhd, p_stream->i_track_id);
1563 bo_add_32be(tkhd, 0); // reserved 0
1564 bo_add_64be(tkhd, p_stream->i_duration *
1565 (int64_t)i_movie_timescale / CLOCK_FREQ); // duration
1568 for (int i = 0; i < 2; i++)
1569 bo_add_32be(tkhd, 0); // reserved
1570 bo_add_16be(tkhd, 0); // layer
1571 bo_add_16be(tkhd, 0); // pre-defined
1573 bo_add_16be(tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0);
1574 bo_add_16be(tkhd, 0); // reserved
1575 matrix_apply_rotation(&p_stream->fmt, mvhd_matrix);
1576 for (int i = 0; i < 9; i++)
1577 bo_add_32be(tkhd, mvhd_matrix[i]); // matrix
1578 if (p_stream->fmt.i_cat == AUDIO_ES) {
1579 bo_add_32be(tkhd, 0); // width (presentation)
1580 bo_add_32be(tkhd, 0); // height(presentation)
1581 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
1582 int i_width = p_stream->fmt.video.i_width << 16;
1583 if (p_stream->fmt.video.i_sar_num > 0 && p_stream->fmt.video.i_sar_den > 0) {
1584 i_width = (int64_t)p_stream->fmt.video.i_sar_num *
1585 ((int64_t)p_stream->fmt.video.i_width << 16) /
1586 p_stream->fmt.video.i_sar_den;
1588 // width (presentation)
1589 bo_add_32be(tkhd, i_width);
1590 // height(presentation)
1591 bo_add_32be(tkhd, p_stream->fmt.video.i_height << 16);
1593 int i_width = 320 << 16;
1595 for (int i = 0; i < p_sys->i_nb_streams; i++) {
1596 mp4_stream_t *tk = p_sys->pp_streams[i];
1597 if (tk->fmt.i_cat == VIDEO_ES) {
1598 if (tk->fmt.video.i_sar_num > 0 &&
1599 tk->fmt.video.i_sar_den > 0)
1600 i_width = (int64_t)tk->fmt.video.i_sar_num *
1601 ((int64_t)tk->fmt.video.i_width << 16) /
1602 tk->fmt.video.i_sar_den;
1604 i_width = tk->fmt.video.i_width << 16;
1605 i_height = tk->fmt.video.i_height;
1609 bo_add_32be(tkhd, i_width); // width (presentation)
1610 bo_add_32be(tkhd, i_height << 16); // height(presentation)
1613 box_gather(trak, tkhd);
1615 /* *** add /moov/trak/edts and elst */
1616 bo_t *edts = box_new("edts");
1617 bo_t *elst = box_full_new("elst", p_sys->b_64_ext ? 1 : 0, 0);
1618 if (p_stream->i_dts_start > p_sys->i_dts_start) {
1619 bo_add_32be(elst, 2);
1621 if (p_sys->b_64_ext) {
1622 bo_add_64be(elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1623 i_movie_timescale / CLOCK_FREQ);
1624 bo_add_64be(elst, -1);
1626 bo_add_32be(elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1627 i_movie_timescale / CLOCK_FREQ);
1628 bo_add_32be(elst, -1);
1630 bo_add_16be(elst, 1);
1631 bo_add_16be(elst, 0);
1633 bo_add_32be(elst, 1);
1635 if (p_sys->b_64_ext) {
1636 bo_add_64be(elst, p_stream->i_duration *
1637 i_movie_timescale / CLOCK_FREQ);
1638 bo_add_64be(elst, 0);
1640 bo_add_32be(elst, p_stream->i_duration *
1641 i_movie_timescale / CLOCK_FREQ);
1642 bo_add_32be(elst, 0);
1644 bo_add_16be(elst, 1);
1645 bo_add_16be(elst, 0);
1647 box_gather(edts, elst);
1648 box_gather(trak, edts);
1650 /* *** add /moov/trak/mdia *** */
1651 bo_t *mdia = box_new("mdia");
1655 if (!p_sys->b_64_ext) {
1656 mdhd = box_full_new("mdhd", 0, 0);
1657 bo_add_32be(mdhd, i_timestamp); // creation time
1658 bo_add_32be(mdhd, i_timestamp); // modification time
1659 bo_add_32be(mdhd, i_timescale); // timescale
1660 bo_add_32be(mdhd, p_stream->i_duration * (int64_t)i_timescale /
1661 CLOCK_FREQ); // duration
1663 mdhd = box_full_new("mdhd", 1, 0);
1664 bo_add_64be(mdhd, i_timestamp); // creation time
1665 bo_add_64be(mdhd, i_timestamp); // modification time
1666 bo_add_32be(mdhd, i_timescale); // timescale
1667 bo_add_64be(mdhd, p_stream->i_duration * (int64_t)i_timescale /
1668 CLOCK_FREQ); // duration
1671 if (p_stream->fmt.psz_language) {
1672 char *psz = p_stream->fmt.psz_language;
1673 const iso639_lang_t *pl = NULL;
1674 uint16_t lang = 0x0;
1676 if (strlen(psz) == 2)
1677 pl = GetLang_1(psz);
1678 else if (strlen(psz) == 3) {
1679 pl = GetLang_2B(psz);
1680 if (!strcmp(pl->psz_iso639_1, "??"))
1681 pl = GetLang_2T(psz);
1684 if (pl && strcmp(pl->psz_iso639_1, "??"))
1685 lang = ((pl->psz_iso639_2T[0] - 0x60) << 10) |
1686 ((pl->psz_iso639_2T[1] - 0x60) << 5) |
1687 ((pl->psz_iso639_2T[2] - 0x60));
1688 bo_add_16be(mdhd, lang); // language
1690 bo_add_16be(mdhd, 0 ); // language
1691 bo_add_16be(mdhd, 0 ); // predefined
1692 box_gather(mdia, mdhd);
1694 /* handler reference */
1695 bo_t *hdlr = box_full_new("hdlr", 0, 0);
1698 bo_add_fourcc(hdlr, "mhlr"); // media handler
1700 bo_add_32be(hdlr, 0);
1702 if (p_stream->fmt.i_cat == AUDIO_ES)
1703 bo_add_fourcc(hdlr, "soun");
1704 else if (p_stream->fmt.i_cat == VIDEO_ES)
1705 bo_add_fourcc(hdlr, "vide");
1706 else if (p_stream->fmt.i_cat == SPU_ES)
1707 bo_add_fourcc(hdlr, "text");
1709 bo_add_32be(hdlr, 0); // reserved
1710 bo_add_32be(hdlr, 0); // reserved
1711 bo_add_32be(hdlr, 0); // reserved
1714 bo_add_8(hdlr, 12); /* Pascal string for .mov */
1716 if (p_stream->fmt.i_cat == AUDIO_ES)
1717 bo_add_mem(hdlr, 12, (uint8_t*)"SoundHandler");
1718 else if (p_stream->fmt.i_cat == VIDEO_ES)
1719 bo_add_mem(hdlr, 12, (uint8_t*)"VideoHandler");
1721 bo_add_mem(hdlr, 12, (uint8_t*)"Text Handler");
1724 bo_add_8(hdlr, 0); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
1726 box_gather(mdia, hdlr);
1729 bo_t *minf = box_new("minf");
1732 if (p_stream->fmt.i_cat == AUDIO_ES) {
1735 smhd = box_full_new("smhd", 0, 0);
1736 bo_add_16be(smhd, 0); // balance
1737 bo_add_16be(smhd, 0); // reserved
1739 box_gather(minf, smhd);
1740 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
1743 vmhd = box_full_new("vmhd", 0, 1);
1744 bo_add_16be(vmhd, 0); // graphicsmode
1745 for (int i = 0; i < 3; i++)
1746 bo_add_16be(vmhd, 0); // opcolor
1748 box_gather(minf, vmhd);
1749 } else if (p_stream->fmt.i_cat == SPU_ES) {
1750 bo_t *gmhd = box_new("gmhd");
1751 bo_t *gmin = box_full_new("gmin", 0, 1);
1753 bo_add_16be(gmin, 0); // graphicsmode
1754 for (int i = 0; i < 3; i++)
1755 bo_add_16be(gmin, 0); // opcolor
1756 bo_add_16be(gmin, 0); // balance
1757 bo_add_16be(gmin, 0); // reserved
1759 box_gather(gmhd, gmin);
1761 box_gather(minf, gmhd);
1765 bo_t *dinf = box_new("dinf");
1766 bo_t *dref = box_full_new("dref", 0, 0);
1767 bo_add_32be(dref, 1);
1768 bo_t *url = box_full_new("url ", 0, 0x01);
1769 box_gather(dref, url);
1770 box_gather(dinf, dref);
1772 /* append dinf to mdia */
1773 box_gather(minf, dinf);
1776 bo_t *stbl = GetStblBox(p_mux, p_stream);
1778 /* append stbl to minf */
1779 p_stream->i_stco_pos += minf->len;
1780 box_gather(minf, stbl);
1782 /* append minf to mdia */
1783 p_stream->i_stco_pos += mdia->len;
1784 box_gather(mdia, minf);
1786 /* append mdia to trak */
1787 p_stream->i_stco_pos += trak->len;
1788 box_gather(trak, mdia);
1790 /* append trak to moov */
1791 p_stream->i_stco_pos += moov->len;
1792 box_gather(moov, trak);
1795 /* Add user data tags */
1796 box_gather(moov, GetUdtaTag(p_mux));
1802 /****************************************************************************/
1804 static void bo_init(bo_t *p_bo)
1807 p_bo->b = block_Alloc(1024);
1810 static void bo_add_8(bo_t *p_bo, uint8_t i)
1812 if (p_bo->len >= p_bo->b->i_buffer)
1813 p_bo->b = block_Realloc(p_bo->b, 0, p_bo->b->i_buffer + 1024);
1815 p_bo->b->p_buffer[p_bo->len++] = i;
1818 static void bo_add_16be(bo_t *p_bo, uint16_t i)
1820 bo_add_8(p_bo, ((i >> 8) &0xff));
1821 bo_add_8(p_bo, i &0xff);
1824 static void bo_add_24be(bo_t *p_bo, uint32_t i)
1826 bo_add_8(p_bo, ((i >> 16) &0xff));
1827 bo_add_8(p_bo, ((i >> 8) &0xff));
1828 bo_add_8(p_bo, ( i &0xff));
1830 static void bo_add_32be(bo_t *p_bo, uint32_t i)
1832 bo_add_16be(p_bo, ((i >> 16) &0xffff));
1833 bo_add_16be(p_bo, i &0xffff);
1836 static void bo_fix_32be (bo_t *p_bo, int i_pos, uint32_t i)
1838 p_bo->b->p_buffer[i_pos ] = (i >> 24)&0xff;
1839 p_bo->b->p_buffer[i_pos + 1] = (i >> 16)&0xff;
1840 p_bo->b->p_buffer[i_pos + 2] = (i >> 8)&0xff;
1841 p_bo->b->p_buffer[i_pos + 3] = (i )&0xff;
1844 static void bo_add_64be(bo_t *p_bo, uint64_t i)
1846 bo_add_32be(p_bo, ((i >> 32) &0xffffffff));
1847 bo_add_32be(p_bo, i &0xffffffff);
1850 static void bo_add_fourcc(bo_t *p_bo, const char *fcc)
1852 bo_add_8(p_bo, fcc[0]);
1853 bo_add_8(p_bo, fcc[1]);
1854 bo_add_8(p_bo, fcc[2]);
1855 bo_add_8(p_bo, fcc[3]);
1858 static void bo_add_mem(bo_t *p_bo, int i_size, uint8_t *p_mem)
1860 for (int i = 0; i < i_size; i++)
1861 bo_add_8(p_bo, p_mem[i]);
1864 static void bo_add_descr(bo_t *p_bo, uint8_t tag, uint32_t size)
1866 bo_add_8(p_bo, tag);
1867 for (int i = 3; i>0; i--)
1868 bo_add_8(p_bo, (size>>(7*i)) | 0x80);
1869 bo_add_8(p_bo, size & 0x7F);
1872 static bo_t *box_new(const char *fcc)
1874 bo_t *box = malloc(sizeof(*box));
1880 bo_add_32be (box, 0);
1881 bo_add_fourcc(box, fcc);
1886 static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f)
1888 bo_t *box = box_new(fcc);
1893 bo_add_24be (box, f);
1898 static void box_fix(bo_t *box)
1900 box->b->p_buffer[0] = box->len >> 24;
1901 box->b->p_buffer[1] = box->len >> 16;
1902 box->b->p_buffer[2] = box->len >> 8;
1903 box->b->p_buffer[3] = box->len;
1906 static void box_gather (bo_t *box, bo_t *box2)
1909 box->b = block_Realloc(box->b, 0, box->len + box2->len);
1910 memcpy(&box->b->p_buffer[box->len], box2->b->p_buffer, box2->len);
1911 box->len += box2->len;
1912 block_Release(box2->b);
1916 static void box_send(sout_mux_t *p_mux, bo_t *box)
1918 box->b->i_buffer = box->len;
1919 sout_AccessOutWrite(p_mux->p_access, box->b);
1923 static int64_t get_timestamp(void)
1925 int64_t i_timestamp = time(NULL);
1927 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
1928 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60