1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "framegrabber.hxx"
23 #include <sal/log.hxx>
24 #include <tools/stream.hxx>
25 #include <vcl/graph.hxx>
26 #include <vcl/cvtgrf.hxx>
27 #include <unotools/localfilehelper.hxx>
29 using namespace ::com::sun::star;
31 namespace avmedia::macavf {
33 FrameGrabber::FrameGrabber()
34 : mpImageGen( nullptr )
38 FrameGrabber::~FrameGrabber()
41 CFRelease( mpImageGen );
45 bool FrameGrabber::create( AVAsset* pMovie )
47 if( [[pMovie tracksWithMediaType:AVMediaTypeVideo] count] == 0)
49 SAL_WARN("avmedia", "AVGrabber::create() found no video content!" );
53 mpImageGen = [AVAssetImageGenerator assetImageGeneratorWithAsset:pMovie];
54 CFRetain( mpImageGen );
59 uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
61 uno::Reference< graphic::XGraphic > xRet;
65 // get the requested image from the movie
66 CGImage* pCGImage = [mpImageGen copyCGImageAtTime:CMTimeMakeWithSeconds(fMediaTime,1000) actualTime:nullptr error:nullptr];
68 // convert the image to a TIFF-formatted byte-array
69 CFMutableDataRef pCFData = CFDataCreateMutable( kCFAllocatorDefault, 0 );
70 CGImageDestination* pCGImgDest = CGImageDestinationCreateWithData( pCFData, kUTTypeTIFF, 1, nullptr );
71 CGImageDestinationAddImage( pCGImgDest, pCGImage, nullptr );
72 CGImageDestinationFinalize( pCGImgDest );
73 CFRelease( pCGImgDest );
74 const CFIndex nBitmapLen = CFDataGetLength( pCFData );
75 UInt8 * pBitmapBytes = const_cast<UInt8 *>(CFDataGetBytePtr( pCFData ));
77 // convert the image into the return-value type which is a graphic::XGraphic
78 SvMemoryStream aMemStm( pBitmapBytes, nBitmapLen, StreamMode::READ | StreamMode::WRITE );
80 if( GraphicConverter::Import( aMemStm, aGraphic, ConvertDataFormat::TIF ) == ERRCODE_NONE )
81 xRet = aGraphic.GetXGraphic();
89 OUString SAL_CALL FrameGrabber::getImplementationName( )
91 return AVMEDIA_MACAVF_FRAMEGRABBER_IMPLEMENTATIONNAME;
94 sal_Bool SAL_CALL FrameGrabber::supportsService( const OUString& ServiceName )
96 return ServiceName == AVMEDIA_MACAVF_FRAMEGRABBER_SERVICENAME;
99 uno::Sequence< OUString > SAL_CALL FrameGrabber::getSupportedServiceNames( )
101 return { AVMEDIA_MACAVF_FRAMEGRABBER_SERVICENAME };
104 } // namespace avmedia::macavf
106 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */