[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 138, Thu Apr 25 19:27:49 2002 UTC revision 192, Sun Jun 9 13:16:26 2002 UTC
# Line 1  Line 1 
1  // 14.04.2002   added FrameCodeB()  /**************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  -  Encoder main module  -
5     *
6     *  This program is an implementation of a part of one or more MPEG-4
7     *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
8     *  to use this software module in hardware or software products are
9     *  advised that its use may infringe existing patents or copyrights, and
10     *  any such use would be at such party's own risk.  The original
11     *  developer of this software module and his/her company, and subsequent
12     *  editors and their companies, will have no liability for use of this
13     *  software or modifications or derivatives thereof.
14     *
15     *  This program is free software; you can redistribute it and/or modify
16     *  it under the terms of the GNU General Public License as published by
17     *  the Free Software Foundation; either version 2 of the License, or
18     *  (at your option) any later version.
19     *
20     *  This program is distributed in the hope that it will be useful,
21     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     *  GNU General Public License for more details.
24     *
25     *  You should have received a copy of the GNU General Public License
26     *  along with this program; if not, write to the Free Software
27     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28     *
29     ***************************************************************************/
30    
31    /****************************************************************************
32     *
33     *  History
34     *
35     *  08.05.2002 fix some problem in DEBUG mode;
36     *             MinChen <chenm001@163.com>
37     *  14.04.2002 added FrameCodeB()
38     *
39     *  $Id: encoder.c,v 1.39 2002-06-09 13:16:26 edgomez Exp $
40     *
41     ***************************************************************************/
42    
43  #include <stdlib.h>  #include <stdlib.h>
44  #include <stdio.h>  #include <stdio.h>
# Line 9  Line 49 
49  #include "global.h"  #include "global.h"
50  #include "utils/timer.h"  #include "utils/timer.h"
51  #include "image/image.h"  #include "image/image.h"
52    #include "motion/motion.h"
53  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
54  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
55  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 23  Line 64 
64  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
65  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
66    
67    static int FrameCodeI(Encoder * pEnc,
68  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);                        Bitstream * bs,
69  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);                        uint32_t *pBits);
70    
71    static int FrameCodeP(Encoder * pEnc,
72                          Bitstream * bs,
73                          uint32_t *pBits,
74                          bool force_inter,
75                          bool vol_header);
76    
77    #ifdef BFRAMES
78    static void FrameCodeB(Encoder * pEnc,
79                           FRAMEINFO * frame,
80                           Bitstream * bs,
81                           uint32_t *pBits);
82    #endif
83    
84  static int DQtab[4] =  static int DQtab[4] =
85  {  {
# Line 58  Line 112 
112          ENC_CHECK(!(pParam->width % 2));          ENC_CHECK(!(pParam->width % 2));
113          ENC_CHECK(!(pParam->height % 2));          ENC_CHECK(!(pParam->height % 2));
114    
115            /* Fps */
116    
117          if (pParam->fincr <= 0 || pParam->fbase <= 0)          if (pParam->fincr <= 0 || pParam->fbase <= 0)
118          {          {
119                  pParam->fincr = 1;                  pParam->fincr = 1;
120                  pParam->fbase = 25;                  pParam->fbase = 25;
121          }          }
122    
123          // simplify the "fincr/fbase" fraction          /*
124          // (neccessary, since windows supplies us with huge numbers)           * Simplify the "fincr/fbase" fraction
125             * (neccessary, since windows supplies us with huge numbers)
126             */
127    
128          i = pParam->fincr;          i = pParam->fincr;
129          while (i > 1)          while (i > 1)
# Line 87  Line 145 
145                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
146          }          }
147    
148            /* Bitrate allocator defaults */
149    
150          if (pParam->rc_bitrate <= 0)          if (pParam->rc_bitrate <= 0)
151                  pParam->rc_bitrate = 900000;                  pParam->rc_bitrate = 900000;
152    
# Line 99  Line 159 
159          if (pParam->rc_buffer <= 0)          if (pParam->rc_buffer <= 0)
160                  pParam->rc_buffer = 100;                  pParam->rc_buffer = 100;
161    
162            /* Max and min quantizers */
163    
164          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
165                  pParam->min_quantizer = 1;                  pParam->min_quantizer = 1;
166    
167          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
168                  pParam->max_quantizer = 31;                  pParam->max_quantizer = 31;
169    
         if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */  
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
   
170          if (pParam->max_quantizer < pParam->min_quantizer)          if (pParam->max_quantizer < pParam->min_quantizer)
171                  pParam->max_quantizer = pParam->min_quantizer;                  pParam->max_quantizer = pParam->min_quantizer;
172    
173          if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL)          /* 1 keyframe each 10 seconds */
174    
175            if (pParam->max_key_interval == 0)
176                    pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
177    
178    
179            pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
180            if (pEnc == NULL)
181                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
182    
183          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
# Line 125  Line 191 
191          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
192          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
193    
194            pEnc->mbParam.fbase = pParam->fbase;
195            pEnc->mbParam.fincr = pParam->fincr;
196    
197          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
198    
199          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 136  Line 205 
205    
206          /* try to allocate frame memory */          /* try to allocate frame memory */
207    
208          pEnc->current = NULL;          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
209          pEnc->reference = NULL;          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
         if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL ||  
                  (pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL)  
         {  
                 if (pEnc->current) xvid_free(pEnc->current);  
                 xvid_free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
210    
211          /* try to allocate mb memory */          if ( pEnc->current == NULL || pEnc->reference == NULL)
212                    goto xvid_err_memory1;
213    
214          pEnc->current->mbs = NULL;          /* try to allocate mb memory */
         pEnc->reference->mbs = NULL;  
215    
216  #ifdef _DEBUG          pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \
217  #ifdef WIN32                                           pEnc->mbParam.mb_width * \
218  OutputDebugString("malloc mbs");                                           pEnc->mbParam.mb_height,
219  #endif                                           CACHE_LINE);
220  #endif          pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \
221                                               pEnc->mbParam.mb_width * \
222                                               pEnc->mbParam.mb_height,
223                                               CACHE_LINE);
224    
225          if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL ||          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
226                  (pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL)                  goto xvid_err_memory2;
         {  
                 if (pEnc->current->mbs) xvid_free(pEnc->current->mbs);  
                 xvid_free(pEnc->current);  
                 xvid_free(pEnc->reference);  
                 xvid_free(pEnc);  
         }  
227    
228          /* try to allocate image memory */          /* try to allocate image memory */
229    
230  #ifdef _DEBUG  #ifdef _DEBUG
231          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
232  #endif  #endif
233    #ifdef BFRAMES
234            image_null(&pEnc->f_refh);
235            image_null(&pEnc->f_refv);
236            image_null(&pEnc->f_refhv);
237    #endif
238          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
239          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
240          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 180  Line 244 
244          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
245    
246  #ifdef _DEBUG  #ifdef _DEBUG
247  #ifdef WIN32          if (image_create(&pEnc->sOriginal,
248  OutputDebugString("malloc images");                           pEnc->mbParam.edged_width,
249  #endif                           pEnc->mbParam.edged_height) < 0)
250                    goto xvid_err_memory3;
251  #endif  #endif
252          if (  #ifdef BFRAMES
253  #ifdef _DEBUG          if (image_create(&pEnc->f_refh,
254                  image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                           pEnc->mbParam.edged_width,
255                             pEnc->mbParam.edged_height) < 0)
256                    goto xvid_err_memory3;
257            if (image_create(&pEnc->f_refv,
258                             pEnc->mbParam.edged_width,
259                             pEnc->mbParam.edged_height) < 0)
260                    goto xvid_err_memory3;
261            if (image_create(&pEnc->f_refhv,
262                             pEnc->mbParam.edged_width,
263                             pEnc->mbParam.edged_height) < 0)
264                    goto xvid_err_memory3;
265  #endif  #endif
266                  image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          if (image_create(&pEnc->current->image,
267                  image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                           pEnc->mbParam.edged_width,
268                  image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                           pEnc->mbParam.edged_height) < 0)
269                  image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  goto xvid_err_memory3;
270                  image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          if (image_create(&pEnc->reference->image,
271                  image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                           pEnc->mbParam.edged_width,
272                  image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
273                    goto xvid_err_memory3;
274            if (image_create(&pEnc->vInterH,
275                             pEnc->mbParam.edged_width,
276                             pEnc->mbParam.edged_height) < 0)
277                    goto xvid_err_memory3;
278            if (image_create(&pEnc->vInterV,
279                             pEnc->mbParam.edged_width,
280                             pEnc->mbParam.edged_height) < 0)
281                    goto xvid_err_memory3;
282            if (image_create(&pEnc->vInterVf,
283                             pEnc->mbParam.edged_width,
284                             pEnc->mbParam.edged_height) < 0)
285                    goto xvid_err_memory3;
286            if (image_create(&pEnc->vInterHV,
287                             pEnc->mbParam.edged_width,
288                             pEnc->mbParam.edged_height) < 0)
289                    goto xvid_err_memory3;
290            if (image_create(&pEnc->vInterHVf,
291                             pEnc->mbParam.edged_width,
292                             pEnc->mbParam.edged_height) < 0)
293                    goto xvid_err_memory3;
294    
295    
296    
297            /* B Frames specific init */
298    #ifdef BFRAMES
299    
300            pEnc->mbParam.max_bframes = pParam->max_bframes;
301            pEnc->bquant_ratio = pParam->bquant_ratio;
302            pEnc->bframes = NULL;
303    
304            if (pEnc->mbParam.max_bframes > 0)
305          {          {
306  #ifdef _DEBUG                  int n;
307                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
308  #endif                  pEnc->bframes = xvid_malloc(pEnc->mbParam.max_bframes * \
309                  image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                              sizeof(FRAMEINFO *),
310                  image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                              CACHE_LINE);
311                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
312                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  if (pEnc->bframes == NULL)
313                  image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                          goto xvid_err_memory3;
314                  image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
315                  image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  for (n=0; n<pEnc->mbParam.max_bframes; n++)
316                            pEnc->bframes[n] = NULL;
317    
318    
319                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
320                    {
321                            pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO),
322                                                           CACHE_LINE);
323    
324                            if (pEnc->bframes[n] == NULL)
325                                    goto xvid_err_memory4;
326    
327                            pEnc->bframes[n]->mbs = xvid_malloc(sizeof(MACROBLOCK) * \
328                                                                pEnc->mbParam.mb_width * \
329                                                                pEnc->mbParam.mb_height,
330                                                                CACHE_LINE);
331    
332                            if (pEnc->bframes[n]->mbs == NULL)
333                                    goto xvid_err_memory4;
334    
335                            image_null(&pEnc->bframes[n]->image);
336    
337                            if (image_create(&pEnc->bframes[n]->image,
338                                             pEnc->mbParam.edged_width,
339                                             pEnc->mbParam.edged_height) < 0)
340                                    goto xvid_err_memory4;
341    
                 xvid_free(pEnc->current);  
                 xvid_free(pEnc->reference);  
                 xvid_free(pEnc);  
                 return XVID_ERR_MEMORY;  
342          }          }
343            }
344    
345            pEnc->bframenum_head = 0;
346            pEnc->bframenum_tail = 0;
347            pEnc->flush_bframes = 0;
348    
349            pEnc->mbParam.m_seconds = 0;
350            pEnc->mbParam.m_ticks = 0;
351    #endif
352    
353          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
354    
355          if (pParam->rc_bitrate)          if (pParam->rc_bitrate)
356          {          {
357                  RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor,                  RateControlInit(&pEnc->rate_control,
358                          pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr,                                  pParam->rc_bitrate,
359                          pParam->max_quantizer, pParam->min_quantizer);                                  pParam->rc_reaction_delay_factor,
360                                    pParam->rc_averaging_period,
361                                    pParam->rc_buffer,
362                                    pParam->fbase * 1000 / pParam->fincr,
363                                    pParam->max_quantizer,
364                                    pParam->min_quantizer);
365          }          }
366    
367          init_timer();          init_timer();
368    
369          return XVID_ERR_OK;          return XVID_ERR_OK;
370    
371            /*
372             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
373             *
374             * ToDo?: We could avoid that piece of code if encoder_destroy could
375             *        handle different destroy levels
376             */
377    #ifdef BFRAMES
378     xvid_err_memory4:
379            for (i=0; i<pEnc->mbParam.max_bframes; i++)
380            {
381    
382                    if (pEnc->bframes[i] == NULL) continue;
383    
384                    image_destroy(&pEnc->bframes[i]->image,
385                                  pEnc->mbParam.edged_width,
386                                  pEnc->mbParam.edged_height);
387    
388                    xvid_free(pEnc->bframes[i]->mbs);
389    
390                    xvid_free(pEnc->bframes[i]);
391    
392  }  }
393    
394            xvid_free(pEnc->bframes);
395    
396    #endif
397    
398     xvid_err_memory3:
399    #ifdef _DEBUG
400            image_destroy(&pEnc->sOriginal,
401                          pEnc->mbParam.edged_width,
402                          pEnc->mbParam.edged_height);
403    #endif
404    
405    #ifdef BFRAMES
406            image_destroy(&pEnc->f_refh,
407                          pEnc->mbParam.edged_width,
408                          pEnc->mbParam.edged_height);
409            image_destroy(&pEnc->f_refv,
410                          pEnc->mbParam.edged_width,
411                          pEnc->mbParam.edged_height);
412            image_destroy(&pEnc->f_refhv,
413                          pEnc->mbParam.edged_width,
414                          pEnc->mbParam.edged_height);
415    #endif
416    
417            image_destroy(&pEnc->current->image,
418                          pEnc->mbParam.edged_width,
419                          pEnc->mbParam.edged_height);
420            image_destroy(&pEnc->reference->image,
421                          pEnc->mbParam.edged_width,
422                          pEnc->mbParam.edged_height);
423            image_destroy(&pEnc->vInterH,
424                          pEnc->mbParam.edged_width,
425                          pEnc->mbParam.edged_height);
426            image_destroy(&pEnc->vInterV,
427                          pEnc->mbParam.edged_width,
428                          pEnc->mbParam.edged_height);
429            image_destroy(&pEnc->vInterVf,
430                          pEnc->mbParam.edged_width,
431                          pEnc->mbParam.edged_height);
432            image_destroy(&pEnc->vInterHV,
433                          pEnc->mbParam.edged_width,
434                          pEnc->mbParam.edged_height);
435            image_destroy(&pEnc->vInterHVf,
436                          pEnc->mbParam.edged_width,
437                          pEnc->mbParam.edged_height);
438    
439     xvid_err_memory2:
440            xvid_free(pEnc->current->mbs);
441            xvid_free(pEnc->reference->mbs);
442    
443     xvid_err_memory1:
444            xvid_free(pEnc->current);
445            xvid_free(pEnc->reference);
446            xvid_free(pEnc);
447    
448            return XVID_ERR_MEMORY;
449    }
450    
451  int encoder_destroy(Encoder * pEnc)  int encoder_destroy(Encoder * pEnc)
452  {  {
453          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
454    
455          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          /* B Frames specific */
456          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  #ifdef BFRAMES
457          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          int i;
458          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
459          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          for (i=0; i<pEnc->mbParam.max_bframes; i++)
460          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          {
461          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
462                    if (pEnc->bframes[i] == NULL) continue;
463    
464                    image_destroy(&pEnc->bframes[i]->image,
465                                  pEnc->mbParam.edged_width,
466                                  pEnc->mbParam.edged_height);
467    
468                    xvid_free(pEnc->bframes[i]->mbs);
469    
470                    xvid_free(pEnc->bframes[i]);
471    
472            }
473    
474            xvid_free(pEnc->bframes);
475    
476    #endif
477    
478            /* All images, reference, current etc ... */
479    
480            image_destroy(&pEnc->current->image,
481                          pEnc->mbParam.edged_width,
482                          pEnc->mbParam.edged_height);
483            image_destroy(&pEnc->reference->image,
484                          pEnc->mbParam.edged_width,
485                          pEnc->mbParam.edged_height);
486            image_destroy(&pEnc->vInterH,
487                          pEnc->mbParam.edged_width,
488                          pEnc->mbParam.edged_height);
489            image_destroy(&pEnc->vInterV,
490                          pEnc->mbParam.edged_width,
491                          pEnc->mbParam.edged_height);
492            image_destroy(&pEnc->vInterVf,
493                          pEnc->mbParam.edged_width,
494                          pEnc->mbParam.edged_height);
495            image_destroy(&pEnc->vInterHV,
496                          pEnc->mbParam.edged_width,
497                          pEnc->mbParam.edged_height);
498            image_destroy(&pEnc->vInterHVf,
499                          pEnc->mbParam.edged_width,
500                          pEnc->mbParam.edged_height);
501    #ifdef BFRAMES
502            image_destroy(&pEnc->f_refh,
503                          pEnc->mbParam.edged_width,
504                          pEnc->mbParam.edged_height);
505            image_destroy(&pEnc->f_refv,
506                          pEnc->mbParam.edged_width,
507                          pEnc->mbParam.edged_height);
508            image_destroy(&pEnc->f_refhv,
509                          pEnc->mbParam.edged_width,
510                          pEnc->mbParam.edged_height);
511    #endif
512  #ifdef _DEBUG  #ifdef _DEBUG
513                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->sOriginal,
514                          pEnc->mbParam.edged_width,
515                          pEnc->mbParam.edged_height);
516  #endif  #endif
517    
518            /* Encoder structure */
519    
520          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
521          xvid_free(pEnc->current);          xvid_free(pEnc->current);
522    
# Line 249  Line 524 
524          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
525    
526          xvid_free(pEnc);          xvid_free(pEnc);
527    
528          return XVID_ERR_OK;          return XVID_ERR_OK;
529  }  }
530    
531    
532    
533    // ==================================================================
534    #ifdef BFRAMES
535    int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)
536    {
537            uint16_t x, y;
538            Bitstream bs;
539            uint32_t bits;
540    
541    #ifdef _DEBUG
542            float psnr;
543            char temp[128];
544    #endif
545    
546            ENC_CHECK(pEnc);
547            ENC_CHECK(pFrame);
548    
549            start_global_timer();
550    
551            BitstreamInit(&bs, pFrame->bitstream, 0);
552    
553    // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
554    // bframe "flush" code
555    // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556    
557            if ((pFrame->image == NULL || pEnc->flush_bframes) &&
558                    pEnc->bframenum_head < pEnc->bframenum_tail)
559            {
560    
561                    if (pEnc->flush_bframes == 0)
562                    {
563                            // we have reached end of stream without getting a future reference
564                            // .. so encode last final frame a pframe
565                            //dprintf("--- PFRAME (final frame correction) --- ");
566                            pEnc->bframenum_tail--;
567                            SWAP(pEnc->current, pEnc->reference);
568    
569                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
570    
571                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
572    
573                            BitstreamPad(&bs);
574                            pFrame->length = BitstreamLength(&bs);
575                            pFrame->input_consumed = 0;
576                            pFrame->intra = 0;
577                            return XVID_ERR_OK;
578                    }
579    
580                    //dprintf("--- BFRAME (flush) --- ");
581                    FrameCodeB(pEnc,
582                            pEnc->bframes[pEnc->bframenum_head],
583                             &bs, &bits);
584                    pEnc->bframenum_head++;
585    
586    
587                    BitstreamPad(&bs);
588                    pFrame->length = BitstreamLength(&bs);
589                    pFrame->input_consumed = 0;
590                    pFrame->intra = 0;
591                    return XVID_ERR_OK;
592            }
593    
594            if (pFrame->image == NULL)
595            {
596                    pFrame->length = 0;
597                    pFrame->input_consumed = 1;
598                    pFrame->intra = 0;
599                    return XVID_ERR_OK;
600            }
601    
602            if (pEnc->bframenum_head > 0)
603            {
604                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
605            }
606    
607            pEnc->flush_bframes = 0;
608    
609    // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
610    
611            SWAP(pEnc->current, pEnc->reference);
612    
613            pEnc->current->quant = (pFrame->quant == 0) ? RateControlGetQ(&pEnc->rate_control, 0) : pFrame->quant;
614    
615            if(pEnc->current->quant < 1)
616                    pEnc->current->quant = 1;
617    
618            if(pEnc->current->quant > 31)
619                    pEnc->current->quant = 31;
620    
621            pEnc->current->global_flags = pFrame->general;
622            pEnc->current->motion_flags = pFrame->motion;
623            pEnc->current->seconds = pEnc->mbParam.m_seconds;
624            pEnc->current->ticks = pEnc->mbParam.m_ticks;
625            //@@@ TODO: dyanmic fcode (in both directions)
626            pEnc->current->fcode = pEnc->mbParam.m_fcode;
627            pEnc->current->bcode = pEnc->mbParam.m_fcode;
628    
629            start_timer();
630            if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,
631                            pFrame->image, pFrame->colorspace))
632            {
633                    return XVID_ERR_FORMAT;
634            }
635            stop_conv_timer();
636    
637    #ifdef _DEBUG
638            image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
639    #endif
640    
641    
642    // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643    // lumi masking
644    // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645    
646            if ((pEnc->current->global_flags & XVID_LUMIMASKING))
647            {
648                    int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
649    
650                    pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,
651                                                                pEnc->mbParam.edged_width,  // stride
652                                                                temp_dquants,
653                                                                pEnc->current->quant,
654                                                                pEnc->current->quant,       // min_quant
655                                                                2*pEnc->current->quant,     // max_quant
656                                                                pEnc->mbParam.mb_width,
657                                                                pEnc->mbParam.mb_height);
658    
659                    for (y = 0; y < pEnc->mbParam.mb_height; y++)
660                            for (x = 0; x < pEnc->mbParam.mb_width; x++)
661                            {
662                                    MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
663                                    pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];
664                            }
665                    xvid_free(temp_dquants);
666            }
667    
668    
669    // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
670    // ivop/pvop/bvop selection
671    // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
672    
673            if (pEnc->iFrameNum == 0 ||
674                    pFrame->intra == 1 ||
675                    (pFrame->intra < 0 && (pEnc->iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->iMaxKeyInterval)) ||
676                    image_mad(&pEnc->reference->image, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.width, pEnc->mbParam.height) > 30)
677            {
678                    //dprintf("--- IFRAME ---");
679    
680                    FrameCodeI(pEnc, &bs, &bits);
681    
682                    pFrame->intra = 1;
683                    pEnc->flush_bframes = 1;
684    
685                    /* note: sequences like "IIBB" decode fine with msfdam but,
686                       go screwy with divx5.00 */
687            }
688            else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)
689            {
690                    //dprintf("--- PFRAME ---");
691    
692                    FrameCodeP(pEnc, &bs, &bits, 1, 0);
693                    pFrame->intra = 0;
694                    pEnc->flush_bframes = 1;
695            }
696            else
697            {
698                    //dprintf("--- BFRAME (store) ---  head=%i tail=%i", pEnc->bframenum_head, pEnc->bframenum_tail);
699    
700                    if (pFrame->bquant < 1)
701                    {
702                            pEnc->current->quant = ((pEnc->reference->quant + pEnc->current->quant) * pEnc->bquant_ratio) / 200;
703                    }
704                    else
705                    {
706                            pEnc->current->quant = pFrame->bquant;
707                    }
708    
709                    // store frame into bframe buffer & swap ref back to current
710                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
711                    SWAP(pEnc->current, pEnc->reference);
712    
713                    pEnc->bframenum_tail++;
714    
715                    pFrame->intra = 0;
716                    pFrame->length = 0;
717                    pFrame->input_consumed = 1;
718    
719                    pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
720                    if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)
721                    {
722                            pEnc->mbParam.m_seconds++;
723                            pEnc->mbParam.m_ticks = 0;
724                    }
725                    return XVID_ERR_OK;
726            }
727    
728            BitstreamPad(&bs);
729            pFrame->length = BitstreamLength(&bs);
730    
731            if (pResult)
732            {
733                    pResult->quant = pEnc->current->quant;
734                    pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
735                    pResult->kblks = pEnc->sStat.kblks;
736                    pResult->mblks = pEnc->sStat.mblks;
737                    pResult->ublks = pEnc->sStat.ublks;
738            }
739    
740    #ifdef _DEBUG
741            psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,
742                                    pEnc->mbParam.width, pEnc->mbParam.height);
743    
744            sprintf(temp, "PSNR: %f\n", psnr);
745            DEBUG(temp);
746    #endif
747    
748            if (pFrame->quant == 0)
749            {
750                    RateControlUpdate(&pEnc->rate_control,
751                                      pEnc->current->quant,
752                                      pFrame->length,
753                                      pFrame->intra);
754            }
755    
756            pEnc->iFrameNum++;
757            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
758            if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)
759            {
760                    pEnc->mbParam.m_seconds++;
761                    pEnc->mbParam.m_ticks = 0;
762            }
763            pFrame->input_consumed = 1;
764    
765            stop_global_timer();
766            write_timer();
767    
768            return XVID_ERR_OK;
769    }
770    // ==================================================================
771    #else
772  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)
773  {  {
774          uint16_t x, y;          uint16_t x, y;
# Line 285  Line 802 
802          stop_conv_timer();          stop_conv_timer();
803    
804  #ifdef _DEBUG  #ifdef _DEBUG
805          image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height);          image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
806  #endif  #endif
807    
808          EMMS();          EMMS();
# Line 294  Line 811 
811    
812          if (pFrame->quant == 0)          if (pFrame->quant == 0)
813          {          {
814                  pEnc->current->quant = RateControlGetQ(0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
815          }          }
816          else          else
817          {          {
# Line 387  Line 904 
904    
905          if (pFrame->quant == 0)          if (pFrame->quant == 0)
906          {          {
907                  RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra);                  RateControlUpdate(&pEnc->rate_control,
908                                      pEnc->current->quant,
909                                      pFrame->length,
910                                      pFrame->intra);
911          }          }
912    
913  #ifdef _DEBUG  #ifdef _DEBUG
# Line 405  Line 925 
925    
926          return XVID_ERR_OK;          return XVID_ERR_OK;
927  }  }
928    #endif
929    
930    
931  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {
# Line 472  Line 993 
993                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
994                          VECTOR pred[4];                          VECTOR pred[4];
995                          VECTOR tmp;                          VECTOR tmp;
996                          int dummy[4];                          int32_t dummy[4];
997                          int vec;                          int vec;
998    
999                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);
# Line 716  Line 1237 
1237          int iLimit;          int iLimit;
1238          uint32_t x, y;          uint32_t x, y;
1239          int iSearchRange;          int iSearchRange;
1240          bool bIntra;          int bIntra;
1241    
1242          IMAGE *pCurrent = &pEnc->current->image;          /* IMAGE *pCurrent = &pEnc->current->image; */
1243          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1244    
1245          start_timer();          start_timer();
# Line 893  Line 1414 
1414  }  }
1415    
1416    
1417    #ifdef BFRAMES
 /*  
1418  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)
1419  {  {
1420      int16_t dct_codes[6][64];      int16_t dct_codes[6*64];
1421      int16_t qcoeff[6][64];      int16_t qcoeff[6*64];
1422      uint32_t x, y;      uint32_t x, y;
1423          VECTOR forward;          VECTOR forward;
1424          VECTOR backward;          VECTOR backward;
# Line 907  Line 1427 
1427          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1428    
1429          // forward          // forward
1430          image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);
1431          start_timer();          start_timer();
1432          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1433                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);
1434          stop_inter_timer();          stop_inter_timer();
1435    
1436          // backward          // backward
1437          image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);
1438      start_timer();      start_timer();
1439          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1440                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);
# Line 925  Line 1445 
1445                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1446                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1447    
1448    
1449          stop_motion_timer();          stop_motion_timer();
1450    
1451          if (test_quant_type(&pEnc->mbParam, pEnc->current))          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))
1452          {          {
1453                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
1454          }          }*/
1455    
1456      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
1457      BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0,      BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);
                         frame->quant, frame->fcode, frame->bcode);  
1458    
1459      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1460    
# Line 973  Line 1493 
1493                                          dct_codes);                                          dct_codes);
1494    
1495                          mb->quant = frame->quant;                          mb->quant = frame->quant;
1496                          mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff);                          mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff);
1497                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);
1498    
1499    
# Line 1004  Line 1524 
1524  //                      printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);  //                      printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);
1525    
1526                          start_timer();                          start_timer();
1527                          MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);
1528                          stop_coding_timer();                          stop_coding_timer();
1529                  }                  }
1530          }          }
# Line 1014  Line 1534 
1534          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1535    
1536          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
   
1537  }  }
1538    #endif
 */  

Legend:
Removed from v.138  
changed lines
  Added in v.192

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