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

Diff of /xvidcore/src/encoder.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.25, Mon Apr 15 08:03:50 2002 UTC revision 1.76.2.12, Sat Oct 5 21:36:35 2002 UTC
# Line 1  Line 1 
1  // 14.04.2002   added FrameCodeB()  /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  -  Encoder main module  -
5     *
6     *  This program is an implementation of a part of one or more MPEG-4
7     *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
8     *  to use this software module in hardware or software products are
9     *  advised that its use may infringe existing patents or copyrights, and
10     *  any such use would be at such party's own risk.  The original
11     *  developer of this software module and his/her company, and subsequent
12     *  editors and their companies, will have no liability for use of this
13     *  software or modifications or derivatives thereof.
14     *
15     *  This program is free software; you can redistribute it and/or modify
16     *  it under the terms of the GNU General Public License as published by
17     *  the Free Software Foundation; either version 2 of the License, or
18     *  (at your option) any later version.
19     *
20     *  This program is distributed in the hope that it will be useful,
21     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     *  GNU General Public License for more details.
24     *
25     *  You should have received a copy of the GNU General Public License
26     *  along with this program; if not, write to the Free Software
27     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28     *
29     ****************************************************************************/
30    
31    /*****************************************************************************
32     *
33     *  History
34     *
35     *  10.07.2002  added BFRAMES_DEC_DEBUG support
36     *              MinChen <chenm001@163.com>
37     *  20.06.2002 bframe patch
38     *  08.05.2002 fix some problem in DEBUG mode;
39     *             MinChen <chenm001@163.com>
40     *  14.04.2002 added FrameCodeB()
41     *
42     *  $Id$
43     *
44     ****************************************************************************/
45    
46    
47  #include <stdlib.h>  #include <stdlib.h>
48  #include <stdio.h>  #include <stdio.h>
49  #include <math.h>  #include <math.h>
50    #include <string.h>
51    
52  #include "encoder.h"  #include "encoder.h"
53  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
54  #include "global.h"  #include "global.h"
55  #include "utils/timer.h"  #include "utils/timer.h"
56  #include "image/image.h"  #include "image/image.h"
57    #ifdef BFRAMES
58    #include "image/font.h"
59    #include "motion/sad.h"
60    #endif
61    #include "motion/motion.h"
62  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
63  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
64  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 20  Line 70 
70  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
71  #include "utils/mem_align.h"  #include "utils/mem_align.h"
72    
73  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  /*****************************************************************************
74     * Local macros
75     ****************************************************************************/
76    
77    #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
78    #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
79    
80  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  /*****************************************************************************
81  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);   * Local function prototypes
82     ****************************************************************************/
83    
84    static int FrameCodeI(Encoder * pEnc,
85                                              Bitstream * bs,
86                                              uint32_t * pBits);
87    
88    static int FrameCodeP(Encoder * pEnc,
89                                              Bitstream * bs,
90                                              uint32_t * pBits,
91                                              bool force_inter,
92                                              bool vol_header);
93    
94    static void FrameCodeB(Encoder * pEnc,
95                                               FRAMEINFO * frame,
96                                               Bitstream * bs,
97                                               uint32_t * pBits);
98    
99    /*****************************************************************************
100     * Local data
101     ****************************************************************************/
102    
103  static int DQtab[4] =  static int DQtab[4] = {
 {  
104          -1, -2, 1, 2          -1, -2, 1, 2
105  };  };
106    
107  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
108          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
109  };  };
110    
111    
112  int encoder_create(XVID_ENC_PARAM * pParam)  static void __inline
113    image_null(IMAGE * image)
114    {
115            image->y = image->u = image->v = NULL;
116    }
117    
118    
119    /*****************************************************************************
120     * Encoder creation
121     *
122     * This function creates an Encoder instance, it allocates all necessary
123     * image buffers (reference, current and bframes) and initialize the internal
124     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
125     *
126     * The code seems to be very long but is very basic, mainly memory allocation
127     * and cleaning code.
128     *
129     * Returned values :
130     *    - XVID_ERR_OK     - no errors
131     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
132     *                        cleans the structure before exiting.
133     *                        pParam->handle is also set to NULL.
134     *
135     ****************************************************************************/
136    
137    int
138    encoder_create(XVID_ENC_PARAM * pParam)
139  {  {
140          Encoder *pEnc;          Encoder *pEnc;
141          uint32_t i;          int i;
142    
143          pParam->handle = NULL;          pParam->handle = NULL;
144    
# Line 51  Line 149 
149          ENC_CHECK(!(pParam->width % 2));          ENC_CHECK(!(pParam->width % 2));
150          ENC_CHECK(!(pParam->height % 2));          ENC_CHECK(!(pParam->height % 2));
151    
152          if (pParam->fincr <= 0 || pParam->fbase <= 0)          /* Fps */
153          {  
154            if (pParam->fincr <= 0 || pParam->fbase <= 0) {
155                  pParam->fincr = 1;                  pParam->fincr = 1;
156                  pParam->fbase = 25;                  pParam->fbase = 25;
157          }          }
158    
159          // simplify the "fincr/fbase" fraction          /*
160          // (neccessary, since windows supplies us with huge numbers)           * Simplify the "fincr/fbase" fraction
161             * (neccessary, since windows supplies us with huge numbers)
162             */
163    
164          i = pParam->fincr;          i = pParam->fincr;
165          while (i > 1)          while (i > 1) {
166          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
167                          pParam->fincr /= i;                          pParam->fincr /= i;
168                          pParam->fbase /= i;                          pParam->fbase /= i;
169                          i = pParam->fincr;                          i = pParam->fincr;
# Line 73  Line 172 
172                  i--;                  i--;
173          }          }
174    
175          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
176                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
177    
178                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
179                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
180          }          }
181    
182            /* Bitrate allocator defaults */
183    
184          if (pParam->rc_bitrate <= 0)          if (pParam->rc_bitrate <= 0)
185                  pParam->rc_bitrate = 900000;                  pParam->rc_bitrate = 900000;
186    
# Line 92  Line 193 
193          if (pParam->rc_buffer <= 0)          if (pParam->rc_buffer <= 0)
194                  pParam->rc_buffer = 100;                  pParam->rc_buffer = 100;
195    
196            /* Max and min quantizers */
197    
198          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
199                  pParam->min_quantizer = 1;                  pParam->min_quantizer = 1;
200    
201          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
202                  pParam->max_quantizer = 31;                  pParam->max_quantizer = 31;
203    
204            if (pParam->max_quantizer < pParam->min_quantizer)
205                    pParam->max_quantizer = pParam->min_quantizer;
206    
207            /* 1 keyframe each 10 seconds */
208    
209            if (pParam->max_key_interval <= 0)
210                    pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
211    
212            pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
213            if (pEnc == NULL)
214                    return XVID_ERR_MEMORY;
215    
216            /* Zero the Encoder Structure */
217    
218            memset(pEnc, 0, sizeof(Encoder));
219    
220            /* Fill members of Encoder structure */
221    
222            pEnc->mbParam.width = pParam->width;
223            pEnc->mbParam.height = pParam->height;
224    
225            pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;
226            pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;
227    
228            pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
229            pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
230    
231            pEnc->mbParam.fbase = pParam->fbase;
232            pEnc->mbParam.fincr = pParam->fincr;
233    
234            pEnc->mbParam.m_quant_type = H263_QUANT;
235    
236            pEnc->sStat.fMvPrevSigma = -1;
237    
238            /* Fill rate control parameters */
239    
240            pEnc->bitrate = pParam->rc_bitrate;
241    
242            pEnc->iFrameNum = 0;
243            pEnc->iMaxKeyInterval = pParam->max_key_interval;
244    
245            /* try to allocate frame memory */
246    
247            pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
248            pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
249    
250            if (pEnc->current == NULL || pEnc->reference == NULL)
251                    goto xvid_err_memory1;
252    
253            /* try to allocate mb memory */
254    
255            pEnc->current->mbs =
256                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
257                                            pEnc->mbParam.mb_height, CACHE_LINE);
258            pEnc->reference->mbs =
259                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
260                                            pEnc->mbParam.mb_height, CACHE_LINE);
261    
262            if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
263                    goto xvid_err_memory2;
264    
265            /* try to allocate image memory */
266    
267    #ifdef _DEBUG_PSNR
268            image_null(&pEnc->sOriginal);
269    #endif
270    
271            image_null(&pEnc->f_refh);
272            image_null(&pEnc->f_refv);
273            image_null(&pEnc->f_refhv);
274    
275            image_null(&pEnc->current->image);
276            image_null(&pEnc->reference->image);
277            image_null(&pEnc->vInterH);
278            image_null(&pEnc->vInterV);
279            image_null(&pEnc->vInterVf);
280            image_null(&pEnc->vInterHV);
281            image_null(&pEnc->vInterHVf);
282    
283    #ifdef _DEBUG_PSNR
284            if (image_create
285                    (&pEnc->sOriginal, pEnc->mbParam.edged_width,
286                     pEnc->mbParam.edged_height) < 0)
287                    goto xvid_err_memory3;
288    #endif
289    
290            if (image_create
291                    (&pEnc->f_refh, pEnc->mbParam.edged_width,
292                     pEnc->mbParam.edged_height) < 0)
293                    goto xvid_err_memory3;
294            if (image_create
295                    (&pEnc->f_refv, pEnc->mbParam.edged_width,
296                     pEnc->mbParam.edged_height) < 0)
297                    goto xvid_err_memory3;
298            if (image_create
299                    (&pEnc->f_refhv, pEnc->mbParam.edged_width,
300                     pEnc->mbParam.edged_height) < 0)
301                    goto xvid_err_memory3;
302    
303            if (image_create
304                    (&pEnc->current->image, pEnc->mbParam.edged_width,
305                     pEnc->mbParam.edged_height) < 0)
306                    goto xvid_err_memory3;
307            if (image_create
308                    (&pEnc->reference->image, pEnc->mbParam.edged_width,
309                     pEnc->mbParam.edged_height) < 0)
310                    goto xvid_err_memory3;
311            if (image_create
312                    (&pEnc->vInterH, pEnc->mbParam.edged_width,
313                     pEnc->mbParam.edged_height) < 0)
314                    goto xvid_err_memory3;
315            if (image_create
316                    (&pEnc->vInterV, pEnc->mbParam.edged_width,
317                     pEnc->mbParam.edged_height) < 0)
318                    goto xvid_err_memory3;
319            if (image_create
320                    (&pEnc->vInterVf, pEnc->mbParam.edged_width,
321                     pEnc->mbParam.edged_height) < 0)
322                    goto xvid_err_memory3;
323            if (image_create
324                    (&pEnc->vInterHV, pEnc->mbParam.edged_width,
325                     pEnc->mbParam.edged_height) < 0)
326                    goto xvid_err_memory3;
327            if (image_create
328                    (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
329                     pEnc->mbParam.edged_height) < 0)
330                    goto xvid_err_memory3;
331    
332    
333    
334            /* B Frames specific init */
335    
336            pEnc->global = pParam->global;
337            pEnc->mbParam.max_bframes = pParam->max_bframes;
338            pEnc->bquant_ratio = pParam->bquant_ratio;
339            pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
340            pEnc->bframes = NULL;
341    
342            if (pEnc->mbParam.max_bframes > 0) {
343                    int n;
344    
345                    pEnc->bframes =
346                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
347                                                    CACHE_LINE);
348    
349                    if (pEnc->bframes == NULL)
350                            goto xvid_err_memory3;
351    
352                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
353                            pEnc->bframes[n] = NULL;
354    
355    
356                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
357                            pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
358    
359                            if (pEnc->bframes[n] == NULL)
360                                    goto xvid_err_memory4;
361    
362                            pEnc->bframes[n]->mbs =
363                                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
364                                                            pEnc->mbParam.mb_height, CACHE_LINE);
365    
366                            if (pEnc->bframes[n]->mbs == NULL)
367                                    goto xvid_err_memory4;
368    
369                            image_null(&pEnc->bframes[n]->image);
370    
371                            if (image_create
372                                    (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
373                                     pEnc->mbParam.edged_height) < 0)
374                                    goto xvid_err_memory4;
375    
376                    }
377            }
378    
379            pEnc->bframenum_head = 0;
380            pEnc->bframenum_tail = 0;
381            pEnc->flush_bframes = 0;
382            pEnc->bframenum_dx50bvop = -1;
383    
384            pEnc->queue = NULL;
385    
386    
387            if (pEnc->mbParam.max_bframes > 0) {
388                    int n;
389    
390                    pEnc->queue =
391                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
392                                                    CACHE_LINE);
393    
394                    if (pEnc->queue == NULL)
395                            goto xvid_err_memory4;
396    
397                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
398                            image_null(&pEnc->queue[n]);
399    
400                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
401                            if (image_create
402                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
403                                     pEnc->mbParam.edged_height) < 0)
404                                    goto xvid_err_memory5;
405    
406                    }
407            }
408    
409            pEnc->queue_head = 0;
410            pEnc->queue_tail = 0;
411            pEnc->queue_size = 0;
412    
413            pEnc->mbParam.m_stamp = 0;
414    
415            pEnc->m_framenum = 0;
416            pEnc->current->stamp = 0;
417            pEnc->reference->stamp = 0;
418    
419            pParam->handle = (void *) pEnc;
420    
421            if (pParam->rc_bitrate) {
422                    RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
423                                                    pParam->rc_reaction_delay_factor,
424                                                    pParam->rc_averaging_period, pParam->rc_buffer,
425                                                    pParam->fbase * 1000 / pParam->fincr,
426                                                    pParam->max_quantizer, pParam->min_quantizer);
427            }
428    
429            init_timer();
430    
431            return XVID_ERR_OK;
432    
433            /*
434             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
435             */
436    
437      xvid_err_memory5:
438    
439    
440            if (pEnc->mbParam.max_bframes > 0) {
441    
442                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
443                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
444                                                      pEnc->mbParam.edged_height);
445                    }
446                    xvid_free(pEnc->queue);
447            }
448    
449      xvid_err_memory4:
450    
451            if (pEnc->mbParam.max_bframes > 0) {
452    
453                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
454    
455                            if (pEnc->bframes[i] == NULL)
456                                    continue;
457    
458                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
459                                                      pEnc->mbParam.edged_height);
460    
461                            xvid_free(pEnc->bframes[i]->mbs);
462    
463                            xvid_free(pEnc->bframes[i]);
464    
465                    }
466    
467                    xvid_free(pEnc->bframes);
468            }
469    
470      xvid_err_memory3:
471    #ifdef _DEBUG_PSNR
472            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
473                                      pEnc->mbParam.edged_height);
474    #endif
475    
476            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
477                                      pEnc->mbParam.edged_height);
478            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
479                                      pEnc->mbParam.edged_height);
480            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
481                                      pEnc->mbParam.edged_height);
482    
483            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
484                                      pEnc->mbParam.edged_height);
485            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
486                                      pEnc->mbParam.edged_height);
487            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
488                                      pEnc->mbParam.edged_height);
489            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
490                                      pEnc->mbParam.edged_height);
491            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
492                                      pEnc->mbParam.edged_height);
493            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
494                                      pEnc->mbParam.edged_height);
495            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
496                                      pEnc->mbParam.edged_height);
497    
498      xvid_err_memory2:
499            xvid_free(pEnc->current->mbs);
500            xvid_free(pEnc->reference->mbs);
501    
502      xvid_err_memory1:
503            xvid_free(pEnc->current);
504            xvid_free(pEnc->reference);
505            xvid_free(pEnc);
506    
507            pParam->handle = NULL;
508    
509            return XVID_ERR_MEMORY;
510    }
511    
512    /*****************************************************************************
513     * Encoder destruction
514     *
515     * This function destroy the entire encoder structure created by a previous
516     * successful encoder_create call.
517     *
518     * Returned values (for now only one returned value) :
519     *    - XVID_ERR_OK     - no errors
520     *
521     ****************************************************************************/
522    
523    int
524    encoder_destroy(Encoder * pEnc)
525    {
526            int i;
527    
528            ENC_CHECK(pEnc);
529    
530            /* B Frames specific */
531            if (pEnc->mbParam.max_bframes > 0) {
532    
533                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
534    
535                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
536                                              pEnc->mbParam.edged_height);
537                    }
538                    xvid_free(pEnc->queue);
539            }
540    
541    
542            if (pEnc->mbParam.max_bframes > 0) {
543    
544                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
545    
546                            if (pEnc->bframes[i] == NULL)
547                                    continue;
548    
549                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
550                                              pEnc->mbParam.edged_height);
551    
552                            xvid_free(pEnc->bframes[i]->mbs);
553    
554                            xvid_free(pEnc->bframes[i]);
555                    }
556    
557                    xvid_free(pEnc->bframes);
558    
559            }
560    
561            /* All images, reference, current etc ... */
562    
563            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
564                                      pEnc->mbParam.edged_height);
565            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
566                                      pEnc->mbParam.edged_height);
567            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
568                                      pEnc->mbParam.edged_height);
569            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
570                                      pEnc->mbParam.edged_height);
571            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
572                                      pEnc->mbParam.edged_height);
573            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
574                                      pEnc->mbParam.edged_height);
575            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
576                                      pEnc->mbParam.edged_height);
577    
578            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
579                                      pEnc->mbParam.edged_height);
580            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
581                                      pEnc->mbParam.edged_height);
582            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
583                                      pEnc->mbParam.edged_height);
584    
585    #ifdef _DEBUG_PSNR
586            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
587                                      pEnc->mbParam.edged_height);
588    #endif
589    
590            /* Encoder structure */
591    
592            xvid_free(pEnc->current->mbs);
593            xvid_free(pEnc->current);
594    
595            xvid_free(pEnc->reference->mbs);
596            xvid_free(pEnc->reference);
597    
598            xvid_free(pEnc);
599    
600            return XVID_ERR_OK;
601    }
602    
603    
604    static __inline void inc_frame_num(Encoder * pEnc)
605    {
606            pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
607            pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
608    }
609    
610    
611    static __inline void
612    queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
613    {
614            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
615            {
616                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
617                    return;
618            }
619    
620            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
621                                    pEnc->bframenum_head, pEnc->bframenum_tail,
622                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
623    
624    
625            start_timer();
626            if (image_input
627                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
628                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
629                    return;
630            stop_conv_timer();
631    
632            pEnc->queue_size++;
633            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
634    }
635    
636    static __inline void
637    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
638    {
639    
640                    pCur->ticks = (int32_t)pCur->stamp % time_base;
641                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
642    
643                    //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable
644    
645    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
646                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
647                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
648                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
649                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
650                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
651    
652    */
653    }
654    
655    
656    
657    
658    
659    /*****************************************************************************
660     * IPB frame encoder entry point
661     *
662     * Returned values :
663     *    - XVID_ERR_OK     - no errors
664     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
665     *                        format
666     ****************************************************************************/
667    
668    int
669    encoder_encode_bframes(Encoder * pEnc,
670                               XVID_ENC_FRAME * pFrame,
671                               XVID_ENC_STATS * pResult)
672    {
673            uint16_t x, y;
674            Bitstream bs;
675            uint32_t bits, mode;
676    
677            int input_valid = 1;
678    
679    #ifdef _DEBUG_PSNR
680            float psnr;
681            char temp[128];
682    #endif
683    
684            ENC_CHECK(pEnc);
685            ENC_CHECK(pFrame);
686            ENC_CHECK(pFrame->image);
687    
688            start_global_timer();
689    
690            BitstreamInit(&bs, pFrame->bitstream, 0);
691    
692    ipvop_loop:
693    
694            /*
695             * bframe "flush" code
696             */
697    
698            if ((pFrame->image == NULL || pEnc->flush_bframes)
699                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
700    
701                    if (pEnc->flush_bframes == 0) {
702                            /*
703                             * we have reached the end of stream without getting
704                             * a future reference frame... so encode last final
705                             * frame as a pframe
706                             */
707    
708                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
709                                    pEnc->bframenum_head, pEnc->bframenum_tail,
710                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
711    
712                            pEnc->bframenum_tail--;
713                            SWAP(pEnc->current, pEnc->reference);
714    
715                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
716    
717                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
718    
719                            BitstreamPad(&bs);
720                            pFrame->length = BitstreamLength(&bs);
721                            pFrame->intra = 0;
722    
723                            return XVID_ERR_OK;
724                    }
725    
726    
727                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
728                                    pEnc->bframenum_head, pEnc->bframenum_tail,
729                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
730    
731                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
732                    pEnc->bframenum_head++;
733    
734                    BitstreamPad(&bs);
735                    pFrame->length = BitstreamLength(&bs);
736                    pFrame->intra = 0;
737    
738                    if (input_valid)
739                            queue_image(pEnc, pFrame);
740    
741                    return XVID_ERR_OK;
742            }
743    
744            if (pEnc->bframenum_head > 0) {
745                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
746    
747                    /* write an empty marker to the bitstream.
748    
749                       for divx5 decoder compatibility, this marker must consist
750                       of a not-coded p-vop, with a time_base of zero, and time_increment
751                       indentical to the future-referece frame.
752                    */
753    
754                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
755                            int tmp;
756    
757                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
758                                    pEnc->bframenum_head, pEnc->bframenum_tail,
759                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
760    
761                            BitstreamPad(&bs);
762    
763                            tmp = pEnc->current->seconds;
764                            pEnc->current->seconds = 0; /* force time_base = 0 */
765                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
766                            pEnc->current->seconds = tmp;
767    
768                            pFrame->length = BitstreamLength(&bs);
769                            pFrame->intra = 0;
770    
771                            if (input_valid)
772                                    queue_image(pEnc, pFrame);
773    
774                            return XVID_ERR_OK;
775                    }
776            }
777    
778    
779    bvop_loop:
780    
781            if (pEnc->bframenum_dx50bvop != -1)
782            {
783    
784                    SWAP(pEnc->current, pEnc->reference);
785                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
786    
787                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
788                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
789                    }
790    
791                    if (input_valid)
792                    {
793                            queue_image(pEnc, pFrame);
794                            input_valid = 0;
795                    }
796    
797            } else if (input_valid) {
798    
799                    SWAP(pEnc->current, pEnc->reference);
800    
801                    start_timer();
802                    if (image_input
803                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
804                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
805                            return XVID_ERR_FORMAT;
806                    stop_conv_timer();
807    
808                    // queue input frame, and dequue next image
809                    if (pEnc->queue_size > 0)
810                    {
811                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
812                            if (pEnc->queue_head != pEnc->queue_tail)
813                            {
814                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
815                            }
816                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
817                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
818                    }
819    
820            } else if (pEnc->queue_size > 0) {
821    
822                    SWAP(pEnc->current, pEnc->reference);
823    
824                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
825                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
826                    pEnc->queue_size--;
827    
828            } else {
829    
830                    /* if nothing was encoded, write an 'ignore this frame' flag
831                       to the bitstream */
832    
833                    if (BitstreamPos(&bs) == 0) {
834    
835                            DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
836                                    pEnc->bframenum_head, pEnc->bframenum_tail,
837                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
838    
839                            BitstreamPutBits(&bs, 0x7f, 8);
840                            pFrame->intra = 0;
841                    }
842    
843                    pFrame->length = BitstreamLength(&bs);
844                    return XVID_ERR_OK;
845            }
846    
847            pEnc->flush_bframes = 0;
848    
849            emms();
850    
851            // only inc frame num, adapt quant, etc. if we havent seen it before
852            if (pEnc->bframenum_dx50bvop < 0 )
853            {
854                    if (pFrame->quant == 0)
855                            pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
856                    else
857                            pEnc->current->quant = pFrame->quant;
858    
859    /*              if (pEnc->current->quant < 1)
860                            pEnc->current->quant = 1;
861    
862                    if (pEnc->current->quant > 31)
863                            pEnc->current->quant = 31;
864    */
865                    pEnc->current->global_flags = pFrame->general;
866                    pEnc->current->motion_flags = pFrame->motion;
867    
868                    /* ToDo : dynamic fcode (in both directions) */
869                    pEnc->current->fcode = pEnc->mbParam.m_fcode;
870                    pEnc->current->bcode = pEnc->mbParam.m_fcode;
871    
872                    inc_frame_num(pEnc);
873    
874    #ifdef _DEBUG_PSNR
875                    image_copy(&pEnc->sOriginal, &pEnc->current->image,
876                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
877    #endif
878    
879                    emms();
880    
881                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
882                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
883                                    "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
884                    }
885    
886            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
887             * Luminance masking
888             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
889    
890                    if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
891                            int *temp_dquants =
892                                    (int *) xvid_malloc(pEnc->mbParam.mb_width *
893                                                                    pEnc->mbParam.mb_height * sizeof(int),
894                                                                    CACHE_LINE);
895    
896                            pEnc->current->quant =
897                                    adaptive_quantization(pEnc->current->image.y,
898                                                                      pEnc->mbParam.edged_width, temp_dquants,
899                                                                      pEnc->current->quant, pEnc->current->quant,
900                                                                      2 * pEnc->current->quant,
901                                                                      pEnc->mbParam.mb_width,
902                                                                      pEnc->mbParam.mb_height);
903    
904                            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
905    
906    #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
907    
908                                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
909                                            MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
910    
911                                            pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
912                                    }
913    
914    #undef OFFSET
915                            }
916    
917                            xvid_free(temp_dquants);
918                    }
919    
920            }
921    
922            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
923             * ivop/pvop/bvop selection
924             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
925    
926    
927            if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
928                    (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
929                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
930                    || /*image_mad(&pEnc->reference->image, &pEnc->current->image,
931                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
932                                             pEnc->mbParam.height) > 30) {*/
933                            2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,
934                                             &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {
935    
936                    /*
937                     * This will be coded as an Intra Frame
938                     */
939    
940                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
941                                    pEnc->bframenum_head, pEnc->bframenum_tail,
942                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
943    
944                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
945                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
946                    }
947    
948          if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */                  // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
949    
950          if (pParam->max_quantizer < pParam->min_quantizer)                  if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
                 pParam->max_quantizer = pParam->min_quantizer;  
951    
952          if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL)                          pEnc->bframenum_tail--;
953                  return XVID_ERR_MEMORY;                          pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
954    
955          /* Fill members of Encoder structure */                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
956                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
957                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
958                            }
959                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
960    
961          pEnc->mbParam.width = pParam->width;                          pFrame->intra = 0;
         pEnc->mbParam.height = pParam->height;  
962    
963          pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;                  } else {
         pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;  
964    
965          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;                          FrameCodeI(pEnc, &bs, &bits);
966          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;                          pFrame->intra = 1;
967    
968          pEnc->sStat.fMvPrevSigma = -1;                          pEnc->bframenum_dx50bvop = -1;
969                    }
970    
971          /* Fill rate control parameters */                  pEnc->flush_bframes = 1;
972    
973          pEnc->mbParam.quant = 4;                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
974                            BitstreamPadAlways(&bs);
975                            input_valid = 0;
976                            goto ipvop_loop;
977                    }
978    
979          pEnc->bitrate = pParam->rc_bitrate;                  /*
980                     * NB : sequences like "IIBB" decode fine with msfdam but,
981                     *      go screwy with divx 5.00
982                     */
983            } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {
984                    /*
985                     * This will be coded as a Predicted Frame
986                     */
987    
988          pEnc->iFrameNum = 0;                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
989          pEnc->iMaxKeyInterval = pParam->max_key_interval;                                  pEnc->bframenum_head, pEnc->bframenum_tail,
990                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
991    
992          /* try to allocate memory */                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
993                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
994                    }
995    
996          pEnc->sCurrent.y        =       pEnc->sCurrent.u        =       pEnc->sCurrent.v        = NULL;                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
997          pEnc->sReference.y      =       pEnc->sReference.u      =       pEnc->sReference.v      = NULL;                  pFrame->intra = 0;
998          pEnc->vInterH.y         =       pEnc->vInterH.u         =       pEnc->vInterH.v         = NULL;                  pEnc->flush_bframes = 1;
999          pEnc->vInterV.y         =       pEnc->vInterV.u         =       pEnc->vInterV.v         = NULL;  
1000          pEnc->vInterVf.y        =       pEnc->vInterVf.u        =       pEnc->vInterVf.v        = NULL;                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1001          pEnc->vInterHV.y        =       pEnc->vInterHV.u        =       pEnc->vInterHV.v        = NULL;                          BitstreamPadAlways(&bs);
1002          pEnc->vInterHVf.y       =       pEnc->vInterHVf.u       =       pEnc->vInterHVf.v       = NULL;                          input_valid = 0;
1003                            goto ipvop_loop;
         pEnc->pMBs = NULL;  
   
         if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
 #ifdef _DEBUG  
                 image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
 #endif  
                 (pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL)  
         {  
                 image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #ifdef _DEBUG  
                 image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #endif  
                 if (pEnc)  
                 {  
                         xvid_free(pEnc);  
1004                  }                  }
1005                  return XVID_ERR_MEMORY;  
1006            } else {
1007                    /*
1008                     * This will be coded as a Bidirectional Frame
1009                     */
1010    
1011                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1012                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1013          }          }
1014    
1015          // init macroblock array                  if (pFrame->bquant < 1) {
1016          for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++)                          pEnc->current->quant =
1017          {                                  ((pEnc->reference->quant +
1018                  pEnc->pMBs[i].dquant = NO_CHANGE;                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;
1019                    } else {
1020                            pEnc->current->quant = pFrame->bquant;
1021          }          }
1022                    if (pEnc->current->quant < 1)
1023                            pEnc->current->quant = 1;
1024    
1025          pParam->handle = (void *)pEnc;                  if (pEnc->current->quant > 31)
1026                            pEnc->current->quant = 31;
1027    
1028          if (pParam->rc_bitrate)  
1029          {                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1030                  RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1031                          pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr,                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1032                          pParam->max_quantizer, pParam->min_quantizer);  
1033    
1034    
1035                    /* store frame into bframe buffer & swap ref back to current */
1036                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1037                    SWAP(pEnc->current, pEnc->reference);
1038    
1039                    pEnc->bframenum_tail++;
1040    
1041                    pFrame->intra = 0;
1042                    pFrame->length = 0;
1043    
1044                    input_valid = 0;
1045                    goto bvop_loop;
1046          }          }
1047    
1048          init_timer();          pEnc->iFrameNum++;
1049    
1050          return XVID_ERR_OK;          BitstreamPad(&bs);
1051            pFrame->length = BitstreamLength(&bs);
1052    
1053            if (pResult) {
1054                    pResult->quant = pEnc->current->quant;
1055                    pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1056                    pResult->kblks = pEnc->sStat.kblks;
1057                    pResult->mblks = pEnc->sStat.mblks;
1058                    pResult->ublks = pEnc->sStat.ublks;
1059  }  }
1060    
1061            emms();
1062    
1063  int encoder_destroy(Encoder * pEnc)  #ifdef _DEBUG_PSNR
1064  {          psnr =
1065          ENC_CHECK(pEnc);                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1066          ENC_CHECK(pEnc->sCurrent.y);                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1067          ENC_CHECK(pEnc->sReference.y);                                     pEnc->mbParam.height);
1068    
1069          xvid_free(pEnc->pMBs);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1070          image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          DEBUG(temp);
         image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #ifdef _DEBUG  
                 image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
1071  #endif  #endif
1072          xvid_free(pEnc);  
1073            if (pFrame->quant == 0) {
1074                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1075                                                      pFrame->length, pFrame->intra);
1076            }
1077    
1078            stop_global_timer();
1079            write_timer();
1080    
1081          return XVID_ERR_OK;          return XVID_ERR_OK;
1082  }  }
1083    
1084  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
1085    
1086    /*****************************************************************************
1087     * "original" IP frame encoder entry point
1088     *
1089     * Returned values :
1090     *    - XVID_ERR_OK     - no errors
1091     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1092     *                        format
1093     ****************************************************************************/
1094    
1095    int
1096    encoder_encode(Encoder * pEnc,
1097                               XVID_ENC_FRAME * pFrame,
1098                               XVID_ENC_STATS * pResult)
1099  {  {
1100          uint16_t x, y;          uint16_t x, y;
1101          Bitstream bs;          Bitstream bs;
1102          uint32_t bits;          uint32_t bits;
1103          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1104  #ifdef _DEBUG  
1105    #ifdef _DEBUG_PSNR
1106          float psnr;          float psnr;
1107          uint8_t temp[100];          uint8_t temp[128];
1108  #endif  #endif
1109    
1110          start_global_timer();          start_global_timer();
# Line 231  Line 1114 
1114          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1115          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1116    
1117          pEnc->mbParam.global_flags = pFrame->general;          SWAP(pEnc->current, pEnc->reference);
1118          pEnc->mbParam.motion_flags = pFrame->motion;  
1119            pEnc->current->global_flags = pFrame->general;
1120            pEnc->current->motion_flags = pFrame->motion;
1121          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1122    
1123          start_timer();          inc_frame_num(pEnc);
1124          if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,  
1125                          pFrame->image, pFrame->colorspace))          /* disable alternate scan flag if interlacing is not enabled */
1126            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1127                    !(pEnc->current->global_flags & XVID_INTERLACING))
1128          {          {
1129                  return XVID_ERR_FORMAT;                  pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1130          }          }
         stop_conv_timer();  
1131    
1132          EMMS();          start_timer();
1133            if (image_input
1134                    (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1135                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
1136                    return XVID_ERR_FORMAT;
1137            stop_conv_timer();
1138    
1139  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1140          image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height);          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1141                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1142  #endif  #endif
1143    
1144            emms();
1145    
1146          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1147    
1148          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1149          {                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
1150                  pEnc->mbParam.quant = RateControlGetQ(0);          } else {
1151          }                  pEnc->current->quant = pFrame->quant;
         else  
         {  
                 pEnc->mbParam.quant = pFrame->quant;  
1152          }          }
1153    
1154          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0)          if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1155          {                  pEnc->mbParam.m_quarterpel = 1;
1156                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);          else
1157                    pEnc->mbParam.m_quarterpel = 0;
1158    
1159                  pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y,          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1160                                                              pEnc->mbParam.width,                  int *temp_dquants =
1161                                                              temp_dquants,                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
1162                                                              pEnc->mbParam.quant,                                                                  pEnc->mbParam.mb_height * sizeof(int),
1163                                                              pEnc->mbParam.quant,                                                                  CACHE_LINE);
1164                                                              2*pEnc->mbParam.quant,  
1165                    pEnc->current->quant =
1166                            adaptive_quantization(pEnc->current->image.y,
1167                                                                      pEnc->mbParam.edged_width, temp_dquants,
1168                                                                      pEnc->current->quant, pEnc->current->quant,
1169                                                                      2 * pEnc->current->quant,
1170                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
1171                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
1172    
1173                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1174                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1175                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1176                                  MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1177                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1178    
1179    
1180                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1181    
1182                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1183                            }
1184    
1185    #undef OFFSET
1186                          }                          }
1187    
1188                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1189          }          }
1190    
1191          if(pEnc->mbParam.global_flags & XVID_H263QUANT) {          if (pEnc->current->global_flags & XVID_H263QUANT) {
1192                  if(pEnc->mbParam.quant_type != H263_QUANT)                  if (pEnc->mbParam.m_quant_type != H263_QUANT)
1193                          write_vol_header = 1;                          write_vol_header = 1;
1194                  pEnc->mbParam.quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1195          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1196          else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) {                  int matrix1_changed, matrix2_changed;
                 int ret1, ret2;  
1197    
1198                  ret1 = ret2 = 0;                  matrix1_changed = matrix2_changed = 0;
1199    
1200                  if(pEnc->mbParam.quant_type != MPEG4_QUANT)                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1201                          write_vol_header = 1;                          write_vol_header = 1;
1202    
1203                  pEnc->mbParam.quant_type = MPEG4_QUANT;                  pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1204    
1205                  if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1206                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1207                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1208                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1209                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1210                  }                  } else {
1211                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1212                          ret1 = set_intra_matrix(get_default_intra_matrix());                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());
                         ret2 = set_inter_matrix(get_default_inter_matrix());  
1213                  }                  }
1214                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1215                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1216          }          }
1217    
1218          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1219          {                  if ((pEnc->iFrameNum == 0)
1220                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
1221                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
1222                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1223                  else                  } else {
1224                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1225          }          }
1226          else          } else {
1227          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
1228                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1229                  else                  } else {
1230                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1231          }          }
1232    
1233            }
1234    
1235          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1236          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1237          BitstreamPad(&bs);          BitstreamPad(&bs);
1238          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1239    
1240          if (pResult)          if (pResult) {
1241          {                  pResult->quant = pEnc->current->quant;
                 pResult->quant = pEnc->mbParam.quant;  
1242                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1243                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
1244                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->sStat.mblks;
1245                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1246          }          }
1247    
1248          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra);  
         }  
1249    
1250  #ifdef _DEBUG          if (pFrame->quant == 0) {
1251          psnr = image_psnr(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1252                                  pEnc->mbParam.width, pEnc->mbParam.height);                                                    pFrame->length, pFrame->intra);
1253            }
1254    #ifdef _DEBUG_PSNR
1255            psnr =
1256                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1257                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1258                                       pEnc->mbParam.height);
1259    
1260          sprintf(temp, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1261          DEBUG(temp);          DEBUG(temp);
1262  #endif  #endif
1263    
1264          pEnc->iFrameNum++;          pEnc->iFrameNum++;
         image_swap(&pEnc->sCurrent, &pEnc->sReference);  
1265    
1266          stop_global_timer();          stop_global_timer();
1267          write_timer();          write_timer();
# Line 367  Line 1270 
1270  }  }
1271    
1272    
1273  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1274    CodeIntraMB(Encoder * pEnc,
1275                            MACROBLOCK * pMB)
1276    {
1277    
1278          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1279    
1280          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {          /* zero mv statistics */
1281                  if(pMB->dquant != NO_CHANGE)          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1282                  {          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1283            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1284            pMB->sad16 = 0;
1285    
1286            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1287                    if (pMB->dquant != NO_CHANGE) {
1288                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1289                          pEnc->mbParam.quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1290    
1291                          if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                          if (pEnc->current->quant > 31)
1292                          if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                  pEnc->current->quant = 31;
1293                            if (pEnc->current->quant < 1)
1294                                    pEnc->current->quant = 1;
1295                  }                  }
1296          }          }
1297    
1298          pMB->quant = pEnc->mbParam.quant;          pMB->quant = pEnc->current->quant;
1299  }  }
1300    
1301    
1302  #define FCODEBITS       3  #define FCODEBITS       3
1303  #define MODEBITS        5  #define MODEBITS        5
1304    
1305  void HintedMESet(Encoder * pEnc, int * intra)  void
1306    HintedMESet(Encoder * pEnc,
1307                            int *intra)
1308  {  {
1309          HINTINFO * hint;          HINTINFO * hint;
1310          Bitstream bs;          Bitstream bs;
# Line 398  Line 1313 
1313    
1314          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1315    
1316          if (hint->rawhints)          if (hint->rawhints) {
         {  
1317                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
1318          }          } else {
         else  
         {  
1319                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1320                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
1321          }          }
1322    
1323          if (*intra)          if (*intra) {
         {  
1324                  return;                  return;
1325          }          }
1326    
1327          pEnc->mbParam.fixed_code = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
1328                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1329                                                                                                                                     FCODEBITS);
1330    
1331          length  = pEnc->mbParam.fixed_code + 5;          length = pEnc->current->fcode + 5;
1332          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1333    
1334          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1335          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1336                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1337                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1338                          MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1339                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1340                          VECTOR pred[4];                          VECTOR pred;
1341                          VECTOR tmp;                          VECTOR tmp;
                         int dummy[4];  
1342                          int vec;                          int vec;
1343    
1344                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
1345                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1346                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                                                                                                                                    MODEBITS);
1347                          {  
1348                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1349                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1350    
1351                            if (pMB->mode == MODE_INTER) {
1352                                    tmp.x =
1353                                            (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1354                                                                                                                                                      length);
1355                                    tmp.y =
1356                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1357                                                                                                                                                      length);
1358                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
1359                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
1360    
1361                                  get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);                                  pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
1362    
1363                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1364                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1365                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1366                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1367                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1368                                  }                                  }
1369                          }                          } else if (pMB->mode == MODE_INTER4V) {
1370                          else if (pMB->mode == MODE_INTER4V)                                  for (vec = 0; vec < 4; ++vec) {
1371                          {                                          tmp.x =
1372                                  for (vec=0 ; vec<4 ; ++vec)                                                  (hint->rawhints) ? bhint->mvs[vec].
1373                                  {                                                  x : BitstreamGetBits(&bs, length);
1374                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                          tmp.y =
1375                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                                  (hint->rawhints) ? bhint->mvs[vec].
1376                                                    y : BitstreamGetBits(&bs, length);
1377                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
1378                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
1379    
1380                                          get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);                                          pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
1381    
1382                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1383                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1384                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1385                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
                                 }  
1386                          }                          }
1387                          else    // intra / intra_q / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
1388                                  {                                  {
1389                                    for (vec = 0; vec < 4; ++vec) {
1390                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1391                                    }
1392                            }
1393    
1394                            if (pMB->mode == MODE_INTER4V &&
1395                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
1396                                    && pMB->dquant != NO_CHANGE) {
1397                                    pMB->mode = MODE_INTRA;
1398    
1399                                    for (vec = 0; vec < 4; ++vec) {
1400                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
1401                                  }                                  }
1402                          }                          }
# Line 477  Line 1405 
1405  }  }
1406    
1407    
1408  void HintedMEGet(Encoder * pEnc, int intra)  void
1409    HintedMEGet(Encoder * pEnc,
1410                            int intra)
1411  {  {
1412          HINTINFO * hint;          HINTINFO * hint;
1413          Bitstream bs;          Bitstream bs;
# Line 486  Line 1416 
1416    
1417          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1418    
1419          if (hint->rawhints)          if (hint->rawhints) {
         {  
1420                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
1421          }          } else {
         else  
         {  
1422                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
1423                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
1424          }          }
1425    
1426          if (intra)          if (intra) {
1427          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
1428                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1429                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1430                  }                  }
1431                  return;                  return;
1432          }          }
1433    
1434          length  = pEnc->mbParam.fixed_code + 5;          length = pEnc->current->fcode + 5;
1435          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1436    
1437          if (hint->rawhints)          if (hint->rawhints) {
1438          {                  hint->mvhint.fcode = pEnc->current->fcode;
1439                  hint->mvhint.fcode = pEnc->mbParam.fixed_code;          } else {
1440          }                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
         else  
         {  
                 BitstreamPutBits(&bs, pEnc->mbParam.fixed_code, FCODEBITS);  
1441          }          }
1442    
1443          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1444          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1445                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1446                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1447                          MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1448                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1449                          VECTOR tmp;                          VECTOR tmp;
1450    
1451                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
1452                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
1453                          }                          } else {
                         else  
                         {  
1454                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1455                          }                          }
1456    
1457                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
1458                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
1459                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
1460                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
1461                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
1462    
1463                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
1464                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
1465                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
1466                                  }                                  } else {
                                 else  
                                 {  
1467                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
1468                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
1469                                  }                                  }
1470                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
1471                                  int vec;                                  int vec;
1472    
1473                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1474                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
1475                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].y;
1476                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                          tmp.x += (tmp.x < 0) ? high*2 : 0;
1477                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                                          tmp.y += (tmp.y < 0) ? high*2 : 0;
1478    
1479                                          if (hint->rawhints)                                          if (hint->rawhints) {
                                         {  
1480                                                  bhint->mvs[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
1481                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].y = tmp.y;
1482                                          }                                          } else {
                                         else  
                                         {  
1483                                                  BitstreamPutBits(&bs, tmp.x, length);                                                  BitstreamPutBits(&bs, tmp.x, length);
1484                                                  BitstreamPutBits(&bs, tmp.y, length);                                                  BitstreamPutBits(&bs, tmp.y, length);
1485                                          }                                          }
# Line 579  Line 1488 
1488                  }                  }
1489          }          }
1490    
1491          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
1492                  BitstreamPad(&bs);                  BitstreamPad(&bs);
1493                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
1494          }          }
1495  }  }
1496    
1497    
1498  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1499    FrameCodeI(Encoder * pEnc,
1500                       Bitstream * bs,
1501                       uint32_t * pBits)
1502  {  {
1503    
1504          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
# Line 596  Line 1507 
1507          uint16_t x, y;          uint16_t x, y;
1508    
1509          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1510          pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1511          pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1512            pEnc->current->coding_type = I_VOP;
1513    
1514          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1515          BitstreamWriteVopHeader(bs, &pEnc->mbParam);  
1516    #define DIVX501B481P "DivX501b481p"
1517            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1518                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1519            }
1520    
1521            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1522            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1523    
1524          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1525    
# Line 609  Line 1528 
1528          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1529    
1530          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1531                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1532                  {                          MACROBLOCK *pMB =
1533                          MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1534    
1535                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1536    
1537                          MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, &pEnc->sCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1538                                                              dct_codes, qcoeff);
1539    
1540                          start_timer();                          start_timer();
1541                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1542                          stop_prediction_timer();                          stop_prediction_timer();
1543    
1544                          start_timer();                          start_timer();
1545                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1546                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1547                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1548                                    qcoeff[5*64+0]=0;
1549                            }
1550                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1551                          stop_coding_timer();                          stop_coding_timer();
1552                  }                  }
1553    
# Line 632  Line 1557 
1557          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
1558          pEnc->sStat.iMvSum = 0;          pEnc->sStat.iMvSum = 0;
1559          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1560          pEnc->mbParam.fixed_code = 2;          pEnc->mbParam.m_fcode = 2;
1561    
1562          if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1563                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1564          }          }
1565    
# Line 644  Line 1568 
1568    
1569    
1570  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1571    #define BFRAME_SKIP_THRESHHOLD 30
1572    
1573  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1574    FrameCodeP(Encoder * pEnc,
1575                       Bitstream * bs,
1576                       uint32_t * pBits,
1577                       bool force_inter,
1578                       bool vol_header)
1579  {  {
1580          float fSigma;          float fSigma;
1581    
# Line 653  Line 1583 
1583          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
1584    
1585          int iLimit;          int iLimit;
1586          uint32_t x, y;          int x, y, k;
1587          int iSearchRange;          int iSearchRange;
1588          bool bIntra;          int bIntra, skip_possible;
1589    
1590          IMAGE *pCurrent = &pEnc->sCurrent;          /* IMAGE *pCurrent = &pEnc->current->image; */
1591          IMAGE *pRef = &pEnc->sReference;          IMAGE *pRef = &pEnc->reference->image;
1592    
1593          start_timer();          start_timer();
1594          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1595                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                        pEnc->mbParam.edged_height,  
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
                        pEnc->mbParam.global_flags & XVID_INTERLACING);  
1596          stop_edges_timer();          stop_edges_timer();
1597    
1598          pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1599            pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1600            pEnc->current->fcode = pEnc->mbParam.m_fcode;
1601    
1602          if (!force_inter)          if (!force_inter)
1603                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1604                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1605                                       INTRA_THRESHOLD);
1606          else          else
1607                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1608    
1609          if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1610                  start_timer();                  start_timer();
1611                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1612                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1613                                    pEnc->mbParam.rounding_type);                                                    pEnc->mbParam.edged_height,
1614                                                      pEnc->mbParam.m_quarterpel,
1615                                                      pEnc->current->rounding_type);
1616                  stop_inter_timer();                  stop_inter_timer();
1617          }          }
1618    
1619          start_timer();          start_timer();
1620          if (pEnc->mbParam.global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
         {  
1621                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1622            if (bIntra == 0) {
1623                            pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current);
1624                            MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1625                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1626          }          }
1627          else  
1628          {          } else {
1629                  bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,  
1630                                            &pEnc->vInterH, &pEnc->vInterV,          bIntra =
1631                                            &pEnc->vInterHV, &pEnc->sCurrent, iLimit);                  MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1632                             &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1633                             iLimit);
1634    
1635          }          }
1636          stop_motion_timer();          stop_motion_timer();
1637    
1638          if (bIntra == 1)          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
         {  
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1639    
1640          pEnc->mbParam.coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1641    
1642          if(vol_header)          if(vol_header)
1643                  BitstreamWriteVolHeader(bs, &pEnc->mbParam);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1644    
1645    
1646          BitstreamWriteVopHeader(bs, &pEnc->mbParam);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1647            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1648    
1649          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1650    
1651          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1652          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1653    
1654          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1655          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1656                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1657                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1658    
1659                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1660    
1661                          if (!bIntra)                          if (!bIntra) {
                         {  
1662                                  start_timer();                                  start_timer();
1663                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1664                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1665                                                       &pEnc->sReference,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1666                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->sCurrent,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
1667                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1668                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1669                                                       pEnc->mbParam.rounding_type);                                                                           pEnc->mbParam.m_quarterpel,
1670                                                                             pEnc->current->rounding_type);
1671                                  stop_comp_timer();                                  stop_comp_timer();
1672    
1673                                  if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1674                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1675                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1676                                                  pEnc->mbParam.quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1677                                                  if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                                  if (pEnc->current->quant > 31)
1678                                                  else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                          pEnc->current->quant = 31;
1679                                                    else if (pEnc->current->quant < 1)
1680                                                            pEnc->current->quant = 1;
1681                                          }                                          }
1682                                  }                                  }
1683                                  pMB->quant = pEnc->mbParam.quant;                                  pMB->quant = pEnc->current->quant;
1684    
1685                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1686    
1687                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                                  if (pMB->mode != MODE_NOT_CODED)
1688                          }                                          pMB->cbp =
1689                          else                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1690                          {                                                                                    dct_codes, qcoeff);
1691                            } else {
1692                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1693                                  MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1694                                                                      dct_codes, qcoeff);
1695                          }                          }
1696    
1697                          start_timer();                          start_timer();
1698                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1699                          stop_prediction_timer();                          stop_prediction_timer();
1700    
1701                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1702                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1703                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1704                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1705                                   pMB->mvs[0].x || pMB->mvs[0].y ||                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
                                  pMB->mvs[1].x || pMB->mvs[1].y ||  
                                  pMB->mvs[2].x || pMB->mvs[2].y ||  
                                  pMB->mvs[3].x || pMB->mvs[3].y)  
                         {  
1706                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1707                            }  else {
1708                                    pEnc->sStat.ublks++;
1709                          }                          }
1710    
1711                            start_timer();
1712    
1713                            /* Finished processing the MB, now check if to CODE or SKIP */
1714    
1715                            skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) &
1716                                                            (pMB->dquant == NO_CHANGE);
1717    
1718                            if(pEnc->mbParam.m_quarterpel)
1719                                    skip_possible &= (pMB->qmvs[0].x == 0) & (pMB->qmvs[0].y == 0);
1720                          else                          else
1721                                    skip_possible &= (pMB->mvs[0].x == 0) & (pMB->mvs[0].y == 0);
1722    
1723                            if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1724    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1725    
1726                                    int bSkip = 1;
1727                                    pMB->mode = MODE_NOT_CODED;
1728    
1729                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1730                          {                          {
1731                                  pEnc->sStat.ublks++;                                          int iSAD;
1732                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1733                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1734                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1735                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1736                                            {       bSkip = 0;
1737                                                    break;
1738                                            }
1739                                    }
1740                                    if (!bSkip)
1741                                    {
1742                                            VECTOR predMV;
1743                                            if(pEnc->mbParam.m_quarterpel)
1744                                                    predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1745                                            else
1746                                                    predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1747                                            pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y;
1748                                            pMB->mode = MODE_INTER;
1749                                            pMB->cbp = 0;
1750                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1751                                    }
1752                                    else MBSkip(bs);
1753    
1754                            } else {
1755                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1756                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1757                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1758                                            qcoeff[5*64+0]=0;
1759                                    }
1760                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1761                          }                          }
1762    
                         start_timer();  
                         MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);  
1763                          stop_coding_timer();                          stop_coding_timer();
1764                  }                  }
1765          }          }
1766    
1767          emms();          emms();
1768    
1769          if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1770                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1771          }          }
1772    
# Line 800  Line 1775 
1775    
1776          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);
1777    
1778          iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1779    
1780          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1781              && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128
1782          {          {
1783                  pEnc->mbParam.fixed_code++;                  pEnc->mbParam.m_fcode++;
1784                  iSearchRange *= 2;                  iSearchRange *= 2;
1785          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1786                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1787                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1788                   && (pEnc->mbParam.fixed_code >= 2))    // minimum search range 16                          && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16
1789          {          {
1790                  pEnc->mbParam.fixed_code--;                  pEnc->mbParam.m_fcode--;
1791                  iSearchRange /= 2;                  iSearchRange /= 2;
1792          }          }
1793    
1794          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1795    
1796    #ifdef FRAMEDROP
1797            /* frame drop code */
1798            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1799            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1800                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1801            {
1802                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1803                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1804    
1805                    BitstreamReset(bs);
1806    
1807                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1808                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1809    
1810                    // copy reference frame details into the current frame
1811                    pEnc->current->quant = pEnc->reference->quant;
1812                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1813                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1814                    pEnc->current->fcode = pEnc->reference->fcode;
1815                    pEnc->current->bcode = pEnc->reference->bcode;
1816                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1817                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1818    
1819            }
1820    #endif
1821    
1822          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1823    
1824          return 0;                                        // inter          return 0;                                        // inter
1825  }  }
1826    
1827    
1828    static __inline void
1829  /*  FrameCodeB(Encoder * pEnc,
1830  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)                     FRAMEINFO * frame,
1831                       Bitstream * bs,
1832                       uint32_t * pBits)
1833  {  {
1834      int16_t dct_codes[6][64];          int16_t dct_codes[6 * 64];
1835      int16_t qcoeff[6][64];          int16_t qcoeff[6 * 64];
1836      uint32_t x, y;      uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1837    
1838      IMAGE *f_ref = &pEnc->reference->image;      IMAGE *f_ref = &pEnc->reference->image;
1839          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1840    
1841    #ifdef BFRAMES_DEC_DEBUG
1842            FILE *fp;
1843            static char first=0;
1844    #define BFRAME_DEBUG    if (!first && fp){ \
1845                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1846            }
1847    
1848            if (!first){
1849                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1850            }
1851    #endif
1852    
1853          // forward          // forward
1854          image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          image_setedges(f_ref, pEnc->mbParam.edged_width,
1855                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1856                                       pEnc->mbParam.height);
1857          start_timer();          start_timer();
1858          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1859                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1860                                              pEnc->mbParam.m_quarterpel, 0);
1861          stop_inter_timer();          stop_inter_timer();
1862    
1863          // backward          // backward
1864          image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          image_setedges(b_ref, pEnc->mbParam.edged_width,
1865                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1866                                       pEnc->mbParam.height);
1867      start_timer();      start_timer();
1868          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1869                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1870                                              pEnc->mbParam.m_quarterpel, 0);
1871          stop_inter_timer();          stop_inter_timer();
1872    
1873          start_timer();          start_timer();
1874    
1875          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1876                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
1877                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
1878                            pEnc->reference->mbs, f_ref,
1879                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1880                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1881                                                     &pEnc->vInterV, &pEnc->vInterHV);
1882    
1883    
1884          stop_motion_timer();          stop_motion_timer();
1885    
1886          if (test_quant_type(&pEnc->mbParam, pEnc->current))          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))
1887          {          {
1888                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
1889          }             } */
1890    
1891      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
1892      BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0,  
1893                          frame->quant, frame->fcode, frame->bcode);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1894            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1895    
1896      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1897    
# Line 876  Line 1901 
1901          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1902    
1903    
1904      for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1905          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1906                  // reset prediction                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1907                            int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0;
                 forward.x = 0;  
                 forward.y = 0;  
                 backward.x = 0;  
                 backward.y = 0;  
   
                 for (x = 0; x < pEnc->mbParam.mb_width; x++)  
                 {  
                         MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];  
1908    
1909                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1910                          if (mb->mode == MODE_NOT_CODED)                          if (mb->mode == MODE_NOT_CODED) {
1911                          {                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].x = 0;  
                                 mb->mvs[0].y = 0;  
1912                                  continue;                                  continue;
1913                          }                          }
1914    
1915                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1916                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1917                                          f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1918                                          b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1919                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1920                                          dct_codes);                                          dct_codes);
1921    
1922                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1923                          mb->quant = frame->quant;                          mb->quant = frame->quant;
                         mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff);  
                         //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);  
   
1924    
1925                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                                  mb->cbp =
1926                                  mb->cbp == 0 &&                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
                                 mb->mvs[0].x == 0 &&  
                                 mb->mvs[0].y == 0)  
                         {  
                                 mb->mode = 5;  // skipped  
                         }  
1927    
1928                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1929                          {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1930                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped
                                 mb->pmvs[0].y = mb->mvs[0].y - forward.y;  
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
1931                          }                          }
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)  
                         {  
                                 mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;  
                                 mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;  
                                 backward.x = mb->b_mvs[0].x;  
                                 backward.y = mb->b_mvs[0].y;  
1932                          }                          }
1933    
1934  //                      printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);  #ifdef BFRAMES_DEC_DEBUG
1935            BFRAME_DEBUG
1936    #endif
1937                          start_timer();                          start_timer();
1938                          MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1939                                                     &pEnc->sStat, direction);
1940                          stop_coding_timer();                          stop_coding_timer();
1941                  }                  }
1942          }          }
# Line 947  Line 1947 
1947    
1948          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1949    
1950    #ifdef BFRAMES_DEC_DEBUG
1951            if (!first){
1952                    first=1;
1953                    if (fp)
1954                            fclose(fp);
1955            }
1956    #endif
1957  }  }
1958    
1959    
1960    /*      in case internal output is needed somewhere... */
1961    /*      {
1962            FILE *filehandle;
1963            filehandle=fopen("last-b.pgm","wb");
1964            if (filehandle)
1965            {
1966                    fprintf(filehandle,"P5\n\n");           //
1967                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1968                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1969                    fclose(filehandle);
1970                    }
1971            }
1972  */  */

Legend:
Removed from v.1.25  
changed lines
  Added in v.1.76.2.12

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