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

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

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

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

Legend:
Removed from v.13  
changed lines
  Added in v.234

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