122
129
if( p_mp4->p_file )
124
/* The mdhd duration is defined as CTS[final] - CTS[0] + duration of last frame.
125
* The mdhd duration (in seconds) should be able to be longer than the tkhd duration since the track is managed by edts.
126
* So, if mdhd duration is equal to the last DTS or less, we give the last composition time delta to the last sample duration.
127
* And then, the mdhd duration is updated, but it time-wise doesn't give the actual duration.
128
* The tkhd duration is the actual track duration. */
129
uint64_t mdhd_duration = (2 * largest_pts - second_largest_pts) * p_mp4->i_time_inc;
130
if( mdhd_duration != gf_isom_get_media_duration( p_mp4->p_file, p_mp4->i_track ) )
132
uint64_t last_dts = gf_isom_get_sample_dts( p_mp4->p_file, p_mp4->i_track, p_mp4->i_numframe );
133
uint32_t last_duration = (uint32_t)( mdhd_duration > last_dts ? mdhd_duration - last_dts : (largest_pts - second_largest_pts) * p_mp4->i_time_inc );
134
gf_isom_set_last_sample_duration( p_mp4->p_file, p_mp4->i_track, last_duration );
137
/* Write an Edit Box if the first CTS offset is positive.
138
* A media_time is given by not the mvhd timescale but rather the mdhd timescale.
139
* The reason is that an Edit Box maps the presentation time-line to the media time-line.
140
* Any demuxers should follow the Edit Box if it exists. */
141
GF_ISOSample *sample = gf_isom_get_sample_info( p_mp4->p_file, p_mp4->i_track, 1, NULL, NULL );
142
if( sample && sample->CTS_Offset > 0 )
144
uint32_t mvhd_timescale = gf_isom_get_timescale( p_mp4->p_file );
145
uint64_t tkhd_duration = (uint64_t)( mdhd_duration * ( (double)mvhd_timescale / p_mp4->i_time_res ) );
146
gf_isom_append_edit_segment( p_mp4->p_file, p_mp4->i_track, tkhd_duration, sample->CTS_Offset, GF_ISOM_EDIT_NORMAL );
148
gf_isom_sample_del( &sample );
150
recompute_bitrate_mp4( p_mp4->p_file, p_mp4->i_track );
133
/* The mdhd duration is defined as CTS[final] - CTS[0] + duration of last frame.
134
* The mdhd duration (in seconds) should be able to be longer than the tkhd duration since the track is managed by edts.
135
* So, if mdhd duration is equal to the last DTS or less, we give the last composition time delta to the last sample duration.
136
* And then, the mdhd duration is updated, but it time-wise doesn't give the actual duration.
137
* The tkhd duration is the actual track duration. */
138
uint64_t mdhd_duration = (2 * largest_pts - second_largest_pts) * p_mp4->i_time_inc;
139
if( mdhd_duration != gf_isom_get_media_duration( p_mp4->p_file, p_mp4->i_track ) )
141
uint64_t last_dts = gf_isom_get_sample_dts( p_mp4->p_file, p_mp4->i_track, p_mp4->i_numframe );
142
uint32_t last_duration = (uint32_t)( mdhd_duration > last_dts ? mdhd_duration - last_dts : (largest_pts - second_largest_pts) * p_mp4->i_time_inc );
143
gf_isom_set_last_sample_duration( p_mp4->p_file, p_mp4->i_track, last_duration );
146
/* Write an Edit Box if the first CTS offset is positive.
147
* A media_time is given by not the mvhd timescale but rather the mdhd timescale.
148
* The reason is that an Edit Box maps the presentation time-line to the media time-line.
149
* Any demuxers should follow the Edit Box if it exists. */
150
GF_ISOSample *sample = gf_isom_get_sample_info( p_mp4->p_file, p_mp4->i_track, 1, NULL, NULL );
151
if( sample && sample->CTS_Offset > 0 )
153
uint32_t mvhd_timescale = gf_isom_get_timescale( p_mp4->p_file );
154
uint64_t tkhd_duration = (uint64_t)( mdhd_duration * ( (double)mvhd_timescale / p_mp4->i_time_res ) );
155
gf_isom_append_edit_segment( p_mp4->p_file, p_mp4->i_track, tkhd_duration, sample->CTS_Offset, GF_ISOM_EDIT_NORMAL );
157
gf_isom_sample_del( &sample );
159
recompute_bitrate_mp4( p_mp4->p_file, p_mp4->i_track );
151
161
gf_isom_set_pl_indication( p_mp4->p_file, GF_ISOM_PL_VISUAL, 0x15 );
152
162
gf_isom_set_storage_mode( p_mp4->p_file, GF_ISOM_STORE_FLAT );
153
163
gf_isom_close( p_mp4->p_file );
193
205
mp4_hnd_t *p_mp4 = handle;
195
p_mp4->i_time_res = p_param->i_timebase_den;
196
p_mp4->i_time_inc = p_param->i_timebase_num;
207
p_mp4->i_delay_frames = p_param->i_bframe ? (p_param->i_bframe_pyramid ? 2 : 1) : 0;
208
p_mp4->i_dts_compress_multiplier = p_mp4->b_dts_compress * p_mp4->i_delay_frames + 1;
210
p_mp4->i_time_res = p_param->i_timebase_den * p_mp4->i_dts_compress_multiplier;
211
p_mp4->i_time_inc = p_param->i_timebase_num * p_mp4->i_dts_compress_multiplier;
212
FAIL_IF_ERR( p_mp4->i_time_res > UINT32_MAX, "mp4", "MP4 media timescale %"PRIu64" exceeds maximum\n", p_mp4->i_time_res )
198
214
p_mp4->i_track = gf_isom_new_track( p_mp4->p_file, 0, GF_ISOM_MEDIA_VISUAL,
199
215
p_mp4->i_time_res );
220
236
gf_isom_set_track_layout_info( p_mp4->p_file, p_mp4->i_track, dw, dh, 0, 0, 0 );
223
p_mp4->p_sample->data = malloc( p_param->i_width * p_param->i_height * 3 / 2 );
239
p_mp4->i_data_size = p_param->i_width * p_param->i_height * 3 / 2;
240
p_mp4->p_sample->data = malloc( p_mp4->i_data_size );
224
241
if( !p_mp4->p_sample->data )
243
p_mp4->i_data_size = 0;
250
static int check_buffer( mp4_hnd_t *p_mp4, int needed_size )
252
if( needed_size > p_mp4->i_data_size )
254
void *ptr = realloc( p_mp4->p_sample->data, needed_size );
257
p_mp4->p_sample->data = ptr;
258
p_mp4->i_data_size = needed_size;
307
if( check_buffer( p_mp4, p_mp4->p_sample->dataLength + sei_size ) )
274
309
memcpy( p_mp4->p_sample->data + p_mp4->p_sample->dataLength, sei, sei_size );
275
310
p_mp4->p_sample->dataLength += sei_size;
277
312
return sei_size + sps_size + pps_size;
279
315
static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
281
317
mp4_hnd_t *p_mp4 = handle;
321
if( check_buffer( p_mp4, p_mp4->p_sample->dataLength + i_size ) )
285
323
memcpy( p_mp4->p_sample->data + p_mp4->p_sample->dataLength, p_nalu, i_size );
286
324
p_mp4->p_sample->dataLength += i_size;
288
326
if( !p_mp4->i_numframe )
289
327
p_mp4->i_delay_time = p_picture->i_dts * -1;
291
dts = (p_picture->i_dts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
292
cts = (p_picture->i_pts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
329
if( p_mp4->b_dts_compress )
331
if( p_mp4->i_numframe == 1 )
332
p_mp4->i_init_delta = (p_picture->i_dts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
333
dts = p_mp4->i_numframe > p_mp4->i_delay_frames
334
? p_picture->i_dts * p_mp4->i_time_inc
335
: p_mp4->i_numframe * (p_mp4->i_init_delta / p_mp4->i_dts_compress_multiplier);
336
cts = p_picture->i_pts * p_mp4->i_time_inc;
340
dts = (p_picture->i_dts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
341
cts = (p_picture->i_pts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
294
344
p_mp4->p_sample->IsRAP = p_picture->b_keyframe;
295
345
p_mp4->p_sample->DTS = dts;