- cv::VideoWriter being bipolar
- #include <opencv/highgui.h>
- #include <iostream>
- int main(int argc, char* argv[])
- {
- if (argc < 3) { return -1; }
- cv::VideoCapture cap(argv[1]);
- std::string str = argv[2];
- int fourcc = CV_FOURCC('j', 'p', 'e', 'g');
- double fps = cap.get(CV_CAP_PROP_FPS);
- int width = int(cap.get(CV_CAP_PROP_FRAME_WIDTH));
- int height = int(cap.get(CV_CAP_PROP_FRAME_HEIGHT));
- // COMMENT OUT THIS BLOCK TO MAKE THE PROGRAM CRASH
- // This code creates a variable on the stack and preforms
- // some operations with it. This somehow alligns the memory
- // for the call "cv::VideoWriter vw(...)" in a way that
- // libavcoded expects and makes the call "vw << img" not crash.
- // Note that these variables are never used outside of this block.
- char * name_c = new char[100];
- strcpy(name_c, str.c_str());
- delete[] name_c;
- // END COMMENT OUT THIS BLOCK TO MAKE THE PROGRAM CRASH
- cv::VideoWriter vw(
- str,
- fourcc,
- fps,
- cv::Size(width, height));
- int fnum = 0;
- cv::Mat img;
- while(cap.grab() && cap.retrieve(img) && fnum < 30)
- {
- cv::imshow("img", img);
- vw << img;
- ++fnum;
- }
- return 0;
- }
- cv::VideoWriter vw(
- str,
- fourcc,
- fps,
- cv::Size(width, height));
- 00401290 fld qword ptr [esp+50h]
- 00401294 add esp,4
- 00401297 push 1
- 00401299 sub esp,8
- 0040129C mov eax,esp
- 0040129E mov dword ptr [esp+60h],esp
- 004012A2 sub esp,8
- 004012A5 fstp qword ptr [esp]
- 004012A8 mov dword ptr [eax],edi
- 004012AA mov dword ptr [eax+4],ebp
- 004012AD push 6765706Ah
- 004012B2 lea eax,[esp+0A8h]
- 004012B9 push eax
- 004012BA lea ecx,[esp+5Ch]
- 004012BE call cv::VideoWriter::VideoWriter (401454h)
- ...
- vw << img;
- 00401353 mov edx,dword ptr [esp+40h]
- 00401357 mov eax,dword ptr [edx+0Ch]
- 0040135A lea ecx,[esp+20h]
- 0040135E push ecx
- 0040135F lea ecx,[esp+44h]
- 00401363 call eax
- 00401365 lea ecx,[esp+14h]
- cv::VideoWriter vw(
- str,
- fourcc,
- fps,
- cv::Size(width, height));
- 00401253 fld qword ptr [esp+44h]
- 00401257 push 1
- 00401259 sub esp,8
- 0040125C mov ecx,esp
- 0040125E mov dword ptr [esp+58h],esp
- 00401262 sub esp,8
- 00401265 fstp qword ptr [esp]
- 00401268 mov dword ptr [ecx+4],eax
- 0040126B push 6765706Ah
- 00401270 lea eax,[esp+0A0h]
- 00401277 mov dword ptr [ecx],edi
- 00401279 push eax
- 0040127A lea ecx,[esp+54h]
- 0040127E call cv::VideoWriter::VideoWriter (401454h)
- ...
- vw << img;
- 00401313 mov edx,dword ptr [esp+38h]
- 00401317 mov eax,dword ptr [edx+0Ch]
- 0040131A lea ecx,[esp+18h]
- 0040131E push ecx
- 0040131F lea ecx,[esp+3Ch]
- 00401323 call eax
- 00401325 lea ecx,[esp+0Ch]
- // Working (with extra block) Crashes (without extra block)
- 00401290 fld qword ptr [esp+50h] 00401253 fld qword ptr [esp+44h]
- *** 00401294 add esp,4
- 00401297 push 1 00401257 push 1
- ...
- 004012A5 fstp qword ptr [esp] 00401265 fstp qword ptr [esp]
- *** 004012A8 mov dword ptr [eax],edi 00401268 mov dword ptr [ecx+4],eax
- *** 004012AA mov dword ptr [eax+4],ebp 0040126B push 6765706Ah
- *** 004012AD push 6765706Ah 00401270 lea eax,[esp+0A0h]
- 004012B2 lea eax,[esp+0A8h] 00401277 mov dword ptr [ecx],edi
- 004012B9 push eax 00401279 push eax
- #include <opencv/highgui.h>
- void save(const std::string & str, cv::VideoCapture & cap)
- {
- int fourcc = int(cap.get(CV_CAP_PROP_FOURCC));
- double fps = cap.get(CV_CAP_PROP_FPS);
- int width = int(cap.get(CV_CAP_PROP_FRAME_WIDTH));
- int height = int(cap.get(CV_CAP_PROP_FRAME_HEIGHT));
- cv::VideoWriter vw(
- str,
- fourcc,
- fps,
- cv::Size(width, height));
- int fnum = 0;
- cv::Mat img;
- while(cap.grab() && cap.retrieve(img) && fnum < 30)
- {
- cv::imshow("img", img);
- cv::waitKey(1);
- vw << img;
- ++fnum;
- }
- }
- int main(int argc, char* argv[])
- {
- if (argc < 3) { return -1; }
- cv::VideoCapture cap(argv[1]);
- std::string str = argv[2];
- save(str, cap);
- /*
- int fourcc = int(cap.get(CV_CAP_PROP_FOURCC));
- double fps = cap.get(CV_CAP_PROP_FPS);
- int width = int(cap.get(CV_CAP_PROP_FRAME_WIDTH));
- int height = int(cap.get(CV_CAP_PROP_FRAME_HEIGHT));
- cv::VideoWriter vw(
- str,
- fourcc,
- fps,
- cv::Size(width, height));
- int fnum = 0;
- cv::Mat img;
- while(cap.grab() && cap.retrieve(img) && fnum < 30)
- {
- cv::imshow("img", img);
- cv::waitKey(1);
- vw << img;
- ++fnum;
- }
- */
- return 0;
- }