VDAVIOutputSegmentedStream::FlushResult VDAVIOutputSegmentedVideoStream::Flush(uint32 samples, bool force) { if (!samples) return kFlushOK; bool gotall = true; if (samples > mScheduledSamples) { if (!force || !mScheduledSamples) { if (mbEnded && mPendingRuns.empty()) return kFlushEnded; else return kFlushPending; } gotall = false; samples = mScheduledSamples; } samples += mExtraSamples; while(samples > 0) { Run& run = mClearedRuns.front(); while(!run.mBlocks.empty() && samples) { Block& block = run.mBlocks.front(); mpOutputStream->write(block.flags, block.data, block.size, 1); delete[] (char *)block.data; run.mBlocks.pop_front(); --samples; --mScheduledSamples; --mBufferedSamples; } if (run.mBlocks.empty()) mClearedRuns.pop_front(); } mExtraSamples = samples; return kFlushOK; }
void VDCreateTestPal8Video(VDGUIHandle h) { CPUEnableExtensions(CPUCheckForExtensions()); try { tVDInputDrivers inputDrivers; std::vector<int> xlat; VDGetInputDrivers(inputDrivers, IVDInputDriver::kF_Video); const VDStringW filter(VDMakeInputDriverFileFilter(inputDrivers, xlat)); const VDFileDialogOption opt[]={ { VDFileDialogOption::kSelectedFilter }, 0 }; int optval[1]={0}; const VDStringW srcfile(VDGetLoadFileName('pl8s', h, L"Choose source file", filter.c_str(), NULL, opt, optval)); if (srcfile.empty()) return; IVDInputDriver *pDrv; int filtidx = xlat[optval[0] - 1]; if (filtidx < 0) pDrv = VDAutoselectInputDriverForFile(srcfile.c_str(), IVDInputDriver::kF_Video); else { tVDInputDrivers::iterator itDrv(inputDrivers.begin()); std::advance(itDrv, filtidx); pDrv = *itDrv; } vdrefptr<InputFile> pIF(pDrv->CreateInputFile(0)); pIF->Init(srcfile.c_str()); const VDStringW dstfile(VDGetSaveFileName('pl8d', h, L"Choose destination 8-bit file", L"Audio-video interleaved (*.avi)\0*.avi\0All files\0*.*", L"avi", NULL, NULL)); if (dstfile.empty()) return; vdrefptr<IVDVideoSource> pVS; pIF->GetVideoSource(0, ~pVS); IVDStreamSource *pVSS = pVS->asStream(); const VDPosition frames = pVSS->getLength(); if (!pVS->setTargetFormat(nsVDPixmap::kPixFormat_XRGB8888)) throw MyError("Cannot set decompression format to 32-bit."); vdautoptr<IVDMediaOutputAVIFile> pOut(VDCreateMediaOutputAVIFile()); IVDMediaOutputStream *pVSOut = pOut->createVideoStream(); const VDPixmap& pxsrc = pVS->getTargetFormat(); const uint32 rowbytes = (pxsrc.w+3) & ~3; AVIStreamHeader_fixed hdr; hdr.fccType = 'sdiv'; hdr.fccHandler = 0; hdr.dwFlags = 0; hdr.wPriority = 0; hdr.wLanguage = 0; hdr.dwScale = pVSS->getStreamInfo().dwScale; hdr.dwRate = pVSS->getStreamInfo().dwRate; hdr.dwStart = 0; hdr.dwLength = 0; hdr.dwInitialFrames = 0; hdr.dwSuggestedBufferSize = 0; hdr.dwQuality = -1; hdr.dwSampleSize = 0; hdr.rcFrame.left = 0; hdr.rcFrame.top = 0; hdr.rcFrame.right = (short)pxsrc.w; hdr.rcFrame.bottom = (short)pxsrc.h; pVSOut->setStreamInfo(hdr); vdstructex<BITMAPINFOHEADER> bih; bih.resize(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*252); bih->biSize = sizeof(BITMAPINFOHEADER); bih->biWidth = pxsrc.w; bih->biHeight = pxsrc.h; bih->biPlanes = 1; bih->biBitCount = 8; bih->biCompression = BI_RGB; bih->biSizeImage = rowbytes*pxsrc.h; bih->biXPelsPerMeter = 0; bih->biYPelsPerMeter = 0; bih->biClrUsed = 252; bih->biClrImportant = 252; RGBQUAD *pal = (RGBQUAD *)((char *)bih.data() + sizeof(BITMAPINFOHEADER)); for(int i=0; i<252; ++i) { pal[i].rgbRed = (BYTE)((i/42)*51); pal[i].rgbGreen = (BYTE)((((i/6)%7)*85)>>1); pal[i].rgbBlue = (BYTE)((i%6)*51); pal[i].rgbReserved = 0; } pVSOut->setFormat(bih.data(), bih.size()); pOut->init(dstfile.c_str()); ProgressDialog dlg((HWND)h, "Processing video stream", "Palettizing frames", (long)frames, true); vdblock<uint8> outbuf(rowbytes * pxsrc.h); const vdpixsize w = pxsrc.w; const vdpixsize h = pxsrc.h; try { for(uint32 frame=0; frame<frames; ++frame) { pVS->getFrame(frame); const uint8 *src = (const uint8 *)pxsrc.data; ptrdiff_t srcpitch = pxsrc.pitch; uint8 *dst = &outbuf[rowbytes * (pxsrc.h - 1)]; for(int y=0; y<h; ++y) { const uint8 *dr = ditherred[y & 15]; const uint8 *dg = dithergrn[y & 15]; const uint8 *db = ditherblu[y & 15]; for(int x=0; x<w; ++x) { const uint8 b = (uint8)((((src[0] * 1286)>>8) + dr[x&15]) >> 8); const uint8 g = (uint8)((((src[1] * 1543)>>8) + dg[x&15]) >> 8); const uint8 r = (uint8)((((src[2] * 1286)>>8) + db[x&15]) >> 8); src += 4; dst[x] = (uint8)(r*42 + g*6 + b); } vdptrstep(dst, -(ptrdiff_t)rowbytes); vdptrstep(src, srcpitch - w*4); } pVSOut->write(AVIOutputStream::kFlagKeyFrame, outbuf.data(), outbuf.size(), 1); dlg.advance(frame); dlg.check(); } } catch(const MyUserAbortError&) { } pVSOut->flush(); pOut->finalize(); } catch(const MyError& e) { e.post((HWND)h, g_szError); } }