[svn] / branches / dev-api-3 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/encoder.c

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

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

Legend:
Removed from v.85  
changed lines
  Added in v.259

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