summaryrefslogtreecommitdiffstats
path: root/libhb/muxcommon.c
blob: b09c5c58fc5952bb8c82ecddb0f0e3f06cc9d2d9 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* $Id: muxcommon.c,v 1.23 2005/03/30 17:27:19 titer Exp $

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

#include "hb.h"

struct hb_mux_object_s
{
    HB_MUX_COMMON;
};

typedef struct
{
    hb_job_t * job;
    uint64_t   pts;

} hb_mux_t;

typedef struct
{
    hb_fifo_t     * fifo;
    hb_mux_data_t * mux_data;
    uint64_t        frames;
    uint64_t        bytes;
    int             eof;
} hb_track_t;

static hb_track_t * GetTrack( hb_list_t * list, hb_job_t *job )
{
    hb_buffer_t * buf;
    hb_track_t  * track = NULL, * track2;
    int64_t       pts = 0;
    int           i;

    for( i = 0; i < hb_list_count( list ); i++ )
    {
        track2 = hb_list_item( list, i );
        if ( ! track2->eof )
        {
            buf    = hb_fifo_see( track2->fifo );
            if( !buf )
            {
                // XXX the libmkv muxer will produce unplayable files if the
                // audio & video are far out of sync.  To keep them in sync we require
                // that *all* fifos have a buffer then we take the oldest.
                // Unfortunately this means we can hang in a deadlock with the
                // reader process filling the fifos.
                if ( job->mux == HB_MUX_MKV )
                {
                    return NULL;
                }

                // To make sure we don't camp on one fifo & prevent the others
                // from making progress we take the earliest data of all the
                // data that's currently available but we don't care if some
                // fifos don't have data.
                continue;
            }
            if ( buf->size <= 0 )
            {
                // EOF - mark this track as done
                buf = hb_fifo_get( track2->fifo );
                hb_buffer_close( &buf );
                track2->eof = 1;
                continue;
            }
            if( !track || buf->start < pts )
            {
                track = track2;
                pts   = buf->start;
            }
        }
    }
    return track;
}

static int AllTracksDone( hb_list_t * list )
{
    hb_track_t  * track;
    int           i;

    for( i = 0; i < hb_list_count( list ); i++ )
    {
        track = hb_list_item( list, i );
        if ( track->eof == 0 )
        {
            return 0;
        }
    }
    return 1;
}

static void MuxerFunc( void * _mux )
{
    hb_mux_t    * mux = _mux;
    hb_job_t    * job = mux->job;
    hb_title_t  * title = job->title;
    hb_audio_t  * audio;
    hb_list_t   * list;
    hb_buffer_t * buf;
    hb_track_t  * track;
    int           i;

    hb_mux_object_t * m = NULL;

    /* Get a real muxer */
    if( job->pass == 0 || job->pass == 2)
    {
        switch( job->mux )
        {
            case HB_MUX_MP4:
            case HB_MUX_PSP:
			case HB_MUX_IPOD:
                m = hb_mux_mp4_init( job );
                break;
            case HB_MUX_AVI:
                m = hb_mux_avi_init( job );
                break;
            case HB_MUX_OGM:
                m = hb_mux_ogm_init( job );
                break;
            case HB_MUX_MKV:
                m = hb_mux_mkv_init( job );
        }
    }

    /* Create file, write headers */
    if( job->pass == 0 || job->pass == 2 )
    {
        m->init( m );
    }

    /* Build list of fifos we're interested in */
    list = hb_list_init();

    track           = calloc( sizeof( hb_track_t ), 1 );
    track->fifo     = job->fifo_mpeg4;
    track->mux_data = job->mux_data;
    hb_list_add( list, track );

    for( i = 0; i < hb_list_count( title->list_audio ); i++ )
    {
        audio           = hb_list_item( title->list_audio, i );
        track           = calloc( sizeof( hb_track_t ), 1 );
        track->fifo     = audio->priv.fifo_out;
        track->mux_data = audio->priv.mux_data;
        hb_list_add( list, track );
    }

	int thread_sleep_interval = 50;
	while( !*job->die )
    {
        if( !( track = GetTrack( list, job ) ) )
        {
            if ( AllTracksDone( list )  )
            {
                // all our input fifos have signaled EOF
                break;
            }
            hb_snooze( thread_sleep_interval );
            continue;
        }

        buf = hb_fifo_get( track->fifo );
        if( job->pass == 0 || job->pass == 2 )
        {
            m->mux( m, track->mux_data, buf );
            track->frames += 1;
            track->bytes  += buf->size;
            mux->pts = buf->stop;
        }
        hb_buffer_close( &buf );
    }

    if( job->pass == 0 || job->pass == 2 )
    {
        struct stat sb;
        uint64_t bytes_total, frames_total;

#define p state.param.muxing
        /* Update the UI */
        hb_state_t state;
        state.state   = HB_STATE_MUXING;
		p.progress = 0;
        hb_set_state( job->h, &state );
#undef p
        m->end( m );

        if( !stat( job->file, &sb ) )
        {
            hb_deep_log( 2, "mux: file size, %lld bytes", (uint64_t) sb.st_size );

            bytes_total  = 0;
            frames_total = 0;
            for( i = 0; i < hb_list_count( list ); i++ )
            {
                track = hb_list_item( list, i );
                hb_deep_log( 2, "mux: track %d, %lld bytes, %.2f kbps",
                        i, track->bytes,
                        90000.0 * track->bytes / mux->pts / 125 );
                if( !i && ( job->vquality < 0.0 || job->vquality > 1.0 ) )
                {
                    /* Video */
                    hb_deep_log( 2, "mux: video bitrate error, %+lld bytes",
                            track->bytes - mux->pts * job->vbitrate *
                            125 / 90000 );
                }
                bytes_total  += track->bytes;
                frames_total += track->frames;
            }

            if( bytes_total && frames_total )
            {
                hb_deep_log( 2, "mux: overhead, %.2f bytes per frame",
                        (float) ( sb.st_size - bytes_total ) /
                        frames_total );
            }
        }
    }

    free( m );

    for( i = 0; i < hb_list_count( list ); i++ )
    {
        track = hb_list_item( list, i );
        if( track->mux_data )
        {
            free( track->mux_data );
        }
        free( track );
    }
    hb_list_close( &list );

    free( mux );
}

hb_thread_t * hb_muxer_init( hb_job_t * job )
{
    hb_mux_t * mux = calloc( sizeof( hb_mux_t ), 1 );
    mux->job = job;
    return hb_thread_init( "muxer", MuxerFunc, mux,
                           HB_NORMAL_PRIORITY );
}