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
|
/* $Id: XvidEnc.c,v 1.5 2003/11/05 19:14:37 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 "XvidEnc.h"
#include "Fifo.h"
#include "Work.h"
#include <xvid.h>
/* Local prototypes */
static int XvidEncWork( HBWork * );
struct HBXvidEnc
{
HB_WORK_COMMON_MEMBERS
HBHandle * handle;
HBTitle * title;
void * xvid;
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->mpeg4Buffer = NULL;
x->pass = 42;
return x;
}
void HBXvidEncClose( HBXvidEnc ** _x )
{
HBXvidEnc * x = *_x;
free( x );
*_x = NULL;
}
static int XvidEncWork( HBWork * w )
{
HBXvidEnc * x = (HBXvidEnc*) w;
HBTitle * title = x->title;
HBBuffer * scaledBuffer;
HBBuffer * mpeg4Buffer;
XVID_ENC_FRAME xframe;
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;
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 * 1000;
/* 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;
}
/* TODO implement 2-pass encoding */
if( x->pass == 1 )
{
HBPosition( x->handle, scaledBuffer->position );
HBBufferClose( &scaledBuffer );
return didSomething;
}
mpeg4Buffer = HBBufferInit( title->outWidth *
title->outHeight * 3 / 2 );
mpeg4Buffer->position = scaledBuffer->position;
xframe.general = XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V;
xframe.motion = PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |
PMV_EXTSEARCH16 | PMV_EARLYSTOP8 |
PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8 |
PMV_USESQUARES16;
xframe.bitstream = mpeg4Buffer->data;
xframe.image = scaledBuffer->data;
xframe.colorspace = XVID_CSP_I420;
xframe.quant_intra_matrix = NULL;
xframe.quant_inter_matrix = NULL;
xframe.quant = 0;
xframe.intra = -1;
xframe.hint.hintstream = NULL;
if( xvid_encore( x->xvid, XVID_ENC_ENCODE, &xframe, NULL ) )
{
HBLog( "HBXvidEnc: xvid_encore() failed" );
}
mpeg4Buffer->size = xframe.length;
mpeg4Buffer->keyFrame = xframe.intra;
/* Inform the GUI about the current position */
HBPosition( x->handle, scaledBuffer->position );
HBBufferClose( &scaledBuffer );
x->mpeg4Buffer = mpeg4Buffer;
return didSomething;
}
|