summaryrefslogtreecommitdiffstats
path: root/core/Mp4Mux.c
blob: d48c1adc64d694921e80bff83c16ede93dc5f430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* $Id: Mp4Mux.c,v 1.31 2004/05/13 21:10:56 titer Exp $

   This file is part of the HandBrake source code.
   Homepage: <http://handbrake.m0k.org/>.
   It may be used under the terms of the GNU General Public License. */

#include "HBInternal.h"

/* libmp4v2 */
#include "mp4.h"

struct HBMux
{
    HB_MUX_COMMON_MEMBERS

    HBHandle      * handle;
    HBTitle       * title;

    MP4FileHandle   file;

    /* QuickTime sync workaround */
    int             sampleRate;
    uint64_t        frames;
    uint64_t        date;

};

typedef struct
{
    int      track;

} Mp4MuxData;

/* Local prototypes */
static int Mp4Start( HBMux * );
static int Mp4MuxVideo( HBMux *, void *, HBBuffer *);
static int Mp4MuxAudio( HBMux *, void *, HBBuffer *);
static int Mp4End( HBMux * );

HBMux * HBMp4MuxInit( HBHandle * handle, HBTitle * title )
{
    HBMux   * m;
    HBAudio * audio;
    int       i;

    if( !( m = calloc( sizeof( HBMux ), 1 ) ) )
    {
        HBLog( "HBMp4Mux: malloc() failed, gonna crash" );
        return NULL;
    }
    m->start    = Mp4Start;
    m->muxVideo = Mp4MuxVideo;
    m->muxAudio = Mp4MuxAudio;
    m->end      = Mp4End;

    m->handle   = handle;
    m->title    = title;

    /* Alloc muxer data */
    title->muxData = calloc( sizeof( Mp4MuxData ), 1 );
    for( i = 0; i < HBListCount( title->ripAudioList ); i++ )
    {
        audio = (HBAudio *) HBListItemAt( title->ripAudioList, i );
        audio->muxData = calloc( sizeof( Mp4MuxData ), 1 );
    }

    return m;
}

void HBMp4MuxClose( HBMux ** _m )
{
    HBMux   * m     = *_m;
    HBTitle * title = m->title;
    HBAudio * audio;
    int       i;

    /* Free muxer data */
    free( title->muxData );
    for( i = 0; i < HBListCount( title->ripAudioList ); i++ )
    {
        audio = (HBAudio *) HBListItemAt( title->ripAudioList, i );
        free( audio->muxData );
    }

    free( m );
    *_m = NULL;
}

static int Mp4Start( HBMux * m )
{
    HBTitle    * title = m->title;
    HBAudio    * audio;
    Mp4MuxData * muxData;
    int          i;

    /* Create file */
    m->file = MP4Create( title->file, 0, 0 );

    /* Add video track */
    muxData = (Mp4MuxData *) title->muxData;
    if( HBListCount( title->ripAudioList ) )
    {
        /* QuickTime sync workaround */
        audio = (HBAudio *) HBListItemAt( title->ripAudioList, 0 );
        m->sampleRate = audio->outSampleRate;
        MP4SetTimeScale( m->file, m->sampleRate );
        muxData->track = MP4AddVideoTrack( m->file, m->sampleRate,
                MP4_INVALID_DURATION, title->outWidth, title->outHeight,
                MP4_MPEG4_VIDEO_TYPE );
    }
    else
    {
        MP4SetTimeScale( m->file, 90000 );
        muxData->track = MP4AddVideoTrack( m->file, 90000,
                (uint64_t) 90000 * title->rateBase / title->rate,
                title->outWidth, title->outHeight,
                MP4_MPEG4_VIDEO_TYPE );
    }
    MP4SetVideoProfileLevel( m->file, 0x03 );
    MP4SetTrackESConfiguration( m->file, muxData->track,
            title->esConfig, title->esConfigLength );

    /* Add audio tracks */
    for( i = 0; i < HBListCount( title->ripAudioList ); i++ )
    {
        audio = HBListItemAt( title->ripAudioList, i );
        muxData = (Mp4MuxData *) audio->muxData;
        muxData->track = MP4AddAudioTrack( m->file,
                audio->outSampleRate, 1024, MP4_MPEG4_AUDIO_TYPE );
        MP4SetAudioProfileLevel( m->file, 0x0F );
        MP4SetTrackESConfiguration( m->file, muxData->track,
                audio->esConfig, audio->esConfigLength );
    }

    return 0;
}

static int Mp4MuxVideo( HBMux * m, void * _muxData, HBBuffer * buffer )
{
    Mp4MuxData * muxData = (Mp4MuxData *) _muxData;
    HBTitle    * title   = m->title;

    if( HBListCount( title->ripAudioList ) )
    {
        /* QuickTime sync workaround */
        int dur = (uint64_t) m->sampleRate * ( ++m->frames ) *
            title->rateBase / title->rate - m->date;
        MP4WriteSample( m->file, muxData->track, buffer->data, buffer->size,
                        dur, 0, buffer->keyFrame );
        m->date += dur;
    }
    else
    {
        MP4WriteSample( m->file, muxData->track, buffer->data,
                buffer->size, MP4_INVALID_DURATION, 0,
                buffer->keyFrame );
    }
    return 0;
}

static int Mp4MuxAudio( HBMux * m, void * _muxData, HBBuffer * buffer )
{
    Mp4MuxData * muxData = (Mp4MuxData *) _muxData;

    MP4WriteSample( m->file, muxData->track, buffer->data, buffer->size,
                    MP4_INVALID_DURATION, 0, buffer->keyFrame );
    return 0;
}

static int Mp4End( HBMux * m )
{
    HBTitle  * title = m->title;
    char       tmpFile[1024];

    MP4Close( m->file );

    HBLog( "HBMp4Mux: making the file ISMA compliant" );
    if( !MP4MakeIsmaCompliant( title->file, 0 /*MP4_DETAILS_ALL*/, 1 ) )
    {
        HBLog( "HBMp4Mux: MP4MakeIsmaCompliant() failed" );
    }

    HBLog( "HBMp4Mux: optimizing" );
    sprintf( tmpFile, "%s.tmp", title->file );
    tmpFile[strlen( title->file ) + 4] = '\0';
    if( !MP4Optimize( title->file, tmpFile, 0 /*MP4_DETAILS_ALL*/ ) )
    {
        HBLog( "HBMp4Mux: MP4Optimize() failed" );
        unlink( tmpFile );
    }
    else
    {
        rename( tmpFile, title->file );
    }
    return 0;
}