1 /*****************************************************************************
2  * mp4.c: mp4 muxer
3  *****************************************************************************
4  * Copyright (C) 2003-2021 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *
23  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26 
27 #include "output.h"
28 #include <gpac/isomedia.h>
29 
30 typedef struct
31 {
32     GF_ISOFile *p_file;
33     GF_AVCConfig *p_config;
34     GF_ISOSample *p_sample;
35     int i_track;
36     uint32_t i_descidx;
37     uint64_t i_time_res;
38     int64_t i_time_inc;
39     int64_t i_delay_time;
40     int64_t i_init_delta;
41     int i_numframe;
42     int i_delay_frames;
43     int b_dts_compress;
44     int i_dts_compress_multiplier;
45     int i_data_size;
46 } mp4_hnd_t;
47 
recompute_bitrate_mp4(GF_ISOFile * p_file,int i_track)48 static void recompute_bitrate_mp4( GF_ISOFile *p_file, int i_track )
49 {
50     u32 count, di, timescale, time_wnd, rate;
51     u64 offset;
52     Double br;
53     GF_ESD *esd;
54 
55     esd = gf_isom_get_esd( p_file, i_track, 1 );
56     if( !esd )
57         return;
58 
59     esd->decoderConfig->avgBitrate = 0;
60     esd->decoderConfig->maxBitrate = 0;
61     rate = time_wnd = 0;
62 
63     timescale = gf_isom_get_media_timescale( p_file, i_track );
64     count = gf_isom_get_sample_count( p_file, i_track );
65     for( u32 i = 0; i < count; i++ )
66     {
67         GF_ISOSample *samp = gf_isom_get_sample_info( p_file, i_track, i+1, &di, &offset );
68         if( !samp )
69         {
70             x264_cli_log( "mp4", X264_LOG_ERROR, "failure reading back frame %u\n", i );
71             break;
72         }
73 
74         if( esd->decoderConfig->bufferSizeDB < samp->dataLength )
75             esd->decoderConfig->bufferSizeDB = samp->dataLength;
76 
77         esd->decoderConfig->avgBitrate += samp->dataLength;
78         rate += samp->dataLength;
79         if( samp->DTS > time_wnd + timescale )
80         {
81             if( rate > esd->decoderConfig->maxBitrate )
82                 esd->decoderConfig->maxBitrate = rate;
83             time_wnd = samp->DTS;
84             rate = 0;
85         }
86 
87         gf_isom_sample_del( &samp );
88     }
89 
90     br = (Double)(s64)gf_isom_get_media_duration( p_file, i_track );
91     br /= timescale;
92     esd->decoderConfig->avgBitrate = (u32)(esd->decoderConfig->avgBitrate / br);
93     /*move to bps*/
94     esd->decoderConfig->avgBitrate *= 8;
95     esd->decoderConfig->maxBitrate *= 8;
96 
97     gf_isom_change_mpeg4_description( p_file, i_track, 1, esd );
98     gf_odf_desc_del( (GF_Descriptor*)esd );
99 }
100 
close_file(hnd_t handle,int64_t largest_pts,int64_t second_largest_pts)101 static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
102 {
103     mp4_hnd_t *p_mp4 = handle;
104 
105     if( !p_mp4 )
106         return 0;
107 
108     if( p_mp4->p_config )
109         gf_odf_avc_cfg_del( p_mp4->p_config );
110 
111     if( p_mp4->p_sample )
112     {
113         if( p_mp4->p_sample->data )
114             free( p_mp4->p_sample->data );
115 
116         p_mp4->p_sample->dataLength = 0;
117         gf_isom_sample_del( &p_mp4->p_sample );
118     }
119 
120     if( p_mp4->p_file )
121     {
122         if( p_mp4->i_track )
123         {
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 ) )
131             {
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 );
135             }
136 
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 )
143             {
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 #if GPAC_VERSION_MAJOR > 8
147                 gf_isom_append_edit( p_mp4->p_file, p_mp4->i_track, tkhd_duration, sample->CTS_Offset, GF_ISOM_EDIT_NORMAL );
148 #else
149                 gf_isom_append_edit_segment( p_mp4->p_file, p_mp4->i_track, tkhd_duration, sample->CTS_Offset, GF_ISOM_EDIT_NORMAL );
150 #endif
151             }
152             gf_isom_sample_del( &sample );
153 
154             recompute_bitrate_mp4( p_mp4->p_file, p_mp4->i_track );
155         }
156         gf_isom_set_pl_indication( p_mp4->p_file, GF_ISOM_PL_VISUAL, 0x15 );
157         gf_isom_set_storage_mode( p_mp4->p_file, GF_ISOM_STORE_FLAT );
158         gf_isom_close( p_mp4->p_file );
159     }
160 
161     free( p_mp4 );
162 
163     return 0;
164 }
165 
open_file(char * psz_filename,hnd_t * p_handle,cli_output_opt_t * opt)166 static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
167 {
168     *p_handle = NULL;
169     FILE *fh = x264_fopen( psz_filename, "w" );
170     if( !fh )
171         return -1;
172     int b_regular = x264_is_regular_file( fh );
173     fclose( fh );
174     FAIL_IF_ERR( !b_regular, "mp4", "MP4 output is incompatible with non-regular file `%s'\n", psz_filename );
175 
176     mp4_hnd_t *p_mp4 = calloc( 1, sizeof(mp4_hnd_t) );
177     if( !p_mp4 )
178         return -1;
179 
180     p_mp4->p_file = gf_isom_open( psz_filename, GF_ISOM_OPEN_WRITE, NULL );
181     p_mp4->b_dts_compress = opt->use_dts_compress;
182 
183     if( !(p_mp4->p_sample = gf_isom_sample_new()) )
184     {
185         close_file( p_mp4, 0, 0 );
186         return -1;
187     }
188 
189     gf_isom_set_brand_info( p_mp4->p_file, GF_ISOM_BRAND_AVC1, 0 );
190 
191     *p_handle = p_mp4;
192 
193     return 0;
194 }
195 
set_param(hnd_t handle,x264_param_t * p_param)196 static int set_param( hnd_t handle, x264_param_t *p_param )
197 {
198     mp4_hnd_t *p_mp4 = handle;
199 
200     p_mp4->i_delay_frames = p_param->i_bframe ? (p_param->i_bframe_pyramid ? 2 : 1) : 0;
201     p_mp4->i_dts_compress_multiplier = p_mp4->b_dts_compress * p_mp4->i_delay_frames + 1;
202 
203     p_mp4->i_time_res = (uint64_t)p_param->i_timebase_den * p_mp4->i_dts_compress_multiplier;
204     p_mp4->i_time_inc = (uint64_t)p_param->i_timebase_num * p_mp4->i_dts_compress_multiplier;
205     FAIL_IF_ERR( p_mp4->i_time_res > UINT32_MAX, "mp4", "MP4 media timescale %"PRIu64" exceeds maximum\n", p_mp4->i_time_res );
206 
207     p_mp4->i_track = gf_isom_new_track( p_mp4->p_file, 0, GF_ISOM_MEDIA_VISUAL,
208                                         p_mp4->i_time_res );
209 
210     p_mp4->p_config = gf_odf_avc_cfg_new();
211     gf_isom_avc_config_new( p_mp4->p_file, p_mp4->i_track, p_mp4->p_config,
212                             NULL, NULL, &p_mp4->i_descidx );
213 
214     gf_isom_set_track_enabled( p_mp4->p_file, p_mp4->i_track, 1 );
215 
216     gf_isom_set_visual_info( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx,
217                              p_param->i_width, p_param->i_height );
218 
219     if( p_param->vui.i_sar_width && p_param->vui.i_sar_height )
220     {
221         uint64_t dw = p_param->i_width << 16;
222         uint64_t dh = p_param->i_height << 16;
223         double sar = (double)p_param->vui.i_sar_width / p_param->vui.i_sar_height;
224         if( sar > 1.0 )
225             dw *= sar;
226         else
227             dh /= sar;
228         gf_isom_set_pixel_aspect_ratio( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, p_param->vui.i_sar_width, p_param->vui.i_sar_height, 0 );
229         gf_isom_set_track_layout_info( p_mp4->p_file, p_mp4->i_track, dw, dh, 0, 0, 0 );
230     }
231 
232     p_mp4->i_data_size = p_param->i_width * p_param->i_height * 3 / 2;
233     p_mp4->p_sample->data = malloc( p_mp4->i_data_size );
234     if( !p_mp4->p_sample->data )
235     {
236         p_mp4->i_data_size = 0;
237         return -1;
238     }
239 
240     return 0;
241 }
242 
check_buffer(mp4_hnd_t * p_mp4,int needed_size)243 static int check_buffer( mp4_hnd_t *p_mp4, int needed_size )
244 {
245     if( needed_size > p_mp4->i_data_size )
246     {
247         void *ptr = realloc( p_mp4->p_sample->data, needed_size );
248         if( !ptr )
249             return -1;
250         p_mp4->p_sample->data = ptr;
251         p_mp4->i_data_size = needed_size;
252     }
253     return 0;
254 }
255 
write_headers(hnd_t handle,x264_nal_t * p_nal)256 static int write_headers( hnd_t handle, x264_nal_t *p_nal )
257 {
258     mp4_hnd_t *p_mp4 = handle;
259     GF_AVCConfigSlot *p_slot;
260 
261     int sps_size = p_nal[0].i_payload - 4;
262     int pps_size = p_nal[1].i_payload - 4;
263     int sei_size = p_nal[2].i_payload;
264 
265     uint8_t *sps = p_nal[0].p_payload + 4;
266     uint8_t *pps = p_nal[1].p_payload + 4;
267     uint8_t *sei = p_nal[2].p_payload;
268 
269     // SPS
270 
271     p_mp4->p_config->configurationVersion = 1;
272     p_mp4->p_config->AVCProfileIndication = sps[1];
273     p_mp4->p_config->profile_compatibility = sps[2];
274     p_mp4->p_config->AVCLevelIndication = sps[3];
275     p_slot = malloc( sizeof(GF_AVCConfigSlot) );
276     if( !p_slot )
277         return -1;
278     p_slot->size = sps_size;
279     p_slot->data = malloc( p_slot->size );
280     if( !p_slot->data )
281         return -1;
282     memcpy( p_slot->data, sps, sps_size );
283     gf_list_add( p_mp4->p_config->sequenceParameterSets, p_slot );
284 
285     // PPS
286 
287     p_slot = malloc( sizeof(GF_AVCConfigSlot) );
288     if( !p_slot )
289         return -1;
290     p_slot->size = pps_size;
291     p_slot->data = malloc( p_slot->size );
292     if( !p_slot->data )
293         return -1;
294     memcpy( p_slot->data, pps, pps_size );
295     gf_list_add( p_mp4->p_config->pictureParameterSets, p_slot );
296     gf_isom_avc_config_update( p_mp4->p_file, p_mp4->i_track, 1, p_mp4->p_config );
297 
298     // SEI
299 
300     if( check_buffer( p_mp4, p_mp4->p_sample->dataLength + sei_size ) )
301         return -1;
302     memcpy( p_mp4->p_sample->data + p_mp4->p_sample->dataLength, sei, sei_size );
303     p_mp4->p_sample->dataLength += sei_size;
304 
305     return sei_size + sps_size + pps_size;
306 }
307 
write_frame(hnd_t handle,uint8_t * p_nalu,int i_size,x264_picture_t * p_picture)308 static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
309 {
310     mp4_hnd_t *p_mp4 = handle;
311     int64_t dts;
312     int64_t cts;
313 
314     if( check_buffer( p_mp4, p_mp4->p_sample->dataLength + i_size ) )
315         return -1;
316     memcpy( p_mp4->p_sample->data + p_mp4->p_sample->dataLength, p_nalu, i_size );
317     p_mp4->p_sample->dataLength += i_size;
318 
319     if( !p_mp4->i_numframe )
320         p_mp4->i_delay_time = p_picture->i_dts * -1;
321 
322     if( p_mp4->b_dts_compress )
323     {
324         if( p_mp4->i_numframe == 1 )
325             p_mp4->i_init_delta = (p_picture->i_dts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
326         dts = p_mp4->i_numframe > p_mp4->i_delay_frames
327             ? p_picture->i_dts * p_mp4->i_time_inc
328             : p_mp4->i_numframe * (p_mp4->i_init_delta / p_mp4->i_dts_compress_multiplier);
329         cts = p_picture->i_pts * p_mp4->i_time_inc;
330     }
331     else
332     {
333         dts = (p_picture->i_dts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
334         cts = (p_picture->i_pts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
335     }
336 
337     p_mp4->p_sample->IsRAP = p_picture->b_keyframe;
338     p_mp4->p_sample->DTS = dts;
339     p_mp4->p_sample->CTS_Offset = (uint32_t)(cts - dts);
340     gf_isom_add_sample( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, p_mp4->p_sample );
341 
342     p_mp4->p_sample->dataLength = 0;
343     p_mp4->i_numframe++;
344 
345     return i_size;
346 }
347 
348 const cli_output_t mp4_output = { open_file, set_param, write_headers, write_frame, close_file };
349