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 typedef OSStatus (*getParameterSetAtIndex)(CMFormatDescriptionRef videoDesc,
43 size_t parameterSetIndex,
44 const uint8_t * _Nullable *parameterSetPointerOut,
45 size_t *parameterSetSizeOut,
46 size_t *parameterSetCountOut,
47 int *NALUnitHeaderLengthOut);
49 //These symbols may not be present
51 CFStringRef kCVImageBufferColorPrimaries_ITU_R_2020;
52 CFStringRef kCVImageBufferTransferFunction_ITU_R_2020;
53 CFStringRef kCVImageBufferYCbCrMatrix_ITU_R_2020;
55 CFStringRef kVTCompressionPropertyKey_H264EntropyMode;
56 CFStringRef kVTH264EntropyMode_CAVLC;
57 CFStringRef kVTH264EntropyMode_CABAC;
59 CFStringRef kVTProfileLevel_H264_Baseline_4_0;
60 CFStringRef kVTProfileLevel_H264_Baseline_4_2;
61 CFStringRef kVTProfileLevel_H264_Baseline_5_0;
62 CFStringRef kVTProfileLevel_H264_Baseline_5_1;
63 CFStringRef kVTProfileLevel_H264_Baseline_5_2;
64 CFStringRef kVTProfileLevel_H264_Baseline_AutoLevel;
65 CFStringRef kVTProfileLevel_H264_Main_4_2;
66 CFStringRef kVTProfileLevel_H264_Main_5_1;
67 CFStringRef kVTProfileLevel_H264_Main_5_2;
68 CFStringRef kVTProfileLevel_H264_Main_AutoLevel;
69 CFStringRef kVTProfileLevel_H264_High_3_0;
70 CFStringRef kVTProfileLevel_H264_High_3_1;
71 CFStringRef kVTProfileLevel_H264_High_3_2;
72 CFStringRef kVTProfileLevel_H264_High_4_0;
73 CFStringRef kVTProfileLevel_H264_High_4_1;
74 CFStringRef kVTProfileLevel_H264_High_4_2;
75 CFStringRef kVTProfileLevel_H264_High_5_1;
76 CFStringRef kVTProfileLevel_H264_High_5_2;
77 CFStringRef kVTProfileLevel_H264_High_AutoLevel;
79 CFStringRef kVTProfileLevel_HEVC_Main_AutoLevel;
80 CFStringRef kVTProfileLevel_HEVC_Main10_AutoLevel;
82 CFStringRef kVTCompressionPropertyKey_RealTime;
84 CFStringRef kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder;
85 CFStringRef kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder;
87 getParameterSetAtIndex CMVideoFormatDescriptionGetHEVCParameterSetAtIndex;
90 #define GET_SYM(symbol, defaultVal) \
92 CFStringRef* handle = (CFStringRef*)dlsym(RTLD_DEFAULT, #symbol); \
94 compat_keys.symbol = CFSTR(defaultVal); \
96 compat_keys.symbol = *handle; \
99 static pthread_once_t once_ctrl = PTHREAD_ONCE_INIT;
101 static void loadVTEncSymbols(){
102 compat_keys.CMVideoFormatDescriptionGetHEVCParameterSetAtIndex =
103 (getParameterSetAtIndex)dlsym(
105 "CMVideoFormatDescriptionGetHEVCParameterSetAtIndex"
108 GET_SYM(kCVImageBufferColorPrimaries_ITU_R_2020, "ITU_R_2020");
109 GET_SYM(kCVImageBufferTransferFunction_ITU_R_2020, "ITU_R_2020");
110 GET_SYM(kCVImageBufferYCbCrMatrix_ITU_R_2020, "ITU_R_2020");
112 GET_SYM(kVTCompressionPropertyKey_H264EntropyMode, "H264EntropyMode");
113 GET_SYM(kVTH264EntropyMode_CAVLC, "CAVLC");
114 GET_SYM(kVTH264EntropyMode_CABAC, "CABAC");
116 GET_SYM(kVTProfileLevel_H264_Baseline_4_0, "H264_Baseline_4_0");
117 GET_SYM(kVTProfileLevel_H264_Baseline_4_2, "H264_Baseline_4_2");
118 GET_SYM(kVTProfileLevel_H264_Baseline_5_0, "H264_Baseline_5_0");
119 GET_SYM(kVTProfileLevel_H264_Baseline_5_1, "H264_Baseline_5_1");
120 GET_SYM(kVTProfileLevel_H264_Baseline_5_2, "H264_Baseline_5_2");
121 GET_SYM(kVTProfileLevel_H264_Baseline_AutoLevel, "H264_Baseline_AutoLevel");
122 GET_SYM(kVTProfileLevel_H264_Main_4_2, "H264_Main_4_2");
123 GET_SYM(kVTProfileLevel_H264_Main_5_1, "H264_Main_5_1");
124 GET_SYM(kVTProfileLevel_H264_Main_5_2, "H264_Main_5_2");
125 GET_SYM(kVTProfileLevel_H264_Main_AutoLevel, "H264_Main_AutoLevel");
126 GET_SYM(kVTProfileLevel_H264_High_3_0, "H264_High_3_0");
127 GET_SYM(kVTProfileLevel_H264_High_3_1, "H264_High_3_1");
128 GET_SYM(kVTProfileLevel_H264_High_3_2, "H264_High_3_2");
129 GET_SYM(kVTProfileLevel_H264_High_4_0, "H264_High_4_0");
130 GET_SYM(kVTProfileLevel_H264_High_4_1, "H264_High_4_1");
131 GET_SYM(kVTProfileLevel_H264_High_4_2, "H264_High_4_2");
132 GET_SYM(kVTProfileLevel_H264_High_5_1, "H264_High_5_1");
133 GET_SYM(kVTProfileLevel_H264_High_5_2, "H264_High_5_2");
134 GET_SYM(kVTProfileLevel_H264_High_AutoLevel, "H264_High_AutoLevel");
136 GET_SYM(kVTProfileLevel_HEVC_Main_AutoLevel, "HEVC_Main_AutoLevel");
137 GET_SYM(kVTProfileLevel_HEVC_Main10_AutoLevel, "HEVC_Main10_AutoLevel");
139 GET_SYM(kVTCompressionPropertyKey_RealTime, "RealTime");
141 GET_SYM(kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
142 "EnableHardwareAcceleratedVideoEncoder");
143 GET_SYM(kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
144 "RequireHardwareAcceleratedVideoEncoder");
147 typedef enum VT_H264Profile {
155 typedef enum VTH264Entropy{
161 typedef enum VT_HEVCProfile {
168 static const uint8_t start_code[] = { 0, 0, 0, 1 };
170 typedef struct ExtraSEI {
175 typedef struct BufNode {
176 CMSampleBufferRef cm_buffer;
178 struct BufNode* next;
182 typedef struct VTEncContext {
184 enum AVCodecID codec_id;
185 VTCompressionSessionRef session;
186 CFStringRef ycbcr_matrix;
187 CFStringRef color_primaries;
188 CFStringRef transfer_function;
189 getParameterSetAtIndex get_param_set_func;
191 pthread_mutex_t lock;
192 pthread_cond_t cv_sample_sent;
199 int64_t frame_ct_out;
209 int64_t frames_before;
210 int64_t frames_after;
216 bool warned_color_range;
220 static int vtenc_populate_extradata(AVCodecContext *avctx,
221 CMVideoCodecType codec_type,
222 CFStringRef profile_level,
223 CFNumberRef gamma_level,
224 CFDictionaryRef enc_info,
225 CFDictionaryRef pixel_buffer_info);
228 * NULL-safe release of *refPtr, and sets value to NULL.
230 static void vt_release_num(CFNumberRef* refPtr){
239 static void set_async_error(VTEncContext *vtctx, int err)
243 pthread_mutex_lock(&vtctx->lock);
245 vtctx->async_error = err;
247 info = vtctx->q_head;
248 vtctx->q_head = vtctx->q_tail = NULL;
251 BufNode *next = info->next;
252 CFRelease(info->cm_buffer);
257 pthread_mutex_unlock(&vtctx->lock);
260 static void clear_frame_queue(VTEncContext *vtctx)
262 set_async_error(vtctx, 0);
265 static int vtenc_q_pop(VTEncContext *vtctx, bool wait, CMSampleBufferRef *buf, ExtraSEI **sei)
269 pthread_mutex_lock(&vtctx->lock);
271 if (vtctx->async_error) {
272 pthread_mutex_unlock(&vtctx->lock);
273 return vtctx->async_error;
276 if (vtctx->flushing && vtctx->frame_ct_in == vtctx->frame_ct_out) {
279 pthread_mutex_unlock(&vtctx->lock);
283 while (!vtctx->q_head && !vtctx->async_error && wait) {
284 pthread_cond_wait(&vtctx->cv_sample_sent, &vtctx->lock);
287 if (!vtctx->q_head) {
288 pthread_mutex_unlock(&vtctx->lock);
293 info = vtctx->q_head;
294 vtctx->q_head = vtctx->q_head->next;
295 if (!vtctx->q_head) {
296 vtctx->q_tail = NULL;
299 pthread_mutex_unlock(&vtctx->lock);
301 *buf = info->cm_buffer;
304 } else if (info->sei) {
305 if (info->sei->data) av_free(info->sei->data);
310 vtctx->frame_ct_out++;
315 static void vtenc_q_push(VTEncContext *vtctx, CMSampleBufferRef buffer, ExtraSEI *sei)
317 BufNode *info = av_malloc(sizeof(BufNode));
319 set_async_error(vtctx, AVERROR(ENOMEM));
324 info->cm_buffer = buffer;
328 pthread_mutex_lock(&vtctx->lock);
329 pthread_cond_signal(&vtctx->cv_sample_sent);
331 if (!vtctx->q_head) {
332 vtctx->q_head = info;
334 vtctx->q_tail->next = info;
337 vtctx->q_tail = info;
339 pthread_mutex_unlock(&vtctx->lock);
342 static int count_nalus(size_t length_code_size,
343 CMSampleBufferRef sample_buffer,
350 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
351 CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample_buffer);
353 if (length_code_size > 4)
354 return AVERROR_INVALIDDATA;
356 while (offset < src_size) {
361 status = CMBlockBufferCopyDataBytes(block,
366 for (i = 0; i < length_code_size; i++) {
368 box_len |= size_buf[i];
371 curr_src_len = box_len + length_code_size;
372 offset += curr_src_len;
381 static CMVideoCodecType get_cm_codec_type(enum AVCodecID id)
384 case AV_CODEC_ID_H264: return kCMVideoCodecType_H264;
385 case AV_CODEC_ID_HEVC: return kCMVideoCodecType_HEVC;
391 * Get the parameter sets from a CMSampleBufferRef.
392 * @param dst If *dst isn't NULL, the parameters are copied into existing
393 * memory. *dst_size must be set accordingly when *dst != NULL.
394 * If *dst is NULL, it will be allocated.
395 * In all cases, *dst_size is set to the number of bytes used starting
398 static int get_params_size(
399 AVCodecContext *avctx,
400 CMVideoFormatDescriptionRef vid_fmt,
403 VTEncContext *vtctx = avctx->priv_data;
404 size_t total_size = 0;
406 int is_count_bad = 0;
409 status = vtctx->get_param_set_func(vid_fmt,
421 for (i = 0; i < ps_count || is_count_bad; i++) {
424 status = vtctx->get_param_set_func(vid_fmt,
432 * When ps_count is invalid, status != 0 ends the loop normally
433 * unless we didn't get any parameter sets.
435 if (i > 0 && is_count_bad) status = 0;
440 total_size += ps_size + sizeof(start_code);
444 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set sizes: %d\n", status);
445 return AVERROR_EXTERNAL;
452 static int copy_param_sets(
453 AVCodecContext *avctx,
454 CMVideoFormatDescriptionRef vid_fmt,
458 VTEncContext *vtctx = avctx->priv_data;
460 int is_count_bad = 0;
465 status = vtctx->get_param_set_func(vid_fmt,
478 for (i = 0; i < ps_count || is_count_bad; i++) {
483 status = vtctx->get_param_set_func(vid_fmt,
490 if (i > 0 && is_count_bad) status = 0;
495 next_offset = offset + sizeof(start_code) + ps_size;
496 if (dst_size < next_offset) {
497 av_log(avctx, AV_LOG_ERROR, "Error: buffer too small for parameter sets.\n");
498 return AVERROR_BUFFER_TOO_SMALL;
501 memcpy(dst + offset, start_code, sizeof(start_code));
502 offset += sizeof(start_code);
504 memcpy(dst + offset, ps, ps_size);
505 offset = next_offset;
509 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set data: %d\n", status);
510 return AVERROR_EXTERNAL;
516 static int set_extradata(AVCodecContext *avctx, CMSampleBufferRef sample_buffer)
518 CMVideoFormatDescriptionRef vid_fmt;
522 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
524 av_log(avctx, AV_LOG_ERROR, "No video format.\n");
525 return AVERROR_EXTERNAL;
528 status = get_params_size(avctx, vid_fmt, &total_size);
530 av_log(avctx, AV_LOG_ERROR, "Could not get parameter sets.\n");
534 avctx->extradata = av_mallocz(total_size + AV_INPUT_BUFFER_PADDING_SIZE);
535 if (!avctx->extradata) {
536 return AVERROR(ENOMEM);
538 avctx->extradata_size = total_size;
540 status = copy_param_sets(avctx, vid_fmt, avctx->extradata, total_size);
543 av_log(avctx, AV_LOG_ERROR, "Could not copy param sets.\n");
550 static void vtenc_output_callback(
552 void *sourceFrameCtx,
554 VTEncodeInfoFlags flags,
555 CMSampleBufferRef sample_buffer)
557 AVCodecContext *avctx = ctx;
558 VTEncContext *vtctx = avctx->priv_data;
559 ExtraSEI *sei = sourceFrameCtx;
561 if (vtctx->async_error) {
562 if(sample_buffer) CFRelease(sample_buffer);
566 if (status || !sample_buffer) {
567 av_log(avctx, AV_LOG_ERROR, "Error encoding frame: %d\n", (int)status);
568 set_async_error(vtctx, AVERROR_EXTERNAL);
572 if (!avctx->extradata && (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
573 int set_status = set_extradata(avctx, sample_buffer);
575 set_async_error(vtctx, set_status);
580 vtenc_q_push(vtctx, sample_buffer, sei);
583 static int get_length_code_size(
584 AVCodecContext *avctx,
585 CMSampleBufferRef sample_buffer,
588 VTEncContext *vtctx = avctx->priv_data;
589 CMVideoFormatDescriptionRef vid_fmt;
593 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
595 av_log(avctx, AV_LOG_ERROR, "Error getting buffer format description.\n");
596 return AVERROR_EXTERNAL;
599 status = vtctx->get_param_set_func(vid_fmt,
606 av_log(avctx, AV_LOG_ERROR, "Error getting length code size: %d\n", status);
607 return AVERROR_EXTERNAL;
615 * Returns true on success.
617 * If profile_level_val is NULL and this method returns true, don't specify the
618 * profile/level to the encoder.
620 static bool get_vt_h264_profile_level(AVCodecContext *avctx,
621 CFStringRef *profile_level_val)
623 VTEncContext *vtctx = avctx->priv_data;
624 int64_t profile = vtctx->profile;
626 if (profile == H264_PROF_AUTO && vtctx->level) {
627 //Need to pick a profile if level is not auto-selected.
628 profile = vtctx->has_b_frames ? H264_PROF_MAIN : H264_PROF_BASELINE;
631 *profile_level_val = NULL;
637 case H264_PROF_BASELINE:
638 switch (vtctx->level) {
639 case 0: *profile_level_val =
640 compat_keys.kVTProfileLevel_H264_Baseline_AutoLevel; break;
641 case 13: *profile_level_val = kVTProfileLevel_H264_Baseline_1_3; break;
642 case 30: *profile_level_val = kVTProfileLevel_H264_Baseline_3_0; break;
643 case 31: *profile_level_val = kVTProfileLevel_H264_Baseline_3_1; break;
644 case 32: *profile_level_val = kVTProfileLevel_H264_Baseline_3_2; break;
645 case 40: *profile_level_val =
646 compat_keys.kVTProfileLevel_H264_Baseline_4_0; break;
647 case 41: *profile_level_val = kVTProfileLevel_H264_Baseline_4_1; break;
648 case 42: *profile_level_val =
649 compat_keys.kVTProfileLevel_H264_Baseline_4_2; break;
650 case 50: *profile_level_val =
651 compat_keys.kVTProfileLevel_H264_Baseline_5_0; break;
652 case 51: *profile_level_val =
653 compat_keys.kVTProfileLevel_H264_Baseline_5_1; break;
654 case 52: *profile_level_val =
655 compat_keys.kVTProfileLevel_H264_Baseline_5_2; break;
660 switch (vtctx->level) {
661 case 0: *profile_level_val =
662 compat_keys.kVTProfileLevel_H264_Main_AutoLevel; break;
663 case 30: *profile_level_val = kVTProfileLevel_H264_Main_3_0; break;
664 case 31: *profile_level_val = kVTProfileLevel_H264_Main_3_1; break;
665 case 32: *profile_level_val = kVTProfileLevel_H264_Main_3_2; break;
666 case 40: *profile_level_val = kVTProfileLevel_H264_Main_4_0; break;
667 case 41: *profile_level_val = kVTProfileLevel_H264_Main_4_1; break;
668 case 42: *profile_level_val =
669 compat_keys.kVTProfileLevel_H264_Main_4_2; break;
670 case 50: *profile_level_val = kVTProfileLevel_H264_Main_5_0; break;
671 case 51: *profile_level_val =
672 compat_keys.kVTProfileLevel_H264_Main_5_1; break;
673 case 52: *profile_level_val =
674 compat_keys.kVTProfileLevel_H264_Main_5_2; break;
679 switch (vtctx->level) {
680 case 0: *profile_level_val =
681 compat_keys.kVTProfileLevel_H264_High_AutoLevel; break;
682 case 30: *profile_level_val =
683 compat_keys.kVTProfileLevel_H264_High_3_0; break;
684 case 31: *profile_level_val =
685 compat_keys.kVTProfileLevel_H264_High_3_1; break;
686 case 32: *profile_level_val =
687 compat_keys.kVTProfileLevel_H264_High_3_2; break;
688 case 40: *profile_level_val =
689 compat_keys.kVTProfileLevel_H264_High_4_0; break;
690 case 41: *profile_level_val =
691 compat_keys.kVTProfileLevel_H264_High_4_1; break;
692 case 42: *profile_level_val =
693 compat_keys.kVTProfileLevel_H264_High_4_2; break;
694 case 50: *profile_level_val = kVTProfileLevel_H264_High_5_0; break;
695 case 51: *profile_level_val =
696 compat_keys.kVTProfileLevel_H264_High_5_1; break;
697 case 52: *profile_level_val =
698 compat_keys.kVTProfileLevel_H264_High_5_2; break;
703 if (!*profile_level_val) {
704 av_log(avctx, AV_LOG_ERROR, "Invalid Profile/Level.\n");
712 * Returns true on success.
714 * If profile_level_val is NULL and this method returns true, don't specify the
715 * profile/level to the encoder.
717 static bool get_vt_hevc_profile_level(AVCodecContext *avctx,
718 CFStringRef *profile_level_val)
720 VTEncContext *vtctx = avctx->priv_data;
721 int64_t profile = vtctx->profile;
723 *profile_level_val = NULL;
730 compat_keys.kVTProfileLevel_HEVC_Main_AutoLevel;
732 case HEVC_PROF_MAIN10:
734 compat_keys.kVTProfileLevel_HEVC_Main10_AutoLevel;
738 if (!*profile_level_val) {
739 av_log(avctx, AV_LOG_ERROR, "Invalid Profile/Level.\n");
746 static int get_cv_pixel_format(AVCodecContext* avctx,
747 enum AVPixelFormat fmt,
748 enum AVColorRange range,
749 int* av_pixel_format,
752 if (range_guessed) *range_guessed = range != AVCOL_RANGE_MPEG &&
753 range != AVCOL_RANGE_JPEG;
755 //MPEG range is used when no range is set
756 if (fmt == AV_PIX_FMT_NV12) {
757 *av_pixel_format = range == AVCOL_RANGE_JPEG ?
758 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange :
759 kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
760 } else if (fmt == AV_PIX_FMT_YUV420P) {
761 *av_pixel_format = range == AVCOL_RANGE_JPEG ?
762 kCVPixelFormatType_420YpCbCr8PlanarFullRange :
763 kCVPixelFormatType_420YpCbCr8Planar;
765 return AVERROR(EINVAL);
771 static void add_color_attr(AVCodecContext *avctx, CFMutableDictionaryRef dict) {
772 VTEncContext *vtctx = avctx->priv_data;
774 if (vtctx->color_primaries) {
775 CFDictionarySetValue(dict,
776 kCVImageBufferColorPrimariesKey,
777 vtctx->color_primaries);
780 if (vtctx->transfer_function) {
781 CFDictionarySetValue(dict,
782 kCVImageBufferTransferFunctionKey,
783 vtctx->transfer_function);
786 if (vtctx->ycbcr_matrix) {
787 CFDictionarySetValue(dict,
788 kCVImageBufferYCbCrMatrixKey,
789 vtctx->ycbcr_matrix);
793 static int create_cv_pixel_buffer_info(AVCodecContext* avctx,
794 CFMutableDictionaryRef* dict)
796 CFNumberRef cv_color_format_num = NULL;
797 CFNumberRef width_num = NULL;
798 CFNumberRef height_num = NULL;
799 CFMutableDictionaryRef pixel_buffer_info = NULL;
801 int status = get_cv_pixel_format(avctx,
806 if (status) return status;
808 pixel_buffer_info = CFDictionaryCreateMutable(
811 &kCFCopyStringDictionaryKeyCallBacks,
812 &kCFTypeDictionaryValueCallBacks);
814 if (!pixel_buffer_info) goto pbinfo_nomem;
816 cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
819 if (!cv_color_format_num) goto pbinfo_nomem;
821 CFDictionarySetValue(pixel_buffer_info,
822 kCVPixelBufferPixelFormatTypeKey,
823 cv_color_format_num);
824 vt_release_num(&cv_color_format_num);
826 width_num = CFNumberCreate(kCFAllocatorDefault,
829 if (!width_num) return AVERROR(ENOMEM);
831 CFDictionarySetValue(pixel_buffer_info,
832 kCVPixelBufferWidthKey,
834 vt_release_num(&width_num);
836 height_num = CFNumberCreate(kCFAllocatorDefault,
839 if (!height_num) goto pbinfo_nomem;
841 CFDictionarySetValue(pixel_buffer_info,
842 kCVPixelBufferHeightKey,
844 vt_release_num(&height_num);
846 add_color_attr(avctx, pixel_buffer_info);
848 *dict = pixel_buffer_info;
852 vt_release_num(&cv_color_format_num);
853 vt_release_num(&width_num);
854 vt_release_num(&height_num);
855 if (pixel_buffer_info) CFRelease(pixel_buffer_info);
857 return AVERROR(ENOMEM);
860 static int get_cv_color_primaries(AVCodecContext *avctx,
861 CFStringRef *primaries)
863 enum AVColorPrimaries pri = avctx->color_primaries;
865 case AVCOL_PRI_UNSPECIFIED:
869 case AVCOL_PRI_BT709:
870 *primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
873 case AVCOL_PRI_BT2020:
874 *primaries = compat_keys.kCVImageBufferColorPrimaries_ITU_R_2020;
878 av_log(avctx, AV_LOG_ERROR, "Color primaries %s is not supported.\n", av_color_primaries_name(pri));
886 static int get_cv_transfer_function(AVCodecContext *avctx,
887 CFStringRef *transfer_fnc,
888 CFNumberRef *gamma_level)
890 enum AVColorTransferCharacteristic trc = avctx->color_trc;
895 case AVCOL_TRC_UNSPECIFIED:
896 *transfer_fnc = NULL;
899 case AVCOL_TRC_BT709:
900 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_709_2;
903 case AVCOL_TRC_SMPTE240M:
904 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_240M_1995;
907 case AVCOL_TRC_GAMMA22:
909 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
910 *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type, &gamma);
913 case AVCOL_TRC_GAMMA28:
915 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
916 *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type, &gamma);
919 case AVCOL_TRC_BT2020_10:
920 case AVCOL_TRC_BT2020_12:
921 *transfer_fnc = compat_keys.kCVImageBufferTransferFunction_ITU_R_2020;
925 av_log(avctx, AV_LOG_ERROR, "Transfer function %s is not supported.\n", av_color_transfer_name(trc));
932 static int get_cv_ycbcr_matrix(AVCodecContext *avctx, CFStringRef *matrix) {
933 switch(avctx->colorspace) {
934 case AVCOL_SPC_BT709:
935 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_709_2;
938 case AVCOL_SPC_UNSPECIFIED:
942 case AVCOL_SPC_BT470BG:
943 case AVCOL_SPC_SMPTE170M:
944 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_601_4;
947 case AVCOL_SPC_SMPTE240M:
948 *matrix = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995;
951 case AVCOL_SPC_BT2020_NCL:
952 *matrix = compat_keys.kCVImageBufferYCbCrMatrix_ITU_R_2020;
956 av_log(avctx, AV_LOG_ERROR, "Color space %s is not supported.\n", av_color_space_name(avctx->colorspace));
963 static int vtenc_create_encoder(AVCodecContext *avctx,
964 CMVideoCodecType codec_type,
965 CFStringRef profile_level,
966 CFNumberRef gamma_level,
967 CFDictionaryRef enc_info,
968 CFDictionaryRef pixel_buffer_info,
969 VTCompressionSessionRef *session)
971 VTEncContext *vtctx = avctx->priv_data;
972 SInt32 bit_rate = avctx->bit_rate;
973 SInt32 max_rate = avctx->rc_max_rate;
974 CFNumberRef bit_rate_num;
975 CFNumberRef bytes_per_second;
976 CFNumberRef one_second;
977 CFArrayRef data_rate_limits;
978 int64_t bytes_per_second_value = 0;
979 int64_t one_second_value = 0;
982 int status = VTCompressionSessionCreate(kCFAllocatorDefault,
989 vtenc_output_callback,
993 if (status || !vtctx->session) {
994 av_log(avctx, AV_LOG_ERROR, "Error: cannot create compression session: %d\n", status);
996 #if !TARGET_OS_IPHONE
997 if (!vtctx->allow_sw) {
998 av_log(avctx, AV_LOG_ERROR, "Try -allow_sw 1. The hardware encoder may be busy, or not supported.\n");
1002 return AVERROR_EXTERNAL;
1005 bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
1006 kCFNumberSInt32Type,
1008 if (!bit_rate_num) return AVERROR(ENOMEM);
1010 status = VTSessionSetProperty(vtctx->session,
1011 kVTCompressionPropertyKey_AverageBitRate,
1013 CFRelease(bit_rate_num);
1016 av_log(avctx, AV_LOG_ERROR, "Error setting bitrate property: %d\n", status);
1017 return AVERROR_EXTERNAL;
1020 if (vtctx->codec_id == AV_CODEC_ID_H264) {
1021 // kVTCompressionPropertyKey_DataRateLimits is not available for HEVC
1022 bytes_per_second_value = max_rate >> 3;
1023 bytes_per_second = CFNumberCreate(kCFAllocatorDefault,
1024 kCFNumberSInt64Type,
1025 &bytes_per_second_value);
1026 if (!bytes_per_second) {
1027 return AVERROR(ENOMEM);
1029 one_second_value = 1;
1030 one_second = CFNumberCreate(kCFAllocatorDefault,
1031 kCFNumberSInt64Type,
1034 CFRelease(bytes_per_second);
1035 return AVERROR(ENOMEM);
1037 nums[0] = (void *)bytes_per_second;
1038 nums[1] = (void *)one_second;
1039 data_rate_limits = CFArrayCreate(kCFAllocatorDefault,
1040 (const void **)nums,
1042 &kCFTypeArrayCallBacks);
1044 if (!data_rate_limits) {
1045 CFRelease(bytes_per_second);
1046 CFRelease(one_second);
1047 return AVERROR(ENOMEM);
1049 status = VTSessionSetProperty(vtctx->session,
1050 kVTCompressionPropertyKey_DataRateLimits,
1053 CFRelease(bytes_per_second);
1054 CFRelease(one_second);
1055 CFRelease(data_rate_limits);
1058 av_log(avctx, AV_LOG_ERROR, "Error setting max bitrate property: %d\n", status);
1059 return AVERROR_EXTERNAL;
1062 if (profile_level) {
1063 status = VTSessionSetProperty(vtctx->session,
1064 kVTCompressionPropertyKey_ProfileLevel,
1067 av_log(avctx, AV_LOG_ERROR, "Error setting profile/level property: %d\n", status);
1072 if (avctx->gop_size > 0) {
1073 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
1077 return AVERROR(ENOMEM);
1080 status = VTSessionSetProperty(vtctx->session,
1081 kVTCompressionPropertyKey_MaxKeyFrameInterval,
1083 CFRelease(interval);
1086 av_log(avctx, AV_LOG_ERROR, "Error setting 'max key-frame interval' property: %d\n", status);
1087 return AVERROR_EXTERNAL;
1091 if (vtctx->frames_before) {
1092 status = VTSessionSetProperty(vtctx->session,
1093 kVTCompressionPropertyKey_MoreFramesBeforeStart,
1096 if (status == kVTPropertyNotSupportedErr) {
1097 av_log(avctx, AV_LOG_WARNING, "frames_before property is not supported on this device. Ignoring.\n");
1098 } else if (status) {
1099 av_log(avctx, AV_LOG_ERROR, "Error setting frames_before property: %d\n", status);
1103 if (vtctx->frames_after) {
1104 status = VTSessionSetProperty(vtctx->session,
1105 kVTCompressionPropertyKey_MoreFramesAfterEnd,
1108 if (status == kVTPropertyNotSupportedErr) {
1109 av_log(avctx, AV_LOG_WARNING, "frames_after property is not supported on this device. Ignoring.\n");
1110 } else if (status) {
1111 av_log(avctx, AV_LOG_ERROR, "Error setting frames_after property: %d\n", status);
1115 if (avctx->sample_aspect_ratio.num != 0) {
1118 CFMutableDictionaryRef par;
1119 AVRational *avpar = &avctx->sample_aspect_ratio;
1121 av_reduce(&avpar->num, &avpar->den,
1122 avpar->num, avpar->den,
1125 num = CFNumberCreate(kCFAllocatorDefault,
1129 den = CFNumberCreate(kCFAllocatorDefault,
1135 par = CFDictionaryCreateMutable(kCFAllocatorDefault,
1137 &kCFCopyStringDictionaryKeyCallBacks,
1138 &kCFTypeDictionaryValueCallBacks);
1140 if (!par || !num || !den) {
1141 if (par) CFRelease(par);
1142 if (num) CFRelease(num);
1143 if (den) CFRelease(den);
1145 return AVERROR(ENOMEM);
1148 CFDictionarySetValue(
1150 kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
1153 CFDictionarySetValue(
1155 kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
1158 status = VTSessionSetProperty(vtctx->session,
1159 kVTCompressionPropertyKey_PixelAspectRatio,
1169 "Error setting pixel aspect ratio to %d:%d: %d.\n",
1170 avctx->sample_aspect_ratio.num,
1171 avctx->sample_aspect_ratio.den,
1174 return AVERROR_EXTERNAL;
1179 if (vtctx->transfer_function) {
1180 status = VTSessionSetProperty(vtctx->session,
1181 kVTCompressionPropertyKey_TransferFunction,
1182 vtctx->transfer_function);
1185 av_log(avctx, AV_LOG_WARNING, "Could not set transfer function: %d\n", status);
1190 if (vtctx->ycbcr_matrix) {
1191 status = VTSessionSetProperty(vtctx->session,
1192 kVTCompressionPropertyKey_YCbCrMatrix,
1193 vtctx->ycbcr_matrix);
1196 av_log(avctx, AV_LOG_WARNING, "Could not set ycbcr matrix: %d\n", status);
1201 if (vtctx->color_primaries) {
1202 status = VTSessionSetProperty(vtctx->session,
1203 kVTCompressionPropertyKey_ColorPrimaries,
1204 vtctx->color_primaries);
1207 av_log(avctx, AV_LOG_WARNING, "Could not set color primaries: %d\n", status);
1212 status = VTSessionSetProperty(vtctx->session,
1213 kCVImageBufferGammaLevelKey,
1217 av_log(avctx, AV_LOG_WARNING, "Could not set gamma level: %d\n", status);
1221 if (!vtctx->has_b_frames) {
1222 status = VTSessionSetProperty(vtctx->session,
1223 kVTCompressionPropertyKey_AllowFrameReordering,
1227 av_log(avctx, AV_LOG_ERROR, "Error setting 'allow frame reordering' property: %d\n", status);
1228 return AVERROR_EXTERNAL;
1232 if (vtctx->entropy != VT_ENTROPY_NOT_SET) {
1233 CFStringRef entropy = vtctx->entropy == VT_CABAC ?
1234 compat_keys.kVTH264EntropyMode_CABAC:
1235 compat_keys.kVTH264EntropyMode_CAVLC;
1237 status = VTSessionSetProperty(vtctx->session,
1238 compat_keys.kVTCompressionPropertyKey_H264EntropyMode,
1242 av_log(avctx, AV_LOG_ERROR, "Error setting entropy property: %d\n", status);
1246 if (vtctx->realtime) {
1247 status = VTSessionSetProperty(vtctx->session,
1248 compat_keys.kVTCompressionPropertyKey_RealTime,
1252 av_log(avctx, AV_LOG_ERROR, "Error setting realtime property: %d\n", status);
1256 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
1258 av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", status);
1259 return AVERROR_EXTERNAL;
1265 static av_cold int vtenc_init(AVCodecContext *avctx)
1267 CFMutableDictionaryRef enc_info;
1268 CFMutableDictionaryRef pixel_buffer_info;
1269 CMVideoCodecType codec_type;
1270 VTEncContext *vtctx = avctx->priv_data;
1271 CFStringRef profile_level;
1272 CFBooleanRef has_b_frames_cfbool;
1273 CFNumberRef gamma_level = NULL;
1276 pthread_once(&once_ctrl, loadVTEncSymbols);
1278 codec_type = get_cm_codec_type(avctx->codec_id);
1280 av_log(avctx, AV_LOG_ERROR, "Error: no mapping for AVCodecID %d\n", avctx->codec_id);
1281 return AVERROR(EINVAL);
1284 vtctx->codec_id = avctx->codec_id;
1286 if (vtctx->codec_id == AV_CODEC_ID_H264) {
1287 vtctx->get_param_set_func = CMVideoFormatDescriptionGetH264ParameterSetAtIndex;
1289 vtctx->has_b_frames = avctx->max_b_frames > 0;
1290 if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){
1291 av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");
1292 vtctx->has_b_frames = false;
1295 if (vtctx->entropy == VT_CABAC && vtctx->profile == H264_PROF_BASELINE) {
1296 av_log(avctx, AV_LOG_WARNING, "CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n");
1297 vtctx->entropy = VT_ENTROPY_NOT_SET;
1300 if (!get_vt_h264_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);
1302 vtctx->get_param_set_func = compat_keys.CMVideoFormatDescriptionGetHEVCParameterSetAtIndex;
1303 if (!vtctx->get_param_set_func) return AVERROR(EINVAL);
1304 if (!get_vt_hevc_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);
1307 vtctx->session = NULL;
1309 enc_info = CFDictionaryCreateMutable(
1310 kCFAllocatorDefault,
1312 &kCFCopyStringDictionaryKeyCallBacks,
1313 &kCFTypeDictionaryValueCallBacks
1316 if (!enc_info) return AVERROR(ENOMEM);
1318 #if !TARGET_OS_IPHONE
1319 if (!vtctx->allow_sw) {
1320 CFDictionarySetValue(enc_info,
1321 compat_keys.kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder,
1324 CFDictionarySetValue(enc_info,
1325 compat_keys.kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder,
1330 if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) {
1331 status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);
1335 pixel_buffer_info = NULL;
1338 pthread_mutex_init(&vtctx->lock, NULL);
1339 pthread_cond_init(&vtctx->cv_sample_sent, NULL);
1340 vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0;
1342 get_cv_transfer_function(avctx, &vtctx->transfer_function, &gamma_level);
1343 get_cv_ycbcr_matrix(avctx, &vtctx->ycbcr_matrix);
1344 get_cv_color_primaries(avctx, &vtctx->color_primaries);
1347 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1348 status = vtenc_populate_extradata(avctx,
1358 status = vtenc_create_encoder(avctx,
1369 status = VTSessionCopyProperty(vtctx->session,
1370 kVTCompressionPropertyKey_AllowFrameReordering,
1371 kCFAllocatorDefault,
1372 &has_b_frames_cfbool);
1374 if (!status && has_b_frames_cfbool) {
1375 //Some devices don't output B-frames for main profile, even if requested.
1376 vtctx->has_b_frames = CFBooleanGetValue(has_b_frames_cfbool);
1377 CFRelease(has_b_frames_cfbool);
1379 avctx->has_b_frames = vtctx->has_b_frames;
1383 CFRelease(gamma_level);
1385 if (pixel_buffer_info)
1386 CFRelease(pixel_buffer_info);
1388 CFRelease(enc_info);
1393 static void vtenc_get_frame_info(CMSampleBufferRef buffer, bool *is_key_frame)
1395 CFArrayRef attachments;
1396 CFDictionaryRef attachment;
1397 CFBooleanRef not_sync;
1400 attachments = CMSampleBufferGetSampleAttachmentsArray(buffer, false);
1401 len = !attachments ? 0 : CFArrayGetCount(attachments);
1404 *is_key_frame = true;
1408 attachment = CFArrayGetValueAtIndex(attachments, 0);
1410 if (CFDictionaryGetValueIfPresent(attachment,
1411 kCMSampleAttachmentKey_NotSync,
1412 (const void **)¬_sync))
1414 *is_key_frame = !CFBooleanGetValue(not_sync);
1416 *is_key_frame = true;
1420 static int is_post_sei_nal_type(int nal_type){
1421 return nal_type != H264_NAL_SEI &&
1422 nal_type != H264_NAL_SPS &&
1423 nal_type != H264_NAL_PPS &&
1424 nal_type != H264_NAL_AUD;
1428 * Finds the sei message start/size of type find_sei_type.
1429 * If more than one of that type exists, the last one is returned.
1431 static int find_sei_end(AVCodecContext *avctx,
1437 size_t sei_payload_size = 0;
1438 int sei_payload_type = 0;
1440 uint8_t *nal_start = nal_data;
1445 nal_type = *nal_data & 0x1F;
1446 if (nal_type != H264_NAL_SEI)
1452 if (nal_data[nal_size - 1] == 0x80)
1455 while (nal_size > 0 && *nal_data > 0) {
1457 sei_payload_type += *nal_data;
1460 } while (nal_size > 0 && *nal_data == 0xFF);
1463 av_log(avctx, AV_LOG_ERROR, "Unexpected end of SEI NAL Unit parsing type.\n");
1464 return AVERROR_INVALIDDATA;
1468 sei_payload_size += *nal_data;
1471 } while (nal_size > 0 && *nal_data == 0xFF);
1473 if (nal_size < sei_payload_size) {
1474 av_log(avctx, AV_LOG_ERROR, "Unexpected end of SEI NAL Unit parsing size.\n");
1475 return AVERROR_INVALIDDATA;
1478 nal_data += sei_payload_size;
1479 nal_size -= sei_payload_size;
1482 *sei_end = nal_data;
1484 return nal_data - nal_start + 1;
1488 * Copies the data inserting emulation prevention bytes as needed.
1489 * Existing data in the destination can be taken into account by providing
1490 * dst with a dst_offset > 0.
1492 * @return The number of bytes copied on success. On failure, the negative of
1493 * the number of bytes needed to copy src is returned.
1495 static int copy_emulation_prev(const uint8_t *src,
1504 uint8_t* dst_end = dst + dst_size;
1505 const uint8_t* src_end = src + src_size;
1506 int start_at = dst_offset > 2 ? dst_offset - 2 : 0;
1508 for (i = start_at; i < dst_offset && i < dst_size; i++) {
1517 for (; src < src_end; src++, dst++) {
1519 int insert_ep3_byte = *src <= 3;
1520 if (insert_ep3_byte) {
1538 wrote_bytes = dst - dst_start;
1541 return -wrote_bytes;
1546 static int write_sei(const ExtraSEI *sei,
1551 uint8_t *sei_start = dst;
1552 size_t remaining_sei_size = sei->size;
1553 size_t remaining_dst_size = dst_size;
1558 if (!remaining_dst_size)
1559 return AVERROR_BUFFER_TOO_SMALL;
1561 while (sei_type && remaining_dst_size != 0) {
1562 int sei_byte = sei_type > 255 ? 255 : sei_type;
1565 sei_type -= sei_byte;
1567 remaining_dst_size--;
1571 return AVERROR_BUFFER_TOO_SMALL;
1573 while (remaining_sei_size && remaining_dst_size != 0) {
1574 int size_byte = remaining_sei_size > 255 ? 255 : remaining_sei_size;
1577 remaining_sei_size -= size_byte;
1579 remaining_dst_size--;
1582 if (remaining_dst_size < sei->size)
1583 return AVERROR_BUFFER_TOO_SMALL;
1585 header_bytes = dst - sei_start;
1587 offset = header_bytes;
1588 bytes_written = copy_emulation_prev(sei->data,
1593 if (bytes_written < 0)
1594 return AVERROR_BUFFER_TOO_SMALL;
1596 bytes_written += header_bytes;
1597 return bytes_written;
1601 * Copies NAL units and replaces length codes with
1602 * H.264 Annex B start codes. On failure, the contents of
1603 * dst_data may have been modified.
1605 * @param length_code_size Byte length of each length code
1606 * @param sample_buffer NAL units prefixed with length codes.
1607 * @param sei Optional A53 closed captions SEI data.
1608 * @param dst_data Must be zeroed before calling this function.
1609 * Contains the copied NAL units prefixed with
1610 * start codes when the function returns
1612 * @param dst_size Length of dst_data
1613 * @return 0 on success
1614 * AVERROR_INVALIDDATA if length_code_size is invalid
1615 * AVERROR_BUFFER_TOO_SMALL if dst_data is too small
1616 * or if a length_code in src_data specifies data beyond
1617 * the end of its buffer.
1619 static int copy_replace_length_codes(
1620 AVCodecContext *avctx,
1621 size_t length_code_size,
1622 CMSampleBufferRef sample_buffer,
1627 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1628 size_t remaining_src_size = src_size;
1629 size_t remaining_dst_size = dst_size;
1630 size_t src_offset = 0;
1633 uint8_t size_buf[4];
1635 CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample_buffer);
1637 if (length_code_size > 4) {
1638 return AVERROR_INVALIDDATA;
1641 while (remaining_src_size > 0) {
1642 size_t curr_src_len;
1643 size_t curr_dst_len;
1649 status = CMBlockBufferCopyDataBytes(block,
1654 av_log(avctx, AV_LOG_ERROR, "Cannot copy length: %d\n", status);
1655 return AVERROR_EXTERNAL;
1658 status = CMBlockBufferCopyDataBytes(block,
1659 src_offset + length_code_size,
1664 av_log(avctx, AV_LOG_ERROR, "Cannot copy type: %d\n", status);
1665 return AVERROR_EXTERNAL;
1670 for (i = 0; i < length_code_size; i++) {
1672 box_len |= size_buf[i];
1675 if (sei && !wrote_sei && is_post_sei_nal_type(nal_type)) {
1676 //No SEI NAL unit - insert.
1679 memcpy(dst_data, start_code, sizeof(start_code));
1680 dst_data += sizeof(start_code);
1681 remaining_dst_size -= sizeof(start_code);
1683 *dst_data = H264_NAL_SEI;
1685 remaining_dst_size--;
1687 wrote_bytes = write_sei(sei,
1688 H264_SEI_TYPE_USER_DATA_REGISTERED,
1690 remaining_dst_size);
1692 if (wrote_bytes < 0)
1695 remaining_dst_size -= wrote_bytes;
1696 dst_data += wrote_bytes;
1698 if (remaining_dst_size <= 0)
1699 return AVERROR_BUFFER_TOO_SMALL;
1704 remaining_dst_size--;
1709 curr_src_len = box_len + length_code_size;
1710 curr_dst_len = box_len + sizeof(start_code);
1712 if (remaining_src_size < curr_src_len) {
1713 return AVERROR_BUFFER_TOO_SMALL;
1716 if (remaining_dst_size < curr_dst_len) {
1717 return AVERROR_BUFFER_TOO_SMALL;
1720 dst_box = dst_data + sizeof(start_code);
1722 memcpy(dst_data, start_code, sizeof(start_code));
1723 status = CMBlockBufferCopyDataBytes(block,
1724 src_offset + length_code_size,
1729 av_log(avctx, AV_LOG_ERROR, "Cannot copy data: %d\n", status);
1730 return AVERROR_EXTERNAL;
1733 if (sei && !wrote_sei && nal_type == H264_NAL_SEI) {
1734 //Found SEI NAL unit - append.
1739 old_sei_length = find_sei_end(avctx, dst_box, box_len, &new_sei);
1740 if (old_sei_length < 0)
1743 wrote_bytes = write_sei(sei,
1744 H264_SEI_TYPE_USER_DATA_REGISTERED,
1746 remaining_dst_size - old_sei_length);
1747 if (wrote_bytes < 0)
1750 if (new_sei + wrote_bytes >= dst_data + remaining_dst_size)
1751 return AVERROR_BUFFER_TOO_SMALL;
1753 new_sei[wrote_bytes++] = 0x80;
1754 extra_bytes = wrote_bytes - (dst_box + box_len - new_sei);
1756 dst_data += extra_bytes;
1757 remaining_dst_size -= extra_bytes;
1762 src_offset += curr_src_len;
1763 dst_data += curr_dst_len;
1765 remaining_src_size -= curr_src_len;
1766 remaining_dst_size -= curr_dst_len;
1773 * Returns a sufficient number of bytes to contain the sei data.
1774 * It may be greater than the minimum required.
1776 static int get_sei_msg_bytes(const ExtraSEI* sei, int type){
1781 copied_size = -copy_emulation_prev(sei->data,
1787 if ((sei->size % 255) == 0) //may result in an extra byte
1790 return copied_size + sei->size / 255 + 1 + type / 255 + 1;
1793 static int vtenc_cm_to_avpacket(
1794 AVCodecContext *avctx,
1795 CMSampleBufferRef sample_buffer,
1799 VTEncContext *vtctx = avctx->priv_data;
1804 size_t length_code_size;
1805 size_t header_size = 0;
1807 size_t out_buf_size;
1808 size_t sei_nalu_size = 0;
1810 int64_t time_base_num;
1814 CMVideoFormatDescriptionRef vid_fmt;
1817 vtenc_get_frame_info(sample_buffer, &is_key_frame);
1818 status = get_length_code_size(avctx, sample_buffer, &length_code_size);
1819 if (status) return status;
1821 add_header = is_key_frame && !(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER);
1824 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
1826 av_log(avctx, AV_LOG_ERROR, "Cannot get format description.\n");
1827 return AVERROR_EXTERNAL;
1830 int status = get_params_size(avctx, vid_fmt, &header_size);
1831 if (status) return status;
1834 status = count_nalus(length_code_size, sample_buffer, &nalu_count);
1839 size_t msg_size = get_sei_msg_bytes(sei,
1840 H264_SEI_TYPE_USER_DATA_REGISTERED);
1842 sei_nalu_size = sizeof(start_code) + 1 + msg_size + 1;
1845 in_buf_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1846 out_buf_size = header_size +
1849 nalu_count * ((int)sizeof(start_code) - (int)length_code_size);
1851 status = ff_alloc_packet2(avctx, pkt, out_buf_size, out_buf_size);
1856 status = copy_param_sets(avctx, vid_fmt, pkt->data, out_buf_size);
1857 if(status) return status;
1860 status = copy_replace_length_codes(
1865 pkt->data + header_size,
1866 pkt->size - header_size
1870 av_log(avctx, AV_LOG_ERROR, "Error copying packet data: %d\n", status);
1875 pkt->flags |= AV_PKT_FLAG_KEY;
1878 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
1879 dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
1881 if (CMTIME_IS_INVALID(dts)) {
1882 if (!vtctx->has_b_frames) {
1885 av_log(avctx, AV_LOG_ERROR, "DTS is invalid.\n");
1886 return AVERROR_EXTERNAL;
1890 dts_delta = vtctx->dts_delta >= 0 ? vtctx->dts_delta : 0;
1891 time_base_num = avctx->time_base.num;
1892 pkt->pts = pts.value / time_base_num;
1893 pkt->dts = dts.value / time_base_num - dts_delta;
1894 pkt->size = out_buf_size;
1900 * contiguous_buf_size is 0 if not contiguous, and the size of the buffer
1901 * containing all planes if so.
1903 static int get_cv_pixel_info(
1904 AVCodecContext *avctx,
1905 const AVFrame *frame,
1911 size_t *contiguous_buf_size)
1913 VTEncContext *vtctx = avctx->priv_data;
1914 int av_format = frame->format;
1915 int av_color_range = frame->color_range;
1920 status = get_cv_pixel_format(avctx, av_format, av_color_range, color, &range_guessed);
1924 "Could not get pixel format for color format '%s' range '%s'.\n",
1925 av_get_pix_fmt_name(av_format),
1926 av_color_range > AVCOL_RANGE_UNSPECIFIED &&
1927 av_color_range < AVCOL_RANGE_NB ?
1928 av_color_range_name(av_color_range) :
1931 return AVERROR(EINVAL);
1934 if (range_guessed) {
1935 if (!vtctx->warned_color_range) {
1936 vtctx->warned_color_range = true;
1939 "Color range not set for %s. Using MPEG range.\n",
1940 av_get_pix_fmt_name(av_format));
1944 switch (av_format) {
1945 case AV_PIX_FMT_NV12:
1948 widths [0] = avctx->width;
1949 heights[0] = avctx->height;
1950 strides[0] = frame ? frame->linesize[0] : avctx->width;
1952 widths [1] = (avctx->width + 1) / 2;
1953 heights[1] = (avctx->height + 1) / 2;
1954 strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) & -2;
1957 case AV_PIX_FMT_YUV420P:
1960 widths [0] = avctx->width;
1961 heights[0] = avctx->height;
1962 strides[0] = frame ? frame->linesize[0] : avctx->width;
1964 widths [1] = (avctx->width + 1) / 2;
1965 heights[1] = (avctx->height + 1) / 2;
1966 strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) / 2;
1968 widths [2] = (avctx->width + 1) / 2;
1969 heights[2] = (avctx->height + 1) / 2;
1970 strides[2] = frame ? frame->linesize[2] : (avctx->width + 1) / 2;
1977 "Could not get frame format info for color %d range %d.\n",
1981 return AVERROR(EINVAL);
1984 *contiguous_buf_size = 0;
1985 for (i = 0; i < *plane_count; i++) {
1986 if (i < *plane_count - 1 &&
1987 frame->data[i] + strides[i] * heights[i] != frame->data[i + 1]) {
1988 *contiguous_buf_size = 0;
1992 *contiguous_buf_size += strides[i] * heights[i];
1998 #if !TARGET_OS_IPHONE
1999 //Not used on iOS - frame is always copied.
2000 static void free_avframe(
2005 const void *plane_addresses[])
2007 AVFrame *frame = release_ctx;
2008 av_frame_free(&frame);
2011 //Not used on OSX - frame is never copied.
2012 static int copy_avframe_to_pixel_buffer(AVCodecContext *avctx,
2013 const AVFrame *frame,
2014 CVPixelBufferRef cv_img,
2015 const size_t *plane_strides,
2016 const size_t *plane_rows)
2028 status = CVPixelBufferLockBaseAddress(cv_img, 0);
2033 "Error: Could not lock base address of CVPixelBuffer: %d.\n",
2038 if (CVPixelBufferIsPlanar(cv_img)) {
2039 plane_count = CVPixelBufferGetPlaneCount(cv_img);
2040 for (i = 0; frame->data[i]; i++) {
2041 if (i == plane_count) {
2042 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2045 "Error: different number of planes in AVFrame and CVPixelBuffer.\n"
2048 return AVERROR_EXTERNAL;
2051 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(cv_img, i);
2052 src_addr = (uint8_t*)frame->data[i];
2053 dst_stride = CVPixelBufferGetBytesPerRowOfPlane(cv_img, i);
2054 src_stride = plane_strides[i];
2055 rows = plane_rows[i];
2057 if (dst_stride == src_stride) {
2058 memcpy(dst_addr, src_addr, src_stride * rows);
2060 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2062 for (j = 0; j < rows; j++) {
2063 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2068 if (frame->data[1]) {
2069 CVPixelBufferUnlockBaseAddress(cv_img, 0);
2072 "Error: different number of planes in AVFrame and non-planar CVPixelBuffer.\n"
2075 return AVERROR_EXTERNAL;
2078 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddress(cv_img);
2079 src_addr = (uint8_t*)frame->data[0];
2080 dst_stride = CVPixelBufferGetBytesPerRow(cv_img);
2081 src_stride = plane_strides[0];
2082 rows = plane_rows[0];
2084 if (dst_stride == src_stride) {
2085 memcpy(dst_addr, src_addr, src_stride * rows);
2087 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
2089 for (j = 0; j < rows; j++) {
2090 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
2095 status = CVPixelBufferUnlockBaseAddress(cv_img, 0);
2097 av_log(avctx, AV_LOG_ERROR, "Error: Could not unlock CVPixelBuffer base address: %d.\n", status);
2098 return AVERROR_EXTERNAL;
2103 #endif //!TARGET_OS_IPHONE
2105 static int create_cv_pixel_buffer(AVCodecContext *avctx,
2106 const AVFrame *frame,
2107 CVPixelBufferRef *cv_img)
2111 size_t widths [AV_NUM_DATA_POINTERS];
2112 size_t heights[AV_NUM_DATA_POINTERS];
2113 size_t strides[AV_NUM_DATA_POINTERS];
2115 size_t contiguous_buf_size;
2116 #if TARGET_OS_IPHONE
2117 CVPixelBufferPoolRef pix_buf_pool;
2118 VTEncContext* vtctx = avctx->priv_data;
2120 CFMutableDictionaryRef pix_buf_attachments = CFDictionaryCreateMutable(
2121 kCFAllocatorDefault,
2123 &kCFCopyStringDictionaryKeyCallBacks,
2124 &kCFTypeDictionaryValueCallBacks);
2126 if (!pix_buf_attachments) return AVERROR(ENOMEM);
2129 if (avctx->pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX) {
2130 av_assert0(frame->format == AV_PIX_FMT_VIDEOTOOLBOX);
2132 *cv_img = (CVPixelBufferRef)frame->data[3];
2133 av_assert0(*cv_img);
2139 memset(widths, 0, sizeof(widths));
2140 memset(heights, 0, sizeof(heights));
2141 memset(strides, 0, sizeof(strides));
2143 status = get_cv_pixel_info(
2151 &contiguous_buf_size
2158 "Error: Cannot convert format %d color_range %d: %d\n",
2164 return AVERROR_EXTERNAL;
2167 #if TARGET_OS_IPHONE
2168 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->session);
2169 if (!pix_buf_pool) {
2170 av_log(avctx, AV_LOG_ERROR, "Could not get pixel buffer pool.\n");
2171 return AVERROR_EXTERNAL;
2174 status = CVPixelBufferPoolCreatePixelBuffer(NULL,
2180 av_log(avctx, AV_LOG_ERROR, "Could not create pixel buffer from pool: %d.\n", status);
2181 return AVERROR_EXTERNAL;
2184 status = copy_avframe_to_pixel_buffer(avctx, frame, *cv_img, strides, heights);
2191 AVFrame *enc_frame = av_frame_alloc();
2192 if (!enc_frame) return AVERROR(ENOMEM);
2194 status = av_frame_ref(enc_frame, frame);
2196 av_frame_free(&enc_frame);
2200 status = CVPixelBufferCreateWithPlanarBytes(
2201 kCFAllocatorDefault,
2206 contiguous_buf_size,
2208 (void **)enc_frame->data,
2218 add_color_attr(avctx, pix_buf_attachments);
2219 CVBufferSetAttachments(*cv_img, pix_buf_attachments, kCVAttachmentMode_ShouldPropagate);
2220 CFRelease(pix_buf_attachments);
2223 av_log(avctx, AV_LOG_ERROR, "Error: Could not create CVPixelBuffer: %d\n", status);
2224 return AVERROR_EXTERNAL;
2231 static int create_encoder_dict_h264(const AVFrame *frame,
2232 CFDictionaryRef* dict_out)
2234 CFDictionaryRef dict = NULL;
2235 if (frame->pict_type == AV_PICTURE_TYPE_I) {
2236 const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
2237 const void *vals[] = { kCFBooleanTrue };
2239 dict = CFDictionaryCreate(NULL, keys, vals, 1, NULL, NULL);
2240 if(!dict) return AVERROR(ENOMEM);
2247 static int vtenc_send_frame(AVCodecContext *avctx,
2248 VTEncContext *vtctx,
2249 const AVFrame *frame)
2252 CFDictionaryRef frame_dict;
2253 CVPixelBufferRef cv_img = NULL;
2254 AVFrameSideData *side_data = NULL;
2255 ExtraSEI *sei = NULL;
2256 int status = create_cv_pixel_buffer(avctx, frame, &cv_img);
2258 if (status) return status;
2260 status = create_encoder_dict_h264(frame, &frame_dict);
2266 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
2267 if (vtctx->a53_cc && side_data && side_data->size) {
2268 sei = av_mallocz(sizeof(*sei));
2270 av_log(avctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
2272 int ret = ff_alloc_a53_sei(frame, 0, &sei->data, &sei->size);
2274 av_log(avctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
2281 time = CMTimeMake(frame->pts * avctx->time_base.num, avctx->time_base.den);
2282 status = VTCompressionSessionEncodeFrame(
2292 if (frame_dict) CFRelease(frame_dict);
2296 av_log(avctx, AV_LOG_ERROR, "Error: cannot encode frame: %d\n", status);
2297 return AVERROR_EXTERNAL;
2303 static av_cold int vtenc_frame(
2304 AVCodecContext *avctx,
2306 const AVFrame *frame,
2309 VTEncContext *vtctx = avctx->priv_data;
2312 CMSampleBufferRef buf = NULL;
2313 ExtraSEI *sei = NULL;
2316 status = vtenc_send_frame(avctx, vtctx, frame);
2319 status = AVERROR_EXTERNAL;
2323 if (vtctx->frame_ct_in == 0) {
2324 vtctx->first_pts = frame->pts;
2325 } else if(vtctx->frame_ct_in == 1 && vtctx->has_b_frames) {
2326 vtctx->dts_delta = frame->pts - vtctx->first_pts;
2329 vtctx->frame_ct_in++;
2330 } else if(!vtctx->flushing) {
2331 vtctx->flushing = true;
2333 status = VTCompressionSessionCompleteFrames(vtctx->session,
2337 av_log(avctx, AV_LOG_ERROR, "Error flushing frames: %d\n", status);
2338 status = AVERROR_EXTERNAL;
2344 get_frame = vtctx->dts_delta >= 0 || !frame;
2350 status = vtenc_q_pop(vtctx, !frame, &buf, &sei);
2351 if (status) goto end_nopkt;
2352 if (!buf) goto end_nopkt;
2354 status = vtenc_cm_to_avpacket(avctx, buf, pkt, sei);
2356 if (sei->data) av_free(sei->data);
2360 if (status) goto end_nopkt;
2366 av_packet_unref(pkt);
2370 static int vtenc_populate_extradata(AVCodecContext *avctx,
2371 CMVideoCodecType codec_type,
2372 CFStringRef profile_level,
2373 CFNumberRef gamma_level,
2374 CFDictionaryRef enc_info,
2375 CFDictionaryRef pixel_buffer_info)
2377 VTEncContext *vtctx = avctx->priv_data;
2378 AVFrame *frame = av_frame_alloc();
2379 int y_size = avctx->width * avctx->height;
2380 int chroma_size = (avctx->width / 2) * (avctx->height / 2);
2381 CMSampleBufferRef buf = NULL;
2385 return AVERROR(ENOMEM);
2387 frame->buf[0] = av_buffer_alloc(y_size + 2 * chroma_size);
2390 status = AVERROR(ENOMEM);
2394 status = vtenc_create_encoder(avctx,
2404 frame->data[0] = frame->buf[0]->data;
2405 memset(frame->data[0], 0, y_size);
2407 frame->data[1] = frame->buf[0]->data + y_size;
2408 memset(frame->data[1], 128, chroma_size);
2411 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
2412 frame->data[2] = frame->buf[0]->data + y_size + chroma_size;
2413 memset(frame->data[2], 128, chroma_size);
2416 frame->linesize[0] = avctx->width;
2418 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
2419 frame->linesize[1] =
2420 frame->linesize[2] = (avctx->width + 1) / 2;
2422 frame->linesize[1] = (avctx->width + 1) / 2;
2425 frame->format = avctx->pix_fmt;
2426 frame->width = avctx->width;
2427 frame->height = avctx->height;
2428 frame->colorspace = avctx->colorspace;
2429 frame->color_range = avctx->color_range;
2430 frame->color_trc = avctx->color_trc;
2431 frame->color_primaries = avctx->color_primaries;
2434 status = vtenc_send_frame(avctx, vtctx, frame);
2436 av_log(avctx, AV_LOG_ERROR, "Error sending frame: %d\n", status);
2440 //Populates extradata - output frames are flushed and param sets are available.
2441 status = VTCompressionSessionCompleteFrames(vtctx->session,
2447 status = vtenc_q_pop(vtctx, 0, &buf, NULL);
2449 av_log(avctx, AV_LOG_ERROR, "popping: %d\n", status);
2459 CFRelease(vtctx->session);
2461 vtctx->session = NULL;
2462 vtctx->frame_ct_out = 0;
2464 av_frame_unref(frame);
2465 av_frame_free(&frame);
2467 av_assert0(status != 0 || (avctx->extradata && avctx->extradata_size > 0));
2472 static av_cold int vtenc_close(AVCodecContext *avctx)
2474 VTEncContext *vtctx = avctx->priv_data;
2476 if(!vtctx->session) return 0;
2478 VTCompressionSessionCompleteFrames(vtctx->session,
2480 clear_frame_queue(vtctx);
2481 pthread_cond_destroy(&vtctx->cv_sample_sent);
2482 pthread_mutex_destroy(&vtctx->lock);
2483 CFRelease(vtctx->session);
2484 vtctx->session = NULL;
2486 if (vtctx->color_primaries) {
2487 CFRelease(vtctx->color_primaries);
2488 vtctx->color_primaries = NULL;
2491 if (vtctx->transfer_function) {
2492 CFRelease(vtctx->transfer_function);
2493 vtctx->transfer_function = NULL;
2496 if (vtctx->ycbcr_matrix) {
2497 CFRelease(vtctx->ycbcr_matrix);
2498 vtctx->ycbcr_matrix = NULL;
2504 static const enum AVPixelFormat pix_fmts[] = {
2505 AV_PIX_FMT_VIDEOTOOLBOX,
2511 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
2512 #define COMMON_OPTIONS \
2513 { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL, \
2514 { .i64 = 0 }, 0, 1, VE }, \
2515 { "realtime", "Hint that encoding should happen in real-time if not faster (e.g. capturing from camera).", \
2516 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2517 { "frames_before", "Other frames will come before the frames in this session. This helps smooth concatenation issues.", \
2518 OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
2519 { "frames_after", "Other frames will come after the frames in this session. This helps smooth concatenation issues.", \
2520 OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
2522 #define OFFSET(x) offsetof(VTEncContext, x)
2523 static const AVOption h264_options[] = {
2524 { "profile", "Profile", OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = H264_PROF_AUTO }, H264_PROF_AUTO, H264_PROF_COUNT, VE, "profile" },
2525 { "baseline", "Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_BASELINE }, INT_MIN, INT_MAX, VE, "profile" },
2526 { "main", "Main Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
2527 { "high", "High Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_HIGH }, INT_MIN, INT_MAX, VE, "profile" },
2529 { "level", "Level", OFFSET(level), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, VE, "level" },
2530 { "1.3", "Level 1.3, only available with Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX, VE, "level" },
2531 { "3.0", "Level 3.0", 0, AV_OPT_TYPE_CONST, { .i64 = 30 }, INT_MIN, INT_MAX, VE, "level" },
2532 { "3.1", "Level 3.1", 0, AV_OPT_TYPE_CONST, { .i64 = 31 }, INT_MIN, INT_MAX, VE, "level" },
2533 { "3.2", "Level 3.2", 0, AV_OPT_TYPE_CONST, { .i64 = 32 }, INT_MIN, INT_MAX, VE, "level" },
2534 { "4.0", "Level 4.0", 0, AV_OPT_TYPE_CONST, { .i64 = 40 }, INT_MIN, INT_MAX, VE, "level" },
2535 { "4.1", "Level 4.1", 0, AV_OPT_TYPE_CONST, { .i64 = 41 }, INT_MIN, INT_MAX, VE, "level" },
2536 { "4.2", "Level 4.2", 0, AV_OPT_TYPE_CONST, { .i64 = 42 }, INT_MIN, INT_MAX, VE, "level" },
2537 { "5.0", "Level 5.0", 0, AV_OPT_TYPE_CONST, { .i64 = 50 }, INT_MIN, INT_MAX, VE, "level" },
2538 { "5.1", "Level 5.1", 0, AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX, VE, "level" },
2539 { "5.2", "Level 5.2", 0, AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX, VE, "level" },
2541 { "coder", "Entropy coding", OFFSET(entropy), AV_OPT_TYPE_INT, { .i64 = VT_ENTROPY_NOT_SET }, VT_ENTROPY_NOT_SET, VT_CABAC, VE, "coder" },
2542 { "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC }, INT_MIN, INT_MAX, VE, "coder" },
2543 { "vlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC }, INT_MIN, INT_MAX, VE, "coder" },
2544 { "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC }, INT_MIN, INT_MAX, VE, "coder" },
2545 { "ac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC }, INT_MIN, INT_MAX, VE, "coder" },
2547 { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VE },
2553 static const AVClass h264_videotoolbox_class = {
2554 .class_name = "h264_videotoolbox",
2555 .item_name = av_default_item_name,
2556 .option = h264_options,
2557 .version = LIBAVUTIL_VERSION_INT,
2560 AVCodec ff_h264_videotoolbox_encoder = {
2561 .name = "h264_videotoolbox",
2562 .long_name = NULL_IF_CONFIG_SMALL("VideoToolbox H.264 Encoder"),
2563 .type = AVMEDIA_TYPE_VIDEO,
2564 .id = AV_CODEC_ID_H264,
2565 .priv_data_size = sizeof(VTEncContext),
2566 .pix_fmts = pix_fmts,
2568 .encode2 = vtenc_frame,
2569 .close = vtenc_close,
2570 .capabilities = AV_CODEC_CAP_DELAY,
2571 .priv_class = &h264_videotoolbox_class,
2572 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2573 FF_CODEC_CAP_INIT_CLEANUP,
2576 static const AVOption hevc_options[] = {
2577 { "profile", "Profile", OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = HEVC_PROF_AUTO }, HEVC_PROF_AUTO, HEVC_PROF_COUNT, VE, "profile" },
2578 { "main", "Main Profile", 0, AV_OPT_TYPE_CONST, { .i64 = HEVC_PROF_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
2579 { "main10", "Main10 Profile", 0, AV_OPT_TYPE_CONST, { .i64 = HEVC_PROF_MAIN10 }, INT_MIN, INT_MAX, VE, "profile" },
2585 static const AVClass hevc_videotoolbox_class = {
2586 .class_name = "hevc_videotoolbox",
2587 .item_name = av_default_item_name,
2588 .option = hevc_options,
2589 .version = LIBAVUTIL_VERSION_INT,
2592 AVCodec ff_hevc_videotoolbox_encoder = {
2593 .name = "hevc_videotoolbox",
2594 .long_name = NULL_IF_CONFIG_SMALL("VideoToolbox H.265 Encoder"),
2595 .type = AVMEDIA_TYPE_VIDEO,
2596 .id = AV_CODEC_ID_HEVC,
2597 .priv_data_size = sizeof(VTEncContext),
2598 .pix_fmts = pix_fmts,
2600 .encode2 = vtenc_frame,
2601 .close = vtenc_close,
2602 .capabilities = AV_CODEC_CAP_DELAY,
2603 .priv_class = &hevc_videotoolbox_class,
2604 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2605 FF_CODEC_CAP_INIT_CLEANUP,