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