summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/recording.cpp
blob: e7cf3d886781ddf83fa650cf44f90e71a1cd77d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    recording.cpp

    Core MAME video routines.

***************************************************************************/

#include "emu.h"
#include "screen.h"
#include "aviio.h"
#include "png.h"


namespace
{
	class avi_movie_recording : public movie_recording
	{
	public:
		avi_movie_recording(screen_device *screen)
			: movie_recording(screen)
		{
		}

		bool initialize(running_machine &machine, std::unique_ptr<emu_file> &&file, int32_t width, int32_t height);
		virtual bool add_sound_to_recording(const s16 *sound, int numsamples) override;

	protected:
		virtual bool append_single_video_frame(bitmap_rgb32 &bitmap, const rgb_t *palette, int palette_entries) override;

	private:
		avi_file::ptr       m_avi_file;                 // handle to the open movie file
	};


	class mng_movie_recording : public movie_recording
	{
	public:
		mng_movie_recording(screen_device *screen, std::map<std::string, std::string> &&info_fields);
		~mng_movie_recording();

		bool initialize(std::unique_ptr<emu_file> &&file, bitmap_t &snap_bitmap);
		virtual bool add_sound_to_recording(const s16 *sound, int numsamples) override;

	protected:
		virtual bool append_single_video_frame(bitmap_rgb32 &bitmap, const rgb_t *palette, int palette_entries) override;

	private:
		std::unique_ptr<emu_file>           m_mng_file;           // handle to the open movie file
		std::map<std::string, std::string>  m_info_fields;
	};
};


//**************************************************************************
//  MOVIE RECORDING
//**************************************************************************

//-------------------------------------------------
//  movie_recording - constructor
//-------------------------------------------------

movie_recording::movie_recording(screen_device *screen)
	: m_screen(screen)
	, m_frame(0)
{
	m_frame_period = m_screen ? m_screen->frame_period() : attotime::from_hz(screen_device::DEFAULT_FRAME_RATE);
}


//-------------------------------------------------
//  movie_recording - destructor
//-------------------------------------------------

movie_recording::~movie_recording()
{
}


//-------------------------------------------------
//  movie_recording::append_video_frame
//-------------------------------------------------

bool movie_recording::append_video_frame(bitmap_rgb32 &bitmap, attotime curtime)
{
	// identify the palette
	bool has_palette = screen() && screen()->has_palette();
	const rgb_t *palette = has_palette ? screen()->palette().palette()->entry_list_adjusted() : nullptr;
	int palette_entries = has_palette ? screen()->palette().entries() : 0;

	// keep appending frames until we're at curtime
	while (next_frame_time() <= curtime)
	{
		// append this bitmap as a single frame
		if (!append_single_video_frame(bitmap, palette, palette_entries))
			return false;
		m_frame++;

		// advance time
		set_next_frame_time(next_frame_time() + frame_period());
	}
	return true;
}


//-------------------------------------------------
//  movie_recording::create - creates a new recording
//  for the specified format
//-------------------------------------------------

movie_recording::ptr movie_recording::create(running_machine &machine, screen_device *screen, format fmt, std::unique_ptr<emu_file> &&file, bitmap_rgb32 &snap_bitmap)
{
	movie_recording::ptr result;
	switch (fmt)
	{
	case movie_recording::format::AVI:
		{
			auto avi_recording = std::make_unique<avi_movie_recording>(screen);
			if (avi_recording->initialize(machine, std::move(file), snap_bitmap.width(), snap_bitmap.height()))
				result = std::move(avi_recording);
		}
		break;

	case movie_recording::format::MNG:
		{
			std::map<std::string, std::string> info_fields;
			info_fields["Software"] = std::string(emulator_info::get_appname()).append(" ").append(emulator_info::get_build_version());
			info_fields["System"] = std::string(machine.system().manufacturer).append(" ").append(machine.system().type.fullname());

			auto mng_recording = std::make_unique<mng_movie_recording>(screen, std::move(info_fields));
			if (mng_recording->initialize(std::move(file), snap_bitmap))
				result = std::move(mng_recording);
		}
		break;

	default:
		throw false;
	}

	// if we successfully create a recording, set the current time and return it
	if (result)
		result->set_next_frame_time(machine.time());
	return result;
}


//-------------------------------------------------
//  movie_recording::format_file_extension
//-------------------------------------------------

const char *movie_recording::format_file_extension(movie_recording::format fmt)
{
	switch (fmt)
	{
		case format::AVI:   return "avi";
		case format::MNG:   return "mng";
		default:            throw false;
	}
}


//-------------------------------------------------
//  avi_movie_recording::initialize
//-------------------------------------------------

bool avi_movie_recording::initialize(running_machine &machine, std::unique_ptr<emu_file> &&file, int32_t width, int32_t height)
{
	// we only use the file we're passed to get the full path
	std::string fullpath = file->fullpath();
	file.reset();

	// build up information about this new movie
	avi_file::movie_info info;
	info.video_format = 0;
	info.video_timescale = 1000 * frame_period().as_hz();
	info.video_sampletime = 1000;
	info.video_numsamples = 0;
	info.video_width = width;
	info.video_height = height;
	info.video_depth = 24;

	info.audio_format = 0;
	info.audio_timescale = machine.sample_rate();
	info.audio_sampletime = 1;
	info.audio_numsamples = 0;
	info.audio_channels = 2;
	info.audio_samplebits = 16;
	info.audio_samplerate = machine.sample_rate();

	// create the file
	avi_file::error avierr = avi_file::create(fullpath, info, m_avi_file);
	if (avierr != avi_file::error::NONE)
		osd_printf_error("Error creating AVI: %s\n", avi_file::error_string(avierr));
	return avierr == avi_file::error::NONE;
}


//-------------------------------------------------
//  avi_movie_recording::append_single_video_frame
//-------------------------------------------------

bool avi_movie_recording::append_single_video_frame(bitmap_rgb32 &bitmap, const rgb_t *palette, int palette_entries)
{
	avi_file::error avierr = m_avi_file->append_video_frame(bitmap);
	return avierr == avi_file::error::NONE;
}


//-------------------------------------------------
//  avi_movie_recording::append_video_frame
//-------------------------------------------------

 bool avi_movie_recording::add_sound_to_recording(const s16 *sound, int numsamples)
{
	g_profiler.start(PROFILER_MOVIE_REC);

	// write the next frame
	avi_file::error avierr = m_avi_file->append_sound_samples(0, sound + 0, numsamples, 1);
	if (avierr == avi_file::error::NONE)
		avierr = m_avi_file->append_sound_samples(1, sound + 1, numsamples, 1);

	g_profiler.stop();
	return avierr == avi_file::error::NONE;
}


//-------------------------------------------------
//  mng_movie_recording - constructor
//-------------------------------------------------

mng_movie_recording::mng_movie_recording(screen_device *screen, std::map<std::string, std::string> &&info_fields)
	: movie_recording(screen)
	, m_info_fields(std::move(info_fields))
{
}


//-------------------------------------------------
//  mng_movie_recording - destructor
//-------------------------------------------------

mng_movie_recording::~mng_movie_recording()
{
	if (m_mng_file)
		mng_capture_stop(*m_mng_file);
}


//-------------------------------------------------
//  mng_movie_recording::initialize
//-------------------------------------------------

bool mng_movie_recording::initialize(std::unique_ptr<emu_file> &&file, bitmap_t &snap_bitmap)
{
	m_mng_file = std::move(file);
	png_error pngerr = mng_capture_start(*m_mng_file, snap_bitmap, frame_period().as_hz());
	if (pngerr != PNGERR_NONE)
		osd_printf_error("Error capturing MNG, png_error=%d\n", pngerr);
	return pngerr == PNGERR_NONE;
}


//-------------------------------------------------
//  mng_movie_recording::append_single_video_frame
//-------------------------------------------------

bool mng_movie_recording::append_single_video_frame(bitmap_rgb32 &bitmap, const rgb_t *palette, int palette_entries)
{
	// set up the text fields in the movie info
	png_info pnginfo;
	if (current_frame() == 0)
	{
		for (auto &ent : m_info_fields)
			pnginfo.add_text(ent.first.c_str(), ent.second.c_str());
	}

	png_error error = mng_capture_frame(*m_mng_file, pnginfo, bitmap, palette_entries, palette);
	return error == png_error::PNGERR_NONE;
}


//-------------------------------------------------
//  mng_movie_recording::add_sound_to_recording
//-------------------------------------------------

bool mng_movie_recording::add_sound_to_recording(const s16 *sound, int numsamples)
{
	// not supported; do nothing
	return true;
}