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
246
247
248
249
250
251
252
|
/* $Id: Scale.c,v 1.10 2004/03/08 11:32:48 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"
#define USE_FFMPEG
#ifdef USE_FFMPEG
#include "ffmpeg/avcodec.h"
#endif
typedef struct HBScale
{
HB_WORK_COMMON_MEMBERS
HBHandle * handle;
HBTitle * title;
HBBuffer * deintBuffer;
HBList * scaledBufferList;
#ifdef USE_FFMPEG
ImgReSampleContext * context;
AVPicture rawPicture;
AVPicture deintPicture;
AVPicture scaledPicture;
#endif
} HBScale;
/* Local prototypes */
static int ScaleWork( HBWork * );
#ifndef USE_FFMPEG
static void Deinterlace( uint8_t * in, uint8_t * out, int w, int h,
int tcrop, int bcrop, int lcrop, int rcrop );
static void Resample( uint8_t * in, uint8_t * out, int oldw, int oldh,
int neww, int newh, int tcrop, int bcrop,
int lcrop, int rcrop );
#endif
HBWork * HBScaleInit( HBHandle * handle, HBTitle * title )
{
HBScale * s;
if( !( s = malloc( sizeof( HBScale ) ) ) )
{
HBLog( "HBScaleInit: malloc() failed, gonna crash" );
return NULL;
}
s->name = strdup( "Scale" );
s->work = ScaleWork;
s->handle = handle;
s->title = title;
/* Allocate a constant buffer used for deinterlacing */
s->deintBuffer = HBBufferInit( 3 * title->inWidth *
title->inHeight / 2 );
#ifdef USE_FFMPEG
avpicture_fill( &s->deintPicture, s->deintBuffer->data,
PIX_FMT_YUV420P, title->inWidth, title->inHeight );
/* Init libavcodec */
s->context =
img_resample_full_init( title->outWidth, title->outHeight,
title->inWidth, title->inHeight,
title->topCrop, title->bottomCrop,
title->leftCrop, title->rightCrop );
#endif
s->scaledBufferList = HBListInit();
return (HBWork*) s;
}
void HBScaleClose( HBWork ** _s )
{
HBScale * s = (HBScale*) *_s;
#ifdef USE_FFMPEG
img_resample_close( s->context );
#endif
HBListClose( &s->scaledBufferList );
HBBufferClose( &s->deintBuffer );
free( s->name );
free( s );
*_s = NULL;
}
static int ScaleWork( HBWork * w )
{
HBScale * s = (HBScale*) w;
HBTitle * title = s->title;
HBBuffer * rawBuffer;
HBBuffer * scaledBuffer;
HBBuffer * tmpBuffer;
#ifndef USE_FFMPEG
uint8_t * in, * out;
int plane, shift;
#endif
int didSomething = 0;
/* Push scaled buffer(s) */
while( ( scaledBuffer = (HBBuffer*)
HBListItemAt( s->scaledBufferList, 0 ) ) )
{
tmpBuffer = scaledBuffer;
if( HBFifoPush( title->scaledFifo, &scaledBuffer ) )
{
didSomething = 1;
HBListRemove( s->scaledBufferList, tmpBuffer );
}
else
{
return didSomething;
}
}
/* Get a new raw picture */
if( ( rawBuffer = HBFifoPop( title->rawFifo ) ) )
{
didSomething = 1;
}
else
{
return didSomething;
}
/* Allocate new buffer for the scaled picture */
scaledBuffer = HBBufferInit( 3 * title->outWidth *
title->outHeight / 2 );
scaledBuffer->position = rawBuffer->position;
scaledBuffer->pass = rawBuffer->pass;
#ifdef USE_FFMPEG
/* libavcodec stuff */
avpicture_fill( &s->rawPicture, rawBuffer->data, PIX_FMT_YUV420P,
title->inWidth, title->inHeight );
avpicture_fill( &s->scaledPicture, scaledBuffer->data,
PIX_FMT_YUV420P, title->outWidth,
title->outHeight );
/* Do the job */
if( title->deinterlace )
{
avpicture_deinterlace( &s->deintPicture, &s->rawPicture,
PIX_FMT_YUV420P, title->inWidth,
title->inHeight );
img_resample( s->context, &s->scaledPicture,
&s->deintPicture );
}
else
{
img_resample( s->context, &s->scaledPicture, &s->rawPicture );
}
#else
if( title->deinterlace )
{
in = rawBuffer->data;
out = s->deintBuffer->data;
for( plane = 0; plane < 3; plane++ )
{
shift = plane ? 1 : 0;
Deinterlace( in, out, title->inWidth >> shift,
title->inHeight >> shift,
title->topCrop >> shift,
title->bottomCrop >> shift,
title->leftCrop >> shift,
title->rightCrop >> shift );
in += title->inWidth * title->inHeight >> ( 2 * shift );
out += title->inWidth * title->inHeight >> ( 2 * shift );
}
}
in = title->deinterlace ? s->deintBuffer->data : rawBuffer->data;
out = scaledBuffer->data;
for( plane = 0; plane < 3; plane++ )
{
shift = plane ? 1 : 0;
Resample( in, out, title->inWidth >> shift,
title->inHeight >> shift, title->outWidth >> shift,
title->outHeight >> shift, title->topCrop >> shift,
title->bottomCrop >> shift, title->leftCrop >> shift,
title->rightCrop >> shift );
in += title->inWidth * title->inHeight >> ( 2 * shift );
out += title->outWidth * title->outHeight >> ( 2 * shift );;
}
#endif
HBListAdd( s->scaledBufferList, scaledBuffer );
if( rawBuffer->repeat )
{
tmpBuffer = HBBufferInit( scaledBuffer->size );
tmpBuffer->position = scaledBuffer->position;
tmpBuffer->pass = scaledBuffer->pass;
memcpy( tmpBuffer->data, scaledBuffer->data,
scaledBuffer->size );
HBListAdd( s->scaledBufferList, tmpBuffer );
}
/* Free memory */
HBBufferClose( &rawBuffer );
return didSomething;
}
#ifndef USE_FFMPEG
static void Deinterlace( uint8_t * in, uint8_t * out, int w, int h,
int tcrop, int bcrop, int lcrop, int rcrop )
{
int i, j;
/* First line */
if( !tcrop )
{
memcpy( out, in + lcrop, w - lcrop - rcrop );
}
/* Merge lines */
for( i = MAX( 1, tcrop ); i < h - bcrop; i++ )
{
for( j = lcrop; j < w - rcrop; j++ )
{
out[i*w+j] = ( in[(i-1)*w+j] + in[i*w+j] ) / 2;
}
}
}
static void Resample( uint8_t * in, uint8_t * out, int oldw, int oldh,
int neww, int newh, int tcrop, int bcrop,
int lcrop, int rcrop )
{
int i, j;
int cropw = oldw - lcrop - rcrop;
int croph = oldh - tcrop - bcrop;
for( i = 0; i < newh; i++ )
{
for( j = 0; j < neww; j++ )
{
out[i*neww+j] = in[(tcrop+i*croph/newh)*oldw +
lcrop+j*cropw/neww];
}
}
}
#endif
|