Parent Directory
|
Revision Log
Revision 1.41 - (view) (download)
1 : | edgomez | 1.40 | /***************************************************************************** |
2 : | edgomez | 1.29 | * |
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 : | edgomez | 1.40 | ****************************************************************************/ |
30 : | edgomez | 1.29 | |
31 : | edgomez | 1.40 | /***************************************************************************** |
32 : | edgomez | 1.29 | * |
33 : | * History | ||
34 : | * | ||
35 : | chenm001 | 1.36 | * 08.05.2002 fix some problem in DEBUG mode; |
36 : | * MinChen <chenm001@163.com> | ||
37 : | edgomez | 1.29 | * 14.04.2002 added FrameCodeB() |
38 : | * | ||
39 : | edgomez | 1.41 | * $Id: encoder.c,v 1.40 2002/06/09 23:30:50 edgomez Exp $ |
40 : | edgomez | 1.29 | * |
41 : | edgomez | 1.40 | ****************************************************************************/ |
42 : | suxen_drol | 1.24 | |
43 : | Isibaar | 1.1 | #include <stdlib.h> |
44 : | #include <stdio.h> | ||
45 : | #include <math.h> | ||
46 : | |||
47 : | #include "encoder.h" | ||
48 : | #include "prediction/mbprediction.h" | ||
49 : | #include "global.h" | ||
50 : | #include "utils/timer.h" | ||
51 : | #include "image/image.h" | ||
52 : | suxen_drol | 1.32 | #include "motion/motion.h" |
53 : | Isibaar | 1.1 | #include "bitstream/cbp.h" |
54 : | #include "utils/mbfunctions.h" | ||
55 : | #include "bitstream/bitstream.h" | ||
56 : | #include "bitstream/mbcoding.h" | ||
57 : | #include "utils/ratecontrol.h" | ||
58 : | #include "utils/emms.h" | ||
59 : | #include "bitstream/mbcoding.h" | ||
60 : | #include "quant/adapt_quant.h" | ||
61 : | Isibaar | 1.2 | #include "quant/quant_matrix.h" |
62 : | Isibaar | 1.7 | #include "utils/mem_align.h" |
63 : | Isibaar | 1.1 | |
64 : | edgomez | 1.40 | /***************************************************************************** |
65 : | * Local macros | ||
66 : | ****************************************************************************/ | ||
67 : | |||
68 : | Isibaar | 1.1 | #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
69 : | suxen_drol | 1.27 | #define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
70 : | Isibaar | 1.1 | |
71 : | edgomez | 1.40 | /***************************************************************************** |
72 : | * Local function prototypes | ||
73 : | ****************************************************************************/ | ||
74 : | |||
75 : | edgomez | 1.39 | static int FrameCodeI(Encoder * pEnc, |
76 : | edgomez | 1.41 | Bitstream * bs, |
77 : | uint32_t * pBits); | ||
78 : | edgomez | 1.39 | |
79 : | static int FrameCodeP(Encoder * pEnc, | ||
80 : | edgomez | 1.41 | Bitstream * bs, |
81 : | uint32_t * pBits, | ||
82 : | bool force_inter, | ||
83 : | bool vol_header); | ||
84 : | Isibaar | 1.1 | |
85 : | edgomez | 1.39 | #ifdef BFRAMES |
86 : | static void FrameCodeB(Encoder * pEnc, | ||
87 : | edgomez | 1.41 | FRAMEINFO * frame, |
88 : | Bitstream * bs, | ||
89 : | uint32_t * pBits); | ||
90 : | edgomez | 1.39 | #endif |
91 : | Isibaar | 1.1 | |
92 : | edgomez | 1.40 | /***************************************************************************** |
93 : | * Local data | ||
94 : | ****************************************************************************/ | ||
95 : | |||
96 : | edgomez | 1.41 | static int DQtab[4] = { |
97 : | Isibaar | 1.1 | -1, -2, 1, 2 |
98 : | }; | ||
99 : | |||
100 : | edgomez | 1.41 | static int iDQtab[5] = { |
101 : | Isibaar | 1.1 | 1, 0, NO_CHANGE, 2, 3 |
102 : | }; | ||
103 : | |||
104 : | |||
105 : | edgomez | 1.41 | static void __inline |
106 : | image_null(IMAGE * image) | ||
107 : | suxen_drol | 1.27 | { |
108 : | image->y = image->u = image->v = NULL; | ||
109 : | } | ||
110 : | |||
111 : | |||
112 : | edgomez | 1.40 | /***************************************************************************** |
113 : | * Encoder creation | ||
114 : | * | ||
115 : | * This function creates an Encoder instance, it allocates all necessary | ||
116 : | * image buffers (reference, current and bframes) and initialize the internal | ||
117 : | * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter. | ||
118 : | * | ||
119 : | * The code seems to be very long but is very basic, mainly memory allocation | ||
120 : | * and cleaning code. | ||
121 : | * | ||
122 : | * Returned values : | ||
123 : | * - XVID_ERR_OK - no errors | ||
124 : | * - XVID_ERR_MEMORY - the libc could not allocate memory, the function | ||
125 : | * cleans the structure before exiting. | ||
126 : | * pParam->handle is also set to NULL. | ||
127 : | * | ||
128 : | ****************************************************************************/ | ||
129 : | |||
130 : | int | ||
131 : | encoder_create(XVID_ENC_PARAM * pParam) | ||
132 : | Isibaar | 1.1 | { |
133 : | edgomez | 1.3 | Encoder *pEnc; |
134 : | Isibaar | 1.1 | uint32_t i; |
135 : | |||
136 : | edgomez | 1.3 | pParam->handle = NULL; |
137 : | Isibaar | 1.1 | |
138 : | edgomez | 1.3 | ENC_CHECK(pParam); |
139 : | Isibaar | 1.1 | |
140 : | edgomez | 1.3 | ENC_CHECK(pParam->width > 0 && pParam->width <= 1920); |
141 : | ENC_CHECK(pParam->height > 0 && pParam->height <= 1280); | ||
142 : | ENC_CHECK(!(pParam->width % 2)); | ||
143 : | ENC_CHECK(!(pParam->height % 2)); | ||
144 : | Isibaar | 1.1 | |
145 : | edgomez | 1.39 | /* Fps */ |
146 : | |||
147 : | edgomez | 1.41 | if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
148 : | Isibaar | 1.1 | pParam->fincr = 1; |
149 : | pParam->fbase = 25; | ||
150 : | } | ||
151 : | |||
152 : | edgomez | 1.39 | /* |
153 : | * Simplify the "fincr/fbase" fraction | ||
154 : | * (neccessary, since windows supplies us with huge numbers) | ||
155 : | */ | ||
156 : | Isibaar | 1.1 | |
157 : | i = pParam->fincr; | ||
158 : | edgomez | 1.41 | while (i > 1) { |
159 : | if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { | ||
160 : | Isibaar | 1.1 | pParam->fincr /= i; |
161 : | pParam->fbase /= i; | ||
162 : | i = pParam->fincr; | ||
163 : | continue; | ||
164 : | } | ||
165 : | i--; | ||
166 : | } | ||
167 : | |||
168 : | edgomez | 1.41 | if (pParam->fbase > 65535) { |
169 : | float div = (float) pParam->fbase / 65535; | ||
170 : | |||
171 : | pParam->fbase = (int) (pParam->fbase / div); | ||
172 : | pParam->fincr = (int) (pParam->fincr / div); | ||
173 : | Isibaar | 1.1 | } |
174 : | |||
175 : | edgomez | 1.39 | /* Bitrate allocator defaults */ |
176 : | |||
177 : | h | 1.25 | if (pParam->rc_bitrate <= 0) |
178 : | pParam->rc_bitrate = 900000; | ||
179 : | Isibaar | 1.1 | |
180 : | h | 1.25 | if (pParam->rc_reaction_delay_factor <= 0) |
181 : | pParam->rc_reaction_delay_factor = 16; | ||
182 : | |||
183 : | if (pParam->rc_averaging_period <= 0) | ||
184 : | pParam->rc_averaging_period = 100; | ||
185 : | |||
186 : | if (pParam->rc_buffer <= 0) | ||
187 : | pParam->rc_buffer = 100; | ||
188 : | Isibaar | 1.1 | |
189 : | edgomez | 1.39 | /* Max and min quantizers */ |
190 : | |||
191 : | edgomez | 1.3 | if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
192 : | Isibaar | 1.1 | pParam->min_quantizer = 1; |
193 : | |||
194 : | edgomez | 1.3 | if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
195 : | Isibaar | 1.1 | pParam->max_quantizer = 31; |
196 : | |||
197 : | edgomez | 1.39 | if (pParam->max_quantizer < pParam->min_quantizer) |
198 : | pParam->max_quantizer = pParam->min_quantizer; | ||
199 : | |||
200 : | edgomez | 1.41 | /* 1 keyframe each 10 seconds */ |
201 : | edgomez | 1.39 | |
202 : | if (pParam->max_key_interval == 0) | ||
203 : | Isibaar | 1.1 | pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
204 : | edgomez | 1.41 | |
205 : | Isibaar | 1.1 | |
206 : | edgomez | 1.39 | pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
207 : | if (pEnc == NULL) | ||
208 : | Isibaar | 1.1 | return XVID_ERR_MEMORY; |
209 : | |||
210 : | /* Fill members of Encoder structure */ | ||
211 : | |||
212 : | edgomez | 1.3 | pEnc->mbParam.width = pParam->width; |
213 : | pEnc->mbParam.height = pParam->height; | ||
214 : | Isibaar | 1.1 | |
215 : | pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; | ||
216 : | pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; | ||
217 : | |||
218 : | edgomez | 1.41 | pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; |
219 : | pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; | ||
220 : | Isibaar | 1.1 | |
221 : | suxen_drol | 1.32 | pEnc->mbParam.fbase = pParam->fbase; |
222 : | pEnc->mbParam.fincr = pParam->fincr; | ||
223 : | |||
224 : | edgomez | 1.3 | pEnc->sStat.fMvPrevSigma = -1; |
225 : | Isibaar | 1.1 | |
226 : | /* Fill rate control parameters */ | ||
227 : | |||
228 : | h | 1.25 | pEnc->bitrate = pParam->rc_bitrate; |
229 : | Isibaar | 1.1 | |
230 : | edgomez | 1.3 | pEnc->iFrameNum = 0; |
231 : | pEnc->iMaxKeyInterval = pParam->max_key_interval; | ||
232 : | Isibaar | 1.1 | |
233 : | suxen_drol | 1.27 | /* try to allocate frame memory */ |
234 : | h | 1.16 | |
235 : | edgomez | 1.39 | pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
236 : | pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); | ||
237 : | |||
238 : | edgomez | 1.41 | if (pEnc->current == NULL || pEnc->reference == NULL) |
239 : | edgomez | 1.39 | goto xvid_err_memory1; |
240 : | suxen_drol | 1.27 | |
241 : | /* try to allocate mb memory */ | ||
242 : | h | 1.16 | |
243 : | edgomez | 1.41 | pEnc->current->mbs = |
244 : | xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * | ||
245 : | pEnc->mbParam.mb_height, CACHE_LINE); | ||
246 : | pEnc->reference->mbs = | ||
247 : | xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * | ||
248 : | pEnc->mbParam.mb_height, CACHE_LINE); | ||
249 : | Isibaar | 1.1 | |
250 : | edgomez | 1.39 | if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
251 : | goto xvid_err_memory2; | ||
252 : | suxen_drol | 1.27 | |
253 : | /* try to allocate image memory */ | ||
254 : | |||
255 : | #ifdef _DEBUG | ||
256 : | image_null(&pEnc->sOriginal); | ||
257 : | #endif | ||
258 : | suxen_drol | 1.32 | #ifdef BFRAMES |
259 : | image_null(&pEnc->f_refh); | ||
260 : | image_null(&pEnc->f_refv); | ||
261 : | image_null(&pEnc->f_refhv); | ||
262 : | #endif | ||
263 : | suxen_drol | 1.27 | image_null(&pEnc->current->image); |
264 : | image_null(&pEnc->reference->image); | ||
265 : | image_null(&pEnc->vInterH); | ||
266 : | image_null(&pEnc->vInterV); | ||
267 : | image_null(&pEnc->vInterVf); | ||
268 : | image_null(&pEnc->vInterHV); | ||
269 : | image_null(&pEnc->vInterHVf); | ||
270 : | edgomez | 1.39 | |
271 : | suxen_drol | 1.27 | #ifdef _DEBUG |
272 : | edgomez | 1.41 | if (image_create |
273 : | (&pEnc->sOriginal, pEnc->mbParam.edged_width, | ||
274 : | pEnc->mbParam.edged_height) < 0) | ||
275 : | edgomez | 1.39 | goto xvid_err_memory3; |
276 : | suxen_drol | 1.27 | #endif |
277 : | suxen_drol | 1.32 | #ifdef BFRAMES |
278 : | edgomez | 1.41 | if (image_create |
279 : | (&pEnc->f_refh, pEnc->mbParam.edged_width, | ||
280 : | pEnc->mbParam.edged_height) < 0) | ||
281 : | edgomez | 1.39 | goto xvid_err_memory3; |
282 : | edgomez | 1.41 | if (image_create |
283 : | (&pEnc->f_refv, pEnc->mbParam.edged_width, | ||
284 : | pEnc->mbParam.edged_height) < 0) | ||
285 : | edgomez | 1.39 | goto xvid_err_memory3; |
286 : | edgomez | 1.41 | if (image_create |
287 : | (&pEnc->f_refhv, pEnc->mbParam.edged_width, | ||
288 : | pEnc->mbParam.edged_height) < 0) | ||
289 : | edgomez | 1.39 | goto xvid_err_memory3; |
290 : | #endif | ||
291 : | edgomez | 1.41 | if (image_create |
292 : | (&pEnc->current->image, pEnc->mbParam.edged_width, | ||
293 : | pEnc->mbParam.edged_height) < 0) | ||
294 : | edgomez | 1.39 | goto xvid_err_memory3; |
295 : | edgomez | 1.41 | if (image_create |
296 : | (&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
297 : | pEnc->mbParam.edged_height) < 0) | ||
298 : | edgomez | 1.39 | goto xvid_err_memory3; |
299 : | edgomez | 1.41 | if (image_create |
300 : | (&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
301 : | pEnc->mbParam.edged_height) < 0) | ||
302 : | edgomez | 1.39 | goto xvid_err_memory3; |
303 : | edgomez | 1.41 | if (image_create |
304 : | (&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
305 : | pEnc->mbParam.edged_height) < 0) | ||
306 : | edgomez | 1.39 | goto xvid_err_memory3; |
307 : | edgomez | 1.41 | if (image_create |
308 : | (&pEnc->vInterVf, pEnc->mbParam.edged_width, | ||
309 : | pEnc->mbParam.edged_height) < 0) | ||
310 : | edgomez | 1.39 | goto xvid_err_memory3; |
311 : | edgomez | 1.41 | if (image_create |
312 : | (&pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
313 : | pEnc->mbParam.edged_height) < 0) | ||
314 : | edgomez | 1.39 | goto xvid_err_memory3; |
315 : | edgomez | 1.41 | if (image_create |
316 : | (&pEnc->vInterHVf, pEnc->mbParam.edged_width, | ||
317 : | pEnc->mbParam.edged_height) < 0) | ||
318 : | edgomez | 1.39 | goto xvid_err_memory3; |
319 : | |||
320 : | |||
321 : | Isibaar | 1.1 | |
322 : | edgomez | 1.39 | /* B Frames specific init */ |
323 : | suxen_drol | 1.32 | #ifdef BFRAMES |
324 : | edgomez | 1.41 | |
325 : | suxen_drol | 1.35 | pEnc->mbParam.max_bframes = pParam->max_bframes; |
326 : | suxen_drol | 1.32 | pEnc->bquant_ratio = pParam->bquant_ratio; |
327 : | edgomez | 1.39 | pEnc->bframes = NULL; |
328 : | |||
329 : | edgomez | 1.41 | if (pEnc->mbParam.max_bframes > 0) { |
330 : | suxen_drol | 1.32 | int n; |
331 : | |||
332 : | edgomez | 1.41 | pEnc->bframes = |
333 : | xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), | ||
334 : | CACHE_LINE); | ||
335 : | edgomez | 1.39 | |
336 : | if (pEnc->bframes == NULL) | ||
337 : | goto xvid_err_memory3; | ||
338 : | |||
339 : | edgomez | 1.41 | for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
340 : | edgomez | 1.39 | pEnc->bframes[n] = NULL; |
341 : | suxen_drol | 1.32 | |
342 : | edgomez | 1.41 | |
343 : | for (n = 0; n < pEnc->mbParam.max_bframes; n++) { | ||
344 : | pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); | ||
345 : | edgomez | 1.39 | |
346 : | if (pEnc->bframes[n] == NULL) | ||
347 : | goto xvid_err_memory4; | ||
348 : | |||
349 : | edgomez | 1.41 | pEnc->bframes[n]->mbs = |
350 : | xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * | ||
351 : | pEnc->mbParam.mb_height, CACHE_LINE); | ||
352 : | edgomez | 1.39 | |
353 : | if (pEnc->bframes[n]->mbs == NULL) | ||
354 : | goto xvid_err_memory4; | ||
355 : | |||
356 : | image_null(&pEnc->bframes[n]->image); | ||
357 : | |||
358 : | edgomez | 1.41 | if (image_create |
359 : | (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, | ||
360 : | pEnc->mbParam.edged_height) < 0) | ||
361 : | edgomez | 1.39 | goto xvid_err_memory4; |
362 : | suxen_drol | 1.32 | |
363 : | } | ||
364 : | } | ||
365 : | edgomez | 1.39 | |
366 : | suxen_drol | 1.32 | pEnc->bframenum_head = 0; |
367 : | pEnc->bframenum_tail = 0; | ||
368 : | pEnc->flush_bframes = 0; | ||
369 : | |||
370 : | pEnc->mbParam.m_seconds = 0; | ||
371 : | pEnc->mbParam.m_ticks = 0; | ||
372 : | #endif | ||
373 : | |||
374 : | edgomez | 1.41 | pParam->handle = (void *) pEnc; |
375 : | Isibaar | 1.1 | |
376 : | edgomez | 1.41 | if (pParam->rc_bitrate) { |
377 : | RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, | ||
378 : | pParam->rc_reaction_delay_factor, | ||
379 : | pParam->rc_averaging_period, pParam->rc_buffer, | ||
380 : | pParam->fbase * 1000 / pParam->fincr, | ||
381 : | pParam->max_quantizer, pParam->min_quantizer); | ||
382 : | Isibaar | 1.1 | } |
383 : | |||
384 : | Isibaar | 1.6 | init_timer(); |
385 : | Isibaar | 1.1 | |
386 : | return XVID_ERR_OK; | ||
387 : | edgomez | 1.39 | |
388 : | /* | ||
389 : | * We handle all XVID_ERR_MEMORY here, this makes the code lighter | ||
390 : | */ | ||
391 : | #ifdef BFRAMES | ||
392 : | edgomez | 1.41 | xvid_err_memory4: |
393 : | for (i = 0; i < pEnc->mbParam.max_bframes; i++) { | ||
394 : | edgomez | 1.39 | |
395 : | edgomez | 1.41 | if (pEnc->bframes[i] == NULL) |
396 : | continue; | ||
397 : | edgomez | 1.39 | |
398 : | edgomez | 1.41 | image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
399 : | pEnc->mbParam.edged_height); | ||
400 : | edgomez | 1.39 | |
401 : | xvid_free(pEnc->bframes[i]->mbs); | ||
402 : | |||
403 : | xvid_free(pEnc->bframes[i]); | ||
404 : | |||
405 : | } | ||
406 : | |||
407 : | xvid_free(pEnc->bframes); | ||
408 : | |||
409 : | #endif | ||
410 : | |||
411 : | edgomez | 1.41 | xvid_err_memory3: |
412 : | edgomez | 1.39 | #ifdef _DEBUG |
413 : | edgomez | 1.41 | image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
414 : | pEnc->mbParam.edged_height); | ||
415 : | edgomez | 1.39 | #endif |
416 : | |||
417 : | #ifdef BFRAMES | ||
418 : | edgomez | 1.41 | image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
419 : | pEnc->mbParam.edged_height); | ||
420 : | image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, | ||
421 : | pEnc->mbParam.edged_height); | ||
422 : | image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, | ||
423 : | pEnc->mbParam.edged_height); | ||
424 : | edgomez | 1.39 | #endif |
425 : | |||
426 : | edgomez | 1.41 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
427 : | pEnc->mbParam.edged_height); | ||
428 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
429 : | pEnc->mbParam.edged_height); | ||
430 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
431 : | pEnc->mbParam.edged_height); | ||
432 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
433 : | pEnc->mbParam.edged_height); | ||
434 : | image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, | ||
435 : | pEnc->mbParam.edged_height); | ||
436 : | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
437 : | pEnc->mbParam.edged_height); | ||
438 : | image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, | ||
439 : | pEnc->mbParam.edged_height); | ||
440 : | edgomez | 1.39 | |
441 : | edgomez | 1.41 | xvid_err_memory2: |
442 : | edgomez | 1.39 | xvid_free(pEnc->current->mbs); |
443 : | xvid_free(pEnc->reference->mbs); | ||
444 : | |||
445 : | edgomez | 1.41 | xvid_err_memory1: |
446 : | edgomez | 1.39 | xvid_free(pEnc->current); |
447 : | xvid_free(pEnc->reference); | ||
448 : | xvid_free(pEnc); | ||
449 : | |||
450 : | edgomez | 1.40 | pParam->handle = NULL; |
451 : | |||
452 : | edgomez | 1.39 | return XVID_ERR_MEMORY; |
453 : | Isibaar | 1.1 | } |
454 : | |||
455 : | edgomez | 1.40 | /***************************************************************************** |
456 : | * Encoder destruction | ||
457 : | * | ||
458 : | * This function destroy the entire encoder structure created by a previous | ||
459 : | * successful encoder_create call. | ||
460 : | * | ||
461 : | * Returned values (for now only one returned value) : | ||
462 : | * - XVID_ERR_OK - no errors | ||
463 : | * | ||
464 : | ****************************************************************************/ | ||
465 : | |||
466 : | int | ||
467 : | encoder_destroy(Encoder * pEnc) | ||
468 : | Isibaar | 1.1 | { |
469 : | edgomez | 1.3 | ENC_CHECK(pEnc); |
470 : | Isibaar | 1.1 | |
471 : | edgomez | 1.39 | /* B Frames specific */ |
472 : | suxen_drol | 1.32 | #ifdef BFRAMES |
473 : | edgomez | 1.39 | int i; |
474 : | |||
475 : | edgomez | 1.41 | for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
476 : | edgomez | 1.39 | |
477 : | edgomez | 1.41 | if (pEnc->bframes[i] == NULL) |
478 : | continue; | ||
479 : | edgomez | 1.39 | |
480 : | edgomez | 1.41 | image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
481 : | pEnc->mbParam.edged_height); | ||
482 : | edgomez | 1.39 | |
483 : | xvid_free(pEnc->bframes[i]->mbs); | ||
484 : | |||
485 : | xvid_free(pEnc->bframes[i]); | ||
486 : | |||
487 : | suxen_drol | 1.32 | } |
488 : | edgomez | 1.39 | |
489 : | xvid_free(pEnc->bframes); | ||
490 : | |||
491 : | edgomez | 1.34 | #endif |
492 : | suxen_drol | 1.32 | |
493 : | edgomez | 1.39 | /* All images, reference, current etc ... */ |
494 : | |||
495 : | edgomez | 1.41 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
496 : | pEnc->mbParam.edged_height); | ||
497 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, | ||
498 : | pEnc->mbParam.edged_height); | ||
499 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, | ||
500 : | pEnc->mbParam.edged_height); | ||
501 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, | ||
502 : | pEnc->mbParam.edged_height); | ||
503 : | image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, | ||
504 : | pEnc->mbParam.edged_height); | ||
505 : | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
506 : | pEnc->mbParam.edged_height); | ||
507 : | image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, | ||
508 : | pEnc->mbParam.edged_height); | ||
509 : | suxen_drol | 1.32 | #ifdef BFRAMES |
510 : | edgomez | 1.41 | image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
511 : | pEnc->mbParam.edged_height); | ||
512 : | image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, | ||
513 : | pEnc->mbParam.edged_height); | ||
514 : | image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, | ||
515 : | pEnc->mbParam.edged_height); | ||
516 : | suxen_drol | 1.32 | #endif |
517 : | Isibaar | 1.23 | #ifdef _DEBUG |
518 : | edgomez | 1.41 | image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
519 : | pEnc->mbParam.edged_height); | ||
520 : | Isibaar | 1.23 | #endif |
521 : | edgomez | 1.39 | |
522 : | /* Encoder structure */ | ||
523 : | edgomez | 1.41 | |
524 : | suxen_drol | 1.27 | xvid_free(pEnc->current->mbs); |
525 : | xvid_free(pEnc->current); | ||
526 : | |||
527 : | xvid_free(pEnc->reference->mbs); | ||
528 : | xvid_free(pEnc->reference); | ||
529 : | |||
530 : | Isibaar | 1.7 | xvid_free(pEnc); |
531 : | edgomez | 1.39 | |
532 : | edgomez | 1.3 | return XVID_ERR_OK; |
533 : | Isibaar | 1.1 | } |
534 : | |||
535 : | edgomez | 1.40 | /***************************************************************************** |
536 : | * Frame encoder entry point | ||
537 : | * | ||
538 : | * At this moment 2 versions coexist : one for IPB compatible encoder, | ||
539 : | * another one for the old IP encoder. | ||
540 : | * | ||
541 : | * Returned values : | ||
542 : | * - XVID_ERR_OK - no errors | ||
543 : | * - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong | ||
544 : | * format | ||
545 : | ****************************************************************************/ | ||
546 : | suxen_drol | 1.32 | |
547 : | |||
548 : | #ifdef BFRAMES | ||
549 : | edgomez | 1.40 | /***************************************************************************** |
550 : | * Frame encoder entry point for IPB capable encoder | ||
551 : | ****************************************************************************/ | ||
552 : | int | ||
553 : | encoder_encode(Encoder * pEnc, | ||
554 : | edgomez | 1.41 | XVID_ENC_FRAME * pFrame, |
555 : | XVID_ENC_STATS * pResult) | ||
556 : | suxen_drol | 1.32 | { |
557 : | uint16_t x, y; | ||
558 : | Bitstream bs; | ||
559 : | uint32_t bits; | ||
560 : | |||
561 : | chenm001 | 1.36 | #ifdef _DEBUG |
562 : | float psnr; | ||
563 : | char temp[128]; | ||
564 : | #endif | ||
565 : | |||
566 : | suxen_drol | 1.32 | ENC_CHECK(pEnc); |
567 : | ENC_CHECK(pFrame); | ||
568 : | |||
569 : | start_global_timer(); | ||
570 : | edgomez | 1.41 | |
571 : | suxen_drol | 1.32 | BitstreamInit(&bs, pFrame->bitstream, 0); |
572 : | |||
573 : | edgomez | 1.40 | /* |
574 : | * bframe "flush" code | ||
575 : | */ | ||
576 : | suxen_drol | 1.32 | |
577 : | edgomez | 1.41 | if ((pFrame->image == NULL || pEnc->flush_bframes) |
578 : | && (pEnc->bframenum_head < pEnc->bframenum_tail)) { | ||
579 : | |||
580 : | if (pEnc->flush_bframes == 0) { | ||
581 : | edgomez | 1.40 | /* |
582 : | * we have reached the end of stream without getting | ||
583 : | * a future reference frame... so encode last final | ||
584 : | * frame as a pframe | ||
585 : | */ | ||
586 : | |||
587 : | /* ToDo : remove dprintf calls */ | ||
588 : | /* | ||
589 : | edgomez | 1.41 | dprintf("--- PFRAME (final frame correction) --- "); |
590 : | */ | ||
591 : | suxen_drol | 1.32 | pEnc->bframenum_tail--; |
592 : | SWAP(pEnc->current, pEnc->reference); | ||
593 : | |||
594 : | edgomez | 1.41 | SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
595 : | suxen_drol | 1.32 | |
596 : | FrameCodeP(pEnc, &bs, &bits, 1, 0); | ||
597 : | |||
598 : | BitstreamPad(&bs); | ||
599 : | pFrame->length = BitstreamLength(&bs); | ||
600 : | pFrame->input_consumed = 0; | ||
601 : | pFrame->intra = 0; | ||
602 : | edgomez | 1.40 | |
603 : | suxen_drol | 1.32 | return XVID_ERR_OK; |
604 : | } | ||
605 : | |||
606 : | edgomez | 1.40 | /* ToDo : remove dprintf calls */ |
607 : | /* | ||
608 : | edgomez | 1.41 | dprintf("--- BFRAME (flush) --- "); |
609 : | */ | ||
610 : | FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); | ||
611 : | suxen_drol | 1.32 | pEnc->bframenum_head++; |
612 : | |||
613 : | |||
614 : | BitstreamPad(&bs); | ||
615 : | pFrame->length = BitstreamLength(&bs); | ||
616 : | pFrame->input_consumed = 0; | ||
617 : | pFrame->intra = 0; | ||
618 : | edgomez | 1.40 | |
619 : | suxen_drol | 1.32 | return XVID_ERR_OK; |
620 : | } | ||
621 : | |||
622 : | edgomez | 1.41 | if (pFrame->image == NULL) { |
623 : | suxen_drol | 1.32 | pFrame->length = 0; |
624 : | pFrame->input_consumed = 1; | ||
625 : | pFrame->intra = 0; | ||
626 : | edgomez | 1.40 | |
627 : | suxen_drol | 1.32 | return XVID_ERR_OK; |
628 : | } | ||
629 : | |||
630 : | edgomez | 1.41 | if (pEnc->bframenum_head > 0) { |
631 : | suxen_drol | 1.32 | pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
632 : | } | ||
633 : | |||
634 : | pEnc->flush_bframes = 0; | ||
635 : | |||
636 : | edgomez | 1.40 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
637 : | * Well there was a separation here so i put it in ANSI C | ||
638 : | * comment style :-) | ||
639 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
640 : | suxen_drol | 1.32 | |
641 : | SWAP(pEnc->current, pEnc->reference); | ||
642 : | |||
643 : | edgomez | 1.40 | EMMS(); |
644 : | |||
645 : | if (pFrame->quant == 0) | ||
646 : | pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); | ||
647 : | else | ||
648 : | pEnc->current->quant = pFrame->quant; | ||
649 : | |||
650 : | edgomez | 1.41 | if (pEnc->current->quant < 1) |
651 : | Isibaar | 1.33 | pEnc->current->quant = 1; |
652 : | edgomez | 1.41 | |
653 : | if (pEnc->current->quant > 31) | ||
654 : | Isibaar | 1.33 | pEnc->current->quant = 31; |
655 : | |||
656 : | suxen_drol | 1.32 | pEnc->current->global_flags = pFrame->general; |
657 : | pEnc->current->motion_flags = pFrame->motion; | ||
658 : | pEnc->current->seconds = pEnc->mbParam.m_seconds; | ||
659 : | pEnc->current->ticks = pEnc->mbParam.m_ticks; | ||
660 : | edgomez | 1.40 | /* ToDo : dynamic fcode (in both directions) */ |
661 : | suxen_drol | 1.32 | pEnc->current->fcode = pEnc->mbParam.m_fcode; |
662 : | edgomez | 1.41 | pEnc->current->bcode = pEnc->mbParam.m_fcode; |
663 : | suxen_drol | 1.32 | |
664 : | start_timer(); | ||
665 : | edgomez | 1.41 | if (image_input |
666 : | (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, | ||
667 : | pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) | ||
668 : | suxen_drol | 1.32 | return XVID_ERR_FORMAT; |
669 : | stop_conv_timer(); | ||
670 : | |||
671 : | #ifdef _DEBUG | ||
672 : | edgomez | 1.41 | image_copy(&pEnc->sOriginal, &pEnc->current->image, |
673 : | pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
674 : | suxen_drol | 1.32 | #endif |
675 : | |||
676 : | edgomez | 1.40 | EMMS(); |
677 : | suxen_drol | 1.32 | |
678 : | edgomez | 1.40 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
679 : | * Luminance masking | ||
680 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
681 : | suxen_drol | 1.32 | |
682 : | edgomez | 1.41 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
683 : | int *temp_dquants = | ||
684 : | (int *) xvid_malloc(pEnc->mbParam.mb_width * | ||
685 : | pEnc->mbParam.mb_height * sizeof(int), | ||
686 : | CACHE_LINE); | ||
687 : | |||
688 : | edgomez | 1.40 | pEnc->current->quant = |
689 : | adaptive_quantization(pEnc->current->image.y, | ||
690 : | edgomez | 1.41 | pEnc->mbParam.edged_width, temp_dquants, |
691 : | pEnc->current->quant, pEnc->current->quant, | ||
692 : | 2 * pEnc->current->quant, | ||
693 : | pEnc->mbParam.mb_width, | ||
694 : | pEnc->mbParam.mb_height); | ||
695 : | |||
696 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) { | ||
697 : | edgomez | 1.40 | |
698 : | edgomez | 1.41 | #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
699 : | edgomez | 1.40 | |
700 : | edgomez | 1.41 | for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
701 : | MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; | ||
702 : | |||
703 : | pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; | ||
704 : | suxen_drol | 1.32 | } |
705 : | edgomez | 1.40 | |
706 : | edgomez | 1.41 | #undef OFFSET |
707 : | edgomez | 1.40 | |
708 : | } | ||
709 : | |||
710 : | suxen_drol | 1.32 | xvid_free(temp_dquants); |
711 : | } | ||
712 : | |||
713 : | edgomez | 1.40 | /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
714 : | * ivop/pvop/bvop selection | ||
715 : | * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ | ||
716 : | suxen_drol | 1.32 | |
717 : | |||
718 : | edgomez | 1.41 | if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || |
719 : | (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && | ||
720 : | pEnc->iFrameNum >= pEnc->iMaxKeyInterval) | ||
721 : | || image_mad(&pEnc->reference->image, &pEnc->current->image, | ||
722 : | pEnc->mbParam.edged_width, pEnc->mbParam.width, | ||
723 : | pEnc->mbParam.height) > 30) { | ||
724 : | edgomez | 1.40 | /* |
725 : | * This will be coded as an Intra Frame | ||
726 : | */ | ||
727 : | |||
728 : | /* ToDo : Remove dprintf calls */ | ||
729 : | /* | ||
730 : | edgomez | 1.41 | dprintf("--- IFRAME ---"); |
731 : | */ | ||
732 : | suxen_drol | 1.32 | |
733 : | FrameCodeI(pEnc, &bs, &bits); | ||
734 : | |||
735 : | pFrame->intra = 1; | ||
736 : | pEnc->flush_bframes = 1; | ||
737 : | |||
738 : | edgomez | 1.40 | /* |
739 : | * NB : sequences like "IIBB" decode fine with msfdam but, | ||
740 : | * go screwy with divx 5.00 | ||
741 : | */ | ||
742 : | edgomez | 1.41 | } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
743 : | edgomez | 1.40 | /* |
744 : | * This will be coded as a Predicted Frame | ||
745 : | */ | ||
746 : | |||
747 : | /* ToDo : Remove dprintf calls */ | ||
748 : | /* | ||
749 : | edgomez | 1.41 | dprintf("--- PFRAME ---"); |
750 : | */ | ||
751 : | suxen_drol | 1.32 | |
752 : | FrameCodeP(pEnc, &bs, &bits, 1, 0); | ||
753 : | pFrame->intra = 0; | ||
754 : | pEnc->flush_bframes = 1; | ||
755 : | edgomez | 1.41 | } else { |
756 : | edgomez | 1.40 | /* |
757 : | * This will be coded as a Bidirectional Frame | ||
758 : | */ | ||
759 : | |||
760 : | /* ToDo : Remove dprintf calls */ | ||
761 : | /* | ||
762 : | edgomez | 1.41 | dprintf("--- BFRAME (store) --- head=%i tail=%i", |
763 : | pEnc->bframenum_head, | ||
764 : | pEnc->bframenum_tail); | ||
765 : | */ | ||
766 : | suxen_drol | 1.32 | |
767 : | edgomez | 1.41 | if (pFrame->bquant < 1) { |
768 : | edgomez | 1.40 | pEnc->current->quant = |
769 : | edgomez | 1.41 | ((pEnc->reference->quant + |
770 : | pEnc->current->quant) * pEnc->bquant_ratio) / 200; | ||
771 : | } else { | ||
772 : | suxen_drol | 1.32 | pEnc->current->quant = pFrame->bquant; |
773 : | } | ||
774 : | edgomez | 1.41 | |
775 : | edgomez | 1.40 | /* store frame into bframe buffer & swap ref back to current */ |
776 : | suxen_drol | 1.32 | SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
777 : | SWAP(pEnc->current, pEnc->reference); | ||
778 : | |||
779 : | pEnc->bframenum_tail++; | ||
780 : | |||
781 : | pFrame->intra = 0; | ||
782 : | pFrame->length = 0; | ||
783 : | pFrame->input_consumed = 1; | ||
784 : | |||
785 : | pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; | ||
786 : | edgomez | 1.41 | if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
787 : | suxen_drol | 1.32 | pEnc->mbParam.m_seconds++; |
788 : | pEnc->mbParam.m_ticks = 0; | ||
789 : | } | ||
790 : | edgomez | 1.40 | |
791 : | suxen_drol | 1.32 | return XVID_ERR_OK; |
792 : | } | ||
793 : | |||
794 : | BitstreamPad(&bs); | ||
795 : | pFrame->length = BitstreamLength(&bs); | ||
796 : | |||
797 : | edgomez | 1.41 | if (pResult) { |
798 : | suxen_drol | 1.32 | pResult->quant = pEnc->current->quant; |
799 : | edgomez | 1.41 | pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
800 : | suxen_drol | 1.32 | pResult->kblks = pEnc->sStat.kblks; |
801 : | pResult->mblks = pEnc->sStat.mblks; | ||
802 : | pResult->ublks = pEnc->sStat.ublks; | ||
803 : | } | ||
804 : | |||
805 : | edgomez | 1.40 | EMMS(); |
806 : | |||
807 : | suxen_drol | 1.32 | #ifdef _DEBUG |
808 : | edgomez | 1.41 | psnr = |
809 : | image_psnr(&pEnc->sOriginal, &pEnc->current->image, | ||
810 : | pEnc->mbParam.edged_width, pEnc->mbParam.width, | ||
811 : | pEnc->mbParam.height); | ||
812 : | suxen_drol | 1.32 | |
813 : | edgomez | 1.40 | snprintf(temp, 127, "PSNR: %f\n", psnr); |
814 : | suxen_drol | 1.32 | DEBUG(temp); |
815 : | #endif | ||
816 : | |||
817 : | edgomez | 1.41 | if (pFrame->quant == 0) { |
818 : | RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, | ||
819 : | pFrame->length, pFrame->intra); | ||
820 : | suxen_drol | 1.32 | } |
821 : | |||
822 : | pEnc->iFrameNum++; | ||
823 : | pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; | ||
824 : | edgomez | 1.41 | if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
825 : | suxen_drol | 1.32 | pEnc->mbParam.m_seconds++; |
826 : | pEnc->mbParam.m_ticks = 0; | ||
827 : | } | ||
828 : | pFrame->input_consumed = 1; | ||
829 : | edgomez | 1.41 | |
830 : | suxen_drol | 1.32 | stop_global_timer(); |
831 : | write_timer(); | ||
832 : | |||
833 : | return XVID_ERR_OK; | ||
834 : | } | ||
835 : | #else | ||
836 : | edgomez | 1.40 | /***************************************************************************** |
837 : | * Frame encoder entry point for IP capable encoder | ||
838 : | ****************************************************************************/ | ||
839 : | int | ||
840 : | encoder_encode(Encoder * pEnc, | ||
841 : | edgomez | 1.41 | XVID_ENC_FRAME * pFrame, |
842 : | XVID_ENC_STATS * pResult) | ||
843 : | Isibaar | 1.1 | { |
844 : | edgomez | 1.3 | uint16_t x, y; |
845 : | Bitstream bs; | ||
846 : | uint32_t bits; | ||
847 : | Isibaar | 1.2 | uint16_t write_vol_header = 0; |
848 : | edgomez | 1.41 | |
849 : | Isibaar | 1.23 | #ifdef _DEBUG |
850 : | float psnr; | ||
851 : | edgomez | 1.40 | uint8_t temp[128]; |
852 : | Isibaar | 1.23 | #endif |
853 : | Isibaar | 1.1 | |
854 : | start_global_timer(); | ||
855 : | |||
856 : | edgomez | 1.3 | ENC_CHECK(pEnc); |
857 : | ENC_CHECK(pFrame); | ||
858 : | ENC_CHECK(pFrame->bitstream); | ||
859 : | ENC_CHECK(pFrame->image); | ||
860 : | Isibaar | 1.1 | |
861 : | suxen_drol | 1.27 | SWAP(pEnc->current, pEnc->reference); |
862 : | |||
863 : | pEnc->current->global_flags = pFrame->general; | ||
864 : | pEnc->current->motion_flags = pFrame->motion; | ||
865 : | h | 1.20 | pEnc->mbParam.hint = &pFrame->hint; |
866 : | Isibaar | 1.1 | |
867 : | start_timer(); | ||
868 : | edgomez | 1.41 | if (image_input |
869 : | (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, | ||
870 : | pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) | ||
871 : | Isibaar | 1.1 | return XVID_ERR_FORMAT; |
872 : | stop_conv_timer(); | ||
873 : | |||
874 : | Isibaar | 1.23 | #ifdef _DEBUG |
875 : | edgomez | 1.41 | image_copy(&pEnc->sOriginal, &pEnc->current->image, |
876 : | pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
877 : | Isibaar | 1.23 | #endif |
878 : | |||
879 : | suxen_drol | 1.27 | EMMS(); |
880 : | |||
881 : | edgomez | 1.3 | BitstreamInit(&bs, pFrame->bitstream, 0); |
882 : | Isibaar | 1.1 | |
883 : | edgomez | 1.41 | if (pFrame->quant == 0) { |
884 : | pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); | ||
885 : | } else { | ||
886 : | suxen_drol | 1.27 | pEnc->current->quant = pFrame->quant; |
887 : | Isibaar | 1.1 | } |
888 : | |||
889 : | edgomez | 1.41 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
890 : | int *temp_dquants = | ||
891 : | (int *) xvid_malloc(pEnc->mbParam.mb_width * | ||
892 : | pEnc->mbParam.mb_height * sizeof(int), | ||
893 : | CACHE_LINE); | ||
894 : | |||
895 : | edgomez | 1.40 | pEnc->current->quant = |
896 : | adaptive_quantization(pEnc->current->image.y, | ||
897 : | edgomez | 1.41 | pEnc->mbParam.edged_width, temp_dquants, |
898 : | pEnc->current->quant, pEnc->current->quant, | ||
899 : | 2 * pEnc->current->quant, | ||
900 : | pEnc->mbParam.mb_width, | ||
901 : | pEnc->mbParam.mb_height); | ||
902 : | |||
903 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) { | ||
904 : | edgomez | 1.40 | |
905 : | edgomez | 1.41 | #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
906 : | |||
907 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) { | ||
908 : | edgomez | 1.40 | |
909 : | |||
910 : | edgomez | 1.41 | MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
911 : | edgomez | 1.40 | |
912 : | edgomez | 1.41 | pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
913 : | Isibaar | 1.1 | } |
914 : | edgomez | 1.40 | |
915 : | edgomez | 1.41 | #undef OFFSET |
916 : | edgomez | 1.40 | } |
917 : | |||
918 : | Isibaar | 1.7 | xvid_free(temp_dquants); |
919 : | Isibaar | 1.1 | } |
920 : | |||
921 : | edgomez | 1.41 | if (pEnc->current->global_flags & XVID_H263QUANT) { |
922 : | if (pEnc->mbParam.m_quant_type != H263_QUANT) | ||
923 : | Isibaar | 1.4 | write_vol_header = 1; |
924 : | suxen_drol | 1.27 | pEnc->mbParam.m_quant_type = H263_QUANT; |
925 : | edgomez | 1.41 | } else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
926 : | edgomez | 1.40 | int matrix1_changed, matrix2_changed; |
927 : | Isibaar | 1.1 | |
928 : | edgomez | 1.40 | matrix1_changed = matrix2_changed = 0; |
929 : | edgomez | 1.13 | |
930 : | edgomez | 1.41 | if (pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
931 : | Isibaar | 1.4 | write_vol_header = 1; |
932 : | edgomez | 1.41 | |
933 : | suxen_drol | 1.27 | pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
934 : | edgomez | 1.41 | |
935 : | suxen_drol | 1.27 | if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
936 : | edgomez | 1.41 | if (pFrame->quant_intra_matrix != NULL) |
937 : | matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); | ||
938 : | if (pFrame->quant_inter_matrix != NULL) | ||
939 : | matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); | ||
940 : | } else { | ||
941 : | edgomez | 1.40 | matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
942 : | matrix2_changed = set_inter_matrix(get_default_inter_matrix()); | ||
943 : | Isibaar | 1.4 | } |
944 : | edgomez | 1.41 | if (write_vol_header == 0) |
945 : | edgomez | 1.40 | write_vol_header = matrix1_changed | matrix2_changed; |
946 : | Isibaar | 1.2 | } |
947 : | Isibaar | 1.1 | |
948 : | edgomez | 1.41 | if (pFrame->intra < 0) { |
949 : | if ((pEnc->iFrameNum == 0) | ||
950 : | || ((pEnc->iMaxKeyInterval > 0) | ||
951 : | && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { | ||
952 : | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); | ||
953 : | } else { | ||
954 : | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); | ||
955 : | edgomez | 1.40 | } |
956 : | edgomez | 1.41 | } else { |
957 : | if (pFrame->intra == 1) { | ||
958 : | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); | ||
959 : | } else { | ||
960 : | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); | ||
961 : | edgomez | 1.40 | } |
962 : | |||
963 : | edgomez | 1.3 | } |
964 : | Isibaar | 1.1 | |
965 : | BitstreamPutBits(&bs, 0xFFFF, 16); | ||
966 : | edgomez | 1.3 | BitstreamPutBits(&bs, 0xFFFF, 16); |
967 : | BitstreamPad(&bs); | ||
968 : | pFrame->length = BitstreamLength(&bs); | ||
969 : | h | 1.5 | |
970 : | edgomez | 1.41 | if (pResult) { |
971 : | suxen_drol | 1.27 | pResult->quant = pEnc->current->quant; |
972 : | Isibaar | 1.1 | pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
973 : | pResult->kblks = pEnc->sStat.kblks; | ||
974 : | pResult->mblks = pEnc->sStat.mblks; | ||
975 : | pResult->ublks = pEnc->sStat.ublks; | ||
976 : | edgomez | 1.3 | } |
977 : | edgomez | 1.41 | |
978 : | Isibaar | 1.7 | EMMS(); |
979 : | |||
980 : | edgomez | 1.41 | if (pFrame->quant == 0) { |
981 : | RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, | ||
982 : | pFrame->length, pFrame->intra); | ||
983 : | Isibaar | 1.1 | } |
984 : | Isibaar | 1.23 | #ifdef _DEBUG |
985 : | edgomez | 1.41 | psnr = |
986 : | image_psnr(&pEnc->sOriginal, &pEnc->current->image, | ||
987 : | pEnc->mbParam.edged_width, pEnc->mbParam.width, | ||
988 : | pEnc->mbParam.height); | ||
989 : | Isibaar | 1.23 | |
990 : | edgomez | 1.40 | snprintf(temp, 127, "PSNR: %f\n", psnr); |
991 : | Isibaar | 1.23 | DEBUG(temp); |
992 : | #endif | ||
993 : | Isibaar | 1.1 | |
994 : | pEnc->iFrameNum++; | ||
995 : | edgomez | 1.41 | |
996 : | Isibaar | 1.1 | stop_global_timer(); |
997 : | write_timer(); | ||
998 : | |||
999 : | return XVID_ERR_OK; | ||
1000 : | } | ||
1001 : | suxen_drol | 1.32 | #endif |
1002 : | Isibaar | 1.1 | |
1003 : | |||
1004 : | edgomez | 1.41 | static __inline void |
1005 : | CodeIntraMB(Encoder * pEnc, | ||
1006 : | MACROBLOCK * pMB) | ||
1007 : | { | ||
1008 : | Isibaar | 1.1 | |
1009 : | pMB->mode = MODE_INTRA; | ||
1010 : | |||
1011 : | suxen_drol | 1.27 | /* zero mv statistics */ |
1012 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; | ||
1013 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; | ||
1014 : | pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; | ||
1015 : | pMB->sad16 = 0; | ||
1016 : | |||
1017 : | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { | ||
1018 : | edgomez | 1.41 | if (pMB->dquant != NO_CHANGE) { |
1019 : | Isibaar | 1.1 | pMB->mode = MODE_INTRA_Q; |
1020 : | suxen_drol | 1.27 | pEnc->current->quant += DQtab[pMB->dquant]; |
1021 : | edgomez | 1.41 | |
1022 : | if (pEnc->current->quant > 31) | ||
1023 : | pEnc->current->quant = 31; | ||
1024 : | if (pEnc->current->quant < 1) | ||
1025 : | pEnc->current->quant = 1; | ||
1026 : | Isibaar | 1.1 | } |
1027 : | } | ||
1028 : | |||
1029 : | suxen_drol | 1.27 | pMB->quant = pEnc->current->quant; |
1030 : | Isibaar | 1.1 | } |
1031 : | |||
1032 : | |||
1033 : | h | 1.20 | #define FCODEBITS 3 |
1034 : | #define MODEBITS 5 | ||
1035 : | |||
1036 : | edgomez | 1.41 | void |
1037 : | HintedMESet(Encoder * pEnc, | ||
1038 : | int *intra) | ||
1039 : | h | 1.20 | { |
1040 : | edgomez | 1.41 | HINTINFO *hint; |
1041 : | h | 1.20 | Bitstream bs; |
1042 : | int length, high; | ||
1043 : | uint32_t x, y; | ||
1044 : | |||
1045 : | hint = pEnc->mbParam.hint; | ||
1046 : | |||
1047 : | edgomez | 1.41 | if (hint->rawhints) { |
1048 : | h | 1.20 | *intra = hint->mvhint.intra; |
1049 : | edgomez | 1.41 | } else { |
1050 : | h | 1.20 | BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
1051 : | *intra = BitstreamGetBit(&bs); | ||
1052 : | } | ||
1053 : | |||
1054 : | edgomez | 1.41 | if (*intra) { |
1055 : | h | 1.20 | return; |
1056 : | } | ||
1057 : | |||
1058 : | edgomez | 1.41 | pEnc->current->fcode = |
1059 : | (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, | ||
1060 : | FCODEBITS); | ||
1061 : | |||
1062 : | length = pEnc->current->fcode + 5; | ||
1063 : | high = 1 << (length - 1); | ||
1064 : | |||
1065 : | for (y = 0; y < pEnc->mbParam.mb_height; ++y) { | ||
1066 : | for (x = 0; x < pEnc->mbParam.mb_width; ++x) { | ||
1067 : | MACROBLOCK *pMB = | ||
1068 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1069 : | MVBLOCKHINT *bhint = | ||
1070 : | &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; | ||
1071 : | h | 1.20 | VECTOR pred[4]; |
1072 : | VECTOR tmp; | ||
1073 : | edgomez | 1.30 | int32_t dummy[4]; |
1074 : | h | 1.20 | int vec; |
1075 : | |||
1076 : | edgomez | 1.41 | pMB->mode = |
1077 : | (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, | ||
1078 : | MODEBITS); | ||
1079 : | h | 1.20 | |
1080 : | h | 1.26 | pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
1081 : | pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; | ||
1082 : | |||
1083 : | edgomez | 1.41 | if (pMB->mode == MODE_INTER) { |
1084 : | tmp.x = | ||
1085 : | (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, | ||
1086 : | length); | ||
1087 : | tmp.y = | ||
1088 : | (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, | ||
1089 : | length); | ||
1090 : | tmp.x -= (tmp.x >= high) ? high * 2 : 0; | ||
1091 : | tmp.y -= (tmp.y >= high) ? high * 2 : 0; | ||
1092 : | |||
1093 : | get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, | ||
1094 : | 0, pred, dummy); | ||
1095 : | |||
1096 : | for (vec = 0; vec < 4; ++vec) { | ||
1097 : | pMB->mvs[vec].x = tmp.x; | ||
1098 : | pMB->mvs[vec].y = tmp.y; | ||
1099 : | h | 1.20 | pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
1100 : | pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; | ||
1101 : | } | ||
1102 : | edgomez | 1.41 | } else if (pMB->mode == MODE_INTER4V) { |
1103 : | for (vec = 0; vec < 4; ++vec) { | ||
1104 : | tmp.x = | ||
1105 : | (hint->rawhints) ? bhint->mvs[vec]. | ||
1106 : | x : BitstreamGetBits(&bs, length); | ||
1107 : | tmp.y = | ||
1108 : | (hint->rawhints) ? bhint->mvs[vec]. | ||
1109 : | y : BitstreamGetBits(&bs, length); | ||
1110 : | tmp.x -= (tmp.x >= high) ? high * 2 : 0; | ||
1111 : | tmp.y -= (tmp.y >= high) ? high * 2 : 0; | ||
1112 : | h | 1.20 | |
1113 : | edgomez | 1.41 | get_pmvdata(pEnc->current->mbs, x, y, |
1114 : | pEnc->mbParam.mb_width, vec, pred, dummy); | ||
1115 : | h | 1.20 | |
1116 : | edgomez | 1.41 | pMB->mvs[vec].x = tmp.x; |
1117 : | pMB->mvs[vec].y = tmp.y; | ||
1118 : | h | 1.20 | pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
1119 : | pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; | ||
1120 : | } | ||
1121 : | edgomez | 1.41 | } else // intra / stuffing / not_coded |
1122 : | h | 1.20 | { |
1123 : | edgomez | 1.41 | for (vec = 0; vec < 4; ++vec) { |
1124 : | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; | ||
1125 : | h | 1.26 | } |
1126 : | } | ||
1127 : | |||
1128 : | suxen_drol | 1.27 | if (pMB->mode == MODE_INTER4V && |
1129 : | edgomez | 1.41 | (pEnc->current->global_flags & XVID_LUMIMASKING) |
1130 : | && pMB->dquant != NO_CHANGE) { | ||
1131 : | h | 1.26 | pMB->mode = MODE_INTRA; |
1132 : | |||
1133 : | edgomez | 1.41 | for (vec = 0; vec < 4; ++vec) { |
1134 : | h | 1.26 | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1135 : | h | 1.20 | } |
1136 : | } | ||
1137 : | } | ||
1138 : | } | ||
1139 : | } | ||
1140 : | |||
1141 : | |||
1142 : | edgomez | 1.41 | void |
1143 : | HintedMEGet(Encoder * pEnc, | ||
1144 : | int intra) | ||
1145 : | h | 1.20 | { |
1146 : | edgomez | 1.41 | HINTINFO *hint; |
1147 : | h | 1.20 | Bitstream bs; |
1148 : | uint32_t x, y; | ||
1149 : | int length, high; | ||
1150 : | |||
1151 : | hint = pEnc->mbParam.hint; | ||
1152 : | |||
1153 : | edgomez | 1.41 | if (hint->rawhints) { |
1154 : | h | 1.20 | hint->mvhint.intra = intra; |
1155 : | edgomez | 1.41 | } else { |
1156 : | h | 1.20 | BitstreamInit(&bs, hint->hintstream, 0); |
1157 : | BitstreamPutBit(&bs, intra); | ||
1158 : | } | ||
1159 : | |||
1160 : | edgomez | 1.41 | if (intra) { |
1161 : | if (!hint->rawhints) { | ||
1162 : | h | 1.20 | BitstreamPad(&bs); |
1163 : | hint->hintlength = BitstreamLength(&bs); | ||
1164 : | } | ||
1165 : | return; | ||
1166 : | } | ||
1167 : | |||
1168 : | edgomez | 1.41 | length = pEnc->current->fcode + 5; |
1169 : | high = 1 << (length - 1); | ||
1170 : | h | 1.20 | |
1171 : | edgomez | 1.41 | if (hint->rawhints) { |
1172 : | suxen_drol | 1.27 | hint->mvhint.fcode = pEnc->current->fcode; |
1173 : | edgomez | 1.41 | } else { |
1174 : | suxen_drol | 1.27 | BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
1175 : | h | 1.20 | } |
1176 : | |||
1177 : | edgomez | 1.41 | for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1178 : | for (x = 0; x < pEnc->mbParam.mb_width; ++x) { | ||
1179 : | MACROBLOCK *pMB = | ||
1180 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1181 : | MVBLOCKHINT *bhint = | ||
1182 : | &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; | ||
1183 : | h | 1.20 | VECTOR tmp; |
1184 : | |||
1185 : | edgomez | 1.41 | if (hint->rawhints) { |
1186 : | h | 1.20 | bhint->mode = pMB->mode; |
1187 : | edgomez | 1.41 | } else { |
1188 : | h | 1.20 | BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
1189 : | } | ||
1190 : | |||
1191 : | edgomez | 1.41 | if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
1192 : | tmp.x = pMB->mvs[0].x; | ||
1193 : | tmp.y = pMB->mvs[0].y; | ||
1194 : | tmp.x += (tmp.x < 0) ? high * 2 : 0; | ||
1195 : | tmp.y += (tmp.y < 0) ? high * 2 : 0; | ||
1196 : | h | 1.20 | |
1197 : | edgomez | 1.41 | if (hint->rawhints) { |
1198 : | h | 1.20 | bhint->mvs[0].x = tmp.x; |
1199 : | bhint->mvs[0].y = tmp.y; | ||
1200 : | edgomez | 1.41 | } else { |
1201 : | h | 1.20 | BitstreamPutBits(&bs, tmp.x, length); |
1202 : | BitstreamPutBits(&bs, tmp.y, length); | ||
1203 : | } | ||
1204 : | edgomez | 1.41 | } else if (pMB->mode == MODE_INTER4V) { |
1205 : | h | 1.20 | int vec; |
1206 : | |||
1207 : | edgomez | 1.41 | for (vec = 0; vec < 4; ++vec) { |
1208 : | tmp.x = pMB->mvs[vec].x; | ||
1209 : | tmp.y = pMB->mvs[vec].y; | ||
1210 : | tmp.x += (tmp.x < 0) ? high * 2 : 0; | ||
1211 : | tmp.y += (tmp.y < 0) ? high * 2 : 0; | ||
1212 : | h | 1.20 | |
1213 : | edgomez | 1.41 | if (hint->rawhints) { |
1214 : | h | 1.20 | bhint->mvs[vec].x = tmp.x; |
1215 : | bhint->mvs[vec].y = tmp.y; | ||
1216 : | edgomez | 1.41 | } else { |
1217 : | h | 1.20 | BitstreamPutBits(&bs, tmp.x, length); |
1218 : | BitstreamPutBits(&bs, tmp.y, length); | ||
1219 : | } | ||
1220 : | } | ||
1221 : | } | ||
1222 : | } | ||
1223 : | } | ||
1224 : | |||
1225 : | edgomez | 1.41 | if (!hint->rawhints) { |
1226 : | h | 1.20 | BitstreamPad(&bs); |
1227 : | hint->hintlength = BitstreamLength(&bs); | ||
1228 : | } | ||
1229 : | } | ||
1230 : | |||
1231 : | |||
1232 : | edgomez | 1.41 | static int |
1233 : | FrameCodeI(Encoder * pEnc, | ||
1234 : | Bitstream * bs, | ||
1235 : | uint32_t * pBits) | ||
1236 : | h | 1.21 | { |
1237 : | |||
1238 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
1239 : | edgomez | 1.41 | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1240 : | h | 1.21 | |
1241 : | uint16_t x, y; | ||
1242 : | |||
1243 : | pEnc->iFrameNum = 0; | ||
1244 : | suxen_drol | 1.27 | pEnc->mbParam.m_rounding_type = 1; |
1245 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
1246 : | pEnc->current->coding_type = I_VOP; | ||
1247 : | h | 1.21 | |
1248 : | suxen_drol | 1.27 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1249 : | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); | ||
1250 : | h | 1.21 | |
1251 : | *pBits = BitstreamPos(bs); | ||
1252 : | |||
1253 : | pEnc->sStat.iTextBits = 0; | ||
1254 : | pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; | ||
1255 : | pEnc->sStat.mblks = pEnc->sStat.ublks = 0; | ||
1256 : | |||
1257 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) | ||
1258 : | edgomez | 1.41 | for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1259 : | MACROBLOCK *pMB = | ||
1260 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1261 : | h | 1.21 | |
1262 : | CodeIntraMB(pEnc, pMB); | ||
1263 : | |||
1264 : | edgomez | 1.41 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1265 : | dct_codes, qcoeff); | ||
1266 : | h | 1.21 | |
1267 : | start_timer(); | ||
1268 : | suxen_drol | 1.27 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1269 : | h | 1.21 | stop_prediction_timer(); |
1270 : | |||
1271 : | start_timer(); | ||
1272 : | suxen_drol | 1.27 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1273 : | h | 1.21 | stop_coding_timer(); |
1274 : | } | ||
1275 : | |||
1276 : | emms(); | ||
1277 : | |||
1278 : | *pBits = BitstreamPos(bs) - *pBits; | ||
1279 : | pEnc->sStat.fMvPrevSigma = -1; | ||
1280 : | pEnc->sStat.iMvSum = 0; | ||
1281 : | pEnc->sStat.iMvCount = 0; | ||
1282 : | suxen_drol | 1.27 | pEnc->mbParam.m_fcode = 2; |
1283 : | h | 1.21 | |
1284 : | edgomez | 1.41 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1285 : | h | 1.21 | HintedMEGet(pEnc, 1); |
1286 : | } | ||
1287 : | |||
1288 : | edgomez | 1.41 | return 1; // intra |
1289 : | h | 1.21 | } |
1290 : | |||
1291 : | |||
1292 : | Isibaar | 1.1 | #define INTRA_THRESHOLD 0.5 |
1293 : | |||
1294 : | edgomez | 1.41 | static int |
1295 : | FrameCodeP(Encoder * pEnc, | ||
1296 : | Bitstream * bs, | ||
1297 : | uint32_t * pBits, | ||
1298 : | bool force_inter, | ||
1299 : | bool vol_header) | ||
1300 : | Isibaar | 1.1 | { |
1301 : | edgomez | 1.3 | float fSigma; |
1302 : | edgomez | 1.13 | |
1303 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
1304 : | edgomez | 1.41 | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1305 : | Isibaar | 1.8 | |
1306 : | Isibaar | 1.1 | int iLimit; |
1307 : | edgomez | 1.3 | uint32_t x, y; |
1308 : | int iSearchRange; | ||
1309 : | edgomez | 1.29 | int bIntra; |
1310 : | Isibaar | 1.1 | |
1311 : | edgomez | 1.29 | /* IMAGE *pCurrent = &pEnc->current->image; */ |
1312 : | suxen_drol | 1.27 | IMAGE *pRef = &pEnc->reference->image; |
1313 : | Isibaar | 1.1 | |
1314 : | h | 1.11 | start_timer(); |
1315 : | edgomez | 1.41 | image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1316 : | pEnc->mbParam.width, pEnc->mbParam.height, | ||
1317 : | pEnc->current->global_flags & XVID_INTERLACING); | ||
1318 : | h | 1.11 | stop_edges_timer(); |
1319 : | Isibaar | 1.1 | |
1320 : | suxen_drol | 1.27 | pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1321 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
1322 : | pEnc->current->fcode = pEnc->mbParam.m_fcode; | ||
1323 : | Isibaar | 1.1 | |
1324 : | if (!force_inter) | ||
1325 : | edgomez | 1.41 | iLimit = |
1326 : | (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * | ||
1327 : | INTRA_THRESHOLD); | ||
1328 : | edgomez | 1.3 | else |
1329 : | Isibaar | 1.1 | iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
1330 : | |||
1331 : | suxen_drol | 1.27 | if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
1332 : | Isibaar | 1.1 | start_timer(); |
1333 : | edgomez | 1.41 | image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1334 : | &pEnc->vInterHV, pEnc->mbParam.edged_width, | ||
1335 : | pEnc->mbParam.edged_height, | ||
1336 : | pEnc->current->rounding_type); | ||
1337 : | Isibaar | 1.1 | stop_inter_timer(); |
1338 : | } | ||
1339 : | |||
1340 : | start_timer(); | ||
1341 : | edgomez | 1.41 | if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1342 : | h | 1.20 | HintedMESet(pEnc, &bIntra); |
1343 : | edgomez | 1.41 | } else { |
1344 : | bIntra = | ||
1345 : | MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, | ||
1346 : | &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, | ||
1347 : | iLimit); | ||
1348 : | h | 1.20 | } |
1349 : | Isibaar | 1.1 | stop_motion_timer(); |
1350 : | |||
1351 : | edgomez | 1.41 | if (bIntra == 1) { |
1352 : | Isibaar | 1.1 | return FrameCodeI(pEnc, bs, pBits); |
1353 : | h | 1.20 | } |
1354 : | Isibaar | 1.1 | |
1355 : | suxen_drol | 1.27 | pEnc->current->coding_type = P_VOP; |
1356 : | Isibaar | 1.1 | |
1357 : | edgomez | 1.41 | if (vol_header) |
1358 : | suxen_drol | 1.27 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1359 : | Isibaar | 1.1 | |
1360 : | suxen_drol | 1.27 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
1361 : | edgomez | 1.3 | |
1362 : | *pBits = BitstreamPos(bs); | ||
1363 : | |||
1364 : | pEnc->sStat.iTextBits = 0; | ||
1365 : | pEnc->sStat.iMvSum = 0; | ||
1366 : | pEnc->sStat.iMvCount = 0; | ||
1367 : | Isibaar | 1.1 | pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1368 : | |||
1369 : | edgomez | 1.41 | for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1370 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) { | ||
1371 : | MACROBLOCK *pMB = | ||
1372 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1373 : | Isibaar | 1.1 | |
1374 : | edgomez | 1.3 | bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1375 : | Isibaar | 1.1 | |
1376 : | edgomez | 1.41 | if (!bIntra) { |
1377 : | Isibaar | 1.1 | start_timer(); |
1378 : | edgomez | 1.41 | MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1379 : | &pEnc->vInterH, &pEnc->vInterV, | ||
1380 : | &pEnc->vInterHV, &pEnc->current->image, | ||
1381 : | dct_codes, pEnc->mbParam.width, | ||
1382 : | pEnc->mbParam.height, | ||
1383 : | pEnc->mbParam.edged_width, | ||
1384 : | pEnc->current->rounding_type); | ||
1385 : | Isibaar | 1.1 | stop_comp_timer(); |
1386 : | |||
1387 : | suxen_drol | 1.27 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1388 : | edgomez | 1.41 | if (pMB->dquant != NO_CHANGE) { |
1389 : | Isibaar | 1.1 | pMB->mode = MODE_INTER_Q; |
1390 : | suxen_drol | 1.27 | pEnc->current->quant += DQtab[pMB->dquant]; |
1391 : | edgomez | 1.41 | if (pEnc->current->quant > 31) |
1392 : | pEnc->current->quant = 31; | ||
1393 : | else if (pEnc->current->quant < 1) | ||
1394 : | pEnc->current->quant = 1; | ||
1395 : | Isibaar | 1.1 | } |
1396 : | } | ||
1397 : | suxen_drol | 1.27 | pMB->quant = pEnc->current->quant; |
1398 : | Isibaar | 1.1 | |
1399 : | h | 1.11 | pMB->field_pred = 0; |
1400 : | |||
1401 : | edgomez | 1.41 | pMB->cbp = |
1402 : | MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, | ||
1403 : | dct_codes, qcoeff); | ||
1404 : | } else { | ||
1405 : | Isibaar | 1.1 | CodeIntraMB(pEnc, pMB); |
1406 : | edgomez | 1.41 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1407 : | dct_codes, qcoeff); | ||
1408 : | Isibaar | 1.1 | } |
1409 : | |||
1410 : | edgomez | 1.3 | start_timer(); |
1411 : | edgomez | 1.41 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1412 : | Isibaar | 1.1 | stop_prediction_timer(); |
1413 : | |||
1414 : | edgomez | 1.41 | if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
1415 : | Isibaar | 1.1 | pEnc->sStat.kblks++; |
1416 : | edgomez | 1.41 | } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1417 : | pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || | ||
1418 : | pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { | ||
1419 : | Isibaar | 1.1 | pEnc->sStat.mblks++; |
1420 : | edgomez | 1.41 | } else { |
1421 : | Isibaar | 1.1 | pEnc->sStat.ublks++; |
1422 : | } | ||
1423 : | |||
1424 : | start_timer(); | ||
1425 : | suxen_drol | 1.27 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1426 : | Isibaar | 1.1 | stop_coding_timer(); |
1427 : | } | ||
1428 : | } | ||
1429 : | |||
1430 : | emms(); | ||
1431 : | h | 1.20 | |
1432 : | edgomez | 1.41 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1433 : | h | 1.20 | HintedMEGet(pEnc, 0); |
1434 : | } | ||
1435 : | Isibaar | 1.1 | |
1436 : | if (pEnc->sStat.iMvCount == 0) | ||
1437 : | pEnc->sStat.iMvCount = 1; | ||
1438 : | |||
1439 : | edgomez | 1.41 | fSigma = (float) sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
1440 : | Isibaar | 1.1 | |
1441 : | suxen_drol | 1.27 | iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
1442 : | Isibaar | 1.1 | |
1443 : | edgomez | 1.41 | if ((fSigma > iSearchRange / 3) |
1444 : | && (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 | ||
1445 : | edgomez | 1.3 | { |
1446 : | suxen_drol | 1.27 | pEnc->mbParam.m_fcode++; |
1447 : | Isibaar | 1.1 | iSearchRange *= 2; |
1448 : | edgomez | 1.41 | } else if ((fSigma < iSearchRange / 6) |
1449 : | && (pEnc->sStat.fMvPrevSigma >= 0) | ||
1450 : | && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) | ||
1451 : | && (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 | ||
1452 : | edgomez | 1.3 | { |
1453 : | suxen_drol | 1.27 | pEnc->mbParam.m_fcode--; |
1454 : | Isibaar | 1.1 | iSearchRange /= 2; |
1455 : | edgomez | 1.3 | } |
1456 : | Isibaar | 1.1 | |
1457 : | edgomez | 1.3 | pEnc->sStat.fMvPrevSigma = fSigma; |
1458 : | edgomez | 1.41 | |
1459 : | Isibaar | 1.1 | *pBits = BitstreamPos(bs) - *pBits; |
1460 : | |||
1461 : | edgomez | 1.41 | return 0; // inter |
1462 : | Isibaar | 1.1 | } |
1463 : | suxen_drol | 1.24 | |
1464 : | |||
1465 : | suxen_drol | 1.32 | #ifdef BFRAMES |
1466 : | edgomez | 1.41 | static void |
1467 : | FrameCodeB(Encoder * pEnc, | ||
1468 : | FRAMEINFO * frame, | ||
1469 : | Bitstream * bs, | ||
1470 : | uint32_t * pBits) | ||
1471 : | suxen_drol | 1.24 | { |
1472 : | edgomez | 1.41 | int16_t dct_codes[6 * 64]; |
1473 : | int16_t qcoeff[6 * 64]; | ||
1474 : | uint32_t x, y; | ||
1475 : | suxen_drol | 1.24 | VECTOR forward; |
1476 : | VECTOR backward; | ||
1477 : | |||
1478 : | edgomez | 1.41 | IMAGE *f_ref = &pEnc->reference->image; |
1479 : | suxen_drol | 1.24 | IMAGE *b_ref = &pEnc->current->image; |
1480 : | |||
1481 : | suxen_drol | 1.32 | // forward |
1482 : | edgomez | 1.41 | image_setedges(f_ref, pEnc->mbParam.edged_width, |
1483 : | pEnc->mbParam.edged_height, pEnc->mbParam.width, | ||
1484 : | pEnc->mbParam.height, | ||
1485 : | frame->global_flags & XVID_INTERLACING); | ||
1486 : | suxen_drol | 1.24 | start_timer(); |
1487 : | image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, | ||
1488 : | edgomez | 1.41 | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1489 : | 0); | ||
1490 : | suxen_drol | 1.24 | stop_inter_timer(); |
1491 : | |||
1492 : | suxen_drol | 1.32 | // backward |
1493 : | edgomez | 1.41 | image_setedges(b_ref, pEnc->mbParam.edged_width, |
1494 : | pEnc->mbParam.edged_height, pEnc->mbParam.width, | ||
1495 : | pEnc->mbParam.height, | ||
1496 : | frame->global_flags & XVID_INTERLACING); | ||
1497 : | start_timer(); | ||
1498 : | suxen_drol | 1.24 | image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1499 : | edgomez | 1.41 | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1500 : | 0); | ||
1501 : | suxen_drol | 1.24 | stop_inter_timer(); |
1502 : | |||
1503 : | start_timer(); | ||
1504 : | edgomez | 1.41 | MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, |
1505 : | &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, | ||
1506 : | pEnc->current->mbs, b_ref, &pEnc->vInterH, | ||
1507 : | &pEnc->vInterV, &pEnc->vInterHV); | ||
1508 : | |||
1509 : | suxen_drol | 1.24 | |
1510 : | stop_motion_timer(); | ||
1511 : | edgomez | 1.41 | |
1512 : | suxen_drol | 1.32 | /*if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
1513 : | edgomez | 1.41 | { |
1514 : | BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); | ||
1515 : | } */ | ||
1516 : | suxen_drol | 1.24 | |
1517 : | edgomez | 1.41 | frame->coding_type = B_VOP; |
1518 : | BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame); | ||
1519 : | edgomez | 1.29 | |
1520 : | edgomez | 1.41 | *pBits = BitstreamPos(bs); |
1521 : | edgomez | 1.29 | |
1522 : | edgomez | 1.41 | pEnc->sStat.iTextBits = 0; |
1523 : | pEnc->sStat.iMvSum = 0; | ||
1524 : | pEnc->sStat.iMvCount = 0; | ||
1525 : | suxen_drol | 1.24 | pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1526 : | |||
1527 : | |||
1528 : | edgomez | 1.41 | for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1529 : | suxen_drol | 1.24 | // reset prediction |
1530 : | |||
1531 : | forward.x = 0; | ||
1532 : | forward.y = 0; | ||
1533 : | backward.x = 0; | ||
1534 : | backward.y = 0; | ||
1535 : | |||
1536 : | edgomez | 1.41 | for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1537 : | MACROBLOCK *f_mb = | ||
1538 : | &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1539 : | MACROBLOCK *b_mb = | ||
1540 : | &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1541 : | MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; | ||
1542 : | suxen_drol | 1.24 | |
1543 : | // decoder ignores mb when refence block is INTER(0,0), CBP=0 | ||
1544 : | edgomez | 1.41 | if (mb->mode == MODE_NOT_CODED) { |
1545 : | suxen_drol | 1.24 | mb->mvs[0].x = 0; |
1546 : | mb->mvs[0].y = 0; | ||
1547 : | continue; | ||
1548 : | } | ||
1549 : | |||
1550 : | MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, | ||
1551 : | edgomez | 1.41 | f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1552 : | &pEnc->f_refhv, b_ref, &pEnc->vInterH, | ||
1553 : | &pEnc->vInterV, &pEnc->vInterHV, | ||
1554 : | dct_codes); | ||
1555 : | |||
1556 : | suxen_drol | 1.24 | mb->quant = frame->quant; |
1557 : | edgomez | 1.41 | mb->cbp = |
1558 : | MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, | ||
1559 : | qcoeff); | ||
1560 : | suxen_drol | 1.24 | //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
1561 : | |||
1562 : | |||
1563 : | edgomez | 1.41 | if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
1564 : | && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { | ||
1565 : | mb->mode = 5; // skipped | ||
1566 : | suxen_drol | 1.24 | } |
1567 : | |||
1568 : | edgomez | 1.41 | if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
1569 : | suxen_drol | 1.24 | mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
1570 : | mb->pmvs[0].y = mb->mvs[0].y - forward.y; | ||
1571 : | forward.x = mb->mvs[0].x; | ||
1572 : | forward.y = mb->mvs[0].y; | ||
1573 : | } | ||
1574 : | edgomez | 1.41 | |
1575 : | if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { | ||
1576 : | suxen_drol | 1.24 | mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
1577 : | mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; | ||
1578 : | backward.x = mb->b_mvs[0].x; | ||
1579 : | backward.y = mb->b_mvs[0].y; | ||
1580 : | } | ||
1581 : | edgomez | 1.41 | // printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y); |
1582 : | suxen_drol | 1.24 | |
1583 : | edgomez | 1.41 | start_timer(); |
1584 : | MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, | ||
1585 : | &pEnc->sStat); | ||
1586 : | suxen_drol | 1.24 | stop_coding_timer(); |
1587 : | } | ||
1588 : | } | ||
1589 : | |||
1590 : | emms(); | ||
1591 : | |||
1592 : | // TODO: dynamic fcode/bcode ??? | ||
1593 : | |||
1594 : | *pBits = BitstreamPos(bs) - *pBits; | ||
1595 : | } | ||
1596 : | edgomez | 1.37 | #endif |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |