Parent Directory
|
Revision Log
Revision 85 -
(view)
(download)
Original Path: trunk/xvidcore/src/encoder.c
1 : | Isibaar | 3 | #include <stdlib.h> |
2 : | #include <stdio.h> | ||
3 : | #include <math.h> | ||
4 : | |||
5 : | #include "encoder.h" | ||
6 : | #include "prediction/mbprediction.h" | ||
7 : | #include "global.h" | ||
8 : | #include "utils/timer.h" | ||
9 : | #include "image/image.h" | ||
10 : | #include "bitstream/cbp.h" | ||
11 : | #include "utils/mbfunctions.h" | ||
12 : | #include "bitstream/bitstream.h" | ||
13 : | #include "bitstream/mbcoding.h" | ||
14 : | #include "utils/ratecontrol.h" | ||
15 : | #include "utils/emms.h" | ||
16 : | #include "bitstream/mbcoding.h" | ||
17 : | #include "quant/adapt_quant.h" | ||
18 : | Isibaar | 4 | #include "quant/quant_matrix.h" |
19 : | Isibaar | 41 | #include "utils/mem_align.h" |
20 : | Isibaar | 3 | |
21 : | #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT | ||
22 : | |||
23 : | |||
24 : | static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); | ||
25 : | static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header); | ||
26 : | |||
27 : | static int DQtab[4] = | ||
28 : | { | ||
29 : | -1, -2, 1, 2 | ||
30 : | }; | ||
31 : | |||
32 : | static int iDQtab[5] = | ||
33 : | { | ||
34 : | 1, 0, NO_CHANGE, 2, 3 | ||
35 : | }; | ||
36 : | |||
37 : | |||
38 : | int encoder_create(XVID_ENC_PARAM * pParam) | ||
39 : | { | ||
40 : | edgomez | 13 | Encoder *pEnc; |
41 : | Isibaar | 3 | uint32_t i; |
42 : | |||
43 : | edgomez | 13 | pParam->handle = NULL; |
44 : | Isibaar | 3 | |
45 : | edgomez | 13 | ENC_CHECK(pParam); |
46 : | Isibaar | 3 | |
47 : | edgomez | 13 | ENC_CHECK(pParam->width > 0 && pParam->width <= 1920); |
48 : | ENC_CHECK(pParam->height > 0 && pParam->height <= 1280); | ||
49 : | ENC_CHECK(!(pParam->width % 2)); | ||
50 : | ENC_CHECK(!(pParam->height % 2)); | ||
51 : | Isibaar | 3 | |
52 : | if (pParam->fincr <= 0 || pParam->fbase <= 0) | ||
53 : | { | ||
54 : | pParam->fincr = 1; | ||
55 : | pParam->fbase = 25; | ||
56 : | } | ||
57 : | |||
58 : | // simplify the "fincr/fbase" fraction | ||
59 : | // (neccessary, since windows supplies us with huge numbers) | ||
60 : | |||
61 : | i = pParam->fincr; | ||
62 : | while (i > 1) | ||
63 : | { | ||
64 : | if (pParam->fincr % i == 0 && pParam->fbase % i == 0) | ||
65 : | { | ||
66 : | pParam->fincr /= i; | ||
67 : | pParam->fbase /= i; | ||
68 : | i = pParam->fincr; | ||
69 : | continue; | ||
70 : | } | ||
71 : | i--; | ||
72 : | } | ||
73 : | |||
74 : | if (pParam->fbase > 65535) | ||
75 : | { | ||
76 : | float div = (float)pParam->fbase / 65535; | ||
77 : | pParam->fbase = (int)(pParam->fbase / div); | ||
78 : | pParam->fincr = (int)(pParam->fincr / div); | ||
79 : | } | ||
80 : | |||
81 : | if (pParam->bitrate <= 0) | ||
82 : | pParam->bitrate = 900000; | ||
83 : | |||
84 : | edgomez | 13 | if (pParam->rc_buffersize <= 0) |
85 : | h | 64 | pParam->rc_buffersize = 16; |
86 : | Isibaar | 3 | |
87 : | edgomez | 13 | if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
88 : | Isibaar | 3 | pParam->min_quantizer = 1; |
89 : | |||
90 : | edgomez | 13 | if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
91 : | Isibaar | 3 | pParam->max_quantizer = 31; |
92 : | |||
93 : | edgomez | 13 | if (pParam->max_key_interval == 0) /* 1 keyframe each 10 seconds */ |
94 : | Isibaar | 3 | pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
95 : | |||
96 : | edgomez | 13 | if (pParam->max_quantizer < pParam->min_quantizer) |
97 : | Isibaar | 3 | pParam->max_quantizer = pParam->min_quantizer; |
98 : | |||
99 : | canard | 66 | if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL) |
100 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
101 : | |||
102 : | /* Fill members of Encoder structure */ | ||
103 : | |||
104 : | edgomez | 13 | pEnc->mbParam.width = pParam->width; |
105 : | pEnc->mbParam.height = pParam->height; | ||
106 : | Isibaar | 3 | |
107 : | pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; | ||
108 : | pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; | ||
109 : | |||
110 : | pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; | ||
111 : | pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; | ||
112 : | |||
113 : | edgomez | 13 | pEnc->sStat.fMvPrevSigma = -1; |
114 : | Isibaar | 3 | |
115 : | /* Fill rate control parameters */ | ||
116 : | |||
117 : | edgomez | 13 | pEnc->mbParam.quant = 4; |
118 : | Isibaar | 3 | |
119 : | pEnc->bitrate = pParam->bitrate; | ||
120 : | |||
121 : | edgomez | 13 | pEnc->iFrameNum = 0; |
122 : | pEnc->iMaxKeyInterval = pParam->max_key_interval; | ||
123 : | Isibaar | 3 | |
124 : | h | 85 | if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
125 : | { | ||
126 : | xvid_free(pEnc); | ||
127 : | return XVID_ERR_MEMORY; | ||
128 : | } | ||
129 : | Isibaar | 3 | |
130 : | h | 85 | 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); | ||
133 : | xvid_free(pEnc); | ||
134 : | return XVID_ERR_MEMORY; | ||
135 : | } | ||
136 : | Isibaar | 3 | |
137 : | h | 85 | if (image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
138 : | { | ||
139 : | 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); | ||
141 : | xvid_free(pEnc); | ||
142 : | return XVID_ERR_MEMORY; | ||
143 : | } | ||
144 : | Isibaar | 3 | |
145 : | h | 85 | if (image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
146 : | edgomez | 13 | { |
147 : | Isibaar | 3 | 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); | ||
149 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
150 : | h | 85 | xvid_free(pEnc); |
151 : | return XVID_ERR_MEMORY; | ||
152 : | } | ||
153 : | |||
154 : | if (image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) | ||
155 : | { | ||
156 : | image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
157 : | 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); | ||
159 : | Isibaar | 3 | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
160 : | h | 85 | xvid_free(pEnc); |
161 : | return XVID_ERR_MEMORY; | ||
162 : | } | ||
163 : | |||
164 : | pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE); | ||
165 : | if (pEnc->pMBs == NULL) | ||
166 : | { | ||
167 : | image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
168 : | image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
169 : | 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); | ||
171 : | Isibaar | 3 | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
172 : | h | 85 | xvid_free(pEnc); |
173 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
174 : | } | ||
175 : | |||
176 : | // init macroblock array | ||
177 : | for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++) | ||
178 : | { | ||
179 : | pEnc->pMBs[i].dquant = NO_CHANGE; | ||
180 : | } | ||
181 : | |||
182 : | edgomez | 13 | pParam->handle = (void *)pEnc; |
183 : | Isibaar | 3 | |
184 : | if (pParam->bitrate) | ||
185 : | { | ||
186 : | h | 64 | RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase * 100 / pParam->fincr, |
187 : | pParam->max_quantizer, pParam->min_quantizer); | ||
188 : | Isibaar | 3 | } |
189 : | |||
190 : | create_vlc_tables(); | ||
191 : | Isibaar | 36 | init_timer(); |
192 : | Isibaar | 3 | |
193 : | return XVID_ERR_OK; | ||
194 : | } | ||
195 : | |||
196 : | |||
197 : | int encoder_destroy(Encoder * pEnc) | ||
198 : | { | ||
199 : | edgomez | 13 | ENC_CHECK(pEnc); |
200 : | ENC_CHECK(pEnc->sCurrent.y); | ||
201 : | ENC_CHECK(pEnc->sReference.y); | ||
202 : | Isibaar | 3 | |
203 : | Isibaar | 41 | xvid_free(pEnc->pMBs); |
204 : | edgomez | 13 | 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); | ||
206 : | 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); | ||
208 : | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
209 : | Isibaar | 41 | xvid_free(pEnc); |
210 : | Isibaar | 3 | |
211 : | destroy_vlc_tables(); | ||
212 : | |||
213 : | edgomez | 13 | return XVID_ERR_OK; |
214 : | Isibaar | 3 | } |
215 : | |||
216 : | int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) | ||
217 : | { | ||
218 : | edgomez | 13 | uint16_t x, y; |
219 : | Bitstream bs; | ||
220 : | uint32_t bits; | ||
221 : | Isibaar | 4 | uint16_t write_vol_header = 0; |
222 : | Isibaar | 3 | |
223 : | start_global_timer(); | ||
224 : | |||
225 : | edgomez | 13 | ENC_CHECK(pEnc); |
226 : | ENC_CHECK(pFrame); | ||
227 : | ENC_CHECK(pFrame->bitstream); | ||
228 : | ENC_CHECK(pFrame->image); | ||
229 : | Isibaar | 3 | |
230 : | pEnc->mbParam.global_flags = pFrame->general; | ||
231 : | pEnc->mbParam.motion_flags = pFrame->motion; | ||
232 : | |||
233 : | start_timer(); | ||
234 : | if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, | ||
235 : | edgomez | 13 | pFrame->image, pFrame->colorspace)) |
236 : | Isibaar | 3 | { |
237 : | return XVID_ERR_FORMAT; | ||
238 : | } | ||
239 : | stop_conv_timer(); | ||
240 : | |||
241 : | Isibaar | 41 | EMMS(); |
242 : | |||
243 : | edgomez | 13 | BitstreamInit(&bs, pFrame->bitstream, 0); |
244 : | Isibaar | 3 | |
245 : | if (pFrame->quant == 0) | ||
246 : | { | ||
247 : | pEnc->mbParam.quant = RateControlGetQ(0); | ||
248 : | } | ||
249 : | else | ||
250 : | { | ||
251 : | pEnc->mbParam.quant = pFrame->quant; | ||
252 : | } | ||
253 : | |||
254 : | if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) | ||
255 : | { | ||
256 : | canard | 66 | int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
257 : | Isibaar | 3 | |
258 : | edgomez | 78 | pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, |
259 : | pEnc->mbParam.width, | ||
260 : | temp_dquants, | ||
261 : | h | 84 | pEnc->mbParam.quant, |
262 : | pEnc->mbParam.quant, | ||
263 : | 2*pEnc->mbParam.quant, | ||
264 : | edgomez | 78 | pEnc->mbParam.mb_width, |
265 : | pEnc->mbParam.mb_height); | ||
266 : | Isibaar | 3 | |
267 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) | ||
268 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) | ||
269 : | { | ||
270 : | MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; | ||
271 : | pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; | ||
272 : | } | ||
273 : | Isibaar | 41 | xvid_free(temp_dquants); |
274 : | Isibaar | 3 | } |
275 : | |||
276 : | Isibaar | 20 | if(pEnc->mbParam.global_flags & XVID_H263QUANT) { |
277 : | if(pEnc->mbParam.quant_type != H263_QUANT) | ||
278 : | write_vol_header = 1; | ||
279 : | pEnc->mbParam.quant_type = H263_QUANT; | ||
280 : | Isibaar | 3 | } |
281 : | Isibaar | 20 | else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) { |
282 : | int ret1, ret2; | ||
283 : | Isibaar | 3 | |
284 : | edgomez | 78 | ret1 = ret2 = 0; |
285 : | |||
286 : | Isibaar | 20 | if(pEnc->mbParam.quant_type != MPEG4_QUANT) |
287 : | write_vol_header = 1; | ||
288 : | |||
289 : | pEnc->mbParam.quant_type = MPEG4_QUANT; | ||
290 : | |||
291 : | if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) { | ||
292 : | if(pFrame->quant_intra_matrix != NULL) | ||
293 : | ret1 = set_intra_matrix(pFrame->quant_intra_matrix); | ||
294 : | if(pFrame->quant_inter_matrix != NULL) | ||
295 : | ret2 = set_inter_matrix(pFrame->quant_inter_matrix); | ||
296 : | } | ||
297 : | else { | ||
298 : | ret1 = set_intra_matrix(get_default_intra_matrix()); | ||
299 : | ret2 = set_inter_matrix(get_default_inter_matrix()); | ||
300 : | } | ||
301 : | Isibaar | 4 | if(write_vol_header == 0) |
302 : | write_vol_header = ret1 | ret2; | ||
303 : | } | ||
304 : | Isibaar | 3 | |
305 : | if (pFrame->intra < 0) | ||
306 : | edgomez | 13 | { |
307 : | Isibaar | 3 | if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
308 : | edgomez | 13 | && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
309 : | Isibaar | 3 | |
310 : | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); | ||
311 : | else | ||
312 : | Isibaar | 4 | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
313 : | edgomez | 13 | } |
314 : | else | ||
315 : | { | ||
316 : | Isibaar | 3 | if (pFrame->intra == 1) |
317 : | edgomez | 13 | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
318 : | Isibaar | 3 | else |
319 : | Isibaar | 4 | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
320 : | edgomez | 13 | } |
321 : | Isibaar | 3 | |
322 : | BitstreamPutBits(&bs, 0xFFFF, 16); | ||
323 : | edgomez | 13 | BitstreamPutBits(&bs, 0xFFFF, 16); |
324 : | BitstreamPad(&bs); | ||
325 : | pFrame->length = BitstreamLength(&bs); | ||
326 : | h | 29 | |
327 : | Isibaar | 3 | if (pResult) |
328 : | edgomez | 13 | { |
329 : | Isibaar | 3 | pResult->quant = pEnc->mbParam.quant; |
330 : | pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); | ||
331 : | pResult->kblks = pEnc->sStat.kblks; | ||
332 : | pResult->mblks = pEnc->sStat.mblks; | ||
333 : | pResult->ublks = pEnc->sStat.ublks; | ||
334 : | edgomez | 13 | } |
335 : | Isibaar | 3 | |
336 : | Isibaar | 41 | EMMS(); |
337 : | |||
338 : | h | 29 | if (pFrame->quant == 0) |
339 : | Isibaar | 3 | { |
340 : | RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra); | ||
341 : | } | ||
342 : | |||
343 : | pEnc->iFrameNum++; | ||
344 : | edgomez | 13 | image_swap(&pEnc->sCurrent, &pEnc->sReference); |
345 : | Isibaar | 3 | |
346 : | stop_global_timer(); | ||
347 : | write_timer(); | ||
348 : | |||
349 : | return XVID_ERR_OK; | ||
350 : | } | ||
351 : | |||
352 : | |||
353 : | static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { | ||
354 : | |||
355 : | pMB->mode = MODE_INTRA; | ||
356 : | |||
357 : | if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) { | ||
358 : | if(pMB->dquant != NO_CHANGE) | ||
359 : | { | ||
360 : | pMB->mode = MODE_INTRA_Q; | ||
361 : | pEnc->mbParam.quant += DQtab[pMB->dquant]; | ||
362 : | |||
363 : | if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31; | ||
364 : | if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1; | ||
365 : | } | ||
366 : | } | ||
367 : | |||
368 : | pMB->quant = pEnc->mbParam.quant; | ||
369 : | } | ||
370 : | |||
371 : | |||
372 : | static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) | ||
373 : | { | ||
374 : | edgomez | 78 | |
375 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
376 : | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); | ||
377 : | |||
378 : | edgomez | 13 | uint16_t x, y; |
379 : | Isibaar | 3 | |
380 : | edgomez | 13 | pEnc->iFrameNum = 0; |
381 : | pEnc->mbParam.rounding_type = 1; | ||
382 : | pEnc->mbParam.coding_type = I_VOP; | ||
383 : | Isibaar | 3 | |
384 : | h | 69 | BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
385 : | BitstreamWriteVopHeader(bs, &pEnc->mbParam); | ||
386 : | Isibaar | 3 | |
387 : | edgomez | 13 | *pBits = BitstreamPos(bs); |
388 : | Isibaar | 3 | |
389 : | pEnc->sStat.iTextBits = 0; | ||
390 : | pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; | ||
391 : | pEnc->sStat.mblks = pEnc->sStat.ublks = 0; | ||
392 : | |||
393 : | edgomez | 13 | for (y = 0; y < pEnc->mbParam.mb_height; y++) |
394 : | Isibaar | 3 | for (x = 0; x < pEnc->mbParam.mb_width; x++) |
395 : | { | ||
396 : | edgomez | 13 | MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
397 : | Isibaar | 3 | |
398 : | CodeIntraMB(pEnc, pMB); | ||
399 : | |||
400 : | h | 69 | MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, &pEnc->sCurrent); |
401 : | Isibaar | 3 | |
402 : | start_timer(); | ||
403 : | MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs); | ||
404 : | stop_prediction_timer(); | ||
405 : | |||
406 : | start_timer(); | ||
407 : | MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat); | ||
408 : | stop_coding_timer(); | ||
409 : | } | ||
410 : | |||
411 : | emms(); | ||
412 : | |||
413 : | edgomez | 13 | *pBits = BitstreamPos(bs) - *pBits; |
414 : | pEnc->sStat.fMvPrevSigma = -1; | ||
415 : | pEnc->sStat.iMvSum = 0; | ||
416 : | pEnc->sStat.iMvCount = 0; | ||
417 : | pEnc->mbParam.fixed_code = 2; | ||
418 : | Isibaar | 3 | |
419 : | edgomez | 13 | return 1; // intra |
420 : | Isibaar | 3 | } |
421 : | |||
422 : | |||
423 : | #define INTRA_THRESHOLD 0.5 | ||
424 : | |||
425 : | static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) | ||
426 : | { | ||
427 : | edgomez | 13 | float fSigma; |
428 : | Isibaar | 42 | |
429 : | edgomez | 78 | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
430 : | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); | ||
431 : | |||
432 : | Isibaar | 3 | int iLimit; |
433 : | edgomez | 13 | uint32_t x, y; |
434 : | int iSearchRange; | ||
435 : | Isibaar | 3 | bool bIntra; |
436 : | |||
437 : | edgomez | 13 | IMAGE *pCurrent = &pEnc->sCurrent; |
438 : | IMAGE *pRef = &pEnc->sReference; | ||
439 : | Isibaar | 3 | |
440 : | h | 69 | start_timer(); |
441 : | edgomez | 78 | image_setedges(pRef, |
442 : | h | 85 | pEnc->mbParam.edged_width, |
443 : | pEnc->mbParam.edged_height, | ||
444 : | pEnc->mbParam.width, | ||
445 : | pEnc->mbParam.height, | ||
446 : | pEnc->mbParam.global_flags & XVID_INTERLACING); | ||
447 : | h | 69 | stop_edges_timer(); |
448 : | Isibaar | 3 | |
449 : | edgomez | 13 | pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type; |
450 : | Isibaar | 3 | |
451 : | if (!force_inter) | ||
452 : | iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); | ||
453 : | edgomez | 13 | else |
454 : | Isibaar | 3 | iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
455 : | |||
456 : | h | 85 | if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) { |
457 : | Isibaar | 3 | start_timer(); |
458 : | h | 85 | image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
459 : | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, | ||
460 : | pEnc->mbParam.rounding_type); | ||
461 : | Isibaar | 3 | stop_inter_timer(); |
462 : | } | ||
463 : | |||
464 : | start_timer(); | ||
465 : | h | 85 | bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference, |
466 : | &pEnc->vInterH, &pEnc->vInterV, | ||
467 : | &pEnc->vInterHV, &pEnc->sCurrent, iLimit); | ||
468 : | Isibaar | 3 | stop_motion_timer(); |
469 : | |||
470 : | if (bIntra == 1) | ||
471 : | return FrameCodeI(pEnc, bs, pBits); | ||
472 : | |||
473 : | edgomez | 13 | pEnc->mbParam.coding_type = P_VOP; |
474 : | Isibaar | 3 | |
475 : | if(vol_header) | ||
476 : | h | 69 | BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
477 : | Isibaar | 3 | |
478 : | h | 69 | BitstreamWriteVopHeader(bs, &pEnc->mbParam); |
479 : | Isibaar | 3 | |
480 : | edgomez | 13 | *pBits = BitstreamPos(bs); |
481 : | Isibaar | 3 | |
482 : | edgomez | 13 | pEnc->sStat.iTextBits = 0; |
483 : | pEnc->sStat.iMvSum = 0; | ||
484 : | pEnc->sStat.iMvCount = 0; | ||
485 : | Isibaar | 3 | pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
486 : | |||
487 : | edgomez | 13 | for(y = 0; y < pEnc->mbParam.mb_height; y++) |
488 : | Isibaar | 3 | { |
489 : | for(x = 0; x < pEnc->mbParam.mb_width; x++) | ||
490 : | { | ||
491 : | MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; | ||
492 : | |||
493 : | edgomez | 13 | bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
494 : | Isibaar | 3 | |
495 : | if (!bIntra) | ||
496 : | edgomez | 13 | { |
497 : | Isibaar | 3 | start_timer(); |
498 : | edgomez | 78 | MBMotionCompensation(pMB, |
499 : | x, y, | ||
500 : | &pEnc->sReference, | ||
501 : | &pEnc->vInterH, | ||
502 : | h | 85 | &pEnc->vInterV, |
503 : | &pEnc->vInterHV, | ||
504 : | edgomez | 78 | &pEnc->sCurrent, |
505 : | dct_codes, | ||
506 : | edgomez | 13 | pEnc->mbParam.width, |
507 : | pEnc->mbParam.height, | ||
508 : | pEnc->mbParam.edged_width, | ||
509 : | pEnc->mbParam.rounding_type); | ||
510 : | Isibaar | 3 | stop_comp_timer(); |
511 : | |||
512 : | if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) { | ||
513 : | if(pMB->dquant != NO_CHANGE) { | ||
514 : | pMB->mode = MODE_INTER_Q; | ||
515 : | pEnc->mbParam.quant += DQtab[pMB->dquant]; | ||
516 : | if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31; | ||
517 : | else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1; | ||
518 : | } | ||
519 : | } | ||
520 : | pMB->quant = pEnc->mbParam.quant; | ||
521 : | |||
522 : | h | 69 | pMB->field_pred = 0; |
523 : | |||
524 : | pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent); | ||
525 : | edgomez | 13 | } |
526 : | Isibaar | 3 | else |
527 : | { | ||
528 : | CodeIntraMB(pEnc, pMB); | ||
529 : | h | 69 | MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent); |
530 : | Isibaar | 3 | } |
531 : | |||
532 : | edgomez | 13 | start_timer(); |
533 : | Isibaar | 3 | MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs); |
534 : | stop_prediction_timer(); | ||
535 : | |||
536 : | if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) | ||
537 : | { | ||
538 : | pEnc->sStat.kblks++; | ||
539 : | } | ||
540 : | else if (pMB->cbp || | ||
541 : | edgomez | 13 | pMB->mvs[0].x || pMB->mvs[0].y || |
542 : | pMB->mvs[1].x || pMB->mvs[1].y || | ||
543 : | pMB->mvs[2].x || pMB->mvs[2].y || | ||
544 : | pMB->mvs[3].x || pMB->mvs[3].y) | ||
545 : | Isibaar | 3 | { |
546 : | pEnc->sStat.mblks++; | ||
547 : | } | ||
548 : | else | ||
549 : | { | ||
550 : | pEnc->sStat.ublks++; | ||
551 : | } | ||
552 : | |||
553 : | start_timer(); | ||
554 : | MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat); | ||
555 : | stop_coding_timer(); | ||
556 : | } | ||
557 : | } | ||
558 : | |||
559 : | emms(); | ||
560 : | |||
561 : | if (pEnc->sStat.iMvCount == 0) | ||
562 : | pEnc->sStat.iMvCount = 1; | ||
563 : | |||
564 : | edgomez | 13 | fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
565 : | Isibaar | 3 | |
566 : | edgomez | 13 | iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code); |
567 : | Isibaar | 3 | |
568 : | edgomez | 13 | if ((fSigma > iSearchRange / 3) |
569 : | && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128 | ||
570 : | { | ||
571 : | Isibaar | 3 | pEnc->mbParam.fixed_code++; |
572 : | iSearchRange *= 2; | ||
573 : | edgomez | 13 | } |
574 : | else if ((fSigma < iSearchRange / 6) | ||
575 : | && (pEnc->sStat.fMvPrevSigma >= 0) | ||
576 : | && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) | ||
577 : | && (pEnc->mbParam.fixed_code >= 2)) // minimum search range 16 | ||
578 : | { | ||
579 : | Isibaar | 3 | pEnc->mbParam.fixed_code--; |
580 : | iSearchRange /= 2; | ||
581 : | edgomez | 13 | } |
582 : | Isibaar | 3 | |
583 : | edgomez | 13 | pEnc->sStat.fMvPrevSigma = fSigma; |
584 : | Isibaar | 3 | |
585 : | *pBits = BitstreamPos(bs) - *pBits; | ||
586 : | |||
587 : | edgomez | 13 | return 0; // inter |
588 : | Isibaar | 3 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |