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

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

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

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

Legend:
Removed from v.136  
changed lines
  Added in v.340

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