From ae9bab5d3d1c99c60818e1b89883ebec18f29c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20B=C3=A9rub=C3=A9?= Date: Mon, 12 Aug 2024 13:26:07 -0400 Subject: [PATCH] (Continued) --- src/mp4/moof.c | 30 +++++++++--------------------- src/mp4/moof.h | 6 ++---- src/mp4/mp4.c | 22 +++++++++++++--------- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/src/mp4/moof.c b/src/mp4/moof.c index 9debc82..8e625c4 100644 --- a/src/mp4/moof.c +++ b/src/mp4/moof.c @@ -4,7 +4,8 @@ uint32_t pos_sequence_number = 0; uint32_t pos_base_data_offset = 0; -uint32_t pos_base_media_decode_time = 0; +uint32_t pos_audio_media_decode_time = 0; +uint32_t pos_video_media_decode_time = 0; struct DataOffsetPos { bool data_offset_present; @@ -23,7 +24,7 @@ enum BufError write_tfhd( const uint64_t base_data_offset, const uint32_t default_sample_size, const uint32_t default_sample_duration, char is_audio); enum BufError -write_tfdt(struct BitBuf *ptr, const uint64_t base_media_decode_time); +write_tfdt(struct BitBuf *ptr, const uint64_t base_media_decode_time, char is_audio); enum BufError write_trun( struct BitBuf *ptr, const struct SampleInfo *samples_info, const uint32_t samples_info_count, struct DataOffsetPos *data_offset, char is_audio); @@ -149,7 +150,7 @@ enum BufError write_traf( ptr, sequence_number, base_data_offset, samples_info[0].size, default_sample_duration, is_audio); chk_err; - err = write_tfdt(ptr, base_media_decode_time); + err = write_tfdt(ptr, base_media_decode_time, is_audio); chk_err; err = write_trun(ptr, samples_info, samples_info_len, data_offset, is_audio); chk_err; @@ -236,7 +237,7 @@ enum BufError write_tfhd( } enum BufError -write_tfdt(struct BitBuf *ptr, const uint64_t base_media_decode_time) { +write_tfdt(struct BitBuf *ptr, const uint64_t base_media_decode_time, char is_audio) { enum BufError err; uint32_t start_atom = ptr->offset; err = put_u32_be(ptr, 0); @@ -253,7 +254,10 @@ write_tfdt(struct BitBuf *ptr, const uint64_t base_media_decode_time) { chk_err; err = put_u8(ptr, 0); chk_err; // 3 flags - pos_base_media_decode_time = ptr->offset; + if (is_audio) + pos_audio_media_decode_time = ptr->offset; + else + pos_video_media_decode_time = ptr->offset; err = put_u64_be(ptr, base_media_decode_time); chk_err; // 4 baseMediaDecodeTime err = put_u32_be_to_offset(ptr, start_atom, ptr->offset - start_atom); @@ -277,8 +281,6 @@ enum BufError write_trun( const bool first_sample_flags_present = false; const bool sample_duration_present = true; const bool sample_size_present = true; - const bool sample_flags_present = true; - const bool sample_composition_time_offsets_present = true; { uint64_t flags = 0x0; if (data_offset_present) { @@ -293,12 +295,6 @@ enum BufError write_trun( if (sample_size_present) { flags = flags | 0x000200; } // 0x000200 sample-size-present - if (sample_flags_present) { - flags = flags | 0x000400; - } // 0x000400 sample-flags-present - if (sample_composition_time_offsets_present) { - flags = flags | 0x000800; - } // 0x000800 sample-composition-time-offsets-present err = put_u8(ptr, flags >> 16); chk_err; @@ -334,14 +330,6 @@ enum BufError write_trun( err = put_u32_be(ptr, sample_info.size); chk_err; // 4 sample_size } - if (sample_flags_present) { - err = put_u32_be(ptr, sample_info.flags); - chk_err; // 4 sample_flags - } - if (sample_composition_time_offsets_present) { - err = put_i32_be(ptr, sample_info.composition_offset); - chk_err; // 4 sample_composition_offset - } } err = put_u32_be_to_offset(ptr, start_atom, ptr->offset - start_atom); diff --git a/src/mp4/moof.h b/src/mp4/moof.h index afbb2e6..5dab83b 100644 --- a/src/mp4/moof.h +++ b/src/mp4/moof.h @@ -4,13 +4,11 @@ extern uint32_t pos_sequence_number; extern uint32_t pos_base_data_offset; -extern uint32_t pos_base_media_decode_time; +extern uint32_t pos_audio_media_decode_time; +extern uint32_t pos_video_media_decode_time; struct SampleInfo { uint32_t duration; - uint32_t decode_time; - uint32_t composition_time; - uint32_t composition_offset; uint32_t size; uint32_t flags; }; diff --git a/src/mp4/mp4.c b/src/mp4/mp4.c index f73bad5..e3668ca 100644 --- a/src/mp4/mp4.c +++ b/src/mp4/mp4.c @@ -6,7 +6,7 @@ uint32_t default_sample_size = 40000; -unsigned int aud_samplerate = 0; +unsigned int aud_samplerate = 0, aud_framesize = 0; unsigned short aud_bitrate = 0; char aud_channels = 0; short vid_width = 1920, vid_height = 1080; @@ -71,6 +71,10 @@ void mp4_set_config(short width, short height, char framerate, char acodec, aud_bitrate = bitrate; aud_channels = channels; aud_samplerate = srate; + aud_framesize = + (aud_samplerate >= 32000 ? 144 : 72) * + (aud_bitrate * 1000) / + aud_samplerate; } void mp4_set_sps(const char *nal_data, const uint32_t nal_len, char is_h265) { @@ -99,23 +103,20 @@ enum BufError mp4_set_slice(const char *nal_data, const uint32_t nal_len, struct SampleInfo samples_info[2]; memset(samples_info, 0, sizeof(samples_info)); samples_info[0].size = nal_len + 4; // add size of sample - samples_info[0].composition_offset = default_sample_size; - samples_info[0].decode_time = default_sample_size; samples_info[0].duration = default_sample_size; samples_info[0].flags = is_iframe ? 0 : 65536; samples_info[1].size = buf_aud.offset; - samples_info[1].duration = default_sample_size; + samples_info[1].duration = buf_aud.offset / aud_framesize * aud_samplerate; buf_moof.offset = 0; err = write_moof( &buf_moof, 0, 0, 0, default_sample_size, samples_info, - samples_info_len, samples_info + 1, - buf_aud.offset >= 2304 ? 1 : 0); + samples_info_len, samples_info + 1, 1); chk_err; buf_mdat.offset = 0; err = write_mdat(&buf_mdat, nal_data, nal_len, - buf_aud.buf, buf_aud.offset >= 2304 ? buf_aud.offset : 0); + buf_aud.buf, buf_aud.offset); chk_err; buf_aud.offset = 0; @@ -138,8 +139,11 @@ enum BufError mp4_set_state(struct Mp4State *state) { &buf_moof, pos_sequence_number, state->sequence_number); chk_err if (pos_base_data_offset > 0) err = put_u64_be_to_offset( &buf_moof, pos_base_data_offset, state->base_data_offset); - chk_err if (pos_base_media_decode_time > 0) err = put_u64_be_to_offset( - &buf_moof, pos_base_media_decode_time, + chk_err if (pos_audio_media_decode_time > 0) err = put_u64_be_to_offset( + &buf_moof, pos_audio_media_decode_time, + state->base_media_decode_time); + chk_err if (pos_video_media_decode_time > 0) err = put_u64_be_to_offset( + &buf_moof, pos_video_media_decode_time, state->base_media_decode_time); chk_err state->sequence_number++; state->base_data_offset += buf_moof.offset + buf_mdat.offset;