summaryrefslogtreecommitdiffstats
path: root/core/XvidEnc.c
blob: 2ca6bca6f5c36f09b88731b0ce34d43d9f0c73b0 (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
/* $Id: XvidEnc.c,v 1.7 2003/11/09 21:26:52 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 "Fifo.h"
#include "Work.h"
#include "XvidEnc.h"
#include "XvidVbr.h"

#include <xvid.h>

/* Local prototypes */
static int XvidEncWork( HBWork * );

struct HBXvidEnc
{
    HB_WORK_COMMON_MEMBERS

    HBHandle * handle;
    HBTitle  * title;

    void           * xvid;
    vbr_control_t    xvidVbr;
    XVID_ENC_FRAME   frame;
    HBBuffer       * mpeg4Buffer;
    int              pass;
};

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

    x->name = strdup( "XvidEnc" );
    x->work = XvidEncWork;

    x->handle = handle;
    x->title = title;

    x->xvid        = NULL;

    x->frame.general   = XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V;
    x->frame.motion    = PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |
                         PMV_EXTSEARCH16 | PMV_EARLYSTOP8 |
                         PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8 |
                         PMV_USESQUARES16;

    x->frame.colorspace = XVID_CSP_I420;

    x->frame.quant_intra_matrix = NULL;
    x->frame.quant_inter_matrix = NULL;

    x->mpeg4Buffer = NULL;
    x->pass        = 42;

    return x;
}

void HBXvidEncClose( HBXvidEnc ** _x )
{
    HBXvidEnc * x = *_x;

    if( x->xvid )
    {
        HBLog( "HBXvidEnc: closing libxvidcore (pass %d)",
                x->pass );

        xvid_encore( x->xvid, XVID_ENC_DESTROY, NULL, NULL);
        vbrFinish( &x->xvidVbr );
    }
    
    free( x );

    *_x = NULL;
}

static int XvidEncWork( HBWork * w )
{
    HBXvidEnc      * x = (HBXvidEnc*) w;
    HBTitle        * title = x->title;
    HBBuffer       * scaledBuffer;
    HBBuffer       * mpeg4Buffer;
    XVID_ENC_STATS   stats;

    int didSomething = 0;

    if( x->mpeg4Buffer )
    {
        if( HBFifoPush( title->mpeg4Fifo, &x->mpeg4Buffer ) )
        {
            didSomething = 1;
        }
        else
        {
            return didSomething;
        }
    }

    if( ( scaledBuffer = HBFifoPop( title->scaledFifo ) ) )
    {
        didSomething = 1;
    }
    else
    {
        return didSomething;
    }

    /* Init or re-init if needed */
    if( scaledBuffer->pass != x->pass )
    {
        XVID_INIT_PARAM xinit;
        XVID_ENC_PARAM xparam;

        if( x->xvid )
        {
            HBLog( "HBXvidEnc: closing libxvidcore (pass %d)",
                    x->pass );

            xvid_encore( x->xvid, XVID_ENC_DESTROY, NULL, NULL);
            vbrFinish( &x->xvidVbr );
        }

        x->pass = scaledBuffer->pass;;

        HBLog( "HBXvidEnc: opening libxvidcore (pass %d)", x->pass );

        xinit.cpu_flags = 0;
        xvid_init( NULL, 0, &xinit, NULL );

        xparam.width  = title->outWidth;
        xparam.height = title->outHeight;

        xparam.fincr  = title->rateBase;
        xparam.fbase  = title->rate;

        xparam.rc_bitrate = title->bitrate * 1024;

        /* Default values should be ok */
        xparam.rc_reaction_delay_factor = -1;
        xparam.rc_averaging_period      = -1;
        xparam.rc_buffer                = -1;
        xparam.max_quantizer            = -1;
        xparam.min_quantizer            = -1;
        xparam.max_key_interval         = -1;

        if( xvid_encore( NULL, XVID_ENC_CREATE, &xparam, NULL ) )
        {
            HBLog( "HBXvidEnc: xvid_encore() failed" );
        }

        x->xvid = xparam.handle;

        /* Init VBR engine */
        vbrSetDefaults( &x->xvidVbr );
        if( !x->pass )
        {
            x->xvidVbr.mode = VBR_MODE_1PASS;
        }
        else if( x->pass == 1 )
        {
            x->xvidVbr.mode = VBR_MODE_2PASS_1;
        }
        else
        {
            x->xvidVbr.mode = VBR_MODE_2PASS_2;
        }
        x->xvidVbr.fps = (double) title->rate / title->rateBase;
        x->xvidVbr.debug = 0;
        x->xvidVbr.filename = malloc( 1024 );
        memset( x->xvidVbr.filename, 0, 1024 );
        snprintf( x->xvidVbr.filename, 1023, "/tmp/HB.%d.xvid.log",
                  HBGetPid( x->handle ) );
        x->xvidVbr.desired_bitrate = title->bitrate * 1024;
        x->xvidVbr.max_key_interval = 10 * title->rate / title->rateBase;

        vbrInit( &x->xvidVbr );
    }

    mpeg4Buffer = HBBufferInit( title->outWidth *
                                title->outHeight * 3 / 2 );
    mpeg4Buffer->position = scaledBuffer->position;

    x->frame.bitstream = mpeg4Buffer->data;
    x->frame.length    = -1;

    x->frame.image = scaledBuffer->data;

    x->frame.quant = vbrGetQuant( &x->xvidVbr );
    x->frame.intra = vbrGetIntra( &x->xvidVbr );

    x->frame.hint.hintstream = NULL;

    if( xvid_encore( x->xvid, XVID_ENC_ENCODE, &x->frame, &stats ) )
    {
        HBLog( "HBXvidEnc: xvid_encore() failed" );
    }

    vbrUpdate( &x->xvidVbr, stats.quant, x->frame.intra, stats.hlength,
               x->frame.length, stats.kblks, stats.mblks, stats.ublks );

    mpeg4Buffer->size     = x->frame.length;
    mpeg4Buffer->keyFrame = x->frame.intra;

    /* Inform the GUI about the current position */
    HBPosition( x->handle, scaledBuffer->position );

    HBBufferClose( &scaledBuffer );

    if( x->pass == 1 )
    {
        HBBufferClose( &mpeg4Buffer );
        return didSomething;
    }

    x->mpeg4Buffer = mpeg4Buffer;
    return didSomething;
}