[svn] / branches / Isibaar / xvidcore / src / encoder.c Repository:
ViewVC logotype

Annotation of /branches/Isibaar/xvidcore/src/encoder.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1004 - (view) (download)

1 : edgomez 194 /*****************************************************************************
2 : edgomez 145 *
3 :     * XVID MPEG-4 VIDEO CODEC
4 : edgomez 851 * - Encoder main module -
5 : edgomez 145 *
6 : edgomez 851 * 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 : edgomez 402 *
15 : edgomez 851 * 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 : edgomez 145 * 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 : Isibaar 1004 * $Id: encoder.c,v 1.99.2.1 2003-05-03 23:24:30 Isibaar Exp $
30 : edgomez 405 *
31 : edgomez 194 ****************************************************************************/
32 : suxen_drol 118
33 : Isibaar 3 #include <stdlib.h>
34 :     #include <stdio.h>
35 :     #include <math.h>
36 : edgomez 198 #include <string.h>
37 : Isibaar 3
38 :     #include "encoder.h"
39 :     #include "prediction/mbprediction.h"
40 :     #include "global.h"
41 :     #include "utils/timer.h"
42 :     #include "image/image.h"
43 : edgomez 851 #include "image/font.h"
44 :     #include "motion/sad.h"
45 : suxen_drol 152 #include "motion/motion.h"
46 : Isibaar 3 #include "bitstream/cbp.h"
47 :     #include "utils/mbfunctions.h"
48 :     #include "bitstream/bitstream.h"
49 :     #include "bitstream/mbcoding.h"
50 :     #include "utils/ratecontrol.h"
51 :     #include "utils/emms.h"
52 :     #include "bitstream/mbcoding.h"
53 :     #include "quant/adapt_quant.h"
54 : Isibaar 4 #include "quant/quant_matrix.h"
55 : Isibaar 41 #include "utils/mem_align.h"
56 : Isibaar 3
57 : edgomez 194 /*****************************************************************************
58 :     * Local macros
59 :     ****************************************************************************/
60 :    
61 : Isibaar 3 #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
62 : syskin 903 #define SWAP(_T_,A,B) { _T_ tmp = A; A = B; B = tmp; }
63 : Isibaar 3
64 : edgomez 194 /*****************************************************************************
65 :     * Local function prototypes
66 :     ****************************************************************************/
67 :    
68 : edgomez 192 static int FrameCodeI(Encoder * pEnc,
69 : edgomez 195 Bitstream * bs,
70 :     uint32_t * pBits);
71 : Isibaar 3
72 : edgomez 192 static int FrameCodeP(Encoder * pEnc,
73 : edgomez 195 Bitstream * bs,
74 :     uint32_t * pBits,
75 :     bool force_inter,
76 : Isibaar 1004 bool vol_header,
77 :     int interpolate);
78 : Isibaar 3
79 : edgomez 851 static void FrameCodeB(Encoder * pEnc,
80 :     FRAMEINFO * frame,
81 :     Bitstream * bs,
82 : Isibaar 1004 uint32_t * pBits,
83 :     int interpolate_forward,
84 :     int interpolate_backward);
85 : edgomez 851
86 : edgomez 194 /*****************************************************************************
87 :     * Local data
88 :     ****************************************************************************/
89 :    
90 : edgomez 195 static int DQtab[4] = {
91 : Isibaar 3 -1, -2, 1, 2
92 :     };
93 :    
94 : edgomez 195 static int iDQtab[5] = {
95 : Isibaar 3 1, 0, NO_CHANGE, 2, 3
96 :     };
97 :    
98 :    
99 : edgomez 194 /*****************************************************************************
100 :     * Encoder creation
101 :     *
102 :     * This function creates an Encoder instance, it allocates all necessary
103 : edgomez 851 * image buffers (reference, current and bframes) and initialize the internal
104 :     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
105 : edgomez 194 *
106 :     * The code seems to be very long but is very basic, mainly memory allocation
107 :     * and cleaning code.
108 :     *
109 :     * Returned values :
110 : syskin 903 * - XVID_ERR_OK - no errors
111 :     * - XVID_ERR_MEMORY - the libc could not allocate memory, the function
112 :     * cleans the structure before exiting.
113 :     * pParam->handle is also set to NULL.
114 : edgomez 194 *
115 :     ****************************************************************************/
116 :    
117 :     int
118 :     encoder_create(XVID_ENC_PARAM * pParam)
119 : Isibaar 3 {
120 : edgomez 13 Encoder *pEnc;
121 : suxen_drol 229 int i;
122 : edgomez 13 pParam->handle = NULL;
123 : Isibaar 3
124 : edgomez 13 ENC_CHECK(pParam);
125 : Isibaar 3
126 : edgomez 13 ENC_CHECK(pParam->width > 0 && pParam->width <= 1920);
127 :     ENC_CHECK(pParam->height > 0 && pParam->height <= 1280);
128 :     ENC_CHECK(!(pParam->width % 2));
129 :     ENC_CHECK(!(pParam->height % 2));
130 : Isibaar 3
131 : edgomez 192 /* Fps */
132 :    
133 : edgomez 195 if (pParam->fincr <= 0 || pParam->fbase <= 0) {
134 : Isibaar 3 pParam->fincr = 1;
135 :     pParam->fbase = 25;
136 :     }
137 :    
138 : edgomez 192 /*
139 :     * Simplify the "fincr/fbase" fraction
140 :     * (neccessary, since windows supplies us with huge numbers)
141 :     */
142 : Isibaar 3
143 :     i = pParam->fincr;
144 : edgomez 195 while (i > 1) {
145 :     if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
146 : Isibaar 3 pParam->fincr /= i;
147 :     pParam->fbase /= i;
148 :     i = pParam->fincr;
149 :     continue;
150 :     }
151 :     i--;
152 :     }
153 :    
154 : edgomez 195 if (pParam->fbase > 65535) {
155 :     float div = (float) pParam->fbase / 65535;
156 :    
157 :     pParam->fbase = (int) (pParam->fbase / div);
158 :     pParam->fincr = (int) (pParam->fincr / div);
159 : Isibaar 3 }
160 :    
161 : edgomez 192 /* Bitrate allocator defaults */
162 :    
163 : h 121 if (pParam->rc_bitrate <= 0)
164 :     pParam->rc_bitrate = 900000;
165 : Isibaar 3
166 : h 121 if (pParam->rc_reaction_delay_factor <= 0)
167 :     pParam->rc_reaction_delay_factor = 16;
168 : Isibaar 3
169 : h 121 if (pParam->rc_averaging_period <= 0)
170 :     pParam->rc_averaging_period = 100;
171 :    
172 :     if (pParam->rc_buffer <= 0)
173 :     pParam->rc_buffer = 100;
174 :    
175 : edgomez 192 /* Max and min quantizers */
176 :    
177 : edgomez 13 if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
178 : Isibaar 3 pParam->min_quantizer = 1;
179 :    
180 : edgomez 13 if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
181 : Isibaar 3 pParam->max_quantizer = 31;
182 :    
183 : edgomez 13 if (pParam->max_quantizer < pParam->min_quantizer)
184 : Isibaar 3 pParam->max_quantizer = pParam->min_quantizer;
185 :    
186 : edgomez 195 /* 1 keyframe each 10 seconds */
187 : edgomez 192
188 : edgomez 325 if (pParam->max_key_interval <= 0)
189 : edgomez 192 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
190 :    
191 :     pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
192 :     if (pEnc == NULL)
193 : Isibaar 3 return XVID_ERR_MEMORY;
194 :    
195 : edgomez 198 /* Zero the Encoder Structure */
196 :    
197 :     memset(pEnc, 0, sizeof(Encoder));
198 :    
199 : Isibaar 3 /* Fill members of Encoder structure */
200 :    
201 : edgomez 13 pEnc->mbParam.width = pParam->width;
202 :     pEnc->mbParam.height = pParam->height;
203 : Isibaar 3
204 :     pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;
205 :     pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;
206 :    
207 : edgomez 195 pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
208 :     pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
209 : Isibaar 3
210 : suxen_drol 152 pEnc->mbParam.fbase = pParam->fbase;
211 :     pEnc->mbParam.fincr = pParam->fincr;
212 :    
213 : Isibaar 208 pEnc->mbParam.m_quant_type = H263_QUANT;
214 :    
215 : edgomez 851 pEnc->fMvPrevSigma = -1;
216 : Isibaar 3
217 :     /* Fill rate control parameters */
218 :    
219 : h 121 pEnc->bitrate = pParam->rc_bitrate;
220 : Isibaar 3
221 : edgomez 851 pEnc->iFrameNum = -1;
222 :     pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;
223 : Isibaar 3
224 : suxen_drol 136 /* try to allocate frame memory */
225 : Isibaar 3
226 : edgomez 192 pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
227 :     pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
228 : Isibaar 3
229 : edgomez 195 if (pEnc->current == NULL || pEnc->reference == NULL)
230 : edgomez 192 goto xvid_err_memory1;
231 :    
232 : suxen_drol 136 /* try to allocate mb memory */
233 : Isibaar 3
234 : edgomez 195 pEnc->current->mbs =
235 :     xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
236 :     pEnc->mbParam.mb_height, CACHE_LINE);
237 :     pEnc->reference->mbs =
238 :     xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
239 :     pEnc->mbParam.mb_height, CACHE_LINE);
240 : suxen_drol 136
241 : edgomez 192 if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
242 :     goto xvid_err_memory2;
243 : suxen_drol 136
244 :     /* try to allocate image memory */
245 :    
246 : edgomez 851 if (pParam->global & XVID_GLOBAL_EXTRASTATS)
247 :     image_null(&pEnc->sOriginal);
248 :    
249 :     image_null(&pEnc->f_refh);
250 :     image_null(&pEnc->f_refv);
251 :     image_null(&pEnc->f_refhv);
252 :    
253 : suxen_drol 136 image_null(&pEnc->current->image);
254 :     image_null(&pEnc->reference->image);
255 :     image_null(&pEnc->vInterH);
256 :     image_null(&pEnc->vInterV);
257 : edgomez 851 image_null(&pEnc->vInterVf);
258 : suxen_drol 136 image_null(&pEnc->vInterHV);
259 : edgomez 851 image_null(&pEnc->vInterHVf);
260 : edgomez 192
261 : edgomez 851 if (pParam->global & XVID_GLOBAL_EXTRASTATS)
262 :     { if (image_create
263 :     (&pEnc->sOriginal, pEnc->mbParam.edged_width,
264 :     pEnc->mbParam.edged_height) < 0)
265 :     goto xvid_err_memory3;
266 :     }
267 :    
268 : edgomez 195 if (image_create
269 : edgomez 851 (&pEnc->f_refh, pEnc->mbParam.edged_width,
270 : edgomez 195 pEnc->mbParam.edged_height) < 0)
271 : edgomez 192 goto xvid_err_memory3;
272 : edgomez 195 if (image_create
273 : edgomez 851 (&pEnc->f_refv, pEnc->mbParam.edged_width,
274 :     pEnc->mbParam.edged_height) < 0)
275 :     goto xvid_err_memory3;
276 :     if (image_create
277 :     (&pEnc->f_refhv, pEnc->mbParam.edged_width,
278 :     pEnc->mbParam.edged_height) < 0)
279 :     goto xvid_err_memory3;
280 :    
281 :     if (image_create
282 : edgomez 195 (&pEnc->current->image, pEnc->mbParam.edged_width,
283 :     pEnc->mbParam.edged_height) < 0)
284 : edgomez 192 goto xvid_err_memory3;
285 : edgomez 195 if (image_create
286 :     (&pEnc->reference->image, pEnc->mbParam.edged_width,
287 :     pEnc->mbParam.edged_height) < 0)
288 : edgomez 192 goto xvid_err_memory3;
289 : edgomez 195 if (image_create
290 :     (&pEnc->vInterH, pEnc->mbParam.edged_width,
291 :     pEnc->mbParam.edged_height) < 0)
292 : edgomez 192 goto xvid_err_memory3;
293 : edgomez 195 if (image_create
294 :     (&pEnc->vInterV, pEnc->mbParam.edged_width,
295 :     pEnc->mbParam.edged_height) < 0)
296 : edgomez 192 goto xvid_err_memory3;
297 : edgomez 195 if (image_create
298 : edgomez 851 (&pEnc->vInterVf, pEnc->mbParam.edged_width,
299 :     pEnc->mbParam.edged_height) < 0)
300 :     goto xvid_err_memory3;
301 :     if (image_create
302 : edgomez 195 (&pEnc->vInterHV, pEnc->mbParam.edged_width,
303 :     pEnc->mbParam.edged_height) < 0)
304 : edgomez 192 goto xvid_err_memory3;
305 : edgomez 851 if (image_create
306 :     (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
307 :     pEnc->mbParam.edged_height) < 0)
308 :     goto xvid_err_memory3;
309 : suxen_drol 136
310 : edgomez 851 /* Create full bitplane for GMC, this might be wasteful */
311 :     if (image_create
312 :     (&pEnc->vGMC, pEnc->mbParam.edged_width,
313 :     pEnc->mbParam.edged_height) < 0)
314 :     goto xvid_err_memory3;
315 :    
316 :    
317 :    
318 :     pEnc->mbParam.global = pParam->global;
319 :    
320 :     /* B Frames specific init */
321 :     pEnc->mbParam.max_bframes = pParam->max_bframes;
322 :     pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;
323 :     pEnc->mbParam.bquant_offset = pParam->bquant_offset;
324 :     pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio;
325 :     pEnc->bframes = NULL;
326 :    
327 :     if (pEnc->mbParam.max_bframes > 0) {
328 :     int n;
329 :    
330 :     pEnc->bframes =
331 :     xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
332 :     CACHE_LINE);
333 :    
334 :     if (pEnc->bframes == NULL)
335 :     goto xvid_err_memory3;
336 :    
337 :     for (n = 0; n < pEnc->mbParam.max_bframes; n++)
338 :     pEnc->bframes[n] = NULL;
339 :    
340 :    
341 :     for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
342 :     pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
343 :    
344 :     if (pEnc->bframes[n] == NULL)
345 :     goto xvid_err_memory4;
346 :    
347 :     pEnc->bframes[n]->mbs =
348 :     xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
349 :     pEnc->mbParam.mb_height, CACHE_LINE);
350 :    
351 :     if (pEnc->bframes[n]->mbs == NULL)
352 :     goto xvid_err_memory4;
353 :    
354 :     image_null(&pEnc->bframes[n]->image);
355 :    
356 :     if (image_create
357 :     (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
358 :     pEnc->mbParam.edged_height) < 0)
359 :     goto xvid_err_memory4;
360 :    
361 :     }
362 :     }
363 :    
364 :     pEnc->bframenum_head = 0;
365 :     pEnc->bframenum_tail = 0;
366 :     pEnc->flush_bframes = 0;
367 :     pEnc->bframenum_dx50bvop = -1;
368 :    
369 :     pEnc->queue = NULL;
370 :    
371 :     if (pEnc->mbParam.max_bframes > 0) {
372 :     int n;
373 :    
374 :     pEnc->queue =
375 :     xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
376 :     CACHE_LINE);
377 :    
378 :     if (pEnc->queue == NULL)
379 :     goto xvid_err_memory4;
380 :    
381 :     for (n = 0; n < pEnc->mbParam.max_bframes; n++)
382 :     image_null(&pEnc->queue[n]);
383 :    
384 :     for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
385 :     if (image_create
386 :     (&pEnc->queue[n], pEnc->mbParam.edged_width,
387 :     pEnc->mbParam.edged_height) < 0)
388 :     goto xvid_err_memory5;
389 :    
390 :     }
391 :     }
392 :    
393 :     pEnc->queue_head = 0;
394 :     pEnc->queue_tail = 0;
395 :     pEnc->queue_size = 0;
396 :    
397 :     pEnc->mbParam.m_stamp = 0;
398 :    
399 :     pEnc->m_framenum = 0;
400 :     pEnc->current->stamp = 0;
401 :     pEnc->reference->stamp = 0;
402 :    
403 : edgomez 195 pParam->handle = (void *) pEnc;
404 : Isibaar 3
405 : edgomez 195 if (pParam->rc_bitrate) {
406 :     RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
407 :     pParam->rc_reaction_delay_factor,
408 :     pParam->rc_averaging_period, pParam->rc_buffer,
409 :     pParam->fbase * 1000 / pParam->fincr,
410 :     pParam->max_quantizer, pParam->min_quantizer);
411 : Isibaar 3 }
412 :    
413 : Isibaar 36 init_timer();
414 : Isibaar 3
415 :     return XVID_ERR_OK;
416 : edgomez 192
417 :     /*
418 :     * We handle all XVID_ERR_MEMORY here, this makes the code lighter
419 :     */
420 : suxen_drol 229
421 : edgomez 851 xvid_err_memory5:
422 :    
423 :     if (pEnc->mbParam.max_bframes > 0) {
424 :    
425 :     for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
426 :     image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
427 :     pEnc->mbParam.edged_height);
428 :     }
429 :     xvid_free(pEnc->queue);
430 :     }
431 :    
432 :     xvid_err_memory4:
433 :    
434 :     if (pEnc->mbParam.max_bframes > 0) {
435 :    
436 :     for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
437 :    
438 :     if (pEnc->bframes[i] == NULL)
439 :     continue;
440 :    
441 :     image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
442 :     pEnc->mbParam.edged_height);
443 : syskin 903
444 : edgomez 851 xvid_free(pEnc->bframes[i]->mbs);
445 : syskin 903
446 : edgomez 851 xvid_free(pEnc->bframes[i]);
447 :    
448 : syskin 903 }
449 : edgomez 851
450 :     xvid_free(pEnc->bframes);
451 :     }
452 :    
453 : edgomez 195 xvid_err_memory3:
454 : edgomez 851
455 :     if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
456 :     { image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
457 :     pEnc->mbParam.edged_height);
458 :     }
459 :    
460 :     image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
461 : edgomez 195 pEnc->mbParam.edged_height);
462 : edgomez 851 image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
463 :     pEnc->mbParam.edged_height);
464 :     image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
465 :     pEnc->mbParam.edged_height);
466 : edgomez 192
467 : edgomez 195 image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
468 :     pEnc->mbParam.edged_height);
469 :     image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
470 :     pEnc->mbParam.edged_height);
471 :     image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
472 :     pEnc->mbParam.edged_height);
473 :     image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
474 :     pEnc->mbParam.edged_height);
475 : edgomez 851 image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
476 :     pEnc->mbParam.edged_height);
477 : edgomez 195 image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
478 :     pEnc->mbParam.edged_height);
479 : edgomez 851 image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
480 :     pEnc->mbParam.edged_height);
481 : edgomez 192
482 : edgomez 851 /* destroy GMC image */
483 :     image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
484 :     pEnc->mbParam.edged_height);
485 :    
486 :    
487 : edgomez 195 xvid_err_memory2:
488 : edgomez 192 xvid_free(pEnc->current->mbs);
489 :     xvid_free(pEnc->reference->mbs);
490 :    
491 : edgomez 195 xvid_err_memory1:
492 : edgomez 192 xvid_free(pEnc->current);
493 :     xvid_free(pEnc->reference);
494 :     xvid_free(pEnc);
495 :    
496 : edgomez 194 pParam->handle = NULL;
497 :    
498 : edgomez 192 return XVID_ERR_MEMORY;
499 : Isibaar 3 }
500 :    
501 : edgomez 194 /*****************************************************************************
502 :     * Encoder destruction
503 :     *
504 :     * This function destroy the entire encoder structure created by a previous
505 :     * successful encoder_create call.
506 :     *
507 :     * Returned values (for now only one returned value) :
508 : syskin 903 * - XVID_ERR_OK - no errors
509 : edgomez 194 *
510 :     ****************************************************************************/
511 :    
512 :     int
513 :     encoder_destroy(Encoder * pEnc)
514 : Isibaar 3 {
515 : edgomez 851 int i;
516 : syskin 903
517 : edgomez 13 ENC_CHECK(pEnc);
518 : Isibaar 3
519 : edgomez 851 /* B Frames specific */
520 :     if (pEnc->mbParam.max_bframes > 0) {
521 :    
522 :     for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
523 : syskin 903
524 : edgomez 851 image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
525 :     pEnc->mbParam.edged_height);
526 :     }
527 :     xvid_free(pEnc->queue);
528 :     }
529 :    
530 :     if (pEnc->mbParam.max_bframes > 0) {
531 :    
532 :     for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
533 :    
534 :     if (pEnc->bframes[i] == NULL)
535 :     continue;
536 :    
537 :     image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
538 :     pEnc->mbParam.edged_height);
539 :    
540 :     xvid_free(pEnc->bframes[i]->mbs);
541 :    
542 :     xvid_free(pEnc->bframes[i]);
543 :     }
544 :    
545 :     xvid_free(pEnc->bframes);
546 : syskin 903
547 : edgomez 851 }
548 :    
549 : edgomez 192 /* All images, reference, current etc ... */
550 : edgomez 851
551 : edgomez 195 image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
552 :     pEnc->mbParam.edged_height);
553 :     image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
554 :     pEnc->mbParam.edged_height);
555 :     image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
556 :     pEnc->mbParam.edged_height);
557 :     image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
558 :     pEnc->mbParam.edged_height);
559 : edgomez 851 image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
560 :     pEnc->mbParam.edged_height);
561 : edgomez 195 image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
562 :     pEnc->mbParam.edged_height);
563 : edgomez 851 image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
564 :     pEnc->mbParam.edged_height);
565 : edgomez 403
566 : edgomez 851 image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
567 : edgomez 195 pEnc->mbParam.edged_height);
568 : edgomez 851 image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
569 :     pEnc->mbParam.edged_height);
570 :     image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
571 :     pEnc->mbParam.edged_height);
572 : edgomez 192
573 : edgomez 851 if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
574 :     { image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
575 :     pEnc->mbParam.edged_height);
576 :     }
577 :    
578 : edgomez 192 /* Encoder structure */
579 : edgomez 851
580 : suxen_drol 136 xvid_free(pEnc->current->mbs);
581 :     xvid_free(pEnc->current);
582 :    
583 :     xvid_free(pEnc->reference->mbs);
584 :     xvid_free(pEnc->reference);
585 :    
586 : Isibaar 41 xvid_free(pEnc);
587 : edgomez 192
588 : edgomez 13 return XVID_ERR_OK;
589 : Isibaar 3 }
590 :    
591 : suxen_drol 229
592 : edgomez 851 static __inline void inc_frame_num(Encoder * pEnc)
593 : suxen_drol 229 {
594 : edgomez 867 pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */
595 : edgomez 851 pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
596 : chl 387 }
597 : suxen_drol 229
598 : edgomez 851
599 :     static __inline void
600 :     queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
601 :     {
602 :     if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
603 :     {
604 :     DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
605 :     return;
606 :     }
607 :    
608 :     DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i",
609 :     pEnc->bframenum_head, pEnc->bframenum_tail,
610 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
611 :    
612 :    
613 :     start_timer();
614 :     if (image_input
615 :     (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
616 :     pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
617 :     return;
618 :     stop_conv_timer();
619 :    
620 : edgomez 853 if ((pFrame->general & XVID_CHROMAOPT)) {
621 : syskin 903 image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],
622 : edgomez 853 pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
623 :     }
624 :    
625 : edgomez 851 pEnc->queue_size++;
626 :     pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
627 :     }
628 :    
629 : syskin 903 static __inline void
630 : edgomez 851 set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
631 :     {
632 :    
633 :     pCur->ticks = (int32_t)pCur->stamp % time_base;
634 :     pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ;
635 : syskin 903
636 : edgomez 867 /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */
637 : edgomez 851
638 :     /* fprintf(stderr,"WriteVop: %d - %d \n",
639 :     ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
640 :     fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n",
641 :     pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
642 :     fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n",
643 :     pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
644 :    
645 :     */
646 :     }
647 :    
648 :    
649 :    
650 :     /* convert pFrame->intra to coding_type */
651 :     static int intra2coding_type(int intra)
652 :     {
653 :     if (intra < 0) return -1;
654 :     if (intra == 1) return I_VOP;
655 :     if (intra == 2) return B_VOP;
656 :    
657 :     return P_VOP;
658 :     }
659 :    
660 :    
661 :    
662 : edgomez 194 /*****************************************************************************
663 : edgomez 851 * IPB frame encoder entry point
664 :     *
665 :     * Returned values :
666 : syskin 903 * - XVID_ERR_OK - no errors
667 :     * - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
668 :     * format
669 : edgomez 851 ****************************************************************************/
670 :    
671 :     int
672 :     encoder_encode_bframes(Encoder * pEnc,
673 : syskin 903 XVID_ENC_FRAME * pFrame,
674 :     XVID_ENC_STATS * pResult)
675 : edgomez 851 {
676 :     uint16_t x, y;
677 :     Bitstream bs;
678 :     uint32_t bits;
679 : edgomez 875 int mode = -1; /* Just to shut up compiler warning */
680 : edgomez 851
681 :     int input_valid = 1;
682 :     int bframes_count = 0;
683 : Isibaar 1004 int interpolate_forward = 1;
684 :     int interpolate_backward = 1;
685 : edgomez 851
686 :     ENC_CHECK(pEnc);
687 :     ENC_CHECK(pFrame);
688 :     ENC_CHECK(pFrame->image);
689 :    
690 :     start_global_timer();
691 :    
692 :     BitstreamInit(&bs, pFrame->bitstream, 0);
693 :    
694 :     ipvop_loop:
695 :    
696 :     /*
697 :     * bframe "flush" code
698 :     */
699 :    
700 :     if ((pFrame->image == NULL || pEnc->flush_bframes)
701 :     && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
702 :    
703 :     if (pEnc->flush_bframes == 0) {
704 :     /*
705 :     * we have reached the end of stream without getting
706 :     * a future reference frame... so encode last final
707 :     * frame as a pframe
708 :     */
709 :    
710 :     DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i",
711 :     pEnc->bframenum_head, pEnc->bframenum_tail,
712 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
713 :    
714 :     pEnc->bframenum_tail--;
715 :     SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
716 :    
717 :     SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
718 :    
719 : Isibaar 1004 if (pEnc->mbParam.m_quarterpel)
720 :     pEnc->current->global_flags |= XVID_QUARTERPEL;
721 :     else
722 :     pEnc->current->global_flags &= ~XVID_QUARTERPEL;
723 :    
724 :     FrameCodeP(pEnc, &bs, &bits, 1, 0, interpolate_backward);
725 : edgomez 851 bframes_count = 0;
726 : Isibaar 1004 interpolate_forward = 0;
727 :     interpolate_backward = 1;
728 : edgomez 851
729 :     BitstreamPadAlways(&bs);
730 :     pFrame->length = BitstreamLength(&bs);
731 :     pFrame->intra = 0;
732 :    
733 :    
734 :     emms();
735 :    
736 :     if (pResult) {
737 :     pResult->quant = pEnc->current->quant;
738 :     pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
739 :     pResult->kblks = pEnc->current->sStat.kblks;
740 :     pResult->mblks = pEnc->current->sStat.mblks;
741 :     pResult->ublks = pEnc->current->sStat.ublks;
742 : Isibaar 1004 pResult->iblks = pEnc->current->sStat.iblks;
743 :     pResult->qblks = pEnc->current->sStat.qblks;
744 : edgomez 851 }
745 :    
746 :     return XVID_ERR_OK;
747 :     }
748 :    
749 : syskin 903
750 : edgomez 851 DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i",
751 :     pEnc->bframenum_head, pEnc->bframenum_tail,
752 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
753 :    
754 : Isibaar 1004 if (pEnc->mbParam.m_quarterpel)
755 :     pEnc->current->global_flags |= XVID_QUARTERPEL;
756 :     else
757 :     pEnc->current->global_flags &= ~XVID_QUARTERPEL;
758 :    
759 :     FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs,
760 :     &bits, interpolate_forward, interpolate_backward);
761 : edgomez 851 pEnc->bframenum_head++;
762 :    
763 : Isibaar 1004 interpolate_forward = 0;
764 :     interpolate_backward = 0;
765 :    
766 : edgomez 851 BitstreamPadAlways(&bs);
767 :     pFrame->length = BitstreamLength(&bs);
768 :     pFrame->intra = 2;
769 :    
770 :     if (pResult) {
771 :     pResult->quant = pEnc->current->quant;
772 :     pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
773 :     pResult->kblks = pEnc->current->sStat.kblks;
774 :     pResult->mblks = pEnc->current->sStat.mblks;
775 :     pResult->ublks = pEnc->current->sStat.ublks;
776 : Isibaar 1004 pResult->iblks = pEnc->current->sStat.iblks;
777 :     pResult->qblks = pEnc->current->sStat.qblks;
778 : edgomez 851 }
779 :    
780 : Isibaar 967 emms();
781 :    
782 :     if (pFrame->quant == 0) {
783 :     RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
784 :     pFrame->length, pFrame->intra);
785 :     }
786 :    
787 : edgomez 851 if (input_valid)
788 :     queue_image(pEnc, pFrame);
789 :    
790 :     emms();
791 :    
792 :     return XVID_ERR_OK;
793 :     }
794 :    
795 :     if (pEnc->bframenum_head > 0) {
796 :     pEnc->bframenum_head = pEnc->bframenum_tail = 0;
797 :    
798 :     /* write an empty marker to the bitstream.
799 :     for divx5 decoder compatibility, this marker must consist
800 : syskin 903 of a not-coded p-vop, with a time_base of zero, and time_increment
801 : edgomez 851 indentical to the future-referece frame.
802 :     */
803 :    
804 :     if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
805 :     int tmp;
806 : syskin 903
807 : edgomez 851 DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i",
808 :     pEnc->bframenum_head, pEnc->bframenum_tail,
809 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
810 :    
811 :     tmp = pEnc->current->seconds;
812 :     pEnc->current->seconds = 0; /* force time_base = 0 */
813 :    
814 :     BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
815 :     pEnc->current->seconds = tmp;
816 :    
817 :     BitstreamPadAlways(&bs);
818 :     pFrame->length = BitstreamLength(&bs);
819 :     pFrame->intra = 4;
820 :    
821 :     if (pResult) {
822 :     pResult->quant = pEnc->current->quant;
823 :     pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
824 :     pResult->kblks = pEnc->current->sStat.kblks;
825 :     pResult->mblks = pEnc->current->sStat.mblks;
826 :     pResult->ublks = pEnc->current->sStat.ublks;
827 : Isibaar 1004 pResult->iblks = pEnc->current->sStat.iblks;
828 :     pResult->qblks = pEnc->current->sStat.qblks;
829 : edgomez 851 }
830 :    
831 : Isibaar 967 emms();
832 :    
833 :     if (pFrame->quant == 0) {
834 :     RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
835 :     pFrame->length, pFrame->intra);
836 :     }
837 :    
838 : edgomez 851 if (input_valid)
839 :     queue_image(pEnc, pFrame);
840 :    
841 :     emms();
842 :    
843 :     return XVID_ERR_OK;
844 :     }
845 :     }
846 :    
847 :    
848 :     bvop_loop:
849 :    
850 :     if (pEnc->bframenum_dx50bvop != -1)
851 :     {
852 :    
853 :     SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
854 : syskin 903 SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
855 : edgomez 851
856 :     if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
857 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
858 :     }
859 :    
860 :     if (input_valid)
861 :     {
862 :     queue_image(pEnc, pFrame);
863 :     input_valid = 0;
864 :     }
865 :    
866 :     } else if (input_valid) {
867 :    
868 :     SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
869 :    
870 :     start_timer();
871 :     if (image_input
872 :     (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
873 :     pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
874 :     {
875 :     emms();
876 :     return XVID_ERR_FORMAT;
877 :     }
878 :     stop_conv_timer();
879 :    
880 : edgomez 853 if ((pFrame->general & XVID_CHROMAOPT)) {
881 : syskin 903 image_chroma_optimize(&pEnc->current->image,
882 : edgomez 853 pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
883 :     }
884 :    
885 : edgomez 867 /* queue input frame, and dequue next image */
886 : edgomez 851 if (pEnc->queue_size > 0)
887 :     {
888 :     image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
889 :     if (pEnc->queue_head != pEnc->queue_tail)
890 :     {
891 :     image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
892 :     }
893 :     pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
894 :     pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
895 :     }
896 :    
897 :     } else if (pEnc->queue_size > 0) {
898 : syskin 903
899 : edgomez 851 SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
900 :    
901 :     image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
902 :     pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
903 :     pEnc->queue_size--;
904 :    
905 :     } else {
906 :    
907 :     /* if nothing was encoded, write an 'ignore this frame' flag
908 :     to the bitstream */
909 :    
910 :     if (BitstreamPos(&bs) == 0) {
911 :    
912 :     DPRINTF(DPRINTF_DEBUG,"*** SKIP 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 :    
916 : edgomez 867 /* That disabled line of code was supposed to inform VirtualDub
917 :     * that the frame was a dummy delay frame - now disabled (thx god :-)
918 :     */
919 : syskin 903 //BitstreamPutBits(&bs, 0x7f, 8);
920 : edgomez 851 pFrame->intra = 5;
921 :    
922 :     if (pResult) {
923 :     /*
924 :     * We must decide what to put there because i know some apps
925 :     * are storing statistics about quantizers and just do
926 :     * stats[quant]++ or stats[quant-1]++
927 :     * transcode is one of these app with its 2pass module
928 :     */
929 :    
930 :     /*
931 :     * For now i prefer 31 than 0 that could lead to a segfault
932 :     * in transcode
933 :     */
934 :     pResult->quant = 31;
935 :    
936 :     pResult->hlength = 0;
937 :     pResult->kblks = 0;
938 :     pResult->mblks = 0;
939 :     pResult->ublks = 0;
940 : Isibaar 1004 pResult->iblks = 0;
941 :     pResult->qblks = 0;
942 : edgomez 851 }
943 :     } else {
944 :    
945 :     if (pResult) {
946 :     pResult->quant = pEnc->current->quant;
947 :     pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
948 :     pResult->kblks = pEnc->current->sStat.kblks;
949 :     pResult->mblks = pEnc->current->sStat.mblks;
950 :     pResult->ublks = pEnc->current->sStat.ublks;
951 : Isibaar 1004 pResult->iblks = pEnc->current->sStat.iblks;
952 :     pResult->qblks = pEnc->current->sStat.qblks;
953 : edgomez 851 }
954 :    
955 :     }
956 :    
957 :     pFrame->length = BitstreamLength(&bs);
958 :    
959 :     emms();
960 :    
961 :     return XVID_ERR_OK;
962 :     }
963 :    
964 :     pEnc->flush_bframes = 0;
965 :    
966 :     emms();
967 :    
968 : edgomez 867 /* only inc frame num, adapt quant, etc. if we havent seen it before */
969 : edgomez 851 if (pEnc->bframenum_dx50bvop < 0 )
970 :     {
971 :     mode = intra2coding_type(pFrame->intra);
972 :     if (pFrame->quant == 0)
973 :     pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
974 :     else
975 :     pEnc->current->quant = pFrame->quant;
976 :    
977 :     /* if (pEnc->current->quant < 1)
978 :     pEnc->current->quant = 1;
979 :    
980 :     if (pEnc->current->quant > 31)
981 :     pEnc->current->quant = 31;
982 :     */
983 :     pEnc->current->global_flags = pFrame->general;
984 :     pEnc->current->motion_flags = pFrame->motion;
985 :    
986 :     /* ToDo : dynamic fcode (in both directions) */
987 :     pEnc->current->fcode = pEnc->mbParam.m_fcode;
988 :     pEnc->current->bcode = pEnc->mbParam.m_fcode;
989 :    
990 :     inc_frame_num(pEnc);
991 :    
992 :     if (pFrame->general & XVID_EXTRASTATS)
993 :     { image_copy(&pEnc->sOriginal, &pEnc->current->image,
994 :     pEnc->mbParam.edged_width, pEnc->mbParam.height);
995 :     }
996 :    
997 :     emms();
998 :    
999 :     if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1000 : syskin 903 image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1001 : edgomez 851 "%i if:%i st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
1002 :     }
1003 :    
1004 :     /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1005 :     * Luminance masking
1006 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1007 :    
1008 :     if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1009 :     int *temp_dquants =
1010 :     (int *) xvid_malloc(pEnc->mbParam.mb_width *
1011 :     pEnc->mbParam.mb_height * sizeof(int),
1012 :     CACHE_LINE);
1013 :    
1014 :     pEnc->current->quant =
1015 :     adaptive_quantization(pEnc->current->image.y,
1016 :     pEnc->mbParam.edged_width, temp_dquants,
1017 :     pEnc->current->quant, pEnc->current->quant,
1018 :     2 * pEnc->current->quant,
1019 :     pEnc->mbParam.mb_width,
1020 :     pEnc->mbParam.mb_height);
1021 :    
1022 :     for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1023 :    
1024 :     #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1025 :    
1026 :     for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1027 :     MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1028 :    
1029 :     pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1030 :     }
1031 :     #undef OFFSET
1032 :     }
1033 :     xvid_free(temp_dquants);
1034 :     }
1035 :     }
1036 :    
1037 :     /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1038 :     * ivop/pvop/bvop selection
1039 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1040 :     pEnc->iFrameNum++;
1041 :    
1042 :     if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
1043 : syskin 903 (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
1044 :     pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) {
1045 : edgomez 851 mode = I_VOP;
1046 : syskin 903 } else {
1047 : edgomez 851 mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1048 :     &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1049 : syskin 903 (/*mode < 0*/1/*hack*/) ? pEnc->iFrameNum : 0,
1050 : syskin 979 bframes_count++, pFrame->bframe_threshold);
1051 : edgomez 851 }
1052 :    
1053 :     if (mode == I_VOP) {
1054 :     /*
1055 :     * This will be coded as an Intra Frame
1056 :     */
1057 :     if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1058 :     pEnc->mbParam.m_quarterpel = 1;
1059 :     else
1060 :     pEnc->mbParam.m_quarterpel = 0;
1061 :    
1062 :     if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1063 :    
1064 :     if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1065 :     if (pFrame->quant_intra_matrix != NULL)
1066 :     set_intra_matrix(pFrame->quant_intra_matrix);
1067 :     if (pFrame->quant_inter_matrix != NULL)
1068 :     set_inter_matrix(pFrame->quant_inter_matrix);
1069 :     }
1070 :    
1071 :    
1072 :     DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i",
1073 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1074 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1075 : syskin 903
1076 : edgomez 851 if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1077 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1078 :     }
1079 :    
1080 : edgomez 867 /* when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe */
1081 : edgomez 851
1082 :     if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1083 :    
1084 :     pEnc->bframenum_tail--;
1085 :     pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
1086 :    
1087 :     SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
1088 :     if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1089 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
1090 :     }
1091 : Isibaar 1004
1092 :     if (pEnc->mbParam.m_quarterpel)
1093 :     pEnc->current->global_flags |= XVID_QUARTERPEL;
1094 :     else
1095 :     pEnc->current->global_flags &= ~XVID_QUARTERPEL;
1096 :    
1097 :     FrameCodeP(pEnc, &bs, &bits, 1, 0, interpolate_backward);
1098 : edgomez 851 bframes_count = 0;
1099 :     pFrame->intra = 0;
1100 : Isibaar 1004 interpolate_forward = 0;
1101 :     interpolate_backward = 1;
1102 : edgomez 851
1103 :     } else {
1104 :    
1105 : Isibaar 1004 if (pEnc->mbParam.m_quarterpel)
1106 :     pEnc->current->global_flags |= XVID_QUARTERPEL;
1107 :     else
1108 :     pEnc->current->global_flags &= ~XVID_QUARTERPEL;
1109 :    
1110 : edgomez 851 FrameCodeI(pEnc, &bs, &bits);
1111 :     bframes_count = 0;
1112 : Isibaar 1004 interpolate_forward = 1;
1113 :     interpolate_backward = 1;
1114 : edgomez 851 pFrame->intra = 1;
1115 :    
1116 :     pEnc->bframenum_dx50bvop = -1;
1117 :     }
1118 :    
1119 :     pEnc->flush_bframes = 1;
1120 :    
1121 :     if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1122 :     BitstreamPadAlways(&bs);
1123 :     input_valid = 0;
1124 :     goto ipvop_loop;
1125 :     }
1126 :    
1127 :     /*
1128 :     * NB : sequences like "IIBB" decode fine with msfdam but,
1129 : syskin 903 * go screwy with divx 5.00
1130 : edgomez 851 */
1131 :     } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1132 :     /*
1133 :     * This will be coded as a Predicted Frame
1134 :     */
1135 :    
1136 :     DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i",
1137 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1138 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1139 : syskin 903
1140 : edgomez 851 if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1141 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1142 :     }
1143 :    
1144 : Isibaar 1004 if (pEnc->mbParam.m_quarterpel)
1145 :     pEnc->current->global_flags |= XVID_QUARTERPEL;
1146 :     else
1147 :     pEnc->current->global_flags &= ~XVID_QUARTERPEL;
1148 :    
1149 :     FrameCodeP(pEnc, &bs, &bits, 1, 0, interpolate_backward);
1150 : edgomez 851 bframes_count = 0;
1151 :     pFrame->intra = 0;
1152 : Isibaar 1004 interpolate_forward = 0;
1153 :     interpolate_backward = 1;
1154 : edgomez 851 pEnc->flush_bframes = 1;
1155 :    
1156 :     if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1157 :     BitstreamPadAlways(&bs);
1158 :     input_valid = 0;
1159 :     goto ipvop_loop;
1160 :     }
1161 :    
1162 :     } else { /* mode == B_VOP */
1163 :     /*
1164 :     * This will be coded as a Bidirectional Frame
1165 :     */
1166 :    
1167 :     if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1168 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1169 :     }
1170 :    
1171 :     if (pFrame->bquant < 1) {
1172 : syskin 903 pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1173 : edgomez 851 pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1174 :    
1175 :     } else {
1176 :     pEnc->current->quant = pFrame->bquant;
1177 :     }
1178 :    
1179 :     if (pEnc->current->quant < 1)
1180 :     pEnc->current->quant = 1;
1181 :     else if (pEnc->current->quant > 31)
1182 : syskin 903 pEnc->current->quant = 31;
1183 :    
1184 : edgomez 851 DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n",
1185 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1186 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1187 :    
1188 :     /* store frame into bframe buffer & swap ref back to current */
1189 :     SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1190 :     SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1191 :    
1192 :     pEnc->bframenum_tail++;
1193 :    
1194 : edgomez 867 /* bframe report by koepi */
1195 : edgomez 851 pFrame->intra = 2;
1196 :     pFrame->length = 0;
1197 :    
1198 :     input_valid = 0;
1199 :     goto bvop_loop;
1200 :     }
1201 :    
1202 :     BitstreamPadAlways(&bs);
1203 :     pFrame->length = BitstreamLength(&bs);
1204 :    
1205 :     if (pResult) {
1206 :     pResult->quant = pEnc->current->quant;
1207 :     pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1208 :     pResult->kblks = pEnc->current->sStat.kblks;
1209 :     pResult->mblks = pEnc->current->sStat.mblks;
1210 :     pResult->ublks = pEnc->current->sStat.ublks;
1211 : Isibaar 1004 pResult->iblks = pEnc->current->sStat.iblks;
1212 :     pResult->qblks = pEnc->current->sStat.qblks;
1213 : edgomez 851
1214 :     if (pFrame->general & XVID_EXTRASTATS)
1215 :     { pResult->sse_y =
1216 :     plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
1217 :     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1218 :     pEnc->mbParam.height);
1219 :    
1220 :     pResult->sse_u =
1221 :     plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1222 :     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1223 :     pEnc->mbParam.height/2);
1224 :    
1225 :     pResult->sse_v =
1226 :     plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1227 :     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1228 : syskin 903 pEnc->mbParam.height/2);
1229 : edgomez 851 }
1230 :     }
1231 :    
1232 :     emms();
1233 :    
1234 :     if (pFrame->quant == 0) {
1235 :     RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1236 :     pFrame->length, pFrame->intra);
1237 :     }
1238 :    
1239 :     stop_global_timer();
1240 :     write_timer();
1241 :    
1242 :     emms();
1243 :     return XVID_ERR_OK;
1244 :     }
1245 :    
1246 :    
1247 :    
1248 :     /*****************************************************************************
1249 : suxen_drol 229 * "original" IP frame encoder entry point
1250 :     *
1251 :     * Returned values :
1252 : syskin 903 * - XVID_ERR_OK - no errors
1253 :     * - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1254 :     * format
1255 : edgomez 194 ****************************************************************************/
1256 : suxen_drol 229
1257 : edgomez 194 int
1258 :     encoder_encode(Encoder * pEnc,
1259 : edgomez 195 XVID_ENC_FRAME * pFrame,
1260 :     XVID_ENC_STATS * pResult)
1261 : suxen_drol 152 {
1262 :     uint16_t x, y;
1263 :     Bitstream bs;
1264 :     uint32_t bits;
1265 : Isibaar 4 uint16_t write_vol_header = 0;
1266 : Isibaar 1004 unsigned int old_qpel;
1267 : edgomez 195
1268 : Isibaar 113 float psnr;
1269 : chl 870 char temp[128];
1270 : Isibaar 3
1271 :     start_global_timer();
1272 :    
1273 : edgomez 13 ENC_CHECK(pEnc);
1274 :     ENC_CHECK(pFrame);
1275 :     ENC_CHECK(pFrame->bitstream);
1276 :     ENC_CHECK(pFrame->image);
1277 : Isibaar 3
1278 : edgomez 851 SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1279 : suxen_drol 136
1280 :     pEnc->current->global_flags = pFrame->general;
1281 :     pEnc->current->motion_flags = pFrame->motion;
1282 : h 101 pEnc->mbParam.hint = &pFrame->hint;
1283 : Isibaar 3
1284 : edgomez 851 inc_frame_num(pEnc);
1285 :    
1286 :     /* disable alternate scan flag if interlacing is not enabled */
1287 :     if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1288 :     !(pEnc->current->global_flags & XVID_INTERLACING))
1289 :     {
1290 :     pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1291 :     }
1292 :    
1293 : Isibaar 3 start_timer();
1294 : edgomez 195 if (image_input
1295 :     (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1296 : edgomez 851 pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
1297 : Isibaar 3 return XVID_ERR_FORMAT;
1298 :     stop_conv_timer();
1299 :    
1300 : edgomez 853 if ((pFrame->general & XVID_CHROMAOPT)) {
1301 : syskin 903 image_chroma_optimize(&pEnc->current->image,
1302 : edgomez 853 pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1303 :     }
1304 :    
1305 : edgomez 851 if (pFrame->general & XVID_EXTRASTATS)
1306 :     { image_copy(&pEnc->sOriginal, &pEnc->current->image,
1307 :     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1308 :     }
1309 : Isibaar 113
1310 : edgomez 198 emms();
1311 : suxen_drol 136
1312 : edgomez 13 BitstreamInit(&bs, pFrame->bitstream, 0);
1313 : Isibaar 3
1314 : edgomez 195 if (pFrame->quant == 0) {
1315 :     pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
1316 :     } else {
1317 : suxen_drol 136 pEnc->current->quant = pFrame->quant;
1318 : Isibaar 3 }
1319 :    
1320 : Isibaar 1004 old_qpel = pEnc->mbParam.m_quarterpel;
1321 :    
1322 : edgomez 851 if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1323 :     pEnc->mbParam.m_quarterpel = 1;
1324 :     else
1325 :     pEnc->mbParam.m_quarterpel = 0;
1326 :    
1327 : edgomez 195 if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1328 :     int *temp_dquants =
1329 :     (int *) xvid_malloc(pEnc->mbParam.mb_width *
1330 :     pEnc->mbParam.mb_height * sizeof(int),
1331 :     CACHE_LINE);
1332 :    
1333 : edgomez 194 pEnc->current->quant =
1334 :     adaptive_quantization(pEnc->current->image.y,
1335 : edgomez 195 pEnc->mbParam.edged_width, temp_dquants,
1336 :     pEnc->current->quant, pEnc->current->quant,
1337 :     2 * pEnc->current->quant,
1338 :     pEnc->mbParam.mb_width,
1339 :     pEnc->mbParam.mb_height);
1340 : edgomez 194
1341 : edgomez 195 for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1342 : edgomez 194
1343 : edgomez 195 #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1344 : edgomez 194
1345 : edgomez 195 for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1346 : edgomez 194
1347 : edgomez 195
1348 :     MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1349 :    
1350 :     pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1351 : Isibaar 3 }
1352 : edgomez 194
1353 : edgomez 195 #undef OFFSET
1354 : edgomez 194 }
1355 :    
1356 : Isibaar 41 xvid_free(temp_dquants);
1357 : Isibaar 3 }
1358 :    
1359 : edgomez 195 if (pEnc->current->global_flags & XVID_H263QUANT) {
1360 :     if (pEnc->mbParam.m_quant_type != H263_QUANT)
1361 : Isibaar 20 write_vol_header = 1;
1362 : suxen_drol 136 pEnc->mbParam.m_quant_type = H263_QUANT;
1363 : edgomez 195 } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1364 : edgomez 194 int matrix1_changed, matrix2_changed;
1365 : Isibaar 3
1366 : edgomez 194 matrix1_changed = matrix2_changed = 0;
1367 : edgomez 78
1368 : edgomez 195 if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1369 : Isibaar 20 write_vol_header = 1;
1370 : edgomez 195
1371 : suxen_drol 136 pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1372 : edgomez 195
1373 : suxen_drol 136 if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1374 : edgomez 195 if (pFrame->quant_intra_matrix != NULL)
1375 :     matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1376 :     if (pFrame->quant_inter_matrix != NULL)
1377 :     matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1378 :     } else {
1379 : edgomez 194 matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1380 :     matrix2_changed = set_inter_matrix(get_default_inter_matrix());
1381 : Isibaar 20 }
1382 : edgomez 195 if (write_vol_header == 0)
1383 : edgomez 194 write_vol_header = matrix1_changed | matrix2_changed;
1384 : Isibaar 4 }
1385 : Isibaar 3
1386 : edgomez 195 if (pFrame->intra < 0) {
1387 : edgomez 851 if ((pEnc->iFrameNum == -1)
1388 :     || ((pEnc->mbParam.iMaxKeyInterval > 0)
1389 :     && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
1390 : edgomez 195 pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1391 :     } else {
1392 : Isibaar 1004 if (old_qpel != pEnc->mbParam.m_quarterpel)
1393 :     pEnc->mbParam.m_quarterpel = old_qpel;
1394 :     pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header, 1);
1395 : edgomez 194 }
1396 : edgomez 195 } else {
1397 :     if (pFrame->intra == 1) {
1398 :     pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1399 :     } else {
1400 : Isibaar 1004 if (old_qpel != pEnc->mbParam.m_quarterpel)
1401 :     pEnc->mbParam.m_quarterpel = old_qpel;
1402 :     pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header, 1);
1403 : edgomez 194 }
1404 :    
1405 : edgomez 13 }
1406 : Isibaar 3
1407 : edgomez 867 /* Relic from OpenDivX - now disabled
1408 :     BitstreamPutBits(&bs, 0xFFFF, 16);
1409 :     BitstreamPutBits(&bs, 0xFFFF, 16);
1410 :     */
1411 :    
1412 : edgomez 851 BitstreamPadAlways(&bs);
1413 : edgomez 13 pFrame->length = BitstreamLength(&bs);
1414 : h 29
1415 : edgomez 195 if (pResult) {
1416 : suxen_drol 136 pResult->quant = pEnc->current->quant;
1417 : edgomez 851 pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1418 :     pResult->kblks = pEnc->current->sStat.kblks;
1419 :     pResult->mblks = pEnc->current->sStat.mblks;
1420 :     pResult->ublks = pEnc->current->sStat.ublks;
1421 : Isibaar 1004 pResult->iblks = pEnc->current->sStat.iblks;
1422 :     pResult->qblks = pEnc->current->sStat.qblks;
1423 : edgomez 13 }
1424 : edgomez 195
1425 : edgomez 198 emms();
1426 : Isibaar 41
1427 : edgomez 195 if (pFrame->quant == 0) {
1428 : edgomez 851 RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1429 : edgomez 195 pFrame->length, pFrame->intra);
1430 : Isibaar 3 }
1431 : edgomez 851 if (pFrame->general & XVID_EXTRASTATS)
1432 :     {
1433 :     psnr =
1434 :     image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1435 :     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1436 :     pEnc->mbParam.height);
1437 : syskin 903
1438 : edgomez 851 snprintf(temp, 127, "PSNR: %f\n", psnr);
1439 :     }
1440 : Isibaar 113
1441 : Isibaar 3 pEnc->iFrameNum++;
1442 : edgomez 195
1443 : Isibaar 3 stop_global_timer();
1444 :     write_timer();
1445 :    
1446 :     return XVID_ERR_OK;
1447 :     }
1448 :    
1449 :    
1450 : edgomez 195 static __inline void
1451 :     CodeIntraMB(Encoder * pEnc,
1452 :     MACROBLOCK * pMB)
1453 :     {
1454 : Isibaar 3
1455 :     pMB->mode = MODE_INTRA;
1456 :    
1457 : suxen_drol 136 /* zero mv statistics */
1458 :     pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1459 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1460 :     pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1461 :     pMB->sad16 = 0;
1462 :    
1463 :     if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1464 : edgomez 195 if (pMB->dquant != NO_CHANGE) {
1465 : Isibaar 3 pMB->mode = MODE_INTRA_Q;
1466 : suxen_drol 136 pEnc->current->quant += DQtab[pMB->dquant];
1467 : edgomez 195
1468 :     if (pEnc->current->quant > 31)
1469 :     pEnc->current->quant = 31;
1470 :     if (pEnc->current->quant < 1)
1471 :     pEnc->current->quant = 1;
1472 : Isibaar 3 }
1473 :     }
1474 :    
1475 : suxen_drol 136 pMB->quant = pEnc->current->quant;
1476 : Isibaar 3 }
1477 :    
1478 :    
1479 : h 101 #define FCODEBITS 3
1480 :     #define MODEBITS 5
1481 :    
1482 : edgomez 195 void
1483 :     HintedMESet(Encoder * pEnc,
1484 :     int *intra)
1485 : h 101 {
1486 : edgomez 195 HINTINFO *hint;
1487 : h 101 Bitstream bs;
1488 :     int length, high;
1489 :     uint32_t x, y;
1490 :    
1491 :     hint = pEnc->mbParam.hint;
1492 :    
1493 : edgomez 195 if (hint->rawhints) {
1494 : h 101 *intra = hint->mvhint.intra;
1495 : edgomez 195 } else {
1496 : h 101 BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1497 :     *intra = BitstreamGetBit(&bs);
1498 :     }
1499 :    
1500 : edgomez 195 if (*intra) {
1501 : h 101 return;
1502 :     }
1503 :    
1504 : edgomez 875 pEnc->current->fcode = (hint->rawhints) ?
1505 :     (uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);
1506 : h 101
1507 : edgomez 195 length = pEnc->current->fcode + 5;
1508 :     high = 1 << (length - 1);
1509 : h 101
1510 : edgomez 195 for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1511 :     for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1512 :     MACROBLOCK *pMB =
1513 :     &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1514 :     MVBLOCKHINT *bhint =
1515 :     &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1516 : chl 284 VECTOR pred;
1517 : h 101 VECTOR tmp;
1518 :     int vec;
1519 :    
1520 : edgomez 875 pMB->mode = (hint->rawhints) ?
1521 :     (uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS);
1522 : h 101
1523 : h 128 pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1524 :     pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1525 :    
1526 : edgomez 195 if (pMB->mode == MODE_INTER) {
1527 : edgomez 875 tmp.x = (hint->rawhints) ?
1528 :     bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length);
1529 :     tmp.y = (hint->rawhints) ?
1530 :     bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length);
1531 : edgomez 195 tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1532 :     tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1533 : h 101
1534 : chl 284 pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
1535 : h 101
1536 : edgomez 195 for (vec = 0; vec < 4; ++vec) {
1537 :     pMB->mvs[vec].x = tmp.x;
1538 :     pMB->mvs[vec].y = tmp.y;
1539 : chl 284 pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1540 :     pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1541 : h 101 }
1542 : edgomez 195 } else if (pMB->mode == MODE_INTER4V) {
1543 :     for (vec = 0; vec < 4; ++vec) {
1544 : edgomez 875 tmp.x = (hint->rawhints) ?
1545 :     bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length);
1546 :     tmp.y = (hint->rawhints) ?
1547 :     bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length);
1548 : edgomez 195 tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1549 :     tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1550 : h 101
1551 : chl 284 pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
1552 : h 101
1553 : edgomez 195 pMB->mvs[vec].x = tmp.x;
1554 :     pMB->mvs[vec].y = tmp.y;
1555 : chl 284 pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1556 :     pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1557 : h 101 }
1558 : edgomez 867 } else /* intra / stuffing / not_coded */
1559 : h 101 {
1560 : edgomez 195 for (vec = 0; vec < 4; ++vec) {
1561 :     pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1562 : h 101 }
1563 :     }
1564 : h 128
1565 : suxen_drol 136 if (pMB->mode == MODE_INTER4V &&
1566 : edgomez 195 (pEnc->current->global_flags & XVID_LUMIMASKING)
1567 :     && pMB->dquant != NO_CHANGE) {
1568 : h 128 pMB->mode = MODE_INTRA;
1569 :    
1570 : edgomez 195 for (vec = 0; vec < 4; ++vec) {
1571 : h 128 pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1572 :     }
1573 :     }
1574 : h 101 }
1575 :     }
1576 :     }
1577 :    
1578 :    
1579 : edgomez 195 void
1580 :     HintedMEGet(Encoder * pEnc,
1581 :     int intra)
1582 : h 101 {
1583 : edgomez 195 HINTINFO *hint;
1584 : h 101 Bitstream bs;
1585 :     uint32_t x, y;
1586 :     int length, high;
1587 :    
1588 :     hint = pEnc->mbParam.hint;
1589 :    
1590 : edgomez 195 if (hint->rawhints) {
1591 : h 101 hint->mvhint.intra = intra;
1592 : edgomez 195 } else {
1593 : h 101 BitstreamInit(&bs, hint->hintstream, 0);
1594 :     BitstreamPutBit(&bs, intra);
1595 :     }
1596 :    
1597 : edgomez 195 if (intra) {
1598 :     if (!hint->rawhints) {
1599 : edgomez 851 BitstreamPadAlways(&bs);
1600 : h 101 hint->hintlength = BitstreamLength(&bs);
1601 :     }
1602 :     return;
1603 :     }
1604 :    
1605 : edgomez 195 length = pEnc->current->fcode + 5;
1606 :     high = 1 << (length - 1);
1607 : h 101
1608 : edgomez 195 if (hint->rawhints) {
1609 : suxen_drol 136 hint->mvhint.fcode = pEnc->current->fcode;
1610 : edgomez 195 } else {
1611 : suxen_drol 136 BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1612 : h 101 }
1613 :    
1614 : edgomez 195 for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1615 :     for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1616 :     MACROBLOCK *pMB =
1617 :     &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1618 :     MVBLOCKHINT *bhint =
1619 :     &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1620 : h 101 VECTOR tmp;
1621 :    
1622 : edgomez 195 if (hint->rawhints) {
1623 : h 101 bhint->mode = pMB->mode;
1624 : edgomez 195 } else {
1625 : h 101 BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1626 :     }
1627 :    
1628 : edgomez 195 if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
1629 :     tmp.x = pMB->mvs[0].x;
1630 :     tmp.y = pMB->mvs[0].y;
1631 :     tmp.x += (tmp.x < 0) ? high * 2 : 0;
1632 :     tmp.y += (tmp.y < 0) ? high * 2 : 0;
1633 : h 101
1634 : edgomez 195 if (hint->rawhints) {
1635 : h 101 bhint->mvs[0].x = tmp.x;
1636 :     bhint->mvs[0].y = tmp.y;
1637 : edgomez 195 } else {
1638 : h 101 BitstreamPutBits(&bs, tmp.x, length);
1639 :     BitstreamPutBits(&bs, tmp.y, length);
1640 :     }
1641 : edgomez 195 } else if (pMB->mode == MODE_INTER4V) {
1642 : h 101 int vec;
1643 :    
1644 : edgomez 195 for (vec = 0; vec < 4; ++vec) {
1645 :     tmp.x = pMB->mvs[vec].x;
1646 :     tmp.y = pMB->mvs[vec].y;
1647 :     tmp.x += (tmp.x < 0) ? high * 2 : 0;
1648 :     tmp.y += (tmp.y < 0) ? high * 2 : 0;
1649 : h 101
1650 : edgomez 195 if (hint->rawhints) {
1651 : h 101 bhint->mvs[vec].x = tmp.x;
1652 :     bhint->mvs[vec].y = tmp.y;
1653 : edgomez 195 } else {
1654 : h 101 BitstreamPutBits(&bs, tmp.x, length);
1655 :     BitstreamPutBits(&bs, tmp.y, length);
1656 :     }
1657 :     }
1658 :     }
1659 :     }
1660 :     }
1661 :    
1662 : edgomez 195 if (!hint->rawhints) {
1663 : h 101 BitstreamPad(&bs);
1664 :     hint->hintlength = BitstreamLength(&bs);
1665 :     }
1666 :     }
1667 :    
1668 :    
1669 : edgomez 195 static int
1670 :     FrameCodeI(Encoder * pEnc,
1671 :     Bitstream * bs,
1672 :     uint32_t * pBits)
1673 : h 104 {
1674 : edgomez 851 int mb_width = pEnc->mbParam.mb_width;
1675 :     int mb_height = pEnc->mbParam.mb_height;
1676 : h 104
1677 :     DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1678 : edgomez 195 DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1679 : h 104
1680 :     uint16_t x, y;
1681 :    
1682 : edgomez 851 if ((pEnc->current->global_flags & XVID_REDUCED))
1683 :     {
1684 :     mb_width = (pEnc->mbParam.width + 31) / 32;
1685 :     mb_height = (pEnc->mbParam.height + 31) / 32;
1686 :    
1687 :     /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1688 :     /* XXX: setedges is overkill */
1689 :     start_timer();
1690 : syskin 903 image_setedges(&pEnc->current->image,
1691 : edgomez 851 pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1692 :     pEnc->mbParam.width, pEnc->mbParam.height);
1693 :     stop_edges_timer();
1694 :     }
1695 : h 104 pEnc->iFrameNum = 0;
1696 : suxen_drol 136 pEnc->mbParam.m_rounding_type = 1;
1697 :     pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1698 : edgomez 851 pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel;
1699 : suxen_drol 136 pEnc->current->coding_type = I_VOP;
1700 : h 104
1701 : suxen_drol 136 BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1702 : edgomez 403
1703 : edgomez 851 set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1704 :    
1705 :     BitstreamPadAlways(bs);
1706 : suxen_drol 229 BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1707 : h 104
1708 :     *pBits = BitstreamPos(bs);
1709 :    
1710 : edgomez 851 pEnc->current->sStat.iTextBits = 0;
1711 :     pEnc->current->sStat.kblks = mb_width * mb_height;
1712 :     pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1713 : Isibaar 1004 pEnc->current->sStat.iblks = pEnc->current->sStat.qblks = 0;
1714 : h 104
1715 : edgomez 851 for (y = 0; y < mb_height; y++)
1716 :     for (x = 0; x < mb_width; x++) {
1717 : edgomez 195 MACROBLOCK *pMB =
1718 :     &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1719 : h 104
1720 :     CodeIntraMB(pEnc, pMB);
1721 :    
1722 : edgomez 195 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1723 :     dct_codes, qcoeff);
1724 : h 104
1725 :     start_timer();
1726 : suxen_drol 136 MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1727 : h 104 stop_prediction_timer();
1728 :    
1729 :     start_timer();
1730 : chl 353 if (pEnc->current->global_flags & XVID_GREYSCALE)
1731 :     { pMB->cbp &= 0x3C; /* keep only bits 5-2 */
1732 :     qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */
1733 :     qcoeff[5*64+0]=0;
1734 :     }
1735 : edgomez 851 MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1736 : h 104 stop_coding_timer();
1737 :     }
1738 :    
1739 : edgomez 851 if ((pEnc->current->global_flags & XVID_REDUCED))
1740 :     {
1741 : syskin 903 image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1742 : edgomez 851 pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1743 :     16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1744 :     }
1745 : h 104 emms();
1746 :    
1747 :     *pBits = BitstreamPos(bs) - *pBits;
1748 : edgomez 851 pEnc->fMvPrevSigma = -1;
1749 : suxen_drol 136 pEnc->mbParam.m_fcode = 2;
1750 : h 104
1751 : edgomez 195 if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1752 : h 104 HintedMEGet(pEnc, 1);
1753 :     }
1754 :    
1755 : edgomez 867 return 1; /* intra */
1756 : h 104 }
1757 :    
1758 :    
1759 : Isibaar 3 #define INTRA_THRESHOLD 0.5
1760 : edgomez 851 #define BFRAME_SKIP_THRESHHOLD 30
1761 : Isibaar 3
1762 : edgomez 851
1763 :     /* FrameCodeP also handles S(GMC)-VOPs */
1764 : edgomez 195 static int
1765 :     FrameCodeP(Encoder * pEnc,
1766 :     Bitstream * bs,
1767 :     uint32_t * pBits,
1768 :     bool force_inter,
1769 : Isibaar 1004 bool vol_header,
1770 :     int interpolate)
1771 : Isibaar 3 {
1772 : edgomez 13 float fSigma;
1773 : Isibaar 42
1774 : edgomez 78 DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1775 : edgomez 195 DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1776 : edgomez 78
1777 : edgomez 851 int mb_width = pEnc->mbParam.mb_width;
1778 :     int mb_height = pEnc->mbParam.mb_height;
1779 :    
1780 : Isibaar 3 int iLimit;
1781 : edgomez 851 int x, y, k;
1782 : edgomez 13 int iSearchRange;
1783 : edgomez 851 int bIntra, skip_possible;
1784 : Isibaar 1004 IMAGE *refh, *refv, *refhv;
1785 : syskin 903
1786 : edgomez 145 /* IMAGE *pCurrent = &pEnc->current->image; */
1787 : suxen_drol 136 IMAGE *pRef = &pEnc->reference->image;
1788 : Isibaar 3
1789 : edgomez 851 if ((pEnc->current->global_flags & XVID_REDUCED))
1790 :     {
1791 :     mb_width = (pEnc->mbParam.width + 31) / 32;
1792 :     mb_height = (pEnc->mbParam.height + 31) / 32;
1793 :     }
1794 :    
1795 : suxen_drol 136 pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1796 :     pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1797 : edgomez 851 pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel;
1798 : suxen_drol 136 pEnc->current->fcode = pEnc->mbParam.m_fcode;
1799 : Isibaar 3
1800 :     if (!force_inter)
1801 : edgomez 851 iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD);
1802 : edgomez 13 else
1803 : edgomez 851 iLimit = mb_width * mb_height + 1;
1804 : Isibaar 3
1805 : Isibaar 1004 if((interpolate) || (pEnc->current->rounding_type != 0)) {
1806 : Isibaar 3 start_timer();
1807 : Isibaar 1004 image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1808 :     pEnc->mbParam.width, pEnc->mbParam.height);
1809 :     stop_edges_timer();
1810 :    
1811 :     if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1812 :     start_timer();
1813 :     image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1814 :     &pEnc->vInterHV, pEnc->mbParam.edged_width,
1815 :     pEnc->mbParam.edged_height,
1816 :     pEnc->mbParam.m_quarterpel,
1817 :     pEnc->current->rounding_type);
1818 :     stop_inter_timer();
1819 :     }
1820 :    
1821 :     refh = &pEnc->vInterH;
1822 :     refv = &pEnc->vInterV;
1823 :     refhv = &pEnc->vInterHV;
1824 : Isibaar 3 }
1825 : Isibaar 1004 else {
1826 :     refh = &pEnc->f_refh;
1827 :     refv = &pEnc->f_refv;
1828 :     refhv = &pEnc->f_refhv;
1829 :     }
1830 : Isibaar 3
1831 : edgomez 851 pEnc->current->coding_type = P_VOP;
1832 : syskin 903
1833 : Isibaar 3 start_timer();
1834 : edgomez 851 if (pEnc->current->global_flags & XVID_HINTEDME_SET)
1835 : h 101 HintedMESet(pEnc, &bIntra);
1836 : edgomez 851 else
1837 : suxen_drol 295 bIntra =
1838 :     MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1839 : Isibaar 1004 refh, refv, refhv,
1840 : syskin 903 iLimit);
1841 : edgomez 851
1842 : Isibaar 3 stop_motion_timer();
1843 :    
1844 : edgomez 851 if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1845 :    
1846 : syskin 903 if ( ( pEnc->current->global_flags & XVID_GMC )
1847 : edgomez 851 && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1848 :     {
1849 :     pEnc->current->coding_type = S_VOP;
1850 :    
1851 : syskin 903 generate_GMCparameters( 2, 16, &pEnc->current->warp,
1852 :     pEnc->mbParam.width, pEnc->mbParam.height,
1853 : edgomez 851 &pEnc->current->gmc_data);
1854 :    
1855 : syskin 903 generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1856 : edgomez 851 pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1857 : syskin 903 pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1858 :     pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1859 : edgomez 851 pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1860 :    
1861 : h 101 }
1862 : Isibaar 3
1863 : edgomez 851 set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1864 : edgomez 195 if (vol_header)
1865 : syskin 903 { BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1866 : edgomez 851 BitstreamPadAlways(bs);
1867 :     }
1868 : Isibaar 3
1869 : suxen_drol 229 BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1870 : Isibaar 3
1871 : edgomez 13 *pBits = BitstreamPos(bs);
1872 : Isibaar 3
1873 : syskin 903 pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1874 : Isibaar 1004 pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1875 :     pEnc->current->sStat.iblks = pEnc->current->sStat.qblks = 0;
1876 : Isibaar 3
1877 : edgomez 851
1878 :     for (y = 0; y < mb_height; y++) {
1879 :     for (x = 0; x < mb_width; x++) {
1880 : edgomez 195 MACROBLOCK *pMB =
1881 :     &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1882 : Isibaar 3
1883 : edgomez 851 /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1884 :     /* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */
1885 :    
1886 : edgomez 13 bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1887 : Isibaar 3
1888 : edgomez 851 if (bIntra) {
1889 : Isibaar 3 CodeIntraMB(pEnc, pMB);
1890 : edgomez 195 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1891 :     dct_codes, qcoeff);
1892 : chl 419
1893 :     start_timer();
1894 :     MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1895 :     stop_prediction_timer();
1896 : edgomez 851
1897 :     pEnc->current->sStat.kblks++;
1898 :    
1899 :     MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1900 :     stop_coding_timer();
1901 :     continue;
1902 : Isibaar 3 }
1903 : syskin 903
1904 : edgomez 851 if (pEnc->current->coding_type == S_VOP) {
1905 : Isibaar 3
1906 : edgomez 851 int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1907 : syskin 903 pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1908 : edgomez 851 pEnc->mbParam.edged_width, 65536);
1909 : syskin 903
1910 : edgomez 851 if (pEnc->current->motion_flags & PMV_CHROMA16) {
1911 :     iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1912 :     pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1913 : Isibaar 3
1914 : edgomez 851 iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1915 :     pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1916 :     }
1917 :    
1918 :     if (iSAD <= pMB->sad16) { /* mode decision GMC */
1919 :    
1920 :     if (pEnc->mbParam.m_quarterpel)
1921 :     pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1922 :     else
1923 :     pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1924 :    
1925 :     pMB->mode = MODE_INTER;
1926 :     pMB->mcsel = 1;
1927 :     pMB->sad16 = iSAD;
1928 :     } else {
1929 :     pMB->mcsel = 0;
1930 :     }
1931 :     } else {
1932 :     pMB->mcsel = 0; /* just a precaution */
1933 :     }
1934 :    
1935 : Isibaar 3 start_timer();
1936 : edgomez 851 MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1937 : Isibaar 1004 refh, refv,
1938 :     refhv, &pEnc->vGMC,
1939 : edgomez 851 &pEnc->current->image,
1940 :     dct_codes, pEnc->mbParam.width,
1941 :     pEnc->mbParam.height,
1942 :     pEnc->mbParam.edged_width,
1943 :     pEnc->mbParam.m_quarterpel,
1944 :     (pEnc->current->global_flags & XVID_REDUCED),
1945 :     pEnc->current->rounding_type);
1946 : chl 347
1947 : edgomez 851 stop_comp_timer();
1948 :    
1949 :     if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1950 :     if (pMB->dquant != NO_CHANGE) {
1951 :     pMB->mode = MODE_INTER_Q;
1952 :     pEnc->current->quant += DQtab[pMB->dquant];
1953 :     if (pEnc->current->quant > 31)
1954 :     pEnc->current->quant = 31;
1955 :     else if (pEnc->current->quant < 1)
1956 :     pEnc->current->quant = 1;
1957 :     }
1958 :     }
1959 :     pMB->quant = pEnc->current->quant;
1960 :    
1961 :     pMB->field_pred = 0;
1962 :    
1963 :     if (pMB->mode != MODE_NOT_CODED)
1964 :     { pMB->cbp =
1965 :     MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1966 :     dct_codes, qcoeff);
1967 :     }
1968 :    
1969 :     if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1970 :     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1971 :     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1972 :     pEnc->current->sStat.mblks++;
1973 :     } else {
1974 :     pEnc->current->sStat.ublks++;
1975 :     }
1976 : Isibaar 1004
1977 :     if(pEnc->mbParam.m_quarterpel) {
1978 :     for (k = 0; k < ((pMB->mode == MODE_INTER4V) ? 4 : 1); k++) {
1979 :     if (((pMB->qmvs[k].x % 4) != 0) || ((pMB->qmvs[k].y % 4) != 0))
1980 :     pEnc->current->sStat.iblks++;
1981 :    
1982 :     if (((pMB->qmvs[k].x % 4) & 1) || ((pMB->qmvs[k].y % 4) & 1))
1983 :     pEnc->current->sStat.qblks++;
1984 :     }
1985 :     }
1986 :    
1987 : edgomez 851 start_timer();
1988 :    
1989 : chl 347 /* Finished processing the MB, now check if to CODE or SKIP */
1990 :    
1991 : edgomez 851 skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1992 :     (pMB->dquant == NO_CHANGE);
1993 : syskin 903
1994 : edgomez 851 if (pEnc->current->coding_type == S_VOP)
1995 :     skip_possible &= (pMB->mcsel == 1);
1996 :     else if (pEnc->current->coding_type == P_VOP) {
1997 :     if (pEnc->mbParam.m_quarterpel)
1998 :     skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1999 : syskin 903 else
2000 : edgomez 851 skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
2001 :     }
2002 : chl 347
2003 : edgomez 851 if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
2004 : chl 347
2005 : edgomez 851 /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
2006 :    
2007 :     if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */
2008 :     {
2009 : syskin 903 int bSkip = 1;
2010 :    
2011 : edgomez 851 for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
2012 :     {
2013 :     int iSAD;
2014 :     iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
2015 :     pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
2016 :     pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
2017 :     if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
2018 : syskin 903 { bSkip = 0;
2019 : edgomez 851 break;
2020 :     }
2021 :     }
2022 : syskin 903
2023 : edgomez 851 if (!bSkip) { /* no SKIP, but trivial block */
2024 :     if(pEnc->mbParam.m_quarterpel) {
2025 :     VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
2026 : syskin 903 pMB->pmvs[0].x = - predMV.x;
2027 :     pMB->pmvs[0].y = - predMV.y;
2028 : edgomez 851 }
2029 :     else {
2030 :     VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
2031 : syskin 903 pMB->pmvs[0].x = - predMV.x;
2032 :     pMB->pmvs[0].y = - predMV.y;
2033 : edgomez 851 }
2034 :     pMB->mode = MODE_INTER;
2035 :     pMB->cbp = 0;
2036 :     MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
2037 :     stop_coding_timer();
2038 :    
2039 :     continue; /* next MB */
2040 :     }
2041 :     }
2042 :     /* do SKIP */
2043 :    
2044 :     pMB->mode = MODE_NOT_CODED;
2045 :     MBSkip(bs);
2046 :     stop_coding_timer();
2047 :     continue; /* next MB */
2048 : edgomez 403 }
2049 : edgomez 851 /* ordinary case: normal coded INTER/INTER4V block */
2050 :    
2051 :     if (pEnc->current->global_flags & XVID_GREYSCALE)
2052 :     { pMB->cbp &= 0x3C; /* keep only bits 5-2 */
2053 :     qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */
2054 :     qcoeff[5*64+0]=0;
2055 :     }
2056 :    
2057 :     if(pEnc->mbParam.m_quarterpel) {
2058 :     VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
2059 : syskin 903 pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
2060 :     pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
2061 : edgomez 851 DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
2062 :     } else {
2063 :     VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
2064 : syskin 903 pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
2065 :     pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
2066 : edgomez 851 DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
2067 :     }
2068 :    
2069 :    
2070 :     if (pMB->mode == MODE_INTER4V)
2071 :     { int k;
2072 :     for (k=1;k<4;k++)
2073 :     {
2074 :     if(pEnc->mbParam.m_quarterpel) {
2075 :     VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
2076 : syskin 903 pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
2077 :     pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
2078 : edgomez 851 DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
2079 :     } else {
2080 :     VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
2081 : syskin 903 pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
2082 :     pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
2083 : edgomez 851 DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
2084 :     }
2085 :    
2086 : chl 353 }
2087 : chl 347 }
2088 : syskin 903
2089 : edgomez 851 MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
2090 :     stop_coding_timer();
2091 : chl 347
2092 : Isibaar 3 }
2093 :     }
2094 :    
2095 : edgomez 851 if ((pEnc->current->global_flags & XVID_REDUCED))
2096 :     {
2097 : syskin 903 image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
2098 : edgomez 851 pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
2099 :     16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
2100 :     }
2101 :    
2102 : Isibaar 3 emms();
2103 :    
2104 : edgomez 195 if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
2105 : h 101 HintedMEGet(pEnc, 0);
2106 :     }
2107 :    
2108 : edgomez 851 if (pEnc->current->sStat.iMvCount == 0)
2109 :     pEnc->current->sStat.iMvCount = 1;
2110 : Isibaar 3
2111 : edgomez 851 fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
2112 : Isibaar 3
2113 : suxen_drol 136 iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2114 : Isibaar 3
2115 : edgomez 195 if ((fSigma > iSearchRange / 3)
2116 : edgomez 867 && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) /* maximum search range 128 */
2117 : edgomez 13 {
2118 : suxen_drol 136 pEnc->mbParam.m_fcode++;
2119 : Isibaar 3 iSearchRange *= 2;
2120 : edgomez 195 } else if ((fSigma < iSearchRange / 6)
2121 : edgomez 851 && (pEnc->fMvPrevSigma >= 0)
2122 :     && (pEnc->fMvPrevSigma < iSearchRange / 6)
2123 : edgomez 867 && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) /* minimum search range 16 */
2124 : edgomez 13 {
2125 : suxen_drol 136 pEnc->mbParam.m_fcode--;
2126 : Isibaar 3 iSearchRange /= 2;
2127 : edgomez 13 }
2128 : Isibaar 3
2129 : edgomez 851 pEnc->fMvPrevSigma = fSigma;
2130 : edgomez 195
2131 : edgomez 851 /* frame drop code */
2132 :     DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
2133 :     if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
2134 :     (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
2135 :     {
2136 :     pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
2137 :     pEnc->current->sStat.ublks = mb_width * mb_height;
2138 :    
2139 :     BitstreamReset(bs);
2140 :    
2141 :     set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
2142 :     BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
2143 :    
2144 : edgomez 867 /* copy reference frame details into the current frame */
2145 : edgomez 851 pEnc->current->quant = pEnc->reference->quant;
2146 :     pEnc->current->motion_flags = pEnc->reference->motion_flags;
2147 :     pEnc->current->rounding_type = pEnc->reference->rounding_type;
2148 :     pEnc->current->quarterpel = pEnc->reference->quarterpel;
2149 :     pEnc->current->fcode = pEnc->reference->fcode;
2150 :     pEnc->current->bcode = pEnc->reference->bcode;
2151 :     image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
2152 :     memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2153 :     }
2154 :    
2155 :     /* XXX: debug
2156 :     {
2157 :     char s[100];
2158 :     sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2159 : syskin 903 image_dump_yuvpgm(&pEnc->current->image,
2160 : edgomez 851 pEnc->mbParam.edged_width,
2161 :     pEnc->mbParam.width, pEnc->mbParam.height, s);
2162 : syskin 903
2163 : edgomez 851 sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2164 : syskin 903 image_dump_yuvpgm(&pEnc->reference->image,
2165 : edgomez 851 pEnc->mbParam.edged_width,
2166 :     pEnc->mbParam.width, pEnc->mbParam.height, s);
2167 : syskin 903 }
2168 : edgomez 851 */
2169 :    
2170 :    
2171 : Isibaar 3 *pBits = BitstreamPos(bs) - *pBits;
2172 :    
2173 : edgomez 867 return 0; /* inter */
2174 : edgomez 851 }
2175 : suxen_drol 118
2176 : edgomez 851
2177 :     static void
2178 :     FrameCodeB(Encoder * pEnc,
2179 :     FRAMEINFO * frame,
2180 :     Bitstream * bs,
2181 : Isibaar 1004 uint32_t * pBits,
2182 :     int interpolate_forward,
2183 :     int interpolate_backward)
2184 : edgomez 851 {
2185 :     DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2186 :     DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2187 :     uint32_t x, y;
2188 :    
2189 :     IMAGE *f_ref = &pEnc->reference->image;
2190 :     IMAGE *b_ref = &pEnc->current->image;
2191 :    
2192 :     #ifdef BFRAMES_DEC_DEBUG
2193 :     FILE *fp;
2194 :     static char first=0;
2195 :     #define BFRAME_DEBUG if (!first && fp){ \
2196 :     fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \
2197 :     }
2198 :    
2199 :     pEnc->current->global_flags &= ~XVID_REDUCED; /* reduced resoltion not yet supported */
2200 :    
2201 :     if (!first){
2202 :     fp=fopen("C:\\XVIDDBGE.TXT","w");
2203 :     }
2204 :     #endif
2205 :    
2206 :     frame->quarterpel = pEnc->mbParam.m_quarterpel;
2207 : syskin 903
2208 : Isibaar 1004 if ((interpolate_forward) || ((pEnc->mbParam.m_rounding_type != 1) && interpolate_backward)) {
2209 :    
2210 :     /* forward */
2211 :     image_setedges(f_ref, pEnc->mbParam.edged_width,
2212 :     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2213 :     pEnc->mbParam.height);
2214 :     start_timer();
2215 :     image_interpolate(f_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2216 :     pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2217 :     pEnc->mbParam.m_quarterpel, 0);
2218 :     stop_inter_timer();
2219 :     }
2220 : edgomez 851
2221 : Isibaar 1004 if (interpolate_backward) {
2222 : edgomez 851
2223 : Isibaar 1004 /* backward */
2224 :     image_setedges(b_ref, pEnc->mbParam.edged_width,
2225 :     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2226 :     pEnc->mbParam.height);
2227 :     start_timer();
2228 :     image_interpolate(b_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2229 :     pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2230 :     pEnc->mbParam.m_quarterpel, 0);
2231 :     stop_inter_timer();
2232 :     }
2233 :    
2234 : edgomez 851 start_timer();
2235 :    
2236 : syskin 903 MotionEstimationBVOP(&pEnc->mbParam, frame,
2237 : edgomez 867 ((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */
2238 :     ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */
2239 : Isibaar 1004 pEnc->reference->mbs, f_ref, &pEnc->vInterH,
2240 :     &pEnc->vInterV, &pEnc->vInterHV,
2241 :     pEnc->current, b_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv);
2242 : edgomez 851
2243 :     stop_motion_timer();
2244 : edgomez 867 /*
2245 :     if (test_quant_type(&pEnc->mbParam, pEnc->current)) {
2246 :     BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
2247 :     }
2248 :     */
2249 : edgomez 851
2250 :     frame->coding_type = B_VOP;
2251 :    
2252 :     set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2253 :     BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
2254 :    
2255 :     *pBits = BitstreamPos(bs);
2256 :    
2257 :     frame->sStat.iTextBits = 0;
2258 :     frame->sStat.iMvSum = 0;
2259 :     frame->sStat.iMvCount = 0;
2260 :     frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2261 : Isibaar 1004 frame->sStat.iblks = frame->sStat.qblks = 0;
2262 : edgomez 851
2263 :    
2264 :     for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2265 :     for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2266 :     MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2267 :     int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
2268 :    
2269 : edgomez 867 /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2270 : edgomez 851 if (mb->mode == MODE_NOT_CODED) {
2271 : edgomez 867 /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */
2272 : edgomez 851 continue;
2273 :     }
2274 :    
2275 :     if (mb->mode != MODE_DIRECT_NONE_MV) {
2276 :     MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2277 : Isibaar 1004 f_ref, &pEnc->vInterH, &pEnc->vInterV,
2278 :     &pEnc->vInterHV, b_ref, &pEnc->f_refh,
2279 :     &pEnc->f_refv, &pEnc->f_refhv,
2280 : edgomez 851 dct_codes);
2281 :    
2282 :     if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
2283 :     mb->quant = frame->quant;
2284 : syskin 903
2285 : edgomez 851 mb->cbp =
2286 :     MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
2287 :    
2288 :     if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
2289 :     && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
2290 : edgomez 867 mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2291 : edgomez 851 }
2292 :     }
2293 :    
2294 :     #ifdef BFRAMES_DEC_DEBUG
2295 :     BFRAME_DEBUG
2296 :     #endif
2297 :     start_timer();
2298 :     MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2299 :     &frame->sStat, direction);
2300 :     stop_coding_timer();
2301 :     }
2302 :     }
2303 :    
2304 :     emms();
2305 :    
2306 : edgomez 867 /* TODO: dynamic fcode/bcode ??? */
2307 : edgomez 851
2308 :     *pBits = BitstreamPos(bs) - *pBits;
2309 :    
2310 :     #ifdef BFRAMES_DEC_DEBUG
2311 :     if (!first){
2312 :     first=1;
2313 :     if (fp)
2314 :     fclose(fp);
2315 :     }
2316 :     #endif
2317 : suxen_drol 118 }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4