Debian Codegraph @

plugins/libwin32/videocodec/VideoDecoder.cpp

raw
1 /********************************************************
2
3 Video decoder implementation
4 Copyright 2000 Eugene Kuznetsov ([email protected])
5
6 *********************************************************/
7
8 #include "wine/windef.h"
9
10 #include "avm_fourcc.h"
11 #include "avm_cpuinfo.h"
12 #include "avm_output.h"
13
14 #include "VideoDecoder.h"
15 #include "VideoEncoder.h"
16 #include "dshow/DS_Filter.h"
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <sys/mman.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdlib.h> // labs
24 #include <stdio.h>
25
26 #define ICDECOMPRESS_HURRYUP 0x80000000 /* don't draw just buffer (hurry up!) */
27 #define ICDECOMPRESS_UPDATE 0x40000000 /* don't draw just update screen */
28 #define ICDECOMPRESS_PREROL 0x20000000 /* this frame is before real start */
29 #define ICDECOMPRESS_NOTKEYFRAME 0x08000000 /* this frame is not a key frame */
30
31 AVM_BEGIN_NAMESPACE;
32
33 VideoDecoder::VideoDecoder(const CodecInfo& info, const BITMAPINFOHEADER& bh, int flip)
34 :IVideoDecoder(info, bh), m_pModule(0), m_HIC(0), m_iStatus(0),
35 m_pLastImage(0), m_bitrick(0), m_divx_trick(false),
36 m_bUseEx((info.kind == CodecInfo::Win32Ex)),
37 m_bFlip(flip)
38 {
39 }
40
41 int VideoDecoder::init()
42 {
43 /* On2 Truemotion VP3.x support */
44 ICINFO ici;
45 //
46 // MPEG-4 format has changed during development. Package
47 // binaries.zip includes mpg4c32.dll - old MPEG-4 codec
48 // with unlocked compression - and divxc32.dll,
49 // which differs from current MPEG-4 only by FOURCC's.
50 // The problem is to identify which is needed to us,
51 // because 'old' codec doesn't work with 'new' clips and
52 // vice versa. Unfortunately, they may simulate correct
53 // work until the moment of actual decompression. Then,
54 // you'll receive garbled picture and lots of warnings about
55 // hr=-100.
56 //
57
58 // I've got old video with fccHandler mp43 and biCompression MPG4.
59 // Dirk Vornheider sent me old video with both values MP42.
60 //AVM_WRITE("Win32 video decoder", "BI %d %.4s\n", m_pFormat->biBitCount, (char*)&m_pFormat->biBitCount);
61 m_pModule = control.Create(m_Info);
62 if (!m_pModule)
63 {
64 AVM_WRITE("Win32 video decoder", "Can't create module\n");
65 return -1;
66 }
67
68 m_HIC = m_pModule->CreateHandle(m_pFormat->biCompression, Module::Decompress);
69 if (!m_HIC)
70 {
71 AVM_WRITE("Win32 video decoder", "ICOpen failed\n");
72 return -1;
73 }
74
75 int hr = ICDecompressGetFormat(m_HIC, m_pFormat, 0);
76 if (hr <= 0)
77 {
78 AVM_WRITE("Win32 video decoder", "ICDecompressGetFormatSize failed\n");
79 return -1;
80 }
81 if (hr < (int)sizeof(BitmapInfo))
82 hr = sizeof(BitmapInfo);
83
84 m_Dest.SetBits(24);
85 m_bitrick = (BITMAPINFOHEADER*) malloc(hr);
86 // ok I guess Xvid people brinks here another interesting
87 // aspect - we are asking for some format but we already
88 // have to pass one here!!!
89 memset(m_bitrick, 0, hr);
90 memcpy(m_bitrick, &m_Dest, sizeof(BITMAPINFOHEADER));
91 m_bitrick->biHeight = labs(m_Dest.biHeight); // xvid trick
92 hr = ICDecompressGetFormat(m_HIC, m_pFormat, m_bitrick);
93
94 if (hr != 0)
95 {
96 AVM_WRITE("Win32 video decoder", "Declined format dump:\n");
97 BitmapInfo(*m_pFormat).Print();
98 m_Dest.Print();
99 AVM_WRITE("Win32 video decoder", " ICDecompressGetFormat failed Error %d\n", hr);
100 return -1;
101 }
102
103 switch (m_Info.fourcc)
104 {
105 case fccCRAM:
106 //if (m_pFormat->biCompression != fccMSVC)
107 // break;
108 case fccCVID:
109 case fccIV31:
110 case fccIV32:
111 case fccTSCC:
112 m_bLastNeeded = true;
113 break;
114 default:
115 m_bLastNeeded = false;
116 }
117
118 hr = ICGetInfo(m_HIC, &ici);
119 // this might be good to use but many codecs returns false info here
120 //if (hr && !(ici.dwFlags & VIDCF_FASTTEMPORALD)) m_bLastNeeded = true;
121
122 switch (m_Info.fourcc)
123 {
124 case fccIV31:
125 case fccIV32:
126 m_Dest.SetBits(16); // doesn't support 24bit RGB
127 break;
128 case fccTSCC:
129 m_Dest.SetBits(15); // doesn't support 24bit RGB
130 break;
131 case fccZLIB:
132 case fccMSZH:
133 case fccASV1:
134 case fccASV2:
135 case mmioFOURCC('A', 'V', 'R', 'n'):
136 m_bFlip = 1; // these codecs are using always top-down RGB
137 /* fall through */
138 default:
139 m_Dest.SetBits(24);
140 }
141
142 if (m_bFlip)
143 m_Dest.biHeight = labs(m_Dest.biHeight);
144 else
145 {
146 setDecoder(m_Dest);
147 hr = (m_bUseEx) ?
148 ICUniversalEx(m_HIC, ICM_DECOMPRESSEX_QUERY, 0,
149 m_pFormat, 0, m_bitrick, 0) :
150 ICDecompressQuery(m_HIC, m_pFormat, m_bitrick);
151 if (hr)
152 {
153 m_Dest.biHeight = labs(m_Dest.biHeight);
154 AVM_WRITE("Win32 video decoder", "Decoder does not support upside-down RGB frames\n");
155 }
156 }
157 setDecoder(m_Dest);
158
159 m_Caps = CAP_NONE;
160 switch (m_Info.fourcc)
161 {
162 case fccDIV3:
163 case fccDIV4:
164 case fccDIV5:
165 case fccDIV6:
166 case fccMP42:
167 // only needed for some older codecs
168 m_Caps = (CAPS) (CAP_YUY2 | CAP_UYVY | CAP_YVYU); //planar formats broken, others untested
169 m_divx_trick = true;
170 break;
171 case fccHFYU:
172 if (m_pFormat->biSize == 232)
173 {
174 m_Caps = CAP_YUY2;
175 //m_Dest.SetSpace(fccYUY2);
176 break;
177 }
178 // fall through for RGB mode
179 default:
180 {
181 static const struct ct {
182 fourcc_t fcc;
183 CAPS cap;
184 } check[] = {
185 { fccIYUV, CAP_IYUV },
186 { fccUYVY, CAP_UYVY },
187 { fccYV12, CAP_YV12 },
188 { fccYVYU, CAP_YVYU },
189 { fccYUY2, CAP_YUY2 },
190 { fccI420, CAP_I420 },
191 { fccYVU9, CAP_YVU9 },
192 // anyone heard about MTX6 ??
193 { 0, CAP_NONE }
194 };
195
196 BitmapInfo mbi(m_Dest);
197 for (const ct* c = check; c->fcc; c++)
198 {
199 mbi.SetSpace(c->fcc);
200 setDecoder(mbi);
201 hr = (m_bUseEx) ?
202 ICUniversalEx(m_HIC, ICM_DECOMPRESSEX_QUERY, 0,
203 m_pFormat, 0, m_bitrick, 0) :
204 ICDecompressQuery(m_HIC, m_pFormat, m_bitrick);
205 //printf ("TEST %.4s %d\n", (char*)&(c->fcc), h);
206 if (hr == 0)
207 m_Caps = (CAPS)(m_Caps | c->cap);
208 }
209 // exceptions
210 if (m_Info.fourcc == fccMJPG
211 && strcmp(m_Info.dll.c_str(), "m3jpeg32.dll") == 0)
212 {
213 // YV12 works but incorrectly - it's I420
214 m_Caps = (CAPS) (m_Caps & ~CAP_YV12);
215 }
216 setDecoder(m_Dest);
217 }
218 }
219 if (m_Caps)
220 AVM_WRITE("Win32 video decoder", "Decoder is capable of YUV output ( flags 0x%x)\n", (int)m_Caps);
221 return 0;
222 }
223
224 VideoDecoder::~VideoDecoder()
225 {
226 Stop();
227 if (m_bitrick)
228 free(m_bitrick);
229 if (m_pModule)
230 m_pModule->CloseHandle(m_HIC);
231 }
232
233 int VideoDecoder::Start()
234 {
235 if (m_iStatus == 1)
236 return 0;
237 #if 0
238 AVM_WRITE("Win32 video decoder", "Starting decompression, format: \n");
239 BitmapInfo(m_Dest).Print();
240 //AVM_WRITE("Win32 video decoder", "Dest fmt:\n");
241 //BitmapInfo(m_decoder).Print();
242 #endif
243
244 int tmpcomp = m_bitrick->biCompression;
245 if (m_divx_trick)
246 m_bitrick->biCompression = 0;
247 /* On2 Truemotion VP3.x support */
248 int hr = (m_bUseEx) ?
249 ICUniversalEx(m_HIC, ICM_DECOMPRESSEX_BEGIN, 0,
250 m_pFormat, 0, m_bitrick, 0) :
251 ICDecompressBegin(m_HIC, m_pFormat, m_bitrick);
252 m_bitrick->biCompression = tmpcomp;
253 //AVM_WRITE("Win32 video decoder", "DECODER COMPRESSSION %.4s\n", (char*)&m_decoder.biCompression);
254 if (hr != 0 && (hr != -2 || m_Info.fourcc != fccMJPG))
255 {
256 // FIXME MJPG decoder (m3jpeg32.dll) returns -2
257 // if anyone knows why - please fix it....
258 //BitmapInfo(*m_pFormat).Print();
259 //AVM_WRITE("Win32 video decoder", "Dest fmt:\n");
260 //BitmapInfo(m_decoder).Print();
261 AVM_WRITE("Win32 video decoder", "WARNING: ICDecompressBegin() failed ( shouldn't happen ), hr=%d (%s)\n",
262 (int)hr, ((hr == ICERR_BADFORMAT) ? "Bad Format)" : "?)"));
263 return -1;
264 }
265 m_iStatus = 1;
266 return 0;
267 }
268
269 int VideoDecoder::Stop()
270 {
271 if (m_iStatus)
272 {
273 int r = ICDecompressEnd(m_HIC);
274 if (r != 0)
275 AVM_WRITE("Win32 video decoder", "WARNING: ICDecompressEnd() failed ( shouldn't happen ), hr=%d\n", r);
276 m_iStatus = 0;
277 }
278 return 0;
279 }
280
281 int VideoDecoder::DecodeFrame(CImage* dest, const void* src, size_t size,
282 int is_keyframe, bool render, CImage** pOut)
283 {
284 CImage* tmp = 0;
285 int hr;
286
287 //printf("DECODERNOMAR\n"); m_Dest.Print(); dest->GetFmt()->Print();
288 uint8_t* d = (dest) ? dest->Data() : 0;
289 int flg = ((is_keyframe) ? 0 : ICDECOMPRESS_NOTKEYFRAME)
290 | ((dest && render) ? 0 : ICDECOMPRESS_HURRYUP/*|ICDECOMPRESS_PREROL*/);
291
292 if (!m_iStatus)
293 return -1;
294
295 if (m_bLastNeeded)
296 {
297 if (!dest || !dest->IsFmt(&m_Dest))
298 {
299 if (!m_pLastImage)
300 m_pLastImage = new CImage(&m_Dest);
301 if (dest)
302 d = m_pLastImage->Data();
303 }
304 else
305 {
306 if (m_pLastImage)
307 {
308 if (1 || !is_keyframe)
309 {
310 // unfortunately for reconstructed files
311 // we can't recognize keyframes for them
312 //printf("Copy prev last %p %p\n", dest, m_pLastImage);
313 dest->Convert(m_pLastImage);
314 }
315 m_pLastImage->Release();
316 }
317 dest->AddRef();
318 m_pLastImage = dest;
319 }
320 }
321
322 //AVM_WRITE("Win32 video decoder", "PTR %p src: %p size: %d kf: %d\n", dest->Data(), src, size, is_keyframe);
323 setDecoder(m_Dest);
324 m_pFormat->biSizeImage = size;
325
326 hr = (m_bUseEx) ?
327 ICUniversalEx(m_HIC, ICM_DECOMPRESSEX, flg, m_pFormat, (void*) src, m_bitrick, d) :
328 ICDecompress(m_HIC, flg, m_pFormat, (void*) src, m_bitrick, d);
329
330 if (dest)
331 {
332 if (hr)
333 AVM_WRITE("Win32 video decoder", "VideoDecoder: warning: hr=%d\n", hr);
334 else if (m_bLastNeeded && dest && d != dest->Data())
335 dest->Convert(m_pLastImage);
336 }
337
338 return hr;
339 }
340
341 int VideoDecoder::SetDestFmt(int bits, fourcc_t csp)
342 {
343 if (!CImage::Supported(csp, bits))
344 return -1;
345
346 switch (m_Info.fourcc)
347 {
348 case fccIV31:
349 case fccIV32:
350 return -1;
351 }
352
353 BitmapInfo tmpbi(m_Dest);
354 AVM_WRITE("Win32 video decoder", 1, "SetDestFmt bits: %d csp: %.4s\n", bits, (const char*)&csp);
355
356 if (bits)
357 {
358 switch (bits)
359 {
360 case 15:
361 case 16:
362 case 24:
363 case 32:
364 // 16bit ??? if (m_Info.fourcc_array[0] == fccMJPG)
365 // m_Dest.biSize=0x28;
366
367 // FIXME - use one routine to setup biHeight!!!
368 m_Dest.SetBits(bits);
369 switch (m_Info.fourcc)
370 {
371 case fccASV1:
372 case fccASV2:
373 m_Dest.biHeight = labs(m_Dest.biHeight);
374 // these codecs produce upside down RGB images
375 break;
376 }
377 break;
378 default:
379 return -1;
380 }
381 } else
382 m_Dest.SetSpace(csp);
383
384 Stop();
385 // note i263 is able to produce downside-up down image for YUY2
386 setDecoder(m_Dest);
387 int tmpcomp = m_bitrick->biCompression;
388 if (m_divx_trick)
389 m_bitrick->biCompression = 0;
390
391 int hr = (m_bUseEx) ?
392 ICUniversalEx(m_HIC, ICM_DECOMPRESSEX_QUERY, 0,
393 m_pFormat, 0, m_bitrick, 0) :
394 ICDecompressQuery(m_HIC, m_pFormat, m_bitrick);
395
396 //m_bitrick.Print();
397 //BitmapInfo tmx((BitmapInfo*)m_pFormat);
398
399 m_bitrick->biCompression = tmpcomp;
400
401 if (hr != 0)
402 {
403 if (csp)
404 {
405 AVM_WRITE("Win32 video decoder", "WARNING: Unsupported color space 0x%x (%.4s)\n",
406 csp, (char*)&csp);
407 }
408 else
409 AVM_WRITE("Win32 video decoder", "WARNING: Unsupported bit depth: %d\n", bits);
410
411 m_Dest = tmpbi;
412 m_Dest.Print();
413 setDecoder(m_Dest);
414 }
415
416 Start();
417
418 return (hr == 0) ? 0 : -1;
419 }
420
421 void VideoDecoder::setDecoder(BitmapInfo& bi)
422 {
423 // persistent biSize
424 memcpy((char*)m_bitrick + 4, (char*)&bi + 4, sizeof(BitmapInfo) - 4);
425 m_bitrick->biSize = 40;
426 switch (m_Info.fourcc)
427 {
428 case fccXVID:
429 //if (m_bitrick->biCompression == 0 || m_bitrick->biCompression == 3)
430 // break;
431 // XviD doesn't like biHeight < 0 even for YUV surfaces
432 break;
433 }
434 }
435
436 AVM_END_NAMESPACE;