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

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

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

revision 143, Sun Apr 28 16:15:51 2002 UTC revision 398, Wed Sep 4 20:49:56 2002 UTC
# Line 1  Line 1 
1  /**************************************************************************  /**************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC   *      XVID MPEG-4 VIDEO CODEC
4   *      OpenDivx API wrapper   *  - OpenDivx API wrapper -
5     *
6     *  Copyright(C) 2001-2002 Peter Ross <pross@cs.rmit.edu.au>
7   *   *
8   *      This program is an implementation of a part of one or more MPEG-4   *      This program is an implementation of a part of one or more MPEG-4
9   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
# Line 28  Line 30 
30   *   *
31   *************************************************************************/   *************************************************************************/
32    
 /**************************************************************************  
  *  
  *      History:  
  *  
  *      26.02.2001      fixed dec_csp bugs  
  *      26.12.2001      xvid_init() support  
  *      22.12.2001      removed some compiler warnings  
  *      16.12.2001      inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>  
  *  
  *************************************************************************/  
   
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <string.h>  #include <string.h>
35    #include <stdio.h>
36    
37  #include "xvid.h"  #include "xvid.h"
38  #include "divx4.h"  #include "divx4.h"
# Line 70  Line 62 
62          void * handle;          void * handle;
63          XVID_DEC_FRAME xframe;          XVID_DEC_FRAME xframe;
64    
65  } DINST;  }
66    DINST;
67    
68    typedef struct EINST
69    {
70            struct EINST *next;
71    
72            void *handle;
73            int quality;
74    
75    }
76    EINST;
77    
78  /**************************************************************************  /**************************************************************************
79   * Global data (needed to emulate correctly exported symbols from divx4)   * Global data (needed to emulate correctly exported symbols from divx4)
# Line 85  Line 88 
88    
89  /* The Divx4 instance chainlist */  /* The Divx4 instance chainlist */
90  static DINST * dhead = NULL;  static DINST * dhead = NULL;
91    static EINST *ehead = NULL;
92    
93  /* Divx4 quality to XviD encoder motion flag presets */  /* Divx4 quality to XviD encoder motion flag presets */
94  static int const divx4_motion_presets[7] = {  static int const divx4_motion_presets[7] = {
95          0,          0,
96    
         PMV_QUICKSTOP16,  
   
97          PMV_EARLYSTOP16,          PMV_EARLYSTOP16,
98    
99            PMV_EARLYSTOP16 | PMV_ADVANCEDDIAMOND16,
100    
101          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,
102    
103          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 |
104          PMV_EARLYSTOP8  | PMV_HALFPELDIAMOND8,                  PMV_HALFPELREFINE8,
105    
106          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 |
107          PMV_EARLYSTOP8  | PMV_HALFPELDIAMOND8,                  PMV_HALFPELREFINE8,
108    
109          PMV_EARLYSTOP16    | PMV_HALFPELREFINE16 |          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | PMV_EARLYSTOP8 |
110          PMV_EXTSEARCH16    | PMV_EARLYSTOP8 |                  PMV_HALFPELREFINE8
         PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8  
111  };  };
112    
113    
# Line 119  Line 122 
122          XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL          XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL
123  };  };
124    
 /*  
  * Current divx4 encoder quality  
  * ToDo : this data should not be shared between encoder instances  
  */  
 static int quality;  
   
125  /**************************************************************************  /**************************************************************************
126   * Local Prototypes   * Local Prototypes
127   *************************************************************************/   *************************************************************************/
# Line 134  Line 131 
131  static DINST * dinst_add(unsigned long key);  static DINST * dinst_add(unsigned long key);
132  static void    dinst_remove(unsigned long key);  static void    dinst_remove(unsigned long key);
133    
134    static EINST *einst_find(void *handle);
135    static EINST *einst_add(void *handle);
136    static void einst_remove(void *handle);
137    
138  /* Converts divx4 colorspaces codes to xvid codes */  /* Converts divx4 colorspaces codes to xvid codes */
139  static int xvid_to_opendivx_dec_csp(int csp);  static int xvid_to_opendivx_dec_csp(int csp);
140  static int xvid_to_opendivx_enc_csp(int csp);  static int xvid_to_opendivx_enc_csp(int csp);
# Line 146  Line 147 
147   *************************************************************************/   *************************************************************************/
148    
149  int  int
150  decore(unsigned long key, unsigned long opt, void * param1, void * param2)  decore(unsigned long key,
151               unsigned long opt,
152               void *param1,
153               void *param2)
154  {  {
155    
156          int xerr;          int xerr;
# Line 167  Line 171 
171                  DEC_PARAM * dparam = (DEC_PARAM *)param1;                  DEC_PARAM * dparam = (DEC_PARAM *)param1;
172    
173                  /* Find the divx4 instance */                  /* Find the divx4 instance */
174                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
175                          dcur = dinst_add(key);                          dcur = dinst_add(key);
176                  }                  }
177    
# Line 199  Line 202 
202                  DINST * dcur;                  DINST * dcur;
203    
204                  /* Find the divx4 instance into the chain list */                  /* Find the divx4 instance into the chain list */
205                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
206                          return DEC_EXIT;                          return DEC_EXIT;
207                  }                  }
208    
# Line 218  Line 220 
220                  DINST * dcur;                  DINST * dcur;
221    
222                  /* Find the divx4 instance into the chain list */                  /* Find the divx4 instance into the chain list */
223                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
224                          return DEC_EXIT;                          return DEC_EXIT;
225                  }                  }
226    
# Line 235  Line 236 
236                  DINST * dcur;                  DINST * dcur;
237                  DEC_PARAM * dparam = (DEC_PARAM *)param1;                  DEC_PARAM * dparam = (DEC_PARAM *)param1;
238    
239                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
240                          return DEC_EXIT;                          return DEC_EXIT;
241                  }                  }
242    
# Line 253  Line 253 
253                  DINST * dcur;                  DINST * dcur;
254                  DEC_FRAME * dframe = (DEC_FRAME *)param1;                  DEC_FRAME * dframe = (DEC_FRAME *)param1;
255    
256                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
257                          return DEC_EXIT;                          return DEC_EXIT;
258                  }                  }
259    
# Line 265  Line 264 
264                  dcur->xframe.stride = dframe->stride;                  dcur->xframe.stride = dframe->stride;
265    
266                  /* Does the frame need to be skipped ? */                  /* Does the frame need to be skipped ? */
267                  if (!dframe->render_flag)                          if (!dframe->render_flag) {
                 {  
268                          /*                          /*
269                           * Then we use the null colorspace to force XviD to                           * Then we use the null colorspace to force XviD to
270                           * skip the frame. The original colorspace will be                           * skip the frame. The original colorspace will be
# Line 280  Line 278 
278                  xerr = decoder_decode(dcur->handle, &dcur->xframe);                  xerr = decoder_decode(dcur->handle, &dcur->xframe);
279    
280                  /* Restore the real colorspace for this instance */                  /* Restore the real colorspace for this instance */
281                  if (!dframe->render_flag)                          if (!dframe->render_flag) {
                 {  
282                          dcur->xframe.colorspace = csp_tmp;                          dcur->xframe.colorspace = csp_tmp;
283                  }                  }
284    
# Line 301  Line 298 
298    
299    
300          /* XviD error code -> Divx4 */          /* XviD error code -> Divx4 */
301          switch(xerr)          switch (xerr) {
         {  
302          case XVID_ERR_OK :          case XVID_ERR_OK :
303                  return DEC_OK;                  return DEC_OK;
304          case XVID_ERR_MEMORY :          case XVID_ERR_MEMORY :
# Line 324  Line 320 
320  #define FRAMERATE_INCR          1001  #define FRAMERATE_INCR          1001
321    
322  int  int
323  encore(void * handle, int opt, void * param1, void * param2)  encore(void *handle,
324               int opt,
325               void *param1,
326               void *param2)
327  {  {
328    
329          int xerr;          int xerr;
# Line 332  Line 331 
331          switch(opt) {          switch(opt) {
332          case ENC_OPT_INIT :          case ENC_OPT_INIT :
333          {          {
334                            EINST *ecur;
335                  ENC_PARAM * eparam = (ENC_PARAM *)param1;                  ENC_PARAM * eparam = (ENC_PARAM *)param1;
336                  XVID_INIT_PARAM xinit;                  XVID_INIT_PARAM xinit;
337                  XVID_ENC_PARAM xparam;                  XVID_ENC_PARAM xparam;
# Line 343  Line 343 
343                  /* Settings are copied to the XviD encoder structure */                  /* Settings are copied to the XviD encoder structure */
344                  xparam.width = eparam->x_dim;                  xparam.width = eparam->x_dim;
345                  xparam.height = eparam->y_dim;                  xparam.height = eparam->y_dim;
346                  if ((eparam->framerate - (int)eparam->framerate) == 0)                          if ((eparam->framerate - (int) eparam->framerate) == 0) {
                 {  
347                          xparam.fincr = 1;                          xparam.fincr = 1;
348                          xparam.fbase = (int)eparam->framerate;                          xparam.fbase = (int)eparam->framerate;
349                  }                          } else {
                 else  
                 {  
350                          xparam.fincr = FRAMERATE_INCR;                          xparam.fincr = FRAMERATE_INCR;
351                          xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate);                          xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate);
352                  }                  }
# Line 360  Line 357 
357                  xparam.min_quantizer = eparam->min_quantizer;                  xparam.min_quantizer = eparam->min_quantizer;
358                  xparam.max_quantizer = eparam->max_quantizer;                  xparam.max_quantizer = eparam->max_quantizer;
359                  xparam.max_key_interval = eparam->max_key_interval;                  xparam.max_key_interval = eparam->max_key_interval;
                 quality = eparam->quality;  
360    
361                  /* Create the encoder session */                  /* Create the encoder session */
362                  xerr = encoder_create(&xparam);                  xerr = encoder_create(&xparam);
363    
364                  eparam->handle = xparam.handle;                  eparam->handle = xparam.handle;
365    
366                            /* Create an encoder instance in the chainlist */
367                            if ((ecur = einst_find(xparam.handle)) == NULL) {
368                                    ecur = einst_add(xparam.handle);
369    
370                                    if (ecur == NULL) {
371                                            encoder_destroy((Encoder *) xparam.handle);
372                                            return ENC_MEMORY;
373                                    }
374    
375                            }
376    
377                            ecur->quality = eparam->quality;
378                            if (ecur->quality < 0)
379                                    ecur->quality = 0;
380                            if (ecur->quality > 6)
381                                    ecur->quality = 6;
382    
383                  break;                  break;
384          }          }
385    
386          case ENC_OPT_RELEASE :          case ENC_OPT_RELEASE :
387          {          {
388                            EINST *ecur;
389    
390                            if ((ecur = einst_find(handle)) == NULL) {
391                                    return ENC_FAIL;
392                            }
393    
394                            einst_remove(handle);
395                  xerr = encoder_destroy((Encoder *) handle);                  xerr = encoder_destroy((Encoder *) handle);
396    
397                  break;                  break;
398          }          }
399    
400          case ENC_OPT_ENCODE :          case ENC_OPT_ENCODE :
401          case ENC_OPT_ENCODE_VBR :          case ENC_OPT_ENCODE_VBR :
402          {          {
403                            EINST *ecur;
404    
405                  ENC_FRAME * eframe = (ENC_FRAME *)param1;                  ENC_FRAME * eframe = (ENC_FRAME *)param1;
406                  ENC_RESULT * eresult = (ENC_RESULT *)param2;                  ENC_RESULT * eresult = (ENC_RESULT *)param2;
407                  XVID_ENC_FRAME xframe;                  XVID_ENC_FRAME xframe;
408                  XVID_ENC_STATS xstats;                  XVID_ENC_STATS xstats;
409    
410                            if ((ecur = einst_find(handle)) == NULL) {
411                                    return ENC_FAIL;
412                            }
413    
414                  /* Copy the divx4 info into the xvid structure */                  /* Copy the divx4 info into the xvid structure */
415                  xframe.bitstream = eframe->bitstream;                  xframe.bitstream = eframe->bitstream;
416                  xframe.length = eframe->length;                  xframe.length = eframe->length;
417                            xframe.motion = divx4_motion_presets[ecur->quality];
418                  xframe.motion = divx4_motion_presets[quality];                          xframe.general = divx4_general_presets[ecur->quality];
                 xframe.general = divx4_general_presets[quality];  
419    
420                  xframe.image = eframe->image;                  xframe.image = eframe->image;
421                  xframe.colorspace =                          xframe.colorspace = xvid_to_opendivx_enc_csp(eframe->colorspace);
                         xvid_to_opendivx_enc_csp(eframe->colorspace);  
422    
423                  if (opt == ENC_OPT_ENCODE_VBR)                          if (opt == ENC_OPT_ENCODE_VBR) {
                 {  
424                          xframe.intra = eframe->intra;                          xframe.intra = eframe->intra;
425                          xframe.quant = eframe->quant;                          xframe.quant = eframe->quant;
426                  }                          } else {
                 else  
                 {  
427                          xframe.intra = -1;                          xframe.intra = -1;
428                          xframe.quant = 0;                          xframe.quant = 0;
429                  }                  }
430    
431                  /* Encode the frame */                  /* Encode the frame */
432                  xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) );                          xerr =
433                                    encoder_encode((Encoder *) handle, &xframe,
434                                                               (eresult ? &xstats : NULL));
435    
436                  /* Copy back the xvid structure to the divx4 one */                  /* Copy back the xvid structure to the divx4 one */
437                  if (eresult)                          if (eresult) {
                 {  
438                          eresult->is_key_frame = xframe.intra;                          eresult->is_key_frame = xframe.intra;
439                          eresult->quantizer = xstats.quant;                          eresult->quantizer = xstats.quant;
440                          eresult->total_bits = xframe.length * 8;                          eresult->total_bits = xframe.length * 8;
441                          eresult->motion_bits = xstats.hlength * 8;                          eresult->motion_bits = xstats.hlength * 8;
442                          eresult->texture_bits = eresult->total_bits - eresult->motion_bits;                                  eresult->texture_bits =
443                                            eresult->total_bits - eresult->motion_bits;
444                  }                  }
445    
446                  eframe->length = xframe.length;                  eframe->length = xframe.length;
# Line 429  Line 453 
453          }          }
454    
455          /* XviD Error code  -> Divx4 error code */          /* XviD Error code  -> Divx4 error code */
456          switch(xerr)          switch (xerr) {
         {  
457          case XVID_ERR_OK :          case XVID_ERR_OK :
458                  return ENC_OK;                  return ENC_OK;
459          case XVID_ERR_MEMORY :          case XVID_ERR_MEMORY :
# Line 451  Line 474 
474   ***************************************/   ***************************************/
475    
476  /* Find an element in the chainlist according to its key value */  /* Find an element in the chainlist according to its key value */
477  static DINST * dinst_find(unsigned long key)  static DINST *
478    dinst_find(unsigned long key)
479  {  {
480          DINST * dcur = dhead;          DINST * dcur = dhead;
481    
482          while (dcur)          while (dcur) {
483          {                  if (dcur->key == key) {
                 if (dcur->key == key)  
                 {  
484                          return dcur;                          return dcur;
485                  }                  }
486                  dcur = dcur->next;                  dcur = dcur->next;
# Line 469  Line 491 
491    
492    
493  /* Add an element to the chainlist */  /* Add an element to the chainlist */
494  static DINST * dinst_add(unsigned long key)  static DINST *
495    dinst_add(unsigned long key)
496  {  {
497          DINST * dnext = dhead;          DINST * dnext = dhead;
498    
499          dhead = malloc(sizeof(DINST));          dhead = malloc(sizeof(DINST));
500          if (dhead == NULL)          if (dhead == NULL) {
         {  
501                  dhead = dnext;                  dhead = dnext;
502                  return NULL;                  return NULL;
503          }          }
# Line 488  Line 510 
510    
511    
512  /* Remove an elmement from the chainlist */  /* Remove an elmement from the chainlist */
513  static void dinst_remove(unsigned long key)  static void
514    dinst_remove(unsigned long key)
515  {  {
516          DINST * dcur = dhead;          DINST * dcur = dhead;
517    
518          if (dhead == NULL)          if (dhead == NULL) {
         {  
519                  return;                  return;
520          }          }
521    
522          if (dcur->key == key)          if (dcur->key == key) {
         {  
523                  dhead = dcur->next;                  dhead = dcur->next;
524                  free(dcur);                  free(dcur);
525                  return;                  return;
526          }          }
527    
528          while (dcur->next)          while (dcur->next) {
529          {                  if (dcur->next->key == key) {
                 if (dcur->next->key == key)  
                 {  
530                          DINST * tmp = dcur->next;                          DINST * tmp = dcur->next;
531    
532                          dcur->next = tmp->next;                          dcur->next = tmp->next;
533                          free(tmp);                          free(tmp);
534                          return;                          return;
# Line 517  Line 537 
537          }          }
538  }  }
539    
540    
541  /***************************************  /***************************************
542   * Colorspace code converter           *   * EINST chainlist helper functions    *
543   ***************************************/   ***************************************/
544    
545  static int xvid_to_opendivx_dec_csp(int csp)  /* Find an element in the chainlist according to its handle */
546    static EINST *
547    einst_find(void *handle)
548  {  {
549            EINST *ecur = ehead;
550    
551            while (ecur) {
552                    if (ecur->handle == handle) {
553                            return ecur;
554                    }
555                    ecur = ecur->next;
556            }
557    
558          switch(csp)          return NULL;
559    }
560    
561    
562    /* Add an element to the chainlist */
563    static EINST *
564    einst_add(void *handle)
565          {          {
566            EINST *enext = ehead;
567    
568            ehead = malloc(sizeof(EINST));
569            if (ehead == NULL) {
570                    ehead = enext;
571                    return NULL;
572            }
573    
574            ehead->handle = handle;
575            ehead->next = enext;
576    
577            return ehead;
578    }
579    
580    
581    /* Remove an elmement from the chainlist */
582    static void
583    einst_remove(void *handle)
584    {
585            EINST *ecur = ehead;
586    
587            if (ehead == NULL) {
588                    return;
589            }
590    
591            if (ecur->handle == handle) {
592                    ehead = ecur->next;
593                    free(ecur);
594                    return;
595            }
596    
597            while (ecur->next) {
598                    if (ecur->next->handle == handle) {
599                            EINST *tmp = ecur->next;
600    
601                            ecur->next = tmp->next;
602                            free(tmp);
603                            return;
604                    }
605                    ecur = ecur->next;
606            }
607    }
608    
609    /***************************************
610     * Colorspace code converter           *
611     ***************************************/
612    
613    static int
614    xvid_to_opendivx_dec_csp(int csp)
615    {
616    
617            switch (csp) {
618          case DEC_YV12 :          case DEC_YV12 :
619                  return XVID_CSP_YV12;                  return XVID_CSP_YV12;
620          case DEC_420 :          case DEC_420 :
# Line 557  Line 646 
646          }          }
647  }  }
648    
649  static int xvid_to_opendivx_enc_csp(int csp)  static int
650    xvid_to_opendivx_enc_csp(int csp)
651  {  {
652    
653          switch (csp)          switch (csp) {
         {  
654          case ENC_CSP_RGB24 :          case ENC_CSP_RGB24 :
655                  return XVID_CSP_VFLIP | XVID_CSP_RGB24;                  return XVID_CSP_VFLIP | XVID_CSP_RGB24;
656          case ENC_CSP_YV12 :          case ENC_CSP_YV12 :

Legend:
Removed from v.143  
changed lines
  Added in v.398

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