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