]> git.sesse.net Git - x264/blob - output/mp4.c
Fix issues relating to input/output files being pipes/FIFOs
[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_inc;
35     int i_time_res;
36     int i_numframe;
37     int i_init_delay;
38     uint8_t b_sps;
39     uint8_t b_pps;
40 } mp4_hnd_t;
41
42 static void recompute_bitrate_mp4( GF_ISOFile *p_file, int i_track )
43 {
44     u32 i, count, di, timescale, time_wnd, rate;
45     u64 offset;
46     Double br;
47     GF_ESD *esd;
48
49     esd = gf_isom_get_esd( p_file, i_track, 1 );
50     if( !esd )
51         return;
52
53     esd->decoderConfig->avgBitrate = 0;
54     esd->decoderConfig->maxBitrate = 0;
55     rate = time_wnd = 0;
56
57     timescale = gf_isom_get_media_timescale( p_file, i_track );
58     count = gf_isom_get_sample_count( p_file, i_track );
59     for( i = 0; i < count; i++ )
60     {
61         GF_ISOSample *samp = gf_isom_get_sample_info( p_file, i_track, i+1, &di, &offset );
62
63         if( samp->dataLength>esd->decoderConfig->bufferSizeDB )
64             esd->decoderConfig->bufferSizeDB = samp->dataLength;
65
66         if( esd->decoderConfig->bufferSizeDB < samp->dataLength )
67             esd->decoderConfig->bufferSizeDB = samp->dataLength;
68
69         esd->decoderConfig->avgBitrate += samp->dataLength;
70         rate += samp->dataLength;
71         if( samp->DTS > time_wnd + timescale )
72         {
73             if( rate > esd->decoderConfig->maxBitrate )
74                 esd->decoderConfig->maxBitrate = rate;
75             time_wnd = samp->DTS;
76             rate = 0;
77         }
78
79         gf_isom_sample_del( &samp );
80     }
81
82     br = (Double)(s64)gf_isom_get_media_duration( p_file, i_track );
83     br /= timescale;
84     esd->decoderConfig->avgBitrate = (u32)(esd->decoderConfig->avgBitrate / br);
85     /*move to bps*/
86     esd->decoderConfig->avgBitrate *= 8;
87     esd->decoderConfig->maxBitrate *= 8;
88
89     gf_isom_change_mpeg4_description( p_file, i_track, 1, esd );
90     gf_odf_desc_del( (GF_Descriptor*)esd );
91 }
92
93 static int close_file( hnd_t handle )
94 {
95     mp4_hnd_t *p_mp4 = handle;
96
97     if( !p_mp4 )
98         return 0;
99
100     if( p_mp4->p_config )
101         gf_odf_avc_cfg_del( p_mp4->p_config );
102
103     if( p_mp4->p_sample )
104     {
105         if( p_mp4->p_sample->data )
106             free( p_mp4->p_sample->data );
107
108         gf_isom_sample_del( &p_mp4->p_sample );
109     }
110
111     if( p_mp4->p_file )
112     {
113         recompute_bitrate_mp4( p_mp4->p_file, p_mp4->i_track );
114         gf_isom_set_pl_indication( p_mp4->p_file, GF_ISOM_PL_VISUAL, 0x15 );
115         gf_isom_set_storage_mode( p_mp4->p_file, GF_ISOM_STORE_FLAT );
116         gf_isom_close( p_mp4->p_file );
117     }
118
119     free( p_mp4 );
120
121     return 0;
122 }
123
124 static int open_file( char *psz_filename, hnd_t *p_handle )
125 {
126     mp4_hnd_t *p_mp4;
127
128     *p_handle = NULL;
129     FILE *fh = fopen( psz_filename, "w" );
130     if( !fh )
131         return -1;
132     else if( !x264_is_regular_file( fh ) )
133     {
134         fprintf( stderr, "mp4 [error]: MP4 output is incompatible with non-regular file `%s'\n", psz_filename );
135         return -1;
136     }
137     fclose( fh );
138
139     if( !(p_mp4 = malloc( sizeof(mp4_hnd_t) )) )
140         return -1;
141
142     memset( p_mp4, 0, sizeof(mp4_hnd_t) );
143     p_mp4->p_file = gf_isom_open( psz_filename, GF_ISOM_OPEN_WRITE, NULL );
144
145     if( !(p_mp4->p_sample = gf_isom_sample_new()) )
146     {
147         close_file( p_mp4 );
148         return -1;
149     }
150
151     gf_isom_set_brand_info( p_mp4->p_file, GF_ISOM_BRAND_AVC1, 0 );
152
153     *p_handle = p_mp4;
154
155     return 0;
156 }
157
158 static int set_param( hnd_t handle, x264_param_t *p_param )
159 {
160     mp4_hnd_t *p_mp4 = handle;
161
162     p_mp4->i_track = gf_isom_new_track( p_mp4->p_file, 0, GF_ISOM_MEDIA_VISUAL,
163                                         p_param->i_fps_num );
164
165     p_mp4->p_config = gf_odf_avc_cfg_new();
166     gf_isom_avc_config_new( p_mp4->p_file, p_mp4->i_track, p_mp4->p_config,
167                             NULL, NULL, &p_mp4->i_descidx );
168
169     gf_isom_set_track_enabled( p_mp4->p_file, p_mp4->i_track, 1 );
170
171     gf_isom_set_visual_info( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx,
172                              p_param->i_width, p_param->i_height );
173
174     if( p_param->vui.i_sar_width && p_param->vui.i_sar_height )
175     {
176         uint64_t dw = p_param->i_width << 16;
177         uint64_t dh = p_param->i_height << 16;
178         double sar = (double)p_param->vui.i_sar_width / p_param->vui.i_sar_height;
179         if( sar > 1.0 )
180             dw *= sar ;
181         else
182             dh /= sar;
183         gf_isom_set_track_layout_info( p_mp4->p_file, p_mp4->i_track, dw, dh, 0, 0, 0 );
184     }
185
186     p_mp4->p_sample->data = malloc( p_param->i_width * p_param->i_height * 3 / 2 );
187     if( !p_mp4->p_sample->data )
188         return -1;
189
190     p_mp4->i_time_res = p_param->i_fps_num;
191     p_mp4->i_time_inc = p_param->i_fps_den;
192     p_mp4->i_init_delay = p_param->i_bframe ? (p_param->i_bframe_pyramid ? 2 : 1) : 0;
193     p_mp4->i_init_delay *= p_mp4->i_time_inc;
194     fprintf( stderr, "mp4 [info]: initial delay %d (scale %d)\n",
195              p_mp4->i_init_delay, p_mp4->i_time_res );
196
197     return 0;
198 }
199
200 static int write_nalu( hnd_t handle, uint8_t *p_nalu, int i_size )
201 {
202     mp4_hnd_t *p_mp4 = handle;
203     GF_AVCConfigSlot *p_slot;
204     uint8_t type = p_nalu[4] & 0x1f;
205     int psize;
206
207     switch( type )
208     {
209         // sps
210         case 0x07:
211             if( !p_mp4->b_sps )
212             {
213                 p_mp4->p_config->configurationVersion = 1;
214                 p_mp4->p_config->AVCProfileIndication = p_nalu[5];
215                 p_mp4->p_config->profile_compatibility = p_nalu[6];
216                 p_mp4->p_config->AVCLevelIndication = p_nalu[7];
217                 p_slot = malloc( sizeof(GF_AVCConfigSlot) );
218                 if( !p_slot )
219                     return -1;
220                 p_slot->size = i_size - 4;
221                 p_slot->data = malloc( p_slot->size );
222                 if( !p_slot->data )
223                     return -1;
224                 memcpy( p_slot->data, p_nalu + 4, i_size - 4 );
225                 gf_list_add( p_mp4->p_config->sequenceParameterSets, p_slot );
226                 p_slot = NULL;
227                 p_mp4->b_sps = 1;
228             }
229             break;
230
231         // pps
232         case 0x08:
233             if( !p_mp4->b_pps )
234             {
235                 p_slot = malloc( sizeof(GF_AVCConfigSlot) );
236                 if( !p_slot )
237                     return -1;
238                 p_slot->size = i_size - 4;
239                 p_slot->data = malloc( p_slot->size );
240                 if( !p_slot->data )
241                     return -1;
242                 memcpy( p_slot->data, p_nalu + 4, i_size - 4 );
243                 gf_list_add( p_mp4->p_config->pictureParameterSets, p_slot );
244                 p_slot = NULL;
245                 p_mp4->b_pps = 1;
246                 if( p_mp4->b_sps )
247                     gf_isom_avc_config_update( p_mp4->p_file, p_mp4->i_track, 1, p_mp4->p_config );
248             }
249             break;
250
251         // slice, sei
252         case 0x1:
253         case 0x5:
254         case 0x6:
255             psize = i_size - 4;
256             memcpy( p_mp4->p_sample->data + p_mp4->p_sample->dataLength, p_nalu, i_size );
257             p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 0] = psize >> 24;
258             p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 1] = psize >> 16;
259             p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 2] = psize >>  8;
260             p_mp4->p_sample->data[p_mp4->p_sample->dataLength + 3] = psize >>  0;
261             p_mp4->p_sample->dataLength += i_size;
262             break;
263     }
264
265     return i_size;
266 }
267
268 static int set_eop( hnd_t handle, x264_picture_t *p_picture )
269 {
270     mp4_hnd_t *p_mp4 = handle;
271     uint64_t dts = (uint64_t)p_mp4->i_numframe * p_mp4->i_time_inc;
272     uint64_t pts = (uint64_t)p_picture->i_pts;
273     int32_t offset = p_mp4->i_init_delay + pts - dts;
274
275     p_mp4->p_sample->IsRAP = p_picture->i_type == X264_TYPE_IDR ? 1 : 0;
276     p_mp4->p_sample->DTS = dts;
277     p_mp4->p_sample->CTS_Offset = offset;
278     gf_isom_add_sample( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, p_mp4->p_sample );
279
280     p_mp4->p_sample->dataLength = 0;
281     p_mp4->i_numframe++;
282
283     return 0;
284 }
285
286 cli_output_t mp4_output = { open_file, set_param, write_nalu, set_eop, close_file };