How to encode picture to H264 use AVFoundation on Mac, not use x264

来源:http://stackoverflow.com/questions/29502563/how-to-encode-picture-to-h264-use-avfoundation-on-mac-not-use-x264

How to encode picture to H264 use AVFoundation on Mac, not use x264


up vote 1 down vote favorite

1

I‘m trying to make a Mac broadcast client to encode into H264 using FFmpeg but not x264 library. So basically, I am able to get raw frames out from AVFoundation in either CMSampleBufferRef or AVPicture. So is there a way to encode a series of those pictures into H264 frames using Apple framework, like AVVideoCodecH264. I know the way to encode it use AVAssetWriter, but that only saves the video into file, but I don‘t want the file, instead, I‘d want to have AVPacket so I can send out using FFmpeg. Does anyone have any idea? Thank you.

osx ffmpeg avfoundation


shareimprove this question

edited Apr 9 at 19:57

asked Apr 7 at 22:35

wao813
154119

 

add a comment

1 Answer

active
oldest
votes

up vote
2
down vote

After refer to VideoCore project, I‘m able to use Apple‘s VideoToolbox framework to encode in hardware.

  1. Start an VTCompressionSession:

    // Create compression session
    err = VTCompressionSessionCreate(kCFAllocatorDefault,
                                 frameW,
                                 frameH,
                                 kCMVideoCodecType_H264,
                                 encoderSpecifications,
                                 NULL,
                                 NULL,
                                 (VTCompressionOutputCallback)vtCallback,
                                 self,
                                 &compression_session);
    
    if(err == noErr) {
        comp_session = session;
    }
    
  2. push the raw frame to the VTCompressionSession
    // Delegate method from the AVCaptureVideoDataOutputSampleBufferDelegate
    - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
        // get pixelbuffer out from samplebuffer
        CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        //set up all the info for the frame
        // call VT to encode frame
        VTCompressionSessionEncodeFrame(compression_session, pixelBuffer, pts, dur, NULL, NULL, NULL);
        }
    
  3. Get the encoded frame in the VTCallback, this is a C method to be used as a parameter of VTCompressionSessionCreate()
    void vtCallback(void *outputCallbackRefCon,
            void *sourceFrameRefCon,
            OSStatus status,
            VTEncodeInfoFlags infoFlags,
            CMSampleBufferRef sampleBuffer ) {
        // Do whatever you want with the sampleBuffer
        CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
    
    }
    

shareimprove this answer

answered Apr 21 at 16:42

wao813
154119

 

    

I have the 3 functions as you do. But my callback is not getting called ever. Am I missing something?
– dcheng
Jul 21 at 23:17
    

@dcheng, if you have the
exact same as mine, it should work. Make sure the callback function and
other function has to be in the same class.
– wao813
Jul 22 at 17:01

add a comment

时间: 2024-08-05 16:41:50

How to encode picture to H264 use AVFoundation on Mac, not use x264的相关文章

How to encode picture to H264 use AVFoundation on Mac, not use x264(续 :其中提到的用VideoToolBox硬编码,RTMP推流的开源工程 VideoCore project)

来源:https://github.com/jgh-/VideoCore Code Issues 77 Pull requests 4 Wiki Pulse Graphs SSH clone URL You can clone with , , or . An audio and video manipulation pipeline 421 commits 5 branches 43 releases 10 contributors C++ 58.0% Objective-C++ 32.9%

H.264解码过程剖析-4

x264开源工程实现H.264的视频编码,但没有提供对应的解码器.ffmpeg开源多媒体编解码集合汇集了市面上几乎所有媒体格式的编解码的源代码.其中的H264.c就是一个能正常解码x264编码码流的独立的源文件,其使用步骤也与上述的编码或解码CODEC应用案例基本相同.这一节通过自顶向下的方式,讲述H264.c如何实现H.264视频解码过程. H264.c源文件有几千行,代码量庞大,很不便于浏览.分析和移植.同时该文件还依赖其他源文件,组织结构较复杂,实现平台由于不是基于Windows的VC++

DAVINCI DM3730开发攻略——应用程序例程分析

过完2015年春节回来了,利用上班前的几天时间,先把这篇文章写完,本来是先写<DAVINCI DM3730开发攻略--linux-2.6.32移植>,但是那篇文章涉及内核的东西太多,不太好写,而本人已经很长时间没写新文章了,先发布这篇文章.后来想了想,从应用程序使用的角度分析,再一步一步深入内核里边去,也许更好. 前面几篇DM3730开发攻略讲到:一个DAVINCI  DM3730板子程序由xload,uboot, linux-2.6.32或者(linux-2.6.37),文件系统rootfs

X264源代码分析——总纲入门最佳

http://www.cnblogs.com/xkfz007/articles/2616133.html l       相关说明: 1.     使用版本: x264-cvs-2004-05-11 2.     这次的分析基本上已经将代码中最难理解的部分做了阐释,对代码的主线也做了剖析,如果这个主线理解了,就容易设置几个区间,进行分工阅读,将各个区间击破了. 3.     需要学习的知识: a)       编码器的工作流程. b)      H.264的码流结构,像x264_sps_t,x2

小小图片爬虫

前言 事前准备 项目结构 项目环境 项目说明 项目目录 项目编码 页面 indexjsp comcjmservlet CatchPciture comcjmmodel JsonFormat MyHttpClient Picture comcjmutils PictureDownload PictureDownloadMachine 最后再说几句 资源下载路径 前言 通过对HttpClient的学习,打开了我对后台的理解,在我的眼中,后台不再是依赖前端的存在,它可以是一种无浏览器,无APP界面的存

编解码技术学习网站汇总

1.音视频开源 1.1 FFMPEG http://www.ffmpeg.org/ http://dranger.com/ffmpeg/ Ffmpeg Basic http://ffmpeg.tv/ http://www.libav.org/ 1.2 H264/AVC http://www.videolan.org/developers/x264.html 1.3 H265/HEVC 1.HM(HEVC test Model) 团队名称 Joint Collaborative Team on V

[李景山php]每天laravel-20160825|Encrypter

<?php namespace Illuminate\Encryption; use RuntimeException; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Encryption\EncryptException; use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract; class Encrypt

[李景山php]每天laravel-20160828|McryptEncrypter-2

/**  * Determine if the given key and cipher combination is valid.  *  * @param  string  $key  * @param  string  $cipher  * @return bool  */ public static function supported($key, $cipher) {// check key and cipher is valid     return defined('MCRYPT_

x264学习资料之——雷神博客系列

第一部分: http://blog.csdn.net/leixiaohua1020/article/category/2619503 FFmpeg与libx264接口源代码简单分析 2015-05-25 01:48 阅读(12999) 评论(1) x264源代码简单分析:熵编码(Entropy Encoding)部分 2015-05-24 22:48 阅读(6041) 评论(3) x264源代码简单分析:宏块编码(Encode)部分 2015-05-24 13:47 阅读(5949) 评论(2)