~ubuntu-branches/ubuntu/precise/x264/precise

« back to all changes in this revision

Viewing changes to output/mp4.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Andres Mejia, Reinhard Tartler
  • Date: 2011-05-22 20:11:48 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20110522201148-68oyygamd1pvfvmd
Tags: 2:0.115.1943+git6e33b51-1
[ Andres Mejia ]
* New upstream release.
* version.sh modifications no longer need to be made, an updated version.sh
  is installed in upstream tarball.
* Shared library package is renamed to lib264-115.
* fix-8x8dct+slices+no-sliced-threads+cavlc+deblock.patch is removed, has
  been applied upstream.
* Add DM-Upload-Allowed: yes.
* Convert package to 3.0 (quilt).
* Convert packaging to dh 8.
* Change Priority to optional.

[ Reinhard Tartler ]
* lower debhelper compat level to 7
* don't set --disable-debug
* bump requirements on libav, also depend on libswscale
* fix typo in dependency declaration
* drop patch link-x264cli-explicitly-against-lavf
* even more strict dependencies on libav libraries

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
2
 * mp4.c: mp4 muxer
3
3
 *****************************************************************************
4
 
 * Copyright (C) 2003-2010 x264 project
 
4
 * Copyright (C) 2003-2011 x264 project
5
5
 *
6
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
7
 *          Loren Merritt <lorenm@u.washington.edu>
30
30
#if HAVE_GF_MALLOC
31
31
#undef malloc
32
32
#undef free
 
33
#undef realloc
33
34
#define malloc gf_malloc
34
35
#define free gf_free
 
36
#define realloc gf_realloc
35
37
#endif
36
38
 
37
39
typedef struct
41
43
    GF_ISOSample *p_sample;
42
44
    int i_track;
43
45
    uint32_t i_descidx;
44
 
    uint32_t i_time_res;
 
46
    uint64_t i_time_res;
45
47
    int64_t i_time_inc;
 
48
    int64_t i_delay_time;
 
49
    int64_t i_init_delta;
46
50
    int i_numframe;
47
 
    int i_delay_time;
 
51
    int i_delay_frames;
 
52
    int b_dts_compress;
 
53
    int i_dts_compress_multiplier;
 
54
    int i_data_size;
48
55
} mp4_hnd_t;
49
56
 
50
57
static void recompute_bitrate_mp4( GF_ISOFile *p_file, int i_track )
121
128
 
122
129
    if( p_mp4->p_file )
123
130
    {
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
 
            gf_isom_append_edit_segment( p_mp4->p_file, p_mp4->i_track, tkhd_duration, sample->CTS_Offset, GF_ISOM_EDIT_NORMAL );
147
 
        }
148
 
        gf_isom_sample_del( &sample );
149
 
 
150
 
        recompute_bitrate_mp4( p_mp4->p_file, p_mp4->i_track );
 
131
        if( p_mp4->i_track )
 
132
        {
 
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 ) )
 
140
            {
 
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 );
 
144
            }
 
145
 
 
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 )
 
152
            {
 
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 );
 
156
            }
 
157
            gf_isom_sample_del( &sample );
 
158
 
 
159
            recompute_bitrate_mp4( p_mp4->p_file, p_mp4->i_track );
 
160
        }
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 );
158
168
    return 0;
159
169
}
160
170
 
161
 
static int open_file( char *psz_filename, hnd_t *p_handle )
 
171
static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
162
172
{
163
173
    mp4_hnd_t *p_mp4;
164
174
 
175
185
    memset( p_mp4, 0, sizeof(mp4_hnd_t) );
176
186
    p_mp4->p_file = gf_isom_open( psz_filename, GF_ISOM_OPEN_WRITE, NULL );
177
187
 
 
188
    p_mp4->b_dts_compress = opt->use_dts_compress;
 
189
 
178
190
    if( !(p_mp4->p_sample = gf_isom_sample_new()) )
179
191
    {
180
192
        close_file( p_mp4, 0, 0 );
192
204
{
193
205
    mp4_hnd_t *p_mp4 = handle;
194
206
 
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;
 
209
 
 
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 )
197
213
 
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 );
221
237
    }
222
238
 
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 )
 
242
    {
 
243
        p_mp4->i_data_size = 0;
225
244
        return -1;
226
 
 
 
245
    }
 
246
 
 
247
    return 0;
 
248
}
 
249
 
 
250
static int check_buffer( mp4_hnd_t *p_mp4, int needed_size )
 
251
{
 
252
    if( needed_size > p_mp4->i_data_size )
 
253
    {
 
254
        void *ptr = realloc( p_mp4->p_sample->data, needed_size );
 
255
        if( !ptr )
 
256
            return -1;
 
257
        p_mp4->p_sample->data = ptr;
 
258
        p_mp4->i_data_size = needed_size;
 
259
    }
227
260
    return 0;
228
261
}
229
262
 
271
304
 
272
305
    // SEI
273
306
 
 
307
    if( check_buffer( p_mp4, p_mp4->p_sample->dataLength + sei_size ) )
 
308
        return -1;
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;
276
311
 
277
312
    return sei_size + sps_size + pps_size;
278
313
}
 
314
 
279
315
static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
280
316
{
281
317
    mp4_hnd_t *p_mp4 = handle;
282
318
    int64_t dts;
283
319
    int64_t cts;
284
320
 
 
321
    if( check_buffer( p_mp4, p_mp4->p_sample->dataLength + i_size ) )
 
322
        return -1;
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;
287
325
 
288
326
    if( !p_mp4->i_numframe )
289
327
        p_mp4->i_delay_time = p_picture->i_dts * -1;
290
328
 
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 )
 
330
    {
 
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;
 
337
    }
 
338
    else
 
339
    {
 
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;
 
342
    }
293
343
 
294
344
    p_mp4->p_sample->IsRAP = p_picture->b_keyframe;
295
345
    p_mp4->p_sample->DTS = dts;