iOS硬件编解码库VideoToolBox的调用

在iOS平台,要实现真正的视频硬件编解码,必须调用私有库VideoToolBox,但有个致命的弊端的是:如果调用这个私有库,那么app在必须在越狱的设备上运行,也就是app的权限要彻底放开

这里目前只给出VideoToolBox动态库路径和函数符号表

VideoToolBox动态库路径:  "/System/Library/PrivateFrameworks/VideoToolbox.framework/VideoToolbox"

有关VideoToolBox的函数符号表的头文件:

#ifndef __GST_VT_API_H__
#define __GST_VT_API_H__
#import "CoreMedia/CoreMedia.h"

//typedef struct _GstVTApiClass GstVTApiClass;

enum _VTStatus
{  kVTSuccess = 0,
    KVTFail = 1
};

typedef enum _VTStatus VTStatus;

typedef int VTFormatId;

typedef CFTypeRef VTCompressionSessionRef;
typedef CFTypeRef VTDecompressionSessionRef;

//extern OSStatus CMVideoFormatDescriptionCreate(CFAllocatorRef allocator, CMVideoCodecType codecType, int32_t width, int32_t height, 
//                                               CFDictionaryRef extensions, CMVideoFormatDescriptionRef *outDesc);

enum _VTFormat
{
  kVTFormatH264 = 'avc1',
  kVTFormatJPEG = 'jpeg'
};

typedef VTStatus (* VTCompressionOutputCallbackFunc) (void * data, int a2,
                                                      int a3, int a4, CMSampleBufferRef sbuf, int a6, int a7);
typedef void (* VTDecompressionOutputCallbackFunc) (void * data, size_t unk1,
                                                    VTStatus result, size_t unk2, CVBufferRef cvbuf);

struct _VTCompressionOutputCallback
{
  VTCompressionOutputCallbackFunc func;
  void * data;
};

struct _VTDecompressionOutputCallback
{
  VTDecompressionOutputCallbackFunc func;
  void * data;
};

typedef struct _VTCompressionOutputCallback VTCompressionOutputCallback;
typedef struct _VTDecompressionOutputCallback VTDecompressionOutputCallback;

typedef VTStatus (* pVTCompressionSessionCompleteFrames)
(VTCompressionSessionRef session, CMTime completeUntilDisplayTimestamp);

typedef VTStatus (* pVTCompressionSessionCopyProperty)
(VTCompressionSessionRef session, CFTypeRef key, void* unk,
 CFTypeRef * value);

typedef VTStatus (* pVTCompressionSessionCopySupportedPropertyDictionary)
(VTCompressionSessionRef session, CFDictionaryRef * dict);

typedef VTStatus (* pVTCompressionSessionCreate)
(CFAllocatorRef allocator, int width, int height, VTFormatId formatId,
 size_t unk1, CFDictionaryRef sourcePixelBufferAttributes, size_t unk2,
 VTCompressionOutputCallback outputCallback,
 VTCompressionSessionRef * session);

typedef VTStatus (* pVTCompressionSessionEncodeFrame)
(VTCompressionSessionRef session, CVPixelBufferRef pixelBuffer,
 CMTime displayTimestamp, CMTime displayDuration,
 CFDictionaryRef frameOptions, void * sourceTrackingCallback,
 void * sourceFrameRefCon);

typedef void (* pVTCompressionSessionInvalidate)
(VTCompressionSessionRef session);

typedef void (* pVTCompressionSessionRelease)
(VTCompressionSessionRef session);

typedef VTCompressionSessionRef (* pVTCompressionSessionRetain)
(VTCompressionSessionRef session);

typedef VTStatus (* pVTCompressionSessionSetProperty)
(VTCompressionSessionRef session, CFStringRef propName,
 CFTypeRef propValue);

typedef VTStatus (* pVTDecompressionSessionCreate)
(CFAllocatorRef allocator,
 CMFormatDescriptionRef videoFormatDescription,
 CFTypeRef sessionOptions,
 CFDictionaryRef destinationPixelBufferAttributes,
 VTDecompressionOutputCallback * outputCallback,
 VTDecompressionSessionRef * session);

typedef VTStatus (* pVTDecompressionSessionDecodeFrame)
(VTDecompressionSessionRef session, CMSampleBufferRef sbuf, size_t unk1,
 size_t unk2, size_t unk3);

typedef void (* pVTDecompressionSessionInvalidate)
(VTDecompressionSessionRef session);

typedef void (* pVTDecompressionSessionRelease)
(VTDecompressionSessionRef session);

typedef VTDecompressionSessionRef (* pVTDecompressionSessionRetain)
(VTDecompressionSessionRef session);

typedef VTStatus (* pVTDecompressionSessionWaitForAsynchronousFrames)
(VTDecompressionSessionRef session);

typedef OSStatus (* pFigVideoFormatDescriptionCreateWithSampleDescriptionExtensionAtom)(CFAllocatorRef allocator, UInt32 formatId, UInt32 width, UInt32 height, UInt32 atomId, const UInt8 *data, CFIndex len, void *unk1, CMFormatDescriptionRef *formatDesc);

struct _GstVTApi
{

  VTStatus (* VTCompressionSessionCompleteFrames)
      (VTCompressionSessionRef session, CMTime completeUntilDisplayTimestamp);
  VTStatus (* VTCompressionSessionCopyProperty)
      (VTCompressionSessionRef session, CFTypeRef key, void* unk,
      CFTypeRef * value);
  VTStatus (* VTCompressionSessionCopySupportedPropertyDictionary)
      (VTCompressionSessionRef session, CFDictionaryRef * dict);
  VTStatus (* VTCompressionSessionCreate)
      (CFAllocatorRef allocator, int width, int height, VTFormatId formatId,
      size_t unk1, CFDictionaryRef sourcePixelBufferAttributes, size_t unk2,
      VTCompressionOutputCallback outputCallback,
      VTCompressionSessionRef * session);
  VTStatus (* VTCompressionSessionEncodeFrame)
      (VTCompressionSessionRef session, CVPixelBufferRef pixelBuffer,
      CMTime displayTimestamp, CMTime displayDuration,
      CFDictionaryRef frameOptions, void * sourceTrackingCallback,
      void * sourceFrameRefCon);
  void (* VTCompressionSessionInvalidate)
      (VTCompressionSessionRef session);
  void (* VTCompressionSessionRelease)
      (VTCompressionSessionRef session);
  VTCompressionSessionRef (* VTCompressionSessionRetain)
      (VTCompressionSessionRef session);
  VTStatus (* VTCompressionSessionSetProperty)
      (VTCompressionSessionRef session, CFStringRef propName,
      CFTypeRef propValue);

  VTStatus (* VTDecompressionSessionCreate)
      (CFAllocatorRef allocator,
      CMFormatDescriptionRef videoFormatDescription,
      CFTypeRef sessionOptions,
      CFDictionaryRef destinationPixelBufferAttributes,
      VTDecompressionOutputCallback * outputCallback,
      VTDecompressionSessionRef * session);
  VTStatus (* VTDecompressionSessionDecodeFrame)
      (VTDecompressionSessionRef session, CMSampleBufferRef sbuf, size_t unk1,
      size_t unk2, size_t unk3);
  void (* VTDecompressionSessionInvalidate)
      (VTDecompressionSessionRef session);
  void (* VTDecompressionSessionRelease)
      (VTDecompressionSessionRef session);
  VTDecompressionSessionRef (* VTDecompressionSessionRetain)
      (VTDecompressionSessionRef session);
  VTStatus (* VTDecompressionSessionWaitForAsynchronousFrames)
      (VTDecompressionSessionRef session);
    
  OSStatus (* FigVideoFormatDescriptionCreateWithSampleDescriptionExtensionAtom)(CFAllocatorRef allocator, UInt32 formatId, UInt32 width, UInt32 height, UInt32 atomId, const UInt8 *data, CFIndex len, void *unk1, CMFormatDescriptionRef *formatDesc);

  CFStringRef * kVTCompressionPropertyKey_AllowTemporalCompression;
  CFStringRef * kVTCompressionPropertyKey_AverageDataRate;
  CFStringRef * kVTCompressionPropertyKey_ExpectedFrameRate;
  CFStringRef * kVTCompressionPropertyKey_ExpectedDuration;
  CFStringRef * kVTCompressionPropertyKey_MaxKeyFrameInterval;
  CFStringRef * kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration;
  CFStringRef * kVTCompressionPropertyKey_ProfileLevel;
  CFStringRef * kVTCompressionPropertyKey_Usage;
  CFStringRef * kVTEncodeFrameOptionKey_ForceKeyFrame;
  CFStringRef * kVTProfileLevel_H264_Baseline_1_3;
  CFStringRef * kVTProfileLevel_H264_Baseline_3_0;
  CFStringRef * kVTProfileLevel_H264_Baseline_3_1;
  CFStringRef * kVTProfileLevel_H264_Extended_5_0;
  CFStringRef * kVTProfileLevel_H264_High_5_0;
  CFStringRef * kVTProfileLevel_H264_Main_3_0;
  CFStringRef * kVTProfileLevel_H264_Main_3_1;
  CFStringRef * kVTProfileLevel_H264_Main_4_0;
  CFStringRef * kVTProfileLevel_H264_Main_4_1;
  CFStringRef * kVTProfileLevel_H264_Main_5_0;
};

typedef struct _GstVTApi GstVTApi;


#endif

具体如何利用VideoToolBox动态库中的这些函数实现IOS硬件编解码,暂时不公开

若需要实现源代码,可联系我。QQ:349260360   Email:manshilingkai@163.com


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ijkplayer 是一个功能强大的开源播放器,但是在 iOS 平台上使用时可能会出现一些性能问题。本文将介绍一些常见的优化方法。 ### 1. 裁剪无用的模块 Ijkplayer 包含了很多模块,但是在实际使用中可能用不到所有模块。可以根据自己的需求,裁剪掉一些无用的模块,以减小的大小和提高启动速度。 在编译 ijkplayer 时,可以使用如下命令指定需要编译的模块: ```bash ./compile-ffmpeg.sh --disable-ffplay --disable-ffprobe --disable-avdevice --disable-doc --disable-sdl2 ``` 这里禁用了 ffplay、ffprobe、avdevice、doc 和 sdl2 模块。可以根据实际需求进行选择。 ### 2. 使用硬件解码 Ijkplayer 默认使用软件解码,但是在一些低端设备上可能会出现卡顿的现象。可以开启硬件解码以提高性能。 在创建 IJKFFMoviePlayerController 对象时,可以设置使用硬件解码: ```swift let options = IJKFFOptions.byDefault() options?.setPlayerOptionIntValue(1, forKey: "videotoolbox") let player = IJKFFMoviePlayerController(contentURL: url, with: options) ``` 这里使用了 videotoolbox 硬件解码器。可以根据实际情况选择其他的硬件解码器。 ### 3. 使用缓存 Ijkplayer 默认不会缓存视频数据,而是直接从网络中读取。可以开启缓存以提高播放流畅度。 在创建 IJKFFMoviePlayerController 对象时,可以设置缓存大小: ```swift let options = IJKFFOptions.byDefault() options?.setPlayerOptionIntValue(1, forKey: "packet-buffering") options?.setPlayerOptionIntValue(30, forKey: "max-buffer-size") let player = IJKFFMoviePlayerController(contentURL: url, with: options) ``` 这里开启了 packet-buffering 缓存,并设置了最大缓存大小为 30 秒。可以根据实际情况进行调整。 ### 4. 使用硬件加速 Ijkplayer 默认使用软件加速,但是在一些低端设备上可能会出现卡顿的现象。可以开启硬件加速以提高性能。 在创建 IJKFFMoviePlayerController 对象时,可以设置使用硬件加速: ```swift let options = IJKFFOptions.byDefault() options?.setPlayerOptionIntValue(1, forKey: "videotoolbox") options?.setPlayerOptionIntValue(1, forKey: "mediacodec") let player = IJKFFMoviePlayerController(contentURL: url, with: options) ``` 这里开启了 videotoolbox 和 mediacodec 硬件加速。可以根据实际情况选择其他的硬件加速器。 ### 5. 其他优化方法 除了上述方法外,还可以尝试以下优化方法: - 使用较低的分辨率和码率; - 使用较新的编解码器; - 避免多次创建和销毁 IJKFFMoviePlayerController 对象; - 避免在主线程中进行耗时操作。 综上,通过裁剪无用的模块、使用硬件解码、使用缓存、使用硬件加速等优化方法,可以有效提高 ijkplayer 在 iOS 平台上的性能和用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值