Parent Directory | Revision Log
Revision 1.31 - (view) (download)
1 : | edgomez | 1.29 | /************************************************************************** |
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 : | * 14.04.2002 added FrameCodeB() | ||
36 : | * | ||
37 : | chenm001 | 1.31 | * $Id: encoder.c,v 1.30 2002/04/28 23:31:00 edgomez Exp $ |
38 : | edgomez | 1.29 | * |
39 : | ***************************************************************************/ | ||
40 : | suxen_drol | 1.24 | |
41 : | Isibaar | 1.1 | #include <stdlib.h> |
42 : | #include <stdio.h> | ||
43 : | #include <math.h> | ||
44 : | |||
45 : | #include "encoder.h" | ||
46 : | #include "prediction/mbprediction.h" | ||
47 : | #include "global.h" | ||
48 : | #include "utils/timer.h" | ||
49 : | #include "image/image.h" | ||
50 : | #include "bitstream/cbp.h" | ||
51 : | #include "utils/mbfunctions.h" | ||
52 : | #include "bitstream/bitstream.h" | ||
53 : | #include "bitstream/mbcoding.h" | ||
54 : | #include "utils/ratecontrol.h" | ||
55 : | #include "utils/emms.h" | ||
56 : | #include "bitstream/mbcoding.h" | ||
57 : | #include "quant/adapt_quant.h" | ||
58 : | Isibaar | 1.2 | #include "quant/quant_matrix.h" |
59 : | Isibaar | 1.7 | #include "utils/mem_align.h" |
60 : | Isibaar | 1.1 | |
61 : | #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT | ||
62 : | suxen_drol | 1.27 | #define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
63 : | Isibaar | 1.1 | |
64 : | |||
65 : | static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); | ||
66 : | static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header); | ||
67 : | |||
68 : | static int DQtab[4] = | ||
69 : | { | ||
70 : | -1, -2, 1, 2 | ||
71 : | }; | ||
72 : | |||
73 : | static int iDQtab[5] = | ||
74 : | { | ||
75 : | 1, 0, NO_CHANGE, 2, 3 | ||
76 : | }; | ||
77 : | |||
78 : | |||
79 : | suxen_drol | 1.27 | void static image_null(IMAGE * image) |
80 : | { | ||
81 : | image->y = image->u = image->v = NULL; | ||
82 : | } | ||
83 : | |||
84 : | |||
85 : | Isibaar | 1.1 | int encoder_create(XVID_ENC_PARAM * pParam) |
86 : | { | ||
87 : | edgomez | 1.3 | Encoder *pEnc; |
88 : | Isibaar | 1.1 | uint32_t i; |
89 : | |||
90 : | edgomez | 1.3 | pParam->handle = NULL; |
91 : | Isibaar | 1.1 | |
92 : | edgomez | 1.3 | ENC_CHECK(pParam); |
93 : | Isibaar | 1.1 | |
94 : | edgomez | 1.3 | ENC_CHECK(pParam->width > 0 && pParam->width <= 1920); |
95 : | ENC_CHECK(pParam->height > 0 && pParam->height <= 1280); | ||
96 : | ENC_CHECK(!(pParam->width % 2)); | ||
97 : | ENC_CHECK(!(pParam->height % 2)); | ||
98 : | Isibaar | 1.1 | |
99 : | if (pParam->fincr <= 0 || pParam->fbase <= 0) | ||
100 : | { | ||
101 : | pParam->fincr = 1; | ||
102 : | pParam->fbase = 25; | ||
103 : | } | ||
104 : | |||
105 : | // simplify the "fincr/fbase" fraction | ||
106 : | // (neccessary, since windows supplies us with huge numbers) | ||
107 : | |||
108 : | i = pParam->fincr; | ||
109 : | while (i > 1) | ||
110 : | { | ||
111 : | if (pParam->fincr % i == 0 && pParam->fbase % i == 0) | ||
112 : | { | ||
113 : | pParam->fincr /= i; | ||
114 : | pParam->fbase /= i; | ||
115 : | i = pParam->fincr; | ||
116 : | continue; | ||
117 : | } | ||
118 : | i--; | ||
119 : | } | ||
120 : | |||
121 : | if (pParam->fbase > 65535) | ||
122 : | { | ||
123 : | float div = (float)pParam->fbase / 65535; | ||
124 : | pParam->fbase = (int)(pParam->fbase / div); | ||
125 : | pParam->fincr = (int)(pParam->fincr / div); | ||
126 : | } | ||
127 : | |||
128 : | h | 1.25 | if (pParam->rc_bitrate <= 0) |
129 : | pParam->rc_bitrate = 900000; | ||
130 : | Isibaar | 1.1 | |
131 : | h | 1.25 | if (pParam->rc_reaction_delay_factor <= 0) |
132 : | pParam->rc_reaction_delay_factor = 16; | ||
133 : | |||
134 : | if (pParam->rc_averaging_period <= 0) | ||
135 : | pParam->rc_averaging_period = 100; | ||
136 : | |||
137 : | if (pParam->rc_buffer <= 0) | ||
138 : | pParam->rc_buffer = 100; | ||
139 : | Isibaar | 1.1 | |
140 : | edgomez | 1.3 | if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
141 : | Isibaar | 1.1 | pParam->min_quantizer = 1; |
142 : | |||
143 : | edgomez | 1.3 | if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
144 : | Isibaar | 1.1 | pParam->max_quantizer = 31; |
145 : | |||
146 : | edgomez | 1.3 | if (pParam->max_key_interval == 0) /* 1 keyframe each 10 seconds */ |
147 : | Isibaar | 1.1 | pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
148 : | |||
149 : | edgomez | 1.3 | if (pParam->max_quantizer < pParam->min_quantizer) |
150 : | Isibaar | 1.1 | pParam->max_quantizer = pParam->min_quantizer; |
151 : | |||
152 : | canard | 1.10 | if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL) |
153 : | Isibaar | 1.1 | return XVID_ERR_MEMORY; |
154 : | |||
155 : | /* Fill members of Encoder structure */ | ||
156 : | |||
157 : | edgomez | 1.3 | pEnc->mbParam.width = pParam->width; |
158 : | pEnc->mbParam.height = pParam->height; | ||
159 : | Isibaar | 1.1 | |
160 : | pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; | ||
161 : | pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; | ||
162 : | |||
163 : | pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; | ||
164 : | pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; | ||
165 : | |||
166 : | edgomez | 1.3 | pEnc->sStat.fMvPrevSigma = -1; |
167 : | Isibaar | 1.1 | |
168 : | /* Fill rate control parameters */ | ||
169 : | |||
170 : | h | 1.25 | pEnc->bitrate = pParam->rc_bitrate; |
171 : | Isibaar | 1.1 | |
172 : | edgomez | 1.3 | pEnc->iFrameNum = 0; |
173 : | pEnc->iMaxKeyInterval = pParam->max_key_interval; | ||
174 : | Isibaar | 1.1 | |
175 : | suxen_drol | 1.27 | /* try to allocate frame memory */ |
176 : | h | 1.16 | |
177 : | suxen_drol | 1.27 | pEnc->current = NULL; |
178 : | pEnc->reference = NULL; | ||
179 : | if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL || | ||
180 : | (pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL) | ||
181 : | { | ||
182 : | if (pEnc->current) xvid_free(pEnc->current); | ||
183 : | xvid_free(pEnc); | ||
184 : | return XVID_ERR_MEMORY; | ||
185 : | } | ||
186 : | |||
187 : | /* try to allocate mb memory */ | ||
188 : | h | 1.16 | |
189 : | suxen_drol | 1.27 | pEnc->current->mbs = NULL; |
190 : | pEnc->reference->mbs = NULL; | ||
191 : | Isibaar | 1.1 | |
192 : | chl | 1.28 | #ifdef _DEBUG |
193 : | #ifdef WIN32 | ||
194 : | suxen_drol | 1.27 | OutputDebugString("malloc mbs"); |
195 : | chl | 1.28 | #endif |
196 : | #endif | ||
197 : | |||
198 : | suxen_drol | 1.27 | if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL || |
199 : | (pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) | ||
200 : | { | ||
201 : | if (pEnc->current->mbs) xvid_free(pEnc->current->mbs); | ||
202 : | xvid_free(pEnc->current); | ||
203 : | xvid_free(pEnc->reference); | ||
204 : | xvid_free(pEnc); | ||
205 : | } | ||
206 : | |||
207 : | /* try to allocate image memory */ | ||
208 : | |||
209 : | #ifdef _DEBUG | ||
210 : | image_null(&pEnc->sOriginal); | ||
211 : | #endif | ||
212 : | image_null(&pEnc->current->image); | ||
213 : | image_null(&pEnc->reference->image); | ||
214 : | image_null(&pEnc->vInterH); | ||
215 : | image_null(&pEnc->vInterV); | ||
216 : | image_null(&pEnc->vInterVf); | ||
217 : | image_null(&pEnc->vInterHV); | ||
218 : | image_null(&pEnc->vInterHVf); | ||
219 : | |||
220 : | chl | 1.28 | #ifdef _DEBUG |
221 : | #ifdef WIN32 | ||
222 : | suxen_drol | 1.27 | OutputDebugString("malloc images"); |
223 : | chl | 1.28 | #endif |
224 : | #endif | ||
225 : | suxen_drol | 1.27 | if ( |
226 : | #ifdef _DEBUG | ||
227 : | image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
228 : | #endif | ||
229 : | image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
230 : | image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
231 : | h | 1.17 | image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
232 : | image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
233 : | image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
234 : | image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
235 : | suxen_drol | 1.27 | image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
236 : | { | ||
237 : | Isibaar | 1.23 | #ifdef _DEBUG |
238 : | suxen_drol | 1.27 | image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
239 : | Isibaar | 1.23 | #endif |
240 : | suxen_drol | 1.27 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
241 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
242 : | Isibaar | 1.1 | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
243 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
244 : | h | 1.17 | image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
245 : | Isibaar | 1.1 | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
246 : | h | 1.17 | image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
247 : | suxen_drol | 1.27 | |
248 : | xvid_free(pEnc->current); | ||
249 : | xvid_free(pEnc->reference); | ||
250 : | xvid_free(pEnc); | ||
251 : | Isibaar | 1.1 | return XVID_ERR_MEMORY; |
252 : | } | ||
253 : | |||
254 : | edgomez | 1.3 | pParam->handle = (void *)pEnc; |
255 : | Isibaar | 1.1 | |
256 : | h | 1.25 | if (pParam->rc_bitrate) |
257 : | Isibaar | 1.1 | { |
258 : | h | 1.25 | RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor, |
259 : | pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr, | ||
260 : | pParam->max_quantizer, pParam->min_quantizer); | ||
261 : | Isibaar | 1.1 | } |
262 : | |||
263 : | Isibaar | 1.6 | init_timer(); |
264 : | Isibaar | 1.1 | |
265 : | return XVID_ERR_OK; | ||
266 : | } | ||
267 : | |||
268 : | |||
269 : | int encoder_destroy(Encoder * pEnc) | ||
270 : | { | ||
271 : | edgomez | 1.3 | ENC_CHECK(pEnc); |
272 : | Isibaar | 1.1 | |
273 : | suxen_drol | 1.27 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
274 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
275 : | edgomez | 1.3 | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
276 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
277 : | h | 1.18 | image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
278 : | edgomez | 1.3 | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
279 : | h | 1.18 | image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
280 : | Isibaar | 1.23 | #ifdef _DEBUG |
281 : | image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
282 : | #endif | ||
283 : | suxen_drol | 1.27 | xvid_free(pEnc->current->mbs); |
284 : | xvid_free(pEnc->current); | ||
285 : | |||
286 : | xvid_free(pEnc->reference->mbs); | ||
287 : | xvid_free(pEnc->reference); | ||
288 : | |||
289 : | Isibaar | 1.7 | xvid_free(pEnc); |
290 : | edgomez | 1.3 | return XVID_ERR_OK; |
291 : | Isibaar | 1.1 | } |
292 : | |||
293 : | int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) | ||
294 : | { | ||
295 : | edgomez | 1.3 | uint16_t x, y; |
296 : | Bitstream bs; | ||
297 : | uint32_t bits; | ||
298 : | Isibaar | 1.2 | uint16_t write_vol_header = 0; |
299 : | Isibaar | 1.23 | #ifdef _DEBUG |
300 : | float psnr; | ||
301 : | uint8_t temp[100]; | ||
302 : | #endif | ||
303 : | Isibaar | 1.1 | |
304 : | start_global_timer(); | ||
305 : | |||
306 : | edgomez | 1.3 | ENC_CHECK(pEnc); |
307 : | ENC_CHECK(pFrame); | ||
308 : | ENC_CHECK(pFrame->bitstream); | ||
309 : | ENC_CHECK(pFrame->image); | ||
310 : | Isibaar | 1.1 | |
311 : | suxen_drol | 1.27 | SWAP(pEnc->current, pEnc->reference); |
312 : | |||
313 : | pEnc->current->global_flags = pFrame->general; | ||
314 : | pEnc->current->motion_flags = pFrame->motion; | ||
315 : | h | 1.20 | pEnc->mbParam.hint = &pFrame->hint; |
316 : | Isibaar | 1.1 | |
317 : | start_timer(); | ||
318 : | suxen_drol | 1.27 | if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
319 : | edgomez | 1.3 | pFrame->image, pFrame->colorspace)) |
320 : | Isibaar | 1.1 | { |
321 : | return XVID_ERR_FORMAT; | ||
322 : | } | ||
323 : | stop_conv_timer(); | ||
324 : | |||
325 : | Isibaar | 1.23 | #ifdef _DEBUG |
326 : | chenm001 | 1.31 | image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
327 : | Isibaar | 1.23 | #endif |
328 : | |||
329 : | suxen_drol | 1.27 | EMMS(); |
330 : | |||
331 : | edgomez | 1.3 | BitstreamInit(&bs, pFrame->bitstream, 0); |
332 : | Isibaar | 1.1 | |
333 : | if (pFrame->quant == 0) | ||
334 : | { | ||
335 : | suxen_drol | 1.27 | pEnc->current->quant = RateControlGetQ(0); |
336 : | Isibaar | 1.1 | } |
337 : | else | ||
338 : | { | ||
339 : | suxen_drol | 1.27 | pEnc->current->quant = pFrame->quant; |
340 : | Isibaar | 1.1 | } |
341 : | |||
342 : | suxen_drol | 1.27 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
343 : | Isibaar | 1.1 | { |
344 : | canard | 1.10 | int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
345 : | Isibaar | 1.1 | |
346 : | suxen_drol | 1.27 | pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
347 : | pEnc->mbParam.edged_width, // stride | ||
348 : | edgomez | 1.13 | temp_dquants, |
349 : | suxen_drol | 1.27 | pEnc->current->quant, |
350 : | pEnc->current->quant, // min_quant | ||
351 : | 2*pEnc->current->quant, // max_quant | ||
352 : | edgomez | 1.13 | pEnc->mbParam.mb_width, |
353 : | pEnc->mbParam.mb_height); | ||
354 : | Isibaar | 1.1 | |
355 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) | ||
356 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) | ||
357 : | { | ||
358 : | suxen_drol | 1.27 | MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
359 : | Isibaar | 1.1 | pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
360 : | } | ||
361 : | Isibaar | 1.7 | xvid_free(temp_dquants); |
362 : | Isibaar | 1.1 | } |
363 : | |||
364 : | suxen_drol | 1.27 | if (pEnc->current->global_flags & XVID_H263QUANT) { |
365 : | if(pEnc->mbParam.m_quant_type != H263_QUANT) | ||
366 : | Isibaar | 1.4 | write_vol_header = 1; |
367 : | suxen_drol | 1.27 | pEnc->mbParam.m_quant_type = H263_QUANT; |
368 : | Isibaar | 1.1 | } |
369 : | suxen_drol | 1.27 | else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
370 : | Isibaar | 1.4 | int ret1, ret2; |
371 : | Isibaar | 1.1 | |
372 : | edgomez | 1.13 | ret1 = ret2 = 0; |
373 : | |||
374 : | suxen_drol | 1.27 | if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
375 : | Isibaar | 1.4 | write_vol_header = 1; |
376 : | |||
377 : | suxen_drol | 1.27 | pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
378 : | Isibaar | 1.4 | |
379 : | suxen_drol | 1.27 | if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
380 : | Isibaar | 1.4 | if(pFrame->quant_intra_matrix != NULL) |
381 : | ret1 = set_intra_matrix(pFrame->quant_intra_matrix); | ||
382 : | if(pFrame->quant_inter_matrix != NULL) | ||
383 : | ret2 = set_inter_matrix(pFrame->quant_inter_matrix); | ||
384 : | } | ||
385 : | else { | ||
386 : | ret1 = set_intra_matrix(get_default_intra_matrix()); | ||
387 : | ret2 = set_inter_matrix(get_default_inter_matrix()); | ||
388 : | } | ||
389 : | Isibaar | 1.2 | if(write_vol_header == 0) |
390 : | write_vol_header = ret1 | ret2; | ||
391 : | } | ||
392 : | Isibaar | 1.1 | |
393 : | if (pFrame->intra < 0) | ||
394 : | edgomez | 1.3 | { |
395 : | Isibaar | 1.1 | if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
396 : | edgomez | 1.3 | && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
397 : | Isibaar | 1.1 | |
398 : | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); | ||
399 : | else | ||
400 : | Isibaar | 1.2 | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
401 : | edgomez | 1.3 | } |
402 : | else | ||
403 : | { | ||
404 : | Isibaar | 1.1 | if (pFrame->intra == 1) |
405 : | edgomez | 1.3 | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
406 : | Isibaar | 1.1 | else |
407 : | Isibaar | 1.2 | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
408 : | edgomez | 1.3 | } |
409 : | Isibaar | 1.1 | |
410 : | BitstreamPutBits(&bs, 0xFFFF, 16); | ||
411 : | edgomez | 1.3 | BitstreamPutBits(&bs, 0xFFFF, 16); |
412 : | BitstreamPad(&bs); | ||
413 : | pFrame->length = BitstreamLength(&bs); | ||
414 : | h | 1.5 | |
415 : | Isibaar | 1.1 | if (pResult) |
416 : | edgomez | 1.3 | { |
417 : | suxen_drol | 1.27 | pResult->quant = pEnc->current->quant; |
418 : | Isibaar | 1.1 | pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
419 : | pResult->kblks = pEnc->sStat.kblks; | ||
420 : | pResult->mblks = pEnc->sStat.mblks; | ||
421 : | pResult->ublks = pEnc->sStat.ublks; | ||
422 : | edgomez | 1.3 | } |
423 : | Isibaar | 1.1 | |
424 : | Isibaar | 1.7 | EMMS(); |
425 : | |||
426 : | h | 1.5 | if (pFrame->quant == 0) |
427 : | Isibaar | 1.1 | { |
428 : | suxen_drol | 1.27 | RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
429 : | Isibaar | 1.1 | } |
430 : | Isibaar | 1.23 | |
431 : | #ifdef _DEBUG | ||
432 : | suxen_drol | 1.27 | psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
433 : | Isibaar | 1.23 | pEnc->mbParam.width, pEnc->mbParam.height); |
434 : | |||
435 : | sprintf(temp, "PSNR: %f\n", psnr); | ||
436 : | DEBUG(temp); | ||
437 : | #endif | ||
438 : | Isibaar | 1.1 | |
439 : | pEnc->iFrameNum++; | ||
440 : | |||
441 : | stop_global_timer(); | ||
442 : | write_timer(); | ||
443 : | |||
444 : | return XVID_ERR_OK; | ||
445 : | } | ||
446 : | |||
447 : | |||
448 : | static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { | ||
449 : | |||
450 : | pMB->mode = MODE_INTRA; | ||
451 : | |||
452 : | suxen_drol | 1.27 | /* zero mv statistics */ |
453 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; | ||
454 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; | ||
455 : | pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; | ||
456 : | pMB->sad16 = 0; | ||
457 : | |||
458 : | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { | ||
459 : | Isibaar | 1.1 | if(pMB->dquant != NO_CHANGE) |
460 : | { | ||
461 : | pMB->mode = MODE_INTRA_Q; | ||
462 : | suxen_drol | 1.27 | pEnc->current->quant += DQtab[pMB->dquant]; |
463 : | Isibaar | 1.1 | |
464 : | suxen_drol | 1.27 | if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
465 : | if (pEnc->current->quant < 1) pEnc->current->quant = 1; | ||
466 : | Isibaar | 1.1 | } |
467 : | } | ||
468 : | |||
469 : | suxen_drol | 1.27 | pMB->quant = pEnc->current->quant; |
470 : | Isibaar | 1.1 | } |
471 : | |||
472 : | |||
473 : | h | 1.20 | #define FCODEBITS 3 |
474 : | #define MODEBITS 5 | ||
475 : | |||
476 : | void HintedMESet(Encoder * pEnc, int * intra) | ||
477 : | { | ||
478 : | HINTINFO * hint; | ||
479 : | Bitstream bs; | ||
480 : | int length, high; | ||
481 : | uint32_t x, y; | ||
482 : | |||
483 : | hint = pEnc->mbParam.hint; | ||
484 : | |||
485 : | if (hint->rawhints) | ||
486 : | { | ||
487 : | *intra = hint->mvhint.intra; | ||
488 : | } | ||
489 : | else | ||
490 : | { | ||
491 : | BitstreamInit(&bs, hint->hintstream, hint->hintlength); | ||
492 : | *intra = BitstreamGetBit(&bs); | ||
493 : | } | ||
494 : | |||
495 : | if (*intra) | ||
496 : | { | ||
497 : | return; | ||
498 : | } | ||
499 : | |||
500 : | suxen_drol | 1.27 | pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
501 : | h | 1.20 | |
502 : | suxen_drol | 1.27 | length = pEnc->current->fcode + 5; |
503 : | h | 1.20 | high = 1 << (length - 1); |
504 : | |||
505 : | for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) | ||
506 : | { | ||
507 : | for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) | ||
508 : | { | ||
509 : | suxen_drol | 1.27 | MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
510 : | h | 1.20 | MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
511 : | VECTOR pred[4]; | ||
512 : | VECTOR tmp; | ||
513 : | edgomez | 1.30 | int32_t dummy[4]; |
514 : | h | 1.20 | int vec; |
515 : | |||
516 : | pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); | ||
517 : | |||
518 : | h | 1.26 | pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
519 : | pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; | ||
520 : | |||
521 : | if (pMB->mode == MODE_INTER) | ||
522 : | h | 1.20 | { |
523 : | tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); | ||
524 : | tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); | ||
525 : | tmp.x -= (tmp.x >= high) ? high*2 : 0; | ||
526 : | tmp.y -= (tmp.y >= high) ? high*2 : 0; | ||
527 : | |||
528 : | suxen_drol | 1.27 | get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy); |
529 : | h | 1.20 | |
530 : | for (vec=0 ; vec<4 ; ++vec) | ||
531 : | { | ||
532 : | pMB->mvs[vec].x = tmp.x; | ||
533 : | pMB->mvs[vec].y = tmp.y; | ||
534 : | pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; | ||
535 : | pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; | ||
536 : | } | ||
537 : | } | ||
538 : | else if (pMB->mode == MODE_INTER4V) | ||
539 : | { | ||
540 : | for (vec=0 ; vec<4 ; ++vec) | ||
541 : | { | ||
542 : | tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); | ||
543 : | tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); | ||
544 : | tmp.x -= (tmp.x >= high) ? high*2 : 0; | ||
545 : | tmp.y -= (tmp.y >= high) ? high*2 : 0; | ||
546 : | |||
547 : | suxen_drol | 1.27 | get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
548 : | h | 1.20 | |
549 : | pMB->mvs[vec].x = tmp.x; | ||
550 : | pMB->mvs[vec].y = tmp.y; | ||
551 : | pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; | ||
552 : | pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; | ||
553 : | } | ||
554 : | } | ||
555 : | h | 1.26 | else // intra / stuffing / not_coded |
556 : | h | 1.20 | { |
557 : | for (vec=0 ; vec<4 ; ++vec) | ||
558 : | { | ||
559 : | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; | ||
560 : | h | 1.26 | } |
561 : | } | ||
562 : | |||
563 : | suxen_drol | 1.27 | if (pMB->mode == MODE_INTER4V && |
564 : | (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) | ||
565 : | h | 1.26 | { |
566 : | pMB->mode = MODE_INTRA; | ||
567 : | |||
568 : | for (vec=0 ; vec<4 ; ++vec) | ||
569 : | { | ||
570 : | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; | ||
571 : | h | 1.20 | } |
572 : | } | ||
573 : | } | ||
574 : | } | ||
575 : | } | ||
576 : | |||
577 : | |||
578 : | void HintedMEGet(Encoder * pEnc, int intra) | ||
579 : | { | ||
580 : | HINTINFO * hint; | ||
581 : | Bitstream bs; | ||
582 : | uint32_t x, y; | ||
583 : | int length, high; | ||
584 : | |||
585 : | hint = pEnc->mbParam.hint; | ||
586 : | |||
587 : | if (hint->rawhints) | ||
588 : | { | ||
589 : | hint->mvhint.intra = intra; | ||
590 : | } | ||
591 : | else | ||
592 : | { | ||
593 : | BitstreamInit(&bs, hint->hintstream, 0); | ||
594 : | BitstreamPutBit(&bs, intra); | ||
595 : | } | ||
596 : | |||
597 : | if (intra) | ||
598 : | { | ||
599 : | if (!hint->rawhints) | ||
600 : | { | ||
601 : | BitstreamPad(&bs); | ||
602 : | hint->hintlength = BitstreamLength(&bs); | ||
603 : | } | ||
604 : | return; | ||
605 : | } | ||
606 : | |||
607 : | suxen_drol | 1.27 | length = pEnc->current->fcode + 5; |
608 : | h | 1.20 | high = 1 << (length - 1); |
609 : | |||
610 : | if (hint->rawhints) | ||
611 : | { | ||
612 : | suxen_drol | 1.27 | hint->mvhint.fcode = pEnc->current->fcode; |
613 : | h | 1.20 | } |
614 : | else | ||
615 : | { | ||
616 : | suxen_drol | 1.27 | BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
617 : | h | 1.20 | } |
618 : | |||
619 : | for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) | ||
620 : | { | ||
621 : | for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) | ||
622 : | { | ||
623 : | suxen_drol | 1.27 | MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
624 : | h | 1.20 | MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
625 : | VECTOR tmp; | ||
626 : | |||
627 : | if (hint->rawhints) | ||
628 : | { | ||
629 : | bhint->mode = pMB->mode; | ||
630 : | } | ||
631 : | else | ||
632 : | { | ||
633 : | BitstreamPutBits(&bs, pMB->mode, MODEBITS); | ||
634 : | } | ||
635 : | |||
636 : | if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) | ||
637 : | { | ||
638 : | tmp.x = pMB->mvs[0].x; | ||
639 : | tmp.y = pMB->mvs[0].y; | ||
640 : | tmp.x += (tmp.x < 0) ? high*2 : 0; | ||
641 : | tmp.y += (tmp.y < 0) ? high*2 : 0; | ||
642 : | |||
643 : | if (hint->rawhints) | ||
644 : | { | ||
645 : | bhint->mvs[0].x = tmp.x; | ||
646 : | bhint->mvs[0].y = tmp.y; | ||
647 : | } | ||
648 : | else | ||
649 : | { | ||
650 : | BitstreamPutBits(&bs, tmp.x, length); | ||
651 : | BitstreamPutBits(&bs, tmp.y, length); | ||
652 : | } | ||
653 : | } | ||
654 : | else if (pMB->mode == MODE_INTER4V) | ||
655 : | { | ||
656 : | int vec; | ||
657 : | |||
658 : | for (vec=0 ; vec<4 ; ++vec) | ||
659 : | { | ||
660 : | tmp.x = pMB->mvs[vec].x; | ||
661 : | tmp.y = pMB->mvs[vec].y; | ||
662 : | tmp.x += (tmp.x < 0) ? high*2 : 0; | ||
663 : | tmp.y += (tmp.y < 0) ? high*2 : 0; | ||
664 : | |||
665 : | if (hint->rawhints) | ||
666 : | { | ||
667 : | bhint->mvs[vec].x = tmp.x; | ||
668 : | bhint->mvs[vec].y = tmp.y; | ||
669 : | } | ||
670 : | else | ||
671 : | { | ||
672 : | BitstreamPutBits(&bs, tmp.x, length); | ||
673 : | BitstreamPutBits(&bs, tmp.y, length); | ||
674 : | } | ||
675 : | } | ||
676 : | } | ||
677 : | } | ||
678 : | } | ||
679 : | |||
680 : | if (!hint->rawhints) | ||
681 : | { | ||
682 : | BitstreamPad(&bs); | ||
683 : | hint->hintlength = BitstreamLength(&bs); | ||
684 : | } | ||
685 : | } | ||
686 : | |||
687 : | |||
688 : | h | 1.21 | static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
689 : | { | ||
690 : | |||
691 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
692 : | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); | ||
693 : | |||
694 : | uint16_t x, y; | ||
695 : | |||
696 : | pEnc->iFrameNum = 0; | ||
697 : | suxen_drol | 1.27 | pEnc->mbParam.m_rounding_type = 1; |
698 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
699 : | pEnc->current->coding_type = I_VOP; | ||
700 : | h | 1.21 | |
701 : | suxen_drol | 1.27 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
702 : | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); | ||
703 : | h | 1.21 | |
704 : | *pBits = BitstreamPos(bs); | ||
705 : | |||
706 : | pEnc->sStat.iTextBits = 0; | ||
707 : | pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; | ||
708 : | pEnc->sStat.mblks = pEnc->sStat.ublks = 0; | ||
709 : | |||
710 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) | ||
711 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) | ||
712 : | { | ||
713 : | suxen_drol | 1.27 | MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
714 : | h | 1.21 | |
715 : | CodeIntraMB(pEnc, pMB); | ||
716 : | |||
717 : | suxen_drol | 1.27 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
718 : | h | 1.21 | |
719 : | start_timer(); | ||
720 : | suxen_drol | 1.27 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
721 : | h | 1.21 | stop_prediction_timer(); |
722 : | |||
723 : | start_timer(); | ||
724 : | suxen_drol | 1.27 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
725 : | h | 1.21 | stop_coding_timer(); |
726 : | } | ||
727 : | |||
728 : | emms(); | ||
729 : | |||
730 : | *pBits = BitstreamPos(bs) - *pBits; | ||
731 : | pEnc->sStat.fMvPrevSigma = -1; | ||
732 : | pEnc->sStat.iMvSum = 0; | ||
733 : | pEnc->sStat.iMvCount = 0; | ||
734 : | suxen_drol | 1.27 | pEnc->mbParam.m_fcode = 2; |
735 : | h | 1.21 | |
736 : | suxen_drol | 1.27 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
737 : | h | 1.21 | { |
738 : | HintedMEGet(pEnc, 1); | ||
739 : | } | ||
740 : | |||
741 : | return 1; // intra | ||
742 : | } | ||
743 : | |||
744 : | |||
745 : | Isibaar | 1.1 | #define INTRA_THRESHOLD 0.5 |
746 : | |||
747 : | static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) | ||
748 : | { | ||
749 : | edgomez | 1.3 | float fSigma; |
750 : | edgomez | 1.13 | |
751 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
752 : | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); | ||
753 : | Isibaar | 1.8 | |
754 : | Isibaar | 1.1 | int iLimit; |
755 : | edgomez | 1.3 | uint32_t x, y; |
756 : | int iSearchRange; | ||
757 : | edgomez | 1.29 | int bIntra; |
758 : | Isibaar | 1.1 | |
759 : | edgomez | 1.29 | /* IMAGE *pCurrent = &pEnc->current->image; */ |
760 : | suxen_drol | 1.27 | IMAGE *pRef = &pEnc->reference->image; |
761 : | Isibaar | 1.1 | |
762 : | h | 1.11 | start_timer(); |
763 : | edgomez | 1.13 | image_setedges(pRef, |
764 : | h | 1.16 | pEnc->mbParam.edged_width, |
765 : | pEnc->mbParam.edged_height, | ||
766 : | pEnc->mbParam.width, | ||
767 : | pEnc->mbParam.height, | ||
768 : | suxen_drol | 1.27 | pEnc->current->global_flags & XVID_INTERLACING); |
769 : | h | 1.11 | stop_edges_timer(); |
770 : | Isibaar | 1.1 | |
771 : | suxen_drol | 1.27 | pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
772 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
773 : | pEnc->current->fcode = pEnc->mbParam.m_fcode; | ||
774 : | Isibaar | 1.1 | |
775 : | if (!force_inter) | ||
776 : | iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); | ||
777 : | edgomez | 1.3 | else |
778 : | Isibaar | 1.1 | iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
779 : | |||
780 : | suxen_drol | 1.27 | if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
781 : | Isibaar | 1.1 | start_timer(); |
782 : | h | 1.16 | image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
783 : | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, | ||
784 : | suxen_drol | 1.27 | pEnc->current->rounding_type); |
785 : | Isibaar | 1.1 | stop_inter_timer(); |
786 : | } | ||
787 : | |||
788 : | start_timer(); | ||
789 : | suxen_drol | 1.27 | if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
790 : | h | 1.20 | { |
791 : | HintedMESet(pEnc, &bIntra); | ||
792 : | } | ||
793 : | else | ||
794 : | { | ||
795 : | suxen_drol | 1.27 | bIntra = MotionEstimation( |
796 : | &pEnc->mbParam, | ||
797 : | pEnc->current, | ||
798 : | pEnc->reference, | ||
799 : | &pEnc->vInterH, | ||
800 : | &pEnc->vInterV, | ||
801 : | &pEnc->vInterHV, | ||
802 : | iLimit); | ||
803 : | h | 1.20 | } |
804 : | Isibaar | 1.1 | stop_motion_timer(); |
805 : | |||
806 : | if (bIntra == 1) | ||
807 : | h | 1.20 | { |
808 : | Isibaar | 1.1 | return FrameCodeI(pEnc, bs, pBits); |
809 : | h | 1.20 | } |
810 : | Isibaar | 1.1 | |
811 : | suxen_drol | 1.27 | pEnc->current->coding_type = P_VOP; |
812 : | Isibaar | 1.1 | |
813 : | if(vol_header) | ||
814 : | suxen_drol | 1.27 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
815 : | Isibaar | 1.1 | |
816 : | suxen_drol | 1.27 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
817 : | edgomez | 1.3 | |
818 : | *pBits = BitstreamPos(bs); | ||
819 : | |||
820 : | pEnc->sStat.iTextBits = 0; | ||
821 : | pEnc->sStat.iMvSum = 0; | ||
822 : | pEnc->sStat.iMvCount = 0; | ||
823 : | Isibaar | 1.1 | pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
824 : | |||
825 : | edgomez | 1.3 | for(y = 0; y < pEnc->mbParam.mb_height; y++) |
826 : | Isibaar | 1.1 | { |
827 : | for(x = 0; x < pEnc->mbParam.mb_width; x++) | ||
828 : | { | ||
829 : | suxen_drol | 1.27 | MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
830 : | Isibaar | 1.1 | |
831 : | edgomez | 1.3 | bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
832 : | Isibaar | 1.1 | |
833 : | if (!bIntra) | ||
834 : | edgomez | 1.3 | { |
835 : | Isibaar | 1.1 | start_timer(); |
836 : | edgomez | 1.13 | MBMotionCompensation(pMB, |
837 : | x, y, | ||
838 : | suxen_drol | 1.27 | &pEnc->reference->image, |
839 : | edgomez | 1.13 | &pEnc->vInterH, |
840 : | h | 1.16 | &pEnc->vInterV, |
841 : | &pEnc->vInterHV, | ||
842 : | suxen_drol | 1.27 | &pEnc->current->image, |
843 : | edgomez | 1.13 | dct_codes, |
844 : | edgomez | 1.3 | pEnc->mbParam.width, |
845 : | pEnc->mbParam.height, | ||
846 : | pEnc->mbParam.edged_width, | ||
847 : | suxen_drol | 1.27 | pEnc->current->rounding_type); |
848 : | Isibaar | 1.1 | stop_comp_timer(); |
849 : | |||
850 : | suxen_drol | 1.27 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
851 : | Isibaar | 1.1 | if(pMB->dquant != NO_CHANGE) { |
852 : | pMB->mode = MODE_INTER_Q; | ||
853 : | suxen_drol | 1.27 | pEnc->current->quant += DQtab[pMB->dquant]; |
854 : | if (pEnc->current->quant > 31) pEnc->current->quant = 31; | ||
855 : | else if(pEnc->current->quant < 1) pEnc->current->quant = 1; | ||
856 : | Isibaar | 1.1 | } |
857 : | } | ||
858 : | suxen_drol | 1.27 | pMB->quant = pEnc->current->quant; |
859 : | Isibaar | 1.1 | |
860 : | h | 1.11 | pMB->field_pred = 0; |
861 : | |||
862 : | suxen_drol | 1.27 | pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
863 : | edgomez | 1.3 | } |
864 : | Isibaar | 1.1 | else |
865 : | { | ||
866 : | CodeIntraMB(pEnc, pMB); | ||
867 : | suxen_drol | 1.27 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
868 : | Isibaar | 1.1 | } |
869 : | |||
870 : | edgomez | 1.3 | start_timer(); |
871 : | suxen_drol | 1.27 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
872 : | Isibaar | 1.1 | stop_prediction_timer(); |
873 : | |||
874 : | if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) | ||
875 : | { | ||
876 : | pEnc->sStat.kblks++; | ||
877 : | } | ||
878 : | else if (pMB->cbp || | ||
879 : | edgomez | 1.3 | pMB->mvs[0].x || pMB->mvs[0].y || |
880 : | pMB->mvs[1].x || pMB->mvs[1].y || | ||
881 : | pMB->mvs[2].x || pMB->mvs[2].y || | ||
882 : | pMB->mvs[3].x || pMB->mvs[3].y) | ||
883 : | Isibaar | 1.1 | { |
884 : | pEnc->sStat.mblks++; | ||
885 : | } | ||
886 : | else | ||
887 : | { | ||
888 : | pEnc->sStat.ublks++; | ||
889 : | } | ||
890 : | |||
891 : | start_timer(); | ||
892 : | suxen_drol | 1.27 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
893 : | Isibaar | 1.1 | stop_coding_timer(); |
894 : | } | ||
895 : | } | ||
896 : | |||
897 : | emms(); | ||
898 : | h | 1.20 | |
899 : | suxen_drol | 1.27 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
900 : | h | 1.20 | { |
901 : | HintedMEGet(pEnc, 0); | ||
902 : | } | ||
903 : | Isibaar | 1.1 | |
904 : | if (pEnc->sStat.iMvCount == 0) | ||
905 : | pEnc->sStat.iMvCount = 1; | ||
906 : | |||
907 : | edgomez | 1.3 | fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
908 : | Isibaar | 1.1 | |
909 : | suxen_drol | 1.27 | iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
910 : | Isibaar | 1.1 | |
911 : | edgomez | 1.3 | if ((fSigma > iSearchRange / 3) |
912 : | suxen_drol | 1.27 | && (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 |
913 : | edgomez | 1.3 | { |
914 : | suxen_drol | 1.27 | pEnc->mbParam.m_fcode++; |
915 : | Isibaar | 1.1 | iSearchRange *= 2; |
916 : | edgomez | 1.3 | } |
917 : | else if ((fSigma < iSearchRange / 6) | ||
918 : | && (pEnc->sStat.fMvPrevSigma >= 0) | ||
919 : | && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) | ||
920 : | suxen_drol | 1.27 | && (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
921 : | edgomez | 1.3 | { |
922 : | suxen_drol | 1.27 | pEnc->mbParam.m_fcode--; |
923 : | Isibaar | 1.1 | iSearchRange /= 2; |
924 : | edgomez | 1.3 | } |
925 : | Isibaar | 1.1 | |
926 : | edgomez | 1.3 | pEnc->sStat.fMvPrevSigma = fSigma; |
927 : | Isibaar | 1.1 | |
928 : | *pBits = BitstreamPos(bs) - *pBits; | ||
929 : | |||
930 : | edgomez | 1.3 | return 0; // inter |
931 : | Isibaar | 1.1 | } |
932 : | suxen_drol | 1.24 | |
933 : | |||
934 : | edgomez | 1.29 | #if 0 |
935 : | suxen_drol | 1.24 | |
936 : | static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) | ||
937 : | { | ||
938 : | edgomez | 1.29 | int16_t dct_codes[6][64]; |
939 : | int16_t qcoeff[6][64]; | ||
940 : | uint32_t x, y; | ||
941 : | suxen_drol | 1.24 | VECTOR forward; |
942 : | VECTOR backward; | ||
943 : | |||
944 : | edgomez | 1.29 | IMAGE *f_ref = &pEnc->reference->image; |
945 : | suxen_drol | 1.24 | IMAGE *b_ref = &pEnc->current->image; |
946 : | |||
947 : | edgomez | 1.29 | /* forward */ |
948 : | suxen_drol | 1.24 | image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
949 : | start_timer(); | ||
950 : | image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, | ||
951 : | edgomez | 1.29 | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
952 : | suxen_drol | 1.24 | stop_inter_timer(); |
953 : | |||
954 : | edgomez | 1.29 | /* backward */ |
955 : | suxen_drol | 1.24 | image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
956 : | edgomez | 1.29 | start_timer(); |
957 : | suxen_drol | 1.24 | image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
958 : | edgomez | 1.29 | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
959 : | suxen_drol | 1.24 | stop_inter_timer(); |
960 : | |||
961 : | start_timer(); | ||
962 : | MotionEstimationBVOP(&pEnc->mbParam, frame, | ||
963 : | edgomez | 1.29 | pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
964 : | pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); | ||
965 : | suxen_drol | 1.24 | |
966 : | stop_motion_timer(); | ||
967 : | |||
968 : | if (test_quant_type(&pEnc->mbParam, pEnc->current)) | ||
969 : | { | ||
970 : | BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); | ||
971 : | } | ||
972 : | |||
973 : | edgomez | 1.29 | frame->coding_type = B_VOP; |
974 : | BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0, | ||
975 : | frame->quant, frame->fcode, frame->bcode); | ||
976 : | |||
977 : | *pBits = BitstreamPos(bs); | ||
978 : | |||
979 : | pEnc->sStat.iTextBits = 0; | ||
980 : | pEnc->sStat.iMvSum = 0; | ||
981 : | pEnc->sStat.iMvCount = 0; | ||
982 : | suxen_drol | 1.24 | pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
983 : | |||
984 : | |||
985 : | edgomez | 1.29 | for (y = 0; y < pEnc->mbParam.mb_height; y++) |
986 : | suxen_drol | 1.24 | { |
987 : | // reset prediction | ||
988 : | |||
989 : | forward.x = 0; | ||
990 : | forward.y = 0; | ||
991 : | backward.x = 0; | ||
992 : | backward.y = 0; | ||
993 : | |||
994 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) | ||
995 : | { | ||
996 : | MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; | ||
997 : | MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
998 : | MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; | ||
999 : | |||
1000 : | // decoder ignores mb when refence block is INTER(0,0), CBP=0 | ||
1001 : | if (mb->mode == MODE_NOT_CODED) | ||
1002 : | { | ||
1003 : | mb->mvs[0].x = 0; | ||
1004 : | mb->mvs[0].y = 0; | ||
1005 : | continue; | ||
1006 : | } | ||
1007 : | |||
1008 : | MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, | ||
1009 : | edgomez | 1.29 | f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1010 : | b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, | ||
1011 : | dct_codes); | ||
1012 : | suxen_drol | 1.24 | |
1013 : | mb->quant = frame->quant; | ||
1014 : | mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff); | ||
1015 : | //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); | ||
1016 : | |||
1017 : | |||
1018 : | if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && | ||
1019 : | edgomez | 1.29 | mb->cbp == 0 && |
1020 : | mb->mvs[0].x == 0 && | ||
1021 : | mb->mvs[0].y == 0) | ||
1022 : | suxen_drol | 1.24 | { |
1023 : | mb->mode = 5; // skipped | ||
1024 : | } | ||
1025 : | |||
1026 : | if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) | ||
1027 : | { | ||
1028 : | mb->pmvs[0].x = mb->mvs[0].x - forward.x; | ||
1029 : | mb->pmvs[0].y = mb->mvs[0].y - forward.y; | ||
1030 : | forward.x = mb->mvs[0].x; | ||
1031 : | forward.y = mb->mvs[0].y; | ||
1032 : | } | ||
1033 : | |||
1034 : | if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) | ||
1035 : | { | ||
1036 : | mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; | ||
1037 : | mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; | ||
1038 : | backward.x = mb->b_mvs[0].x; | ||
1039 : | backward.y = mb->b_mvs[0].y; | ||
1040 : | } | ||
1041 : | |||
1042 : | edgomez | 1.29 | /* |
1043 : | printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i %i,%i\n", | ||
1044 : | x, | ||
1045 : | y, | ||
1046 : | pMB->mode, | ||
1047 : | pMB->cbp, | ||
1048 : | pMB->mvs[0].x, | ||
1049 : | bmb->pmvs[0].x, | ||
1050 : | bmb->pmvs[0].y, | ||
1051 : | forward.x, | ||
1052 : | forward.y); | ||
1053 : | */ | ||
1054 : | suxen_drol | 1.24 | |
1055 : | start_timer(); | ||
1056 : | MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat); | ||
1057 : | stop_coding_timer(); | ||
1058 : | } | ||
1059 : | } | ||
1060 : | |||
1061 : | emms(); | ||
1062 : | |||
1063 : | // TODO: dynamic fcode/bcode ??? | ||
1064 : | |||
1065 : | *pBits = BitstreamPos(bs) - *pBits; | ||
1066 : | |||
1067 : | } | ||
1068 : | |||
1069 : | edgomez | 1.29 | #endif |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |