summaryrefslogtreecommitdiffstats
path: root/core/Mp4Mux.c
blob: c545c96fc6f14cde44c2daf6070411f0a2e86eb2 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* $Id: Mp4Mux.c,v 1.24 2004/03/08 13:42:29 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"

int64_t videoFrames;
int64_t videoBytes;
int64_t audioFrames;
int64_t audioBytes;

/* Local prototypes */
static void Mp4MuxThread( void * );

struct HBMp4Mux
{
    HBHandle      * handle;
    HBTitle       * title;

    volatile int    die;
    HBThread      * thread;
};

HBMp4Mux * HBMp4MuxInit( HBHandle * handle, HBTitle * title )
{
    HBMp4Mux * m;
    if( !( m = malloc( sizeof( HBMp4Mux ) ) ) )
    {
        HBLog( "HBMp4MuxInit: malloc() failed, gonna crash" );
        return NULL;
    }

    videoFrames = 0;
    videoBytes  = 0;
    audioFrames = 0;
    audioBytes  = 0;

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

    m->die    = 0;
    m->thread = HBThreadInit( "mp4 muxer", Mp4MuxThread, m,
                              HB_NORMAL_PRIORITY );
    return m;
}

void HBMp4MuxClose( HBMp4Mux ** _m )
{
    HBMp4Mux * m = *_m;
    FILE * file;
    long   size;

    m->die = 1;
    HBThreadClose( &m->thread );

    file = fopen( m->title->file, "r" );
    if( file )
    {
        fseek( file, 0, SEEK_END );
        size = ftell( file );
        fclose( file );

        HBLog( "HBMp4Mux: videoFrames=%lld, %lld bytes",
               videoFrames, videoBytes );
        HBLog( "HBMp4Mux: audioFrames=%lld, %lld bytes",
               audioFrames, audioBytes );
        HBLog( "HBMp4Mux: overhead=%.2f bytes / frame",
                ( (float) size - videoBytes - audioBytes ) /
                ( videoFrames + audioFrames ) );
    }

    free( m );

    *_m = NULL;
}

static void Mp4MuxThread( void * _m )
{
    HBMp4Mux * m     = (HBMp4Mux*) _m;
    HBTitle  * title = m->title;
    HBAudio  * audio;
    HBBuffer * buffer;
    char       tmpFile[1024];

    int audioCount = HBListCount( m->title->ripAudioList );
    int i;

    MP4FileHandle file;

    /* Wait until we have one encoded frame for each track */
    while( !m->die && !HBFifoSize( title->outFifo ) )
    {
        HBSnooze( 10000 );
    }
    for( i = 0; i < audioCount; i++ )
    {
        audio = HBListItemAt( title->ripAudioList, i );
        while( !m->die && !HBFifoSize( audio->outFifo ) )
        {
            HBSnooze( 10000 );
        }
    }

    if( m->die )
    {
        return;
    }

    /* Write file headers */
    file = MP4Create( title->file, 0, 0 );
    MP4SetTimeScale( file, 90000 );
    title->track = MP4AddVideoTrack( file, 90000,
                                     MP4_INVALID_DURATION,
                                     title->outWidth, title->outHeight,
                                     MP4_MPEG4_VIDEO_TYPE );
    MP4SetVideoProfileLevel( file, 0x03 );
    MP4SetTrackESConfiguration( file, title->track, title->esConfig,
                                title->esConfigLength );

    for( i = 0; i < audioCount; i++ )
    {
        audio = HBListItemAt( title->ripAudioList, i );
        audio->track = MP4AddAudioTrack( file, audio->outSampleRate,
                                         1024, MP4_MPEG4_AUDIO_TYPE );
        MP4SetAudioProfileLevel( file, 0x0F );
        MP4SetTrackESConfiguration( file, audio->track, audio->esConfig,
                                    audio->esConfigLength );
    }

    for( ;; )
    {
        /* Wait until we have one encoded frame for each track */
        if( !HBFifoWait( title->outFifo ) )
        {
            m->die = 1;
            break;
        }
        for( i = 0; i < audioCount; i++ )
        {
            audio = HBListItemAt( title->ripAudioList, i );
            if( !HBFifoWait( audio->outFifo ) )
            {
                m->die = 1;
                break;
            }
        }

        if( m->die )
        {
            break;
        }

        /* Interleave frames in the same order than they were in the
           original MPEG stream */
        audio = NULL;
        for( i = 0; i < audioCount; i++ )
        {
            HBAudio * otherAudio;
            otherAudio = HBListItemAt( title->ripAudioList, i );
            if( !audio || HBFifoPosition( otherAudio->outFifo ) <
                          HBFifoPosition( audio->outFifo ) )
            {
                audio = otherAudio;
            }
        }

        if( !audio || HBFifoPosition( title->outFifo ) <
                HBFifoPosition( audio->outFifo ) )
        {
            buffer = HBFifoPop( title->outFifo );
            MP4WriteSample( file, title->track, buffer->data,
                            buffer->size,
                            (uint64_t) 90000 * title->rateBase / title->rate,
                            0, buffer->keyFrame );
            videoFrames++;
            videoBytes += buffer->size;
            HBBufferClose( &buffer );
        }
        else
        {
            buffer = HBFifoPop( audio->outFifo );
            MP4WriteSample( file, audio->track, buffer->data,
                            buffer->size, MP4_INVALID_DURATION,
                            0, buffer->keyFrame );
            audioFrames++;
            audioBytes += buffer->size;
            HBBufferClose( &buffer );
        }
    }

    MP4Close( 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 );
    }
}