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 /*****************************************************************************
45 *****************************************************************************/
46 #define FASTSTART_TEXT N_("Create \"Fast Start\" files")
47 #define FASTSTART_LONGTEXT N_(\
48 "Create \"Fast Start\" files. " \
49 "\"Fast Start\" files are optimized for downloads and allow the user " \
50 "to start previewing the file while it is downloading.")
52 static int Open (vlc_object_t *);
53 static void Close (vlc_object_t *);
55 #define SOUT_CFG_PREFIX "sout-mp4-"
58 set_description(N_("MP4/MOV muxer"))
59 set_category(CAT_SOUT)
60 set_subcategory(SUBCAT_SOUT_MUX)
63 add_bool(SOUT_CFG_PREFIX "faststart", true,
64 FASTSTART_TEXT, FASTSTART_LONGTEXT,
66 set_capability("sout mux", 5)
67 add_shortcut("mp4", "mov", "3gp")
68 set_callbacks(Open, Close)
71 /*****************************************************************************
73 *****************************************************************************/
74 static const char *const ppsz_sout_options[] = {
78 static int Control(sout_mux_t *, int, va_list);
79 static int AddStream(sout_mux_t *, sout_input_t *);
80 static int DelStream(sout_mux_t *, sout_input_t *);
81 static int Mux (sout_mux_t *);
83 /*****************************************************************************
85 *****************************************************************************/
103 unsigned int i_entry_count;
104 unsigned int i_entry_max;
106 int64_t i_length_neg;
112 /* for later stco fix-up (fast start files) */
121 struct sout_mux_sys_t
134 mp4_stream_t **pp_streams;
143 static void bo_init (bo_t *);
144 static void bo_add_8 (bo_t *, uint8_t);
145 static void bo_add_16be (bo_t *, uint16_t);
146 static void bo_add_24be (bo_t *, uint32_t);
147 static void bo_add_32be (bo_t *, uint32_t);
148 static void bo_add_64be (bo_t *, uint64_t);
149 static void bo_add_fourcc(bo_t *, const char *);
150 static void bo_add_mem (bo_t *, int , uint8_t *);
151 static void bo_add_descr(bo_t *, uint8_t , uint32_t);
153 static void bo_fix_32be (bo_t *, int , uint32_t);
155 static bo_t *box_new (const char *fcc);
156 static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f);
157 static void box_fix (bo_t *box);
158 static void box_gather (bo_t *box, bo_t *box2);
160 static void box_send(sout_mux_t *p_mux, bo_t *box);
162 static bo_t *GetMoovBox(sout_mux_t *p_mux);
164 static block_t *ConvertSUBT(block_t *);
165 static block_t *ConvertAVC1(block_t *);
167 static const char avc1_start_code[4] = { 0, 0, 0, 1 };
169 /*****************************************************************************
171 *****************************************************************************/
172 static int Open(vlc_object_t *p_this)
174 sout_mux_t *p_mux = (sout_mux_t*)p_this;
175 sout_mux_sys_t *p_sys;
178 msg_Dbg(p_mux, "Mp4 muxer opened");
179 config_ChainParse(p_mux, SOUT_CFG_PREFIX, ppsz_sout_options, p_mux->p_cfg);
181 p_mux->pf_control = Control;
182 p_mux->pf_addstream = AddStream;
183 p_mux->pf_delstream = DelStream;
185 p_mux->p_sys = p_sys = malloc(sizeof(sout_mux_sys_t));
189 p_sys->i_nb_streams = 0;
190 p_sys->pp_streams = NULL;
191 p_sys->i_mdat_pos = 0;
192 p_sys->b_mov = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "mov");
193 p_sys->b_3gp = p_mux->psz_mux && !strcmp(p_mux->psz_mux, "3gp");
194 p_sys->i_dts_start = 0;
198 /* Now add ftyp header */
199 box = box_new("ftyp");
201 bo_add_fourcc(box, "3gp6");
203 bo_add_fourcc(box, "isom");
204 bo_add_32be (box, 0);
206 bo_add_fourcc(box, "3gp4");
208 bo_add_fourcc(box, "mp41");
209 bo_add_fourcc(box, "avc1");
210 bo_add_fourcc(box, "qt ");
213 p_sys->i_pos += box->len;
214 p_sys->i_mdat_pos = p_sys->i_pos;
216 box_send(p_mux, box);
220 * Quicktime actually doesn't like the 64 bits extensions !!! */
221 p_sys->b_64_ext = false;
223 /* Now add mdat header */
224 box = box_new("mdat");
225 bo_add_64be (box, 0); // enough to store an extended size
227 p_sys->i_pos += box->len;
229 box_send(p_mux, box);
234 /*****************************************************************************
236 *****************************************************************************/
237 static void Close(vlc_object_t *p_this)
239 sout_mux_t *p_mux = (sout_mux_t*)p_this;
240 sout_mux_sys_t *p_sys = p_mux->p_sys;
242 msg_Dbg(p_mux, "Close");
244 /* Update mdat size */
247 if (p_sys->i_pos - p_sys->i_mdat_pos >= (((uint64_t)1)<<32)) {
249 bo_add_32be (&bo, 1);
250 bo_add_fourcc(&bo, "mdat");
251 bo_add_64be (&bo, p_sys->i_pos - p_sys->i_mdat_pos);
253 bo_add_32be (&bo, 8);
254 bo_add_fourcc(&bo, "wide");
255 bo_add_32be (&bo, p_sys->i_pos - p_sys->i_mdat_pos - 8);
256 bo_add_fourcc(&bo, "mdat");
259 bo.b->i_buffer = bo.len;
260 sout_AccessOutSeek(p_mux->p_access, p_sys->i_mdat_pos);
261 sout_AccessOutWrite(p_mux->p_access, bo.b);
263 /* Create MOOV header */
264 uint64_t i_moov_pos = p_sys->i_pos;
265 bo_t *moov = GetMoovBox(p_mux);
267 /* Check we need to create "fast start" files */
268 p_sys->b_fast_start = var_GetBool(p_this, SOUT_CFG_PREFIX "faststart");
269 while (p_sys->b_fast_start) {
270 /* Move data to the end of the file so we can fit the moov header
272 int64_t i_size = p_sys->i_pos - p_sys->i_mdat_pos;
273 int i_moov_size = moov->len;
276 int64_t i_chunk = __MIN(32768, i_size);
277 block_t *p_buf = block_Alloc(i_chunk);
278 sout_AccessOutSeek(p_mux->p_access,
279 p_sys->i_mdat_pos + i_size - i_chunk);
280 if (sout_AccessOutRead(p_mux->p_access, p_buf) < i_chunk) {
281 msg_Warn(p_this, "read() not supported by access output, "
282 "won't create a fast start file");
283 p_sys->b_fast_start = false;
284 block_Release(p_buf);
287 sout_AccessOutSeek(p_mux->p_access, p_sys->i_mdat_pos + i_size +
288 i_moov_size - i_chunk);
289 sout_AccessOutWrite(p_mux->p_access, p_buf);
293 if (!p_sys->b_fast_start)
296 /* Fix-up samples to chunks table in MOOV header */
297 for (int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
298 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
300 moov->len = p_stream->i_stco_pos;
301 for (unsigned i = 0; i < p_stream->i_entry_count; ) {
302 mp4_entry_t *entry = p_stream->entry;
303 if (p_stream->b_stco64)
304 bo_add_64be(moov, entry[i].i_pos + i_moov_size);
306 bo_add_32be(moov, entry[i].i_pos + i_moov_size);
308 for (; i < p_stream->i_entry_count; i++)
309 if (i >= p_stream->i_entry_count - 1 ||
310 entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
317 moov->len = i_moov_size;
318 i_moov_pos = p_sys->i_mdat_pos;
319 p_sys->b_fast_start = false;
322 /* Write MOOV header */
323 sout_AccessOutSeek(p_mux->p_access, i_moov_pos);
324 box_send(p_mux, moov);
327 for (int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
328 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
330 es_format_Clean(&p_stream->fmt);
331 free(p_stream->entry);
334 if (p_sys->i_nb_streams)
335 free(p_sys->pp_streams);
339 /*****************************************************************************
341 *****************************************************************************/
342 static int Control(sout_mux_t *p_mux, int i_query, va_list args)
349 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
350 pb_bool = (bool*)va_arg(args, bool *);
354 case MUX_GET_ADD_STREAM_WAIT:
355 pb_bool = (bool*)va_arg(args, bool *);
359 case MUX_GET_MIME: /* Not needed, as not streamable */
365 /*****************************************************************************
367 *****************************************************************************/
368 static int AddStream(sout_mux_t *p_mux, sout_input_t *p_input)
370 sout_mux_sys_t *p_sys = p_mux->p_sys;
371 mp4_stream_t *p_stream;
373 switch(p_input->p_fmt->i_codec)
380 case VLC_CODEC_MJPGB:
385 case VLC_CODEC_AMR_NB:
386 case VLC_CODEC_AMR_WB:
391 msg_Warn(p_mux, "subtitle track added like in .mov (even when creating .mp4)");
394 msg_Err(p_mux, "unsupported codec %4.4s in mp4",
395 (char*)&p_input->p_fmt->i_codec);
399 p_stream = malloc(sizeof(mp4_stream_t));
402 es_format_Copy(&p_stream->fmt, p_input->p_fmt);
403 p_stream->i_track_id = p_sys->i_nb_streams + 1;
404 p_stream->i_length_neg = 0;
405 p_stream->i_entry_count = 0;
406 p_stream->i_entry_max = 1000;
408 calloc(p_stream->i_entry_max, sizeof(mp4_entry_t));
409 p_stream->i_dts_start = 0;
410 p_stream->i_duration = 0;
412 p_input->p_sys = p_stream;
414 msg_Dbg(p_mux, "adding input");
416 TAB_APPEND(p_sys->i_nb_streams, p_sys->pp_streams, p_stream);
420 /*****************************************************************************
422 *****************************************************************************/
423 static int DelStream(sout_mux_t *p_mux, sout_input_t *p_input)
426 msg_Dbg(p_mux, "removing input");
430 /*****************************************************************************
432 *****************************************************************************/
433 static int Mux(sout_mux_t *p_mux)
435 sout_mux_sys_t *p_sys = p_mux->p_sys;
438 int i_stream = sout_MuxGetStream(p_mux, 2, NULL);
442 sout_input_t *p_input = p_mux->pp_inputs[i_stream];
443 mp4_stream_t *p_stream = (mp4_stream_t*)p_input->p_sys;
447 p_data = block_FifoGet(p_input->p_fifo);
448 if (p_stream->fmt.i_codec == VLC_CODEC_H264)
449 p_data = ConvertAVC1(p_data);
450 else if (p_stream->fmt.i_codec == VLC_CODEC_SUBT)
451 p_data = ConvertSUBT(p_data);
454 if (p_stream->fmt.i_cat != SPU_ES) {
455 /* Fix length of the sample */
456 if (block_FifoCount(p_input->p_fifo) > 0) {
457 block_t *p_next = block_FifoShow(p_input->p_fifo);
458 int64_t i_diff = p_next->i_dts - p_data->i_dts;
460 if (i_diff < CLOCK_FREQ) /* protection */
461 p_data->i_length = i_diff;
463 if (p_data->i_length <= 0) {
464 msg_Warn(p_mux, "i_length <= 0");
465 p_stream->i_length_neg += p_data->i_length - 1;
466 p_data->i_length = 1;
467 } else if (p_stream->i_length_neg < 0) {
468 int64_t i_recover = __MIN(p_data->i_length / 4, - p_stream->i_length_neg);
470 p_data->i_length -= i_recover;
471 p_stream->i_length_neg += i_recover;
475 /* Save starting time */
476 if (p_stream->i_entry_count == 0) {
477 p_stream->i_dts_start = p_data->i_dts;
479 /* Update global dts_start */
480 if (p_sys->i_dts_start <= 0 || p_stream->i_dts_start < p_sys->i_dts_start)
481 p_sys->i_dts_start = p_stream->i_dts_start;
484 if (p_stream->fmt.i_cat == SPU_ES && p_stream->i_entry_count > 0) {
485 int64_t i_length = p_data->i_dts - p_stream->i_last_dts;
487 if (i_length <= 0) /* FIXME handle this broken case */
491 if (p_stream->entry[p_stream->i_entry_count-1].i_length <= 0)
492 p_stream->entry[p_stream->i_entry_count-1].i_length = i_length;
495 /* add index entry */
496 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
497 e->i_pos = p_sys->i_pos;
498 e->i_size = p_data->i_buffer;
499 e->i_pts_dts = p_data->i_pts - p_data->i_dts;
500 if (e->i_pts_dts < 0)
502 e->i_length = p_data->i_length;
503 e->i_flags = p_data->i_flags;
505 p_stream->i_entry_count++;
506 /* XXX: -1 to always have 2 entry for easy adding of empty SPU */
507 if (p_stream->i_entry_count >= p_stream->i_entry_max - 1) {
508 p_stream->i_entry_max += 1000;
509 p_stream->entry = xrealloc(p_stream->entry,
510 p_stream->i_entry_max * sizeof(mp4_entry_t));
514 p_stream->i_duration = p_stream->i_last_dts - p_stream->i_dts_start + p_data->i_length;
515 p_sys->i_pos += p_data->i_buffer;
518 p_stream->i_last_dts = p_data->i_dts;
521 sout_AccessOutWrite(p_mux->p_access, p_data);
523 if (p_stream->fmt.i_cat == SPU_ES) {
524 int64_t i_length = p_stream->entry[p_stream->i_entry_count-1].i_length;
528 msg_Dbg(p_mux, "writing an empty sub") ;
530 /* Append a idx entry */
531 mp4_entry_t *e = &p_stream->entry[p_stream->i_entry_count];
532 e->i_pos = p_sys->i_pos;
538 /* XXX: No need to grow the entry here */
539 p_stream->i_entry_count++;
542 p_stream->i_last_dts += i_length;
545 p_data = block_Alloc(3);
546 p_data->p_buffer[0] = 0;
547 p_data->p_buffer[1] = 1;
548 p_data->p_buffer[2] = ' ';
550 p_sys->i_pos += p_data->i_buffer;
552 sout_AccessOutWrite(p_mux->p_access, p_data);
556 p_stream->i_duration = p_stream->i_last_dts - p_stream->i_dts_start;
563 /*****************************************************************************
565 *****************************************************************************/
566 static block_t *ConvertSUBT(block_t *p_block)
568 p_block = block_Realloc(p_block, 2, p_block->i_buffer);
570 /* No trailling '\0' */
571 if (p_block->i_buffer > 2 && p_block->p_buffer[p_block->i_buffer-1] == '\0')
574 p_block->p_buffer[0] = ((p_block->i_buffer - 2) >> 8)&0xff;
575 p_block->p_buffer[1] = ((p_block->i_buffer - 2) )&0xff;
580 static block_t *ConvertAVC1(block_t *p_block)
582 uint8_t *last = p_block->p_buffer; /* Assume it starts with 0x00000001 */
583 uint8_t *dat = &p_block->p_buffer[4];
584 uint8_t *end = &p_block->p_buffer[p_block->i_buffer];
587 /* Replace the 4 bytes start code with 4 bytes size,
588 * FIXME are all startcodes 4 bytes ? (I don't think :(*/
590 while (dat < end - 4) {
591 if (!memcmp(dat, avc1_start_code, 4))
599 int i_size = dat - &last[4];
600 last[0] = (i_size >> 24)&0xff;
601 last[1] = (i_size >> 16)&0xff;
602 last[2] = (i_size >> 8)&0xff;
603 last[3] = (i_size )&0xff;
605 /* Skip blocks with SPS/PPS */
606 //if ((last[4]&0x1f) == 7 || (last[4]&0x1f) == 8)
607 // ; // FIXME Find a way to skip dat without frelling everything
614 static bo_t *GetESDS(mp4_stream_t *p_stream)
617 int64_t i_bitrate_avg = 0;
618 int64_t i_bitrate_max = 0;
620 /* Compute avg/max bitrate */
621 for (unsigned i = 0; i < p_stream->i_entry_count; i++) {
622 i_bitrate_avg += p_stream->entry[i].i_size;
623 if (p_stream->entry[i].i_length > 0) {
624 int64_t i_bitrate = INT64_C(8000000) * p_stream->entry[i].i_size / p_stream->entry[i].i_length;
625 if (i_bitrate > i_bitrate_max)
626 i_bitrate_max = i_bitrate;
630 if (p_stream->i_duration > 0)
631 i_bitrate_avg = INT64_C(8000000) * i_bitrate_avg / p_stream->i_duration;
634 if (i_bitrate_max <= 1)
635 i_bitrate_max = 0x7fffffff;
638 int i_decoder_specific_info_size = (p_stream->fmt.i_extra > 0) ? 5 + p_stream->fmt.i_extra : 0;
640 esds = box_full_new("esds", 0, 0);
643 bo_add_descr(esds, 0x03, 3 + 5 + 13 + i_decoder_specific_info_size + 5 + 1);
644 bo_add_16be(esds, p_stream->i_track_id);
645 bo_add_8 (esds, 0x1f); // flags=0|streamPriority=0x1f
647 /* DecoderConfigDescr */
648 bo_add_descr(esds, 0x04, 13 + i_decoder_specific_info_size);
650 int i_object_type_indication;
651 switch(p_stream->fmt.i_codec)
654 i_object_type_indication = 0x20;
657 /* FIXME MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
658 i_object_type_indication = 0x60;
661 /* FIXME for mpeg2-aac == 0x66->0x68 */
662 i_object_type_indication = 0x40;
665 i_object_type_indication =
666 p_stream->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
669 i_object_type_indication = 0x00;
672 int i_stream_type = p_stream->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
674 bo_add_8 (esds, i_object_type_indication);
675 bo_add_8 (esds, (i_stream_type << 2) | 1);
676 bo_add_24be(esds, 1024 * 1024); // bufferSizeDB
677 bo_add_32be(esds, i_bitrate_max); // maxBitrate
678 bo_add_32be(esds, i_bitrate_avg); // avgBitrate
680 if (p_stream->fmt.i_extra > 0) {
681 /* DecoderSpecificInfo */
682 bo_add_descr(esds, 0x05, p_stream->fmt.i_extra);
684 for (int i = 0; i < p_stream->fmt.i_extra; i++)
685 bo_add_8(esds, ((uint8_t*)p_stream->fmt.p_extra)[i]);
688 /* SL_Descr mandatory */
689 bo_add_descr(esds, 0x06, 1);
690 bo_add_8 (esds, 0x02); // sl_predefined
695 static bo_t *GetWaveTag(mp4_stream_t *p_stream)
700 wave = box_new("wave");
702 box = box_new("frma");
703 bo_add_fourcc(box, "mp4a");
704 box_gather(wave, box);
706 box = box_new("mp4a");
708 box_gather(wave, box);
710 box = GetESDS(p_stream);
711 box_gather(wave, box);
713 box = box_new("srcq");
714 bo_add_32be(box, 0x40);
715 box_gather(wave, box);
718 bo_add_32be(wave, 8); /* new empty box */
719 bo_add_32be(wave, 0); /* box label */
724 static bo_t *GetDamrTag(mp4_stream_t *p_stream)
728 damr = box_new("damr");
730 bo_add_fourcc(damr, "REFC");
733 if (p_stream->fmt.i_codec == VLC_CODEC_AMR_NB)
734 bo_add_16be(damr, 0x81ff); /* Mode set (all modes for AMR_NB) */
736 bo_add_16be(damr, 0x83ff); /* Mode set (all modes for AMR_WB) */
737 bo_add_16be(damr, 0x1); /* Mode change period (no restriction) */
742 static bo_t *GetD263Tag(void)
746 d263 = box_new("d263");
748 bo_add_fourcc(d263, "VLC ");
749 bo_add_16be(d263, 0xa);
755 static bo_t *GetAvcCTag(mp4_stream_t *p_stream)
758 uint8_t *p_sps = NULL;
759 uint8_t *p_pps = NULL;
763 if (p_stream->fmt.i_extra > 0) {
764 /* FIXME: take into account multiple sps/pps */
765 uint8_t *p_buffer = p_stream->fmt.p_extra;
766 int i_buffer = p_stream->fmt.i_extra;
768 while (i_buffer > 3) {
769 while (memcmp(p_buffer, &avc1_start_code[1], 3)) {
773 const int i_nal_type = p_buffer[3]&0x1f;
776 for (int i_offset = 1; i_offset+2 < i_buffer ; i_offset++)
777 if (!memcmp(&p_buffer[i_offset], &avc1_start_code[1], 3)) {
778 /* we found another startcode */
779 i_startcode = i_offset;
780 while (p_buffer[i_startcode-1] == 0 && i_startcode > 0)
785 int i_size = i_startcode ? i_startcode : i_buffer;
787 if (i_nal_type == 7) {
788 p_sps = &p_buffer[3];
789 i_sps_size = i_size - 3;
791 if (i_nal_type == 8) {
792 p_pps = &p_buffer[3];
793 i_pps_size = i_size - 3;
800 /* FIXME use better value */
801 avcC = box_new("avcC");
802 bo_add_8(avcC, 1); /* configuration version */
803 bo_add_8(avcC, i_sps_size ? p_sps[1] : 77);
804 bo_add_8(avcC, i_sps_size ? p_sps[2] : 64);
805 bo_add_8(avcC, i_sps_size ? p_sps[3] : 30); /* level, 5.1 */
806 bo_add_8(avcC, 0xff); /* 0b11111100 | lengthsize = 0x11 */
808 bo_add_8(avcC, 0xe0 | (i_sps_size > 0 ? 1 : 0)); /* 0b11100000 | sps_count */
809 if (i_sps_size > 0) {
810 bo_add_16be(avcC, i_sps_size);
811 bo_add_mem(avcC, i_sps_size, p_sps);
814 bo_add_8(avcC, (i_pps_size > 0 ? 1 : 0)); /* pps_count */
815 if (i_pps_size > 0) {
816 bo_add_16be(avcC, i_pps_size);
817 bo_add_mem(avcC, i_pps_size, p_pps);
823 /* TODO: No idea about these values */
824 static bo_t *GetSVQ3Tag(mp4_stream_t *p_stream)
826 bo_t *smi = box_new("SMI ");
828 if (p_stream->fmt.i_extra > 0x4e) {
829 uint8_t *p_end = &((uint8_t*)p_stream->fmt.p_extra)[p_stream->fmt.i_extra];
830 uint8_t *p = &((uint8_t*)p_stream->fmt.p_extra)[0x46];
832 while (p + 8 < p_end) {
833 int i_size = GetDWBE(p);
834 if (i_size <= 1) /* FIXME handle 1 as long size */
836 if (!strncmp((const char *)&p[4], "SMI ", 4)) {
837 bo_add_mem(smi, p_end - p - 8, &p[8]);
844 /* Create a dummy one in fallback */
845 bo_add_fourcc(smi, "SEQH");
846 bo_add_32be(smi, 0x5);
847 bo_add_32be(smi, 0xe2c0211d);
853 static bo_t *GetUdtaTag(sout_mux_t *p_mux)
855 sout_mux_sys_t *p_sys = p_mux->p_sys;
856 bo_t *udta = box_new("udta");
859 for (int i_track = 0; i_track < p_sys->i_nb_streams; i_track++) {
860 mp4_stream_t *p_stream = p_sys->pp_streams[i_track];
861 vlc_fourcc_t codec = p_stream->fmt.i_codec;
863 if (codec == VLC_CODEC_MP4V || codec == VLC_CODEC_MP4A) {
864 bo_t *box = box_new("\251req");
866 bo_add_16be(box, sizeof("QuickTime 6.0 or greater") - 1);
868 bo_add_mem(box, sizeof("QuickTime 6.0 or greater") - 1,
869 (uint8_t *)"QuickTime 6.0 or greater");
870 box_gather(udta, box);
877 bo_t *box = box_new("\251enc");
879 bo_add_16be(box, sizeof(PACKAGE_STRING " stream output") - 1);
881 bo_add_mem(box, sizeof(PACKAGE_STRING " stream output") - 1,
882 (uint8_t*)PACKAGE_STRING " stream output");
883 box_gather(udta, box);
887 vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
889 #define ADD_META_BOX(type, box_string) { \
891 if (vlc_meta_Get(p_meta, vlc_meta_##type)) \
892 box = box_new("\251" box_string); \
894 bo_add_16be(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type))); \
895 bo_add_16be(box, 0); \
896 bo_add_mem(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type)), \
897 (uint8_t*)(vlc_meta_Get(p_meta, vlc_meta_##type))); \
898 box_gather(udta, box); \
901 ADD_META_BOX(Title, "nam");
902 ADD_META_BOX(Artist, "ART");
903 ADD_META_BOX(Genre, "gen");
904 ADD_META_BOX(Copyright, "cpy");
905 ADD_META_BOX(Description, "des");
906 ADD_META_BOX(Date, "day");
907 ADD_META_BOX(URL, "url");
914 static bo_t *GetSounBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
916 sout_mux_sys_t *p_sys = p_mux->p_sys;
918 vlc_fourcc_t codec = p_stream->fmt.i_codec;
920 vlc_fourcc_to_char(codec, fcc);
922 if (codec == VLC_CODEC_MPGA) {
925 memcpy(fcc, ".mp3", 4);
927 memcpy(fcc, "mp4a", 4);
930 bo_t *soun = box_new(fcc);
931 for (int i = 0; i < 6; i++)
932 bo_add_8(soun, 0); // reserved;
933 bo_add_16be(soun, 1); // data-reference-index
935 /* SoundDescription */
936 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
937 bo_add_16be(soun, 1); // version 1;
939 bo_add_16be(soun, 0); // version 0;
940 bo_add_16be(soun, 0); // revision level (0)
941 bo_add_32be(soun, 0); // vendor
943 bo_add_16be(soun, p_stream->fmt.audio.i_channels);
945 bo_add_16be(soun, p_stream->fmt.audio.i_bitspersample ?
946 p_stream->fmt.audio.i_bitspersample : 16);
947 bo_add_16be(soun, -2); // compression id
948 bo_add_16be(soun, 0); // packet size (0)
949 bo_add_16be(soun, p_stream->fmt.audio.i_rate); // sampleratehi
950 bo_add_16be(soun, 0); // sampleratelo
952 /* Extended data for SoundDescription V1 */
953 if (p_sys->b_mov && p_stream->fmt.i_codec == VLC_CODEC_MP4A) {
954 /* samples per packet */
955 bo_add_32be(soun, p_stream->fmt.audio.i_frame_length);
956 bo_add_32be(soun, 1536); /* bytes per packet */
957 bo_add_32be(soun, 2); /* bytes per frame */
958 /* bytes per sample */
959 bo_add_32be(soun, 2 /*p_stream->fmt.audio.i_bitspersample/8 */);
962 /* Add an ES Descriptor */
966 if (p_sys->b_mov && codec == VLC_CODEC_MP4A)
967 box = GetWaveTag(p_stream);
968 else if (codec == VLC_CODEC_AMR_NB)
969 box = GetDamrTag(p_stream);
971 box = GetESDS(p_stream);
972 box_gather(soun, box);
978 static bo_t *GetVideBox(mp4_stream_t *p_stream)
982 switch(p_stream->fmt.i_codec)
985 case VLC_CODEC_MPGV: memcpy(fcc, "mp4v", 4); break;
986 case VLC_CODEC_MJPG: memcpy(fcc, "mjpa", 4); break;
987 case VLC_CODEC_SVQ1: memcpy(fcc, "SVQ1", 4); break;
988 case VLC_CODEC_SVQ3: memcpy(fcc, "SVQ3", 4); break;
989 case VLC_CODEC_H263: memcpy(fcc, "s263", 4); break;
990 case VLC_CODEC_H264: memcpy(fcc, "avc1", 4); break;
991 case VLC_CODEC_YV12: memcpy(fcc, "yv12", 4); break;
992 case VLC_CODEC_YUYV: memcpy(fcc, "yuy2", 4); break;
994 vlc_fourcc_to_char(p_stream->fmt.i_codec, fcc);
998 bo_t *vide = box_new(fcc);
999 for (int i = 0; i < 6; i++)
1000 bo_add_8(vide, 0); // reserved;
1001 bo_add_16be(vide, 1); // data-reference-index
1003 bo_add_16be(vide, 0); // predefined;
1004 bo_add_16be(vide, 0); // reserved;
1005 for (int i = 0; i < 3; i++)
1006 bo_add_32be(vide, 0); // predefined;
1008 bo_add_16be(vide, p_stream->fmt.video.i_width); // i_width
1009 bo_add_16be(vide, p_stream->fmt.video.i_height); // i_height
1011 bo_add_32be(vide, 0x00480000); // h 72dpi
1012 bo_add_32be(vide, 0x00480000); // v 72dpi
1014 bo_add_32be(vide, 0); // data size, always 0
1015 bo_add_16be(vide, 1); // frames count per sample
1018 for (int i = 0; i < 32; i++)
1021 bo_add_16be(vide, 0x18); // depth
1022 bo_add_16be(vide, 0xffff); // predefined
1024 /* add an ES Descriptor */
1025 switch(p_stream->fmt.i_codec)
1027 case VLC_CODEC_MP4V:
1028 case VLC_CODEC_MPGV:
1029 box_gather(vide, GetESDS(p_stream));
1032 case VLC_CODEC_H263:
1033 box_gather(vide, GetD263Tag());
1036 case VLC_CODEC_SVQ3:
1037 box_gather(vide, GetSVQ3Tag(p_stream));
1040 case VLC_CODEC_H264:
1041 box_gather(vide, GetAvcCTag(p_stream));
1048 static bo_t *GetTextBox(void)
1050 bo_t *text = box_new("text");
1052 for (int i = 0; i < 6; i++)
1053 bo_add_8(text, 0); // reserved;
1054 bo_add_16be(text, 1); // data-reference-index
1056 bo_add_32be(text, 0); // display flags
1057 bo_add_32be(text, 0); // justification
1058 for (int i = 0; i < 3; i++)
1059 bo_add_16be(text, 0); // back ground color
1061 bo_add_16be(text, 0); // box text
1062 bo_add_16be(text, 0); // box text
1063 bo_add_16be(text, 0); // box text
1064 bo_add_16be(text, 0); // box text
1066 bo_add_64be(text, 0); // reserved
1067 for (int i = 0; i < 3; i++)
1068 bo_add_16be(text, 0xff); // foreground color
1071 bo_add_mem(text, 9, (uint8_t*)"Helvetica");
1076 static bo_t *GetStblBox(sout_mux_t *p_mux, mp4_stream_t *p_stream)
1078 sout_mux_sys_t *p_sys = p_mux->p_sys;
1080 /* sample description */
1081 bo_t *stsd = box_full_new("stsd", 0, 0);
1082 bo_add_32be(stsd, 1);
1083 if (p_stream->fmt.i_cat == AUDIO_ES)
1084 box_gather(stsd, GetSounBox(p_mux, p_stream));
1085 else if (p_stream->fmt.i_cat == VIDEO_ES)
1086 box_gather(stsd, GetVideBox(p_stream));
1087 else if (p_stream->fmt.i_cat == SPU_ES)
1088 box_gather(stsd, GetTextBox());
1090 /* chunk offset table */
1092 if (p_sys->i_pos >= (((uint64_t)0x1) << 32)) {
1093 /* 64 bits version */
1094 p_stream->b_stco64 = true;
1095 stco = box_full_new("co64", 0, 0);
1097 /* 32 bits version */
1098 p_stream->b_stco64 = false;
1099 stco = box_full_new("stco", 0, 0);
1101 bo_add_32be(stco, 0); // entry-count (fixed latter)
1103 /* sample to chunk table */
1104 bo_t *stsc = box_full_new("stsc", 0, 0);
1105 bo_add_32be(stsc, 0); // entry-count (fixed latter)
1107 unsigned i_chunk = 0;
1108 unsigned i_stsc_last_val = 0, i_stsc_entries = 0;
1109 for (unsigned i = 0; i < p_stream->i_entry_count; i_chunk++) {
1110 mp4_entry_t *entry = p_stream->entry;
1113 if (p_stream->b_stco64)
1114 bo_add_64be(stco, entry[i].i_pos);
1116 bo_add_32be(stco, entry[i].i_pos);
1118 for (; i < p_stream->i_entry_count; i++)
1119 if (i >= p_stream->i_entry_count - 1 ||
1120 entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
1125 /* Add entry to the stsc table */
1126 if (i_stsc_last_val != i - i_first) {
1127 bo_add_32be(stsc, 1 + i_chunk); // first-chunk
1128 bo_add_32be(stsc, i - i_first) ; // samples-per-chunk
1129 bo_add_32be(stsc, 1); // sample-descr-index
1130 i_stsc_last_val = i - i_first;
1135 /* Fix stco entry count */
1136 bo_fix_32be(stco, 12, i_chunk);
1137 msg_Dbg(p_mux, "created %d chunks (stco)", i_chunk);
1139 /* Fix stsc entry count */
1140 bo_fix_32be(stsc, 12, i_stsc_entries );
1143 bo_t *stts = box_full_new("stts", 0, 0);
1144 bo_add_32be(stts, 0); // entry-count (fixed latter)
1146 uint32_t i_timescale;
1147 if (p_stream->fmt.i_cat == AUDIO_ES)
1148 i_timescale = p_stream->fmt.audio.i_rate;
1150 i_timescale = CLOCK_FREQ;
1152 unsigned i_index = 0;
1153 for (unsigned i = 0; i < p_stream->i_entry_count; i_index++) {
1154 p_stream->entry[i].i_length = p_stream->entry[i].i_length
1155 * (int64_t)i_timescale / CLOCK_FREQ;
1157 int64_t i_delta = p_stream->entry[i].i_length;
1159 for (; i < p_stream->i_entry_count; ++i)
1160 if (i == p_stream->i_entry_count || p_stream->entry[i].i_length != i_delta)
1163 bo_add_32be(stts, i - i_first); // sample-count
1164 bo_add_32be(stts, i_delta); // sample-delta
1166 bo_fix_32be(stts, 12, i_index);
1168 /* FIXME add ctts ?? FIXME */
1170 bo_t *stsz = box_full_new("stsz", 0, 0);
1171 bo_add_32be(stsz, 0); // sample-size
1172 bo_add_32be(stsz, p_stream->i_entry_count); // sample-count
1173 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1174 bo_add_32be(stsz, p_stream->entry[i].i_size); // sample-size
1176 /* create stss table */
1179 for (unsigned i = 0; i < p_stream->i_entry_count; i++)
1180 if (p_stream->entry[i].i_flags & BLOCK_FLAG_TYPE_I) {
1182 stss = box_full_new("stss", 0, 0);
1183 bo_add_32be(stss, 0); /* fixed later */
1185 bo_add_32be(stss, 1 + i);
1190 bo_fix_32be(stss, 12, i_index);
1192 /* Now gather all boxes into stbl */
1193 bo_t *stbl = box_new("stbl");
1195 box_gather(stbl, stsd);
1196 box_gather(stbl, stts);
1198 box_gather(stbl, stss);
1199 box_gather(stbl, stsc);
1200 box_gather(stbl, stsz);
1201 p_stream->i_stco_pos = stbl->len + 16;
1202 box_gather(stbl, stco);
1207 static int64_t get_timestamp(void);
1209 static const uint32_t mvhd_matrix[9] =
1210 { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
1212 static bo_t *GetMoovBox(sout_mux_t *p_mux)
1214 sout_mux_sys_t *p_sys = p_mux->p_sys;
1218 uint32_t i_movie_timescale = 90000;
1219 int64_t i_movie_duration = 0;
1220 int64_t i_timestamp = get_timestamp();
1222 moov = box_new("moov");
1224 /* Create general info */
1225 for (int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
1226 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1227 i_movie_duration = __MAX(i_movie_duration, p_stream->i_duration);
1229 msg_Dbg(p_mux, "movie duration %"PRId64"s", i_movie_duration / CLOCK_FREQ);
1231 i_movie_duration = i_movie_duration * i_movie_timescale / CLOCK_FREQ;
1233 /* *** add /moov/mvhd *** */
1234 if (!p_sys->b_64_ext) {
1235 mvhd = box_full_new("mvhd", 0, 0);
1236 bo_add_32be(mvhd, i_timestamp); // creation time
1237 bo_add_32be(mvhd, i_timestamp); // modification time
1238 bo_add_32be(mvhd, i_movie_timescale); // timescale
1239 bo_add_32be(mvhd, i_movie_duration); // duration
1241 mvhd = box_full_new("mvhd", 1, 0);
1242 bo_add_64be(mvhd, i_timestamp); // creation time
1243 bo_add_64be(mvhd, i_timestamp); // modification time
1244 bo_add_32be(mvhd, i_movie_timescale); // timescale
1245 bo_add_64be(mvhd, i_movie_duration); // duration
1247 bo_add_32be(mvhd, 0x10000); // rate
1248 bo_add_16be(mvhd, 0x100); // volume
1249 bo_add_16be(mvhd, 0); // reserved
1250 for (int i = 0; i < 2; i++)
1251 bo_add_32be(mvhd, 0); // reserved
1252 for (int i = 0; i < 9; i++)
1253 bo_add_32be(mvhd, mvhd_matrix[i]);// matrix
1254 for (int i = 0; i < 6; i++)
1255 bo_add_32be(mvhd, 0); // pre-defined
1257 /* Next available track id */
1258 bo_add_32be(mvhd, p_sys->i_nb_streams + 1); // next-track-id
1260 box_gather(moov, mvhd);
1262 for (int i_trak = 0; i_trak < p_sys->i_nb_streams; i_trak++) {
1263 mp4_stream_t *p_stream = p_sys->pp_streams[i_trak];
1265 uint32_t i_timescale;
1266 if (p_stream->fmt.i_cat == AUDIO_ES)
1267 i_timescale = p_stream->fmt.audio.i_rate;
1269 i_timescale = CLOCK_FREQ;
1271 /* *** add /moov/trak *** */
1272 bo_t *trak = box_new("trak");
1274 /* *** add /moov/trak/tkhd *** */
1276 if (!p_sys->b_64_ext) {
1278 tkhd = box_full_new("tkhd", 0, 0x0f);
1280 tkhd = box_full_new("tkhd", 0, 1);
1282 bo_add_32be(tkhd, i_timestamp); // creation time
1283 bo_add_32be(tkhd, i_timestamp); // modification time
1284 bo_add_32be(tkhd, p_stream->i_track_id);
1285 bo_add_32be(tkhd, 0); // reserved 0
1286 bo_add_32be(tkhd, p_stream->i_duration *
1287 (int64_t)i_movie_timescale / CLOCK_FREQ); // duration
1290 tkhd = box_full_new("tkhd", 1, 0x0f);
1292 tkhd = box_full_new("tkhd", 1, 1);
1294 bo_add_64be(tkhd, i_timestamp); // creation time
1295 bo_add_64be(tkhd, i_timestamp); // modification time
1296 bo_add_32be(tkhd, p_stream->i_track_id);
1297 bo_add_32be(tkhd, 0); // reserved 0
1298 bo_add_64be(tkhd, p_stream->i_duration *
1299 (int64_t)i_movie_timescale / CLOCK_FREQ); // duration
1302 for (int i = 0; i < 2; i++)
1303 bo_add_32be(tkhd, 0); // reserved
1304 bo_add_16be(tkhd, 0); // layer
1305 bo_add_16be(tkhd, 0); // pre-defined
1307 bo_add_16be(tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0);
1308 bo_add_16be(tkhd, 0); // reserved
1309 for (int i = 0; i < 9; i++)
1310 bo_add_32be(tkhd, mvhd_matrix[i]); // matrix
1311 if (p_stream->fmt.i_cat == AUDIO_ES) {
1312 bo_add_32be(tkhd, 0); // width (presentation)
1313 bo_add_32be(tkhd, 0); // height(presentation)
1314 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
1315 int i_width = p_stream->fmt.video.i_width << 16;
1316 if (p_stream->fmt.video.i_sar_num > 0 && p_stream->fmt.video.i_sar_den > 0) {
1317 i_width = (int64_t)p_stream->fmt.video.i_sar_num *
1318 ((int64_t)p_stream->fmt.video.i_width << 16) /
1319 p_stream->fmt.video.i_sar_den;
1321 // width (presentation)
1322 bo_add_32be(tkhd, i_width);
1323 // height(presentation)
1324 bo_add_32be(tkhd, p_stream->fmt.video.i_height << 16);
1326 int i_width = 320 << 16;
1328 for (int i = 0; i < p_sys->i_nb_streams; i++) {
1329 mp4_stream_t *tk = p_sys->pp_streams[i];
1330 if (tk->fmt.i_cat == VIDEO_ES) {
1331 if (tk->fmt.video.i_sar_num > 0 &&
1332 tk->fmt.video.i_sar_den > 0)
1333 i_width = (int64_t)tk->fmt.video.i_sar_num *
1334 ((int64_t)tk->fmt.video.i_width << 16) /
1335 tk->fmt.video.i_sar_den;
1337 i_width = tk->fmt.video.i_width << 16;
1338 i_height = tk->fmt.video.i_height;
1342 bo_add_32be(tkhd, i_width); // width (presentation)
1343 bo_add_32be(tkhd, i_height << 16); // height(presentation)
1346 box_gather(trak, tkhd);
1348 /* *** add /moov/trak/edts and elst */
1349 bo_t *edts = box_new("edts");
1350 bo_t *elst = box_full_new("elst", p_sys->b_64_ext ? 1 : 0, 0);
1351 if (p_stream->i_dts_start > p_sys->i_dts_start) {
1352 bo_add_32be(elst, 2);
1354 if (p_sys->b_64_ext) {
1355 bo_add_64be(elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1356 i_movie_timescale / CLOCK_FREQ);
1357 bo_add_64be(elst, -1);
1359 bo_add_32be(elst, (p_stream->i_dts_start-p_sys->i_dts_start) *
1360 i_movie_timescale / CLOCK_FREQ);
1361 bo_add_32be(elst, -1);
1363 bo_add_16be(elst, 1);
1364 bo_add_16be(elst, 0);
1366 bo_add_32be(elst, 1);
1368 if (p_sys->b_64_ext) {
1369 bo_add_64be(elst, p_stream->i_duration *
1370 i_movie_timescale / CLOCK_FREQ);
1371 bo_add_64be(elst, 0);
1373 bo_add_32be(elst, p_stream->i_duration *
1374 i_movie_timescale / CLOCK_FREQ);
1375 bo_add_32be(elst, 0);
1377 bo_add_16be(elst, 1);
1378 bo_add_16be(elst, 0);
1380 box_gather(edts, elst);
1381 box_gather(trak, edts);
1383 /* *** add /moov/trak/mdia *** */
1384 bo_t *mdia = box_new("mdia");
1388 if (!p_sys->b_64_ext) {
1389 mdhd = box_full_new("mdhd", 0, 0);
1390 bo_add_32be(mdhd, i_timestamp); // creation time
1391 bo_add_32be(mdhd, i_timestamp); // modification time
1392 bo_add_32be(mdhd, i_timescale); // timescale
1393 bo_add_32be(mdhd, p_stream->i_duration * (int64_t)i_timescale /
1394 CLOCK_FREQ); // duration
1396 mdhd = box_full_new("mdhd", 1, 0);
1397 bo_add_64be(mdhd, i_timestamp); // creation time
1398 bo_add_64be(mdhd, i_timestamp); // modification time
1399 bo_add_32be(mdhd, i_timescale); // timescale
1400 bo_add_64be(mdhd, p_stream->i_duration * (int64_t)i_timescale /
1401 CLOCK_FREQ); // duration
1404 if (p_stream->fmt.psz_language) {
1405 char *psz = p_stream->fmt.psz_language;
1406 const iso639_lang_t *pl = NULL;
1407 uint16_t lang = 0x0;
1409 if (strlen(psz) == 2)
1410 pl = GetLang_1(psz);
1411 else if (strlen(psz) == 3) {
1412 pl = GetLang_2B(psz);
1413 if (!strcmp(pl->psz_iso639_1, "??"))
1414 pl = GetLang_2T(psz);
1417 if (pl && strcmp(pl->psz_iso639_1, "??"))
1418 lang = ((pl->psz_iso639_2T[0] - 0x60) << 10) |
1419 ((pl->psz_iso639_2T[1] - 0x60) << 5) |
1420 ((pl->psz_iso639_2T[2] - 0x60));
1421 bo_add_16be(mdhd, lang); // language
1423 bo_add_16be(mdhd, 0 ); // language
1424 bo_add_16be(mdhd, 0 ); // predefined
1425 box_gather(mdia, mdhd);
1427 /* handler reference */
1428 bo_t *hdlr = box_full_new("hdlr", 0, 0);
1431 bo_add_fourcc(hdlr, "mhlr"); // media handler
1433 bo_add_32be(hdlr, 0);
1435 if (p_stream->fmt.i_cat == AUDIO_ES)
1436 bo_add_fourcc(hdlr, "soun");
1437 else if (p_stream->fmt.i_cat == VIDEO_ES)
1438 bo_add_fourcc(hdlr, "vide");
1439 else if (p_stream->fmt.i_cat == SPU_ES)
1440 bo_add_fourcc(hdlr, "text");
1442 bo_add_32be(hdlr, 0); // reserved
1443 bo_add_32be(hdlr, 0); // reserved
1444 bo_add_32be(hdlr, 0); // reserved
1447 bo_add_8(hdlr, 12); /* Pascal string for .mov */
1449 if (p_stream->fmt.i_cat == AUDIO_ES)
1450 bo_add_mem(hdlr, 12, (uint8_t*)"SoundHandler");
1451 else if (p_stream->fmt.i_cat == VIDEO_ES)
1452 bo_add_mem(hdlr, 12, (uint8_t*)"VideoHandler");
1454 bo_add_mem(hdlr, 12, (uint8_t*)"Text Handler");
1457 bo_add_8(hdlr, 0); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
1459 box_gather(mdia, hdlr);
1462 bo_t *minf = box_new("minf");
1465 if (p_stream->fmt.i_cat == AUDIO_ES) {
1468 smhd = box_full_new("smhd", 0, 0);
1469 bo_add_16be(smhd, 0); // balance
1470 bo_add_16be(smhd, 0); // reserved
1472 box_gather(minf, smhd);
1473 } else if (p_stream->fmt.i_cat == VIDEO_ES) {
1476 vmhd = box_full_new("vmhd", 0, 1);
1477 bo_add_16be(vmhd, 0); // graphicsmode
1478 for (int i = 0; i < 3; i++)
1479 bo_add_16be(vmhd, 0); // opcolor
1481 box_gather(minf, vmhd);
1482 } else if (p_stream->fmt.i_cat == SPU_ES) {
1483 bo_t *gmhd = box_new("gmhd");
1484 bo_t *gmin = box_full_new("gmin", 0, 1);
1486 bo_add_16be(gmin, 0); // graphicsmode
1487 for (int i = 0; i < 3; i++)
1488 bo_add_16be(gmin, 0); // opcolor
1489 bo_add_16be(gmin, 0); // balance
1490 bo_add_16be(gmin, 0); // reserved
1492 box_gather(gmhd, gmin);
1494 box_gather(minf, gmhd);
1498 bo_t *dinf = box_new("dinf");
1499 bo_t *dref = box_full_new("dref", 0, 0);
1500 bo_add_32be(dref, 1);
1501 bo_t *url = box_full_new("url ", 0, 0x01);
1502 box_gather(dref, url);
1503 box_gather(dinf, dref);
1505 /* append dinf to mdia */
1506 box_gather(minf, dinf);
1509 bo_t *stbl = GetStblBox(p_mux, p_stream);
1511 /* append stbl to minf */
1512 p_stream->i_stco_pos += minf->len;
1513 box_gather(minf, stbl);
1515 /* append minf to mdia */
1516 p_stream->i_stco_pos += mdia->len;
1517 box_gather(mdia, minf);
1519 /* append mdia to trak */
1520 p_stream->i_stco_pos += trak->len;
1521 box_gather(trak, mdia);
1523 /* append trak to moov */
1524 p_stream->i_stco_pos += moov->len;
1525 box_gather(moov, trak);
1528 /* Add user data tags */
1529 box_gather(moov, GetUdtaTag(p_mux));
1535 /****************************************************************************/
1537 static void bo_init(bo_t *p_bo)
1540 p_bo->b = block_Alloc(1024);
1543 static void bo_add_8(bo_t *p_bo, uint8_t i)
1545 if (p_bo->len >= p_bo->b->i_buffer)
1546 p_bo->b = block_Realloc(p_bo->b, 0, p_bo->b->i_buffer + 1024);
1548 p_bo->b->p_buffer[p_bo->len++] = i;
1551 static void bo_add_16be(bo_t *p_bo, uint16_t i)
1553 bo_add_8(p_bo, ((i >> 8) &0xff));
1554 bo_add_8(p_bo, i &0xff);
1557 static void bo_add_24be(bo_t *p_bo, uint32_t i)
1559 bo_add_8(p_bo, ((i >> 16) &0xff));
1560 bo_add_8(p_bo, ((i >> 8) &0xff));
1561 bo_add_8(p_bo, ( i &0xff));
1563 static void bo_add_32be(bo_t *p_bo, uint32_t i)
1565 bo_add_16be(p_bo, ((i >> 16) &0xffff));
1566 bo_add_16be(p_bo, i &0xffff);
1569 static void bo_fix_32be (bo_t *p_bo, int i_pos, uint32_t i)
1571 p_bo->b->p_buffer[i_pos ] = (i >> 24)&0xff;
1572 p_bo->b->p_buffer[i_pos + 1] = (i >> 16)&0xff;
1573 p_bo->b->p_buffer[i_pos + 2] = (i >> 8)&0xff;
1574 p_bo->b->p_buffer[i_pos + 3] = (i )&0xff;
1577 static void bo_add_64be(bo_t *p_bo, uint64_t i)
1579 bo_add_32be(p_bo, ((i >> 32) &0xffffffff));
1580 bo_add_32be(p_bo, i &0xffffffff);
1583 static void bo_add_fourcc(bo_t *p_bo, const char *fcc)
1585 bo_add_8(p_bo, fcc[0]);
1586 bo_add_8(p_bo, fcc[1]);
1587 bo_add_8(p_bo, fcc[2]);
1588 bo_add_8(p_bo, fcc[3]);
1591 static void bo_add_mem(bo_t *p_bo, int i_size, uint8_t *p_mem)
1593 for (int i = 0; i < i_size; i++)
1594 bo_add_8(p_bo, p_mem[i]);
1597 static void bo_add_descr(bo_t *p_bo, uint8_t tag, uint32_t size)
1599 bo_add_8(p_bo, tag);
1600 for (int i = 3; i>0; i--)
1601 bo_add_8(p_bo, (size>>(7*i)) | 0x80);
1602 bo_add_8(p_bo, size & 0x7F);
1605 static bo_t *box_new(const char *fcc)
1607 bo_t *box = malloc(sizeof(*box));
1613 bo_add_32be (box, 0);
1614 bo_add_fourcc(box, fcc);
1619 static bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f)
1621 bo_t *box = box_new(fcc);
1626 bo_add_24be (box, f);
1631 static void box_fix(bo_t *box)
1633 box->b->p_buffer[0] = box->len >> 24;
1634 box->b->p_buffer[1] = box->len >> 16;
1635 box->b->p_buffer[2] = box->len >> 8;
1636 box->b->p_buffer[3] = box->len;
1639 static void box_gather (bo_t *box, bo_t *box2)
1642 box->b = block_Realloc(box->b, 0, box->len + box2->len);
1643 memcpy(&box->b->p_buffer[box->len], box2->b->p_buffer, box2->len);
1644 box->len += box2->len;
1645 block_Release(box2->b);
1649 static void box_send(sout_mux_t *p_mux, bo_t *box)
1651 box->b->i_buffer = box->len;
1652 sout_AccessOutWrite(p_mux->p_access, box->b);
1656 static int64_t get_timestamp(void)
1658 int64_t i_timestamp = time(NULL);
1660 i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
1661 // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60