1 | /** @file
|
---|
2 | *
|
---|
3 | * Framebuffer implementation that interfaces with FFmpeg
|
---|
4 | * to create a video of the guest.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #define LOG_GROUP LOG_GROUP_GUI
|
---|
20 |
|
---|
21 | #include "FFmpegFB.h"
|
---|
22 |
|
---|
23 | #include <iprt/file.h>
|
---|
24 | #include <iprt/param.h>
|
---|
25 | #include <iprt/assert.h>
|
---|
26 | #include <VBox/log.h>
|
---|
27 | #include <png.h>
|
---|
28 | #include <iprt/stream.h>
|
---|
29 |
|
---|
30 | #define VBOX_SHOW_AVAILABLE_FORMATS
|
---|
31 |
|
---|
32 | // external constructor for dynamic loading
|
---|
33 | /////////////////////////////////////////////////////////////////////////////
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Callback function to register an ffmpeg framebuffer.
|
---|
37 | *
|
---|
38 | * @returns COM status code.
|
---|
39 | * @param width Framebuffer width.
|
---|
40 | * @param height Framebuffer height.
|
---|
41 | * @param bitrate Bitrate of mpeg file to be created.
|
---|
42 | * @param filename Name of mpeg file to be created
|
---|
43 | * @retval retVal The new framebuffer
|
---|
44 | */
|
---|
45 | extern "C" DECLEXPORT(HRESULT) VBoxRegisterFFmpegFB(ULONG width,
|
---|
46 | ULONG height, ULONG bitrate,
|
---|
47 | com::Bstr filename,
|
---|
48 | IFramebuffer **retVal)
|
---|
49 | {
|
---|
50 | Log2(("VBoxRegisterFFmpegFB: called\n"));
|
---|
51 | FFmpegFB *pFramebuffer = new FFmpegFB(width, height, bitrate, filename);
|
---|
52 | int rc = pFramebuffer->init();
|
---|
53 | AssertMsg(rc == S_OK,
|
---|
54 | ("failed to initialise the FFmpeg framebuffer, rc = %d\n",
|
---|
55 | rc));
|
---|
56 | if (rc == S_OK)
|
---|
57 | {
|
---|
58 | *retVal = pFramebuffer;
|
---|
59 | return S_OK;
|
---|
60 | }
|
---|
61 | delete pFramebuffer;
|
---|
62 | return rc;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | // constructor / destructor
|
---|
70 | /////////////////////////////////////////////////////////////////////////////
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Perform parts of initialisation which are guaranteed not to fail
|
---|
74 | * unless we run out of memory. In this case, we just set the guest
|
---|
75 | * buffer to 0 so that RequestResize() does not free it the first time
|
---|
76 | * it is called.
|
---|
77 | */
|
---|
78 | #ifdef VBOX_WITH_VPX
|
---|
79 | FFmpegFB::FFmpegFB(ULONG width, ULONG height, ULONG bitrate,
|
---|
80 | com::Bstr filename) :
|
---|
81 | mfUrlOpen(false),
|
---|
82 | mBitRate(bitrate),
|
---|
83 | mPixelFormat(FramebufferPixelFormat_Opaque),
|
---|
84 | mBitsPerPixel(0),
|
---|
85 | mFileName(filename),
|
---|
86 | mBytesPerLine(0),
|
---|
87 | mFrameWidth(width), mFrameHeight(height),
|
---|
88 | mYUVFrameSize(width * height * 3 / 2),
|
---|
89 | mRGBBuffer(0),
|
---|
90 | mOutOfMemory(false), mToggle(false)
|
---|
91 | #else
|
---|
92 | FFmpegFB::FFmpegFB(ULONG width, ULONG height, ULONG bitrate,
|
---|
93 | com::Bstr filename) :
|
---|
94 | mpFormatContext(0), mpStream(0),
|
---|
95 | mfUrlOpen(false),
|
---|
96 | mBitRate(bitrate),
|
---|
97 | mPixelFormat(FramebufferPixelFormat_Opaque),
|
---|
98 | mBitsPerPixel(0),
|
---|
99 | mFileName(filename),
|
---|
100 | mBytesPerLine(0),
|
---|
101 | mFrameWidth(width), mFrameHeight(height),
|
---|
102 | mYUVFrameSize(width * height * 3 / 2),mRGBBuffer(0),
|
---|
103 | mOutOfMemory(false), mToggle(false)
|
---|
104 | #endif
|
---|
105 | {
|
---|
106 | ULONG cPixels = width * height;
|
---|
107 |
|
---|
108 | #ifdef VBOX_WITH_VPX
|
---|
109 | Assert(width % 2 == 0 && height % 2 == 0);
|
---|
110 | /* For temporary RGB frame we allocate enough memory to deal with
|
---|
111 | RGB16 to RGB32 */
|
---|
112 | mTempRGBBuffer = reinterpret_cast<uint8_t *>(RTMemAlloc(cPixels * 4));
|
---|
113 | if (!mTempRGBBuffer)
|
---|
114 | goto nomem_temp_rgb_buffer;
|
---|
115 | mYUVBuffer = reinterpret_cast<uint8_t *>(RTMemAlloc(mYUVFrameSize));
|
---|
116 | if (!mYUVBuffer)
|
---|
117 | goto nomem_yuv_buffer;
|
---|
118 | return;
|
---|
119 |
|
---|
120 | /* C-based memory allocation and how to deal with it in C++ :) */
|
---|
121 | nomem_yuv_buffer:
|
---|
122 | Log(("Failed to allocate memory for mYUVBuffer\n"));
|
---|
123 | RTMemFree(mYUVBuffer);
|
---|
124 | nomem_temp_rgb_buffer:
|
---|
125 | Log(("Failed to allocate memory for mTempRGBBuffer\n"));
|
---|
126 | RTMemFree(mTempRGBBuffer);
|
---|
127 | mOutOfMemory = true;
|
---|
128 | #else
|
---|
129 | LogFlow(("Creating FFmpegFB object %p, width=%lu, height=%lu\n",
|
---|
130 | this, (unsigned long) width, (unsigned long) height));
|
---|
131 | Assert(width % 2 == 0 && height % 2 == 0);
|
---|
132 | /* For temporary RGB frame we allocate enough memory to deal with
|
---|
133 | RGB16 to RGB32 */
|
---|
134 | mTempRGBBuffer = reinterpret_cast<uint8_t *>(av_malloc(cPixels * 4));
|
---|
135 | if (!mTempRGBBuffer)
|
---|
136 | goto nomem_temp_rgb_buffer;
|
---|
137 | mYUVBuffer = reinterpret_cast<uint8_t *>(av_malloc(mYUVFrameSize));
|
---|
138 | if (!mYUVBuffer)
|
---|
139 | goto nomem_yuv_buffer;
|
---|
140 | mFrame = avcodec_alloc_frame();
|
---|
141 | if (!mFrame)
|
---|
142 | goto nomem_mframe;
|
---|
143 | mOutBuf = reinterpret_cast<uint8_t *>(av_malloc(mYUVFrameSize * 2));
|
---|
144 | if (!mOutBuf)
|
---|
145 | goto nomem_moutbuf;
|
---|
146 |
|
---|
147 | return;
|
---|
148 |
|
---|
149 | /* C-based memory allocation and how to deal with it in C++ :) */
|
---|
150 | nomem_moutbuf:
|
---|
151 | Log(("Failed to allocate memory for mOutBuf\n"));
|
---|
152 | av_free(mFrame);
|
---|
153 | nomem_mframe:
|
---|
154 | Log(("Failed to allocate memory for mFrame\n"));
|
---|
155 | av_free(mYUVBuffer);
|
---|
156 | nomem_yuv_buffer:
|
---|
157 | Log(("Failed to allocate memory for mYUVBuffer\n"));
|
---|
158 | av_free(mTempRGBBuffer);
|
---|
159 | nomem_temp_rgb_buffer:
|
---|
160 | Log(("Failed to allocate memory for mTempRGBBuffer\n"));
|
---|
161 | mOutOfMemory = true;
|
---|
162 | #endif
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Write the last frame to disk and free allocated memory
|
---|
168 | */
|
---|
169 | FFmpegFB::~FFmpegFB()
|
---|
170 | {
|
---|
171 | LogFlow(("Destroying FFmpegFB object %p\n", this));
|
---|
172 | #ifdef VBOX_WITH_VPX
|
---|
173 | /* Dummy update to make sure we get all the frame (timing). */
|
---|
174 | NotifyUpdate(0, 0, 0, 0);
|
---|
175 | /* Write the last pending frame before exiting */
|
---|
176 | int rc = do_rgb_to_yuv_conversion();
|
---|
177 | if (rc == S_OK)
|
---|
178 | VideoRecEncodeAndWrite(pVideoRecContext, mFrameWidth, mFrameHeight, mYUVBuffer);
|
---|
179 | # if 1
|
---|
180 | /* Add another 10 seconds. */
|
---|
181 | for (int i = 10*25; i > 0; i--)
|
---|
182 | VideoRecEncodeAndWrite(pVideoRecContext, mFrameWidth, mFrameHeight, mYUVBuffer);
|
---|
183 | # endif
|
---|
184 | VideoRecContextClose(pVideoRecContext);
|
---|
185 | RTCritSectDelete(&mCritSect);
|
---|
186 |
|
---|
187 | /* We have already freed the stream above */
|
---|
188 | if (mTempRGBBuffer)
|
---|
189 | RTMemFree(mTempRGBBuffer);
|
---|
190 | if (mYUVBuffer)
|
---|
191 | RTMemFree(mYUVBuffer);
|
---|
192 | if (mRGBBuffer)
|
---|
193 | RTMemFree(mRGBBuffer);
|
---|
194 | #else
|
---|
195 | if (mpFormatContext != 0)
|
---|
196 | {
|
---|
197 | if (mfUrlOpen)
|
---|
198 | {
|
---|
199 | /* Dummy update to make sure we get all the frame (timing). */
|
---|
200 | NotifyUpdate(0, 0, 0, 0);
|
---|
201 | /* Write the last pending frame before exiting */
|
---|
202 | int rc = do_rgb_to_yuv_conversion();
|
---|
203 | if (rc == S_OK)
|
---|
204 | do_encoding_and_write();
|
---|
205 | # if 1
|
---|
206 | /* Add another 10 seconds. */
|
---|
207 | for (int i = 10*25; i > 0; i--)
|
---|
208 | do_encoding_and_write();
|
---|
209 | # endif
|
---|
210 | /* write a png file of the last frame */
|
---|
211 | write_png();
|
---|
212 | avcodec_close(mpStream->codec);
|
---|
213 | av_write_trailer(mpFormatContext);
|
---|
214 | /* free the streams */
|
---|
215 | for(unsigned i = 0; i < (unsigned)mpFormatContext->nb_streams; i++) {
|
---|
216 | av_freep(&mpFormatContext->streams[i]->codec);
|
---|
217 | av_freep(&mpFormatContext->streams[i]);
|
---|
218 | }
|
---|
219 | /* Changed sometime between 50.5.0 and 52.7.0 */
|
---|
220 | # if LIBAVFORMAT_VERSION_INT >= (52 << 16)
|
---|
221 | url_fclose(mpFormatContext->pb);
|
---|
222 | # else /* older version */
|
---|
223 | url_fclose(&mpFormatContext->pb);
|
---|
224 | # endif /* older version */
|
---|
225 | }
|
---|
226 | av_free(mpFormatContext);
|
---|
227 | }
|
---|
228 | RTCritSectDelete(&mCritSect);
|
---|
229 | /* We have already freed the stream above */
|
---|
230 | mpStream = 0;
|
---|
231 | if (mTempRGBBuffer)
|
---|
232 | av_free(mTempRGBBuffer);
|
---|
233 | if (mYUVBuffer)
|
---|
234 | av_free(mYUVBuffer);
|
---|
235 | if (mFrame)
|
---|
236 | av_free(mFrame);
|
---|
237 | if (mOutBuf)
|
---|
238 | av_free(mOutBuf);
|
---|
239 | if (mRGBBuffer)
|
---|
240 | RTMemFree(mRGBBuffer);
|
---|
241 | #endif
|
---|
242 | }
|
---|
243 |
|
---|
244 | // public methods only for internal purposes
|
---|
245 | /////////////////////////////////////////////////////////////////////////////
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Perform any parts of the initialisation which could potentially fail
|
---|
249 | * for reasons other than "out of memory".
|
---|
250 | *
|
---|
251 | * @returns COM status code
|
---|
252 | * @param width width to be used for MPEG frame framebuffer and initially
|
---|
253 | * for the guest frame buffer - must be a multiple of two
|
---|
254 | * @param height height to be used for MPEG frame framebuffer and
|
---|
255 | * initially for the guest framebuffer - must be a multiple
|
---|
256 | * of two
|
---|
257 | * @param depth depth to be used initially for the guest framebuffer
|
---|
258 | */
|
---|
259 | HRESULT FFmpegFB::init()
|
---|
260 | {
|
---|
261 | LogFlow(("Initialising FFmpegFB object %p\n", this));
|
---|
262 | if (mOutOfMemory == true)
|
---|
263 | return E_OUTOFMEMORY;
|
---|
264 | int rc;
|
---|
265 | int rcOpenFile;
|
---|
266 | int rcOpenCodec;
|
---|
267 |
|
---|
268 | #ifdef VBOX_WITH_VPX
|
---|
269 |
|
---|
270 | rc = VideoRecContextCreate(&pVideoRecContext);
|
---|
271 | rc = RTCritSectInit(&mCritSect);
|
---|
272 | AssertReturn(rc == VINF_SUCCESS, E_UNEXPECTED);
|
---|
273 |
|
---|
274 | if(rc == VINF_SUCCESS)
|
---|
275 | rc = VideoRecContextInit(pVideoRecContext, mFileName, mFrameWidth, mFrameHeight);
|
---|
276 | #else
|
---|
277 | rc = RTCritSectInit(&mCritSect);
|
---|
278 | AssertReturn(rc == VINF_SUCCESS, E_UNEXPECTED);
|
---|
279 | int rcSetupLibrary = setup_library();
|
---|
280 | AssertReturn(rcSetupLibrary == S_OK, rcSetupLibrary);
|
---|
281 | int rcSetupFormat = setup_output_format();
|
---|
282 | AssertReturn(rcSetupFormat == S_OK, rcSetupFormat);
|
---|
283 | rcOpenCodec = open_codec();
|
---|
284 | AssertReturn(rcOpenCodec == S_OK, rcOpenCodec);
|
---|
285 | rcOpenFile = open_output_file();
|
---|
286 | AssertReturn(rcOpenFile == S_OK, rcOpenFile);
|
---|
287 |
|
---|
288 | /* Fill in the picture data for the AVFrame - not particularly
|
---|
289 | elegant, but that is the API. */
|
---|
290 | avpicture_fill((AVPicture *) mFrame, mYUVBuffer, PIX_FMT_YUV420P,
|
---|
291 | mFrameWidth, mFrameHeight);
|
---|
292 | #endif
|
---|
293 |
|
---|
294 | /* Set the initial framebuffer size to the mpeg frame dimensions */
|
---|
295 | BOOL finished;
|
---|
296 | RequestResize(0, FramebufferPixelFormat_Opaque, NULL, 0, 0,
|
---|
297 | mFrameWidth, mFrameHeight, &finished);
|
---|
298 | /* Start counting time */
|
---|
299 | mLastTime = RTTimeMilliTS();
|
---|
300 | mLastTime = mLastTime - mLastTime % 40;
|
---|
301 | return rc;
|
---|
302 | }
|
---|
303 |
|
---|
304 | // IFramebuffer properties
|
---|
305 | /////////////////////////////////////////////////////////////////////////////
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Return the address of the frame buffer for the virtual VGA device to
|
---|
309 | * write to. If COMGETTER(UsesGuestVRAM) returns FLASE (or if this address
|
---|
310 | * is not the same as the guests VRAM buffer), the device will perform
|
---|
311 | * translation.
|
---|
312 | *
|
---|
313 | * @returns COM status code
|
---|
314 | * @retval address The address of the buffer
|
---|
315 | */
|
---|
316 | STDMETHODIMP FFmpegFB::COMGETTER(Address) (BYTE **address)
|
---|
317 | {
|
---|
318 | if (!address)
|
---|
319 | return E_POINTER;
|
---|
320 | LogFlow(("FFmpeg::COMGETTER(Address): returning address %p\n", mBufferAddress));
|
---|
321 | *address = mBufferAddress;
|
---|
322 | return S_OK;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Return the width of our frame buffer.
|
---|
327 | *
|
---|
328 | * @returns COM status code
|
---|
329 | * @retval width The width of the frame buffer
|
---|
330 | */
|
---|
331 | STDMETHODIMP FFmpegFB::COMGETTER(Width) (ULONG *width)
|
---|
332 | {
|
---|
333 | if (!width)
|
---|
334 | return E_POINTER;
|
---|
335 | LogFlow(("FFmpeg::COMGETTER(Width): returning width %lu\n",
|
---|
336 | (unsigned long) mGuestWidth));
|
---|
337 | *width = mGuestWidth;
|
---|
338 | return S_OK;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Return the height of our frame buffer.
|
---|
343 | *
|
---|
344 | * @returns COM status code
|
---|
345 | * @retval height The height of the frame buffer
|
---|
346 | */
|
---|
347 | STDMETHODIMP FFmpegFB::COMGETTER(Height) (ULONG *height)
|
---|
348 | {
|
---|
349 | if (!height)
|
---|
350 | return E_POINTER;
|
---|
351 | LogFlow(("FFmpeg::COMGETTER(Height): returning height %lu\n",
|
---|
352 | (unsigned long) mGuestHeight));
|
---|
353 | *height = mGuestHeight;
|
---|
354 | return S_OK;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * Return the colour depth of our frame buffer. Note that we actually
|
---|
359 | * store the pixel format, not the colour depth internally, since
|
---|
360 | * when display sets FramebufferPixelFormat_Opaque, it
|
---|
361 | * wants to retrieve FramebufferPixelFormat_Opaque and
|
---|
362 | * nothing else.
|
---|
363 | *
|
---|
364 | * @returns COM status code
|
---|
365 | * @retval bitsPerPixel The colour depth of the frame buffer
|
---|
366 | */
|
---|
367 | STDMETHODIMP FFmpegFB::COMGETTER(BitsPerPixel) (ULONG *bitsPerPixel)
|
---|
368 | {
|
---|
369 | if (!bitsPerPixel)
|
---|
370 | return E_POINTER;
|
---|
371 | *bitsPerPixel = mBitsPerPixel;
|
---|
372 | LogFlow(("FFmpeg::COMGETTER(BitsPerPixel): returning depth %lu\n",
|
---|
373 | (unsigned long) *bitsPerPixel));
|
---|
374 | return S_OK;
|
---|
375 | }
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Return the number of bytes per line in our frame buffer.
|
---|
379 | *
|
---|
380 | * @returns COM status code
|
---|
381 | * @retval bytesPerLine The number of bytes per line
|
---|
382 | */
|
---|
383 | STDMETHODIMP FFmpegFB::COMGETTER(BytesPerLine) (ULONG *bytesPerLine)
|
---|
384 | {
|
---|
385 | if (!bytesPerLine)
|
---|
386 | return E_POINTER;
|
---|
387 | LogFlow(("FFmpeg::COMGETTER(BytesPerLine): returning line size %lu\n",
|
---|
388 | (unsigned long) mBytesPerLine));
|
---|
389 | *bytesPerLine = mBytesPerLine;
|
---|
390 | return S_OK;
|
---|
391 | }
|
---|
392 |
|
---|
393 | /**
|
---|
394 | * Return the pixel layout of our frame buffer.
|
---|
395 | *
|
---|
396 | * @returns COM status code
|
---|
397 | * @retval pixelFormat The pixel layout
|
---|
398 | */
|
---|
399 | STDMETHODIMP FFmpegFB::COMGETTER(PixelFormat) (ULONG *pixelFormat)
|
---|
400 | {
|
---|
401 | if (!pixelFormat)
|
---|
402 | return E_POINTER;
|
---|
403 | LogFlow(("FFmpeg::COMGETTER(PixelFormat): returning pixel format: %lu\n",
|
---|
404 | (unsigned long) mPixelFormat));
|
---|
405 | *pixelFormat = mPixelFormat;
|
---|
406 | return S_OK;
|
---|
407 | }
|
---|
408 |
|
---|
409 | /**
|
---|
410 | * Return whether we use the guest VRAM directly.
|
---|
411 | *
|
---|
412 | * @returns COM status code
|
---|
413 | * @retval pixelFormat The pixel layout
|
---|
414 | */
|
---|
415 | STDMETHODIMP FFmpegFB::COMGETTER(UsesGuestVRAM) (BOOL *usesGuestVRAM)
|
---|
416 | {
|
---|
417 | if (!usesGuestVRAM)
|
---|
418 | return E_POINTER;
|
---|
419 | LogFlow(("FFmpeg::COMGETTER(UsesGuestVRAM): uses guest VRAM? %d\n",
|
---|
420 | mRGBBuffer == NULL));
|
---|
421 | *usesGuestVRAM = (mRGBBuffer == NULL);
|
---|
422 | return S_OK;
|
---|
423 | }
|
---|
424 |
|
---|
425 | /**
|
---|
426 | * Return the number of lines of our frame buffer which can not be used
|
---|
427 | * (e.g. for status lines etc?).
|
---|
428 | *
|
---|
429 | * @returns COM status code
|
---|
430 | * @retval heightReduction The number of unused lines
|
---|
431 | */
|
---|
432 | STDMETHODIMP FFmpegFB::COMGETTER(HeightReduction) (ULONG *heightReduction)
|
---|
433 | {
|
---|
434 | if (!heightReduction)
|
---|
435 | return E_POINTER;
|
---|
436 | /* no reduction */
|
---|
437 | *heightReduction = 0;
|
---|
438 | LogFlow(("FFmpeg::COMGETTER(HeightReduction): returning 0\n"));
|
---|
439 | return S_OK;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /**
|
---|
443 | * Return a pointer to the alpha-blended overlay used to render status icons
|
---|
444 | * etc above the framebuffer.
|
---|
445 | *
|
---|
446 | * @returns COM status code
|
---|
447 | * @retval aOverlay The overlay framebuffer
|
---|
448 | */
|
---|
449 | STDMETHODIMP FFmpegFB::COMGETTER(Overlay) (IFramebufferOverlay **aOverlay)
|
---|
450 | {
|
---|
451 | if (!aOverlay)
|
---|
452 | return E_POINTER;
|
---|
453 | /* not yet implemented */
|
---|
454 | *aOverlay = 0;
|
---|
455 | LogFlow(("FFmpeg::COMGETTER(Overlay): returning 0\n"));
|
---|
456 | return S_OK;
|
---|
457 | }
|
---|
458 |
|
---|
459 | /**
|
---|
460 | * Return id of associated window
|
---|
461 | *
|
---|
462 | * @returns COM status code
|
---|
463 | * @retval winId Associated window id
|
---|
464 | */
|
---|
465 | STDMETHODIMP FFmpegFB::COMGETTER(WinId) (LONG64 *winId)
|
---|
466 | {
|
---|
467 | if (!winId)
|
---|
468 | return E_POINTER;
|
---|
469 | *winId = 0;
|
---|
470 | return S_OK;
|
---|
471 | }
|
---|
472 |
|
---|
473 | // IFramebuffer methods
|
---|
474 | /////////////////////////////////////////////////////////////////////////////
|
---|
475 |
|
---|
476 | STDMETHODIMP FFmpegFB::Lock()
|
---|
477 | {
|
---|
478 | LogFlow(("FFmpeg::Lock: called\n"));
|
---|
479 | int rc = RTCritSectEnter(&mCritSect);
|
---|
480 | AssertRC(rc);
|
---|
481 | if (rc == VINF_SUCCESS)
|
---|
482 | return S_OK;
|
---|
483 | return E_UNEXPECTED;
|
---|
484 | }
|
---|
485 |
|
---|
486 | STDMETHODIMP FFmpegFB::Unlock()
|
---|
487 | {
|
---|
488 | LogFlow(("FFmpeg::Unlock: called\n"));
|
---|
489 | RTCritSectLeave(&mCritSect);
|
---|
490 | return S_OK;
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | /**
|
---|
495 | * This method is used to notify us that an area of the guest framebuffer
|
---|
496 | * has been updated.
|
---|
497 | *
|
---|
498 | * @returns COM status code
|
---|
499 | * @param x X co-ordinate of the upper left-hand corner of the
|
---|
500 | * area which has been updated
|
---|
501 | * @param y Y co-ordinate of the upper left-hand corner of the
|
---|
502 | * area which has been updated
|
---|
503 | * @param w width of the area which has been updated
|
---|
504 | * @param h height of the area which has been updated
|
---|
505 | */
|
---|
506 | STDMETHODIMP FFmpegFB::NotifyUpdate(ULONG x, ULONG y, ULONG w, ULONG h)
|
---|
507 | {
|
---|
508 | int rc;
|
---|
509 | int64_t iCurrentTime = RTTimeMilliTS();
|
---|
510 |
|
---|
511 | LogFlow(("FFmpeg::NotifyUpdate called: x=%lu, y=%lu, w=%lu, h=%lu\n",
|
---|
512 | (unsigned long) x, (unsigned long) y, (unsigned long) w,
|
---|
513 | (unsigned long) h));
|
---|
514 |
|
---|
515 | /* We always leave at least one frame update pending, which we
|
---|
516 | process when the time until the next frame has elapsed. */
|
---|
517 | if (iCurrentTime - mLastTime >= 40)
|
---|
518 | {
|
---|
519 | rc = do_rgb_to_yuv_conversion();
|
---|
520 | if (rc != S_OK)
|
---|
521 | {
|
---|
522 | #ifdef VBOX_WITH_VPX
|
---|
523 | VideoRecCopyToIntBuffer(pVideoRecContext, x, y, w, h, mPixelFormat,
|
---|
524 | mBitsPerPixel, mBytesPerLine, mFrameWidth,
|
---|
525 | mFrameHeight, mGuestHeight, mGuestWidth,
|
---|
526 | mBufferAddress, mTempRGBBuffer);
|
---|
527 | #else
|
---|
528 | copy_to_intermediate_buffer(x, y, w, h);
|
---|
529 | #endif
|
---|
530 | return rc;
|
---|
531 | }
|
---|
532 | rc = do_encoding_and_write();
|
---|
533 | if (rc != S_OK)
|
---|
534 | {
|
---|
535 | #ifdef VBOX_WITH_VPX
|
---|
536 | VideoRecCopyToIntBuffer(pVideoRecContext, x, y, w, h, mPixelFormat,
|
---|
537 | mBitsPerPixel, mBytesPerLine, mFrameWidth,
|
---|
538 | mFrameHeight, mGuestHeight, mGuestWidth,
|
---|
539 | mBufferAddress, mTempRGBBuffer);
|
---|
540 | #else
|
---|
541 | copy_to_intermediate_buffer(x, y, w, h);
|
---|
542 | #endif
|
---|
543 |
|
---|
544 | return rc;
|
---|
545 | }
|
---|
546 | mLastTime = mLastTime + 40;
|
---|
547 | /* Write frames for the time in-between. Not a good way
|
---|
548 | to handle this. */
|
---|
549 | while (iCurrentTime - mLastTime >= 40)
|
---|
550 | {
|
---|
551 | /* rc = do_rgb_to_yuv_conversion();
|
---|
552 | if (rc != S_OK)
|
---|
553 | {
|
---|
554 | copy_to_intermediate_buffer(x, y, w, h);
|
---|
555 | return rc;
|
---|
556 | }
|
---|
557 | */ rc = do_encoding_and_write();
|
---|
558 | if (rc != S_OK)
|
---|
559 | {
|
---|
560 | #ifdef VBOX_WITH_VPX
|
---|
561 | VideoRecCopyToIntBuffer(pVideoRecContext, x, y, w, h, mPixelFormat,
|
---|
562 | mBitsPerPixel, mBytesPerLine, mFrameWidth,
|
---|
563 | mFrameHeight, mGuestHeight, mGuestWidth,
|
---|
564 | mBufferAddress, mTempRGBBuffer);
|
---|
565 | #else
|
---|
566 | copy_to_intermediate_buffer(x, y, w, h);
|
---|
567 | #endif
|
---|
568 | return rc;
|
---|
569 | }
|
---|
570 | mLastTime = mLastTime + 40;
|
---|
571 | }
|
---|
572 | }
|
---|
573 | /* Finally we copy the updated data to the intermediate buffer,
|
---|
574 | ready for the next update. */
|
---|
575 | #ifdef VBOX_WITH_VPX
|
---|
576 | VideoRecCopyToIntBuffer(pVideoRecContext, x, y, w, h, mPixelFormat,
|
---|
577 | mBitsPerPixel, mBytesPerLine, mFrameWidth,
|
---|
578 | mFrameHeight, mGuestHeight, mGuestWidth,
|
---|
579 | mBufferAddress, mTempRGBBuffer);
|
---|
580 |
|
---|
581 | #else
|
---|
582 | copy_to_intermediate_buffer(x, y, w, h);
|
---|
583 | #endif
|
---|
584 | return S_OK;
|
---|
585 | }
|
---|
586 |
|
---|
587 |
|
---|
588 | /**
|
---|
589 | * Requests a resize of our "screen".
|
---|
590 | *
|
---|
591 | * @returns COM status code
|
---|
592 | * @param pixelFormat Layout of the guest video RAM (i.e. 16, 24,
|
---|
593 | * 32 bpp)
|
---|
594 | * @param vram host context pointer to the guest video RAM,
|
---|
595 | * in case we can cope with the format
|
---|
596 | * @param bitsPerPixel color depth of the guest video RAM
|
---|
597 | * @param bytesPerLine length of a screen line in the guest video RAM
|
---|
598 | * @param w video mode width in pixels
|
---|
599 | * @param h video mode height in pixels
|
---|
600 | * @retval finished set to true if the method is synchronous and
|
---|
601 | * to false otherwise
|
---|
602 | *
|
---|
603 | * This method is called when the guest attempts to resize the virtual
|
---|
604 | * screen. The pointer to the guest's video RAM is supplied in case
|
---|
605 | * the framebuffer can handle the pixel format. If it can't, it should
|
---|
606 | * allocate a memory buffer itself, and the virtual VGA device will copy
|
---|
607 | * the guest VRAM to that in a format we can handle. The
|
---|
608 | * COMGETTER(UsesGuestVRAM) method is used to tell the VGA device which method
|
---|
609 | * we have chosen, and the other COMGETTER methods tell the device about
|
---|
610 | * the layout of our buffer. We currently handle all VRAM layouts except
|
---|
611 | * FramebufferPixelFormat_Opaque (which cannot be handled by
|
---|
612 | * definition).
|
---|
613 | */
|
---|
614 | STDMETHODIMP FFmpegFB::RequestResize(ULONG aScreenId, ULONG pixelFormat,
|
---|
615 | BYTE *vram, ULONG bitsPerPixel,
|
---|
616 | ULONG bytesPerLine,
|
---|
617 | ULONG w, ULONG h, BOOL *finished)
|
---|
618 | {
|
---|
619 | NOREF(aScreenId);
|
---|
620 | if (!finished)
|
---|
621 | return E_POINTER;
|
---|
622 | LogFlow(("FFmpeg::RequestResize called: pixelFormat=%lu, vram=%lu, "
|
---|
623 | "bpp=%lu bpl=%lu, w=%lu, h=%lu\n",
|
---|
624 | (unsigned long) pixelFormat, (unsigned long) vram,
|
---|
625 | (unsigned long) bitsPerPixel, (unsigned long) bytesPerLine,
|
---|
626 | (unsigned long) w, (unsigned long) h));
|
---|
627 | /* For now, we are doing things synchronously */
|
---|
628 | *finished = true;
|
---|
629 |
|
---|
630 | /* We always reallocate our buffer */
|
---|
631 | if (mRGBBuffer)
|
---|
632 | RTMemFree(mRGBBuffer);
|
---|
633 | mGuestWidth = w;
|
---|
634 | mGuestHeight = h;
|
---|
635 |
|
---|
636 | bool fallback = false;
|
---|
637 |
|
---|
638 | /* See if there are conditions under which we can use the guest's VRAM,
|
---|
639 | * fallback to our own memory buffer otherwise */
|
---|
640 |
|
---|
641 | if (pixelFormat == FramebufferPixelFormat_FOURCC_RGB)
|
---|
642 | {
|
---|
643 | switch (bitsPerPixel)
|
---|
644 | {
|
---|
645 | #ifdef VBOX_WITH_VPX
|
---|
646 | case 32:
|
---|
647 | mFFMPEGPixelFormat = VPX_IMG_FMT_RGB32;
|
---|
648 | Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to VPX_IMG_FMT_RGB32\n"));
|
---|
649 | break;
|
---|
650 | case 24:
|
---|
651 | mFFMPEGPixelFormat = VPX_IMG_FMT_RGB24;
|
---|
652 | Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to VPX_IMG_FMT_RGB24\n"));
|
---|
653 | break;
|
---|
654 | case 16:
|
---|
655 | mFFMPEGPixelFormat = VPX_IMG_FMT_RGB565;
|
---|
656 | Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to VPX_IMG_FMT_RGB565\n"));
|
---|
657 | break;
|
---|
658 | #else
|
---|
659 | case 32:
|
---|
660 | mFFMPEGPixelFormat = PIX_FMT_RGBA32;
|
---|
661 | Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to PIX_FMT_RGBA32\n"));
|
---|
662 | break;
|
---|
663 | case 24:
|
---|
664 | mFFMPEGPixelFormat = PIX_FMT_RGB24;
|
---|
665 | Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to PIX_FMT_RGB24\n"));
|
---|
666 | break;
|
---|
667 | case 16:
|
---|
668 | mFFMPEGPixelFormat = PIX_FMT_RGB565;
|
---|
669 | Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to PIX_FMT_RGB565\n"));
|
---|
670 | break;
|
---|
671 | #endif
|
---|
672 | default:
|
---|
673 | fallback = true;
|
---|
674 | break;
|
---|
675 | }
|
---|
676 | }
|
---|
677 | else
|
---|
678 | {
|
---|
679 | fallback = true;
|
---|
680 | }
|
---|
681 |
|
---|
682 | if (!fallback)
|
---|
683 | {
|
---|
684 | mPixelFormat = FramebufferPixelFormat_FOURCC_RGB;
|
---|
685 | mBufferAddress = reinterpret_cast<uint8_t *>(vram);
|
---|
686 | mBytesPerLine = bytesPerLine;
|
---|
687 | mBitsPerPixel = bitsPerPixel;
|
---|
688 | mRGBBuffer = 0;
|
---|
689 | Log2(("FFmpeg::RequestResize: setting mBufferAddress to vram and mLineSize to %lu\n",
|
---|
690 | (unsigned long) mBytesPerLine));
|
---|
691 | }
|
---|
692 | else
|
---|
693 | {
|
---|
694 | /* we always fallback to 32bpp RGB */
|
---|
695 | mPixelFormat = FramebufferPixelFormat_FOURCC_RGB;
|
---|
696 | #ifdef VBOX_WITH_VPX
|
---|
697 | mFFMPEGPixelFormat = VPX_IMG_FMT_RGB32;
|
---|
698 | Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to VPX_IMG_FMT_RGB32\n"));
|
---|
699 | #else
|
---|
700 | mFFMPEGPixelFormat = PIX_FMT_RGBA32;
|
---|
701 | Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to PIX_FMT_RGBA32\n"));
|
---|
702 | #endif
|
---|
703 |
|
---|
704 | mBytesPerLine = w * 4;
|
---|
705 | mBitsPerPixel = 32;
|
---|
706 | mRGBBuffer = reinterpret_cast<uint8_t *>(RTMemAlloc(mBytesPerLine * h));
|
---|
707 | AssertReturn(mRGBBuffer != 0, E_OUTOFMEMORY);
|
---|
708 | Log2(("FFmpeg::RequestResize: alloc'ing mBufferAddress and mRGBBuffer to %p and mBytesPerLine to %lu\n",
|
---|
709 | mBufferAddress, (unsigned long) mBytesPerLine));
|
---|
710 | mBufferAddress = mRGBBuffer;
|
---|
711 | }
|
---|
712 |
|
---|
713 | /* Blank out the intermediate frame framebuffer */
|
---|
714 | memset(mTempRGBBuffer, 0, mFrameWidth * mFrameHeight * 4);
|
---|
715 | return S_OK;
|
---|
716 | }
|
---|
717 |
|
---|
718 | /**
|
---|
719 | * Returns whether we like the given video mode.
|
---|
720 | *
|
---|
721 | * @returns COM status code
|
---|
722 | * @param width video mode width in pixels
|
---|
723 | * @param height video mode height in pixels
|
---|
724 | * @param bpp video mode bit depth in bits per pixel
|
---|
725 | * @param supported pointer to result variable
|
---|
726 | *
|
---|
727 | * As far as I know, the only restriction we have on video modes is that
|
---|
728 | * we have to have an even number of horizontal and vertical pixels.
|
---|
729 | * I sincerely doubt that anything else will be requested, and if it
|
---|
730 | * is anyway, we will just silently amputate one line when we write to
|
---|
731 | * the mpeg file.
|
---|
732 | */
|
---|
733 | STDMETHODIMP FFmpegFB::VideoModeSupported(ULONG width, ULONG height,
|
---|
734 | ULONG bpp, BOOL *supported)
|
---|
735 | {
|
---|
736 | if (!supported)
|
---|
737 | return E_POINTER;
|
---|
738 | *supported = true;
|
---|
739 | return S_OK;
|
---|
740 | }
|
---|
741 |
|
---|
742 | /** Stubbed */
|
---|
743 | STDMETHODIMP FFmpegFB::GetVisibleRegion(BYTE *rectangles, ULONG /* count */, ULONG * /* countCopied */)
|
---|
744 | {
|
---|
745 | if (!rectangles)
|
---|
746 | return E_POINTER;
|
---|
747 | *rectangles = 0;
|
---|
748 | return S_OK;
|
---|
749 | }
|
---|
750 |
|
---|
751 | /** Stubbed */
|
---|
752 | STDMETHODIMP FFmpegFB::SetVisibleRegion(BYTE *rectangles, ULONG /* count */)
|
---|
753 | {
|
---|
754 | if (!rectangles)
|
---|
755 | return E_POINTER;
|
---|
756 | return S_OK;
|
---|
757 | }
|
---|
758 |
|
---|
759 | STDMETHODIMP FFmpegFB::ProcessVHWACommand(BYTE *pCommand)
|
---|
760 | {
|
---|
761 | return E_NOTIMPL;
|
---|
762 | }
|
---|
763 | // Private Methods
|
---|
764 | //////////////////////////////////////////////////////////////////////////
|
---|
765 | //
|
---|
766 | #ifndef VBOX_WITH_VPX
|
---|
767 | HRESULT FFmpegFB::setup_library()
|
---|
768 | {
|
---|
769 | /* Set up the avcodec library */
|
---|
770 | avcodec_init();
|
---|
771 | /* Register all codecs in the library. */
|
---|
772 | avcodec_register_all();
|
---|
773 | /* Register all formats in the format library */
|
---|
774 | av_register_all();
|
---|
775 | mpFormatContext = av_alloc_format_context();
|
---|
776 | AssertReturn(mpFormatContext != 0, E_OUTOFMEMORY);
|
---|
777 | mpStream = av_new_stream(mpFormatContext, 0);
|
---|
778 | AssertReturn(mpStream != 0, E_UNEXPECTED);
|
---|
779 | strncpy(mpFormatContext->filename, com::Utf8Str(mFileName).c_str(),
|
---|
780 | sizeof(mpFormatContext->filename));
|
---|
781 | return S_OK;
|
---|
782 | }
|
---|
783 |
|
---|
784 |
|
---|
785 | /**
|
---|
786 | * Determine the correct output format and codec for our MPEG file.
|
---|
787 | *
|
---|
788 | * @returns COM status code
|
---|
789 | *
|
---|
790 | * @pre The format context (mpFormatContext) should have already been
|
---|
791 | * allocated.
|
---|
792 | */
|
---|
793 | HRESULT FFmpegFB::setup_output_format()
|
---|
794 | {
|
---|
795 | Assert(mpFormatContext != 0);
|
---|
796 | AVOutputFormat *pOutFormat = guess_format(0, com::Utf8Str(mFileName).c_str(),
|
---|
797 | 0);
|
---|
798 | # ifdef VBOX_SHOW_AVAILABLE_FORMATS
|
---|
799 | if (!pOutFormat)
|
---|
800 | {
|
---|
801 | RTPrintf("Could not guess an output format for that extension.\n"
|
---|
802 | "Available formats:\n");
|
---|
803 | list_formats();
|
---|
804 | }
|
---|
805 | # endif
|
---|
806 | AssertMsgReturn(pOutFormat != 0,
|
---|
807 | ("Could not deduce output format from file name\n"),
|
---|
808 | E_INVALIDARG);
|
---|
809 | AssertMsgReturn((pOutFormat->flags & AVFMT_RAWPICTURE) == 0,
|
---|
810 | ("Can't handle output format for file\n"),
|
---|
811 | E_INVALIDARG);
|
---|
812 | AssertMsgReturn((pOutFormat->flags & AVFMT_NOFILE) == 0,
|
---|
813 | ("pOutFormat->flags=%x, pOutFormat->name=%s\n",
|
---|
814 | pOutFormat->flags, pOutFormat->name), E_UNEXPECTED);
|
---|
815 | AssertMsgReturn(pOutFormat->video_codec != CODEC_ID_NONE,
|
---|
816 | ("No video codec available - you have probably selected a non-video file format\n"), E_UNEXPECTED);
|
---|
817 | mpFormatContext->oformat = pOutFormat;
|
---|
818 | /* Set format specific parameters - requires the format to be set. */
|
---|
819 | int rcSetParam = av_set_parameters(mpFormatContext, 0);
|
---|
820 | AssertReturn(rcSetParam >= 0, E_UNEXPECTED);
|
---|
821 | # if 1 /* bird: This works for me on the mac, please review & test elsewhere. */
|
---|
822 | /* Fill in any uninitialized parameters like opt_output_file in ffpmeg.c does.
|
---|
823 | This fixes most of the buffer underflow warnings:
|
---|
824 | http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2005-June/001699.html */
|
---|
825 | if (!mpFormatContext->preload)
|
---|
826 | mpFormatContext->preload = (int)(0.5 * AV_TIME_BASE);
|
---|
827 | if (!mpFormatContext->max_delay)
|
---|
828 | mpFormatContext->max_delay = (int)(0.7 * AV_TIME_BASE);
|
---|
829 | # endif
|
---|
830 | return S_OK;
|
---|
831 | }
|
---|
832 |
|
---|
833 |
|
---|
834 | HRESULT FFmpegFB::list_formats()
|
---|
835 | {
|
---|
836 | AVCodec *codec;
|
---|
837 | for (codec = first_avcodec; codec != NULL; codec = codec->next)
|
---|
838 | {
|
---|
839 | if (codec->type == CODEC_TYPE_VIDEO && codec->encode)
|
---|
840 | {
|
---|
841 | AVOutputFormat *ofmt;
|
---|
842 | for (ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next)
|
---|
843 | {
|
---|
844 | if (ofmt->video_codec == codec->id)
|
---|
845 | RTPrintf(" %20s: %20s => '%s'\n", codec->name, ofmt->extensions, ofmt->long_name);
|
---|
846 | }
|
---|
847 | }
|
---|
848 | }
|
---|
849 | return S_OK;
|
---|
850 | }
|
---|
851 | #endif
|
---|
852 |
|
---|
853 | #ifndef VBOX_WITH_VPX
|
---|
854 | /**
|
---|
855 | * Open the FFmpeg codec and set it up (width, etc) for our MPEG file.
|
---|
856 | *
|
---|
857 | * @returns COM status code
|
---|
858 | *
|
---|
859 | * @pre The format context (mpFormatContext) and the stream (mpStream)
|
---|
860 | * should have already been allocated.
|
---|
861 | */
|
---|
862 | HRESULT FFmpegFB::open_codec()
|
---|
863 | {
|
---|
864 | Assert(mpFormatContext != 0);
|
---|
865 | Assert(mpStream != 0);
|
---|
866 | AVOutputFormat *pOutFormat = mpFormatContext->oformat;
|
---|
867 | AVCodecContext *pCodecContext = mpStream->codec;
|
---|
868 | AssertReturn(pCodecContext != 0, E_UNEXPECTED);
|
---|
869 | AVCodec *pCodec = avcodec_find_encoder(pOutFormat->video_codec);
|
---|
870 | # ifdef VBOX_SHOW_AVAILABLE_FORMATS
|
---|
871 | if (!pCodec)
|
---|
872 | {
|
---|
873 | RTPrintf("Could not find a suitable codec for the output format on your system\n"
|
---|
874 | "Available formats:\n");
|
---|
875 | list_formats();
|
---|
876 | }
|
---|
877 | # endif
|
---|
878 | AssertReturn(pCodec != 0, E_UNEXPECTED);
|
---|
879 | pCodecContext->codec_id = pOutFormat->video_codec;
|
---|
880 | pCodecContext->codec_type = CODEC_TYPE_VIDEO;
|
---|
881 | pCodecContext->bit_rate = mBitRate;
|
---|
882 | pCodecContext->width = mFrameWidth;
|
---|
883 | pCodecContext->height = mFrameHeight;
|
---|
884 | pCodecContext->time_base.den = 25;
|
---|
885 | pCodecContext->time_base.num = 1;
|
---|
886 | pCodecContext->gop_size = 12; /* at most one intra frame in 12 */
|
---|
887 | pCodecContext->max_b_frames = 1;
|
---|
888 | pCodecContext->pix_fmt = PIX_FMT_YUV420P;
|
---|
889 | /* taken from the ffmpeg output example */
|
---|
890 | // some formats want stream headers to be separate
|
---|
891 | if (!strcmp(pOutFormat->name, "mp4")
|
---|
892 | || !strcmp(pOutFormat->name, "mov")
|
---|
893 | || !strcmp(pOutFormat->name, "3gp"))
|
---|
894 | pCodecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
|
---|
895 | /* end output example section */
|
---|
896 | int rcOpenCodec = avcodec_open(pCodecContext, pCodec);
|
---|
897 | AssertReturn(rcOpenCodec >= 0, E_UNEXPECTED);
|
---|
898 | return S_OK;
|
---|
899 | }
|
---|
900 |
|
---|
901 |
|
---|
902 | /**
|
---|
903 | * Open our MPEG file and write the header.
|
---|
904 | *
|
---|
905 | * @returns COM status code
|
---|
906 | *
|
---|
907 | * @pre The format context (mpFormatContext) and the stream (mpStream)
|
---|
908 | * should have already been allocated and set up.
|
---|
909 | */
|
---|
910 | HRESULT FFmpegFB::open_output_file()
|
---|
911 | {
|
---|
912 | char szFileName[RTPATH_MAX];
|
---|
913 | Assert(mpFormatContext);
|
---|
914 | Assert(mpFormatContext->oformat);
|
---|
915 | strcpy(szFileName, com::Utf8Str(mFileName).c_str());
|
---|
916 | int rcUrlFopen = url_fopen(&mpFormatContext->pb,
|
---|
917 | szFileName, URL_WRONLY);
|
---|
918 | AssertReturn(rcUrlFopen >= 0, E_UNEXPECTED);
|
---|
919 | mfUrlOpen = true;
|
---|
920 | av_write_header(mpFormatContext);
|
---|
921 | return S_OK;
|
---|
922 | }
|
---|
923 | #endif
|
---|
924 |
|
---|
925 | /**
|
---|
926 | * Copy an area from the output buffer used by the virtual VGA (may
|
---|
927 | * just be the guest's VRAM) to our fixed size intermediate buffer.
|
---|
928 | * The picture in the intermediate buffer is centred if the guest
|
---|
929 | * screen dimensions are smaller and amputated if they are larger than
|
---|
930 | * our frame dimensions.
|
---|
931 | *
|
---|
932 | * @param x X co-ordinate of the upper left-hand corner of the
|
---|
933 | * area which has been updated
|
---|
934 | * @param y Y co-ordinate of the upper left-hand corner of the
|
---|
935 | * area which has been updated
|
---|
936 | * @param w width of the area which has been updated
|
---|
937 | * @param h height of the area which has been updated
|
---|
938 | */
|
---|
939 | void FFmpegFB::copy_to_intermediate_buffer(ULONG x, ULONG y, ULONG w, ULONG h)
|
---|
940 | {
|
---|
941 | Log2(("FFmpegFB::copy_to_intermediate_buffer: x=%lu, y=%lu, w=%lu, h=%lu\n",
|
---|
942 | (unsigned long) x, (unsigned long) y, (unsigned long) w, (unsigned long) h));
|
---|
943 | /* Perform clipping and calculate the destination co-ordinates */
|
---|
944 | ULONG destX, destY, bpp;
|
---|
945 | LONG xDiff = (LONG(mFrameWidth) - LONG(mGuestWidth)) / 2;
|
---|
946 | LONG yDiff = (LONG(mFrameHeight) - LONG(mGuestHeight)) / 2;
|
---|
947 | if (LONG(w) + xDiff + LONG(x) <= 0) /* nothing visible */
|
---|
948 | return;
|
---|
949 | if (LONG(x) < -xDiff)
|
---|
950 | {
|
---|
951 | w = LONG(w) + xDiff + x;
|
---|
952 | x = -xDiff;
|
---|
953 | destX = 0;
|
---|
954 | }
|
---|
955 | else
|
---|
956 | destX = x + xDiff;
|
---|
957 |
|
---|
958 | if (LONG(h) + yDiff + LONG(y) <= 0) /* nothing visible */
|
---|
959 | return;
|
---|
960 | if (LONG(y) < -yDiff)
|
---|
961 | {
|
---|
962 | h = LONG(h) + yDiff + LONG(y);
|
---|
963 | y = -yDiff;
|
---|
964 | destY = 0;
|
---|
965 | }
|
---|
966 | else
|
---|
967 | destY = y + yDiff;
|
---|
968 | if (destX > mFrameWidth || destY > mFrameHeight)
|
---|
969 | return; /* nothing visible */
|
---|
970 | if (destX + w > mFrameWidth)
|
---|
971 | w = mFrameWidth - destX;
|
---|
972 | if (destY + h > mFrameHeight)
|
---|
973 | h = mFrameHeight - destY;
|
---|
974 | /* Calculate bytes per pixel */
|
---|
975 | if (mPixelFormat == FramebufferPixelFormat_FOURCC_RGB)
|
---|
976 | {
|
---|
977 | switch (mBitsPerPixel)
|
---|
978 | {
|
---|
979 | case 32:
|
---|
980 | case 24:
|
---|
981 | case 16:
|
---|
982 | bpp = mBitsPerPixel / 8;
|
---|
983 | break;
|
---|
984 | default:
|
---|
985 | AssertMsgFailed(("Unknown color depth! mBitsPerPixel=%d\n", mBitsPerPixel));
|
---|
986 | bpp = 1;
|
---|
987 | break;
|
---|
988 | }
|
---|
989 | }
|
---|
990 | else
|
---|
991 | {
|
---|
992 | AssertMsgFailed(("Unknown pixel format! mPixelFormat=%d\n", mPixelFormat));
|
---|
993 | bpp = 1;
|
---|
994 | }
|
---|
995 | /* Calculate start offset in source and destination buffers */
|
---|
996 | ULONG srcOffs = y * mBytesPerLine + x * bpp;
|
---|
997 | ULONG destOffs = (destY * mFrameWidth + destX) * bpp;
|
---|
998 | /* do the copy */
|
---|
999 | for (unsigned int i = 0; i < h; i++)
|
---|
1000 | {
|
---|
1001 | /* Overflow check */
|
---|
1002 | Assert(srcOffs + w * bpp <= mGuestHeight * mBytesPerLine);
|
---|
1003 | Assert(destOffs + w * bpp <= mFrameHeight * mFrameWidth * bpp);
|
---|
1004 | memcpy(mTempRGBBuffer + destOffs, mBufferAddress + srcOffs,
|
---|
1005 | w * bpp);
|
---|
1006 | srcOffs = srcOffs + mBytesPerLine;
|
---|
1007 | destOffs = destOffs + mFrameWidth * bpp;
|
---|
1008 | }
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 |
|
---|
1012 | /**
|
---|
1013 | * Copy the RGB data in the intermediate framebuffer to YUV data in
|
---|
1014 | * the YUV framebuffer.
|
---|
1015 | *
|
---|
1016 | * @returns COM status code
|
---|
1017 | */
|
---|
1018 | HRESULT FFmpegFB::do_rgb_to_yuv_conversion()
|
---|
1019 | {
|
---|
1020 | switch (mFFMPEGPixelFormat)
|
---|
1021 | {
|
---|
1022 | #ifdef VBOX_WITH_VPX
|
---|
1023 | case VPX_IMG_FMT_RGB32:
|
---|
1024 | if (!FFmpegWriteYUV420p<FFmpegBGRA32Iter>(mFrameWidth, mFrameHeight,
|
---|
1025 | mYUVBuffer, mTempRGBBuffer))
|
---|
1026 | return E_UNEXPECTED;
|
---|
1027 | break;
|
---|
1028 | case VPX_IMG_FMT_RGB24:
|
---|
1029 | if (!FFmpegWriteYUV420p<FFmpegBGR24Iter>(mFrameWidth, mFrameHeight,
|
---|
1030 | mYUVBuffer, mTempRGBBuffer))
|
---|
1031 | return E_UNEXPECTED;
|
---|
1032 | break;
|
---|
1033 | case VPX_IMG_FMT_RGB565:
|
---|
1034 | if (!FFmpegWriteYUV420p<FFmpegBGR565Iter>(mFrameWidth, mFrameHeight,
|
---|
1035 | mYUVBuffer, mTempRGBBuffer))
|
---|
1036 | return E_UNEXPECTED;
|
---|
1037 | break;
|
---|
1038 | #else
|
---|
1039 | case PIX_FMT_RGBA32:
|
---|
1040 | if (!FFmpegWriteYUV420p<FFmpegBGRA32Iter>(mFrameWidth, mFrameHeight,
|
---|
1041 | mYUVBuffer, mTempRGBBuffer))
|
---|
1042 | return E_UNEXPECTED;
|
---|
1043 | break;
|
---|
1044 | case PIX_FMT_RGB24:
|
---|
1045 | if (!FFmpegWriteYUV420p<FFmpegBGR24Iter>(mFrameWidth, mFrameHeight,
|
---|
1046 | mYUVBuffer, mTempRGBBuffer))
|
---|
1047 | return E_UNEXPECTED;
|
---|
1048 | break;
|
---|
1049 | case PIX_FMT_RGB565:
|
---|
1050 | if (!FFmpegWriteYUV420p<FFmpegBGR565Iter>(mFrameWidth, mFrameHeight,
|
---|
1051 | mYUVBuffer, mTempRGBBuffer))
|
---|
1052 | return E_UNEXPECTED;
|
---|
1053 | break;
|
---|
1054 |
|
---|
1055 | #endif
|
---|
1056 | default:
|
---|
1057 | return E_UNEXPECTED;
|
---|
1058 | }
|
---|
1059 | return S_OK;
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | /**
|
---|
1063 | * Encode the YUV framebuffer as an MPEG frame and write it to the file.
|
---|
1064 | *
|
---|
1065 | * @returns COM status code
|
---|
1066 | */
|
---|
1067 | HRESULT FFmpegFB::do_encoding_and_write()
|
---|
1068 | {
|
---|
1069 |
|
---|
1070 |
|
---|
1071 | /* A hack: ffmpeg mpeg2 only writes a frame if something has
|
---|
1072 | changed. So we flip the low luminance bit of the first
|
---|
1073 | pixel every frame. */
|
---|
1074 | if (mToggle)
|
---|
1075 | mYUVBuffer[0] |= 1;
|
---|
1076 | else
|
---|
1077 | mYUVBuffer[0] &= 0xfe;
|
---|
1078 | mToggle = !mToggle;
|
---|
1079 |
|
---|
1080 | #ifdef VBOX_WITH_VPX
|
---|
1081 | VideoRecEncodeAndWrite(pVideoRecContext, mFrameWidth, mFrameHeight, mYUVBuffer);
|
---|
1082 | #else
|
---|
1083 | AVCodecContext *pContext = mpStream->codec;
|
---|
1084 | int cSize = avcodec_encode_video(pContext, mOutBuf, mYUVFrameSize * 2,
|
---|
1085 | mFrame);
|
---|
1086 | AssertMsgReturn(cSize >= 0,
|
---|
1087 | ("avcodec_encode_video() failed with rc=%d.\n"
|
---|
1088 | "mFrameWidth=%u, mFrameHeight=%u\n", cSize,
|
---|
1089 | mFrameWidth, mFrameHeight), E_UNEXPECTED);
|
---|
1090 | if (cSize > 0)
|
---|
1091 | {
|
---|
1092 | AVPacket Packet;
|
---|
1093 | av_init_packet(&Packet);
|
---|
1094 | Packet.pts = av_rescale_q(pContext->coded_frame->pts,
|
---|
1095 | pContext->time_base,
|
---|
1096 | mpStream->time_base);
|
---|
1097 | if(pContext->coded_frame->key_frame)
|
---|
1098 | Packet.flags |= PKT_FLAG_KEY;
|
---|
1099 | Packet.stream_index = mpStream->index;
|
---|
1100 | Packet.data = mOutBuf;
|
---|
1101 | Packet.size = cSize;
|
---|
1102 |
|
---|
1103 | /* write the compressed frame in the media file */
|
---|
1104 | int rcWriteFrame = av_write_frame(mpFormatContext, &Packet);
|
---|
1105 | AssertReturn(rcWriteFrame == 0, E_UNEXPECTED);
|
---|
1106 | }
|
---|
1107 | #endif
|
---|
1108 | return S_OK;
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | #ifndef VBOX_WITH_VPX
|
---|
1112 | /**
|
---|
1113 | * Capture the current (i.e. the last) frame as a PNG file with the
|
---|
1114 | * same basename as the captured video file.
|
---|
1115 | */
|
---|
1116 | HRESULT FFmpegFB::write_png()
|
---|
1117 | {
|
---|
1118 | HRESULT errorCode = E_OUTOFMEMORY;
|
---|
1119 | png_bytep *row_pointers;
|
---|
1120 | char PNGFileName[RTPATH_MAX], oldName[RTPATH_MAX];
|
---|
1121 | png_structp png_ptr;
|
---|
1122 | png_infop info_ptr;
|
---|
1123 | uint8_t *PNGBuffer;
|
---|
1124 | /* Work out the new file name - for some reason, we can't use
|
---|
1125 | the com::Utf8Str() directly, but have to copy it */
|
---|
1126 | strcpy(oldName, com::Utf8Str(mFileName).c_str());
|
---|
1127 | int baseLen = strrchr(oldName, '.') - oldName;
|
---|
1128 | if (baseLen == 0)
|
---|
1129 | baseLen = strlen(oldName);
|
---|
1130 | if (baseLen >= RTPATH_MAX - 5) /* for whatever reason */
|
---|
1131 | baseLen = RTPATH_MAX - 5;
|
---|
1132 | memcpy(&PNGFileName[0], oldName, baseLen);
|
---|
1133 | PNGFileName[baseLen] = '.';
|
---|
1134 | PNGFileName[baseLen + 1] = 'p';
|
---|
1135 | PNGFileName[baseLen + 2] = 'n';
|
---|
1136 | PNGFileName[baseLen + 3] = 'g';
|
---|
1137 | PNGFileName[baseLen + 4] = 0;
|
---|
1138 | /* Open output file */
|
---|
1139 | FILE *fp = fopen(PNGFileName, "wb");
|
---|
1140 | if (fp == 0)
|
---|
1141 | {
|
---|
1142 | errorCode = E_UNEXPECTED;
|
---|
1143 | goto fopen_failed;
|
---|
1144 | }
|
---|
1145 | /* Create libpng basic structures */
|
---|
1146 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
|
---|
1147 | 0 /* error function */, 0 /* warning function */);
|
---|
1148 | if (png_ptr == 0)
|
---|
1149 | goto png_create_write_struct_failed;
|
---|
1150 | info_ptr = png_create_info_struct(png_ptr);
|
---|
1151 | if (info_ptr == 0)
|
---|
1152 | {
|
---|
1153 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
---|
1154 | goto png_create_info_struct_failed;
|
---|
1155 | }
|
---|
1156 | /* Convert image to standard RGB24 to simplify life */
|
---|
1157 | PNGBuffer = reinterpret_cast<uint8_t *>(av_malloc(mFrameWidth
|
---|
1158 | * mFrameHeight * 4));
|
---|
1159 | if (PNGBuffer == 0)
|
---|
1160 | goto av_malloc_buffer_failed;
|
---|
1161 | row_pointers =
|
---|
1162 | reinterpret_cast<png_bytep *>(av_malloc(mFrameHeight
|
---|
1163 | * sizeof(png_bytep)));
|
---|
1164 | if (row_pointers == 0)
|
---|
1165 | goto av_malloc_pointers_failed;
|
---|
1166 | switch (mFFMPEGPixelFormat)
|
---|
1167 | {
|
---|
1168 | case PIX_FMT_RGBA32:
|
---|
1169 | if (!FFmpegWriteRGB24<FFmpegBGRA32Iter>(mFrameWidth, mFrameHeight,
|
---|
1170 | PNGBuffer, mTempRGBBuffer))
|
---|
1171 | goto setjmp_exception;
|
---|
1172 | break;
|
---|
1173 | case PIX_FMT_RGB24:
|
---|
1174 | if (!FFmpegWriteRGB24<FFmpegBGR24Iter>(mFrameWidth, mFrameHeight,
|
---|
1175 | PNGBuffer, mTempRGBBuffer))
|
---|
1176 | goto setjmp_exception;
|
---|
1177 | break;
|
---|
1178 | case PIX_FMT_RGB565:
|
---|
1179 | if (!FFmpegWriteRGB24<FFmpegBGR565Iter>(mFrameWidth, mFrameHeight,
|
---|
1180 | PNGBuffer, mTempRGBBuffer))
|
---|
1181 | goto setjmp_exception;
|
---|
1182 | break;
|
---|
1183 | default:
|
---|
1184 | goto setjmp_exception;
|
---|
1185 | }
|
---|
1186 | /* libpng exception handling */
|
---|
1187 | if (setjmp(png_jmpbuf(png_ptr)))
|
---|
1188 | goto setjmp_exception;
|
---|
1189 | /* pass libpng the file pointer */
|
---|
1190 | png_init_io(png_ptr, fp);
|
---|
1191 | /* set the image properties */
|
---|
1192 | png_set_IHDR(png_ptr, info_ptr, mFrameWidth, mFrameHeight,
|
---|
1193 | 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
|
---|
1194 | PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
---|
1195 | /* set up the information about the bitmap for libpng */
|
---|
1196 | row_pointers[0] = png_bytep(PNGBuffer);
|
---|
1197 | for (unsigned i = 1; i < mFrameHeight; i++)
|
---|
1198 | row_pointers[i] = row_pointers[i - 1] + mFrameWidth * 3;
|
---|
1199 | png_set_rows(png_ptr, info_ptr, &row_pointers[0]);
|
---|
1200 | /* and write the thing! */
|
---|
1201 | png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, 0);
|
---|
1202 | /* drop through to cleanup */
|
---|
1203 | errorCode = S_OK;
|
---|
1204 | setjmp_exception:
|
---|
1205 | av_free(row_pointers);
|
---|
1206 | av_malloc_pointers_failed:
|
---|
1207 | av_free(PNGBuffer);
|
---|
1208 | av_malloc_buffer_failed:
|
---|
1209 | png_destroy_write_struct(&png_ptr, &info_ptr);
|
---|
1210 | png_create_info_struct_failed:
|
---|
1211 | png_create_write_struct_failed:
|
---|
1212 | fclose(fp);
|
---|
1213 | fopen_failed:
|
---|
1214 | if (errorCode != S_OK)
|
---|
1215 | Log(("FFmpegFB::write_png: Failed to write .png image of final frame\n"));
|
---|
1216 | return errorCode;
|
---|
1217 | }
|
---|
1218 | #endif
|
---|
1219 |
|
---|
1220 | #ifdef VBOX_WITH_XPCOM
|
---|
1221 | NS_DECL_CLASSINFO(FFmpegFB)
|
---|
1222 | NS_IMPL_THREADSAFE_ISUPPORTS1_CI(FFmpegFB, IFramebuffer)
|
---|
1223 | #endif
|
---|