Fix few uninitialized variables and a stack smash in AVI dumper
[lsnes.git] / src / video / avi / writer.cpp
blob9f46d07e813461d28ac7b28d329e4b203da921c8
1 #include "video/avi/writer.hpp"
2 #include "core/framerate.hpp"
3 #include "core/messages.hpp"
4 #include <sstream>
5 #include <iomanip>
7 avi_writer::~avi_writer()
9 try {
10 if(!closed)
11 close();
12 } catch(...) {
16 std::deque<frame_object>& avi_writer::video_queue()
18 return vqueue;
21 sample_queue& avi_writer::audio_queue()
23 return aqueue;
26 void avi_writer::flush()
28 flush(false);
31 void avi_writer::close()
33 flush(true);
34 aviout.end();
35 avifile.close();
36 closed = true;
37 curwidth = curheight = curfps_n = curfps_d = 0;
40 void avi_writer::flush(bool force)
42 do_again:
43 if(vqueue.empty())
44 return;
45 bool sbreak = false;
46 if(closed)
47 sbreak = true; //Start first segment.
48 if(aviout.get_size_estimate() > 2100000000)
49 sbreak = true; //Break due to size.
50 struct frame_object& f = vqueue.front();
51 if(f.force_break)
52 sbreak = true; //Manual force break.
53 if(f.width != curwidth || f.height != curheight || f.fps_n != curfps_n || f.fps_d != curfps_d)
54 sbreak = true; //Break due resolution / rate change.
55 if(sbreak) {
56 if(!closed) {
57 aviout.end();
58 avifile.close();
59 closed = true;
61 curwidth = f.width;
62 curheight = f.height;
63 curfps_n = f.fps_n;
64 curfps_d = f.fps_d;
65 std::string aviname;
67 std::ostringstream x;
68 x << prefix << "_" << std::setw(5) << std::setfill('0') << next_segment << ".avi";
69 aviname = x.str();
71 avifile.open(aviname, std::ios::out | std::ios::binary);
72 if(!avifile)
73 throw std::runtime_error("Can't open '" + aviname + "'");
74 next_segment++;
75 aviout.start(avifile, vcodec, acodec, curwidth, curheight, curfps_n, curfps_d, samplerate,
76 channels);
77 closed = false;
78 messages << "Start AVI: " << curwidth << "x" << curheight << "@" << curfps_n << "/" << curfps_d
79 << " to '" << aviname << "'" << std::endl;
81 uint64_t t = framerate_regulator::get_utime();
82 if(aviout.readqueue(f.data, f.odata, f.stride, aqueue, force)) {
83 t = framerate_regulator::get_utime() - t;
84 if(t > 20000)
85 std::cerr << "aviout.readqueue took " << t << std::endl;
86 vqueue.pop_front();
87 goto do_again;
91 avi_writer::avi_writer(const std::string& _prefix, struct avi_video_codec& _vcodec, struct avi_audio_codec& _acodec,
92 uint32_t _samplerate, uint16_t _audiochannels)
93 : vcodec(_vcodec), acodec(_acodec)
95 prefix = _prefix;
96 closed = true;
97 next_segment = 0;
98 samplerate = _samplerate;
99 channels = _audiochannels;
100 curwidth = curheight = curfps_n = curfps_d = 0;