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

Diff of /xvidcore/src/encoder.c

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

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

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.54

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