169
|
1 /********************************************************
|
|
2
|
|
3 DirectShow Video decoder implementation
|
|
4 Copyright 2000 Eugene Kuznetsov (divx@euro.ru)
|
|
5 Converted C++ --> C :) by A'rpi/ESP-team
|
|
6
|
|
7 *********************************************************/
|
|
8
|
|
9 //#include <config.h>
|
|
10
|
|
11 //#include "DS_VideoDecoder.h"
|
|
12 #include <string.h>
|
|
13 #include <stdlib.h>
|
|
14 #include <except.h>
|
|
15 #define __MODULE__ "DirectShow_VideoDecoder"
|
|
16
|
|
17 #include <errno.h>
|
|
18 #ifdef HAVE_MALLOC_H
|
|
19 #include <malloc.h>
|
|
20 #endif
|
|
21 //#include <loader.h>
|
|
22 //#include <wine/winbase.h>
|
|
23 #include <stdio.h>
|
|
24 #include <unistd.h>
|
|
25 #include <fcntl.h>
|
|
26 #include <strstream>
|
|
27 #include <dlfcn.h>
|
|
28 #include <sys/types.h>
|
|
29 #include <sys/mman.h>
|
|
30
|
173
|
31 #include <registry.h>
|
|
32 #include <wine/winreg.h>
|
|
33
|
169
|
34 #include "guids.h"
|
|
35 #include "interfaces.h"
|
|
36 #include "DS_Filter.h"
|
|
37
|
|
38 #include "BitmapInfo.h"
|
|
39
|
|
40 #include <string>
|
|
41 #include <default.h>
|
|
42
|
|
43 #include "DS_VideoDec.h"
|
|
44
|
173
|
45
|
169
|
46 using namespace std;
|
|
47 extern "C" char* def_path;
|
|
48
|
|
49 static char** m_destptr=0;
|
|
50
|
|
51 static DS_Filter* dsf=0;
|
|
52
|
|
53 static AM_MEDIA_TYPE m_sOurType, m_sDestType;
|
|
54 static VIDEOINFOHEADER m_sVhdr;
|
|
55 static VIDEOINFOHEADER *m_sVhdr2;
|
|
56 static void* m_pCust;
|
|
57
|
|
58 static BITMAPINFOHEADER m_bh;//format of input data
|
|
59 static BitmapInfo m_decoder;//format of decoder output
|
|
60 static BitmapInfo m_obh; //format of returned frames
|
|
61 // CImage* m_outFrame;
|
|
62
|
|
63 // int m_iState=0;
|
|
64
|
|
65 extern "C" int DS_VideoDecoder_Open(char* dllname, GUID* guid, BITMAPINFOHEADER* format, int flip,char** d_ptr)
|
|
66 // :IVideoDecoder(info), m_sVhdr2(0)
|
|
67 {
|
|
68
|
|
69 m_destptr=d_ptr;
|
|
70
|
|
71 //m_outFrame=0;
|
|
72 //decpos = 0;
|
|
73 //playpos = 0;
|
|
74 //realtime = 0;
|
|
75
|
|
76 try
|
|
77 {
|
|
78 m_bh=*format;
|
|
79 memset(&m_obh, 0, sizeof(m_obh));
|
|
80 m_obh.biSize=sizeof(m_obh);
|
|
81
|
713
|
82 memset(&m_sVhdr, 0, sizeof(m_sVhdr));
|
169
|
83 m_sVhdr.bmiHeader=m_bh;
|
|
84 m_sVhdr.rcSource.left=m_sVhdr.rcSource.top=0;
|
|
85 m_sVhdr.rcSource.right=m_sVhdr.bmiHeader.biWidth;
|
|
86 m_sVhdr.rcSource.bottom=m_sVhdr.bmiHeader.biHeight;
|
|
87 m_sVhdr.rcTarget=m_sVhdr.rcSource;
|
|
88 m_sOurType.majortype=MEDIATYPE_Video;
|
|
89
|
|
90 m_sOurType.subtype=MEDIATYPE_Video;
|
|
91 m_sOurType.subtype.f1=m_sVhdr.bmiHeader.biCompression;
|
|
92 m_sOurType.formattype=FORMAT_VideoInfo;
|
|
93 m_sOurType.bFixedSizeSamples=false;
|
|
94 m_sOurType.bTemporalCompression=true;
|
|
95 m_sOurType.pUnk=0;
|
713
|
96 m_sOurType.cbFormat=sizeof(m_sVhdr);
|
169
|
97 m_sOurType.pbFormat=(char*)&m_sVhdr;
|
|
98
|
|
99 m_sVhdr2=(VIDEOINFOHEADER*)(new char[sizeof(VIDEOINFOHEADER)+12]);
|
|
100 *m_sVhdr2=m_sVhdr;
|
|
101 m_sVhdr2->bmiHeader.biCompression=0;
|
|
102 m_sVhdr2->bmiHeader.biBitCount=24;
|
|
103
|
713
|
104 memset(&m_sDestType, 0, sizeof(m_sDestType));
|
169
|
105 m_sDestType.majortype=MEDIATYPE_Video;
|
|
106 m_sDestType.subtype=MEDIASUBTYPE_RGB24;
|
|
107 m_sDestType.formattype=FORMAT_VideoInfo;
|
|
108 m_sDestType.bFixedSizeSamples=true;
|
|
109 m_sDestType.bTemporalCompression=false;
|
|
110 m_sDestType.lSampleSize=abs(m_sVhdr2->bmiHeader.biWidth*m_sVhdr2->bmiHeader.biHeight*
|
|
111 ((m_sVhdr2->bmiHeader.biBitCount+7)/8));
|
|
112 m_sVhdr2->bmiHeader.biSizeImage=m_sDestType.lSampleSize;
|
|
113 m_sDestType.pUnk=0;
|
|
114 m_sDestType.cbFormat=sizeof(VIDEOINFOHEADER);
|
|
115 m_sDestType.pbFormat=(char*)m_sVhdr2;
|
|
116
|
|
117 m_obh=m_bh;
|
|
118 m_obh.setBits(24);
|
|
119
|
|
120 HRESULT result;
|
|
121
|
713
|
122 dsf=new DS_Filter();
|
|
123 dsf->Create(dllname, guid, &m_sOurType, &m_sDestType);
|
|
124
|
169
|
125 if(!flip)
|
|
126 {
|
|
127 m_sVhdr2->bmiHeader.biHeight*=-1;
|
|
128 m_obh.biHeight*=-1;
|
713
|
129 result=dsf->m_pOutputPin->vt->QueryAccept(dsf->m_pOutputPin, &m_sDestType);
|
|
130 if(result){
|
|
131 printf("DShow: Decoder does not support upside-down frames");
|
|
132 m_obh.biHeight*=-1;
|
|
133 }
|
169
|
134 }
|
|
135
|
468
|
136 #if 0
|
169
|
137 m_sVhdr2->bmiHeader.biBitCount=16;
|
|
138 m_sVhdr2->bmiHeader.biCompression=fccYUY2;
|
|
139 m_sDestType.subtype=MEDIASUBTYPE_YUY2;
|
|
140 result=dsf->m_pOutputPin->vt->QueryAccept(dsf->m_pOutputPin, &m_sDestType);
|
|
141 // if(!result) caps=(CAPS)(caps | CAP_YUY2);
|
468
|
142 #endif
|
169
|
143
|
|
144 m_sVhdr2->bmiHeader.biBitCount=24;
|
|
145 m_sVhdr2->bmiHeader.biCompression=0;
|
|
146 m_sDestType.subtype=MEDIASUBTYPE_RGB24;
|
|
147 m_decoder=m_obh;
|
|
148 //qual = 0-1;
|
|
149 }
|
|
150 catch(FatalError& error)
|
|
151 {
|
|
152 delete[] m_sVhdr2;
|
|
153 return 1;
|
|
154 }
|
|
155 return 0;
|
|
156 }
|
|
157
|
|
158 extern "C" void DS_VideoDecoder_Start(){
|
|
159 if(dsf->m_iState!=1) return;
|
|
160 dsf->Start();
|
|
161
|
|
162 ALLOCATOR_PROPERTIES props, props1;
|
|
163 props.cBuffers=1;
|
|
164 props.cbBuffer=1024*1024; //m_sDestType.lSampleSize;//don't know how to do this correctly
|
|
165 props.cbAlign=props.cbPrefix=0;
|
|
166 dsf->m_pAll->vt->SetProperties(dsf->m_pAll, &props, &props1);
|
|
167 dsf->m_pAll->vt->Commit(dsf->m_pAll);
|
|
168
|
|
169 // m_outFrame=new CImage(&m_decoder,(unsigned char *)malloc(m_sDestType.lSampleSize),false);
|
|
170 //m_outFrame=new CImage(&m_decoder, 0, false);
|
|
171 // printf("Datap %x\n",m_outFrame->getaddr());
|
|
172
|
|
173
|
|
174 // dsf->m_pOurOutput->SetFramePointer((char **)m_outFrame->getaddr()); //!FIXME!
|
|
175 dsf->m_pOurOutput->SetFramePointer(m_destptr); //!FIXME!
|
|
176
|
|
177 // filling = realtime;
|
|
178
|
|
179 dsf->m_iState=2;
|
|
180 return;
|
|
181 }
|
|
182
|
|
183 extern "C" void DS_VideoDecoder_Stop(){
|
|
184 if(dsf->m_iState!=2) return;
|
|
185 dsf->Stop();
|
713
|
186 // dsf->m_pOurOutput->SetFramePointer(0);
|
169
|
187 // free(m_outFrame->data());
|
|
188 //m_outFrame->release();//just in case
|
|
189 //m_outFrame=0;
|
|
190 // FlushCache();
|
|
191 dsf->m_iState=1;
|
|
192 return;
|
|
193 }
|
|
194
|
|
195 extern "C" void DS_VideoDecoder_Restart(){
|
|
196 if(dsf->m_iState!=2) return;
|
|
197
|
|
198 dsf->Stop();
|
|
199 dsf->Start();
|
|
200
|
|
201 ALLOCATOR_PROPERTIES props, props1;
|
|
202 props.cBuffers=1;
|
|
203 props.cbBuffer=m_sDestType.lSampleSize;//don't know how to do this correctly
|
|
204 props.cbAlign=props.cbPrefix=0;
|
|
205 dsf->m_pAll->vt->SetProperties(dsf->m_pAll, &props, &props1);
|
|
206 dsf->m_pAll->vt->Commit(dsf->m_pAll);
|
|
207 }
|
|
208
|
|
209 extern "C" void DS_VideoDecoder_Close(){
|
|
210 if(dsf->m_iState==0) return;
|
|
211 if(dsf->m_iState==2) DS_VideoDecoder_Stop();
|
|
212 delete[] m_sVhdr2;
|
|
213 // delete m_outFrame;
|
|
214 }
|
|
215
|
|
216 extern "C" int DS_VideoDecoder_DecodeFrame(char* src, int size, int is_keyframe, int render){
|
|
217
|
|
218 if(!size)return 0;
|
|
219
|
|
220 m_bh.biSizeImage=size;
|
|
221
|
|
222 IMediaSample* sample=0;
|
173
|
223 //printf("GetBuffer... (m_pAll=%X) ",dsf->m_pAll);fflush(stdout);
|
169
|
224 dsf->m_pAll->vt->GetBuffer(dsf->m_pAll, &sample, 0, 0, 0);
|
173
|
225 //printf("OK!\n");
|
169
|
226 if(!sample)
|
|
227 {
|
|
228 Debug cerr<<"ERROR: null sample"<<endl;
|
|
229 return -1;
|
|
230 }
|
|
231 char* ptr;
|
173
|
232 //printf("GetPtr...");fflush(stdout);
|
169
|
233 sample->vt->GetPointer(sample, (BYTE **)&ptr);
|
173
|
234 //printf("OK!\n");
|
169
|
235 memcpy(ptr, src, size);
|
173
|
236 //printf("memcpy OK!\n");
|
169
|
237 sample->vt->SetActualDataLength(sample, size);
|
173
|
238 //printf("SetActualDataLength OK!\n");
|
169
|
239 sample->vt->SetSyncPoint(sample, is_keyframe);
|
173
|
240 //printf("SetSyncPoint OK!\n");
|
169
|
241 sample->vt->SetPreroll(sample, !render);
|
|
242 // sample->vt->SetMediaType(sample, &m_sOurType);
|
|
243 int result=dsf->m_pImp->vt->Receive(dsf->m_pImp, sample);
|
|
244 if(result)
|
173
|
245 printf("Error putting data into input pin %x\n", result);
|
169
|
246
|
|
247 sample->vt->Release((IUnknown*)sample);
|
|
248
|
|
249 return 0;
|
|
250 }
|
|
251
|
|
252 extern "C" int DS_VideoDecoder_SetDestFmt(int bits, int csp){
|
|
253 if(dsf->m_iState==0) return -1;
|
|
254 // if(!CImage::supported(csp, bits)) return -1;
|
|
255 HRESULT result;
|
|
256 // BitmapInfo temp=m_obh;
|
|
257 if(csp==0)
|
|
258 {
|
|
259 switch(bits)
|
|
260 {
|
|
261 case 15:
|
|
262 m_sDestType.subtype=MEDIASUBTYPE_RGB555;
|
|
263 break;
|
|
264 case 16:
|
|
265 m_sDestType.subtype=MEDIASUBTYPE_RGB565;
|
|
266 break;
|
|
267 case 24:
|
|
268 m_sDestType.subtype=MEDIASUBTYPE_RGB24;
|
|
269 break;
|
|
270 case 32:
|
|
271 m_sDestType.subtype=MEDIASUBTYPE_RGB32;
|
|
272 break;
|
|
273 default:
|
|
274 break;
|
|
275 }
|
|
276 m_obh.setBits(bits);
|
|
277 // .biSizeImage=abs(temp.biWidth*temp.biHeight*((temp.biBitCount+7)/8));
|
|
278 }
|
|
279 else
|
|
280 {
|
468
|
281 m_obh.setSpace(csp,bits);
|
169
|
282 switch(csp)
|
|
283 {
|
|
284 case fccYUY2:
|
|
285 m_sDestType.subtype=MEDIASUBTYPE_YUY2;
|
468
|
286 printf("DShow: using YUY2 colorspace\n");
|
169
|
287 break;
|
|
288 case fccYV12:
|
|
289 m_sDestType.subtype=MEDIASUBTYPE_YV12;
|
468
|
290 printf("DShow: using YV12 colorspace\n");
|
169
|
291 break;
|
|
292 case fccIYUV:
|
|
293 m_sDestType.subtype=MEDIASUBTYPE_IYUV;
|
468
|
294 printf("DShow: using IYUV colorspace\n");
|
169
|
295 break;
|
|
296 case fccUYVY:
|
|
297 m_sDestType.subtype=MEDIASUBTYPE_UYVY;
|
468
|
298 printf("DShow: using UYVY colorspace\n");
|
169
|
299 break;
|
|
300 case fccYVYU:
|
|
301 m_sDestType.subtype=MEDIASUBTYPE_YVYU;
|
468
|
302 printf("DShow: using YVYU colorspace\n");
|
169
|
303 break;
|
|
304 }
|
|
305 }
|
|
306
|
|
307 m_sDestType.lSampleSize=m_obh.biSizeImage;
|
|
308 memcpy(&(m_sVhdr2->bmiHeader), &m_obh, sizeof(m_obh));
|
|
309 m_sVhdr2->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
|
|
310 if(m_sVhdr2->bmiHeader.biCompression==3)
|
|
311 m_sDestType.cbFormat=sizeof(VIDEOINFOHEADER)+12;
|
|
312 else
|
|
313 m_sDestType.cbFormat=sizeof(VIDEOINFOHEADER);
|
|
314
|
|
315 result=dsf->m_pOutputPin->vt->QueryAccept(dsf->m_pOutputPin, &m_sDestType);
|
|
316
|
|
317 if(result!=0)
|
|
318 {
|
|
319 if(csp)
|
|
320 cerr<<"Warning: unsupported color space"<<endl;
|
|
321 else
|
|
322 cerr<<"Warning: unsupported bit depth"<<endl;
|
|
323
|
|
324 m_sDestType.lSampleSize=m_decoder.biSizeImage;
|
|
325 memcpy(&(m_sVhdr2->bmiHeader), &m_decoder, sizeof(m_decoder));
|
|
326 m_sVhdr2->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
|
|
327 if(m_sVhdr2->bmiHeader.biCompression==3)
|
|
328 m_sDestType.cbFormat=sizeof(VIDEOINFOHEADER)+12;
|
|
329 else
|
|
330 m_sDestType.cbFormat=sizeof(VIDEOINFOHEADER);
|
|
331 return 1;
|
|
332 }
|
|
333
|
|
334 m_decoder=m_obh;
|
|
335 // m_obh=temp;
|
|
336 // if(csp)
|
|
337 // m_obh.biBitCount=BitmapInfo::BitCount(csp);
|
|
338 m_bh.biBitCount=bits;
|
|
339 if(dsf->m_iState>0)
|
|
340 {
|
|
341 int old_state=dsf->m_iState;
|
|
342 if(dsf->m_iState==2) DS_VideoDecoder_Stop();
|
|
343 dsf->m_pInputPin->vt->Disconnect(dsf->m_pInputPin);
|
|
344 dsf->m_pOutputPin->vt->Disconnect(dsf->m_pOutputPin);
|
|
345 dsf->m_pOurOutput->SetNewFormat(m_sDestType);
|
|
346 result=dsf->m_pInputPin->vt->ReceiveConnection(dsf->m_pInputPin, dsf->m_pOurInput, &m_sOurType);
|
|
347 if(result)
|
|
348 {
|
|
349 cerr<<"Error reconnecting input pin "<<hex<<result<<dec<<endl;
|
|
350 return -1;
|
|
351 }
|
|
352 result=dsf->m_pOutputPin->vt->ReceiveConnection(dsf->m_pOutputPin,
|
|
353 dsf->m_pOurOutput, &m_sDestType);
|
|
354 if(result)
|
|
355 {
|
|
356 cerr<<"Error reconnecting output pin "<<hex<<result<<dec<<endl;
|
|
357 return -1;
|
|
358 }
|
|
359 if(old_state==2) DS_VideoDecoder_Start();
|
|
360 }
|
|
361 return 0;
|
|
362 }
|
|
363
|
|
364
|
|
365 extern "C" int DS_SetValue_DivX(char* name, int value){
|
|
366 int temp;
|
|
367 if(dsf->m_iState!=2) return VFW_E_NOT_RUNNING;
|
|
368 // brightness 87
|
|
369 // contrast 74
|
|
370 // hue 23
|
|
371 // saturation 20
|
|
372 // post process mode 0
|
|
373 // get1 0x01
|
|
374 // get2 10
|
|
375 // get3=set2 86
|
|
376 // get4=set3 73
|
|
377 // get5=set4 19
|
|
378 // get6=set5 23
|
|
379 printf("DivX setting: %s = %d\n",name,value);
|
|
380
|
|
381 IHidden* hidden=(IHidden*)((int)dsf->m_pFilter+0xb8);
|
|
382 if(strcmp(name, "Brightness")==0)
|
|
383 return hidden->vt->SetSmth2(hidden, value, 0);
|
|
384 if(strcmp(name, "Contrast")==0)
|
|
385 return hidden->vt->SetSmth3(hidden, value, 0);
|
|
386 if(strcmp(name, "Hue")==0)
|
|
387 return hidden->vt->SetSmth5(hidden, value, 0);
|
|
388 if(strcmp(name, "Saturation")==0)
|
|
389 return hidden->vt->SetSmth4(hidden, value, 0);
|
|
390 if(strcmp(name, "Quality")==0)
|
|
391 return hidden->vt->SetSmth(hidden, value, 0);
|
|
392
|
|
393 printf("Invalid setting!\n");
|
|
394 return -200;
|
|
395 }
|
|
396
|
173
|
397 extern "C" int DS_SetAttr_DivX(char* attribute, int value){
|
|
398 int result, status, newkey, count;
|
|
399 if(strcmp(attribute, "Quality")==0){
|
|
400 char* keyname="SOFTWARE\\Microsoft\\Scrunch";
|
|
401 result=RegCreateKeyExA(HKEY_CURRENT_USER, keyname, 0, 0, 0, 0, 0, &newkey, &status);
|
|
402 if(result!=0)
|
|
403 {
|
|
404 printf("VideoDecoder::SetExtAttr: registry failure\n");
|
|
405 return -1;
|
|
406 }
|
|
407 result=RegSetValueExA(newkey, "Current Post Process Mode", 0, REG_DWORD, &value, 4);
|
|
408 if(result!=0)
|
|
409 {
|
|
410 printf("VideoDecoder::SetExtAttr: error writing value\n");
|
|
411 return -1;
|
|
412 }
|
|
413 value=-1;
|
|
414 result=RegSetValueExA(newkey, "Force Post Process Mode", 0, REG_DWORD, &value, 4);
|
|
415 if(result!=0)
|
|
416 {
|
|
417 printf("VideoDecoder::SetExtAttr: error writing value\n");
|
|
418 return -1;
|
|
419 }
|
|
420 RegCloseKey(newkey);
|
|
421 return 0;
|
|
422 }
|
169
|
423
|
173
|
424 if(
|
|
425 (strcmp(attribute, "Saturation")==0) ||
|
|
426 (strcmp(attribute, "Hue")==0) ||
|
|
427 (strcmp(attribute, "Contrast")==0) ||
|
|
428 (strcmp(attribute, "Brightness")==0)
|
|
429 )
|
|
430 {
|
|
431 char* keyname="SOFTWARE\\Microsoft\\Scrunch\\Video";
|
|
432 result=RegCreateKeyExA(HKEY_CURRENT_USER, keyname, 0, 0, 0, 0, 0, &newkey, &status);
|
|
433 if(result!=0)
|
|
434 {
|
|
435 printf("VideoDecoder::SetExtAttr: registry failure\n");
|
|
436 return -1;
|
|
437 }
|
|
438 result=RegSetValueExA(newkey, attribute, 0, REG_DWORD, &value, 4);
|
|
439 if(result!=0)
|
|
440 {
|
|
441 printf("VideoDecoder::SetExtAttr: error writing value\n");
|
|
442 return -1;
|
|
443 }
|
|
444 RegCloseKey(newkey);
|
|
445 return 0;
|
|
446 }
|
|
447
|
|
448 printf("Unknown attribute!\n");
|
|
449 return -200;
|
|
450 }
|
|
451
|