me icon indicating copy to clipboard operation
me copied to clipboard

学习 C++ (Video Toolbox with CMake)

Open nonocast opened this issue 2 years ago • 0 comments

Direct Access to Video Encoding and Decoding - WWDC14 - Videos - Apple Developer

Case Studies

  1. Displaying an H.264 stream in a layer in your application
  2. Decoding an H.264 stream and accessing the decoded buffers
  3. Compressing a sequence of images into a movie file
  4. Compressing a sequence of images into an H.264 stream for the network

CMake

首先我们需要从C++找到调用Video Toolbox的方式,找到include和对应的lib,我们从CMake开始。简单来说,我理解CMake就是对Makefile都上一层抽象,有点接近package.json和maven, gradle的概念,通过CMake来实现跨平台的构建文件生成工作,其实比写Makefile肯定方便,就是要多学一层抽象语言。

传送:

main.cpp

#include <iostream>

int main() {
  std::cout << "hello world" << std::endl;
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

# set the project name
project(Tutorial)

# add the executable
add_executable(Tutorial tutorial.cxx)

然后你可以在任何目录,即可以在hello/build,也可以在../build_hello中操作生成过程,可以不污染source目录,

生成Makefile

➜ cmake ../Step1
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/nonocast/Desktop/guide/tutorial/Step1_build

编译和运行

➜ cmake --build .
Consolidate compiler generated dependencies of target Tutorial
[ 50%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cxx.o
[100%] Linking CXX executable Tutorial
[100%] Built target Tutorial
➜ ./Tutorial 
hello world

这是最基础的一个cmake flow。

add library

我们在cpp中加入#include <opencv2/opencv.hpp>, 原本我们需要在clang -I中加入include path, 现在就需要通过CMake来 做。

OpenCV官方指引: OpenCV: Using OpenCV with gcc and CMake

cmake_minimum_required(VERSION 3.10)

# set the project name
project(hello)

find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(OpenCV_LIBS "opencv_core;opencv_highgui;opencv_videoio;opencv_imgcodecs")

message(STATUS "OpenCV library status:")
message(STATUS "..version: ${OpenCV_VERSION}")
message(STATUS "..include: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "..lib: ${OpenCV_LIBS}")


# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_OSX_ARCHITECTURES arm64)

# add the executable
add_executable(app main.cpp)

# Link your application with OpenCV libraries
target_link_libraries(app ${OpenCV_LIBS})

注:

  1. 因为是M1的Mac Mini,所以需要设定arm64
  2. 如果直接用OpenCV所有LIBS感觉还缺少dep,所以手动指定了最小范围

然后cmake, make, run

➜  cmake ../Step1
-- OpenCV library status:
-- ..version: 4.5.4
-- ..include: /opt/homebrew/Cellar/opencv/4.5.4_3/include/opencv4
-- ..lib: opencv_core;opencv_highgui;opencv_videoio;opencv_imgcodecs
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/nonocast/Desktop/guide/tutorial/Step1_build
➜  make
Consolidate compiler generated dependencies of target app
[100%] Built target app
➜  ./app
YOU CAN SEE THE CAMERA WINDOW.

接着来看VideoToolbox,Framework是一个bundle。

A framework is a bundle (a structured directory) that contains a dynamic shared library along with associated resources, such as nib files, image files, and header files.

从这个来说就要比OpenCV方便,因为include和lib都在一起了,

cmake_minimum_required(VERSION 3.10)

# set the project name
project(hello)

find_package(OpenCV REQUIRED)
find_library(AVFOUNDATION AVFoundation)
find_library(COCOA Cocoa)
find_library(COREFOUNDATION CoreFoundation)
find_library(COREVIDEO CoreVideo)
find_library(VIDEOTOOLBOX VideoToolbox)
find_library(COREMEDIA CoreMedia)

include_directories(${OpenCV_INCLUDE_DIRS}
  ${COCOA}
  ${COREFOUNDATION}
  ${COREVIDEO}
  ${VIDEOTOOLBOX}
  ${COREMEDIA})

message(STATUS "COCOA: ${COCOA}")
message(STATUS "COREFOUNDATION: ${COREFOUNDATION}")
message(STATUS "COREVIDEO: ${COREVIDEO}")
message(STATUS "VIDEOTOOLBOX: ${VIDEOTOOLBOX}")
message(STATUS "COREMEDIA: ${COREMEDIA}")

set(OpenCV_LIBS "opencv_core;opencv_highgui;opencv_videoio;opencv_imgcodecs")
message(STATUS "OpenCV library status:")
message(STATUS "..version: ${OpenCV_VERSION}")
message(STATUS "..include: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "..lib: ${OpenCV_LIBS}")

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_OSX_ARCHITECTURES arm64)

# add the executable
add_executable(app main.cpp)

# Link your application with OpenCV libraries
target_link_libraries(app
  ${OpenCV_LIBS}
  ${AVFOUNDATION}
  ${COCOA}
  ${COREFOUNDATIOIN}
  ${COREVIDEO}
  ${VIDEOTOOLBOX}
  ${COREMEDIA})

cmake

-- COCOA: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/Cocoa.framework
-- COREFOUNDATION: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/CoreFoundation.framework
-- COREVIDEO: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/CoreVideo.framework
-- VIDEOTOOLBOX: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/VideoToolbox.framework
-- COREMEDIA: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/CoreMedia.framework
-- OpenCV library status:
-- ..version: 4.5.4
-- ..include: /opt/homebrew/Cellar/opencv/4.5.4_3/include/opencv4
-- ..lib: opencv_core;opencv_highgui;opencv_videoio;opencv_imgcodecs
-- Configuring done
-- Generating done
-- Build files have been written to: ...

nonocast avatar Apr 18 '22 07:04 nonocast