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 |
|
* 08.05.2002 fix some problem in DEBUG mode; |
36 |
|
* MinChen <chenm001@163.com> |
37 |
|
* 14.04.2002 added FrameCodeB() |
38 |
|
* |
39 |
|
* $Id: encoder.c,v 1.43 2002-06-14 13:21:35 Isibaar Exp $ |
40 |
|
* |
41 |
|
****************************************************************************/ |
42 |
|
|
43 |
#include <stdlib.h> |
#include <stdlib.h> |
44 |
#include <stdio.h> |
#include <stdio.h> |
45 |
#include <math.h> |
#include <math.h> |
46 |
|
#include <string.h> |
47 |
|
|
48 |
#include "encoder.h" |
#include "encoder.h" |
49 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
50 |
#include "global.h" |
#include "global.h" |
51 |
#include "utils/timer.h" |
#include "utils/timer.h" |
52 |
#include "image/image.h" |
#include "image/image.h" |
53 |
|
#include "motion/motion.h" |
54 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
55 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
56 |
#include "bitstream/bitstream.h" |
#include "bitstream/bitstream.h" |
62 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
63 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
64 |
|
|
65 |
|
/***************************************************************************** |
66 |
|
* Local macros |
67 |
|
****************************************************************************/ |
68 |
|
|
69 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
70 |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
71 |
|
|
72 |
|
/***************************************************************************** |
73 |
|
* Local function prototypes |
74 |
|
****************************************************************************/ |
75 |
|
|
76 |
|
static int FrameCodeI(Encoder * pEnc, |
77 |
|
Bitstream * bs, |
78 |
|
uint32_t * pBits); |
79 |
|
|
80 |
|
static int FrameCodeP(Encoder * pEnc, |
81 |
|
Bitstream * bs, |
82 |
|
uint32_t * pBits, |
83 |
|
bool force_inter, |
84 |
|
bool vol_header); |
85 |
|
|
86 |
|
#ifdef BFRAMES |
87 |
|
static void FrameCodeB(Encoder * pEnc, |
88 |
|
FRAMEINFO * frame, |
89 |
|
Bitstream * bs, |
90 |
|
uint32_t * pBits); |
91 |
|
#endif |
92 |
|
|
93 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); |
/***************************************************************************** |
94 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header); |
* Local data |
95 |
|
****************************************************************************/ |
96 |
|
|
97 |
static int DQtab[4] = |
static int DQtab[4] = { |
|
{ |
|
98 |
-1, -2, 1, 2 |
-1, -2, 1, 2 |
99 |
}; |
}; |
100 |
|
|
101 |
static int iDQtab[5] = |
static int iDQtab[5] = { |
|
{ |
|
102 |
1, 0, NO_CHANGE, 2, 3 |
1, 0, NO_CHANGE, 2, 3 |
103 |
}; |
}; |
104 |
|
|
105 |
|
|
106 |
void static image_null(IMAGE * image) |
static void __inline |
107 |
|
image_null(IMAGE * image) |
108 |
{ |
{ |
109 |
image->y = image->u = image->v = NULL; |
image->y = image->u = image->v = NULL; |
110 |
} |
} |
111 |
|
|
112 |
|
|
113 |
int encoder_create(XVID_ENC_PARAM * pParam) |
/***************************************************************************** |
114 |
|
* Encoder creation |
115 |
|
* |
116 |
|
* This function creates an Encoder instance, it allocates all necessary |
117 |
|
* image buffers (reference, current and bframes) and initialize the internal |
118 |
|
* xvid encoder paremeters according to the XVID_ENC_PARAM input parameter. |
119 |
|
* |
120 |
|
* The code seems to be very long but is very basic, mainly memory allocation |
121 |
|
* and cleaning code. |
122 |
|
* |
123 |
|
* Returned values : |
124 |
|
* - XVID_ERR_OK - no errors |
125 |
|
* - XVID_ERR_MEMORY - the libc could not allocate memory, the function |
126 |
|
* cleans the structure before exiting. |
127 |
|
* pParam->handle is also set to NULL. |
128 |
|
* |
129 |
|
****************************************************************************/ |
130 |
|
|
131 |
|
int |
132 |
|
encoder_create(XVID_ENC_PARAM * pParam) |
133 |
{ |
{ |
134 |
Encoder *pEnc; |
Encoder *pEnc; |
135 |
uint32_t i; |
uint32_t i; |
143 |
ENC_CHECK(!(pParam->width % 2)); |
ENC_CHECK(!(pParam->width % 2)); |
144 |
ENC_CHECK(!(pParam->height % 2)); |
ENC_CHECK(!(pParam->height % 2)); |
145 |
|
|
146 |
if (pParam->fincr <= 0 || pParam->fbase <= 0) |
/* Fps */ |
147 |
{ |
|
148 |
|
if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
149 |
pParam->fincr = 1; |
pParam->fincr = 1; |
150 |
pParam->fbase = 25; |
pParam->fbase = 25; |
151 |
} |
} |
152 |
|
|
153 |
// simplify the "fincr/fbase" fraction |
/* |
154 |
// (neccessary, since windows supplies us with huge numbers) |
* Simplify the "fincr/fbase" fraction |
155 |
|
* (neccessary, since windows supplies us with huge numbers) |
156 |
|
*/ |
157 |
|
|
158 |
i = pParam->fincr; |
i = pParam->fincr; |
159 |
while (i > 1) |
while (i > 1) { |
160 |
{ |
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { |
|
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) |
|
|
{ |
|
161 |
pParam->fincr /= i; |
pParam->fincr /= i; |
162 |
pParam->fbase /= i; |
pParam->fbase /= i; |
163 |
i = pParam->fincr; |
i = pParam->fincr; |
166 |
i--; |
i--; |
167 |
} |
} |
168 |
|
|
169 |
if (pParam->fbase > 65535) |
if (pParam->fbase > 65535) { |
|
{ |
|
170 |
float div = (float)pParam->fbase / 65535; |
float div = (float)pParam->fbase / 65535; |
171 |
|
|
172 |
pParam->fbase = (int)(pParam->fbase / div); |
pParam->fbase = (int)(pParam->fbase / div); |
173 |
pParam->fincr = (int)(pParam->fincr / div); |
pParam->fincr = (int)(pParam->fincr / div); |
174 |
} |
} |
175 |
|
|
176 |
|
/* Bitrate allocator defaults */ |
177 |
|
|
178 |
if (pParam->rc_bitrate <= 0) |
if (pParam->rc_bitrate <= 0) |
179 |
pParam->rc_bitrate = 900000; |
pParam->rc_bitrate = 900000; |
180 |
|
|
187 |
if (pParam->rc_buffer <= 0) |
if (pParam->rc_buffer <= 0) |
188 |
pParam->rc_buffer = 100; |
pParam->rc_buffer = 100; |
189 |
|
|
190 |
|
/* Max and min quantizers */ |
191 |
|
|
192 |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
193 |
pParam->min_quantizer = 1; |
pParam->min_quantizer = 1; |
194 |
|
|
195 |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
196 |
pParam->max_quantizer = 31; |
pParam->max_quantizer = 31; |
197 |
|
|
|
if (pParam->max_key_interval == 0) /* 1 keyframe each 10 seconds */ |
|
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
|
|
|
|
198 |
if (pParam->max_quantizer < pParam->min_quantizer) |
if (pParam->max_quantizer < pParam->min_quantizer) |
199 |
pParam->max_quantizer = pParam->min_quantizer; |
pParam->max_quantizer = pParam->min_quantizer; |
200 |
|
|
201 |
if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL) |
/* 1 keyframe each 10 seconds */ |
202 |
|
|
203 |
|
if (pParam->max_key_interval == 0) |
204 |
|
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
205 |
|
|
206 |
|
|
207 |
|
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
208 |
|
if (pEnc == NULL) |
209 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
210 |
|
|
211 |
|
/* Zero the Encoder Structure */ |
212 |
|
|
213 |
|
memset(pEnc, 0, sizeof(Encoder)); |
214 |
|
|
215 |
/* Fill members of Encoder structure */ |
/* Fill members of Encoder structure */ |
216 |
|
|
217 |
pEnc->mbParam.width = pParam->width; |
pEnc->mbParam.width = pParam->width; |
223 |
pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
224 |
pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; |
pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; |
225 |
|
|
226 |
|
pEnc->mbParam.fbase = pParam->fbase; |
227 |
|
pEnc->mbParam.fincr = pParam->fincr; |
228 |
|
|
229 |
|
pEnc->mbParam.m_quant_type = H263_QUANT; |
230 |
|
|
231 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->sStat.fMvPrevSigma = -1; |
232 |
|
|
233 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
239 |
|
|
240 |
/* try to allocate frame memory */ |
/* try to allocate frame memory */ |
241 |
|
|
242 |
pEnc->current = NULL; |
pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
243 |
pEnc->reference = NULL; |
pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
|
if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL || |
|
|
(pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL) |
|
|
{ |
|
|
if (pEnc->current) xvid_free(pEnc->current); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
244 |
|
|
245 |
/* try to allocate mb memory */ |
if (pEnc->current == NULL || pEnc->reference == NULL) |
246 |
|
goto xvid_err_memory1; |
247 |
|
|
248 |
pEnc->current->mbs = NULL; |
/* try to allocate mb memory */ |
|
pEnc->reference->mbs = NULL; |
|
249 |
|
|
250 |
#ifdef _DEBUG |
pEnc->current->mbs = |
251 |
#ifdef WIN32 |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
252 |
OutputDebugString("malloc mbs"); |
pEnc->mbParam.mb_height, CACHE_LINE); |
253 |
#endif |
pEnc->reference->mbs = |
254 |
#endif |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
255 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
256 |
|
|
257 |
if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL || |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
258 |
(pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) |
goto xvid_err_memory2; |
|
{ |
|
|
if (pEnc->current->mbs) xvid_free(pEnc->current->mbs); |
|
|
xvid_free(pEnc->current); |
|
|
xvid_free(pEnc->reference); |
|
|
xvid_free(pEnc); |
|
|
} |
|
259 |
|
|
260 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
261 |
|
|
262 |
#ifdef _DEBUG |
#ifdef _DEBUG |
263 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
264 |
#endif |
#endif |
265 |
|
#ifdef BFRAMES |
266 |
|
image_null(&pEnc->f_refh); |
267 |
|
image_null(&pEnc->f_refv); |
268 |
|
image_null(&pEnc->f_refhv); |
269 |
|
#endif |
270 |
image_null(&pEnc->current->image); |
image_null(&pEnc->current->image); |
271 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
272 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
276 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
277 |
|
|
278 |
#ifdef _DEBUG |
#ifdef _DEBUG |
279 |
#ifdef WIN32 |
if (image_create |
280 |
OutputDebugString("malloc images"); |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
281 |
|
pEnc->mbParam.edged_height) < 0) |
282 |
|
goto xvid_err_memory3; |
283 |
#endif |
#endif |
284 |
|
#ifdef BFRAMES |
285 |
|
if (image_create |
286 |
|
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
287 |
|
pEnc->mbParam.edged_height) < 0) |
288 |
|
goto xvid_err_memory3; |
289 |
|
if (image_create |
290 |
|
(&pEnc->f_refv, pEnc->mbParam.edged_width, |
291 |
|
pEnc->mbParam.edged_height) < 0) |
292 |
|
goto xvid_err_memory3; |
293 |
|
if (image_create |
294 |
|
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
295 |
|
pEnc->mbParam.edged_height) < 0) |
296 |
|
goto xvid_err_memory3; |
297 |
#endif |
#endif |
298 |
if ( |
if (image_create |
299 |
#ifdef _DEBUG |
(&pEnc->current->image, pEnc->mbParam.edged_width, |
300 |
image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
pEnc->mbParam.edged_height) < 0) |
301 |
|
goto xvid_err_memory3; |
302 |
|
if (image_create |
303 |
|
(&pEnc->reference->image, pEnc->mbParam.edged_width, |
304 |
|
pEnc->mbParam.edged_height) < 0) |
305 |
|
goto xvid_err_memory3; |
306 |
|
if (image_create |
307 |
|
(&pEnc->vInterH, pEnc->mbParam.edged_width, |
308 |
|
pEnc->mbParam.edged_height) < 0) |
309 |
|
goto xvid_err_memory3; |
310 |
|
if (image_create |
311 |
|
(&pEnc->vInterV, pEnc->mbParam.edged_width, |
312 |
|
pEnc->mbParam.edged_height) < 0) |
313 |
|
goto xvid_err_memory3; |
314 |
|
if (image_create |
315 |
|
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
316 |
|
pEnc->mbParam.edged_height) < 0) |
317 |
|
goto xvid_err_memory3; |
318 |
|
if (image_create |
319 |
|
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
320 |
|
pEnc->mbParam.edged_height) < 0) |
321 |
|
goto xvid_err_memory3; |
322 |
|
if (image_create |
323 |
|
(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
324 |
|
pEnc->mbParam.edged_height) < 0) |
325 |
|
goto xvid_err_memory3; |
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
/* B Frames specific init */ |
330 |
|
#ifdef BFRAMES |
331 |
|
|
332 |
|
pEnc->mbParam.max_bframes = pParam->max_bframes; |
333 |
|
pEnc->bquant_ratio = pParam->bquant_ratio; |
334 |
|
pEnc->bframes = NULL; |
335 |
|
|
336 |
|
if (pEnc->mbParam.max_bframes > 0) { |
337 |
|
int n; |
338 |
|
|
339 |
|
pEnc->bframes = |
340 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), |
341 |
|
CACHE_LINE); |
342 |
|
|
343 |
|
if (pEnc->bframes == NULL) |
344 |
|
goto xvid_err_memory3; |
345 |
|
|
346 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
347 |
|
pEnc->bframes[n] = NULL; |
348 |
|
|
349 |
|
|
350 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
351 |
|
pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
352 |
|
|
353 |
|
if (pEnc->bframes[n] == NULL) |
354 |
|
goto xvid_err_memory4; |
355 |
|
|
356 |
|
pEnc->bframes[n]->mbs = |
357 |
|
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
358 |
|
pEnc->mbParam.mb_height, CACHE_LINE); |
359 |
|
|
360 |
|
if (pEnc->bframes[n]->mbs == NULL) |
361 |
|
goto xvid_err_memory4; |
362 |
|
|
363 |
|
image_null(&pEnc->bframes[n]->image); |
364 |
|
|
365 |
|
if (image_create |
366 |
|
(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, |
367 |
|
pEnc->mbParam.edged_height) < 0) |
368 |
|
goto xvid_err_memory4; |
369 |
|
|
370 |
|
} |
371 |
|
} |
372 |
|
|
373 |
|
pEnc->bframenum_head = 0; |
374 |
|
pEnc->bframenum_tail = 0; |
375 |
|
pEnc->flush_bframes = 0; |
376 |
|
|
377 |
|
pEnc->mbParam.m_seconds = 0; |
378 |
|
pEnc->mbParam.m_ticks = 0; |
379 |
#endif |
#endif |
380 |
image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
381 |
image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
pParam->handle = (void *) pEnc; |
382 |
image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
|
383 |
image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
if (pParam->rc_bitrate) { |
384 |
image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, |
385 |
image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
pParam->rc_reaction_delay_factor, |
386 |
image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
pParam->rc_averaging_period, pParam->rc_buffer, |
387 |
{ |
pParam->fbase * 1000 / pParam->fincr, |
388 |
|
pParam->max_quantizer, pParam->min_quantizer); |
389 |
|
} |
390 |
|
|
391 |
|
init_timer(); |
392 |
|
|
393 |
|
return XVID_ERR_OK; |
394 |
|
|
395 |
|
/* |
396 |
|
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
397 |
|
*/ |
398 |
|
#ifdef BFRAMES |
399 |
|
xvid_err_memory4: |
400 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
401 |
|
|
402 |
|
if (pEnc->bframes[i] == NULL) |
403 |
|
continue; |
404 |
|
|
405 |
|
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
406 |
|
pEnc->mbParam.edged_height); |
407 |
|
|
408 |
|
xvid_free(pEnc->bframes[i]->mbs); |
409 |
|
|
410 |
|
xvid_free(pEnc->bframes[i]); |
411 |
|
|
412 |
|
} |
413 |
|
|
414 |
|
xvid_free(pEnc->bframes); |
415 |
|
|
416 |
|
#endif |
417 |
|
|
418 |
|
xvid_err_memory3: |
419 |
#ifdef _DEBUG |
#ifdef _DEBUG |
420 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
421 |
|
pEnc->mbParam.edged_height); |
422 |
|
#endif |
423 |
|
|
424 |
|
#ifdef BFRAMES |
425 |
|
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
426 |
|
pEnc->mbParam.edged_height); |
427 |
|
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
428 |
|
pEnc->mbParam.edged_height); |
429 |
|
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
430 |
|
pEnc->mbParam.edged_height); |
431 |
#endif |
#endif |
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
432 |
|
|
433 |
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
434 |
|
pEnc->mbParam.edged_height); |
435 |
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
436 |
|
pEnc->mbParam.edged_height); |
437 |
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
438 |
|
pEnc->mbParam.edged_height); |
439 |
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
440 |
|
pEnc->mbParam.edged_height); |
441 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
442 |
|
pEnc->mbParam.edged_height); |
443 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
444 |
|
pEnc->mbParam.edged_height); |
445 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
446 |
|
pEnc->mbParam.edged_height); |
447 |
|
|
448 |
|
xvid_err_memory2: |
449 |
|
xvid_free(pEnc->current->mbs); |
450 |
|
xvid_free(pEnc->reference->mbs); |
451 |
|
|
452 |
|
xvid_err_memory1: |
453 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
454 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
455 |
xvid_free(pEnc); |
xvid_free(pEnc); |
456 |
|
|
457 |
|
pParam->handle = NULL; |
458 |
|
|
459 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
460 |
} |
} |
461 |
|
|
462 |
pParam->handle = (void *)pEnc; |
/***************************************************************************** |
463 |
|
* Encoder destruction |
464 |
|
* |
465 |
|
* This function destroy the entire encoder structure created by a previous |
466 |
|
* successful encoder_create call. |
467 |
|
* |
468 |
|
* Returned values (for now only one returned value) : |
469 |
|
* - XVID_ERR_OK - no errors |
470 |
|
* |
471 |
|
****************************************************************************/ |
472 |
|
|
473 |
if (pParam->rc_bitrate) |
int |
474 |
|
encoder_destroy(Encoder * pEnc) |
475 |
{ |
{ |
476 |
RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor, |
ENC_CHECK(pEnc); |
|
pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr, |
|
|
pParam->max_quantizer, pParam->min_quantizer); |
|
|
} |
|
477 |
|
|
478 |
init_timer(); |
/* B Frames specific */ |
479 |
|
#ifdef BFRAMES |
480 |
|
int i; |
481 |
|
|
482 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
483 |
|
|
484 |
|
if (pEnc->bframes[i] == NULL) |
485 |
|
continue; |
486 |
|
|
487 |
|
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
488 |
|
pEnc->mbParam.edged_height); |
489 |
|
|
490 |
|
xvid_free(pEnc->bframes[i]->mbs); |
491 |
|
|
492 |
|
xvid_free(pEnc->bframes[i]); |
493 |
|
|
|
return XVID_ERR_OK; |
|
494 |
} |
} |
495 |
|
|
496 |
|
xvid_free(pEnc->bframes); |
497 |
|
|
498 |
int encoder_destroy(Encoder * pEnc) |
#endif |
499 |
{ |
|
500 |
ENC_CHECK(pEnc); |
/* All images, reference, current etc ... */ |
501 |
|
|
502 |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
503 |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
504 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
505 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
506 |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
507 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
508 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
509 |
|
pEnc->mbParam.edged_height); |
510 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
511 |
|
pEnc->mbParam.edged_height); |
512 |
|
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
513 |
|
pEnc->mbParam.edged_height); |
514 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
515 |
|
pEnc->mbParam.edged_height); |
516 |
|
#ifdef BFRAMES |
517 |
|
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
518 |
|
pEnc->mbParam.edged_height); |
519 |
|
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
520 |
|
pEnc->mbParam.edged_height); |
521 |
|
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
522 |
|
pEnc->mbParam.edged_height); |
523 |
|
#endif |
524 |
#ifdef _DEBUG |
#ifdef _DEBUG |
525 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
526 |
|
pEnc->mbParam.edged_height); |
527 |
#endif |
#endif |
528 |
|
|
529 |
|
/* Encoder structure */ |
530 |
|
|
531 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
532 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
533 |
|
|
535 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
536 |
|
|
537 |
xvid_free(pEnc); |
xvid_free(pEnc); |
538 |
|
|
539 |
|
return XVID_ERR_OK; |
540 |
|
} |
541 |
|
|
542 |
|
/***************************************************************************** |
543 |
|
* Frame encoder entry point |
544 |
|
* |
545 |
|
* At this moment 2 versions coexist : one for IPB compatible encoder, |
546 |
|
* another one for the old IP encoder. |
547 |
|
* |
548 |
|
* Returned values : |
549 |
|
* - XVID_ERR_OK - no errors |
550 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
551 |
|
* format |
552 |
|
****************************************************************************/ |
553 |
|
|
554 |
|
|
555 |
|
#ifdef BFRAMES |
556 |
|
/***************************************************************************** |
557 |
|
* Frame encoder entry point for IPB capable encoder |
558 |
|
****************************************************************************/ |
559 |
|
int |
560 |
|
encoder_encode(Encoder * pEnc, |
561 |
|
XVID_ENC_FRAME * pFrame, |
562 |
|
XVID_ENC_STATS * pResult) |
563 |
|
{ |
564 |
|
uint16_t x, y; |
565 |
|
Bitstream bs; |
566 |
|
uint32_t bits; |
567 |
|
|
568 |
|
#ifdef _DEBUG |
569 |
|
float psnr; |
570 |
|
char temp[128]; |
571 |
|
#endif |
572 |
|
|
573 |
|
ENC_CHECK(pEnc); |
574 |
|
ENC_CHECK(pFrame); |
575 |
|
|
576 |
|
start_global_timer(); |
577 |
|
|
578 |
|
BitstreamInit(&bs, pFrame->bitstream, 0); |
579 |
|
|
580 |
|
/* |
581 |
|
* bframe "flush" code |
582 |
|
*/ |
583 |
|
|
584 |
|
if ((pFrame->image == NULL || pEnc->flush_bframes) |
585 |
|
&& (pEnc->bframenum_head < pEnc->bframenum_tail)) { |
586 |
|
|
587 |
|
if (pEnc->flush_bframes == 0) { |
588 |
|
/* |
589 |
|
* we have reached the end of stream without getting |
590 |
|
* a future reference frame... so encode last final |
591 |
|
* frame as a pframe |
592 |
|
*/ |
593 |
|
|
594 |
|
/* ToDo : remove dprintf calls */ |
595 |
|
/* |
596 |
|
dprintf("--- PFRAME (final frame correction) --- "); |
597 |
|
*/ |
598 |
|
pEnc->bframenum_tail--; |
599 |
|
SWAP(pEnc->current, pEnc->reference); |
600 |
|
|
601 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
602 |
|
|
603 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
604 |
|
|
605 |
|
BitstreamPad(&bs); |
606 |
|
pFrame->length = BitstreamLength(&bs); |
607 |
|
pFrame->input_consumed = 0; |
608 |
|
pFrame->intra = 0; |
609 |
|
|
610 |
|
return XVID_ERR_OK; |
611 |
|
} |
612 |
|
|
613 |
|
/* ToDo : remove dprintf calls */ |
614 |
|
/* |
615 |
|
dprintf("--- BFRAME (flush) --- "); |
616 |
|
*/ |
617 |
|
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
618 |
|
pEnc->bframenum_head++; |
619 |
|
|
620 |
|
|
621 |
|
BitstreamPad(&bs); |
622 |
|
pFrame->length = BitstreamLength(&bs); |
623 |
|
pFrame->input_consumed = 0; |
624 |
|
pFrame->intra = 0; |
625 |
|
|
626 |
|
return XVID_ERR_OK; |
627 |
|
} |
628 |
|
|
629 |
|
if (pFrame->image == NULL) { |
630 |
|
pFrame->length = 0; |
631 |
|
pFrame->input_consumed = 1; |
632 |
|
pFrame->intra = 0; |
633 |
|
|
634 |
|
return XVID_ERR_OK; |
635 |
|
} |
636 |
|
|
637 |
|
if (pEnc->bframenum_head > 0) { |
638 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
639 |
|
} |
640 |
|
|
641 |
|
pEnc->flush_bframes = 0; |
642 |
|
|
643 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
644 |
|
* Well there was a separation here so i put it in ANSI C |
645 |
|
* comment style :-) |
646 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
647 |
|
|
648 |
|
SWAP(pEnc->current, pEnc->reference); |
649 |
|
|
650 |
|
emms(); |
651 |
|
|
652 |
|
if (pFrame->quant == 0) |
653 |
|
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
654 |
|
else |
655 |
|
pEnc->current->quant = pFrame->quant; |
656 |
|
|
657 |
|
if (pEnc->current->quant < 1) |
658 |
|
pEnc->current->quant = 1; |
659 |
|
|
660 |
|
if (pEnc->current->quant > 31) |
661 |
|
pEnc->current->quant = 31; |
662 |
|
|
663 |
|
pEnc->current->global_flags = pFrame->general; |
664 |
|
pEnc->current->motion_flags = pFrame->motion; |
665 |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
666 |
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
667 |
|
/* ToDo : dynamic fcode (in both directions) */ |
668 |
|
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
669 |
|
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
670 |
|
|
671 |
|
start_timer(); |
672 |
|
if (image_input |
673 |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
674 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
675 |
|
return XVID_ERR_FORMAT; |
676 |
|
stop_conv_timer(); |
677 |
|
|
678 |
|
#ifdef _DEBUG |
679 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
680 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
681 |
|
#endif |
682 |
|
|
683 |
|
emms(); |
684 |
|
|
685 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
686 |
|
* Luminance masking |
687 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
688 |
|
|
689 |
|
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
690 |
|
int *temp_dquants = |
691 |
|
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
692 |
|
pEnc->mbParam.mb_height * sizeof(int), |
693 |
|
CACHE_LINE); |
694 |
|
|
695 |
|
pEnc->current->quant = |
696 |
|
adaptive_quantization(pEnc->current->image.y, |
697 |
|
pEnc->mbParam.edged_width, temp_dquants, |
698 |
|
pEnc->current->quant, pEnc->current->quant, |
699 |
|
2 * pEnc->current->quant, |
700 |
|
pEnc->mbParam.mb_width, |
701 |
|
pEnc->mbParam.mb_height); |
702 |
|
|
703 |
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
704 |
|
|
705 |
|
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
706 |
|
|
707 |
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
708 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
709 |
|
|
710 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
711 |
|
} |
712 |
|
|
713 |
|
#undef OFFSET |
714 |
|
|
715 |
|
} |
716 |
|
|
717 |
|
xvid_free(temp_dquants); |
718 |
|
} |
719 |
|
|
720 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
721 |
|
* ivop/pvop/bvop selection |
722 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
723 |
|
|
724 |
|
|
725 |
|
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || |
726 |
|
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
727 |
|
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
728 |
|
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
729 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
730 |
|
pEnc->mbParam.height) > 30) { |
731 |
|
/* |
732 |
|
* This will be coded as an Intra Frame |
733 |
|
*/ |
734 |
|
|
735 |
|
/* ToDo : Remove dprintf calls */ |
736 |
|
/* |
737 |
|
dprintf("--- IFRAME ---"); |
738 |
|
*/ |
739 |
|
|
740 |
|
FrameCodeI(pEnc, &bs, &bits); |
741 |
|
|
742 |
|
pFrame->intra = 1; |
743 |
|
pEnc->flush_bframes = 1; |
744 |
|
|
745 |
|
/* |
746 |
|
* NB : sequences like "IIBB" decode fine with msfdam but, |
747 |
|
* go screwy with divx 5.00 |
748 |
|
*/ |
749 |
|
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
750 |
|
/* |
751 |
|
* This will be coded as a Predicted Frame |
752 |
|
*/ |
753 |
|
|
754 |
|
/* ToDo : Remove dprintf calls */ |
755 |
|
/* |
756 |
|
dprintf("--- PFRAME ---"); |
757 |
|
*/ |
758 |
|
|
759 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
760 |
|
pFrame->intra = 0; |
761 |
|
pEnc->flush_bframes = 1; |
762 |
|
} else { |
763 |
|
/* |
764 |
|
* This will be coded as a Bidirectional Frame |
765 |
|
*/ |
766 |
|
|
767 |
|
/* ToDo : Remove dprintf calls */ |
768 |
|
/* |
769 |
|
dprintf("--- BFRAME (store) --- head=%i tail=%i", |
770 |
|
pEnc->bframenum_head, |
771 |
|
pEnc->bframenum_tail); |
772 |
|
*/ |
773 |
|
|
774 |
|
if (pFrame->bquant < 1) { |
775 |
|
pEnc->current->quant = |
776 |
|
((pEnc->reference->quant + |
777 |
|
pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
778 |
|
} else { |
779 |
|
pEnc->current->quant = pFrame->bquant; |
780 |
|
} |
781 |
|
|
782 |
|
/* store frame into bframe buffer & swap ref back to current */ |
783 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
784 |
|
SWAP(pEnc->current, pEnc->reference); |
785 |
|
|
786 |
|
pEnc->bframenum_tail++; |
787 |
|
|
788 |
|
pFrame->intra = 0; |
789 |
|
pFrame->length = 0; |
790 |
|
pFrame->input_consumed = 1; |
791 |
|
|
792 |
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
793 |
|
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
794 |
|
pEnc->mbParam.m_seconds++; |
795 |
|
pEnc->mbParam.m_ticks = 0; |
796 |
|
} |
797 |
|
|
798 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
799 |
} |
} |
800 |
|
|
801 |
int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) |
BitstreamPad(&bs); |
802 |
|
pFrame->length = BitstreamLength(&bs); |
803 |
|
|
804 |
|
if (pResult) { |
805 |
|
pResult->quant = pEnc->current->quant; |
806 |
|
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
807 |
|
pResult->kblks = pEnc->sStat.kblks; |
808 |
|
pResult->mblks = pEnc->sStat.mblks; |
809 |
|
pResult->ublks = pEnc->sStat.ublks; |
810 |
|
} |
811 |
|
|
812 |
|
emms(); |
813 |
|
|
814 |
|
#ifdef _DEBUG |
815 |
|
psnr = |
816 |
|
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
817 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
818 |
|
pEnc->mbParam.height); |
819 |
|
|
820 |
|
snprintf(temp, 127, "PSNR: %f\n", psnr); |
821 |
|
DEBUG(temp); |
822 |
|
#endif |
823 |
|
|
824 |
|
if (pFrame->quant == 0) { |
825 |
|
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
826 |
|
pFrame->length, pFrame->intra); |
827 |
|
} |
828 |
|
|
829 |
|
pEnc->iFrameNum++; |
830 |
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
831 |
|
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
832 |
|
pEnc->mbParam.m_seconds++; |
833 |
|
pEnc->mbParam.m_ticks = 0; |
834 |
|
} |
835 |
|
pFrame->input_consumed = 1; |
836 |
|
|
837 |
|
stop_global_timer(); |
838 |
|
write_timer(); |
839 |
|
|
840 |
|
return XVID_ERR_OK; |
841 |
|
} |
842 |
|
#else |
843 |
|
/***************************************************************************** |
844 |
|
* Frame encoder entry point for IP capable encoder |
845 |
|
****************************************************************************/ |
846 |
|
int |
847 |
|
encoder_encode(Encoder * pEnc, |
848 |
|
XVID_ENC_FRAME * pFrame, |
849 |
|
XVID_ENC_STATS * pResult) |
850 |
{ |
{ |
851 |
uint16_t x, y; |
uint16_t x, y; |
852 |
Bitstream bs; |
Bitstream bs; |
853 |
uint32_t bits; |
uint32_t bits; |
854 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
855 |
|
|
856 |
#ifdef _DEBUG |
#ifdef _DEBUG |
857 |
float psnr; |
float psnr; |
858 |
uint8_t temp[100]; |
uint8_t temp[128]; |
859 |
#endif |
#endif |
860 |
|
|
861 |
start_global_timer(); |
start_global_timer(); |
872 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
873 |
|
|
874 |
start_timer(); |
start_timer(); |
875 |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input |
876 |
pFrame->image, pFrame->colorspace)) |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
877 |
{ |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) |
878 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
|
} |
|
879 |
stop_conv_timer(); |
stop_conv_timer(); |
880 |
|
|
881 |
#ifdef _DEBUG |
#ifdef _DEBUG |
882 |
image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
883 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
884 |
#endif |
#endif |
885 |
|
|
886 |
EMMS(); |
emms(); |
887 |
|
|
888 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
889 |
|
|
890 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
891 |
{ |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
892 |
pEnc->current->quant = RateControlGetQ(0); |
} else { |
|
} |
|
|
else |
|
|
{ |
|
893 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
894 |
} |
} |
895 |
|
|
896 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
897 |
{ |
int *temp_dquants = |
898 |
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 * |
899 |
|
pEnc->mbParam.mb_height * sizeof(int), |
900 |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
CACHE_LINE); |
901 |
pEnc->mbParam.edged_width, // stride |
|
902 |
temp_dquants, |
pEnc->current->quant = |
903 |
pEnc->current->quant, |
adaptive_quantization(pEnc->current->image.y, |
904 |
pEnc->current->quant, // min_quant |
pEnc->mbParam.edged_width, temp_dquants, |
905 |
2*pEnc->current->quant, // max_quant |
pEnc->current->quant, pEnc->current->quant, |
906 |
|
2 * pEnc->current->quant, |
907 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
908 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
909 |
|
|
910 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
911 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
|
912 |
{ |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
913 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
914 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
915 |
|
|
916 |
|
|
917 |
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
918 |
|
|
919 |
|
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
920 |
|
} |
921 |
|
|
922 |
|
#undef OFFSET |
923 |
} |
} |
924 |
|
|
925 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
926 |
} |
} |
927 |
|
|
929 |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
930 |
write_vol_header = 1; |
write_vol_header = 1; |
931 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
932 |
} |
} else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
933 |
else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
int matrix1_changed, matrix2_changed; |
|
int ret1, ret2; |
|
934 |
|
|
935 |
ret1 = ret2 = 0; |
matrix1_changed = matrix2_changed = 0; |
936 |
|
|
937 |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
938 |
write_vol_header = 1; |
write_vol_header = 1; |
941 |
|
|
942 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
943 |
if(pFrame->quant_intra_matrix != NULL) |
if(pFrame->quant_intra_matrix != NULL) |
944 |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); |
945 |
if(pFrame->quant_inter_matrix != NULL) |
if(pFrame->quant_inter_matrix != NULL) |
946 |
ret2 = set_inter_matrix(pFrame->quant_inter_matrix); |
matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); |
947 |
} |
} else { |
948 |
else { |
matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
949 |
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()); |
|
950 |
} |
} |
951 |
if(write_vol_header == 0) |
if(write_vol_header == 0) |
952 |
write_vol_header = ret1 | ret2; |
write_vol_header = matrix1_changed | matrix2_changed; |
953 |
} |
} |
954 |
|
|
955 |
if (pFrame->intra < 0) |
if (pFrame->intra < 0) { |
956 |
{ |
if ((pEnc->iFrameNum == 0) |
957 |
if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
|| ((pEnc->iMaxKeyInterval > 0) |
958 |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { |
|
|
|
959 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
960 |
else |
} else { |
961 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
962 |
} |
} |
963 |
else |
} else { |
964 |
{ |
if (pFrame->intra == 1) { |
|
if (pFrame->intra == 1) |
|
965 |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
966 |
else |
} else { |
967 |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
968 |
} |
} |
969 |
|
|
970 |
|
} |
971 |
|
|
972 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
973 |
BitstreamPutBits(&bs, 0xFFFF, 16); |
BitstreamPutBits(&bs, 0xFFFF, 16); |
974 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
975 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
976 |
|
|
977 |
if (pResult) |
if (pResult) { |
|
{ |
|
978 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
979 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
980 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
982 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
983 |
} |
} |
984 |
|
|
985 |
EMMS(); |
emms(); |
986 |
|
|
987 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
988 |
{ |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
989 |
RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
990 |
} |
} |
|
|
|
991 |
#ifdef _DEBUG |
#ifdef _DEBUG |
992 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
psnr = |
993 |
pEnc->mbParam.width, pEnc->mbParam.height); |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
994 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
995 |
|
pEnc->mbParam.height); |
996 |
|
|
997 |
sprintf(temp, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
998 |
DEBUG(temp); |
DEBUG(temp); |
999 |
#endif |
#endif |
1000 |
|
|
1005 |
|
|
1006 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1007 |
} |
} |
1008 |
|
#endif |
1009 |
|
|
1010 |
|
|
1011 |
static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { |
static __inline void |
1012 |
|
CodeIntraMB(Encoder * pEnc, |
1013 |
|
MACROBLOCK * pMB) |
1014 |
|
{ |
1015 |
|
|
1016 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1017 |
|
|
1022 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1023 |
|
|
1024 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1025 |
if(pMB->dquant != NO_CHANGE) |
if (pMB->dquant != NO_CHANGE) { |
|
{ |
|
1026 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
1027 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1028 |
|
|
1029 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1030 |
if (pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1031 |
|
if (pEnc->current->quant < 1) |
1032 |
|
pEnc->current->quant = 1; |
1033 |
} |
} |
1034 |
} |
} |
1035 |
|
|
1040 |
#define FCODEBITS 3 |
#define FCODEBITS 3 |
1041 |
#define MODEBITS 5 |
#define MODEBITS 5 |
1042 |
|
|
1043 |
void HintedMESet(Encoder * pEnc, int * intra) |
void |
1044 |
|
HintedMESet(Encoder * pEnc, |
1045 |
|
int *intra) |
1046 |
{ |
{ |
1047 |
HINTINFO * hint; |
HINTINFO * hint; |
1048 |
Bitstream bs; |
Bitstream bs; |
1051 |
|
|
1052 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1053 |
|
|
1054 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1055 |
*intra = hint->mvhint.intra; |
*intra = hint->mvhint.intra; |
1056 |
} |
} else { |
|
else |
|
|
{ |
|
1057 |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
1058 |
*intra = BitstreamGetBit(&bs); |
*intra = BitstreamGetBit(&bs); |
1059 |
} |
} |
1060 |
|
|
1061 |
if (*intra) |
if (*intra) { |
|
{ |
|
1062 |
return; |
return; |
1063 |
} |
} |
1064 |
|
|
1065 |
pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
pEnc->current->fcode = |
1066 |
|
(hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, |
1067 |
|
FCODEBITS); |
1068 |
|
|
1069 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1070 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1071 |
|
|
1072 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1073 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1074 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1075 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1076 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1077 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1078 |
VECTOR pred[4]; |
VECTOR pred[4]; |
1079 |
VECTOR tmp; |
VECTOR tmp; |
1080 |
int dummy[4]; |
int32_t dummy[4]; |
1081 |
int vec; |
int vec; |
1082 |
|
|
1083 |
pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
pMB->mode = |
1084 |
|
(hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, |
1085 |
|
MODEBITS); |
1086 |
|
|
1087 |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
1088 |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
1089 |
|
|
1090 |
if (pMB->mode == MODE_INTER) |
if (pMB->mode == MODE_INTER) { |
1091 |
{ |
tmp.x = |
1092 |
tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, |
1093 |
tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); |
length); |
1094 |
|
tmp.y = |
1095 |
|
(hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, |
1096 |
|
length); |
1097 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1098 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1099 |
|
|
1100 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy); |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, |
1101 |
|
0, pred, dummy); |
1102 |
|
|
1103 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1104 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1105 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1106 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
1107 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
1108 |
} |
} |
1109 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
1110 |
else if (pMB->mode == MODE_INTER4V) |
for (vec = 0; vec < 4; ++vec) { |
1111 |
{ |
tmp.x = |
1112 |
for (vec=0 ; vec<4 ; ++vec) |
(hint->rawhints) ? bhint->mvs[vec]. |
1113 |
{ |
x : BitstreamGetBits(&bs, length); |
1114 |
tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); |
tmp.y = |
1115 |
tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[vec]. |
1116 |
|
y : BitstreamGetBits(&bs, length); |
1117 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1118 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1119 |
|
|
1120 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
get_pmvdata(pEnc->current->mbs, x, y, |
1121 |
|
pEnc->mbParam.mb_width, vec, pred, dummy); |
1122 |
|
|
1123 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1124 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1125 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
1126 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
1127 |
} |
} |
1128 |
} |
} else // intra / stuffing / not_coded |
|
else // intra / stuffing / not_coded |
|
|
{ |
|
|
for (vec=0 ; vec<4 ; ++vec) |
|
1129 |
{ |
{ |
1130 |
|
for (vec = 0; vec < 4; ++vec) { |
1131 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1132 |
} |
} |
1133 |
} |
} |
1134 |
|
|
1135 |
if (pMB->mode == MODE_INTER4V && |
if (pMB->mode == MODE_INTER4V && |
1136 |
(pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) |
(pEnc->current->global_flags & XVID_LUMIMASKING) |
1137 |
{ |
&& pMB->dquant != NO_CHANGE) { |
1138 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1139 |
|
|
1140 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1141 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1142 |
} |
} |
1143 |
} |
} |
1146 |
} |
} |
1147 |
|
|
1148 |
|
|
1149 |
void HintedMEGet(Encoder * pEnc, int intra) |
void |
1150 |
|
HintedMEGet(Encoder * pEnc, |
1151 |
|
int intra) |
1152 |
{ |
{ |
1153 |
HINTINFO * hint; |
HINTINFO * hint; |
1154 |
Bitstream bs; |
Bitstream bs; |
1157 |
|
|
1158 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1159 |
|
|
1160 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1161 |
hint->mvhint.intra = intra; |
hint->mvhint.intra = intra; |
1162 |
} |
} else { |
|
else |
|
|
{ |
|
1163 |
BitstreamInit(&bs, hint->hintstream, 0); |
BitstreamInit(&bs, hint->hintstream, 0); |
1164 |
BitstreamPutBit(&bs, intra); |
BitstreamPutBit(&bs, intra); |
1165 |
} |
} |
1166 |
|
|
1167 |
if (intra) |
if (intra) { |
1168 |
{ |
if (!hint->rawhints) { |
|
if (!hint->rawhints) |
|
|
{ |
|
1169 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1170 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1171 |
} |
} |
1175 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1176 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1177 |
|
|
1178 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1179 |
hint->mvhint.fcode = pEnc->current->fcode; |
hint->mvhint.fcode = pEnc->current->fcode; |
1180 |
} |
} else { |
|
else |
|
|
{ |
|
1181 |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
1182 |
} |
} |
1183 |
|
|
1184 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1185 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1186 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1187 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1188 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1189 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1190 |
VECTOR tmp; |
VECTOR tmp; |
1191 |
|
|
1192 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1193 |
bhint->mode = pMB->mode; |
bhint->mode = pMB->mode; |
1194 |
} |
} else { |
|
else |
|
|
{ |
|
1195 |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
1196 |
} |
} |
1197 |
|
|
1198 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
1199 |
tmp.x = pMB->mvs[0].x; |
tmp.x = pMB->mvs[0].x; |
1200 |
tmp.y = pMB->mvs[0].y; |
tmp.y = pMB->mvs[0].y; |
1201 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1202 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1203 |
|
|
1204 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1205 |
bhint->mvs[0].x = tmp.x; |
bhint->mvs[0].x = tmp.x; |
1206 |
bhint->mvs[0].y = tmp.y; |
bhint->mvs[0].y = tmp.y; |
1207 |
} |
} else { |
|
else |
|
|
{ |
|
1208 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1209 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1210 |
} |
} |
1211 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
|
else if (pMB->mode == MODE_INTER4V) |
|
|
{ |
|
1212 |
int vec; |
int vec; |
1213 |
|
|
1214 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1215 |
tmp.x = pMB->mvs[vec].x; |
tmp.x = pMB->mvs[vec].x; |
1216 |
tmp.y = pMB->mvs[vec].y; |
tmp.y = pMB->mvs[vec].y; |
1217 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1218 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1219 |
|
|
1220 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1221 |
bhint->mvs[vec].x = tmp.x; |
bhint->mvs[vec].x = tmp.x; |
1222 |
bhint->mvs[vec].y = tmp.y; |
bhint->mvs[vec].y = tmp.y; |
1223 |
} |
} else { |
|
else |
|
|
{ |
|
1224 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1225 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1226 |
} |
} |
1229 |
} |
} |
1230 |
} |
} |
1231 |
|
|
1232 |
if (!hint->rawhints) |
if (!hint->rawhints) { |
|
{ |
|
1233 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1234 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1235 |
} |
} |
1236 |
} |
} |
1237 |
|
|
1238 |
|
|
1239 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
static int |
1240 |
|
FrameCodeI(Encoder * pEnc, |
1241 |
|
Bitstream * bs, |
1242 |
|
uint32_t * pBits) |
1243 |
{ |
{ |
1244 |
|
|
1245 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1262 |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1263 |
|
|
1264 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
1265 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1266 |
{ |
MACROBLOCK *pMB = |
1267 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1268 |
|
|
1269 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1270 |
|
|
1271 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1272 |
|
dct_codes, qcoeff); |
1273 |
|
|
1274 |
start_timer(); |
start_timer(); |
1275 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1288 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1289 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1290 |
|
|
1291 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1292 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
1293 |
} |
} |
1294 |
|
|
1298 |
|
|
1299 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1300 |
|
|
1301 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) |
static int |
1302 |
|
FrameCodeP(Encoder * pEnc, |
1303 |
|
Bitstream * bs, |
1304 |
|
uint32_t * pBits, |
1305 |
|
bool force_inter, |
1306 |
|
bool vol_header) |
1307 |
{ |
{ |
1308 |
float fSigma; |
float fSigma; |
1309 |
|
|
1313 |
int iLimit; |
int iLimit; |
1314 |
uint32_t x, y; |
uint32_t x, y; |
1315 |
int iSearchRange; |
int iSearchRange; |
1316 |
bool bIntra; |
int bIntra; |
1317 |
|
|
1318 |
IMAGE *pCurrent = &pEnc->current->image; |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
1319 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1320 |
|
|
1321 |
start_timer(); |
start_timer(); |
1322 |
image_setedges(pRef, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1323 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.width, pEnc->mbParam.height, |
|
pEnc->mbParam.edged_height, |
|
|
pEnc->mbParam.width, |
|
|
pEnc->mbParam.height, |
|
1324 |
pEnc->current->global_flags & XVID_INTERLACING); |
pEnc->current->global_flags & XVID_INTERLACING); |
1325 |
stop_edges_timer(); |
stop_edges_timer(); |
1326 |
|
|
1329 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1330 |
|
|
1331 |
if (!force_inter) |
if (!force_inter) |
1332 |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
iLimit = |
1333 |
|
(int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * |
1334 |
|
INTRA_THRESHOLD); |
1335 |
else |
else |
1336 |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
1337 |
|
|
1338 |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
1339 |
start_timer(); |
start_timer(); |
1340 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1341 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1342 |
|
pEnc->mbParam.edged_height, |
1343 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1344 |
stop_inter_timer(); |
stop_inter_timer(); |
1345 |
} |
} |
1346 |
|
|
1347 |
start_timer(); |
start_timer(); |
1348 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
|
{ |
|
1349 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1350 |
} |
} else { |
1351 |
else |
bIntra = |
1352 |
{ |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1353 |
bIntra = MotionEstimation( |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
&pEnc->mbParam, |
|
|
pEnc->current, |
|
|
pEnc->reference, |
|
|
&pEnc->vInterH, |
|
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
1354 |
iLimit); |
iLimit); |
1355 |
} |
} |
1356 |
stop_motion_timer(); |
stop_motion_timer(); |
1357 |
|
|
1358 |
if (bIntra == 1) |
if (bIntra == 1) { |
|
{ |
|
1359 |
return FrameCodeI(pEnc, bs, pBits); |
return FrameCodeI(pEnc, bs, pBits); |
1360 |
} |
} |
1361 |
|
|
1373 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1374 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1375 |
|
|
1376 |
for(y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1377 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1378 |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
MACROBLOCK *pMB = |
1379 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1380 |
|
|
1381 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1382 |
|
|
1383 |
if (!bIntra) |
if (!bIntra) { |
|
{ |
|
1384 |
start_timer(); |
start_timer(); |
1385 |
MBMotionCompensation(pMB, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1386 |
x, y, |
&pEnc->vInterH, &pEnc->vInterV, |
1387 |
&pEnc->reference->image, |
&pEnc->vInterHV, &pEnc->current->image, |
1388 |
&pEnc->vInterH, |
dct_codes, pEnc->mbParam.width, |
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
|
&pEnc->current->image, |
|
|
dct_codes, |
|
|
pEnc->mbParam.width, |
|
1389 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1390 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1391 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1395 |
if(pMB->dquant != NO_CHANGE) { |
if(pMB->dquant != NO_CHANGE) { |
1396 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
1397 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1398 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1399 |
else if(pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1400 |
|
else if (pEnc->current->quant < 1) |
1401 |
|
pEnc->current->quant = 1; |
1402 |
} |
} |
1403 |
} |
} |
1404 |
pMB->quant = pEnc->current->quant; |
pMB->quant = pEnc->current->quant; |
1405 |
|
|
1406 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1407 |
|
|
1408 |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
pMB->cbp = |
1409 |
} |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1410 |
else |
dct_codes, qcoeff); |
1411 |
{ |
} else { |
1412 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1413 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1414 |
|
dct_codes, qcoeff); |
1415 |
} |
} |
1416 |
|
|
1417 |
start_timer(); |
start_timer(); |
1418 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1419 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1420 |
|
|
1421 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
{ |
|
1422 |
pEnc->sStat.kblks++; |
pEnc->sStat.kblks++; |
1423 |
} |
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1424 |
else if (pMB->cbp || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1425 |
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) |
|
|
{ |
|
1426 |
pEnc->sStat.mblks++; |
pEnc->sStat.mblks++; |
1427 |
} |
} else { |
|
else |
|
|
{ |
|
1428 |
pEnc->sStat.ublks++; |
pEnc->sStat.ublks++; |
1429 |
} |
} |
1430 |
|
|
1436 |
|
|
1437 |
emms(); |
emms(); |
1438 |
|
|
1439 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1440 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
1441 |
} |
} |
1442 |
|
|
1452 |
{ |
{ |
1453 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1454 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1455 |
} |
} else if ((fSigma < iSearchRange / 6) |
|
else if ((fSigma < iSearchRange / 6) |
|
1456 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
1457 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
1458 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
1469 |
} |
} |
1470 |
|
|
1471 |
|
|
1472 |
|
#ifdef BFRAMES |
1473 |
/* |
static void |
1474 |
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) |
FrameCodeB(Encoder * pEnc, |
1475 |
|
FRAMEINFO * frame, |
1476 |
|
Bitstream * bs, |
1477 |
|
uint32_t * pBits) |
1478 |
{ |
{ |
1479 |
int16_t dct_codes[6][64]; |
int16_t dct_codes[6 * 64]; |
1480 |
int16_t qcoeff[6][64]; |
int16_t qcoeff[6 * 64]; |
1481 |
uint32_t x, y; |
uint32_t x, y; |
1482 |
VECTOR forward; |
VECTOR forward; |
1483 |
VECTOR backward; |
VECTOR backward; |
1486 |
IMAGE *b_ref = &pEnc->current->image; |
IMAGE *b_ref = &pEnc->current->image; |
1487 |
|
|
1488 |
// forward |
// forward |
1489 |
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, |
1490 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1491 |
|
pEnc->mbParam.height, |
1492 |
|
frame->global_flags & XVID_INTERLACING); |
1493 |
start_timer(); |
start_timer(); |
1494 |
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, |
1495 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1496 |
|
0); |
1497 |
stop_inter_timer(); |
stop_inter_timer(); |
1498 |
|
|
1499 |
// backward |
// backward |
1500 |
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, |
1501 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1502 |
|
pEnc->mbParam.height, |
1503 |
|
frame->global_flags & XVID_INTERLACING); |
1504 |
start_timer(); |
start_timer(); |
1505 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1506 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1507 |
|
0); |
1508 |
stop_inter_timer(); |
stop_inter_timer(); |
1509 |
|
|
1510 |
start_timer(); |
start_timer(); |
1511 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, |
1512 |
pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1513 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1514 |
|
&pEnc->vInterV, &pEnc->vInterHV); |
1515 |
|
|
1516 |
|
|
1517 |
stop_motion_timer(); |
stop_motion_timer(); |
1518 |
|
|
1519 |
if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
/*if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
1520 |
{ |
{ |
1521 |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
1522 |
} |
} */ |
1523 |
|
|
1524 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1525 |
BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0, |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame); |
|
frame->quant, frame->fcode, frame->bcode); |
|
1526 |
|
|
1527 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1528 |
|
|
1532 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1533 |
|
|
1534 |
|
|
1535 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
{ |
|
1536 |
// reset prediction |
// reset prediction |
1537 |
|
|
1538 |
forward.x = 0; |
forward.x = 0; |
1540 |
backward.x = 0; |
backward.x = 0; |
1541 |
backward.y = 0; |
backward.y = 0; |
1542 |
|
|
1543 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1544 |
{ |
MACROBLOCK *f_mb = |
1545 |
MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
1546 |
MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK *b_mb = |
1547 |
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1548 |
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1549 |
|
|
1550 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
1551 |
if (mb->mode == MODE_NOT_CODED) |
if (mb->mode == MODE_NOT_CODED) { |
|
{ |
|
1552 |
mb->mvs[0].x = 0; |
mb->mvs[0].x = 0; |
1553 |
mb->mvs[0].y = 0; |
mb->mvs[0].y = 0; |
1554 |
continue; |
continue; |
1555 |
} |
} |
1556 |
|
|
1557 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1558 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1559 |
b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1560 |
|
&pEnc->vInterV, &pEnc->vInterHV, |
1561 |
dct_codes); |
dct_codes); |
1562 |
|
|
1563 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1564 |
mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff); |
mb->cbp = |
1565 |
|
MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, |
1566 |
|
qcoeff); |
1567 |
//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); |
1568 |
|
|
1569 |
|
|
1570 |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
1571 |
mb->cbp == 0 && |
&& mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { |
|
mb->mvs[0].x == 0 && |
|
|
mb->mvs[0].y == 0) |
|
|
{ |
|
1572 |
mb->mode = 5; // skipped |
mb->mode = 5; // skipped |
1573 |
} |
} |
1574 |
|
|
1575 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
|
{ |
|
1576 |
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
1577 |
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
1578 |
forward.x = mb->mvs[0].x; |
forward.x = mb->mvs[0].x; |
1579 |
forward.y = mb->mvs[0].y; |
forward.y = mb->mvs[0].y; |
1580 |
} |
} |
1581 |
|
|
1582 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
|
{ |
|
1583 |
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
1584 |
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
1585 |
backward.x = mb->b_mvs[0].x; |
backward.x = mb->b_mvs[0].x; |
1586 |
backward.y = mb->b_mvs[0].y; |
backward.y = mb->b_mvs[0].y; |
1587 |
} |
} |
|
|
|
1588 |
// 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); |
// 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); |
1589 |
|
|
1590 |
start_timer(); |
start_timer(); |
1591 |
MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat); |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1592 |
|
&pEnc->sStat); |
1593 |
stop_coding_timer(); |
stop_coding_timer(); |
1594 |
} |
} |
1595 |
} |
} |
1599 |
// TODO: dynamic fcode/bcode ??? |
// TODO: dynamic fcode/bcode ??? |
1600 |
|
|
1601 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
|
|
|
1602 |
} |
} |
1603 |
|
#endif |
|
*/ |
|