1 |
/************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Encoder main module - |
* - Encoder main module - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* 2002 Peter Ross <pross@xvid.org> |
8 |
* to use this software module in hardware or software products are |
* 2002 Daniel Smith <danielsmith@astroboymail.com> |
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
9 |
* |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
11 |
* it under the terms of the GNU General Public License as published by |
* |
12 |
|
* XviD is free software; you can redistribute it and/or modify it |
13 |
|
* under the terms of the GNU General Public License as published by |
14 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
15 |
* (at your option) any later version. |
* (at your option) any later version. |
16 |
* |
* |
23 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
25 |
* |
* |
26 |
***************************************************************************/ |
* Under section 8 of the GNU General Public License, the copyright |
27 |
|
* holders of XVID explicitly forbid distribution in the following |
28 |
/**************************************************************************** |
* countries: |
29 |
|
* |
30 |
|
* - Japan |
31 |
|
* - United States of America |
32 |
* |
* |
33 |
* History |
* Linking XviD statically or dynamically with other modules is making a |
34 |
|
* combined work based on XviD. Thus, the terms and conditions of the |
35 |
|
* GNU General Public License cover the whole combination. |
36 |
* |
* |
37 |
* 14.04.2002 added FrameCodeB() |
* As a special exception, the copyright holders of XviD give you |
38 |
|
* permission to link XviD with independent modules that communicate with |
39 |
|
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
40 |
|
* license terms of these independent modules, and to copy and distribute |
41 |
|
* the resulting combined work under terms of your choice, provided that |
42 |
|
* every copy of the combined work is accompanied by a complete copy of |
43 |
|
* the source code of XviD (the version of XviD used to produce the |
44 |
|
* combined work), being distributed under the terms of the GNU General |
45 |
|
* Public License plus this exception. An independent module is a module |
46 |
|
* which is not derived from or based on XviD. |
47 |
|
* |
48 |
|
* Note that people who make modified versions of XviD are not obligated |
49 |
|
* to grant this special exception for their modified versions; it is |
50 |
|
* their choice whether to do so. The GNU General Public License gives |
51 |
|
* permission to release a modified version without this exception; this |
52 |
|
* exception also makes it possible to release a modified version which |
53 |
|
* carries forward this exception. |
54 |
* |
* |
55 |
* $Id$ |
* $Id$ |
56 |
* |
* |
57 |
***************************************************************************/ |
****************************************************************************/ |
58 |
|
|
59 |
#include <stdlib.h> |
#include <stdlib.h> |
60 |
#include <stdio.h> |
#include <stdio.h> |
61 |
#include <math.h> |
#include <math.h> |
62 |
|
#include <string.h> |
63 |
|
|
64 |
#include "encoder.h" |
#include "encoder.h" |
65 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
66 |
#include "global.h" |
#include "global.h" |
67 |
#include "utils/timer.h" |
#include "utils/timer.h" |
68 |
#include "image/image.h" |
#include "image/image.h" |
69 |
|
#include "motion/motion.h" |
70 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
71 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
72 |
#include "bitstream/bitstream.h" |
#include "bitstream/bitstream.h" |
78 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
79 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
80 |
|
|
81 |
|
/***************************************************************************** |
82 |
|
* Local macros |
83 |
|
****************************************************************************/ |
84 |
|
|
85 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
86 |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
87 |
|
|
88 |
|
/***************************************************************************** |
89 |
|
* Local function prototypes |
90 |
|
****************************************************************************/ |
91 |
|
|
92 |
|
static int FrameCodeI(Encoder * pEnc, |
93 |
|
Bitstream * bs, |
94 |
|
uint32_t * pBits); |
95 |
|
|
96 |
|
static int FrameCodeP(Encoder * pEnc, |
97 |
|
Bitstream * bs, |
98 |
|
uint32_t * pBits, |
99 |
|
bool force_inter, |
100 |
|
bool vol_header); |
101 |
|
|
102 |
|
/***************************************************************************** |
103 |
|
* Local data |
104 |
|
****************************************************************************/ |
105 |
|
|
106 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); |
static int DQtab[4] = { |
|
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header); |
|
|
|
|
|
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 |
void static image_null(IMAGE * image) |
static void __inline |
116 |
|
image_null(IMAGE * image) |
117 |
{ |
{ |
118 |
image->y = image->u = image->v = NULL; |
image->y = image->u = image->v = NULL; |
119 |
} |
} |
120 |
|
|
121 |
|
|
122 |
int encoder_create(XVID_ENC_PARAM * pParam) |
/***************************************************************************** |
123 |
|
* Encoder creation |
124 |
|
* |
125 |
|
* This function creates an Encoder instance, it allocates all necessary |
126 |
|
* image buffers (reference, current) and initialize the internal xvid |
127 |
|
* 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 |
|
if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
158 |
pParam->fincr = 1; |
pParam->fincr = 1; |
159 |
pParam->fbase = 25; |
pParam->fbase = 25; |
160 |
} |
} |
161 |
|
|
162 |
// simplify the "fincr/fbase" fraction |
/* |
163 |
// (neccessary, since windows supplies us with huge numbers) |
* Simplify the "fincr/fbase" fraction |
164 |
|
* (neccessary, since windows supplies us with huge numbers) |
165 |
|
*/ |
166 |
|
|
167 |
i = pParam->fincr; |
i = pParam->fincr; |
168 |
while (i > 1) |
while (i > 1) { |
169 |
{ |
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { |
|
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) |
|
|
{ |
|
170 |
pParam->fincr /= i; |
pParam->fincr /= i; |
171 |
pParam->fbase /= i; |
pParam->fbase /= i; |
172 |
i = pParam->fincr; |
i = pParam->fincr; |
175 |
i--; |
i--; |
176 |
} |
} |
177 |
|
|
178 |
if (pParam->fbase > 65535) |
if (pParam->fbase > 65535) { |
|
{ |
|
179 |
float div = (float)pParam->fbase / 65535; |
float div = (float)pParam->fbase / 65535; |
180 |
|
|
181 |
pParam->fbase = (int)(pParam->fbase / div); |
pParam->fbase = (int)(pParam->fbase / div); |
182 |
pParam->fincr = (int)(pParam->fincr / div); |
pParam->fincr = (int)(pParam->fincr / div); |
183 |
} |
} |
184 |
|
|
185 |
|
/* Bitrate allocator defaults */ |
186 |
|
|
187 |
if (pParam->rc_bitrate <= 0) |
if (pParam->rc_bitrate <= 0) |
188 |
pParam->rc_bitrate = 900000; |
pParam->rc_bitrate = 900000; |
189 |
|
|
196 |
if (pParam->rc_buffer <= 0) |
if (pParam->rc_buffer <= 0) |
197 |
pParam->rc_buffer = 100; |
pParam->rc_buffer = 100; |
198 |
|
|
199 |
|
/* Max and min quantizers */ |
200 |
|
|
201 |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
202 |
pParam->min_quantizer = 1; |
pParam->min_quantizer = 1; |
203 |
|
|
204 |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
205 |
pParam->max_quantizer = 31; |
pParam->max_quantizer = 31; |
206 |
|
|
|
if (pParam->max_key_interval == 0) /* 1 keyframe each 10 seconds */ |
|
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
|
|
|
|
207 |
if (pParam->max_quantizer < pParam->min_quantizer) |
if (pParam->max_quantizer < pParam->min_quantizer) |
208 |
pParam->max_quantizer = pParam->min_quantizer; |
pParam->max_quantizer = pParam->min_quantizer; |
209 |
|
|
210 |
if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL) |
/* 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; |
return XVID_ERR_MEMORY; |
218 |
|
|
219 |
|
/* Zero the Encoder Structure */ |
220 |
|
|
221 |
|
memset(pEnc, 0, sizeof(Encoder)); |
222 |
|
|
223 |
/* Fill members of Encoder structure */ |
/* Fill members of Encoder structure */ |
224 |
|
|
225 |
pEnc->mbParam.width = pParam->width; |
pEnc->mbParam.width = pParam->width; |
231 |
pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
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; |
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 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->sStat.fMvPrevSigma = -1; |
240 |
|
|
241 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
247 |
|
|
248 |
/* try to allocate frame memory */ |
/* try to allocate frame memory */ |
249 |
|
|
250 |
pEnc->current = NULL; |
pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
251 |
pEnc->reference = NULL; |
pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
252 |
if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL || |
|
253 |
(pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL) |
if (pEnc->current == NULL || pEnc->reference == NULL) |
254 |
{ |
goto xvid_err_memory1; |
|
if (pEnc->current) xvid_free(pEnc->current); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
255 |
|
|
256 |
/* try to allocate mb memory */ |
/* try to allocate mb memory */ |
257 |
|
|
258 |
pEnc->current->mbs = NULL; |
pEnc->current->mbs = |
259 |
pEnc->reference->mbs = NULL; |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
260 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
261 |
|
pEnc->reference->mbs = |
262 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
263 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
264 |
|
|
265 |
#ifdef _DEBUG |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
266 |
#ifdef WIN32 |
goto xvid_err_memory2; |
|
OutputDebugString("malloc mbs"); |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL || |
|
|
(pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) |
|
|
{ |
|
|
if (pEnc->current->mbs) xvid_free(pEnc->current->mbs); |
|
|
xvid_free(pEnc->current); |
|
|
xvid_free(pEnc->reference); |
|
|
xvid_free(pEnc); |
|
|
} |
|
267 |
|
|
268 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
269 |
|
|
270 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
271 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
272 |
#endif |
#endif |
273 |
image_null(&pEnc->current->image); |
image_null(&pEnc->current->image); |
274 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
275 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
276 |
image_null(&pEnc->vInterV); |
image_null(&pEnc->vInterV); |
|
image_null(&pEnc->vInterVf); |
|
277 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
|
image_null(&pEnc->vInterHVf); |
|
278 |
|
|
279 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
280 |
#ifdef WIN32 |
if (image_create |
281 |
OutputDebugString("malloc images"); |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
282 |
#endif |
pEnc->mbParam.edged_height) < 0) |
283 |
|
goto xvid_err_memory3; |
284 |
#endif |
#endif |
285 |
if ( |
if (image_create |
286 |
#ifdef _DEBUG |
(&pEnc->current->image, pEnc->mbParam.edged_width, |
287 |
image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
pEnc->mbParam.edged_height) < 0) |
288 |
#endif |
goto xvid_err_memory3; |
289 |
image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
if (image_create |
290 |
image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
(&pEnc->reference->image, pEnc->mbParam.edged_width, |
291 |
image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
pEnc->mbParam.edged_height) < 0) |
292 |
image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
goto xvid_err_memory3; |
293 |
image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
if (image_create |
294 |
image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
(&pEnc->vInterH, pEnc->mbParam.edged_width, |
295 |
image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
296 |
{ |
goto xvid_err_memory3; |
297 |
#ifdef _DEBUG |
if (image_create |
298 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
(&pEnc->vInterV, pEnc->mbParam.edged_width, |
299 |
#endif |
pEnc->mbParam.edged_height) < 0) |
300 |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
goto xvid_err_memory3; |
301 |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
if (image_create |
302 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
303 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height) < 0) |
304 |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
goto xvid_err_memory3; |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
|
|
|
xvid_free(pEnc->current); |
|
|
xvid_free(pEnc->reference); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
305 |
|
|
306 |
pParam->handle = (void *)pEnc; |
pParam->handle = (void *)pEnc; |
307 |
|
|
308 |
if (pParam->rc_bitrate) |
if (pParam->rc_bitrate) { |
309 |
{ |
RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, |
310 |
RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor, |
pParam->rc_reaction_delay_factor, |
311 |
pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr, |
pParam->rc_averaging_period, pParam->rc_buffer, |
312 |
|
pParam->fbase * 1000 / pParam->fincr, |
313 |
pParam->max_quantizer, pParam->min_quantizer); |
pParam->max_quantizer, pParam->min_quantizer); |
314 |
} |
} |
315 |
|
|
316 |
init_timer(); |
init_timer(); |
317 |
|
|
318 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
319 |
|
|
320 |
|
/* |
321 |
|
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
322 |
|
*/ |
323 |
|
|
324 |
|
xvid_err_memory3: |
325 |
|
#ifdef _DEBUG_PSNR |
326 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
327 |
|
pEnc->mbParam.edged_height); |
328 |
|
#endif |
329 |
|
|
330 |
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
331 |
|
pEnc->mbParam.edged_height); |
332 |
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
333 |
|
pEnc->mbParam.edged_height); |
334 |
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
335 |
|
pEnc->mbParam.edged_height); |
336 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
337 |
|
pEnc->mbParam.edged_height); |
338 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
339 |
|
pEnc->mbParam.edged_height); |
340 |
|
|
341 |
|
xvid_err_memory2: |
342 |
|
xvid_free(pEnc->current->mbs); |
343 |
|
xvid_free(pEnc->reference->mbs); |
344 |
|
|
345 |
|
xvid_err_memory1: |
346 |
|
xvid_free(pEnc->current); |
347 |
|
xvid_free(pEnc->reference); |
348 |
|
xvid_free(pEnc); |
349 |
|
|
350 |
|
pParam->handle = NULL; |
351 |
|
|
352 |
|
return XVID_ERR_MEMORY; |
353 |
} |
} |
354 |
|
|
355 |
|
/***************************************************************************** |
356 |
|
* Encoder destruction |
357 |
|
* |
358 |
|
* This function destroy the entire encoder structure created by a previous |
359 |
|
* successful encoder_create call. |
360 |
|
* |
361 |
|
* Returned values (for now only one returned value) : |
362 |
|
* - XVID_ERR_OK - no errors |
363 |
|
* |
364 |
|
****************************************************************************/ |
365 |
|
|
366 |
int encoder_destroy(Encoder * pEnc) |
int |
367 |
|
encoder_destroy(Encoder * pEnc) |
368 |
{ |
{ |
369 |
|
|
370 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
371 |
|
|
372 |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
/* All images, reference, current etc ... */ |
373 |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
374 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
375 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
376 |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
377 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
378 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
379 |
#ifdef _DEBUG |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
380 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
381 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
382 |
|
pEnc->mbParam.edged_height); |
383 |
|
|
384 |
|
#ifdef _DEBUG_PSNR |
385 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
386 |
|
pEnc->mbParam.edged_height); |
387 |
#endif |
#endif |
388 |
|
|
389 |
|
/* Encoder structure */ |
390 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
391 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
392 |
|
|
394 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
395 |
|
|
396 |
xvid_free(pEnc); |
xvid_free(pEnc); |
397 |
|
|
398 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
399 |
} |
} |
400 |
|
|
401 |
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
|
402 |
|
void inc_frame_num(Encoder * pEnc) |
403 |
|
{ |
404 |
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
405 |
|
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
406 |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
407 |
|
} |
408 |
|
|
409 |
|
/***************************************************************************** |
410 |
|
* "original" IP frame encoder entry point |
411 |
|
* |
412 |
|
* Returned values : |
413 |
|
* - XVID_ERR_OK - no errors |
414 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
415 |
|
* format |
416 |
|
****************************************************************************/ |
417 |
|
|
418 |
|
int |
419 |
|
encoder_encode(Encoder * pEnc, |
420 |
|
XVID_ENC_FRAME * pFrame, |
421 |
|
XVID_ENC_STATS * pResult) |
422 |
{ |
{ |
423 |
uint16_t x, y; |
uint16_t x, y; |
424 |
Bitstream bs; |
Bitstream bs; |
425 |
uint32_t bits; |
uint32_t bits; |
426 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
427 |
#ifdef _DEBUG |
|
428 |
|
#ifdef _DEBUG_PSNR |
429 |
float psnr; |
float psnr; |
430 |
uint8_t temp[100]; |
uint8_t temp[128]; |
431 |
#endif |
#endif |
432 |
|
|
433 |
start_global_timer(); |
start_global_timer(); |
441 |
|
|
442 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
443 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
444 |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
445 |
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
446 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
447 |
|
|
448 |
start_timer(); |
start_timer(); |
449 |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input |
450 |
pFrame->image, pFrame->colorspace)) |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
451 |
{ |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) |
452 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
|
} |
|
453 |
stop_conv_timer(); |
stop_conv_timer(); |
454 |
|
|
455 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
456 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
457 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
458 |
#endif |
#endif |
459 |
|
|
460 |
EMMS(); |
emms(); |
461 |
|
|
462 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
463 |
|
|
464 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
465 |
{ |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
466 |
pEnc->current->quant = RateControlGetQ(0); |
} else { |
|
} |
|
|
else |
|
|
{ |
|
467 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
468 |
} |
} |
469 |
|
|
470 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
471 |
{ |
int *temp_dquants = |
472 |
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 * |
473 |
|
pEnc->mbParam.mb_height * sizeof(int), |
474 |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
CACHE_LINE); |
475 |
pEnc->mbParam.edged_width, // stride |
|
476 |
temp_dquants, |
pEnc->current->quant = |
477 |
pEnc->current->quant, |
adaptive_quantization(pEnc->current->image.y, |
478 |
pEnc->current->quant, // min_quant |
pEnc->mbParam.edged_width, temp_dquants, |
479 |
2*pEnc->current->quant, // max_quant |
pEnc->current->quant, pEnc->current->quant, |
480 |
|
2 * pEnc->current->quant, |
481 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
482 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
483 |
|
|
484 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
485 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
486 |
{ |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
487 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
488 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
489 |
|
|
490 |
|
|
491 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
492 |
|
|
493 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
494 |
|
} |
495 |
|
|
496 |
|
#undef OFFSET |
497 |
} |
} |
498 |
|
|
499 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
500 |
} |
} |
501 |
|
|
503 |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
504 |
write_vol_header = 1; |
write_vol_header = 1; |
505 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
506 |
} |
} else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
507 |
else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
int matrix1_changed, matrix2_changed; |
|
int ret1, ret2; |
|
508 |
|
|
509 |
ret1 = ret2 = 0; |
matrix1_changed = matrix2_changed = 0; |
510 |
|
|
511 |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
512 |
write_vol_header = 1; |
write_vol_header = 1; |
515 |
|
|
516 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
517 |
if(pFrame->quant_intra_matrix != NULL) |
if(pFrame->quant_intra_matrix != NULL) |
518 |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); |
519 |
if(pFrame->quant_inter_matrix != NULL) |
if(pFrame->quant_inter_matrix != NULL) |
520 |
ret2 = set_inter_matrix(pFrame->quant_inter_matrix); |
matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); |
521 |
} |
} else { |
522 |
else { |
matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
523 |
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()); |
|
524 |
} |
} |
525 |
if(write_vol_header == 0) |
if(write_vol_header == 0) |
526 |
write_vol_header = ret1 | ret2; |
write_vol_header = matrix1_changed | matrix2_changed; |
527 |
} |
} |
528 |
|
|
529 |
if (pFrame->intra < 0) |
if (pFrame->intra < 0) { |
530 |
{ |
if ((pEnc->iFrameNum == 0) |
531 |
if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
|| ((pEnc->iMaxKeyInterval > 0) |
532 |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { |
|
|
|
533 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
534 |
else |
} else { |
535 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
536 |
} |
} |
537 |
else |
} else { |
538 |
{ |
if (pFrame->intra == 1) { |
|
if (pFrame->intra == 1) |
|
539 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
540 |
else |
} else { |
541 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
542 |
} |
} |
543 |
|
|
544 |
|
} |
545 |
|
|
546 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
547 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
548 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
549 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
550 |
|
|
551 |
if (pResult) |
if (pResult) { |
|
{ |
|
552 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
553 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
554 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
556 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
557 |
} |
} |
558 |
|
|
559 |
EMMS(); |
emms(); |
|
|
|
|
if (pFrame->quant == 0) |
|
|
{ |
|
|
RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
|
|
} |
|
560 |
|
|
561 |
#ifdef _DEBUG |
if (pFrame->quant == 0) { |
562 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
RateControlUpdate(&pEnc->rate_control, (int16_t)pEnc->current->quant, |
563 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pFrame->length, pFrame->intra); |
564 |
|
} |
565 |
|
#ifdef _DEBUG_PSNR |
566 |
|
psnr = |
567 |
|
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
568 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
569 |
|
pEnc->mbParam.height); |
570 |
|
|
571 |
sprintf(temp, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
572 |
DEBUG(temp); |
DEBUG(temp); |
573 |
#endif |
#endif |
574 |
|
|
575 |
|
inc_frame_num(pEnc); |
576 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
577 |
|
|
578 |
stop_global_timer(); |
stop_global_timer(); |
582 |
} |
} |
583 |
|
|
584 |
|
|
585 |
static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { |
static __inline void |
586 |
|
CodeIntraMB(Encoder * pEnc, |
587 |
|
MACROBLOCK * pMB) |
588 |
|
{ |
589 |
|
|
590 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
591 |
|
|
596 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
597 |
|
|
598 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
599 |
if(pMB->dquant != NO_CHANGE) |
if (pMB->dquant != NO_CHANGE) { |
|
{ |
|
600 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
601 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
602 |
|
|
603 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
604 |
if (pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
605 |
|
if (pEnc->current->quant < 1) |
606 |
|
pEnc->current->quant = 1; |
607 |
} |
} |
608 |
} |
} |
609 |
|
|
614 |
#define FCODEBITS 3 |
#define FCODEBITS 3 |
615 |
#define MODEBITS 5 |
#define MODEBITS 5 |
616 |
|
|
617 |
void HintedMESet(Encoder * pEnc, int * intra) |
void |
618 |
|
HintedMESet(Encoder * pEnc, |
619 |
|
int *intra) |
620 |
{ |
{ |
621 |
HINTINFO * hint; |
HINTINFO * hint; |
622 |
Bitstream bs; |
Bitstream bs; |
625 |
|
|
626 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
627 |
|
|
628 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
629 |
*intra = hint->mvhint.intra; |
*intra = hint->mvhint.intra; |
630 |
} |
} else { |
|
else |
|
|
{ |
|
631 |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
632 |
*intra = BitstreamGetBit(&bs); |
*intra = BitstreamGetBit(&bs); |
633 |
} |
} |
634 |
|
|
635 |
if (*intra) |
if (*intra) { |
|
{ |
|
636 |
return; |
return; |
637 |
} |
} |
638 |
|
|
639 |
pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
pEnc->current->fcode = (hint->rawhints != 0) ? |
640 |
|
(uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
641 |
|
|
642 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
643 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
644 |
|
|
645 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
646 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
647 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
648 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
649 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
650 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
651 |
VECTOR pred[4]; |
VECTOR pred; |
652 |
VECTOR tmp; |
VECTOR tmp; |
|
int32_t dummy[4]; |
|
653 |
int vec; |
int vec; |
654 |
|
|
655 |
pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
pMB->mode = (hint->rawhints != 0) ? |
656 |
|
(uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
657 |
|
|
658 |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
659 |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
660 |
|
|
661 |
if (pMB->mode == MODE_INTER) |
if (pMB->mode == MODE_INTER) { |
662 |
{ |
tmp.x = (hint->rawhints) ? |
663 |
tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); |
bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length); |
664 |
tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); |
tmp.y = (hint->rawhints) ? |
665 |
|
bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length); |
666 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
667 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
668 |
|
|
669 |
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); |
670 |
|
|
671 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
672 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
673 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
674 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x; |
675 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y; |
|
} |
|
676 |
} |
} |
677 |
else if (pMB->mode == MODE_INTER4V) |
} else if (pMB->mode == MODE_INTER4V) { |
678 |
{ |
for (vec = 0; vec < 4; ++vec) { |
679 |
for (vec=0 ; vec<4 ; ++vec) |
tmp.x = (hint->rawhints) ? |
680 |
{ |
bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length); |
681 |
tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); |
tmp.y = (hint->rawhints) ? |
682 |
tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); |
bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length); |
683 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
684 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
685 |
|
|
686 |
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); |
687 |
|
|
688 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
689 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
690 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
691 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; |
692 |
} |
} |
693 |
} |
} else /* intra / stuffing / not_coded */ |
|
else // intra / stuffing / not_coded |
|
|
{ |
|
|
for (vec=0 ; vec<4 ; ++vec) |
|
694 |
{ |
{ |
695 |
|
for (vec = 0; vec < 4; ++vec) { |
696 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
697 |
} |
} |
698 |
} |
} |
699 |
|
|
700 |
if (pMB->mode == MODE_INTER4V && |
if (pMB->mode == MODE_INTER4V && |
701 |
(pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) |
(pEnc->current->global_flags & XVID_LUMIMASKING) |
702 |
{ |
&& pMB->dquant != NO_CHANGE) { |
703 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
704 |
|
|
705 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
706 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
707 |
} |
} |
708 |
} |
} |
711 |
} |
} |
712 |
|
|
713 |
|
|
714 |
void HintedMEGet(Encoder * pEnc, int intra) |
void |
715 |
|
HintedMEGet(Encoder * pEnc, |
716 |
|
int intra) |
717 |
{ |
{ |
718 |
HINTINFO * hint; |
HINTINFO * hint; |
719 |
Bitstream bs; |
Bitstream bs; |
722 |
|
|
723 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
724 |
|
|
725 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
726 |
hint->mvhint.intra = intra; |
hint->mvhint.intra = intra; |
727 |
} |
} else { |
|
else |
|
|
{ |
|
728 |
BitstreamInit(&bs, hint->hintstream, 0); |
BitstreamInit(&bs, hint->hintstream, 0); |
729 |
BitstreamPutBit(&bs, intra); |
BitstreamPutBit(&bs, intra); |
730 |
} |
} |
731 |
|
|
732 |
if (intra) |
if (intra) { |
733 |
{ |
if (!hint->rawhints) { |
|
if (!hint->rawhints) |
|
|
{ |
|
734 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
735 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
736 |
} |
} |
740 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
741 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
742 |
|
|
743 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
744 |
hint->mvhint.fcode = pEnc->current->fcode; |
hint->mvhint.fcode = pEnc->current->fcode; |
745 |
} |
} else { |
|
else |
|
|
{ |
|
746 |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
747 |
} |
} |
748 |
|
|
749 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
750 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
751 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
752 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
753 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
754 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
755 |
VECTOR tmp; |
VECTOR tmp; |
756 |
|
|
757 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
758 |
bhint->mode = pMB->mode; |
bhint->mode = pMB->mode; |
759 |
} |
} else { |
|
else |
|
|
{ |
|
760 |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
761 |
} |
} |
762 |
|
|
763 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
764 |
tmp.x = pMB->mvs[0].x; |
tmp.x = pMB->mvs[0].x; |
765 |
tmp.y = pMB->mvs[0].y; |
tmp.y = pMB->mvs[0].y; |
766 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
767 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
768 |
|
|
769 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
770 |
bhint->mvs[0].x = tmp.x; |
bhint->mvs[0].x = tmp.x; |
771 |
bhint->mvs[0].y = tmp.y; |
bhint->mvs[0].y = tmp.y; |
772 |
} |
} else { |
|
else |
|
|
{ |
|
773 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
774 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
775 |
} |
} |
776 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
|
else if (pMB->mode == MODE_INTER4V) |
|
|
{ |
|
777 |
int vec; |
int vec; |
778 |
|
|
779 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
780 |
tmp.x = pMB->mvs[vec].x; |
tmp.x = pMB->mvs[vec].x; |
781 |
tmp.y = pMB->mvs[vec].y; |
tmp.y = pMB->mvs[vec].y; |
782 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
783 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
784 |
|
|
785 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
786 |
bhint->mvs[vec].x = tmp.x; |
bhint->mvs[vec].x = tmp.x; |
787 |
bhint->mvs[vec].y = tmp.y; |
bhint->mvs[vec].y = tmp.y; |
788 |
} |
} else { |
|
else |
|
|
{ |
|
789 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
790 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
791 |
} |
} |
794 |
} |
} |
795 |
} |
} |
796 |
|
|
797 |
if (!hint->rawhints) |
if (!hint->rawhints) { |
|
{ |
|
798 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
799 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
800 |
} |
} |
801 |
} |
} |
802 |
|
|
803 |
|
|
804 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
static int |
805 |
|
FrameCodeI(Encoder * pEnc, |
806 |
|
Bitstream * bs, |
807 |
|
uint32_t * pBits) |
808 |
{ |
{ |
809 |
|
|
810 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
818 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
819 |
|
|
820 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
821 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
|
822 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
823 |
|
|
824 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
825 |
|
|
828 |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
829 |
|
|
830 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
831 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
832 |
{ |
MACROBLOCK *pMB = |
833 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
834 |
|
|
835 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
836 |
|
|
837 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
838 |
|
dct_codes, qcoeff); |
839 |
|
|
840 |
start_timer(); |
start_timer(); |
841 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
842 |
stop_prediction_timer(); |
stop_prediction_timer(); |
843 |
|
|
844 |
start_timer(); |
start_timer(); |
845 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
846 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
847 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
848 |
|
qcoeff[5*64+0]=0; |
849 |
|
} |
850 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
851 |
stop_coding_timer(); |
stop_coding_timer(); |
852 |
} |
} |
859 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
860 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
861 |
|
|
862 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
863 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
864 |
} |
} |
865 |
|
|
866 |
return 1; // intra |
return 1; /* intra */ |
867 |
} |
} |
868 |
|
|
869 |
|
|
870 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
871 |
|
|
872 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) |
static int |
873 |
|
FrameCodeP(Encoder * pEnc, |
874 |
|
Bitstream * bs, |
875 |
|
uint32_t * pBits, |
876 |
|
bool force_inter, |
877 |
|
bool vol_header) |
878 |
{ |
{ |
879 |
float fSigma; |
float fSigma; |
880 |
|
|
882 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
883 |
|
|
884 |
int iLimit; |
int iLimit; |
885 |
uint32_t x, y; |
unsigned int x, y; |
886 |
int iSearchRange; |
int iSearchRange; |
887 |
int bIntra; |
int bIntra; |
888 |
|
|
890 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
891 |
|
|
892 |
start_timer(); |
start_timer(); |
893 |
image_setedges(pRef, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
894 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.width, pEnc->mbParam.height); |
|
pEnc->mbParam.edged_height, |
|
|
pEnc->mbParam.width, |
|
|
pEnc->mbParam.height, |
|
|
pEnc->current->global_flags & XVID_INTERLACING); |
|
895 |
stop_edges_timer(); |
stop_edges_timer(); |
896 |
|
|
897 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
899 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
900 |
|
|
901 |
if (!force_inter) |
if (!force_inter) |
902 |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
iLimit = |
903 |
|
(int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * |
904 |
|
INTRA_THRESHOLD); |
905 |
else |
else |
906 |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
907 |
|
|
908 |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
909 |
start_timer(); |
start_timer(); |
910 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
911 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
912 |
|
pEnc->mbParam.edged_height, |
913 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
914 |
stop_inter_timer(); |
stop_inter_timer(); |
915 |
} |
} |
916 |
|
|
917 |
start_timer(); |
start_timer(); |
918 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
|
{ |
|
919 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
920 |
} |
} else { |
921 |
else |
|
922 |
{ |
bIntra = |
923 |
bIntra = MotionEstimation( |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
924 |
&pEnc->mbParam, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
pEnc->current, |
|
|
pEnc->reference, |
|
|
&pEnc->vInterH, |
|
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
925 |
iLimit); |
iLimit); |
926 |
|
|
927 |
} |
} |
928 |
stop_motion_timer(); |
stop_motion_timer(); |
929 |
|
|
930 |
if (bIntra == 1) |
if (bIntra == 1) { |
|
{ |
|
931 |
return FrameCodeI(pEnc, bs, pBits); |
return FrameCodeI(pEnc, bs, pBits); |
932 |
} |
} |
933 |
|
|
936 |
if(vol_header) |
if(vol_header) |
937 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
938 |
|
|
939 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
940 |
|
|
941 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
942 |
|
|
945 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
946 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
947 |
|
|
948 |
for(y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
949 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
950 |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
MACROBLOCK *pMB = |
951 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
952 |
|
|
953 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
954 |
|
|
955 |
if (!bIntra) |
if (!bIntra) { |
|
{ |
|
956 |
start_timer(); |
start_timer(); |
957 |
MBMotionCompensation(pMB, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
958 |
x, y, |
&pEnc->vInterH, &pEnc->vInterV, |
959 |
&pEnc->reference->image, |
&pEnc->vInterHV, &pEnc->current->image, |
960 |
&pEnc->vInterH, |
dct_codes, pEnc->mbParam.width, |
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
|
&pEnc->current->image, |
|
|
dct_codes, |
|
|
pEnc->mbParam.width, |
|
961 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
962 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
963 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
967 |
if(pMB->dquant != NO_CHANGE) { |
if(pMB->dquant != NO_CHANGE) { |
968 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
969 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
970 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
971 |
else if(pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
972 |
|
else if (pEnc->current->quant < 1) |
973 |
|
pEnc->current->quant = 1; |
974 |
} |
} |
975 |
} |
} |
976 |
pMB->quant = pEnc->current->quant; |
pMB->quant = pEnc->current->quant; |
977 |
|
|
978 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
979 |
|
|
980 |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
pMB->cbp = |
981 |
} |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
982 |
else |
dct_codes, qcoeff); |
983 |
{ |
} else { |
984 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
985 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
986 |
} |
dct_codes, qcoeff); |
987 |
|
|
988 |
start_timer(); |
start_timer(); |
989 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
990 |
stop_prediction_timer(); |
stop_prediction_timer(); |
991 |
|
} |
992 |
|
|
993 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
{ |
|
994 |
pEnc->sStat.kblks++; |
pEnc->sStat.kblks++; |
995 |
} |
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
996 |
else if (pMB->cbp || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
997 |
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) |
|
|
{ |
|
998 |
pEnc->sStat.mblks++; |
pEnc->sStat.mblks++; |
999 |
} |
} else { |
|
else |
|
|
{ |
|
1000 |
pEnc->sStat.ublks++; |
pEnc->sStat.ublks++; |
1001 |
} |
} |
1002 |
|
|
1003 |
start_timer(); |
start_timer(); |
1004 |
|
|
1005 |
|
/* Finished processing the MB, now check if to CODE or SKIP */ |
1006 |
|
|
1007 |
|
if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
1008 |
|
pMB->mvs[0].y == 0) { |
1009 |
|
|
1010 |
|
MBSkip(bs); /* without B-frames, no precautions are needed */ |
1011 |
|
|
1012 |
|
} |
1013 |
|
else { |
1014 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) { |
1015 |
|
pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1016 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1017 |
|
qcoeff[5*64+0]=0; |
1018 |
|
} |
1019 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1020 |
|
} |
1021 |
|
|
1022 |
stop_coding_timer(); |
stop_coding_timer(); |
1023 |
} |
} |
1024 |
} |
} |
1025 |
|
|
1026 |
emms(); |
emms(); |
1027 |
|
|
1028 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1029 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
1030 |
} |
} |
1031 |
|
|
1037 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
1038 |
|
|
1039 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
1040 |
&& (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 |
&& (pEnc->mbParam.m_fcode <= 3)) /* maximum search range 128 */ |
1041 |
{ |
{ |
1042 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1043 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1044 |
} |
} else if ((fSigma < iSearchRange / 6) |
|
else if ((fSigma < iSearchRange / 6) |
|
1045 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
1046 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
1047 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= 2)) /* minimum search range 16 */ |
1048 |
{ |
{ |
1049 |
pEnc->mbParam.m_fcode--; |
pEnc->mbParam.m_fcode--; |
1050 |
iSearchRange /= 2; |
iSearchRange /= 2; |
1054 |
|
|
1055 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1056 |
|
|
1057 |
return 0; // inter |
return 0; /* inter */ |
|
} |
|
|
|
|
|
|
|
|
#if 0 |
|
|
|
|
|
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) |
|
|
{ |
|
|
int16_t dct_codes[6][64]; |
|
|
int16_t qcoeff[6][64]; |
|
|
uint32_t x, y; |
|
|
VECTOR forward; |
|
|
VECTOR backward; |
|
|
|
|
|
IMAGE *f_ref = &pEnc->reference->image; |
|
|
IMAGE *b_ref = &pEnc->current->image; |
|
|
|
|
|
/* forward */ |
|
|
image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
|
|
start_timer(); |
|
|
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
|
|
stop_inter_timer(); |
|
1058 |
|
|
|
/* backward */ |
|
|
image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
|
|
start_timer(); |
|
|
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
|
|
stop_inter_timer(); |
|
|
|
|
|
start_timer(); |
|
|
MotionEstimationBVOP(&pEnc->mbParam, frame, |
|
|
pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
|
|
pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
|
|
|
|
|
stop_motion_timer(); |
|
|
|
|
|
if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
|
|
{ |
|
|
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
|
1059 |
} |
} |
|
|
|
|
frame->coding_type = B_VOP; |
|
|
BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0, |
|
|
frame->quant, frame->fcode, frame->bcode); |
|
|
|
|
|
*pBits = BitstreamPos(bs); |
|
|
|
|
|
pEnc->sStat.iTextBits = 0; |
|
|
pEnc->sStat.iMvSum = 0; |
|
|
pEnc->sStat.iMvCount = 0; |
|
|
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
|
|
|
|
|
|
|
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
|
|
{ |
|
|
// reset prediction |
|
|
|
|
|
forward.x = 0; |
|
|
forward.y = 0; |
|
|
backward.x = 0; |
|
|
backward.y = 0; |
|
|
|
|
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
|
{ |
|
|
MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
|
|
MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
|
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
|
|
|
|
|
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
|
|
if (mb->mode == MODE_NOT_CODED) |
|
|
{ |
|
|
mb->mvs[0].x = 0; |
|
|
mb->mvs[0].y = 0; |
|
|
continue; |
|
|
} |
|
|
|
|
|
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
|
|
f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
|
|
b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
|
dct_codes); |
|
|
|
|
|
mb->quant = frame->quant; |
|
|
mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff); |
|
|
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
|
|
|
|
|
|
|
|
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && |
|
|
mb->cbp == 0 && |
|
|
mb->mvs[0].x == 0 && |
|
|
mb->mvs[0].y == 0) |
|
|
{ |
|
|
mb->mode = 5; // skipped |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
|
|
{ |
|
|
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
|
|
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
|
|
forward.x = mb->mvs[0].x; |
|
|
forward.y = mb->mvs[0].y; |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
|
|
{ |
|
|
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
|
|
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
|
|
backward.x = mb->b_mvs[0].x; |
|
|
backward.y = mb->b_mvs[0].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); |
|
|
*/ |
|
|
|
|
|
start_timer(); |
|
|
MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat); |
|
|
stop_coding_timer(); |
|
|
} |
|
|
} |
|
|
|
|
|
emms(); |
|
|
|
|
|
// TODO: dynamic fcode/bcode ??? |
|
|
|
|
|
*pBits = BitstreamPos(bs) - *pBits; |
|
|
|
|
|
} |
|
|
|
|
|
#endif |
|