Parent Directory
|
Revision Log
Revision 405 -
(view)
(download)
Original Path: trunk/xvidcore/src/encoder.c
1 : | edgomez | 194 | /***************************************************************************** |
2 : | edgomez | 145 | * |
3 : | * XVID MPEG-4 VIDEO CODEC | ||
4 : | edgomez | 405 | * - Encoder main module - |
5 : | edgomez | 145 | * |
6 : | edgomez | 402 | * Copyright(C) 2002 Michael Militzer |
7 : | * | ||
8 : | edgomez | 145 | * This program is an implementation of a part of one or more MPEG-4 |
9 : | * Video tools as specified in ISO/IEC 14496-2 standard. Those intending | ||
10 : | * to use this software module in hardware or software products are | ||
11 : | * advised that its use may infringe existing patents or copyrights, and | ||
12 : | * any such use would be at such party's own risk. The original | ||
13 : | * developer of this software module and his/her company, and subsequent | ||
14 : | * editors and their companies, will have no liability for use of this | ||
15 : | * software or modifications or derivatives thereof. | ||
16 : | * | ||
17 : | * This program is free software; you can redistribute it and/or modify | ||
18 : | * it under the terms of the GNU General Public License as published by | ||
19 : | * the Free Software Foundation; either version 2 of the License, or | ||
20 : | * (at your option) any later version. | ||
21 : | * | ||
22 : | * This program is distributed in the hope that it will be useful, | ||
23 : | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
24 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
25 : | * GNU General Public License for more details. | ||
26 : | * | ||
27 : | * You should have received a copy of the GNU General Public License | ||
28 : | * along with this program; if not, write to the Free Software | ||
29 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
30 : | * | ||
31 : | edgomez | 405 | * $Id: encoder.c,v 1.79 2002-09-04 21:43:46 edgomez Exp $ |
32 : | * | ||
33 : | edgomez | 194 | ****************************************************************************/ |
34 : | suxen_drol | 118 | |
35 : | Isibaar | 3 | #include <stdlib.h> |
36 : | #include <stdio.h> | ||
37 : | #include <math.h> | ||
38 : | edgomez | 198 | #include <string.h> |
39 : | Isibaar | 3 | |
40 : | #include "encoder.h" | ||
41 : | #include "prediction/mbprediction.h" | ||
42 : | #include "global.h" | ||
43 : | #include "utils/timer.h" | ||
44 : | #include "image/image.h" | ||
45 : | suxen_drol | 152 | #include "motion/motion.h" |
46 : | Isibaar | 3 | #include "bitstream/cbp.h" |
47 : | #include "utils/mbfunctions.h" | ||
48 : | #include "bitstream/bitstream.h" | ||
49 : | #include "bitstream/mbcoding.h" | ||
50 : | #include "utils/ratecontrol.h" | ||
51 : | #include "utils/emms.h" | ||
52 : | #include "bitstream/mbcoding.h" | ||
53 : | #include "quant/adapt_quant.h" | ||
54 : | Isibaar | 4 | #include "quant/quant_matrix.h" |
55 : | Isibaar | 41 | #include "utils/mem_align.h" |
56 : | Isibaar | 3 | |
57 : | chl | 259 | #ifdef _SMP |
58 : | #include "motion/smp_motion_est.h" | ||
59 : | #endif | ||
60 : | edgomez | 194 | /***************************************************************************** |
61 : | * Local macros | ||
62 : | ****************************************************************************/ | ||
63 : | |||
64 : | Isibaar | 3 | #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
65 : | suxen_drol | 136 | #define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
66 : | Isibaar | 3 | |
67 : | edgomez | 194 | /***************************************************************************** |
68 : | * Local function prototypes | ||
69 : | ****************************************************************************/ | ||
70 : | |||
71 : | edgomez | 192 | static int FrameCodeI(Encoder * pEnc, |
72 : | edgomez | 195 | Bitstream * bs, |
73 : | uint32_t * pBits); | ||
74 : | Isibaar | 3 | |
75 : | edgomez | 192 | static int FrameCodeP(Encoder * pEnc, |
76 : | edgomez | 195 | Bitstream * bs, |
77 : | uint32_t * pBits, | ||
78 : | bool force_inter, | ||
79 : | bool vol_header); | ||
80 : | Isibaar | 3 | |
81 : | edgomez | 194 | /***************************************************************************** |
82 : | * Local data | ||
83 : | ****************************************************************************/ | ||
84 : | |||
85 : | edgomez | 195 | static int DQtab[4] = { |
86 : | Isibaar | 3 | -1, -2, 1, 2 |
87 : | }; | ||
88 : | |||
89 : | edgomez | 195 | static int iDQtab[5] = { |
90 : | Isibaar | 3 | 1, 0, NO_CHANGE, 2, 3 |
91 : | }; | ||
92 : | |||
93 : | |||
94 : | edgomez | 195 | static void __inline |
95 : | image_null(IMAGE * image) | ||
96 : | suxen_drol | 136 | { |
97 : | image->y = image->u = image->v = NULL; | ||
98 : | } | ||
99 : | |||
100 : | |||
101 : | edgomez | 194 | /***************************************************************************** |
102 : | * Encoder creation | ||
103 : | * | ||
104 : | * This function creates an Encoder instance, it allocates all necessary | ||
105 : | * image buffers (reference, current and bframes) and initialize the internal | ||
106 : | * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter. | ||
107 : | * | ||
108 : | * The code seems to be very long but is very basic, mainly memory allocation | ||
109 : | * and cleaning code. | ||
110 : | * | ||
111 : | * Returned values : | ||
112 : | * - XVID_ERR_OK - no errors | ||
113 : | * - XVID_ERR_MEMORY - the libc could not allocate memory, the function | ||
114 : | * cleans the structure before exiting. | ||
115 : | * pParam->handle is also set to NULL. | ||
116 : | * | ||
117 : | ****************************************************************************/ | ||
118 : | |||
119 : | int | ||
120 : | encoder_create(XVID_ENC_PARAM * pParam) | ||
121 : | Isibaar | 3 | { |
122 : | edgomez | 13 | Encoder *pEnc; |
123 : | suxen_drol | 229 | int i; |
124 : | Isibaar | 3 | |
125 : | edgomez | 13 | pParam->handle = NULL; |
126 : | Isibaar | 3 | |
127 : | edgomez | 13 | ENC_CHECK(pParam); |
128 : | Isibaar | 3 | |
129 : | edgomez | 13 | ENC_CHECK(pParam->width > 0 && pParam->width <= 1920); |
130 : | ENC_CHECK(pParam->height > 0 && pParam->height <= 1280); | ||
131 : | ENC_CHECK(!(pParam->width % 2)); | ||
132 : | ENC_CHECK(!(pParam->height % 2)); | ||
133 : | Isibaar | 3 | |
134 : | edgomez | 192 | /* Fps */ |
135 : | |||
136 : | edgomez | 195 | if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
137 : | Isibaar | 3 | pParam->fincr = 1; |
138 : | pParam->fbase = 25; | ||
139 : | } | ||
140 : | |||
141 : | edgomez | 192 | /* |
142 : | * Simplify the "fincr/fbase" fraction | ||
143 : | * (neccessary, since windows supplies us with huge numbers) | ||
144 : | */ | ||
145 : | Isibaar | 3 | |
146 : | i = pParam->fincr; | ||
147 : | edgomez | 195 | while (i > 1) { |
148 : | if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { | ||
149 : | Isibaar | 3 | pParam->fincr /= i; |
150 : | pParam->fbase /= i; | ||
151 : | i = pParam->fincr; | ||
152 : | continue; | ||
153 : | } | ||
154 : | i--; | ||
155 : | } | ||
156 : | |||
157 : | edgomez | 195 | if (pParam->fbase > 65535) { |
158 : | float div = (float) pParam->fbase / 65535; | ||
159 : | |||
160 : | pParam->fbase = (int) (pParam->fbase / div); | ||
161 : | pParam->fincr = (int) (pParam->fincr / div); | ||
162 : | Isibaar | 3 | } |
163 : | |||
164 : | edgomez | 192 | /* Bitrate allocator defaults */ |
165 : | |||
166 : | h | 121 | if (pParam->rc_bitrate <= 0) |
167 : | pParam->rc_bitrate = 900000; | ||
168 : | Isibaar | 3 | |
169 : | h | 121 | if (pParam->rc_reaction_delay_factor <= 0) |
170 : | pParam->rc_reaction_delay_factor = 16; | ||
171 : | Isibaar | 3 | |
172 : | h | 121 | if (pParam->rc_averaging_period <= 0) |
173 : | pParam->rc_averaging_period = 100; | ||
174 : | |||
175 : | if (pParam->rc_buffer <= 0) | ||
176 : | pParam->rc_buffer = 100; | ||
177 : | |||
178 : | edgomez | 192 | /* Max and min quantizers */ |
179 : | |||
180 : | edgomez | 13 | if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
181 : | Isibaar | 3 | pParam->min_quantizer = 1; |
182 : | |||
183 : | edgomez | 13 | if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
184 : | Isibaar | 3 | pParam->max_quantizer = 31; |
185 : | |||
186 : | edgomez | 13 | if (pParam->max_quantizer < pParam->min_quantizer) |
187 : | Isibaar | 3 | pParam->max_quantizer = pParam->min_quantizer; |
188 : | |||
189 : | edgomez | 195 | /* 1 keyframe each 10 seconds */ |
190 : | edgomez | 192 | |
191 : | edgomez | 325 | if (pParam->max_key_interval <= 0) |
192 : | edgomez | 192 | pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
193 : | |||
194 : | pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); | ||
195 : | if (pEnc == NULL) | ||
196 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
197 : | |||
198 : | edgomez | 198 | /* Zero the Encoder Structure */ |
199 : | |||
200 : | memset(pEnc, 0, sizeof(Encoder)); | ||
201 : | |||
202 : | Isibaar | 3 | /* Fill members of Encoder structure */ |
203 : | |||
204 : | edgomez | 13 | pEnc->mbParam.width = pParam->width; |
205 : | pEnc->mbParam.height = pParam->height; | ||
206 : | Isibaar | 3 | |
207 : | pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; | ||
208 : | pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; | ||
209 : | |||
210 : | edgomez | 195 | pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
211 : | pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; | ||
212 : | Isibaar | 3 | |
213 : | suxen_drol | 152 | pEnc->mbParam.fbase = pParam->fbase; |
214 : | pEnc->mbParam.fincr = pParam->fincr; | ||
215 : | |||
216 : | Isibaar | 208 | pEnc->mbParam.m_quant_type = H263_QUANT; |
217 : | |||
218 : | suxen_drol | 295 | #ifdef _SMP |
219 : | pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS); | ||
220 : | #endif | ||
221 : | |||
222 : | edgomez | 13 | pEnc->sStat.fMvPrevSigma = -1; |
223 : | Isibaar | 3 | |
224 : | /* Fill rate control parameters */ | ||
225 : | |||
226 : | h | 121 | pEnc->bitrate = pParam->rc_bitrate; |
227 : | Isibaar | 3 | |
228 : | edgomez | 13 | pEnc->iFrameNum = 0; |
229 : | pEnc->iMaxKeyInterval = pParam->max_key_interval; | ||
230 : | Isibaar | 3 | |
231 : | suxen_drol | 136 | /* try to allocate frame memory */ |
232 : | Isibaar | 3 | |
233 : | edgomez | 192 | pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
234 : | pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); | ||
235 : | Isibaar | 3 | |
236 : | edgomez | 195 | if (pEnc->current == NULL || pEnc->reference == NULL) |
237 : | edgomez | 192 | goto xvid_err_memory1; |
238 : | |||
239 : | suxen_drol | 136 | /* try to allocate mb memory */ |
240 : | Isibaar | 3 | |
241 : | edgomez | 195 | pEnc->current->mbs = |
242 : | xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * | ||
243 : | pEnc->mbParam.mb_height, CACHE_LINE); | ||
244 : | pEnc->reference->mbs = | ||
245 : | xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * | ||
246 : | pEnc->mbParam.mb_height, CACHE_LINE); | ||
247 : | suxen_drol | 136 | |
248 : | edgomez | 192 | if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
249 : | goto xvid_err_memory2; | ||
250 : | suxen_drol | 136 | |
251 : | /* try to allocate image memory */ | ||
252 : | |||
253 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
254 : | suxen_drol | 136 | image_null(&pEnc->sOriginal); |
255 : | #endif | ||
256 : | image_null(&pEnc->current->image); | ||
257 : | image_null(&pEnc->reference->image); | ||
258 : | image_null(&pEnc->vInterH); | ||
259 : | image_null(&pEnc->vInterV); | ||
260 : | image_null(&pEnc->vInterVf); | ||
261 : | image_null(&pEnc->vInterHV); | ||
262 : | image_null(&pEnc->vInterHVf); | ||
263 : | edgomez | 192 | |
264 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
265 : | edgomez | 195 | if (image_create |
266 : | (&pEnc->sOriginal, pEnc->mbParam.edged_width, | ||
267 : | pEnc->mbParam.edged_height) < 0) | ||
268 : | edgomez | 192 | goto xvid_err_memory3; |
269 : | suxen_drol | 136 | #endif |
270 : | edgomez | 195 | if (image_create |
271 : | (&pEnc->current->image, pEnc->mbParam.edged_width, | ||
272 : | pEnc->mbParam.edged_height) < 0) | ||
273 : | edgomez | 192 | goto xvid_err_memory3; |
274 : | edgomez | 195 | if (image_create |
275 : | (&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
276 : | pEnc->mbParam.edged_height) < 0) | ||
277 : | edgomez | 192 | goto xvid_err_memory3; |
278 : | edgomez | 195 | if (image_create |
279 : | (&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
280 : | pEnc->mbParam.edged_height) < 0) | ||
281 : | edgomez | 192 | goto xvid_err_memory3; |
282 : | edgomez | 195 | if (image_create |
283 : | (&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
284 : | pEnc->mbParam.edged_height) < 0) | ||
285 : | edgomez | 192 | goto xvid_err_memory3; |
286 : | edgomez | 195 | if (image_create |
287 : | (&pEnc->vInterVf, pEnc->mbParam.edged_width, | ||
288 : | pEnc->mbParam.edged_height) < 0) | ||
289 : | edgomez | 192 | goto xvid_err_memory3; |
290 : | edgomez | 195 | if (image_create |
291 : | (&pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
292 : | pEnc->mbParam.edged_height) < 0) | ||
293 : | edgomez | 192 | goto xvid_err_memory3; |
294 : | edgomez | 195 | if (image_create |
295 : | (&pEnc->vInterHVf, pEnc->mbParam.edged_width, | ||
296 : | pEnc->mbParam.edged_height) < 0) | ||
297 : | edgomez | 192 | goto xvid_err_memory3; |
298 : | suxen_drol | 136 | |
299 : | edgomez | 195 | pParam->handle = (void *) pEnc; |
300 : | Isibaar | 3 | |
301 : | edgomez | 195 | if (pParam->rc_bitrate) { |
302 : | RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, | ||
303 : | pParam->rc_reaction_delay_factor, | ||
304 : | pParam->rc_averaging_period, pParam->rc_buffer, | ||
305 : | pParam->fbase * 1000 / pParam->fincr, | ||
306 : | pParam->max_quantizer, pParam->min_quantizer); | ||
307 : | Isibaar | 3 | } |
308 : | |||
309 : | Isibaar | 36 | init_timer(); |
310 : | Isibaar | 3 | |
311 : | return XVID_ERR_OK; | ||
312 : | edgomez | 192 | |
313 : | /* | ||
314 : | * We handle all XVID_ERR_MEMORY here, this makes the code lighter | ||
315 : | */ | ||
316 : | suxen_drol | 229 | |
317 : | edgomez | 195 | xvid_err_memory3: |
318 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
319 : | edgomez | 195 | image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
320 : | pEnc->mbParam.edged_height); | ||
321 : | edgomez | 192 | #endif |
322 : | |||
323 : | edgomez | 195 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
324 : | pEnc->mbParam.edged_height); | ||
325 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
326 : | pEnc->mbParam.edged_height); | ||
327 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
328 : | pEnc->mbParam.edged_height); | ||
329 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
330 : | pEnc->mbParam.edged_height); | ||
331 : | image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, | ||
332 : | pEnc->mbParam.edged_height); | ||
333 : | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
334 : | pEnc->mbParam.edged_height); | ||
335 : | image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, | ||
336 : | pEnc->mbParam.edged_height); | ||
337 : | edgomez | 192 | |
338 : | edgomez | 195 | xvid_err_memory2: |
339 : | edgomez | 192 | xvid_free(pEnc->current->mbs); |
340 : | xvid_free(pEnc->reference->mbs); | ||
341 : | |||
342 : | edgomez | 195 | xvid_err_memory1: |
343 : | edgomez | 192 | xvid_free(pEnc->current); |
344 : | xvid_free(pEnc->reference); | ||
345 : | xvid_free(pEnc); | ||
346 : | |||
347 : | edgomez | 194 | pParam->handle = NULL; |
348 : | |||
349 : | edgomez | 192 | return XVID_ERR_MEMORY; |
350 : | Isibaar | 3 | } |
351 : | |||
352 : | edgomez | 194 | /***************************************************************************** |
353 : | * Encoder destruction | ||
354 : | * | ||
355 : | * This function destroy the entire encoder structure created by a previous | ||
356 : | * successful encoder_create call. | ||
357 : | * | ||
358 : | * Returned values (for now only one returned value) : | ||
359 : | * - XVID_ERR_OK - no errors | ||
360 : | * | ||
361 : | ****************************************************************************/ | ||
362 : | |||
363 : | int | ||
364 : | encoder_destroy(Encoder * pEnc) | ||
365 : | Isibaar | 3 | { |
366 : | suxen_drol | 229 | |
367 : | edgomez | 13 | ENC_CHECK(pEnc); |
368 : | Isibaar | 3 | |
369 : | edgomez | 192 | /* All images, reference, current etc ... */ |
370 : | edgomez | 195 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
371 : | pEnc->mbParam.edged_height); | ||
372 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
373 : | pEnc->mbParam.edged_height); | ||
374 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
375 : | pEnc->mbParam.edged_height); | ||
376 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
377 : | pEnc->mbParam.edged_height); | ||
378 : | image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, | ||
379 : | pEnc->mbParam.edged_height); | ||
380 : | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
381 : | pEnc->mbParam.edged_height); | ||
382 : | image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, | ||
383 : | pEnc->mbParam.edged_height); | ||
384 : | edgomez | 403 | |
385 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
386 : | edgomez | 195 | image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
387 : | pEnc->mbParam.edged_height); | ||
388 : | Isibaar | 113 | #endif |
389 : | edgomez | 192 | |
390 : | /* Encoder structure */ | ||
391 : | suxen_drol | 136 | xvid_free(pEnc->current->mbs); |
392 : | xvid_free(pEnc->current); | ||
393 : | |||
394 : | xvid_free(pEnc->reference->mbs); | ||
395 : | xvid_free(pEnc->reference); | ||
396 : | |||
397 : | Isibaar | 41 | xvid_free(pEnc); |
398 : | edgomez | 192 | |
399 : | edgomez | 13 | return XVID_ERR_OK; |
400 : | Isibaar | 3 | } |
401 : | |||
402 : | suxen_drol | 229 | |
403 : | void inc_frame_num(Encoder * pEnc) | ||
404 : | { | ||
405 : | pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; | ||
406 : | chl | 387 | pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
407 : | pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; | ||
408 : | } | ||
409 : | suxen_drol | 229 | |
410 : | edgomez | 194 | /***************************************************************************** |
411 : | suxen_drol | 229 | * "original" IP frame encoder entry point |
412 : | * | ||
413 : | * Returned values : | ||
414 : | * - XVID_ERR_OK - no errors | ||
415 : | * - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong | ||
416 : | * format | ||
417 : | edgomez | 194 | ****************************************************************************/ |
418 : | suxen_drol | 229 | |
419 : | edgomez | 194 | int |
420 : | encoder_encode(Encoder * pEnc, | ||
421 : | edgomez | 195 | XVID_ENC_FRAME * pFrame, |
422 : | XVID_ENC_STATS * pResult) | ||
423 : | suxen_drol | 152 | { |
424 : | uint16_t x, y; | ||
425 : | Bitstream bs; | ||
426 : | uint32_t bits; | ||
427 : | Isibaar | 4 | uint16_t write_vol_header = 0; |
428 : | edgomez | 195 | |
429 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
430 : | Isibaar | 113 | float psnr; |
431 : | edgomez | 194 | uint8_t temp[128]; |
432 : | Isibaar | 113 | #endif |
433 : | Isibaar | 3 | |
434 : | start_global_timer(); | ||
435 : | |||
436 : | edgomez | 13 | ENC_CHECK(pEnc); |
437 : | ENC_CHECK(pFrame); | ||
438 : | ENC_CHECK(pFrame->bitstream); | ||
439 : | ENC_CHECK(pFrame->image); | ||
440 : | Isibaar | 3 | |
441 : | suxen_drol | 136 | SWAP(pEnc->current, pEnc->reference); |
442 : | |||
443 : | pEnc->current->global_flags = pFrame->general; | ||
444 : | pEnc->current->motion_flags = pFrame->motion; | ||
445 : | suxen_drol | 229 | pEnc->current->seconds = pEnc->mbParam.m_seconds; |
446 : | pEnc->current->ticks = pEnc->mbParam.m_ticks; | ||
447 : | h | 101 | pEnc->mbParam.hint = &pFrame->hint; |
448 : | Isibaar | 3 | |
449 : | start_timer(); | ||
450 : | edgomez | 195 | if (image_input |
451 : | (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, | ||
452 : | pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) | ||
453 : | Isibaar | 3 | return XVID_ERR_FORMAT; |
454 : | stop_conv_timer(); | ||
455 : | |||
456 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
457 : | edgomez | 195 | image_copy(&pEnc->sOriginal, &pEnc->current->image, |
458 : | pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
459 : | Isibaar | 113 | #endif |
460 : | |||
461 : | edgomez | 198 | emms(); |
462 : | suxen_drol | 136 | |
463 : | edgomez | 13 | BitstreamInit(&bs, pFrame->bitstream, 0); |
464 : | Isibaar | 3 | |
465 : | edgomez | 195 | if (pFrame->quant == 0) { |
466 : | pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); | ||
467 : | } else { | ||
468 : | suxen_drol | 136 | pEnc->current->quant = pFrame->quant; |
469 : | Isibaar | 3 | } |
470 : | |||
471 : | edgomez | 195 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
472 : | int *temp_dquants = | ||
473 : | (int *) xvid_malloc(pEnc->mbParam.mb_width * | ||
474 : | pEnc->mbParam.mb_height * sizeof(int), | ||
475 : | CACHE_LINE); | ||
476 : | |||
477 : | edgomez | 194 | pEnc->current->quant = |
478 : | adaptive_quantization(pEnc->current->image.y, | ||
479 : | edgomez | 195 | pEnc->mbParam.edged_width, temp_dquants, |
480 : | pEnc->current->quant, pEnc->current->quant, | ||
481 : | 2 * pEnc->current->quant, | ||
482 : | pEnc->mbParam.mb_width, | ||
483 : | pEnc->mbParam.mb_height); | ||
484 : | edgomez | 194 | |
485 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
486 : | edgomez | 194 | |
487 : | edgomez | 195 | #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
488 : | edgomez | 194 | |
489 : | edgomez | 195 | for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
490 : | edgomez | 194 | |
491 : | edgomez | 195 | |
492 : | MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; | ||
493 : | |||
494 : | pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; | ||
495 : | Isibaar | 3 | } |
496 : | edgomez | 194 | |
497 : | edgomez | 195 | #undef OFFSET |
498 : | edgomez | 194 | } |
499 : | |||
500 : | Isibaar | 41 | xvid_free(temp_dquants); |
501 : | Isibaar | 3 | } |
502 : | |||
503 : | edgomez | 195 | if (pEnc->current->global_flags & XVID_H263QUANT) { |
504 : | if (pEnc->mbParam.m_quant_type != H263_QUANT) | ||
505 : | Isibaar | 20 | write_vol_header = 1; |
506 : | suxen_drol | 136 | pEnc->mbParam.m_quant_type = H263_QUANT; |
507 : | edgomez | 195 | } else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
508 : | edgomez | 194 | int matrix1_changed, matrix2_changed; |
509 : | Isibaar | 3 | |
510 : | edgomez | 194 | matrix1_changed = matrix2_changed = 0; |
511 : | edgomez | 78 | |
512 : | edgomez | 195 | if (pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
513 : | Isibaar | 20 | write_vol_header = 1; |
514 : | edgomez | 195 | |
515 : | suxen_drol | 136 | pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
516 : | edgomez | 195 | |
517 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
518 : | edgomez | 195 | if (pFrame->quant_intra_matrix != NULL) |
519 : | matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); | ||
520 : | if (pFrame->quant_inter_matrix != NULL) | ||
521 : | matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); | ||
522 : | } else { | ||
523 : | edgomez | 194 | matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
524 : | matrix2_changed = set_inter_matrix(get_default_inter_matrix()); | ||
525 : | Isibaar | 20 | } |
526 : | edgomez | 195 | if (write_vol_header == 0) |
527 : | edgomez | 194 | write_vol_header = matrix1_changed | matrix2_changed; |
528 : | Isibaar | 4 | } |
529 : | Isibaar | 3 | |
530 : | edgomez | 195 | if (pFrame->intra < 0) { |
531 : | if ((pEnc->iFrameNum == 0) | ||
532 : | || ((pEnc->iMaxKeyInterval > 0) | ||
533 : | && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { | ||
534 : | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); | ||
535 : | } else { | ||
536 : | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); | ||
537 : | edgomez | 194 | } |
538 : | edgomez | 195 | } else { |
539 : | if (pFrame->intra == 1) { | ||
540 : | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); | ||
541 : | } else { | ||
542 : | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); | ||
543 : | edgomez | 194 | } |
544 : | |||
545 : | edgomez | 13 | } |
546 : | Isibaar | 3 | |
547 : | BitstreamPutBits(&bs, 0xFFFF, 16); | ||
548 : | edgomez | 13 | BitstreamPutBits(&bs, 0xFFFF, 16); |
549 : | BitstreamPad(&bs); | ||
550 : | pFrame->length = BitstreamLength(&bs); | ||
551 : | h | 29 | |
552 : | edgomez | 195 | if (pResult) { |
553 : | suxen_drol | 136 | pResult->quant = pEnc->current->quant; |
554 : | Isibaar | 3 | pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
555 : | pResult->kblks = pEnc->sStat.kblks; | ||
556 : | pResult->mblks = pEnc->sStat.mblks; | ||
557 : | pResult->ublks = pEnc->sStat.ublks; | ||
558 : | edgomez | 13 | } |
559 : | edgomez | 195 | |
560 : | edgomez | 198 | emms(); |
561 : | Isibaar | 41 | |
562 : | edgomez | 195 | if (pFrame->quant == 0) { |
563 : | RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, | ||
564 : | pFrame->length, pFrame->intra); | ||
565 : | Isibaar | 3 | } |
566 : | suxen_drol | 229 | #ifdef _DEBUG_PSNR |
567 : | edgomez | 195 | psnr = |
568 : | image_psnr(&pEnc->sOriginal, &pEnc->current->image, | ||
569 : | pEnc->mbParam.edged_width, pEnc->mbParam.width, | ||
570 : | pEnc->mbParam.height); | ||
571 : | Isibaar | 113 | |
572 : | edgomez | 194 | snprintf(temp, 127, "PSNR: %f\n", psnr); |
573 : | Isibaar | 113 | DEBUG(temp); |
574 : | #endif | ||
575 : | |||
576 : | suxen_drol | 229 | inc_frame_num(pEnc); |
577 : | Isibaar | 3 | pEnc->iFrameNum++; |
578 : | edgomez | 195 | |
579 : | Isibaar | 3 | stop_global_timer(); |
580 : | write_timer(); | ||
581 : | |||
582 : | return XVID_ERR_OK; | ||
583 : | } | ||
584 : | |||
585 : | |||
586 : | edgomez | 195 | static __inline void |
587 : | CodeIntraMB(Encoder * pEnc, | ||
588 : | MACROBLOCK * pMB) | ||
589 : | { | ||
590 : | Isibaar | 3 | |
591 : | pMB->mode = MODE_INTRA; | ||
592 : | |||
593 : | suxen_drol | 136 | /* zero mv statistics */ |
594 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; | ||
595 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; | ||
596 : | pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; | ||
597 : | pMB->sad16 = 0; | ||
598 : | |||
599 : | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { | ||
600 : | edgomez | 195 | if (pMB->dquant != NO_CHANGE) { |
601 : | Isibaar | 3 | pMB->mode = MODE_INTRA_Q; |
602 : | suxen_drol | 136 | pEnc->current->quant += DQtab[pMB->dquant]; |
603 : | edgomez | 195 | |
604 : | if (pEnc->current->quant > 31) | ||
605 : | pEnc->current->quant = 31; | ||
606 : | if (pEnc->current->quant < 1) | ||
607 : | pEnc->current->quant = 1; | ||
608 : | Isibaar | 3 | } |
609 : | } | ||
610 : | |||
611 : | suxen_drol | 136 | pMB->quant = pEnc->current->quant; |
612 : | Isibaar | 3 | } |
613 : | |||
614 : | |||
615 : | h | 101 | #define FCODEBITS 3 |
616 : | #define MODEBITS 5 | ||
617 : | |||
618 : | edgomez | 195 | void |
619 : | HintedMESet(Encoder * pEnc, | ||
620 : | int *intra) | ||
621 : | h | 101 | { |
622 : | edgomez | 195 | HINTINFO *hint; |
623 : | h | 101 | Bitstream bs; |
624 : | int length, high; | ||
625 : | uint32_t x, y; | ||
626 : | |||
627 : | hint = pEnc->mbParam.hint; | ||
628 : | |||
629 : | edgomez | 195 | if (hint->rawhints) { |
630 : | h | 101 | *intra = hint->mvhint.intra; |
631 : | edgomez | 195 | } else { |
632 : | h | 101 | BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
633 : | *intra = BitstreamGetBit(&bs); | ||
634 : | } | ||
635 : | |||
636 : | edgomez | 195 | if (*intra) { |
637 : | h | 101 | return; |
638 : | } | ||
639 : | |||
640 : | edgomez | 195 | pEnc->current->fcode = |
641 : | (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, | ||
642 : | FCODEBITS); | ||
643 : | h | 101 | |
644 : | edgomez | 195 | length = pEnc->current->fcode + 5; |
645 : | high = 1 << (length - 1); | ||
646 : | h | 101 | |
647 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
648 : | for (x = 0; x < pEnc->mbParam.mb_width; ++x) { | ||
649 : | MACROBLOCK *pMB = | ||
650 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
651 : | MVBLOCKHINT *bhint = | ||
652 : | &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; | ||
653 : | chl | 284 | VECTOR pred; |
654 : | h | 101 | VECTOR tmp; |
655 : | int vec; | ||
656 : | |||
657 : | edgomez | 195 | pMB->mode = |
658 : | (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, | ||
659 : | MODEBITS); | ||
660 : | h | 101 | |
661 : | h | 128 | pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
662 : | pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; | ||
663 : | |||
664 : | edgomez | 195 | if (pMB->mode == MODE_INTER) { |
665 : | tmp.x = | ||
666 : | (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, | ||
667 : | length); | ||
668 : | tmp.y = | ||
669 : | (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, | ||
670 : | length); | ||
671 : | tmp.x -= (tmp.x >= high) ? high * 2 : 0; | ||
672 : | tmp.y -= (tmp.y >= high) ? high * 2 : 0; | ||
673 : | h | 101 | |
674 : | chl | 284 | pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0); |
675 : | h | 101 | |
676 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
677 : | pMB->mvs[vec].x = tmp.x; | ||
678 : | pMB->mvs[vec].y = tmp.y; | ||
679 : | chl | 284 | pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x; |
680 : | pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y; | ||
681 : | h | 101 | } |
682 : | edgomez | 195 | } else if (pMB->mode == MODE_INTER4V) { |
683 : | for (vec = 0; vec < 4; ++vec) { | ||
684 : | tmp.x = | ||
685 : | (hint->rawhints) ? bhint->mvs[vec]. | ||
686 : | x : BitstreamGetBits(&bs, length); | ||
687 : | tmp.y = | ||
688 : | (hint->rawhints) ? bhint->mvs[vec]. | ||
689 : | y : BitstreamGetBits(&bs, length); | ||
690 : | tmp.x -= (tmp.x >= high) ? high * 2 : 0; | ||
691 : | tmp.y -= (tmp.y >= high) ? high * 2 : 0; | ||
692 : | h | 101 | |
693 : | chl | 284 | pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec); |
694 : | h | 101 | |
695 : | edgomez | 195 | pMB->mvs[vec].x = tmp.x; |
696 : | pMB->mvs[vec].y = tmp.y; | ||
697 : | chl | 284 | pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
698 : | pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; | ||
699 : | h | 101 | } |
700 : | edgomez | 195 | } else // intra / stuffing / not_coded |
701 : | h | 101 | { |
702 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
703 : | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; | ||
704 : | h | 101 | } |
705 : | } | ||
706 : | h | 128 | |
707 : | suxen_drol | 136 | if (pMB->mode == MODE_INTER4V && |
708 : | edgomez | 195 | (pEnc->current->global_flags & XVID_LUMIMASKING) |
709 : | && pMB->dquant != NO_CHANGE) { | ||
710 : | h | 128 | pMB->mode = MODE_INTRA; |
711 : | |||
712 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
713 : | h | 128 | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
714 : | } | ||
715 : | } | ||
716 : | h | 101 | } |
717 : | } | ||
718 : | } | ||
719 : | |||
720 : | |||
721 : | edgomez | 195 | void |
722 : | HintedMEGet(Encoder * pEnc, | ||
723 : | int intra) | ||
724 : | h | 101 | { |
725 : | edgomez | 195 | HINTINFO *hint; |
726 : | h | 101 | Bitstream bs; |
727 : | uint32_t x, y; | ||
728 : | int length, high; | ||
729 : | |||
730 : | hint = pEnc->mbParam.hint; | ||
731 : | |||
732 : | edgomez | 195 | if (hint->rawhints) { |
733 : | h | 101 | hint->mvhint.intra = intra; |
734 : | edgomez | 195 | } else { |
735 : | h | 101 | BitstreamInit(&bs, hint->hintstream, 0); |
736 : | BitstreamPutBit(&bs, intra); | ||
737 : | } | ||
738 : | |||
739 : | edgomez | 195 | if (intra) { |
740 : | if (!hint->rawhints) { | ||
741 : | h | 101 | BitstreamPad(&bs); |
742 : | hint->hintlength = BitstreamLength(&bs); | ||
743 : | } | ||
744 : | return; | ||
745 : | } | ||
746 : | |||
747 : | edgomez | 195 | length = pEnc->current->fcode + 5; |
748 : | high = 1 << (length - 1); | ||
749 : | h | 101 | |
750 : | edgomez | 195 | if (hint->rawhints) { |
751 : | suxen_drol | 136 | hint->mvhint.fcode = pEnc->current->fcode; |
752 : | edgomez | 195 | } else { |
753 : | suxen_drol | 136 | BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
754 : | h | 101 | } |
755 : | |||
756 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
757 : | for (x = 0; x < pEnc->mbParam.mb_width; ++x) { | ||
758 : | MACROBLOCK *pMB = | ||
759 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
760 : | MVBLOCKHINT *bhint = | ||
761 : | &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; | ||
762 : | h | 101 | VECTOR tmp; |
763 : | |||
764 : | edgomez | 195 | if (hint->rawhints) { |
765 : | h | 101 | bhint->mode = pMB->mode; |
766 : | edgomez | 195 | } else { |
767 : | h | 101 | BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
768 : | } | ||
769 : | |||
770 : | edgomez | 195 | if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
771 : | tmp.x = pMB->mvs[0].x; | ||
772 : | tmp.y = pMB->mvs[0].y; | ||
773 : | tmp.x += (tmp.x < 0) ? high * 2 : 0; | ||
774 : | tmp.y += (tmp.y < 0) ? high * 2 : 0; | ||
775 : | h | 101 | |
776 : | edgomez | 195 | if (hint->rawhints) { |
777 : | h | 101 | bhint->mvs[0].x = tmp.x; |
778 : | bhint->mvs[0].y = tmp.y; | ||
779 : | edgomez | 195 | } else { |
780 : | h | 101 | BitstreamPutBits(&bs, tmp.x, length); |
781 : | BitstreamPutBits(&bs, tmp.y, length); | ||
782 : | } | ||
783 : | edgomez | 195 | } else if (pMB->mode == MODE_INTER4V) { |
784 : | h | 101 | int vec; |
785 : | |||
786 : | edgomez | 195 | for (vec = 0; vec < 4; ++vec) { |
787 : | tmp.x = pMB->mvs[vec].x; | ||
788 : | tmp.y = pMB->mvs[vec].y; | ||
789 : | tmp.x += (tmp.x < 0) ? high * 2 : 0; | ||
790 : | tmp.y += (tmp.y < 0) ? high * 2 : 0; | ||
791 : | h | 101 | |
792 : | edgomez | 195 | if (hint->rawhints) { |
793 : | h | 101 | bhint->mvs[vec].x = tmp.x; |
794 : | bhint->mvs[vec].y = tmp.y; | ||
795 : | edgomez | 195 | } else { |
796 : | h | 101 | BitstreamPutBits(&bs, tmp.x, length); |
797 : | BitstreamPutBits(&bs, tmp.y, length); | ||
798 : | } | ||
799 : | } | ||
800 : | } | ||
801 : | } | ||
802 : | } | ||
803 : | |||
804 : | edgomez | 195 | if (!hint->rawhints) { |
805 : | h | 101 | BitstreamPad(&bs); |
806 : | hint->hintlength = BitstreamLength(&bs); | ||
807 : | } | ||
808 : | } | ||
809 : | |||
810 : | |||
811 : | edgomez | 195 | static int |
812 : | FrameCodeI(Encoder * pEnc, | ||
813 : | Bitstream * bs, | ||
814 : | uint32_t * pBits) | ||
815 : | h | 104 | { |
816 : | |||
817 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
818 : | edgomez | 195 | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
819 : | h | 104 | |
820 : | uint16_t x, y; | ||
821 : | |||
822 : | pEnc->iFrameNum = 0; | ||
823 : | suxen_drol | 136 | pEnc->mbParam.m_rounding_type = 1; |
824 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
825 : | pEnc->current->coding_type = I_VOP; | ||
826 : | h | 104 | |
827 : | suxen_drol | 136 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
828 : | edgomez | 403 | |
829 : | suxen_drol | 229 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
830 : | h | 104 | |
831 : | *pBits = BitstreamPos(bs); | ||
832 : | |||
833 : | pEnc->sStat.iTextBits = 0; | ||
834 : | pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; | ||
835 : | pEnc->sStat.mblks = pEnc->sStat.ublks = 0; | ||
836 : | |||
837 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) | ||
838 : | edgomez | 195 | for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
839 : | MACROBLOCK *pMB = | ||
840 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
841 : | h | 104 | |
842 : | CodeIntraMB(pEnc, pMB); | ||
843 : | |||
844 : | edgomez | 195 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
845 : | dct_codes, qcoeff); | ||
846 : | h | 104 | |
847 : | start_timer(); | ||
848 : | suxen_drol | 136 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
849 : | h | 104 | stop_prediction_timer(); |
850 : | |||
851 : | start_timer(); | ||
852 : | chl | 353 | if (pEnc->current->global_flags & XVID_GREYSCALE) |
853 : | { pMB->cbp &= 0x3C; /* keep only bits 5-2 */ | ||
854 : | qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ | ||
855 : | qcoeff[5*64+0]=0; | ||
856 : | } | ||
857 : | suxen_drol | 136 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
858 : | h | 104 | stop_coding_timer(); |
859 : | } | ||
860 : | |||
861 : | emms(); | ||
862 : | |||
863 : | *pBits = BitstreamPos(bs) - *pBits; | ||
864 : | pEnc->sStat.fMvPrevSigma = -1; | ||
865 : | pEnc->sStat.iMvSum = 0; | ||
866 : | pEnc->sStat.iMvCount = 0; | ||
867 : | suxen_drol | 136 | pEnc->mbParam.m_fcode = 2; |
868 : | h | 104 | |
869 : | edgomez | 195 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
870 : | h | 104 | HintedMEGet(pEnc, 1); |
871 : | } | ||
872 : | |||
873 : | edgomez | 195 | return 1; // intra |
874 : | h | 104 | } |
875 : | |||
876 : | |||
877 : | Isibaar | 3 | #define INTRA_THRESHOLD 0.5 |
878 : | |||
879 : | edgomez | 195 | static int |
880 : | FrameCodeP(Encoder * pEnc, | ||
881 : | Bitstream * bs, | ||
882 : | uint32_t * pBits, | ||
883 : | bool force_inter, | ||
884 : | bool vol_header) | ||
885 : | Isibaar | 3 | { |
886 : | edgomez | 13 | float fSigma; |
887 : | Isibaar | 42 | |
888 : | edgomez | 78 | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
889 : | edgomez | 195 | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
890 : | edgomez | 78 | |
891 : | Isibaar | 3 | int iLimit; |
892 : | chl | 354 | int x, y, k; |
893 : | edgomez | 13 | int iSearchRange; |
894 : | edgomez | 145 | int bIntra; |
895 : | suxen_drol | 240 | |
896 : | edgomez | 145 | /* IMAGE *pCurrent = &pEnc->current->image; */ |
897 : | suxen_drol | 136 | IMAGE *pRef = &pEnc->reference->image; |
898 : | Isibaar | 3 | |
899 : | h | 69 | start_timer(); |
900 : | edgomez | 195 | image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
901 : | pEnc->mbParam.width, pEnc->mbParam.height, | ||
902 : | pEnc->current->global_flags & XVID_INTERLACING); | ||
903 : | h | 69 | stop_edges_timer(); |
904 : | Isibaar | 3 | |
905 : | suxen_drol | 136 | pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
906 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
907 : | pEnc->current->fcode = pEnc->mbParam.m_fcode; | ||
908 : | Isibaar | 3 | |
909 : | if (!force_inter) | ||
910 : | edgomez | 195 | iLimit = |
911 : | (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * | ||
912 : | INTRA_THRESHOLD); | ||
913 : | edgomez | 13 | else |
914 : | Isibaar | 3 | iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
915 : | |||
916 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
917 : | Isibaar | 3 | start_timer(); |
918 : | edgomez | 195 | image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
919 : | &pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
920 : | pEnc->mbParam.edged_height, | ||
921 : | pEnc->current->rounding_type); | ||
922 : | Isibaar | 3 | stop_inter_timer(); |
923 : | } | ||
924 : | |||
925 : | start_timer(); | ||
926 : | edgomez | 195 | if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
927 : | h | 101 | HintedMESet(pEnc, &bIntra); |
928 : | edgomez | 195 | } else { |
929 : | chl | 259 | |
930 : | #ifdef _SMP | ||
931 : | suxen_drol | 295 | if (pEnc->mbParam.num_threads > 1) |
932 : | bIntra = | ||
933 : | SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, | ||
934 : | &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, | ||
935 : | iLimit); | ||
936 : | else | ||
937 : | chl | 259 | #endif |
938 : | suxen_drol | 295 | bIntra = |
939 : | MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, | ||
940 : | &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, | ||
941 : | iLimit); | ||
942 : | |||
943 : | h | 101 | } |
944 : | Isibaar | 3 | stop_motion_timer(); |
945 : | |||
946 : | edgomez | 195 | if (bIntra == 1) { |
947 : | Isibaar | 3 | return FrameCodeI(pEnc, bs, pBits); |
948 : | h | 101 | } |
949 : | Isibaar | 3 | |
950 : | suxen_drol | 136 | pEnc->current->coding_type = P_VOP; |
951 : | Isibaar | 3 | |
952 : | edgomez | 195 | if (vol_header) |
953 : | suxen_drol | 136 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
954 : | Isibaar | 3 | |
955 : | suxen_drol | 229 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
956 : | Isibaar | 3 | |
957 : | edgomez | 13 | *pBits = BitstreamPos(bs); |
958 : | Isibaar | 3 | |
959 : | edgomez | 13 | pEnc->sStat.iTextBits = 0; |
960 : | pEnc->sStat.iMvSum = 0; | ||
961 : | pEnc->sStat.iMvCount = 0; | ||
962 : | Isibaar | 3 | pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
963 : | |||
964 : | edgomez | 195 | for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
965 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) { | ||
966 : | MACROBLOCK *pMB = | ||
967 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
968 : | Isibaar | 3 | |
969 : | edgomez | 13 | bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
970 : | Isibaar | 3 | |
971 : | edgomez | 195 | if (!bIntra) { |
972 : | Isibaar | 3 | start_timer(); |
973 : | edgomez | 195 | MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
974 : | &pEnc->vInterH, &pEnc->vInterV, | ||
975 : | &pEnc->vInterHV, &pEnc->current->image, | ||
976 : | dct_codes, pEnc->mbParam.width, | ||
977 : | pEnc->mbParam.height, | ||
978 : | pEnc->mbParam.edged_width, | ||
979 : | pEnc->current->rounding_type); | ||
980 : | Isibaar | 3 | stop_comp_timer(); |
981 : | |||
982 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
983 : | edgomez | 195 | if (pMB->dquant != NO_CHANGE) { |
984 : | Isibaar | 3 | pMB->mode = MODE_INTER_Q; |
985 : | suxen_drol | 136 | pEnc->current->quant += DQtab[pMB->dquant]; |
986 : | edgomez | 195 | if (pEnc->current->quant > 31) |
987 : | pEnc->current->quant = 31; | ||
988 : | else if (pEnc->current->quant < 1) | ||
989 : | pEnc->current->quant = 1; | ||
990 : | Isibaar | 3 | } |
991 : | } | ||
992 : | suxen_drol | 136 | pMB->quant = pEnc->current->quant; |
993 : | Isibaar | 3 | |
994 : | h | 69 | pMB->field_pred = 0; |
995 : | |||
996 : | edgomez | 195 | pMB->cbp = |
997 : | MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, | ||
998 : | dct_codes, qcoeff); | ||
999 : | } else { | ||
1000 : | Isibaar | 3 | CodeIntraMB(pEnc, pMB); |
1001 : | edgomez | 195 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1002 : | dct_codes, qcoeff); | ||
1003 : | Isibaar | 3 | } |
1004 : | |||
1005 : | edgomez | 13 | start_timer(); |
1006 : | edgomez | 195 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1007 : | Isibaar | 3 | stop_prediction_timer(); |
1008 : | |||
1009 : | edgomez | 195 | if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
1010 : | Isibaar | 3 | pEnc->sStat.kblks++; |
1011 : | edgomez | 195 | } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1012 : | pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || | ||
1013 : | pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { | ||
1014 : | Isibaar | 3 | pEnc->sStat.mblks++; |
1015 : | chl | 340 | } else { |
1016 : | Isibaar | 3 | pEnc->sStat.ublks++; |
1017 : | chl | 340 | } |
1018 : | Isibaar | 3 | |
1019 : | start_timer(); | ||
1020 : | chl | 347 | |
1021 : | /* Finished processing the MB, now check if to CODE or SKIP */ | ||
1022 : | |||
1023 : | if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && | ||
1024 : | pMB->mvs[0].y == 0) { | ||
1025 : | |||
1026 : | MBSkip(bs); /* without B-frames, no precautions are needed */ | ||
1027 : | |||
1028 : | edgomez | 403 | } |
1029 : | else { | ||
1030 : | if (pEnc->current->global_flags & XVID_GREYSCALE) { | ||
1031 : | pMB->cbp &= 0x3C; /* keep only bits 5-2 */ | ||
1032 : | chl | 353 | qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1033 : | qcoeff[5*64+0]=0; | ||
1034 : | } | ||
1035 : | chl | 347 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1036 : | } | ||
1037 : | |||
1038 : | Isibaar | 3 | stop_coding_timer(); |
1039 : | } | ||
1040 : | } | ||
1041 : | |||
1042 : | emms(); | ||
1043 : | |||
1044 : | edgomez | 195 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1045 : | h | 101 | HintedMEGet(pEnc, 0); |
1046 : | } | ||
1047 : | |||
1048 : | Isibaar | 3 | if (pEnc->sStat.iMvCount == 0) |
1049 : | pEnc->sStat.iMvCount = 1; | ||
1050 : | |||
1051 : | edgomez | 195 | fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
1052 : | Isibaar | 3 | |
1053 : | suxen_drol | 136 | iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
1054 : | Isibaar | 3 | |
1055 : | edgomez | 195 | if ((fSigma > iSearchRange / 3) |
1056 : | && (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 | ||
1057 : | edgomez | 13 | { |
1058 : | suxen_drol | 136 | pEnc->mbParam.m_fcode++; |
1059 : | Isibaar | 3 | iSearchRange *= 2; |
1060 : | edgomez | 195 | } else if ((fSigma < iSearchRange / 6) |
1061 : | && (pEnc->sStat.fMvPrevSigma >= 0) | ||
1062 : | && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) | ||
1063 : | && (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 | ||
1064 : | edgomez | 13 | { |
1065 : | suxen_drol | 136 | pEnc->mbParam.m_fcode--; |
1066 : | Isibaar | 3 | iSearchRange /= 2; |
1067 : | edgomez | 13 | } |
1068 : | Isibaar | 3 | |
1069 : | edgomez | 13 | pEnc->sStat.fMvPrevSigma = fSigma; |
1070 : | edgomez | 195 | |
1071 : | Isibaar | 3 | *pBits = BitstreamPos(bs) - *pBits; |
1072 : | |||
1073 : | edgomez | 195 | return 0; // inter |
1074 : | suxen_drol | 118 | |
1075 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |