VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxHeadless/VideoCapture/FFmpegFB.cpp@ 7485

Last change on this file since 7485 was 7485, checked in by vboxsync, 17 years ago

warnings

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette