2 * copyright (c) 2015 Rick Kern <kernrj@gmail.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <VideoToolbox/VideoToolbox.h>
22 #include <CoreVideo/CoreVideo.h>
23 #include <CoreMedia/CoreMedia.h>
24 #include <TargetConditionals.h>
25 #include <Availability.h>
27 #include "libavutil/opt.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/avstring.h"
30 #include "libavcodec/avcodec.h"
31 #include "libavutil/pixdesc.h"
38 #if !HAVE_KCMVIDEOCODECTYPE_HEVC
39 enum { kCMVideoCodecType_HEVC = 'hvc1' };
42 #if !HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
43 enum { kCVPixelFormatType_420YpCbCr10BiPlanarFullRange = 'xf20' };
44 enum { kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange = 'x420' };
47 typedef OSStatus (*getParameterSetAtIndex)(CMFormatDescriptionRef videoDesc,
48 size_t parameterSetIndex,
49 const uint8_t **parameterSetPointerOut,
50 size_t *parameterSetSizeOut,
51 size_t *parameterSetCountOut,
52 int *NALUnitHeaderLengthOut);
54 //These symbols may not be present
56 CFStringRef kCVImageBufferColorPrimaries_ITU_R_2020;
57 CFStringRef kCVImageBufferTransferFunction_ITU_R_2020;
58 CFStringRef kCVImageBufferYCbCrMatrix_ITU_R_2020;
60 CFStringRef kVTCompressionPropertyKey_H264EntropyMode;
61 CFStringRef kVTH264EntropyMode_CAVLC;
62 CFStringRef kVTH264EntropyMode_CABAC;
64 CFStringRef kVTProfileLevel_H264_Baseline_4_0;
65 CFStringRef kVTProfileLevel_H264_Baseline_4_2;
66 CFStringRef kVTProfileLevel_H264_Baseline_5_0;
67 CFStringRef kVTProfileLevel_H264_Baseline_5_1;
68 CFStringRef kVTProfileLevel_H264_Baseline_5_2;
69 CFStringRef kVTProfileLevel_H264_Baseline_AutoLevel;
70 CFStringRef kVTProfileLevel_H264_Main_4_2;
71 CFStringRef kVTProfileLevel_H264_Main_5_1;
72 CFStringRef kVTProfileLevel_H264_Main_5_2;
73 CFStringRef kVTProfileLevel_H264_Main_AutoLevel;
74 CFStringRef kVTProfileLevel_H264_High_3_0;
75 CFStringRef kVTProfileLevel_H264_High_3_1;
76 CFStringRef kVTProfileLevel_H264_High_3_2;
77 CFStringRef kVTProfileLevel_H264_High_4_0;
78 CFStringRef kVTProfileLevel_H264_High_4_1;
79 CFStringRef kVTProfileLevel_H264_High_4_2;
80 CFStringRef kVTProfileLevel_H264_High_5_1;
81 CFStringRef kVTProfileLevel_H264_High_5_2;
82 CFStringRef kVTProfileLevel_H264_High_AutoLevel;
83 CFStringRef kVTProfileLevel_H264_Extended_5_0;
84 CFStringRef kVTProfileLevel_H264_Extended_AutoLevel;
86 CFStringRef kVTProfileLevel_HEVC_Main_AutoLevel;
87 CFStringRef kVTProfileLevel_HEVC_Main10_AutoLevel;
89 CFStringRef kVTCompressionPropertyKey_RealTime;
91 CFStringRef kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
92 CFStringRef kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder;
94 getParameterSetAtIndex CMVideoFormatDescriptionGetHEVCParameterSetAtIndex;
97 #define GET_SYM(symbol, defaultVal) \
99 CFStringRef* handle = (CFStringRef*)dlsym(RTLD_DEFAULT, #symbol); \
101 compat_keys.symbol = CFSTR(defaultVal); \
103 compat_keys.symbol = *handle; \
106 static pthread_once_t once_ctrl = PTHREAD_ONCE_INIT;
108 static void loadVTEncSymbols(){
109 compat_keys.CMVideoFormatDescriptionGetHEVCParameterSetAtIndex =
110 (getParameterSetAtIndex)dlsym(
112 "CMVideoFormatDescriptionGetHEVCParameterSetAtIndex"
115 GET_SYM(kCVImageBufferColorPrimaries_ITU_R_2020, "ITU_R_2020");
116 GET_SYM(kCVImageBufferTransferFunction_ITU_R_2020, "ITU_R_2020");
117 GET_SYM(kCVImageBufferYCbCrMatrix_ITU_R_2020, "ITU_R_2020");
119 GET_SYM(kVTCompressionPropertyKey_H264EntropyMode, "H264EntropyMode");
120 GET_SYM(kVTH264EntropyMode_CAVLC, "CAVLC");
121 GET_SYM(kVTH264EntropyMode_CABAC, "CABAC");
123 GET_SYM(kVTProfileLevel_H264_Baseline_4_0, "H264_Baseline_4_0");
124 GET_SYM(kVTProfileLevel_H264_Baseline_4_2, "H264_Baseline_4_2");
125 GET_SYM(kVTProfileLevel_H264_Baseline_5_0, "H264_Baseline_5_0");
126 GET_SYM(kVTProfileLevel_H264_Baseline_5_1, "H264_Baseline_5_1");
127 GET_SYM(kVTProfileLevel_H264_Baseline_5_2, "H264_Baseline_5_2");
128 GET_SYM(kVTProfileLevel_H264_Baseline_AutoLevel, "H264_Baseline_AutoLevel");
129 GET_SYM(kVTProfileLevel_H264_Main_4_2, "H264_Main_4_2");
130 GET_SYM(kVTProfileLevel_H264_Main_5_1, "H264_Main_5_1");
131 GET_SYM(kVTProfileLevel_H264_Main_5_2, "H264_Main_5_2");
132 GET_SYM(kVTProfileLevel_H264_Main_AutoLevel, "H264_Main_AutoLevel");
133 GET_SYM(kVTProfileLevel_H264_High_3_0, "H264_High_3_0");
134 GET_SYM(kVTProfileLevel_H264_High_3_1, "H264_High_3_1");
135 GET_SYM(kVTProfileLevel_H264_High_3_2, "H264_High_3_2");
136 GET_SYM(kVTProfileLevel_H264_High_4_0, "H264_High_4_0");
137 GET_SYM(kVTProfileLevel_H264_High_4_1, "H264_High_4_1");
138 GET_SYM(kVTProfileLevel_H264_High_4_2, "H264_High_4_2");
139 GET_SYM(kVTProfileLevel_H264_High_5_1, "H264_High_5_1");
140 GET_SYM(kVTProfileLevel_H264_High_5_2, "H264_High_5_2");
141 GET_SYM(kVTProfileLevel_H264_High_AutoLevel, "H264_High_AutoLevel");
142 GET_SYM(kVTProfileLevel_H264_Extended_5_0, "H264_Extended_5_0");
143 GET_SYM(kVTProfileLevel_H264_Extended_AutoLevel, "H264_Extended_AutoLevel");
145 GET_SYM(kVTProfileLevel_HEVC_Main_AutoLevel, "HEVC_Main_AutoLevel");
146 GET_SYM(kVTProfileLevel_HEVC_Main10_AutoLevel, "HEVC_Main10_AutoLevel");
148 GET_SYM(kVTCompressionPropertyKey_RealTime, "RealTime");
150 GET_SYM(kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
151 "EnableHardwareAcceleratedVideoEncoder");
152 GET_SYM(kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
153 "RequireHardwareAcceleratedVideoEncoder");
156 typedef enum VT_H264Profile {
165 typedef enum VTH264Entropy{
171 typedef enum VT_HEVCProfile {
178 static const uint8_t start_code[] = { 0, 0, 0, 1 };
180 typedef struct ExtraSEI {
185 typedef struct BufNode {
186 CMSampleBufferRef cm_buffer;
188 struct BufNode* next;
192 typedef struct VTEncContext {
194 enum AVCodecID codec_id;
195 VTCompressionSessionRef session;
196 CFStringRef ycbcr_matrix;
197 CFStringRef color_primaries;
198 CFStringRef transfer_function;
199 getParameterSetAtIndex get_param_set_func;
201 pthread_mutex_t lock;
202 pthread_cond_t cv_sample_sent;
209 int64_t frame_ct_out;
219 int64_t frames_before;
220 int64_t frames_after;
227 bool warned_color_range;
231 static int vtenc_populate_extradata(AVCodecContext *avctx,
232 CMVideoCodecType codec_type,
233 CFStringRef profile_level,
234 CFNumberRef gamma_level,
235 CFDictionaryRef enc_info,
236 CFDictionaryRef pixel_buffer_info);
239 * NULL-safe release of *refPtr, and sets value to NULL.
241 static void vt_release_num(CFNumberRef* refPtr){
250 static void set_async_error(VTEncContext *vtctx, int err)
254 pthread_mutex_lock(&vtctx->lock);
256 vtctx->async_error = err;
258 info = vtctx->q_head;
259 vtctx->q_head = vtctx->q_tail = NULL;
262 BufNode *next = info->next;
263 CFRelease(info->cm_buffer);
268 pthread_mutex_unlock(&vtctx->lock);
271 static void clear_frame_queue(VTEncContext *vtctx)
273 set_async_error(vtctx, 0);
276 static int vtenc_q_pop(VTEncContext *vtctx, bool wait, CMSampleBufferRef *buf, ExtraSEI **sei)
280 pthread_mutex_lock(&vtctx->lock);
282 if (vtctx->async_error) {
283 pthread_mutex_unlock(&vtctx->lock);
284 return vtctx->async_error;
287 if (vtctx->flushing && vtctx->frame_ct_in == vtctx->frame_ct_out) {
290 pthread_mutex_unlock(&vtctx->lock);
294 while (!vtctx->q_head && !vtctx->async_error && wait) {
295 pthread_cond_wait(&vtctx->cv_sample_sent, &vtctx->lock);
298 if (!vtctx->q_head) {
299 pthread_mutex_unlock(&vtctx->lock);
304 info = vtctx->q_head;
305 vtctx->q_head = vtctx->q_head->next;
306 if (!vtctx->q_head) {
307 vtctx->q_tail = NULL;
310 pthread_mutex_unlock(&vtctx->lock);
312 *buf = info->cm_buffer;
315 } else if (info->sei) {
316 if (info->sei->data) av_free(info->sei->data);
321 vtctx->frame_ct_out++;
326 static void vtenc_q_push(VTEncContext *vtctx, CMSampleBufferRef buffer, ExtraSEI *sei)
328 BufNode *info = av_malloc(sizeof(BufNode));
330 set_async_error(vtctx, AVERROR(ENOMEM));
335 info->cm_buffer = buffer;
339 pthread_mutex_lock(&vtctx->lock);
340 pthread_cond_signal(&vtctx->cv_sample_sent);
342 if (!vtctx->q_head) {
343 vtctx->q_head = info;
345 vtctx->q_tail->next = info;
348 vtctx->q_tail = info;
350 pthread_mutex_unlock(&vtctx->lock);
353 static int count_nalus(size_t length_code_size,
354 CMSampleBufferRef sample_buffer,
361 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
362 CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample_buffer);
364 if (length_code_size > 4)
365 return AVERROR_INVALIDDATA;
367 while (offset < src_size) {
372 status = CMBlockBufferCopyDataBytes(block,
377 for (i = 0; i < length_code_size; i++) {
379 box_len |= size_buf[i];
382 curr_src_len = box_len + length_code_size;
383 offset += curr_src_len;
392 static CMVideoCodecType get_cm_codec_type(enum AVCodecID id)
395 case AV_CODEC_ID_H264: return kCMVideoCodecType_H264;
396 case AV_CODEC_ID_HEVC: return kCMVideoCodecType_HEVC;
402 * Get the parameter sets from a CMSampleBufferRef.
403 * @param dst If *dst isn't NULL, the parameters are copied into existing
404 * memory. *dst_size must be set accordingly when *dst != NULL.
405 * If *dst is NULL, it will be allocated.
406 * In all cases, *dst_size is set to the number of bytes used starting
409 static int get_params_size(
410 AVCodecContext *avctx,
411 CMVideoFormatDescriptionRef vid_fmt,
414 VTEncContext *vtctx = avctx->priv_data;
415 size_t total_size = 0;
417 int is_count_bad = 0;
420 status = vtctx->get_param_set_func(vid_fmt,
432 for (i = 0; i < ps_count || is_count_bad; i++) {
435 status = vtctx->get_param_set_func(vid_fmt,
443 * When ps_count is invalid, status != 0 ends the loop normally
444 * unless we didn't get any parameter sets.
446 if (i > 0 && is_count_bad) status = 0;
451 total_size += ps_size + sizeof(start_code);
455 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set sizes: %d\n", status);
456 return AVERROR_EXTERNAL;
463 static int copy_param_sets(
464 AVCodecContext *avctx,
465 CMVideoFormatDescriptionRef vid_fmt,
469 VTEncContext *vtctx = avctx->priv_data;
471 int is_count_bad = 0;
476 status = vtctx->get_param_set_func(vid_fmt,
489 for (i = 0; i < ps_count || is_count_bad; i++) {
494 status = vtctx->get_param_set_func(vid_fmt,
501 if (i > 0 && is_count_bad) status = 0;
506 next_offset = offset + sizeof(start_code) + ps_size;
507 if (dst_size < next_offset) {
508 av_log(avctx, AV_LOG_ERROR, "Error: buffer too small for parameter sets.\n");
509 return AVERROR_BUFFER_TOO_SMALL;
512 memcpy(dst + offset, start_code, sizeof(start_code));
513 offset += sizeof(start_code);
515 memcpy(dst + offset, ps, ps_size);
516 offset = next_offset;
520 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set data: %d\n", status);
521 return AVERROR_EXTERNAL;
527 static int set_extradata(AVCodecContext *avctx, CMSampleBufferRef sample_buffer)
529 CMVideoFormatDescriptionRef vid_fmt;
533 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
535 av_log(avctx, AV_LOG_ERROR, "No video format.\n");
536 return AVERROR_EXTERNAL;
539 status = get_params_size(avctx, vid_fmt, &total_size);
541 av_log(avctx, AV_LOG_ERROR, "Could not get parameter sets.\n");
545 avctx->extradata = av_mallocz(total_size + AV_INPUT_BUFFER_PADDING_SIZE);
546 if (!avctx->extradata) {
547 return AVERROR(ENOMEM);
549 avctx->extradata_size = total_size;
551 status = copy_param_sets(avctx, vid_fmt, avctx->extradata, total_size);
554 av_log(avctx, AV_LOG_ERROR, "Could not copy param sets.\n");
561 static void vtenc_output_callback(
563 void *sourceFrameCtx,
565 VTEncodeInfoFlags flags,
566 CMSampleBufferRef sample_buffer)
568 AVCodecContext *avctx = ctx;
569 VTEncContext *vtctx = avctx->priv_data;
570 ExtraSEI *sei = sourceFrameCtx;
572 if (vtctx->async_error) {
573 if(sample_buffer) CFRelease(sample_buffer);
578 av_log(avctx, AV_LOG_ERROR, "Error encoding frame: %d\n", (int)status);
579 set_async_error(vtctx, AVERROR_EXTERNAL);
583 if (!sample_buffer) {
587 if (!avctx->extradata && (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
588 int set_status = set_extradata(avctx, sample_buffer);
590 set_async_error(vtctx, set_status);
595 vtenc_q_push(vtctx, sample_buffer, sei);
598 static int get_length_code_size(
599 AVCodecContext *avctx,
600 CMSampleBufferRef sample_buffer,
603 VTEncContext *vtctx = avctx->priv_data;
604 CMVideoFormatDescriptionRef vid_fmt;
608 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
610 av_log(avctx, AV_LOG_ERROR, "Error getting buffer format description.\n");
611 return AVERROR_EXTERNAL;
614 status = vtctx->get_param_set_func(vid_fmt,
621 av_log(avctx, AV_LOG_ERROR, "Error getting length code size: %d\n", status);
622 return AVERROR_EXTERNAL;
630 * Returns true on success.
632 * If profile_level_val is NULL and this method returns true, don't specify the
633 * profile/level to the encoder.
635 static bool get_vt_h264_profile_level(AVCodecContext *avctx,
636 CFStringRef *profile_level_val)
638 VTEncContext *vtctx = avctx->priv_data;
639 int64_t profile = vtctx->profile;
641 if (profile == H264_PROF_AUTO && vtctx->level) {
642 //Need to pick a profile if level is not auto-selected.
643 profile = vtctx->has_b_frames ? H264_PROF_MAIN : H264_PROF_BASELINE;
646 *profile_level_val = NULL;
652 case H264_PROF_BASELINE:
653 switch (vtctx->level) {
654 case 0: *profile_level_val =
655 compat_keys.kVTProfileLevel_H264_Baseline_AutoLevel; break;
656 case 13: *profile_level_val = kVTProfileLevel_H264_Baseline_1_3; break;
657 case 30: *profile_level_val = kVTProfileLevel_H264_Baseline_3_0; break;
658 case 31: *profile_level_val = kVTProfileLevel_H264_Baseline_3_1; break;
659 case 32: *profile_level_val = kVTProfileLevel_H264_Baseline_3_2; break;
660 case 40: *profile_level_val =
661 compat_keys.kVTProfileLevel_H264_Baseline_4_0; break;
662 case 41: *profile_level_val = kVTProfileLevel_H264_Baseline_4_1; break;
663 case 42: *profile_level_val =
664 compat_keys.kVTProfileLevel_H264_Baseline_4_2; break;
665 case 50: *profile_level_val =
666 compat_keys.kVTProfileLevel_H264_Baseline_5_0; break;
667 case 51: *profile_level_val =
668 compat_keys.kVTProfileLevel_H264_Baseline_5_1; break;
669 case 52: *profile_level_val =
670 compat_keys.kVTProfileLevel_H264_Baseline_5_2; break;
675 switch (vtctx->level) {
676 case 0: *profile_level_val =
677 compat_keys.kVTProfileLevel_H264_Main_AutoLevel; break;
678 case 30: *profile_level_val = kVTProfileLevel_H264_Main_3_0; break;
679 case 31: *profile_level_val = kVTProfileLevel_H264_Main_3_1; break;
680 case 32: *profile_level_val = kVTProfileLevel_H264_Main_3_2; break;
681 case 40: *profile_level_val = kVTProfileLevel_H264_Main_4_0; break;
682 case 41: *profile_level_val = kVTProfileLevel_H264_Main_4_1; break;
683 case 42: *profile_level_val =
684 compat_keys.kVTProfileLevel_H264_Main_4_2; break;
685 case 50: *profile_level_val = kVTProfileLevel_H264_Main_5_0; break;
686 case 51: *profile_level_val =
687 compat_keys.kVTProfileLevel_H264_Main_5_1; break;
688 case 52: *profile_level_val =
689 compat_keys.kVTProfileLevel_H264_Main_5_2; break;
694 switch (vtctx->level) {
695 case 0: *profile_level_val =
696 compat_keys.kVTProfileLevel_H264_High_AutoLevel; break;
697 case 30: *profile_level_val =
698 compat_keys.kVTProfileLevel_H264_High_3_0; break;
699 case 31: *profile_level_val =
700 compat_keys.kVTProfileLevel_H264_High_3_1; break;
701 case 32: *profile_level_val =
702 compat_keys.kVTProfileLevel_H264_High_3_2; break;
703 case 40: *profile_level_val =
704 compat_keys.kVTProfileLevel_H264_High_4_0; break;
705 case 41: *profile_level_val =
706 compat_keys.kVTProfileLevel_H264_High_4_1; break;
707 case 42: *profile_level_val =
708 compat_keys.kVTProfileLevel_H264_High_4_2; break;
709 case 50: *profile_level_val = kVTProfileLevel_H264_High_5_0; break;
710 case 51: *profile_level_val =
711 compat_keys.kVTProfileLevel_H264_High_5_1; break;
712 case 52: *profile_level_val =
713 compat_keys.kVTProfileLevel_H264_High_5_2; break;
716 case H264_PROF_EXTENDED:
717 switch (vtctx->level) {
718 case 0: *profile_level_val =
719 compat_keys.kVTProfileLevel_H264_Extended_AutoLevel; break;
720 case 50: *profile_level_val =
721 compat_keys.kVTProfileLevel_H264_Extended_5_0; break;
726 if (!*profile_level_val) {
727 av_log(avctx, AV_LOG_ERROR, "Invalid Profile/Level.\n");
735 * Returns true on success.
737 * If profile_level_val is NULL and this method returns true, don't specify the
738 * profile/level to the encoder.
740 static bool get_vt_hevc_profile_level(AVCodecContext *avctx,
741 CFStringRef *profile_level_val)
743 VTEncContext *vtctx = avctx->priv_data;
744 int64_t profile = vtctx->profile;
746 *profile_level_val = NULL;
753 compat_keys.kVTProfileLevel_HEVC_Main_AutoLevel;
755 case HEVC_PROF_MAIN10:
757 compat_keys.kVTProfileLevel_HEVC_Main10_AutoLevel;
761 if (!*profile_level_val) {
762 av_log(avctx, AV_LOG_ERROR, "Invalid Profile/Level.\n");
769 static int get_cv_pixel_format(AVCodecContext* avctx,
770 enum AVPixelFormat fmt,
771 enum AVColorRange range,
772 int* av_pixel_format,
775 if (range_guessed) *range_guessed = range != AVCOL_RANGE_MPEG &&
776 range != AVCOL_RANGE_JPEG;
778 //MPEG range is used when no range is set
779 if (fmt == AV_PIX_FMT_NV12) {
780 *av_pixel_format = range == AVCOL_RANGE_JPEG ?
781 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange :
782 kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
783 } else if (fmt == AV_PIX_FMT_YUV420P) {
784 *av_pixel_format = range == AVCOL_RANGE_JPEG ?
785 kCVPixelFormatType_420YpCbCr8PlanarFullRange :
786 kCVPixelFormatType_420YpCbCr8Planar;
787 } else if (fmt == AV_PIX_FMT_P010LE) {
788 *av_pixel_format = range == AVCOL_RANGE_JPEG ?
789 kCVPixelFormatType_420YpCbCr10BiPlanarFullRange :
790 kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange;
791 *av_pixel_format = kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange;
793 return AVERROR(EINVAL);
799 static void add_color_attr(AVCodecContext *avctx, CFMutableDictionaryRef dict) {
800 VTEncContext *vtctx = avctx->priv_data;
802 if (vtctx->color_primaries) {
803 CFDictionarySetValue(dict,
804 kCVImageBufferColorPrimariesKey,
805 vtctx->color_primaries);
808 if (vtctx->transfer_function) {
809 CFDictionarySetValue(dict,
810 kCVImageBufferTransferFunctionKey,
811 vtctx->transfer_function);
814 if (vtctx->ycbcr_matrix) {
815 CFDictionarySetValue(dict,
816 kCVImageBufferYCbCrMatrixKey,
817 vtctx->ycbcr_matrix);
821 static int create_cv_pixel_buffer_info(AVCodecContext* avctx,
822 CFMutableDictionaryRef* dict)
824 CFNumberRef cv_color_format_num = NULL;
825 CFNumberRef width_num = NULL;
826 CFNumberRef height_num = NULL;
827 CFMutableDictionaryRef pixel_buffer_info = NULL;
829 int status = get_cv_pixel_format(avctx,
834 if (status) return status;
836 pixel_buffer_info = CFDictionaryCreateMutable(
839 &kCFCopyStringDictionaryKeyCallBacks,
840 &kCFTypeDictionaryValueCallBacks);
842 if (!pixel_buffer_info) goto pbinfo_nomem;
844 cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
847 if (!cv_color_format_num) goto pbinfo_nomem;
849 CFDictionarySetValue(pixel_buffer_info,
850 kCVPixelBufferPixelFormatTypeKey,
851 cv_color_format_num);
852 vt_release_num(&cv_color_format_num);
854 width_num = CFNumberCreate(kCFAllocatorDefault,
857 if (!width_num) return AVERROR(ENOMEM);
859 CFDictionarySetValue(pixel_buffer_info,
860 kCVPixelBufferWidthKey,
862 vt_release_num(&width_num);
864 height_num = CFNumberCreate(kCFAllocatorDefault,
867 if (!height_num) goto pbinfo_nomem;
869 CFDictionarySetValue(pixel_buffer_info,
870 kCVPixelBufferHeightKey,
872 vt_release_num(&height_num);
874 add_color_attr(avctx, pixel_buffer_info);
876 *dict = pixel_buffer_info;
880 vt_release_num(&cv_color_format_num);
881 vt_release_num(&width_num);
882 vt_release_num(&height_num);
883 if (pixel_buffer_info) CFRelease(pixel_buffer_info);
885 return AVERROR(ENOMEM);
888 static int get_cv_color_primaries(AVCodecContext *avctx,
889 CFStringRef *primaries)
891 enum AVColorPrimaries pri = avctx->color_primaries;
893 case AVCOL_PRI_UNSPECIFIED:
897 case AVCOL_PRI_BT709:
898 *primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
901 case AVCOL_PRI_BT2020:
902 *primaries = compat_keys.kCVImageBufferColorPrimaries_ITU_R_2020;
906 av_log(avctx, AV_LOG_ERROR, "Color primaries %s is not supported.\n", av_color_primaries_name(pri));
914 static int get_cv_transfer_function(AVCodecContext *avctx,
915 CFStringRef *transfer_fnc,
916 CFNumberRef *gamma_level)
918 enum AVColorTransferCharacteristic trc = avctx->color_trc;
923 case AVCOL_TRC_UNSPECIFIED:
924 *transfer_fnc = NULL;
927 case AVCOL_TRC_BT709:
928 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_709_2;
931 case AVCOL_TRC_SMPTE240M:
932 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_240M_1995;
935 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_SMPTE_ST_2084_PQ
936 case AVCOL_TRC_SMPTE2084:
937 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ;
940 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_LINEAR
941 case AVCOL_TRC_LINEAR:
942 *transfer_fnc = kCVImageBufferTransferFunction_Linear;
945 #if HAVE_KCVIMAGEBUFFERTRANSFERFUNCTION_ITU_R_2100_HLG
946 case AVCOL_TRC_ARIB_STD_B67:
947 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_2100_HLG;
951 case AVCOL_TRC_GAMMA22:
953 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
954 *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type, &gamma);
957 case AVCOL_TRC_GAMMA28:
959 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
960 *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type, &gamma);
963 case AVCOL_TRC_BT2020_10:
964 case AVCOL_TRC_BT2020_12:
965 *transfer_fnc = compat_keys.kCVImageBufferTransferFunction_ITU_R_2020;
969 *transfer_fnc = NULL;
970 av_log(avctx, AV_LOG_ERROR, "Transfer function %s is not supported.\n", av_color_transfer_name(trc));
977 static int get_cv_ycbcr_matrix(AVCodecContext *avctx, CFStringRef *matrix) {
978 switch(avctx->colorspace) {
979 case AVCOL_SPC_BT709:
980 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_709_2;
983 case AVCOL_SPC_UNSPECIFIED:
987 case AVCOL_SPC_BT470BG:
988 case AVCOL_SPC_SMPTE170M:
989 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_601_4;
992 case AVCOL_SPC_SMPTE240M:
993 *matrix = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995;
996 case AVCOL_SPC_BT2020_NCL:
997 *matrix = compat_keys.kCVImageBufferYCbCrMatrix_ITU_R_2020;
1001 av_log(avctx, AV_LOG_ERROR, "Color space %s is not supported.\n", av_color_space_name(avctx->colorspace));
1008 static int vtenc_create_encoder(AVCodecContext *avctx,
1009 CMVideoCodecType codec_type,
1010 CFStringRef profile_level,
1011 CFNumberRef gamma_level,
1012 CFDictionaryRef enc_info,
1013 CFDictionaryRef pixel_buffer_info,
1014 VTCompressionSessionRef *session)
1016 VTEncContext *vtctx = avctx->priv_data;
1017 SInt32 bit_rate = avctx->bit_rate;
1018 SInt32 max_rate = avctx->rc_max_rate;
1019 CFNumberRef bit_rate_num;
1020 CFNumberRef bytes_per_second;
1021 CFNumberRef one_second;
1022 CFArrayRef data_rate_limits;
1023 int64_t bytes_per_second_value = 0;
1024 int64_t one_second_value = 0;
1027 int status = VTCompressionSessionCreate(kCFAllocatorDefault,
1033 kCFAllocatorDefault,
1034 vtenc_output_callback,
1038 if (status || !vtctx->session) {
1039 av_log(avctx, AV_LOG_ERROR, "Error: cannot create compression session: %d\n", status);
1041 #if !TARGET_OS_IPHONE
1042 if (!vtctx->allow_sw) {
1043 av_log(avctx, AV_LOG_ERROR, "Try -allow_sw 1. The hardware encoder may be busy, or not supported.\n");
1047 return AVERROR_EXTERNAL;
1050 bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
1051 kCFNumberSInt32Type,
1053 if (!bit_rate_num) return AVERROR(ENOMEM);
1055 status = VTSessionSetProperty(vtctx->session,
1056 kVTCompressionPropertyKey_AverageBitRate,
1058 CFRelease(bit_rate_num);
1061 av_log(avctx, AV_LOG_ERROR, "Error setting bitrate property: %d\n", status);
1062 return AVERROR_EXTERNAL;
1065 if (vtctx->codec_id == AV_CODEC_ID_H264 && max_rate > 0) {
1066 // kVTCompressionPropertyKey_DataRateLimits is not available for HEVC
1067 bytes_per_second_value = max_rate >> 3;
1068 bytes_per_second = CFNumberCreate(kCFAllocatorDefault,
1069 kCFNumberSInt64Type,
1070 &bytes_per_second_value);
1071 if (!bytes_per_second) {
1072 return AVERROR(ENOMEM);
1074 one_second_value = 1;
1075 one_second = CFNumberCreate(kCFAllocatorDefault,
1076 kCFNumberSInt64Type,
1079 CFRelease(bytes_per_second);
1080 return AVERROR(ENOMEM);
1082 nums[0] = (void *)bytes_per_second;
1083 nums[1] = (void *)one_second;
1084 data_rate_limits = CFArrayCreate(kCFAllocatorDefault,
1085 (const void **)nums,
1087 &kCFTypeArrayCallBacks);
1089 if (!data_rate_limits) {
1090 CFRelease(bytes_per_second);
1091 CFRelease(one_second);
1092 return AVERROR(ENOMEM);
1094 status = VTSessionSetProperty(vtctx->session,
1095 kVTCompressionPropertyKey_DataRateLimits,
1098 CFRelease(bytes_per_second);
1099 CFRelease(one_second);
1100 CFRelease(data_rate_limits);
1103 av_log(avctx, AV_LOG_ERROR, "Error setting max bitrate property: %d\n", status);
1104 return AVERROR_EXTERNAL;
1108 if (vtctx->codec_id == AV_CODEC_ID_H264) {
1109 // kVTCompressionPropertyKey_ProfileLevel is not available for HEVC
1110 if (profile_level) {
1111 status = VTSessionSetProperty(vtctx->session,
1112 kVTCompressionPropertyKey_ProfileLevel,
1115 av_log(avctx, AV_LOG_ERROR, "Error setting profile/level property: %d. Output will be encoded using a supported profile/level combination.\n", status);
1120 if (avctx->gop_size > 0) {
1121 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
1125 return AVERROR(ENOMEM);
1128 status = VTSessionSetProperty(vtctx->session,
1129 kVTCompressionPropertyKey_MaxKeyFrameInterval,
1131 CFRelease(interval);
1134 av_log(avctx, AV_LOG_ERROR, "Error setting 'max key-frame interval' property: %d\n", status);
1135 return AVERROR_EXTERNAL;
1139 if (vtctx->frames_before) {
1140 status = VTSessionSetProperty(vtctx->session,
1141 kVTCompressionPropertyKey_MoreFramesBeforeStart,
1144 if (status == kVTPropertyNotSupportedErr) {
1145 av_log(avctx, AV_LOG_WARNING, "frames_before property is not supported on this device. Ignoring.\n");
1146 } else if (status) {
1147 av_log(avctx, AV_LOG_ERROR, "Error setting frames_before property: %d\n", status);
1151 if (vtctx->frames_after) {
1152 status = VTSessionSetProperty(vtctx->session,
1153 kVTCompressionPropertyKey_MoreFramesAfterEnd,
1156 if (status == kVTPropertyNotSupportedErr) {
1157 av_log(avctx, AV_LOG_WARNING, "frames_after property is not supported on this device. Ignoring.\n");
1158 } else if (status) {
1159 av_log(avctx, AV_LOG_ERROR, "Error setting frames_after property: %d\n", status);
1163 if (avctx->sample_aspect_ratio.num != 0) {
1166 CFMutableDictionaryRef par;
1167 AVRational *avpar = &avctx->sample_aspect_ratio;
1169 av_reduce(&avpar->num, &avpar->den,
1170 avpar->num, avpar->den,
1173 num = CFNumberCreate(kCFAllocatorDefault,
1177 den = CFNumberCreate(kCFAllocatorDefault,
1183 par = CFDictionaryCreateMutable(kCFAllocatorDefault,
1185 &kCFCopyStringDictionaryKeyCallBacks,
1186 &kCFTypeDictionaryValueCallBacks);
1188 if (!par || !num || !den) {
1189 if (par) CFRelease(par);
1190 if (num) CFRelease(num);
1191 if (den) CFRelease(den);
1193 return AVERROR(ENOMEM);
1196 CFDictionarySetValue(
1198 kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
1201 CFDictionarySetValue(
1203 kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
1206 status = VTSessionSetProperty(vtctx->session,
1207 kVTCompressionPropertyKey_PixelAspectRatio,
1217 "Error setting pixel aspect ratio to %d:%d: %d.\n",
1218 avctx->sample_aspect_ratio.num,
1219 avctx->sample_aspect_ratio.den,
1222 return AVERROR_EXTERNAL;
1227 if (vtctx->transfer_function) {
1228 status = VTSessionSetProperty(vtctx->session,
1229 kVTCompressionPropertyKey_TransferFunction,
1230 vtctx->transfer_function);
1233 av_log(avctx, AV_LOG_WARNING, "Could not set transfer function: %d\n", status);
1238 if (vtctx->ycbcr_matrix) {
1239 status = VTSessionSetProperty(vtctx->session,
1240 kVTCompressionPropertyKey_YCbCrMatrix,
1241 vtctx->ycbcr_matrix);
1244 av_log(avctx, AV_LOG_WARNING, "Could not set ycbcr matrix: %d\n", status);
1249 if (vtctx->color_primaries) {
1250 status = VTSessionSetProperty(vtctx->session,
1251 kVTCompressionPropertyKey_ColorPrimaries,
1252 vtctx->color_primaries);
1255 av_log(avctx, AV_LOG_WARNING, "Could not set color primaries: %d\n", status);
1260 status = VTSessionSetProperty(vtctx->session,
1261 kCVImageBufferGammaLevelKey,
1265 av_log(avctx, AV_LOG_WARNING, "Could not set gamma level: %d\n", status);
1269 if (!vtctx->has_b_frames) {
1270 status = VTSessionSetProperty(vtctx->session,
1271 kVTCompressionPropertyKey_AllowFrameReordering,
1275 av_log(avctx, AV_LOG_ERROR, "Error setting 'allow frame reordering' property: %d\n", status);
1276 return AVERROR_EXTERNAL;
1280 if (vtctx->entropy != VT_ENTROPY_NOT_SET) {
1281 CFStringRef entropy = vtctx->entropy == VT_CABAC ?
1282 compat_keys.kVTH264EntropyMode_CABAC:
1283 compat_keys.kVTH264EntropyMode_CAVLC;
1285 status = VTSessionSetProperty(vtctx->session,
1286 compat_keys.kVTCompressionPropertyKey_H264EntropyMode,
1290 av_log(avctx, AV_LOG_ERROR, "Error setting entropy property: %d\n", status);
1294 if (vtctx->realtime) {
1295 status = VTSessionSetProperty(vtctx->session,
1296 compat_keys.kVTCompressionPropertyKey_RealTime,
1300 av_log(avctx, AV_LOG_ERROR, "Error setting realtime property: %d\n", status);
1304 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
1306 av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", status);
1307 return AVERROR_EXTERNAL;
1313 static int vtenc_configure_encoder(AVCodecContext *avctx)
1315 CFMutableDictionaryRef enc_info;
1316 CFMutableDictionaryRef pixel_buffer_info;
1317 CMVideoCodecType codec_type;
1318 VTEncContext *vtctx = avctx->priv_data;
1319 CFStringRef profile_level;
1320 CFNumberRef gamma_level = NULL;
1323 codec_type = get_cm_codec_type(avctx->codec_id);
1325 av_log(avctx, AV_LOG_ERROR, "Error: no mapping for AVCodecID %d\n", avctx->codec_id);
1326 return AVERROR(EINVAL);
1329 vtctx->codec_id = avctx->codec_id;
1331 if (vtctx->codec_id == AV_CODEC_ID_H264) {
1332 vtctx->get_param_set_func = CMVideoFormatDescriptionGetH264ParameterSetAtIndex;
1334 vtctx->has_b_frames = avctx->max_b_frames > 0;
1335 if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){
1336 av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");
1337 vtctx->has_b_frames = false;
1340 if (vtctx->entropy == VT_CABAC && vtctx->profile == H264_PROF_BASELINE) {
1341 av_log(avctx, AV_LOG_WARNING, "CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n");
1342 vtctx->entropy = VT_ENTROPY_NOT_SET;
1345 if (!get_vt_h264_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);
1347 vtctx->get_param_set_func = compat_keys.CMVideoFormatDescriptionGetHEVCParameterSetAtIndex;
1348 if (!vtctx->get_param_set_func) return AVERROR(EINVAL);
1349 if (!get_vt_hevc_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);
1352 enc_info = CFDictionaryCreateMutable(
1353 kCFAllocatorDefault,
1355 &kCFCopyStringDictionaryKeyCallBacks,
1356 &kCFTypeDictionaryValueCallBacks
1359 if (!enc_info) return AVERROR(ENOMEM);
1361 #if !TARGET_OS_IPHONE
1362 if(vtctx->require_sw) {
1363 CFDictionarySetValue(enc_info,
1364 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1366 } else if (!vtctx->allow_sw) {
1367 CFDictionarySetValue(enc_info,
1368 compat_keys.kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
1371 CFDictionarySetValue(enc_info,
1372 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1377 if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) {
1378 status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);
1382 pixel_buffer_info = NULL;
1385 vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0;
1387 get_cv_transfer_function(avctx, &vtctx->transfer_function, &gamma_level);
1388 get_cv_ycbcr_matrix(avctx, &vtctx->ycbcr_matrix);
1389 get_cv_color_primaries(avctx, &vtctx->color_primaries);
1392 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1393 status = vtenc_populate_extradata(avctx,
1403 status = vtenc_create_encoder(avctx,
1413 CFRelease(gamma_level);
1415 if (pixel_buffer_info)
1416 CFRelease(pixel_buffer_info);
1418 CFRelease(enc_info);
1423 static av_cold int vtenc_init(AVCodecContext *avctx)
1425 VTEncContext *vtctx = avctx->priv_data;
1426 CFBooleanRef has_b_frames_cfbool;
1429 pthread_once(&once_ctrl, loadVTEncSymbols);
1431 pthread_mutex_init(&vtctx->lock, NULL);
1432 pthread_cond_init(&vtctx->cv_sample_sent, NULL);
1434 vtctx->session = NULL;
1435 status = vtenc_configure_encoder(avctx);
1436 if (status) return status;
1438 status = VTSessionCopyProperty(vtctx->session,
1439 kVTCompressionPropertyKey_AllowFrameReordering,
1440 kCFAllocatorDefault,
1441 &has_b_frames_cfbool);
1443 if (!status && has_b_frames_cfbool) {
1444 //Some devices don't output B-frames for main profile, even if requested.
1445 vtctx->has_b_frames = CFBooleanGetValue(has_b_frames_cfbool);
1446 CFRelease(has_b_frames_cfbool);
1448 avctx->has_b_frames = vtctx->has_b_frames;
1453 static void vtenc_get_frame_info(CMSampleBufferRef buffer, bool *is_key_frame)
1455 CFArrayRef attachments;
1456 CFDictionaryRef attachment;
1457 CFBooleanRef not_sync;
1460 attachments = CMSampleBufferGetSampleAttachmentsArray(buffer, false);
1461 len = !attachments ? 0 : CFArrayGetCount(attachments);
1464 *is_key_frame = true;
1468 attachment = CFArrayGetValueAtIndex(attachments, 0);
1470 if (CFDictionaryGetValueIfPresent(attachment,
1471 kCMSampleAttachmentKey_NotSync,
1472 (const void **)¬_sync))
1474 *is_key_frame = !CFBooleanGetValue(not_sync);
1476 *is_key_frame = true;
1480 static int is_post_sei_nal_type(int nal_type){
1481 return nal_type != H264_NAL_SEI &&
1482 nal_type != H264_NAL_SPS &&
1483 nal_type != H264_NAL_PPS &&
1484 nal_type != H264_NAL_AUD;
1488 * Finds the sei message start/size of type find_sei_type.
1489 * If more than one of that type exists, the last one is returned.
1491 static int find_sei_end(AVCodecContext *avctx,
1497 size_t sei_payload_size = 0;
1498 int sei_payload_type = 0;
1500 uint8_t *nal_start = nal_data;
1505 nal_type = *nal_data & 0x1F;
1506 if (nal_type != H264_NAL_SEI)
1512 if (nal_data[nal_size - 1] == 0x80)
1515 while (nal_size > 0 && *nal_data > 0) {
1517 sei_payload_type += *nal_data;
1520 } while (nal_size > 0 && *nal_data == 0xFF);
1523 av_log(avctx, AV_LOG_ERROR, "Unexpected end of SEI NAL Unit parsing type.\n");
1524 return AVERROR_INVALIDDATA;
1528 sei_payload_size += *nal_data;
1531 } while (nal_size > 0 && *nal_data == 0xFF);
1533 if (nal_size < sei_payload_size) {
1534 av_log(avctx, AV_LOG_ERROR, "Unexpected end of SEI NAL Unit parsing size.\n");
1535 return AVERROR_INVALIDDATA;
1538 nal_data += sei_payload_size;
1539 nal_size -= sei_payload_size;
1542 *sei_end = nal_data;
1544 return nal_data - nal_start + 1;
1548 * Copies the data inserting emulation prevention bytes as needed.
1549 * Existing data in the destination can be taken into account by providing
1550 * dst with a dst_offset > 0.
1552 * @return The number of bytes copied on success. On failure, the negative of
1553 * the number of bytes needed to copy src is returned.
1555 static int copy_emulation_prev(const uint8_t *src,
1564 uint8_t* dst_end = dst + dst_size;
1565 const uint8_t* src_end = src + src_size;
1566 int start_at = dst_offset > 2 ? dst_offset - 2 : 0;
1568 for (i = start_at; i < dst_offset && i < dst_size; i++) {
1577 for (; src < src_end; src++, dst++) {
1579 int insert_ep3_byte = *src <= 3;
1580 if (insert_ep3_byte) {
1598 wrote_bytes = dst - dst_start;
1601 return -wrote_bytes;
1606 static int write_sei(const ExtraSEI *sei,
1611 uint8_t *sei_start = dst;
1612 size_t remaining_sei_size = sei->size;
1613 size_t remaining_dst_size = dst_size;
1618 if (!remaining_dst_size)
1619 return AVERROR_BUFFER_TOO_SMALL;
1621 while (sei_type && remaining_dst_size != 0) {
1622 int sei_byte = sei_type > 255 ? 255 : sei_type;
1625 sei_type -= sei_byte;
1627 remaining_dst_size--;
1631 return AVERROR_BUFFER_TOO_SMALL;
1633 while (remaining_sei_size && remaining_dst_size != 0) {
1634 int size_byte = remaining_sei_size > 255 ? 255 : remaining_sei_size;
1637 remaining_sei_size -= size_byte;
1639 remaining_dst_size--;
1642 if (remaining_dst_size < sei->size)
1643 return AVERROR_BUFFER_TOO_SMALL;
1645 header_bytes = dst - sei_start;
1647 offset = header_bytes;
1648 bytes_written = copy_emulation_prev(sei->data,
1653 if (bytes_written < 0)
1654 return AVERROR_BUFFER_TOO_SMALL;
1656 bytes_written += header_bytes;
1657 return bytes_written;
1661 * Copies NAL units and replaces length codes with
1662 * H.264 Annex B start codes. On failure, the contents of
1663 * dst_data may have been modified.
1665 * @param length_code_size Byte length of each length code
1666 * @param sample_buffer NAL units prefixed with length codes.
1667 * @param sei Optional A53 closed captions SEI data.
1668 * @param dst_data Must be zeroed before calling this function.
1669 * Contains the copied NAL units prefixed with
1670 * start codes when the function returns
1672 * @param dst_size Length of dst_data
1673 * @return 0 on success
1674 * AVERROR_INVALIDDATA if length_code_size is invalid
1675 * AVERROR_BUFFER_TOO_SMALL if dst_data is too small
1676 * or if a length_code in src_data specifies data beyond
1677 * the end of its buffer.
1679 static int copy_replace_length_codes(
1680 AVCodecContext *avctx,
1681 size_t length_code_size,
1682 CMSampleBufferRef sample_buffer,
1687 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1688 size_t remaining_src_size = src_size;
1689 size_t remaining_dst_size = dst_size;
1690 size_t src_offset = 0;
1693 uint8_t size_buf[4];
1695 CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample_buffer);
1697 if (length_code_size > 4) {
1698 return AVERROR_INVALIDDATA;
1701 while (remaining_src_size > 0) {
1702 size_t curr_src_len;
1703 size_t curr_dst_len;
1709 status = CMBlockBufferCopyDataBytes(block,
1714 av_log(avctx, AV_LOG_ERROR, "Cannot copy length: %d\n", status);
1715 return AVERROR_EXTERNAL;
1718 status = CMBlockBufferCopyDataBytes(block,
1719 src_offset + length_code_size,
1724 av_log(avctx, AV_LOG_ERROR, "Cannot copy type: %d\n", status);
1725 return AVERROR_EXTERNAL;
1730 for (i = 0; i < length_code_size; i++) {
1732 box_len |= size_buf[i];
1735 if (sei && !wrote_sei && is_post_sei_nal_type(nal_type)) {
1736 //No SEI NAL unit - insert.
1739 memcpy(dst_data, start_code, sizeof(start_code));
1740 dst_data += sizeof(start_code);
1741 remaining_dst_size -= sizeof(start_code);
1743 *dst_data = H264_NAL_SEI;
1745 remaining_dst_size--;
1747 wrote_bytes = write_sei(sei,
1748 H264_SEI_TYPE_USER_DATA_REGISTERED,
1750 remaining_dst_size);
1752 if (wrote_bytes < 0)
1755 remaining_dst_size -= wrote_bytes;
1756 dst_data += wrote_bytes;
1758 if (remaining_dst_size <= 0)
1759 return AVERROR_BUFFER_TOO_SMALL;
1764 remaining_dst_size--;
1769 curr_src_len = box_len + length_code_size;
1770 curr_dst_len = box_len + sizeof(start_code);
1772 if (remaining_src_size < curr_src_len) {
1773 return AVERROR_BUFFER_TOO_SMALL;
1776 if (remaining_dst_size < curr_dst_len) {
1777 return AVERROR_BUFFER_TOO_SMALL;
1780 dst_box = dst_data + sizeof(start_code);
1782 memcpy(dst_data, start_code, sizeof(start_code));
1783 status = CMBlockBufferCopyDataBytes(block,
1784 src_offset + length_code_size,
1789 av_log(avctx, AV_LOG_ERROR, "Cannot copy data: %d\n", status);
1790 return AVERROR_EXTERNAL;
1793 if (sei && !wrote_sei && nal_type == H264_NAL_SEI) {
1794 //Found SEI NAL unit - append.
1799 old_sei_length = find_sei_end(avctx, dst_box, box_len, &new_sei);
1800 if (old_sei_length < 0)
1803 wrote_bytes = write_sei(sei,
1804 H264_SEI_TYPE_USER_DATA_REGISTERED,
1806 remaining_dst_size - old_sei_length);
1807 if (wrote_bytes < 0)
1810 if (new_sei + wrote_bytes >= dst_data + remaining_dst_size)
1811 return AVERROR_BUFFER_TOO_SMALL;
1813 new_sei[wrote_bytes++] = 0x80;
1814 extra_bytes = wrote_bytes - (dst_box + box_len - new_sei);
1816 dst_data += extra_bytes;
1817 remaining_dst_size -= extra_bytes;
1822 src_offset += curr_src_len;
1823 dst_data += curr_dst_len;
1825 remaining_src_size -= curr_src_len;
1826 remaining_dst_size -= curr_dst_len;
1833 * Returns a sufficient number of bytes to contain the sei data.
1834 * It may be greater than the minimum required.
1836 static int get_sei_msg_bytes(const ExtraSEI* sei, int type){
1841 copied_size = -copy_emulation_prev(sei->data,
1847 if ((sei->size % 255) == 0) //may result in an extra byte
1850 return copied_size + sei->size / 255 + 1 + type / 255 + 1;
1853 static int vtenc_cm_to_avpacket(
1854 AVCodecContext *avctx,
1855 CMSampleBufferRef sample_buffer,
1859 VTEncContext *vtctx = avctx->priv_data;
1864 size_t length_code_size;
1865 size_t header_size = 0;
1867 size_t out_buf_size;
1868 size_t sei_nalu_size = 0;
1870 int64_t time_base_num;
1874 CMVideoFormatDescriptionRef vid_fmt;
1877 vtenc_get_frame_info(sample_buffer, &is_key_frame);
1878 status = get_length_code_size(avctx, sample_buffer, &length_code_size);
1879 if (status) return status;
1881 add_header = is_key_frame && !(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER);
1884 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
1886 av_log(avctx, AV_LOG_ERROR, "Cannot get format description.\n");
1887 return AVERROR_EXTERNAL;
1890 int status = get_params_size(avctx, vid_fmt, &header_size);
1891 if (status) return status;
1894 status = count_nalus(length_code_size, sample_buffer, &nalu_count);
1899 size_t msg_size = get_sei_msg_bytes(sei,
1900 H264_SEI_TYPE_USER_DATA_REGISTERED);
1902 sei_nalu_size = sizeof(start_code) + 1 + msg_size + 1;
1905 in_buf_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1906 out_buf_size = header_size +
1909 nalu_count * ((int)sizeof(start_code) - (int)length_code_size);
1911 status = ff_alloc_packet2(avctx, pkt, out_buf_size, out_buf_size);
1916 status = copy_param_sets(avctx, vid_fmt, pkt->data, out_buf_size);
1917 if(status) return status;
1920 status = copy_replace_length_codes(
1925 pkt->data + header_size,
1926 pkt->size - header_size
1930 av_log(avctx, AV_LOG_ERROR, "Error copying packet data: %d\n", status);
1935 pkt->flags |= AV_PKT_FLAG_KEY;
1938 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
1939 dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
1941 if (CMTIME_IS_INVALID(dts)) {
1942 if (!vtctx->has_b_frames) {
1945 av_log(avctx, AV_LOG_ERROR, "DTS is invalid.\n");
1946 return AVERROR_EXTERNAL;
1950 dts_delta = vtctx->dts_delta >= 0 ? vtctx->dts_delta : 0;
1951 time_base_num = avctx->time_base.num;
1952 pkt->pts = pts.value / time_base_num;
1953 pkt->dts = dts.value / time_base_num - dts_delta;
1954 pkt->size = out_buf_size;
1960 * contiguous_buf_size is 0 if not contiguous, and the size of the buffer
1961 * containing all planes if so.
1963 static int get_cv_pixel_info(
1964 AVCodecContext *avctx,
1965 const AVFrame *frame,
1971 size_t *contiguous_buf_size)
1973 VTEncContext *vtctx = avctx->priv_data;
1974 int av_format = frame->format;
1975 int av_color_range = frame->color_range;
1980 status = get_cv_pixel_format(avctx, av_format, av_color_range, color, &range_guessed);
1984 "Could not get pixel format for color format '%s' range '%s'.\n",
1985 av_get_pix_fmt_name(av_format),
1986 av_color_range > AVCOL_RANGE_UNSPECIFIED &&
1987 av_color_range < AVCOL_RANGE_NB ?
1988 av_color_range_name(av_color_range) :
1991 return AVERROR(EINVAL);
1994 if (range_guessed) {
1995 if (!vtctx->warned_color_range) {
1996 vtctx->warned_color_range = true;
1999 "Color range not set for %s. Using MPEG range.\n",
2000 av_get_pix_fmt_name(av_format));
2004 switch (av_format) {
2005 case AV_PIX_FMT_NV12:
2008 widths [0] = avctx->width;
2009 heights[0] = avctx->height;
2010 strides[0] = frame ? frame->linesize[0] : avctx->width;
2012 widths [1] = (avctx->width + 1) / 2;
2013 heights[1] = (avctx->height + 1) / 2;
2014 strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) & -2;
2017 case AV_PIX_FMT_YUV420P:
2020 widths [0] = avctx->width;
2021 heights[0] = avctx->height;
2022 strides[0] = frame ? frame->linesize[0] : avctx->width;
2024 widths [1] = (avctx->width + 1) / 2;
2025 heights[1] = (avctx->height + 1) / 2;
2026 strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) / 2;
2028 widths [2] = (avctx->width + 1) / 2;
2029 heights[2] = (avctx->height + 1) / 2;
2030 strides[2] = frame ? frame->linesize[2] : (avctx->width + 1) / 2;
2033 case AV_PIX_FMT_P010LE:
2035 widths[0] = avctx->width;
2036 heights[0] = avctx->height;
2037 strides[0] = frame ? frame->linesize[0] : (avctx->width * 2 + 63) & -64;
2039 widths[1] = (avctx->width + 1) / 2;
2040 heights[1] = (avctx->height + 1) / 2;
2041 strides[1] = frame ? frame->linesize[1] : ((avctx->width + 1) / 2 + 63) & -64;
2048 "Could not get frame format info for color %d range %d.\n",
2052 return AVERROR(EINVAL);
2055 *contiguous_buf_size = 0;
2056 for (i = 0; i < *plane_count; i++) {
2057 if (i < *plane_count - 1 &&
2058 frame->data[i] + strides[i] * heights[i] != frame->data[i + 1]) {
2059 *contiguous_buf_size = 0;
2063 *contiguous_buf_size += strides[i] * heights[i];
2069 //Not used on OSX - frame is never copied.
2070 static int copy_avframe_to_pixel_buffer(AVCodecContext *avctx,
2071 const AVFrame *frame,
2072 CVPixelBufferRef cv_img,
2073 const size_t *plane_strides,
2074 const size_t *plane_rows)
2086 status = CVPixelBufferLockBaseAddress(cv_img, 0);
2091 "Error: Could not lock base address of CVPixelBuffer: %d.\n",
2096 if (CVPixelBufferIsPlanar(cv_img)) {
2097 plane_count = CVPixelBufferGetPlaneCount(cv_img);
2098 for (i = 0; frame->data[i]; i++) {
2099 if (i == plane_count) {
2100 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2103 "Error: different number of planes in AVFrame and CVPixelBuffer.\n"
2106 return AVERROR_EXTERNAL;
2109 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(cv_img, i);
2110 src_addr = (uint8_t*)frame->data[i];
2111 dst_stride = CVPixelBufferGetBytesPerRowOfPlane(cv_img, i);
2112 src_stride = plane_strides[i];
2113 rows = plane_rows[i];
2115 if (dst_stride == src_stride) {
2116 memcpy(dst_addr, src_addr, src_stride * rows);
2118 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2120 for (j = 0; j < rows; j++) {
2121 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2126 if (frame->data[1]) {
2127 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2130 "Error: different number of planes in AVFrame and non-planar CVPixelBuffer.\n"
2133 return AVERROR_EXTERNAL;
2136 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddress(cv_img);
2137 src_addr = (uint8_t*)frame->data[0];
2138 dst_stride = CVPixelBufferGetBytesPerRow(cv_img);
2139 src_stride = plane_strides[0];
2140 rows = plane_rows[0];
2142 if (dst_stride == src_stride) {
2143 memcpy(dst_addr, src_addr, src_stride * rows);
2145 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2147 for (j = 0; j < rows; j++) {
2148 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2153 status = CVPixelBufferUnlockBaseAddress(cv_img, 0);
2155 av_log(avctx, AV_LOG_ERROR, "Error: Could not unlock CVPixelBuffer base address: %d.\n", status);
2156 return AVERROR_EXTERNAL;
2162 static int create_cv_pixel_buffer(AVCodecContext *avctx,
2163 const AVFrame *frame,
2164 CVPixelBufferRef *cv_img)
2168 size_t widths [AV_NUM_DATA_POINTERS];
2169 size_t heights[AV_NUM_DATA_POINTERS];
2170 size_t strides[AV_NUM_DATA_POINTERS];
2172 size_t contiguous_buf_size;
2173 CVPixelBufferPoolRef pix_buf_pool;
2174 VTEncContext* vtctx = avctx->priv_data;
2176 if (avctx->pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX) {
2177 av_assert0(frame->format == AV_PIX_FMT_VIDEOTOOLBOX);
2179 *cv_img = (CVPixelBufferRef)frame->data[3];
2180 av_assert0(*cv_img);
2186 memset(widths, 0, sizeof(widths));
2187 memset(heights, 0, sizeof(heights));
2188 memset(strides, 0, sizeof(strides));
2190 status = get_cv_pixel_info(
2198 &contiguous_buf_size
2205 "Error: Cannot convert format %d color_range %d: %d\n",
2211 return AVERROR_EXTERNAL;
2214 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->session);
2215 if (!pix_buf_pool) {
2216 /* On iOS, the VT session is invalidated when the APP switches from
2217 * foreground to background and vice versa. Fetch the actual error code
2218 * of the VT session to detect that case and restart the VT session
2222 vtstatus = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
2223 if (vtstatus == kVTInvalidSessionErr) {
2224 CFRelease(vtctx->session);
2225 vtctx->session = NULL;
2226 status = vtenc_configure_encoder(avctx);
2228 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->session);
2230 if (!pix_buf_pool) {
2231 av_log(avctx, AV_LOG_ERROR, "Could not get pixel buffer pool.\n");
2232 return AVERROR_EXTERNAL;
2235 av_log(avctx, AV_LOG_WARNING, "VT session restarted because of a "
2236 "kVTInvalidSessionErr error.\n");
2239 status = CVPixelBufferPoolCreatePixelBuffer(NULL,
2245 av_log(avctx, AV_LOG_ERROR, "Could not create pixel buffer from pool: %d.\n", status);
2246 return AVERROR_EXTERNAL;
2249 status = copy_avframe_to_pixel_buffer(avctx, frame, *cv_img, strides, heights);
2259 static int create_encoder_dict_h264(const AVFrame *frame,
2260 CFDictionaryRef* dict_out)
2262 CFDictionaryRef dict = NULL;
2263 if (frame->pict_type == AV_PICTURE_TYPE_I) {
2264 const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
2265 const void *vals[] = { kCFBooleanTrue };
2267 dict = CFDictionaryCreate(NULL, keys, vals, 1, NULL, NULL);
2268 if(!dict) return AVERROR(ENOMEM);
2275 static int vtenc_send_frame(AVCodecContext *avctx,
2276 VTEncContext *vtctx,
2277 const AVFrame *frame)
2280 CFDictionaryRef frame_dict;
2281 CVPixelBufferRef cv_img = NULL;
2282 AVFrameSideData *side_data = NULL;
2283 ExtraSEI *sei = NULL;
2284 int status = create_cv_pixel_buffer(avctx, frame, &cv_img);
2286 if (status) return status;
2288 status = create_encoder_dict_h264(frame, &frame_dict);
2294 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
2295 if (vtctx->a53_cc && side_data && side_data->size) {
2296 sei = av_mallocz(sizeof(*sei));
2298 av_log(avctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
2300 int ret = ff_alloc_a53_sei(frame, 0, &sei->data, &sei->size);
2302 av_log(avctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
2309 time = CMTimeMake(frame->pts * avctx->time_base.num, avctx->time_base.den);
2310 status = VTCompressionSessionEncodeFrame(
2320 if (frame_dict) CFRelease(frame_dict);
2324 av_log(avctx, AV_LOG_ERROR, "Error: cannot encode frame: %d\n", status);
2325 return AVERROR_EXTERNAL;
2331 static av_cold int vtenc_frame(
2332 AVCodecContext *avctx,
2334 const AVFrame *frame,
2337 VTEncContext *vtctx = avctx->priv_data;
2340 CMSampleBufferRef buf = NULL;
2341 ExtraSEI *sei = NULL;
2344 status = vtenc_send_frame(avctx, vtctx, frame);
2347 status = AVERROR_EXTERNAL;
2351 if (vtctx->frame_ct_in == 0) {
2352 vtctx->first_pts = frame->pts;
2353 } else if(vtctx->frame_ct_in == 1 && vtctx->has_b_frames) {
2354 vtctx->dts_delta = frame->pts - vtctx->first_pts;
2357 vtctx->frame_ct_in++;
2358 } else if(!vtctx->flushing) {
2359 vtctx->flushing = true;
2361 status = VTCompressionSessionCompleteFrames(vtctx->session,
2365 av_log(avctx, AV_LOG_ERROR, "Error flushing frames: %d\n", status);
2366 status = AVERROR_EXTERNAL;
2372 get_frame = vtctx->dts_delta >= 0 || !frame;
2378 status = vtenc_q_pop(vtctx, !frame, &buf, &sei);
2379 if (status) goto end_nopkt;
2380 if (!buf) goto end_nopkt;
2382 status = vtenc_cm_to_avpacket(avctx, buf, pkt, sei);
2384 if (sei->data) av_free(sei->data);
2388 if (status) goto end_nopkt;
2394 av_packet_unref(pkt);
2398 static int vtenc_populate_extradata(AVCodecContext *avctx,
2399 CMVideoCodecType codec_type,
2400 CFStringRef profile_level,
2401 CFNumberRef gamma_level,
2402 CFDictionaryRef enc_info,
2403 CFDictionaryRef pixel_buffer_info)
2405 VTEncContext *vtctx = avctx->priv_data;
2407 CVPixelBufferPoolRef pool = NULL;
2408 CVPixelBufferRef pix_buf = NULL;
2410 CMSampleBufferRef buf = NULL;
2412 status = vtenc_create_encoder(avctx,
2422 pool = VTCompressionSessionGetPixelBufferPool(vtctx->session);
2424 av_log(avctx, AV_LOG_ERROR, "Error getting pixel buffer pool.\n");
2428 status = CVPixelBufferPoolCreatePixelBuffer(NULL,
2432 if(status != kCVReturnSuccess){
2433 av_log(avctx, AV_LOG_ERROR, "Error creating frame from pool: %d\n", status);
2437 time = CMTimeMake(0, avctx->time_base.den);
2438 status = VTCompressionSessionEncodeFrame(vtctx->session,
2449 "Error sending frame for extradata: %d\n",
2455 //Populates extradata - output frames are flushed and param sets are available.
2456 status = VTCompressionSessionCompleteFrames(vtctx->session,
2462 status = vtenc_q_pop(vtctx, 0, &buf, NULL);
2464 av_log(avctx, AV_LOG_ERROR, "popping: %d\n", status);
2474 CFRelease(vtctx->session);
2476 vtctx->session = NULL;
2477 vtctx->frame_ct_out = 0;
2479 av_assert0(status != 0 || (avctx->extradata && avctx->extradata_size > 0));
2484 static av_cold int vtenc_close(AVCodecContext *avctx)
2486 VTEncContext *vtctx = avctx->priv_data;
2488 pthread_cond_destroy(&vtctx->cv_sample_sent);
2489 pthread_mutex_destroy(&vtctx->lock);
2491 if(!vtctx->session) return 0;
2493 VTCompressionSessionCompleteFrames(vtctx->session,
2495 clear_frame_queue(vtctx);
2496 CFRelease(vtctx->session);
2497 vtctx->session = NULL;
2499 if (vtctx->color_primaries) {
2500 CFRelease(vtctx->color_primaries);
2501 vtctx->color_primaries = NULL;
2504 if (vtctx->transfer_function) {
2505 CFRelease(vtctx->transfer_function);
2506 vtctx->transfer_function = NULL;
2509 if (vtctx->ycbcr_matrix) {
2510 CFRelease(vtctx->ycbcr_matrix);
2511 vtctx->ycbcr_matrix = NULL;
2517 static const enum AVPixelFormat avc_pix_fmts[] = {
2518 AV_PIX_FMT_VIDEOTOOLBOX,
2524 static const enum AVPixelFormat hevc_pix_fmts[] = {
2525 AV_PIX_FMT_VIDEOTOOLBOX,
2532 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
2533 #define COMMON_OPTIONS \
2534 { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL, \
2535 { .i64 = 0 }, 0, 1, VE }, \
2536 { "require_sw", "Require software encoding", OFFSET(require_sw), AV_OPT_TYPE_BOOL, \
2537 { .i64 = 0 }, 0, 1, VE }, \
2538 { "realtime", "Hint that encoding should happen in real-time if not faster (e.g. capturing from camera).", \
2539 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2540 { "frames_before", "Other frames will come before the frames in this session. This helps smooth concatenation issues.", \
2541 OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2542 { "frames_after", "Other frames will come after the frames in this session. This helps smooth concatenation issues.", \
2543 OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
2545 #define OFFSET(x) offsetof(VTEncContext, x)
2546 static const AVOption h264_options[] = {
2547 { "profile", "Profile", OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = H264_PROF_AUTO }, H264_PROF_AUTO, H264_PROF_COUNT, VE, "profile" },
2548 { "baseline", "Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_BASELINE }, INT_MIN, INT_MAX, VE, "profile" },
2549 { "main", "Main Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
2550 { "high", "High Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_HIGH }, INT_MIN, INT_MAX, VE, "profile" },
2551 { "extended", "Extend Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_EXTENDED }, INT_MIN, INT_MAX, VE, "profile" },
2553 { "level", "Level", OFFSET(level), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, VE, "level" },
2554 { "1.3", "Level 1.3, only available with Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX, VE, "level" },
2555 { "3.0", "Level 3.0", 0, AV_OPT_TYPE_CONST, { .i64 = 30 }, INT_MIN, INT_MAX, VE, "level" },
2556 { "3.1", "Level 3.1", 0, AV_OPT_TYPE_CONST, { .i64 = 31 }, INT_MIN, INT_MAX, VE, "level" },
2557 { "3.2", "Level 3.2", 0, AV_OPT_TYPE_CONST, { .i64 = 32 }, INT_MIN, INT_MAX, VE, "level" },
2558 { "4.0", "Level 4.0", 0, AV_OPT_TYPE_CONST, { .i64 = 40 }, INT_MIN, INT_MAX, VE, "level" },
2559 { "4.1", "Level 4.1", 0, AV_OPT_TYPE_CONST, { .i64 = 41 }, INT_MIN, INT_MAX, VE, "level" },
2560 { "4.2", "Level 4.2", 0, AV_OPT_TYPE_CONST, { .i64 = 42 }, INT_MIN, INT_MAX, VE, "level" },
2561 { "5.0", "Level 5.0", 0, AV_OPT_TYPE_CONST, { .i64 = 50 }, INT_MIN, INT_MAX, VE, "level" },
2562 { "5.1", "Level 5.1", 0, AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX, VE, "level" },
2563 { "5.2", "Level 5.2", 0, AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX, VE, "level" },
2565 { "coder", "Entropy coding", OFFSET(entropy), AV_OPT_TYPE_INT, { .i64 = VT_ENTROPY_NOT_SET }, VT_ENTROPY_NOT_SET, VT_CABAC, VE, "coder" },
2566 { "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC }, INT_MIN, INT_MAX, VE, "coder" },
2567 { "vlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC }, INT_MIN, INT_MAX, VE, "coder" },
2568 { "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC }, INT_MIN, INT_MAX, VE, "coder" },
2569 { "ac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC }, INT_MIN, INT_MAX, VE, "coder" },
2571 { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VE },
2577 static const AVClass h264_videotoolbox_class = {
2578 .class_name = "h264_videotoolbox",
2579 .item_name = av_default_item_name,
2580 .option = h264_options,
2581 .version = LIBAVUTIL_VERSION_INT,
2584 AVCodec ff_h264_videotoolbox_encoder = {
2585 .name = "h264_videotoolbox",
2586 .long_name = NULL_IF_CONFIG_SMALL("VideoToolbox H.264 Encoder"),
2587 .type = AVMEDIA_TYPE_VIDEO,
2588 .id = AV_CODEC_ID_H264,
2589 .priv_data_size = sizeof(VTEncContext),
2590 .pix_fmts = avc_pix_fmts,
2592 .encode2 = vtenc_frame,
2593 .close = vtenc_close,
2594 .capabilities = AV_CODEC_CAP_DELAY,
2595 .priv_class = &h264_videotoolbox_class,
2596 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2597 FF_CODEC_CAP_INIT_CLEANUP,
2600 static const AVOption hevc_options[] = {
2601 { "profile", "Profile", OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = HEVC_PROF_AUTO }, HEVC_PROF_AUTO, HEVC_PROF_COUNT, VE, "profile" },
2602 { "main", "Main Profile", 0, AV_OPT_TYPE_CONST, { .i64 = HEVC_PROF_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
2603 { "main10", "Main10 Profile", 0, AV_OPT_TYPE_CONST, { .i64 = HEVC_PROF_MAIN10 }, INT_MIN, INT_MAX, VE, "profile" },
2609 static const AVClass hevc_videotoolbox_class = {
2610 .class_name = "hevc_videotoolbox",
2611 .item_name = av_default_item_name,
2612 .option = hevc_options,
2613 .version = LIBAVUTIL_VERSION_INT,
2616 AVCodec ff_hevc_videotoolbox_encoder = {
2617 .name = "hevc_videotoolbox",
2618 .long_name = NULL_IF_CONFIG_SMALL("VideoToolbox H.265 Encoder"),
2619 .type = AVMEDIA_TYPE_VIDEO,
2620 .id = AV_CODEC_ID_HEVC,
2621 .priv_data_size = sizeof(VTEncContext),
2622 .pix_fmts = hevc_pix_fmts,
2624 .encode2 = vtenc_frame,
2625 .close = vtenc_close,
2626 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
2627 .priv_class = &hevc_videotoolbox_class,
2628 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2629 FF_CODEC_CAP_INIT_CLEANUP,
2630 .wrapper_name = "videotoolbox",