1 |
|
// 14.04.2002 added FrameCodeB() |
2 |
|
|
3 |
#include <stdlib.h> |
#include <stdlib.h> |
4 |
#include <stdio.h> |
#include <stdio.h> |
5 |
#include <math.h> |
#include <math.h> |
21 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
22 |
|
|
23 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
24 |
|
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
25 |
|
|
26 |
|
|
27 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); |
38 |
}; |
}; |
39 |
|
|
40 |
|
|
41 |
|
void static image_null(IMAGE * image) |
42 |
|
{ |
43 |
|
image->y = image->u = image->v = NULL; |
44 |
|
} |
45 |
|
|
46 |
|
|
47 |
int encoder_create(XVID_ENC_PARAM * pParam) |
int encoder_create(XVID_ENC_PARAM * pParam) |
48 |
{ |
{ |
49 |
Encoder *pEnc; |
Encoder *pEnc; |
87 |
pParam->fincr = (int)(pParam->fincr / div); |
pParam->fincr = (int)(pParam->fincr / div); |
88 |
} |
} |
89 |
|
|
90 |
if (pParam->bitrate <= 0) |
if (pParam->rc_bitrate <= 0) |
91 |
pParam->bitrate = 900000; |
pParam->rc_bitrate = 900000; |
92 |
|
|
93 |
|
if (pParam->rc_reaction_delay_factor <= 0) |
94 |
|
pParam->rc_reaction_delay_factor = 16; |
95 |
|
|
96 |
if (pParam->rc_buffersize <= 0) |
if (pParam->rc_averaging_period <= 0) |
97 |
pParam->rc_buffersize = 16; |
pParam->rc_averaging_period = 100; |
98 |
|
|
99 |
|
if (pParam->rc_buffer <= 0) |
100 |
|
pParam->rc_buffer = 100; |
101 |
|
|
102 |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
103 |
pParam->min_quantizer = 1; |
pParam->min_quantizer = 1; |
129 |
|
|
130 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
131 |
|
|
132 |
pEnc->mbParam.quant = 4; |
pEnc->bitrate = pParam->rc_bitrate; |
|
|
|
|
pEnc->bitrate = pParam->bitrate; |
|
133 |
|
|
134 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
135 |
pEnc->iMaxKeyInterval = pParam->max_key_interval; |
pEnc->iMaxKeyInterval = pParam->max_key_interval; |
136 |
|
|
137 |
if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
/* try to allocate frame memory */ |
|
{ |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
138 |
|
|
139 |
if (image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
pEnc->current = NULL; |
140 |
|
pEnc->reference = NULL; |
141 |
|
if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL || |
142 |
|
(pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL) |
143 |
{ |
{ |
144 |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
if (pEnc->current) xvid_free(pEnc->current); |
145 |
xvid_free(pEnc); |
xvid_free(pEnc); |
146 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
147 |
} |
} |
148 |
|
|
149 |
if (image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
/* try to allocate mb memory */ |
|
{ |
|
|
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
150 |
|
|
151 |
if (image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
pEnc->current->mbs = NULL; |
152 |
{ |
pEnc->reference->mbs = NULL; |
|
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
153 |
|
|
154 |
if (image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
#ifdef _DEBUG |
155 |
{ |
#ifdef WIN32 |
156 |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
OutputDebugString("malloc mbs"); |
157 |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
#endif |
158 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
#endif |
159 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
160 |
|
if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL || |
161 |
|
(pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) |
162 |
|
{ |
163 |
|
if (pEnc->current->mbs) xvid_free(pEnc->current->mbs); |
164 |
|
xvid_free(pEnc->current); |
165 |
|
xvid_free(pEnc->reference); |
166 |
xvid_free(pEnc); |
xvid_free(pEnc); |
|
return XVID_ERR_MEMORY; |
|
167 |
} |
} |
168 |
|
|
169 |
pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE); |
/* try to allocate image memory */ |
170 |
if (pEnc->pMBs == NULL) |
|
171 |
{ |
#ifdef _DEBUG |
172 |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_null(&pEnc->sOriginal); |
173 |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
#endif |
174 |
|
image_null(&pEnc->current->image); |
175 |
|
image_null(&pEnc->reference->image); |
176 |
|
image_null(&pEnc->vInterH); |
177 |
|
image_null(&pEnc->vInterV); |
178 |
|
image_null(&pEnc->vInterVf); |
179 |
|
image_null(&pEnc->vInterHV); |
180 |
|
image_null(&pEnc->vInterHVf); |
181 |
|
|
182 |
|
#ifdef _DEBUG |
183 |
|
#ifdef WIN32 |
184 |
|
OutputDebugString("malloc images"); |
185 |
|
#endif |
186 |
|
#endif |
187 |
|
if ( |
188 |
|
#ifdef _DEBUG |
189 |
|
image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
190 |
|
#endif |
191 |
|
image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
192 |
|
image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
193 |
|
image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
194 |
|
image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
195 |
|
image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
196 |
|
image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
197 |
|
image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
198 |
|
{ |
199 |
|
#ifdef _DEBUG |
200 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
201 |
|
#endif |
202 |
|
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
203 |
|
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
204 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
205 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
206 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
207 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
208 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
209 |
|
|
210 |
|
xvid_free(pEnc->current); |
211 |
|
xvid_free(pEnc->reference); |
212 |
xvid_free(pEnc); |
xvid_free(pEnc); |
213 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
214 |
} |
} |
215 |
|
|
|
// init macroblock array |
|
|
for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++) |
|
|
{ |
|
|
pEnc->pMBs[i].dquant = NO_CHANGE; |
|
|
} |
|
|
|
|
216 |
pParam->handle = (void *)pEnc; |
pParam->handle = (void *)pEnc; |
217 |
|
|
218 |
if (pParam->bitrate) |
if (pParam->rc_bitrate) |
219 |
{ |
{ |
220 |
RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase * 100 / pParam->fincr, |
RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor, |
221 |
|
pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr, |
222 |
pParam->max_quantizer, pParam->min_quantizer); |
pParam->max_quantizer, pParam->min_quantizer); |
223 |
} |
} |
224 |
|
|
|
create_vlc_tables(); |
|
225 |
init_timer(); |
init_timer(); |
226 |
|
|
227 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
231 |
int encoder_destroy(Encoder * pEnc) |
int encoder_destroy(Encoder * pEnc) |
232 |
{ |
{ |
233 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
|
ENC_CHECK(pEnc->sCurrent.y); |
|
|
ENC_CHECK(pEnc->sReference.y); |
|
234 |
|
|
235 |
xvid_free(pEnc->pMBs); |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
236 |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
237 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
238 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
239 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
240 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
241 |
xvid_free(pEnc); |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
242 |
|
#ifdef _DEBUG |
243 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
244 |
|
#endif |
245 |
|
xvid_free(pEnc->current->mbs); |
246 |
|
xvid_free(pEnc->current); |
247 |
|
|
248 |
destroy_vlc_tables(); |
xvid_free(pEnc->reference->mbs); |
249 |
|
xvid_free(pEnc->reference); |
250 |
|
|
251 |
|
xvid_free(pEnc); |
252 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
253 |
} |
} |
254 |
|
|
258 |
Bitstream bs; |
Bitstream bs; |
259 |
uint32_t bits; |
uint32_t bits; |
260 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
261 |
|
#ifdef _DEBUG |
262 |
|
float psnr; |
263 |
|
uint8_t temp[100]; |
264 |
|
#endif |
265 |
|
|
266 |
start_global_timer(); |
start_global_timer(); |
267 |
|
|
270 |
ENC_CHECK(pFrame->bitstream); |
ENC_CHECK(pFrame->bitstream); |
271 |
ENC_CHECK(pFrame->image); |
ENC_CHECK(pFrame->image); |
272 |
|
|
273 |
pEnc->mbParam.global_flags = pFrame->general; |
SWAP(pEnc->current, pEnc->reference); |
274 |
pEnc->mbParam.motion_flags = pFrame->motion; |
|
275 |
|
pEnc->current->global_flags = pFrame->general; |
276 |
|
pEnc->current->motion_flags = pFrame->motion; |
277 |
|
pEnc->mbParam.hint = &pFrame->hint; |
278 |
|
|
279 |
start_timer(); |
start_timer(); |
280 |
if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
281 |
pFrame->image, pFrame->colorspace)) |
pFrame->image, pFrame->colorspace)) |
282 |
{ |
{ |
283 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
284 |
} |
} |
285 |
stop_conv_timer(); |
stop_conv_timer(); |
286 |
|
|
287 |
|
#ifdef _DEBUG |
288 |
|
image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
289 |
|
#endif |
290 |
|
|
291 |
EMMS(); |
EMMS(); |
292 |
|
|
293 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
294 |
|
|
295 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
296 |
{ |
{ |
297 |
pEnc->mbParam.quant = RateControlGetQ(0); |
pEnc->current->quant = RateControlGetQ(0); |
298 |
} |
} |
299 |
else |
else |
300 |
{ |
{ |
301 |
pEnc->mbParam.quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
302 |
} |
} |
303 |
|
|
304 |
if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
305 |
{ |
{ |
306 |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
307 |
|
|
308 |
pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
309 |
pEnc->mbParam.width, |
pEnc->mbParam.edged_width, // stride |
310 |
temp_dquants, |
temp_dquants, |
311 |
pFrame->quant, |
pEnc->current->quant, |
312 |
pFrame->quant, |
pEnc->current->quant, // min_quant |
313 |
2*pFrame->quant, |
2*pEnc->current->quant, // max_quant |
314 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
315 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
316 |
|
|
317 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
318 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
319 |
{ |
{ |
320 |
MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
321 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
322 |
} |
} |
323 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
324 |
} |
} |
325 |
|
|
326 |
if(pEnc->mbParam.global_flags & XVID_H263QUANT) { |
if (pEnc->current->global_flags & XVID_H263QUANT) { |
327 |
if(pEnc->mbParam.quant_type != H263_QUANT) |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
328 |
write_vol_header = 1; |
write_vol_header = 1; |
329 |
pEnc->mbParam.quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
330 |
} |
} |
331 |
else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) { |
else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
332 |
int ret1, ret2; |
int ret1, ret2; |
333 |
|
|
334 |
ret1 = ret2 = 0; |
ret1 = ret2 = 0; |
335 |
|
|
336 |
if(pEnc->mbParam.quant_type != MPEG4_QUANT) |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
337 |
write_vol_header = 1; |
write_vol_header = 1; |
338 |
|
|
339 |
pEnc->mbParam.quant_type = MPEG4_QUANT; |
pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
340 |
|
|
341 |
if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
342 |
if(pFrame->quant_intra_matrix != NULL) |
if(pFrame->quant_intra_matrix != NULL) |
343 |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
344 |
if(pFrame->quant_inter_matrix != NULL) |
if(pFrame->quant_inter_matrix != NULL) |
376 |
|
|
377 |
if (pResult) |
if (pResult) |
378 |
{ |
{ |
379 |
pResult->quant = pEnc->mbParam.quant; |
pResult->quant = pEnc->current->quant; |
380 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
381 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
382 |
pResult->mblks = pEnc->sStat.mblks; |
pResult->mblks = pEnc->sStat.mblks; |
387 |
|
|
388 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
389 |
{ |
{ |
390 |
RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra); |
RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
391 |
} |
} |
392 |
|
|
393 |
|
#ifdef _DEBUG |
394 |
|
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
395 |
|
pEnc->mbParam.width, pEnc->mbParam.height); |
396 |
|
|
397 |
|
sprintf(temp, "PSNR: %f\n", psnr); |
398 |
|
DEBUG(temp); |
399 |
|
#endif |
400 |
|
|
401 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
|
image_swap(&pEnc->sCurrent, &pEnc->sReference); |
|
402 |
|
|
403 |
stop_global_timer(); |
stop_global_timer(); |
404 |
write_timer(); |
write_timer(); |
411 |
|
|
412 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
413 |
|
|
414 |
if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) { |
/* zero mv statistics */ |
415 |
|
pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; |
416 |
|
pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; |
417 |
|
pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
418 |
|
pMB->sad16 = 0; |
419 |
|
|
420 |
|
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
421 |
if(pMB->dquant != NO_CHANGE) |
if(pMB->dquant != NO_CHANGE) |
422 |
{ |
{ |
423 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
424 |
pEnc->mbParam.quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
425 |
|
|
426 |
|
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
427 |
|
if (pEnc->current->quant < 1) pEnc->current->quant = 1; |
428 |
|
} |
429 |
|
} |
430 |
|
|
431 |
|
pMB->quant = pEnc->current->quant; |
432 |
|
} |
433 |
|
|
434 |
|
|
435 |
|
#define FCODEBITS 3 |
436 |
|
#define MODEBITS 5 |
437 |
|
|
438 |
|
void HintedMESet(Encoder * pEnc, int * intra) |
439 |
|
{ |
440 |
|
HINTINFO * hint; |
441 |
|
Bitstream bs; |
442 |
|
int length, high; |
443 |
|
uint32_t x, y; |
444 |
|
|
445 |
|
hint = pEnc->mbParam.hint; |
446 |
|
|
447 |
|
if (hint->rawhints) |
448 |
|
{ |
449 |
|
*intra = hint->mvhint.intra; |
450 |
|
} |
451 |
|
else |
452 |
|
{ |
453 |
|
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
454 |
|
*intra = BitstreamGetBit(&bs); |
455 |
|
} |
456 |
|
|
457 |
|
if (*intra) |
458 |
|
{ |
459 |
|
return; |
460 |
|
} |
461 |
|
|
462 |
|
pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
463 |
|
|
464 |
|
length = pEnc->current->fcode + 5; |
465 |
|
high = 1 << (length - 1); |
466 |
|
|
467 |
|
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
468 |
|
{ |
469 |
|
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
470 |
|
{ |
471 |
|
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
472 |
|
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
473 |
|
VECTOR pred[4]; |
474 |
|
VECTOR tmp; |
475 |
|
int dummy[4]; |
476 |
|
int vec; |
477 |
|
|
478 |
|
pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
479 |
|
|
480 |
|
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
481 |
|
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
482 |
|
|
483 |
|
if (pMB->mode == MODE_INTER) |
484 |
|
{ |
485 |
|
tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); |
486 |
|
tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); |
487 |
|
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
488 |
|
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
489 |
|
|
490 |
|
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy); |
491 |
|
|
492 |
|
for (vec=0 ; vec<4 ; ++vec) |
493 |
|
{ |
494 |
|
pMB->mvs[vec].x = tmp.x; |
495 |
|
pMB->mvs[vec].y = tmp.y; |
496 |
|
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
497 |
|
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
498 |
|
} |
499 |
|
} |
500 |
|
else if (pMB->mode == MODE_INTER4V) |
501 |
|
{ |
502 |
|
for (vec=0 ; vec<4 ; ++vec) |
503 |
|
{ |
504 |
|
tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); |
505 |
|
tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); |
506 |
|
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
507 |
|
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
508 |
|
|
509 |
|
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
510 |
|
|
511 |
|
pMB->mvs[vec].x = tmp.x; |
512 |
|
pMB->mvs[vec].y = tmp.y; |
513 |
|
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
514 |
|
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
515 |
|
} |
516 |
|
} |
517 |
|
else // intra / stuffing / not_coded |
518 |
|
{ |
519 |
|
for (vec=0 ; vec<4 ; ++vec) |
520 |
|
{ |
521 |
|
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
522 |
|
} |
523 |
|
} |
524 |
|
|
525 |
|
if (pMB->mode == MODE_INTER4V && |
526 |
|
(pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) |
527 |
|
{ |
528 |
|
pMB->mode = MODE_INTRA; |
529 |
|
|
530 |
if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31; |
for (vec=0 ; vec<4 ; ++vec) |
531 |
if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1; |
{ |
532 |
|
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
533 |
|
} |
534 |
|
} |
535 |
|
} |
536 |
} |
} |
537 |
} |
} |
538 |
|
|
539 |
pMB->quant = pEnc->mbParam.quant; |
|
540 |
|
void HintedMEGet(Encoder * pEnc, int intra) |
541 |
|
{ |
542 |
|
HINTINFO * hint; |
543 |
|
Bitstream bs; |
544 |
|
uint32_t x, y; |
545 |
|
int length, high; |
546 |
|
|
547 |
|
hint = pEnc->mbParam.hint; |
548 |
|
|
549 |
|
if (hint->rawhints) |
550 |
|
{ |
551 |
|
hint->mvhint.intra = intra; |
552 |
|
} |
553 |
|
else |
554 |
|
{ |
555 |
|
BitstreamInit(&bs, hint->hintstream, 0); |
556 |
|
BitstreamPutBit(&bs, intra); |
557 |
|
} |
558 |
|
|
559 |
|
if (intra) |
560 |
|
{ |
561 |
|
if (!hint->rawhints) |
562 |
|
{ |
563 |
|
BitstreamPad(&bs); |
564 |
|
hint->hintlength = BitstreamLength(&bs); |
565 |
|
} |
566 |
|
return; |
567 |
|
} |
568 |
|
|
569 |
|
length = pEnc->current->fcode + 5; |
570 |
|
high = 1 << (length - 1); |
571 |
|
|
572 |
|
if (hint->rawhints) |
573 |
|
{ |
574 |
|
hint->mvhint.fcode = pEnc->current->fcode; |
575 |
|
} |
576 |
|
else |
577 |
|
{ |
578 |
|
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
579 |
|
} |
580 |
|
|
581 |
|
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
582 |
|
{ |
583 |
|
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
584 |
|
{ |
585 |
|
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
586 |
|
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
587 |
|
VECTOR tmp; |
588 |
|
|
589 |
|
if (hint->rawhints) |
590 |
|
{ |
591 |
|
bhint->mode = pMB->mode; |
592 |
|
} |
593 |
|
else |
594 |
|
{ |
595 |
|
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
596 |
|
} |
597 |
|
|
598 |
|
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
599 |
|
{ |
600 |
|
tmp.x = pMB->mvs[0].x; |
601 |
|
tmp.y = pMB->mvs[0].y; |
602 |
|
tmp.x += (tmp.x < 0) ? high*2 : 0; |
603 |
|
tmp.y += (tmp.y < 0) ? high*2 : 0; |
604 |
|
|
605 |
|
if (hint->rawhints) |
606 |
|
{ |
607 |
|
bhint->mvs[0].x = tmp.x; |
608 |
|
bhint->mvs[0].y = tmp.y; |
609 |
|
} |
610 |
|
else |
611 |
|
{ |
612 |
|
BitstreamPutBits(&bs, tmp.x, length); |
613 |
|
BitstreamPutBits(&bs, tmp.y, length); |
614 |
|
} |
615 |
|
} |
616 |
|
else if (pMB->mode == MODE_INTER4V) |
617 |
|
{ |
618 |
|
int vec; |
619 |
|
|
620 |
|
for (vec=0 ; vec<4 ; ++vec) |
621 |
|
{ |
622 |
|
tmp.x = pMB->mvs[vec].x; |
623 |
|
tmp.y = pMB->mvs[vec].y; |
624 |
|
tmp.x += (tmp.x < 0) ? high*2 : 0; |
625 |
|
tmp.y += (tmp.y < 0) ? high*2 : 0; |
626 |
|
|
627 |
|
if (hint->rawhints) |
628 |
|
{ |
629 |
|
bhint->mvs[vec].x = tmp.x; |
630 |
|
bhint->mvs[vec].y = tmp.y; |
631 |
|
} |
632 |
|
else |
633 |
|
{ |
634 |
|
BitstreamPutBits(&bs, tmp.x, length); |
635 |
|
BitstreamPutBits(&bs, tmp.y, length); |
636 |
|
} |
637 |
|
} |
638 |
|
} |
639 |
|
} |
640 |
|
} |
641 |
|
|
642 |
|
if (!hint->rawhints) |
643 |
|
{ |
644 |
|
BitstreamPad(&bs); |
645 |
|
hint->hintlength = BitstreamLength(&bs); |
646 |
|
} |
647 |
} |
} |
648 |
|
|
649 |
|
|
656 |
uint16_t x, y; |
uint16_t x, y; |
657 |
|
|
658 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
659 |
pEnc->mbParam.rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
660 |
pEnc->mbParam.coding_type = I_VOP; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
661 |
|
pEnc->current->coding_type = I_VOP; |
662 |
|
|
663 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
664 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
665 |
|
|
666 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
667 |
|
|
672 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
673 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
674 |
{ |
{ |
675 |
MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
676 |
|
|
677 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
678 |
|
|
679 |
MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, &pEnc->sCurrent); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
680 |
|
|
681 |
start_timer(); |
start_timer(); |
682 |
MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
683 |
stop_prediction_timer(); |
stop_prediction_timer(); |
684 |
|
|
685 |
start_timer(); |
start_timer(); |
686 |
MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
687 |
stop_coding_timer(); |
stop_coding_timer(); |
688 |
} |
} |
689 |
|
|
693 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->sStat.fMvPrevSigma = -1; |
694 |
pEnc->sStat.iMvSum = 0; |
pEnc->sStat.iMvSum = 0; |
695 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
696 |
pEnc->mbParam.fixed_code = 2; |
pEnc->mbParam.m_fcode = 2; |
697 |
|
|
698 |
|
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
699 |
|
{ |
700 |
|
HintedMEGet(pEnc, 1); |
701 |
|
} |
702 |
|
|
703 |
return 1; // intra |
return 1; // intra |
704 |
} |
} |
718 |
int iSearchRange; |
int iSearchRange; |
719 |
bool bIntra; |
bool bIntra; |
720 |
|
|
721 |
IMAGE *pCurrent = &pEnc->sCurrent; |
IMAGE *pCurrent = &pEnc->current->image; |
722 |
IMAGE *pRef = &pEnc->sReference; |
IMAGE *pRef = &pEnc->reference->image; |
723 |
|
|
724 |
start_timer(); |
start_timer(); |
725 |
image_setedges(pRef, |
image_setedges(pRef, |
727 |
pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_height, |
728 |
pEnc->mbParam.width, |
pEnc->mbParam.width, |
729 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
730 |
pEnc->mbParam.global_flags & XVID_INTERLACING); |
pEnc->current->global_flags & XVID_INTERLACING); |
731 |
stop_edges_timer(); |
stop_edges_timer(); |
732 |
|
|
733 |
pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
734 |
|
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
735 |
|
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
736 |
|
|
737 |
if (!force_inter) |
if (!force_inter) |
738 |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
739 |
else |
else |
740 |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
741 |
|
|
742 |
if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) { |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
743 |
start_timer(); |
start_timer(); |
744 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
745 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
746 |
pEnc->mbParam.rounding_type); |
pEnc->current->rounding_type); |
747 |
stop_inter_timer(); |
stop_inter_timer(); |
748 |
} |
} |
749 |
|
|
750 |
start_timer(); |
start_timer(); |
751 |
bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference, |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
752 |
&pEnc->vInterH, &pEnc->vInterV, |
{ |
753 |
&pEnc->vInterHV, &pEnc->sCurrent, iLimit); |
HintedMESet(pEnc, &bIntra); |
754 |
|
} |
755 |
|
else |
756 |
|
{ |
757 |
|
bIntra = MotionEstimation( |
758 |
|
&pEnc->mbParam, |
759 |
|
pEnc->current, |
760 |
|
pEnc->reference, |
761 |
|
&pEnc->vInterH, |
762 |
|
&pEnc->vInterV, |
763 |
|
&pEnc->vInterHV, |
764 |
|
iLimit); |
765 |
|
} |
766 |
stop_motion_timer(); |
stop_motion_timer(); |
767 |
|
|
768 |
if (bIntra == 1) |
if (bIntra == 1) |
769 |
|
{ |
770 |
return FrameCodeI(pEnc, bs, pBits); |
return FrameCodeI(pEnc, bs, pBits); |
771 |
|
} |
772 |
|
|
773 |
pEnc->mbParam.coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
774 |
|
|
775 |
if(vol_header) |
if(vol_header) |
776 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
777 |
|
|
778 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
779 |
|
|
780 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
781 |
|
|
788 |
{ |
{ |
789 |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
790 |
{ |
{ |
791 |
MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
792 |
|
|
793 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
794 |
|
|
797 |
start_timer(); |
start_timer(); |
798 |
MBMotionCompensation(pMB, |
MBMotionCompensation(pMB, |
799 |
x, y, |
x, y, |
800 |
&pEnc->sReference, |
&pEnc->reference->image, |
801 |
&pEnc->vInterH, |
&pEnc->vInterH, |
802 |
&pEnc->vInterV, |
&pEnc->vInterV, |
803 |
&pEnc->vInterHV, |
&pEnc->vInterHV, |
804 |
&pEnc->sCurrent, |
&pEnc->current->image, |
805 |
dct_codes, |
dct_codes, |
806 |
pEnc->mbParam.width, |
pEnc->mbParam.width, |
807 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
808 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
809 |
pEnc->mbParam.rounding_type); |
pEnc->current->rounding_type); |
810 |
stop_comp_timer(); |
stop_comp_timer(); |
811 |
|
|
812 |
if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
813 |
if(pMB->dquant != NO_CHANGE) { |
if(pMB->dquant != NO_CHANGE) { |
814 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
815 |
pEnc->mbParam.quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
816 |
if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31; |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
817 |
else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1; |
else if(pEnc->current->quant < 1) pEnc->current->quant = 1; |
818 |
} |
} |
819 |
} |
} |
820 |
pMB->quant = pEnc->mbParam.quant; |
pMB->quant = pEnc->current->quant; |
821 |
|
|
822 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
823 |
|
|
824 |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent); |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
825 |
} |
} |
826 |
else |
else |
827 |
{ |
{ |
828 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
829 |
MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
830 |
} |
} |
831 |
|
|
832 |
start_timer(); |
start_timer(); |
833 |
MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
834 |
stop_prediction_timer(); |
stop_prediction_timer(); |
835 |
|
|
836 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
851 |
} |
} |
852 |
|
|
853 |
start_timer(); |
start_timer(); |
854 |
MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
855 |
stop_coding_timer(); |
stop_coding_timer(); |
856 |
} |
} |
857 |
} |
} |
858 |
|
|
859 |
emms(); |
emms(); |
860 |
|
|
861 |
|
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
862 |
|
{ |
863 |
|
HintedMEGet(pEnc, 0); |
864 |
|
} |
865 |
|
|
866 |
if (pEnc->sStat.iMvCount == 0) |
if (pEnc->sStat.iMvCount == 0) |
867 |
pEnc->sStat.iMvCount = 1; |
pEnc->sStat.iMvCount = 1; |
868 |
|
|
869 |
fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
870 |
|
|
871 |
iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
872 |
|
|
873 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
874 |
&& (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128 |
&& (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 |
875 |
{ |
{ |
876 |
pEnc->mbParam.fixed_code++; |
pEnc->mbParam.m_fcode++; |
877 |
iSearchRange *= 2; |
iSearchRange *= 2; |
878 |
} |
} |
879 |
else if ((fSigma < iSearchRange / 6) |
else if ((fSigma < iSearchRange / 6) |
880 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
881 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
882 |
&& (pEnc->mbParam.fixed_code >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
883 |
{ |
{ |
884 |
pEnc->mbParam.fixed_code--; |
pEnc->mbParam.m_fcode--; |
885 |
iSearchRange /= 2; |
iSearchRange /= 2; |
886 |
} |
} |
887 |
|
|
891 |
|
|
892 |
return 0; // inter |
return 0; // inter |
893 |
} |
} |
894 |
|
|
895 |
|
|
896 |
|
|
897 |
|
/* |
898 |
|
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) |
899 |
|
{ |
900 |
|
int16_t dct_codes[6][64]; |
901 |
|
int16_t qcoeff[6][64]; |
902 |
|
uint32_t x, y; |
903 |
|
VECTOR forward; |
904 |
|
VECTOR backward; |
905 |
|
|
906 |
|
IMAGE *f_ref = &pEnc->reference->image; |
907 |
|
IMAGE *b_ref = &pEnc->current->image; |
908 |
|
|
909 |
|
// forward |
910 |
|
image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
911 |
|
start_timer(); |
912 |
|
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
913 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
914 |
|
stop_inter_timer(); |
915 |
|
|
916 |
|
// backward |
917 |
|
image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
918 |
|
start_timer(); |
919 |
|
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
920 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
921 |
|
stop_inter_timer(); |
922 |
|
|
923 |
|
start_timer(); |
924 |
|
MotionEstimationBVOP(&pEnc->mbParam, frame, |
925 |
|
pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
926 |
|
pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
927 |
|
|
928 |
|
stop_motion_timer(); |
929 |
|
|
930 |
|
if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
931 |
|
{ |
932 |
|
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
933 |
|
} |
934 |
|
|
935 |
|
frame->coding_type = B_VOP; |
936 |
|
BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0, |
937 |
|
frame->quant, frame->fcode, frame->bcode); |
938 |
|
|
939 |
|
*pBits = BitstreamPos(bs); |
940 |
|
|
941 |
|
pEnc->sStat.iTextBits = 0; |
942 |
|
pEnc->sStat.iMvSum = 0; |
943 |
|
pEnc->sStat.iMvCount = 0; |
944 |
|
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
945 |
|
|
946 |
|
|
947 |
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
948 |
|
{ |
949 |
|
// reset prediction |
950 |
|
|
951 |
|
forward.x = 0; |
952 |
|
forward.y = 0; |
953 |
|
backward.x = 0; |
954 |
|
backward.y = 0; |
955 |
|
|
956 |
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
957 |
|
{ |
958 |
|
MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
959 |
|
MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
960 |
|
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
961 |
|
|
962 |
|
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
963 |
|
if (mb->mode == MODE_NOT_CODED) |
964 |
|
{ |
965 |
|
mb->mvs[0].x = 0; |
966 |
|
mb->mvs[0].y = 0; |
967 |
|
continue; |
968 |
|
} |
969 |
|
|
970 |
|
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
971 |
|
f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
972 |
|
b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
973 |
|
dct_codes); |
974 |
|
|
975 |
|
mb->quant = frame->quant; |
976 |
|
mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff); |
977 |
|
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
978 |
|
|
979 |
|
|
980 |
|
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && |
981 |
|
mb->cbp == 0 && |
982 |
|
mb->mvs[0].x == 0 && |
983 |
|
mb->mvs[0].y == 0) |
984 |
|
{ |
985 |
|
mb->mode = 5; // skipped |
986 |
|
} |
987 |
|
|
988 |
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
989 |
|
{ |
990 |
|
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
991 |
|
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
992 |
|
forward.x = mb->mvs[0].x; |
993 |
|
forward.y = mb->mvs[0].y; |
994 |
|
} |
995 |
|
|
996 |
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
997 |
|
{ |
998 |
|
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
999 |
|
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
1000 |
|
backward.x = mb->b_mvs[0].x; |
1001 |
|
backward.y = mb->b_mvs[0].y; |
1002 |
|
} |
1003 |
|
|
1004 |
|
// 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); |
1005 |
|
|
1006 |
|
start_timer(); |
1007 |
|
MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat); |
1008 |
|
stop_coding_timer(); |
1009 |
|
} |
1010 |
|
} |
1011 |
|
|
1012 |
|
emms(); |
1013 |
|
|
1014 |
|
// TODO: dynamic fcode/bcode ??? |
1015 |
|
|
1016 |
|
*pBits = BitstreamPos(bs) - *pBits; |
1017 |
|
|
1018 |
|
} |
1019 |
|
|
1020 |
|
*/ |