16 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
17 |
#include "quant/adapt_quant.h" |
#include "quant/adapt_quant.h" |
18 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
19 |
|
#include "utils/mem_align.h" |
20 |
|
|
21 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
22 |
|
|
96 |
if (pParam->max_quantizer < pParam->min_quantizer) |
if (pParam->max_quantizer < pParam->min_quantizer) |
97 |
pParam->max_quantizer = pParam->min_quantizer; |
pParam->max_quantizer = pParam->min_quantizer; |
98 |
|
|
99 |
if ((pEnc = (Encoder *) malloc(sizeof(Encoder))) == NULL) |
if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), 16)) == NULL) |
100 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
101 |
|
|
102 |
/* Fill members of Encoder structure */ |
/* Fill members of Encoder structure */ |
123 |
|
|
124 |
if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
125 |
{ |
{ |
126 |
free(pEnc); |
xvid_free(pEnc); |
127 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
128 |
} |
} |
129 |
|
|
130 |
if (image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
if (image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
131 |
{ |
{ |
132 |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
133 |
free(pEnc); |
xvid_free(pEnc); |
134 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
135 |
} |
} |
136 |
|
|
138 |
{ |
{ |
139 |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
140 |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
141 |
free(pEnc); |
xvid_free(pEnc); |
142 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
143 |
} |
} |
144 |
|
|
147 |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
148 |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
149 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
150 |
free(pEnc); |
xvid_free(pEnc); |
151 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
152 |
} |
} |
153 |
|
|
157 |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
158 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
159 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
160 |
free(pEnc); |
xvid_free(pEnc); |
161 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
162 |
} |
} |
163 |
|
|
164 |
pEnc->pMBs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, 16); |
165 |
if (pEnc->pMBs == NULL) |
if (pEnc->pMBs == NULL) |
166 |
{ |
{ |
167 |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
169 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
170 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
171 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
172 |
free(pEnc); |
xvid_free(pEnc); |
173 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
174 |
} |
} |
175 |
|
|
200 |
ENC_CHECK(pEnc->sCurrent.y); |
ENC_CHECK(pEnc->sCurrent.y); |
201 |
ENC_CHECK(pEnc->sReference.y); |
ENC_CHECK(pEnc->sReference.y); |
202 |
|
|
203 |
free(pEnc->pMBs); |
xvid_free(pEnc->pMBs); |
204 |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
205 |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
206 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
207 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
208 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
209 |
free(pEnc); |
xvid_free(pEnc); |
210 |
|
|
211 |
destroy_vlc_tables(); |
destroy_vlc_tables(); |
212 |
|
|
238 |
} |
} |
239 |
stop_conv_timer(); |
stop_conv_timer(); |
240 |
|
|
241 |
|
EMMS(); |
242 |
|
|
243 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
244 |
|
|
245 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
253 |
|
|
254 |
if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) |
if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) |
255 |
{ |
{ |
256 |
int * temp_dquants = (int *) malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int)); |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), 16); |
257 |
|
|
258 |
pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, pEnc->mbParam.width, |
pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, pEnc->mbParam.width, |
259 |
temp_dquants, pFrame->quant, pFrame->quant, |
temp_dquants, pFrame->quant, pFrame->quant, |
265 |
MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
266 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
267 |
} |
} |
268 |
free(temp_dquants); |
xvid_free(temp_dquants); |
269 |
} |
} |
270 |
|
|
271 |
if(pEnc->mbParam.global_flags & XVID_H263QUANT) { |
if(pEnc->mbParam.global_flags & XVID_H263QUANT) { |
326 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
327 |
} |
} |
328 |
|
|
329 |
|
EMMS(); |
330 |
|
|
331 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
332 |
{ |
{ |
333 |
RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra); |
RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra); |