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
|
|
82 memset(&m_sVhdr, 0, sizeof m_sVhdr);
|
|
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;
|
|
96 m_sOurType.cbFormat=sizeof m_sVhdr;
|
|
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
|
|
104 memset(&m_sDestType, 0, sizeof m_sDestType);
|
|
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
|
|
122 if(!flip)
|
|
123 {
|
|
124 m_sVhdr2->bmiHeader.biHeight*=-1;
|
|
125 m_obh.biHeight*=-1;
|
|
126 // result=m_pOutputPin->vt->QueryAccept(m_pOutputPin, &m_sDestType);
|
|
127 // if(result)
|
|
128 // throw FATAL("Decoder does not support upside-down frames");
|
|
129 }
|
|
130
|
|
131 dsf=new DS_Filter();
|
|
132
|
|
133 dsf->Create(dllname, guid, &m_sOurType, &m_sDestType);
|
|
134
|
|
135 m_sVhdr2->bmiHeader.biBitCount=16;
|
|
136 m_sVhdr2->bmiHeader.biCompression=fccYUY2;
|
|
137 m_sDestType.subtype=MEDIASUBTYPE_YUY2;
|
|
138 result=dsf->m_pOutputPin->vt->QueryAccept(dsf->m_pOutputPin, &m_sDestType);
|
|
139 // if(!result) caps=(CAPS)(caps | CAP_YUY2);
|
|
140
|
|
141 m_sVhdr2->bmiHeader.biBitCount=24;
|
|
142 m_sVhdr2->bmiHeader.biCompression=0;
|
|
143 m_sDestType.subtype=MEDIASUBTYPE_RGB24;
|
|
144 m_decoder=m_obh;
|
|
145 //qual = 0-1;
|
|
146 }
|
|
147 catch(FatalError& error)
|
|
148 {
|
|
149 delete[] m_sVhdr2;
|
|
150 return 1;
|
|
151 }
|
|
152 return 0;
|
|
153 }
|
|
154
|
|
155 extern "C" void DS_VideoDecoder_Start(){
|
|
156 if(dsf->m_iState!=1) return;
|
|
157 dsf->Start();
|
|
158
|
|
159 ALLOCATOR_PROPERTIES props, props1;
|
|
160 props.cBuffers=1;
|
|
161 props.cbBuffer=1024*1024; //m_sDestType.lSampleSize;//don't know how to do this correctly
|
|
162 props.cbAlign=props.cbPrefix=0;
|
|
163 dsf->m_pAll->vt->SetProperties(dsf->m_pAll, &props, &props1);
|
|
164 dsf->m_pAll->vt->Commit(dsf->m_pAll);
|
|
165
|
|
166 // m_outFrame=new CImage(&m_decoder,(unsigned char *)malloc(m_sDestType.lSampleSize),false);
|
|
167 //m_outFrame=new CImage(&m_decoder, 0, false);
|
|
168 // printf("Datap %x\n",m_outFrame->getaddr());
|
|
169
|
|
170
|
|
171 // dsf->m_pOurOutput->SetFramePointer((char **)m_outFrame->getaddr()); //!FIXME!
|
|
172 dsf->m_pOurOutput->SetFramePointer(m_destptr); //!FIXME!
|
|
173
|
|
174 // filling = realtime;
|
|
175
|
|
176 dsf->m_iState=2;
|
|
177 return;
|
|
178 }
|
|
179
|
|
180 extern "C" void DS_VideoDecoder_Stop(){
|
|
181 if(dsf->m_iState!=2) return;
|
|
182 dsf->Stop();
|
|
183 dsf->m_pOurOutput->SetFramePointer(0);
|
|
184 // free(m_outFrame->data());
|
|
185 //m_outFrame->release();//just in case
|
|
186 //m_outFrame=0;
|
|
187 // FlushCache();
|
|
188 dsf->m_iState=1;
|
|
189 return;
|
|
190 }
|
|
191
|
|
192 extern "C" void DS_VideoDecoder_Restart(){
|
|
193 if(dsf->m_iState!=2) return;
|
|
194
|
|
195 dsf->Stop();
|
|
196 dsf->Start();
|
|
197
|
|
198 ALLOCATOR_PROPERTIES props, props1;
|
|
199 props.cBuffers=1;
|
|
200 props.cbBuffer=m_sDestType.lSampleSize;//don't know how to do this correctly
|
|
201 props.cbAlign=props.cbPrefix=0;
|
|
202 dsf->m_pAll->vt->SetProperties(dsf->m_pAll, &props, &props1);
|
|
203 dsf->m_pAll->vt->Commit(dsf->m_pAll);
|
|
204 }
|
|
205
|
|
206 extern "C" void DS_VideoDecoder_Close(){
|
|
207 if(dsf->m_iState==0) return;
|
|
208 if(dsf->m_iState==2) DS_VideoDecoder_Stop();
|
|
209 delete[] m_sVhdr2;
|
|
210 // delete m_outFrame;
|
|
211 }
|
|
212
|
|
213 extern "C" int DS_VideoDecoder_DecodeFrame(char* src, int size, int is_keyframe, int render){
|
|
214
|
|
215 if(!size)return 0;
|
|
216
|
|
217 m_bh.biSizeImage=size;
|
|
218
|
|
219 IMediaSample* sample=0;
|
173
|
220 //printf("GetBuffer... (m_pAll=%X) ",dsf->m_pAll);fflush(stdout);
|
169
|
221 dsf->m_pAll->vt->GetBuffer(dsf->m_pAll, &sample, 0, 0, 0);
|
173
|
222 //printf("OK!\n");
|
169
|
223 if(!sample)
|
|
224 {
|
|
225 Debug cerr<<"ERROR: null sample"<<endl;
|
|
226 return -1;
|
|
227 }
|
|
228 char* ptr;
|
173
|
229 //printf("GetPtr...");fflush(stdout);
|
169
|
230 sample->vt->GetPointer(sample, (BYTE **)&ptr);
|
173
|
231 //printf("OK!\n");
|
169
|
232 memcpy(ptr, src, size);
|
173
|
233 //printf("memcpy OK!\n");
|
169
|
234 sample->vt->SetActualDataLength(sample, size);
|
173
|
235 //printf("SetActualDataLength OK!\n");
|
169
|
236 sample->vt->SetSyncPoint(sample, is_keyframe);
|
173
|
237 //printf("SetSyncPoint OK!\n");
|
169
|
238 sample->vt->SetPreroll(sample, !render);
|
|
239 // sample->vt->SetMediaType(sample, &m_sOurType);
|
|
240 int result=dsf->m_pImp->vt->Receive(dsf->m_pImp, sample);
|
|
241 if(result)
|
173
|
242 printf("Error putting data into input pin %x\n", result);
|
169
|
243
|
|
244 sample->vt->Release((IUnknown*)sample);
|
|
245
|
|
246 return 0;
|
|
247 }
|
|
248
|
|
249 extern "C" int DS_VideoDecoder_SetDestFmt(int bits, int csp){
|
|
250 if(dsf->m_iState==0) return -1;
|
|
251 // if(!CImage::supported(csp, bits)) return -1;
|
|
252 HRESULT result;
|
|
253 // BitmapInfo temp=m_obh;
|
|
254 if(csp==0)
|
|
255 {
|
|
256 switch(bits)
|
|
257 {
|
|
258 case 15:
|
|
259 m_sDestType.subtype=MEDIASUBTYPE_RGB555;
|
|
260 break;
|
|
261 case 16:
|
|
262 m_sDestType.subtype=MEDIASUBTYPE_RGB565;
|
|
263 break;
|
|
264 case 24:
|
|
265 m_sDestType.subtype=MEDIASUBTYPE_RGB24;
|
|
266 break;
|
|
267 case 32:
|
|
268 m_sDestType.subtype=MEDIASUBTYPE_RGB32;
|
|
269 break;
|
|
270 default:
|
|
271 break;
|
|
272 }
|
|
273 m_obh.setBits(bits);
|
|
274 // .biSizeImage=abs(temp.biWidth*temp.biHeight*((temp.biBitCount+7)/8));
|
|
275 }
|
|
276 else
|
|
277 {
|
|
278 m_obh.setSpace(csp);
|
|
279 switch(csp)
|
|
280 {
|
|
281 case fccYUY2:
|
|
282 m_sDestType.subtype=MEDIASUBTYPE_YUY2;
|
|
283 break;
|
|
284 case fccYV12:
|
|
285 m_sDestType.subtype=MEDIASUBTYPE_YV12;
|
|
286 break;
|
|
287 case fccIYUV:
|
|
288 m_sDestType.subtype=MEDIASUBTYPE_IYUV;
|
|
289 break;
|
|
290 case fccUYVY:
|
|
291 m_sDestType.subtype=MEDIASUBTYPE_UYVY;
|
|
292 break;
|
|
293 case fccYVYU:
|
|
294 m_sDestType.subtype=MEDIASUBTYPE_YVYU;
|
|
295 break;
|
|
296 }
|
|
297 }
|
|
298
|
|
299 m_sDestType.lSampleSize=m_obh.biSizeImage;
|
|
300 memcpy(&(m_sVhdr2->bmiHeader), &m_obh, sizeof(m_obh));
|
|
301 m_sVhdr2->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
|
|
302 if(m_sVhdr2->bmiHeader.biCompression==3)
|
|
303 m_sDestType.cbFormat=sizeof(VIDEOINFOHEADER)+12;
|
|
304 else
|
|
305 m_sDestType.cbFormat=sizeof(VIDEOINFOHEADER);
|
|
306
|
|
307 result=dsf->m_pOutputPin->vt->QueryAccept(dsf->m_pOutputPin, &m_sDestType);
|
|
308
|
|
309 if(result!=0)
|
|
310 {
|
|
311 if(csp)
|
|
312 cerr<<"Warning: unsupported color space"<<endl;
|
|
313 else
|
|
314 cerr<<"Warning: unsupported bit depth"<<endl;
|
|
315
|
|
316 m_sDestType.lSampleSize=m_decoder.biSizeImage;
|
|
317 memcpy(&(m_sVhdr2->bmiHeader), &m_decoder, sizeof(m_decoder));
|
|
318 m_sVhdr2->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
|
|
319 if(m_sVhdr2->bmiHeader.biCompression==3)
|
|
320 m_sDestType.cbFormat=sizeof(VIDEOINFOHEADER)+12;
|
|
321 else
|
|
322 m_sDestType.cbFormat=sizeof(VIDEOINFOHEADER);
|
|
323 return 1;
|
|
324 }
|
|
325
|
|
326 m_decoder=m_obh;
|
|
327 // m_obh=temp;
|
|
328 // if(csp)
|
|
329 // m_obh.biBitCount=BitmapInfo::BitCount(csp);
|
|
330 m_bh.biBitCount=bits;
|
|
331 if(dsf->m_iState>0)
|
|
332 {
|
|
333 int old_state=dsf->m_iState;
|
|
334 if(dsf->m_iState==2) DS_VideoDecoder_Stop();
|
|
335 dsf->m_pInputPin->vt->Disconnect(dsf->m_pInputPin);
|
|
336 dsf->m_pOutputPin->vt->Disconnect(dsf->m_pOutputPin);
|
|
337 dsf->m_pOurOutput->SetNewFormat(m_sDestType);
|
|
338 result=dsf->m_pInputPin->vt->ReceiveConnection(dsf->m_pInputPin, dsf->m_pOurInput, &m_sOurType);
|
|
339 if(result)
|
|
340 {
|
|
341 cerr<<"Error reconnecting input pin "<<hex<<result<<dec<<endl;
|
|
342 return -1;
|
|
343 }
|
|
344 result=dsf->m_pOutputPin->vt->ReceiveConnection(dsf->m_pOutputPin,
|
|
345 dsf->m_pOurOutput, &m_sDestType);
|
|
346 if(result)
|
|
347 {
|
|
348 cerr<<"Error reconnecting output pin "<<hex<<result<<dec<<endl;
|
|
349 return -1;
|
|
350 }
|
|
351 if(old_state==2) DS_VideoDecoder_Start();
|
|
352 }
|
|
353 return 0;
|
|
354 }
|
|
355
|
|
356
|
|
357 extern "C" int DS_SetValue_DivX(char* name, int value){
|
|
358 int temp;
|
|
359 if(dsf->m_iState!=2) return VFW_E_NOT_RUNNING;
|
|
360 // brightness 87
|
|
361 // contrast 74
|
|
362 // hue 23
|
|
363 // saturation 20
|
|
364 // post process mode 0
|
|
365 // get1 0x01
|
|
366 // get2 10
|
|
367 // get3=set2 86
|
|
368 // get4=set3 73
|
|
369 // get5=set4 19
|
|
370 // get6=set5 23
|
|
371 printf("DivX setting: %s = %d\n",name,value);
|
|
372
|
|
373 IHidden* hidden=(IHidden*)((int)dsf->m_pFilter+0xb8);
|
|
374 if(strcmp(name, "Brightness")==0)
|
|
375 return hidden->vt->SetSmth2(hidden, value, 0);
|
|
376 if(strcmp(name, "Contrast")==0)
|
|
377 return hidden->vt->SetSmth3(hidden, value, 0);
|
|
378 if(strcmp(name, "Hue")==0)
|
|
379 return hidden->vt->SetSmth5(hidden, value, 0);
|
|
380 if(strcmp(name, "Saturation")==0)
|
|
381 return hidden->vt->SetSmth4(hidden, value, 0);
|
|
382 if(strcmp(name, "Quality")==0)
|
|
383 return hidden->vt->SetSmth(hidden, value, 0);
|
|
384
|
|
385 printf("Invalid setting!\n");
|
|
386 return -200;
|
|
387 }
|
|
388
|
173
|
389 extern "C" int DS_SetAttr_DivX(char* attribute, int value){
|
|
390 int result, status, newkey, count;
|
|
391 if(strcmp(attribute, "Quality")==0){
|
|
392 char* keyname="SOFTWARE\\Microsoft\\Scrunch";
|
|
393 result=RegCreateKeyExA(HKEY_CURRENT_USER, keyname, 0, 0, 0, 0, 0, &newkey, &status);
|
|
394 if(result!=0)
|
|
395 {
|
|
396 printf("VideoDecoder::SetExtAttr: registry failure\n");
|
|
397 return -1;
|
|
398 }
|
|
399 result=RegSetValueExA(newkey, "Current Post Process Mode", 0, REG_DWORD, &value, 4);
|
|
400 if(result!=0)
|
|
401 {
|
|
402 printf("VideoDecoder::SetExtAttr: error writing value\n");
|
|
403 return -1;
|
|
404 }
|
|
405 value=-1;
|
|
406 result=RegSetValueExA(newkey, "Force Post Process Mode", 0, REG_DWORD, &value, 4);
|
|
407 if(result!=0)
|
|
408 {
|
|
409 printf("VideoDecoder::SetExtAttr: error writing value\n");
|
|
410 return -1;
|
|
411 }
|
|
412 RegCloseKey(newkey);
|
|
413 return 0;
|
|
414 }
|
169
|
415
|
173
|
416 if(
|
|
417 (strcmp(attribute, "Saturation")==0) ||
|
|
418 (strcmp(attribute, "Hue")==0) ||
|
|
419 (strcmp(attribute, "Contrast")==0) ||
|
|
420 (strcmp(attribute, "Brightness")==0)
|
|
421 )
|
|
422 {
|
|
423 char* keyname="SOFTWARE\\Microsoft\\Scrunch\\Video";
|
|
424 result=RegCreateKeyExA(HKEY_CURRENT_USER, keyname, 0, 0, 0, 0, 0, &newkey, &status);
|
|
425 if(result!=0)
|
|
426 {
|
|
427 printf("VideoDecoder::SetExtAttr: registry failure\n");
|
|
428 return -1;
|
|
429 }
|
|
430 result=RegSetValueExA(newkey, attribute, 0, REG_DWORD, &value, 4);
|
|
431 if(result!=0)
|
|
432 {
|
|
433 printf("VideoDecoder::SetExtAttr: error writing value\n");
|
|
434 return -1;
|
|
435 }
|
|
436 RegCloseKey(newkey);
|
|
437 return 0;
|
|
438 }
|
|
439
|
|
440 printf("Unknown attribute!\n");
|
|
441 return -200;
|
|
442 }
|
|
443
|