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