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