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

Diff of /xvidcore/src/divx4.c

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

revision 1.12, Wed May 22 12:40:25 2002 UTC revision 1.21.2.2, Mon Jun 9 13:50:12 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC   *      XVID MPEG-4 VIDEO CODEC
4   *      OpenDivx API wrapper   *  - DivX 4.x compatibility layer (Deprecated and not up2date code) -
5   *   *
6   *      This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2001-2003 Peter Ross <pross@xvid.org>
7   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002-2003 Edouard Gomez <ed.gomez@free.fr>
  *      to use this software module in hardware or software products are  
  *      advised that its use may infringe existing patents or copyrights, and  
  *      any such use would be at such party's own risk.  The original  
  *      developer of this software module and his/her company, and subsequent  
  *      editors and their companies, will have no liability for use of this  
  *      software or modifications or derivatives thereof.  
8   *   *
9   *      This program is free software; you can redistribute it and/or modify   *      This program is free software; you can redistribute it and/or modify
10   *      it under the terms of the GNU General Public License as published by   *      it under the terms of the GNU General Public License as published by
# Line 24  Line 18 
18   *   *
19   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
20   *      along with this program; if not, write to the Free Software   *      along with this program; if not, write to the Free Software
21   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  *  
  *************************************************************************/  
   
 /**************************************************************************  
  *  
  *      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>  
22   *   *
23   * $Id$   * $Id$
24   *   *
25   *************************************************************************/   ****************************************************************************/
26    
27  #include <stdlib.h>  #include <stdlib.h>
28  #include <string.h>  #include <string.h>
# Line 52  Line 35 
35    
36  #define EMULATED_DIVX_VERSION 20011001  #define EMULATED_DIVX_VERSION 20011001
37    
38  /**************************************************************************  /*****************************************************************************
39   * Divx Instance Structure   * Divx Instance Structure
40   *   *
41   * This chain list datatype allows XviD do instanciate multiples divx4   * This chain list datatype allows XviD do instanciate multiples divx4
# Line 62  Line 45 
45   *        because they are not protected by any kind of mutex to allow   *        because they are not protected by any kind of mutex to allow
46   *        only one modifier. We should add a mutex for each element in   *        only one modifier. We should add a mutex for each element in
47   *        the chainlist.   *        the chainlist.
48   *************************************************************************/   ****************************************************************************/
49    
50    
51  typedef struct DINST  typedef struct DINST
# Line 73  Line 56 
56          void * handle;          void * handle;
57          XVID_DEC_FRAME xframe;          XVID_DEC_FRAME xframe;
58    
59  } DINST;  }
60    DINST;
61    
62  typedef struct EINST  typedef struct EINST
63  {  {
# Line 82  Line 66 
66          void * handle;          void * handle;
67          int quality;          int quality;
68    
69  } EINST;  }
70    EINST;
71    
72  /**************************************************************************  /*****************************************************************************
73   * Global data (needed to emulate correctly exported symbols from divx4)   * Global data (needed to emulate correctly exported symbols from divx4)
74   *************************************************************************/   ****************************************************************************/
75    
76  /* This is not used in this module but is required by some divx4 encoders*/  /* This is not used in this module but is required by some divx4 encoders*/
77  int quiet_encore = 1;  int quiet_encore = 1;
78    
79  /**************************************************************************  /*****************************************************************************
80   * Local data   * Local data
81   *************************************************************************/   ****************************************************************************/
82    
83  /* The Divx4 instance chainlist */  /* The Divx4 instance chainlist */
84  static DINST * dhead = NULL;  static DINST * dhead = NULL;
# Line 103  Line 88 
88  static int const divx4_motion_presets[7] = {  static int const divx4_motion_presets[7] = {
89          0,          0,
90    
91          PMV_EARLYSTOP16,          0,
92    
93          PMV_EARLYSTOP16 | PMV_ADVANCEDDIAMOND16,          XVID_ME_ADVANCEDDIAMOND16,
94    
95          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,          XVID_ME_HALFPELREFINE16,
96    
97          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |          XVID_ME_HALFPELREFINE16 | XVID_ME_HALFPELREFINE8,
         PMV_EARLYSTOP8  | PMV_HALFPELREFINE8,  
98    
99          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |          XVID_ME_HALFPELREFINE16 | XVID_ME_HALFPELREFINE8,
         PMV_EARLYSTOP8  | PMV_HALFPELREFINE8,  
100    
101          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 |          XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | XVID_ME_HALFPELREFINE8
         PMV_EARLYSTOP8  | PMV_HALFPELREFINE8  
102  };  };
103    
104    
# Line 125  Line 107 
107          0,          0,
108          XVID_H263QUANT,          XVID_H263QUANT,
109          XVID_H263QUANT,          XVID_H263QUANT,
110          XVID_H263QUANT | XVID_HALFPEL,          XVID_H263QUANT | XVID_VOP_HALFPEL,
111          XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL,          XVID_H263QUANT | XVID_VOP_INTER4V | XVID_VOP_HALFPEL,
112          XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL,          XVID_H263QUANT | XVID_VOP_INTER4V | XVID_VOP_HALFPEL,
113          XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL          XVID_H263QUANT | XVID_VOP_INTER4V | XVID_VOP_HALFPEL
114  };  };
115    
116  /**************************************************************************  /*****************************************************************************
117   * Local Prototypes   * Local Prototypes
118   *************************************************************************/   ****************************************************************************/
119    
120  /* Chain list helper functions */  /* Chain list helper functions */
121  static DINST * dinst_find(unsigned long key);  static DINST * dinst_find(unsigned long key);
# Line 148  Line 130 
130  static int xvid_to_opendivx_dec_csp(int csp);  static int xvid_to_opendivx_dec_csp(int csp);
131  static int xvid_to_opendivx_enc_csp(int csp);  static int xvid_to_opendivx_enc_csp(int csp);
132    
133  /**************************************************************************  /*****************************************************************************
134   * decore part   * decore part
135   *   *
136   * decore is the divx4 entry point used to decompress the mpeg4 bitstream   * decore is the divx4 entry point used to decompress the mpeg4 bitstream
137   * into a user defined image format.   * into a user defined image format.
138   *************************************************************************/   ****************************************************************************/
139    
140  int  int
141  decore(unsigned long key, unsigned long opt, void * param1, void * param2)  decore(unsigned long key,
142               unsigned long opt,
143               void *param1,
144               void *param2)
145  {  {
146    
147          int xerr;          int xerr;
# Line 177  Line 162 
162                  DEC_PARAM * dparam = (DEC_PARAM *)param1;                  DEC_PARAM * dparam = (DEC_PARAM *)param1;
163    
164                  /* Find the divx4 instance */                  /* Find the divx4 instance */
165                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
166                          dcur = dinst_add(key);                          dcur = dinst_add(key);
167                  }                  }
168    
# Line 209  Line 193 
193                  DINST * dcur;                  DINST * dcur;
194    
195                  /* Find the divx4 instance into the chain list */                  /* Find the divx4 instance into the chain list */
196                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
197                          return DEC_EXIT;                          return DEC_EXIT;
198                  }                  }
199    
# Line 228  Line 211 
211                  DINST * dcur;                  DINST * dcur;
212    
213                  /* Find the divx4 instance into the chain list */                  /* Find the divx4 instance into the chain list */
214                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
215                          return DEC_EXIT;                          return DEC_EXIT;
216                  }                  }
217    
# Line 245  Line 227 
227                  DINST * dcur;                  DINST * dcur;
228                  DEC_PARAM * dparam = (DEC_PARAM *)param1;                  DEC_PARAM * dparam = (DEC_PARAM *)param1;
229    
230                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
231                          return DEC_EXIT;                          return DEC_EXIT;
232                  }                  }
233    
# Line 263  Line 244 
244                  DINST * dcur;                  DINST * dcur;
245                  DEC_FRAME * dframe = (DEC_FRAME *)param1;                  DEC_FRAME * dframe = (DEC_FRAME *)param1;
246    
247                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
248                          return DEC_EXIT;                          return DEC_EXIT;
249                  }                  }
250    
# Line 275  Line 255 
255                  dcur->xframe.stride = dframe->stride;                  dcur->xframe.stride = dframe->stride;
256    
257                  /* Does the frame need to be skipped ? */                  /* Does the frame need to be skipped ? */
258                  if (!dframe->render_flag)                          if (!dframe->render_flag) {
                 {  
259                          /*                          /*
260                           * Then we use the null colorspace to force XviD to                           * Then we use the null colorspace to force XviD to
261                           * skip the frame. The original colorspace will be                           * skip the frame. The original colorspace will be
# Line 287  Line 266 
266                  }                  }
267    
268                  /* Decode the bitstream */                  /* Decode the bitstream */
269                  xerr = decoder_decode(dcur->handle, &dcur->xframe);                          xerr = decoder_decode(dcur->handle, &dcur->xframe, NULL);
270    
271                  /* Restore the real colorspace for this instance */                  /* Restore the real colorspace for this instance */
272                  if (!dframe->render_flag)                          if (!dframe->render_flag) {
                 {  
273                          dcur->xframe.colorspace = csp_tmp;                          dcur->xframe.colorspace = csp_tmp;
274                  }                  }
275    
# Line 311  Line 289 
289    
290    
291          /* XviD error code -> Divx4 */          /* XviD error code -> Divx4 */
292          switch(xerr)          switch (xerr) {
         {  
293          case XVID_ERR_OK :          case XVID_ERR_OK :
294                  return DEC_OK;                  return DEC_OK;
295          case XVID_ERR_MEMORY :          case XVID_ERR_MEMORY :
# Line 324  Line 301 
301          }          }
302  }  }
303    
304  /**************************************************************************  /*****************************************************************************
305   * Encore Part   * Encore Part
306   *   *
307   * encore is the divx4 entry point used to compress a frame to a mpeg4   * encore is the divx4 entry point used to compress a frame to a mpeg4
308   * bitstream.   * bitstream.
309   *************************************************************************/   ****************************************************************************/
310    
311  #define FRAMERATE_INCR          1001  #define FRAMERATE_INCR          1001
312    
313  int  int
314  encore(void * handle, int opt, void * param1, void * param2)  encore(void *handle,
315               int opt,
316               void *param1,
317               void *param2)
318  {  {
319    
320          int xerr;          int xerr;
# Line 354  Line 334 
334                  /* Settings are copied to the XviD encoder structure */                  /* Settings are copied to the XviD encoder structure */
335                  xparam.width = eparam->x_dim;                  xparam.width = eparam->x_dim;
336                  xparam.height = eparam->y_dim;                  xparam.height = eparam->y_dim;
337                  if ((eparam->framerate - (int)eparam->framerate) == 0)                          if ((eparam->framerate - (int) eparam->framerate) == 0) {
                 {  
338                          xparam.fincr = 1;                          xparam.fincr = 1;
339                          xparam.fbase = (int)eparam->framerate;                          xparam.fbase = (int)eparam->framerate;
340                  }                          } else {
                 else  
                 {  
341                          xparam.fincr = FRAMERATE_INCR;                          xparam.fincr = FRAMERATE_INCR;
342                          xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate);                          xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate);
343                  }                  }
# Line 372  Line 349 
349                  xparam.max_quantizer = eparam->max_quantizer;                  xparam.max_quantizer = eparam->max_quantizer;
350                  xparam.max_key_interval = eparam->max_key_interval;                  xparam.max_key_interval = eparam->max_key_interval;
351    
352                            xparam.global = 0;
353                            xparam.max_bframes = -1;        /* use "original" IP-frame encoder */
354                            xparam.bquant_ratio = 200;
355                            xparam.frame_drop_ratio = 0;    /* dont drop frames */
356    
357                  /* Create the encoder session */                  /* Create the encoder session */
358                  xerr = encoder_create(&xparam);                  xerr = encoder_create(&xparam);
359    
360                  eparam->handle = xparam.handle;                  eparam->handle = xparam.handle;
361    
362                  /* Create an encoder instance in the chainlist */                  /* Create an encoder instance in the chainlist */
363                  if ((ecur = einst_find(xparam.handle)) == NULL)                          if ((ecur = einst_find(xparam.handle)) == NULL) {
                 {  
364                          ecur = einst_add(xparam.handle);                          ecur = einst_add(xparam.handle);
365    
366                          if(ecur == NULL) {                          if(ecur == NULL) {
# Line 390  Line 371 
371                  }                  }
372    
373                  ecur->quality = eparam->quality;                  ecur->quality = eparam->quality;
374                  if(ecur->quality < 0) ecur->quality = 0;                          if (ecur->quality < 0)
375                  if(ecur->quality > 6) ecur->quality = 6;                                  ecur->quality = 0;
376                            if (ecur->quality > 6)
377                                    ecur->quality = 6;
378    
379                  break;                  break;
380          }          }
# Line 400  Line 383 
383          {          {
384                  EINST *ecur;                  EINST *ecur;
385    
386                  if ((ecur = einst_find(handle)) == NULL)                          if ((ecur = einst_find(handle)) == NULL) {
                 {  
387                          return ENC_FAIL;                          return ENC_FAIL;
388                  }                  }
389    
390                            einst_remove(handle);
391                  xerr = encoder_destroy((Encoder *) handle);                  xerr = encoder_destroy((Encoder *) handle);
392    
393                  break;                  break;
394          }          }
395    
# Line 419  Line 403 
403                  XVID_ENC_FRAME xframe;                  XVID_ENC_FRAME xframe;
404                  XVID_ENC_STATS xstats;                  XVID_ENC_STATS xstats;
405    
406                  if ((ecur = einst_find(handle)) == NULL)                          if ((ecur = einst_find(handle)) == NULL) {
                 {  
407                          return ENC_FAIL;                          return ENC_FAIL;
408                  }                  }
409    
# Line 431  Line 414 
414                  xframe.general = divx4_general_presets[ecur->quality];                  xframe.general = divx4_general_presets[ecur->quality];
415    
416                  xframe.image = eframe->image;                  xframe.image = eframe->image;
417                  xframe.colorspace =                          xframe.colorspace = xvid_to_opendivx_enc_csp(eframe->colorspace);
                         xvid_to_opendivx_enc_csp(eframe->colorspace);  
418    
419                  if (opt == ENC_OPT_ENCODE_VBR)                          if (opt == ENC_OPT_ENCODE_VBR) {
                 {  
420                          xframe.intra = eframe->intra;                          xframe.intra = eframe->intra;
421                          xframe.quant = eframe->quant;                          xframe.quant = eframe->quant;
422                  }                          } else {
                 else  
                 {  
423                          xframe.intra = -1;                          xframe.intra = -1;
424                          xframe.quant = 0;                          xframe.quant = 0;
425                  }                  }
426    
427                  /* Encode the frame */                  /* Encode the frame */
428                  xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) );                          xerr =
429                                    encoder_encode((Encoder *) handle, &xframe,
430                                                               (eresult ? &xstats : NULL));
431    
432                  /* Copy back the xvid structure to the divx4 one */                  /* Copy back the xvid structure to the divx4 one */
433                  if (eresult)                          if (eresult) {
                 {  
434                          eresult->is_key_frame = xframe.intra;                          eresult->is_key_frame = xframe.intra;
435                          eresult->quantizer = xstats.quant;                          eresult->quantizer = xstats.quant;
436                          eresult->total_bits = xframe.length * 8;                          eresult->total_bits = xframe.length * 8;
437                          eresult->motion_bits = xstats.hlength * 8;                          eresult->motion_bits = xstats.hlength * 8;
438                          eresult->texture_bits = eresult->total_bits - eresult->motion_bits;                                  eresult->texture_bits =
439                                            eresult->total_bits - eresult->motion_bits;
440                  }                  }
441    
442                  eframe->length = xframe.length;                  eframe->length = xframe.length;
# Line 468  Line 449 
449          }          }
450    
451          /* XviD Error code  -> Divx4 error code */          /* XviD Error code  -> Divx4 error code */
452          switch(xerr)          switch (xerr) {
         {  
453          case XVID_ERR_OK :          case XVID_ERR_OK :
454                  return ENC_OK;                  return ENC_OK;
455          case XVID_ERR_MEMORY :          case XVID_ERR_MEMORY :
# Line 481  Line 461 
461          }          }
462  }  }
463    
464  /**************************************************************************  /*****************************************************************************
465   * Local Functions   * Local Functions
466   *************************************************************************/   ****************************************************************************/
467    
468  /***************************************  /***************************************
469   * DINST chainlist helper functions    *   * DINST chainlist helper functions    *
470   ***************************************/   ***************************************/
471    
472  /* Find an element in the chainlist according to its key value */  /* Find an element in the chainlist according to its key value */
473  static DINST * dinst_find(unsigned long key)  static DINST *
474    dinst_find(unsigned long key)
475  {  {
476          DINST * dcur = dhead;          DINST * dcur = dhead;
477    
478          while (dcur)          while (dcur) {
479          {                  if (dcur->key == key) {
                 if (dcur->key == key)  
                 {  
480                          return dcur;                          return dcur;
481                  }                  }
482                  dcur = dcur->next;                  dcur = dcur->next;
# Line 508  Line 487 
487    
488    
489  /* Add an element to the chainlist */  /* Add an element to the chainlist */
490  static DINST * dinst_add(unsigned long key)  static DINST *
491    dinst_add(unsigned long key)
492  {  {
493          DINST * dnext = dhead;          DINST * dnext = dhead;
494    
495          dhead = malloc(sizeof(DINST));          dhead = malloc(sizeof(DINST));
496          if (dhead == NULL)          if (dhead == NULL) {
         {  
497                  dhead = dnext;                  dhead = dnext;
498                  return NULL;                  return NULL;
499          }          }
# Line 527  Line 506 
506    
507    
508  /* Remove an elmement from the chainlist */  /* Remove an elmement from the chainlist */
509  static void dinst_remove(unsigned long key)  static void
510    dinst_remove(unsigned long key)
511  {  {
512          DINST * dcur = dhead;          DINST * dcur = dhead;
513    
514          if (dhead == NULL)          if (dhead == NULL) {
         {  
515                  return;                  return;
516          }          }
517    
518          if (dcur->key == key)          if (dcur->key == key) {
         {  
519                  dhead = dcur->next;                  dhead = dcur->next;
520                  free(dcur);                  free(dcur);
521                  return;                  return;
522          }          }
523    
524          while (dcur->next)          while (dcur->next) {
525          {                  if (dcur->next->key == key) {
                 if (dcur->next->key == key)  
                 {  
526                          DINST * tmp = dcur->next;                          DINST * tmp = dcur->next;
527    
528                          dcur->next = tmp->next;                          dcur->next = tmp->next;
529                          free(tmp);                          free(tmp);
530                          return;                          return;
# Line 562  Line 539 
539   ***************************************/   ***************************************/
540    
541  /* Find an element in the chainlist according to its handle */  /* Find an element in the chainlist according to its handle */
542  static EINST * einst_find(void *handle)  static EINST *
543    einst_find(void *handle)
544  {  {
545          EINST * ecur = ehead;          EINST * ecur = ehead;
546    
547          while (ecur)          while (ecur) {
548          {                  if (ecur->handle == handle) {
                 if (ecur->handle == handle)  
                 {  
549                          return ecur;                          return ecur;
550                  }                  }
551                  ecur = ecur->next;                  ecur = ecur->next;
# Line 580  Line 556 
556    
557    
558  /* Add an element to the chainlist */  /* Add an element to the chainlist */
559  static EINST * einst_add(void *handle)  static EINST *
560    einst_add(void *handle)
561  {  {
562          EINST * enext = ehead;          EINST * enext = ehead;
563    
564          ehead = malloc(sizeof(EINST));          ehead = malloc(sizeof(EINST));
565          if (ehead == NULL)          if (ehead == NULL) {
         {  
566                  ehead = enext;                  ehead = enext;
567                  return NULL;                  return NULL;
568          }          }
# Line 599  Line 575 
575    
576    
577  /* Remove an elmement from the chainlist */  /* Remove an elmement from the chainlist */
578  static void einst_remove(void *handle)  static void
579    einst_remove(void *handle)
580  {  {
581          EINST * ecur = ehead;          EINST * ecur = ehead;
582    
583          if (ehead == NULL)          if (ehead == NULL) {
         {  
584                  return;                  return;
585          }          }
586    
587          if (ecur->handle == handle)          if (ecur->handle == handle) {
         {  
588                  ehead = ecur->next;                  ehead = ecur->next;
589                  free(ecur);                  free(ecur);
590                  return;                  return;
591          }          }
592    
593          while (ecur->next)          while (ecur->next) {
594          {                  if (ecur->next->handle == handle) {
                 if (ecur->next->handle == handle)  
                 {  
595                          EINST * tmp = ecur->next;                          EINST * tmp = ecur->next;
596    
597                          ecur->next = tmp->next;                          ecur->next = tmp->next;
598                          free(tmp);                          free(tmp);
599                          return;                          return;
# Line 627  Line 601 
601                  ecur = ecur->next;                  ecur = ecur->next;
602          }          }
603  }  }
604    
605  /***************************************  /***************************************
606   * Colorspace code converter           *   * Colorspace code converter           *
607   ***************************************/   ***************************************/
608    
609  static int xvid_to_opendivx_dec_csp(int csp)  static int
610    xvid_to_opendivx_dec_csp(int csp)
611  {  {
612    
613          switch(csp)          switch (csp) {
         {  
614          case DEC_YV12 :          case DEC_YV12 :
615                  return XVID_CSP_YV12;                  return XVID_CSP_YV12;
616          case DEC_420 :          case DEC_420 :
# Line 667  Line 642 
642          }          }
643  }  }
644    
645  static int xvid_to_opendivx_enc_csp(int csp)  static int
646    xvid_to_opendivx_enc_csp(int csp)
647  {  {
648    
649          switch (csp)          switch (csp) {
         {  
650          case ENC_CSP_RGB24 :          case ENC_CSP_RGB24 :
651                  return XVID_CSP_VFLIP | XVID_CSP_RGB24;                  return XVID_CSP_VFLIP | XVID_CSP_RGB24;
652          case ENC_CSP_YV12 :          case ENC_CSP_YV12 :

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.21.2.2

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