[svn] / trunk / xvidcore / dshow / src / CXvidDecoder.cpp Repository:
ViewVC logotype

Diff of /trunk/xvidcore/dshow/src/CXvidDecoder.cpp

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

revision 1427, Wed Apr 14 02:44:20 2004 UTC revision 1605, Mon Mar 14 01:18:20 2005 UTC
# Line 19  Line 19 
19   *  along with this program ; if not, write to the Free Software   *  along with this program ; if not, write to the Free Software
20   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21   *   *
22   * $Id: CXvidDecoder.cpp,v 1.4 2004-04-14 02:44:20 syskin Exp $   * $Id: CXvidDecoder.cpp,v 1.14 2005-03-14 01:18:20 Isibaar Exp $
23   *   *
24   ****************************************************************************/   ****************************************************************************/
25    
# Line 36  Line 36 
36          place these paths at the top of the Tools|Options|Directories list          place these paths at the top of the Tools|Options|Directories list
37    
38          headers:          headers:
39          C:\DXVCSDK\include          C:\DX90SDK\Include
40          C:\DXVCSDK\samples\Multimedia\DirectShow\BaseClasses          C:\DX90SDK\Samples\C++\DirectShow\BaseClasses
41    
42          libraries (optional):          C:\DX90SDK\Samples\C++\DirectShow\BaseClasses\Release
43          C:\DXVCSDK\samples\Multimedia\DirectShow\BaseClasses\Release          C:\DX90SDK\Samples\C++\DirectShow\BaseClasses\Debug
44  */  */
45    
46    
# Line 204  Line 204 
204  #define XVID_DLL_NAME "xvidcore.dll"  #define XVID_DLL_NAME "xvidcore.dll"
205    
206  CXvidDecoder::CXvidDecoder(LPUNKNOWN punk, HRESULT *phr) :  CXvidDecoder::CXvidDecoder(LPUNKNOWN punk, HRESULT *phr) :
207      CVideoTransformFilter(NAME("CXvidDecoder"), punk, CLSID_XVID)      CVideoTransformFilter(NAME("CXvidDecoder"), punk, CLSID_XVID), m_hdll (NULL)
208  {  {
209          DPRINTF("Constructor");          DPRINTF("Constructor");
210    
211        xvid_decore_func = NULL; // Hmm, some strange errors appearing if I try to initialize...
212        xvid_global_func = NULL; // ...this in constructor's init-list. So, they assigned here.
213    
214        LoadRegistryInfo();
215    
216        *phr = OpenLib();
217    }
218    
219    HRESULT CXvidDecoder::OpenLib()
220    {
221        DPRINTF("OpenLib");
222    
223        if (m_hdll != NULL)
224                    return E_UNEXPECTED; // Seems, that library already opened.
225    
226          xvid_gbl_init_t init;          xvid_gbl_init_t init;
227          memset(&init, 0, sizeof(init));          memset(&init, 0, sizeof(init));
228          init.version = XVID_VERSION;          init.version = XVID_VERSION;
229    
         ar_x = ar_y = 0;  
   
230          m_hdll = LoadLibrary(XVID_DLL_NAME);          m_hdll = LoadLibrary(XVID_DLL_NAME);
231          if (m_hdll == NULL) {          if (m_hdll == NULL) {
232                  DPRINTF("dll load failed");                  DPRINTF("dll load failed");
233                  MessageBox(0, XVID_DLL_NAME " not found","Error", 0);                  MessageBox(0, XVID_DLL_NAME " not found","Error", MB_TOPMOST);
234                  return;                  return E_FAIL;
235          }          }
236    
237          xvid_global_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_global");          xvid_global_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_global");
238          if (xvid_global_func == NULL) {          if (xvid_global_func == NULL) {
239                  MessageBox(0, "xvid_global() not found", "Error", 0);          FreeLibrary(m_hdll);
240                  return;          m_hdll = NULL;
241                    MessageBox(0, "xvid_global() not found", "Error", MB_TOPMOST);
242                    return E_FAIL;
243          }          }
244    
245          xvid_decore_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_decore");          xvid_decore_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_decore");
246          if (xvid_decore_func == NULL) {          if (xvid_decore_func == NULL) {
247                  MessageBox(0, "xvid_decore() not found", "Error", 0);          xvid_global_func = NULL;
248                  return;          FreeLibrary(m_hdll);
249            m_hdll = NULL;
250                    MessageBox(0, "xvid_decore() not found", "Error", MB_TOPMOST);
251                    return E_FAIL;
252          }          }
253    
254          if (xvid_global_func(0, XVID_GBL_INIT, &init, NULL) < 0)          if (xvid_global_func(0, XVID_GBL_INIT, &init, NULL) < 0)
255          {          {
256                  MessageBox(0, "xvid_global() failed", "Error", 0);          xvid_global_func = NULL;
257                  return;          xvid_decore_func = NULL;
258            FreeLibrary(m_hdll);
259            m_hdll = NULL;
260                    MessageBox(0, "xvid_global() failed", "Error", MB_TOPMOST);
261                    return E_FAIL;
262          }          }
263    
264          memset(&m_create, 0, sizeof(m_create));          memset(&m_create, 0, sizeof(m_create));
# Line 246  Line 268 
268          memset(&m_frame, 0, sizeof(m_frame));          memset(&m_frame, 0, sizeof(m_frame));
269          m_frame.version = XVID_VERSION;          m_frame.version = XVID_VERSION;
270    
         LoadRegistryInfo();  
   
271          USE_IYUV = false;          USE_IYUV = false;
272          USE_YV12 = false;          USE_YV12 = false;
273          USE_YUY2 = false;          USE_YUY2 = false;
# Line 285  Line 305 
305                  USE_RGB32 = true;                  USE_RGB32 = true;
306                  break;                  break;
307          }          }
 }  
   
308    
309            switch (g_config.aspect_ratio)
310            {
311            case 0:
312            case 1:
313                    break;
314            case 2:
315                    ar_x = 4;
316                    ar_y = 3;
317                    break;
318            case 3:
319                    ar_x = 16;
320                    ar_y = 9;
321                    break;
322            case 4:
323                    ar_x = 47;
324                    ar_y = 20;
325                    break;
326            }
327    
328  /* destructor */          return S_OK;
329    }
330    
331  CXvidDecoder::~CXvidDecoder()  void CXvidDecoder::CloseLib()
332  {  {
333          DPRINTF("Destructor");          DPRINTF("CloseLib");
334    
335          if (m_create.handle != NULL)          if ((m_create.handle != NULL) && (xvid_decore_func != NULL))
336          {          {
337                  xvid_decore_func(m_create.handle, XVID_DEC_DESTROY, 0, 0);                  xvid_decore_func(m_create.handle, XVID_DEC_DESTROY, 0, 0);
338                  m_create.handle = NULL;                  m_create.handle = NULL;
339          }          }
340    
341          if (m_hdll != NULL)          if (m_hdll != NULL) {
         {  
342                  FreeLibrary(m_hdll);                  FreeLibrary(m_hdll);
343                  m_hdll = NULL;                  m_hdll = NULL;
344          }          }
345        xvid_decore_func = NULL;
346        xvid_global_func = NULL;
347    }
348    
349    /* destructor */
350    
351    CXvidDecoder::~CXvidDecoder()
352    {
353        DPRINTF("Destructor");
354            CloseLib();
355  }  }
356    
357    
# Line 317  Line 363 
363          DPRINTF("CheckInputType");          DPRINTF("CheckInputType");
364          BITMAPINFOHEADER * hdr;          BITMAPINFOHEADER * hdr;
365    
366            ar_x = ar_y = 0;
367    
368          if (*mtIn->Type() != MEDIATYPE_Video)          if (*mtIn->Type() != MEDIATYPE_Video)
369          {          {
370                  DPRINTF("Error: Unknown Type");                  DPRINTF("Error: Unknown Type");
371                    CloseLib();
372                    return VFW_E_TYPE_NOT_ACCEPTED;
373            }
374    
375        if (m_hdll == NULL)
376        {
377                    HRESULT hr = OpenLib();
378    
379            if (FAILED(hr) || (m_hdll == NULL)) // Paranoid checks.
380                  return VFW_E_TYPE_NOT_ACCEPTED;                  return VFW_E_TYPE_NOT_ACCEPTED;
381          }          }
382    
# Line 327  Line 384 
384          {          {
385                  VIDEOINFOHEADER * vih = (VIDEOINFOHEADER *) mtIn->Format();                  VIDEOINFOHEADER * vih = (VIDEOINFOHEADER *) mtIn->Format();
386                  hdr = &vih->bmiHeader;                  hdr = &vih->bmiHeader;
                 /* PAR (x:y) is (1/ppm_X):(1/ppm_Y) where ppm is pixels-per-meter  
                    which is equal to ppm_Y:ppm_X */  
                 ar_x = vih->bmiHeader.biYPelsPerMeter * abs(hdr->biWidth);  
                 ar_y = vih->bmiHeader.biXPelsPerMeter * abs(hdr->biHeight);  
                 DPRINTF("VIDEOINFOHEADER PAR: %d:%d -> AR %d:%d",  
                         vih->bmiHeader.biYPelsPerMeter,vih->bmiHeader.biXPelsPerMeter, ar_x, ar_y);  
387          }          }
388          else if (*mtIn->FormatType() == FORMAT_VideoInfo2)          else if (*mtIn->FormatType() == FORMAT_VideoInfo2)
389          {          {
390                  VIDEOINFOHEADER2 * vih2 = (VIDEOINFOHEADER2 *) mtIn->Format();                  VIDEOINFOHEADER2 * vih2 = (VIDEOINFOHEADER2 *) mtIn->Format();
391                  hdr = &vih2->bmiHeader;                  hdr = &vih2->bmiHeader;
392                    if (g_config.aspect_ratio == 0 || g_config.aspect_ratio == 1) {
393                  ar_x = vih2->dwPictAspectRatioX;                  ar_x = vih2->dwPictAspectRatioX;
394                  ar_y = vih2->dwPictAspectRatioY;                  ar_y = vih2->dwPictAspectRatioY;
395                    }
396                  DPRINTF("VIDEOINFOHEADER2 AR: %d:%d", ar_x, ar_y);                  DPRINTF("VIDEOINFOHEADER2 AR: %d:%d", ar_x, ar_y);
397          }          }
398          else          else
399          {          {
400                  DPRINTF("Error: Unknown FormatType");                  DPRINTF("Error: Unknown FormatType");
401                    CloseLib();
402                  return VFW_E_TYPE_NOT_ACCEPTED;                  return VFW_E_TYPE_NOT_ACCEPTED;
403          }          }
404    
# Line 360  Line 414 
414          {          {
415    
416          case FOURCC_MP4V:          case FOURCC_MP4V:
417                  if (!(g_config.supported_4cc & SUPPORT_MP4V)) return VFW_E_TYPE_NOT_ACCEPTED;                  if (!(g_config.supported_4cc & SUPPORT_MP4V)) {
418                            CloseLib();
419                            return VFW_E_TYPE_NOT_ACCEPTED;
420                    }
421                  break;                  break;
422          case FOURCC_DIVX :          case FOURCC_DIVX :
423                  if (!(g_config.supported_4cc & SUPPORT_DIVX)) return VFW_E_TYPE_NOT_ACCEPTED;                  if (!(g_config.supported_4cc & SUPPORT_DIVX)) {
424                            CloseLib();
425                            return VFW_E_TYPE_NOT_ACCEPTED;
426                    }
427                  break;                  break;
428          case FOURCC_DX50 :          case FOURCC_DX50 :
429                  if (!(g_config.supported_4cc & SUPPORT_DX50)) return VFW_E_TYPE_NOT_ACCEPTED;                  if (!(g_config.supported_4cc & SUPPORT_DX50)) {
430                            CloseLib();
431                            return VFW_E_TYPE_NOT_ACCEPTED;
432                    }
433          case FOURCC_XVID :          case FOURCC_XVID :
434                  break;                  break;
435    
# Line 378  Line 441 
441                          (hdr->biCompression>>8)&0xff,                          (hdr->biCompression>>8)&0xff,
442                          (hdr->biCompression>>16)&0xff,                          (hdr->biCompression>>16)&0xff,
443                          (hdr->biCompression>>24)&0xff);                          (hdr->biCompression>>24)&0xff);
444                    CloseLib();
445                  return VFW_E_TYPE_NOT_ACCEPTED;                  return VFW_E_TYPE_NOT_ACCEPTED;
446          }          }
447          return S_OK;          return S_OK;
# Line 408  Line 472 
472                  if (ar_x != 0 && ar_y != 0) {                  if (ar_x != 0 && ar_y != 0) {
473                          vih->dwPictAspectRatioX = ar_x;                          vih->dwPictAspectRatioX = ar_x;
474                          vih->dwPictAspectRatioY = ar_y;                          vih->dwPictAspectRatioY = ar_y;
475                            forced_ar = true;
476                  } else { // just to be safe                  } else { // just to be safe
477                          vih->dwPictAspectRatioX = m_create.width;                          vih->dwPictAspectRatioX = m_create.width;
478                          vih->dwPictAspectRatioY = abs(m_create.height);                          vih->dwPictAspectRatioY = abs(m_create.height);
479                            forced_ar = false;
480                  }                  }
   
481          } else {          } else {
482    
483                  VIDEOINFOHEADER * vih = (VIDEOINFOHEADER *) mtOut->ReallocFormatBuffer(sizeof(VIDEOINFOHEADER));                  VIDEOINFOHEADER * vih = (VIDEOINFOHEADER *) mtOut->ReallocFormatBuffer(sizeof(VIDEOINFOHEADER));
# Line 520  Line 585 
585    
586    
587  /* (internal function) change colorspace */  /* (internal function) change colorspace */
588    #define CALC_BI_STRIDE(width,bitcount)  ((((width * bitcount) + 31) & ~31) >> 3)
589    
590  HRESULT CXvidDecoder::ChangeColorspace(GUID subtype, GUID formattype, void * format)  HRESULT CXvidDecoder::ChangeColorspace(GUID subtype, GUID formattype, void * format)
591  {  {
592            DWORD biWidth;
593    
594          if (formattype == FORMAT_VideoInfo)          if (formattype == FORMAT_VideoInfo)
595          {          {
596                  VIDEOINFOHEADER * vih = (VIDEOINFOHEADER * )format;                  VIDEOINFOHEADER * vih = (VIDEOINFOHEADER * )format;
597                  m_frame.output.stride[0] = (((vih->bmiHeader.biWidth * vih->bmiHeader.biBitCount) + 31) & ~31) >> 3;                  biWidth = vih->bmiHeader.biWidth;
598                    m_frame.output.stride[0] = CALC_BI_STRIDE(vih->bmiHeader.biWidth, vih->bmiHeader.biBitCount);
599                  rgb_flip = (vih->bmiHeader.biHeight < 0 ? 0 : XVID_CSP_VFLIP);                  rgb_flip = (vih->bmiHeader.biHeight < 0 ? 0 : XVID_CSP_VFLIP);
600          }          }
601          else if (formattype == FORMAT_VideoInfo2)          else if (formattype == FORMAT_VideoInfo2)
602          {          {
603                  VIDEOINFOHEADER2 * vih2 = (VIDEOINFOHEADER2 * )format;                  VIDEOINFOHEADER2 * vih2 = (VIDEOINFOHEADER2 * )format;
604                  m_frame.output.stride[0] = (((vih2->bmiHeader.biWidth * vih2->bmiHeader.biBitCount) + 31) & ~31) >> 3;                  biWidth = vih2->bmiHeader.biWidth;
605                    m_frame.output.stride[0] = CALC_BI_STRIDE(vih2->bmiHeader.biWidth, vih2->bmiHeader.biBitCount);
606                  rgb_flip = (vih2->bmiHeader.biHeight < 0 ? 0 : XVID_CSP_VFLIP);                  rgb_flip = (vih2->bmiHeader.biHeight < 0 ? 0 : XVID_CSP_VFLIP);
607          }          }
608          else          else
# Line 545  Line 615 
615                  DPRINTF("IYUV");                  DPRINTF("IYUV");
616                  rgb_flip = 0;                  rgb_flip = 0;
617                  m_frame.output.csp = XVID_CSP_I420;                  m_frame.output.csp = XVID_CSP_I420;
618                  m_frame.output.stride[0] = (m_frame.output.stride[0] * 2) / 3;  /* planar format fix */                  m_frame.output.stride[0] = CALC_BI_STRIDE(biWidth, 8);  /* planar format fix */
619          }          }
620          else if (subtype == MEDIASUBTYPE_YV12)          else if (subtype == MEDIASUBTYPE_YV12)
621          {          {
622                  DPRINTF("YV12");                  DPRINTF("YV12");
623                  rgb_flip = 0;                  rgb_flip = 0;
624                  m_frame.output.csp = XVID_CSP_YV12;                  m_frame.output.csp = XVID_CSP_YV12;
625                  m_frame.output.stride[0] = (m_frame.output.stride[0] * 2) / 3;  /* planar format fix */                  m_frame.output.stride[0] = CALC_BI_STRIDE(biWidth, 8);  /* planar format fix */
626          }          }
627          else if (subtype == MEDIASUBTYPE_YUY2)          else if (subtype == MEDIASUBTYPE_YUY2)
628          {          {
# Line 676  Line 746 
746    
747          if (m_create.handle == NULL)          if (m_create.handle == NULL)
748          {          {
749                    if (xvid_decore_func == NULL)
750                            return E_FAIL;
751    
752                  if (xvid_decore_func(0, XVID_DEC_CREATE, &m_create, 0) < 0)                  if (xvid_decore_func(0, XVID_DEC_CREATE, &m_create, 0) < 0)
753                  {                  {
754              DPRINTF("*** XVID_DEC_CREATE error");              DPRINTF("*** XVID_DEC_CREATE error");
# Line 720  Line 793 
793    
794          if (g_config.nDeblock_UV)          if (g_config.nDeblock_UV)
795                  m_frame.general |= XVID_DEBLOCKUV;                  m_frame.general |= XVID_DEBLOCKUV;
796  /*  
797          if (g_config.nDering)          if (g_config.nDering_Y)
798                  m_frame.general |= XVID_DERING;                  m_frame.general |= XVID_DERINGY;
799  */  
800            if (g_config.nDering_UV)
801                    m_frame.general |= XVID_DERINGUV;
802    
803          if (g_config.nFilmEffect)          if (g_config.nFilmEffect)
804                  m_frame.general |= XVID_FILMEFFECT;                  m_frame.general |= XVID_FILMEFFECT;
805    
# Line 732  Line 808 
808          m_frame.output.csp &= ~XVID_CSP_VFLIP;          m_frame.output.csp &= ~XVID_CSP_VFLIP;
809          m_frame.output.csp |= rgb_flip^(g_config.nFlipVideo ? XVID_CSP_VFLIP : 0);          m_frame.output.csp |= rgb_flip^(g_config.nFlipVideo ? XVID_CSP_VFLIP : 0);
810    
811        // Paranoid check.
812        if (xvid_decore_func == NULL)
813                    return E_FAIL;
814    
815    
816    
817  repeat :  repeat :
818    
819          if (pIn->IsPreroll() != S_OK)          if (pIn->IsPreroll() != S_OK)
# Line 742  Line 824 
824                  {                  {
825              DPRINTF("*** XVID_DEC_DECODE");              DPRINTF("*** XVID_DEC_DECODE");
826                          return S_FALSE;                          return S_FALSE;
827                    } else
828                            if (g_config.aspect_ratio == 0 || g_config.aspect_ratio == 1 && forced_ar == false) {
829                            // inspired by minolta! works for VMR 7 + 9
830    
831                            IMediaSample2 *pOut2 = NULL;
832                            AM_SAMPLE2_PROPERTIES outProp2;
833                            if (SUCCEEDED(pOut->QueryInterface(IID_IMediaSample2, (void **)&pOut2)) &&
834                                    SUCCEEDED(pOut2->GetProperties(FIELD_OFFSET(AM_SAMPLE2_PROPERTIES, tStart), (PBYTE)&outProp2)))
835                            {
836                                    CMediaType mtOut2 = m_pOutput->CurrentMediaType();
837                                    VIDEOINFOHEADER2* vihOut2 = (VIDEOINFOHEADER2*)mtOut2.Format();
838    
839                                    if (*mtOut2.FormatType() == FORMAT_VideoInfo2 &&
840                                            vihOut2->dwPictAspectRatioX != ar_x && vihOut2->dwPictAspectRatioY != ar_y)
841                                    {
842                                            vihOut2->dwPictAspectRatioX = ar_x;
843                                            vihOut2->dwPictAspectRatioY = ar_y;
844                                            pOut2->SetMediaType(&mtOut2);
845                                            m_pOutput->SetMediaType(&mtOut2);
846                                    }
847                                    pOut2->Release();
848                            }
849                  }                  }
850          }          }
851          else          else
# Line 783  Line 887 
887                          return S_FALSE;                          return S_FALSE;
888                  }                  }
889    
 //              pOut->SetDiscontinuity(TRUE);  
890                  pOut->SetSyncPoint(TRUE);                  pOut->SetSyncPoint(TRUE);
891    
892                    if (g_config.aspect_ratio == 0 || g_config.aspect_ratio == 1) { /* auto */
893                            int par_x, par_y;
894                            if (stats.data.vol.par == XVID_PAR_EXT) {
895                                    par_x = stats.data.vol.par_width;
896                                    par_y = stats.data.vol.par_height;
897                            } else {
898                                    par_x = PARS[stats.data.vol.par-1][0];
899                                    par_y = PARS[stats.data.vol.par-1][1];
900                            }
901    
902                            ar_x = par_x * stats.data.vol.width;
903                            ar_y = par_y * stats.data.vol.height;
904                    }
905    
906                  m_frame.bitstream = (BYTE*)m_frame.bitstream + length;                  m_frame.bitstream = (BYTE*)m_frame.bitstream + length;
907                  m_frame.length -= length;                  m_frame.length -= length;
908                  goto repeat;                  goto repeat;

Legend:
Removed from v.1427  
changed lines
  Added in v.1605

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