]> git.sesse.net Git - x264/blob - output/mp4.c
Periodic intra refresh
[x264] / output / mp4.c
1 /*****************************************************************************
2  * mp4.c: x264 mp4 output module
3  *****************************************************************************
4  * Copyright (C) 2003-2009 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
24 #include "muxers.h"
25 #include <gpac/isomedia.h>
26
27 typedef struct
28 {
29     GF_ISOFile *p_file;
30     GF_AVCConfig *p_config;
31     GF_ISOSample *p_sample;
32     int i_track;
33     uint32_t i_descidx;
34     int i_time_res;
35     int64_t i_time_inc;
36     int i_numframe;
37     int i_init_delay;
38     int i_delay_time;
39
40     int64_t i_prev_timestamps[2];
41     int64_t i_init_delta;
42 } mp4_hnd_t;
43
44 static void recompute_bitrate_mp4( GF_ISOFile *p_file, int i_track )
45 {
46     u32 i, count, di, timescale, time_wnd, rate;
47     u64 offset;
48     Double br;
49     GF_ESD *esd;
50
51     esd = gf_isom_get_esd( p_file, i_track, 1 );
52     if( !esd )
53         return;
54
55     esd->decoderConfig->avgBitrate = 0;
56     esd->decoderConfig->maxBitrate = 0;
57     rate = time_wnd = 0;
58
59     timescale = gf_isom_get_media_timescale( p_file, i_track );
60     count = gf_isom_get_sample_count( p_file, i_track );
61     for( i = 0; i < count; i++ )
62     {
63         GF_ISOSample *samp = gf_isom_get_sample_info( p_file, i_track, i+1, &di, &offset );
64         if( !samp )
65         {
66             fprintf( stderr, "mp4 [error]: failure reading back frame %u\n", i );
67             break;
68         }
69
70         if( esd->decoderConfig->bufferSizeDB < samp->dataLength )
71             esd->decoderConfig->bufferSizeDB = samp->dataLength;
72
73         esd->decoderConfig->avgBitrate += samp->dataLength;
74         rate += samp->dataLength;
75         if( samp->DTS > time_wnd + timescale )
76         {
77             if( rate > esd->decoderConfig->maxBitrate )
78                 esd->decoderConfig->maxBitrate = rate;
79             time_wnd = samp->DTS;
80             rate = 0;
81         }
82
83         gf_isom_sample_del( &samp );
84     }
85
86     br = (Double)(s64)gf_isom_get_media_duration( p_file, i_track );
87     br /= timescale;
88     esd->decoderConfig->avgBitrate = (u32)(esd->decoderConfig->avgBitrate / br);
89     /*move to bps*/
90     esd->decoderConfig->avgBitrate *= 8;
91     esd->decoderConfig->maxBitrate *= 8;
92
93     gf_isom_change_mpeg4_description( p_file, i_track, 1, esd );
94     gf_odf_desc_del( (GF_Descriptor*)esd );
95 }
96
97 static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
98 {
99     mp4_hnd_t *p_mp4 = handle;
100     uint64_t total_duration = 0;
101
102     if( !p_mp4 )
103         return 0;
104
105     if( p_mp4->p_config )
106         gf_odf_avc_cfg_del( p_mp4->p_config );
107
108     if( p_mp4->p_sample )
109     {
110         if( p_mp4->p_sample->data )
111             free( p_mp4->p_sample->data );
112
113         gf_isom_sample_del( &p_mp4->p_sample );
114     }
115
116     if( p_mp4->p_file )
117     {
118         /* The mdhd duration is defined as CTS[final] - CTS[0] + duration of last frame.
119          * The mdhd duration (in seconds) should be able to be longer than the tkhd duration since the track is managed by edts.
120          * So, if mdhd duration is equal to the last DTS or less, we give the last composition time delta to the last sample duration.
121          * And then, the mdhd duration is updated, but it time-wise doesn't give the actual duration.
122          * The tkhd duration is the actual track duration. */
123         uint64_t mdhd_duration = (2 * largest_pts - second_largest_pts - p_mp4->i_delay_time) * p_mp4->i_time_inc;
124         total_duration = gf_isom_get_media_duration( p_mp4->p_file, p_mp4->i_track );
125         if( mdhd_duration != total_duration )
126         {
127             uint64_t last_dts = gf_isom_get_sample_dts( p_mp4->p_file, p_mp4->i_track, p_mp4->i_numframe );
128             uint32_t last_duration = (uint32_t)( mdhd_duration > last_dts ? mdhd_duration - last_dts : (largest_pts - second_largest_pts) * p_mp4->i_time_inc  );
129             gf_isom_set_last_sample_duration( p_mp4->p_file, p_mp4->i_track, last_duration );
130             total_duration = gf_isom_get_media_duration( p_mp4->p_file, p_mp4->i_track );
131         }
132
133         /* Write an Edit Box if the first CTS offset is positive.
134          * A media_time is given by not the mvhd timescale but rather the mdhd timescale.
135          * The reason is that an Edit Box maps the presentation time-line to the media time-line.
136          * Any demuxers should follow the Edit Box if it exists. */
137         GF_ISOSample *sample = gf_isom_get_sample_info( p_mp4->p_file, p_mp4->i_track, 1, NULL, NULL );
138         if( sample->CTS_Offset > 0 )
139         {
140             uint32_t mvhd_timescale = gf_isom_get_timescale( p_mp4->p_file );
141             uint64_t tkhd_duration = (uint64_t)( mdhd_duration * ( (double)mvhd_timescale / p_mp4->i_time_res ) );
142             gf_isom_append_edit_segment( p_mp4->p_file, p_mp4->i_track, tkhd_duration, sample->CTS_Offset, GF_ISOM_EDIT_NORMAL );
143         }
144         gf_isom_sample_del( &sample );
145
146         recompute_bitrate_mp4( p_mp4->p_file, p_mp4->i_track );
147         gf_isom_set_pl_indication( p_mp4->p_file, GF_ISOM_PL_VISUAL, 0x15 );
148         gf_isom_set_storage_mode( p_mp4->p_file, GF_ISOM_STORE_FLAT );
149         gf_isom_close( p_mp4->p_file );
150     }
151
152     free( p_mp4 );
153
154     return 0;
155 }
156
157 static int open_file( char *psz_filename, hnd_t *p_handle )
158 {
159     mp4_hnd_t *p_mp4;
160
161     *p_handle = NULL;
162     FILE *fh = fopen( psz_filename, "w" );
163     if( !fh )
164         return -1;
165     else if( !x264_is_regular_file( fh ) )
166     {
167         fprintf( stderr, "mp4 [error]: MP4 output is incompatible with non-regular file `%s'\n", psz_filename );
168         return -1;
169     }
170     fclose( fh );
171
172     if( !(p_mp4 = malloc( sizeof(mp4_hnd_t) )) )
173         return -1;
174
175     memset( p_mp4, 0, sizeof(mp4_hnd_t) );
176     p_mp4->p_file = gf_isom_open( psz_filename, GF_ISOM_OPEN_WRITE, NULL );
177
178     if( !(p_mp4->p_sample = gf_isom_sample_new()) )
179     {
180         close_file( p_mp4, 0, 0 );
181         return -1;
182     }
183
184     gf_isom_set_brand_info( p_mp4->p_file, GF_ISOM_BRAND_AVC1, 0 );
185
186     *p_handle = p_mp4;
187
188     return 0;
189 }
190
191 static int set_param( hnd_t handle, x264_param_t *p_param )
192 {
193     mp4_hnd_t *p_mp4 = handle;
194
195     p_mp4->i_time_res = p_param->i_timebase_den;
196     p_mp4->i_time_inc = p_param->i_timebase_num;
197
198     p_mp4->i_init_delay = p_param->i_bframe ? (p_param->i_bframe_pyramid ? 2 : 1) : 0;
199
200     p_mp4->i_track = gf_isom_new_track( p_mp4->p_file, 0, GF_ISOM_MEDIA_VISUAL,
201                                         p_mp4->i_time_res );
202
203     p_mp4->p_config = gf_odf_avc_cfg_new();
204     gf_isom_avc_config_new( p_mp4->p_file, p_mp4->i_track, p_mp4->p_config,
205                             NULL, NULL, &p_mp4->i_descidx );
206
207     gf_isom_set_track_enabled( p_mp4->p_file, p_mp4->i_track, 1 );
208
209     gf_isom_set_visual_info( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx,
210                              p_param->i_width, p_param->i_height );
211
212     if( p_param->vui.i_sar_width && p_param->vui.i_sar_height )
213     {
214         uint64_t dw = p_param->i_width << 16;
215         uint64_t dh = p_param->i_height << 16;
216         double sar = (double)p_param->vui.i_sar_width / p_param->vui.i_sar_height;
217         if( sar > 1.0 )
218             dw *= sar ;
219         else
220             dh /= sar;
221         gf_isom_set_track_layout_info( p_mp4->p_file, p_mp4->i_track, dw, dh, 0, 0, 0 );
222     }
223
224     p_mp4->p_sample->data = malloc( p_param->i_width * p_param->i_height * 3 / 2 );
225     if( !p_mp4->p_sample->data )
226         return -1;
227
228     return 0;
229 }
230
231 static int write_headers( hnd_t handle, x264_nal_t *p_nal )
232 {
233     mp4_hnd_t *p_mp4 = handle;
234     GF_AVCConfigSlot *p_slot;
235
236     int sei_size = p_nal[0].i_payload;
237     int sps_size = p_nal[1].i_payload - 4;
238     int pps_size = p_nal[2].i_payload - 4;
239
240     uint8_t *sei = p_nal[0].p_payload;
241     uint8_t *sps = p_nal[1].p_payload + 4;
242     uint8_t *pps = p_nal[2].p_payload + 4;
243
244     // SPS
245
246     p_mp4->p_config->configurationVersion = 1;
247     p_mp4->p_config->AVCProfileIndication = sps[1];
248     p_mp4->p_config->profile_compatibility = sps[2];
249     p_mp4->p_config->AVCLevelIndication = sps[3];
250     p_slot = malloc( sizeof(GF_AVCConfigSlot) );
251     if( !p_slot )
252         return -1;
253     p_slot->size = sps_size;
254     p_slot->data = malloc( p_slot->size );
255     if( !p_slot->data )
256         return -1;
257     memcpy( p_slot->data, sps, sps_size );
258     gf_list_add( p_mp4->p_config->sequenceParameterSets, p_slot );
259
260     // PPS
261
262     p_slot = malloc( sizeof(GF_AVCConfigSlot) );
263     if( !p_slot )
264         return -1;
265     p_slot->size = pps_size;
266     p_slot->data = malloc( p_slot->size );
267     if( !p_slot->data )
268         return -1;
269     memcpy( p_slot->data, pps, pps_size );
270     gf_list_add( p_mp4->p_config->pictureParameterSets, p_slot );
271     gf_isom_avc_config_update( p_mp4->p_file, p_mp4->i_track, 1, p_mp4->p_config );
272
273     // SEI
274
275     memcpy( p_mp4->p_sample->data + p_mp4->p_sample->dataLength, sei, sei_size );
276     p_mp4->p_sample->dataLength += sei_size;
277
278     return sei_size + sps_size + pps_size;
279 }
280 static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
281 {
282     mp4_hnd_t *p_mp4 = handle;
283     int64_t dts;
284     int64_t cts;
285     int32_t offset = 0;
286
287     memcpy( p_mp4->p_sample->data + p_mp4->p_sample->dataLength, p_nalu, i_size );
288     p_mp4->p_sample->dataLength += i_size;
289
290     if( !p_mp4->i_numframe )
291         p_mp4->i_delay_time = p_picture->i_dts * -1;
292
293     if( !p_mp4->i_init_delay )
294         dts = cts = p_picture->i_pts * p_mp4->i_time_inc;
295     else
296     {
297         if( p_mp4->i_numframe <= p_mp4->i_init_delay )
298             dts = p_picture->i_dts + p_mp4->i_delay_time;
299         else
300             dts = p_mp4->i_prev_timestamps[ (p_mp4->i_numframe - p_mp4->i_init_delay) % p_mp4->i_init_delay ] + p_mp4->i_delay_time;
301
302         // unordered pts
303         p_mp4->i_prev_timestamps[ p_mp4->i_numframe % p_mp4->i_init_delay ] = p_picture->i_dts + p_mp4->i_delay_time;
304
305         dts *= p_mp4->i_time_inc;
306         cts = (p_picture->i_pts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
307
308         offset = cts - dts;
309     }
310
311     p_mp4->p_sample->IsRAP = p_picture->b_keyframe;
312     p_mp4->p_sample->DTS = dts;
313     p_mp4->p_sample->CTS_Offset = offset;
314     gf_isom_add_sample( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, p_mp4->p_sample );
315
316     p_mp4->p_sample->dataLength = 0;
317     p_mp4->i_numframe++;
318
319     return i_size;
320 }
321
322 cli_output_t mp4_output = { open_file, set_param, write_headers, write_frame, close_file };