[cvs] / xvidcore / src / encoder.c Repository:
ViewVC logotype

Annotation of /xvidcore/src/encoder.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.99 - (view) (download)

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

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