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 |
* |
* |
36 |
* MinChen <chenm001@163.com> |
* MinChen <chenm001@163.com> |
37 |
* 14.04.2002 added FrameCodeB() |
* 14.04.2002 added FrameCodeB() |
38 |
* |
* |
39 |
* $Id: encoder.c,v 1.37 2002-06-07 10:21:48 edgomez Exp $ |
* $Id: encoder.c,v 1.41 2002-06-12 20:38:40 edgomez Exp $ |
40 |
* |
* |
41 |
***************************************************************************/ |
****************************************************************************/ |
42 |
|
|
43 |
#include <stdlib.h> |
#include <stdlib.h> |
44 |
#include <stdio.h> |
#include <stdio.h> |
61 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
62 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
63 |
|
|
64 |
|
/***************************************************************************** |
65 |
|
* Local macros |
66 |
|
****************************************************************************/ |
67 |
|
|
68 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
69 |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
70 |
|
|
71 |
|
/***************************************************************************** |
72 |
|
* Local function prototypes |
73 |
|
****************************************************************************/ |
74 |
|
|
75 |
|
static int FrameCodeI(Encoder * pEnc, |
76 |
|
Bitstream * bs, |
77 |
|
uint32_t * pBits); |
78 |
|
|
79 |
|
static int FrameCodeP(Encoder * pEnc, |
80 |
|
Bitstream * bs, |
81 |
|
uint32_t * pBits, |
82 |
|
bool force_inter, |
83 |
|
bool vol_header); |
84 |
|
|
85 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); |
#ifdef BFRAMES |
86 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header); |
static void FrameCodeB(Encoder * pEnc, |
87 |
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits); |
FRAMEINFO * frame, |
88 |
|
Bitstream * bs, |
89 |
|
uint32_t * pBits); |
90 |
|
#endif |
91 |
|
|
92 |
static int DQtab[4] = |
/***************************************************************************** |
93 |
{ |
* Local data |
94 |
|
****************************************************************************/ |
95 |
|
|
96 |
|
static int DQtab[4] = { |
97 |
-1, -2, 1, 2 |
-1, -2, 1, 2 |
98 |
}; |
}; |
99 |
|
|
100 |
static int iDQtab[5] = |
static int iDQtab[5] = { |
|
{ |
|
101 |
1, 0, NO_CHANGE, 2, 3 |
1, 0, NO_CHANGE, 2, 3 |
102 |
}; |
}; |
103 |
|
|
104 |
|
|
105 |
void static image_null(IMAGE * image) |
static void __inline |
106 |
|
image_null(IMAGE * image) |
107 |
{ |
{ |
108 |
image->y = image->u = image->v = NULL; |
image->y = image->u = image->v = NULL; |
109 |
} |
} |
110 |
|
|
111 |
|
|
112 |
int encoder_create(XVID_ENC_PARAM * pParam) |
/***************************************************************************** |
113 |
|
* Encoder creation |
114 |
|
* |
115 |
|
* This function creates an Encoder instance, it allocates all necessary |
116 |
|
* image buffers (reference, current and bframes) and initialize the internal |
117 |
|
* xvid encoder paremeters according to the XVID_ENC_PARAM input parameter. |
118 |
|
* |
119 |
|
* The code seems to be very long but is very basic, mainly memory allocation |
120 |
|
* and cleaning code. |
121 |
|
* |
122 |
|
* Returned values : |
123 |
|
* - XVID_ERR_OK - no errors |
124 |
|
* - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
125 |
|
* cleans the structure before exiting. |
126 |
|
* pParam->handle is also set to NULL. |
127 |
|
* |
128 |
|
****************************************************************************/ |
129 |
|
|
130 |
|
int |
131 |
|
encoder_create(XVID_ENC_PARAM * pParam) |
132 |
{ |
{ |
133 |
Encoder *pEnc; |
Encoder *pEnc; |
134 |
uint32_t i; |
uint32_t i; |
142 |
ENC_CHECK(!(pParam->width % 2)); |
ENC_CHECK(!(pParam->width % 2)); |
143 |
ENC_CHECK(!(pParam->height % 2)); |
ENC_CHECK(!(pParam->height % 2)); |
144 |
|
|
145 |
if (pParam->fincr <= 0 || pParam->fbase <= 0) |
/* Fps */ |
146 |
{ |
|
147 |
|
if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
148 |
pParam->fincr = 1; |
pParam->fincr = 1; |
149 |
pParam->fbase = 25; |
pParam->fbase = 25; |
150 |
} |
} |
151 |
|
|
152 |
// simplify the "fincr/fbase" fraction |
/* |
153 |
// (neccessary, since windows supplies us with huge numbers) |
* Simplify the "fincr/fbase" fraction |
154 |
|
* (neccessary, since windows supplies us with huge numbers) |
155 |
|
*/ |
156 |
|
|
157 |
i = pParam->fincr; |
i = pParam->fincr; |
158 |
while (i > 1) |
while (i > 1) { |
159 |
{ |
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { |
|
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) |
|
|
{ |
|
160 |
pParam->fincr /= i; |
pParam->fincr /= i; |
161 |
pParam->fbase /= i; |
pParam->fbase /= i; |
162 |
i = pParam->fincr; |
i = pParam->fincr; |
165 |
i--; |
i--; |
166 |
} |
} |
167 |
|
|
168 |
if (pParam->fbase > 65535) |
if (pParam->fbase > 65535) { |
|
{ |
|
169 |
float div = (float)pParam->fbase / 65535; |
float div = (float)pParam->fbase / 65535; |
170 |
|
|
171 |
pParam->fbase = (int)(pParam->fbase / div); |
pParam->fbase = (int)(pParam->fbase / div); |
172 |
pParam->fincr = (int)(pParam->fincr / div); |
pParam->fincr = (int)(pParam->fincr / div); |
173 |
} |
} |
174 |
|
|
175 |
|
/* Bitrate allocator defaults */ |
176 |
|
|
177 |
if (pParam->rc_bitrate <= 0) |
if (pParam->rc_bitrate <= 0) |
178 |
pParam->rc_bitrate = 900000; |
pParam->rc_bitrate = 900000; |
179 |
|
|
186 |
if (pParam->rc_buffer <= 0) |
if (pParam->rc_buffer <= 0) |
187 |
pParam->rc_buffer = 100; |
pParam->rc_buffer = 100; |
188 |
|
|
189 |
|
/* Max and min quantizers */ |
190 |
|
|
191 |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
192 |
pParam->min_quantizer = 1; |
pParam->min_quantizer = 1; |
193 |
|
|
194 |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
195 |
pParam->max_quantizer = 31; |
pParam->max_quantizer = 31; |
196 |
|
|
|
if (pParam->max_key_interval == 0) /* 1 keyframe each 10 seconds */ |
|
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
|
|
|
|
197 |
if (pParam->max_quantizer < pParam->min_quantizer) |
if (pParam->max_quantizer < pParam->min_quantizer) |
198 |
pParam->max_quantizer = pParam->min_quantizer; |
pParam->max_quantizer = pParam->min_quantizer; |
199 |
|
|
200 |
if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL) |
/* 1 keyframe each 10 seconds */ |
201 |
|
|
202 |
|
if (pParam->max_key_interval == 0) |
203 |
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
204 |
|
|
205 |
|
|
206 |
|
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
207 |
|
if (pEnc == NULL) |
208 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
209 |
|
|
210 |
/* Fill members of Encoder structure */ |
/* Fill members of Encoder structure */ |
232 |
|
|
233 |
/* try to allocate frame memory */ |
/* try to allocate frame memory */ |
234 |
|
|
235 |
pEnc->current = NULL; |
pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
236 |
pEnc->reference = NULL; |
pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
237 |
if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL || |
|
238 |
(pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL) |
if (pEnc->current == NULL || pEnc->reference == NULL) |
239 |
{ |
goto xvid_err_memory1; |
|
if (pEnc->current) xvid_free(pEnc->current); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
240 |
|
|
241 |
/* try to allocate mb memory */ |
/* try to allocate mb memory */ |
242 |
|
|
243 |
pEnc->current->mbs = NULL; |
pEnc->current->mbs = |
244 |
pEnc->reference->mbs = NULL; |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
245 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
246 |
|
pEnc->reference->mbs = |
247 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
248 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
249 |
|
|
250 |
if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL || |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
251 |
(pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) |
goto xvid_err_memory2; |
|
{ |
|
|
if (pEnc->current->mbs) xvid_free(pEnc->current->mbs); |
|
|
xvid_free(pEnc->current); |
|
|
xvid_free(pEnc->reference); |
|
|
xvid_free(pEnc); |
|
|
} |
|
252 |
|
|
253 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
254 |
|
|
268 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
269 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
270 |
|
|
|
if ( |
|
|
#ifdef _DEBUG |
|
|
image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
#endif |
|
|
#ifdef BFRAMES |
|
|
image_create(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
|
image_create(&pEnc->f_refhv, 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) |
|
|
{ |
|
271 |
#ifdef _DEBUG |
#ifdef _DEBUG |
272 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
if (image_create |
273 |
|
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
274 |
|
pEnc->mbParam.edged_height) < 0) |
275 |
|
goto xvid_err_memory3; |
276 |
#endif |
#endif |
277 |
#ifdef BFRAMES |
#ifdef BFRAMES |
278 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
if (image_create |
279 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
280 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height) < 0) |
281 |
|
goto xvid_err_memory3; |
282 |
|
if (image_create |
283 |
|
(&pEnc->f_refv, pEnc->mbParam.edged_width, |
284 |
|
pEnc->mbParam.edged_height) < 0) |
285 |
|
goto xvid_err_memory3; |
286 |
|
if (image_create |
287 |
|
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
288 |
|
pEnc->mbParam.edged_height) < 0) |
289 |
|
goto xvid_err_memory3; |
290 |
#endif |
#endif |
291 |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
if (image_create |
292 |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
(&pEnc->current->image, pEnc->mbParam.edged_width, |
293 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height) < 0) |
294 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
goto xvid_err_memory3; |
295 |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
if (image_create |
296 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
(&pEnc->reference->image, pEnc->mbParam.edged_width, |
297 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height) < 0) |
298 |
|
goto xvid_err_memory3; |
299 |
|
if (image_create |
300 |
|
(&pEnc->vInterH, pEnc->mbParam.edged_width, |
301 |
|
pEnc->mbParam.edged_height) < 0) |
302 |
|
goto xvid_err_memory3; |
303 |
|
if (image_create |
304 |
|
(&pEnc->vInterV, pEnc->mbParam.edged_width, |
305 |
|
pEnc->mbParam.edged_height) < 0) |
306 |
|
goto xvid_err_memory3; |
307 |
|
if (image_create |
308 |
|
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
309 |
|
pEnc->mbParam.edged_height) < 0) |
310 |
|
goto xvid_err_memory3; |
311 |
|
if (image_create |
312 |
|
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
313 |
|
pEnc->mbParam.edged_height) < 0) |
314 |
|
goto xvid_err_memory3; |
315 |
|
if (image_create |
316 |
|
(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
317 |
|
pEnc->mbParam.edged_height) < 0) |
318 |
|
goto xvid_err_memory3; |
319 |
|
|
|
xvid_free(pEnc->current); |
|
|
xvid_free(pEnc->reference); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
320 |
|
|
321 |
// ============================================================================== |
|
322 |
|
/* B Frames specific init */ |
323 |
#ifdef BFRAMES |
#ifdef BFRAMES |
324 |
|
|
|
// TODO: handle malloc() == NULL |
|
325 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
326 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
327 |
if (pEnc->mbParam.max_bframes > 0) |
pEnc->bframes = NULL; |
328 |
{ |
|
329 |
|
if (pEnc->mbParam.max_bframes > 0) { |
330 |
int n; |
int n; |
331 |
|
|
332 |
pEnc->bframes = malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *)); |
pEnc->bframes = |
333 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), |
334 |
|
CACHE_LINE); |
335 |
|
|
336 |
|
if (pEnc->bframes == NULL) |
337 |
|
goto xvid_err_memory3; |
338 |
|
|
339 |
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
340 |
{ |
pEnc->bframes[n] = NULL; |
341 |
pEnc->bframes[n] = malloc(sizeof(FRAMEINFO)); |
|
342 |
pEnc->bframes[n]->mbs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
|
343 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
344 |
|
pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
345 |
|
|
346 |
|
if (pEnc->bframes[n] == NULL) |
347 |
|
goto xvid_err_memory4; |
348 |
|
|
349 |
|
pEnc->bframes[n]->mbs = |
350 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
351 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
352 |
|
|
353 |
|
if (pEnc->bframes[n]->mbs == NULL) |
354 |
|
goto xvid_err_memory4; |
355 |
|
|
356 |
|
image_null(&pEnc->bframes[n]->image); |
357 |
|
|
358 |
|
if (image_create |
359 |
|
(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, |
360 |
|
pEnc->mbParam.edged_height) < 0) |
361 |
|
goto xvid_err_memory4; |
362 |
|
|
|
if (image_create(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
|
|
{ |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
363 |
} |
} |
364 |
} |
} |
365 |
|
|
366 |
pEnc->bframenum_head = 0; |
pEnc->bframenum_head = 0; |
367 |
pEnc->bframenum_tail = 0; |
pEnc->bframenum_tail = 0; |
368 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
371 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
372 |
#endif |
#endif |
373 |
|
|
|
|
|
|
// ============================================================================== |
|
|
|
|
|
|
|
374 |
pParam->handle = (void *)pEnc; |
pParam->handle = (void *)pEnc; |
375 |
|
|
376 |
if (pParam->rc_bitrate) |
if (pParam->rc_bitrate) { |
377 |
{ |
RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, |
|
RateControlInit(&pEnc->rate_control, |
|
|
pParam->rc_bitrate, |
|
378 |
pParam->rc_reaction_delay_factor, |
pParam->rc_reaction_delay_factor, |
379 |
pParam->rc_averaging_period, |
pParam->rc_averaging_period, pParam->rc_buffer, |
|
pParam->rc_buffer, |
|
380 |
pParam->fbase * 1000 / pParam->fincr, |
pParam->fbase * 1000 / pParam->fincr, |
381 |
pParam->max_quantizer, |
pParam->max_quantizer, pParam->min_quantizer); |
|
pParam->min_quantizer); |
|
382 |
} |
} |
383 |
|
|
384 |
init_timer(); |
init_timer(); |
385 |
|
|
386 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
387 |
|
|
388 |
|
/* |
389 |
|
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
390 |
|
*/ |
391 |
|
#ifdef BFRAMES |
392 |
|
xvid_err_memory4: |
393 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
394 |
|
|
395 |
|
if (pEnc->bframes[i] == NULL) |
396 |
|
continue; |
397 |
|
|
398 |
|
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
399 |
|
pEnc->mbParam.edged_height); |
400 |
|
|
401 |
|
xvid_free(pEnc->bframes[i]->mbs); |
402 |
|
|
403 |
|
xvid_free(pEnc->bframes[i]); |
404 |
|
|
405 |
|
} |
406 |
|
|
407 |
|
xvid_free(pEnc->bframes); |
408 |
|
|
409 |
|
#endif |
410 |
|
|
411 |
|
xvid_err_memory3: |
412 |
|
#ifdef _DEBUG |
413 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
414 |
|
pEnc->mbParam.edged_height); |
415 |
|
#endif |
416 |
|
|
417 |
|
#ifdef BFRAMES |
418 |
|
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
419 |
|
pEnc->mbParam.edged_height); |
420 |
|
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
421 |
|
pEnc->mbParam.edged_height); |
422 |
|
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
423 |
|
pEnc->mbParam.edged_height); |
424 |
|
#endif |
425 |
|
|
426 |
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
427 |
|
pEnc->mbParam.edged_height); |
428 |
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
429 |
|
pEnc->mbParam.edged_height); |
430 |
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
431 |
|
pEnc->mbParam.edged_height); |
432 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
433 |
|
pEnc->mbParam.edged_height); |
434 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
435 |
|
pEnc->mbParam.edged_height); |
436 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
437 |
|
pEnc->mbParam.edged_height); |
438 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
439 |
|
pEnc->mbParam.edged_height); |
440 |
|
|
441 |
|
xvid_err_memory2: |
442 |
|
xvid_free(pEnc->current->mbs); |
443 |
|
xvid_free(pEnc->reference->mbs); |
444 |
|
|
445 |
|
xvid_err_memory1: |
446 |
|
xvid_free(pEnc->current); |
447 |
|
xvid_free(pEnc->reference); |
448 |
|
xvid_free(pEnc); |
449 |
|
|
450 |
|
pParam->handle = NULL; |
451 |
|
|
452 |
|
return XVID_ERR_MEMORY; |
453 |
} |
} |
454 |
|
|
455 |
|
/***************************************************************************** |
456 |
|
* Encoder destruction |
457 |
|
* |
458 |
|
* This function destroy the entire encoder structure created by a previous |
459 |
|
* successful encoder_create call. |
460 |
|
* |
461 |
|
* Returned values (for now only one returned value) : |
462 |
|
* - XVID_ERR_OK - no errors |
463 |
|
* |
464 |
|
****************************************************************************/ |
465 |
|
|
466 |
int encoder_destroy(Encoder * pEnc) |
int |
467 |
|
encoder_destroy(Encoder * pEnc) |
468 |
{ |
{ |
469 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
470 |
|
|
471 |
// ================================================================= |
/* B Frames specific */ |
472 |
#ifdef BFRAMES |
#ifdef BFRAMES |
473 |
if (pEnc->mbParam.max_bframes > 0) |
int i; |
474 |
{ |
|
475 |
int n; |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
476 |
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
|
477 |
{ |
if (pEnc->bframes[i] == NULL) |
478 |
image_destroy(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
continue; |
479 |
free(pEnc->bframes[n]->mbs); |
|
480 |
free(pEnc->bframes[n]); |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
481 |
} |
pEnc->mbParam.edged_height); |
482 |
free(pEnc->bframes); |
|
483 |
|
xvid_free(pEnc->bframes[i]->mbs); |
484 |
|
|
485 |
|
xvid_free(pEnc->bframes[i]); |
486 |
|
|
487 |
} |
} |
488 |
|
|
489 |
|
xvid_free(pEnc->bframes); |
490 |
|
|
491 |
#endif |
#endif |
|
//==================================================================== |
|
492 |
|
|
493 |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
/* All images, reference, current etc ... */ |
494 |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
495 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
496 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
497 |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
498 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
499 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
500 |
|
pEnc->mbParam.edged_height); |
501 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
502 |
|
pEnc->mbParam.edged_height); |
503 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
504 |
|
pEnc->mbParam.edged_height); |
505 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
506 |
|
pEnc->mbParam.edged_height); |
507 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
508 |
|
pEnc->mbParam.edged_height); |
509 |
#ifdef BFRAMES |
#ifdef BFRAMES |
510 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
511 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
512 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
513 |
|
pEnc->mbParam.edged_height); |
514 |
|
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
515 |
|
pEnc->mbParam.edged_height); |
516 |
#endif |
#endif |
517 |
#ifdef _DEBUG |
#ifdef _DEBUG |
518 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
519 |
|
pEnc->mbParam.edged_height); |
520 |
#endif |
#endif |
521 |
|
|
522 |
|
/* Encoder structure */ |
523 |
|
|
524 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
525 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
526 |
|
|
528 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
529 |
|
|
530 |
xvid_free(pEnc); |
xvid_free(pEnc); |
531 |
|
|
532 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
533 |
} |
} |
534 |
|
|
535 |
|
/***************************************************************************** |
536 |
|
* Frame encoder entry point |
537 |
|
* |
538 |
|
* At this moment 2 versions coexist : one for IPB compatible encoder, |
539 |
|
* another one for the old IP encoder. |
540 |
|
* |
541 |
|
* Returned values : |
542 |
|
* - XVID_ERR_OK - no errors |
543 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
544 |
|
* format |
545 |
|
****************************************************************************/ |
546 |
|
|
547 |
|
|
|
// ================================================================== |
|
548 |
#ifdef BFRAMES |
#ifdef BFRAMES |
549 |
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
/***************************************************************************** |
550 |
|
* Frame encoder entry point for IPB capable encoder |
551 |
|
****************************************************************************/ |
552 |
|
int |
553 |
|
encoder_encode(Encoder * pEnc, |
554 |
|
XVID_ENC_FRAME * pFrame, |
555 |
|
XVID_ENC_STATS * pResult) |
556 |
{ |
{ |
557 |
uint16_t x, y; |
uint16_t x, y; |
558 |
Bitstream bs; |
Bitstream bs; |
570 |
|
|
571 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
572 |
|
|
573 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* |
574 |
// bframe "flush" code |
* bframe "flush" code |
575 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
*/ |
576 |
|
|
577 |
|
if ((pFrame->image == NULL || pEnc->flush_bframes) |
578 |
|
&& (pEnc->bframenum_head < pEnc->bframenum_tail)) { |
579 |
|
|
580 |
|
if (pEnc->flush_bframes == 0) { |
581 |
|
/* |
582 |
|
* we have reached the end of stream without getting |
583 |
|
* a future reference frame... so encode last final |
584 |
|
* frame as a pframe |
585 |
|
*/ |
586 |
|
|
587 |
if ((pFrame->image == NULL || pEnc->flush_bframes) && |
/* ToDo : remove dprintf calls */ |
588 |
pEnc->bframenum_head < pEnc->bframenum_tail) |
/* |
|
{ |
|
|
|
|
|
if (pEnc->flush_bframes == 0) |
|
|
{ |
|
|
// we have reached end of stream without getting a future reference |
|
|
// .. so encode last final frame a pframe |
|
589 |
dprintf("--- PFRAME (final frame correction) --- "); |
dprintf("--- PFRAME (final frame correction) --- "); |
590 |
|
*/ |
591 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
592 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
593 |
|
|
599 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
600 |
pFrame->input_consumed = 0; |
pFrame->input_consumed = 0; |
601 |
pFrame->intra = 0; |
pFrame->intra = 0; |
602 |
|
|
603 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
604 |
} |
} |
605 |
|
|
606 |
|
/* ToDo : remove dprintf calls */ |
607 |
|
/* |
608 |
dprintf("--- BFRAME (flush) --- "); |
dprintf("--- BFRAME (flush) --- "); |
609 |
FrameCodeB(pEnc, |
*/ |
610 |
pEnc->bframes[pEnc->bframenum_head], |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
|
&bs, &bits); |
|
611 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
612 |
|
|
613 |
|
|
615 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
616 |
pFrame->input_consumed = 0; |
pFrame->input_consumed = 0; |
617 |
pFrame->intra = 0; |
pFrame->intra = 0; |
618 |
|
|
619 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
620 |
} |
} |
621 |
|
|
622 |
if (pFrame->image == NULL) |
if (pFrame->image == NULL) { |
|
{ |
|
623 |
pFrame->length = 0; |
pFrame->length = 0; |
624 |
pFrame->input_consumed = 1; |
pFrame->input_consumed = 1; |
625 |
pFrame->intra = 0; |
pFrame->intra = 0; |
626 |
|
|
627 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
628 |
} |
} |
629 |
|
|
630 |
if (pEnc->bframenum_head > 0) |
if (pEnc->bframenum_head > 0) { |
|
{ |
|
631 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
632 |
} |
} |
633 |
|
|
634 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
635 |
|
|
636 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
637 |
|
* Well there was a separation here so i put it in ANSI C |
638 |
|
* comment style :-) |
639 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
640 |
|
|
641 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
642 |
|
|
643 |
pEnc->current->quant = (pFrame->quant == 0) ? RateControlGetQ(0) : pFrame->quant; |
EMMS(); |
644 |
|
|
645 |
|
if (pFrame->quant == 0) |
646 |
|
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
647 |
|
else |
648 |
|
pEnc->current->quant = pFrame->quant; |
649 |
|
|
650 |
if(pEnc->current->quant < 1) |
if(pEnc->current->quant < 1) |
651 |
pEnc->current->quant = 1; |
pEnc->current->quant = 1; |
657 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
658 |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
659 |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
660 |
//@@@ TODO: dyanmic fcode (in both directions) |
/* ToDo : dynamic fcode (in both directions) */ |
661 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
662 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
663 |
|
|
664 |
start_timer(); |
start_timer(); |
665 |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input |
666 |
pFrame->image, pFrame->colorspace)) |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
667 |
{ |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
668 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
|
} |
|
669 |
stop_conv_timer(); |
stop_conv_timer(); |
670 |
|
|
671 |
#ifdef _DEBUG |
#ifdef _DEBUG |
672 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
673 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
674 |
#endif |
#endif |
675 |
|
|
676 |
|
EMMS(); |
677 |
|
|
678 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
679 |
// lumi masking |
* Luminance masking |
680 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
|
|
|
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
|
|
{ |
|
|
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
|
681 |
|
|
682 |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
683 |
pEnc->mbParam.edged_width, // stride |
int *temp_dquants = |
684 |
temp_dquants, |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
685 |
pEnc->current->quant, |
pEnc->mbParam.mb_height * sizeof(int), |
686 |
pEnc->current->quant, // min_quant |
CACHE_LINE); |
687 |
2*pEnc->current->quant, // max_quant |
|
688 |
|
pEnc->current->quant = |
689 |
|
adaptive_quantization(pEnc->current->image.y, |
690 |
|
pEnc->mbParam.edged_width, temp_dquants, |
691 |
|
pEnc->current->quant, pEnc->current->quant, |
692 |
|
2 * pEnc->current->quant, |
693 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
694 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
695 |
|
|
696 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
697 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
698 |
{ |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
699 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
700 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
701 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
702 |
|
|
703 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
704 |
} |
} |
705 |
|
|
706 |
|
#undef OFFSET |
707 |
|
|
708 |
|
} |
709 |
|
|
710 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
711 |
} |
} |
712 |
|
|
713 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
714 |
|
* ivop/pvop/bvop selection |
715 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
716 |
|
|
717 |
|
|
718 |
|
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || |
719 |
|
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
720 |
|
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
721 |
|
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
722 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
723 |
|
pEnc->mbParam.height) > 30) { |
724 |
|
/* |
725 |
|
* This will be coded as an Intra Frame |
726 |
|
*/ |
727 |
|
|
728 |
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* ToDo : Remove dprintf calls */ |
729 |
// ivop/pvop/bvop selection |
/* |
|
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
|
|
|
if (pEnc->iFrameNum == 0 || |
|
|
pFrame->intra == 1 || |
|
|
(pFrame->intra < 0 && (pEnc->iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->iMaxKeyInterval)) || |
|
|
image_mad(&pEnc->reference->image, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.width, pEnc->mbParam.height) > 30) |
|
|
{ |
|
730 |
dprintf("--- IFRAME ---"); |
dprintf("--- IFRAME ---"); |
731 |
|
*/ |
732 |
|
|
733 |
FrameCodeI(pEnc, &bs, &bits); |
FrameCodeI(pEnc, &bs, &bits); |
734 |
|
|
735 |
pFrame->intra = 1; |
pFrame->intra = 1; |
736 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
737 |
|
|
738 |
/* note: sequences like "IIBB" decode fine with msfdam but, |
/* |
739 |
go screwy with divx5.00 */ |
* NB : sequences like "IIBB" decode fine with msfdam but, |
740 |
} |
* go screwy with divx 5.00 |
741 |
else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) |
*/ |
742 |
{ |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
743 |
|
/* |
744 |
|
* This will be coded as a Predicted Frame |
745 |
|
*/ |
746 |
|
|
747 |
|
/* ToDo : Remove dprintf calls */ |
748 |
|
/* |
749 |
dprintf("--- PFRAME ---"); |
dprintf("--- PFRAME ---"); |
750 |
|
*/ |
751 |
|
|
752 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
753 |
pFrame->intra = 0; |
pFrame->intra = 0; |
754 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
755 |
} |
} else { |
756 |
else |
/* |
757 |
{ |
* This will be coded as a Bidirectional Frame |
758 |
dprintf("--- BFRAME (store) --- head=%i tail=%i", pEnc->bframenum_head, pEnc->bframenum_tail); |
*/ |
759 |
|
|
760 |
if (pFrame->bquant < 1) |
/* ToDo : Remove dprintf calls */ |
761 |
{ |
/* |
762 |
pEnc->current->quant = ((pEnc->reference->quant + pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
dprintf("--- BFRAME (store) --- head=%i tail=%i", |
763 |
} |
pEnc->bframenum_head, |
764 |
else |
pEnc->bframenum_tail); |
765 |
{ |
*/ |
766 |
|
|
767 |
|
if (pFrame->bquant < 1) { |
768 |
|
pEnc->current->quant = |
769 |
|
((pEnc->reference->quant + |
770 |
|
pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
771 |
|
} else { |
772 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = pFrame->bquant; |
773 |
} |
} |
774 |
|
|
775 |
// store frame into bframe buffer & swap ref back to current |
/* store frame into bframe buffer & swap ref back to current */ |
776 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
777 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
778 |
|
|
783 |
pFrame->input_consumed = 1; |
pFrame->input_consumed = 1; |
784 |
|
|
785 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
786 |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
|
{ |
|
787 |
pEnc->mbParam.m_seconds++; |
pEnc->mbParam.m_seconds++; |
788 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
789 |
} |
} |
790 |
|
|
791 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
792 |
} |
} |
793 |
|
|
794 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
795 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
796 |
|
|
797 |
if (pResult) |
if (pResult) { |
|
{ |
|
798 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
799 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
800 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
802 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
803 |
} |
} |
804 |
|
|
805 |
|
EMMS(); |
806 |
|
|
807 |
#ifdef _DEBUG |
#ifdef _DEBUG |
808 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
psnr = |
809 |
pEnc->mbParam.width, pEnc->mbParam.height); |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
810 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
811 |
|
pEnc->mbParam.height); |
812 |
|
|
813 |
sprintf(temp, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
814 |
DEBUG(temp); |
DEBUG(temp); |
815 |
#endif |
#endif |
816 |
|
|
817 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
818 |
{ |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
819 |
RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
820 |
} |
} |
821 |
|
|
822 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
823 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
824 |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
|
{ |
|
825 |
pEnc->mbParam.m_seconds++; |
pEnc->mbParam.m_seconds++; |
826 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
827 |
} |
} |
832 |
|
|
833 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
834 |
} |
} |
|
// ================================================================== |
|
835 |
#else |
#else |
836 |
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
/***************************************************************************** |
837 |
|
* Frame encoder entry point for IP capable encoder |
838 |
|
****************************************************************************/ |
839 |
|
int |
840 |
|
encoder_encode(Encoder * pEnc, |
841 |
|
XVID_ENC_FRAME * pFrame, |
842 |
|
XVID_ENC_STATS * pResult) |
843 |
{ |
{ |
844 |
uint16_t x, y; |
uint16_t x, y; |
845 |
Bitstream bs; |
Bitstream bs; |
846 |
uint32_t bits; |
uint32_t bits; |
847 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
848 |
|
|
849 |
#ifdef _DEBUG |
#ifdef _DEBUG |
850 |
float psnr; |
float psnr; |
851 |
uint8_t temp[100]; |
uint8_t temp[128]; |
852 |
#endif |
#endif |
853 |
|
|
854 |
start_global_timer(); |
start_global_timer(); |
865 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
866 |
|
|
867 |
start_timer(); |
start_timer(); |
868 |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input |
869 |
pFrame->image, pFrame->colorspace)) |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
870 |
{ |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) |
871 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
|
} |
|
872 |
stop_conv_timer(); |
stop_conv_timer(); |
873 |
|
|
874 |
#ifdef _DEBUG |
#ifdef _DEBUG |
875 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
876 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
877 |
#endif |
#endif |
878 |
|
|
879 |
EMMS(); |
EMMS(); |
880 |
|
|
881 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
882 |
|
|
883 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
|
{ |
|
884 |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0); |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0); |
885 |
} |
} else { |
|
else |
|
|
{ |
|
886 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
887 |
} |
} |
888 |
|
|
889 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
890 |
{ |
int *temp_dquants = |
891 |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
892 |
|
pEnc->mbParam.mb_height * sizeof(int), |
893 |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
CACHE_LINE); |
894 |
pEnc->mbParam.edged_width, // stride |
|
895 |
temp_dquants, |
pEnc->current->quant = |
896 |
pEnc->current->quant, |
adaptive_quantization(pEnc->current->image.y, |
897 |
pEnc->current->quant, // min_quant |
pEnc->mbParam.edged_width, temp_dquants, |
898 |
2*pEnc->current->quant, // max_quant |
pEnc->current->quant, pEnc->current->quant, |
899 |
|
2 * pEnc->current->quant, |
900 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
901 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
902 |
|
|
903 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
904 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
905 |
{ |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
906 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
907 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
908 |
|
|
909 |
|
|
910 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
911 |
|
|
912 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
913 |
} |
} |
914 |
|
|
915 |
|
#undef OFFSET |
916 |
|
} |
917 |
|
|
918 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
919 |
} |
} |
920 |
|
|
922 |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
923 |
write_vol_header = 1; |
write_vol_header = 1; |
924 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
925 |
} |
} else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
926 |
else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
int matrix1_changed, matrix2_changed; |
|
int ret1, ret2; |
|
927 |
|
|
928 |
ret1 = ret2 = 0; |
matrix1_changed = matrix2_changed = 0; |
929 |
|
|
930 |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
931 |
write_vol_header = 1; |
write_vol_header = 1; |
934 |
|
|
935 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
936 |
if(pFrame->quant_intra_matrix != NULL) |
if(pFrame->quant_intra_matrix != NULL) |
937 |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); |
938 |
if(pFrame->quant_inter_matrix != NULL) |
if(pFrame->quant_inter_matrix != NULL) |
939 |
ret2 = set_inter_matrix(pFrame->quant_inter_matrix); |
matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); |
940 |
} |
} else { |
941 |
else { |
matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
942 |
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()); |
|
943 |
} |
} |
944 |
if(write_vol_header == 0) |
if(write_vol_header == 0) |
945 |
write_vol_header = ret1 | ret2; |
write_vol_header = matrix1_changed | matrix2_changed; |
946 |
} |
} |
947 |
|
|
948 |
if (pFrame->intra < 0) |
if (pFrame->intra < 0) { |
949 |
{ |
if ((pEnc->iFrameNum == 0) |
950 |
if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
|| ((pEnc->iMaxKeyInterval > 0) |
951 |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { |
|
|
|
952 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
953 |
else |
} else { |
954 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
955 |
} |
} |
956 |
else |
} else { |
957 |
{ |
if (pFrame->intra == 1) { |
|
if (pFrame->intra == 1) |
|
958 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
959 |
else |
} else { |
960 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
961 |
} |
} |
962 |
|
|
963 |
|
} |
964 |
|
|
965 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
966 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
967 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
968 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
969 |
|
|
970 |
if (pResult) |
if (pResult) { |
|
{ |
|
971 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
972 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
973 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
977 |
|
|
978 |
EMMS(); |
EMMS(); |
979 |
|
|
980 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
981 |
{ |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
982 |
RateControlUpdate(&pEnc->rate_control, |
pFrame->length, pFrame->intra); |
|
pEnc->current->quant, |
|
|
pFrame->length, |
|
|
pFrame->intra); |
|
983 |
} |
} |
|
|
|
984 |
#ifdef _DEBUG |
#ifdef _DEBUG |
985 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
psnr = |
986 |
pEnc->mbParam.width, pEnc->mbParam.height); |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
987 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
988 |
|
pEnc->mbParam.height); |
989 |
|
|
990 |
sprintf(temp, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
991 |
DEBUG(temp); |
DEBUG(temp); |
992 |
#endif |
#endif |
993 |
|
|
1001 |
#endif |
#endif |
1002 |
|
|
1003 |
|
|
1004 |
static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { |
static __inline void |
1005 |
|
CodeIntraMB(Encoder * pEnc, |
1006 |
|
MACROBLOCK * pMB) |
1007 |
|
{ |
1008 |
|
|
1009 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1010 |
|
|
1015 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1016 |
|
|
1017 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1018 |
if(pMB->dquant != NO_CHANGE) |
if (pMB->dquant != NO_CHANGE) { |
|
{ |
|
1019 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
1020 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1021 |
|
|
1022 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1023 |
if (pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1024 |
|
if (pEnc->current->quant < 1) |
1025 |
|
pEnc->current->quant = 1; |
1026 |
} |
} |
1027 |
} |
} |
1028 |
|
|
1033 |
#define FCODEBITS 3 |
#define FCODEBITS 3 |
1034 |
#define MODEBITS 5 |
#define MODEBITS 5 |
1035 |
|
|
1036 |
void HintedMESet(Encoder * pEnc, int * intra) |
void |
1037 |
|
HintedMESet(Encoder * pEnc, |
1038 |
|
int *intra) |
1039 |
{ |
{ |
1040 |
HINTINFO * hint; |
HINTINFO * hint; |
1041 |
Bitstream bs; |
Bitstream bs; |
1044 |
|
|
1045 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1046 |
|
|
1047 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1048 |
*intra = hint->mvhint.intra; |
*intra = hint->mvhint.intra; |
1049 |
} |
} else { |
|
else |
|
|
{ |
|
1050 |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
1051 |
*intra = BitstreamGetBit(&bs); |
*intra = BitstreamGetBit(&bs); |
1052 |
} |
} |
1053 |
|
|
1054 |
if (*intra) |
if (*intra) { |
|
{ |
|
1055 |
return; |
return; |
1056 |
} |
} |
1057 |
|
|
1058 |
pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
pEnc->current->fcode = |
1059 |
|
(hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, |
1060 |
|
FCODEBITS); |
1061 |
|
|
1062 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1063 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1064 |
|
|
1065 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1066 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1067 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1068 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1069 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1070 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1071 |
VECTOR pred[4]; |
VECTOR pred[4]; |
1072 |
VECTOR tmp; |
VECTOR tmp; |
1073 |
int32_t dummy[4]; |
int32_t dummy[4]; |
1074 |
int vec; |
int vec; |
1075 |
|
|
1076 |
pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
pMB->mode = |
1077 |
|
(hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, |
1078 |
|
MODEBITS); |
1079 |
|
|
1080 |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
1081 |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
1082 |
|
|
1083 |
if (pMB->mode == MODE_INTER) |
if (pMB->mode == MODE_INTER) { |
1084 |
{ |
tmp.x = |
1085 |
tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, |
1086 |
tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); |
length); |
1087 |
|
tmp.y = |
1088 |
|
(hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, |
1089 |
|
length); |
1090 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1091 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1092 |
|
|
1093 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy); |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, |
1094 |
|
0, pred, dummy); |
1095 |
|
|
1096 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1097 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1098 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1099 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
1100 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
1101 |
} |
} |
1102 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
1103 |
else if (pMB->mode == MODE_INTER4V) |
for (vec = 0; vec < 4; ++vec) { |
1104 |
{ |
tmp.x = |
1105 |
for (vec=0 ; vec<4 ; ++vec) |
(hint->rawhints) ? bhint->mvs[vec]. |
1106 |
{ |
x : BitstreamGetBits(&bs, length); |
1107 |
tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); |
tmp.y = |
1108 |
tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[vec]. |
1109 |
|
y : BitstreamGetBits(&bs, length); |
1110 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1111 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1112 |
|
|
1113 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
get_pmvdata(pEnc->current->mbs, x, y, |
1114 |
|
pEnc->mbParam.mb_width, vec, pred, dummy); |
1115 |
|
|
1116 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1117 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1118 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
1119 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
1120 |
} |
} |
1121 |
} |
} else // intra / stuffing / not_coded |
|
else // intra / stuffing / not_coded |
|
|
{ |
|
|
for (vec=0 ; vec<4 ; ++vec) |
|
1122 |
{ |
{ |
1123 |
|
for (vec = 0; vec < 4; ++vec) { |
1124 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1125 |
} |
} |
1126 |
} |
} |
1127 |
|
|
1128 |
if (pMB->mode == MODE_INTER4V && |
if (pMB->mode == MODE_INTER4V && |
1129 |
(pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) |
(pEnc->current->global_flags & XVID_LUMIMASKING) |
1130 |
{ |
&& pMB->dquant != NO_CHANGE) { |
1131 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1132 |
|
|
1133 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1134 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1135 |
} |
} |
1136 |
} |
} |
1139 |
} |
} |
1140 |
|
|
1141 |
|
|
1142 |
void HintedMEGet(Encoder * pEnc, int intra) |
void |
1143 |
|
HintedMEGet(Encoder * pEnc, |
1144 |
|
int intra) |
1145 |
{ |
{ |
1146 |
HINTINFO * hint; |
HINTINFO * hint; |
1147 |
Bitstream bs; |
Bitstream bs; |
1150 |
|
|
1151 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1152 |
|
|
1153 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1154 |
hint->mvhint.intra = intra; |
hint->mvhint.intra = intra; |
1155 |
} |
} else { |
|
else |
|
|
{ |
|
1156 |
BitstreamInit(&bs, hint->hintstream, 0); |
BitstreamInit(&bs, hint->hintstream, 0); |
1157 |
BitstreamPutBit(&bs, intra); |
BitstreamPutBit(&bs, intra); |
1158 |
} |
} |
1159 |
|
|
1160 |
if (intra) |
if (intra) { |
1161 |
{ |
if (!hint->rawhints) { |
|
if (!hint->rawhints) |
|
|
{ |
|
1162 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1163 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1164 |
} |
} |
1168 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1169 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1170 |
|
|
1171 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1172 |
hint->mvhint.fcode = pEnc->current->fcode; |
hint->mvhint.fcode = pEnc->current->fcode; |
1173 |
} |
} else { |
|
else |
|
|
{ |
|
1174 |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
1175 |
} |
} |
1176 |
|
|
1177 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1178 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1179 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1180 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1181 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1182 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1183 |
VECTOR tmp; |
VECTOR tmp; |
1184 |
|
|
1185 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1186 |
bhint->mode = pMB->mode; |
bhint->mode = pMB->mode; |
1187 |
} |
} else { |
|
else |
|
|
{ |
|
1188 |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
1189 |
} |
} |
1190 |
|
|
1191 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
1192 |
tmp.x = pMB->mvs[0].x; |
tmp.x = pMB->mvs[0].x; |
1193 |
tmp.y = pMB->mvs[0].y; |
tmp.y = pMB->mvs[0].y; |
1194 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1195 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1196 |
|
|
1197 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1198 |
bhint->mvs[0].x = tmp.x; |
bhint->mvs[0].x = tmp.x; |
1199 |
bhint->mvs[0].y = tmp.y; |
bhint->mvs[0].y = tmp.y; |
1200 |
} |
} else { |
|
else |
|
|
{ |
|
1201 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1202 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1203 |
} |
} |
1204 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
|
else if (pMB->mode == MODE_INTER4V) |
|
|
{ |
|
1205 |
int vec; |
int vec; |
1206 |
|
|
1207 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1208 |
tmp.x = pMB->mvs[vec].x; |
tmp.x = pMB->mvs[vec].x; |
1209 |
tmp.y = pMB->mvs[vec].y; |
tmp.y = pMB->mvs[vec].y; |
1210 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1211 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1212 |
|
|
1213 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1214 |
bhint->mvs[vec].x = tmp.x; |
bhint->mvs[vec].x = tmp.x; |
1215 |
bhint->mvs[vec].y = tmp.y; |
bhint->mvs[vec].y = tmp.y; |
1216 |
} |
} else { |
|
else |
|
|
{ |
|
1217 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1218 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1219 |
} |
} |
1222 |
} |
} |
1223 |
} |
} |
1224 |
|
|
1225 |
if (!hint->rawhints) |
if (!hint->rawhints) { |
|
{ |
|
1226 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1227 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1228 |
} |
} |
1229 |
} |
} |
1230 |
|
|
1231 |
|
|
1232 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
static int |
1233 |
|
FrameCodeI(Encoder * pEnc, |
1234 |
|
Bitstream * bs, |
1235 |
|
uint32_t * pBits) |
1236 |
{ |
{ |
1237 |
|
|
1238 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1255 |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1256 |
|
|
1257 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
1258 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1259 |
{ |
MACROBLOCK *pMB = |
1260 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1261 |
|
|
1262 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1263 |
|
|
1264 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1265 |
|
dct_codes, qcoeff); |
1266 |
|
|
1267 |
start_timer(); |
start_timer(); |
1268 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1281 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1282 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1283 |
|
|
1284 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1285 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
1286 |
} |
} |
1287 |
|
|
1291 |
|
|
1292 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1293 |
|
|
1294 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) |
static int |
1295 |
|
FrameCodeP(Encoder * pEnc, |
1296 |
|
Bitstream * bs, |
1297 |
|
uint32_t * pBits, |
1298 |
|
bool force_inter, |
1299 |
|
bool vol_header) |
1300 |
{ |
{ |
1301 |
float fSigma; |
float fSigma; |
1302 |
|
|
1312 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1313 |
|
|
1314 |
start_timer(); |
start_timer(); |
1315 |
image_setedges(pRef, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1316 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.width, pEnc->mbParam.height, |
|
pEnc->mbParam.edged_height, |
|
|
pEnc->mbParam.width, |
|
|
pEnc->mbParam.height, |
|
1317 |
pEnc->current->global_flags & XVID_INTERLACING); |
pEnc->current->global_flags & XVID_INTERLACING); |
1318 |
stop_edges_timer(); |
stop_edges_timer(); |
1319 |
|
|
1322 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1323 |
|
|
1324 |
if (!force_inter) |
if (!force_inter) |
1325 |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
iLimit = |
1326 |
|
(int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * |
1327 |
|
INTRA_THRESHOLD); |
1328 |
else |
else |
1329 |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
1330 |
|
|
1331 |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
1332 |
start_timer(); |
start_timer(); |
1333 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1334 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1335 |
|
pEnc->mbParam.edged_height, |
1336 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1337 |
stop_inter_timer(); |
stop_inter_timer(); |
1338 |
} |
} |
1339 |
|
|
1340 |
start_timer(); |
start_timer(); |
1341 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
|
{ |
|
1342 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1343 |
} |
} else { |
1344 |
else |
bIntra = |
1345 |
{ |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1346 |
bIntra = MotionEstimation( |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
&pEnc->mbParam, |
|
|
pEnc->current, |
|
|
pEnc->reference, |
|
|
&pEnc->vInterH, |
|
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
1347 |
iLimit); |
iLimit); |
1348 |
} |
} |
1349 |
stop_motion_timer(); |
stop_motion_timer(); |
1350 |
|
|
1351 |
if (bIntra == 1) |
if (bIntra == 1) { |
|
{ |
|
1352 |
return FrameCodeI(pEnc, bs, pBits); |
return FrameCodeI(pEnc, bs, pBits); |
1353 |
} |
} |
1354 |
|
|
1366 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1367 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1368 |
|
|
1369 |
for(y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1370 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1371 |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
MACROBLOCK *pMB = |
1372 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1373 |
|
|
1374 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1375 |
|
|
1376 |
if (!bIntra) |
if (!bIntra) { |
|
{ |
|
1377 |
start_timer(); |
start_timer(); |
1378 |
MBMotionCompensation(pMB, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1379 |
x, y, |
&pEnc->vInterH, &pEnc->vInterV, |
1380 |
&pEnc->reference->image, |
&pEnc->vInterHV, &pEnc->current->image, |
1381 |
&pEnc->vInterH, |
dct_codes, pEnc->mbParam.width, |
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
|
&pEnc->current->image, |
|
|
dct_codes, |
|
|
pEnc->mbParam.width, |
|
1382 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1383 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1384 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1388 |
if(pMB->dquant != NO_CHANGE) { |
if(pMB->dquant != NO_CHANGE) { |
1389 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
1390 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1391 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1392 |
else if(pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1393 |
|
else if (pEnc->current->quant < 1) |
1394 |
|
pEnc->current->quant = 1; |
1395 |
} |
} |
1396 |
} |
} |
1397 |
pMB->quant = pEnc->current->quant; |
pMB->quant = pEnc->current->quant; |
1398 |
|
|
1399 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1400 |
|
|
1401 |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
pMB->cbp = |
1402 |
} |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1403 |
else |
dct_codes, qcoeff); |
1404 |
{ |
} else { |
1405 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1406 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1407 |
|
dct_codes, qcoeff); |
1408 |
} |
} |
1409 |
|
|
1410 |
start_timer(); |
start_timer(); |
1411 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1412 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1413 |
|
|
1414 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
{ |
|
1415 |
pEnc->sStat.kblks++; |
pEnc->sStat.kblks++; |
1416 |
} |
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1417 |
else if (pMB->cbp || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1418 |
pMB->mvs[0].x || pMB->mvs[0].y || |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
|
pMB->mvs[1].x || pMB->mvs[1].y || |
|
|
pMB->mvs[2].x || pMB->mvs[2].y || |
|
|
pMB->mvs[3].x || pMB->mvs[3].y) |
|
|
{ |
|
1419 |
pEnc->sStat.mblks++; |
pEnc->sStat.mblks++; |
1420 |
} |
} else { |
|
else |
|
|
{ |
|
1421 |
pEnc->sStat.ublks++; |
pEnc->sStat.ublks++; |
1422 |
} |
} |
1423 |
|
|
1429 |
|
|
1430 |
emms(); |
emms(); |
1431 |
|
|
1432 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1433 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
1434 |
} |
} |
1435 |
|
|
1445 |
{ |
{ |
1446 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1447 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1448 |
} |
} else if ((fSigma < iSearchRange / 6) |
|
else if ((fSigma < iSearchRange / 6) |
|
1449 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
1450 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
1451 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
1463 |
|
|
1464 |
|
|
1465 |
#ifdef BFRAMES |
#ifdef BFRAMES |
1466 |
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) |
static void |
1467 |
|
FrameCodeB(Encoder * pEnc, |
1468 |
|
FRAMEINFO * frame, |
1469 |
|
Bitstream * bs, |
1470 |
|
uint32_t * pBits) |
1471 |
{ |
{ |
1472 |
int16_t dct_codes[6*64]; |
int16_t dct_codes[6*64]; |
1473 |
int16_t qcoeff[6*64]; |
int16_t qcoeff[6*64]; |
1479 |
IMAGE *b_ref = &pEnc->current->image; |
IMAGE *b_ref = &pEnc->current->image; |
1480 |
|
|
1481 |
// forward |
// forward |
1482 |
image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING); |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1483 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1484 |
|
pEnc->mbParam.height, |
1485 |
|
frame->global_flags & XVID_INTERLACING); |
1486 |
start_timer(); |
start_timer(); |
1487 |
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, |
1488 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1489 |
|
0); |
1490 |
stop_inter_timer(); |
stop_inter_timer(); |
1491 |
|
|
1492 |
// backward |
// backward |
1493 |
image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING); |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1494 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1495 |
|
pEnc->mbParam.height, |
1496 |
|
frame->global_flags & XVID_INTERLACING); |
1497 |
start_timer(); |
start_timer(); |
1498 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1499 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1500 |
|
0); |
1501 |
stop_inter_timer(); |
stop_inter_timer(); |
1502 |
|
|
1503 |
start_timer(); |
start_timer(); |
1504 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, |
1505 |
pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1506 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1507 |
|
&pEnc->vInterV, &pEnc->vInterHV); |
1508 |
|
|
1509 |
|
|
1510 |
stop_motion_timer(); |
stop_motion_timer(); |
1525 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1526 |
|
|
1527 |
|
|
1528 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
{ |
|
1529 |
// reset prediction |
// reset prediction |
1530 |
|
|
1531 |
forward.x = 0; |
forward.x = 0; |
1533 |
backward.x = 0; |
backward.x = 0; |
1534 |
backward.y = 0; |
backward.y = 0; |
1535 |
|
|
1536 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1537 |
{ |
MACROBLOCK *f_mb = |
1538 |
MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
1539 |
MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK *b_mb = |
1540 |
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1541 |
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1542 |
|
|
1543 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
1544 |
if (mb->mode == MODE_NOT_CODED) |
if (mb->mode == MODE_NOT_CODED) { |
|
{ |
|
1545 |
mb->mvs[0].x = 0; |
mb->mvs[0].x = 0; |
1546 |
mb->mvs[0].y = 0; |
mb->mvs[0].y = 0; |
1547 |
continue; |
continue; |
1548 |
} |
} |
1549 |
|
|
1550 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1551 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1552 |
b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1553 |
|
&pEnc->vInterV, &pEnc->vInterHV, |
1554 |
dct_codes); |
dct_codes); |
1555 |
|
|
1556 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1557 |
mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
mb->cbp = |
1558 |
|
MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, |
1559 |
|
qcoeff); |
1560 |
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
1561 |
|
|
1562 |
|
|
1563 |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
1564 |
mb->cbp == 0 && |
&& mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { |
|
mb->mvs[0].x == 0 && |
|
|
mb->mvs[0].y == 0) |
|
|
{ |
|
1565 |
mb->mode = 5; // skipped |
mb->mode = 5; // skipped |
1566 |
} |
} |
1567 |
|
|
1568 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
|
{ |
|
1569 |
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
1570 |
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
1571 |
forward.x = mb->mvs[0].x; |
forward.x = mb->mvs[0].x; |
1572 |
forward.y = mb->mvs[0].y; |
forward.y = mb->mvs[0].y; |
1573 |
} |
} |
1574 |
|
|
1575 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
|
{ |
|
1576 |
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
1577 |
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
1578 |
backward.x = mb->b_mvs[0].x; |
backward.x = mb->b_mvs[0].x; |
1579 |
backward.y = mb->b_mvs[0].y; |
backward.y = mb->b_mvs[0].y; |
1580 |
} |
} |
|
|
|
1581 |
// printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y); |
// printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y); |
1582 |
|
|
1583 |
start_timer(); |
start_timer(); |
1584 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat); |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1585 |
|
&pEnc->sStat); |
1586 |
stop_coding_timer(); |
stop_coding_timer(); |
1587 |
} |
} |
1588 |
} |
} |