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

Annotation of /xvidcore/src/encoder.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.138 - (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 : Isibaar 1.131 * Copyright(C) 2002-2010 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.138 * $Id: encoder.c,v 1.137 2010/12/29 22:39:35 Isibaar 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 : syskin 1.126 # include "motion/motion_smp.h"
53 :    
54 :    
55 : edgomez 1.40 /*****************************************************************************
56 :     * Local function prototypes
57 :     ****************************************************************************/
58 :    
59 : edgomez 1.39 static int FrameCodeI(Encoder * pEnc,
60 : edgomez 1.102 Bitstream * bs);
61 : edgomez 1.39
62 :     static int FrameCodeP(Encoder * pEnc,
63 : syskin 1.104 Bitstream * bs);
64 : Isibaar 1.1
65 : edgomez 1.91 static void FrameCodeB(Encoder * pEnc,
66 :     FRAMEINFO * frame,
67 : edgomez 1.102 Bitstream * bs);
68 : Isibaar 1.1
69 :    
70 : edgomez 1.40 /*****************************************************************************
71 :     * Encoder creation
72 :     *
73 :     * This function creates an Encoder instance, it allocates all necessary
74 : edgomez 1.91 * image buffers (reference, current and bframes) and initialize the internal
75 :     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
76 : edgomez 1.40 *
77 :     * The code seems to be very long but is very basic, mainly memory allocation
78 :     * and cleaning code.
79 :     *
80 :     * Returned values :
81 : edgomez 1.102 * - 0 - no errors
82 : syskin 1.96 * - XVID_ERR_MEMORY - the libc could not allocate memory, the function
83 :     * cleans the structure before exiting.
84 :     * pParam->handle is also set to NULL.
85 : edgomez 1.40 *
86 :     ****************************************************************************/
87 :    
88 : edgomez 1.102 /*
89 :     * Simplify the "fincr/fbase" fraction
90 :     */
91 : Skal 1.118 static int
92 :     gcd(int a, int b)
93 :     {
94 :     int r ;
95 :    
96 :     if (b > a) {
97 :     r = a;
98 :     a = b;
99 :     b = r;
100 :     }
101 :    
102 :     while ((r = a % b)) {
103 :     a = b;
104 :     b = r;
105 :     }
106 :     return b;
107 :     }
108 :    
109 : edgomez 1.102 static void
110 :     simplify_time(int *inc, int *base)
111 : Isibaar 1.1 {
112 : edgomez 1.102 /* common factor */
113 : Skal 1.118 const int s = gcd(*inc, *base);
114 :     *inc /= s;
115 :     *base /= s;
116 : Isibaar 1.1
117 : edgomez 1.106 if (*base > 65535 || *inc > 65535) {
118 :     int *biggest;
119 :     int *other;
120 : edgomez 1.107 float div;
121 : edgomez 1.108
122 : edgomez 1.106 if (*base > *inc) {
123 :     biggest = base;
124 :     other = inc;
125 :     } else {
126 :     biggest = inc;
127 :     other = base;
128 :     }
129 :    
130 : edgomez 1.107 div = ((float)*biggest)/((float)65535);
131 : Skal 1.118 *biggest = (unsigned int)(((float)*biggest)/div);
132 :     *other = (unsigned int)(((float)*other)/div);
133 : Isibaar 1.1 }
134 : edgomez 1.102 }
135 : Isibaar 1.1
136 : edgomez 1.39
137 : edgomez 1.102 int
138 :     enc_create(xvid_enc_create_t * create)
139 :     {
140 :     Encoder *pEnc;
141 : syskin 1.126 int n;
142 : edgomez 1.39
143 : edgomez 1.102 if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */
144 :     return XVID_ERR_VERSION;
145 : Isibaar 1.1
146 : edgomez 1.102 if (create->width%2 || create->height%2)
147 :     return XVID_ERR_FAIL;
148 : Isibaar 1.1
149 : syskin 1.103 if (create->width<=0 || create->height<=0)
150 :     return XVID_ERR_FAIL;
151 :    
152 : edgomez 1.102 /* allocate encoder struct */
153 : edgomez 1.41
154 : edgomez 1.39 pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
155 :     if (pEnc == NULL)
156 : Isibaar 1.1 return XVID_ERR_MEMORY;
157 : edgomez 1.42 memset(pEnc, 0, sizeof(Encoder));
158 :    
159 : edgomez 1.102 pEnc->mbParam.profile = create->profile;
160 : Isibaar 1.1
161 : edgomez 1.102 /* global flags */
162 :     pEnc->mbParam.global_flags = create->global;
163 : suxen_drol 1.117 if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED))
164 :     pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA;
165 : Isibaar 1.1
166 : edgomez 1.102 /* width, height */
167 :     pEnc->mbParam.width = create->width;
168 :     pEnc->mbParam.height = create->height;
169 : Isibaar 1.1 pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;
170 :     pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;
171 : edgomez 1.41 pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
172 :     pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
173 : Isibaar 1.1
174 : edgomez 1.102 /* framerate */
175 :     pEnc->mbParam.fincr = MAX(create->fincr, 0);
176 :     pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
177 :     if (pEnc->mbParam.fincr>0)
178 : Skal 1.118 simplify_time((int*)&pEnc->mbParam.fincr, (int*)&pEnc->mbParam.fbase);
179 : Isibaar 1.119
180 : edgomez 1.102 /* zones */
181 :     if(create->num_zones > 0) {
182 :     pEnc->num_zones = create->num_zones;
183 :     pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE);
184 :     if (pEnc->zones == NULL)
185 :     goto xvid_err_memory0;
186 :     memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones);
187 :     } else {
188 :     pEnc->num_zones = 0;
189 :     pEnc->zones = NULL;
190 :     }
191 :    
192 :     /* plugins */
193 :     if(create->num_plugins > 0) {
194 :     pEnc->num_plugins = create->num_plugins;
195 :     pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);
196 :     if (pEnc->plugins == NULL)
197 :     goto xvid_err_memory0;
198 :     } else {
199 :     pEnc->num_plugins = 0;
200 :     pEnc->plugins = NULL;
201 :     }
202 :    
203 :     for (n=0; n<pEnc->num_plugins;n++) {
204 :     xvid_plg_create_t pcreate;
205 :     xvid_plg_info_t pinfo;
206 :    
207 :     memset(&pinfo, 0, sizeof(xvid_plg_info_t));
208 :     pinfo.version = XVID_VERSION;
209 : suxen_drol 1.120 if (create->plugins[n].func(NULL, XVID_PLG_INFO, &pinfo, NULL) >= 0) {
210 : edgomez 1.102 pEnc->mbParam.plugin_flags |= pinfo.flags;
211 :     }
212 :    
213 :     memset(&pcreate, 0, sizeof(xvid_plg_create_t));
214 :     pcreate.version = XVID_VERSION;
215 :     pcreate.num_zones = pEnc->num_zones;
216 :     pcreate.zones = pEnc->zones;
217 :     pcreate.width = pEnc->mbParam.width;
218 :     pcreate.height = pEnc->mbParam.height;
219 :     pcreate.mb_width = pEnc->mbParam.mb_width;
220 :     pcreate.mb_height = pEnc->mbParam.mb_height;
221 :     pcreate.fincr = pEnc->mbParam.fincr;
222 :     pcreate.fbase = pEnc->mbParam.fbase;
223 :     pcreate.param = create->plugins[n].param;
224 :    
225 :     pEnc->plugins[n].func = NULL; /* disable plugins that fail */
226 : suxen_drol 1.120 if (create->plugins[n].func(NULL, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
227 : edgomez 1.102 pEnc->plugins[n].func = create->plugins[n].func;
228 :     }
229 :     }
230 :    
231 :     if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) ||
232 :     (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
233 :     pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */
234 :     }
235 : Isibaar 1.43
236 : edgomez 1.102 /* temp dquants */
237 :     if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
238 :     pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
239 :     pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
240 :     if (pEnc->temp_dquants==NULL)
241 :     goto xvid_err_memory1a;
242 :     }
243 : suxen_drol 1.32
244 : syskin 1.121 /* temp lambdas */
245 :     if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
246 :     pEnc->temp_lambda = (float *) xvid_malloc(pEnc->mbParam.mb_width *
247 :     pEnc->mbParam.mb_height * 6 * sizeof(float), CACHE_LINE);
248 :     if (pEnc->temp_lambda == NULL)
249 :     goto xvid_err_memory1a;
250 :     }
251 :    
252 : edgomez 1.102 /* bframes */
253 :     pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
254 :     pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
255 :     pEnc->mbParam.bquant_offset = create->bquant_offset;
256 : Isibaar 1.1
257 : edgomez 1.102 /* min/max quant */
258 :     for (n=0; n<3; n++) {
259 :     pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2;
260 :     pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31;
261 :     }
262 : Isibaar 1.1
263 : edgomez 1.102 /* frame drop ratio */
264 :     pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
265 : Isibaar 1.1
266 : edgomez 1.102 /* max keyframe interval */
267 :     pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval;
268 : Isibaar 1.1
269 : edgomez 1.102 /* allocate working frame-image memory */
270 : h 1.16
271 : edgomez 1.39 pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
272 :     pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
273 :    
274 : edgomez 1.41 if (pEnc->current == NULL || pEnc->reference == NULL)
275 : edgomez 1.39 goto xvid_err_memory1;
276 : suxen_drol 1.27
277 : edgomez 1.102 /* allocate macroblock memory */
278 : h 1.16
279 : edgomez 1.41 pEnc->current->mbs =
280 :     xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
281 :     pEnc->mbParam.mb_height, CACHE_LINE);
282 :     pEnc->reference->mbs =
283 :     xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
284 :     pEnc->mbParam.mb_height, CACHE_LINE);
285 : Isibaar 1.1
286 : edgomez 1.39 if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
287 :     goto xvid_err_memory2;
288 : suxen_drol 1.27
289 : edgomez 1.102 /* allocate quant matrix memory */
290 :    
291 :     pEnc->mbParam.mpeg_quant_matrices =
292 :     xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
293 :    
294 :     if (pEnc->mbParam.mpeg_quant_matrices == NULL)
295 :     goto xvid_err_memory2a;
296 :    
297 :     /* allocate interpolation image memory */
298 : suxen_drol 1.27
299 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
300 : edgomez 1.91 image_null(&pEnc->sOriginal);
301 : edgomez 1.102 image_null(&pEnc->sOriginal2);
302 :     }
303 : edgomez 1.91
304 :     image_null(&pEnc->f_refh);
305 :     image_null(&pEnc->f_refv);
306 :     image_null(&pEnc->f_refhv);
307 :    
308 : suxen_drol 1.27 image_null(&pEnc->current->image);
309 :     image_null(&pEnc->reference->image);
310 :     image_null(&pEnc->vInterH);
311 :     image_null(&pEnc->vInterV);
312 :     image_null(&pEnc->vInterHV);
313 : edgomez 1.39
314 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
315 :     if (image_create
316 : edgomez 1.91 (&pEnc->sOriginal, pEnc->mbParam.edged_width,
317 :     pEnc->mbParam.edged_height) < 0)
318 :     goto xvid_err_memory3;
319 : edgomez 1.102
320 :     if (image_create
321 :     (&pEnc->sOriginal2, pEnc->mbParam.edged_width,
322 :     pEnc->mbParam.edged_height) < 0)
323 :     goto xvid_err_memory3;
324 : edgomez 1.91 }
325 :    
326 :     if (image_create
327 :     (&pEnc->f_refh, pEnc->mbParam.edged_width,
328 :     pEnc->mbParam.edged_height) < 0)
329 :     goto xvid_err_memory3;
330 :     if (image_create
331 :     (&pEnc->f_refv, pEnc->mbParam.edged_width,
332 :     pEnc->mbParam.edged_height) < 0)
333 :     goto xvid_err_memory3;
334 : edgomez 1.41 if (image_create
335 : edgomez 1.91 (&pEnc->f_refhv, pEnc->mbParam.edged_width,
336 : edgomez 1.41 pEnc->mbParam.edged_height) < 0)
337 : edgomez 1.39 goto xvid_err_memory3;
338 : edgomez 1.91
339 : edgomez 1.41 if (image_create
340 :     (&pEnc->current->image, pEnc->mbParam.edged_width,
341 :     pEnc->mbParam.edged_height) < 0)
342 : edgomez 1.39 goto xvid_err_memory3;
343 : edgomez 1.41 if (image_create
344 :     (&pEnc->reference->image, pEnc->mbParam.edged_width,
345 :     pEnc->mbParam.edged_height) < 0)
346 : edgomez 1.39 goto xvid_err_memory3;
347 : edgomez 1.41 if (image_create
348 :     (&pEnc->vInterH, pEnc->mbParam.edged_width,
349 :     pEnc->mbParam.edged_height) < 0)
350 : edgomez 1.39 goto xvid_err_memory3;
351 : edgomez 1.41 if (image_create
352 :     (&pEnc->vInterV, pEnc->mbParam.edged_width,
353 :     pEnc->mbParam.edged_height) < 0)
354 : edgomez 1.39 goto xvid_err_memory3;
355 : edgomez 1.41 if (image_create
356 :     (&pEnc->vInterHV, pEnc->mbParam.edged_width,
357 :     pEnc->mbParam.edged_height) < 0)
358 : edgomez 1.39 goto xvid_err_memory3;
359 : edgomez 1.91
360 :     /* Create full bitplane for GMC, this might be wasteful */
361 :     if (image_create
362 :     (&pEnc->vGMC, pEnc->mbParam.edged_width,
363 :     pEnc->mbParam.edged_height) < 0)
364 :     goto xvid_err_memory3;
365 :    
366 : edgomez 1.102 /* init bframe image buffers */
367 : edgomez 1.91
368 : edgomez 1.102 pEnc->bframenum_head = 0;
369 :     pEnc->bframenum_tail = 0;
370 :     pEnc->flush_bframes = 0;
371 :     pEnc->closed_bframenum = -1;
372 : edgomez 1.91
373 :     /* B Frames specific init */
374 :     pEnc->bframes = NULL;
375 :    
376 :     if (pEnc->mbParam.max_bframes > 0) {
377 :    
378 :     pEnc->bframes =
379 :     xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
380 :     CACHE_LINE);
381 :    
382 :     if (pEnc->bframes == NULL)
383 :     goto xvid_err_memory3;
384 :    
385 :     for (n = 0; n < pEnc->mbParam.max_bframes; n++)
386 :     pEnc->bframes[n] = NULL;
387 :    
388 :    
389 :     for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
390 :     pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
391 :    
392 :     if (pEnc->bframes[n] == NULL)
393 :     goto xvid_err_memory4;
394 :    
395 :     pEnc->bframes[n]->mbs =
396 :     xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
397 :     pEnc->mbParam.mb_height, CACHE_LINE);
398 :    
399 :     if (pEnc->bframes[n]->mbs == NULL)
400 :     goto xvid_err_memory4;
401 :    
402 :     image_null(&pEnc->bframes[n]->image);
403 :    
404 :     if (image_create
405 :     (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
406 :     pEnc->mbParam.edged_height) < 0)
407 :     goto xvid_err_memory4;
408 :    
409 :     }
410 :     }
411 :    
412 : edgomez 1.102 /* init incoming frame queue */
413 :     pEnc->queue_head = 0;
414 :     pEnc->queue_tail = 0;
415 :     pEnc->queue_size = 0;
416 : edgomez 1.91
417 : edgomez 1.102 pEnc->queue =
418 :     xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO),
419 :     CACHE_LINE);
420 : edgomez 1.91
421 : edgomez 1.102 if (pEnc->queue == NULL)
422 :     goto xvid_err_memory4;
423 : edgomez 1.91
424 : edgomez 1.102 for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
425 :     image_null(&pEnc->queue[n].image);
426 : edgomez 1.91
427 :    
428 : edgomez 1.102 for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
429 :     if (image_create
430 :     (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
431 :     pEnc->mbParam.edged_height) < 0)
432 :     goto xvid_err_memory5;
433 : edgomez 1.91 }
434 :    
435 : edgomez 1.102 /* timestamp stuff */
436 : edgomez 1.91
437 :     pEnc->mbParam.m_stamp = 0;
438 : Isibaar 1.131 pEnc->m_framenum = create->start_frame_num;
439 : edgomez 1.91 pEnc->current->stamp = 0;
440 :     pEnc->reference->stamp = 0;
441 : edgomez 1.39
442 : edgomez 1.102 /* other stuff */
443 :    
444 :     pEnc->iFrameNum = 0;
445 :     pEnc->fMvPrevSigma = -1;
446 : Isibaar 1.1
447 : Isibaar 1.134 /* slices */
448 :     pEnc->num_slices = MIN(MAX(1, create->num_slices), (int) pEnc->mbParam.mb_height);
449 :    
450 : syskin 1.126 /* multithreaded stuff */
451 :     if (create->num_threads > 0) {
452 : Isibaar 1.134 int t = MIN(create->num_threads, (int) (pEnc->mbParam.mb_height>>1)); /* at least two rows per thread */
453 :     int threads_per_slice = MAX(1, (t / pEnc->num_slices));
454 :     int rows_per_thread = (pEnc->mbParam.mb_height + threads_per_slice - 1) / threads_per_slice;
455 :    
456 : syskin 1.126 pEnc->num_threads = t;
457 : Isibaar 1.134 pEnc->smpData = xvid_malloc(t*sizeof(SMPData), CACHE_LINE);
458 :     if (!pEnc->smpData)
459 : syskin 1.126 goto xvid_err_nosmp;
460 : Isibaar 1.134
461 :     /* tmp bitstream buffer for slice coding */
462 :     pEnc->smpData[0].tmp_buffer = xvid_malloc(16*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height*sizeof(uint8_t), CACHE_LINE);
463 :     if (! pEnc->smpData[0].tmp_buffer) goto xvid_err_nosmp;
464 :    
465 : syskin 1.126 for (n = 0; n < t; n++) {
466 : Isibaar 1.134 int s = MIN(pEnc->num_threads, pEnc->num_slices);
467 :    
468 :     pEnc->smpData[n].complete_count_self =
469 : syskin 1.126 xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE);
470 :    
471 : Isibaar 1.134 if (!pEnc->smpData[n].complete_count_self)
472 : syskin 1.126 goto xvid_err_nosmp;
473 : Isibaar 1.134
474 :     if (n > 0 && n < s) {
475 :     pEnc->smpData[n].bs = (Bitstream *) xvid_malloc(sizeof(Bitstream), CACHE_LINE);
476 :     if (!pEnc->smpData[n].bs)
477 :     goto xvid_err_nosmp;
478 :    
479 :     pEnc->smpData[n].sStat = (Statistics *) xvid_malloc(sizeof(Statistics), CACHE_LINE);
480 :     if (!pEnc->smpData[n].sStat)
481 :     goto xvid_err_nosmp;
482 :    
483 :     pEnc->smpData[n].tmp_buffer = pEnc->smpData[0].tmp_buffer + 16*(((n-1)*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height)/s);
484 :     BitstreamInit(pEnc->smpData[n].bs, pEnc->smpData[n].tmp_buffer, 0);
485 :     }
486 :    
487 : syskin 1.126 if (n != 0)
488 : Isibaar 1.134 pEnc->smpData[n].complete_count_above =
489 :     pEnc->smpData[n-1].complete_count_self;
490 : syskin 1.126 }
491 : Isibaar 1.134 pEnc->smpData[0].complete_count_above =
492 :     pEnc->smpData[t-1].complete_count_self - 1;
493 : syskin 1.126
494 :     } else {
495 :     xvid_err_nosmp:
496 :     /* no SMP */
497 : Isibaar 1.134 if (pEnc->smpData) {
498 :     if (pEnc->smpData[0].tmp_buffer)
499 :     xvid_free(pEnc->smpData[0].tmp_buffer);
500 :     }
501 :     else {
502 :     pEnc->smpData = xvid_malloc(1*sizeof(SMPData), CACHE_LINE);
503 :     if (pEnc->smpData == NULL)
504 :     goto xvid_err_memory5;
505 :     }
506 :    
507 : syskin 1.126 create->num_threads = 0;
508 :     }
509 :    
510 : edgomez 1.102 create->handle = (void *) pEnc;
511 : Isibaar 1.1
512 : Isibaar 1.6 init_timer();
513 : edgomez 1.102 init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices);
514 : Isibaar 1.1
515 : edgomez 1.102 return 0; /* ok */
516 : edgomez 1.39
517 :     /*
518 :     * We handle all XVID_ERR_MEMORY here, this makes the code lighter
519 :     */
520 :    
521 : edgomez 1.91 xvid_err_memory5:
522 :    
523 : edgomez 1.102 for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
524 :     image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width,
525 : edgomez 1.91 pEnc->mbParam.edged_height);
526 :     }
527 : edgomez 1.102
528 :     xvid_free(pEnc->queue);
529 : edgomez 1.91
530 :     xvid_err_memory4:
531 :    
532 :     if (pEnc->mbParam.max_bframes > 0) {
533 : edgomez 1.102 int i;
534 : edgomez 1.91
535 :     for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
536 :    
537 :     if (pEnc->bframes[i] == NULL)
538 :     continue;
539 :    
540 :     image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
541 :     pEnc->mbParam.edged_height);
542 :     xvid_free(pEnc->bframes[i]->mbs);
543 :     xvid_free(pEnc->bframes[i]);
544 : syskin 1.96 }
545 : edgomez 1.91
546 :     xvid_free(pEnc->bframes);
547 :     }
548 :    
549 : edgomez 1.41 xvid_err_memory3:
550 : edgomez 1.91
551 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
552 :     image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
553 :     pEnc->mbParam.edged_height);
554 :     image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
555 : edgomez 1.91 pEnc->mbParam.edged_height);
556 :     }
557 :    
558 :     image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
559 :     pEnc->mbParam.edged_height);
560 :     image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
561 :     pEnc->mbParam.edged_height);
562 :     image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
563 : edgomez 1.41 pEnc->mbParam.edged_height);
564 : edgomez 1.39
565 : edgomez 1.41 image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
566 :     pEnc->mbParam.edged_height);
567 :     image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
568 :     pEnc->mbParam.edged_height);
569 :     image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
570 :     pEnc->mbParam.edged_height);
571 :     image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
572 :     pEnc->mbParam.edged_height);
573 :     image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
574 :     pEnc->mbParam.edged_height);
575 : edgomez 1.91
576 :     /* destroy GMC image */
577 :     image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
578 :     pEnc->mbParam.edged_height);
579 :    
580 : edgomez 1.102 xvid_err_memory2a:
581 :     xvid_free(pEnc->mbParam.mpeg_quant_matrices);
582 : edgomez 1.39
583 : edgomez 1.41 xvid_err_memory2:
584 : edgomez 1.39 xvid_free(pEnc->current->mbs);
585 :     xvid_free(pEnc->reference->mbs);
586 :    
587 : edgomez 1.41 xvid_err_memory1:
588 : edgomez 1.39 xvid_free(pEnc->current);
589 :     xvid_free(pEnc->reference);
590 : edgomez 1.102
591 :     xvid_err_memory1a:
592 :     if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
593 :     xvid_free(pEnc->temp_dquants);
594 :     }
595 :    
596 : syskin 1.121 if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
597 :     xvid_free(pEnc->temp_lambda);
598 :     }
599 :    
600 : edgomez 1.102 xvid_err_memory0:
601 :     for (n=0; n<pEnc->num_plugins;n++) {
602 :     if (pEnc->plugins[n].func) {
603 : suxen_drol 1.120 pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, NULL, NULL);
604 : edgomez 1.102 }
605 :     }
606 :     xvid_free(pEnc->plugins);
607 :    
608 :     xvid_free(pEnc->zones);
609 :    
610 : edgomez 1.39 xvid_free(pEnc);
611 :    
612 : edgomez 1.102 create->handle = NULL;
613 : edgomez 1.40
614 : edgomez 1.39 return XVID_ERR_MEMORY;
615 : Isibaar 1.1 }
616 :    
617 : edgomez 1.40 /*****************************************************************************
618 :     * Encoder destruction
619 :     *
620 :     * This function destroy the entire encoder structure created by a previous
621 : edgomez 1.102 * successful enc_create call.
622 : edgomez 1.40 *
623 :     * Returned values (for now only one returned value) :
624 : edgomez 1.102 * - 0 - no errors
625 : edgomez 1.40 *
626 :     ****************************************************************************/
627 :    
628 :     int
629 : edgomez 1.102 enc_destroy(Encoder * pEnc)
630 : Isibaar 1.1 {
631 : edgomez 1.91 int i;
632 : syskin 1.96
633 : edgomez 1.91 /* B Frames specific */
634 : edgomez 1.102 for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
635 :     image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
636 : edgomez 1.91 pEnc->mbParam.edged_height);
637 :     }
638 :    
639 : edgomez 1.102 xvid_free(pEnc->queue);
640 :    
641 : edgomez 1.91 if (pEnc->mbParam.max_bframes > 0) {
642 :    
643 :     for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
644 :    
645 :     if (pEnc->bframes[i] == NULL)
646 :     continue;
647 :    
648 :     image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
649 :     pEnc->mbParam.edged_height);
650 :     xvid_free(pEnc->bframes[i]->mbs);
651 :     xvid_free(pEnc->bframes[i]);
652 :     }
653 :    
654 :     xvid_free(pEnc->bframes);
655 : syskin 1.96
656 : edgomez 1.91 }
657 :    
658 : edgomez 1.39 /* All images, reference, current etc ... */
659 : edgomez 1.91
660 : edgomez 1.41 image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
661 :     pEnc->mbParam.edged_height);
662 :     image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
663 :     pEnc->mbParam.edged_height);
664 :     image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
665 :     pEnc->mbParam.edged_height);
666 :     image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
667 :     pEnc->mbParam.edged_height);
668 :     image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
669 :     pEnc->mbParam.edged_height);
670 : edgomez 1.91 image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
671 : edgomez 1.41 pEnc->mbParam.edged_height);
672 : edgomez 1.91 image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
673 :     pEnc->mbParam.edged_height);
674 :     image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
675 :     pEnc->mbParam.edged_height);
676 : edgomez 1.102 image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
677 :     pEnc->mbParam.edged_height);
678 : edgomez 1.91
679 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
680 :     image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
681 :     pEnc->mbParam.edged_height);
682 :     image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
683 : edgomez 1.91 pEnc->mbParam.edged_height);
684 :     }
685 : edgomez 1.39
686 :     /* Encoder structure */
687 : edgomez 1.91
688 : suxen_drol 1.27 xvid_free(pEnc->current->mbs);
689 :     xvid_free(pEnc->current);
690 :    
691 :     xvid_free(pEnc->reference->mbs);
692 :     xvid_free(pEnc->reference);
693 :    
694 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
695 :     xvid_free(pEnc->temp_dquants);
696 : edgomez 1.91 }
697 :    
698 : syskin 1.128 if ((pEnc->mbParam.plugin_flags & XVID_REQLAMBDA)) {
699 :     xvid_free(pEnc->temp_lambda);
700 :     }
701 : edgomez 1.91
702 : edgomez 1.102 if (pEnc->num_plugins>0) {
703 :     xvid_plg_destroy_t pdestroy;
704 :     memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t));
705 : edgomez 1.91
706 : edgomez 1.102 pdestroy.version = XVID_VERSION;
707 :     pdestroy.num_frames = pEnc->m_framenum;
708 : edgomez 1.91
709 : edgomez 1.102 for (i=0; i<pEnc->num_plugins;i++) {
710 :     if (pEnc->plugins[i].func) {
711 : suxen_drol 1.120 pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, NULL);
712 : edgomez 1.102 }
713 :     }
714 :     xvid_free(pEnc->plugins);
715 : edgomez 1.92 }
716 :    
717 : edgomez 1.102 xvid_free(pEnc->mbParam.mpeg_quant_matrices);
718 : edgomez 1.91
719 : syskin 1.126 if (pEnc->num_zones > 0)
720 : edgomez 1.102 xvid_free(pEnc->zones);
721 : edgomez 1.91
722 : syskin 1.126 if (pEnc->num_threads > 0) {
723 : Isibaar 1.134 for (i = 1; i < MAX(1, MIN(pEnc->num_threads, pEnc->num_slices)); i++) {
724 :     xvid_free(pEnc->smpData[i].bs);
725 :     xvid_free(pEnc->smpData[i].sStat);
726 :     }
727 :     if (pEnc->smpData[0].tmp_buffer) xvid_free(pEnc->smpData[0].tmp_buffer);
728 :    
729 : syskin 1.126 for (i = 0; i < pEnc->num_threads; i++)
730 : Isibaar 1.134 xvid_free(pEnc->smpData[i].complete_count_self);
731 : syskin 1.126 }
732 : Isibaar 1.134 xvid_free(pEnc->smpData);
733 : syskin 1.126
734 : edgomez 1.102 xvid_free(pEnc);
735 : edgomez 1.91
736 : edgomez 1.102 return 0; /* ok */
737 : edgomez 1.91 }
738 :    
739 :    
740 : edgomez 1.102 /*
741 :     call the plugins
742 :     */
743 : edgomez 1.91
744 : edgomez 1.102 static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
745 :     int opt, int * type, int * quant, xvid_enc_stats_t * stats)
746 : suxen_drol 1.44 {
747 : syskin 1.121 unsigned int i, j, k;
748 : edgomez 1.102 xvid_plg_data_t data;
749 : edgomez 1.91
750 : edgomez 1.102 /* set data struct */
751 : suxen_drol 1.44
752 : edgomez 1.102 memset(&data, 0, sizeof(xvid_plg_data_t));
753 :     data.version = XVID_VERSION;
754 : edgomez 1.91
755 : edgomez 1.102 /* find zone */
756 :     for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ;
757 :     data.zone = i>0 ? &pEnc->zones[i-1] : NULL;
758 :    
759 :     data.width = pEnc->mbParam.width;
760 :     data.height = pEnc->mbParam.height;
761 :     data.mb_width = pEnc->mbParam.mb_width;
762 :     data.mb_height = pEnc->mbParam.mb_height;
763 :     data.fincr = frame->fincr;
764 :     data.fbase = pEnc->mbParam.fbase;
765 :     data.bquant_ratio = pEnc->mbParam.bquant_ratio;
766 :     data.bquant_offset = pEnc->mbParam.bquant_offset;
767 :    
768 :     for (i=0; i<3; i++) {
769 :     data.min_quant[i] = pEnc->mbParam.min_quant[i];
770 :     data.max_quant[i] = pEnc->mbParam.max_quant[i];
771 :     }
772 :    
773 :     data.reference.csp = XVID_CSP_PLANAR;
774 :     data.reference.plane[0] = pEnc->reference->image.y;
775 :     data.reference.plane[1] = pEnc->reference->image.u;
776 :     data.reference.plane[2] = pEnc->reference->image.v;
777 :     data.reference.stride[0] = pEnc->mbParam.edged_width;
778 :     data.reference.stride[1] = pEnc->mbParam.edged_width/2;
779 :     data.reference.stride[2] = pEnc->mbParam.edged_width/2;
780 :    
781 :     data.current.csp = XVID_CSP_PLANAR;
782 :     data.current.plane[0] = frame->image.y;
783 :     data.current.plane[1] = frame->image.u;
784 :     data.current.plane[2] = frame->image.v;
785 :     data.current.stride[0] = pEnc->mbParam.edged_width;
786 :     data.current.stride[1] = pEnc->mbParam.edged_width/2;
787 :     data.current.stride[2] = pEnc->mbParam.edged_width/2;
788 :    
789 :     data.frame_num = frame->frame_num;
790 :    
791 :     if (opt == XVID_PLG_BEFORE) {
792 :     data.type = *type;
793 :     data.quant = *quant;
794 :    
795 :     data.vol_flags = frame->vol_flags;
796 :     data.vop_flags = frame->vop_flags;
797 :     data.motion_flags = frame->motion_flags;
798 :    
799 :     } else if (opt == XVID_PLG_FRAME) {
800 :     data.type = coding2type(frame->coding_type);
801 :     data.quant = frame->quant;
802 :    
803 :     if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
804 :     data.dquant = pEnc->temp_dquants;
805 :     data.dquant_stride = pEnc->mbParam.mb_width;
806 : syskin 1.125 memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int));
807 : edgomez 1.102 }
808 : syskin 1.121
809 :     if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
810 :     int block = 0;
811 : Isibaar 1.129 emms();
812 : syskin 1.121 data.lambda = pEnc->temp_lambda;
813 :     for(i = 0;i < pEnc->mbParam.mb_height; i++)
814 :     for(j = 0;j < pEnc->mbParam.mb_width; j++)
815 :     for (k = 0; k < 6; k++)
816 :     data.lambda[block++] = 1.0f;
817 :     }
818 :    
819 : edgomez 1.102 } else { /* XVID_PLG_AFTER */
820 :     if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
821 :     data.original.csp = XVID_CSP_PLANAR;
822 :     data.original.plane[0] = original->y;
823 :     data.original.plane[1] = original->u;
824 :     data.original.plane[2] = original->v;
825 :     data.original.stride[0] = pEnc->mbParam.edged_width;
826 :     data.original.stride[1] = pEnc->mbParam.edged_width/2;
827 :     data.original.stride[2] = pEnc->mbParam.edged_width/2;
828 :     }
829 : edgomez 1.91
830 : edgomez 1.102 if ((frame->vol_flags & XVID_VOL_EXTRASTATS) ||
831 :     (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
832 : edgomez 1.91
833 : edgomez 1.102 data.sse_y =
834 :     plane_sse( original->y, frame->image.y,
835 :     pEnc->mbParam.edged_width, pEnc->mbParam.width,
836 :     pEnc->mbParam.height);
837 : edgomez 1.91
838 : edgomez 1.102 data.sse_u =
839 :     plane_sse( original->u, frame->image.u,
840 :     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
841 :     pEnc->mbParam.height/2);
842 : edgomez 1.91
843 : edgomez 1.102 data.sse_v =
844 :     plane_sse( original->v, frame->image.v,
845 :     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
846 :     pEnc->mbParam.height/2);
847 :     }
848 : edgomez 1.91
849 : edgomez 1.102 data.type = coding2type(frame->coding_type);
850 :     data.quant = frame->quant;
851 : edgomez 1.91
852 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
853 :     data.dquant = pEnc->temp_dquants;
854 :     data.dquant_stride = pEnc->mbParam.mb_width;
855 :    
856 :     for (j=0; j<pEnc->mbParam.mb_height; j++)
857 :     for (i=0; i<pEnc->mbParam.mb_width; i++) {
858 :     data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;
859 :     }
860 :     }
861 : edgomez 1.91
862 : edgomez 1.102 data.vol_flags = frame->vol_flags;
863 :     data.vop_flags = frame->vop_flags;
864 :     data.motion_flags = frame->motion_flags;
865 :    
866 :     data.length = frame->length;
867 :     data.kblks = frame->sStat.kblks;
868 :     data.mblks = frame->sStat.mblks;
869 :     data.ublks = frame->sStat.ublks;
870 :    
871 :     /* New code */
872 :     data.stats.type = coding2type(frame->coding_type);
873 :     data.stats.quant = frame->quant;
874 :     data.stats.vol_flags = frame->vol_flags;
875 :     data.stats.vop_flags = frame->vop_flags;
876 :     data.stats.length = frame->length;
877 :     data.stats.hlength = frame->length - (frame->sStat.iTextBits / 8);
878 :     data.stats.kblks = frame->sStat.kblks;
879 :     data.stats.mblks = frame->sStat.mblks;
880 :     data.stats.ublks = frame->sStat.ublks;
881 :     data.stats.sse_y = data.sse_y;
882 :     data.stats.sse_u = data.sse_u;
883 :     data.stats.sse_v = data.sse_v;
884 : edgomez 1.91
885 : edgomez 1.102 if (stats)
886 :     *stats = data.stats;
887 :     }
888 : edgomez 1.91
889 : edgomez 1.102 /* call plugins */
890 :     for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
891 :     emms();
892 :     if (pEnc->plugins[i].func) {
893 : suxen_drol 1.120 if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, NULL) < 0) {
894 : edgomez 1.102 continue;
895 :     }
896 :     }
897 :     }
898 :     emms();
899 : edgomez 1.91
900 : edgomez 1.102 /* copy modified values back into frame*/
901 :     if (opt == XVID_PLG_BEFORE) {
902 :     *type = data.type;
903 :     *quant = data.quant > 0 ? data.quant : 2; /* default */
904 :    
905 :     frame->vol_flags = data.vol_flags;
906 :     frame->vop_flags = data.vop_flags;
907 :     frame->motion_flags = data.motion_flags;
908 :    
909 :     } else if (opt == XVID_PLG_FRAME) {
910 : edgomez 1.91
911 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
912 :     for (j=0; j<pEnc->mbParam.mb_height; j++)
913 :     for (i=0; i<pEnc->mbParam.mb_width; i++) {
914 :     frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i];
915 :     }
916 :     } else {
917 :     for (j=0; j<pEnc->mbParam.mb_height; j++)
918 :     for (i=0; i<pEnc->mbParam.mb_width; i++) {
919 :     frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
920 :     }
921 :     }
922 : syskin 1.121
923 :     if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
924 :     for (j = 0; j < pEnc->mbParam.mb_height; j++)
925 :     for (i = 0; i < pEnc->mbParam.mb_width; i++)
926 :     for (k = 0; k < 6; k++) {
927 :     frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] =
928 :     (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]);
929 : Isibaar 1.133 }
930 : syskin 1.121 } else {
931 :     for (j = 0; j<pEnc->mbParam.mb_height; j++)
932 :     for (i = 0; i<pEnc->mbParam.mb_width; i++)
933 :     for (k = 0; k < 6; k++) {
934 :     frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP;
935 : Isibaar 1.133 }
936 : syskin 1.121 }
937 :    
938 :    
939 : edgomez 1.102 frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
940 :     }
941 : edgomez 1.91
942 :    
943 : edgomez 1.102 }
944 : edgomez 1.91
945 :    
946 : edgomez 1.102 static __inline void inc_frame_num(Encoder * pEnc)
947 :     {
948 :     pEnc->current->frame_num = pEnc->m_framenum;
949 :     pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */
950 : edgomez 1.91
951 : edgomez 1.102 pEnc->mbParam.m_stamp += pEnc->current->fincr;
952 :     pEnc->m_framenum++; /* debug ticker */
953 :     }
954 :    
955 :     static __inline void dec_frame_num(Encoder * pEnc)
956 :     {
957 :     pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr;
958 :     pEnc->m_framenum--; /* debug ticker */
959 :     }
960 : edgomez 1.91
961 : edgomez 1.102 static __inline void
962 :     MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam)
963 :     {
964 :     if (pMB->cbp == 0) {
965 :     /* we want to code dquant but the quantizer value will not be used yet
966 :     let's find out if we can postpone dquant to next MB
967 :     */
968 :     if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) {
969 :     pMB->dquant = 0; /* it's the last MB of all, the easiest case */
970 :     return;
971 :     } else {
972 :     MACROBLOCK * next = pMB + 1;
973 :     const MACROBLOCK * prev = pMB - 1;
974 :     if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED)
975 :     /* mode allows dquant change in the future */
976 :     if (abs(next->quant - prev->quant) <= 2) {
977 :     /* quant change is not out of range */
978 :     pMB->quant = prev->quant;
979 :     pMB->dquant = 0;
980 :     next->dquant = next->quant - prev->quant;
981 :     return;
982 :     }
983 :     }
984 :     }
985 :     /* couldn't skip this dquant */
986 :     pMB->mode = MODE_INTER_Q;
987 :     }
988 :    
989 : edgomez 1.91
990 :    
991 : edgomez 1.102 static __inline void
992 :     set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
993 :     {
994 : edgomez 1.91
995 : edgomez 1.102 pCur->ticks = (int32_t)pCur->stamp % time_base;
996 :     pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ;
997 : edgomez 1.91
998 : edgomez 1.102 #if 0 /* HEAVY DEBUG OUTPUT */
999 :     fprintf(stderr,"WriteVop: %d - %d \n",
1000 :     ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
1001 :     fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n",
1002 :     pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
1003 :     fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n",
1004 :     pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
1005 :     #endif
1006 :     }
1007 : edgomez 1.91
1008 : edgomez 1.102 static void
1009 :     simplify_par(int *par_width, int *par_height)
1010 :     {
1011 : edgomez 1.91
1012 : edgomez 1.102 int _par_width = (!*par_width) ? 1 : (*par_width<0) ? -*par_width: *par_width;
1013 :     int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height;
1014 :     int divisor = gcd(_par_width, _par_height);
1015 :    
1016 :     _par_width /= divisor;
1017 :     _par_height /= divisor;
1018 :    
1019 :     /* 2^8 precision maximum */
1020 :     if (_par_width>255 || _par_height>255) {
1021 :     float div;
1022 : edgomez 1.91 emms();
1023 : edgomez 1.102 if (_par_width>_par_height)
1024 :     div = (float)_par_width/255;
1025 :     else
1026 :     div = (float)_par_height/255;
1027 : edgomez 1.91
1028 : edgomez 1.102 _par_width = (int)((float)_par_width/div);
1029 :     _par_height = (int)((float)_par_height/div);
1030 : edgomez 1.91 }
1031 :    
1032 : edgomez 1.102 *par_width = _par_width;
1033 :     *par_height = _par_height;
1034 : edgomez 1.91
1035 : edgomez 1.102 return;
1036 :     }
1037 : edgomez 1.91
1038 : edgomez 1.102 /*****************************************************************************
1039 :     * IPB frame encoder entry point
1040 :     *
1041 :     * Returned values :
1042 :     * - >0 - output bytes
1043 :     * - 0 - no output
1044 :     * - XVID_ERR_VERSION - wrong version passed to core
1045 :     * - XVID_ERR_END - End of stream reached before end of coding
1046 :     * - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1047 :     * format
1048 :     ****************************************************************************/
1049 : edgomez 1.91
1050 :    
1051 : edgomez 1.102 int
1052 :     enc_encode(Encoder * pEnc,
1053 :     xvid_enc_frame_t * xFrame,
1054 :     xvid_enc_stats_t * stats)
1055 :     {
1056 :     xvid_enc_frame_t * frame;
1057 :     int type;
1058 :     Bitstream bs;
1059 : Isibaar 1.97
1060 : edgomez 1.102 if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1)) /* v1.x.x */
1061 :     return XVID_ERR_VERSION;
1062 : Isibaar 1.97
1063 : edgomez 1.102 xFrame->out_flags = 0;
1064 : edgomez 1.91
1065 : edgomez 1.102 start_global_timer();
1066 :     BitstreamInit(&bs, xFrame->bitstream, 0);
1067 : edgomez 1.91
1068 :    
1069 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1070 :     * enqueue image to the encoding-queue
1071 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1072 : edgomez 1.91
1073 : edgomez 1.102 if (xFrame->input.csp != XVID_CSP_NULL)
1074 : edgomez 1.91 {
1075 : edgomez 1.102 QUEUEINFO * q = &pEnc->queue[pEnc->queue_tail];
1076 : edgomez 1.91
1077 :     start_timer();
1078 :     if (image_input
1079 : edgomez 1.102 (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
1080 :     pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
1081 :     xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING))
1082 : edgomez 1.91 {
1083 :     emms();
1084 :     return XVID_ERR_FORMAT;
1085 :     }
1086 :     stop_conv_timer();
1087 :    
1088 : edgomez 1.102 if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1089 :     image_chroma_optimize(&q->image,
1090 : edgomez 1.92 pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1091 :     }
1092 :    
1093 : edgomez 1.102 q->frame = *xFrame;
1094 :    
1095 :     if (xFrame->quant_intra_matrix)
1096 : edgomez 1.91 {
1097 : edgomez 1.102 memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char));
1098 :     q->frame.quant_intra_matrix = q->quant_intra_matrix;
1099 : edgomez 1.91 }
1100 :    
1101 : edgomez 1.102 if (xFrame->quant_inter_matrix)
1102 :     {
1103 :     memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char));
1104 :     q->frame.quant_inter_matrix = q->quant_inter_matrix;
1105 : edgomez 1.91 }
1106 :    
1107 : edgomez 1.102 pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1);
1108 :     pEnc->queue_size++;
1109 : edgomez 1.91 }
1110 :    
1111 :    
1112 :     /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1113 : edgomez 1.102 * bframe flush code
1114 : edgomez 1.91 * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1115 :    
1116 : edgomez 1.102 repeat:
1117 : edgomez 1.91
1118 : edgomez 1.102 if (pEnc->flush_bframes)
1119 :     {
1120 :     if (pEnc->bframenum_head < pEnc->bframenum_tail) {
1121 : edgomez 1.91
1122 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1123 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1124 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1125 : edgomez 1.91
1126 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1127 :     image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image,
1128 :     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1129 : edgomez 1.91 }
1130 :    
1131 : edgomez 1.102 FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1132 : suxen_drol 1.120 call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, NULL, NULL, stats);
1133 : edgomez 1.102 pEnc->bframenum_head++;
1134 : edgomez 1.91
1135 : edgomez 1.102 goto done;
1136 :     }
1137 : edgomez 1.91
1138 : edgomez 1.102 /* write an empty marker to the bitstream.
1139 : edgomez 1.91
1140 : edgomez 1.102 for divx5 decoder compatibility, this marker must consist
1141 :     of a not-coded p-vop, with a time_base of zero, and time_increment
1142 :     indentical to the future-referece frame.
1143 :     */
1144 : edgomez 1.91
1145 : edgomez 1.102 if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) {
1146 :     int tmp;
1147 :     int bits;
1148 : edgomez 1.91
1149 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1150 : edgomez 1.91 pEnc->bframenum_head, pEnc->bframenum_tail,
1151 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1152 :    
1153 : edgomez 1.102 bits = BitstreamPos(&bs);
1154 : edgomez 1.91
1155 : edgomez 1.102 tmp = pEnc->current->seconds;
1156 :     pEnc->current->seconds = 0; /* force time_base = 0 */
1157 : edgomez 1.91
1158 : edgomez 1.102 BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
1159 :     BitstreamPad(&bs);
1160 :     pEnc->current->seconds = tmp;
1161 : edgomez 1.91
1162 : edgomez 1.102 /* add the not-coded length to the reference frame size */
1163 :     pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1164 : suxen_drol 1.120 call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1165 : edgomez 1.91
1166 : edgomez 1.102 /* flush complete: reset counters */
1167 :     pEnc->flush_bframes = 0;
1168 :     pEnc->bframenum_head = pEnc->bframenum_tail = 0;
1169 :     goto done;
1170 : edgomez 1.91
1171 :     }
1172 :    
1173 : edgomez 1.102 /* flush complete: reset counters */
1174 :     pEnc->flush_bframes = 0;
1175 :     pEnc->bframenum_head = pEnc->bframenum_tail = 0;
1176 :     }
1177 : edgomez 1.91
1178 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1179 :     * dequeue frame from the encoding queue
1180 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1181 : edgomez 1.91
1182 : edgomez 1.102 if (pEnc->queue_size == 0) /* empty */
1183 :     {
1184 :     if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
1185 :     {
1186 : edgomez 1.91
1187 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1188 : edgomez 1.91 pEnc->bframenum_head, pEnc->bframenum_tail,
1189 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1190 :    
1191 : edgomez 1.102 if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
1192 : suxen_drol 1.120 call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1193 : edgomez 1.102 }
1194 : edgomez 1.91
1195 : edgomez 1.102 /* if the very last frame is to be b-vop, we must change it to a p-vop */
1196 :     if (pEnc->bframenum_tail > 0) {
1197 : edgomez 1.91
1198 : edgomez 1.102 SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1199 :     pEnc->bframenum_tail--;
1200 :     SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1201 : edgomez 1.91
1202 : edgomez 1.102 /* convert B-VOP to P-VOP */
1203 :     pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1204 :     pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1205 :     pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1206 : edgomez 1.91
1207 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1208 :     image_copy(&pEnc->sOriginal, &pEnc->current->image,
1209 :     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1210 :     }
1211 : syskin 1.96
1212 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1213 : edgomez 1.91 pEnc->bframenum_head, pEnc->bframenum_tail,
1214 : edgomez 1.102 pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1215 : syskin 1.103 pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */
1216 : edgomez 1.91
1217 : syskin 1.104 FrameCodeP(pEnc, &bs);
1218 : edgomez 1.91
1219 :    
1220 : edgomez 1.102 if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1221 : suxen_drol 1.120 call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1222 : edgomez 1.102 }else{
1223 :     pEnc->flush_bframes = 1;
1224 :     goto done;
1225 :     }
1226 :     }
1227 :     DPRINTF(XVID_DEBUG_DEBUG, "*** END\n");
1228 : edgomez 1.91
1229 : edgomez 1.102 emms();
1230 :     return XVID_ERR_END; /* end of stream reached */
1231 : edgomez 1.91 }
1232 : edgomez 1.102 goto done; /* nothing to encode yet; encoder lag */
1233 : edgomez 1.91 }
1234 :    
1235 : edgomez 1.102 /* the current FRAME becomes the reference */
1236 :     SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1237 : edgomez 1.91
1238 : edgomez 1.102 /* remove frame from encoding-queue (head), and move it into the current */
1239 :     image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1240 :     frame = &pEnc->queue[pEnc->queue_head].frame;
1241 :     pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);
1242 :     pEnc->queue_size--;
1243 : edgomez 1.91
1244 : Isibaar 1.1
1245 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1246 :     * init pEnc->current fields
1247 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1248 : Isibaar 1.1
1249 : edgomez 1.102 pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1250 : edgomez 1.91 inc_frame_num(pEnc);
1251 : edgomez 1.102 pEnc->current->vol_flags = frame->vol_flags;
1252 :     pEnc->current->vop_flags = frame->vop_flags;
1253 :     pEnc->current->motion_flags = frame->motion;
1254 :     pEnc->current->fcode = pEnc->mbParam.m_fcode;
1255 :     pEnc->current->bcode = pEnc->mbParam.m_fcode;
1256 : edgomez 1.91
1257 : edgomez 1.92
1258 : edgomez 1.102 if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1259 : syskin 1.96 image_chroma_optimize(&pEnc->current->image,
1260 : edgomez 1.92 pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1261 :     }
1262 : Isibaar 1.1
1263 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1264 :     * frame type & quant selection
1265 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1266 :    
1267 :     type = frame->type;
1268 :     pEnc->current->quant = frame->quant;
1269 :    
1270 : Skal 1.118 call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats);
1271 : edgomez 1.102
1272 :     if (type > 0){ /* XVID_TYPE_?VOP */
1273 :     type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */
1274 :     } else{ /* XVID_TYPE_AUTO */
1275 :     if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){
1276 :     pEnc->iFrameNum = 0;
1277 :     type = I_VOP;
1278 :     }else{
1279 :     type = MEanalysis(&pEnc->reference->image, pEnc->current,
1280 :     &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1281 :     pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1282 :     (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
1283 :     }
1284 : edgomez 1.91 }
1285 : Isibaar 1.23
1286 : edgomez 1.102 if (type != I_VOP)
1287 :     pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */
1288 :    
1289 :     /* bframes buffer overflow check */
1290 :     if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1291 :     type = P_VOP;
1292 :     }
1293 : suxen_drol 1.27
1294 : edgomez 1.102 pEnc->iFrameNum++;
1295 : Isibaar 1.1
1296 : edgomez 1.102 if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1297 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1298 :     "%d st:%lld if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1299 : Isibaar 1.1 }
1300 :    
1301 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1302 :     * encode this frame as a b-vop
1303 :     * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)
1304 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1305 :     if (type == B_VOP) {
1306 :     if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1307 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1308 :     }
1309 :    
1310 :     if (frame->quant < 1) {
1311 :     pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1312 :     pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1313 : edgomez 1.91
1314 : edgomez 1.102 } else {
1315 :     pEnc->current->quant = frame->quant;
1316 :     }
1317 : edgomez 1.41
1318 : edgomez 1.102 if (pEnc->current->quant < 1)
1319 :     pEnc->current->quant = 1;
1320 :     else if (pEnc->current->quant > 31)
1321 :     pEnc->current->quant = 31;
1322 : edgomez 1.41
1323 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n",
1324 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1325 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1326 : edgomez 1.40
1327 : edgomez 1.102 /* store frame into bframe buffer & swap ref back to current */
1328 :     SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1329 :     SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1330 : edgomez 1.41
1331 : edgomez 1.102 pEnc->bframenum_tail++;
1332 : edgomez 1.40
1333 : edgomez 1.102 goto repeat;
1334 :     }
1335 : edgomez 1.40
1336 :    
1337 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1338 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1339 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1340 : edgomez 1.40
1341 : edgomez 1.102 /* for unpacked bframes, output the stats for the last encoded frame */
1342 :     if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)
1343 :     {
1344 :     if (pEnc->current->stamp > 0) {
1345 : suxen_drol 1.120 call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1346 : edgomez 1.40 }
1347 : Isibaar 1.130 else if (stats) {
1348 :     stats->type = XVID_TYPE_NOTHING;
1349 :     }
1350 : edgomez 1.102 }
1351 : edgomez 1.40
1352 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1353 :     * closed-gop
1354 :     * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe
1355 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1356 : Isibaar 1.1
1357 : edgomez 1.102 if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1358 : Isibaar 1.1
1359 : edgomez 1.102 /* place this frame back on the encoding-queue (head) */
1360 :     /* we will deal with it next time */
1361 :     dec_frame_num(pEnc);
1362 :     pEnc->iFrameNum--;
1363 : edgomez 1.13
1364 : edgomez 1.102 pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);
1365 :     pEnc->queue_size++;
1366 :     image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1367 : edgomez 1.41
1368 : edgomez 1.102 /* grab the last frame from the bframe-queue */
1369 : edgomez 1.41
1370 : edgomez 1.102 pEnc->bframenum_tail--;
1371 :     SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1372 : Isibaar 1.1
1373 : edgomez 1.102 if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1374 : suxen_drol 1.117 image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "CLOSED GOP BVOP->PVOP");
1375 : edgomez 1.40 }
1376 :    
1377 : edgomez 1.102 /* convert B-VOP quant to P-VOP */
1378 :     pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1379 :     pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1380 :     pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1381 :     type = P_VOP;
1382 : edgomez 1.3 }
1383 : Isibaar 1.1
1384 : edgomez 1.93
1385 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1386 :     * encode this frame as an i-vop
1387 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1388 : h 1.5
1389 : edgomez 1.102 if (type == I_VOP) {
1390 : edgomez 1.41
1391 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1392 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1393 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1394 : Isibaar 1.7
1395 : edgomez 1.102 if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1396 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1397 :     }
1398 : syskin 1.96
1399 : edgomez 1.102 pEnc->iFrameNum = 1;
1400 : Isibaar 1.1
1401 : edgomez 1.102 /* ---- update vol flags at IVOP ----------- */
1402 :     pEnc->mbParam.vol_flags = pEnc->current->vol_flags;
1403 : edgomez 1.41
1404 : edgomez 1.102 /* Aspect ratio */
1405 :     switch(frame->par) {
1406 :     case XVID_PAR_11_VGA:
1407 :     case XVID_PAR_43_PAL:
1408 :     case XVID_PAR_43_NTSC:
1409 :     case XVID_PAR_169_PAL:
1410 :     case XVID_PAR_169_NTSC:
1411 :     case XVID_PAR_EXT:
1412 :     pEnc->mbParam.par = frame->par;
1413 :     break;
1414 :     default:
1415 :     pEnc->mbParam.par = XVID_PAR_11_VGA;
1416 :     break;
1417 :     }
1418 : Isibaar 1.1
1419 : edgomez 1.102 /* For extended PAR only, we try to sanityse/simplify par values */
1420 :     if (pEnc->mbParam.par == XVID_PAR_EXT) {
1421 :     pEnc->mbParam.par_width = frame->par_width;
1422 :     pEnc->mbParam.par_height = frame->par_height;
1423 :     simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height);
1424 :     }
1425 : Isibaar 1.1
1426 : edgomez 1.102 if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1427 :     if (frame->quant_intra_matrix != NULL)
1428 :     set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix);
1429 :     if (frame->quant_inter_matrix != NULL)
1430 :     set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix);
1431 :     }
1432 : Isibaar 1.1
1433 : edgomez 1.102 /* prevent vol/vop misuse */
1434 : Isibaar 1.1
1435 : edgomez 1.102 if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1436 :     pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1437 : suxen_drol 1.27
1438 : edgomez 1.102 /* ^^^------------------------ */
1439 : edgomez 1.41
1440 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1441 :     image_copy(&pEnc->sOriginal, &pEnc->current->image,
1442 :     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1443 : Isibaar 1.1 }
1444 :    
1445 : edgomez 1.102 FrameCodeI(pEnc, &bs);
1446 :     xFrame->out_flags |= XVID_KEYFRAME;
1447 : Isibaar 1.1
1448 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1449 :     * encode this frame as an p-vop
1450 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1451 : Isibaar 1.1
1452 : edgomez 1.102 } else { /* (type == P_VOP || type == S_VOP) */
1453 : h 1.20
1454 : edgomez 1.102 DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n",
1455 :     pEnc->bframenum_head, pEnc->bframenum_tail,
1456 :     pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1457 : h 1.20
1458 : edgomez 1.102 if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1459 :     image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1460 :     }
1461 : h 1.20
1462 : edgomez 1.102 if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1463 :     image_copy(&pEnc->sOriginal, &pEnc->current->image,
1464 :     pEnc->mbParam.edged_width, pEnc->mbParam.height);
1465 :     }
1466 : h 1.20
1467 : syskin 1.104 if ( FrameCodeP(pEnc, &bs) == 0 ) {
1468 : syskin 1.103 /* N-VOP, we mustn't code b-frames yet */
1469 : Isibaar 1.124 if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) ||
1470 :     pEnc->mbParam.max_bframes == 0)
1471 :     call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1472 : syskin 1.103 goto done;
1473 :     }
1474 : h 1.20 }
1475 :    
1476 : edgomez 1.41
1477 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1478 :     * on next enc_encode call we must flush bframes
1479 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1480 : edgomez 1.41
1481 : edgomez 1.102 /*done_flush:*/
1482 : h 1.26
1483 : edgomez 1.102 pEnc->flush_bframes = 1;
1484 : h 1.26
1485 : edgomez 1.102 /* packed & queued_bframes: dont bother outputting stats here, we do so after the flush */
1486 :     if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1487 :     goto repeat;
1488 : h 1.20 }
1489 :    
1490 : edgomez 1.102 /* packed or no-bframes or no-bframes-queued: output stats */
1491 :     if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {
1492 : suxen_drol 1.120 call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1493 : edgomez 1.102 }
1494 : h 1.20
1495 : edgomez 1.102 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1496 :     * done; return number of bytes consumed
1497 :     * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1498 : h 1.20
1499 : edgomez 1.102 done:
1500 : h 1.20
1501 : edgomez 1.102 stop_global_timer();
1502 :     write_timer();
1503 : h 1.20
1504 : edgomez 1.102 emms();
1505 :     return BitstreamLength(&bs);
1506 :     }
1507 : h 1.20
1508 :    
1509 : edgomez 1.102 static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1510 :     {
1511 :     unsigned int i;
1512 :     MACROBLOCK * pMB = frame->mbs;
1513 :     int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1514 :     if (quant > 31)
1515 :     frame->quant = quant = 31;
1516 :     else if (quant < 1)
1517 :     frame->quant = quant = 1;
1518 :    
1519 :     for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) {
1520 :     quant += pMB->dquant;
1521 :     if (quant > 31)
1522 :     quant = 31;
1523 :     else if (quant < 1)
1524 :     quant = 1;
1525 :     pMB->quant = quant;
1526 :     pMB++;
1527 : h 1.20 }
1528 : edgomez 1.102 }
1529 : h 1.20
1530 :    
1531 : edgomez 1.102 static __inline void
1532 : Isibaar 1.134 CodeIntraMB(MACROBLOCK * pMB)
1533 : edgomez 1.102 {
1534 :     pMB->mode = MODE_INTRA;
1535 : h 1.20
1536 : edgomez 1.102 /* zero mv statistics */
1537 :     pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1538 :     pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1539 :     pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1540 :     pMB->sad16 = 0;
1541 : h 1.20
1542 : edgomez 1.102 if (pMB->dquant != 0) {
1543 :     pMB->mode = MODE_INTRA_Q;
1544 : h 1.20 }
1545 :     }
1546 :    
1547 : Isibaar 1.134 static void
1548 :     SliceCodeI(SMPData *data)
1549 :     {
1550 :     Encoder *pEnc = (Encoder *) data->pEnc;
1551 :     Bitstream *bs = (Bitstream *) data->bs;
1552 :    
1553 :     uint16_t x, y;
1554 :     int mb_width = pEnc->mbParam.mb_width;
1555 :     int mb_height = pEnc->mbParam.mb_height;
1556 :    
1557 :     int bound = 0, num_slices = pEnc->num_slices;
1558 :     FRAMEINFO *const current = pEnc->current;
1559 :    
1560 :     DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1561 :     DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1562 :    
1563 :     if (data->start_y > 0) { /* write resync marker */
1564 :     bound = data->start_y*mb_width;
1565 :     write_video_packet_header(bs, &pEnc->mbParam, current, bound);
1566 :     }
1567 :    
1568 :     for (y = data->start_y; y < data->stop_y; y++) {
1569 :     int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1570 :    
1571 :     if (new_bound > bound) {
1572 :     bound = new_bound;
1573 :     BitstreamPadAlways(bs);
1574 :     write_video_packet_header(bs, &pEnc->mbParam, current, bound);
1575 :     }
1576 :    
1577 :     for (x = 0; x < mb_width; x++) {
1578 :     MACROBLOCK *pMB = &current->mbs[x + y * mb_width];
1579 :    
1580 :     CodeIntraMB(pMB);
1581 :    
1582 :     MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1583 :     dct_codes, qcoeff);
1584 :    
1585 :     start_timer();
1586 :     MBPrediction(current, x, y, mb_width, qcoeff, bound);
1587 :     stop_prediction_timer();
1588 :    
1589 :     start_timer();
1590 :     MBCoding(current, pMB, qcoeff, bs, data->sStat);
1591 :     stop_coding_timer();
1592 :    
1593 :     }
1594 :     }
1595 :    
1596 :     emms();
1597 :     BitstreamPadAlways(bs);
1598 :     }
1599 :    
1600 :     static __inline void
1601 :     SerializeBitstreams(Encoder *pEnc, FRAMEINFO *current, Bitstream *bs, int num_threads)
1602 :     {
1603 :     int k;
1604 :     uint32_t pos = BitstreamLength(bs);
1605 :    
1606 :     for (k = 1; k < num_threads; k++) {
1607 :     uint32_t len = BitstreamLength(pEnc->smpData[k].bs);
1608 : h 1.20
1609 : Isibaar 1.134 memcpy((void *)((ptr_t)bs->start + pos),
1610 :     (void *)((ptr_t)pEnc->smpData[k].bs->start), len);
1611 :    
1612 : Isibaar 1.138 current->length += len;
1613 :     pos += len;
1614 : Isibaar 1.134
1615 :     /* collect stats */
1616 :     current->sStat.iTextBits += pEnc->smpData[k].sStat->iTextBits;
1617 :     current->sStat.kblks += pEnc->smpData[k].sStat->kblks;
1618 :     current->sStat.mblks += pEnc->smpData[k].sStat->mblks;
1619 :     current->sStat.ublks += pEnc->smpData[k].sStat->ublks;
1620 :     current->sStat.iMVBits += pEnc->smpData[k].sStat->iMVBits;
1621 :     }
1622 :    
1623 :     if (num_threads > 1) {
1624 :     uint32_t pos32 = pos>>2;
1625 :     bs->tail = bs->start + pos32;
1626 :     bs->pos = 8*(pos - (pos32<<2));
1627 :     bs->buf = 0;
1628 :    
1629 :     if (bs->pos > 0) {
1630 :     uint32_t pos8 = bs->pos/8;
1631 :     memset((void *)((ptr_t)bs->tail+pos8), 0, (4-pos8));
1632 :     pos = *bs->tail;
1633 :     #ifndef ARCH_IS_BIG_ENDIAN
1634 :     BSWAP(pos);
1635 :     #endif
1636 :     bs->buf = pos;
1637 :     }
1638 :     }
1639 :     }
1640 : edgomez 1.102
1641 : edgomez 1.41 static int
1642 :     FrameCodeI(Encoder * pEnc,
1643 : edgomez 1.102 Bitstream * bs)
1644 : h 1.21 {
1645 : edgomez 1.102 int bits = BitstreamPos(bs);
1646 : Isibaar 1.134 int bound = 0, num_slices = pEnc->num_slices;
1647 :     int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1648 :     int slices_per_thread = (num_slices*1024 / num_threads);
1649 : edgomez 1.91 int mb_height = pEnc->mbParam.mb_height;
1650 : Isibaar 1.134 void * status = NULL;
1651 :     uint16_t k;
1652 : h 1.21
1653 : suxen_drol 1.27 pEnc->mbParam.m_rounding_type = 1;
1654 :     pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1655 :     pEnc->current->coding_type = I_VOP;
1656 : h 1.21
1657 : edgomez 1.102 call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1658 :    
1659 :     SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1660 :    
1661 : Isibaar 1.134 BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current, num_slices);
1662 : edgomez 1.78
1663 : edgomez 1.91 set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1664 :    
1665 : edgomez 1.101 BitstreamPad(bs);
1666 : h 1.21
1667 : edgomez 1.102 BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1668 : h 1.21
1669 : edgomez 1.91 pEnc->current->sStat.iTextBits = 0;
1670 : h 1.21
1671 : Isibaar 1.134 /* multithreaded intra coding - dispatch threads */
1672 :     for (k = 0; k < num_threads; k++) {
1673 :     int add = ((slices_per_thread + 512) >> 10);
1674 : h 1.21
1675 : Isibaar 1.134 slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
1676 : h 1.21
1677 : Isibaar 1.134 pEnc->smpData[k].pEnc = (void *) pEnc;
1678 :     pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
1679 :     pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
1680 : h 1.21
1681 : Isibaar 1.134 bound += add;
1682 : h 1.21
1683 : Isibaar 1.134 if (k > 0) {
1684 :     BitstreamReset(pEnc->smpData[k].bs);
1685 :     pEnc->smpData[k].sStat->iTextBits = 0;
1686 : h 1.21 }
1687 : Isibaar 1.134 }
1688 :     pEnc->smpData[0].bs = bs;
1689 :     pEnc->smpData[0].sStat = &pEnc->current->sStat;
1690 :    
1691 :     /* create threads */
1692 :     for (k = 1; k < num_threads; k++) {
1693 :     pthread_create(&pEnc->smpData[k].handle, NULL,
1694 :     (void*)SliceCodeI, (void*)&pEnc->smpData[k]);
1695 :     }
1696 :    
1697 :     SliceCodeI(&pEnc->smpData[0]);
1698 :    
1699 :     /* wait until all threads are finished */
1700 :     for (k = 1; k < num_threads; k++) {
1701 :     pthread_join(pEnc->smpData[k].handle, &status);
1702 :     }
1703 : h 1.21
1704 : Isibaar 1.134 pEnc->current->length = BitstreamLength(bs) - (bits/8);
1705 : h 1.21
1706 : Isibaar 1.134 /* reassemble the pieces together */
1707 :     SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
1708 : edgomez 1.102
1709 : Isibaar 1.134 pEnc->current->sStat.iMVBits = 0;
1710 :     pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1711 :     pEnc->current->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1712 : edgomez 1.102
1713 : edgomez 1.91 pEnc->fMvPrevSigma = -1;
1714 : suxen_drol 1.27 pEnc->mbParam.m_fcode = 2;
1715 : h 1.21
1716 : edgomez 1.102 pEnc->current->is_edged = 0; /* not edged */
1717 :     pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1718 : h 1.21
1719 : edgomez 1.93 return 1; /* intra */
1720 : h 1.21 }
1721 :    
1722 : syskin 1.114 static __inline void
1723 :     updateFcode(Statistics * sStat, Encoder * pEnc)
1724 :     {
1725 :     float fSigma;
1726 :     int iSearchRange;
1727 :    
1728 :     if (sStat->iMvCount == 0)
1729 :     sStat->iMvCount = 1;
1730 :    
1731 :     fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
1732 :    
1733 :     iSearchRange = 16 << pEnc->mbParam.m_fcode;
1734 :    
1735 :     if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1736 :     pEnc->mbParam.m_fcode++;
1737 :    
1738 :     else if ((5.0 * fSigma < iSearchRange)
1739 :     && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1740 :     && (pEnc->mbParam.m_fcode >= 2) )
1741 :     pEnc->mbParam.m_fcode--;
1742 :    
1743 :     pEnc->fMvPrevSigma = fSigma;
1744 :     }
1745 : h 1.21
1746 : edgomez 1.91 #define BFRAME_SKIP_THRESHHOLD 30
1747 :    
1748 : Isibaar 1.134 static void
1749 :     SliceCodeP(SMPData *data)
1750 :     {
1751 :     Encoder *pEnc = (Encoder *) data->pEnc;
1752 :     Bitstream *bs = (Bitstream *) data->bs;
1753 :    
1754 :     int x, y, k;
1755 :     FRAMEINFO *const current = pEnc->current;
1756 :     FRAMEINFO *const reference = pEnc->reference;
1757 :     MBParam * const pParam = &pEnc->mbParam;
1758 :     int mb_width = pParam->mb_width;
1759 :     int mb_height = pParam->mb_height;
1760 :    
1761 :     DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1762 :     DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1763 :    
1764 :     int bound = 0, num_slices = pEnc->num_slices;
1765 :    
1766 :     if (data->start_y > 0) { /* write resync marker */
1767 :     bound = data->start_y*mb_width;
1768 :     write_video_packet_header(bs, pParam, current, bound);
1769 :     }
1770 :    
1771 :     for (y = data->start_y; y < data->stop_y; y++) {
1772 :     int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1773 :    
1774 :     if (new_bound > bound) {
1775 :     bound = new_bound;
1776 :     BitstreamPadAlways(bs);
1777 :     write_video_packet_header(bs, pParam, current, bound);
1778 :     }
1779 :    
1780 :     for (x = 0; x < mb_width; x++) {
1781 :     MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1782 :     int skip_possible;
1783 :    
1784 :     if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1785 :     CodeIntraMB(pMB);
1786 :     MBTransQuantIntra(pParam, current, pMB, x, y,
1787 :     dct_codes, qcoeff);
1788 :    
1789 :     start_timer();
1790 :     MBPrediction(current, x, y, pParam->mb_width, qcoeff, bound);
1791 :     stop_prediction_timer();
1792 :    
1793 :     data->sStat->kblks++;
1794 :    
1795 :     MBCoding(current, pMB, qcoeff, bs, data->sStat);
1796 :     stop_coding_timer();
1797 :     continue;
1798 :     }
1799 :    
1800 :     start_timer();
1801 :     MBMotionCompensation(pMB, x, y, &reference->image,
1802 :     &pEnc->vInterH, &pEnc->vInterV,
1803 :     &pEnc->vInterHV, &pEnc->vGMC,
1804 :     &current->image,
1805 :     dct_codes, pParam->width,
1806 :     pParam->height,
1807 :     pParam->edged_width,
1808 :     (current->vol_flags & XVID_VOL_QUARTERPEL),
1809 :     current->rounding_type,
1810 :     data->RefQ);
1811 :    
1812 :     stop_comp_timer();
1813 :    
1814 :     pMB->field_pred = 0;
1815 :    
1816 :     if (pMB->cbp != 0) {
1817 :     pMB->cbp = MBTransQuantInter(pParam, current, pMB, x, y,
1818 :     dct_codes, qcoeff);
1819 :     }
1820 :    
1821 :     if (pMB->dquant != 0)
1822 :     MBSetDquant(pMB, x, y, pParam);
1823 :    
1824 :    
1825 :     if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1826 :     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1827 :     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1828 :     data->sStat->mblks++;
1829 :     } else {
1830 :     data->sStat->ublks++;
1831 :     }
1832 :    
1833 :     start_timer();
1834 :    
1835 :     /* Finished processing the MB, now check if to CODE or SKIP */
1836 :    
1837 :     skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
1838 :    
1839 :     if (current->coding_type == S_VOP)
1840 :     skip_possible &= (pMB->mcsel == 1);
1841 :     else { /* PVOP */
1842 :     const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1843 :     pMB->qmvs : pMB->mvs;
1844 :     skip_possible &= ((mv->x|mv->y) == 0);
1845 :     }
1846 :    
1847 :     if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1848 :     /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1849 :     int bSkip = 1;
1850 :    
1851 :     if (current->coding_type == P_VOP) { /* special rule for P-VOP's SKIP */
1852 :     for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1853 :     int iSAD;
1854 :     iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1855 :     pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1856 :     pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1857 : Isibaar 1.135 if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant || ((bound > 1) &&
1858 :     ((y*mb_width+x == bound) || (y*mb_width+x == bound+1)))) { /* Some third-party decoders have problems with coloc skip MB before or after
1859 :     resync marker in BVOP. We avoid any ambiguity and force no skip at slice boundary */
1860 : Isibaar 1.134 bSkip = 0; /* could not SKIP */
1861 :     if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1862 :     VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1863 :     pMB->pmvs[0].x = - predMV.x;
1864 :     pMB->pmvs[0].y = - predMV.y;
1865 :     } else {
1866 :     VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
1867 :     pMB->pmvs[0].x = - predMV.x;
1868 :     pMB->pmvs[0].y = - predMV.y;
1869 :     }
1870 :     pMB->mode = MODE_INTER;
1871 :     pMB->cbp = 0;
1872 :     break;
1873 :     }
1874 :     }
1875 :     }
1876 :    
1877 :     if (bSkip) {
1878 :     /* do SKIP */
1879 :     pMB->mode = MODE_NOT_CODED;
1880 :     MBSkip(bs);
1881 :     stop_coding_timer();
1882 :     continue; /* next MB */
1883 :     }
1884 :     }
1885 :    
1886 :     /* ordinary case: normal coded INTER/INTER4V block */
1887 :     MBCoding(current, pMB, qcoeff, bs, data->sStat);
1888 :     stop_coding_timer();
1889 :     }
1890 :     }
1891 :    
1892 :     BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1893 :     emms();
1894 :     }
1895 :    
1896 : edgomez 1.91 /* FrameCodeP also handles S(GMC)-VOPs */
1897 : edgomez 1.41 static int
1898 : Isibaar 1.134 FrameCodeP(Encoder * pEnc, Bitstream * bs)
1899 : Isibaar 1.1 {
1900 : edgomez 1.102 int bits = BitstreamPos(bs);
1901 : edgomez 1.13
1902 : edgomez 1.102 FRAMEINFO *const current = pEnc->current;
1903 :     FRAMEINFO *const reference = pEnc->reference;
1904 :     MBParam * const pParam = &pEnc->mbParam;
1905 :     int mb_width = pParam->mb_width;
1906 :     int mb_height = pParam->mb_height;
1907 : syskin 1.103 int coded = 1;
1908 : syskin 1.96
1909 : Isibaar 1.134 int k = 0, bound = 0, num_slices = pEnc->num_slices;
1910 :     int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1911 :     void * status = NULL;
1912 :     int slices_per_thread = (num_slices*1024 / num_threads);
1913 :     int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
1914 :    
1915 : edgomez 1.102 IMAGE *pRef = &reference->image;
1916 :    
1917 :     if (!reference->is_edged) {
1918 :     start_timer();
1919 :     image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1920 :     pParam->width, pParam->height, 0);
1921 :     stop_edges_timer();
1922 :     reference->is_edged = 1;
1923 :     }
1924 :    
1925 :     pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1926 :     current->rounding_type = pParam->m_rounding_type;
1927 :     current->fcode = pParam->m_fcode;
1928 : Isibaar 1.1
1929 : edgomez 1.102 if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1930 :     if (reference->is_interpolated != current->rounding_type) {
1931 :     start_timer();
1932 : syskin 1.122 image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y,
1933 :     pEnc->vInterHV.y, pParam->edged_width,
1934 : edgomez 1.102 pParam->edged_height,
1935 :     (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1936 :     current->rounding_type);
1937 :     stop_inter_timer();
1938 :     reference->is_interpolated = current->rounding_type;
1939 :     }
1940 : Isibaar 1.1 }
1941 :    
1942 : syskin 1.111 current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1943 : Isibaar 1.123 current->sStat.kblks = current->sStat.mblks = current->sStat.ublks =
1944 :     current->sStat.iMVBits = 0;
1945 : syskin 1.111
1946 : edgomez 1.102 current->coding_type = P_VOP;
1947 :    
1948 : Isibaar 1.133 if (current->vop_flags & XVID_VOP_RD_PSNRHVSM) {
1949 :     image_block_variance(&current->image, pParam->edged_width, current->mbs,
1950 :     pParam->mb_width, pParam->mb_height);
1951 :     }
1952 :    
1953 : edgomez 1.102 call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1954 :    
1955 :     SetMacroblockQuants(&pEnc->mbParam, current);
1956 : syskin 1.96
1957 : Isibaar 1.1 start_timer();
1958 : Isibaar 1.134 if (current->vol_flags & XVID_VOL_GMC) /* GMC only for S(GMC)-VOPs */
1959 : edgomez 1.102 { int gmcval;
1960 :     current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1961 : Isibaar 1.134 &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, num_slices);
1962 : edgomez 1.102
1963 :     if (current->motion_flags & XVID_ME_GME_REFINE) {
1964 :     gmcval = GlobalMotionEstRefine(&current->warp,
1965 :     current->mbs, pParam,
1966 :     current, reference,
1967 :     &current->image,
1968 :     &reference->image,
1969 :     &pEnc->vInterH,
1970 :     &pEnc->vInterV,
1971 :     &pEnc->vInterHV);
1972 :     } else {
1973 :     gmcval = globalSAD(&current->warp, pParam, current->mbs,
1974 :     current,
1975 :     &reference->image,
1976 :     &current->image,
1977 :     pEnc->vGMC.y);
1978 :     }
1979 :    
1980 :     gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1981 :    
1982 :     /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1983 :     generate_GMCparameters( 3, 3, &current->warp,
1984 :     pParam->width, pParam->height,
1985 :     &current->new_gmc_data);
1986 :    
1987 :     if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
1988 :     (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
1989 :     {
1990 :     current->coding_type = S_VOP;
1991 :    
1992 :     generate_GMCimage(&current->new_gmc_data, &reference->image,
1993 :     pParam->mb_width, pParam->mb_height,
1994 :     pParam->edged_width, pParam->edged_width/2,
1995 :     pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1996 :     current->rounding_type, current->mbs, &pEnc->vGMC);
1997 :    
1998 :     } else {
1999 : edgomez 1.91
2000 : edgomez 1.102 generate_GMCimage(&current->new_gmc_data, &reference->image,
2001 :     pParam->mb_width, pParam->mb_height,
2002 :     pParam->edged_width, pParam->edged_width/2,
2003 :     pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
2004 :     current->rounding_type, current->mbs, NULL); /* no warping, just AMV */
2005 :     }
2006 :     }
2007 : Isibaar 1.1
2008 : Isibaar 1.134 if (pEnc->num_threads > 0) {
2009 : edgomez 1.91
2010 : syskin 1.126 /* multithreaded motion estimation - dispatch threads */
2011 : Isibaar 1.134 while (k < pEnc->num_threads) {
2012 :     int i, add_s = (slices_per_thread + 512) >> 10;
2013 :     int add_t = (threads_per_slice + 512) >> 10;
2014 :    
2015 :     int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2016 :     int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2017 :     int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2018 :    
2019 :     slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2020 :     threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2021 :    
2022 :     for (i = 0; i < add_t; i++) {
2023 :     memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
2024 :    
2025 :     pEnc->smpData[k+i].pEnc = (void *) pEnc;
2026 :     pEnc->smpData[k+i].y_row = i;
2027 :     pEnc->smpData[k+i].y_step = add_t;
2028 :     pEnc->smpData[k+i].stop_y = stop_y;
2029 :     pEnc->smpData[k+i].start_y = start_y;
2030 :    
2031 :     /* todo: sort out temp space once and for all */
2032 :     pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2033 :     16*((k+i)>>1)*pParam->edged_width;
2034 :     }
2035 :    
2036 :     pEnc->smpData[k].complete_count_above =
2037 :     pEnc->smpData[k+add_t-1].complete_count_self - 1;
2038 : syskin 1.127
2039 : Isibaar 1.134 bound += add_s;
2040 :     k += add_t;
2041 : syskin 1.126 }
2042 :    
2043 : syskin 1.127 for (k = 1; k < pEnc->num_threads; k++) {
2044 : Isibaar 1.134 pthread_create(&pEnc->smpData[k].handle, NULL,
2045 :     (void*)MotionEstimateSMP, (void*)&pEnc->smpData[k]);
2046 : syskin 1.126 }
2047 : syskin 1.127
2048 : Isibaar 1.134 MotionEstimateSMP(&pEnc->smpData[0]);
2049 : syskin 1.127
2050 :     for (k = 1; k < pEnc->num_threads; k++) {
2051 : Isibaar 1.134 pthread_join(pEnc->smpData[k].handle, &status);
2052 : syskin 1.127 }
2053 :    
2054 :     current->fcode = 0;
2055 :     for (k = 0; k < pEnc->num_threads; k++) {
2056 : Isibaar 1.134 current->sStat.iMvSum += pEnc->smpData[k].mvSum;
2057 :     current->sStat.iMvCount += pEnc->smpData[k].mvCount;
2058 :     if (pEnc->smpData[k].minfcode > current->fcode)
2059 :     current->fcode = pEnc->smpData[k].minfcode;
2060 : syskin 1.127 }
2061 :    
2062 : syskin 1.126 } else {
2063 : Isibaar 1.134
2064 : syskin 1.126 /* regular ME */
2065 :    
2066 :     MotionEstimation(&pEnc->mbParam, current, reference,
2067 :     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2068 : Isibaar 1.134 &pEnc->vGMC, 256*4096, num_slices);
2069 :    
2070 : syskin 1.126 }
2071 : edgomez 1.91
2072 : edgomez 1.102 stop_motion_timer();
2073 : edgomez 1.91
2074 : edgomez 1.102 set_timecodes(current,reference,pParam->fbase);
2075 : Isibaar 1.1
2076 : edgomez 1.102 BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
2077 : edgomez 1.3
2078 : Isibaar 1.134 /* multithreaded inter coding - dispatch threads */
2079 : edgomez 1.91
2080 : Isibaar 1.134 bound = 0;
2081 :     slices_per_thread = (num_slices*1024 / num_threads);
2082 : edgomez 1.91
2083 : Isibaar 1.134 for (k = 0; k < num_threads; k++) {
2084 :     int add = ((slices_per_thread + 512) >> 10);
2085 : edgomez 1.91
2086 : Isibaar 1.134 slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
2087 : edgomez 1.102
2088 : Isibaar 1.134 pEnc->smpData[k].pEnc = (void *) pEnc;
2089 :     pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2090 :     pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2091 :     pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
2092 : edgomez 1.102
2093 : Isibaar 1.134 bound += add;
2094 : edgomez 1.102
2095 : Isibaar 1.134 if (k > 0) {
2096 :     pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2097 :     pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks =
2098 :     pEnc->smpData[k].sStat->iMVBits = 0;
2099 :    
2100 :     BitstreamReset(pEnc->smpData[k].bs);
2101 :     }
2102 :     }
2103 :     pEnc->smpData[0].bs = bs;
2104 :     pEnc->smpData[0].sStat = &current->sStat;
2105 : chl 1.65
2106 : Isibaar 1.134 /* create threads */
2107 :     for (k = 1; k < num_threads; k++) {
2108 :     pthread_create(&pEnc->smpData[k].handle, NULL,
2109 :     (void*)SliceCodeP, (void*)&pEnc->smpData[k]);
2110 :     }
2111 : chl 1.65
2112 : Isibaar 1.134 SliceCodeP(&pEnc->smpData[0]);
2113 : edgomez 1.102
2114 : Isibaar 1.134 /* wait until all threads are finished */
2115 :     for (k = 1; k < num_threads; k++) {
2116 :     pthread_join(pEnc->smpData[k].handle, &status);
2117 :     }
2118 : edgomez 1.91
2119 : Isibaar 1.134 current->length = BitstreamLength(bs) - (bits/8);
2120 : edgomez 1.102
2121 : Isibaar 1.134 /* reassemble the pieces together */
2122 :     SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
2123 : Isibaar 1.1
2124 : syskin 1.114 updateFcode(&current->sStat, pEnc);
2125 : edgomez 1.91
2126 :     /* frame drop code */
2127 : edgomez 1.102 #if 0
2128 :     DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
2129 :     #endif
2130 : Isibaar 1.134
2131 : Isibaar 1.132 if (current->sStat.kblks + current->sStat.mblks <
2132 : syskin 1.105 (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
2133 :     ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )
2134 : edgomez 1.91 {
2135 : Isibaar 1.124 current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0;
2136 : edgomez 1.102 current->sStat.ublks = mb_width * mb_height;
2137 : edgomez 1.91
2138 :     BitstreamReset(bs);
2139 :    
2140 : edgomez 1.102 set_timecodes(current,reference,pParam->fbase);
2141 :     BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
2142 : edgomez 1.91
2143 : edgomez 1.93 /* copy reference frame details into the current frame */
2144 : edgomez 1.102 current->quant = reference->quant;
2145 :     current->motion_flags = reference->motion_flags;
2146 :     current->rounding_type = reference->rounding_type;
2147 :     current->fcode = reference->fcode;
2148 :     current->bcode = reference->bcode;
2149 : syskin 1.103 current->stamp = reference->stamp;
2150 : edgomez 1.102 image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
2151 :     memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2152 : syskin 1.103 coded = 0;
2153 :    
2154 : Isibaar 1.134 BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2155 :    
2156 :     current->length = (BitstreamPos(bs) - bits) / 8;
2157 :    
2158 : syskin 1.103 } else {
2159 : edgomez 1.102
2160 : syskin 1.103 pEnc->current->is_edged = 0; /* not edged */
2161 :     pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
2162 : edgomez 1.102
2163 : syskin 1.103 /* what was this frame's interpolated reference will become
2164 :     forward (past) reference in b-frame coding */
2165 : edgomez 1.102
2166 : syskin 1.103 image_swap(&pEnc->vInterH, &pEnc->f_refh);
2167 :     image_swap(&pEnc->vInterV, &pEnc->f_refv);
2168 :     image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
2169 :     }
2170 : edgomez 1.91
2171 :     /* XXX: debug
2172 :     {
2173 :     char s[100];
2174 :     sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2175 : edgomez 1.102 image_dump_yuvpgm(&current->image,
2176 :     pParam->edged_width,
2177 :     pParam->width, pParam->height, s);
2178 :    
2179 : edgomez 1.91 sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2180 : edgomez 1.102 image_dump_yuvpgm(&reference->image,
2181 :     pParam->edged_width,
2182 :     pParam->width, pParam->height, s);
2183 : syskin 1.96 }
2184 : edgomez 1.91 */
2185 :    
2186 : Isibaar 1.134 return coded;
2187 :     }
2188 :    
2189 :     static void
2190 :     SliceCodeB(SMPData *data)
2191 :     {
2192 :     Encoder *pEnc = (Encoder *) data->pEnc;
2193 :     Bitstream *bs = (Bitstream *) data->bs;
2194 :    
2195 :     DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2196 :     DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2197 :    
2198 :     int x, y;
2199 :     FRAMEINFO * const frame = (FRAMEINFO * const) data->current;
2200 :     MBParam * const pParam = &pEnc->mbParam;
2201 :     int mb_width = pParam->mb_width;
2202 :     int mb_height = pParam->mb_height;
2203 :     IMAGE *f_ref = &pEnc->reference->image;
2204 :     IMAGE *b_ref = &pEnc->current->image;
2205 :    
2206 :     int bound = data->start_y*mb_width;
2207 :     int num_slices = pEnc->num_slices;
2208 :    
2209 :     if (data->start_y > 0) { /* write resync marker */
2210 : Isibaar 1.135 write_video_packet_header(bs, pParam, frame, bound+1);
2211 : Isibaar 1.134 }
2212 :    
2213 : Isibaar 1.135 for (y = data->start_y; y < MIN(data->stop_y+1, mb_height); y++) {
2214 : Isibaar 1.134 int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
2215 : Isibaar 1.135 int stop_x = (y == data->stop_y) ? 1 : mb_width;
2216 :     int start_x = (y == data->start_y && y > 0) ? 1 : 0;
2217 : Isibaar 1.134
2218 : Isibaar 1.135 for (x = start_x; x < stop_x; x++) {
2219 : Isibaar 1.134 MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2220 :    
2221 :     /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2222 :     if (mb->mode == MODE_NOT_CODED) {
2223 :     if (pParam->plugin_flags & XVID_REQORIGINAL) {
2224 :     MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
2225 :     NULL, 0, 0, pParam->edged_width, 0, 0, data->RefQ);
2226 :     }
2227 :     continue;
2228 :     }
2229 :    
2230 : Isibaar 1.135 if (new_bound > bound && x > 0) {
2231 :     bound = new_bound;
2232 :     BitstreamPadAlways(bs);
2233 :     write_video_packet_header(bs, pParam, frame, y*mb_width+x);
2234 :     }
2235 :    
2236 : Isibaar 1.134 mb->quant = frame->quant;
2237 :    
2238 :     if (mb->cbp != 0 || pParam->plugin_flags & XVID_REQORIGINAL) {
2239 :     /* we have to motion-compensate, transfer etc,
2240 :     because there might be blocks to code */
2241 :    
2242 :     MBMotionCompensationBVOP(pParam, mb, x, y, &frame->image,
2243 :     f_ref, &pEnc->f_refh, &pEnc->f_refv,
2244 :     &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2245 :     &pEnc->vInterV, &pEnc->vInterHV, dct_codes,
2246 :     data->RefQ);
2247 :    
2248 :     mb->cbp = MBTransQuantInterBVOP(pParam, frame, mb, x, y, dct_codes, qcoeff);
2249 :     }
2250 :    
2251 :     if (mb->mode == MODE_DIRECT_NO4V)
2252 :     mb->mode = MODE_DIRECT;
2253 :    
2254 :     if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
2255 :     mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2256 :     else
2257 :     if (frame->vop_flags & XVID_VOP_GREYSCALE)
2258 :     /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
2259 :     mb->cbp &= 0x3C;
2260 : suxen_drol 1.60
2261 : Isibaar 1.134 start_timer();
2262 :     MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, data->sStat);
2263 :     stop_coding_timer();
2264 :     }
2265 :     }
2266 : Isibaar 1.1
2267 : Isibaar 1.134 BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2268 :     emms();
2269 : edgomez 1.91 }
2270 :    
2271 :     static void
2272 :     FrameCodeB(Encoder * pEnc,
2273 :     FRAMEINFO * frame,
2274 : edgomez 1.102 Bitstream * bs)
2275 : edgomez 1.91 {
2276 : edgomez 1.102 int bits = BitstreamPos(bs);
2277 : Isibaar 1.134 int k = 0, bound = 0, num_slices = pEnc->num_slices;
2278 :     int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
2279 :     void * status = NULL;
2280 :     int slices_per_thread = (num_slices*1024 / num_threads);
2281 :     int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
2282 : edgomez 1.91
2283 :     IMAGE *f_ref = &pEnc->reference->image;
2284 :     IMAGE *b_ref = &pEnc->current->image;
2285 :    
2286 : Isibaar 1.134 MBParam * const pParam = &pEnc->mbParam;
2287 :     int mb_height = pParam->mb_height;
2288 :    
2289 : edgomez 1.102 #ifdef BFRAMES_DEC_DEBUG
2290 : edgomez 1.91 FILE *fp;
2291 :     static char first=0;
2292 :     #define BFRAME_DEBUG if (!first && fp){ \
2293 :     fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \
2294 :     }
2295 :    
2296 :     if (!first){
2297 :     fp=fopen("C:\\XVIDDBGE.TXT","w");
2298 :     }
2299 :     #endif
2300 :    
2301 : edgomez 1.102 /* forward */
2302 :     if (!pEnc->reference->is_edged) {
2303 :     image_setedges(f_ref, pEnc->mbParam.edged_width,
2304 :     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2305 :     pEnc->mbParam.height, 0);
2306 : Isibaar 1.137 pEnc->reference->is_edged = 1;
2307 : edgomez 1.102 }
2308 : syskin 1.96
2309 : edgomez 1.102 if (pEnc->reference->is_interpolated != 0) {
2310 :     start_timer();
2311 : syskin 1.122 image_interpolate(f_ref->y, pEnc->f_refh.y, pEnc->f_refv.y, pEnc->f_refhv.y,
2312 : edgomez 1.102 pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2313 :     (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
2314 :     stop_inter_timer();
2315 :     pEnc->reference->is_interpolated = 0;
2316 :     }
2317 : edgomez 1.91
2318 : edgomez 1.93 /* backward */
2319 : edgomez 1.102 if (!pEnc->current->is_edged) {
2320 :     image_setedges(b_ref, pEnc->mbParam.edged_width,
2321 :     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2322 :     pEnc->mbParam.height, 0);
2323 :     pEnc->current->is_edged = 1;
2324 :     }
2325 :    
2326 :     if (pEnc->current->is_interpolated != 0) {
2327 :     start_timer();
2328 : syskin 1.122 image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y,
2329 : edgomez 1.102 pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2330 :     (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
2331 :     stop_inter_timer();
2332 :     pEnc->current->is_interpolated = 0;
2333 :     }
2334 :    
2335 :     frame->coding_type = B_VOP;
2336 : Isibaar 1.133
2337 : Isibaar 1.137 if ((frame->vop_flags & XVID_VOP_RD_PSNRHVSM) && (frame->vop_flags & XVID_VOP_RD_BVOP)) {
2338 :     image_block_variance(&frame->image, pEnc->mbParam.edged_width, frame->mbs,
2339 : Isibaar 1.133 pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);
2340 :     }
2341 :    
2342 : syskin 1.121 call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
2343 : edgomez 1.91
2344 : syskin 1.126 frame->fcode = frame->bcode = pEnc->current->fcode;
2345 :    
2346 : syskin 1.127 start_timer();
2347 : Isibaar 1.134
2348 : syskin 1.126 if (pEnc->num_threads > 0) {
2349 : Isibaar 1.134
2350 : syskin 1.126 /* multithreaded motion estimation - dispatch threads */
2351 : Isibaar 1.134 while (k < pEnc->num_threads) {
2352 :     int i, add_s = (slices_per_thread + 512) >> 10;
2353 :     int add_t = (threads_per_slice + 512) >> 10;
2354 :    
2355 :     int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2356 :     int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2357 :     int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2358 :    
2359 :     slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2360 :     threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2361 :    
2362 :     for (i = 0; i < add_t; i++) {
2363 :     memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
2364 :    
2365 :     pEnc->smpData[k+i].pEnc = (void *) pEnc;
2366 :     pEnc->smpData[k+i].current = frame;
2367 :    
2368 :     pEnc->smpData[k+i].y_row = i;
2369 :     pEnc->smpData[k+i].y_step = add_t;
2370 :     pEnc->smpData[k+i].stop_y = stop_y;
2371 :     pEnc->smpData[k+i].start_y = start_y;
2372 :    
2373 :     /* todo: sort out temp space once and for all */
2374 :     pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2375 :     16*((k+i)>>1)*pParam->edged_width;
2376 :     }
2377 :    
2378 :     pEnc->smpData[k].complete_count_above =
2379 :     pEnc->smpData[k+add_t-1].complete_count_self - 1;
2380 : syskin 1.126
2381 : Isibaar 1.134 bound += add_s;
2382 :     k += add_t;
2383 : syskin 1.126 }
2384 :    
2385 : syskin 1.127 for (k = 1; k < pEnc->num_threads; k++) {
2386 : Isibaar 1.134 pthread_create(&pEnc->smpData[k].handle, NULL,
2387 :     (void*)SMPMotionEstimationBVOP, (void*)&pEnc->smpData[k]);
2388 : syskin 1.126 }
2389 : syskin 1.128
2390 : Isibaar 1.134 SMPMotionEstimationBVOP(&pEnc->smpData[0]);
2391 : syskin 1.127
2392 :     for (k = 1; k < pEnc->num_threads; k++) {
2393 : Isibaar 1.134 pthread_join(pEnc->smpData[k].handle, &status);
2394 : syskin 1.127 }
2395 :    
2396 :     frame->fcode = frame->bcode = 0;
2397 :     for (k = 0; k < pEnc->num_threads; k++) {
2398 : Isibaar 1.134 if (pEnc->smpData[k].minfcode > frame->fcode)
2399 :     frame->fcode = pEnc->smpData[k].minfcode;
2400 :     if (pEnc->smpData[k].minbcode > frame->bcode)
2401 :     frame->bcode = pEnc->smpData[k].minbcode;
2402 : syskin 1.127 }
2403 : syskin 1.126 } else {
2404 : Isibaar 1.134
2405 : syskin 1.126 MotionEstimationBVOP(&pEnc->mbParam, frame,
2406 :     ((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */
2407 :     ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */
2408 :     pEnc->reference->mbs, f_ref,
2409 :     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2410 :     pEnc->current, b_ref, &pEnc->vInterH,
2411 : Isibaar 1.135 &pEnc->vInterV, &pEnc->vInterHV,
2412 :     pEnc->num_slices);
2413 : syskin 1.126 }
2414 : syskin 1.127 stop_motion_timer();
2415 : edgomez 1.91
2416 :     set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2417 : edgomez 1.102 BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
2418 : edgomez 1.91
2419 : Isibaar 1.134 /* reset stats */
2420 : edgomez 1.91 frame->sStat.iTextBits = 0;
2421 : Isibaar 1.123 frame->sStat.iMVBits = 0;
2422 : edgomez 1.91 frame->sStat.iMvSum = 0;
2423 :     frame->sStat.iMvCount = 0;
2424 :     frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2425 : edgomez 1.102 frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
2426 :     frame->sStat.kblks = frame->sStat.ublks = 0;
2427 : Isibaar 1.134
2428 :     /* multithreaded inter coding - dispatch threads */
2429 :     bound = 0;
2430 :     slices_per_thread = (num_slices*1024 / num_threads);
2431 :    
2432 :     for (k = 0; k < num_threads; k++) {
2433 :     int add = ((slices_per_thread + 512) >> 10);
2434 : edgomez 1.91
2435 : Isibaar 1.134 slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
2436 : edgomez 1.91
2437 : Isibaar 1.134 pEnc->smpData[k].pEnc = (void *) pEnc;
2438 :     pEnc->smpData[k].current = frame;
2439 :     pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2440 :     pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2441 :     bound += add;
2442 : edgomez 1.91
2443 : Isibaar 1.134 /* todo: sort out temp space once and for all */
2444 :     pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
2445 : syskin 1.112
2446 : Isibaar 1.134 if (k > 0) {
2447 :     BitstreamReset(pEnc->smpData[k].bs);
2448 :     pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2449 :     pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks = pEnc->smpData[k].sStat->iMVBits = 0;
2450 :     }
2451 :     }
2452 : syskin 1.112
2453 : Isibaar 1.134 for (k = 1; k < num_threads; k++) {
2454 :     pthread_create(&pEnc->smpData[k].handle, NULL,
2455 :     (void*)SliceCodeB, (void*)&pEnc->smpData[k]);
2456 :     }
2457 : syskin 1.112
2458 : Isibaar 1.134 pEnc->smpData[0].bs = bs;
2459 :     pEnc->smpData[0].sStat = &frame->sStat;
2460 :     SliceCodeB(&pEnc->smpData[0]);
2461 : syskin 1.112
2462 : Isibaar 1.134 for (k = 1; k < num_threads; k++) {
2463 :     pthread_join(pEnc->smpData[k].handle, &status);
2464 :     }
2465 : edgomez 1.102
2466 : Isibaar 1.134 frame->length = BitstreamLength(bs) - (bits/8);
2467 : edgomez 1.91
2468 : Isibaar 1.134 /* reassemble the pieces together */
2469 :     SerializeBitstreams(pEnc, frame, bs, num_threads);
2470 : edgomez 1.91
2471 :     #ifdef BFRAMES_DEC_DEBUG
2472 :     if (!first){
2473 :     first=1;
2474 :     if (fp)
2475 :     fclose(fp);
2476 :     }
2477 :     #endif
2478 : suxen_drol 1.24 }

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