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

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.76.2.6

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