1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Encoder main module - |
* - Encoder main module - |
26 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
27 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 |
* |
* |
29 |
***************************************************************************/ |
****************************************************************************/ |
30 |
|
|
31 |
/**************************************************************************** |
/***************************************************************************** |
32 |
* |
* |
33 |
* History |
* History |
34 |
* |
* |
35 |
|
* 10.07.2002 added BFRAMES_DEC_DEBUG support |
36 |
|
* MinChen <chenm001@163.com> |
37 |
|
* 20.06.2002 bframe patch |
38 |
|
* 08.05.2002 fix some problem in DEBUG mode; |
39 |
|
* MinChen <chenm001@163.com> |
40 |
* 14.04.2002 added FrameCodeB() |
* 14.04.2002 added FrameCodeB() |
41 |
* |
* |
42 |
* $Id: encoder.c,v 1.30 2002-04-28 23:31:00 edgomez Exp $ |
* $Id: encoder.c,v 1.92 2003-02-15 18:48:15 edgomez Exp $ |
43 |
* |
* |
44 |
***************************************************************************/ |
****************************************************************************/ |
45 |
|
|
46 |
#include <stdlib.h> |
#include <stdlib.h> |
47 |
#include <stdio.h> |
#include <stdio.h> |
48 |
#include <math.h> |
#include <math.h> |
49 |
|
#include <string.h> |
50 |
|
|
51 |
#include "encoder.h" |
#include "encoder.h" |
52 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
53 |
#include "global.h" |
#include "global.h" |
54 |
#include "utils/timer.h" |
#include "utils/timer.h" |
55 |
#include "image/image.h" |
#include "image/image.h" |
56 |
|
#include "image/font.h" |
57 |
|
#include "motion/sad.h" |
58 |
|
#include "motion/motion.h" |
59 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
60 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
61 |
#include "bitstream/bitstream.h" |
#include "bitstream/bitstream.h" |
67 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
68 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
69 |
|
|
70 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
/***************************************************************************** |
71 |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
* Local macros |
72 |
|
****************************************************************************/ |
73 |
|
|
74 |
|
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
75 |
|
#define SWAP(_T_,A,B) { _T_ tmp = A; A = B; B = tmp; } |
76 |
|
|
77 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); |
/***************************************************************************** |
78 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header); |
* Local function prototypes |
79 |
|
****************************************************************************/ |
80 |
|
|
81 |
|
static int FrameCodeI(Encoder * pEnc, |
82 |
|
Bitstream * bs, |
83 |
|
uint32_t * pBits); |
84 |
|
|
85 |
|
static int FrameCodeP(Encoder * pEnc, |
86 |
|
Bitstream * bs, |
87 |
|
uint32_t * pBits, |
88 |
|
bool force_inter, |
89 |
|
bool vol_header); |
90 |
|
|
91 |
|
static void FrameCodeB(Encoder * pEnc, |
92 |
|
FRAMEINFO * frame, |
93 |
|
Bitstream * bs, |
94 |
|
uint32_t * pBits); |
95 |
|
|
96 |
|
/***************************************************************************** |
97 |
|
* Local data |
98 |
|
****************************************************************************/ |
99 |
|
|
100 |
static int DQtab[4] = |
static int DQtab[4] = { |
|
{ |
|
101 |
-1, -2, 1, 2 |
-1, -2, 1, 2 |
102 |
}; |
}; |
103 |
|
|
104 |
static int iDQtab[5] = |
static int iDQtab[5] = { |
|
{ |
|
105 |
1, 0, NO_CHANGE, 2, 3 |
1, 0, NO_CHANGE, 2, 3 |
106 |
}; |
}; |
107 |
|
|
108 |
|
|
109 |
void static image_null(IMAGE * image) |
/***************************************************************************** |
110 |
{ |
* Encoder creation |
111 |
image->y = image->u = image->v = NULL; |
* |
112 |
} |
* This function creates an Encoder instance, it allocates all necessary |
113 |
|
* image buffers (reference, current and bframes) and initialize the internal |
114 |
|
* xvid encoder paremeters according to the XVID_ENC_PARAM input parameter. |
115 |
|
* |
116 |
|
* The code seems to be very long but is very basic, mainly memory allocation |
117 |
|
* and cleaning code. |
118 |
|
* |
119 |
|
* Returned values : |
120 |
|
* - XVID_ERR_OK - no errors |
121 |
|
* - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
122 |
|
* cleans the structure before exiting. |
123 |
|
* pParam->handle is also set to NULL. |
124 |
|
* |
125 |
|
****************************************************************************/ |
126 |
|
|
127 |
int encoder_create(XVID_ENC_PARAM * pParam) |
int |
128 |
|
encoder_create(XVID_ENC_PARAM * pParam) |
129 |
{ |
{ |
130 |
Encoder *pEnc; |
Encoder *pEnc; |
131 |
uint32_t i; |
int i; |
132 |
|
|
133 |
pParam->handle = NULL; |
pParam->handle = NULL; |
134 |
|
|
139 |
ENC_CHECK(!(pParam->width % 2)); |
ENC_CHECK(!(pParam->width % 2)); |
140 |
ENC_CHECK(!(pParam->height % 2)); |
ENC_CHECK(!(pParam->height % 2)); |
141 |
|
|
142 |
if (pParam->fincr <= 0 || pParam->fbase <= 0) |
/* Fps */ |
143 |
{ |
|
144 |
|
if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
145 |
pParam->fincr = 1; |
pParam->fincr = 1; |
146 |
pParam->fbase = 25; |
pParam->fbase = 25; |
147 |
} |
} |
148 |
|
|
149 |
// simplify the "fincr/fbase" fraction |
/* |
150 |
// (neccessary, since windows supplies us with huge numbers) |
* Simplify the "fincr/fbase" fraction |
151 |
|
* (neccessary, since windows supplies us with huge numbers) |
152 |
|
*/ |
153 |
|
|
154 |
i = pParam->fincr; |
i = pParam->fincr; |
155 |
while (i > 1) |
while (i > 1) { |
156 |
{ |
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { |
|
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) |
|
|
{ |
|
157 |
pParam->fincr /= i; |
pParam->fincr /= i; |
158 |
pParam->fbase /= i; |
pParam->fbase /= i; |
159 |
i = pParam->fincr; |
i = pParam->fincr; |
162 |
i--; |
i--; |
163 |
} |
} |
164 |
|
|
165 |
if (pParam->fbase > 65535) |
if (pParam->fbase > 65535) { |
|
{ |
|
166 |
float div = (float)pParam->fbase / 65535; |
float div = (float)pParam->fbase / 65535; |
167 |
|
|
168 |
pParam->fbase = (int)(pParam->fbase / div); |
pParam->fbase = (int)(pParam->fbase / div); |
169 |
pParam->fincr = (int)(pParam->fincr / div); |
pParam->fincr = (int)(pParam->fincr / div); |
170 |
} |
} |
171 |
|
|
172 |
|
/* Bitrate allocator defaults */ |
173 |
|
|
174 |
if (pParam->rc_bitrate <= 0) |
if (pParam->rc_bitrate <= 0) |
175 |
pParam->rc_bitrate = 900000; |
pParam->rc_bitrate = 900000; |
176 |
|
|
183 |
if (pParam->rc_buffer <= 0) |
if (pParam->rc_buffer <= 0) |
184 |
pParam->rc_buffer = 100; |
pParam->rc_buffer = 100; |
185 |
|
|
186 |
|
/* Max and min quantizers */ |
187 |
|
|
188 |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
189 |
pParam->min_quantizer = 1; |
pParam->min_quantizer = 1; |
190 |
|
|
191 |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
192 |
pParam->max_quantizer = 31; |
pParam->max_quantizer = 31; |
193 |
|
|
|
if (pParam->max_key_interval == 0) /* 1 keyframe each 10 seconds */ |
|
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
|
|
|
|
194 |
if (pParam->max_quantizer < pParam->min_quantizer) |
if (pParam->max_quantizer < pParam->min_quantizer) |
195 |
pParam->max_quantizer = pParam->min_quantizer; |
pParam->max_quantizer = pParam->min_quantizer; |
196 |
|
|
197 |
if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL) |
/* 1 keyframe each 10 seconds */ |
198 |
|
|
199 |
|
if (pParam->max_key_interval <= 0) |
200 |
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
201 |
|
|
202 |
|
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
203 |
|
if (pEnc == NULL) |
204 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
205 |
|
|
206 |
|
/* Zero the Encoder Structure */ |
207 |
|
|
208 |
|
memset(pEnc, 0, sizeof(Encoder)); |
209 |
|
|
210 |
/* Fill members of Encoder structure */ |
/* Fill members of Encoder structure */ |
211 |
|
|
212 |
pEnc->mbParam.width = pParam->width; |
pEnc->mbParam.width = pParam->width; |
218 |
pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
219 |
pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; |
pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; |
220 |
|
|
221 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->mbParam.fbase = pParam->fbase; |
222 |
|
pEnc->mbParam.fincr = pParam->fincr; |
223 |
|
|
224 |
|
pEnc->mbParam.m_quant_type = H263_QUANT; |
225 |
|
|
226 |
|
pEnc->fMvPrevSigma = -1; |
227 |
|
|
228 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
229 |
|
|
230 |
pEnc->bitrate = pParam->rc_bitrate; |
pEnc->bitrate = pParam->rc_bitrate; |
231 |
|
|
232 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = -1; |
233 |
pEnc->iMaxKeyInterval = pParam->max_key_interval; |
pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval; |
234 |
|
|
235 |
/* try to allocate frame memory */ |
/* try to allocate frame memory */ |
236 |
|
|
237 |
pEnc->current = NULL; |
pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
238 |
pEnc->reference = NULL; |
pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
239 |
if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL || |
|
240 |
(pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL) |
if (pEnc->current == NULL || pEnc->reference == NULL) |
241 |
{ |
goto xvid_err_memory1; |
|
if (pEnc->current) xvid_free(pEnc->current); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
242 |
|
|
243 |
/* try to allocate mb memory */ |
/* try to allocate mb memory */ |
244 |
|
|
245 |
pEnc->current->mbs = NULL; |
pEnc->current->mbs = |
246 |
pEnc->reference->mbs = NULL; |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
247 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
248 |
|
pEnc->reference->mbs = |
249 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
250 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
251 |
|
|
252 |
#ifdef _DEBUG |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
253 |
#ifdef WIN32 |
goto xvid_err_memory2; |
|
OutputDebugString("malloc mbs"); |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL || |
|
|
(pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) |
|
|
{ |
|
|
if (pEnc->current->mbs) xvid_free(pEnc->current->mbs); |
|
|
xvid_free(pEnc->current); |
|
|
xvid_free(pEnc->reference); |
|
|
xvid_free(pEnc); |
|
|
} |
|
254 |
|
|
255 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
256 |
|
|
257 |
#ifdef _DEBUG |
if (pParam->global & XVID_GLOBAL_EXTRASTATS) |
258 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
259 |
#endif |
|
260 |
|
image_null(&pEnc->f_refh); |
261 |
|
image_null(&pEnc->f_refv); |
262 |
|
image_null(&pEnc->f_refhv); |
263 |
|
|
264 |
image_null(&pEnc->current->image); |
image_null(&pEnc->current->image); |
265 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
266 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
269 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
270 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
271 |
|
|
272 |
#ifdef _DEBUG |
if (pParam->global & XVID_GLOBAL_EXTRASTATS) |
273 |
#ifdef WIN32 |
{ if (image_create |
274 |
OutputDebugString("malloc images"); |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
275 |
#endif |
pEnc->mbParam.edged_height) < 0) |
276 |
#endif |
goto xvid_err_memory3; |
277 |
if ( |
} |
|
#ifdef _DEBUG |
|
|
image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
#endif |
|
|
image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
|
|
{ |
|
|
#ifdef _DEBUG |
|
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
#endif |
|
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
278 |
|
|
279 |
xvid_free(pEnc->current); |
if (image_create |
280 |
xvid_free(pEnc->reference); |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
281 |
xvid_free(pEnc); |
pEnc->mbParam.edged_height) < 0) |
282 |
return XVID_ERR_MEMORY; |
goto xvid_err_memory3; |
283 |
|
if (image_create |
284 |
|
(&pEnc->f_refv, pEnc->mbParam.edged_width, |
285 |
|
pEnc->mbParam.edged_height) < 0) |
286 |
|
goto xvid_err_memory3; |
287 |
|
if (image_create |
288 |
|
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
289 |
|
pEnc->mbParam.edged_height) < 0) |
290 |
|
goto xvid_err_memory3; |
291 |
|
|
292 |
|
if (image_create |
293 |
|
(&pEnc->current->image, pEnc->mbParam.edged_width, |
294 |
|
pEnc->mbParam.edged_height) < 0) |
295 |
|
goto xvid_err_memory3; |
296 |
|
if (image_create |
297 |
|
(&pEnc->reference->image, pEnc->mbParam.edged_width, |
298 |
|
pEnc->mbParam.edged_height) < 0) |
299 |
|
goto xvid_err_memory3; |
300 |
|
if (image_create |
301 |
|
(&pEnc->vInterH, pEnc->mbParam.edged_width, |
302 |
|
pEnc->mbParam.edged_height) < 0) |
303 |
|
goto xvid_err_memory3; |
304 |
|
if (image_create |
305 |
|
(&pEnc->vInterV, pEnc->mbParam.edged_width, |
306 |
|
pEnc->mbParam.edged_height) < 0) |
307 |
|
goto xvid_err_memory3; |
308 |
|
if (image_create |
309 |
|
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
310 |
|
pEnc->mbParam.edged_height) < 0) |
311 |
|
goto xvid_err_memory3; |
312 |
|
if (image_create |
313 |
|
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
314 |
|
pEnc->mbParam.edged_height) < 0) |
315 |
|
goto xvid_err_memory3; |
316 |
|
if (image_create |
317 |
|
(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
318 |
|
pEnc->mbParam.edged_height) < 0) |
319 |
|
goto xvid_err_memory3; |
320 |
|
|
321 |
|
/* Create full bitplane for GMC, this might be wasteful */ |
322 |
|
if (image_create |
323 |
|
(&pEnc->vGMC, pEnc->mbParam.edged_width, |
324 |
|
pEnc->mbParam.edged_height) < 0) |
325 |
|
goto xvid_err_memory3; |
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
pEnc->mbParam.global = pParam->global; |
330 |
|
|
331 |
|
/* B Frames specific init */ |
332 |
|
pEnc->mbParam.max_bframes = pParam->max_bframes; |
333 |
|
pEnc->mbParam.bquant_ratio = pParam->bquant_ratio; |
334 |
|
pEnc->mbParam.bquant_offset = pParam->bquant_offset; |
335 |
|
pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio; |
336 |
|
pEnc->bframes = NULL; |
337 |
|
|
338 |
|
if (pEnc->mbParam.max_bframes > 0) { |
339 |
|
int n; |
340 |
|
|
341 |
|
pEnc->bframes = |
342 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), |
343 |
|
CACHE_LINE); |
344 |
|
|
345 |
|
if (pEnc->bframes == NULL) |
346 |
|
goto xvid_err_memory3; |
347 |
|
|
348 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
349 |
|
pEnc->bframes[n] = NULL; |
350 |
|
|
351 |
|
|
352 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
353 |
|
pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
354 |
|
|
355 |
|
if (pEnc->bframes[n] == NULL) |
356 |
|
goto xvid_err_memory4; |
357 |
|
|
358 |
|
pEnc->bframes[n]->mbs = |
359 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
360 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
361 |
|
|
362 |
|
if (pEnc->bframes[n]->mbs == NULL) |
363 |
|
goto xvid_err_memory4; |
364 |
|
|
365 |
|
image_null(&pEnc->bframes[n]->image); |
366 |
|
|
367 |
|
if (image_create |
368 |
|
(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, |
369 |
|
pEnc->mbParam.edged_height) < 0) |
370 |
|
goto xvid_err_memory4; |
371 |
|
|
372 |
|
} |
373 |
|
} |
374 |
|
|
375 |
|
pEnc->bframenum_head = 0; |
376 |
|
pEnc->bframenum_tail = 0; |
377 |
|
pEnc->flush_bframes = 0; |
378 |
|
pEnc->bframenum_dx50bvop = -1; |
379 |
|
|
380 |
|
pEnc->queue = NULL; |
381 |
|
|
382 |
|
|
383 |
|
if (pEnc->mbParam.max_bframes > 0) { |
384 |
|
int n; |
385 |
|
|
386 |
|
pEnc->queue = |
387 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), |
388 |
|
CACHE_LINE); |
389 |
|
|
390 |
|
if (pEnc->queue == NULL) |
391 |
|
goto xvid_err_memory4; |
392 |
|
|
393 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
394 |
|
image_null(&pEnc->queue[n]); |
395 |
|
|
396 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
397 |
|
if (image_create |
398 |
|
(&pEnc->queue[n], pEnc->mbParam.edged_width, |
399 |
|
pEnc->mbParam.edged_height) < 0) |
400 |
|
goto xvid_err_memory5; |
401 |
|
|
402 |
|
} |
403 |
} |
} |
404 |
|
|
405 |
|
pEnc->queue_head = 0; |
406 |
|
pEnc->queue_tail = 0; |
407 |
|
pEnc->queue_size = 0; |
408 |
|
|
409 |
|
pEnc->mbParam.m_stamp = 0; |
410 |
|
|
411 |
|
pEnc->m_framenum = 0; |
412 |
|
pEnc->current->stamp = 0; |
413 |
|
pEnc->reference->stamp = 0; |
414 |
|
|
415 |
pParam->handle = (void *)pEnc; |
pParam->handle = (void *)pEnc; |
416 |
|
|
417 |
if (pParam->rc_bitrate) |
if (pParam->rc_bitrate) { |
418 |
{ |
RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, |
419 |
RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor, |
pParam->rc_reaction_delay_factor, |
420 |
pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr, |
pParam->rc_averaging_period, pParam->rc_buffer, |
421 |
|
pParam->fbase * 1000 / pParam->fincr, |
422 |
pParam->max_quantizer, pParam->min_quantizer); |
pParam->max_quantizer, pParam->min_quantizer); |
423 |
} |
} |
424 |
|
|
425 |
init_timer(); |
init_timer(); |
426 |
|
|
427 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
428 |
|
|
429 |
|
/* |
430 |
|
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
431 |
|
*/ |
432 |
|
|
433 |
|
xvid_err_memory5: |
434 |
|
|
435 |
|
|
436 |
|
if (pEnc->mbParam.max_bframes > 0) { |
437 |
|
|
438 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
439 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
440 |
|
pEnc->mbParam.edged_height); |
441 |
|
} |
442 |
|
xvid_free(pEnc->queue); |
443 |
|
} |
444 |
|
|
445 |
|
xvid_err_memory4: |
446 |
|
|
447 |
|
if (pEnc->mbParam.max_bframes > 0) { |
448 |
|
|
449 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
450 |
|
|
451 |
|
if (pEnc->bframes[i] == NULL) |
452 |
|
continue; |
453 |
|
|
454 |
|
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
455 |
|
pEnc->mbParam.edged_height); |
456 |
|
|
457 |
|
xvid_free(pEnc->bframes[i]->mbs); |
458 |
|
|
459 |
|
xvid_free(pEnc->bframes[i]); |
460 |
|
|
461 |
|
} |
462 |
|
|
463 |
|
xvid_free(pEnc->bframes); |
464 |
} |
} |
465 |
|
|
466 |
|
xvid_err_memory3: |
467 |
|
|
468 |
int encoder_destroy(Encoder * pEnc) |
if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS) |
469 |
|
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
470 |
|
pEnc->mbParam.edged_height); |
471 |
|
} |
472 |
|
|
473 |
|
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
474 |
|
pEnc->mbParam.edged_height); |
475 |
|
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
476 |
|
pEnc->mbParam.edged_height); |
477 |
|
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
478 |
|
pEnc->mbParam.edged_height); |
479 |
|
|
480 |
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
481 |
|
pEnc->mbParam.edged_height); |
482 |
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
483 |
|
pEnc->mbParam.edged_height); |
484 |
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
485 |
|
pEnc->mbParam.edged_height); |
486 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
487 |
|
pEnc->mbParam.edged_height); |
488 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
489 |
|
pEnc->mbParam.edged_height); |
490 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
491 |
|
pEnc->mbParam.edged_height); |
492 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
493 |
|
pEnc->mbParam.edged_height); |
494 |
|
|
495 |
|
/* destroy GMC image */ |
496 |
|
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
497 |
|
pEnc->mbParam.edged_height); |
498 |
|
|
499 |
|
|
500 |
|
xvid_err_memory2: |
501 |
|
xvid_free(pEnc->current->mbs); |
502 |
|
xvid_free(pEnc->reference->mbs); |
503 |
|
|
504 |
|
xvid_err_memory1: |
505 |
|
xvid_free(pEnc->current); |
506 |
|
xvid_free(pEnc->reference); |
507 |
|
xvid_free(pEnc); |
508 |
|
|
509 |
|
pParam->handle = NULL; |
510 |
|
|
511 |
|
return XVID_ERR_MEMORY; |
512 |
|
} |
513 |
|
|
514 |
|
/***************************************************************************** |
515 |
|
* Encoder destruction |
516 |
|
* |
517 |
|
* This function destroy the entire encoder structure created by a previous |
518 |
|
* successful encoder_create call. |
519 |
|
* |
520 |
|
* Returned values (for now only one returned value) : |
521 |
|
* - XVID_ERR_OK - no errors |
522 |
|
* |
523 |
|
****************************************************************************/ |
524 |
|
|
525 |
|
int |
526 |
|
encoder_destroy(Encoder * pEnc) |
527 |
{ |
{ |
528 |
|
int i; |
529 |
|
|
530 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
531 |
|
|
532 |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
/* B Frames specific */ |
533 |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
if (pEnc->mbParam.max_bframes > 0) { |
534 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
535 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
536 |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
537 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
538 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
539 |
#ifdef _DEBUG |
} |
540 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
xvid_free(pEnc->queue); |
541 |
#endif |
} |
542 |
|
|
543 |
|
|
544 |
|
if (pEnc->mbParam.max_bframes > 0) { |
545 |
|
|
546 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
547 |
|
|
548 |
|
if (pEnc->bframes[i] == NULL) |
549 |
|
continue; |
550 |
|
|
551 |
|
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
552 |
|
pEnc->mbParam.edged_height); |
553 |
|
|
554 |
|
xvid_free(pEnc->bframes[i]->mbs); |
555 |
|
|
556 |
|
xvid_free(pEnc->bframes[i]); |
557 |
|
} |
558 |
|
|
559 |
|
xvid_free(pEnc->bframes); |
560 |
|
|
561 |
|
} |
562 |
|
|
563 |
|
/* All images, reference, current etc ... */ |
564 |
|
|
565 |
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
566 |
|
pEnc->mbParam.edged_height); |
567 |
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
568 |
|
pEnc->mbParam.edged_height); |
569 |
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
570 |
|
pEnc->mbParam.edged_height); |
571 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
572 |
|
pEnc->mbParam.edged_height); |
573 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
574 |
|
pEnc->mbParam.edged_height); |
575 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
576 |
|
pEnc->mbParam.edged_height); |
577 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
578 |
|
pEnc->mbParam.edged_height); |
579 |
|
|
580 |
|
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
581 |
|
pEnc->mbParam.edged_height); |
582 |
|
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
583 |
|
pEnc->mbParam.edged_height); |
584 |
|
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
585 |
|
pEnc->mbParam.edged_height); |
586 |
|
|
587 |
|
if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS) |
588 |
|
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
589 |
|
pEnc->mbParam.edged_height); |
590 |
|
} |
591 |
|
|
592 |
|
/* Encoder structure */ |
593 |
|
|
594 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
595 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
596 |
|
|
598 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
599 |
|
|
600 |
xvid_free(pEnc); |
xvid_free(pEnc); |
601 |
|
|
602 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
603 |
} |
} |
604 |
|
|
605 |
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
|
606 |
|
static __inline void inc_frame_num(Encoder * pEnc) |
607 |
|
{ |
608 |
|
pEnc->current->stamp = pEnc->mbParam.m_stamp; // first frame is zero |
609 |
|
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
610 |
|
} |
611 |
|
|
612 |
|
|
613 |
|
static __inline void |
614 |
|
queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
615 |
|
{ |
616 |
|
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
617 |
|
{ |
618 |
|
DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL"); |
619 |
|
return; |
620 |
|
} |
621 |
|
|
622 |
|
DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
623 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
624 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
625 |
|
|
626 |
|
|
627 |
|
start_timer(); |
628 |
|
if (image_input |
629 |
|
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
630 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING)) |
631 |
|
return; |
632 |
|
stop_conv_timer(); |
633 |
|
|
634 |
|
if ((pFrame->general & XVID_CHROMAOPT)) { |
635 |
|
image_chroma_optimize(&pEnc->queue[pEnc->queue_tail], |
636 |
|
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
637 |
|
} |
638 |
|
|
639 |
|
pEnc->queue_size++; |
640 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
641 |
|
} |
642 |
|
|
643 |
|
static __inline void |
644 |
|
set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) |
645 |
|
{ |
646 |
|
|
647 |
|
pCur->ticks = (int32_t)pCur->stamp % time_base; |
648 |
|
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
649 |
|
|
650 |
|
//HEAVY DEBUG OUTPUT remove when timecodes prove to be stable |
651 |
|
|
652 |
|
/* fprintf(stderr,"WriteVop: %d - %d \n", |
653 |
|
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
654 |
|
fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", |
655 |
|
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
656 |
|
fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", |
657 |
|
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
658 |
|
|
659 |
|
*/ |
660 |
|
} |
661 |
|
|
662 |
|
|
663 |
|
|
664 |
|
/* convert pFrame->intra to coding_type */ |
665 |
|
static int intra2coding_type(int intra) |
666 |
|
{ |
667 |
|
if (intra < 0) return -1; |
668 |
|
if (intra == 1) return I_VOP; |
669 |
|
if (intra == 2) return B_VOP; |
670 |
|
|
671 |
|
return P_VOP; |
672 |
|
} |
673 |
|
|
674 |
|
|
675 |
|
|
676 |
|
/***************************************************************************** |
677 |
|
* IPB frame encoder entry point |
678 |
|
* |
679 |
|
* Returned values : |
680 |
|
* - XVID_ERR_OK - no errors |
681 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
682 |
|
* format |
683 |
|
****************************************************************************/ |
684 |
|
|
685 |
|
int |
686 |
|
encoder_encode_bframes(Encoder * pEnc, |
687 |
|
XVID_ENC_FRAME * pFrame, |
688 |
|
XVID_ENC_STATS * pResult) |
689 |
|
{ |
690 |
|
uint16_t x, y; |
691 |
|
Bitstream bs; |
692 |
|
uint32_t bits; |
693 |
|
int mode; |
694 |
|
|
695 |
|
int input_valid = 1; |
696 |
|
int bframes_count = 0; |
697 |
|
|
698 |
|
ENC_CHECK(pEnc); |
699 |
|
ENC_CHECK(pFrame); |
700 |
|
ENC_CHECK(pFrame->image); |
701 |
|
|
702 |
|
start_global_timer(); |
703 |
|
|
704 |
|
BitstreamInit(&bs, pFrame->bitstream, 0); |
705 |
|
|
706 |
|
ipvop_loop: |
707 |
|
|
708 |
|
/* |
709 |
|
* bframe "flush" code |
710 |
|
*/ |
711 |
|
|
712 |
|
if ((pFrame->image == NULL || pEnc->flush_bframes) |
713 |
|
&& (pEnc->bframenum_head < pEnc->bframenum_tail)) { |
714 |
|
|
715 |
|
if (pEnc->flush_bframes == 0) { |
716 |
|
/* |
717 |
|
* we have reached the end of stream without getting |
718 |
|
* a future reference frame... so encode last final |
719 |
|
* frame as a pframe |
720 |
|
*/ |
721 |
|
|
722 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
723 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
724 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
725 |
|
|
726 |
|
pEnc->bframenum_tail--; |
727 |
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
728 |
|
|
729 |
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
730 |
|
|
731 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
732 |
|
bframes_count = 0; |
733 |
|
|
734 |
|
BitstreamPadAlways(&bs); |
735 |
|
pFrame->length = BitstreamLength(&bs); |
736 |
|
pFrame->intra = 0; |
737 |
|
|
738 |
|
|
739 |
|
emms(); |
740 |
|
|
741 |
|
if (pResult) { |
742 |
|
pResult->quant = pEnc->current->quant; |
743 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
744 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
745 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
746 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
747 |
|
} |
748 |
|
|
749 |
|
return XVID_ERR_OK; |
750 |
|
} |
751 |
|
|
752 |
|
|
753 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
754 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
755 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
756 |
|
|
757 |
|
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
758 |
|
pEnc->bframenum_head++; |
759 |
|
|
760 |
|
BitstreamPadAlways(&bs); |
761 |
|
pFrame->length = BitstreamLength(&bs); |
762 |
|
pFrame->intra = 2; |
763 |
|
|
764 |
|
if (pResult) { |
765 |
|
pResult->quant = pEnc->current->quant; |
766 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
767 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
768 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
769 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
770 |
|
} |
771 |
|
|
772 |
|
if (input_valid) |
773 |
|
queue_image(pEnc, pFrame); |
774 |
|
|
775 |
|
emms(); |
776 |
|
|
777 |
|
return XVID_ERR_OK; |
778 |
|
} |
779 |
|
|
780 |
|
if (pEnc->bframenum_head > 0) { |
781 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
782 |
|
|
783 |
|
/* write an empty marker to the bitstream. |
784 |
|
|
785 |
|
for divx5 decoder compatibility, this marker must consist |
786 |
|
of a not-coded p-vop, with a time_base of zero, and time_increment |
787 |
|
indentical to the future-referece frame. |
788 |
|
*/ |
789 |
|
|
790 |
|
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) { |
791 |
|
int tmp; |
792 |
|
|
793 |
|
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
794 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
795 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
796 |
|
|
797 |
|
|
798 |
|
tmp = pEnc->current->seconds; |
799 |
|
pEnc->current->seconds = 0; /* force time_base = 0 */ |
800 |
|
|
801 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
802 |
|
pEnc->current->seconds = tmp; |
803 |
|
|
804 |
|
BitstreamPadAlways(&bs); |
805 |
|
pFrame->length = BitstreamLength(&bs); |
806 |
|
pFrame->intra = 4; |
807 |
|
|
808 |
|
if (pResult) { |
809 |
|
pResult->quant = pEnc->current->quant; |
810 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
811 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
812 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
813 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
814 |
|
} |
815 |
|
|
816 |
|
if (input_valid) |
817 |
|
queue_image(pEnc, pFrame); |
818 |
|
|
819 |
|
emms(); |
820 |
|
|
821 |
|
return XVID_ERR_OK; |
822 |
|
} |
823 |
|
} |
824 |
|
|
825 |
|
|
826 |
|
bvop_loop: |
827 |
|
|
828 |
|
if (pEnc->bframenum_dx50bvop != -1) |
829 |
|
{ |
830 |
|
|
831 |
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
832 |
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
833 |
|
|
834 |
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
835 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
836 |
|
} |
837 |
|
|
838 |
|
if (input_valid) |
839 |
|
{ |
840 |
|
queue_image(pEnc, pFrame); |
841 |
|
input_valid = 0; |
842 |
|
} |
843 |
|
|
844 |
|
} else if (input_valid) { |
845 |
|
|
846 |
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
847 |
|
|
848 |
|
start_timer(); |
849 |
|
if (image_input |
850 |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
851 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING)) |
852 |
|
{ |
853 |
|
emms(); |
854 |
|
return XVID_ERR_FORMAT; |
855 |
|
} |
856 |
|
stop_conv_timer(); |
857 |
|
|
858 |
|
if ((pFrame->general & XVID_CHROMAOPT)) { |
859 |
|
image_chroma_optimize(&pEnc->current->image, |
860 |
|
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
861 |
|
} |
862 |
|
|
863 |
|
// queue input frame, and dequue next image |
864 |
|
if (pEnc->queue_size > 0) |
865 |
|
{ |
866 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
867 |
|
if (pEnc->queue_head != pEnc->queue_tail) |
868 |
|
{ |
869 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
870 |
|
} |
871 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
872 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
873 |
|
} |
874 |
|
|
875 |
|
} else if (pEnc->queue_size > 0) { |
876 |
|
|
877 |
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
878 |
|
|
879 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
880 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
881 |
|
pEnc->queue_size--; |
882 |
|
|
883 |
|
} else { |
884 |
|
|
885 |
|
/* if nothing was encoded, write an 'ignore this frame' flag |
886 |
|
to the bitstream */ |
887 |
|
|
888 |
|
if (BitstreamPos(&bs) == 0) { |
889 |
|
|
890 |
|
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
891 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
892 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
893 |
|
|
894 |
|
// BitstreamPutBits(&bs, 0x7f, 8); |
895 |
|
pFrame->intra = 5; |
896 |
|
|
897 |
|
if (pResult) { |
898 |
|
/* |
899 |
|
* We must decide what to put there because i know some apps |
900 |
|
* are storing statistics about quantizers and just do |
901 |
|
* stats[quant]++ or stats[quant-1]++ |
902 |
|
* transcode is one of these app with its 2pass module |
903 |
|
*/ |
904 |
|
|
905 |
|
/* |
906 |
|
* For now i prefer 31 than 0 that could lead to a segfault |
907 |
|
* in transcode |
908 |
|
*/ |
909 |
|
pResult->quant = 31; |
910 |
|
|
911 |
|
pResult->hlength = 0; |
912 |
|
pResult->kblks = 0; |
913 |
|
pResult->mblks = 0; |
914 |
|
pResult->ublks = 0; |
915 |
|
} |
916 |
|
|
917 |
|
} else { |
918 |
|
|
919 |
|
if (pResult) { |
920 |
|
pResult->quant = pEnc->current->quant; |
921 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
922 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
923 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
924 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
925 |
|
} |
926 |
|
|
927 |
|
} |
928 |
|
|
929 |
|
pFrame->length = BitstreamLength(&bs); |
930 |
|
|
931 |
|
emms(); |
932 |
|
|
933 |
|
return XVID_ERR_OK; |
934 |
|
} |
935 |
|
|
936 |
|
pEnc->flush_bframes = 0; |
937 |
|
|
938 |
|
emms(); |
939 |
|
|
940 |
|
// only inc frame num, adapt quant, etc. if we havent seen it before |
941 |
|
if (pEnc->bframenum_dx50bvop < 0 ) |
942 |
|
{ |
943 |
|
mode = intra2coding_type(pFrame->intra); |
944 |
|
if (pFrame->quant == 0) |
945 |
|
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
946 |
|
else |
947 |
|
pEnc->current->quant = pFrame->quant; |
948 |
|
|
949 |
|
/* if (pEnc->current->quant < 1) |
950 |
|
pEnc->current->quant = 1; |
951 |
|
|
952 |
|
if (pEnc->current->quant > 31) |
953 |
|
pEnc->current->quant = 31; |
954 |
|
*/ |
955 |
|
pEnc->current->global_flags = pFrame->general; |
956 |
|
pEnc->current->motion_flags = pFrame->motion; |
957 |
|
|
958 |
|
/* ToDo : dynamic fcode (in both directions) */ |
959 |
|
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
960 |
|
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
961 |
|
|
962 |
|
inc_frame_num(pEnc); |
963 |
|
|
964 |
|
if (pFrame->general & XVID_EXTRASTATS) |
965 |
|
{ image_copy(&pEnc->sOriginal, &pEnc->current->image, |
966 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
967 |
|
} |
968 |
|
|
969 |
|
emms(); |
970 |
|
|
971 |
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
972 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
973 |
|
"%i if:%i st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp); |
974 |
|
} |
975 |
|
|
976 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
977 |
|
* Luminance masking |
978 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
979 |
|
|
980 |
|
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
981 |
|
int *temp_dquants = |
982 |
|
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
983 |
|
pEnc->mbParam.mb_height * sizeof(int), |
984 |
|
CACHE_LINE); |
985 |
|
|
986 |
|
pEnc->current->quant = |
987 |
|
adaptive_quantization(pEnc->current->image.y, |
988 |
|
pEnc->mbParam.edged_width, temp_dquants, |
989 |
|
pEnc->current->quant, pEnc->current->quant, |
990 |
|
2 * pEnc->current->quant, |
991 |
|
pEnc->mbParam.mb_width, |
992 |
|
pEnc->mbParam.mb_height); |
993 |
|
|
994 |
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
995 |
|
|
996 |
|
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
997 |
|
|
998 |
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
999 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
1000 |
|
|
1001 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
1002 |
|
} |
1003 |
|
|
1004 |
|
#undef OFFSET |
1005 |
|
} |
1006 |
|
|
1007 |
|
xvid_free(temp_dquants); |
1008 |
|
} |
1009 |
|
|
1010 |
|
} |
1011 |
|
|
1012 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1013 |
|
* ivop/pvop/bvop selection |
1014 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1015 |
|
pEnc->iFrameNum++; |
1016 |
|
|
1017 |
|
if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 || |
1018 |
|
(mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 && |
1019 |
|
pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) |
1020 |
|
{ |
1021 |
|
mode = I_VOP; |
1022 |
|
}else{ |
1023 |
|
mode = MEanalysis(&pEnc->reference->image, pEnc->current, |
1024 |
|
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1025 |
|
(mode < 0) ? pEnc->iFrameNum : 0, |
1026 |
|
bframes_count++); |
1027 |
|
} |
1028 |
|
|
1029 |
|
if (mode == I_VOP) { |
1030 |
|
/* |
1031 |
|
* This will be coded as an Intra Frame |
1032 |
|
*/ |
1033 |
|
if ((pEnc->current->global_flags & XVID_QUARTERPEL)) |
1034 |
|
pEnc->mbParam.m_quarterpel = 1; |
1035 |
|
else |
1036 |
|
pEnc->mbParam.m_quarterpel = 0; |
1037 |
|
|
1038 |
|
if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
1039 |
|
|
1040 |
|
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
1041 |
|
if (pFrame->quant_intra_matrix != NULL) |
1042 |
|
set_intra_matrix(pFrame->quant_intra_matrix); |
1043 |
|
if (pFrame->quant_inter_matrix != NULL) |
1044 |
|
set_inter_matrix(pFrame->quant_inter_matrix); |
1045 |
|
} |
1046 |
|
|
1047 |
|
|
1048 |
|
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1049 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1050 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1051 |
|
|
1052 |
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
1053 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
1054 |
|
} |
1055 |
|
|
1056 |
|
// when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe |
1057 |
|
|
1058 |
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
1059 |
|
|
1060 |
|
pEnc->bframenum_tail--; |
1061 |
|
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
1062 |
|
|
1063 |
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
1064 |
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
1065 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
1066 |
|
} |
1067 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
1068 |
|
bframes_count = 0; |
1069 |
|
pFrame->intra = 0; |
1070 |
|
|
1071 |
|
} else { |
1072 |
|
|
1073 |
|
FrameCodeI(pEnc, &bs, &bits); |
1074 |
|
bframes_count = 0; |
1075 |
|
pFrame->intra = 1; |
1076 |
|
|
1077 |
|
pEnc->bframenum_dx50bvop = -1; |
1078 |
|
} |
1079 |
|
|
1080 |
|
pEnc->flush_bframes = 1; |
1081 |
|
|
1082 |
|
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
1083 |
|
BitstreamPadAlways(&bs); |
1084 |
|
input_valid = 0; |
1085 |
|
goto ipvop_loop; |
1086 |
|
} |
1087 |
|
|
1088 |
|
/* |
1089 |
|
* NB : sequences like "IIBB" decode fine with msfdam but, |
1090 |
|
* go screwy with divx 5.00 |
1091 |
|
*/ |
1092 |
|
} else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1093 |
|
/* |
1094 |
|
* This will be coded as a Predicted Frame |
1095 |
|
*/ |
1096 |
|
|
1097 |
|
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1098 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1099 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1100 |
|
|
1101 |
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
1102 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
1103 |
|
} |
1104 |
|
|
1105 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
1106 |
|
bframes_count = 0; |
1107 |
|
pFrame->intra = 0; |
1108 |
|
pEnc->flush_bframes = 1; |
1109 |
|
|
1110 |
|
if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) { |
1111 |
|
BitstreamPadAlways(&bs); |
1112 |
|
input_valid = 0; |
1113 |
|
goto ipvop_loop; |
1114 |
|
} |
1115 |
|
|
1116 |
|
} else { /* mode == B_VOP */ |
1117 |
|
/* |
1118 |
|
* This will be coded as a Bidirectional Frame |
1119 |
|
*/ |
1120 |
|
|
1121 |
|
if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) { |
1122 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1123 |
|
} |
1124 |
|
|
1125 |
|
if (pFrame->bquant < 1) { |
1126 |
|
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
1127 |
|
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
1128 |
|
|
1129 |
|
} else { |
1130 |
|
pEnc->current->quant = pFrame->bquant; |
1131 |
|
} |
1132 |
|
|
1133 |
|
if (pEnc->current->quant < 1) |
1134 |
|
pEnc->current->quant = 1; |
1135 |
|
else if (pEnc->current->quant > 31) |
1136 |
|
pEnc->current->quant = 31; |
1137 |
|
|
1138 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
1139 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1140 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1141 |
|
|
1142 |
|
/* store frame into bframe buffer & swap ref back to current */ |
1143 |
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1144 |
|
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
1145 |
|
|
1146 |
|
pEnc->bframenum_tail++; |
1147 |
|
|
1148 |
|
// bframe report by koepi |
1149 |
|
pFrame->intra = 2; |
1150 |
|
pFrame->length = 0; |
1151 |
|
|
1152 |
|
input_valid = 0; |
1153 |
|
goto bvop_loop; |
1154 |
|
} |
1155 |
|
|
1156 |
|
BitstreamPadAlways(&bs); |
1157 |
|
pFrame->length = BitstreamLength(&bs); |
1158 |
|
|
1159 |
|
if (pResult) { |
1160 |
|
pResult->quant = pEnc->current->quant; |
1161 |
|
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
1162 |
|
pResult->kblks = pEnc->current->sStat.kblks; |
1163 |
|
pResult->mblks = pEnc->current->sStat.mblks; |
1164 |
|
pResult->ublks = pEnc->current->sStat.ublks; |
1165 |
|
|
1166 |
|
if (pFrame->general & XVID_EXTRASTATS) |
1167 |
|
{ pResult->sse_y = |
1168 |
|
plane_sse( pEnc->sOriginal.y, pEnc->current->image.y, |
1169 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1170 |
|
pEnc->mbParam.height); |
1171 |
|
|
1172 |
|
pResult->sse_u = |
1173 |
|
plane_sse( pEnc->sOriginal.u, pEnc->current->image.u, |
1174 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
1175 |
|
pEnc->mbParam.height/2); |
1176 |
|
|
1177 |
|
pResult->sse_v = |
1178 |
|
plane_sse( pEnc->sOriginal.v, pEnc->current->image.v, |
1179 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
1180 |
|
pEnc->mbParam.height/2); |
1181 |
|
} |
1182 |
|
} |
1183 |
|
|
1184 |
|
emms(); |
1185 |
|
|
1186 |
|
if (pFrame->quant == 0) { |
1187 |
|
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1188 |
|
pFrame->length, pFrame->intra); |
1189 |
|
} |
1190 |
|
|
1191 |
|
stop_global_timer(); |
1192 |
|
write_timer(); |
1193 |
|
|
1194 |
|
emms(); |
1195 |
|
return XVID_ERR_OK; |
1196 |
|
} |
1197 |
|
|
1198 |
|
|
1199 |
|
|
1200 |
|
/***************************************************************************** |
1201 |
|
* "original" IP frame encoder entry point |
1202 |
|
* |
1203 |
|
* Returned values : |
1204 |
|
* - XVID_ERR_OK - no errors |
1205 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
1206 |
|
* format |
1207 |
|
****************************************************************************/ |
1208 |
|
|
1209 |
|
int |
1210 |
|
encoder_encode(Encoder * pEnc, |
1211 |
|
XVID_ENC_FRAME * pFrame, |
1212 |
|
XVID_ENC_STATS * pResult) |
1213 |
{ |
{ |
1214 |
uint16_t x, y; |
uint16_t x, y; |
1215 |
Bitstream bs; |
Bitstream bs; |
1216 |
uint32_t bits; |
uint32_t bits; |
1217 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1218 |
#ifdef _DEBUG |
|
1219 |
float psnr; |
float psnr; |
1220 |
uint8_t temp[100]; |
uint8_t temp[128]; |
|
#endif |
|
1221 |
|
|
1222 |
start_global_timer(); |
start_global_timer(); |
1223 |
|
|
1226 |
ENC_CHECK(pFrame->bitstream); |
ENC_CHECK(pFrame->bitstream); |
1227 |
ENC_CHECK(pFrame->image); |
ENC_CHECK(pFrame->image); |
1228 |
|
|
1229 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO *, pEnc->current, pEnc->reference); |
1230 |
|
|
1231 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1232 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1233 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1234 |
|
|
1235 |
|
inc_frame_num(pEnc); |
1236 |
|
|
1237 |
|
/* disable alternate scan flag if interlacing is not enabled */ |
1238 |
|
if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) && |
1239 |
|
!(pEnc->current->global_flags & XVID_INTERLACING)) |
1240 |
|
{ |
1241 |
|
pEnc->current->global_flags -= XVID_ALTERNATESCAN; |
1242 |
|
} |
1243 |
|
|
1244 |
start_timer(); |
start_timer(); |
1245 |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input |
1246 |
pFrame->image, pFrame->colorspace)) |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
1247 |
{ |
pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0) |
1248 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
|
} |
|
1249 |
stop_conv_timer(); |
stop_conv_timer(); |
1250 |
|
|
1251 |
#ifdef _DEBUG |
if ((pFrame->general & XVID_CHROMAOPT)) { |
1252 |
image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_chroma_optimize(&pEnc->current->image, |
1253 |
#endif |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
1254 |
|
} |
1255 |
|
|
1256 |
EMMS(); |
if (pFrame->general & XVID_EXTRASTATS) |
1257 |
|
{ image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1258 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1259 |
|
} |
1260 |
|
|
1261 |
|
emms(); |
1262 |
|
|
1263 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
1264 |
|
|
1265 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
1266 |
{ |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
1267 |
pEnc->current->quant = RateControlGetQ(0); |
} else { |
|
} |
|
|
else |
|
|
{ |
|
1268 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
1269 |
} |
} |
1270 |
|
|
1271 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
if ((pEnc->current->global_flags & XVID_QUARTERPEL)) |
1272 |
{ |
pEnc->mbParam.m_quarterpel = 1; |
1273 |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
else |
1274 |
|
pEnc->mbParam.m_quarterpel = 0; |
1275 |
|
|
1276 |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1277 |
pEnc->mbParam.edged_width, // stride |
int *temp_dquants = |
1278 |
temp_dquants, |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
1279 |
pEnc->current->quant, |
pEnc->mbParam.mb_height * sizeof(int), |
1280 |
pEnc->current->quant, // min_quant |
CACHE_LINE); |
1281 |
2*pEnc->current->quant, // max_quant |
|
1282 |
|
pEnc->current->quant = |
1283 |
|
adaptive_quantization(pEnc->current->image.y, |
1284 |
|
pEnc->mbParam.edged_width, temp_dquants, |
1285 |
|
pEnc->current->quant, pEnc->current->quant, |
1286 |
|
2 * pEnc->current->quant, |
1287 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
1288 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
1289 |
|
|
1290 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1291 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
1292 |
{ |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
1293 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1294 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1295 |
|
|
1296 |
|
|
1297 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
1298 |
|
|
1299 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
1300 |
|
} |
1301 |
|
|
1302 |
|
#undef OFFSET |
1303 |
} |
} |
1304 |
|
|
1305 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
1306 |
} |
} |
1307 |
|
|
1309 |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
1310 |
write_vol_header = 1; |
write_vol_header = 1; |
1311 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
1312 |
} |
} else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
1313 |
else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
int matrix1_changed, matrix2_changed; |
|
int ret1, ret2; |
|
1314 |
|
|
1315 |
ret1 = ret2 = 0; |
matrix1_changed = matrix2_changed = 0; |
1316 |
|
|
1317 |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
1318 |
write_vol_header = 1; |
write_vol_header = 1; |
1321 |
|
|
1322 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
1323 |
if(pFrame->quant_intra_matrix != NULL) |
if(pFrame->quant_intra_matrix != NULL) |
1324 |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); |
1325 |
if(pFrame->quant_inter_matrix != NULL) |
if(pFrame->quant_inter_matrix != NULL) |
1326 |
ret2 = set_inter_matrix(pFrame->quant_inter_matrix); |
matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); |
1327 |
} |
} else { |
1328 |
else { |
matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
1329 |
ret1 = set_intra_matrix(get_default_intra_matrix()); |
matrix2_changed = set_inter_matrix(get_default_inter_matrix()); |
|
ret2 = set_inter_matrix(get_default_inter_matrix()); |
|
1330 |
} |
} |
1331 |
if(write_vol_header == 0) |
if(write_vol_header == 0) |
1332 |
write_vol_header = ret1 | ret2; |
write_vol_header = matrix1_changed | matrix2_changed; |
1333 |
} |
} |
1334 |
|
|
1335 |
if (pFrame->intra < 0) |
if (pFrame->intra < 0) { |
1336 |
{ |
if ((pEnc->iFrameNum == -1) |
1337 |
if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
|| ((pEnc->mbParam.iMaxKeyInterval > 0) |
1338 |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
&& (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) { |
|
|
|
1339 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
1340 |
else |
} else { |
1341 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
1342 |
} |
} |
1343 |
else |
} else { |
1344 |
{ |
if (pFrame->intra == 1) { |
|
if (pFrame->intra == 1) |
|
1345 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
1346 |
else |
} else { |
1347 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
1348 |
} |
} |
1349 |
|
|
1350 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
} |
1351 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
|
1352 |
BitstreamPad(&bs); |
// BitstreamPutBits(&bs, 0xFFFF, 16); |
1353 |
|
// BitstreamPutBits(&bs, 0xFFFF, 16); |
1354 |
|
BitstreamPadAlways(&bs); |
1355 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1356 |
|
|
1357 |
if (pResult) |
if (pResult) { |
|
{ |
|
1358 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
1359 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8); |
1360 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->current->sStat.kblks; |
1361 |
pResult->mblks = pEnc->sStat.mblks; |
pResult->mblks = pEnc->current->sStat.mblks; |
1362 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->current->sStat.ublks; |
1363 |
} |
} |
1364 |
|
|
1365 |
EMMS(); |
emms(); |
|
|
|
|
if (pFrame->quant == 0) |
|
|
{ |
|
|
RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
|
|
} |
|
1366 |
|
|
1367 |
#ifdef _DEBUG |
if (pFrame->quant == 0) { |
1368 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1369 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pFrame->length, pFrame->intra); |
1370 |
|
} |
1371 |
|
if (pFrame->general & XVID_EXTRASTATS) |
1372 |
|
{ |
1373 |
|
psnr = |
1374 |
|
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1375 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1376 |
|
pEnc->mbParam.height); |
1377 |
|
|
1378 |
sprintf(temp, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1379 |
DEBUG(temp); |
} |
|
#endif |
|
1380 |
|
|
1381 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1382 |
|
|
1387 |
} |
} |
1388 |
|
|
1389 |
|
|
1390 |
static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { |
static __inline void |
1391 |
|
CodeIntraMB(Encoder * pEnc, |
1392 |
|
MACROBLOCK * pMB) |
1393 |
|
{ |
1394 |
|
|
1395 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1396 |
|
|
1401 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1402 |
|
|
1403 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1404 |
if(pMB->dquant != NO_CHANGE) |
if (pMB->dquant != NO_CHANGE) { |
|
{ |
|
1405 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
1406 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1407 |
|
|
1408 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1409 |
if (pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1410 |
|
if (pEnc->current->quant < 1) |
1411 |
|
pEnc->current->quant = 1; |
1412 |
} |
} |
1413 |
} |
} |
1414 |
|
|
1419 |
#define FCODEBITS 3 |
#define FCODEBITS 3 |
1420 |
#define MODEBITS 5 |
#define MODEBITS 5 |
1421 |
|
|
1422 |
void HintedMESet(Encoder * pEnc, int * intra) |
void |
1423 |
|
HintedMESet(Encoder * pEnc, |
1424 |
|
int *intra) |
1425 |
{ |
{ |
1426 |
HINTINFO * hint; |
HINTINFO * hint; |
1427 |
Bitstream bs; |
Bitstream bs; |
1430 |
|
|
1431 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1432 |
|
|
1433 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1434 |
*intra = hint->mvhint.intra; |
*intra = hint->mvhint.intra; |
1435 |
} |
} else { |
|
else |
|
|
{ |
|
1436 |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
1437 |
*intra = BitstreamGetBit(&bs); |
*intra = BitstreamGetBit(&bs); |
1438 |
} |
} |
1439 |
|
|
1440 |
if (*intra) |
if (*intra) { |
|
{ |
|
1441 |
return; |
return; |
1442 |
} |
} |
1443 |
|
|
1444 |
pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
pEnc->current->fcode = |
1445 |
|
(hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, |
1446 |
|
FCODEBITS); |
1447 |
|
|
1448 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1449 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1450 |
|
|
1451 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1452 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1453 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1454 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1455 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1456 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1457 |
VECTOR pred[4]; |
VECTOR pred; |
1458 |
VECTOR tmp; |
VECTOR tmp; |
|
int32_t dummy[4]; |
|
1459 |
int vec; |
int vec; |
1460 |
|
|
1461 |
pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
pMB->mode = |
1462 |
|
(hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, |
1463 |
|
MODEBITS); |
1464 |
|
|
1465 |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
1466 |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
1467 |
|
|
1468 |
if (pMB->mode == MODE_INTER) |
if (pMB->mode == MODE_INTER) { |
1469 |
{ |
tmp.x = |
1470 |
tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, |
1471 |
tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); |
length); |
1472 |
|
tmp.y = |
1473 |
|
(hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, |
1474 |
|
length); |
1475 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1476 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1477 |
|
|
1478 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy); |
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0); |
1479 |
|
|
1480 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1481 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1482 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1483 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x; |
1484 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y; |
|
} |
|
1485 |
} |
} |
1486 |
else if (pMB->mode == MODE_INTER4V) |
} else if (pMB->mode == MODE_INTER4V) { |
1487 |
{ |
for (vec = 0; vec < 4; ++vec) { |
1488 |
for (vec=0 ; vec<4 ; ++vec) |
tmp.x = |
1489 |
{ |
(hint->rawhints) ? bhint->mvs[vec]. |
1490 |
tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); |
x : BitstreamGetBits(&bs, length); |
1491 |
tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); |
tmp.y = |
1492 |
|
(hint->rawhints) ? bhint->mvs[vec]. |
1493 |
|
y : BitstreamGetBits(&bs, length); |
1494 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1495 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1496 |
|
|
1497 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec); |
1498 |
|
|
1499 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1500 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1501 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
1502 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; |
1503 |
} |
} |
1504 |
} |
} else // intra / stuffing / not_coded |
|
else // intra / stuffing / not_coded |
|
|
{ |
|
|
for (vec=0 ; vec<4 ; ++vec) |
|
1505 |
{ |
{ |
1506 |
|
for (vec = 0; vec < 4; ++vec) { |
1507 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1508 |
} |
} |
1509 |
} |
} |
1510 |
|
|
1511 |
if (pMB->mode == MODE_INTER4V && |
if (pMB->mode == MODE_INTER4V && |
1512 |
(pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) |
(pEnc->current->global_flags & XVID_LUMIMASKING) |
1513 |
{ |
&& pMB->dquant != NO_CHANGE) { |
1514 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1515 |
|
|
1516 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1517 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1518 |
} |
} |
1519 |
} |
} |
1522 |
} |
} |
1523 |
|
|
1524 |
|
|
1525 |
void HintedMEGet(Encoder * pEnc, int intra) |
void |
1526 |
|
HintedMEGet(Encoder * pEnc, |
1527 |
|
int intra) |
1528 |
{ |
{ |
1529 |
HINTINFO * hint; |
HINTINFO * hint; |
1530 |
Bitstream bs; |
Bitstream bs; |
1533 |
|
|
1534 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1535 |
|
|
1536 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1537 |
hint->mvhint.intra = intra; |
hint->mvhint.intra = intra; |
1538 |
} |
} else { |
|
else |
|
|
{ |
|
1539 |
BitstreamInit(&bs, hint->hintstream, 0); |
BitstreamInit(&bs, hint->hintstream, 0); |
1540 |
BitstreamPutBit(&bs, intra); |
BitstreamPutBit(&bs, intra); |
1541 |
} |
} |
1542 |
|
|
1543 |
if (intra) |
if (intra) { |
1544 |
{ |
if (!hint->rawhints) { |
1545 |
if (!hint->rawhints) |
BitstreamPadAlways(&bs); |
|
{ |
|
|
BitstreamPad(&bs); |
|
1546 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1547 |
} |
} |
1548 |
return; |
return; |
1551 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1552 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1553 |
|
|
1554 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1555 |
hint->mvhint.fcode = pEnc->current->fcode; |
hint->mvhint.fcode = pEnc->current->fcode; |
1556 |
} |
} else { |
|
else |
|
|
{ |
|
1557 |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
1558 |
} |
} |
1559 |
|
|
1560 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1561 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1562 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1563 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1564 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1565 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1566 |
VECTOR tmp; |
VECTOR tmp; |
1567 |
|
|
1568 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1569 |
bhint->mode = pMB->mode; |
bhint->mode = pMB->mode; |
1570 |
} |
} else { |
|
else |
|
|
{ |
|
1571 |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
1572 |
} |
} |
1573 |
|
|
1574 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
1575 |
tmp.x = pMB->mvs[0].x; |
tmp.x = pMB->mvs[0].x; |
1576 |
tmp.y = pMB->mvs[0].y; |
tmp.y = pMB->mvs[0].y; |
1577 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1578 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1579 |
|
|
1580 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1581 |
bhint->mvs[0].x = tmp.x; |
bhint->mvs[0].x = tmp.x; |
1582 |
bhint->mvs[0].y = tmp.y; |
bhint->mvs[0].y = tmp.y; |
1583 |
} |
} else { |
|
else |
|
|
{ |
|
1584 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1585 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1586 |
} |
} |
1587 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
|
else if (pMB->mode == MODE_INTER4V) |
|
|
{ |
|
1588 |
int vec; |
int vec; |
1589 |
|
|
1590 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1591 |
tmp.x = pMB->mvs[vec].x; |
tmp.x = pMB->mvs[vec].x; |
1592 |
tmp.y = pMB->mvs[vec].y; |
tmp.y = pMB->mvs[vec].y; |
1593 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1594 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1595 |
|
|
1596 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1597 |
bhint->mvs[vec].x = tmp.x; |
bhint->mvs[vec].x = tmp.x; |
1598 |
bhint->mvs[vec].y = tmp.y; |
bhint->mvs[vec].y = tmp.y; |
1599 |
} |
} else { |
|
else |
|
|
{ |
|
1600 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1601 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1602 |
} |
} |
1605 |
} |
} |
1606 |
} |
} |
1607 |
|
|
1608 |
if (!hint->rawhints) |
if (!hint->rawhints) { |
|
{ |
|
1609 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1610 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1611 |
} |
} |
1612 |
} |
} |
1613 |
|
|
1614 |
|
|
1615 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
static int |
1616 |
|
FrameCodeI(Encoder * pEnc, |
1617 |
|
Bitstream * bs, |
1618 |
|
uint32_t * pBits) |
1619 |
{ |
{ |
1620 |
|
int mb_width = pEnc->mbParam.mb_width; |
1621 |
|
int mb_height = pEnc->mbParam.mb_height; |
1622 |
|
|
1623 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1624 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1625 |
|
|
1626 |
uint16_t x, y; |
uint16_t x, y; |
1627 |
|
|
1628 |
|
if ((pEnc->current->global_flags & XVID_REDUCED)) |
1629 |
|
{ |
1630 |
|
mb_width = (pEnc->mbParam.width + 31) / 32; |
1631 |
|
mb_height = (pEnc->mbParam.height + 31) / 32; |
1632 |
|
|
1633 |
|
/* 16x16->8x8 downsample requires 1 additional edge pixel*/ |
1634 |
|
/* XXX: setedges is overkill */ |
1635 |
|
start_timer(); |
1636 |
|
image_setedges(&pEnc->current->image, |
1637 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1638 |
|
pEnc->mbParam.width, pEnc->mbParam.height); |
1639 |
|
stop_edges_timer(); |
1640 |
|
} |
1641 |
|
|
1642 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
1643 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1644 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1645 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1646 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1647 |
|
|
1648 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
|
1649 |
|
|
1650 |
*pBits = BitstreamPos(bs); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1651 |
|
|
1652 |
pEnc->sStat.iTextBits = 0; |
BitstreamPadAlways(bs); |
1653 |
pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
|
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
|
1654 |
|
|
1655 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
*pBits = BitstreamPos(bs); |
1656 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
1657 |
{ |
pEnc->current->sStat.iTextBits = 0; |
1658 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
pEnc->current->sStat.kblks = mb_width * mb_height; |
1659 |
|
pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
1660 |
|
|
1661 |
|
for (y = 0; y < mb_height; y++) |
1662 |
|
for (x = 0; x < mb_width; x++) { |
1663 |
|
MACROBLOCK *pMB = |
1664 |
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1665 |
|
|
1666 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1667 |
|
|
1668 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1669 |
|
dct_codes, qcoeff); |
1670 |
|
|
1671 |
start_timer(); |
start_timer(); |
1672 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1673 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1674 |
|
|
1675 |
start_timer(); |
start_timer(); |
1676 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1677 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1678 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1679 |
|
qcoeff[5*64+0]=0; |
1680 |
|
} |
1681 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1682 |
stop_coding_timer(); |
stop_coding_timer(); |
1683 |
} |
} |
1684 |
|
|
1685 |
|
if ((pEnc->current->global_flags & XVID_REDUCED)) |
1686 |
|
{ |
1687 |
|
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
1688 |
|
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
1689 |
|
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
1690 |
|
} |
1691 |
emms(); |
emms(); |
1692 |
|
|
1693 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1694 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
|
pEnc->sStat.iMvSum = 0; |
|
|
pEnc->sStat.iMvCount = 0; |
|
1695 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1696 |
|
|
1697 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1698 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
1699 |
} |
} |
1700 |
|
|
1703 |
|
|
1704 |
|
|
1705 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1706 |
|
#define BFRAME_SKIP_THRESHHOLD 30 |
1707 |
|
|
1708 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) |
|
1709 |
|
/* FrameCodeP also handles S(GMC)-VOPs */ |
1710 |
|
static int |
1711 |
|
FrameCodeP(Encoder * pEnc, |
1712 |
|
Bitstream * bs, |
1713 |
|
uint32_t * pBits, |
1714 |
|
bool force_inter, |
1715 |
|
bool vol_header) |
1716 |
{ |
{ |
1717 |
float fSigma; |
float fSigma; |
1718 |
|
|
1719 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1720 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1721 |
|
|
1722 |
|
int mb_width = pEnc->mbParam.mb_width; |
1723 |
|
int mb_height = pEnc->mbParam.mb_height; |
1724 |
|
|
1725 |
int iLimit; |
int iLimit; |
1726 |
uint32_t x, y; |
int x, y, k; |
1727 |
int iSearchRange; |
int iSearchRange; |
1728 |
int bIntra; |
int bIntra, skip_possible; |
1729 |
|
|
1730 |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
1731 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1732 |
|
|
1733 |
|
if ((pEnc->current->global_flags & XVID_REDUCED)) |
1734 |
|
{ |
1735 |
|
mb_width = (pEnc->mbParam.width + 31) / 32; |
1736 |
|
mb_height = (pEnc->mbParam.height + 31) / 32; |
1737 |
|
} |
1738 |
|
|
1739 |
|
|
1740 |
start_timer(); |
start_timer(); |
1741 |
image_setedges(pRef, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1742 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.width, pEnc->mbParam.height); |
|
pEnc->mbParam.edged_height, |
|
|
pEnc->mbParam.width, |
|
|
pEnc->mbParam.height, |
|
|
pEnc->current->global_flags & XVID_INTERLACING); |
|
1743 |
stop_edges_timer(); |
stop_edges_timer(); |
1744 |
|
|
1745 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1746 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1747 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1748 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1749 |
|
|
1750 |
if (!force_inter) |
if (!force_inter) |
1751 |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); |
1752 |
else |
else |
1753 |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
iLimit = mb_width * mb_height + 1; |
1754 |
|
|
1755 |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
1756 |
start_timer(); |
start_timer(); |
1757 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1758 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1759 |
|
pEnc->mbParam.edged_height, |
1760 |
|
pEnc->mbParam.m_quarterpel, |
1761 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1762 |
stop_inter_timer(); |
stop_inter_timer(); |
1763 |
} |
} |
1764 |
|
|
1765 |
|
pEnc->current->coding_type = P_VOP; |
1766 |
|
|
1767 |
start_timer(); |
start_timer(); |
1768 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
|
{ |
|
1769 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
|
} |
|
1770 |
else |
else |
1771 |
{ |
bIntra = |
1772 |
bIntra = MotionEstimation( |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1773 |
&pEnc->mbParam, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
pEnc->current, |
|
|
pEnc->reference, |
|
|
&pEnc->vInterH, |
|
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
1774 |
iLimit); |
iLimit); |
1775 |
} |
|
1776 |
stop_motion_timer(); |
stop_motion_timer(); |
1777 |
|
|
1778 |
if (bIntra == 1) |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
1779 |
|
|
1780 |
|
if ( ( pEnc->current->global_flags & XVID_GMC ) |
1781 |
|
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
1782 |
{ |
{ |
1783 |
return FrameCodeI(pEnc, bs, pBits); |
pEnc->current->coding_type = S_VOP; |
|
} |
|
1784 |
|
|
1785 |
pEnc->current->coding_type = P_VOP; |
generate_GMCparameters( 2, 16, &pEnc->current->warp, |
1786 |
|
pEnc->mbParam.width, pEnc->mbParam.height, |
1787 |
|
&pEnc->current->gmc_data); |
1788 |
|
|
1789 |
|
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
1790 |
|
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
1791 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
1792 |
|
pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0, |
1793 |
|
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
1794 |
|
|
1795 |
|
} |
1796 |
|
|
1797 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1798 |
if(vol_header) |
if(vol_header) |
1799 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1800 |
|
BitstreamPadAlways(bs); |
1801 |
|
} |
1802 |
|
|
1803 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1804 |
|
|
1805 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1806 |
|
|
1807 |
pEnc->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
1808 |
pEnc->sStat.iMvSum = 0; |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
|
pEnc->sStat.iMvCount = 0; |
|
|
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
|
1809 |
|
|
1810 |
for(y = 0; y < pEnc->mbParam.mb_height; y++) |
|
1811 |
{ |
for (y = 0; y < mb_height; y++) { |
1812 |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < mb_width; x++) { |
1813 |
{ |
MACROBLOCK *pMB = |
1814 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1815 |
|
|
1816 |
|
/* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */ |
1817 |
|
/* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */ |
1818 |
|
|
1819 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1820 |
|
|
1821 |
if (!bIntra) |
if (bIntra) { |
1822 |
{ |
CodeIntraMB(pEnc, pMB); |
1823 |
|
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1824 |
|
dct_codes, qcoeff); |
1825 |
|
|
1826 |
|
start_timer(); |
1827 |
|
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1828 |
|
stop_prediction_timer(); |
1829 |
|
|
1830 |
|
pEnc->current->sStat.kblks++; |
1831 |
|
|
1832 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1833 |
|
stop_coding_timer(); |
1834 |
|
continue; |
1835 |
|
} |
1836 |
|
|
1837 |
|
if (pEnc->current->coding_type == S_VOP) { |
1838 |
|
|
1839 |
|
int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1840 |
|
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1841 |
|
pEnc->mbParam.edged_width, 65536); |
1842 |
|
|
1843 |
|
if (pEnc->current->motion_flags & PMV_CHROMA16) { |
1844 |
|
iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
1845 |
|
pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
1846 |
|
|
1847 |
|
iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
1848 |
|
pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
1849 |
|
} |
1850 |
|
|
1851 |
|
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
1852 |
|
|
1853 |
|
if (pEnc->mbParam.m_quarterpel) |
1854 |
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
1855 |
|
else |
1856 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
1857 |
|
|
1858 |
|
pMB->mode = MODE_INTER; |
1859 |
|
pMB->mcsel = 1; |
1860 |
|
pMB->sad16 = iSAD; |
1861 |
|
} else { |
1862 |
|
pMB->mcsel = 0; |
1863 |
|
} |
1864 |
|
} else { |
1865 |
|
pMB->mcsel = 0; /* just a precaution */ |
1866 |
|
} |
1867 |
|
|
1868 |
start_timer(); |
start_timer(); |
1869 |
MBMotionCompensation(pMB, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1870 |
x, y, |
&pEnc->vInterH, &pEnc->vInterV, |
1871 |
&pEnc->reference->image, |
&pEnc->vInterHV, &pEnc->vGMC, |
|
&pEnc->vInterH, |
|
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
1872 |
&pEnc->current->image, |
&pEnc->current->image, |
1873 |
dct_codes, |
dct_codes, pEnc->mbParam.width, |
|
pEnc->mbParam.width, |
|
1874 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1875 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1876 |
|
pEnc->mbParam.m_quarterpel, |
1877 |
|
(pEnc->current->global_flags & XVID_REDUCED), |
1878 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1879 |
|
|
1880 |
stop_comp_timer(); |
stop_comp_timer(); |
1881 |
|
|
1882 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1883 |
if(pMB->dquant != NO_CHANGE) { |
if(pMB->dquant != NO_CHANGE) { |
1884 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
1885 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1886 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1887 |
else if(pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1888 |
|
else if (pEnc->current->quant < 1) |
1889 |
|
pEnc->current->quant = 1; |
1890 |
} |
} |
1891 |
} |
} |
1892 |
pMB->quant = pEnc->current->quant; |
pMB->quant = pEnc->current->quant; |
1893 |
|
|
1894 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1895 |
|
|
1896 |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
if (pMB->mode != MODE_NOT_CODED) |
1897 |
|
{ pMB->cbp = |
1898 |
|
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1899 |
|
dct_codes, qcoeff); |
1900 |
} |
} |
1901 |
else |
|
1902 |
{ |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1903 |
CodeIntraMB(pEnc, pMB); |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1904 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1905 |
|
pEnc->current->sStat.mblks++; |
1906 |
|
} else { |
1907 |
|
pEnc->current->sStat.ublks++; |
1908 |
} |
} |
1909 |
|
|
1910 |
start_timer(); |
start_timer(); |
|
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
|
|
stop_prediction_timer(); |
|
1911 |
|
|
1912 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1913 |
{ |
|
1914 |
pEnc->sStat.kblks++; |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1915 |
|
(pMB->dquant == NO_CHANGE); |
1916 |
|
|
1917 |
|
if (pEnc->current->coding_type == S_VOP) |
1918 |
|
skip_possible &= (pMB->mcsel == 1); |
1919 |
|
else if (pEnc->current->coding_type == P_VOP) { |
1920 |
|
if (pEnc->mbParam.m_quarterpel) |
1921 |
|
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
1922 |
|
else |
1923 |
|
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
1924 |
} |
} |
1925 |
else if (pMB->cbp || |
|
1926 |
pMB->mvs[0].x || pMB->mvs[0].y || |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1927 |
pMB->mvs[1].x || pMB->mvs[1].y || |
|
1928 |
pMB->mvs[2].x || pMB->mvs[2].y || |
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
1929 |
pMB->mvs[3].x || pMB->mvs[3].y) |
|
1930 |
|
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
1931 |
|
{ |
1932 |
|
int bSkip = 1; |
1933 |
|
|
1934 |
|
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1935 |
{ |
{ |
1936 |
pEnc->sStat.mblks++; |
int iSAD; |
1937 |
|
iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1938 |
|
pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1939 |
|
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
1940 |
|
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
1941 |
|
{ bSkip = 0; |
1942 |
|
break; |
1943 |
} |
} |
1944 |
else |
} |
1945 |
|
|
1946 |
|
if (!bSkip) { /* no SKIP, but trivial block */ |
1947 |
|
if(pEnc->mbParam.m_quarterpel) { |
1948 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1949 |
|
pMB->pmvs[0].x = - predMV.x; |
1950 |
|
pMB->pmvs[0].y = - predMV.y; |
1951 |
|
} |
1952 |
|
else { |
1953 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1954 |
|
pMB->pmvs[0].x = - predMV.x; |
1955 |
|
pMB->pmvs[0].y = - predMV.y; |
1956 |
|
} |
1957 |
|
pMB->mode = MODE_INTER; |
1958 |
|
pMB->cbp = 0; |
1959 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1960 |
|
stop_coding_timer(); |
1961 |
|
|
1962 |
|
continue; /* next MB */ |
1963 |
|
} |
1964 |
|
} |
1965 |
|
/* do SKIP */ |
1966 |
|
|
1967 |
|
pMB->mode = MODE_NOT_CODED; |
1968 |
|
MBSkip(bs); |
1969 |
|
stop_coding_timer(); |
1970 |
|
continue; /* next MB */ |
1971 |
|
} |
1972 |
|
/* ordinary case: normal coded INTER/INTER4V block */ |
1973 |
|
|
1974 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1975 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1976 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1977 |
|
qcoeff[5*64+0]=0; |
1978 |
|
} |
1979 |
|
|
1980 |
|
if(pEnc->mbParam.m_quarterpel) { |
1981 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1982 |
|
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1983 |
|
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1984 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
1985 |
|
} else { |
1986 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1987 |
|
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
1988 |
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1989 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
1990 |
|
} |
1991 |
|
|
1992 |
|
|
1993 |
|
if (pMB->mode == MODE_INTER4V) |
1994 |
|
{ int k; |
1995 |
|
for (k=1;k<4;k++) |
1996 |
{ |
{ |
1997 |
pEnc->sStat.ublks++; |
if(pEnc->mbParam.m_quarterpel) { |
1998 |
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1999 |
|
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
2000 |
|
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
2001 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
2002 |
|
} else { |
2003 |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
2004 |
|
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
2005 |
|
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
2006 |
|
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
2007 |
} |
} |
2008 |
|
|
2009 |
start_timer(); |
} |
2010 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
} |
2011 |
|
|
2012 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
2013 |
stop_coding_timer(); |
stop_coding_timer(); |
2014 |
|
|
2015 |
} |
} |
2016 |
} |
} |
2017 |
|
|
2018 |
|
if ((pEnc->current->global_flags & XVID_REDUCED)) |
2019 |
|
{ |
2020 |
|
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
2021 |
|
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
2022 |
|
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
2023 |
|
} |
2024 |
|
|
2025 |
emms(); |
emms(); |
2026 |
|
|
2027 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
2028 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
2029 |
} |
} |
2030 |
|
|
2031 |
if (pEnc->sStat.iMvCount == 0) |
if (pEnc->current->sStat.iMvCount == 0) |
2032 |
pEnc->sStat.iMvCount = 1; |
pEnc->current->sStat.iMvCount = 1; |
2033 |
|
|
2034 |
fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount); |
2035 |
|
|
2036 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
2037 |
|
|
2038 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
2039 |
&& (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 |
&& (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128 |
2040 |
{ |
{ |
2041 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
2042 |
iSearchRange *= 2; |
iSearchRange *= 2; |
2043 |
} |
} else if ((fSigma < iSearchRange / 6) |
2044 |
else if ((fSigma < iSearchRange / 6) |
&& (pEnc->fMvPrevSigma >= 0) |
2045 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
2046 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16 |
|
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
|
2047 |
{ |
{ |
2048 |
pEnc->mbParam.m_fcode--; |
pEnc->mbParam.m_fcode--; |
2049 |
iSearchRange /= 2; |
iSearchRange /= 2; |
2050 |
} |
} |
2051 |
|
|
2052 |
pEnc->sStat.fMvPrevSigma = fSigma; |
pEnc->fMvPrevSigma = fSigma; |
2053 |
|
|
2054 |
|
/* frame drop code */ |
2055 |
|
DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
2056 |
|
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
2057 |
|
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
2058 |
|
{ |
2059 |
|
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0; |
2060 |
|
pEnc->current->sStat.ublks = mb_width * mb_height; |
2061 |
|
|
2062 |
|
BitstreamReset(bs); |
2063 |
|
|
2064 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
2065 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
2066 |
|
|
2067 |
|
// copy reference frame details into the current frame |
2068 |
|
pEnc->current->quant = pEnc->reference->quant; |
2069 |
|
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
2070 |
|
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
2071 |
|
pEnc->current->quarterpel = pEnc->reference->quarterpel; |
2072 |
|
pEnc->current->fcode = pEnc->reference->fcode; |
2073 |
|
pEnc->current->bcode = pEnc->reference->bcode; |
2074 |
|
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
2075 |
|
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
2076 |
|
} |
2077 |
|
|
2078 |
|
/* XXX: debug |
2079 |
|
{ |
2080 |
|
char s[100]; |
2081 |
|
sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum); |
2082 |
|
image_dump_yuvpgm(&pEnc->current->image, |
2083 |
|
pEnc->mbParam.edged_width, |
2084 |
|
pEnc->mbParam.width, pEnc->mbParam.height, s); |
2085 |
|
|
2086 |
|
sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum); |
2087 |
|
image_dump_yuvpgm(&pEnc->reference->image, |
2088 |
|
pEnc->mbParam.edged_width, |
2089 |
|
pEnc->mbParam.width, pEnc->mbParam.height, s); |
2090 |
|
} |
2091 |
|
*/ |
2092 |
|
|
2093 |
|
|
2094 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
2095 |
|
|
2097 |
} |
} |
2098 |
|
|
2099 |
|
|
2100 |
#if 0 |
static void |
2101 |
|
FrameCodeB(Encoder * pEnc, |
2102 |
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) |
FRAMEINFO * frame, |
2103 |
|
Bitstream * bs, |
2104 |
|
uint32_t * pBits) |
2105 |
{ |
{ |
2106 |
int16_t dct_codes[6][64]; |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
2107 |
int16_t qcoeff[6][64]; |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
2108 |
uint32_t x, y; |
uint32_t x, y; |
|
VECTOR forward; |
|
|
VECTOR backward; |
|
2109 |
|
|
2110 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
2111 |
IMAGE *b_ref = &pEnc->current->image; |
IMAGE *b_ref = &pEnc->current->image; |
2112 |
|
|
2113 |
/* forward */ |
#ifdef BFRAMES_DEC_DEBUG |
2114 |
image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
FILE *fp; |
2115 |
|
static char first=0; |
2116 |
|
#define BFRAME_DEBUG if (!first && fp){ \ |
2117 |
|
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
2118 |
|
} |
2119 |
|
|
2120 |
|
pEnc->current->global_flags &= ~XVID_REDUCED; /* reduced resoltion not yet supported */ |
2121 |
|
|
2122 |
|
if (!first){ |
2123 |
|
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
2124 |
|
} |
2125 |
|
#endif |
2126 |
|
|
2127 |
|
frame->quarterpel = pEnc->mbParam.m_quarterpel; |
2128 |
|
|
2129 |
|
// forward |
2130 |
|
image_setedges(f_ref, pEnc->mbParam.edged_width, |
2131 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
2132 |
|
pEnc->mbParam.height); |
2133 |
start_timer(); |
start_timer(); |
2134 |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
2135 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
2136 |
|
pEnc->mbParam.m_quarterpel, 0); |
2137 |
stop_inter_timer(); |
stop_inter_timer(); |
2138 |
|
|
2139 |
/* backward */ |
// backward |
2140 |
image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
2141 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
2142 |
|
pEnc->mbParam.height); |
2143 |
start_timer(); |
start_timer(); |
2144 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
2145 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
2146 |
|
pEnc->mbParam.m_quarterpel, 0); |
2147 |
stop_inter_timer(); |
stop_inter_timer(); |
2148 |
|
|
2149 |
start_timer(); |
start_timer(); |
2150 |
|
|
2151 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
2152 |
pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
((int32_t)(pEnc->current->stamp - frame->stamp)), // time_bp |
2153 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), // time_pp |
2154 |
|
pEnc->reference->mbs, f_ref, |
2155 |
|
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
2156 |
|
pEnc->current, b_ref, &pEnc->vInterH, |
2157 |
|
&pEnc->vInterV, &pEnc->vInterHV); |
2158 |
|
|
2159 |
|
|
2160 |
stop_motion_timer(); |
stop_motion_timer(); |
2161 |
|
|
2162 |
if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
/*if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
2163 |
{ |
{ |
2164 |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
2165 |
} |
} */ |
2166 |
|
|
2167 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
|
BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0, |
|
|
frame->quant, frame->fcode, frame->bcode); |
|
|
|
|
|
*pBits = BitstreamPos(bs); |
|
2168 |
|
|
2169 |
pEnc->sStat.iTextBits = 0; |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
2170 |
pEnc->sStat.iMvSum = 0; |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
|
pEnc->sStat.iMvCount = 0; |
|
|
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
|
2171 |
|
|
2172 |
|
*pBits = BitstreamPos(bs); |
2173 |
|
|
2174 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
frame->sStat.iTextBits = 0; |
2175 |
{ |
frame->sStat.iMvSum = 0; |
2176 |
// reset prediction |
frame->sStat.iMvCount = 0; |
2177 |
|
frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0; |
2178 |
|
|
|
forward.x = 0; |
|
|
forward.y = 0; |
|
|
backward.x = 0; |
|
|
backward.y = 0; |
|
2179 |
|
|
2180 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
2181 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
2182 |
MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
2183 |
MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0; |
|
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
|
2184 |
|
|
2185 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
2186 |
if (mb->mode == MODE_NOT_CODED) |
if (mb->mode == MODE_NOT_CODED) { |
2187 |
{ |
//mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; |
|
mb->mvs[0].x = 0; |
|
|
mb->mvs[0].y = 0; |
|
2188 |
continue; |
continue; |
2189 |
} |
} |
2190 |
|
|
2191 |
|
if (mb->mode != MODE_DIRECT_NONE_MV) { |
2192 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
2193 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
2194 |
b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
2195 |
|
&pEnc->vInterV, &pEnc->vInterHV, |
2196 |
dct_codes); |
dct_codes); |
2197 |
|
|
2198 |
|
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
2199 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
|
mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff); |
|
|
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
|
2200 |
|
|
2201 |
|
mb->cbp = |
2202 |
|
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
2203 |
|
|
2204 |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
2205 |
mb->cbp == 0 && |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
2206 |
mb->mvs[0].x == 0 && |
mb->mode = MODE_DIRECT_NONE_MV; // skipped |
|
mb->mvs[0].y == 0) |
|
|
{ |
|
|
mb->mode = 5; // skipped |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
|
|
{ |
|
|
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
|
|
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
|
|
forward.x = mb->mvs[0].x; |
|
|
forward.y = mb->mvs[0].y; |
|
2207 |
} |
} |
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
|
|
{ |
|
|
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
|
|
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
|
|
backward.x = mb->b_mvs[0].x; |
|
|
backward.y = mb->b_mvs[0].y; |
|
2208 |
} |
} |
2209 |
|
|
2210 |
/* |
#ifdef BFRAMES_DEC_DEBUG |
2211 |
printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i %i,%i\n", |
BFRAME_DEBUG |
2212 |
x, |
#endif |
|
y, |
|
|
pMB->mode, |
|
|
pMB->cbp, |
|
|
pMB->mvs[0].x, |
|
|
bmb->pmvs[0].x, |
|
|
bmb->pmvs[0].y, |
|
|
forward.x, |
|
|
forward.y); |
|
|
*/ |
|
|
|
|
2213 |
start_timer(); |
start_timer(); |
2214 |
MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat); |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
2215 |
|
&frame->sStat, direction); |
2216 |
stop_coding_timer(); |
stop_coding_timer(); |
2217 |
} |
} |
2218 |
} |
} |
2223 |
|
|
2224 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
2225 |
|
|
2226 |
|
#ifdef BFRAMES_DEC_DEBUG |
2227 |
|
if (!first){ |
2228 |
|
first=1; |
2229 |
|
if (fp) |
2230 |
|
fclose(fp); |
2231 |
} |
} |
|
|
|
2232 |
#endif |
#endif |
2233 |
|
} |