[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.4, Sat Mar 9 21:44:47 2002 UTC revision 1.76.2.1, Mon Sep 23 20:36:01 2002 UTC
# Line 1  Line 1 
1    /*****************************************************************************
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 16  Line 68 
68  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
69  #include "quant/adapt_quant.h"  #include "quant/adapt_quant.h"
70  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
71    #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 48  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 70  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          if (pParam->bitrate <= 0)          /* Bitrate allocator defaults */
183                  pParam->bitrate = 900000;  
184            if (pParam->rc_bitrate <= 0)
185                    pParam->rc_bitrate = 900000;
186    
187            if (pParam->rc_reaction_delay_factor <= 0)
188                    pParam->rc_reaction_delay_factor = 16;
189    
190          if (pParam->rc_buffersize <= 0)          if (pParam->rc_averaging_period <= 0)
191                  pParam->rc_buffersize = pParam->bitrate * pParam->fbase;                  pParam->rc_averaging_period = 100;
192    
193            if (pParam->rc_buffer <= 0)
194                    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;
# Line 89  Line 201 
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    
         if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */  
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
   
204          if (pParam->max_quantizer < pParam->min_quantizer)          if (pParam->max_quantizer < pParam->min_quantizer)
205                  pParam->max_quantizer = pParam->min_quantizer;                  pParam->max_quantizer = pParam->min_quantizer;
206    
207          if ((pEnc = (Encoder *) malloc(sizeof(Encoder))) == NULL)          /* 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;                  return XVID_ERR_MEMORY;
215    
216            /* Zero the Encoder Structure */
217    
218            memset(pEnc, 0, sizeof(Encoder));
219    
220          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
221    
222          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 109  Line 228 
228          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          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;          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;          pEnc->sStat.fMvPrevSigma = -1;
237    
238          /* Fill rate control parameters */          /* 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    
414            pEnc->mbParam.m_seconds = 0;
415            pEnc->mbParam.m_ticks = 0;
416            pEnc->m_framenum = 0;
417            pEnc->last_pframe = 0;
418            pEnc->last_sync = 0;
419    
420            pParam->handle = (void *) pEnc;
421    
422            if (pParam->rc_bitrate) {
423                    RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
424                                                    pParam->rc_reaction_delay_factor,
425                                                    pParam->rc_averaging_period, pParam->rc_buffer,
426                                                    pParam->fbase * 1000 / pParam->fincr,
427                                                    pParam->max_quantizer, pParam->min_quantizer);
428            }
429    
430            init_timer();
431    
432            return XVID_ERR_OK;
433    
434            /*
435             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
436             */
437    
438      xvid_err_memory5:
439    
440    
441            if (pEnc->mbParam.max_bframes > 0) {
442    
443                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
444                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
445                                                      pEnc->mbParam.edged_height);
446                    }
447                    xvid_free(pEnc->queue);
448            }
449    
450      xvid_err_memory4:
451    
452            if (pEnc->mbParam.max_bframes > 0) {
453    
454                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
455    
456                            if (pEnc->bframes[i] == NULL)
457                                    continue;
458    
459                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
460                                                      pEnc->mbParam.edged_height);
461    
462                            xvid_free(pEnc->bframes[i]->mbs);
463    
464                            xvid_free(pEnc->bframes[i]);
465    
466                    }
467    
468                    xvid_free(pEnc->bframes);
469            }
470    
471      xvid_err_memory3:
472    #ifdef _DEBUG_PSNR
473            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
474                                      pEnc->mbParam.edged_height);
475    #endif
476    
477            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
478                                      pEnc->mbParam.edged_height);
479            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
480                                      pEnc->mbParam.edged_height);
481            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
482                                      pEnc->mbParam.edged_height);
483    
484            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
485                                      pEnc->mbParam.edged_height);
486            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
487                                      pEnc->mbParam.edged_height);
488            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
489                                      pEnc->mbParam.edged_height);
490            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
491                                      pEnc->mbParam.edged_height);
492            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
493                                      pEnc->mbParam.edged_height);
494            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
495                                      pEnc->mbParam.edged_height);
496            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
497                                      pEnc->mbParam.edged_height);
498    
499      xvid_err_memory2:
500            xvid_free(pEnc->current->mbs);
501            xvid_free(pEnc->reference->mbs);
502    
503      xvid_err_memory1:
504            xvid_free(pEnc->current);
505            xvid_free(pEnc->reference);
506            xvid_free(pEnc);
507    
508            pParam->handle = NULL;
509    
510            return XVID_ERR_MEMORY;
511    }
512    
513    /*****************************************************************************
514     * Encoder destruction
515     *
516     * This function destroy the entire encoder structure created by a previous
517     * successful encoder_create call.
518     *
519     * Returned values (for now only one returned value) :
520     *    - XVID_ERR_OK     - no errors
521     *
522     ****************************************************************************/
523    
524    int
525    encoder_destroy(Encoder * pEnc)
526    {
527            int i;
528    
529            ENC_CHECK(pEnc);
530    
531            /* B Frames specific */
532            if (pEnc->mbParam.max_bframes > 0) {
533    
534                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
535    
536                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
537                                              pEnc->mbParam.edged_height);
538                    }
539                    xvid_free(pEnc->queue);
540            }
541    
542    
543            if (pEnc->mbParam.max_bframes > 0) {
544    
545                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
546    
547                            if (pEnc->bframes[i] == NULL)
548                                    continue;
549    
550                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
551                                              pEnc->mbParam.edged_height);
552    
553                            xvid_free(pEnc->bframes[i]->mbs);
554    
555                            xvid_free(pEnc->bframes[i]);
556                    }
557    
558                    xvid_free(pEnc->bframes);
559    
560            }
561    
562            /* All images, reference, current etc ... */
563    
564            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
565                                      pEnc->mbParam.edged_height);
566            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
567                                      pEnc->mbParam.edged_height);
568            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
569                                      pEnc->mbParam.edged_height);
570            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
571                                      pEnc->mbParam.edged_height);
572            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
573                                      pEnc->mbParam.edged_height);
574            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
575                                      pEnc->mbParam.edged_height);
576            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
577                                      pEnc->mbParam.edged_height);
578    
579            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
580                                      pEnc->mbParam.edged_height);
581            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
582                                      pEnc->mbParam.edged_height);
583            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
584                                      pEnc->mbParam.edged_height);
585    
586    #ifdef _DEBUG_PSNR
587            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
588                                      pEnc->mbParam.edged_height);
589    #endif
590    
591            /* Encoder structure */
592    
593            xvid_free(pEnc->current->mbs);
594            xvid_free(pEnc->current);
595    
596            xvid_free(pEnc->reference->mbs);
597            xvid_free(pEnc->reference);
598    
599            xvid_free(pEnc);
600    
601            return XVID_ERR_OK;
602    }
603    
604    
605    static __inline void inc_frame_num(Encoder * pEnc)
606    {
607            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
608    
609            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
610            if (pEnc->mbParam.m_ticks < pEnc->last_sync)
611                    pEnc->mbParam.m_seconds = 1;
612                                    // more than 1 second since last I or P is not supported.
613            else
614                    pEnc->mbParam.m_seconds = 0;
615    
616    }
617    
618    
619    static __inline void
620    queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
621    {
622            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
623            {
624                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
625                    return;
626            }
627    
628            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
629                                    pEnc->bframenum_head, pEnc->bframenum_tail,
630                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
631    
632    
633            start_timer();
634            if (image_input
635                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
636                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
637                    return;
638            stop_conv_timer();
639    
640            pEnc->queue_size++;
641            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
642    }
643    
644    
645    
646    /*****************************************************************************
647     * IPB frame encoder entry point
648     *
649     * Returned values :
650     *    - XVID_ERR_OK     - no errors
651     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
652     *                        format
653     ****************************************************************************/
654    
655    int
656    encoder_encode_bframes(Encoder * pEnc,
657                               XVID_ENC_FRAME * pFrame,
658                               XVID_ENC_STATS * pResult)
659    {
660            uint16_t x, y;
661            Bitstream bs;
662            uint32_t bits;
663    
664            int input_valid = 1;
665    
666    #ifdef _DEBUG_PSNR
667            float psnr;
668            char temp[128];
669    #endif
670    
671            ENC_CHECK(pEnc);
672            ENC_CHECK(pFrame);
673            ENC_CHECK(pFrame->image);
674    
675            start_global_timer();
676    
677            BitstreamInit(&bs, pFrame->bitstream, 0);
678    
679    ipvop_loop:
680    
681            /*
682             * bframe "flush" code
683             */
684    
685            if ((pFrame->image == NULL || pEnc->flush_bframes)
686                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
687    
688                    if (pEnc->flush_bframes == 0) {
689                            /*
690                             * we have reached the end of stream without getting
691                             * a future reference frame... so encode last final
692                             * frame as a pframe
693                             */
694    
695                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
696                                    pEnc->bframenum_head, pEnc->bframenum_tail,
697                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
698    
699                            pEnc->bframenum_tail--;
700                            SWAP(pEnc->current, pEnc->reference);
701    
702                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
703    
704                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
705    
706                            BitstreamPad(&bs);
707                            pFrame->length = BitstreamLength(&bs);
708                            pFrame->intra = 0;
709    
710                            return XVID_ERR_OK;
711                    }
712    
713    
714                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
715                                    pEnc->bframenum_head, pEnc->bframenum_tail,
716                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
717    
718                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
719                    pEnc->bframenum_head++;
720    
721                    BitstreamPad(&bs);
722                    pFrame->length = BitstreamLength(&bs);
723                    pFrame->intra = 0;
724    
725                    if (input_valid)
726                            queue_image(pEnc, pFrame);
727    
728                    return XVID_ERR_OK;
729            }
730    
731            if (pEnc->bframenum_head > 0) {
732                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
733    
734                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
735    
736                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
737                                    pEnc->bframenum_head, pEnc->bframenum_tail,
738                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
739    
740                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
741                            BitstreamPad(&bs);
742                            BitstreamPutBits(&bs, 0x7f, 8);
743    
744                            pFrame->length = BitstreamLength(&bs);
745                            pFrame->intra = 0;
746    
747                            if (input_valid)
748                                    queue_image(pEnc, pFrame);
749    
750                            return XVID_ERR_OK;
751                    }
752            }
753    
754    
755    bvop_loop:
756    
757            if (pEnc->bframenum_dx50bvop != -1)
758            {
759    
760                    SWAP(pEnc->current, pEnc->reference);
761                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
762    
763                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
764                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
765                    }
766    
767                    if (input_valid)
768                    {
769                            queue_image(pEnc, pFrame);
770                            input_valid = 0;
771                    }
772    
773            } else if (input_valid) {
774    
775                    SWAP(pEnc->current, pEnc->reference);
776    
777                    start_timer();
778                    if (image_input
779                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
780                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
781                            return XVID_ERR_FORMAT;
782                    stop_conv_timer();
783    
784                    // queue input frame, and dequue next image
785                    if (pEnc->queue_size > 0)
786                    {
787                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
788                            if (pEnc->queue_head != pEnc->queue_tail)
789                            {
790                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
791                            }
792                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
793                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
794                    }
795    
796            } else if (pEnc->queue_size > 0) {
797    
798                    SWAP(pEnc->current, pEnc->reference);
799    
800                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
801                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
802                    pEnc->queue_size--;
803    
804            } else if (BitstreamPos(&bs) == 0) {
805    
806                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
807                                    pEnc->bframenum_head, pEnc->bframenum_tail,
808                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
809    
810                    pFrame->intra = 0;
811    
812                    BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
813                    BitstreamPad(&bs);
814                    pFrame->length = BitstreamLength(&bs);
815    
816                    return XVID_ERR_OK;
817    
818            } else {
819    
820                    pFrame->length = BitstreamLength(&bs);
821                    return XVID_ERR_OK;
822            }
823    
824            pEnc->flush_bframes = 0;
825    
826            emms();
827    
828            // only inc frame num, adapt quant, etc. if we havent seen it before
829            if (pEnc->bframenum_dx50bvop < 0 )
830            {
831                    if (pFrame->quant == 0)
832                            pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
833                    else
834                            pEnc->current->quant = pFrame->quant;
835    
836    /*              if (pEnc->current->quant < 1)
837                            pEnc->current->quant = 1;
838    
839                    if (pEnc->current->quant > 31)
840                            pEnc->current->quant = 31;
841    */
842                    pEnc->current->global_flags = pFrame->general;
843                    pEnc->current->motion_flags = pFrame->motion;
844    
845                    /* ToDo : dynamic fcode (in both directions) */
846                    pEnc->current->fcode = pEnc->mbParam.m_fcode;
847                    pEnc->current->bcode = pEnc->mbParam.m_fcode;
848    
849                    pEnc->current->seconds = pEnc->mbParam.m_seconds;
850                    pEnc->current->ticks = pEnc->mbParam.m_ticks;
851    
852                    inc_frame_num(pEnc);
853    
854    #ifdef _DEBUG_PSNR
855                    image_copy(&pEnc->sOriginal, &pEnc->current->image,
856                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
857    #endif
858    
859                    emms();
860    
861                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
862                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
863                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
864                    }
865    
866            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867             * Luminance masking
868             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
869    
870                    if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
871                            int *temp_dquants =
872                                    (int *) xvid_malloc(pEnc->mbParam.mb_width *
873                                                                    pEnc->mbParam.mb_height * sizeof(int),
874                                                                    CACHE_LINE);
875    
876                            pEnc->current->quant =
877                                    adaptive_quantization(pEnc->current->image.y,
878                                                                      pEnc->mbParam.edged_width, temp_dquants,
879                                                                      pEnc->current->quant, pEnc->current->quant,
880                                                                      2 * pEnc->current->quant,
881                                                                      pEnc->mbParam.mb_width,
882                                                                      pEnc->mbParam.mb_height);
883    
884                            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
885    
886    #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
887    
888                                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
889                                            MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
890    
891                                            pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
892                                    }
893    
894    #undef OFFSET
895                            }
896    
897                            xvid_free(temp_dquants);
898                    }
899    
900            }
901    
902            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
903             * ivop/pvop/bvop selection
904             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
905    
906    
907            if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
908                    (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
909                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
910                    || image_mad(&pEnc->reference->image, &pEnc->current->image,
911                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
912                                             pEnc->mbParam.height) > 30) {
913                    /*
914                     * This will be coded as an Intra Frame
915                     */
916    
917                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
918                                    pEnc->bframenum_head, pEnc->bframenum_tail,
919                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
920    
921                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
922                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
923                    }
924    
925                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
926    
927                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
928    
929                            pEnc->bframenum_tail--;
930                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
931    
932                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
933                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
934                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
935                            }
936                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
937    
938                            pFrame->intra = 0;
939    
940                    } else {
941    
942                            FrameCodeI(pEnc, &bs, &bits);
943                            pFrame->intra = 1;
944    
945                            pEnc->bframenum_dx50bvop = -1;
946                    }
947    
948                    pEnc->flush_bframes = 1;
949    
950                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
951                            BitstreamPad(&bs);
952                            input_valid = 0;
953                            goto ipvop_loop;
954                    }
955    
956                    /*
957                     * NB : sequences like "IIBB" decode fine with msfdam but,
958                     *      go screwy with divx 5.00
959                     */
960            } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
961                    /*
962                     * This will be coded as a Predicted Frame
963                     */
964    
965          pEnc->mbParam.quant = 4;                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
966                                    pEnc->bframenum_head, pEnc->bframenum_tail,
967                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
968    
969          pEnc->bitrate = pParam->bitrate;                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
970                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
971                    }
972    
973          pEnc->iFrameNum = 0;                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
974          pEnc->iMaxKeyInterval = pParam->max_key_interval;                  pFrame->intra = 0;
975                    pEnc->flush_bframes = 1;
976    
977          if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
978          {                          BitstreamPad(&bs);
979                  free(pEnc);                          input_valid = 0;
980                  return XVID_ERR_MEMORY;                          goto ipvop_loop;
981          }          }
982    
983          if (image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          } else {
984          {                  /*
985                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                   * This will be coded as a Bidirectional Frame
986                  free(pEnc);                   */
                 return XVID_ERR_MEMORY;  
         }  
987    
988          if (image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
989          {                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
                 image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 free(pEnc);  
                 return XVID_ERR_MEMORY;  
990          }          }
991    
992          if (image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                  if (pFrame->bquant < 1) {
993          {                          pEnc->current->quant =
994                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                  ((pEnc->reference->quant +
995                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;
996                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  } else {
997                  free(pEnc);                          pEnc->current->quant = pFrame->bquant;
                 return XVID_ERR_MEMORY;  
998          }          }
999                    if (pEnc->current->quant < 1)
1000                            pEnc->current->quant = 1;
1001    
1002          if (image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                  if (pEnc->current->quant > 31)
1003          {                          pEnc->current->quant = 31;
                 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);  
                 free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
1004    
         pEnc->pMBs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);  
         if (pEnc->pMBs == 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->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
1005    
1006          // init macroblock array                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1007          for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++)                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1008          {                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
                 pEnc->pMBs[i].dquant = NO_CHANGE;  
         }  
1009    
         pParam->handle = (void *)pEnc;  
1010    
1011          if (pParam->bitrate)  
1012          {                  /* store frame into bframe buffer & swap ref back to current */
1013                  RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase, pParam->width,                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1014                                  pParam->height, pParam->max_quantizer, pParam->min_quantizer);                  SWAP(pEnc->current, pEnc->reference);
1015    
1016                    pEnc->bframenum_tail++;
1017    
1018                    pFrame->intra = 0;
1019                    pFrame->length = 0;
1020    
1021                    input_valid = 0;
1022                    goto bvop_loop;
1023          }          }
1024    
1025          create_vlc_tables();          pEnc->iFrameNum++;
1026    
1027          return XVID_ERR_OK;          BitstreamPad(&bs);
1028            pFrame->length = BitstreamLength(&bs);
1029    
1030            if (pResult) {
1031                    pResult->quant = pEnc->current->quant;
1032                    pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1033                    pResult->kblks = pEnc->sStat.kblks;
1034                    pResult->mblks = pEnc->sStat.mblks;
1035                    pResult->ublks = pEnc->sStat.ublks;
1036  }  }
1037    
1038            emms();
1039    
1040  int encoder_destroy(Encoder * pEnc)  #ifdef _DEBUG_PSNR
1041  {          psnr =
1042          ENC_CHECK(pEnc);                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1043          ENC_CHECK(pEnc->sCurrent.y);                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
1044          ENC_CHECK(pEnc->sReference.y);                                     pEnc->mbParam.height);
1045    
1046            snprintf(temp, 127, "PSNR: %f\n", psnr);
1047            DEBUG(temp);
1048    #endif
1049    
1050            if (pFrame->quant == 0) {
1051                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1052                                                      pFrame->length, pFrame->intra);
1053            }
1054    
         free(pEnc->pMBs);  
         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->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         free(pEnc);  
1055    
1056          destroy_vlc_tables();          stop_global_timer();
1057            write_timer();
1058    
1059          return XVID_ERR_OK;          return XVID_ERR_OK;
1060  }  }
1061    
1062  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
1063    
1064    /*****************************************************************************
1065     * "original" IP frame encoder entry point
1066     *
1067     * Returned values :
1068     *    - XVID_ERR_OK     - no errors
1069     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1070     *                        format
1071     ****************************************************************************/
1072    
1073    int
1074    encoder_encode(Encoder * pEnc,
1075                               XVID_ENC_FRAME * pFrame,
1076                               XVID_ENC_STATS * pResult)
1077  {  {
1078          uint16_t x, y;          uint16_t x, y;
1079          Bitstream bs;          Bitstream bs;
1080          uint32_t bits;          uint32_t bits;
1081          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1082    
1083    #ifdef _DEBUG_PSNR
1084            float psnr;
1085            uint8_t temp[128];
1086    #endif
1087    
1088          start_global_timer();          start_global_timer();
1089    
1090          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
# Line 225  Line 1092 
1092          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1093          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1094    
1095          pEnc->mbParam.global_flags = pFrame->general;          SWAP(pEnc->current, pEnc->reference);
1096          pEnc->mbParam.motion_flags = pFrame->motion;  
1097            pEnc->current->global_flags = pFrame->general;
1098            pEnc->current->motion_flags = pFrame->motion;
1099            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1100            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1101            pEnc->mbParam.hint = &pFrame->hint;
1102    
1103          start_timer();          start_timer();
1104          if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          if (image_input
1105                          pFrame->image, pFrame->colorspace))                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1106          {                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
1107                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
         }  
1108          stop_conv_timer();          stop_conv_timer();
1109    
1110    #ifdef _DEBUG_PSNR
1111            image_copy(&pEnc->sOriginal, &pEnc->current->image,
1112                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1113    #endif
1114    
1115            emms();
1116    
1117          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1118    
1119          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1120          {                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
1121                  pEnc->mbParam.quant = RateControlGetQ(0);          } else {
1122          }                  pEnc->current->quant = pFrame->quant;
         else  
         {  
                 pEnc->mbParam.quant = pFrame->quant;  
1123          }          }
1124    
1125          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0)          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1126          {                  int *temp_dquants =
1127                  int * temp_dquants = (int *) malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int));                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
1128                                                                    pEnc->mbParam.mb_height * sizeof(int),
1129                                                                    CACHE_LINE);
1130    
1131                  pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, pEnc->mbParam.width,                  pEnc->current->quant =
1132                                                              temp_dquants, pFrame->quant, pFrame->quant,                          adaptive_quantization(pEnc->current->image.y,
1133                                                              2*pFrame->quant, pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);                                                                    pEnc->mbParam.edged_width, temp_dquants,
1134                                                                      pEnc->current->quant, pEnc->current->quant,
1135                                                                      2 * pEnc->current->quant,
1136                                                                      pEnc->mbParam.mb_width,
1137                                                                      pEnc->mbParam.mb_height);
1138    
1139                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1140                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1141                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1142                                  MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1143                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1144    
1145    
1146                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1147    
1148                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1149                          }                          }
1150                  free(temp_dquants);  
1151    #undef OFFSET
1152          }          }
1153    
1154          if(pEnc->mbParam.global_flags & XVID_H263QUANT) {                  xvid_free(temp_dquants);
                 if(pEnc->mbParam.quant_type != H263_QUANT)  
                         write_vol_header = 1;  
                 pEnc->mbParam.quant_type = H263_QUANT;  
1155          }          }
         else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) {  
                 int ret1, ret2;  
1156    
1157                  if(pEnc->mbParam.quant_type != MPEG4_QUANT)          if (pEnc->current->global_flags & XVID_H263QUANT) {
1158                    if (pEnc->mbParam.m_quant_type != H263_QUANT)
1159                            write_vol_header = 1;
1160                    pEnc->mbParam.m_quant_type = H263_QUANT;
1161            } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1162                    int matrix1_changed, matrix2_changed;
1163    
1164                    matrix1_changed = matrix2_changed = 0;
1165    
1166                    if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1167                          write_vol_header = 1;                          write_vol_header = 1;
1168    
1169                  pEnc->mbParam.quant_type = MPEG4_QUANT;                  pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1170    
1171                  if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1172                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1173                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1174                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1175                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1176                  }                  } else {
1177                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1178                          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());  
1179                  }                  }
1180                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1181                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1182          }          }
1183    
1184          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1185          {                  if ((pEnc->iFrameNum == 0)
1186                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
1187                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
1188                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1189                  else                  } else {
1190                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1191          }          }
1192          else          } else {
1193          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
1194                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1195                  else                  } else {
1196                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1197          }          }
1198    
1199            }
1200    
1201          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1202          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1203          BitstreamPad(&bs);          BitstreamPad(&bs);
1204          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1205    
1206          if (pResult)          if (pResult) {
1207          {                  pResult->quant = pEnc->current->quant;
                 pResult->quant = pEnc->mbParam.quant;  
1208                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1209                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
1210                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->sStat.mblks;
1211                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1212          }          }
1213    
1214          if (pEnc->bitrate)          emms();
1215          {  
1216                  RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra);          if (pFrame->quant == 0) {
1217          }                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1218                                                      pFrame->length, pFrame->intra);
1219            }
1220    #ifdef _DEBUG_PSNR
1221            psnr =
1222                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1223                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1224                                       pEnc->mbParam.height);
1225    
1226            snprintf(temp, 127, "PSNR: %f\n", psnr);
1227            DEBUG(temp);
1228    #endif
1229    
1230            inc_frame_num(pEnc);
1231          pEnc->iFrameNum++;          pEnc->iFrameNum++;
         image_swap(&pEnc->sCurrent, &pEnc->sReference);  
1232    
1233          stop_global_timer();          stop_global_timer();
1234          write_timer();          write_timer();
# Line 337  Line 1237 
1237  }  }
1238    
1239    
1240  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1241    CodeIntraMB(Encoder * pEnc,
1242                            MACROBLOCK * pMB)
1243    {
1244    
1245          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1246    
1247          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {          /* zero mv statistics */
1248                  if(pMB->dquant != NO_CHANGE)          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1249                  {          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1250            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1251            pMB->sad16 = 0;
1252    
1253            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1254                    if (pMB->dquant != NO_CHANGE) {
1255                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1256                          pEnc->mbParam.quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1257    
1258                            if (pEnc->current->quant > 31)
1259                                    pEnc->current->quant = 31;
1260                            if (pEnc->current->quant < 1)
1261                                    pEnc->current->quant = 1;
1262                    }
1263            }
1264    
1265            pMB->quant = pEnc->current->quant;
1266    }
1267    
1268    
1269    #define FCODEBITS       3
1270    #define MODEBITS        5
1271    
1272    void
1273    HintedMESet(Encoder * pEnc,
1274                            int *intra)
1275    {
1276            HINTINFO *hint;
1277            Bitstream bs;
1278            int length, high;
1279            uint32_t x, y;
1280    
1281            hint = pEnc->mbParam.hint;
1282    
1283            if (hint->rawhints) {
1284                    *intra = hint->mvhint.intra;
1285            } else {
1286                    BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1287                    *intra = BitstreamGetBit(&bs);
1288            }
1289    
1290            if (*intra) {
1291                    return;
1292            }
1293    
1294            pEnc->current->fcode =
1295                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1296                                                                                                                                     FCODEBITS);
1297    
1298            length = pEnc->current->fcode + 5;
1299            high = 1 << (length - 1);
1300    
1301            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1302                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1303                            MACROBLOCK *pMB =
1304                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1305                            MVBLOCKHINT *bhint =
1306                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1307                            VECTOR pred;
1308                            VECTOR tmp;
1309                            int vec;
1310    
1311                            pMB->mode =
1312                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1313                                                                                                                                      MODEBITS);
1314    
1315                            pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1316                            pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1317    
1318                            if (pMB->mode == MODE_INTER) {
1319                                    tmp.x =
1320                                            (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1321                                                                                                                                                      length);
1322                                    tmp.y =
1323                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1324                                                                                                                                                      length);
1325                                    tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1326                                    tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1327    
1328                                    pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
1329    
1330                                    for (vec = 0; vec < 4; ++vec) {
1331                                            pMB->mvs[vec].x = tmp.x;
1332                                            pMB->mvs[vec].y = tmp.y;
1333                                            pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1334                                            pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1335                                    }
1336                            } else if (pMB->mode == MODE_INTER4V) {
1337                                    for (vec = 0; vec < 4; ++vec) {
1338                                            tmp.x =
1339                                                    (hint->rawhints) ? bhint->mvs[vec].
1340                                                    x : BitstreamGetBits(&bs, length);
1341                                            tmp.y =
1342                                                    (hint->rawhints) ? bhint->mvs[vec].
1343                                                    y : BitstreamGetBits(&bs, length);
1344                                            tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1345                                            tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1346    
1347                                            pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
1348    
1349                                            pMB->mvs[vec].x = tmp.x;
1350                                            pMB->mvs[vec].y = tmp.y;
1351                                            pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1352                                            pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1353                                    }
1354                            } else                          // intra / stuffing / not_coded
1355                            {
1356                                    for (vec = 0; vec < 4; ++vec) {
1357                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1358                                    }
1359                            }
1360    
1361                            if (pMB->mode == MODE_INTER4V &&
1362                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
1363                                    && pMB->dquant != NO_CHANGE) {
1364                                    pMB->mode = MODE_INTRA;
1365    
1366                                    for (vec = 0; vec < 4; ++vec) {
1367                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1368                                    }
1369                            }
1370                    }
1371            }
1372    }
1373    
1374    
1375    void
1376    HintedMEGet(Encoder * pEnc,
1377                            int intra)
1378    {
1379            HINTINFO *hint;
1380            Bitstream bs;
1381            uint32_t x, y;
1382            int length, high;
1383    
1384            hint = pEnc->mbParam.hint;
1385    
1386            if (hint->rawhints) {
1387                    hint->mvhint.intra = intra;
1388            } else {
1389                    BitstreamInit(&bs, hint->hintstream, 0);
1390                    BitstreamPutBit(&bs, intra);
1391            }
1392    
1393            if (intra) {
1394                    if (!hint->rawhints) {
1395                            BitstreamPad(&bs);
1396                            hint->hintlength = BitstreamLength(&bs);
1397                    }
1398                    return;
1399            }
1400    
1401            length = pEnc->current->fcode + 5;
1402            high = 1 << (length - 1);
1403    
1404            if (hint->rawhints) {
1405                    hint->mvhint.fcode = pEnc->current->fcode;
1406            } else {
1407                    BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1408            }
1409    
1410            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1411                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1412                            MACROBLOCK *pMB =
1413                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1414                            MVBLOCKHINT *bhint =
1415                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1416                            VECTOR tmp;
1417    
1418                            if (hint->rawhints) {
1419                                    bhint->mode = pMB->mode;
1420                            } else {
1421                                    BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1422                            }
1423    
1424                            if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
1425                                    tmp.x = pMB->mvs[0].x;
1426                                    tmp.y = pMB->mvs[0].y;
1427                                    tmp.x += (tmp.x < 0) ? high * 2 : 0;
1428                                    tmp.y += (tmp.y < 0) ? high * 2 : 0;
1429    
1430                                    if (hint->rawhints) {
1431                                            bhint->mvs[0].x = tmp.x;
1432                                            bhint->mvs[0].y = tmp.y;
1433                                    } else {
1434                                            BitstreamPutBits(&bs, tmp.x, length);
1435                                            BitstreamPutBits(&bs, tmp.y, length);
1436                                    }
1437                            } else if (pMB->mode == MODE_INTER4V) {
1438                                    int vec;
1439    
1440                                    for (vec = 0; vec < 4; ++vec) {
1441                                            tmp.x = pMB->mvs[vec].x;
1442                                            tmp.y = pMB->mvs[vec].y;
1443                                            tmp.x += (tmp.x < 0) ? high * 2 : 0;
1444                                            tmp.y += (tmp.y < 0) ? high * 2 : 0;
1445    
1446                          if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                          if (hint->rawhints) {
1447                          if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                  bhint->mvs[vec].x = tmp.x;
1448                                                    bhint->mvs[vec].y = tmp.y;
1449                                            } else {
1450                                                    BitstreamPutBits(&bs, tmp.x, length);
1451                                                    BitstreamPutBits(&bs, tmp.y, length);
1452                                            }
1453                                    }
1454                            }
1455                  }                  }
1456          }          }
1457    
1458          pMB->quant = pEnc->mbParam.quant;          if (!hint->rawhints) {
1459                    BitstreamPad(&bs);
1460                    hint->hintlength = BitstreamLength(&bs);
1461            }
1462  }  }
1463    
1464    
1465  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1466    FrameCodeI(Encoder * pEnc,
1467                       Bitstream * bs,
1468                       uint32_t * pBits)
1469  {  {
1470          int16_t dct_codes[6][64];  
1471          int16_t qcoeff[6][64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1472            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1473    
1474          uint16_t x, y;          uint16_t x, y;
1475    
1476          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1477          pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1478          pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1479            pEnc->current->coding_type = I_VOP;
1480    
1481          BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1482          BitstreamWriteVopHeader(bs, I_VOP, pEnc->mbParam.rounding_type,  
1483                                  pEnc->mbParam.quant,  #define DIVX501B481P "DivX501b481p"
1484                                  pEnc->mbParam.fixed_code);          if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1485                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1486            }
1487            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1488    
1489          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1490    
# Line 378  Line 1493 
1493          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1494    
1495          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1496                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1497                  {                          MACROBLOCK *pMB =
1498                          MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1499    
1500                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1501    
1502                          MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, &pEnc->sCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1503                                                              dct_codes, qcoeff);
1504    
1505                          start_timer();                          start_timer();
1506                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1507                          stop_prediction_timer();                          stop_prediction_timer();
1508    
1509                          start_timer();                          start_timer();
1510                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1511                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1512                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1513                                    qcoeff[5*64+0]=0;
1514                            }
1515                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1516                          stop_coding_timer();                          stop_coding_timer();
1517                  }                  }
1518    
# Line 401  Line 1522 
1522          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
1523          pEnc->sStat.iMvSum = 0;          pEnc->sStat.iMvSum = 0;
1524          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1525          pEnc->mbParam.fixed_code = 2;          pEnc->mbParam.m_fcode = 2;
1526    
1527            pEnc->last_pframe = pEnc->current->ticks;
1528            pEnc->last_sync = pEnc->current->ticks;
1529    
1530            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1531                    HintedMEGet(pEnc, 1);
1532            }
1533    
1534          return 1;                                        // intra          return 1;                                        // intra
1535  }  }
1536    
1537    
1538  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1539    #define BFRAME_SKIP_THRESHHOLD 30
1540    
1541  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1542    FrameCodeP(Encoder * pEnc,
1543                       Bitstream * bs,
1544                       uint32_t * pBits,
1545                       bool force_inter,
1546                       bool vol_header)
1547  {  {
1548          float fSigma;          float fSigma;
1549          int16_t dct_codes[6][64];  
1550          int16_t qcoeff[6][64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1551            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1552    
1553          int iLimit;          int iLimit;
1554          uint32_t x, y;          int x, y, k;
1555          int iSearchRange;          int iSearchRange;
1556          bool bIntra;          int bIntra;
1557    
1558          IMAGE *pCurrent = &pEnc->sCurrent;          /* IMAGE *pCurrent = &pEnc->current->image; */
1559          IMAGE *pRef = &pEnc->sReference;          IMAGE *pRef = &pEnc->reference->image;
1560    
1561          image_setedges(pRef,pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          start_timer();
1562            image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1563          pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;                                     pEnc->mbParam.width, pEnc->mbParam.height,
1564                                       pEnc->current->global_flags & XVID_INTERLACING);
1565            stop_edges_timer();
1566    
1567            pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1568            pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1569            pEnc->current->fcode = pEnc->mbParam.m_fcode;
1570    
1571          if (!force_inter)          if (!force_inter)
1572                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1573                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1574                                       INTRA_THRESHOLD);
1575          else          else
1576                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1577    
1578          if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1579                  start_timer();                  start_timer();
1580                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1581                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1582                                    pEnc->mbParam.rounding_type);                                                    pEnc->mbParam.edged_height,
1583                                                      pEnc->current->rounding_type);
1584                  stop_inter_timer();                  stop_inter_timer();
1585          }          }
1586    
1587          start_timer();          start_timer();
1588          bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1589                                    &pEnc->vInterH, &pEnc->vInterV,                  HintedMESet(pEnc, &bIntra);
1590                                    &pEnc->vInterHV, &pEnc->sCurrent, iLimit);                  if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1591                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1592    
1593            } else {
1594    
1595            bIntra =
1596                    MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1597                             &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1598                             iLimit);
1599    
1600            }
1601          stop_motion_timer();          stop_motion_timer();
1602    
1603          if (bIntra == 1)          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
                 return FrameCodeI(pEnc, bs, pBits);  
1604    
1605          pEnc->mbParam.coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1606    
1607          if(vol_header)          if(vol_header)
1608                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1609    
1610          BitstreamWriteVopHeader(bs, P_VOP, pEnc->mbParam.rounding_type,          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
                                 pEnc->mbParam.quant,  
                                 pEnc->mbParam.fixed_code);  
1611    
1612          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1613    
1614          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1615          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1616    
1617          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1618          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1619                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1620                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1621    
1622                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1623    
1624                          if (!bIntra)                          if (!bIntra) {
                         {  
1625                                  start_timer();                                  start_timer();
1626                                  MBMotionCompensation(pMB, x, y, &pEnc->sReference,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1627                                                       &pEnc->vInterH, &pEnc->vInterV,                                                       &pEnc->vInterH, &pEnc->vInterV,
1628                                                       &pEnc->vInterHV, &pEnc->sCurrent, dct_codes,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1629                                                       pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
1630                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1631                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1632                                                       pEnc->mbParam.rounding_type);                                                                           pEnc->current->rounding_type);
1633                                  stop_comp_timer();                                  stop_comp_timer();
1634    
1635                                  if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1636                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1637                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1638                                                  pEnc->mbParam.quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1639                                                  if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                                  if (pEnc->current->quant > 31)
1640                                                  else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                          pEnc->current->quant = 31;
1641                                          }                                                  else if (pEnc->current->quant < 1)
1642                                  }                                                          pEnc->current->quant = 1;
1643                                  pMB->quant = pEnc->mbParam.quant;                                          }
1644                                    }
1645                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent);                                  pMB->quant = pEnc->current->quant;
1646                          }  
1647                          else                                  pMB->field_pred = 0;
1648                          {  
1649                                    if (pMB->mode != MODE_NOT_CODED)
1650                                            pMB->cbp =
1651                                                    MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1652                                                                                      dct_codes, qcoeff);
1653                            } else {
1654                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1655                                  MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1656                                                                      dct_codes, qcoeff);
1657                          }                          }
1658    
1659                          start_timer();                          start_timer();
1660                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1661                          stop_prediction_timer();                          stop_prediction_timer();
1662    
1663                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1664                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1665                            } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1666                                               pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1667                                               pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1668                                    pEnc->sStat.mblks++;
1669                            }  else {
1670                                    pEnc->sStat.ublks++;
1671                          }                          }
1672                          else if (pMB->cbp ||  
1673                                   pMB->mvs[0].x || pMB->mvs[0].y ||                          start_timer();
1674                                   pMB->mvs[1].x || pMB->mvs[1].y ||  
1675                                   pMB->mvs[2].x || pMB->mvs[2].y ||                          /* Finished processing the MB, now check if to CODE or SKIP */
1676                                   pMB->mvs[3].x || pMB->mvs[3].y)  
1677                            if ((pMB->mode == MODE_NOT_CODED) ||
1678                                    (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1679                                    pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) {
1680    
1681    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1682    
1683                                    int bSkip = 1;
1684                                    pMB->mode = MODE_NOT_CODED;
1685    
1686                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1687                          {                          {
1688                                  pEnc->sStat.mblks++;                                          int iSAD;
1689                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1690                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1691                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1692                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1693                                            {       bSkip = 0;
1694                                                    break;
1695                          }                          }
1696                          else                                  }
1697                                    if (!bSkip)
1698                          {                          {
1699                                  pEnc->sStat.ublks++;                                          VECTOR predMV;
1700                                            predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1701                                            pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y;
1702                                            pMB->mode = MODE_INTER;
1703                                            pMB->cbp = 0;
1704                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1705                                    }
1706                                    else MBSkip(bs);
1707    
1708                            } else {
1709                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1710                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1711                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1712                                            qcoeff[5*64+0]=0;
1713                                    }
1714                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1715                          }                          }
1716    
                         start_timer();  
                         MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);  
1717                          stop_coding_timer();                          stop_coding_timer();
1718                  }                  }
1719          }          }
1720    
1721          emms();          emms();
1722    
1723            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1724                    HintedMEGet(pEnc, 0);
1725            }
1726    
1727          if (pEnc->sStat.iMvCount == 0)          if (pEnc->sStat.iMvCount == 0)
1728                  pEnc->sStat.iMvCount = 1;                  pEnc->sStat.iMvCount = 1;
1729    
1730          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);
1731    
1732          iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1733    
1734          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1735              && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128
1736          {          {
1737                  pEnc->mbParam.fixed_code++;                  pEnc->mbParam.m_fcode++;
1738                  iSearchRange *= 2;                  iSearchRange *= 2;
1739          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1740                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1741                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1742                   && (pEnc->mbParam.fixed_code >= 2))    // minimum search range 16                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16
1743          {          {
1744                  pEnc->mbParam.fixed_code--;                  pEnc->mbParam.m_fcode--;
1745                  iSearchRange /= 2;                  iSearchRange /= 2;
1746          }          }
1747    
1748          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1749    
1750    #ifdef BFRAMES
1751            /* frame drop code */
1752            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1753            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1754                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1755            {
1756                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1757                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1758    
1759                    BitstreamReset(bs);
1760                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1761    
1762                    // copy reference frame details into the current frame
1763                    pEnc->current->quant = pEnc->reference->quant;
1764                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1765                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1766                    pEnc->current->fcode = pEnc->reference->fcode;
1767                    pEnc->current->bcode = pEnc->reference->bcode;
1768                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1769                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1770    
1771            }
1772    #endif
1773    
1774          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1775    
1776            pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) %
1777                            (int32_t)pEnc->mbParam.fbase;
1778            pEnc->last_pframe = pEnc->current->ticks;
1779    
1780          return 0;                                        // inter          return 0;                                        // inter
1781  }  }
1782    
1783    
1784    static __inline void
1785    FrameCodeB(Encoder * pEnc,
1786                       FRAMEINFO * frame,
1787                       Bitstream * bs,
1788                       uint32_t * pBits)
1789    {
1790            int16_t dct_codes[6 * 64];
1791            int16_t qcoeff[6 * 64];
1792            uint32_t x, y;
1793    
1794            IMAGE *f_ref = &pEnc->reference->image;
1795            IMAGE *b_ref = &pEnc->current->image;
1796    
1797    #ifdef BFRAMES_DEC_DEBUG
1798            FILE *fp;
1799            static char first=0;
1800    #define BFRAME_DEBUG    if (!first && fp){ \
1801                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1802            }
1803    
1804            if (!first){
1805                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1806            }
1807    #endif
1808    
1809            // forward
1810            image_setedges(f_ref, pEnc->mbParam.edged_width,
1811                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1812                                       pEnc->mbParam.height,
1813                                       frame->global_flags & XVID_INTERLACING);
1814            start_timer();
1815            image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1816                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1817                                              0);
1818            stop_inter_timer();
1819    
1820            // backward
1821            image_setedges(b_ref, pEnc->mbParam.edged_width,
1822                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1823                                       pEnc->mbParam.height,
1824                                       frame->global_flags & XVID_INTERLACING);
1825            start_timer();
1826            image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1827                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1828                                              0);
1829            stop_inter_timer();
1830    
1831            start_timer();
1832            MotionEstimationBVOP(&pEnc->mbParam, frame,
1833                    ((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase,
1834                                                    pEnc->time_pp,
1835                                                    pEnc->reference->mbs, f_ref,
1836                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1837                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1838                                                     &pEnc->vInterV, &pEnc->vInterHV);
1839    
1840    
1841            stop_motion_timer();
1842    
1843            /*if (test_quant_type(&pEnc->mbParam, pEnc->current))
1844               {
1845               BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
1846               } */
1847    
1848            frame->coding_type = B_VOP;
1849            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1850    
1851            *pBits = BitstreamPos(bs);
1852    
1853            pEnc->sStat.iTextBits = 0;
1854            pEnc->sStat.iMvSum = 0;
1855            pEnc->sStat.iMvCount = 0;
1856            pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1857    
1858    
1859            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1860                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1861                            MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1862    
1863                            // decoder ignores mb when refence block is INTER(0,0), CBP=0
1864                            if (mb->mode == MODE_NOT_CODED) {
1865                                    //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
1866                                    continue;
1867                            }
1868    
1869                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1870                                    MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1871                                                                             f_ref, &pEnc->f_refh, &pEnc->f_refv,
1872                                                                             &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1873                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1874                                                                             dct_codes);
1875    
1876                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1877                                    mb->quant = frame->quant;
1878    
1879                                    mb->cbp =
1880                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
1881    
1882                                    if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1883                                            && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1884                                            mb->mode = MODE_DIRECT_NONE_MV; // skipped
1885                                    }
1886                            }
1887    
1888    #ifdef BFRAMES_DEC_DEBUG
1889            BFRAME_DEBUG
1890    #endif
1891                            start_timer();
1892                            MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1893                                                     &pEnc->sStat);
1894                            stop_coding_timer();
1895                    }
1896            }
1897    
1898            emms();
1899    
1900            // TODO: dynamic fcode/bcode ???
1901    
1902            *pBits = BitstreamPos(bs) - *pBits;
1903    
1904    #ifdef BFRAMES_DEC_DEBUG
1905            if (!first){
1906                    first=1;
1907                    if (fp)
1908                            fclose(fp);
1909            }
1910    #endif
1911    }
1912    
1913    
1914    /*      in case internal output is needed somewhere... */
1915    /*      {
1916            FILE *filehandle;
1917            filehandle=fopen("last-b.pgm","wb");
1918            if (filehandle)
1919            {
1920                    fprintf(filehandle,"P5\n\n");           //
1921                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1922                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1923                    fclose(filehandle);
1924                    }
1925            }
1926    */

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.76.2.1

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