[svn] / tags / release-0_9_0 / dshow / src / CXvidDecoder.cpp Repository:
ViewVC logotype

Annotation of /tags/release-0_9_0/dshow/src/CXvidDecoder.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 615 - (view) (download)
Original Path: trunk/dshow/src/CXvidDecoder.cpp

1 : Isibaar 6 /*
2 :     this requires the directx sdk
3 :     place these paths at the top of the Tools|Options|Directories list
4 :    
5 :     headers:
6 :     C:\DXVCSDK\include
7 :     C:\DXVCSDK\samples\Multimedia\DirectShow\BaseClasses
8 :    
9 :     libraries (optional):
10 :     C:\DXVCSDK\samples\Multimedia\DirectShow\BaseClasses\Release
11 :     */
12 :    
13 :     #include <windows.h>
14 :    
15 :     #include <streams.h>
16 :     #include <initguid.h>
17 :     #include <olectl.h>
18 :     #if (1100 > _MSC_VER)
19 :     #include <olectlid.h>
20 :     #endif
21 :     #include <dvdmedia.h> // VIDEOINFOHEADER2
22 :    
23 : Isibaar 380 #include <xvid.h> // XviD API
24 :    
25 : Isibaar 6 #include "IXvidDecoder.h"
26 :     #include "CXvidDecoder.h"
27 :     #include "CAbout.h"
28 :    
29 :    
30 :    
31 :     const AMOVIESETUP_MEDIATYPE sudInputPinTypes[] =
32 :     {
33 :     { &MEDIATYPE_Video, &CLSID_XVID },
34 :     { &MEDIATYPE_Video, &CLSID_XVID_UC },
35 :     { &MEDIATYPE_Video, &CLSID_DIVX },
36 :     { &MEDIATYPE_Video, &CLSID_DIVX_UC }
37 :     };
38 :    
39 :     const AMOVIESETUP_MEDIATYPE sudOutputPinTypes[] =
40 :     {
41 :     { &MEDIATYPE_Video, &MEDIASUBTYPE_NULL }
42 :     };
43 :    
44 :    
45 :     const AMOVIESETUP_PIN psudPins[] =
46 :     {
47 :     {
48 :     L"Input", // String pin name
49 :     FALSE, // Is it rendered
50 :     FALSE, // Is it an output
51 :     FALSE, // Allowed none
52 :     FALSE, // Allowed many
53 :     &CLSID_NULL, // Connects to filter
54 :     L"Output", // Connects to pin
55 :     sizeof(sudInputPinTypes) / sizeof(AMOVIESETUP_MEDIATYPE), // Number of types
56 :     &sudInputPinTypes[0] // The pin details
57 :     },
58 :     {
59 :     L"Output", // String pin name
60 :     FALSE, // Is it rendered
61 :     TRUE, // Is it an output
62 :     FALSE, // Allowed none
63 :     FALSE, // Allowed many
64 :     &CLSID_NULL, // Connects to filter
65 :     L"Input", // Connects to pin
66 :     sizeof(sudOutputPinTypes) / sizeof(AMOVIESETUP_MEDIATYPE), // Number of types
67 :     sudOutputPinTypes // The pin details
68 :     }
69 :     };
70 :    
71 :    
72 :     const AMOVIESETUP_FILTER sudXvidDecoder =
73 :     {
74 :     &CLSID_XVID, // Filter CLSID
75 :     XVID_NAME_L, // Filter name
76 :     MERIT_PREFERRED, // Its merit
77 :     sizeof(psudPins) / sizeof(AMOVIESETUP_PIN), // Number of pins
78 :     psudPins // Pin details
79 :     };
80 :    
81 :    
82 :     // List of class IDs and creator functions for the class factory. This
83 :     // provides the link between the OLE entry point in the DLL and an object
84 :     // being created. The class factory will call the static CreateInstance
85 :    
86 :     CFactoryTemplate g_Templates[] =
87 :     {
88 :     {
89 :     XVID_NAME_L,
90 :     &CLSID_XVID,
91 :     CXvidDecoder::CreateInstance,
92 :     NULL,
93 :     &sudXvidDecoder
94 :     },
95 :     {
96 :     XVID_NAME_L L"About",
97 :     &CLSID_CABOUT,
98 :     CAbout::CreateInstance
99 :     }
100 :    
101 :     };
102 :    
103 :     int g_cTemplates = sizeof(g_Templates) / sizeof(CFactoryTemplate);
104 :    
105 :    
106 :    
107 :    
108 :     STDAPI DllRegisterServer()
109 :     {
110 :     return AMovieDllRegisterServer2( TRUE );
111 :     }
112 :    
113 :    
114 :     STDAPI DllUnregisterServer()
115 :     {
116 :     return AMovieDllRegisterServer2( FALSE );
117 :     }
118 :    
119 :    
120 :     /* create instance */
121 :    
122 :     CUnknown * WINAPI CXvidDecoder::CreateInstance(LPUNKNOWN punk, HRESULT *phr)
123 :     {
124 :     CXvidDecoder * pNewObject = new CXvidDecoder(punk, phr);
125 :     if (pNewObject == NULL)
126 :     {
127 :     *phr = E_OUTOFMEMORY;
128 :     }
129 :     return pNewObject;
130 :     }
131 :    
132 :    
133 :     /* query interfaces */
134 :    
135 :     STDMETHODIMP CXvidDecoder::NonDelegatingQueryInterface(REFIID riid, void **ppv)
136 :     {
137 :     CheckPointer(ppv, E_POINTER);
138 :    
139 :     if (riid == IID_IXvidDecoder)
140 :     {
141 :     return GetInterface((IXvidDecoder *) this, ppv);
142 :     }
143 :    
144 :     if (riid == IID_ISpecifyPropertyPages)
145 :     {
146 :     return GetInterface((ISpecifyPropertyPages *) this, ppv);
147 :     }
148 :    
149 :     return CVideoTransformFilter::NonDelegatingQueryInterface(riid, ppv);
150 :     }
151 :    
152 :    
153 :    
154 :     /* dummy decore() */
155 :    
156 :     static int dummy_xvid_decore(void * handle, int opt, void * param1, void * param2)
157 :     {
158 :     return XVID_ERR_FAIL;
159 :     }
160 :    
161 :    
162 :    
163 :     /* constructor */
164 :    
165 :     #define XVID_DLL_NAME "xvid.dll"
166 :     #define XVID_INIT_NAME "xvid_init"
167 :     #define XVID_DECORE_NAME "xvid_decore"
168 :    
169 :     CXvidDecoder::CXvidDecoder(LPUNKNOWN punk, HRESULT *phr) :
170 :     CVideoTransformFilter(NAME("CXvidDecoder"), punk, CLSID_XVID)
171 :     {
172 :     DEBUG("Constructor");
173 :    
174 :     ASSERT(phr);
175 :    
176 :     m_xvid_decore = dummy_xvid_decore;
177 :    
178 :     m_hdll = LoadLibrary(XVID_DLL_NAME);
179 :     if (m_hdll == NULL) {
180 :     DEBUG("dll load failed");
181 :     MessageBox(0, XVID_DLL_NAME " not found","Error", 0);
182 :     return;
183 :     }
184 :    
185 :     m_xvid_init = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, XVID_INIT_NAME);
186 :     if (m_xvid_init == NULL) {
187 :     MessageBox(0, XVID_INIT_NAME "() not found", "Error", 0);
188 :     return;
189 :     }
190 :    
191 :     m_xvid_decore = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, XVID_DECORE_NAME);
192 :     if (m_xvid_decore == NULL) {
193 :     MessageBox(0, XVID_DECORE_NAME "() not found", "Error", 0);
194 :     return;
195 :     }
196 :    
197 :     XVID_INIT_PARAM init;
198 :    
199 :     init.cpu_flags = 0;
200 :     if (m_xvid_init(0, 0, &init, 0) != XVID_ERR_OK)
201 :     {
202 :     MessageBox(0, XVID_INIT_NAME "() failed", "Error", 0);
203 :     return;
204 :     }
205 :    
206 :     if (init.api_version < API_VERSION)
207 :     {
208 :     MessageBox(0, XVID_DLL_NAME " implements an older api version; update your " XVID_DLL_NAME, "Warning", 0);
209 :     }
210 :     else if (init.api_version > API_VERSION)
211 :     {
212 :     MessageBox(0, XVID_DLL_NAME " implements a newer api version; update your directshow filter", "Error", 0);
213 :     return;
214 :     }
215 :    
216 :     m_param.handle = NULL;
217 :     }
218 :    
219 :    
220 :    
221 :     /* destructor */
222 :    
223 :     CXvidDecoder::~CXvidDecoder()
224 :     {
225 :     DEBUG("Destructor");
226 :    
227 :     if (m_param.handle != NULL)
228 :     {
229 :     m_xvid_decore(m_param.handle, XVID_DEC_DESTROY, 0, 0);
230 :     m_param.handle = NULL;
231 :     }
232 :    
233 :     if (m_hdll != NULL)
234 :     {
235 :     FreeLibrary(m_hdll);
236 :     m_hdll = NULL;
237 :     }
238 :     }
239 :    
240 :    
241 :    
242 :     /* check input type */
243 :    
244 :     HRESULT CXvidDecoder::CheckInputType(const CMediaType * mtIn)
245 :     {
246 :     DEBUG("CheckInputType");
247 :     BITMAPINFOHEADER * hdr;
248 :    
249 :     if (*mtIn->Type() != MEDIATYPE_Video)
250 :     {
251 :     return VFW_E_TYPE_NOT_ACCEPTED;
252 :     }
253 :    
254 :     if (*mtIn->FormatType() == FORMAT_VideoInfo)
255 :     {
256 :     VIDEOINFOHEADER * vih = (VIDEOINFOHEADER *) mtIn->Format();
257 :     hdr = &vih->bmiHeader;
258 :     }
259 :     else if (*mtIn->FormatType() == FORMAT_VideoInfo2)
260 :     {
261 :     VIDEOINFOHEADER2 * vih2 = (VIDEOINFOHEADER2 *) mtIn->Format();
262 :     hdr = &vih2->bmiHeader;
263 :     }
264 :     else
265 :     {
266 :     return VFW_E_TYPE_NOT_ACCEPTED;
267 :     }
268 :    
269 :     if (hdr->biHeight < 0)
270 :     {
271 :     DEBUG("colorspace: inverted input format not supported");
272 :     }
273 :    
274 :     m_param.width = hdr->biWidth;
275 :     m_param.height = hdr->biHeight;
276 :    
277 :     switch(hdr->biCompression)
278 :     {
279 :     case FOURCC_XVID :
280 :     // case FOURCC_DIVX :
281 :     break;
282 :    
283 :     default :
284 :     return VFW_E_TYPE_NOT_ACCEPTED;
285 :     }
286 :    
287 :     return S_OK;
288 :     }
289 :    
290 :    
291 :     /* get list of supported output colorspaces */
292 :    
293 :     HRESULT CXvidDecoder::GetMediaType(int iPosition, CMediaType *mtOut)
294 :     {
295 :     DEBUG("GetMediaType");
296 :    
297 :     if (m_pInput->IsConnected() == FALSE)
298 :     {
299 :     return E_UNEXPECTED;
300 :     }
301 :    
302 :     VIDEOINFOHEADER * vih = (VIDEOINFOHEADER *) mtOut->ReallocFormatBuffer(sizeof(VIDEOINFOHEADER));
303 :     if (vih == NULL)
304 :     {
305 :     return E_OUTOFMEMORY;
306 :     }
307 :    
308 :     ZeroMemory(vih, sizeof (VIDEOINFOHEADER));
309 :     vih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
310 :     vih->bmiHeader.biWidth = m_param.width;
311 :     vih->bmiHeader.biHeight = m_param.height;
312 :     vih->bmiHeader.biPlanes = 1;
313 :    
314 :     if (iPosition < 0)
315 :     {
316 :     return E_INVALIDARG;
317 :     }
318 :    
319 :     switch(iPosition)
320 :     {
321 :     case 0 :
322 : suxen_drol 615 vih->bmiHeader.biCompression = MEDIASUBTYPE_IYUV.Data1;
323 :     vih->bmiHeader.biBitCount = 12;
324 :     mtOut->SetSubtype(&MEDIASUBTYPE_IYUV);
325 :     break;
326 :    
327 :     case 1 :
328 : Isibaar 6 vih->bmiHeader.biCompression = MEDIASUBTYPE_YV12.Data1;
329 :     vih->bmiHeader.biBitCount = 12;
330 :     mtOut->SetSubtype(&MEDIASUBTYPE_YV12);
331 :     break;
332 :    
333 : suxen_drol 615 case 2:
334 : Isibaar 6 vih->bmiHeader.biCompression = MEDIASUBTYPE_YUY2.Data1;
335 :     vih->bmiHeader.biBitCount = 16;
336 :     mtOut->SetSubtype(&MEDIASUBTYPE_YUY2);
337 :     break;
338 :    
339 : suxen_drol 615 case 3 :
340 : Isibaar 6 vih->bmiHeader.biCompression = MEDIASUBTYPE_YVYU.Data1;
341 :     vih->bmiHeader.biBitCount = 16;
342 :     mtOut->SetSubtype(&MEDIASUBTYPE_YVYU);
343 :     break;
344 :    
345 : suxen_drol 615 case 4 :
346 : Isibaar 6 vih->bmiHeader.biCompression = MEDIASUBTYPE_UYVY.Data1;
347 :     vih->bmiHeader.biBitCount = 16;
348 :     mtOut->SetSubtype(&MEDIASUBTYPE_UYVY);
349 :     break;
350 :    
351 : suxen_drol 615 case 5 :
352 : Isibaar 6 vih->bmiHeader.biCompression = BI_RGB;
353 :     vih->bmiHeader.biBitCount = 32;
354 :     mtOut->SetSubtype(&MEDIASUBTYPE_RGB32);
355 :     break;
356 :    
357 : suxen_drol 615 case 6 :
358 : Isibaar 6 vih->bmiHeader.biCompression = BI_RGB;
359 :     vih->bmiHeader.biBitCount = 24;
360 :     mtOut->SetSubtype(&MEDIASUBTYPE_RGB24);
361 :     break;
362 :    
363 : suxen_drol 615 case 7 :
364 : Isibaar 6 vih->bmiHeader.biCompression = BI_RGB;
365 :     vih->bmiHeader.biBitCount = 16;
366 :     mtOut->SetSubtype(&MEDIASUBTYPE_RGB555);
367 :     break;
368 :    
369 : suxen_drol 615 case 8 :
370 : Isibaar 6 vih->bmiHeader.biCompression = BI_RGB;
371 :     vih->bmiHeader.biBitCount = 16;
372 :     mtOut->SetSubtype(&MEDIASUBTYPE_RGB565);
373 :     break;
374 :    
375 :     default :
376 :     return VFW_S_NO_MORE_ITEMS;
377 :     }
378 :    
379 :     vih->bmiHeader.biSizeImage = GetBitmapSize(&vih->bmiHeader);
380 :    
381 :     mtOut->SetType(&MEDIATYPE_Video);
382 :     mtOut->SetFormatType(&FORMAT_VideoInfo);
383 :     mtOut->SetTemporalCompression(FALSE);
384 :     mtOut->SetSampleSize(vih->bmiHeader.biSizeImage);
385 :    
386 :     return S_OK;
387 :     }
388 :    
389 :    
390 :     /* (internal function) change colorspace */
391 :    
392 :     HRESULT CXvidDecoder::ChangeColorspace(GUID subtype, GUID formattype, void * format)
393 :     {
394 : suxen_drol 615 if (subtype == MEDIASUBTYPE_IYUV)
395 : Isibaar 6 {
396 : suxen_drol 615 DEBUG("IYUV");
397 :     m_frame.colorspace = XVID_CSP_I420;
398 :     }
399 :     else if (subtype == MEDIASUBTYPE_YV12)
400 :     {
401 : Isibaar 6 DEBUG("YV12");
402 :     m_frame.colorspace = XVID_CSP_YV12;
403 :     }
404 :     else if (subtype == MEDIASUBTYPE_YUY2)
405 :     {
406 :     DEBUG("YUY2");
407 :     m_frame.colorspace = XVID_CSP_YUY2;
408 :     }
409 :     else if (subtype == MEDIASUBTYPE_YVYU)
410 :     {
411 :     DEBUG("YVYU");
412 :     m_frame.colorspace = XVID_CSP_YVYU;
413 :     }
414 :     else if (subtype == MEDIASUBTYPE_UYVY)
415 :     {
416 :     DEBUG("UYVY");
417 :     m_frame.colorspace = XVID_CSP_UYVY;
418 :     }
419 :     else if (subtype == MEDIASUBTYPE_RGB32)
420 :     {
421 :     DEBUG("RGB32");
422 :     m_frame.colorspace = XVID_CSP_VFLIP | XVID_CSP_RGB32;
423 :     }
424 :     else if (subtype == MEDIASUBTYPE_RGB24)
425 :     {
426 :     DEBUG("RGB24");
427 :     m_frame.colorspace = XVID_CSP_VFLIP | XVID_CSP_RGB24;
428 :     }
429 :     else if (subtype == MEDIASUBTYPE_RGB555)
430 :     {
431 :     DEBUG("RGB555");
432 :     m_frame.colorspace = XVID_CSP_VFLIP | XVID_CSP_RGB555;
433 :     }
434 :     else if (subtype == MEDIASUBTYPE_RGB565)
435 :     {
436 :     DEBUG("RGB565");
437 :     m_frame.colorspace = XVID_CSP_VFLIP | XVID_CSP_RGB565;
438 :     }
439 :     else if (subtype == GUID_NULL)
440 :     {
441 :     m_frame.colorspace = XVID_CSP_NULL;
442 :     }
443 :     else
444 :     {
445 :     return S_FALSE;
446 :     }
447 :    
448 :    
449 :     if (formattype == FORMAT_VideoInfo)
450 :     {
451 :     VIDEOINFOHEADER * vih = (VIDEOINFOHEADER * )format;
452 :     m_frame.stride = vih->bmiHeader.biWidth;
453 :     }
454 :     else if (formattype == FORMAT_VideoInfo2)
455 :     {
456 :     VIDEOINFOHEADER2 * vih2 = (VIDEOINFOHEADER2 * )format;
457 :     m_frame.stride = vih2->bmiHeader.biWidth;
458 :     }
459 :     else
460 :     {
461 :     return S_FALSE;
462 :     }
463 :    
464 :     return S_OK;
465 :    
466 :     }
467 :    
468 :    
469 :     /* set output colorspace */
470 :    
471 :     HRESULT CXvidDecoder::SetMediaType(PIN_DIRECTION direction, const CMediaType *pmt)
472 :     {
473 :     DEBUG("SetMediaType");
474 :    
475 :     if (direction == PINDIR_OUTPUT)
476 :     {
477 :     return ChangeColorspace(*pmt->Subtype(), *pmt->FormatType(), pmt->Format());
478 :     }
479 :    
480 :     return S_OK;
481 :     }
482 :    
483 :    
484 :     /* check input<->output compatiblity */
485 :    
486 :     HRESULT CXvidDecoder::CheckTransform(const CMediaType *mtIn, const CMediaType *mtOut)
487 :     {
488 :     DEBUG("CheckTransform");
489 :     return S_OK;
490 :     }
491 :    
492 :    
493 :     /* alloc output buffer */
494 :    
495 :     HRESULT CXvidDecoder::DecideBufferSize(IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
496 :     {
497 :     DEBUG("DecideBufferSize");
498 :     HRESULT result;
499 :     ALLOCATOR_PROPERTIES ppropActual;
500 :    
501 :     if (m_pInput->IsConnected() == FALSE)
502 :     {
503 :     return E_UNEXPECTED;
504 :     }
505 :    
506 :     ppropInputRequest->cBuffers = 1;
507 :     ppropInputRequest->cbBuffer = m_param.width * m_param.height * 4;
508 :     // cbAlign causes problems with the resize filter */
509 :     // ppropInputRequest->cbAlign = 16;
510 :     ppropInputRequest->cbPrefix = 0;
511 :    
512 :     result = pAlloc->SetProperties(ppropInputRequest, &ppropActual);
513 :     if (result != S_OK)
514 :     {
515 :     return result;
516 :     }
517 :    
518 :     if (ppropActual.cbBuffer < ppropInputRequest->cbBuffer)
519 :     {
520 :     return E_FAIL;
521 :     }
522 :    
523 :     return S_OK;
524 :     }
525 :    
526 :    
527 :    
528 :     /* decode frame */
529 :    
530 :     HRESULT CXvidDecoder::Transform(IMediaSample *pIn, IMediaSample *pOut)
531 :     {
532 :     DEBUG("Transform");
533 :    
534 :     if (m_param.handle == NULL)
535 :     {
536 :     if (m_xvid_decore(0, XVID_DEC_CREATE, &m_param, 0) != XVID_ERR_OK)
537 :     {
538 :     return S_FALSE;
539 :     }
540 :     }
541 :    
542 :     AM_MEDIA_TYPE * mtOut;
543 :     pOut->GetMediaType(&mtOut);
544 :     if (mtOut != NULL)
545 :     {
546 :     HRESULT result;
547 :    
548 :     result = ChangeColorspace(mtOut->subtype, mtOut->formattype, mtOut->pbFormat);
549 :     DeleteMediaType(mtOut);
550 :    
551 :     if (result != S_OK)
552 :     {
553 :     return result;
554 :     }
555 :     }
556 :    
557 :     if (pIn->GetPointer((BYTE**)&m_frame.bitstream) != S_OK)
558 :     {
559 :     return S_FALSE;
560 :     }
561 :    
562 :     if (pOut->GetPointer((BYTE**)&m_frame.image) != S_OK)
563 :     {
564 :     return S_FALSE;
565 :     }
566 :    
567 :     m_frame.length = pIn->GetSize();
568 :    
569 :     if (pIn->IsPreroll() != S_OK)
570 :     {
571 :     if (m_xvid_decore(m_param.handle, XVID_DEC_DECODE, &m_frame, 0) != XVID_ERR_OK)
572 :     {
573 :     return S_FALSE;
574 :     }
575 :     }
576 :     else
577 :     {
578 :     int tmp = m_frame.colorspace;
579 :     m_frame.colorspace = XVID_CSP_NULL;
580 :    
581 :     if (m_xvid_decore(m_param.handle, XVID_DEC_DECODE, &m_frame, 0) != XVID_ERR_OK)
582 :     {
583 :     return S_FALSE;
584 :     }
585 :    
586 :     m_frame.colorspace = tmp;
587 :    
588 :     }
589 :    
590 :     return S_OK;
591 :     }
592 :    
593 :    
594 :     /* get property page list */
595 :    
596 :     STDMETHODIMP CXvidDecoder::GetPages(CAUUID * pPages)
597 :     {
598 :     DEBUG("GetPages");
599 :    
600 :     pPages->cElems = 1;
601 :     pPages->pElems = (GUID *)CoTaskMemAlloc(pPages->cElems * sizeof(GUID));
602 :     if (pPages->pElems == NULL)
603 :     {
604 :     return E_OUTOFMEMORY;
605 :     }
606 :     pPages->pElems[0] = CLSID_CABOUT;
607 :    
608 :     return S_OK;
609 :     }
610 :    
611 :    
612 :     /* cleanup pages */
613 :    
614 :     STDMETHODIMP CXvidDecoder::FreePages(CAUUID * pPages)
615 :     {
616 :     DEBUG("FreePages");
617 :     CoTaskMemFree(pPages->pElems);
618 :     return S_OK;
619 :     }

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