7#include <packager/media/formats/mp4/mp4_muxer.h>
12#include <absl/log/check.h>
13#include <absl/strings/escaping.h>
14#include <absl/strings/numbers.h>
16#include <packager/file.h>
17#include <packager/macros/logging.h>
18#include <packager/macros/status.h>
19#include <packager/media/base/aes_encryptor.h>
20#include <packager/media/base/audio_stream_info.h>
21#include <packager/media/base/fourccs.h>
22#include <packager/media/base/key_source.h>
23#include <packager/media/base/media_sample.h>
24#include <packager/media/base/text_stream_info.h>
25#include <packager/media/base/video_stream_info.h>
26#include <packager/media/codecs/es_descriptor.h>
27#include <packager/media/event/muxer_listener.h>
28#include <packager/media/formats/mp4/box_definitions.h>
29#include <packager/media/formats/mp4/low_latency_segment_segmenter.h>
30#include <packager/media/formats/mp4/multi_segment_segmenter.h>
31#include <packager/media/formats/mp4/single_segment_segmenter.h>
32#include <packager/media/formats/ttml/ttml_generator.h>
42void SetStartAndEndFromOffsetAndSize(
size_t offset,
size_t size, Range* range) {
44 range->start =
static_cast<uint32_t
>(offset);
46 range->end = range->start +
static_cast<uint32_t
>(size) - 1;
49FourCC CodecToFourCC(Codec codec, H26xStreamFormat h26x_stream_format) {
54 return h26x_stream_format ==
55 H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
59 return h26x_stream_format ==
60 H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
63 case kCodecH265DolbyVision:
64 return h26x_stream_format ==
65 H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
110void GenerateSinf(FourCC old_type,
111 const EncryptionConfig& encryption_config,
112 ProtectionSchemeInfo* sinf) {
113 sinf->format.format = old_type;
115 DCHECK_NE(encryption_config.protection_scheme, FOURCC_NULL);
116 sinf->type.type = encryption_config.protection_scheme;
119 const int kCencSchemeVersion = 0x00010000;
120 sinf->type.version = kCencSchemeVersion;
122 auto& track_encryption = sinf->info.track_encryption;
123 track_encryption.default_is_protected = 1;
125 track_encryption.default_crypt_byte_block =
126 encryption_config.crypt_byte_block;
127 track_encryption.default_skip_byte_block = encryption_config.skip_byte_block;
128 switch (encryption_config.protection_scheme) {
131 DCHECK_EQ(track_encryption.default_crypt_byte_block, 0u);
132 DCHECK_EQ(track_encryption.default_skip_byte_block, 0u);
135 track_encryption.version = 0;
142 track_encryption.version = 1;
145 NOTIMPLEMENTED() <<
"Unexpected protection scheme "
146 << encryption_config.protection_scheme;
149 track_encryption.default_per_sample_iv_size =
150 encryption_config.per_sample_iv_size;
151 track_encryption.default_constant_iv = encryption_config.constant_iv;
152 track_encryption.default_kid = encryption_config.key_id;
157int16_t GetRollDistance(uint64_t seek_preroll_ns, uint32_t sampling_frequency) {
158 const double kNanosecondsPerSecond = 1000000000;
159 const double preroll_in_samples =
160 seek_preroll_ns / kNanosecondsPerSecond * sampling_frequency;
162 return -
static_cast<int16_t
>(preroll_in_samples + 0.5);
168MP4Muxer::~MP4Muxer() {}
170Status MP4Muxer::InitializeMuxer() {
172 to_be_initialized_ =
true;
176Status MP4Muxer::Finalize() {
181 DCHECK(to_be_initialized_);
183 <<
"' which does not contain any sample.";
187 Status segmenter_finalized = segmenter_->Finalize();
189 if (!segmenter_finalized.ok())
190 return segmenter_finalized;
192 FireOnMediaEndEvent();
197Status MP4Muxer::AddMediaSample(
size_t stream_id,
const MediaSample& sample) {
198 if (to_be_initialized_) {
199 RETURN_IF_ERROR(UpdateEditListOffsetFromSample(sample));
200 RETURN_IF_ERROR(DelayInitializeMuxer());
201 to_be_initialized_ =
false;
204 return segmenter_->AddSample(stream_id, sample);
207Status MP4Muxer::FinalizeSegment(
size_t stream_id,
208 const SegmentInfo& segment_info) {
210 VLOG(3) <<
"Finalizing " << (segment_info.is_subsegment ?
"sub" :
"")
211 <<
"segment " << segment_info.start_timestamp <<
" duration "
212 << segment_info.duration <<
" segment number "
213 << segment_info.segment_number;
214 return segmenter_->FinalizeSegment(stream_id, segment_info);
217Status MP4Muxer::DelayInitializeMuxer() {
218 DCHECK(!streams().empty());
220 std::unique_ptr<FileType> ftyp(
new FileType);
221 std::unique_ptr<Movie> moov(
new Movie);
223 ftyp->major_brand = FOURCC_mp41;
224 ftyp->compatible_brands.push_back(FOURCC_iso8);
225 ftyp->compatible_brands.push_back(FOURCC_isom);
226 ftyp->compatible_brands.push_back(FOURCC_mp41);
227 ftyp->compatible_brands.push_back(FOURCC_dash);
229 if (streams().size() == 1) {
230 FourCC codec_fourcc = FOURCC_NULL;
231 if (streams()[0]->stream_type() == kStreamVideo) {
233 CodecToFourCC(streams()[0]->codec(),
234 static_cast<const VideoStreamInfo*
>(streams()[0].get())
235 ->h26x_stream_format());
236 if (codec_fourcc != FOURCC_NULL)
237 ftyp->compatible_brands.push_back(codec_fourcc);
240 std::string codec_string =
241 static_cast<const VideoStreamInfo*
>(streams()[0].get())
243 std::string supplemental_codec_string =
244 static_cast<const VideoStreamInfo*
>(streams()[0].get())
245 ->supplemental_codec();
246 if (codec_string.find(
"dvh") != std::string::npos ||
247 supplemental_codec_string.find(
"dvh") != std::string::npos ||
248 codec_string.find(
"dav1") != std::string::npos ||
249 supplemental_codec_string.find(
"dav1") != std::string::npos)
250 ftyp->compatible_brands.push_back(FOURCC_dby1);
252 static_cast<const VideoStreamInfo*
>(streams()[0].get())
253 ->compatible_brand();
254 if (extra_brand != FOURCC_NULL)
255 ftyp->compatible_brands.push_back(extra_brand);
261 if (codec_fourcc != FOURCC_avc3 && codec_fourcc != FOURCC_hev1)
262 ftyp->compatible_brands.push_back(FOURCC_cmfc);
264 if (streams()[0]->stream_type() == kStreamAudio) {
266 CodecToFourCC(streams()[0]->codec(), H26xStreamFormat::kUnSpecified);
267 if (codec_fourcc == FOURCC_iamf)
268 ftyp->compatible_brands.push_back(FOURCC_iamf);
272 moov->header.creation_time = IsoTimeNow();
273 moov->header.modification_time = IsoTimeNow();
274 moov->header.next_track_id =
static_cast<uint32_t
>(streams().size()) + 1;
276 moov->tracks.resize(streams().size());
277 moov->extends.tracks.resize(streams().size());
280 for (uint32_t i = 0; i < streams().size(); ++i) {
281 const StreamInfo* stream = streams()[i].get();
282 Track& trak = moov->tracks[i];
283 trak.header.track_id = i + 1;
285 TrackExtends& trex = moov->extends.tracks[i];
286 trex.track_id = trak.header.track_id;
287 trex.default_sample_description_index = 1;
289 bool generate_trak_result =
false;
290 switch (stream->stream_type()) {
292 generate_trak_result = GenerateVideoTrak(
293 static_cast<const VideoStreamInfo*
>(stream), &trak);
296 generate_trak_result = GenerateAudioTrak(
297 static_cast<const AudioStreamInfo*
>(stream), &trak);
300 generate_trak_result =
301 GenerateTextTrak(
static_cast<const TextStreamInfo*
>(stream), &trak);
304 NOTIMPLEMENTED() <<
"Not implemented for stream type: "
305 << stream->stream_type();
307 if (!generate_trak_result)
308 return Status(error::MUXER_FAILURE,
"Failed to generate trak.");
312 if (edit_list_offset_.value() > 0) {
314 entry.media_time = edit_list_offset_.value();
315 entry.media_rate_integer = 1;
316 trak.edit.list.edits.push_back(entry);
319 if (stream->is_encrypted() && options().mp4_params.include_pssh_in_stream) {
321 const auto& key_system_info = stream->encryption_config().key_system_info;
322 for (
const ProtectionSystemSpecificInfo& system : key_system_info) {
323 if (system.psshs.empty())
325 ProtectionSystemSpecificHeader pssh;
326 pssh.raw_box = system.psshs;
327 moov->pssh.push_back(pssh);
332 if (options().segment_template.empty()) {
333 segmenter_.reset(
new SingleSegmentSegmenter(options(), std::move(ftyp),
335 }
else if (options().mp4_params.low_latency_dash_mode) {
336 segmenter_.reset(
new LowLatencySegmentSegmenter(options(), std::move(ftyp),
340 new MultiSegmentSegmenter(options(), std::move(ftyp), std::move(moov)));
343 const Status segmenter_initialized =
344 segmenter_->Initialize(streams(), muxer_listener(), progress_listener());
345 if (!segmenter_initialized.ok())
346 return segmenter_initialized;
348 FireOnMediaStartEvent();
352Status MP4Muxer::UpdateEditListOffsetFromSample(
const MediaSample& sample) {
353 if (edit_list_offset_)
356 const int64_t pts = sample.pts();
357 const int64_t dts = sample.dts();
389 const int64_t pts_dts_offset = pts - dts;
390 if (pts_dts_offset > 0) {
392 LOG(ERROR) <<
"Negative presentation timestamp (" << pts
393 <<
") is not supported when there is an offset between "
394 "presentation timestamp and decoding timestamp ("
396 return Status(error::MUXER_FAILURE,
397 "Unsupported negative pts when there is an offset between "
400 edit_list_offset_ = pts_dts_offset;
403 if (pts_dts_offset < 0) {
404 LOG(ERROR) <<
"presentation timestamp (" << pts
405 <<
") is not supposed to be greater than decoding timestamp ("
407 return Status(error::MUXER_FAILURE,
"Not expecting pts < dts.");
409 edit_list_offset_ = std::max(-sample.pts(),
static_cast<int64_t
>(0));
413void MP4Muxer::InitializeTrak(
const StreamInfo* info, Track* trak) {
414 int64_t now = IsoTimeNow();
415 trak->header.creation_time = now;
416 trak->header.modification_time = now;
417 trak->header.duration = 0;
418 trak->media.header.creation_time = now;
419 trak->media.header.modification_time = now;
420 trak->media.header.timescale = info->time_scale();
421 trak->media.header.duration = 0;
422 if (!info->language().empty()) {
424 std::string main_language = info->language();
425 size_t dash = main_language.find(
'-');
426 if (dash != std::string::npos) {
427 main_language.erase(dash);
431 if (main_language.size() != 3) {
432 LOG(WARNING) <<
"'" << main_language <<
"' is not a valid ISO-639-2 "
433 <<
"language code, ignoring.";
435 trak->media.header.language.code = main_language;
440bool MP4Muxer::GenerateVideoTrak(
const VideoStreamInfo* video_info,
442 InitializeTrak(video_info, trak);
446 uint32_t pixel_width = video_info->pixel_width();
447 uint32_t pixel_height = video_info->pixel_height();
448 if (pixel_width == 0 || pixel_height == 0) {
449 LOG(WARNING) <<
"pixel width/height are not set. Assuming 1:1.";
453 const double sample_aspect_ratio =
454 static_cast<double>(pixel_width) / pixel_height;
455 trak->header.width = video_info->width() * sample_aspect_ratio * 0x10000;
456 trak->header.height = video_info->height() * 0x10000;
458 VideoSampleEntry video;
460 CodecToFourCC(video_info->codec(), video_info->h26x_stream_format());
461 video.width = video_info->width();
462 video.height = video_info->height();
463 video.colr.raw_box = video_info->colr_data();
464 video.codec_configuration.data = video_info->codec_config();
465 if (!video.ParseExtraCodecConfigsVector(video_info->extra_config())) {
466 LOG(ERROR) <<
"Malformed extra codec configs: "
467 << absl::BytesToHexString(
468 absl::string_view(
reinterpret_cast<const char*
>(
469 video_info->extra_config().data()),
470 video_info->extra_config().size()));
473 if (pixel_width != 1 || pixel_height != 1) {
474 video.pixel_aspect.h_spacing = pixel_width;
475 video.pixel_aspect.v_spacing = pixel_height;
478 SampleDescription& sample_description =
479 trak->media.information.sample_table.description;
480 sample_description.type = kVideo;
481 sample_description.video_entries.push_back(video);
483 if (video_info->is_encrypted()) {
484 if (video_info->has_clear_lead()) {
486 sample_description.video_entries.push_back(video);
489 VideoSampleEntry& entry = sample_description.video_entries[0];
490 GenerateSinf(entry.format, video_info->encryption_config(), &entry.sinf);
491 entry.format = FOURCC_encv;
496bool MP4Muxer::GenerateAudioTrak(
const AudioStreamInfo* audio_info,
498 InitializeTrak(audio_info, trak);
500 trak->header.volume = 0x100;
502 AudioSampleEntry audio;
504 CodecToFourCC(audio_info->codec(), H26xStreamFormat::kUnSpecified);
505 switch (audio_info->codec()) {
507 DecoderConfigDescriptor* decoder_config =
508 audio.esds.es_descriptor.mutable_decoder_config_descriptor();
509 decoder_config->set_object_type(ObjectType::kISO_14496_3);
510 decoder_config->set_max_bitrate(audio_info->max_bitrate());
511 decoder_config->set_avg_bitrate(audio_info->avg_bitrate());
512 decoder_config->mutable_decoder_specific_info_descriptor()->set_data(
513 audio_info->codec_config());
521 audio.ddts.extra_data = audio_info->codec_config();
522 audio.ddts.max_bitrate = audio_info->max_bitrate();
523 audio.ddts.avg_bitrate = audio_info->avg_bitrate();
524 audio.ddts.sampling_frequency = audio_info->sampling_frequency();
525 audio.ddts.pcm_sample_depth = audio_info->sample_bits();
528 audio.udts.data = audio_info->codec_config();
531 audio.dac3.data = audio_info->codec_config();
534 audio.dec3.data = audio_info->codec_config();
537 audio.dac4.data = audio_info->codec_config();
540 audio.alac.data = audio_info->codec_config();
543 audio.dfla.data = audio_info->codec_config();
546 DecoderConfigDescriptor* decoder_config =
547 audio.esds.es_descriptor.mutable_decoder_config_descriptor();
548 uint32_t samplerate = audio_info->sampling_frequency();
549 if (samplerate < 32000)
550 decoder_config->set_object_type(ObjectType::kISO_13818_3_MPEG1);
552 decoder_config->set_object_type(ObjectType::kISO_11172_3_MPEG1);
553 decoder_config->set_max_bitrate(audio_info->max_bitrate());
554 decoder_config->set_avg_bitrate(audio_info->avg_bitrate());
564 audio.dops.opus_identification_header = audio_info->codec_config();
567 audio.iacb.data = audio_info->codec_config();
571 audio.mhac.data = audio_info->codec_config();
574 NOTIMPLEMENTED() <<
" Unsupported audio codec " << audio_info->codec();
578 if (audio_info->codec() == kCodecAC3 || audio_info->codec() == kCodecEAC3) {
581 audio.channelcount = 2;
582 audio.samplesize = 16;
583 }
else if (audio_info->codec() == kCodecAC4) {
586 audio.channelcount = audio_info->num_channels();
588 audio.samplesize = 16;
589 }
else if (audio_info->codec() == kCodecIAMF) {
592 audio.channelcount = 0;
594 audio.channelcount = audio_info->num_channels();
595 audio.samplesize = audio_info->sample_bits();
601 audio_info->codec() == kCodecIAMF ? 0 : audio_info->sampling_frequency();
603 SampleTable& sample_table = trak->media.information.sample_table;
604 SampleDescription& sample_description = sample_table.description;
605 sample_description.type = kAudio;
606 sample_description.audio_entries.push_back(audio);
608 if (audio_info->is_encrypted()) {
609 if (audio_info->has_clear_lead()) {
611 sample_description.audio_entries.push_back(audio);
614 AudioSampleEntry& entry = sample_description.audio_entries[0];
615 GenerateSinf(entry.format, audio_info->encryption_config(), &entry.sinf);
616 entry.format = FOURCC_enca;
619 if (audio_info->seek_preroll_ns() > 0) {
620 sample_table.sample_group_descriptions.resize(1);
621 SampleGroupDescription& sample_group_description =
622 sample_table.sample_group_descriptions.back();
623 sample_group_description.grouping_type = FOURCC_roll;
624 sample_group_description.audio_roll_recovery_entries.resize(1);
625 sample_group_description.audio_roll_recovery_entries[0].roll_distance =
626 GetRollDistance(audio_info->seek_preroll_ns(), audio.samplerate);
633bool MP4Muxer::GenerateTextTrak(
const TextStreamInfo* text_info, Track* trak) {
634 InitializeTrak(text_info, trak);
636 if (text_info->codec_string() ==
"wvtt") {
638 TextSampleEntry webvtt;
639 webvtt.format = FOURCC_wvtt;
645 webvtt.config.config =
"WEBVTT";
648 if (!text_info->regions().empty() || !text_info->css_styles().empty()) {
649 LOG(INFO) <<
"Skipping possible style / region configuration as the spec "
650 "does not define a way to carry them inside ISO-BMFF files.";
656 webvtt.label.source_label =
"source_label";
657 SampleDescription& sample_description =
658 trak->media.information.sample_table.description;
659 sample_description.type = kText;
660 sample_description.text_entries.push_back(webvtt);
662 }
else if (text_info->codec_string() ==
"ttml") {
664 TextSampleEntry ttml;
665 ttml.format = FOURCC_stpp;
666 ttml.namespace_ = ttml::TtmlGenerator::kTtNamespace;
668 SampleDescription& sample_description =
669 trak->media.information.sample_table.description;
670 sample_description.type = kSubtitle;
671 sample_description.text_entries.push_back(ttml);
674 NOTIMPLEMENTED() << text_info->codec_string()
675 <<
" handling not implemented yet.";
679std::optional<Range> MP4Muxer::GetInitRangeStartAndEnd() {
680 size_t range_offset = 0;
681 size_t range_size = 0;
682 const bool has_range = segmenter_->GetInitRange(&range_offset, &range_size);
688 SetStartAndEndFromOffsetAndSize(range_offset, range_size, &range);
692std::optional<Range> MP4Muxer::GetIndexRangeStartAndEnd() {
693 size_t range_offset = 0;
694 size_t range_size = 0;
695 const bool has_range = segmenter_->GetIndexRange(&range_offset, &range_size);
701 SetStartAndEndFromOffsetAndSize(range_offset, range_size, &range);
705void MP4Muxer::FireOnMediaStartEvent() {
706 if (!muxer_listener())
709 if (streams().size() > 1) {
710 LOG(ERROR) <<
"MuxerListener cannot take more than 1 stream.";
713 DCHECK(!streams().empty()) <<
"Media started without a stream.";
715 const int32_t timescale = segmenter_->GetReferenceTimeScale();
716 muxer_listener()->
OnMediaStart(options(), *streams().front(), timescale,
717 MuxerListener::kContainerMp4);
720void MP4Muxer::FireOnMediaEndEvent() {
721 if (!muxer_listener())
724 MuxerListener::MediaRanges media_range;
725 media_range.init_range = GetInitRangeStartAndEnd();
726 media_range.index_range = GetIndexRangeStartAndEnd();
727 media_range.subsegment_ranges = segmenter_->GetSegmentRanges();
729 const float duration_seconds =
static_cast<float>(segmenter_->GetDuration());
730 muxer_listener()->
OnMediaEnd(media_range, duration_seconds);
733uint64_t MP4Muxer::IsoTimeNow() {
735 const uint64_t kIsomTimeOffset = 2082844800l;
738 std::int64_t secondsSince1970 = Now();
741 return secondsSince1970 + kIsomTimeOffset;
All the methods that are virtual are virtual for mocking.