bool AVIReadTunnelStream::IsKeyFrame(VDPosition lFrame) { LONG lFrame32 = (LONG)lFrame; if (lFrame32 != lFrame) lFrame32 = lFrame < 0 ? (LONG)0x80000000 : (LONG)0x7FFFFFFF; return !!AVIStreamIsKeyFrame(pas, lFrame32); }
AvisynthVideoProvider::AvisynthVideoProvider(agi::fs::path const& filename, std::string const& colormatrix) { agi::acs::CheckFileRead(filename); std::lock_guard<std::mutex> lock(avs.GetMutex()); #ifdef _WIN32 if (agi::fs::HasExtension(filename, "avi")) { // Try to read the keyframes before actually opening the file as trying // to open the file while it's already open can cause problems with // badly written VFW decoders AVIFileInit(); PAVIFILE pfile; long hr = AVIFileOpen(&pfile, filename.c_str(), OF_SHARE_DENY_WRITE, 0); if (hr) { warning = "Unable to open AVI file for reading keyframes:\n"; switch (hr) { case AVIERR_BADFORMAT: warning += "The file is corrupted, incomplete or has an otherwise bad format."; break; case AVIERR_MEMORY: warning += "The file could not be opened because of insufficient memory."; break; case AVIERR_FILEREAD: warning += "An error occurred reading the file. There might be a problem with the storage media."; break; case AVIERR_FILEOPEN: warning += "The file could not be opened. It might be in use by another application, or you do not have permission to access it."; break; case REGDB_E_CLASSNOTREG: warning += "There is no handler installed for the file extension. This might indicate a fundamental problem in your Video for Windows installation, and can be caused by extremely stripped Windows installations."; break; default: warning += "Unknown error."; break; } goto file_exit; } PAVISTREAM ppavi; if (hr = AVIFileGetStream(pfile, &ppavi, streamtypeVIDEO, 0)) { warning = "Unable to open AVI video stream for reading keyframes:\n"; switch (hr) { case AVIERR_NODATA: warning += "The file does not contain a usable video stream."; break; case AVIERR_MEMORY: warning += "Not enough memory."; break; default: warning += "Unknown error."; break; } goto file_release; } AVISTREAMINFO avis; if (FAILED(AVIStreamInfo(ppavi,&avis,sizeof(avis)))) { warning = "Unable to read keyframes from AVI file:\nCould not get stream information."; goto stream_release; } for (size_t i = 0; i < avis.dwLength; i++) { if (AVIStreamIsKeyFrame(ppavi, i)) keyframes.push_back(i); } // If every frame is a keyframe then just discard the keyframe data as it's useless if (keyframes.size() == (size_t)avis.dwLength) keyframes.clear(); // Clean up stream_release: AVIStreamRelease(ppavi); file_release: AVIFileRelease(pfile); file_exit: AVIFileExit(); } #endif try { auto script = Open(filename); // Check if video was loaded properly if (!script.IsClip() || !script.AsClip()->GetVideoInfo().HasVideo()) throw VideoNotSupported("No usable video found"); vi = script.AsClip()->GetVideoInfo(); if (!vi.IsRGB()) { /// @todo maybe read ColorMatrix hints for d2v files? AVSValue args[2] = { script, "Rec601" }; bool force_bt601 = OPT_GET("Video/Force BT.601")->GetBool() || colormatrix == "TV.601"; bool bt709 = vi.width > 1024 || vi.height >= 600; if (bt709 && (!force_bt601 || colormatrix == "TV.709")) { args[1] = "Rec709"; colorspace = "TV.709"; } else colorspace = "TV.601"; const char *argnames[2] = { 0, "matrix" }; script = avs.GetEnv()->Invoke("ConvertToRGB32", AVSValue(args, 2), argnames); } else colorspace = "None"; RGB32Video = avs.GetEnv()->Invoke("Cache", script).AsClip(); vi = RGB32Video->GetVideoInfo(); fps = (double)vi.fps_numerator / vi.fps_denominator; } catch (AvisynthError const& err) { throw VideoOpenError("Avisynth error: " + std::string(err.msg)); } }
void AVIDump::StoreFrame(const void* data) { if (s_bitmap.biSizeImage > s_stored_frame_size) { void* temp_stored_frame = realloc(s_stored_frame, s_bitmap.biSizeImage); if (temp_stored_frame) { s_stored_frame = temp_stored_frame; } else { free(s_stored_frame); PanicAlertT("Something has gone seriously wrong.\n" "Stopping video recording.\n" "Your video will likely be broken."); Stop(); } s_stored_frame_size = s_bitmap.biSizeImage; memset(s_stored_frame, 0, s_bitmap.biSizeImage); } if (s_stored_frame) { //PanicAlertT("Width: %i, Height: %i, Bit Count: %i", s_bitmap.biWidth, s_bitmap.biHeight, s_bitmap.biBitCount); if (data && (s_file_count || !Movie::cmp_isRunning || s_frame_count > 0)) { bool lastSide = false, readOnly = false; if (Movie::cmp_isRunning && (Movie::cmp_leftFinished || Movie::cmp_rightFinished)) lastSide = true; if (lastSide && Movie::cmp_startTimerFrame > Movie::cmp_curentBranchFrame) //Dragonbane: Combine frames readOnly = true; else readOnly = false; if (readOnly && s_getFrame_temp) { size_t totalBytes = s_bitmap.biSizeImage / 2; size_t rowSize = (s_bitmap.biWidth * (s_bitmap.biBitCount / 8)) / 2; size_t currentByte = 0; if (s_last_key_temp < 2) { BOOL result = AVIStreamIsKeyFrame(s_stream_temp, s_last_key_temp); if (!result) s_last_key_temp = AVIStreamNextKeyFrame(s_stream_temp, s_last_key_temp); } u64 samplePos = AVIStreamFindSample(s_stream_temp, s_last_key_temp, FIND_ANY); u64 s_last_key_old = s_last_key_temp; s_last_key_temp = AVIStreamNextKeyFrame(s_stream_temp, s_last_key_temp); void* s_uncompressed_frame = AVIStreamGetFrame(s_getFrame_temp, samplePos); std::string movie_file_name; if (!s_uncompressed_frame || s_stopTempFile) { //Close current file if (s_getFrame_temp) { AVIStreamGetFrameClose(s_getFrame_temp); s_getFrame_temp = nullptr; } if (s_stream_temp) { AVIStreamClose(s_stream_temp); s_stream_temp = nullptr; } if (s_file_temp) { AVIFileRelease(s_file_temp); s_file_temp = nullptr; movie_file_name = GetCurrDumpFile(tempFileCount, true); if (File::Exists(movie_file_name)) File::Delete(movie_file_name); } //Check if we have another temp file tempFileCount++; s_stopTempFile = false; movie_file_name = GetCurrDumpFile(tempFileCount, true); if (File::Exists(movie_file_name)) //Dragonbane: Open temp file for reading { HRESULT h2 = AVIFileOpenA(&s_file_temp, movie_file_name.c_str(), OF_READ, nullptr); HRESULT h3 = AVIFileGetStream(s_file_temp, &s_stream_temp, streamtypeVIDEO, 0); s_last_key_temp = 0; //Not the first file anymore, so start from keyframe 0 s_getFrame_temp = AVIStreamGetFrameOpen(s_stream_temp, &s_bitmap); if (!s_getFrame_temp) { PanicAlertT("Your chosen compression codec can not be decompressed again! Can't continue video comparison!"); Movie::CancelComparison(); return; } BOOL result = AVIStreamIsKeyFrame(s_stream_temp, s_last_key_temp); if (!result) s_last_key_temp = AVIStreamNextKeyFrame(s_stream_temp, s_last_key_temp); samplePos = AVIStreamFindSample(s_stream_temp, s_last_key_temp, FIND_ANY); s_last_key_old = s_last_key_temp; s_last_key_temp = AVIStreamNextKeyFrame(s_stream_temp, s_last_key_temp); s_uncompressed_frame = AVIStreamGetFrame(s_getFrame_temp, samplePos); if (!s_uncompressed_frame) { //PanicAlertT("Last frame stored. Start timer now!"); Movie::cmp_startTimerFrame = Movie::cmp_curentBranchFrame; memcpy(s_stored_frame, data, s_bitmap.biSizeImage); return; } } else { //PanicAlertT("Last frame stored. Start timer now!"); Movie::cmp_startTimerFrame = Movie::cmp_curentBranchFrame; memcpy(s_stored_frame, data, s_bitmap.biSizeImage); return; } } //Stop temp file on next frame if last frame is processed if (s_last_key_old == s_last_key_temp || AVIStreamFindSample(s_stream_temp, s_last_key_temp, FIND_ANY) == samplePos) s_stopTempFile = true; void* memptr1 = s_uncompressed_frame; memptr1 = static_cast<u8*>(memptr1) + sizeof(BITMAPINFOHEADER); if (Movie::cmp_leftFinished) { memcpy(s_stored_frame, memptr1, s_bitmap.biSizeImage); for (u64 currentRow = 0; currentRow < s_bitmap.biHeight; currentRow++) { currentByte += rowSize; void* memptr = s_stored_frame; const void* memptr2 = data; memptr = static_cast<u8*>(memptr) + currentByte; memptr2 = static_cast<const u8*>(memptr2) + currentByte; memcpy(memptr, memptr2, rowSize); currentByte += rowSize; } } else if (Movie::cmp_rightFinished) { memcpy(s_stored_frame, memptr1, s_bitmap.biSizeImage); //BITMAPINFOHEADER test; //memset(&test, 0, sizeof(BITMAPINFOHEADER)); //memcpy(&test, s_uncompressed_frame, sizeof(BITMAPINFOHEADER)); for (u64 currentRow = 0; currentRow < s_bitmap.biHeight; currentRow++) { void* memptr = s_stored_frame; const void* memptr2 = data; memptr = static_cast<u8*>(memptr) + currentByte; memptr2 = static_cast<const u8*>(memptr2) + currentByte; memcpy(memptr, memptr2, rowSize); currentByte += rowSize * 2; } } else { memcpy(s_stored_frame, data, s_bitmap.biSizeImage); } } else { memcpy(s_stored_frame, data, s_bitmap.biSizeImage); } } else // pitch black frame { memset(s_stored_frame, 0, s_bitmap.biSizeImage); } } }