[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.10, Sun Apr 28 20:03:14 2002 UTC revision 1.14, Wed Jun 12 20:38:39 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   *      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
7   *      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 43  Line 43 
43    
44  #include <stdlib.h>  #include <stdlib.h>
45  #include <string.h>  #include <string.h>
46    #include <stdio.h>
47    
48  #include "xvid.h"  #include "xvid.h"
49  #include "divx4.h"  #include "divx4.h"
# Line 72  Line 73 
73          void * handle;          void * handle;
74          XVID_DEC_FRAME xframe;          XVID_DEC_FRAME xframe;
75    
76  } DINST;  }
77    DINST;
78    
79    typedef struct EINST
80    {
81            struct EINST *next;
82    
83            void *handle;
84            int quality;
85    
86    }
87    EINST;
88    
89  /**************************************************************************  /**************************************************************************
90   * Global data (needed to emulate correctly exported symbols from divx4)   * Global data (needed to emulate correctly exported symbols from divx4)
# Line 87  Line 99 
99    
100  /* The Divx4 instance chainlist */  /* The Divx4 instance chainlist */
101  static DINST * dhead = NULL;  static DINST * dhead = NULL;
102    static EINST *ehead = NULL;
103    
104  /* Divx4 quality to XviD encoder motion flag presets */  /* Divx4 quality to XviD encoder motion flag presets */
105  static int const divx4_motion_presets[7] = {  static int const divx4_motion_presets[7] = {
106          0,          0,
107    
         PMV_QUICKSTOP16,  
   
108          PMV_EARLYSTOP16,          PMV_EARLYSTOP16,
109    
110            PMV_EARLYSTOP16 | PMV_ADVANCEDDIAMOND16,
111    
112          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16,
113    
114          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 |
115          PMV_EARLYSTOP8  | PMV_HALFPELDIAMOND8,                  PMV_HALFPELREFINE8,
116    
117          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 |          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 |
118          PMV_EARLYSTOP8  | PMV_HALFPELDIAMOND8,                  PMV_HALFPELREFINE8,
119    
120          PMV_EARLYSTOP16    | PMV_HALFPELREFINE16 |          PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | PMV_EARLYSTOP8 |
121          PMV_EXTSEARCH16    | PMV_EARLYSTOP8 |                  PMV_HALFPELREFINE8
         PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8  
122  };  };
123    
124    
# Line 121  Line 133 
133          XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL          XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL
134  };  };
135    
 /*  
  * Current divx4 encoder quality  
  * ToDo : this data should not be shared between encoder instances  
  */  
 static int quality;  
   
136  /**************************************************************************  /**************************************************************************
137   * Local Prototypes   * Local Prototypes
138   *************************************************************************/   *************************************************************************/
# Line 136  Line 142 
142  static DINST * dinst_add(unsigned long key);  static DINST * dinst_add(unsigned long key);
143  static void    dinst_remove(unsigned long key);  static void    dinst_remove(unsigned long key);
144    
145    static EINST *einst_find(void *handle);
146    static EINST *einst_add(void *handle);
147    static void einst_remove(void *handle);
148    
149  /* Converts divx4 colorspaces codes to xvid codes */  /* Converts divx4 colorspaces codes to xvid codes */
150  static int xvid_to_opendivx_dec_csp(int csp);  static int xvid_to_opendivx_dec_csp(int csp);
151  static int xvid_to_opendivx_enc_csp(int csp);  static int xvid_to_opendivx_enc_csp(int csp);
# Line 148  Line 158 
158   *************************************************************************/   *************************************************************************/
159    
160  int  int
161  decore(unsigned long key, unsigned long opt, void * param1, void * param2)  decore(unsigned long key,
162               unsigned long opt,
163               void *param1,
164               void *param2)
165  {  {
166    
167          int xerr;          int xerr;
# Line 169  Line 182 
182                  DEC_PARAM * dparam = (DEC_PARAM *)param1;                  DEC_PARAM * dparam = (DEC_PARAM *)param1;
183    
184                  /* Find the divx4 instance */                  /* Find the divx4 instance */
185                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
186                          dcur = dinst_add(key);                          dcur = dinst_add(key);
187                  }                  }
188    
# Line 201  Line 213 
213                  DINST * dcur;                  DINST * dcur;
214    
215                  /* Find the divx4 instance into the chain list */                  /* Find the divx4 instance into the chain list */
216                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
217                          return DEC_EXIT;                          return DEC_EXIT;
218                  }                  }
219    
# Line 220  Line 231 
231                  DINST * dcur;                  DINST * dcur;
232    
233                  /* Find the divx4 instance into the chain list */                  /* Find the divx4 instance into the chain list */
234                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
235                          return DEC_EXIT;                          return DEC_EXIT;
236                  }                  }
237    
# Line 237  Line 247 
247                  DINST * dcur;                  DINST * dcur;
248                  DEC_PARAM * dparam = (DEC_PARAM *)param1;                  DEC_PARAM * dparam = (DEC_PARAM *)param1;
249    
250                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
251                          return DEC_EXIT;                          return DEC_EXIT;
252                  }                  }
253    
# Line 255  Line 264 
264                  DINST * dcur;                  DINST * dcur;
265                  DEC_FRAME * dframe = (DEC_FRAME *)param1;                  DEC_FRAME * dframe = (DEC_FRAME *)param1;
266    
267                  if ((dcur = dinst_find(key)) == NULL)                          if ((dcur = dinst_find(key)) == NULL) {
                 {  
268                          return DEC_EXIT;                          return DEC_EXIT;
269                  }                  }
270    
# Line 267  Line 275 
275                  dcur->xframe.stride = dframe->stride;                  dcur->xframe.stride = dframe->stride;
276    
277                  /* Does the frame need to be skipped ? */                  /* Does the frame need to be skipped ? */
278                  if (!dframe->render_flag)                          if (!dframe->render_flag) {
                 {  
279                          /*                          /*
280                           * Then we use the null colorspace to force XviD to                           * Then we use the null colorspace to force XviD to
281                           * skip the frame. The original colorspace will be                           * skip the frame. The original colorspace will be
# Line 282  Line 289 
289                  xerr = decoder_decode(dcur->handle, &dcur->xframe);                  xerr = decoder_decode(dcur->handle, &dcur->xframe);
290    
291                  /* Restore the real colorspace for this instance */                  /* Restore the real colorspace for this instance */
292                  if (!dframe->render_flag)                          if (!dframe->render_flag) {
                 {  
293                          dcur->xframe.colorspace = csp_tmp;                          dcur->xframe.colorspace = csp_tmp;
294                  }                  }
295    
# Line 303  Line 309 
309    
310    
311          /* XviD error code -> Divx4 */          /* XviD error code -> Divx4 */
312          switch(xerr)          switch (xerr) {
         {  
313          case XVID_ERR_OK :          case XVID_ERR_OK :
314                  return DEC_OK;                  return DEC_OK;
315          case XVID_ERR_MEMORY :          case XVID_ERR_MEMORY :
# Line 326  Line 331 
331  #define FRAMERATE_INCR          1001  #define FRAMERATE_INCR          1001
332    
333  int  int
334  encore(void * handle, int opt, void * param1, void * param2)  encore(void *handle,
335               int opt,
336               void *param1,
337               void *param2)
338  {  {
339    
340          int xerr;          int xerr;
# Line 334  Line 342 
342          switch(opt) {          switch(opt) {
343          case ENC_OPT_INIT :          case ENC_OPT_INIT :
344          {          {
345                            EINST *ecur;
346                  ENC_PARAM * eparam = (ENC_PARAM *)param1;                  ENC_PARAM * eparam = (ENC_PARAM *)param1;
347                  XVID_INIT_PARAM xinit;                  XVID_INIT_PARAM xinit;
348                  XVID_ENC_PARAM xparam;                  XVID_ENC_PARAM xparam;
# Line 345  Line 354 
354                  /* Settings are copied to the XviD encoder structure */                  /* Settings are copied to the XviD encoder structure */
355                  xparam.width = eparam->x_dim;                  xparam.width = eparam->x_dim;
356                  xparam.height = eparam->y_dim;                  xparam.height = eparam->y_dim;
357                  if ((eparam->framerate - (int)eparam->framerate) == 0)                          if ((eparam->framerate - (int) eparam->framerate) == 0) {
                 {  
358                          xparam.fincr = 1;                          xparam.fincr = 1;
359                          xparam.fbase = (int)eparam->framerate;                          xparam.fbase = (int)eparam->framerate;
360                  }                          } else {
                 else  
                 {  
361                          xparam.fincr = FRAMERATE_INCR;                          xparam.fincr = FRAMERATE_INCR;
362                          xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate);                          xparam.fbase = (int)(FRAMERATE_INCR * eparam->framerate);
363                  }                  }
# Line 362  Line 368 
368                  xparam.min_quantizer = eparam->min_quantizer;                  xparam.min_quantizer = eparam->min_quantizer;
369                  xparam.max_quantizer = eparam->max_quantizer;                  xparam.max_quantizer = eparam->max_quantizer;
370                  xparam.max_key_interval = eparam->max_key_interval;                  xparam.max_key_interval = eparam->max_key_interval;
                 quality = eparam->quality;  
371    
372                  /* Create the encoder session */                  /* Create the encoder session */
373                  xerr = encoder_create(&xparam);                  xerr = encoder_create(&xparam);
374    
375                  eparam->handle = xparam.handle;                  eparam->handle = xparam.handle;
376    
377                            /* Create an encoder instance in the chainlist */
378                            if ((ecur = einst_find(xparam.handle)) == NULL) {
379                                    ecur = einst_add(xparam.handle);
380    
381                                    if (ecur == NULL) {
382                                            encoder_destroy((Encoder *) xparam.handle);
383                                            return ENC_MEMORY;
384                                    }
385    
386                            }
387    
388                            ecur->quality = eparam->quality;
389                            if (ecur->quality < 0)
390                                    ecur->quality = 0;
391                            if (ecur->quality > 6)
392                                    ecur->quality = 6;
393    
394                  break;                  break;
395          }          }
396    
397          case ENC_OPT_RELEASE :          case ENC_OPT_RELEASE :
398          {          {
399                            EINST *ecur;
400    
401                            if ((ecur = einst_find(handle)) == NULL) {
402                                    return ENC_FAIL;
403                            }
404    
405                            einst_remove(handle);
406                  xerr = encoder_destroy((Encoder *) handle);                  xerr = encoder_destroy((Encoder *) handle);
407    
408                  break;                  break;
409          }          }
410    
411          case ENC_OPT_ENCODE :          case ENC_OPT_ENCODE :
412          case ENC_OPT_ENCODE_VBR :          case ENC_OPT_ENCODE_VBR :
413          {          {
414                            EINST *ecur;
415    
416                  ENC_FRAME * eframe = (ENC_FRAME *)param1;                  ENC_FRAME * eframe = (ENC_FRAME *)param1;
417                  ENC_RESULT * eresult = (ENC_RESULT *)param2;                  ENC_RESULT * eresult = (ENC_RESULT *)param2;
418                  XVID_ENC_FRAME xframe;                  XVID_ENC_FRAME xframe;
419                  XVID_ENC_STATS xstats;                  XVID_ENC_STATS xstats;
420    
421                            if ((ecur = einst_find(handle)) == NULL) {
422                                    return ENC_FAIL;
423                            }
424    
425                  /* Copy the divx4 info into the xvid structure */                  /* Copy the divx4 info into the xvid structure */
426                  xframe.bitstream = eframe->bitstream;                  xframe.bitstream = eframe->bitstream;
427                  xframe.length = eframe->length;                  xframe.length = eframe->length;
428                            xframe.motion = divx4_motion_presets[ecur->quality];
429                  xframe.motion = divx4_motion_presets[quality];                          xframe.general = divx4_general_presets[ecur->quality];
                 xframe.general = divx4_general_presets[quality];  
430    
431                  xframe.image = eframe->image;                  xframe.image = eframe->image;
432                  xframe.colorspace =                          xframe.colorspace = xvid_to_opendivx_enc_csp(eframe->colorspace);
                         xvid_to_opendivx_enc_csp(eframe->colorspace);  
433    
434                  if (opt == ENC_OPT_ENCODE_VBR)                          if (opt == ENC_OPT_ENCODE_VBR) {
                 {  
435                          xframe.intra = eframe->intra;                          xframe.intra = eframe->intra;
436                          xframe.quant = eframe->quant;                          xframe.quant = eframe->quant;
437                  }                          } else {
                 else  
                 {  
438                          xframe.intra = -1;                          xframe.intra = -1;
439                          xframe.quant = 0;                          xframe.quant = 0;
440                  }                  }
441    
442                  /* Encode the frame */                  /* Encode the frame */
443                  xerr = encoder_encode((Encoder *) handle, &xframe, (eresult ? &xstats : NULL) );                          xerr =
444                                    encoder_encode((Encoder *) handle, &xframe,
445                                                               (eresult ? &xstats : NULL));
446    
447                  /* Copy back the xvid structure to the divx4 one */                  /* Copy back the xvid structure to the divx4 one */
448                  if (eresult)                          if (eresult) {
                 {  
449                          eresult->is_key_frame = xframe.intra;                          eresult->is_key_frame = xframe.intra;
450                          eresult->quantizer = xstats.quant;                          eresult->quantizer = xstats.quant;
451                          eresult->total_bits = xframe.length * 8;                          eresult->total_bits = xframe.length * 8;
452                          eresult->motion_bits = xstats.hlength * 8;                          eresult->motion_bits = xstats.hlength * 8;
453                          eresult->texture_bits = eresult->total_bits - eresult->motion_bits;                                  eresult->texture_bits =
454                                            eresult->total_bits - eresult->motion_bits;
455                  }                  }
456    
457                  eframe->length = xframe.length;                  eframe->length = xframe.length;
# Line 431  Line 464 
464          }          }
465    
466          /* XviD Error code  -> Divx4 error code */          /* XviD Error code  -> Divx4 error code */
467          switch(xerr)          switch (xerr) {
         {  
468          case XVID_ERR_OK :          case XVID_ERR_OK :
469                  return ENC_OK;                  return ENC_OK;
470          case XVID_ERR_MEMORY :          case XVID_ERR_MEMORY :
# Line 453  Line 485 
485   ***************************************/   ***************************************/
486    
487  /* Find an element in the chainlist according to its key value */  /* Find an element in the chainlist according to its key value */
488  static DINST * dinst_find(unsigned long key)  static DINST *
489    dinst_find(unsigned long key)
490  {  {
491          DINST * dcur = dhead;          DINST * dcur = dhead;
492    
493          while (dcur)          while (dcur) {
494          {                  if (dcur->key == key) {
                 if (dcur->key == key)  
                 {  
495                          return dcur;                          return dcur;
496                  }                  }
497                  dcur = dcur->next;                  dcur = dcur->next;
# Line 471  Line 502 
502    
503    
504  /* Add an element to the chainlist */  /* Add an element to the chainlist */
505  static DINST * dinst_add(unsigned long key)  static DINST *
506    dinst_add(unsigned long key)
507  {  {
508          DINST * dnext = dhead;          DINST * dnext = dhead;
509    
510          dhead = malloc(sizeof(DINST));          dhead = malloc(sizeof(DINST));
511          if (dhead == NULL)          if (dhead == NULL) {
         {  
512                  dhead = dnext;                  dhead = dnext;
513                  return NULL;                  return NULL;
514          }          }
# Line 490  Line 521 
521    
522    
523  /* Remove an elmement from the chainlist */  /* Remove an elmement from the chainlist */
524  static void dinst_remove(unsigned long key)  static void
525    dinst_remove(unsigned long key)
526  {  {
527          DINST * dcur = dhead;          DINST * dcur = dhead;
528    
529          if (dhead == NULL)          if (dhead == NULL) {
         {  
530                  return;                  return;
531          }          }
532    
533          if (dcur->key == key)          if (dcur->key == key) {
         {  
534                  dhead = dcur->next;                  dhead = dcur->next;
535                  free(dcur);                  free(dcur);
536                  return;                  return;
537          }          }
538    
539          while (dcur->next)          while (dcur->next) {
540          {                  if (dcur->next->key == key) {
                 if (dcur->next->key == key)  
                 {  
541                          DINST * tmp = dcur->next;                          DINST * tmp = dcur->next;
542    
543                          dcur->next = tmp->next;                          dcur->next = tmp->next;
544                          free(tmp);                          free(tmp);
545                          return;                          return;
# Line 519  Line 548 
548          }          }
549  }  }
550    
551    
552  /***************************************  /***************************************
553   * Colorspace code converter           *   * EINST chainlist helper functions    *
554   ***************************************/   ***************************************/
555    
556  static int xvid_to_opendivx_dec_csp(int csp)  /* Find an element in the chainlist according to its handle */
557    static EINST *
558    einst_find(void *handle)
559  {  {
560            EINST *ecur = ehead;
561    
562            while (ecur) {
563                    if (ecur->handle == handle) {
564                            return ecur;
565                    }
566                    ecur = ecur->next;
567            }
568    
569            return NULL;
570    }
571    
572    
573          switch(csp)  /* Add an element to the chainlist */
574    static EINST *
575    einst_add(void *handle)
576          {          {
577            EINST *enext = ehead;
578    
579            ehead = malloc(sizeof(EINST));
580            if (ehead == NULL) {
581                    ehead = enext;
582                    return NULL;
583            }
584    
585            ehead->handle = handle;
586            ehead->next = enext;
587    
588            return ehead;
589    }
590    
591    
592    /* Remove an elmement from the chainlist */
593    static void
594    einst_remove(void *handle)
595    {
596            EINST *ecur = ehead;
597    
598            if (ehead == NULL) {
599                    return;
600            }
601    
602            if (ecur->handle == handle) {
603                    ehead = ecur->next;
604                    free(ecur);
605                    return;
606            }
607    
608            while (ecur->next) {
609                    if (ecur->next->handle == handle) {
610                            EINST *tmp = ecur->next;
611    
612                            ecur->next = tmp->next;
613                            free(tmp);
614                            return;
615                    }
616                    ecur = ecur->next;
617            }
618    }
619    
620    /***************************************
621     * Colorspace code converter           *
622     ***************************************/
623    
624    static int
625    xvid_to_opendivx_dec_csp(int csp)
626    {
627    
628            switch (csp) {
629          case DEC_YV12 :          case DEC_YV12 :
630                  return XVID_CSP_YV12;                  return XVID_CSP_YV12;
631          case DEC_420 :          case DEC_420 :
# Line 559  Line 657 
657          }          }
658  }  }
659    
660  static int xvid_to_opendivx_enc_csp(int csp)  static int
661    xvid_to_opendivx_enc_csp(int csp)
662  {  {
663    
664          switch (csp)          switch (csp) {
         {  
665          case ENC_CSP_RGB24 :          case ENC_CSP_RGB24 :
666                  return XVID_CSP_VFLIP | XVID_CSP_RGB24;                  return XVID_CSP_VFLIP | XVID_CSP_RGB24;
667          case ENC_CSP_YV12 :          case ENC_CSP_YV12 :

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.14

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