FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Enumerations | Functions | Variables
videotoolboxenc.c File Reference
#include <VideoToolbox/VideoToolbox.h>
#include <CoreVideo/CoreVideo.h>
#include <CoreMedia/CoreMedia.h>
#include <TargetConditionals.h>
#include <Availability.h>
#include "avcodec.h"
#include "libavutil/opt.h"
#include "libavutil/avassert.h"
#include "libavutil/atomic.h"
#include "libavutil/avstring.h"
#include "libavutil/pixdesc.h"
#include "internal.h"
#include <pthread.h>

Go to the source code of this file.

Data Structures

struct  BufNode
 
struct  VTEncContext
 

Macros

#define kCVImageBufferColorPrimaries_ITU_R_2020   CFSTR("ITU_R_2020")
 
#define kCVImageBufferTransferFunction_ITU_R_2020   CFSTR("ITU_R_2020")
 
#define kCVImageBufferYCbCrMatrix_ITU_R_2020   CFSTR("ITU_R_2020")
 
#define OFFSET(x)   offsetof(VTEncContext, x)
 
#define VE   AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 

Enumerations

enum  VT_H264Profile {
  H264_PROF_AUTO, H264_PROF_BASELINE, H264_PROF_MAIN, H264_PROF_HIGH,
  H264_PROF_COUNT
}
 
enum  VTH264Entropy { VT_ENTROPY_NOT_SET, VT_CAVLC, VT_CABAC }
 

Functions

static int vtenc_populate_extradata (AVCodecContext *avctx, CMVideoCodecType codec_type, CFStringRef profile_level, CFNumberRef gamma_level, CFDictionaryRef enc_info, CFDictionaryRef pixel_buffer_info)
 
static void vt_release_num (CFNumberRef *refPtr)
 NULL-safe release of *refPtr, and sets value to NULL. More...
 
static void set_async_error (VTEncContext *vtctx, int err)
 
static int vtenc_q_pop (VTEncContext *vtctx, bool wait, CMSampleBufferRef *buf)
 
static void vtenc_q_push (VTEncContext *vtctx, CMSampleBufferRef buffer)
 
static int count_nalus (size_t length_code_size, CMSampleBufferRef sample_buffer, int *count)
 
static CMVideoCodecType get_cm_codec_type (enum AVCodecID id)
 
static int get_params_size (AVCodecContext *avctx, CMVideoFormatDescriptionRef vid_fmt, size_t *size)
 Get the parameter sets from a CMSampleBufferRef. More...
 
static int copy_param_sets (AVCodecContext *avctx, CMVideoFormatDescriptionRef vid_fmt, uint8_t *dst, size_t dst_size)
 
static int set_extradata (AVCodecContext *avctx, CMSampleBufferRef sample_buffer)
 
static void vtenc_output_callback (void *ctx, void *sourceFrameCtx, OSStatus status, VTEncodeInfoFlags flags, CMSampleBufferRef sample_buffer)
 
static int get_length_code_size (AVCodecContext *avctx, CMSampleBufferRef sample_buffer, size_t *size)
 
static bool get_vt_profile_level (AVCodecContext *avctx, CFStringRef *profile_level_val)
 
static int get_cv_pixel_format (AVCodecContext *avctx, enum AVPixelFormat fmt, enum AVColorRange range, int *av_pixel_format, int *range_guessed)
 
static void add_color_attr (AVCodecContext *avctx, CFMutableDictionaryRef dict)
 
static int create_cv_pixel_buffer_info (AVCodecContext *avctx, CFMutableDictionaryRef *dict)
 
static int get_cv_color_primaries (AVCodecContext *avctx, CFStringRef *primaries)
 
static int get_cv_transfer_function (AVCodecContext *avctx, CFStringRef *transfer_fnc, CFNumberRef *gamma_level)
 
static int get_cv_ycbcr_matrix (AVCodecContext *avctx, CFStringRef *matrix)
 
static int vtenc_create_encoder (AVCodecContext *avctx, CMVideoCodecType codec_type, CFStringRef profile_level, CFNumberRef gamma_level, CFDictionaryRef enc_info, CFDictionaryRef pixel_buffer_info, VTCompressionSessionRef *session)
 
static av_cold int vtenc_init (AVCodecContext *avctx)
 
static void vtenc_get_frame_info (CMSampleBufferRef buffer, bool *is_key_frame)
 
static int copy_replace_length_codes (AVCodecContext *avctx, size_t length_code_size, CMSampleBufferRef sample_buffer, uint8_t *dst_data, size_t dst_size)
 Copies NAL units and replaces length codes with H.264 Annex B start codes. More...
 
static int vtenc_cm_to_avpacket (AVCodecContext *avctx, CMSampleBufferRef sample_buffer, AVPacket *pkt)
 
static int get_cv_pixel_info (AVCodecContext *avctx, const AVFrame *frame, int *color, int *plane_count, size_t *widths, size_t *heights, size_t *strides, size_t *contiguous_buf_size)
 
static void free_avframe (void *release_ctx, const void *data, size_t size, size_t plane_count, const void *plane_addresses[])
 
static int create_cv_pixel_buffer (AVCodecContext *avctx, const AVFrame *frame, CVPixelBufferRef *cv_img)
 TARGET_OS_IPHONE. More...
 
static int create_encoder_dict_h264 (const AVFrame *frame, CFDictionaryRef *dict_out)
 
static int vtenc_send_frame (AVCodecContext *avctx, VTEncContext *vtctx, const AVFrame *frame)
 
static av_cold int vtenc_frame (AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
 
static av_cold int vtenc_close (AVCodecContext *avctx)
 

Variables

static const uint8_t start_code [] = { 0, 0, 0, 1 }
 
static enum AVPixelFormat pix_fmts []
 
static const AVOption options []
 
static const AVClass h264_videotoolbox_class
 
AVCodec ff_h264_videotoolbox_encoder
 

Macro Definition Documentation

#define kCVImageBufferColorPrimaries_ITU_R_2020   CFSTR("ITU_R_2020")

Definition at line 37 of file videotoolboxenc.c.

Referenced by get_cv_color_primaries().

#define kCVImageBufferTransferFunction_ITU_R_2020   CFSTR("ITU_R_2020")

Definition at line 38 of file videotoolboxenc.c.

Referenced by get_cv_transfer_function().

#define kCVImageBufferYCbCrMatrix_ITU_R_2020   CFSTR("ITU_R_2020")

Definition at line 39 of file videotoolboxenc.c.

Referenced by get_cv_ycbcr_matrix().

#define OFFSET (   x)    offsetof(VTEncContext, x)

Definition at line 1942 of file videotoolboxenc.c.

Definition at line 1943 of file videotoolboxenc.c.

Enumeration Type Documentation

Enumerator
H264_PROF_AUTO 
H264_PROF_BASELINE 
H264_PROF_MAIN 
H264_PROF_HIGH 
H264_PROF_COUNT 

Definition at line 42 of file videotoolboxenc.c.

Enumerator
VT_ENTROPY_NOT_SET 
VT_CAVLC 
VT_CABAC 

Definition at line 50 of file videotoolboxenc.c.

Function Documentation

static int vtenc_populate_extradata ( AVCodecContext avctx,
CMVideoCodecType  codec_type,
CFStringRef  profile_level,
CFNumberRef  gamma_level,
CFDictionaryRef  enc_info,
CFDictionaryRef  pixel_buffer_info 
)
static

Definition at line 1804 of file videotoolboxenc.c.

Referenced by vtenc_init().

static void vt_release_num ( CFNumberRef *  refPtr)
static

NULL-safe release of *refPtr, and sets value to NULL.

Definition at line 109 of file videotoolboxenc.c.

Referenced by create_cv_pixel_buffer_info().

static void set_async_error ( VTEncContext vtctx,
int  err 
)
static

Definition at line 118 of file videotoolboxenc.c.

Referenced by vtenc_output_callback(), and vtenc_q_push().

static int vtenc_q_pop ( VTEncContext vtctx,
bool  wait,
CMSampleBufferRef *  buf 
)
static

Definition at line 139 of file videotoolboxenc.c.

Referenced by vtenc_frame(), and vtenc_populate_extradata().

static void vtenc_q_push ( VTEncContext vtctx,
CMSampleBufferRef  buffer 
)
static

Definition at line 183 of file videotoolboxenc.c.

Referenced by vtenc_output_callback().

static int count_nalus ( size_t  length_code_size,
CMSampleBufferRef  sample_buffer,
int *  count 
)
static

Definition at line 209 of file videotoolboxenc.c.

Referenced by vtenc_cm_to_avpacket().

static CMVideoCodecType get_cm_codec_type ( enum AVCodecID  id)
static

Definition at line 248 of file videotoolboxenc.c.

Referenced by vtenc_init().

static int get_params_size ( AVCodecContext avctx,
CMVideoFormatDescriptionRef  vid_fmt,
size_t *  size 
)
static

Get the parameter sets from a CMSampleBufferRef.

Parameters
dstIf *dst isn't NULL, the parameters are copied into existing memory. *dst_size must be set accordingly when *dst != NULL. If *dst is NULL, it will be allocated. In all cases, *dst_size is set to the number of bytes used starting at *dst.

Definition at line 264 of file videotoolboxenc.c.

Referenced by set_extradata(), and vtenc_cm_to_avpacket().

static int copy_param_sets ( AVCodecContext avctx,
CMVideoFormatDescriptionRef  vid_fmt,
uint8_t dst,
size_t  dst_size 
)
static

Definition at line 317 of file videotoolboxenc.c.

Referenced by set_extradata(), and vtenc_cm_to_avpacket().

static int set_extradata ( AVCodecContext avctx,
CMSampleBufferRef  sample_buffer 
)
static

Definition at line 380 of file videotoolboxenc.c.

Referenced by vtenc_output_callback().

static void vtenc_output_callback ( void ctx,
void sourceFrameCtx,
OSStatus  status,
VTEncodeInfoFlags  flags,
CMSampleBufferRef  sample_buffer 
)
static

Definition at line 414 of file videotoolboxenc.c.

Referenced by vtenc_create_encoder().

static int get_length_code_size ( AVCodecContext avctx,
CMSampleBufferRef  sample_buffer,
size_t *  size 
)
static

Definition at line 446 of file videotoolboxenc.c.

Referenced by vtenc_cm_to_avpacket().

static bool get_vt_profile_level ( AVCodecContext avctx,
CFStringRef *  profile_level_val 
)
static

Definition at line 482 of file videotoolboxenc.c.

Referenced by vtenc_init().

static int get_cv_pixel_format ( AVCodecContext avctx,
enum AVPixelFormat  fmt,
enum AVColorRange  range,
int *  av_pixel_format,
int *  range_guessed 
)
static

Definition at line 554 of file videotoolboxenc.c.

Referenced by create_cv_pixel_buffer_info(), and get_cv_pixel_info().

static void add_color_attr ( AVCodecContext avctx,
CFMutableDictionaryRef  dict 
)
static

Definition at line 579 of file videotoolboxenc.c.

Referenced by create_cv_pixel_buffer(), and create_cv_pixel_buffer_info().

static int create_cv_pixel_buffer_info ( AVCodecContext avctx,
CFMutableDictionaryRef *  dict 
)
static

Definition at line 601 of file videotoolboxenc.c.

Referenced by vtenc_init().

static int get_cv_color_primaries ( AVCodecContext avctx,
CFStringRef *  primaries 
)
static

Definition at line 668 of file videotoolboxenc.c.

Referenced by vtenc_init().

static int get_cv_transfer_function ( AVCodecContext avctx,
CFStringRef *  transfer_fnc,
CFNumberRef *  gamma_level 
)
static

Definition at line 694 of file videotoolboxenc.c.

Referenced by vtenc_init().

static int get_cv_ycbcr_matrix ( AVCodecContext avctx,
CFStringRef *  matrix 
)
static

Definition at line 740 of file videotoolboxenc.c.

Referenced by vtenc_init().

static int vtenc_create_encoder ( AVCodecContext avctx,
CMVideoCodecType  codec_type,
CFStringRef  profile_level,
CFNumberRef  gamma_level,
CFDictionaryRef  enc_info,
CFDictionaryRef  pixel_buffer_info,
VTCompressionSessionRef *  session 
)
static

Definition at line 771 of file videotoolboxenc.c.

Referenced by vtenc_init(), and vtenc_populate_extradata().

static av_cold int vtenc_init ( AVCodecContext avctx)
static

Definition at line 1025 of file videotoolboxenc.c.

static void vtenc_get_frame_info ( CMSampleBufferRef  buffer,
bool *  is_key_frame 
)
static

Definition at line 1137 of file videotoolboxenc.c.

Referenced by vtenc_cm_to_avpacket().

static int copy_replace_length_codes ( AVCodecContext avctx,
size_t  length_code_size,
CMSampleBufferRef  sample_buffer,
uint8_t dst_data,
size_t  dst_size 
)
static

Copies NAL units and replaces length codes with H.264 Annex B start codes.

On failure, the contents of dst_data may have been modified.

Parameters
length_code_sizeByte length of each length code
src_dataNAL units prefixed with length codes.
src_sizeLength of buffer, excluding any padding.
dst_dataMust be zeroed before calling this function. Contains the copied NAL units prefixed with start codes when the function returns successfully.
dst_sizeLength of dst_data
Returns
0 on success AVERROR_INVALIDDATA if length_code_size is invalid AVERROR_BUFFER_TOO_SMALL if dst_data is too small or if a length_code in src_data specifies data beyond the end of its buffer.

Definition at line 1183 of file videotoolboxenc.c.

Referenced by vtenc_cm_to_avpacket().

static int vtenc_cm_to_avpacket ( AVCodecContext avctx,
CMSampleBufferRef  sample_buffer,
AVPacket pkt 
)
static

Definition at line 1258 of file videotoolboxenc.c.

Referenced by vtenc_frame().

static int get_cv_pixel_info ( AVCodecContext avctx,
const AVFrame frame,
int *  color,
int *  plane_count,
size_t *  widths,
size_t *  heights,
size_t *  strides,
size_t *  contiguous_buf_size 
)
static

Definition at line 1357 of file videotoolboxenc.c.

Referenced by create_cv_pixel_buffer().

static void free_avframe ( void release_ctx,
const void data,
size_t  size,
size_t  plane_count,
const void plane_addresses[] 
)
static

Definition at line 1456 of file videotoolboxenc.c.

Referenced by create_cv_pixel_buffer().

static int create_cv_pixel_buffer ( AVCodecContext avctx,
const AVFrame frame,
CVPixelBufferRef *  cv_img 
)
static

TARGET_OS_IPHONE.

Definition at line 1561 of file videotoolboxenc.c.

Referenced by vtenc_send_frame().

static int create_encoder_dict_h264 ( const AVFrame frame,
CFDictionaryRef *  dict_out 
)
static

Definition at line 1687 of file videotoolboxenc.c.

Referenced by vtenc_send_frame().

static int vtenc_send_frame ( AVCodecContext avctx,
VTEncContext vtctx,
const AVFrame frame 
)
static

Definition at line 1703 of file videotoolboxenc.c.

Referenced by vtenc_frame(), and vtenc_populate_extradata().

static av_cold int vtenc_frame ( AVCodecContext avctx,
AVPacket pkt,
const AVFrame frame,
int *  got_packet 
)
static

Definition at line 1742 of file videotoolboxenc.c.

static av_cold int vtenc_close ( AVCodecContext avctx)
static

Definition at line 1906 of file videotoolboxenc.c.

Variable Documentation

const uint8_t start_code[] = { 0, 0, 0, 1 }
static
enum AVPixelFormat pix_fmts[]
static
Initial value:
= {
}
hardware decoding through Videotoolbox
Definition: pixfmt.h:295
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:90
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62

Definition at line 1935 of file videotoolboxenc.c.

const AVOption options[]
static

Definition at line 1944 of file videotoolboxenc.c.

const AVClass h264_videotoolbox_class
static
Initial value:
= {
.class_name = "h264_videotoolbox",
.item_name = av_default_item_name,
.option = options,
}
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
static const AVOption options[]
av_default_item_name

Definition at line 1982 of file videotoolboxenc.c.

AVCodec ff_h264_videotoolbox_encoder
Initial value:
= {
.name = "h264_videotoolbox",
.long_name = NULL_IF_CONFIG_SMALL("VideoToolbox H.264 Encoder"),
.priv_data_size = sizeof(VTEncContext),
.encode2 = vtenc_frame,
.close = vtenc_close,
.capabilities = AV_CODEC_CAP_DELAY,
.priv_class = &h264_videotoolbox_class,
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
}
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:981
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
static av_cold int vtenc_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
static enum AVPixelFormat pix_fmts[]
static const AVClass h264_videotoolbox_class
static av_cold int vtenc_close(AVCodecContext *avctx)
static av_cold int vtenc_init(AVCodecContext *avctx)

Definition at line 1989 of file videotoolboxenc.c.