Debian Codegraph @

plugins/libwin32/videocodec/VideoEncoder.cpp

raw
1 /********************************************************
2
3 Video encoder implementation
4 Copyright 2000 Eugene Kuznetsov ([email protected])
5
6 *********************************************************/
7
8 #include "wine/windef.h"
9 #include "wine/winreg.h"
10 #include "image.h"
11 #include "avm_fourcc.h"
12 //#define TIMING
13 #include "avm_output.h"
14 //#undef TIMING
15 #ifdef TIMING
16 #include "cpuinfo.h"
17 #endif
18
19 #include "VideoEncoder.h"
20 #include "registry.h"
21
22 #if 0
23 struct ICINFO
24 {
25 long dwSize; /* 00: */
26 long fccType; /* 04:compressor type 'vidc' 'audc' */
27 long fccHandler; /* 08:compressor sub-type 'rle ' 'jpeg' 'pcm '*/
28 long dwFlags; /* 0c:flags LOshort is type specific */
29 long dwVersion; /* 10:version of the driver */
30 long dwVersionICM; /* 14:version of the ICM used */
31 /*
32 * under Win32, the driver always returns UNICODE strings.
33 */
34 short szName[16]; /* 18:short name */
35 short szDescription[128]; /* 38:long name */
36 short szDriver[128]; /* 138:driver that contains compressor*/
37 /* 238: */
38 /*
39 Applicable flags. Zero or more of the following flags can be set:
40 VIDCF_COMPRESSFRAMES
41 Driver is requesting to compress all frames. For information about compressing all
42 frames, see the ICM_COMPRESS_FRAMES_INFO message.
43 VIDCF_CRUNCH
44 Driver supports compressing to a frame size.
45 VIDCF_DRAW
46 Driver supports drawing.
47 VIDCF_FASTTEMPORALC
48 Driver can perform temporal compression and maintains its own copy of the
49 current frame. When compressing a stream of frame data, the driver doesn't need
50 image data from the previous frame.
51 VIDCF_FASTTEMPORALD
52 Driver can perform temporal decompression and maintains its own copy of the
53 current frame. When decompressing a stream of frame data, the driver doesn't
54 need image data from the previous frame.
55 VIDCF_QUALITY
56 Driver supports quality values.
57 VIDCF_TEMPORAL
58 Driver supports inter-frame compression
59 */
60 };
61 #endif
62
63 #include <time.h>
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <string.h>
67 #include <ctype.h>
68
69 AVM_BEGIN_NAMESPACE;
70
71 VideoEncoder::VideoEncoder(const CodecInfo& info, fourcc_t compressor, const BITMAPINFOHEADER& format)
72 :IVideoEncoder(info), m_pModule(0), m_HIC(0), m_bh(0),
73 m_bhorig(0), m_obh(0),
74 m_prev(0), m_pConfigData(0), m_iConfigDataSize(0),
75 m_iState(0), m_iBitrate(910000), m_fFps(25.0)
76 {
77 unsigned bihs = (format.biSize < (int) sizeof(BITMAPINFO)) ?
78 sizeof(BITMAPINFO) : format.biSize;
79 m_bh = (BitmapInfo*) malloc(bihs);
80 memcpy(m_bh, &format, bihs);
81 m_bhorig = (BitmapInfo*) malloc(bihs);
82 memcpy(m_bhorig, &format, bihs);
83 m_bh->biHeight = labs(m_bh->biHeight);
84
85 if (m_bhorig->biCompression == BI_RGB ||
86 m_bhorig->biCompression == BI_BITFIELDS)
87 // only bottom-up RGB images
88 m_bhorig->biHeight = labs(m_bhorig->biHeight);
89
90 switch (compressor)
91 {
92 case fccMP41:
93 case fccMP43:
94 compressor = fccDIV3;
95 break;
96 }
97 m_comp_id = compressor;
98 }
99
100 int VideoEncoder::init()
101 {
102 m_pModule = control.Create(m_Info);
103 if (!m_pModule)
104 return -1;
105
106 m_HIC = m_pModule->CreateHandle(m_comp_id, Module::Compress);
107 if (!m_HIC)
108 return -1;
109
110 int r = ICCompressGetFormat(m_HIC, m_bh, 0);
111 if (r < 0)
112 {
113 AVM_WRITE("VideoEncoder", "Can't handle this format\n");
114 return -1;
115 }
116 //printf("Format size %d\n", r);
117 m_obh = (BITMAPINFOHEADER*) malloc(r);
118 memset(m_obh, 0, r);
119 m_obh->biSize = r;
120 r = ICCompressGetFormat(m_HIC, m_bh, m_obh);
121 if (r != 0)
122 {
123 AVM_WRITE("VideoEncoder", "Can't handle this format\n");
124 return -1;
125 }
126
127 m_obh->biHeight = labs(m_obh->biHeight);
128 m_obh->biBitCount = m_bh->biBitCount;
129
130 m_iState = 1;
131
132 // test if it really works
133 if (Start() != 0)
134 {
135 AVM_WRITE("VideoEncoder", "WARNING: CompressBegin failed ( probably unsupported input format %d )\n", r);
136 return -1;
137 }
138 Stop();
139
140 // Save configuration state.
141 //
142 // Ordinarily, we wouldn't do this, but there seems to be a bug in
143 // the Microsoft MPEG-4 compressor that causes it to reset its
144 // configuration data after a compression session. This occurs
145 // in all versions from V1 through V3.
146 //
147 // Stupid fscking Matrox driver returns -1!!!
148
149 m_iConfigDataSize = ICGetState(m_HIC, 0, 0);
150
151 if (m_iConfigDataSize > 0)
152 {
153 m_pConfigData = (char*) malloc(m_iConfigDataSize);
154 //printf("****************** %d\n", m_iConfigDataSize);
155 m_iConfigDataSize = ICGetState(m_HIC, m_pConfigData, m_iConfigDataSize);
156
157 // As odd as this may seem, if this isn't done, then the Indeo5
158 // compressor won't allow data rate control until the next
159 // compression operation!
160
161 if (m_iConfigDataSize)
162 ICSetState(m_HIC, m_pConfigData, m_iConfigDataSize);
163 }
164 return 0;
165 }
166
167 VideoEncoder::~VideoEncoder()
168 {
169 if (m_iState != 1)
170 Stop();
171
172 if (m_pModule)
173 m_pModule->CloseHandle(m_HIC);
174 if (m_bh) free(m_bh);
175 if (m_bhorig) free(m_bhorig);
176 if (m_obh) free(m_obh);
177 if (m_prev) free(m_prev);
178 if (m_pConfigData) free(m_pConfigData);
179 }
180
181 int VideoEncoder::EncodeFrame(const CImage* src, void* dest, int* is_keyframe, size_t* size, int* lpckid)
182 {
183 if (m_iState != 2)
184 return -1;
185
186 #ifdef TIMING
187 st1=localcount();
188 #endif
189 CImage* temp = (src->IsFmt(m_bhorig)) ? 0 : new CImage(src, m_bhorig);
190
191 *is_keyframe = 0;
192 if (m_iKeyRate && (m_iFrameNum - m_iLastKF) > m_iKeyRate)
193 {
194 *is_keyframe = ICCOMPRESS_KEYFRAME; // force KF if too long there wasn't one
195 //printf("FORCING KEFRAME %d %d\n", m_iFrameNum, m_iLastKF);
196 }
197 int r = ICCompress(m_HIC, *is_keyframe,
198 m_obh, dest,
199 m_bh, temp ? (void*)temp->Data() : (void*)src->Data(),
200 (long*)lpckid,
201 (long*)is_keyframe,
202 m_iFrameNum, m_iFrameNum ? 0 : 0x7fffffff,
203 m_iQuality,
204 (is_keyframe) ? 0 : m_bh,
205 (is_keyframe) ? 0 : m_prev);
206 printf("==> hr:%d rest:%d fnum:%d 0x%x\n", r, m_iFrameNum%m_iKeyRate, m_iFrameNum, *is_keyframe);
207 if (temp)
208 temp->Release();
209 *is_keyframe &= 0x10;
210 if (*is_keyframe)
211 m_iLastKF = m_iFrameNum;
212
213 if (r == 0)
214 {
215 //if(m_prev==0)m_prev=new char[m_bh->biSizeImage];
216 //memcpy(m_prev, src, m_bh->biSizeImage);
217 if (!m_prev)
218 m_prev = (char*) malloc(ICCompressGetSize(m_HIC, m_bh, m_obh));
219 memcpy(m_prev, dest, m_obh->biSizeImage);
220
221 #if 0
222 // If we're using a compressor with a stupid algorithm (Microsoft Video 1),
223 // we have to decompress the frame again to compress the next one....
224
225 if (res==ICERR_OK && pPrevBuffer && (!lKeyRate || lKeyRateCounter>1)) {
226 res = ICDecompress(hic, dwFlags & AVIIF_KEYFRAME ? 0 : ICDECOMPRESS_NOTKEYFRAME,
227 (LPBITMAPINFOHEADER)pbiOutput,
228 pOutputBuffer,
229 (LPBITMAPINFOHEADER)pbiInput,
230 pPrevBuffer);
231 }
232 #endif
233 }
234 m_iFrameNum++;
235 if (size)
236 *size = m_obh->biSizeImage;
237 return r;
238 }
239
240 int VideoEncoder::Start()
241 {
242 if (m_iState != 1)
243 return -1;//wrong state
244 //HRESULT hr=ICCompressBegin(hic, m_bh, m_obh);
245
246 ICINFO ici;
247
248 int r = ICGetInfo(m_HIC, &ici);
249 if (!r)
250 {
251 AVM_WRITE("Win32 video encoder", "Unable to retrieve video compressor info!");
252 }
253
254 if (!(ici.dwFlags & VIDCF_QUALITY))
255 m_iQuality = 0;
256 else
257 ICGetDefaultQuality(m_HIC, &m_iQuality);
258 if (ICGetDefaultKeyFrameRate(m_HIC, &m_iKeyRate) != 0)
259 m_iKeyRate = 0xFFFF;
260 printf("KEYRATE %d\n", m_iKeyRate);
261 m_iKeyRate = 100;
262
263 #if 0
264 AVM_WRITE("Win32 video encoder", "ICINFO dwSize:%ld fccType:%.4s fccHandler:%.4s\n"
265 " dwFlags:0x%lx version:%d versionICM:%d\n",
266 //" name:%s descrition:%s driver:%s\n",
267 ici.dwSize, (char*)&ici.fccType, (char*)&ici.fccHandler,
268 ici.dwFlags, ici.dwVersion, ici.dwVersionICM
269 //,ici.szName, ici.szDescription, ici.szDriver
270 );
271 #endif
272 if (ici.dwFlags & VIDCF_TEMPORAL) {
273 if (!(ici.dwFlags & VIDCF_FASTTEMPORALC)) {
274 // Allocate backbuffer
275 //throw FATAL("BACKBUFFER needed - implement me!!");
276 //if (!(pPrevBuffer = new char[pbiInput->bmiHeader.biSizeImage]))
277 // throw MyMemoryError();
278 }
279 }
280
281 if (m_bh->biSizeImage == 0)
282 m_bh->biSizeImage = m_bh->biWidth*labs(m_bh->biHeight)*((m_bh->biBitCount+7)/8);
283 // Make sure that we can work with this format!
284
285 AVM_WRITE("Win32 video encoder", "W32 Quality %d KeyFrames: %d BitRate: %d\n", m_iQuality, m_iKeyRate, m_iBitrate);
286
287 setDivXRegs();
288
289 ICCOMPRESSFRAMES icf;
290 memset(&icf, 0, sizeof icf);
291 // ICM_COMPRESS_FRAMES_INFO:
292 //
293 // dwFlags Trashed with address of lKeyRate in tests. Something
294 // might be looking for a non-zero value here, so better
295 // set it.
296 // lpbiOutput NULL.
297 // lOutput 0.
298 // lpbiInput NULL.
299 // lInput 0. (reserved)
300 // lStartFrame 0.
301 // lFrameCount Number of frames.
302 // lQuality Set to quality factor, or zero if not supported.
303 // lDataRate Set to data rate in 1024*kilobytes, or zero if not
304 // supported.
305 // lKeyRate Set to the desired maximum keyframe interval. For
306 // all keyframes, set to 1.
307
308 icf.dwFlags = (long)&icf.lKeyRate;
309 icf.lStartFrame = 0;
310 icf.lFrameCount = 0x0FFFFFFF;
311 icf.lQuality = m_iQuality;
312 icf.lDataRate = m_iBitrate * 1000 / 1024;
313 icf.lKeyRate = (m_iKeyRate > 0) ? 0 * m_iKeyRate : 0;
314 icf.dwRate = 1000000;
315 icf.dwScale = (int) (icf.dwRate / m_fFps);
316 //printf("SETTING BITRATE B:%ld K:%ld Q:%d %ld / %f s:%ld\n", icf.lDataRate, m_iKeyRate, m_iQuality, icf.dwRate, m_fFps, icf.dwScale);
317
318 ICCompressFramesInfo(m_HIC, &icf);
319
320 r = ICCompressBegin(m_HIC, m_bh, m_obh);
321 if (r != 0)
322 {
323 AVM_WRITE("Win32 video encoder", "ICCompressBegin() failed ( shouldn't happen ), error code %d\n", (int)r);
324 return -1;
325 }
326
327 m_iLastKF = m_iFrameNum = 0;
328 m_iState = 2;
329 //AVM_WRITE("Win32 video encoder", "START done\n");
330 return 0;
331 }
332
333 int VideoEncoder::Stop()
334 {
335 if (m_iState == 2)
336 {
337 //AVM_WRITE("Win32 video encoder", "STOP\n");
338 int r = ICCompressEnd(m_HIC);
339 if (r != 0)
340 AVM_WRITE("Win32 video encoder", "ICCompressEnd() failed ( shouldn't happen ), error code %d\n", (int)r);
341 m_iState = 1;
342
343 // Reset MPEG-4 compressor
344
345 if (m_pConfigData && m_iConfigDataSize)
346 ICSetState(m_HIC, m_pConfigData, m_iConfigDataSize);
347 }
348 return 0;
349 }
350
351 const BITMAPINFOHEADER& VideoEncoder::GetOutputFormat() const
352 {
353 return *m_obh; // maybe this is correct
354 }
355
356 size_t VideoEncoder::GetOutputSize() const
357 {
358 int lMaxPackedSize = ICCompressGetSize(m_HIC, m_bh, m_obh);
359 // Work around a bug in Huffyuv. Ben tried to save some memory
360 // and specified a "near-worst-case" bound in the codec instead
361 // of the actual worst case bound. Unfortunately, it's actually
362 // not that hard to exceed the m_HIC's estimate with noisy
363 // captures -- the most common way is accidentally capturing
364 // static from a non-existent channel.
365 //
366 // According to the 2.1.1 comments, Huffyuv uses worst-case
367 // values of 24-bpp for YUY2/UYVY and 40-bpp for RGB, while the
368 // actual worst case values are 43 and 51. We'll compute the
369 // 43/51 value, and use the higher of the two.
370
371 if (m_Info.fourcc == fccHFYU) {
372 int lRealMaxPackedSize = m_obh->biWidth * m_obh->biHeight;
373
374 if (m_bh->biCompression == 0)//BI_RGB
375 lRealMaxPackedSize = (lRealMaxPackedSize * 51) >> 3;
376 else
377 lRealMaxPackedSize = (lRealMaxPackedSize * 43) >> 3;
378
379 if (lRealMaxPackedSize > lMaxPackedSize)
380 lMaxPackedSize = lRealMaxPackedSize;
381
382 }
383 //printf("VideoEncoder::GetOutputSize() %d\n", lMaxPackedSize);
384 return lMaxPackedSize;
385 }
386
387
388 int VideoEncoder::SetQuality(int quality)
389 {
390 int newkey;
391 if (quality < 0 || quality >10000)
392 return -1;
393
394 m_iQuality = quality;
395 //printf("SETQUALITY %d\n", m_iQuality);
396 #if 0
397 if (RegCreateKeyExA(HKEY_CURRENT_USER,
398 "SOFTWARE\\Microsoft\\Scrunch\\Video",
399 0, 0, 0, 0, 0,
400 &newkey, 0) == 0)
401 {
402 RegSetValueExA(newkey, "Quality", 0, REG_DWORD, &quality, 4);
403 }
404 #endif
405 return 0;
406 }
407
408 int VideoEncoder::SetKeyFrame(int freq)
409 {
410 if (freq <= 0)
411 return -1;
412
413 m_iKeyRate = freq;
414 //printf("SETKeyFrame %d\n", m_iKeyRate);
415 return 0;
416 }
417
418 int VideoEncoder::SetFps(float fps)
419 {
420 if (fps <= 0. || fps > 100.)
421 return -1;
422 m_fFps = fps;
423
424 return 0;
425 }
426
427 void VideoEncoder::setDivXRegs(void)
428 {
429 const char* keyname;
430 const char* name = m_Info.dll.c_str();
431 int newkey;
432 int bitrate;
433 int keyframes = 100;
434 int crispness = 100;
435 int count = 4;
436
437 if (strcmp(name, "divxc32.dll") == 0)
438 keyname = "Software\\LinuxLoader\\div3";
439 else if (strcmp(name, "divxc32f.dll") == 0)
440 keyname = "Software\\LinuxLoader\\div4";
441 else if (strcmp(name, "divxcvki.dll") == 0)
442 keyname = "Software\\LinuxLoader\\div5";
443 else if (strcmp(name, "divxcfvk.dll") == 0)
444 keyname = "Software\\LinuxLoader\\div6";
445 else
446 return;
447
448 printf("KEYNAME %s %s\n", name, keyname);
449
450 if (RegOpenKeyExA(HKEY_CURRENT_USER, keyname, 0, 0, &newkey) == 0)
451 {
452 // location in original win32 codec
453 //The Bit Rate at AC6
454 //The Quality/Cripness at 1EA8
455 //The Keyframes at 1EAF
456 //printf("sethandle %s\n", keyname);
457 char* handle = (char*) m_pModule->GetLibHandle();
458 if (RegQueryValueExA(newkey, "BitRate", 0, 0, &m_iBitrate, &count) == 0)
459 {
460 if (handle)
461 {
462 double d = *(double*)(handle + 0x14c0);
463 *(double*)(handle + 0x14c0) = m_iBitrate;
464 AVM_WRITE("Win32 video encoder", "BitRate %d (old: %d)\n", m_iBitrate, (int) d);
465 }
466 bitrate = m_iBitrate;
467 m_iBitrate *= 1000;
468 }
469 else
470 AVM_WRITE("Win32 video encoder", "No 'BitRate' value present\n");
471 if (RegQueryValueExA(newkey, "Crispness", 0, 0, &crispness, &count) == 0)
472 {
473 if (handle)
474 {
475 int a = *(char*)(handle + 0x28a8); //
476 *(long*)(handle + 0x28a8) = crispness;
477 }
478 //printf("Crispness %d (%d)\n", crispness, a);
479 }
480 if (RegQueryValueExA(newkey, "KeyFrames", 0, 0, &keyframes, &count) == 0)
481 {
482 if (handle)
483 {
484 int a = *(char*)(handle + 0x28af); //0x28af
485 *(long*)(handle + 0x28af) = keyframes;
486 printf("KeyFrames %d (%d)\n", keyframes, a);
487 }
488 m_iKeyRate = keyframes;
489 }
490 RegCloseKey(newkey);
491 }
492 else
493 AVM_WRITE("Win32 video encoder", "Could not open key %s\n", keyname);
494
495 #if 0
496 if (RegCreateKeyExA(HKEY_CURRENT_USER,
497 "SOFTWARE\\Microsoft\\Scrunch\\Video",
498 0, 0, 0, 0, 0,
499 &newkey, 0) == 0)
500 {
501 RegSetValueExA(newkey, "BitRate", 0, REG_DWORD, &bitrate, 4);
502 RegSetValueExA(newkey, "KeyFrames", 0, REG_DWORD, &keyframes, 4);
503 //RegSetValueExA(newkey, "Quality", 0, REG_DWORD, &crispness, 4);
504 }
505 RegCloseKey(newkey);
506 #endif
507 }
508
509 AVM_END_NAMESPACE;