Skip to content

Commit

Permalink
(Continued)
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Aug 12, 2024
1 parent d815fda commit ae9bab5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 34 deletions.
30 changes: 9 additions & 21 deletions src/mp4/moof.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions src/mp4/moof.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
22 changes: 13 additions & 9 deletions src/mp4/mp4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit ae9bab5

Please sign in to comment.