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