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
|
/* $Id: Scale.c,v 1.14 2004/05/02 16:25:00 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"
#include "ffmpeg/avcodec.h"
struct HBWork
{
HB_WORK_COMMON_MEMBERS
HBHandle * handle;
HBTitle * title;
HBBuffer * deintBuffer;
ImgReSampleContext * context;
AVPicture rawPicture;
AVPicture deintPicture;
AVPicture scaledPicture;
};
/* Local prototypes */
static int ScaleWork( HBWork * );
HBWork * HBScaleInit( HBHandle * handle, HBTitle * title )
{
HBWork * w;
if( !( w = malloc( sizeof( HBWork ) ) ) )
{
HBLog( "HBScaleInit: malloc() failed, gonna crash" );
return NULL;
}
w->name = strdup( "Scale" );
w->work = ScaleWork;
w->handle = handle;
w->title = title;
/* Allocate a constant buffer used for deinterlacing */
w->deintBuffer = HBBufferInit( 3 * title->inWidth *
title->inHeight / 2 );
avpicture_fill( &w->deintPicture, w->deintBuffer->data,
PIX_FMT_YUV420P, title->inWidth, title->inHeight );
/* Init libavcodec */
w->context =
img_resample_full_init( title->outWidth, title->outHeight,
title->inWidth, title->inHeight,
title->topCrop, title->bottomCrop,
title->leftCrop, title->rightCrop );
return w;
}
void HBScaleClose( HBWork ** _w )
{
HBWork * w = *_w;
img_resample_close( w->context );
HBBufferClose( &w->deintBuffer );
free( w->name );
free( w );
*_w = NULL;
}
static int ScaleWork( HBWork * w )
{
HBTitle * title = w->title;
HBBuffer * rawBuffer;
HBBuffer * scaledBuffer;
HBBuffer * tmpBuffer;
if( HBFifoIsHalfFull( title->scaledFifo ) )
{
return 0;
}
/* Get a new raw picture */
if( !( rawBuffer = HBFifoPop( title->rawFifo ) ) )
{
return 0;
}
/* Allocate new buffer for the scaled picture */
scaledBuffer = HBBufferInit( 3 * title->outWidth *
title->outHeight / 2 );
scaledBuffer->position = rawBuffer->position;
scaledBuffer->pass = rawBuffer->pass;
/* libavcodec stuff */
avpicture_fill( &w->rawPicture, rawBuffer->data, PIX_FMT_YUV420P,
title->inWidth, title->inHeight );
avpicture_fill( &w->scaledPicture, scaledBuffer->data,
PIX_FMT_YUV420P, title->outWidth,
title->outHeight );
/* Do the job */
if( title->deinterlace )
{
avpicture_deinterlace( &w->deintPicture, &w->rawPicture,
PIX_FMT_YUV420P, title->inWidth,
title->inHeight );
img_resample( w->context, &w->scaledPicture,
&w->deintPicture );
}
else
{
img_resample( w->context, &w->scaledPicture, &w->rawPicture );
}
if( rawBuffer->repeat )
{
tmpBuffer = HBBufferInit( scaledBuffer->size );
tmpBuffer->position = scaledBuffer->position;
tmpBuffer->pass = scaledBuffer->pass;
memcpy( tmpBuffer->data, scaledBuffer->data,
scaledBuffer->size );
if( !HBFifoPush( title->scaledFifo, &tmpBuffer ) )
{
HBLog( "HBScale: HBFifoPush failed" );
}
}
if( !HBFifoPush( title->scaledFifo, &scaledBuffer ) )
{
HBLog( "HBScale: HBFifoPush failed" );
}
/* Free memory */
HBBufferClose( &rawBuffer );
return 1;
}
|