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

Annotation of /trunk/xvidcore/src/encoder.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1677 - (view) (download)

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

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