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 |
|
* 20.06.2002 bframe patch |
36 |
|
* 08.05.2002 fix some problem in DEBUG mode; |
37 |
|
* MinChen <chenm001@163.com> |
38 |
* 14.04.2002 added FrameCodeB() |
* 14.04.2002 added FrameCodeB() |
39 |
* |
* |
40 |
* $Id$ |
* $Id$ |
41 |
* |
* |
42 |
***************************************************************************/ |
****************************************************************************/ |
43 |
|
|
44 |
#include <stdlib.h> |
#include <stdlib.h> |
45 |
#include <stdio.h> |
#include <stdio.h> |
46 |
#include <math.h> |
#include <math.h> |
47 |
|
#include <string.h> |
48 |
|
|
49 |
#include "encoder.h" |
#include "encoder.h" |
50 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
51 |
#include "global.h" |
#include "global.h" |
52 |
#include "utils/timer.h" |
#include "utils/timer.h" |
53 |
#include "image/image.h" |
#include "image/image.h" |
54 |
|
#ifdef BFRAMES |
55 |
|
#include "image/font.h" |
56 |
|
#endif |
57 |
|
#include "motion/motion.h" |
58 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
59 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
60 |
#include "bitstream/bitstream.h" |
#include "bitstream/bitstream.h" |
66 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
67 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
68 |
|
|
69 |
|
#ifdef _SMP |
70 |
|
#include "motion/smp_motion_est.h" |
71 |
|
#endif |
72 |
|
/***************************************************************************** |
73 |
|
* Local macros |
74 |
|
****************************************************************************/ |
75 |
|
|
76 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
77 |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
78 |
|
|
79 |
|
/***************************************************************************** |
80 |
|
* Local function prototypes |
81 |
|
****************************************************************************/ |
82 |
|
|
83 |
|
static int FrameCodeI(Encoder * pEnc, |
84 |
|
Bitstream * bs, |
85 |
|
uint32_t * pBits); |
86 |
|
|
87 |
|
static int FrameCodeP(Encoder * pEnc, |
88 |
|
Bitstream * bs, |
89 |
|
uint32_t * pBits, |
90 |
|
bool force_inter, |
91 |
|
bool vol_header); |
92 |
|
|
93 |
|
#ifdef BFRAMES |
94 |
|
static void FrameCodeB(Encoder * pEnc, |
95 |
|
FRAMEINFO * frame, |
96 |
|
Bitstream * bs, |
97 |
|
uint32_t * pBits); |
98 |
|
#endif |
99 |
|
|
100 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); |
/***************************************************************************** |
101 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header); |
* Local data |
102 |
|
****************************************************************************/ |
103 |
|
|
104 |
static int DQtab[4] = |
static int DQtab[4] = { |
|
{ |
|
105 |
-1, -2, 1, 2 |
-1, -2, 1, 2 |
106 |
}; |
}; |
107 |
|
|
108 |
static int iDQtab[5] = |
static int iDQtab[5] = { |
|
{ |
|
109 |
1, 0, NO_CHANGE, 2, 3 |
1, 0, NO_CHANGE, 2, 3 |
110 |
}; |
}; |
111 |
|
|
112 |
|
|
113 |
void static image_null(IMAGE * image) |
static void __inline |
114 |
|
image_null(IMAGE * image) |
115 |
{ |
{ |
116 |
image->y = image->u = image->v = NULL; |
image->y = image->u = image->v = NULL; |
117 |
} |
} |
118 |
|
|
119 |
|
|
120 |
int encoder_create(XVID_ENC_PARAM * pParam) |
/***************************************************************************** |
121 |
|
* Encoder creation |
122 |
|
* |
123 |
|
* This function creates an Encoder instance, it allocates all necessary |
124 |
|
* image buffers (reference, current and bframes) and initialize the internal |
125 |
|
* xvid encoder paremeters according to the XVID_ENC_PARAM input parameter. |
126 |
|
* |
127 |
|
* The code seems to be very long but is very basic, mainly memory allocation |
128 |
|
* and cleaning code. |
129 |
|
* |
130 |
|
* Returned values : |
131 |
|
* - XVID_ERR_OK - no errors |
132 |
|
* - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
133 |
|
* cleans the structure before exiting. |
134 |
|
* pParam->handle is also set to NULL. |
135 |
|
* |
136 |
|
****************************************************************************/ |
137 |
|
|
138 |
|
int |
139 |
|
encoder_create(XVID_ENC_PARAM * pParam) |
140 |
{ |
{ |
141 |
Encoder *pEnc; |
Encoder *pEnc; |
142 |
uint32_t i; |
int i; |
143 |
|
|
144 |
pParam->handle = NULL; |
pParam->handle = NULL; |
145 |
|
|
150 |
ENC_CHECK(!(pParam->width % 2)); |
ENC_CHECK(!(pParam->width % 2)); |
151 |
ENC_CHECK(!(pParam->height % 2)); |
ENC_CHECK(!(pParam->height % 2)); |
152 |
|
|
153 |
if (pParam->fincr <= 0 || pParam->fbase <= 0) |
/* Fps */ |
154 |
{ |
|
155 |
|
if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
156 |
pParam->fincr = 1; |
pParam->fincr = 1; |
157 |
pParam->fbase = 25; |
pParam->fbase = 25; |
158 |
} |
} |
159 |
|
|
160 |
// simplify the "fincr/fbase" fraction |
/* |
161 |
// (neccessary, since windows supplies us with huge numbers) |
* Simplify the "fincr/fbase" fraction |
162 |
|
* (neccessary, since windows supplies us with huge numbers) |
163 |
|
*/ |
164 |
|
|
165 |
i = pParam->fincr; |
i = pParam->fincr; |
166 |
while (i > 1) |
while (i > 1) { |
167 |
{ |
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { |
|
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) |
|
|
{ |
|
168 |
pParam->fincr /= i; |
pParam->fincr /= i; |
169 |
pParam->fbase /= i; |
pParam->fbase /= i; |
170 |
i = pParam->fincr; |
i = pParam->fincr; |
171 |
continue; |
continue; |
172 |
} |
} |
173 |
i--; |
i--; |
174 |
|
} |
175 |
|
|
176 |
|
if (pParam->fbase > 65535) { |
177 |
|
float div = (float) pParam->fbase / 65535; |
178 |
|
|
179 |
|
pParam->fbase = (int) (pParam->fbase / div); |
180 |
|
pParam->fincr = (int) (pParam->fincr / div); |
181 |
|
} |
182 |
|
|
183 |
|
/* Bitrate allocator defaults */ |
184 |
|
|
185 |
|
if (pParam->rc_bitrate <= 0) |
186 |
|
pParam->rc_bitrate = 900000; |
187 |
|
|
188 |
|
if (pParam->rc_reaction_delay_factor <= 0) |
189 |
|
pParam->rc_reaction_delay_factor = 16; |
190 |
|
|
191 |
|
if (pParam->rc_averaging_period <= 0) |
192 |
|
pParam->rc_averaging_period = 100; |
193 |
|
|
194 |
|
if (pParam->rc_buffer <= 0) |
195 |
|
pParam->rc_buffer = 100; |
196 |
|
|
197 |
|
/* Max and min quantizers */ |
198 |
|
|
199 |
|
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
200 |
|
pParam->min_quantizer = 1; |
201 |
|
|
202 |
|
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
203 |
|
pParam->max_quantizer = 31; |
204 |
|
|
205 |
|
if (pParam->max_quantizer < pParam->min_quantizer) |
206 |
|
pParam->max_quantizer = pParam->min_quantizer; |
207 |
|
|
208 |
|
/* 1 keyframe each 10 seconds */ |
209 |
|
|
210 |
|
if (pParam->max_key_interval == 0) |
211 |
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
212 |
|
|
213 |
|
|
214 |
|
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
215 |
|
if (pEnc == NULL) |
216 |
|
return XVID_ERR_MEMORY; |
217 |
|
|
218 |
|
/* Zero the Encoder Structure */ |
219 |
|
|
220 |
|
memset(pEnc, 0, sizeof(Encoder)); |
221 |
|
|
222 |
|
/* Fill members of Encoder structure */ |
223 |
|
|
224 |
|
pEnc->mbParam.width = pParam->width; |
225 |
|
pEnc->mbParam.height = pParam->height; |
226 |
|
|
227 |
|
pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; |
228 |
|
pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; |
229 |
|
|
230 |
|
pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
231 |
|
pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; |
232 |
|
|
233 |
|
pEnc->mbParam.fbase = pParam->fbase; |
234 |
|
pEnc->mbParam.fincr = pParam->fincr; |
235 |
|
|
236 |
|
pEnc->mbParam.m_quant_type = H263_QUANT; |
237 |
|
|
238 |
|
pEnc->sStat.fMvPrevSigma = -1; |
239 |
|
|
240 |
|
/* Fill rate control parameters */ |
241 |
|
|
242 |
|
pEnc->bitrate = pParam->rc_bitrate; |
243 |
|
|
244 |
|
pEnc->iFrameNum = 0; |
245 |
|
pEnc->iMaxKeyInterval = pParam->max_key_interval; |
246 |
|
|
247 |
|
/* try to allocate frame memory */ |
248 |
|
|
249 |
|
pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
250 |
|
pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
251 |
|
|
252 |
|
if (pEnc->current == NULL || pEnc->reference == NULL) |
253 |
|
goto xvid_err_memory1; |
254 |
|
|
255 |
|
/* try to allocate mb memory */ |
256 |
|
|
257 |
|
pEnc->current->mbs = |
258 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
259 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
260 |
|
pEnc->reference->mbs = |
261 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
262 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
263 |
|
|
264 |
|
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
265 |
|
goto xvid_err_memory2; |
266 |
|
|
267 |
|
/* try to allocate image memory */ |
268 |
|
|
269 |
|
#ifdef _DEBUG_PSNR |
270 |
|
image_null(&pEnc->sOriginal); |
271 |
|
#endif |
272 |
|
#ifdef BFRAMES |
273 |
|
image_null(&pEnc->f_refh); |
274 |
|
image_null(&pEnc->f_refv); |
275 |
|
image_null(&pEnc->f_refhv); |
276 |
|
#endif |
277 |
|
image_null(&pEnc->current->image); |
278 |
|
image_null(&pEnc->reference->image); |
279 |
|
image_null(&pEnc->vInterH); |
280 |
|
image_null(&pEnc->vInterV); |
281 |
|
image_null(&pEnc->vInterVf); |
282 |
|
image_null(&pEnc->vInterHV); |
283 |
|
image_null(&pEnc->vInterHVf); |
284 |
|
|
285 |
|
#ifdef _DEBUG_PSNR |
286 |
|
if (image_create |
287 |
|
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
288 |
|
pEnc->mbParam.edged_height) < 0) |
289 |
|
goto xvid_err_memory3; |
290 |
|
#endif |
291 |
|
#ifdef BFRAMES |
292 |
|
if (image_create |
293 |
|
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
294 |
|
pEnc->mbParam.edged_height) < 0) |
295 |
|
goto xvid_err_memory3; |
296 |
|
if (image_create |
297 |
|
(&pEnc->f_refv, pEnc->mbParam.edged_width, |
298 |
|
pEnc->mbParam.edged_height) < 0) |
299 |
|
goto xvid_err_memory3; |
300 |
|
if (image_create |
301 |
|
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
302 |
|
pEnc->mbParam.edged_height) < 0) |
303 |
|
goto xvid_err_memory3; |
304 |
|
#endif |
305 |
|
if (image_create |
306 |
|
(&pEnc->current->image, pEnc->mbParam.edged_width, |
307 |
|
pEnc->mbParam.edged_height) < 0) |
308 |
|
goto xvid_err_memory3; |
309 |
|
if (image_create |
310 |
|
(&pEnc->reference->image, pEnc->mbParam.edged_width, |
311 |
|
pEnc->mbParam.edged_height) < 0) |
312 |
|
goto xvid_err_memory3; |
313 |
|
if (image_create |
314 |
|
(&pEnc->vInterH, pEnc->mbParam.edged_width, |
315 |
|
pEnc->mbParam.edged_height) < 0) |
316 |
|
goto xvid_err_memory3; |
317 |
|
if (image_create |
318 |
|
(&pEnc->vInterV, pEnc->mbParam.edged_width, |
319 |
|
pEnc->mbParam.edged_height) < 0) |
320 |
|
goto xvid_err_memory3; |
321 |
|
if (image_create |
322 |
|
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
323 |
|
pEnc->mbParam.edged_height) < 0) |
324 |
|
goto xvid_err_memory3; |
325 |
|
if (image_create |
326 |
|
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
327 |
|
pEnc->mbParam.edged_height) < 0) |
328 |
|
goto xvid_err_memory3; |
329 |
|
if (image_create |
330 |
|
(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
331 |
|
pEnc->mbParam.edged_height) < 0) |
332 |
|
goto xvid_err_memory3; |
333 |
|
|
334 |
|
|
335 |
|
|
336 |
|
/* B Frames specific init */ |
337 |
|
#ifdef BFRAMES |
338 |
|
|
339 |
|
pEnc->global = pParam->global; |
340 |
|
pEnc->mbParam.max_bframes = pParam->max_bframes; |
341 |
|
pEnc->bquant_ratio = pParam->bquant_ratio; |
342 |
|
pEnc->bframes = NULL; |
343 |
|
|
344 |
|
if (pEnc->mbParam.max_bframes > 0) { |
345 |
|
int n; |
346 |
|
|
347 |
|
pEnc->bframes = |
348 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), |
349 |
|
CACHE_LINE); |
350 |
|
|
351 |
|
if (pEnc->bframes == NULL) |
352 |
|
goto xvid_err_memory3; |
353 |
|
|
354 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
355 |
|
pEnc->bframes[n] = NULL; |
356 |
|
|
357 |
|
|
358 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
359 |
|
pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
360 |
|
|
361 |
|
if (pEnc->bframes[n] == NULL) |
362 |
|
goto xvid_err_memory4; |
363 |
|
|
364 |
|
pEnc->bframes[n]->mbs = |
365 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
366 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
367 |
|
|
368 |
|
if (pEnc->bframes[n]->mbs == NULL) |
369 |
|
goto xvid_err_memory4; |
370 |
|
|
371 |
|
image_null(&pEnc->bframes[n]->image); |
372 |
|
|
373 |
|
if (image_create |
374 |
|
(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, |
375 |
|
pEnc->mbParam.edged_height) < 0) |
376 |
|
goto xvid_err_memory4; |
377 |
|
|
378 |
|
} |
379 |
|
} |
380 |
|
|
381 |
|
pEnc->bframenum_head = 0; |
382 |
|
pEnc->bframenum_tail = 0; |
383 |
|
pEnc->flush_bframes = 0; |
384 |
|
pEnc->bframenum_dx50bvop = -1; |
385 |
|
|
386 |
|
pEnc->queue = NULL; |
387 |
|
|
388 |
|
|
389 |
|
if (pEnc->mbParam.max_bframes > 0) { |
390 |
|
int n; |
391 |
|
|
392 |
|
pEnc->queue = |
393 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), |
394 |
|
CACHE_LINE); |
395 |
|
|
396 |
|
if (pEnc->queue == NULL) |
397 |
|
goto xvid_err_memory4; |
398 |
|
|
399 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
400 |
|
image_null(&pEnc->queue[n]); |
401 |
|
|
402 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
403 |
|
if (image_create |
404 |
|
(&pEnc->queue[n], pEnc->mbParam.edged_width, |
405 |
|
pEnc->mbParam.edged_height) < 0) |
406 |
|
goto xvid_err_memory5; |
407 |
|
|
408 |
|
} |
409 |
|
} |
410 |
|
|
411 |
|
pEnc->queue_head = 0; |
412 |
|
pEnc->queue_tail = 0; |
413 |
|
pEnc->queue_size = 0; |
414 |
|
|
415 |
|
|
416 |
|
pEnc->mbParam.m_seconds = 0; |
417 |
|
pEnc->mbParam.m_ticks = 0; |
418 |
|
pEnc->m_framenum = 0; |
419 |
|
#endif |
420 |
|
|
421 |
|
pParam->handle = (void *) pEnc; |
422 |
|
|
423 |
|
if (pParam->rc_bitrate) { |
424 |
|
RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, |
425 |
|
pParam->rc_reaction_delay_factor, |
426 |
|
pParam->rc_averaging_period, pParam->rc_buffer, |
427 |
|
pParam->fbase * 1000 / pParam->fincr, |
428 |
|
pParam->max_quantizer, pParam->min_quantizer); |
429 |
|
} |
430 |
|
|
431 |
|
init_timer(); |
432 |
|
|
433 |
|
return XVID_ERR_OK; |
434 |
|
|
435 |
|
/* |
436 |
|
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
437 |
|
*/ |
438 |
|
#ifdef BFRAMES |
439 |
|
xvid_err_memory5: |
440 |
|
|
441 |
|
|
442 |
|
if (pEnc->mbParam.max_bframes > 0) { |
443 |
|
|
444 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
445 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
446 |
|
pEnc->mbParam.edged_height); |
447 |
|
} |
448 |
|
xvid_free(pEnc->queue); |
449 |
|
} |
450 |
|
|
451 |
|
xvid_err_memory4: |
452 |
|
|
453 |
|
if (pEnc->mbParam.max_bframes > 0) { |
454 |
|
|
455 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
456 |
|
|
457 |
|
if (pEnc->bframes[i] == NULL) |
458 |
|
continue; |
459 |
|
|
460 |
|
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
461 |
|
pEnc->mbParam.edged_height); |
462 |
|
|
463 |
|
xvid_free(pEnc->bframes[i]->mbs); |
464 |
|
|
465 |
|
xvid_free(pEnc->bframes[i]); |
466 |
|
|
467 |
|
} |
468 |
|
|
469 |
|
xvid_free(pEnc->bframes); |
470 |
|
} |
471 |
|
|
472 |
|
#endif |
473 |
|
|
474 |
|
xvid_err_memory3: |
475 |
|
#ifdef _DEBUG_PSNR |
476 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
477 |
|
pEnc->mbParam.edged_height); |
478 |
|
#endif |
479 |
|
|
480 |
|
#ifdef BFRAMES |
481 |
|
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
482 |
|
pEnc->mbParam.edged_height); |
483 |
|
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
484 |
|
pEnc->mbParam.edged_height); |
485 |
|
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
486 |
|
pEnc->mbParam.edged_height); |
487 |
|
#endif |
488 |
|
|
489 |
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
490 |
|
pEnc->mbParam.edged_height); |
491 |
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
492 |
|
pEnc->mbParam.edged_height); |
493 |
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
494 |
|
pEnc->mbParam.edged_height); |
495 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
496 |
|
pEnc->mbParam.edged_height); |
497 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
498 |
|
pEnc->mbParam.edged_height); |
499 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
500 |
|
pEnc->mbParam.edged_height); |
501 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
502 |
|
pEnc->mbParam.edged_height); |
503 |
|
|
504 |
|
xvid_err_memory2: |
505 |
|
xvid_free(pEnc->current->mbs); |
506 |
|
xvid_free(pEnc->reference->mbs); |
507 |
|
|
508 |
|
xvid_err_memory1: |
509 |
|
xvid_free(pEnc->current); |
510 |
|
xvid_free(pEnc->reference); |
511 |
|
xvid_free(pEnc); |
512 |
|
|
513 |
|
pParam->handle = NULL; |
514 |
|
|
515 |
|
return XVID_ERR_MEMORY; |
516 |
|
} |
517 |
|
|
518 |
|
/***************************************************************************** |
519 |
|
* Encoder destruction |
520 |
|
* |
521 |
|
* This function destroy the entire encoder structure created by a previous |
522 |
|
* successful encoder_create call. |
523 |
|
* |
524 |
|
* Returned values (for now only one returned value) : |
525 |
|
* - XVID_ERR_OK - no errors |
526 |
|
* |
527 |
|
****************************************************************************/ |
528 |
|
|
529 |
|
int |
530 |
|
encoder_destroy(Encoder * pEnc) |
531 |
|
{ |
532 |
|
#ifdef BFRAMES |
533 |
|
int i; |
534 |
|
#endif |
535 |
|
|
536 |
|
ENC_CHECK(pEnc); |
537 |
|
|
538 |
|
/* B Frames specific */ |
539 |
|
#ifdef BFRAMES |
540 |
|
if (pEnc->mbParam.max_bframes > 0) { |
541 |
|
|
542 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
543 |
|
|
544 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
545 |
|
pEnc->mbParam.edged_height); |
546 |
|
} |
547 |
|
xvid_free(pEnc->queue); |
548 |
|
} |
549 |
|
|
550 |
|
|
551 |
|
if (pEnc->mbParam.max_bframes > 0) { |
552 |
|
|
553 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
554 |
|
|
555 |
|
if (pEnc->bframes[i] == NULL) |
556 |
|
continue; |
557 |
|
|
558 |
|
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
559 |
|
pEnc->mbParam.edged_height); |
560 |
|
|
561 |
|
xvid_free(pEnc->bframes[i]->mbs); |
562 |
|
|
563 |
|
xvid_free(pEnc->bframes[i]); |
564 |
|
} |
565 |
|
|
566 |
|
xvid_free(pEnc->bframes); |
567 |
|
|
568 |
|
} |
569 |
|
#endif |
570 |
|
|
571 |
|
/* All images, reference, current etc ... */ |
572 |
|
|
573 |
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
574 |
|
pEnc->mbParam.edged_height); |
575 |
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
576 |
|
pEnc->mbParam.edged_height); |
577 |
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
578 |
|
pEnc->mbParam.edged_height); |
579 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
580 |
|
pEnc->mbParam.edged_height); |
581 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
582 |
|
pEnc->mbParam.edged_height); |
583 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
584 |
|
pEnc->mbParam.edged_height); |
585 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
586 |
|
pEnc->mbParam.edged_height); |
587 |
|
#ifdef BFRAMES |
588 |
|
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
589 |
|
pEnc->mbParam.edged_height); |
590 |
|
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
591 |
|
pEnc->mbParam.edged_height); |
592 |
|
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
593 |
|
pEnc->mbParam.edged_height); |
594 |
|
#endif |
595 |
|
#ifdef _DEBUG_PSNR |
596 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
597 |
|
pEnc->mbParam.edged_height); |
598 |
|
#endif |
599 |
|
|
600 |
|
/* Encoder structure */ |
601 |
|
|
602 |
|
xvid_free(pEnc->current->mbs); |
603 |
|
xvid_free(pEnc->current); |
604 |
|
|
605 |
|
xvid_free(pEnc->reference->mbs); |
606 |
|
xvid_free(pEnc->reference); |
607 |
|
|
608 |
|
xvid_free(pEnc); |
609 |
|
|
610 |
|
return XVID_ERR_OK; |
611 |
|
} |
612 |
|
|
613 |
|
|
614 |
|
#ifdef BFRAMES |
615 |
|
void inc_frame_num(Encoder * pEnc) |
616 |
|
{ |
617 |
|
pEnc->iFrameNum++; |
618 |
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
619 |
|
|
620 |
|
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
621 |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
622 |
|
} |
623 |
|
#endif |
624 |
|
|
625 |
|
|
626 |
|
#ifdef BFRAMES |
627 |
|
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
628 |
|
{ |
629 |
|
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
630 |
|
{ |
631 |
|
DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL"); |
632 |
|
return; |
633 |
|
} |
634 |
|
|
635 |
|
DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
636 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
637 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
638 |
|
|
639 |
|
|
640 |
|
start_timer(); |
641 |
|
if (image_input |
642 |
|
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
643 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
644 |
|
return; |
645 |
|
stop_conv_timer(); |
646 |
|
|
647 |
|
pEnc->queue_size++; |
648 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
649 |
|
} |
650 |
|
#endif |
651 |
|
|
652 |
|
|
653 |
|
#ifdef BFRAMES |
654 |
|
/***************************************************************************** |
655 |
|
* IPB frame encoder entry point |
656 |
|
* |
657 |
|
* Returned values : |
658 |
|
* - XVID_ERR_OK - no errors |
659 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
660 |
|
* format |
661 |
|
****************************************************************************/ |
662 |
|
|
663 |
|
int |
664 |
|
encoder_encode_bframes(Encoder * pEnc, |
665 |
|
XVID_ENC_FRAME * pFrame, |
666 |
|
XVID_ENC_STATS * pResult) |
667 |
|
{ |
668 |
|
uint16_t x, y; |
669 |
|
Bitstream bs; |
670 |
|
uint32_t bits; |
671 |
|
|
672 |
|
int input_valid = 1; |
673 |
|
|
674 |
|
#ifdef _DEBUG_PSNR |
675 |
|
float psnr; |
676 |
|
char temp[128]; |
677 |
|
#endif |
678 |
|
|
679 |
|
ENC_CHECK(pEnc); |
680 |
|
ENC_CHECK(pFrame); |
681 |
|
ENC_CHECK(pFrame->image); |
682 |
|
|
683 |
|
start_global_timer(); |
684 |
|
|
685 |
|
BitstreamInit(&bs, pFrame->bitstream, 0); |
686 |
|
|
687 |
|
ipvop_loop: |
688 |
|
|
689 |
|
/* |
690 |
|
* bframe "flush" code |
691 |
|
*/ |
692 |
|
|
693 |
|
if ((pFrame->image == NULL || pEnc->flush_bframes) |
694 |
|
&& (pEnc->bframenum_head < pEnc->bframenum_tail)) { |
695 |
|
|
696 |
|
if (pEnc->flush_bframes == 0) { |
697 |
|
/* |
698 |
|
* we have reached the end of stream without getting |
699 |
|
* a future reference frame... so encode last final |
700 |
|
* frame as a pframe |
701 |
|
*/ |
702 |
|
|
703 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
704 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
705 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
706 |
|
|
707 |
|
pEnc->bframenum_tail--; |
708 |
|
SWAP(pEnc->current, pEnc->reference); |
709 |
|
|
710 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
711 |
|
|
712 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
713 |
|
|
714 |
|
BitstreamPad(&bs); |
715 |
|
pFrame->length = BitstreamLength(&bs); |
716 |
|
pFrame->intra = 0; |
717 |
|
|
718 |
|
return XVID_ERR_OK; |
719 |
|
} |
720 |
|
|
721 |
|
|
722 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) 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 |
|
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
727 |
|
pEnc->bframenum_head++; |
728 |
|
|
729 |
|
BitstreamPad(&bs); |
730 |
|
pFrame->length = BitstreamLength(&bs); |
731 |
|
pFrame->intra = 0; |
732 |
|
|
733 |
|
if (input_valid) |
734 |
|
queue_image(pEnc, pFrame); |
735 |
|
|
736 |
|
return XVID_ERR_OK; |
737 |
|
} |
738 |
|
|
739 |
|
if (pEnc->bframenum_head > 0) { |
740 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
741 |
|
|
742 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
743 |
|
|
744 |
|
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
745 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
746 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
747 |
|
|
748 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
749 |
|
BitstreamPad(&bs); |
750 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
751 |
|
|
752 |
|
pFrame->length = BitstreamLength(&bs); |
753 |
|
pFrame->intra = 0; |
754 |
|
|
755 |
|
if (input_valid) |
756 |
|
queue_image(pEnc, pFrame); |
757 |
|
|
758 |
|
return XVID_ERR_OK; |
759 |
|
} |
760 |
|
} |
761 |
|
|
762 |
|
|
763 |
|
bvop_loop: |
764 |
|
|
765 |
|
if (pEnc->bframenum_dx50bvop != -1) |
766 |
|
{ |
767 |
|
|
768 |
|
SWAP(pEnc->current, pEnc->reference); |
769 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
770 |
|
|
771 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
772 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
773 |
|
} |
774 |
|
|
775 |
|
if (input_valid) |
776 |
|
{ |
777 |
|
queue_image(pEnc, pFrame); |
778 |
|
input_valid = 0; |
779 |
|
} |
780 |
|
|
781 |
|
} else if (input_valid) { |
782 |
|
|
783 |
|
SWAP(pEnc->current, pEnc->reference); |
784 |
|
|
785 |
|
start_timer(); |
786 |
|
if (image_input |
787 |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
788 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
789 |
|
return XVID_ERR_FORMAT; |
790 |
|
stop_conv_timer(); |
791 |
|
|
792 |
|
// queue input frame, and dequue next image |
793 |
|
if (pEnc->queue_size > 0) |
794 |
|
{ |
795 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
796 |
|
if (pEnc->queue_head != pEnc->queue_tail) |
797 |
|
{ |
798 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
799 |
|
} |
800 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
801 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
802 |
|
} |
803 |
|
|
804 |
|
} else if (pEnc->queue_size > 0) { |
805 |
|
|
806 |
|
SWAP(pEnc->current, pEnc->reference); |
807 |
|
|
808 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
809 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
810 |
|
pEnc->queue_size--; |
811 |
|
|
812 |
|
} else if (BitstreamPos(&bs) == 0) { |
813 |
|
|
814 |
|
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
815 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
816 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
817 |
|
|
818 |
|
pFrame->intra = 0; |
819 |
|
|
820 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
821 |
|
BitstreamPad(&bs); |
822 |
|
pFrame->length = BitstreamLength(&bs); |
823 |
|
|
824 |
|
return XVID_ERR_OK; |
825 |
|
|
826 |
|
} else { |
827 |
|
|
828 |
|
pFrame->length = BitstreamLength(&bs); |
829 |
|
return XVID_ERR_OK; |
830 |
|
} |
831 |
|
|
832 |
|
pEnc->flush_bframes = 0; |
833 |
|
|
834 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
835 |
|
* Well there was a separation here so i put it in ANSI C |
836 |
|
* comment style :-) |
837 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
838 |
|
|
839 |
|
emms(); |
840 |
|
|
841 |
|
// only inc frame num, adapt quant, etc. if we havent seen it before |
842 |
|
if (pEnc->bframenum_dx50bvop < 0 ) |
843 |
|
{ |
844 |
|
if (pFrame->quant == 0) |
845 |
|
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
846 |
|
else |
847 |
|
pEnc->current->quant = pFrame->quant; |
848 |
|
|
849 |
|
if (pEnc->current->quant < 1) |
850 |
|
pEnc->current->quant = 1; |
851 |
|
|
852 |
|
if (pEnc->current->quant > 31) |
853 |
|
pEnc->current->quant = 31; |
854 |
|
|
855 |
|
pEnc->current->global_flags = pFrame->general; |
856 |
|
pEnc->current->motion_flags = pFrame->motion; |
857 |
|
|
858 |
|
/* ToDo : dynamic fcode (in both directions) */ |
859 |
|
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
860 |
|
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
861 |
|
|
862 |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
863 |
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
864 |
|
|
865 |
|
inc_frame_num(pEnc); |
866 |
|
|
867 |
|
#ifdef _DEBUG_PSNR |
868 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
869 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
870 |
|
#endif |
871 |
|
|
872 |
|
emms(); |
873 |
|
|
874 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
875 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
876 |
|
"%i if:%i st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks); |
877 |
|
} |
878 |
|
|
879 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
880 |
|
* Luminance masking |
881 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
882 |
|
|
883 |
|
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
884 |
|
int *temp_dquants = |
885 |
|
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
886 |
|
pEnc->mbParam.mb_height * sizeof(int), |
887 |
|
CACHE_LINE); |
888 |
|
|
889 |
|
pEnc->current->quant = |
890 |
|
adaptive_quantization(pEnc->current->image.y, |
891 |
|
pEnc->mbParam.edged_width, temp_dquants, |
892 |
|
pEnc->current->quant, pEnc->current->quant, |
893 |
|
2 * pEnc->current->quant, |
894 |
|
pEnc->mbParam.mb_width, |
895 |
|
pEnc->mbParam.mb_height); |
896 |
|
|
897 |
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
898 |
|
|
899 |
|
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
900 |
|
|
901 |
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
902 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
903 |
|
|
904 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
905 |
|
} |
906 |
|
|
907 |
|
#undef OFFSET |
908 |
} |
} |
909 |
|
|
910 |
if (pParam->fbase > 65535) |
xvid_free(temp_dquants); |
|
{ |
|
|
float div = (float)pParam->fbase / 65535; |
|
|
pParam->fbase = (int)(pParam->fbase / div); |
|
|
pParam->fincr = (int)(pParam->fincr / div); |
|
911 |
} |
} |
912 |
|
|
913 |
if (pParam->rc_bitrate <= 0) |
} |
|
pParam->rc_bitrate = 900000; |
|
914 |
|
|
915 |
if (pParam->rc_reaction_delay_factor <= 0) |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
916 |
pParam->rc_reaction_delay_factor = 16; |
* ivop/pvop/bvop selection |
917 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
918 |
|
|
|
if (pParam->rc_averaging_period <= 0) |
|
|
pParam->rc_averaging_period = 100; |
|
919 |
|
|
920 |
if (pParam->rc_buffer <= 0) |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
921 |
pParam->rc_buffer = 100; |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
922 |
|
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
923 |
|
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
924 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
925 |
|
pEnc->mbParam.height) > 30) { |
926 |
|
/* |
927 |
|
* This will be coded as an Intra Frame |
928 |
|
*/ |
929 |
|
|
930 |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
931 |
pParam->min_quantizer = 1; |
pEnc->bframenum_head, pEnc->bframenum_tail, |
932 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
933 |
|
|
934 |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
935 |
pParam->max_quantizer = 31; |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
936 |
|
} |
937 |
|
|
938 |
if (pParam->max_key_interval == 0) /* 1 keyframe each 10 seconds */ |
// when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe |
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
|
939 |
|
|
940 |
if (pParam->max_quantizer < pParam->min_quantizer) |
if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
|
pParam->max_quantizer = pParam->min_quantizer; |
|
941 |
|
|
942 |
if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL) |
pEnc->bframenum_tail--; |
943 |
return XVID_ERR_MEMORY; |
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
944 |
|
|
945 |
/* Fill members of Encoder structure */ |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
946 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
947 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
948 |
|
} |
949 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
950 |
|
|
951 |
pEnc->mbParam.width = pParam->width; |
pFrame->intra = 0; |
|
pEnc->mbParam.height = pParam->height; |
|
952 |
|
|
953 |
pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; |
} else { |
|
pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; |
|
954 |
|
|
955 |
pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
FrameCodeI(pEnc, &bs, &bits); |
956 |
pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; |
pFrame->intra = 1; |
957 |
|
|
958 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->bframenum_dx50bvop = -1; |
959 |
|
} |
960 |
|
|
961 |
/* Fill rate control parameters */ |
pEnc->flush_bframes = 1; |
962 |
|
|
963 |
pEnc->bitrate = pParam->rc_bitrate; |
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
964 |
|
BitstreamPad(&bs); |
965 |
|
input_valid = 0; |
966 |
|
goto ipvop_loop; |
967 |
|
} |
968 |
|
|
969 |
pEnc->iFrameNum = 0; |
/* |
970 |
pEnc->iMaxKeyInterval = pParam->max_key_interval; |
* NB : sequences like "IIBB" decode fine with msfdam but, |
971 |
|
* go screwy with divx 5.00 |
972 |
|
*/ |
973 |
|
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
974 |
|
/* |
975 |
|
* This will be coded as a Predicted Frame |
976 |
|
*/ |
977 |
|
|
978 |
/* try to allocate frame memory */ |
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
979 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
980 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
981 |
|
|
982 |
pEnc->current = NULL; |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
983 |
pEnc->reference = NULL; |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
|
if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL || |
|
|
(pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL) |
|
|
{ |
|
|
if (pEnc->current) xvid_free(pEnc->current); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
984 |
} |
} |
985 |
|
|
986 |
/* try to allocate mb memory */ |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
987 |
|
pFrame->intra = 0; |
988 |
pEnc->current->mbs = NULL; |
pEnc->flush_bframes = 1; |
|
pEnc->reference->mbs = NULL; |
|
|
|
|
|
#ifdef _DEBUG |
|
|
#ifdef WIN32 |
|
|
OutputDebugString("malloc mbs"); |
|
|
#endif |
|
|
#endif |
|
989 |
|
|
990 |
if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL || |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
991 |
(pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) |
BitstreamPad(&bs); |
992 |
{ |
input_valid = 0; |
993 |
if (pEnc->current->mbs) xvid_free(pEnc->current->mbs); |
goto ipvop_loop; |
|
xvid_free(pEnc->current); |
|
|
xvid_free(pEnc->reference); |
|
|
xvid_free(pEnc); |
|
994 |
} |
} |
995 |
|
|
996 |
/* try to allocate image memory */ |
} else { |
997 |
|
/* |
998 |
|
* This will be coded as a Bidirectional Frame |
999 |
|
*/ |
1000 |
|
|
1001 |
#ifdef _DEBUG |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1002 |
image_null(&pEnc->sOriginal); |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1003 |
#endif |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
image_null(&pEnc->current->image); |
|
|
image_null(&pEnc->reference->image); |
|
|
image_null(&pEnc->vInterH); |
|
|
image_null(&pEnc->vInterV); |
|
|
image_null(&pEnc->vInterVf); |
|
|
image_null(&pEnc->vInterHV); |
|
|
image_null(&pEnc->vInterHVf); |
|
1004 |
|
|
1005 |
#ifdef _DEBUG |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
1006 |
#ifdef WIN32 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1007 |
OutputDebugString("malloc images"); |
} |
|
#endif |
|
|
#endif |
|
|
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); |
|
1008 |
|
|
1009 |
xvid_free(pEnc->current); |
if (pFrame->bquant < 1) { |
1010 |
xvid_free(pEnc->reference); |
pEnc->current->quant = |
1011 |
xvid_free(pEnc); |
((pEnc->reference->quant + |
1012 |
return XVID_ERR_MEMORY; |
pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
1013 |
|
} else { |
1014 |
|
pEnc->current->quant = pFrame->bquant; |
1015 |
} |
} |
1016 |
|
|
1017 |
pParam->handle = (void *)pEnc; |
/* store frame into bframe buffer & swap ref back to current */ |
1018 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1019 |
|
SWAP(pEnc->current, pEnc->reference); |
1020 |
|
|
1021 |
if (pParam->rc_bitrate) |
pEnc->bframenum_tail++; |
1022 |
{ |
|
1023 |
RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor, |
pFrame->intra = 0; |
1024 |
pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr, |
pFrame->length = 0; |
1025 |
pParam->max_quantizer, pParam->min_quantizer); |
|
1026 |
|
input_valid = 0; |
1027 |
|
goto bvop_loop; |
1028 |
} |
} |
1029 |
|
|
1030 |
init_timer(); |
BitstreamPad(&bs); |
1031 |
|
pFrame->length = BitstreamLength(&bs); |
1032 |
|
|
1033 |
return XVID_ERR_OK; |
if (pResult) { |
1034 |
|
pResult->quant = pEnc->current->quant; |
1035 |
|
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
1036 |
|
pResult->kblks = pEnc->sStat.kblks; |
1037 |
|
pResult->mblks = pEnc->sStat.mblks; |
1038 |
|
pResult->ublks = pEnc->sStat.ublks; |
1039 |
} |
} |
1040 |
|
|
1041 |
|
emms(); |
1042 |
|
|
1043 |
int encoder_destroy(Encoder * pEnc) |
#ifdef _DEBUG_PSNR |
1044 |
{ |
psnr = |
1045 |
ENC_CHECK(pEnc); |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1046 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1047 |
|
pEnc->mbParam.height); |
1048 |
|
|
1049 |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1050 |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
DEBUG(temp); |
|
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); |
|
|
#ifdef _DEBUG |
|
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
1051 |
#endif |
#endif |
|
xvid_free(pEnc->current->mbs); |
|
|
xvid_free(pEnc->current); |
|
1052 |
|
|
1053 |
xvid_free(pEnc->reference->mbs); |
if (pFrame->quant == 0) { |
1054 |
xvid_free(pEnc->reference); |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1055 |
|
pFrame->length, pFrame->intra); |
1056 |
|
} |
1057 |
|
|
1058 |
|
|
1059 |
|
stop_global_timer(); |
1060 |
|
write_timer(); |
1061 |
|
|
|
xvid_free(pEnc); |
|
1062 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1063 |
} |
} |
1064 |
|
|
1065 |
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
#endif |
1066 |
|
|
1067 |
|
|
1068 |
|
|
1069 |
|
/***************************************************************************** |
1070 |
|
* "original" IP frame encoder entry point |
1071 |
|
* |
1072 |
|
* Returned values : |
1073 |
|
* - XVID_ERR_OK - no errors |
1074 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
1075 |
|
* format |
1076 |
|
****************************************************************************/ |
1077 |
|
|
1078 |
|
int |
1079 |
|
encoder_encode(Encoder * pEnc, |
1080 |
|
XVID_ENC_FRAME * pFrame, |
1081 |
|
XVID_ENC_STATS * pResult) |
1082 |
{ |
{ |
1083 |
uint16_t x, y; |
uint16_t x, y; |
1084 |
Bitstream bs; |
Bitstream bs; |
1085 |
uint32_t bits; |
uint32_t bits; |
1086 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1087 |
#ifdef _DEBUG |
|
1088 |
|
#ifdef _DEBUG_PSNR |
1089 |
float psnr; |
float psnr; |
1090 |
uint8_t temp[100]; |
uint8_t temp[128]; |
1091 |
#endif |
#endif |
1092 |
|
|
1093 |
start_global_timer(); |
start_global_timer(); |
1101 |
|
|
1102 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1103 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1104 |
|
#ifdef BFRAMES |
1105 |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
1106 |
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
1107 |
|
#endif |
1108 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1109 |
|
|
1110 |
start_timer(); |
start_timer(); |
1111 |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input |
1112 |
pFrame->image, pFrame->colorspace)) |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
1113 |
{ |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) |
1114 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
|
} |
|
1115 |
stop_conv_timer(); |
stop_conv_timer(); |
1116 |
|
|
1117 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1118 |
image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1119 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1120 |
#endif |
#endif |
1121 |
|
|
1122 |
EMMS(); |
emms(); |
1123 |
|
|
1124 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
1125 |
|
|
1126 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
1127 |
{ |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
1128 |
pEnc->current->quant = RateControlGetQ(0); |
} else { |
|
} |
|
|
else |
|
|
{ |
|
1129 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
1130 |
} |
} |
1131 |
|
|
1132 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1133 |
{ |
int *temp_dquants = |
1134 |
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 * |
1135 |
|
pEnc->mbParam.mb_height * sizeof(int), |
1136 |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
CACHE_LINE); |
1137 |
pEnc->mbParam.edged_width, // stride |
|
1138 |
temp_dquants, |
pEnc->current->quant = |
1139 |
pEnc->current->quant, |
adaptive_quantization(pEnc->current->image.y, |
1140 |
pEnc->current->quant, // min_quant |
pEnc->mbParam.edged_width, temp_dquants, |
1141 |
2*pEnc->current->quant, // max_quant |
pEnc->current->quant, pEnc->current->quant, |
1142 |
|
2 * pEnc->current->quant, |
1143 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
1144 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
1145 |
|
|
1146 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1147 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
1148 |
{ |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
1149 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1150 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1151 |
|
|
1152 |
|
|
1153 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
1154 |
|
|
1155 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
1156 |
|
} |
1157 |
|
|
1158 |
|
#undef OFFSET |
1159 |
} |
} |
1160 |
|
|
1161 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
1162 |
} |
} |
1163 |
|
|
1165 |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
1166 |
write_vol_header = 1; |
write_vol_header = 1; |
1167 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
1168 |
} |
} else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
1169 |
else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
int matrix1_changed, matrix2_changed; |
|
int ret1, ret2; |
|
1170 |
|
|
1171 |
ret1 = ret2 = 0; |
matrix1_changed = matrix2_changed = 0; |
1172 |
|
|
1173 |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
1174 |
write_vol_header = 1; |
write_vol_header = 1; |
1177 |
|
|
1178 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
1179 |
if(pFrame->quant_intra_matrix != NULL) |
if(pFrame->quant_intra_matrix != NULL) |
1180 |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); |
1181 |
if(pFrame->quant_inter_matrix != NULL) |
if(pFrame->quant_inter_matrix != NULL) |
1182 |
ret2 = set_inter_matrix(pFrame->quant_inter_matrix); |
matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); |
1183 |
} |
} else { |
1184 |
else { |
matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
1185 |
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()); |
|
1186 |
} |
} |
1187 |
if(write_vol_header == 0) |
if(write_vol_header == 0) |
1188 |
write_vol_header = ret1 | ret2; |
write_vol_header = matrix1_changed | matrix2_changed; |
1189 |
} |
} |
1190 |
|
|
1191 |
if (pFrame->intra < 0) |
if (pFrame->intra < 0) { |
1192 |
{ |
if ((pEnc->iFrameNum == 0) |
1193 |
if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
|| ((pEnc->iMaxKeyInterval > 0) |
1194 |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { |
|
|
|
1195 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
1196 |
else |
} else { |
1197 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
1198 |
} |
} |
1199 |
else |
} else { |
1200 |
{ |
if (pFrame->intra == 1) { |
|
if (pFrame->intra == 1) |
|
1201 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
1202 |
else |
} else { |
1203 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
1204 |
} |
} |
1205 |
|
|
1206 |
|
} |
1207 |
|
|
1208 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
1209 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
1210 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1211 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1212 |
|
|
1213 |
if (pResult) |
if (pResult) { |
|
{ |
|
1214 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
1215 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
1216 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
1218 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
1219 |
} |
} |
1220 |
|
|
1221 |
EMMS(); |
emms(); |
|
|
|
|
if (pFrame->quant == 0) |
|
|
{ |
|
|
RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
|
|
} |
|
1222 |
|
|
1223 |
#ifdef _DEBUG |
if (pFrame->quant == 0) { |
1224 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1225 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pFrame->length, pFrame->intra); |
1226 |
|
} |
1227 |
|
#ifdef _DEBUG_PSNR |
1228 |
|
psnr = |
1229 |
|
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1230 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1231 |
|
pEnc->mbParam.height); |
1232 |
|
|
1233 |
sprintf(temp, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1234 |
DEBUG(temp); |
DEBUG(temp); |
1235 |
#endif |
#endif |
1236 |
|
|
1237 |
|
#ifdef BFRAMES |
1238 |
|
inc_frame_num(pEnc); |
1239 |
|
#else |
1240 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1241 |
|
#endif |
1242 |
|
|
1243 |
|
|
1244 |
stop_global_timer(); |
stop_global_timer(); |
1245 |
write_timer(); |
write_timer(); |
1248 |
} |
} |
1249 |
|
|
1250 |
|
|
1251 |
static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { |
static __inline void |
1252 |
|
CodeIntraMB(Encoder * pEnc, |
1253 |
|
MACROBLOCK * pMB) |
1254 |
|
{ |
1255 |
|
|
1256 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1257 |
|
|
1262 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1263 |
|
|
1264 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1265 |
if(pMB->dquant != NO_CHANGE) |
if (pMB->dquant != NO_CHANGE) { |
|
{ |
|
1266 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
1267 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1268 |
|
|
1269 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1270 |
if (pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1271 |
|
if (pEnc->current->quant < 1) |
1272 |
|
pEnc->current->quant = 1; |
1273 |
} |
} |
1274 |
} |
} |
1275 |
|
|
1280 |
#define FCODEBITS 3 |
#define FCODEBITS 3 |
1281 |
#define MODEBITS 5 |
#define MODEBITS 5 |
1282 |
|
|
1283 |
void HintedMESet(Encoder * pEnc, int * intra) |
void |
1284 |
|
HintedMESet(Encoder * pEnc, |
1285 |
|
int *intra) |
1286 |
{ |
{ |
1287 |
HINTINFO * hint; |
HINTINFO * hint; |
1288 |
Bitstream bs; |
Bitstream bs; |
1291 |
|
|
1292 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1293 |
|
|
1294 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1295 |
*intra = hint->mvhint.intra; |
*intra = hint->mvhint.intra; |
1296 |
} |
} else { |
|
else |
|
|
{ |
|
1297 |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
1298 |
*intra = BitstreamGetBit(&bs); |
*intra = BitstreamGetBit(&bs); |
1299 |
} |
} |
1300 |
|
|
1301 |
if (*intra) |
if (*intra) { |
|
{ |
|
1302 |
return; |
return; |
1303 |
} |
} |
1304 |
|
|
1305 |
pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
pEnc->current->fcode = |
1306 |
|
(hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, |
1307 |
|
FCODEBITS); |
1308 |
|
|
1309 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1310 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1311 |
|
|
1312 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1313 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1314 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1315 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1316 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1317 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1318 |
VECTOR pred[4]; |
VECTOR pred[4]; |
1319 |
VECTOR tmp; |
VECTOR tmp; |
1320 |
uint32_t dummy[4]; |
int32_t dummy[4]; |
1321 |
int vec; |
int vec; |
1322 |
|
|
1323 |
pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
pMB->mode = |
1324 |
|
(hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, |
1325 |
|
MODEBITS); |
1326 |
|
|
1327 |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
1328 |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
1329 |
|
|
1330 |
if (pMB->mode == MODE_INTER) |
if (pMB->mode == MODE_INTER) { |
1331 |
{ |
tmp.x = |
1332 |
tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, |
1333 |
tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); |
length); |
1334 |
|
tmp.y = |
1335 |
|
(hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, |
1336 |
|
length); |
1337 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1338 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1339 |
|
|
1340 |
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, |
1341 |
|
0, pred, dummy); |
1342 |
|
|
1343 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1344 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1345 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1346 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
1347 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
1348 |
} |
} |
1349 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
1350 |
else if (pMB->mode == MODE_INTER4V) |
for (vec = 0; vec < 4; ++vec) { |
1351 |
{ |
tmp.x = |
1352 |
for (vec=0 ; vec<4 ; ++vec) |
(hint->rawhints) ? bhint->mvs[vec]. |
1353 |
{ |
x : BitstreamGetBits(&bs, length); |
1354 |
tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); |
tmp.y = |
1355 |
tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[vec]. |
1356 |
|
y : BitstreamGetBits(&bs, length); |
1357 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1358 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1359 |
|
|
1360 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
get_pmvdata(pEnc->current->mbs, x, y, |
1361 |
|
pEnc->mbParam.mb_width, vec, pred, dummy); |
1362 |
|
|
1363 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1364 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1365 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
1366 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
1367 |
} |
} |
1368 |
} |
} else // intra / stuffing / not_coded |
|
else // intra / stuffing / not_coded |
|
|
{ |
|
|
for (vec=0 ; vec<4 ; ++vec) |
|
1369 |
{ |
{ |
1370 |
|
for (vec = 0; vec < 4; ++vec) { |
1371 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1372 |
} |
} |
1373 |
} |
} |
1374 |
|
|
1375 |
if (pMB->mode == MODE_INTER4V && |
if (pMB->mode == MODE_INTER4V && |
1376 |
(pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) |
(pEnc->current->global_flags & XVID_LUMIMASKING) |
1377 |
{ |
&& pMB->dquant != NO_CHANGE) { |
1378 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1379 |
|
|
1380 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1381 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1382 |
} |
} |
1383 |
} |
} |
1386 |
} |
} |
1387 |
|
|
1388 |
|
|
1389 |
void HintedMEGet(Encoder * pEnc, int intra) |
void |
1390 |
|
HintedMEGet(Encoder * pEnc, |
1391 |
|
int intra) |
1392 |
{ |
{ |
1393 |
HINTINFO * hint; |
HINTINFO * hint; |
1394 |
Bitstream bs; |
Bitstream bs; |
1397 |
|
|
1398 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1399 |
|
|
1400 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1401 |
hint->mvhint.intra = intra; |
hint->mvhint.intra = intra; |
1402 |
} |
} else { |
|
else |
|
|
{ |
|
1403 |
BitstreamInit(&bs, hint->hintstream, 0); |
BitstreamInit(&bs, hint->hintstream, 0); |
1404 |
BitstreamPutBit(&bs, intra); |
BitstreamPutBit(&bs, intra); |
1405 |
} |
} |
1406 |
|
|
1407 |
if (intra) |
if (intra) { |
1408 |
{ |
if (!hint->rawhints) { |
|
if (!hint->rawhints) |
|
|
{ |
|
1409 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1410 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1411 |
} |
} |
1415 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1416 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1417 |
|
|
1418 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1419 |
hint->mvhint.fcode = pEnc->current->fcode; |
hint->mvhint.fcode = pEnc->current->fcode; |
1420 |
} |
} else { |
|
else |
|
|
{ |
|
1421 |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
1422 |
} |
} |
1423 |
|
|
1424 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1425 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1426 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1427 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1428 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1429 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1430 |
VECTOR tmp; |
VECTOR tmp; |
1431 |
|
|
1432 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1433 |
bhint->mode = pMB->mode; |
bhint->mode = pMB->mode; |
1434 |
} |
} else { |
|
else |
|
|
{ |
|
1435 |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
1436 |
} |
} |
1437 |
|
|
1438 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
1439 |
tmp.x = pMB->mvs[0].x; |
tmp.x = pMB->mvs[0].x; |
1440 |
tmp.y = pMB->mvs[0].y; |
tmp.y = pMB->mvs[0].y; |
1441 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1442 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1443 |
|
|
1444 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1445 |
bhint->mvs[0].x = tmp.x; |
bhint->mvs[0].x = tmp.x; |
1446 |
bhint->mvs[0].y = tmp.y; |
bhint->mvs[0].y = tmp.y; |
1447 |
} |
} else { |
|
else |
|
|
{ |
|
1448 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1449 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1450 |
} |
} |
1451 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
|
else if (pMB->mode == MODE_INTER4V) |
|
|
{ |
|
1452 |
int vec; |
int vec; |
1453 |
|
|
1454 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1455 |
tmp.x = pMB->mvs[vec].x; |
tmp.x = pMB->mvs[vec].x; |
1456 |
tmp.y = pMB->mvs[vec].y; |
tmp.y = pMB->mvs[vec].y; |
1457 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1458 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1459 |
|
|
1460 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1461 |
bhint->mvs[vec].x = tmp.x; |
bhint->mvs[vec].x = tmp.x; |
1462 |
bhint->mvs[vec].y = tmp.y; |
bhint->mvs[vec].y = tmp.y; |
1463 |
} |
} else { |
|
else |
|
|
{ |
|
1464 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1465 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1466 |
} |
} |
1469 |
} |
} |
1470 |
} |
} |
1471 |
|
|
1472 |
if (!hint->rawhints) |
if (!hint->rawhints) { |
|
{ |
|
1473 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1474 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1475 |
} |
} |
1476 |
} |
} |
1477 |
|
|
1478 |
|
|
1479 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
static int |
1480 |
|
FrameCodeI(Encoder * pEnc, |
1481 |
|
Bitstream * bs, |
1482 |
|
uint32_t * pBits) |
1483 |
{ |
{ |
1484 |
|
|
1485 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1493 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1494 |
|
|
1495 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1496 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
#ifdef BFRAMES |
1497 |
|
#define DIVX501B481P "DivX501b481p" |
1498 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1499 |
|
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1500 |
|
} |
1501 |
|
#endif |
1502 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1503 |
|
|
1504 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1505 |
|
|
1508 |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1509 |
|
|
1510 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
1511 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1512 |
{ |
MACROBLOCK *pMB = |
1513 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1514 |
|
|
1515 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1516 |
|
|
1517 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1518 |
|
dct_codes, qcoeff); |
1519 |
|
|
1520 |
start_timer(); |
start_timer(); |
1521 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1534 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1535 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1536 |
|
|
1537 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1538 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
1539 |
} |
} |
1540 |
|
|
1544 |
|
|
1545 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1546 |
|
|
1547 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) |
static int |
1548 |
|
FrameCodeP(Encoder * pEnc, |
1549 |
|
Bitstream * bs, |
1550 |
|
uint32_t * pBits, |
1551 |
|
bool force_inter, |
1552 |
|
bool vol_header) |
1553 |
{ |
{ |
1554 |
float fSigma; |
float fSigma; |
1555 |
|
|
1565 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1566 |
|
|
1567 |
start_timer(); |
start_timer(); |
1568 |
image_setedges(pRef, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1569 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.width, pEnc->mbParam.height, |
|
pEnc->mbParam.edged_height, |
|
|
pEnc->mbParam.width, |
|
|
pEnc->mbParam.height, |
|
1570 |
pEnc->current->global_flags & XVID_INTERLACING); |
pEnc->current->global_flags & XVID_INTERLACING); |
1571 |
stop_edges_timer(); |
stop_edges_timer(); |
1572 |
|
|
1575 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1576 |
|
|
1577 |
if (!force_inter) |
if (!force_inter) |
1578 |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
iLimit = |
1579 |
|
(int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * |
1580 |
|
INTRA_THRESHOLD); |
1581 |
else |
else |
1582 |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
1583 |
|
|
1584 |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
1585 |
start_timer(); |
start_timer(); |
1586 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1587 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1588 |
|
pEnc->mbParam.edged_height, |
1589 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1590 |
stop_inter_timer(); |
stop_inter_timer(); |
1591 |
} |
} |
1592 |
|
|
1593 |
start_timer(); |
start_timer(); |
1594 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
|
{ |
|
1595 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1596 |
} |
} else { |
1597 |
|
|
1598 |
|
#ifdef _SMP |
1599 |
|
if (NUMTHREADS > 1) |
1600 |
|
bIntra = |
1601 |
|
SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1602 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1603 |
|
iLimit); |
1604 |
else |
else |
1605 |
{ |
#endif |
1606 |
bIntra = MotionEstimation( |
|
1607 |
&pEnc->mbParam, |
bIntra = |
1608 |
pEnc->current, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1609 |
pEnc->reference, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
&pEnc->vInterH, |
|
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
1610 |
iLimit); |
iLimit); |
1611 |
|
|
1612 |
|
|
1613 |
} |
} |
1614 |
stop_motion_timer(); |
stop_motion_timer(); |
1615 |
|
|
1616 |
if (bIntra == 1) |
if (bIntra == 1) { |
|
{ |
|
1617 |
return FrameCodeI(pEnc, bs, pBits); |
return FrameCodeI(pEnc, bs, pBits); |
1618 |
} |
} |
1619 |
|
|
1622 |
if(vol_header) |
if(vol_header) |
1623 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1624 |
|
|
1625 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1626 |
|
|
1627 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1628 |
|
|
1631 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1632 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1633 |
|
|
1634 |
for(y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1635 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1636 |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
MACROBLOCK *pMB = |
1637 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1638 |
|
|
1639 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1640 |
|
|
1641 |
if (!bIntra) |
if (!bIntra) { |
|
{ |
|
1642 |
start_timer(); |
start_timer(); |
1643 |
MBMotionCompensation(pMB, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1644 |
x, y, |
&pEnc->vInterH, &pEnc->vInterV, |
1645 |
&pEnc->reference->image, |
&pEnc->vInterHV, &pEnc->current->image, |
1646 |
&pEnc->vInterH, |
dct_codes, pEnc->mbParam.width, |
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
|
&pEnc->current->image, |
|
|
dct_codes, |
|
|
pEnc->mbParam.width, |
|
1647 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1648 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1649 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1653 |
if(pMB->dquant != NO_CHANGE) { |
if(pMB->dquant != NO_CHANGE) { |
1654 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
1655 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1656 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1657 |
else if(pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1658 |
|
else if (pEnc->current->quant < 1) |
1659 |
|
pEnc->current->quant = 1; |
1660 |
} |
} |
1661 |
} |
} |
1662 |
pMB->quant = pEnc->current->quant; |
pMB->quant = pEnc->current->quant; |
1663 |
|
|
1664 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1665 |
|
|
1666 |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
pMB->cbp = |
1667 |
} |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1668 |
else |
dct_codes, qcoeff); |
1669 |
{ |
} else { |
1670 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1671 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1672 |
|
dct_codes, qcoeff); |
1673 |
} |
} |
1674 |
|
|
1675 |
start_timer(); |
start_timer(); |
1676 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1677 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1678 |
|
|
1679 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
{ |
|
1680 |
pEnc->sStat.kblks++; |
pEnc->sStat.kblks++; |
1681 |
} |
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1682 |
else if (pMB->cbp || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1683 |
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) |
|
|
{ |
|
1684 |
pEnc->sStat.mblks++; |
pEnc->sStat.mblks++; |
1685 |
} |
} else { |
|
else |
|
|
{ |
|
1686 |
pEnc->sStat.ublks++; |
pEnc->sStat.ublks++; |
1687 |
} |
} |
1688 |
|
|
1694 |
|
|
1695 |
emms(); |
emms(); |
1696 |
|
|
1697 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1698 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
1699 |
} |
} |
1700 |
|
|
1710 |
{ |
{ |
1711 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1712 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1713 |
} |
} else if ((fSigma < iSearchRange / 6) |
|
else if ((fSigma < iSearchRange / 6) |
|
1714 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
1715 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
1716 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
1727 |
} |
} |
1728 |
|
|
1729 |
|
|
1730 |
#if 0 |
#ifdef BFRAMES |
1731 |
|
static void |
1732 |
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) |
FrameCodeB(Encoder * pEnc, |
1733 |
|
FRAMEINFO * frame, |
1734 |
|
Bitstream * bs, |
1735 |
|
uint32_t * pBits) |
1736 |
{ |
{ |
1737 |
int16_t dct_codes[6][64]; |
int16_t dct_codes[6 * 64]; |
1738 |
int16_t qcoeff[6][64]; |
int16_t qcoeff[6 * 64]; |
1739 |
uint32_t x, y; |
uint32_t x, y; |
1740 |
VECTOR forward; |
VECTOR forward; |
1741 |
VECTOR backward; |
VECTOR backward; |
1743 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
1744 |
IMAGE *b_ref = &pEnc->current->image; |
IMAGE *b_ref = &pEnc->current->image; |
1745 |
|
|
1746 |
/* forward */ |
// forward |
1747 |
image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1748 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1749 |
|
pEnc->mbParam.height, |
1750 |
|
frame->global_flags & XVID_INTERLACING); |
1751 |
start_timer(); |
start_timer(); |
1752 |
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, |
1753 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1754 |
|
0); |
1755 |
stop_inter_timer(); |
stop_inter_timer(); |
1756 |
|
|
1757 |
/* backward */ |
// backward |
1758 |
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, |
1759 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1760 |
|
pEnc->mbParam.height, |
1761 |
|
frame->global_flags & XVID_INTERLACING); |
1762 |
start_timer(); |
start_timer(); |
1763 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1764 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1765 |
|
0); |
1766 |
stop_inter_timer(); |
stop_inter_timer(); |
1767 |
|
|
1768 |
start_timer(); |
start_timer(); |
1769 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, |
1770 |
pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1771 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1772 |
|
&pEnc->vInterV, &pEnc->vInterHV); |
1773 |
|
|
1774 |
|
|
1775 |
stop_motion_timer(); |
stop_motion_timer(); |
1776 |
|
|
1777 |
if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
/*if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
1778 |
{ |
{ |
1779 |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
1780 |
} |
} */ |
1781 |
|
|
1782 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1783 |
BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0, |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
|
frame->quant, frame->fcode, frame->bcode); |
|
1784 |
|
|
1785 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1786 |
|
|
1790 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1791 |
|
|
1792 |
|
|
1793 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
{ |
|
1794 |
// reset prediction |
// reset prediction |
1795 |
|
|
1796 |
forward.x = 0; |
forward.x = 0; |
1798 |
backward.x = 0; |
backward.x = 0; |
1799 |
backward.y = 0; |
backward.y = 0; |
1800 |
|
|
1801 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1802 |
{ |
MACROBLOCK *f_mb = |
1803 |
MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
1804 |
MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK *b_mb = |
1805 |
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1806 |
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1807 |
|
|
1808 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
1809 |
if (mb->mode == MODE_NOT_CODED) |
if (mb->mode == MODE_NOT_CODED) { |
|
{ |
|
1810 |
mb->mvs[0].x = 0; |
mb->mvs[0].x = 0; |
1811 |
mb->mvs[0].y = 0; |
mb->mvs[0].y = 0; |
1812 |
continue; |
continue; |
1813 |
} |
} |
1814 |
|
|
1815 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1816 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1817 |
b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1818 |
|
&pEnc->vInterV, &pEnc->vInterHV, |
1819 |
dct_codes); |
dct_codes); |
1820 |
|
|
1821 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1822 |
mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff); |
mb->cbp = |
1823 |
|
MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, |
1824 |
|
qcoeff); |
1825 |
//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); |
1826 |
|
|
1827 |
|
|
1828 |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
1829 |
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) |
|
|
{ |
|
1830 |
mb->mode = 5; // skipped |
mb->mode = 5; // skipped |
1831 |
} |
} |
1832 |
|
|
1833 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
|
{ |
|
1834 |
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
1835 |
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
1836 |
forward.x = mb->mvs[0].x; |
forward.x = mb->mvs[0].x; |
1837 |
forward.y = mb->mvs[0].y; |
forward.y = mb->mvs[0].y; |
1838 |
} |
} |
1839 |
|
|
1840 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
|
{ |
|
1841 |
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
1842 |
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
1843 |
backward.x = mb->b_mvs[0].x; |
backward.x = mb->b_mvs[0].x; |
1844 |
backward.y = mb->b_mvs[0].y; |
backward.y = mb->b_mvs[0].y; |
1845 |
} |
} |
1846 |
|
// DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[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); |
|
|
*/ |
|
1847 |
|
|
1848 |
start_timer(); |
start_timer(); |
1849 |
MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat); |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1850 |
|
&pEnc->sStat); |
1851 |
stop_coding_timer(); |
stop_coding_timer(); |
1852 |
} |
} |
1853 |
} |
} |
1857 |
// TODO: dynamic fcode/bcode ??? |
// TODO: dynamic fcode/bcode ??? |
1858 |
|
|
1859 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
|
|
|
1860 |
} |
} |
|
|
|
1861 |
#endif |
#endif |