码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-11-19 00:44:14      阅读:1761      评论:0      收藏:0      [点我收藏+]

标签:

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

 

技术分享技术分享

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.

shareimprove this question
 

1 Answer

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
 
    
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

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

标签:

原文地址:http://www.cnblogs.com/sunminmin/p/4976348.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!