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

Annotation of /xvidcore/src/encoder.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.120.2.1 - (view) (download)

1 : edgomez 1.40 /*****************************************************************************
2 : edgomez 1.29 *
3 :     * XVID MPEG-4 VIDEO CODEC
4 : edgomez 1.102 * - Encoder main module -
5 : edgomez 1.29 *
6 : edgomez 1.102 * Copyright(C) 2002 Michael Militzer <isibaar@xvid.org>
7 :     * 2002-2003 Peter Ross <pross@xvid.org>
8 :     * 2002 Daniel Smith <danielsmith@astroboymail.com>
9 : edgomez 1.77 *
10 : edgomez 1.102 * This program is free software ; you can redistribute it and/or modify
11 : edgomez 1.91 * it under the terms of the GNU General Public License as published by
12 : edgomez 1.102 * the Free Software Foundation ; either version 2 of the License, or
13 : edgomez 1.29 * (at your option) any later version.
14 :     *
15 :     * This program is distributed in the hope that it will be useful,
16 : edgomez 1.102 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
17 : edgomez 1.29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 :     * GNU General Public License for more details.
19 :     *
20 :     * You should have received a copy of the GNU General Public License
21 : edgomez 1.102 * along with this program ; if not, write to the Free Software
22 : edgomez 1.29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 : edgomez 1.79 *
24 : Isibaar 1.120.2.1 * $Id: encoder.c,v 1.120 2005/11/22 10:23:01 suxen_drol Exp $
25 : edgomez 1.29 *
26 : edgomez 1.40 ****************************************************************************/
27 : chl 1.64
28 : Isibaar 1.1 #include <stdlib.h>
29 :     #include <stdio.h>
30 :     #include <math.h>
31 : edgomez 1.42 #include <string.h>
32 : Isibaar 1.1
33 :     #include "encoder.h"
34 :     #include "prediction/mbprediction.h"
35 :     #include "global.h"
36 :     #include "utils/timer.h"
37 :     #include "image/image.h"
38 : edgomez 1.91 #include "image/font.h"
39 :     #include "motion/sad.h"
40 : suxen_drol 1.32 #include "motion/motion.h"
41 : edgomez 1.102 #include "motion/gmc.h"
42 :    
43 : Isibaar 1.1 #include "bitstream/cbp.h"
44 :     #include "utils/mbfunctions.h"
45 :     #include "bitstream/bitstream.h"
46 :     #include "bitstream/mbcoding.h"
47 :     #include "utils/emms.h"
48 :     #include "bitstream/mbcoding.h"
49 : Isibaar 1.2 #include "quant/quant_matrix.h"
50 : Isibaar 1.7 #include "utils/mem_align.h"
51 : Isibaar 1.1
52 : edgomez 1.40 /*****************************************************************************
53 :     * Local function prototypes
54 :     ****************************************************************************/
55 :    
56 : edgomez 1.39 static int FrameCodeI(Encoder * pEnc,
57 : edgomez 1.102 Bitstream * bs);
58 : edgomez 1.39
59 :     static int FrameCodeP(Encoder * pEnc,
60 : syskin 1.104 Bitstream * bs);
61 : Isibaar 1.1
62 : edgomez 1.91 static void FrameCodeB(Encoder * pEnc,
63 :     FRAMEINFO * frame,
64 : edgomez 1.102 Bitstream * bs);
65 : Isibaar 1.1
66 :    
67 : edgomez 1.40 /*****************************************************************************
68 :     * Encoder creation
69 :     *
70 :     * This function creates an Encoder instance, it allocates all necessary
71 : edgomez 1.91 * image buffers (reference, current and bframes) and initialize the internal
72 :     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
73 : edgomez 1.40 *
74 :     * The code seems to be very long but is very basic, mainly memory allocation
75 :     * and cleaning code.
76 :     *
77 :     * Returned values :
78 : edgomez 1.102 * - 0 - no errors
79 : syskin 1.96 * - XVID_ERR_MEMORY - the libc could not allocate memory, the function
80 :     * cleans the structure before exiting.
81 :     * pParam->handle is also set to NULL.
82 : edgomez 1.40 *
83 :     ****************************************************************************/
84 :    
85 : edgomez 1.102 /*
86 :     * Simplify the "fincr/fbase" fraction
87 :     */
88 : Skal 1.118 static int
89 :     gcd(int a, int b)
90 :     {
91 :     int r ;
92 :    
93 :     if (b > a) {
94 :     r = a;
95 :     a = b;
96 :     b = r;
97 :     }
98 :    
99 :     while ((r = a % b)) {
100 :     a = b;
101 :     b = r;
102 :     }
103 :     return b;
104 :     }
105 :    
106 : edgomez 1.102 static void
107 :     simplify_time(int *inc, int *base)
108 : Isibaar 1.1 {
109 : edgomez 1.102 /* common factor */
110 : Skal 1.118 const int s = gcd(*inc, *base);
111 :     *inc /= s;
112 :     *base /= s;
113 : Isibaar 1.1
114 : edgomez 1.106 if (*base > 65535 || *inc > 65535) {
115 :     int *biggest;
116 :     int *other;
117 : edgomez 1.107 float div;
118 : edgomez 1.108
119 : edgomez 1.106 if (*base > *inc) {
120 :     biggest = base;
121 :     other = inc;
122 :     } else {
123 :     biggest = inc;
124 :     other = base;
125 :     }
126 :    
127 : edgomez 1.107 div = ((float)*biggest)/((float)65535);
128 : Skal 1.118 *biggest = (unsigned int)(((float)*biggest)/div);
129 :     *other = (unsigned int)(((float)*other)/div);
130 : Isibaar 1.1 }
131 : edgomez 1.102 }
132 : Isibaar 1.1
133 : edgomez 1.39
134 : edgomez 1.102 int
135 :     enc_create(xvid_enc_create_t * create)
136 :     {
137 :     Encoder *pEnc;
138 :     int n;
139 : edgomez 1.39
140 : edgomez 1.102 if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */
141 :     return XVID_ERR_VERSION;
142 : Isibaar 1.1
143 : edgomez 1.102 if (create->width%2 || create->height%2)
144 :     return XVID_ERR_FAIL;
145 : Isibaar 1.1
146 : syskin 1.103 if (create->width<=0 || create->height<=0)
147 :     return XVID_ERR_FAIL;
148 :    
149 : edgomez 1.102 /* allocate encoder struct */
150 : edgomez 1.41
151 : edgomez 1.39 pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
152 :     if (pEnc == NULL)
153 : Isibaar 1.1 return XVID_ERR_MEMORY;
154 : edgomez 1.42 memset(pEnc, 0, sizeof(Encoder));
155 :    
156 : edgomez 1.102 pEnc->mbParam.profile = create->profile;
157 : Isibaar 1.1
158 : edgomez 1.102 /* global flags */
159 :     pEnc->mbParam.global_flags = create->global;
160 : suxen_drol 1.117 if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED))
161 :     pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA;
162 : Isibaar 1.1
163 : edgomez 1.102 /* width, height */
164 :     pEnc->mbParam.width = create->width;
165 :     pEnc->mbParam.height = create->height;
166 : Isibaar 1.1 pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;
167 :     pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;
168 : edgomez 1.41 pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
169 :     pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
170 : Isibaar 1.1
171 : edgomez 1.102 /* framerate */
172 :     pEnc->mbParam.fincr = MAX(create->fincr, 0);
173 :     pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
174 :     if (pEnc->mbParam.fincr>0)
175 : Skal 1.118 simplify_time((int*)&pEnc->mbParam.fincr, (int*)&pEnc->mbParam.fbase);
176 : Isibaar 1.119
177 : edgomez 1.102 /* zones */
178 :     if(create->num_zones > 0) {
179 :     pEnc->num_zones = create->num_zones;
180 :     pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE);
181 :     if (pEnc->zones == NULL)
182 :     goto xvid_err_memory0;
183 :     memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones);
184 :     } else {
185 :     pEnc->num_zones = 0;
186 :     pEnc->zones = NULL;
187 :     }
188 :    
189 :     /* plugins */
190 :     if(create->num_plugins > 0) {
191 :     pEnc->num_plugins = create->num_plugins;
192 :     pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);
193 :     if (pEnc->plugins == NULL)
194 :     goto xvid_err_memory0;
195 :     } else {
196 :     pEnc->num_plugins = 0;
197 :     pEnc->plugins = NULL;
198 :     }
199 :    
200 :     for (n=0; n<pEnc->num_plugins;n++) {
201 :     xvid_plg_create_t pcreate;
202 :     xvid_plg_info_t pinfo;
203 :    
204 :     memset(&pinfo, 0, sizeof(xvid_plg_info_t));
205 :     pinfo.version = XVID_VERSION;
206 : suxen_drol 1.120 if (create->plugins[n].func(NULL, XVID_PLG_INFO, &pinfo, NULL) >= 0) {
207 : edgomez 1.102 pEnc->mbParam.plugin_flags |= pinfo.flags;
208 :     }
209 :    
210 :     memset(&pcreate, 0, sizeof(xvid_plg_create_t));
211 :     pcreate.version = XVID_VERSION;
212 :     pcreate.num_zones = pEnc->num_zones;
213 :     pcreate.zones = pEnc->zones;
214 :     pcreate.width = pEnc->mbParam.width;
215 :     pcreate.height = pEnc->mbParam.height;
216 :     pcreate.mb_width = pEnc->mbParam.mb_width;
217 :     pcreate.mb_height = pEnc->mbParam.mb_height;
218 :     pcreate.fincr = pEnc->mbParam.fincr;
219 :     pcreate.fbase = pEnc->mbParam.fbase;
220 :     pcreate.param = create->plugins[n].param;
221 :    
222 :     pEnc->plugins[n].func = NULL; /* disable plugins that fail */
223 : suxen_drol 1.120 if (create->plugins[n].func(NULL, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
224 : edgomez 1.102 pEnc->plugins[n].func = create->plugins[n].func;
225 :     }
226 :     }
227 :    
228 :     if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) ||
229 :     (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
230 :     pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */
231 :     }
232 : Isibaar 1.43
233 : edgomez 1.102 /* temp dquants */
234 :     if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
235 :     pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
236 :     pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
237 :     if (pEnc->temp_dquants==NULL)
238 :     goto xvid_err_memory1a;
239 :     }
240 : suxen_drol 1.32
241 : edgomez 1.102 /* bframes */
242 :     pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
243 :     pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
244 :     pEnc->mbParam.bquant_offset = create->bquant_offset;
245 : Isibaar 1.1
246 : edgomez 1.102 /* min/max quant */
247 :     for (n=0; n<3; n++) {
248 :     pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2;
249 :     pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31;
250 :     }
251 : Isibaar 1.1
252 : edgomez 1.102 /* frame drop ratio */
253 :     pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
254 : Isibaar 1.1
255 : edgomez 1.102 /* max keyframe interval */
256 :     pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval;
257 : Isibaar 1.1
258 : edgomez 1.102 /* allocate working frame-image memory */
259 : h 1.16
260 : edgomez 1.39 pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
261 :     pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
262 :    
263 : edgomez 1.41 if (pEnc->current == NULL || pEnc->reference == NULL)
264 : edgomez 1.39 goto xvid_err_memory1;
265 : suxen_drol 1.27
266 : edgomez 1.102 /* allocate macroblock memory */
267 : h 1.16
268 : edgomez 1.41 pEnc->current->mbs =
269 :     xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
270 :     pEnc->mbParam.mb_height, CACHE_LINE);
271 :     pEnc->reference->mbs =
272 :     xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
273 :     pEnc->mbParam.mb_height, CACHE_LINE);
274 : Isibaar 1.1
275 : edgomez 1.39 if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
276 :     goto xvid_err_memory2;
277 : suxen_drol 1.27
278 : edgomez 1.102 /* allocate quant matrix memory */
279 :    
280 :     pEnc->mbParam.mpeg_quant_matrices =
281 :     xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
282 :    
283 :     if (pEnc->mbParam.mpeg_quant_matrices == NULL)
284 :     goto xvid_err_memory2a;
285 :    
286 :     /* allocate interpolation image memory */
287 : suxen_drol 1.27
288 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
289 : edgomez 1.91 image_null(&pEnc->sOriginal);
290 : edgomez 1.102 image_null(&pEnc->sOriginal2);
291 :     }
292 : edgomez 1.91
293 :     image_null(&pEnc->f_refh);
294 :     image_null(&pEnc->f_refv);
295 :     image_null(&pEnc->f_refhv);
296 :    
297 : suxen_drol 1.27 image_null(&pEnc->current->image);
298 :     image_null(&pEnc->reference->image);
299 :     image_null(&pEnc->vInterH);
300 :     image_null(&pEnc->vInterV);
301 :     image_null(&pEnc->vInterHV);
302 : edgomez 1.39
303 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
304 :     if (image_create
305 : edgomez 1.91 (&pEnc->sOriginal, pEnc->mbParam.edged_width,
306 :     pEnc->mbParam.edged_height) < 0)
307 :     goto xvid_err_memory3;
308 : edgomez 1.102
309 :     if (image_create
310 :     (&pEnc->sOriginal2, pEnc->mbParam.edged_width,
311 :     pEnc->mbParam.edged_height) < 0)
312 :     goto xvid_err_memory3;
313 : edgomez 1.91 }
314 :    
315 :     if (image_create
316 :     (&pEnc->f_refh, pEnc->mbParam.edged_width,
317 :     pEnc->mbParam.edged_height) < 0)
318 :     goto xvid_err_memory3;
319 :     if (image_create
320 :     (&pEnc->f_refv, pEnc->mbParam.edged_width,
321 :     pEnc->mbParam.edged_height) < 0)
322 :     goto xvid_err_memory3;
323 : edgomez 1.41 if (image_create
324 : edgomez 1.91 (&pEnc->f_refhv, pEnc->mbParam.edged_width,
325 : edgomez 1.41 pEnc->mbParam.edged_height) < 0)
326 : edgomez 1.39 goto xvid_err_memory3;
327 : edgomez 1.91
328 : edgomez 1.41 if (image_create
329 :     (&pEnc->current->image, pEnc->mbParam.edged_width,
330 :     pEnc->mbParam.edged_height) < 0)
331 : edgomez 1.39 goto xvid_err_memory3;
332 : edgomez 1.41 if (image_create
333 :     (&pEnc->reference->image, pEnc->mbParam.edged_width,
334 :     pEnc->mbParam.edged_height) < 0)
335 : edgomez 1.39 goto xvid_err_memory3;
336 : edgomez 1.41 if (image_create
337 :     (&pEnc->vInterH, pEnc->mbParam.edged_width,
338 :     pEnc->mbParam.edged_height) < 0)
339 : edgomez 1.39 goto xvid_err_memory3;
340 : edgomez 1.41 if (image_create
341 :     (&pEnc->vInterV, pEnc->mbParam.edged_width,
342 :     pEnc->mbParam.edged_height) < 0)
343 : edgomez 1.39 goto xvid_err_memory3;
344 : edgomez 1.41 if (image_create
345 :     (&pEnc->vInterHV, pEnc->mbParam.edged_width,
346 :     pEnc->mbParam.edged_height) < 0)
347 : edgomez 1.39 goto xvid_err_memory3;
348 : edgomez 1.91
349 :     /* Create full bitplane for GMC, this might be wasteful */
350 :     if (image_create
351 :     (&pEnc->vGMC, pEnc->mbParam.edged_width,
352 :     pEnc->mbParam.edged_height) < 0)
353 :     goto xvid_err_memory3;
354 :    
355 : edgomez 1.102 /* init bframe image buffers */
356 : edgomez 1.91
357 : edgomez 1.102 pEnc->bframenum_head = 0;
358 :     pEnc->bframenum_tail = 0;
359 :     pEnc->flush_bframes = 0;
360 :     pEnc->closed_bframenum = -1;
361 : edgomez 1.91
362 :     /* B Frames specific init */
363 :     pEnc->bframes = NULL;
364 :    
365 :     if (pEnc->mbParam.max_bframes > 0) {
366 :    
367 :     pEnc->bframes =
368 :     xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
369 :     CACHE_LINE);
370 :    
371 :     if (pEnc->bframes == NULL)
372 :     goto xvid_err_memory3;
373 :    
374 :     for (n = 0; n < pEnc->mbParam.max_bframes; n++)
375 :     pEnc->bframes[n] = NULL;
376 :    
377 :    
378 :     for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
379 :     pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
380 :    
381 :     if (pEnc->bframes[n] == NULL)
382 :     goto xvid_err_memory4;
383 :    
384 :     pEnc->bframes[n]->mbs =
385 :     xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
386 :     pEnc->mbParam.mb_height, CACHE_LINE);
387 :    
388 :     if (pEnc->bframes[n]->mbs == NULL)
389 :     goto xvid_err_memory4;
390 :    
391 :     image_null(&pEnc->bframes[n]->image);
392 :    
393 :     if (image_create
394 :     (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
395 :     pEnc->mbParam.edged_height) < 0)
396 :     goto xvid_err_memory4;
397 :    
398 :     }
399 :     }
400 :    
401 : edgomez 1.102 /* init incoming frame queue */
402 :     pEnc->queue_head = 0;
403 :     pEnc->queue_tail = 0;
404 :     pEnc->queue_size = 0;
405 : edgomez 1.91
406 : edgomez 1.102 pEnc->queue =
407 :     xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO),
408 :     CACHE_LINE);
409 : edgomez 1.91
410 : edgomez 1.102 if (pEnc->queue == NULL)
411 :     goto xvid_err_memory4;
412 : edgomez 1.91
413 : edgomez 1.102 for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
414 :     image_null(&pEnc->queue[n].image);
415 : edgomez 1.91
416 :    
417 : edgomez 1.102 for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
418 :     if (image_create
419 :     (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
420 :     pEnc->mbParam.edged_height) < 0)
421 :     goto xvid_err_memory5;
422 : edgomez 1.91 }
423 :    
424 : edgomez 1.102 /* timestamp stuff */
425 : edgomez 1.91
426 :     pEnc->mbParam.m_stamp = 0;
427 :     pEnc->m_framenum = 0;
428 :     pEnc->current->stamp = 0;
429 :     pEnc->reference->stamp = 0;
430 : edgomez 1.39
431 : edgomez 1.102 /* other stuff */
432 :    
433 :     pEnc->iFrameNum = 0;
434 :     pEnc->fMvPrevSigma = -1;
435 : Isibaar 1.1
436 : edgomez 1.102 create->handle = (void *) pEnc;
437 : Isibaar 1.1
438 : Isibaar 1.6 init_timer();
439 : edgomez 1.102 init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices);
440 : Isibaar 1.1
441 : edgomez 1.102 return 0; /* ok */
442 : edgomez 1.39
443 :     /*
444 :     * We handle all XVID_ERR_MEMORY here, this makes the code lighter
445 :     */
446 :    
447 : edgomez 1.91 xvid_err_memory5:
448 :    
449 : edgomez 1.102 for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
450 :     image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width,
451 : edgomez 1.91 pEnc->mbParam.edged_height);
452 :     }
453 : edgomez 1.102
454 :     xvid_free(pEnc->queue);
455 : edgomez 1.91
456 :     xvid_err_memory4:
457 :    
458 :     if (pEnc->mbParam.max_bframes > 0) {
459 : edgomez 1.102 int i;
460 : edgomez 1.91
461 :     for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
462 :    
463 :     if (pEnc->bframes[i] == NULL)
464 :     continue;
465 :    
466 :     image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
467 :     pEnc->mbParam.edged_height);
468 :     xvid_free(pEnc->bframes[i]->mbs);
469 :     xvid_free(pEnc->bframes[i]);
470 : syskin 1.96 }
471 : edgomez 1.91
472 :     xvid_free(pEnc->bframes);
473 :     }
474 :    
475 : edgomez 1.41 xvid_err_memory3:
476 : edgomez 1.91
477 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
478 :     image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
479 :     pEnc->mbParam.edged_height);
480 :     image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
481 : edgomez 1.91 pEnc->mbParam.edged_height);
482 :     }
483 :    
484 :     image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
485 :     pEnc->mbParam.edged_height);
486 :     image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
487 :     pEnc->mbParam.edged_height);
488 :     image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
489 : edgomez 1.41 pEnc->mbParam.edged_height);
490 : edgomez 1.39
491 : edgomez 1.41 image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
492 :     pEnc->mbParam.edged_height);
493 :     image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
494 :     pEnc->mbParam.edged_height);
495 :     image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
496 :     pEnc->mbParam.edged_height);
497 :     image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
498 :     pEnc->mbParam.edged_height);
499 :     image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
500 :     pEnc->mbParam.edged_height);
501 : edgomez 1.91
502 :     /* destroy GMC image */
503 :     image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
504 :     pEnc->mbParam.edged_height);
505 :    
506 : edgomez 1.102 xvid_err_memory2a:
507 :     xvid_free(pEnc->mbParam.mpeg_quant_matrices);
508 : edgomez 1.39
509 : edgomez 1.41 xvid_err_memory2:
510 : edgomez 1.39 xvid_free(pEnc->current->mbs);
511 :     xvid_free(pEnc->reference->mbs);
512 :    
513 : edgomez 1.41 xvid_err_memory1:
514 : edgomez 1.39 xvid_free(pEnc->current);
515 :     xvid_free(pEnc->reference);
516 : edgomez 1.102
517 :     xvid_err_memory1a:
518 :     if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
519 :     xvid_free(pEnc->temp_dquants);
520 :     }
521 :    
522 :     xvid_err_memory0:
523 :     for (n=0; n<pEnc->num_plugins;n++) {
524 :     if (pEnc->plugins[n].func) {
525 : suxen_drol 1.120 pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, NULL, NULL);
526 : edgomez 1.102 }
527 :     }
528 :     xvid_free(pEnc->plugins);
529 :    
530 :     xvid_free(pEnc->zones);
531 :    
532 : edgomez 1.39 xvid_free(pEnc);
533 :    
534 : edgomez 1.102 create->handle = NULL;
535 : edgomez 1.40
536 : edgomez 1.39 return XVID_ERR_MEMORY;
537 : Isibaar 1.1 }
538 :    
539 : edgomez 1.40 /*****************************************************************************
540 :     * Encoder destruction
541 :     *
542 :     * This function destroy the entire encoder structure created by a previous
543 : edgomez 1.102 * successful enc_create call.
544 : edgomez 1.40 *
545 :     * Returned values (for now only one returned value) :
546 : edgomez 1.102 * - 0 - no errors
547 : edgomez 1.40 *
548 :     ****************************************************************************/
549 :    
550 :     int
551 : edgomez 1.102 enc_destroy(Encoder * pEnc)
552 : Isibaar 1.1 {
553 : edgomez 1.91 int i;
554 : syskin 1.96
555 : edgomez 1.91 /* B Frames specific */
556 : edgomez 1.102 for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
557 :     image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
558 : edgomez 1.91 pEnc->mbParam.edged_height);
559 :     }
560 :    
561 : edgomez 1.102 xvid_free(pEnc->queue);
562 :    
563 : edgomez 1.91 if (pEnc->mbParam.max_bframes > 0) {
564 :    
565 :     for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
566 :    
567 :     if (pEnc->bframes[i] == NULL)
568 :     continue;
569 :    
570 :     image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
571 :     pEnc->mbParam.edged_height);
572 :     xvid_free(pEnc->bframes[i]->mbs);
573 :     xvid_free(pEnc->bframes[i]);
574 :     }
575 :    
576 :     xvid_free(pEnc->bframes);
577 : syskin 1.96
578 : edgomez 1.91 }
579 :    
580 : edgomez 1.39 /* All images, reference, current etc ... */
581 : edgomez 1.91
582 : edgomez 1.41 image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
583 :     pEnc->mbParam.edged_height);
584 :     image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
585 :     pEnc->mbParam.edged_height);
586 :     image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
587 :     pEnc->mbParam.edged_height);
588 :     image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
589 :     pEnc->mbParam.edged_height);
590 :     image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
591 :     pEnc->mbParam.edged_height);
592 : edgomez 1.91 image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
593 : edgomez 1.41 pEnc->mbParam.edged_height);
594 : edgomez 1.91 image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
595 :     pEnc->mbParam.edged_height);
596 :     image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
597 :     pEnc->mbParam.edged_height);
598 : edgomez 1.102 image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
599 :     pEnc->mbParam.edged_height);
600 : edgomez 1.91
601 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
602 :     image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
603 :     pEnc->mbParam.edged_height);
604 :     image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
605 : edgomez 1.91 pEnc->mbParam.edged_height);
606 :     }
607 : edgomez 1.39
608 :     /* Encoder structure */
609 : edgomez 1.91
610 : suxen_drol 1.27 xvid_free(pEnc->current->mbs);
611 :     xvid_free(pEnc->current);
612 :    
613 :     xvid_free(pEnc->reference->mbs);
614 :     xvid_free(pEnc->reference);
615 :    
616 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
617 :     xvid_free(pEnc->temp_dquants);
618 : edgomez 1.91 }
619 :    
620 :    
621 : edgomez 1.102 if (pEnc->num_plugins>0) {
622 :     xvid_plg_destroy_t pdestroy;
623 :     memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t));
624 : edgomez 1.91
625 : edgomez 1.102 pdestroy.version = XVID_VERSION;
626 :     pdestroy.num_frames = pEnc->m_framenum;
627 : edgomez 1.91
628 : edgomez 1.102 for (i=0; i<pEnc->num_plugins;i++) {
629 :     if (pEnc->plugins[i].func) {
630 : suxen_drol 1.120 pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, NULL);
631 : edgomez 1.102 }
632 :     }
633 :     xvid_free(pEnc->plugins);
634 : edgomez 1.92 }
635 :    
636 : edgomez 1.102 xvid_free(pEnc->mbParam.mpeg_quant_matrices);
637 : edgomez 1.91
638 : edgomez 1.102 if (pEnc->num_plugins>0)
639 :     xvid_free(pEnc->zones);
640 : edgomez 1.91
641 : edgomez 1.102 xvid_free(pEnc);
642 : edgomez 1.91
643 : edgomez 1.102 return 0; /* ok */
644 : edgomez 1.91 }
645 :    
646 :    
647 : edgomez 1.102 /*
648 :     call the plugins
649 :     */
650 : edgomez 1.91
651 : edgomez 1.102 static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
652 :     int opt, int * type, int * quant, xvid_enc_stats_t * stats)
653 : suxen_drol 1.44 {
654 : edgomez 1.102 unsigned int i, j;
655 :     xvid_plg_data_t data;
656 : edgomez 1.91
657 : edgomez 1.102 /* set data struct */
658 : suxen_drol 1.44
659 : edgomez 1.102 memset(&data, 0, sizeof(xvid_plg_data_t));
660 :     data.version = XVID_VERSION;
661 : edgomez 1.91
662 : edgomez 1.102 /* find zone */
663 :     for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ;
664 :     data.zone = i>0 ? &pEnc->zones[i-1] : NULL;
665 :    
666 :     data.width = pEnc->mbParam.width;
667 :     data.height = pEnc->mbParam.height;
668 :     data.mb_width = pEnc->mbParam.mb_width;
669 :     data.mb_height = pEnc->mbParam.mb_height;
670 :     data.fincr = frame->fincr;
671 :     data.fbase = pEnc->mbParam.fbase;
672 :     data.bquant_ratio = pEnc->mbParam.bquant_ratio;
673 :     data.bquant_offset = pEnc->mbParam.bquant_offset;
674 :    
675 :     for (i=0; i<3; i++) {
676 :     data.min_quant[i] = pEnc->mbParam.min_quant[i];
677 :     data.max_quant[i] = pEnc->mbParam.max_quant[i];
678 :     }
679 :    
680 :     data.reference.csp = XVID_CSP_PLANAR;
681 :     data.reference.plane[0] = pEnc->reference->image.y;
682 :     data.reference.plane[1] = pEnc->reference->image.u;
683 :     data.reference.plane[2] = pEnc->reference->image.v;
684 :     data.reference.stride[0] = pEnc->mbParam.edged_width;
685 :     data.reference.stride[1] = pEnc->mbParam.edged_width/2;
686 :     data.reference.stride[2] = pEnc->mbParam.edged_width/2;
687 :    
688 :     data.current.csp = XVID_CSP_PLANAR;
689 :     data.current.plane[0] = frame->image.y;
690 :     data.current.plane[1] = frame->image.u;
691 :     data.current.plane[2] = frame->image.v;
692 :     data.current.stride[0] = pEnc->mbParam.edged_width;
693 :     data.current.stride[1] = pEnc->mbParam.edged_width/2;
694 :     data.current.stride[2] = pEnc->mbParam.edged_width/2;
695 :    
696 :     data.frame_num = frame->frame_num;
697 :    
698 :     if (opt == XVID_PLG_BEFORE) {
699 :     data.type = *type;
700 :     data.quant = *quant;
701 :    
702 :     data.vol_flags = frame->vol_flags;
703 :     data.vop_flags = frame->vop_flags;
704 :     data.motion_flags = frame->motion_flags;
705 :    
706 :     } else if (opt == XVID_PLG_FRAME) {
707 :     data.type = coding2type(frame->coding_type);
708 :     data.quant = frame->quant;
709 :    
710 :     if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
711 :     data.dquant = pEnc->temp_dquants;
712 :     data.dquant_stride = pEnc->mbParam.mb_width;
713 :     memset(data.dquant, 0, data.mb_width*data.mb_height);
714 :     }
715 :    
716 :     } else { /* XVID_PLG_AFTER */
717 :     if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
718 :     data.original.csp = XVID_CSP_PLANAR;
719 :     data.original.plane[0] = original->y;
720 :     data.original.plane[1] = original->u;
721 :     data.original.plane[2] = original->v;
722 :     data.original.stride[0] = pEnc->mbParam.edged_width;
723 :     data.original.stride[1] = pEnc->mbParam.edged_width/2;
724 :     data.original.stride[2] = pEnc->mbParam.edged_width/2;
725 :     }
726 : edgomez 1.91
727 : edgomez 1.102 if ((frame->vol_flags & XVID_VOL_EXTRASTATS) ||
728 :     (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
729 : edgomez 1.91
730 : edgomez 1.102 data.sse_y =
731 :     plane_sse( original->y, frame->image.y,
732 :     pEnc->mbParam.edged_width, pEnc->mbParam.width,
733 :     pEnc->mbParam.height);
734 : edgomez 1.91
735 : edgomez 1.102 data.sse_u =
736 :     plane_sse( original->u, frame->image.u,
737 :     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
738 :     pEnc->mbParam.height/2);
739 : edgomez 1.91
740 : edgomez 1.102 data.sse_v =
741 :     plane_sse( original->v, frame->image.v,
742 :     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
743 :     pEnc->mbParam.height/2);
744 :     }
745 : edgomez 1.91
746 : edgomez 1.102 data.type = coding2type(frame->coding_type);
747 :     data.quant = frame->quant;
748 : edgomez 1.91
749 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
750 :     data.dquant = pEnc->temp_dquants;
751 :     data.dquant_stride = pEnc->mbParam.mb_width;
752 :    
753 :     for (j=0; j<pEnc->mbParam.mb_height; j++)
754 :     for (i=0; i<pEnc->mbParam.mb_width; i++) {
755 :     data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;
756 :     }
757 :     }
758 : edgomez 1.91
759 : edgomez 1.102 data.vol_flags = frame->vol_flags;
760 :     data.vop_flags = frame->vop_flags;
761 :     data.motion_flags = frame->motion_flags;
762 :    
763 :     data.length = frame->length;
764 :     data.kblks = frame->sStat.kblks;
765 :     data.mblks = frame->sStat.mblks;
766 :     data.ublks = frame->sStat.ublks;
767 :    
768 :     /* New code */
769 :     data.stats.type = coding2type(frame->coding_type);
770 :     data.stats.quant = frame->quant;
771 :     data.stats.vol_flags = frame->vol_flags;
772 :     data.stats.vop_flags = frame->vop_flags;
773 :     data.stats.length = frame->length;
774 :     data.stats.hlength = frame->length - (frame->sStat.iTextBits / 8);
775 :     data.stats.kblks = frame->sStat.kblks;
776 :     data.stats.mblks = frame->sStat.mblks;
777 :     data.stats.ublks = frame->sStat.ublks;
778 :     data.stats.sse_y = data.sse_y;
779 :     data.stats.sse_u = data.sse_u;
780 :     data.stats.sse_v = data.sse_v;
781 : edgomez 1.91
782 : edgomez 1.102 if (stats)
783 :     *stats = data.stats;
784 :     }
785 : edgomez 1.91
786 : edgomez 1.102 /* call plugins */
787 :     for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
788 :     emms();
789 :     if (pEnc->plugins[i].func) {
790 : suxen_drol 1.120 if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, NULL) < 0) {
791 : edgomez 1.102 continue;
792 :     }
793 :     }
794 :     }
795 :     emms();
796 : edgomez 1.91
797 : edgomez 1.102 /* copy modified values back into frame*/
798 :     if (opt == XVID_PLG_BEFORE) {
799 :     *type = data.type;
800 :     *quant = data.quant > 0 ? data.quant : 2; /* default */
801 :    
802 :     frame->vol_flags = data.vol_flags;
803 :     frame->vop_flags = data.vop_flags;
804 :     frame->motion_flags = data.motion_flags;
805 :    
806 :     } else if (opt == XVID_PLG_FRAME) {
807 : edgomez 1.91
808 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
809 :     for (j=0; j<pEnc->mbParam.mb_height; j++)
810 :     for (i=0; i<pEnc->mbParam.mb_width; i++) {
811 :     frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i];
812 :     }
813 :     } else {
814 :     for (j=0; j<pEnc->mbParam.mb_height; j++)
815 :     for (i=0; i<pEnc->mbParam.mb_width; i++) {
816 :     frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
817 :     }
818 :     }
819 :     frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
820 :     }
821 : edgomez 1.91
822 :    
823 : edgomez 1.102 }
824 : edgomez 1.91
825 :    
826 : edgomez 1.102 static __inline void inc_frame_num(Encoder * pEnc)
827 :     {
828 :     pEnc->current->frame_num = pEnc->m_framenum;
829 :     pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */
830 : edgomez 1.91
831 : edgomez 1.102 pEnc->mbParam.m_stamp += pEnc->current->fincr;
832 :     pEnc->m_framenum++; /* debug ticker */
833 :     }
834 :    
835 :     static __inline void dec_frame_num(Encoder * pEnc)
836 :     {
837 :     pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr;
838 :     pEnc->m_framenum--; /* debug ticker */
839 :     }
840 : edgomez 1.91
841 : edgomez 1.102 static __inline void
842 :     MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam)
843 :     {
844 :     if (pMB->cbp == 0) {
845 :     /* we want to code dquant but the quantizer value will not be used yet
846 :     let's find out if we can postpone dquant to next MB
847 :     */
848 :     if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) {
849 :     pMB->dquant = 0; /* it's the last MB of all, the easiest case */
850 :     return;
851 :     } else {
852 :     MACROBLOCK * next = pMB + 1;
853 :     const MACROBLOCK * prev = pMB - 1;
854 :     if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED)
855 :     /* mode allows dquant change in the future */
856 :     if (abs(next->quant - prev->quant) <= 2) {
857 :     /* quant change is not out of range */
858 :     pMB->quant = prev->quant;
859 :     pMB->dquant = 0;
860 :     next->dquant = next->quant - prev->quant;
861 :     return;
862 :     }
863 :     }
864 :     }
865 :     /* couldn't skip this dquant */
866 :     pMB->mode = MODE_INTER_Q;
867 :     }
868 :    
869 : edgomez 1.91
870 :    
871 : edgomez 1.102 static __inline void
872 :     set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
873 :     {
874 : edgomez 1.91
875 : edgomez 1.102 pCur->ticks = (int32_t)pCur->stamp % time_base;
876 :     pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ;
877 : edgomez 1.91
878 : edgomez 1.102 #if 0 /* HEAVY DEBUG OUTPUT */
879 :     fprintf(stderr,"WriteVop: %d - %d \n",
880 :     ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
881 :     fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n",
882 :     pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
883 :     fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n",
884 :     pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
885 :     #endif
886 :     }
887 : edgomez 1.91
888 : edgomez 1.102 static void
889 :     simplify_par(int *par_width, int *par_height)
890 :     {
891 : edgomez 1.91
892 : edgomez 1.102 int _par_width = (!*par_width) ? 1 : (*par_width<0) ? -*par_width: *par_width;
893 :     int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height;
894 :     int divisor = gcd(_par_width, _par_height);
895 :    
896 :     _par_width /= divisor;
897 :     _par_height /= divisor;
898 :    
899 :     /* 2^8 precision maximum */
900 :     if (_par_width>255 || _par_height>255) {
901 :     float div;
902 : edgomez 1.91 emms();
903 : edgomez 1.102 if (_par_width>_par_height)
904 :     div = (float)_par_width/255;
905 :     else
906 :     div = (float)_par_height/255;
907 : edgomez 1.91
908 : edgomez 1.102 _par_width = (int)((float)_par_width/div);
909 :     _par_height = (int)((float)_par_height/div);
910 : edgomez 1.91 }
911 :    
912 : edgomez 1.102 *par_width = _par_width;
913 :     *par_height = _par_height;
914 : edgomez 1.91
915 : edgomez 1.102 return;
916 :     }
917 : edgomez 1.91
918 : syskin 1.96
919 : edgomez 1.102 /*****************************************************************************
920 :     * IPB frame encoder entry point
921 :     *
922 :     * Returned values :
923 :     * - >0 - output bytes
924 :     * - 0 - no output
925 :     * - XVID_ERR_VERSION - wrong version passed to core
926 :     * - XVID_ERR_END - End of stream reached before end of coding
927 :     * - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
928 :     * format
929 :     ****************************************************************************/
930 : edgomez 1.91
931 :    
932 : edgomez 1.102 int
933 :     enc_encode(Encoder * pEnc,
934 :     xvid_enc_frame_t * xFrame,
935 :     xvid_enc_stats_t * stats)
936 :     {
937 :     xvid_enc_frame_t * frame;
938 :     int type;
939 :     Bitstream bs;
940 : Isibaar 1.97
941 : edgomez 1.102 if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1)) /* v1.x.x */
942 :     return XVID_ERR_VERSION;
943 : Isibaar 1.97
944 : edgomez 1.102 xFrame->out_flags = 0;
945 : edgomez 1.91
946 : edgomez 1.102 start_global_timer();
947 :     BitstreamInit(&bs, xFrame->bitstream, 0);
948 : edgomez 1.91
949 :    
950 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
951 :     * enqueue image to the encoding-queue
952 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
953 : edgomez 1.91
954 : edgomez 1.102 if (xFrame->input.csp != XVID_CSP_NULL)
955 : edgomez 1.91 {
956 : edgomez 1.102 QUEUEINFO * q = &pEnc->queue[pEnc->queue_tail];
957 : edgomez 1.91
958 :     start_timer();
959 :     if (image_input
960 : edgomez 1.102 (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
961 :     pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
962 :     xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING))
963 : edgomez 1.91 {
964 :     emms();
965 :     return XVID_ERR_FORMAT;
966 :     }
967 :     stop_conv_timer();
968 :    
969 : edgomez 1.102 if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
970 :     image_chroma_optimize(&q->image,
971 : edgomez 1.92 pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
972 :     }
973 :    
974 : edgomez 1.102 q->frame = *xFrame;
975 :    
976 :     if (xFrame->quant_intra_matrix)
977 : edgomez 1.91 {
978 : edgomez 1.102 memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char));
979 :     q->frame.quant_intra_matrix = q->quant_intra_matrix;
980 : edgomez 1.91 }
981 :    
982 : edgomez 1.102 if (xFrame->quant_inter_matrix)
983 :     {
984 :     memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char));
985 :     q->frame.quant_inter_matrix = q->quant_inter_matrix;
986 : edgomez 1.91 }
987 :    
988 : edgomez 1.102 pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1);
989 :     pEnc->queue_size++;
990 : edgomez 1.91 }
991 :    
992 :    
993 :     /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
994 : edgomez 1.102 * bframe flush code
995 : edgomez 1.91 * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
996 :    
997 : edgomez 1.102 repeat:
998 : edgomez 1.91
999 : edgomez 1.102 if (pEnc->flush_bframes)
1000 :     {
1001 :     if (pEnc->bframenum_head < pEnc->bframenum_tail) {
1002 : edgomez 1.91
1003 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1004 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1005 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1006 : edgomez 1.91
1007 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1008 :     image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image,
1009 :     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1010 : edgomez 1.91 }
1011 :    
1012 : edgomez 1.102 FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1013 : suxen_drol 1.120 call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, NULL, NULL, stats);
1014 : edgomez 1.102 pEnc->bframenum_head++;
1015 : edgomez 1.91
1016 : edgomez 1.102 goto done;
1017 :     }
1018 : edgomez 1.91
1019 : edgomez 1.102 /* write an empty marker to the bitstream.
1020 : edgomez 1.91
1021 : edgomez 1.102 for divx5 decoder compatibility, this marker must consist
1022 :     of a not-coded p-vop, with a time_base of zero, and time_increment
1023 :     indentical to the future-referece frame.
1024 :     */
1025 : edgomez 1.91
1026 : edgomez 1.102 if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) {
1027 :     int tmp;
1028 :     int bits;
1029 : edgomez 1.91
1030 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1031 : edgomez 1.91 pEnc->bframenum_head, pEnc->bframenum_tail,
1032 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1033 :    
1034 : edgomez 1.102 bits = BitstreamPos(&bs);
1035 : edgomez 1.91
1036 : edgomez 1.102 tmp = pEnc->current->seconds;
1037 :     pEnc->current->seconds = 0; /* force time_base = 0 */
1038 : edgomez 1.91
1039 : edgomez 1.102 BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
1040 :     BitstreamPad(&bs);
1041 :     pEnc->current->seconds = tmp;
1042 : edgomez 1.91
1043 : edgomez 1.102 /* add the not-coded length to the reference frame size */
1044 :     pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1045 : suxen_drol 1.120 call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1046 : edgomez 1.91
1047 : edgomez 1.102 /* flush complete: reset counters */
1048 :     pEnc->flush_bframes = 0;
1049 :     pEnc->bframenum_head = pEnc->bframenum_tail = 0;
1050 :     goto done;
1051 : edgomez 1.91
1052 :     }
1053 :    
1054 : edgomez 1.102 /* flush complete: reset counters */
1055 :     pEnc->flush_bframes = 0;
1056 :     pEnc->bframenum_head = pEnc->bframenum_tail = 0;
1057 :     }
1058 : edgomez 1.91
1059 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1060 :     * dequeue frame from the encoding queue
1061 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1062 : edgomez 1.91
1063 : edgomez 1.102 if (pEnc->queue_size == 0) /* empty */
1064 :     {
1065 :     if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
1066 :     {
1067 : edgomez 1.91
1068 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1069 : edgomez 1.91 pEnc->bframenum_head, pEnc->bframenum_tail,
1070 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1071 :    
1072 : edgomez 1.102 if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
1073 : suxen_drol 1.120 call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1074 : edgomez 1.102 }
1075 : edgomez 1.91
1076 : edgomez 1.102 /* if the very last frame is to be b-vop, we must change it to a p-vop */
1077 :     if (pEnc->bframenum_tail > 0) {
1078 : edgomez 1.91
1079 : edgomez 1.102 SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1080 :     pEnc->bframenum_tail--;
1081 :     SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1082 : edgomez 1.91
1083 : edgomez 1.102 /* convert B-VOP to P-VOP */
1084 :     pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1085 :     pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1086 :     pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1087 : edgomez 1.91
1088 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1089 :     image_copy(&pEnc->sOriginal, &pEnc->current->image,
1090 :     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1091 :     }
1092 : syskin 1.96
1093 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1094 : edgomez 1.91 pEnc->bframenum_head, pEnc->bframenum_tail,
1095 : edgomez 1.102 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1096 : syskin 1.103 pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */
1097 : edgomez 1.91
1098 : syskin 1.104 FrameCodeP(pEnc, &bs);
1099 : edgomez 1.91
1100 :    
1101 : edgomez 1.102 if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1102 : suxen_drol 1.120 call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1103 : edgomez 1.102 }else{
1104 :     pEnc->flush_bframes = 1;
1105 :     goto done;
1106 :     }
1107 :     }
1108 :     DPRINTF(XVID_DEBUG_DEBUG, "*** END\n");
1109 : edgomez 1.91
1110 : edgomez 1.102 emms();
1111 :     return XVID_ERR_END; /* end of stream reached */
1112 : edgomez 1.91 }
1113 : edgomez 1.102 goto done; /* nothing to encode yet; encoder lag */
1114 : edgomez 1.91 }
1115 :    
1116 : edgomez 1.102 /* the current FRAME becomes the reference */
1117 :     SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1118 : edgomez 1.91
1119 : edgomez 1.102 /* remove frame from encoding-queue (head), and move it into the current */
1120 :     image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1121 :     frame = &pEnc->queue[pEnc->queue_head].frame;
1122 :     pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);
1123 :     pEnc->queue_size--;
1124 : edgomez 1.91
1125 : Isibaar 1.1
1126 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1127 :     * init pEnc->current fields
1128 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1129 : Isibaar 1.1
1130 : edgomez 1.102 pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1131 : edgomez 1.91 inc_frame_num(pEnc);
1132 : edgomez 1.102 pEnc->current->vol_flags = frame->vol_flags;
1133 :     pEnc->current->vop_flags = frame->vop_flags;
1134 :     pEnc->current->motion_flags = frame->motion;
1135 :     pEnc->current->fcode = pEnc->mbParam.m_fcode;
1136 :     pEnc->current->bcode = pEnc->mbParam.m_fcode;
1137 : edgomez 1.91
1138 : edgomez 1.92
1139 : edgomez 1.102 if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1140 : syskin 1.96 image_chroma_optimize(&pEnc->current->image,
1141 : edgomez 1.92 pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1142 :     }
1143 : Isibaar 1.1
1144 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1145 :     * frame type & quant selection
1146 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1147 :    
1148 :     type = frame->type;
1149 :     pEnc->current->quant = frame->quant;
1150 :    
1151 : Skal 1.118 call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats);
1152 : edgomez 1.102
1153 :     if (type > 0){ /* XVID_TYPE_?VOP */
1154 :     type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */
1155 :     } else{ /* XVID_TYPE_AUTO */
1156 :     if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){
1157 :     pEnc->iFrameNum = 0;
1158 :     type = I_VOP;
1159 :     }else{
1160 :     type = MEanalysis(&pEnc->reference->image, pEnc->current,
1161 :     &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1162 :     pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1163 :     (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
1164 :     }
1165 : edgomez 1.91 }
1166 : Isibaar 1.23
1167 : edgomez 1.102 if (type != I_VOP)
1168 :     pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */
1169 :    
1170 :     /* bframes buffer overflow check */
1171 :     if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1172 :     type = P_VOP;
1173 :     }
1174 : suxen_drol 1.27
1175 : edgomez 1.102 pEnc->iFrameNum++;
1176 : Isibaar 1.1
1177 : edgomez 1.102 if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1178 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1179 :     "%d st:%lld if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1180 : Isibaar 1.1 }
1181 :    
1182 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1183 :     * encode this frame as a b-vop
1184 :     * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)
1185 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1186 :     if (type == B_VOP) {
1187 :     if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1188 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1189 :     }
1190 :    
1191 :     if (frame->quant < 1) {
1192 :     pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1193 :     pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1194 : edgomez 1.91
1195 : edgomez 1.102 } else {
1196 :     pEnc->current->quant = frame->quant;
1197 :     }
1198 : edgomez 1.41
1199 : edgomez 1.102 if (pEnc->current->quant < 1)
1200 :     pEnc->current->quant = 1;
1201 :     else if (pEnc->current->quant > 31)
1202 :     pEnc->current->quant = 31;
1203 : edgomez 1.41
1204 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n",
1205 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1206 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1207 : edgomez 1.40
1208 : edgomez 1.102 /* store frame into bframe buffer & swap ref back to current */
1209 :     SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1210 :     SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1211 : edgomez 1.41
1212 : edgomez 1.102 pEnc->bframenum_tail++;
1213 : edgomez 1.40
1214 : edgomez 1.102 goto repeat;
1215 :     }
1216 : edgomez 1.40
1217 :    
1218 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1219 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1220 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1221 : edgomez 1.40
1222 : edgomez 1.102 /* for unpacked bframes, output the stats for the last encoded frame */
1223 :     if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)
1224 :     {
1225 :     if (pEnc->current->stamp > 0) {
1226 : suxen_drol 1.120 call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1227 : edgomez 1.40 }
1228 : edgomez 1.102 else
1229 :     stats->type = XVID_TYPE_NOTHING;
1230 :     }
1231 : edgomez 1.40
1232 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1233 :     * closed-gop
1234 :     * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe
1235 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1236 : Isibaar 1.1
1237 : edgomez 1.102 if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1238 : Isibaar 1.1
1239 : edgomez 1.102 /* place this frame back on the encoding-queue (head) */
1240 :     /* we will deal with it next time */
1241 :     dec_frame_num(pEnc);
1242 :     pEnc->iFrameNum--;
1243 : edgomez 1.13
1244 : edgomez 1.102 pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);
1245 :     pEnc->queue_size++;
1246 :     image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1247 : edgomez 1.41
1248 : edgomez 1.102 /* grab the last frame from the bframe-queue */
1249 : edgomez 1.41
1250 : edgomez 1.102 pEnc->bframenum_tail--;
1251 :     SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1252 : Isibaar 1.1
1253 : edgomez 1.102 if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1254 : suxen_drol 1.117 image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "CLOSED GOP BVOP->PVOP");
1255 : edgomez 1.40 }
1256 :    
1257 : edgomez 1.102 /* convert B-VOP quant to P-VOP */
1258 :     pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1259 :     pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1260 :     pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1261 :     type = P_VOP;
1262 : edgomez 1.3 }
1263 : Isibaar 1.1
1264 : edgomez 1.93
1265 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1266 :     * encode this frame as an i-vop
1267 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1268 : h 1.5
1269 : edgomez 1.102 if (type == I_VOP) {
1270 : edgomez 1.41
1271 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1272 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1273 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1274 : Isibaar 1.7
1275 : edgomez 1.102 if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1276 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1277 :     }
1278 : syskin 1.96
1279 : edgomez 1.102 pEnc->iFrameNum = 1;
1280 : Isibaar 1.1
1281 : edgomez 1.102 /* ---- update vol flags at IVOP ----------- */
1282 :     pEnc->mbParam.vol_flags = pEnc->current->vol_flags;
1283 : edgomez 1.41
1284 : edgomez 1.102 /* Aspect ratio */
1285 :     switch(frame->par) {
1286 :     case XVID_PAR_11_VGA:
1287 :     case XVID_PAR_43_PAL:
1288 :     case XVID_PAR_43_NTSC:
1289 :     case XVID_PAR_169_PAL:
1290 :     case XVID_PAR_169_NTSC:
1291 :     case XVID_PAR_EXT:
1292 :     pEnc->mbParam.par = frame->par;
1293 :     break;
1294 :     default:
1295 :     pEnc->mbParam.par = XVID_PAR_11_VGA;
1296 :     break;
1297 :     }
1298 : Isibaar 1.1
1299 : edgomez 1.102 /* For extended PAR only, we try to sanityse/simplify par values */
1300 :     if (pEnc->mbParam.par == XVID_PAR_EXT) {
1301 :     pEnc->mbParam.par_width = frame->par_width;
1302 :     pEnc->mbParam.par_height = frame->par_height;
1303 :     simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height);
1304 :     }
1305 : Isibaar 1.1
1306 : edgomez 1.102 if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1307 :     if (frame->quant_intra_matrix != NULL)
1308 :     set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix);
1309 :     if (frame->quant_inter_matrix != NULL)
1310 :     set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix);
1311 :     }
1312 : Isibaar 1.1
1313 : edgomez 1.102 /* prevent vol/vop misuse */
1314 : Isibaar 1.1
1315 : edgomez 1.102 if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1316 :     pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1317 : suxen_drol 1.27
1318 : edgomez 1.102 /* ^^^------------------------ */
1319 : edgomez 1.41
1320 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1321 :     image_copy(&pEnc->sOriginal, &pEnc->current->image,
1322 :     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1323 : Isibaar 1.1 }
1324 :    
1325 : edgomez 1.102 FrameCodeI(pEnc, &bs);
1326 :     xFrame->out_flags |= XVID_KEYFRAME;
1327 : Isibaar 1.1
1328 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1329 :     * encode this frame as an p-vop
1330 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1331 : Isibaar 1.1
1332 : edgomez 1.102 } else { /* (type == P_VOP || type == S_VOP) */
1333 : h 1.20
1334 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1335 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1336 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1337 : h 1.20
1338 : edgomez 1.102 if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1339 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1340 :     }
1341 : h 1.20
1342 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1343 :     image_copy(&pEnc->sOriginal, &pEnc->current->image,
1344 :     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1345 :     }
1346 : h 1.20
1347 : syskin 1.104 if ( FrameCodeP(pEnc, &bs) == 0 ) {
1348 : syskin 1.103 /* N-VOP, we mustn't code b-frames yet */
1349 : Isibaar 1.120.2.1 if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) ||
1350 :     pEnc->mbParam.max_bframes == 0)
1351 :     call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1352 : syskin 1.103 goto done;
1353 :     }
1354 : h 1.20 }
1355 :    
1356 : edgomez 1.41
1357 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1358 :     * on next enc_encode call we must flush bframes
1359 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1360 : edgomez 1.41
1361 : edgomez 1.102 /*done_flush:*/
1362 : h 1.26
1363 : edgomez 1.102 pEnc->flush_bframes = 1;
1364 : h 1.26
1365 : edgomez 1.102 /* packed & queued_bframes: dont bother outputting stats here, we do so after the flush */
1366 :     if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1367 :     goto repeat;
1368 : h 1.20 }
1369 :    
1370 : edgomez 1.102 /* packed or no-bframes or no-bframes-queued: output stats */
1371 :     if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {
1372 : suxen_drol 1.120 call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1373 : edgomez 1.102 }
1374 : h 1.20
1375 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1376 :     * done; return number of bytes consumed
1377 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1378 : h 1.20
1379 : edgomez 1.102 done:
1380 : h 1.20
1381 : edgomez 1.102 stop_global_timer();
1382 :     write_timer();
1383 : h 1.20
1384 : edgomez 1.102 emms();
1385 :     return BitstreamLength(&bs);
1386 :     }
1387 : h 1.20
1388 :    
1389 : edgomez 1.102 static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1390 :     {
1391 :     unsigned int i;
1392 :     MACROBLOCK * pMB = frame->mbs;
1393 :     int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1394 :     if (quant > 31)
1395 :     frame->quant = quant = 31;
1396 :     else if (quant < 1)
1397 :     frame->quant = quant = 1;
1398 :    
1399 :     for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) {
1400 :     quant += pMB->dquant;
1401 :     if (quant > 31)
1402 :     quant = 31;
1403 :     else if (quant < 1)
1404 :     quant = 1;
1405 :     pMB->quant = quant;
1406 :     pMB++;
1407 : h 1.20 }
1408 : edgomez 1.102 }
1409 : h 1.20
1410 :    
1411 : edgomez 1.102 static __inline void
1412 :     CodeIntraMB(Encoder * pEnc,
1413 :     MACROBLOCK * pMB)
1414 :     {
1415 : h 1.20
1416 : edgomez 1.102 pMB->mode = MODE_INTRA;
1417 : h 1.20
1418 : edgomez 1.102 /* zero mv statistics */
1419 :     pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1420 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1421 :     pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1422 :     pMB->sad16 = 0;
1423 : h 1.20
1424 : edgomez 1.102 if (pMB->dquant != 0) {
1425 :     pMB->mode = MODE_INTRA_Q;
1426 : h 1.20 }
1427 :     }
1428 :    
1429 :    
1430 : edgomez 1.102
1431 : edgomez 1.41 static int
1432 :     FrameCodeI(Encoder * pEnc,
1433 : edgomez 1.102 Bitstream * bs)
1434 : h 1.21 {
1435 : edgomez 1.102 int bits = BitstreamPos(bs);
1436 : edgomez 1.91 int mb_width = pEnc->mbParam.mb_width;
1437 :     int mb_height = pEnc->mbParam.mb_height;
1438 : h 1.21
1439 :     DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1440 : edgomez 1.41 DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1441 : h 1.21
1442 :     uint16_t x, y;
1443 :    
1444 : suxen_drol 1.27 pEnc->mbParam.m_rounding_type = 1;
1445 :     pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1446 :     pEnc->current->coding_type = I_VOP;
1447 : h 1.21
1448 : edgomez 1.102 call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1449 :    
1450 :     SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1451 :    
1452 : suxen_drol 1.27 BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1453 : edgomez 1.78
1454 : edgomez 1.91 set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1455 :    
1456 : edgomez 1.101 BitstreamPad(bs);
1457 : h 1.21
1458 : edgomez 1.102 BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1459 : h 1.21
1460 : edgomez 1.91 pEnc->current->sStat.iTextBits = 0;
1461 :     pEnc->current->sStat.kblks = mb_width * mb_height;
1462 :     pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1463 : h 1.21
1464 : edgomez 1.91 for (y = 0; y < mb_height; y++)
1465 :     for (x = 0; x < mb_width; x++) {
1466 : edgomez 1.41 MACROBLOCK *pMB =
1467 :     &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1468 : h 1.21
1469 :     CodeIntraMB(pEnc, pMB);
1470 :    
1471 : edgomez 1.41 MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1472 :     dct_codes, qcoeff);
1473 : h 1.21
1474 :     start_timer();
1475 : suxen_drol 1.27 MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1476 : h 1.21 stop_prediction_timer();
1477 :    
1478 :     start_timer();
1479 : edgomez 1.91 MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1480 : h 1.21 stop_coding_timer();
1481 :     }
1482 :    
1483 :     emms();
1484 :    
1485 : edgomez 1.102 BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1486 :    
1487 :     pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1488 :    
1489 : edgomez 1.91 pEnc->fMvPrevSigma = -1;
1490 : suxen_drol 1.27 pEnc->mbParam.m_fcode = 2;
1491 : h 1.21
1492 : edgomez 1.102 pEnc->current->is_edged = 0; /* not edged */
1493 :     pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1494 : h 1.21
1495 : edgomez 1.93 return 1; /* intra */
1496 : h 1.21 }
1497 :    
1498 : syskin 1.114 static __inline void
1499 :     updateFcode(Statistics * sStat, Encoder * pEnc)
1500 :     {
1501 :     float fSigma;
1502 :     int iSearchRange;
1503 :    
1504 :     if (sStat->iMvCount == 0)
1505 :     sStat->iMvCount = 1;
1506 :    
1507 :     fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
1508 :    
1509 :     iSearchRange = 16 << pEnc->mbParam.m_fcode;
1510 :    
1511 :     if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1512 :     pEnc->mbParam.m_fcode++;
1513 :    
1514 :     else if ((5.0 * fSigma < iSearchRange)
1515 :     && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1516 :     && (pEnc->mbParam.m_fcode >= 2) )
1517 :     pEnc->mbParam.m_fcode--;
1518 :    
1519 :     pEnc->fMvPrevSigma = fSigma;
1520 :     }
1521 : h 1.21
1522 : edgomez 1.91 #define BFRAME_SKIP_THRESHHOLD 30
1523 :    
1524 :     /* FrameCodeP also handles S(GMC)-VOPs */
1525 : edgomez 1.41 static int
1526 :     FrameCodeP(Encoder * pEnc,
1527 : syskin 1.104 Bitstream * bs)
1528 : Isibaar 1.1 {
1529 : edgomez 1.102 int bits = BitstreamPos(bs);
1530 : edgomez 1.13
1531 :     DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1532 : edgomez 1.41 DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1533 : Isibaar 1.8
1534 : edgomez 1.91 int x, y, k;
1535 : edgomez 1.102 FRAMEINFO *const current = pEnc->current;
1536 :     FRAMEINFO *const reference = pEnc->reference;
1537 :     MBParam * const pParam = &pEnc->mbParam;
1538 :     int mb_width = pParam->mb_width;
1539 :     int mb_height = pParam->mb_height;
1540 : syskin 1.103 int coded = 1;
1541 : syskin 1.96
1542 : edgomez 1.102 IMAGE *pRef = &reference->image;
1543 :    
1544 :     if (!reference->is_edged) {
1545 :     start_timer();
1546 :     image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1547 :     pParam->width, pParam->height, 0);
1548 :     stop_edges_timer();
1549 :     reference->is_edged = 1;
1550 :     }
1551 :    
1552 :     pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1553 :     current->rounding_type = pParam->m_rounding_type;
1554 :     current->fcode = pParam->m_fcode;
1555 : Isibaar 1.1
1556 : edgomez 1.102 if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1557 :     if (reference->is_interpolated != current->rounding_type) {
1558 :     start_timer();
1559 :     image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1560 :     &pEnc->vInterHV, pParam->edged_width,
1561 :     pParam->edged_height,
1562 :     (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1563 :     current->rounding_type);
1564 :     stop_inter_timer();
1565 :     reference->is_interpolated = current->rounding_type;
1566 :     }
1567 : Isibaar 1.1 }
1568 :    
1569 : syskin 1.111 current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1570 :     current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0;
1571 :    
1572 : edgomez 1.102 current->coding_type = P_VOP;
1573 :    
1574 :     call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1575 :    
1576 :     SetMacroblockQuants(&pEnc->mbParam, current);
1577 : syskin 1.96
1578 : Isibaar 1.1 start_timer();
1579 : edgomez 1.102 if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1580 :     { int gmcval;
1581 :     current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1582 :     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1583 :    
1584 :     if (current->motion_flags & XVID_ME_GME_REFINE) {
1585 :     gmcval = GlobalMotionEstRefine(&current->warp,
1586 :     current->mbs, pParam,
1587 :     current, reference,
1588 :     &current->image,
1589 :     &reference->image,
1590 :     &pEnc->vInterH,
1591 :     &pEnc->vInterV,
1592 :     &pEnc->vInterHV);
1593 :     } else {
1594 :     gmcval = globalSAD(&current->warp, pParam, current->mbs,
1595 :     current,
1596 :     &reference->image,
1597 :     &current->image,
1598 :     pEnc->vGMC.y);
1599 :     }
1600 :    
1601 :     gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1602 :    
1603 :     /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1604 :     generate_GMCparameters( 3, 3, &current->warp,
1605 :     pParam->width, pParam->height,
1606 :     &current->new_gmc_data);
1607 :    
1608 :     if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
1609 :     (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
1610 :     {
1611 :     current->coding_type = S_VOP;
1612 :    
1613 :     generate_GMCimage(&current->new_gmc_data, &reference->image,
1614 :     pParam->mb_width, pParam->mb_height,
1615 :     pParam->edged_width, pParam->edged_width/2,
1616 :     pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1617 :     current->rounding_type, current->mbs, &pEnc->vGMC);
1618 :    
1619 :     } else {
1620 : edgomez 1.91
1621 : edgomez 1.102 generate_GMCimage(&current->new_gmc_data, &reference->image,
1622 :     pParam->mb_width, pParam->mb_height,
1623 :     pParam->edged_width, pParam->edged_width/2,
1624 :     pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1625 :     current->rounding_type, current->mbs, NULL); /* no warping, just AMV */
1626 :     }
1627 :     }
1628 : Isibaar 1.1
1629 : syskin 1.104 MotionEstimation(&pEnc->mbParam, current, reference,
1630 : edgomez 1.102 &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1631 : syskin 1.104 &pEnc->vGMC, 256*4096);
1632 : edgomez 1.91
1633 :    
1634 : edgomez 1.102 stop_motion_timer();
1635 : edgomez 1.91
1636 : edgomez 1.102 set_timecodes(current,reference,pParam->fbase);
1637 : Isibaar 1.1
1638 : edgomez 1.102 BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
1639 : edgomez 1.3
1640 : edgomez 1.91 for (y = 0; y < mb_height; y++) {
1641 :     for (x = 0; x < mb_width; x++) {
1642 : syskin 1.116 MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1643 :     int skip_possible;
1644 : edgomez 1.91
1645 : syskin 1.115 if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1646 : Isibaar 1.1 CodeIntraMB(pEnc, pMB);
1647 : edgomez 1.102 MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1648 : edgomez 1.41 dct_codes, qcoeff);
1649 : chl 1.81
1650 :     start_timer();
1651 : edgomez 1.102 MBPrediction(current, x, y, pParam->mb_width, qcoeff);
1652 : chl 1.81 stop_prediction_timer();
1653 : edgomez 1.91
1654 : edgomez 1.102 current->sStat.kblks++;
1655 : edgomez 1.91
1656 : edgomez 1.102 MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1657 : edgomez 1.91 stop_coding_timer();
1658 :     continue;
1659 :     }
1660 :    
1661 :     start_timer();
1662 : edgomez 1.102 MBMotionCompensation(pMB, x, y, &reference->image,
1663 : edgomez 1.91 &pEnc->vInterH, &pEnc->vInterV,
1664 : syskin 1.96 &pEnc->vInterHV, &pEnc->vGMC,
1665 : edgomez 1.102 &current->image,
1666 :     dct_codes, pParam->width,
1667 :     pParam->height,
1668 :     pParam->edged_width,
1669 :     (current->vol_flags & XVID_VOL_QUARTERPEL),
1670 :     current->rounding_type);
1671 : edgomez 1.91
1672 :     stop_comp_timer();
1673 :    
1674 :     pMB->field_pred = 0;
1675 :    
1676 : syskin 1.112 if (pMB->cbp != 0) {
1677 : syskin 1.113 pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
1678 : edgomez 1.91 dct_codes, qcoeff);
1679 :     }
1680 :    
1681 : edgomez 1.102 if (pMB->dquant != 0)
1682 :     MBSetDquant(pMB, x, y, &pEnc->mbParam);
1683 :    
1684 :    
1685 : edgomez 1.91 if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1686 :     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1687 :     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1688 : edgomez 1.102 current->sStat.mblks++;
1689 : chl 1.64 } else {
1690 : edgomez 1.102 current->sStat.ublks++;
1691 : edgomez 1.91 }
1692 : edgomez 1.102
1693 : Isibaar 1.1 start_timer();
1694 : chl 1.65
1695 :     /* Finished processing the MB, now check if to CODE or SKIP */
1696 :    
1697 : syskin 1.116 skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
1698 : edgomez 1.102
1699 :     if (current->coding_type == S_VOP)
1700 : edgomez 1.91 skip_possible &= (pMB->mcsel == 1);
1701 : syskin 1.116 else { /* PVOP */
1702 :     const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1703 :     pMB->qmvs : pMB->mvs;
1704 :     skip_possible &= ((mv->x|mv->y) == 0);
1705 : edgomez 1.91 }
1706 :    
1707 : syskin 1.116 if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1708 :     /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1709 :     int bSkip = 1;
1710 : edgomez 1.91
1711 : syskin 1.116 if (current->coding_type == P_VOP) { /* special rule for P-VOP's SKIP */
1712 : edgomez 1.91
1713 : syskin 1.116 for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1714 : edgomez 1.91 int iSAD;
1715 : edgomez 1.102 iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1716 : syskin 1.116 pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1717 :     pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1718 :     if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) {
1719 :     bSkip = 0; /* could not SKIP */
1720 :     if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1721 :     VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1722 :     pMB->pmvs[0].x = - predMV.x;
1723 :     pMB->pmvs[0].y = - predMV.y;
1724 :     } else {
1725 :     VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1726 :     pMB->pmvs[0].x = - predMV.x;
1727 :     pMB->pmvs[0].y = - predMV.y;
1728 :     }
1729 :     pMB->mode = MODE_INTER;
1730 :     pMB->cbp = 0;
1731 : edgomez 1.91 break;
1732 :     }
1733 :     }
1734 :     }
1735 :    
1736 : syskin 1.116 if (bSkip) {
1737 :     /* do SKIP */
1738 :     pMB->mode = MODE_NOT_CODED;
1739 :     MBSkip(bs);
1740 :     stop_coding_timer();
1741 :     continue; /* next MB */
1742 : chl 1.68 }
1743 : chl 1.65 }
1744 : edgomez 1.102
1745 : syskin 1.116 /* ordinary case: normal coded INTER/INTER4V block */
1746 : edgomez 1.102 MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
1747 : edgomez 1.91 stop_coding_timer();
1748 : Isibaar 1.1 }
1749 :     }
1750 :    
1751 :     emms();
1752 : syskin 1.114 updateFcode(&current->sStat, pEnc);
1753 : edgomez 1.91
1754 :     /* frame drop code */
1755 : edgomez 1.102 #if 0
1756 :     DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
1757 :     #endif
1758 : syskin 1.103 if (current->sStat.kblks + current->sStat.mblks <=
1759 : syskin 1.105 (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
1760 :     ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )
1761 : edgomez 1.91 {
1762 : Isibaar 1.120.2.1 current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0;
1763 : edgomez 1.102 current->sStat.ublks = mb_width * mb_height;
1764 : edgomez 1.91
1765 :     BitstreamReset(bs);
1766 :    
1767 : edgomez 1.102 set_timecodes(current,reference,pParam->fbase);
1768 :     BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
1769 : edgomez 1.91
1770 : edgomez 1.93 /* copy reference frame details into the current frame */
1771 : edgomez 1.102 current->quant = reference->quant;
1772 :     current->motion_flags = reference->motion_flags;
1773 :     current->rounding_type = reference->rounding_type;
1774 :     current->fcode = reference->fcode;
1775 :     current->bcode = reference->bcode;
1776 : syskin 1.103 current->stamp = reference->stamp;
1777 : edgomez 1.102 image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
1778 :     memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1779 : syskin 1.103 coded = 0;
1780 :    
1781 :     } else {
1782 : edgomez 1.102
1783 : syskin 1.103 pEnc->current->is_edged = 0; /* not edged */
1784 :     pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1785 : edgomez 1.102
1786 : syskin 1.103 /* what was this frame's interpolated reference will become
1787 :     forward (past) reference in b-frame coding */
1788 : edgomez 1.102
1789 : syskin 1.103 image_swap(&pEnc->vInterH, &pEnc->f_refh);
1790 :     image_swap(&pEnc->vInterV, &pEnc->f_refv);
1791 :     image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
1792 :     }
1793 : edgomez 1.91
1794 :     /* XXX: debug
1795 :     {
1796 :     char s[100];
1797 :     sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1798 : edgomez 1.102 image_dump_yuvpgm(&current->image,
1799 :     pParam->edged_width,
1800 :     pParam->width, pParam->height, s);
1801 :    
1802 : edgomez 1.91 sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
1803 : edgomez 1.102 image_dump_yuvpgm(&reference->image,
1804 :     pParam->edged_width,
1805 :     pParam->width, pParam->height, s);
1806 : syskin 1.96 }
1807 : edgomez 1.91 */
1808 :    
1809 : edgomez 1.102 BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1810 : suxen_drol 1.60
1811 : edgomez 1.102 current->length = (BitstreamPos(bs) - bits) / 8;
1812 : Isibaar 1.1
1813 : syskin 1.103 return coded;
1814 : edgomez 1.91 }
1815 :    
1816 :    
1817 :     static void
1818 :     FrameCodeB(Encoder * pEnc,
1819 :     FRAMEINFO * frame,
1820 : edgomez 1.102 Bitstream * bs)
1821 : edgomez 1.91 {
1822 : edgomez 1.102 int bits = BitstreamPos(bs);
1823 : edgomez 1.91 DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1824 :     DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1825 :     uint32_t x, y;
1826 :    
1827 :     IMAGE *f_ref = &pEnc->reference->image;
1828 :     IMAGE *b_ref = &pEnc->current->image;
1829 :    
1830 : edgomez 1.102 #ifdef BFRAMES_DEC_DEBUG
1831 : edgomez 1.91 FILE *fp;
1832 :     static char first=0;
1833 :     #define BFRAME_DEBUG if (!first && fp){ \
1834 :     fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1835 :     }
1836 :    
1837 :     if (!first){
1838 :     fp=fopen("C:\\XVIDDBGE.TXT","w");
1839 :     }
1840 :     #endif
1841 :    
1842 : edgomez 1.102 /* forward */
1843 :     if (!pEnc->reference->is_edged) {
1844 :     image_setedges(f_ref, pEnc->mbParam.edged_width,
1845 :     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1846 :     pEnc->mbParam.height, 0);
1847 :     pEnc->current->is_edged = 1;
1848 :     }
1849 : syskin 1.96
1850 : edgomez 1.102 if (pEnc->reference->is_interpolated != 0) {
1851 :     start_timer();
1852 :     image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1853 :     pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1854 :     (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1855 :     stop_inter_timer();
1856 :     pEnc->reference->is_interpolated = 0;
1857 :     }
1858 : edgomez 1.91
1859 : edgomez 1.93 /* backward */
1860 : edgomez 1.102 if (!pEnc->current->is_edged) {
1861 :     image_setedges(b_ref, pEnc->mbParam.edged_width,
1862 :     pEnc->mbParam.edged_height, pEnc->mbParam.width,
1863 :     pEnc->mbParam.height, 0);
1864 :     pEnc->current->is_edged = 1;
1865 :     }
1866 :    
1867 :     if (pEnc->current->is_interpolated != 0) {
1868 :     start_timer();
1869 :     image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1870 :     pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1871 :     (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1872 :     stop_inter_timer();
1873 :     pEnc->current->is_interpolated = 0;
1874 :     }
1875 :    
1876 :     frame->coding_type = B_VOP;
1877 :     call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1878 : edgomez 1.91
1879 :     start_timer();
1880 : syskin 1.96 MotionEstimationBVOP(&pEnc->mbParam, frame,
1881 : edgomez 1.93 ((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */
1882 :     ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */
1883 :     pEnc->reference->mbs, f_ref,
1884 : edgomez 1.91 &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1885 :     pEnc->current, b_ref, &pEnc->vInterH,
1886 :     &pEnc->vInterV, &pEnc->vInterHV);
1887 :     stop_motion_timer();
1888 :    
1889 :     set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1890 : edgomez 1.102 BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
1891 : edgomez 1.91
1892 :     frame->sStat.iTextBits = 0;
1893 :     frame->sStat.iMvSum = 0;
1894 :     frame->sStat.iMvCount = 0;
1895 :     frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
1896 : edgomez 1.102 frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1897 :     frame->sStat.kblks = frame->sStat.ublks = 0;
1898 : edgomez 1.91
1899 :     for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1900 :     for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1901 :     MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1902 :    
1903 : edgomez 1.93 /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1904 : edgomez 1.91 if (mb->mode == MODE_NOT_CODED) {
1905 : edgomez 1.102 if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1906 :     MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
1907 : syskin 1.109 NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);
1908 : edgomez 1.102 }
1909 : edgomez 1.91 continue;
1910 :     }
1911 :    
1912 : syskin 1.112 mb->quant = frame->quant;
1913 :    
1914 :     if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
1915 :     /* we have to motion-compensate, transfer etc,
1916 :     because there might be blocks to code */
1917 :    
1918 : edgomez 1.91 MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1919 : syskin 1.112 f_ref, &pEnc->f_refh, &pEnc->f_refv,
1920 :     &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1921 :     &pEnc->vInterV, &pEnc->vInterHV,
1922 :     dct_codes);
1923 :    
1924 :     mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff);
1925 : edgomez 1.91 }
1926 : syskin 1.112
1927 :     if (mb->mode == MODE_DIRECT_NO4V)
1928 :     mb->mode = MODE_DIRECT;
1929 :    
1930 :     if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
1931 :     mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
1932 : syskin 1.115 else
1933 :     if (frame->vop_flags & XVID_VOP_GREYSCALE)
1934 :     /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
1935 :     mb->cbp &= 0x3C;
1936 : edgomez 1.102
1937 : edgomez 1.91 start_timer();
1938 : edgomez 1.102 MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,
1939 :     &frame->sStat);
1940 : edgomez 1.91 stop_coding_timer();
1941 :     }
1942 :     }
1943 :    
1944 :     emms();
1945 :    
1946 : edgomez 1.102 BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1947 :     frame->length = (BitstreamPos(bs) - bits) / 8;
1948 : edgomez 1.91
1949 :     #ifdef BFRAMES_DEC_DEBUG
1950 :     if (!first){
1951 :     first=1;
1952 :     if (fp)
1953 :     fclose(fp);
1954 :     }
1955 :     #endif
1956 : suxen_drol 1.24 }

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