| 1 |
/** |
| 2 |
* DMO Video decoder implementation |
| 3 |
* Copyright 2002 Zdenek Kabelac ([email protected]) |
| 4 |
* Eugene Kuznetsov ([email protected]) |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "dshow/guids.h" |
| 8 |
#include "dshow/interfaces.h" |
| 9 |
#include "avm_output.h" |
| 10 |
|
| 11 |
#include "DMO_VideoDecoder.h" |
| 12 |
#include "ldt_keeper.h" |
| 13 |
#include "configfile.h" |
| 14 |
#include "avm_cpuinfo.h" |
| 15 |
|
| 16 |
#include "wine/winerror.h" |
| 17 |
#include "wine/windef.h" |
| 18 |
#include "wine/winreg.h" |
| 19 |
#include "wine/vfw.h" |
| 20 |
#include "registry.h" |
| 21 |
|
| 22 |
#ifndef NOAVIFILE_HEADERS |
| 23 |
#define VFW_E_NOT_RUNNING 0x80040226 |
| 24 |
#include "avm_fourcc.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <unistd.h> |
| 28 |
#include <fcntl.h> |
| 29 |
#include <errno.h> |
| 30 |
#include <sys/types.h> |
| 31 |
#include <sys/mman.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> // labs |
| 34 |
|
| 35 |
extern "C" void trapbug(); |
| 36 |
|
| 37 |
AVM_BEGIN_NAMESPACE; |
| 38 |
|
| 39 |
static const struct fcc2gc_s { |
| 40 |
fourcc_t fcc; |
| 41 |
unsigned int bits; |
| 42 |
GUID subtype; |
| 43 |
DMO_VideoDecoder::CAPS cap; |
| 44 |
} fcc2gctab[] = { |
| 45 |
{ fccI420, 12, MEDIASUBTYPE_I420, IVideoDecoder::CAP_I420 }, |
| 46 |
{ fccYV12, 12, MEDIASUBTYPE_YV12, IVideoDecoder::CAP_YV12 }, |
| 47 |
{ fccYUY2, 16, MEDIASUBTYPE_YUY2, IVideoDecoder::CAP_YUY2 }, |
| 48 |
{ fccUYVY, 16, MEDIASUBTYPE_UYVY, IVideoDecoder::CAP_UYVY }, |
| 49 |
{ fccYVYU, 16, MEDIASUBTYPE_YVYU, IVideoDecoder::CAP_YVYU }, |
| 50 |
{ fccIYUV, 24, MEDIASUBTYPE_IYUV, IVideoDecoder::CAP_IYUV }, |
| 51 |
|
| 52 |
{ 8, 8, MEDIASUBTYPE_RGB8, IVideoDecoder::CAP_NONE }, |
| 53 |
{ 15, 16, MEDIASUBTYPE_RGB555, IVideoDecoder::CAP_NONE }, |
| 54 |
{ 16, 16, MEDIASUBTYPE_RGB565, IVideoDecoder::CAP_NONE }, |
| 55 |
{ 24, 24, MEDIASUBTYPE_RGB24, IVideoDecoder::CAP_NONE }, |
| 56 |
{ 32, 32, MEDIASUBTYPE_RGB32, IVideoDecoder::CAP_NONE }, |
| 57 |
|
| 58 |
{ 0 }, |
| 59 |
}; |
| 60 |
|
| 61 |
static const struct fcc2gc_s* fcc2gc(uint_t fcc) |
| 62 |
{ |
| 63 |
const struct fcc2gc_s* c; |
| 64 |
for (c = fcc2gctab; c->fcc; c++) |
| 65 |
if (c->fcc == fcc) |
| 66 |
return c; |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
DMO_VideoDecoder::DMO_VideoDecoder(const CodecInfo& info, const BITMAPINFOHEADER& format, int flip) |
| 71 |
:IVideoDecoder(info, format), m_iStatus(0), |
| 72 |
m_iLastPPMode(0), m_iLastBrightness(0), m_iLastContrast(0), |
| 73 |
m_iLastSaturation(0), m_iLastHue(0), |
| 74 |
m_bHaveUpsideDownRGB(true), m_bFlip(flip) |
| 75 |
{ |
| 76 |
unsigned bihs = (m_pFormat->biSize < (int) sizeof(BITMAPINFOHEADER)) ? |
| 77 |
sizeof(BITMAPINFOHEADER) : m_pFormat->biSize; |
| 78 |
bihs = sizeof(VIDEOINFOHEADER) - sizeof(BITMAPINFOHEADER) + bihs; |
| 79 |
m_pDMO_Filter = 0; |
| 80 |
|
| 81 |
//int ss = 0;for (unsigned i = 0; i < m_pFormat->biSize; i++) { ss += ((unsigned char*) m_pFormat)[i]; printf(" %2x", ((unsigned char*) m_pFormat)[i]); if (!(i%16)) printf("\n"); } |
| 82 |
//printf("\nBITMAPINFOHEADER %d %d %d\n", m_pFormat->biSize, bihs, ss); |
| 83 |
//m_Dest.SetSpace(IMG_FMT_YV12); |
| 84 |
//m_Dest = *m_pFormat; |
| 85 |
//m_Dest.biHeight = labs(m_Dest.biHeight); |
| 86 |
//m_Dest.Print(); |
| 87 |
|
| 88 |
m_sVhdr = (VIDEOINFOHEADER*) malloc(bihs); |
| 89 |
memset(m_sVhdr, 0, bihs); |
| 90 |
memcpy(&m_sVhdr->bmiHeader, m_pFormat, m_pFormat->biSize); |
| 91 |
m_sVhdr->rcSource.left = m_sVhdr->rcSource.top = 0; |
| 92 |
m_sVhdr->rcSource.right = m_sVhdr->bmiHeader.biWidth; |
| 93 |
m_sVhdr->rcSource.bottom = m_sVhdr->bmiHeader.biHeight; |
| 94 |
m_sVhdr->rcTarget = m_sVhdr->rcSource; |
| 95 |
|
| 96 |
m_sOurType.majortype = MEDIATYPE_Video; |
| 97 |
m_sOurType.subtype = MEDIATYPE_Video; |
| 98 |
m_sOurType.subtype.f1 = m_sVhdr->bmiHeader.biCompression; |
| 99 |
m_sOurType.formattype = FORMAT_VideoInfo; |
| 100 |
m_sOurType.bFixedSizeSamples = false; |
| 101 |
m_sOurType.bTemporalCompression = true; |
| 102 |
m_sOurType.pUnk = 0; |
| 103 |
m_sOurType.cbFormat = bihs; |
| 104 |
m_sOurType.pbFormat = (char*)m_sVhdr; |
| 105 |
|
| 106 |
m_sVhdr2 = (VIDEOINFOHEADER*) malloc(sizeof(VIDEOINFOHEADER) + 12); |
| 107 |
memset(m_sVhdr2, 0, sizeof(VIDEOINFOHEADER) + 12); |
| 108 |
m_sVhdr2->rcTarget = m_sVhdr->rcTarget; |
| 109 |
m_sVhdr2->rcSource = m_sVhdr->rcSource; |
| 110 |
memset(&m_sDestType, 0, sizeof(m_sDestType)); |
| 111 |
m_sDestType.majortype = MEDIATYPE_Video; |
| 112 |
m_sDestType.formattype = FORMAT_VideoInfo; |
| 113 |
m_sDestType.bFixedSizeSamples = true; |
| 114 |
m_sDestType.bTemporalCompression = false; |
| 115 |
m_sDestType.cbFormat = sizeof(VIDEOINFOHEADER); |
| 116 |
m_sDestType.pbFormat = (char*)m_sVhdr2; |
| 117 |
|
| 118 |
SetDestFmt(24, 0); |
| 119 |
//SetDestFmt(0, IMG_FMT_YV12); |
| 120 |
} |
| 121 |
|
| 122 |
// this is cruel hack to avoid problems with throws - |
| 123 |
// could anyone explain me why GNU people can't build reliable |
| 124 |
// C++ compiler |
| 125 |
int DMO_VideoDecoder::init() |
| 126 |
{ |
| 127 |
const struct fcc2gc_s* c; |
| 128 |
int r; |
| 129 |
int bbc; |
| 130 |
int bic; |
| 131 |
GUID sbt; |
| 132 |
|
| 133 |
Setup_FS_Segment(); |
| 134 |
|
| 135 |
m_pDMO_Filter = DMO_FilterCreate(m_Info.dll.c_str(), &m_Info.guid, &m_sOurType, &m_sDestType); |
| 136 |
if (!m_pDMO_Filter) |
| 137 |
{ |
| 138 |
AVM_WRITE("Win32 DMO video decoder", "WARNING: format not accepted!\n"); |
| 139 |
return -1; |
| 140 |
} |
| 141 |
|
| 142 |
AVM_WRITE("Win32 DMO video decoder", "opened dll: %s\n", m_Info.dll.c_str()); |
| 143 |
|
| 144 |
if (m_Dest.biHeight < 0) |
| 145 |
{ |
| 146 |
r = m_pDMO_Filter->m_pMedia->vt->SetOutputType(m_pDMO_Filter->m_pMedia, 0, &m_sDestType, DMO_SET_TYPEF_TEST_ONLY); |
| 147 |
if (r) |
| 148 |
{ |
| 149 |
AVM_WRITE("Win32 DMO video decoder", "WARNING: decoder does not support upside-down RGB frames!\n"); |
| 150 |
m_Dest.biHeight = -m_Dest.biHeight; |
| 151 |
m_sVhdr2->bmiHeader.biHeight = m_Dest.biHeight; |
| 152 |
m_bHaveUpsideDownRGB = false; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
m_Caps = CAP_NONE; |
| 157 |
bbc = m_sVhdr2->bmiHeader.biBitCount; |
| 158 |
bic = m_sVhdr2->bmiHeader.biCompression; |
| 159 |
sbt = m_sDestType.subtype; |
| 160 |
for (c = fcc2gctab; c->bits && (c->cap != CAP_NONE); c++) |
| 161 |
{ |
| 162 |
// check only for YUV |
| 163 |
m_sVhdr2->bmiHeader.biBitCount = c->bits; |
| 164 |
m_sVhdr2->bmiHeader.biCompression = c->fcc; |
| 165 |
m_sDestType.subtype = c->subtype; |
| 166 |
r = m_pDMO_Filter->m_pMedia->vt->SetOutputType(m_pDMO_Filter->m_pMedia, 0, &m_sDestType, DMO_SET_TYPEF_TEST_ONLY); |
| 167 |
if (!r) |
| 168 |
m_Caps = (CAPS)(m_Caps | c->cap); |
| 169 |
//printf("Testing CSP err: %x %x %.4s\n", r, c->fcc, (char*)&c->fcc); |
| 170 |
} |
| 171 |
//m_Caps = CAP_YV12; |
| 172 |
|
| 173 |
m_sVhdr2->bmiHeader.biBitCount = bbc; |
| 174 |
m_sVhdr2->bmiHeader.biCompression = bic; |
| 175 |
m_sDestType.subtype = sbt; |
| 176 |
|
| 177 |
SetDirection(m_bFlip); |
| 178 |
|
| 179 |
//m_Dest.Print(); |
| 180 |
//r = m_pDMO_Filter->m_pMedia->vt->Flush(m_pDMO_Filter->m_pMedia); |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
DMO_VideoDecoder::~DMO_VideoDecoder() |
| 185 |
{ |
| 186 |
Stop(); |
| 187 |
|
| 188 |
if (m_sVhdr) |
| 189 |
free(m_sVhdr); |
| 190 |
if (m_sVhdr2) |
| 191 |
free(m_sVhdr2); |
| 192 |
if (m_pDMO_Filter) |
| 193 |
DMO_Filter_Destroy(m_pDMO_Filter); |
| 194 |
} |
| 195 |
|
| 196 |
int DMO_VideoDecoder::Start() |
| 197 |
{ |
| 198 |
if (!m_iStatus) |
| 199 |
{ |
| 200 |
//AVM_WRITE("Win32 video decoder", "DSSTART %p %d\n", this, m_sVhdr2->bmiHeader.biHeight); |
| 201 |
m_iStatus = 1; |
| 202 |
Flush(); |
| 203 |
} |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
int DMO_VideoDecoder::Stop() |
| 208 |
{ |
| 209 |
if (m_iStatus) |
| 210 |
{ |
| 211 |
m_iStatus = 0; |
| 212 |
//AVM_WRITE("Win32 video decoder", "DSSTOP %p\n", this); |
| 213 |
} |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
int DMO_VideoDecoder::DecodeFrame(CImage* dest, const void* src, size_t size, |
| 218 |
int is_keyframe, bool render, CImage** pOut) |
| 219 |
{ |
| 220 |
unsigned long status; // to be ignored by M$ specs |
| 221 |
DMO_OUTPUT_DATA_BUFFER db; |
| 222 |
CMediaBuffer* bufferin; |
| 223 |
uint8_t* imdata = dest ? dest->Data() : 0; |
| 224 |
|
| 225 |
if (!m_iStatus) |
| 226 |
{ |
| 227 |
AVM_WRITE("Win32 DMO video decoder", "not started!\n"); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
Setup_FS_Segment(); |
| 232 |
|
| 233 |
//if (!dest->IsFmt(&m_Dest)) { dest->GetFmt()->Print(); m_Dest.Print(); printf("\n\nERRRRRRRRR\n\n"); } |
| 234 |
//unsigned long dwf = 0; |
| 235 |
//int rr1 = m_pDMO_Filter->m_pMedia->vt->GetInputStatus(m_pDMO_Filter->m_pMedia, 0, &dwf); |
| 236 |
//printf("PROCESSTESTINPUT %d %x\n", rr1, dwf); |
| 237 |
//rr1 = m_pDMO_Filter->m_pMedia->vt->GetInputStreamInfo(m_pDMO_Filter->m_pMedia, 0, &dwf); |
| 238 |
//printf("PROCESSTESTINPUTSTREAM %d %x\n", rr1, dwf); |
| 239 |
//trapbug(); |
| 240 |
bufferin = CMediaBufferCreate(size, (void*)src, size, 0); |
| 241 |
int r = m_pDMO_Filter->m_pMedia->vt->ProcessInput(m_pDMO_Filter->m_pMedia, 0, |
| 242 |
(IMediaBuffer*)bufferin, |
| 243 |
(is_keyframe) ? DMO_INPUT_DATA_BUFFERF_SYNCPOINT : 0, |
| 244 |
0, 0); |
| 245 |
((IMediaBuffer*)bufferin)->vt->Release((IUnknown*)bufferin); |
| 246 |
|
| 247 |
if (r != S_OK) |
| 248 |
{ |
| 249 |
/* something for process */ |
| 250 |
if (r != S_FALSE) |
| 251 |
printf("ProcessInputError r:0x%x=%d (keyframe: %d)\n", r, r, is_keyframe); |
| 252 |
else |
| 253 |
printf("ProcessInputError FALSE ?? (keyframe: %d)\n", is_keyframe); |
| 254 |
return size; |
| 255 |
} |
| 256 |
|
| 257 |
db.rtTimestamp = 0; |
| 258 |
db.rtTimelength = 0; |
| 259 |
db.dwStatus = 0; |
| 260 |
db.pBuffer = (IMediaBuffer*) CMediaBufferCreate(m_sDestType.lSampleSize, |
| 261 |
imdata, 0, 0); |
| 262 |
r = m_pDMO_Filter->m_pMedia->vt->ProcessOutput(m_pDMO_Filter->m_pMedia, |
| 263 |
(imdata) ? 0 : DMO_PROCESS_OUTPUT_DISCARD_WHEN_NO_BUFFER, |
| 264 |
1, &db, &status); |
| 265 |
//m_pDMO_Filter->m_pMedia->vt->Lock(m_pDMO_Filter->m_pMedia, 0); |
| 266 |
if ((unsigned)r == DMO_E_NOTACCEPTING) |
| 267 |
printf("ProcessOutputError: Not accepting\n"); |
| 268 |
else if (r) |
| 269 |
printf("ProcessOutputError: r:0x%x=%d %ld stat:%ld\n", r, r, status, db.dwStatus); |
| 270 |
|
| 271 |
((IMediaBuffer*)db.pBuffer)->vt->Release((IUnknown*)db.pBuffer); |
| 272 |
|
| 273 |
|
| 274 |
//int r = m_pDMO_Filter->m_pMedia->vt->Flush(m_pDMO_Filter->m_pMedia); |
| 275 |
//printf("FLUSH %d\n", r); |
| 276 |
int check = 0; |
| 277 |
if (m_bSetFlg) |
| 278 |
{ |
| 279 |
//printf("SETFLG %d \n", m_iLastPPMode); |
| 280 |
if (m_iLastPPMode >= 0 && m_iLastHue != -1) |
| 281 |
{ |
| 282 |
m_bSetFlg = false; |
| 283 |
setCodecValues(); |
| 284 |
} |
| 285 |
check++; |
| 286 |
} |
| 287 |
|
| 288 |
if (check) |
| 289 |
getCodecValues(); |
| 290 |
|
| 291 |
return size;//hr; |
| 292 |
} |
| 293 |
|
| 294 |
/* |
| 295 |
* bits == 0 - leave unchanged |
| 296 |
*/ |
| 297 |
int DMO_VideoDecoder::SetDestFmt(int bits, fourcc_t csp) |
| 298 |
{ |
| 299 |
|
| 300 |
const struct fcc2gc_s* t; |
| 301 |
int r; |
| 302 |
|
| 303 |
if ((bits || csp) && !CImage::Supported(csp, bits)) |
| 304 |
return -1; |
| 305 |
|
| 306 |
AVM_WRITE("Win32 DMO video decoder", 1, "SetDestFmt %d %.4s\n", bits, (char*)&csp); |
| 307 |
|
| 308 |
if (bits == 0) |
| 309 |
bits = csp; |
| 310 |
|
| 311 |
BitmapInfo temp = m_Dest; // rememeber current setting |
| 312 |
Setup_FS_Segment(); |
| 313 |
|
| 314 |
t = fcc2gc(bits); |
| 315 |
if (t) |
| 316 |
{ |
| 317 |
m_sDestType.subtype = t->subtype; |
| 318 |
if (!t->cap) |
| 319 |
{ |
| 320 |
m_Dest.SetBits(bits); |
| 321 |
if (!m_bHaveUpsideDownRGB) |
| 322 |
m_Dest.biHeight = labs(m_Dest.biHeight); |
| 323 |
} |
| 324 |
else |
| 325 |
m_Dest.SetSpace(bits); |
| 326 |
} // else reset what we already have (dir change) |
| 327 |
|
| 328 |
//m_Dest.Print(); |
| 329 |
m_sDestType.lSampleSize = m_Dest.biSizeImage; |
| 330 |
memcpy(&m_sVhdr2->bmiHeader, &m_Dest, sizeof(m_sVhdr2->bmiHeader)); |
| 331 |
m_sDestType.cbFormat = sizeof(VIDEOINFOHEADER); |
| 332 |
if (m_sVhdr2->bmiHeader.biCompression == 3) |
| 333 |
m_sDestType.cbFormat += 12; |
| 334 |
|
| 335 |
if (!m_pDMO_Filter) |
| 336 |
return 0; |
| 337 |
// test accept |
| 338 |
r = m_pDMO_Filter->m_pMedia->vt->SetOutputType(m_pDMO_Filter->m_pMedia, 0, &m_sDestType, DMO_SET_TYPEF_TEST_ONLY); |
| 339 |
if (r != 0) |
| 340 |
{ |
| 341 |
if (csp) |
| 342 |
AVM_WRITE("Win32 video decoder", "Warning: unsupported color space\n"); |
| 343 |
else |
| 344 |
AVM_WRITE("Win32 video decoder", "Warning: unsupported bit depth\n"); |
| 345 |
m_Dest = temp; |
| 346 |
m_sDestType.lSampleSize = m_Dest.biSizeImage; |
| 347 |
memcpy(&(m_sVhdr2->bmiHeader), &m_Dest, sizeof(temp)); |
| 348 |
m_sDestType.cbFormat = sizeof(VIDEOINFOHEADER); |
| 349 |
if (m_sVhdr2->bmiHeader.biCompression == 3) |
| 350 |
m_sDestType.cbFormat += 12; |
| 351 |
|
| 352 |
return -1; |
| 353 |
} |
| 354 |
m_pDMO_Filter->m_pMedia->vt->SetOutputType(m_pDMO_Filter->m_pMedia, 0, &m_sDestType, 0); |
| 355 |
return 0; |
| 356 |
} |
| 357 |
|
| 358 |
int DMO_VideoDecoder::SetDirection(int d) |
| 359 |
{ |
| 360 |
if (m_Dest.biHeight < 0) |
| 361 |
m_Dest.biHeight = -m_Dest.biHeight; |
| 362 |
|
| 363 |
if (!d && m_bHaveUpsideDownRGB) |
| 364 |
m_Dest.biHeight = -m_Dest.biHeight; |
| 365 |
|
| 366 |
m_sVhdr2->bmiHeader.biHeight = m_Dest.biHeight; |
| 367 |
|
| 368 |
//printf("SETDIRECTION %d\n", m_sVhdr2->bmiHeader.biHeight); m_Dest.Print(); |
| 369 |
if (m_pDMO_Filter) |
| 370 |
SetDestFmt(0, 0); |
| 371 |
return 0; |
| 372 |
} |
| 373 |
|
| 374 |
int DMO_VideoDecoder::GetValue(const char* name, int* value) const |
| 375 |
{ |
| 376 |
if (strcmp(name, "postprocessing") == 0) // Pp |
| 377 |
*value = m_iLastPPMode; |
| 378 |
else if (strcmp(name, "Brightness") == 0) |
| 379 |
*value = m_iLastBrightness; |
| 380 |
else if (strcmp(name, "Contrast") == 0) |
| 381 |
*value = m_iLastContrast; |
| 382 |
else if (strcmp(name, "Saturation") == 0) |
| 383 |
*value = m_iLastSaturation; |
| 384 |
else if (strcmp(name, "Hue") == 0) |
| 385 |
*value = m_iLastHue; |
| 386 |
else if (strcmp(name, "maxauto") == 0) |
| 387 |
*value = m_iMaxAuto; |
| 388 |
else |
| 389 |
return -1; |
| 390 |
|
| 391 |
//printf("BR %d C %d S %d H %d PP: %d (%s)\n", m_iLastBrightness, |
| 392 |
// m_iLastContrast, m_iLastSaturation, m_iLastHue, m_iLastPPMode, name); |
| 393 |
|
| 394 |
return 0; |
| 395 |
} |
| 396 |
|
| 397 |
int DMO_VideoDecoder::getCodecValues() |
| 398 |
{ |
| 399 |
return 0; |
| 400 |
} |
| 401 |
|
| 402 |
int DMO_VideoDecoder::SetValue(const char* name, int value) |
| 403 |
{ |
| 404 |
m_bSetFlg = true; |
| 405 |
if (strcmp(name, "postprocessing") == 0) // Pp |
| 406 |
m_iLastPPMode = value; |
| 407 |
else if (strcmp(name, "Brightness") == 0) |
| 408 |
m_iLastBrightness = value; |
| 409 |
else if (strcmp(name, "Contrast") == 0) |
| 410 |
m_iLastContrast = value; |
| 411 |
else if (strcmp(name, "Saturation") == 0) |
| 412 |
m_iLastSaturation = value; |
| 413 |
else if (strcmp(name, "Hue") == 0) |
| 414 |
m_iLastHue = value; |
| 415 |
else if (strcmp(name, "maxauto") == 0) |
| 416 |
m_iMaxAuto = value; |
| 417 |
//printf("SETVAL B %d C %d S %d H %d \n", m_iLastBrightness, |
| 418 |
// m_iLastContrast, m_iLastSaturation, m_iLastHue); |
| 419 |
//else AVM_WRITE("Win32 video decoder", "Set %s %d\n", name, value); |
| 420 |
return 0; |
| 421 |
} |
| 422 |
|
| 423 |
int DMO_VideoDecoder::setCodecValues() |
| 424 |
{ |
| 425 |
if (!m_iStatus) |
| 426 |
return -1; |
| 427 |
return 0; |
| 428 |
} |
| 429 |
|
| 430 |
AVM_END_NAMESPACE; |