来源:https://github.com/galad87/HandBrake-QuickSync-Mac/commit/2c1332958f7095c640cbcbcb45ffc955739d5945#diff-72d938f71df3506b8ad74530b39e7a0bR83

Skip to content

 
This repository

 
  •  
 
 
 

QuickSync encoder via VideoToolbox

Implements a new encoder in HandBrake that uses VideoToolbox.framework
to access the hardware h.264 encoder.
 
1 parent a13430b commit 2c1332958f7095c640cbcbcb45ffc955739d5945 galad87 committed on 20 Jun 2013
 
 
 

Showing

with 540 additions and 14 deletions.
  1. +10 −8 
 
 
18  libhb/common.c
 
@@ -185,15 +185,16 @@ hb_encoder_t *hb_video_encoders_last_item = NULL;
 
hb_encoder_internal_t hb_video_encoders[] =
 
{
 
// legacy encoders, back to HB 0.9.4 whenever possible (disabled)
 
- { { "FFmpeg", "ffmpeg", HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG4, },
 
- { { "MPEG-4 (FFmpeg)", "ffmpeg4", HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG4, },
 
- { { "MPEG-2 (FFmpeg)", "ffmpeg2", HB_VCODEC_FFMPEG_MPEG2, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG2, },
 
- { { "VP3 (Theora)", "libtheora", HB_VCODEC_THEORA, HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_THEORA, },
 
+ { { "FFmpeg", "ffmpeg", HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG4, },
 
+ { { "MPEG-4 (FFmpeg)", "ffmpeg4", HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG4, },
 
+ { { "MPEG-2 (FFmpeg)", "ffmpeg2", HB_VCODEC_FFMPEG_MPEG2, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_MPEG2, },
 
+ { { "VP3 (Theora)", "libtheora", HB_VCODEC_THEORA, HB_MUX_MASK_MKV, }, NULL, 0, HB_GID_VCODEC_THEORA, },
 
// actual encoders
 
- { { "H.264 (x264)", "x264", HB_VCODEC_X264, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_H264, },
 
- { { "MPEG-4", "mpeg4", HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_MPEG4, },
 
- { { "MPEG-2", "mpeg2", HB_VCODEC_FFMPEG_MPEG2, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_MPEG2, },
 
- { { "Theora", "theora", HB_VCODEC_THEORA, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_THEORA, },
 
+ { { "H.264 (x264)", "x264", HB_VCODEC_X264, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_H264, },
 
+ { { "H.264 (VideoToolbox)", "h264", HB_VCODEC_VT_H264, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_H264, },
 
+ { { "MPEG-4", "mpeg4", HB_VCODEC_FFMPEG_MPEG4, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_MPEG4, },
 
+ { { "MPEG-2", "mpeg2", HB_VCODEC_FFMPEG_MPEG2, HB_MUX_MASK_MP4|HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_MPEG2, },
 
+ { { "Theora", "theora", HB_VCODEC_THEORA, HB_MUX_MASK_MKV, }, NULL, 1, HB_GID_VCODEC_THEORA, },
 
};
 
int hb_video_encoders_count = sizeof(hb_video_encoders) / sizeof(hb_video_encoders[0]);
 
static int hb_video_encoder_is_enabled(int encoder)
@@ -202,6 +203,7 @@ static int hb_video_encoder_is_enabled(int encoder)
 
{
 
// the following encoders are always enabled
 
case HB_VCODEC_X264:
 
+ case HB_VCODEC_VT_H264:
 
case HB_VCODEC_THEORA:
 
case HB_VCODEC_FFMPEG_MPEG4:
 
case HB_VCODEC_FFMPEG_MPEG2:
 
 
 
2  libhb/common.h
 
@@ -403,6 +403,7 @@ struct hb_job_s
 
#define HB_VCODEC_MASK 0x00000FF
 
#define HB_VCODEC_X264 0x0000001
 
#define HB_VCODEC_THEORA 0x0000002
 
+#define HB_VCODEC_VT_H264 0x0000004
 
#define HB_VCODEC_FFMPEG_MPEG4 0x0000010
 
#define HB_VCODEC_FFMPEG_MPEG2 0x0000020
 
#define HB_VCODEC_FFMPEG_MASK 0x00000F0
@@ -1008,6 +1009,7 @@ extern hb_work_object_t hb_encvorbis;
 
extern hb_work_object_t hb_muxer;
 
extern hb_work_object_t hb_encca_aac;
 
extern hb_work_object_t hb_encca_haac;
 
+extern hb_work_object_t hb_encvt_h264;
 
extern hb_work_object_t hb_encavcodeca;
 
extern hb_work_object_t hb_reader;
 
 
 
 
1 

libhb/hb.c

 
@@ -1644,6 +1644,7 @@ int hb_global_init()
 

#ifdef __APPLE__

 

hb_register(&hb_encca_aac);

 

hb_register(&hb_encca_haac);

 

+ hb_register(&hb_encvt_h264);

 

#endif

 

#ifdef USE_FAAC

 

hb_register(&hb_encfaac);

 
 
 
1 

libhb/internal.h

 
@@ -406,6 +406,7 @@ enum
 

WORK_ENCAVCODEC,

 

WORK_ENCX264,

 

WORK_ENCTHEORA,

 

+ WORK_ENCVT_H264,

 

WORK_DECA52,

 

WORK_DECAVCODEC,

 

WORK_DECAVCODECV,

 
 
 
1 

libhb/muxmkv.c

 
@@ -97,6 +97,7 @@ static int MKVInit( hb_mux_object_t * m )
 

switch (job->vcodec)

 

{

 

case HB_VCODEC_X264:

 

+ case HB_VCODEC_VT_H264:

 

track->codecID = MK_VCODEC_MP4AVC;

 

/* Taken from x264 muxers.c */

 

avcC_len = 5 + 1 + 2 + job->config.h264.sps_length + 1 + 2 + job->config.h264.pps_length;

 
 
 
10 

libhb/muxmp4.c

 
@@ -129,7 +129,7 @@ static int MP4Init( hb_mux_object_t * m )
 

return 0;

 

}

 

 

- if( job->vcodec == HB_VCODEC_X264 )

 

+ if( job->vcodec == HB_VCODEC_X264 || job->vcodec == HB_VCODEC_VT_H264 )

 

{

 

/* Stolen from mp4creator */

 

MP4SetVideoProfileLevel( m->file, 0x7F );

@@ -684,7 +684,7 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
 

if( mux_data == job->mux_data )

 

{

 

/* Video */

 

- if( job->vcodec == HB_VCODEC_X264 ||

 

+ if( job->vcodec == HB_VCODEC_X264 || job->vcodec == HB_VCODEC_VT_H264 ||

 

( job->vcodec & HB_VCODEC_FFMPEG_MASK ) )

 

{

 

if ( buf && buf->s.start < buf->s.renderOffset )

@@ -706,7 +706,7 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
 

 

stop = buf->s.start + buf->s.duration;

 

 

- if( job->vcodec == HB_VCODEC_X264 ||

 

+ if( job->vcodec == HB_VCODEC_X264 || job->vcodec == HB_VCODEC_VT_H264 ||

 

( job->vcodec & HB_VCODEC_FFMPEG_MASK ) )

 

{

 

// x264 supplies us with DTS, so offset is PTS - DTS

@@ -744,7 +744,7 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
 

}

 

}

 

 

- if( job->vcodec == HB_VCODEC_X264 ||

 

+ if( job->vcodec == HB_VCODEC_X264 || job->vcodec == HB_VCODEC_VT_H264 ||

 

( job->vcodec & HB_VCODEC_FFMPEG_MASK ) )

 

{

 

// x264 supplies us with DTS

@@ -822,7 +822,7 @@ static int MP4Mux( hb_mux_object_t * m, hb_mux_data_t * mux_data,
 

}

 

 

/* Here's where the sample actually gets muxed. */

 

- if( ( job->vcodec == HB_VCODEC_X264 ||

 

+ if( ( job->vcodec == HB_VCODEC_X264 || job->vcodec == HB_VCODEC_VT_H264||

 

( job->vcodec & HB_VCODEC_FFMPEG_MASK ) )

 

&& mux_data == job->mux_data )

 

{

 
 
 
498 

libhb/platform/macosx/encvt_h264.c

 
@@ -0,0 +1,498 @@
 

+/* encvt_h264.c

 

+

 

+ Copyright (c) 2003-2013 HandBrake Team

 

+ This file is part of the HandBrake source code

 

+ Homepage: <http://handbrake.fr/>.

 

+ It may be used under the terms of the GNU General Public License v2.

 

+ For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html

 

+ */

 

+

 

+#include "hb.h"

 

+#include <VideoToolbox/VideoToolbox.h>

 

+#include <CoreMedia/CoreMedia.h>

 

+#include <CoreVideo/CoreVideo.h>

 

+

 

+int encvt_h264Init( hb_work_object_t *, hb_job_t * );

 

+int encvt_h264Work( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );

 

+void encvt_h264Close( hb_work_object_t * );

 

+

 

+hb_work_object_t hb_encvt_h264 =

 

+{

 

+ WORK_ENCVT_H264,

 

+ "H.264 encoder (VideoToolbox)",

 

+ encvt_h264Init,

 

+ encvt_h264Work,

 

+ encvt_h264Close

 

+};

 

+

 

+struct hb_work_private_s

 

+{

 

+ hb_job_t * job;

 

+

 

+ VTCompressionSessionRef session;

 

+ CMSimpleQueueRef queue;

 

+

 

+ int chap_mark; // saved chap mark when we're propagating it

 

+ int64_t next_chap;

 

+};

 

+

 

+void pixelBufferReleasePlanarBytesCallback( void *releaseRefCon, const void *dataPtr, size_t dataSize, size_t numberOfPlanes, const void *planeAddresses[] )

 

+{

 

+ hb_buffer_t * buf = (hb_buffer_t *) releaseRefCon;

 

+ hb_buffer_close( &buf );

 

+}

 

+

 

+void pixelBufferReleaseBytesCallback( void *releaseRefCon, const void *baseAddress )

 

+{

 

+ free( (void *) baseAddress );

 

+}

 

+

 

+void myVTCompressionOutputCallback(

 

+void *outputCallbackRefCon,

 

+void *sourceFrameRefCon,

 

+OSStatus status,

 

+VTEncodeInfoFlags infoFlags,

 

+CMSampleBufferRef sampleBuffer )

 

+{

 

+ OSStatus err;

 

+

 

+ if (sourceFrameRefCon)

 

+ {

 

+ CVPixelBufferRef pixelbuffer = sourceFrameRefCon;

 

+ CVPixelBufferRelease(pixelbuffer);

 

+ }

 

+

 

+ if (status != noErr)

 

+ {

 

+ hb_log("VTCompressionSession: myVTCompressionOutputCallback called error");

 

+ }

 

+ else

 

+ {

 

+ CFRetain(sampleBuffer);

 

+ CMSimpleQueueRef queue = outputCallbackRefCon;

 

+ err = CMSimpleQueueEnqueue(queue, sampleBuffer);

 

+ if (err)

 

+ hb_log("VTCompressionSession: myVTCompressionOutputCallback queue full");

 

+ }

 

+}

 

+

 

+OSStatus initVTSession(hb_work_object_t * w, hb_job_t * job, hb_work_private_t * pv)

 

+{

 

+ OSStatus err = noErr;

 

+

 

+ CFStringRef key = kVTVideoEncoderSpecification_EncoderID;

 

+ CFStringRef value = CFSTR("com.apple.videotoolbox.videoencoder.h264.gva");

 

+

 

+ CFStringRef bkey = CFSTR("EnableHardwareAcceleratedVideoEncoder");

 

+ CFBooleanRef bvalue = kCFBooleanTrue;

 

+

 

+ CFStringRef ckey = CFSTR("RequireHardwareAcceleratedVideoEncoder");

 

+ CFBooleanRef cvalue = kCFBooleanTrue;

 

+

 

+ CFMutableDictionaryRef encoderSpecifications = CFDictionaryCreateMutable(

 

+ kCFAllocatorDefault,

 

+ 3,

 

+ &kCFTypeDictionaryKeyCallBacks,

 

+ &kCFTypeDictionaryValueCallBacks);

 

+

 

+ // Comment out to disable QuickSync

 

+ CFDictionaryAddValue(encoderSpecifications, bkey, bvalue);

 

+ CFDictionaryAddValue(encoderSpecifications, ckey, cvalue);

 

+ CFDictionaryAddValue(encoderSpecifications, key, value);

 

+

 

+ err = VTCompressionSessionCreate(

 

+ kCFAllocatorDefault,

 

+ job->width,

 

+ job->height,

 

+ kCMVideoCodecType_H264,

 

+ encoderSpecifications,

 

+ NULL,

 

+ NULL,

 

+ &myVTCompressionOutputCallback,

 

+ pv->queue,

 

+ &pv->session);

 

+

 

+ if (err != noErr)

 

+ {

 

+ hb_log("Error creating a VTCompressionSession err=%"PRId64"", (int64_t)err);

 

+ return err;

 

+ }

 

+

 

+ err = VTSessionSetProperty(pv->session, kVTCompressionPropertyKey_AllowFrameReordering, kCFBooleanTrue);

 

+ if (err != noErr)

 

+ hb_log("VTSessionSetProperty: kVTCompressionPropertyKey_AllowFrameReordering failed");

 

+

 

+ const int maxKeyFrameInterval = 10 * 30;

 

+ err = VTSessionSetProperty(pv->session, kVTCompressionPropertyKey_MaxKeyFrameInterval,

 

+ CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &maxKeyFrameInterval));

 

+ if (err != noErr)

 

+ hb_log("VTSessionSetProperty: kVTCompressionPropertyKey_MaxKeyFrameInterval failed");

 

+

 

+ const int maxFrameDelayCount = 24;

 

+ err = VTSessionSetProperty(pv->session, kVTCompressionPropertyKey_MaxFrameDelayCount,

 

+ CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &maxFrameDelayCount));

 

+ if (err != noErr)

 

+ hb_log("VTSessionSetProperty: kVTCompressionPropertyKey_MaxKeyFrameInterval failed");

 

+

 

+ const int frameRate = (int)( (double)job->vrate / (double)job->vrate_base + 0.5 );

 

+ err = VTSessionSetProperty(pv->session, kVTCompressionPropertyKey_ExpectedFrameRate,

 

+ CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &frameRate));

 

+ if (err != noErr)

 

+ hb_log("VTSessionSetProperty: kVTCompressionPropertyKey_ExpectedFrameRate failed");

 

+

 

+ if( job->vquality < 0 )

 

+ {

 

+ const int averageBitRate = job->vbitrate * 1024;

 

+ err = VTSessionSetProperty(pv->session, kVTCompressionPropertyKey_AverageBitRate,

 

+ CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &averageBitRate));

 

+ if (err != noErr)

 

+ hb_log("VTSessionSetProperty: kVTCompressionPropertyKey_AverageBitRate failed");

 

+ }

 

+

 

+ err = VTSessionSetProperty(pv->session, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_High_5_0);

 

+ if (err != noErr)

 

+ hb_log("VTSessionSetProperty: kVTCompressionPropertyKey_ProfileLevel failed");

 

+

 

+ CFRelease(encoderSpecifications);

 

+

 

+ return err;

 

+}

 

+

 

+void setupMagicCookie(hb_work_object_t * w, hb_job_t * job, hb_work_private_t * pv)

 

+{

 

+ OSStatus err;

 

+ CMFormatDescriptionRef format = NULL;

 

+

 

+ err = initVTSession(w, job, pv);

 

+

 

+ size_t rgbBufSize = sizeof(uint8) * 3 * job->width * job->height;

 

+ uint8 *rgbBuf = malloc(rgbBufSize);

 

+

 

+ // Compress a random frame to get the magicCookie

 

+ CVPixelBufferRef pxbuffer = NULL;

 

+ CVPixelBufferCreateWithBytes(kCFAllocatorDefault,

 

+ job->width,

 

+ job->height,

 

+ kCVPixelFormatType_24RGB,

 

+ rgbBuf,

 

+ job->width * 3,

 

+ &pixelBufferReleaseBytesCallback,

 

+ NULL,

 

+ NULL,

 

+ &pxbuffer);

 

+

 

+ if (kCVReturnSuccess != err)

 

+ hb_log("VTCompressionSession: CVPixelBuffer error");

 

+

 

+ CMTime pts = CMTimeMake(0, 90000);

 

+ err = VTCompressionSessionEncodeFrame(

 

+ pv->session,

 

+ pxbuffer,

 

+ pts,

 

+ kCMTimeInvalid,

 

+ NULL,

 

+ pxbuffer,

 

+ NULL);

 

+ VTCompressionSessionCompleteFrames(pv->session, CMTimeMake(0,90000));

 

+

 

+ CMSampleBufferRef sampleBuffer = (CMSampleBufferRef) CMSimpleQueueDequeue(pv->queue);

 

+

 

+ format = CMSampleBufferGetFormatDescription(sampleBuffer);

 

+ if (!format)

 

+ hb_log("VTCompressionSession: Format Description error");

 

+

 

+ CFDictionaryRef extentions = CMFormatDescriptionGetExtensions(format);

 

+ if (!extentions)

 

+ hb_log("VTCompressionSession: Format Description Extensions error");

 

+

 

+ CFDictionaryRef atoms = CFDictionaryGetValue(extentions, kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms);

 

+ CFDataRef magicCookie = CFDictionaryGetValue(atoms, CFSTR("avcC"));

 

+ if (!magicCookie)

 

+ hb_log("VTCompressionSession: Magic Cookie error");

 

+

 

+ const uint8_t *avcCAtom = CFDataGetBytePtr(magicCookie);

 

+

 

+ SInt64 i;

 

+ int8_t spsCount = (avcCAtom[5] & 0x1f);

 

+ uint8_t ptrPos = 6;

 

+ uint8_t spsPos = 0;

 

+ for (i = 0; i < spsCount; i++) {

 

+ uint16_t spsSize = (avcCAtom[ptrPos++] << 8) & 0xff00;

 

+ spsSize += avcCAtom[ptrPos++] & 0xff;

 

+ memcpy(w->config->h264.sps + spsPos, avcCAtom+ptrPos, spsSize);;

 

+ ptrPos += spsSize;

 

+ spsPos += spsSize;

 

+ }

 

+ w->config->h264.sps_length = spsPos;

 

+

 

+ int8_t ppsCount = avcCAtom[ptrPos++];

 

+ uint8_t ppsPos = 0;

 

+ for (i = 0; i < ppsCount; i++) {

 

+ uint16_t ppsSize = (avcCAtom[ptrPos++] << 8) & 0xff00;

 

+ ppsSize += avcCAtom[ptrPos++] & 0xff;

 

+ memcpy(w->config->h264.pps + ppsPos, avcCAtom+ptrPos, ppsSize);;

 

+

 

+ ptrPos += ppsSize;

 

+ ppsPos += ppsSize;

 

+ }

 

+ w->config->h264.pps_length = ppsPos;

 

+

 

+ VTCompressionSessionInvalidate(pv->session);

 

+ CFRelease(pv->session);

 

+ CFRelease(sampleBuffer);

 

+}

 

+

 

+int encvt_h264Init( hb_work_object_t * w, hb_job_t * job )

 

+{

 

+ OSStatus err;

 

+ hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );

 

+ w->private_data = pv;

 

+

 

+ pv->job = job;

 

+

 

+ CMSimpleQueueCreate(

 

+ kCFAllocatorDefault,

 

+ 200,

 

+ &pv->queue);

 

+

 

+ setupMagicCookie(w, job, pv);

 

+

 

+ err = initVTSession(w, job, pv);

 

+ if (err != noErr)

 

+ {

 

+ hb_log("Error creating a VTCompressionSession err=%"PRId64"", (int64_t)err);

 

+ *job->die = 1;

 

+ return -1;

 

+ }

 

+

 

+ return 0;

 

+}

 

+

 

+/***********************************************************************

 

+ * Close

 

+ ***********************************************************************

 

+ *

 

+ **********************************************************************/

 

+void encvt_h264Close( hb_work_object_t * w )

 

+{

 

+ hb_work_private_t * pv = w->private_data;

 

+

 

+ VTCompressionSessionInvalidate(pv->session);

 

+ CFRelease(pv->session);

 

+ CFRelease(pv->queue);

 

+

 

+ free( pv );

 

+ w->private_data = NULL;

 

+}

 

+

 

+/***********************************************************************

 

+ * Work

 

+ ***********************************************************************

 

+ *

 

+ **********************************************************************/

 

+

 

+hb_buffer_t* extractData(CMSampleBufferRef sampleBuffer, hb_work_object_t * w)

 

+{

 

+ OSStatus err;

 

+ hb_work_private_t * pv = w->private_data;

 

+ hb_job_t * job = pv->job;

 

+ hb_buffer_t *buf = NULL;

 

+

 

+ CMItemCount samplesNum = CMSampleBufferGetNumSamples(sampleBuffer);

 

+ if (samplesNum > 1)

 

+ hb_log("VTCompressionSession: more than 1 sample in sampleBuffer = %ld", samplesNum);

 

+

 

+ CMBlockBufferRef buffer = CMSampleBufferGetDataBuffer(sampleBuffer);

 

+ if (buffer)

 

+ {

 

+ size_t sampleSize = CMBlockBufferGetDataLength(buffer);

 

+ buf = hb_buffer_init( sampleSize );

 

+

 

+ err = CMBlockBufferCopyDataBytes(buffer, 0, sampleSize, buf->data);

 

+

 

+ if (err != kCMBlockBufferNoErr)

 

+ hb_log("VTCompressionSession: CMBlockBufferCopyDataBytes error");

 

+

 

+ buf->s.frametype = HB_FRAME_IDR;

 

+ buf->s.flags |= HB_FRAME_REF;

 

+

 

+ CFArrayRef attachmentsArray = CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, 0);

 

+ if (CFArrayGetCount(attachmentsArray))

 

+ {

 

+ CFDictionaryRef dict = CFArrayGetValueAtIndex(attachmentsArray, 0);

 

+ if (CFDictionaryGetValueIfPresent(dict, kCMSampleAttachmentKey_NotSync, NULL))

 

+ {

 

+ CFBooleanRef b;

 

+ if (CFDictionaryGetValueIfPresent(dict, kCMSampleAttachmentKey_PartialSync, NULL))

 

+ {

 

+ buf->s.frametype = HB_FRAME_I;

 

+ }

 

+ else if (CFDictionaryGetValueIfPresent(dict, kCMSampleAttachmentKey_IsDependedOnByOthers,(const void **) &b))

 

+ {

 

+ Boolean bv = CFBooleanGetValue(b);

 

+ if (bv)

 

+ buf->s.frametype = HB_FRAME_P;

 

+ else

 

+ {

 

+ buf->s.frametype = HB_FRAME_B;

 

+ buf->s.flags &= ~HB_FRAME_REF;

 

+ }

 

+ }

 

+ else {

 

+ buf->s.frametype = HB_FRAME_P;

 

+ }

 

+ }

 

+ }

 

+

 

+ CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(sampleBuffer);

 

+ CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);

 

+

 

+ if ( !w->config->h264.init_delay && presentationTimeStamp.value )

 

+ {

 

+ w->config->h264.init_delay = presentationTimeStamp.value;

 

+ }

 

+

 

+ buf->f.width = job->width;

 

+ buf->f.height = job->height;

 

+ buf->s.start = presentationTimeStamp.value + w->config->h264.init_delay;

 

+ buf->s.stop = presentationTimeStamp.value + w->config->h264.init_delay;

 

+ buf->s.renderOffset = decodeTimeStamp.value;

 

+

 

+ /* if we have a chapter marker pending and this

 

+ frame's presentation time stamp is at or after

 

+ the marker's time stamp, use this as the

 

+ chapter start. */

 

+ if( buf->s.frametype == HB_FRAME_IDR && pv->next_chap != 0 && pv->next_chap <= presentationTimeStamp.value )

 

+ {

 

+ pv->next_chap = 0;

 

+ buf->s.new_chap = pv->chap_mark;

 

+ }

 

+ }

 

+

 

+ return buf;

 

+}

 

+

 

+int encvt_h264Work( hb_work_object_t * w, hb_buffer_t ** buf_in,

 

+ hb_buffer_t ** buf_out )

 

+{

 

+ hb_work_private_t * pv = w->private_data;

 

+ hb_job_t * job = pv->job;

 

+ hb_buffer_t * in = *buf_in, * buf = NULL;

 

+ CMSampleBufferRef sampleBuffer = NULL;

 

+

 

+ OSStatus err;

 

+

 

+ if ( in->size <= 0 )

 

+ {

 

+ // EOF on input. Flush any frames still in the decoder then

 

+ // send the eof downstream to tell the muxer we're done.

 

+ VTCompressionSessionCompleteFrames(pv->session, kCMTimeInvalid);

 

+

 

+ hb_buffer_t *last_buf = NULL;

 

+

 

+ while ( ( sampleBuffer = (CMSampleBufferRef) CMSimpleQueueDequeue(pv->queue) ) )

 

+ {

 

+ buf = extractData(sampleBuffer, w);

 

+ CFRelease(sampleBuffer);

 

+

 

+ if ( buf )

 

+ {

 

+ if ( last_buf == NULL )

 

+ *buf_out = buf;

 

+ else

 

+ last_buf->next = buf;

 

+ last_buf = buf;

 

+ }

 

+ }

 

+

 

+ // Flushed everything - add the eof to the end of the chain.

 

+ if ( last_buf == NULL )

 

+ *buf_out = in;

 

+ else

 

+ last_buf->next = in;

 

+

 

+ *buf_in = NULL;

 

+ return HB_WORK_DONE;

 

+ }

 

+

 

+ // Create a CVPixelBuffer to wrap the frame data

 

+ CVPixelBufferRef pxbuffer = NULL;

 

+

 

+ hb_buffer_t *in_c = hb_buffer_dup(in);

 

+ void *planeBaseAddress[3] = {in_c->plane[0].data, in_c->plane[1].data, in_c->plane[2].data};

 

+ size_t planeWidth[3] = {in_c->plane[0].width, in_c->plane[1].width, in_c->plane[2].width};

 

+ size_t planeHeight[3] = {in_c->plane[0].height, in_c->plane[1].height, in_c->plane[2].height};

 

+ size_t planeBytesPerRow[3] = {in_c->plane[0].stride, in_c->plane[1].stride, in_c->plane[2].stride};

 

+

 

+ err = CVPixelBufferCreateWithPlanarBytes(

 

+ kCFAllocatorDefault,

 

+ job->width,

 

+ job->height,

 

+ kCVPixelFormatType_420YpCbCr8Planar,

 

+ in_c->data,

 

+ 0,

 

+ 3,

 

+ planeBaseAddress,

 

+ planeWidth,

 

+ planeHeight,

 

+ planeBytesPerRow,

 

+ &pixelBufferReleasePlanarBytesCallback,

 

+ in_c,

 

+ NULL,

 

+ &pxbuffer);

 

+

 

+ if (kCVReturnSuccess != err)

 

+ hb_log("VTCompressionSession: CVPixelBuffer error");

 

+

 

+ CFDictionaryRef frameProperties = NULL;

 

+ if( in->s.new_chap && job->chapter_markers )

 

+ {

 

+ /* chapters have to start with an IDR frame so request that this

 

+ frame be coded as IDR. Since there may be up to 16 frames

 

+ currently buffered in the encoder remember the timestamp so

 

+ when this frame finally pops out of the encoder we'll mark

 

+ its buffer as the start of a chapter. */

 

+ const void *keys[1] = { kVTEncodeFrameOptionKey_ForceKeyFrame };

 

+ const void *values[1] = { kCFBooleanTrue };

 

+

 

+ frameProperties = CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

 

+

 

+ if( pv->next_chap == 0 )

 

+ {

 

+ pv->next_chap = in->s.start;

 

+ pv->chap_mark = in->s.new_chap;

 

+ }

 

+ /* don't let 'work_loop' put a chapter mark on the wrong buffer */

 

+ in->s.new_chap = 0;

 

+ }

 

+

 

+ // Send the frame to be encoded

 

+ err = VTCompressionSessionEncodeFrame(

 

+ pv->session,

 

+ pxbuffer,

 

+ CMTimeMake(in->s.start, 90000),

 

+ kCMTimeInvalid,

 

+ frameProperties,

 

+ pxbuffer,

 

+ NULL);

 

+

 

+ if (err)

 

+ hb_log("VTCompressionSession: VTCompressionSessionEncodeFrame error");

 

+

 

+ if (frameProperties)

 

+ CFRelease(frameProperties);

 

+

 

+ // Return a frame if ready

 

+ sampleBuffer = (CMSampleBufferRef) CMSimpleQueueDequeue(pv->queue);

 

+

 

+ if (sampleBuffer)

 

+ {

 

+ buf = extractData(sampleBuffer, w);

 

+ CFRelease(sampleBuffer);

 

+ }

 

+

 

+ *buf_out = buf;

 

+

 

+ return HB_WORK_OK;

 

+}

 

+

 
 
3 

libhb/work.c

 
@@ -1027,6 +1027,9 @@ static void do_job(hb_job_t *job)
 

case HB_VCODEC_X264:

 

w = hb_get_work( WORK_ENCX264 );

 

break;

 

+ case HB_VCODEC_VT_H264:

 

+ w = hb_get_work( WORK_ENCVT_H264 );

 

+ break;

 

case HB_VCODEC_THEORA:

 

w = hb_get_work( WORK_ENCTHEORA );

 

break;

 
 
 
18 

macosx/HandBrake.xcodeproj/project.pbxproj

 
@@ -120,6 +120,12 @@
 

27D6C77314B102DA00B785E4 /* libxml2.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 27D6C74014B102DA00B785E4 /* libxml2.a */; };

 

3490BCB41614CF8D002A5AD7 /* HandBrake.icns in Resources */ = {isa = PBXBuildFile; fileRef = 3490BCB31614CF8D002A5AD7 /* HandBrake.icns */; };

 

46AB433515F98A2B009C0961 /* DockTextField.m in Sources */ = {isa = PBXBuildFile; fileRef = 46AB433415F98A2B009C0961 /* DockTextField.m */; };

 

+ A90156C21770AB9200079F5A /* VideoToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A90156C11770AB9200079F5A /* VideoToolbox.framework */; };

 

+ A90156C31770ADCE00079F5A /* VideoToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A90156C11770AB9200079F5A /* VideoToolbox.framework */; };

 

+ A94BC59D1770C0110055F56B /* CoreMedia.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A94BC59C1770C0110055F56B /* CoreMedia.framework */; };

 

+ A94BC59E1770C0180055F56B /* CoreMedia.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A94BC59C1770C0110055F56B /* CoreMedia.framework */; };

 

+ A94BC5A01770C0C00055F56B /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A94BC59F1770C0C00055F56B /* CoreVideo.framework */; };

 

+ A94BC5A11770C0C70055F56B /* CoreVideo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A94BC59F1770C0C00055F56B /* CoreVideo.framework */; };

 

A9E1467B16BC2ABD00C307BC /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A9E1467A16BC2ABD00C307BC /* QuartzCore.framework */; };

 

A9E1468016BC2AD800C307BC /* next-p.pdf in Resources */ = {isa = PBXBuildFile; fileRef = A9E1467C16BC2AD800C307BC /* next-p.pdf */; };

 

A9E1468116BC2AD800C307BC /* pause-p.pdf in Resources */ = {isa = PBXBuildFile; fileRef = A9E1467D16BC2AD800C307BC /* pause-p.pdf */; };

@@ -305,6 +311,9 @@
 

34FF2FC014EEC363004C2400 /* HBAdvancedController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBAdvancedController.h; sourceTree = "<group>"; };

 

46AB433315F98A2B009C0961 /* DockTextField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DockTextField.h; sourceTree = "<group>"; };

 

46AB433415F98A2B009C0961 /* DockTextField.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DockTextField.m; sourceTree = "<group>"; };

 

+ A90156C11770AB9200079F5A /* VideoToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = VideoToolbox.framework; path = /System/Library/Frameworks/VideoToolbox.framework; sourceTree = "<absolute>"; };

 

+ A94BC59C1770C0110055F56B /* CoreMedia.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMedia.framework; path = /System/Library/Frameworks/CoreMedia.framework; sourceTree = "<absolute>"; };

 

+ A94BC59F1770C0C00055F56B /* CoreVideo.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreVideo.framework; path = /System/Library/Frameworks/CoreVideo.framework; sourceTree = "<absolute>"; };

 

A9E1467A16BC2ABD00C307BC /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = /System/Library/Frameworks/QuartzCore.framework; sourceTree = "<absolute>"; };

 

A9E1467C16BC2AD800C307BC /* next-p.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = "next-p.pdf"; sourceTree = "<group>"; };

 

A9E1467D16BC2AD800C307BC /* pause-p.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = "pause-p.pdf"; sourceTree = "<group>"; };

@@ -343,6 +352,9 @@
 

isa = PBXFrameworksBuildPhase;

 

buildActionMask = 2147483647;

 

files = (

 

+ A94BC5A11770C0C70055F56B /* CoreVideo.framework in Frameworks */,

 

+ A94BC59D1770C0110055F56B /* CoreMedia.framework in Frameworks */,

 

+ A90156C31770ADCE00079F5A /* VideoToolbox.framework in Frameworks */,

 

273F203014ADB9790021BE6D /* AudioToolbox.framework in Frameworks */,

 

273F202314ADB8650021BE6D /* IOKit.framework in Frameworks */,

 

273F203314ADB9F00021BE6D /* CoreServices.framework in Frameworks */,

@@ -379,6 +391,9 @@
 

isa = PBXFrameworksBuildPhase;

 

buildActionMask = 2147483647;

 

files = (

 

+ A94BC5A01770C0C00055F56B /* CoreVideo.framework in Frameworks */,

 

+ A94BC59E1770C0180055F56B /* CoreMedia.framework in Frameworks */,

 

+ A90156C21770AB9200079F5A /* VideoToolbox.framework in Frameworks */,

 

A9E1467B16BC2ABD00C307BC /* QuartzCore.framework in Frameworks */,

 

273F21C114ADE7A20021BE6D /* Growl.framework in Frameworks */,

 

273F21C214ADE7BC0021BE6D /* Sparkle.framework in Frameworks */,

@@ -495,6 +510,9 @@
 

273F203414ADBAC30021BE6D /* Frameworks */ = {

 

isa = PBXGroup;

 

children = (

 

+ A94BC59F1770C0C00055F56B /* CoreVideo.framework */,

 

+ A94BC59C1770C0110055F56B /* CoreMedia.framework */,

 

+ A90156C11770AB9200079F5A /* VideoToolbox.framework */,

 

A9E1467A16BC2ABD00C307BC /* QuartzCore.framework */,

 

273F20C714ADC4FF0021BE6D /* QTKit.framework */,

 

273F202F14ADB9790021BE6D /* AudioToolbox.framework */,

 
 
 
2 

test/module.defs

 
@@ -50,7 +50,7 @@ BUILD.out += $(TEST.install.exe)
 

TEST.GCC.I += $(LIBHB.GCC.I)

 

 

ifeq ($(BUILD.system),darwin)

 

- TEST.GCC.f += IOKit CoreServices AudioToolbox

 

+ TEST.GCC.f += IOKit CoreServices AudioToolbox VideoToolbox CoreMedia CoreVideo

 

TEST.GCC.l += iconv

 

else ifeq ($(BUILD.system),linux)

 

TEST.GCC.l += pthread dl m

 

0 comments
on commit 2c13329

 

 

Attach files by dragging & dropping or selecting them.

 
 
 
 

You’re not receiving notifications from this thread.

HandBrake-QuickSync-Mac (内容:QuickSync encoder via VideoToolbox )的更多相关文章

  1. 重新想象 Windows 8 Store Apps (29) - 图片处理

    原文:重新想象 Windows 8 Store Apps (29) - 图片处理 [源码下载] 重新想象 Windows 8 Store Apps (29) - 图片处理 作者:webabcd介绍重新 ...

  2. Feign源码解析系列-注册套路

    感谢不知名朋友的打赏,感谢你的支持! 开始 在追寻Feign源码的过程中发现了一些套路,既然是套路,就可以举一反三,所以值得关注. 这篇会详细解析Feign Client配置和初始化的方式,这些方式大 ...

  3. xml中“ < > ”转义为“ &lt; &gt; ”问题处理

    曾经也碰到过类似问题,解决方法是在发送或者解析报文前执行上面的方法将内容转义一下,现在我用dom4j组装如下的报文(报文体中内容传输时加密处理),大致介绍一下上面方法的使用,具体看代码. import ...

  4. Base64加密工具

    正常来讲加密基本上永远都要伴随着解密,所谓的加密或者解密,往往都需要有一些规则,在JDK1.8开始,提供有新的加密处理操作类,Base64处理类--Base64类 在该类之中存在两个内部类:Base6 ...

  5. 【机器学习基础】无监督学习(3)——AutoEncoder

    前面主要回顾了无监督学习中的三种降维方法,本节主要学习另一种无监督学习AutoEncoder,这个方法在无监督学习领域应用比较广泛,尤其是其思想比较通用. AutoEncoder 0.AutoEnco ...

  6. mac安装office2011,提示无法打开文件Normal.dotm,因为内容有错误

    最近使用mac上的office,发现一个问题,每次打开office11都会报错,提示“无法打开文件Normal.dotm,因为内容有错误”,于是就在网络上搜索了一下,找到如下一段话, I just f ...

  7. Mac下显示隐藏文件 以及修改 hosts文件内容

    修改hosts 文件内容: 进入etc 文件夹,找到hosts 文件,把该文件复制出来,修改完里面的内容后,先把etc中的hosts 文件删除,然后在把修改后的文件脱机去 可能需要管理员的密码,你输入 ...

  8. mac 连接windows 共享内容

    mac 连接windows 共享内容 一:场景 在win7上下载了一个5G左右的系统文件,想弄到mac上,本打算用使用U盘,把文件从win7copy到mac电脑上: 可是U盘的分区是fat的,大于4G ...

  9. mac中的word内容丢失

    改了一晚上好不容易快搞完了,结果1万字的内容丢了,并且不知道自己当时怎么想的还清理了回收站 还是用mac自带的工具吧,同时代码也要及时上传github

随机推荐

  1. JWT(Json Web Token):一种在Web应用中安全传递信息的规范 转载

    文本将介绍一种在Web应用中安全传递信息的方式,称为JWT. 本文内容是对JWT官网介绍说明的英文翻译而来,由于本文英文水平有限,如有错误,还请指出,谢谢. What is JSON Web Toke ...

  2. Visualforce 页面的生命周期

    了解 Visualforce 页面的生命周期可以让我们明白其各个元素的执行顺序.改变逻辑,从而提高开发效率,避免不必要的错误. 在官方文档中有详细的讲解.本文摘取其中的要点,作为一个总结. Visua ...

  3. css网格布局

    先来一段基本布局 <!doctype html> <html> <head> <meta charset="utf-8"> < ...

  4. [LeetCode] 827. Making A Large Island 建造一个巨大岛屿

    In a 2D grid of 0s and 1s, we change at most one 0 to a 1. After, what is the size of the largest is ...

  5. bower安装教程

    进入node.js官网下载相应操作系统的安装文件http://www.nodejs.org/download/ ,windows环境下载msi文件即可 打开下载的文件,一直点击下一步,完成安装 安装完 ...

  6. 基于sign的pose-graph残差函数构建

    沿sign的法线方向优化车辆位姿,优化目标函数也即残差函数构建过程如下:

  7. 推荐一款移动端小视频App玲珑视频

    推荐一款移动端小视频App玲珑视频 一 应用描述 玲珑小视频,边看边聊![海量视频,刷个不停,还能找妹子语音聊天哦][随手拍一拍,记录美好生活,还能拿金币哦][看视频领金币.登录领金币.拍视频领金币. ...

  8. LeetCode题库整理(自学整理)

    1. Two Sum 两数之和       来源:力扣(LeetCode) 题目:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利 ...

  9. Hanlp分词插件docker集群安装

    背景:我是用docker-compose的方式装的es集群,正常情况es镜像没有插件,如果在docker里面用命令安装了那么重启以后又没了,所以采用挂载离线安装的方式 版本: es7.2 1下载Han ...

  10. js 编译原理

    引擎:从头到尾负责整个javaScript 程序的编译过程和执行过程. 编译器: 负责语法分析以及代码的生成. 作用域:负责收集并维护由所有声明的标识符(变量)组成的一系列查询, 并实施一套非常严格的 ...