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/atomic.h"
30 #include "libavutil/avstring.h"
31 #include "libavcodec/avcodec.h"
32 #include "libavutil/pixdesc.h"
39 # define kCVImageBufferColorPrimaries_ITU_R_2020 CFSTR("ITU_R_2020")
40 # define kCVImageBufferTransferFunction_ITU_R_2020 CFSTR("ITU_R_2020")
41 # define kCVImageBufferYCbCrMatrix_ITU_R_2020 CFSTR("ITU_R_2020")
44 typedef enum VT_H264Profile {
52 typedef enum VTH264Entropy{
58 static const uint8_t start_code[] = { 0, 0, 0, 1 };
60 typedef struct ExtraSEI {
65 typedef struct BufNode {
66 CMSampleBufferRef cm_buffer;
72 typedef struct VTEncContext {
74 VTCompressionSessionRef session;
75 CFStringRef ycbcr_matrix;
76 CFStringRef color_primaries;
77 CFStringRef transfer_function;
80 pthread_cond_t cv_sample_sent;
97 int64_t frames_before;
104 bool warned_color_range;
108 static int vtenc_populate_extradata(AVCodecContext *avctx,
109 CMVideoCodecType codec_type,
110 CFStringRef profile_level,
111 CFNumberRef gamma_level,
112 CFDictionaryRef enc_info,
113 CFDictionaryRef pixel_buffer_info);
116 * NULL-safe release of *refPtr, and sets value to NULL.
118 static void vt_release_num(CFNumberRef* refPtr){
127 static void set_async_error(VTEncContext *vtctx, int err)
131 pthread_mutex_lock(&vtctx->lock);
133 vtctx->async_error = err;
135 info = vtctx->q_head;
136 vtctx->q_head = vtctx->q_tail = NULL;
139 BufNode *next = info->next;
140 CFRelease(info->cm_buffer);
145 pthread_mutex_unlock(&vtctx->lock);
148 static void clear_frame_queue(VTEncContext *vtctx)
150 set_async_error(vtctx, 0);
153 static int vtenc_q_pop(VTEncContext *vtctx, bool wait, CMSampleBufferRef *buf, ExtraSEI **sei)
157 pthread_mutex_lock(&vtctx->lock);
159 if (vtctx->async_error) {
160 pthread_mutex_unlock(&vtctx->lock);
161 return vtctx->async_error;
164 if (vtctx->flushing && vtctx->frame_ct_in == vtctx->frame_ct_out) {
167 pthread_mutex_unlock(&vtctx->lock);
171 while (!vtctx->q_head && !vtctx->async_error && wait) {
172 pthread_cond_wait(&vtctx->cv_sample_sent, &vtctx->lock);
175 if (!vtctx->q_head) {
176 pthread_mutex_unlock(&vtctx->lock);
181 info = vtctx->q_head;
182 vtctx->q_head = vtctx->q_head->next;
183 if (!vtctx->q_head) {
184 vtctx->q_tail = NULL;
187 pthread_mutex_unlock(&vtctx->lock);
189 *buf = info->cm_buffer;
192 } else if (info->sei) {
193 if (info->sei->data) av_free(info->sei->data);
198 vtctx->frame_ct_out++;
203 static void vtenc_q_push(VTEncContext *vtctx, CMSampleBufferRef buffer, ExtraSEI *sei)
205 BufNode *info = av_malloc(sizeof(BufNode));
207 set_async_error(vtctx, AVERROR(ENOMEM));
212 info->cm_buffer = buffer;
216 pthread_mutex_lock(&vtctx->lock);
217 pthread_cond_signal(&vtctx->cv_sample_sent);
219 if (!vtctx->q_head) {
220 vtctx->q_head = info;
222 vtctx->q_tail->next = info;
225 vtctx->q_tail = info;
227 pthread_mutex_unlock(&vtctx->lock);
230 static int count_nalus(size_t length_code_size,
231 CMSampleBufferRef sample_buffer,
238 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
239 CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample_buffer);
241 if (length_code_size > 4)
242 return AVERROR_INVALIDDATA;
244 while (offset < src_size) {
249 status = CMBlockBufferCopyDataBytes(block,
254 for (i = 0; i < length_code_size; i++) {
256 box_len |= size_buf[i];
259 curr_src_len = box_len + length_code_size;
260 offset += curr_src_len;
269 static CMVideoCodecType get_cm_codec_type(enum AVCodecID id)
272 case AV_CODEC_ID_H264: return kCMVideoCodecType_H264;
278 * Get the parameter sets from a CMSampleBufferRef.
279 * @param dst If *dst isn't NULL, the parameters are copied into existing
280 * memory. *dst_size must be set accordingly when *dst != NULL.
281 * If *dst is NULL, it will be allocated.
282 * In all cases, *dst_size is set to the number of bytes used starting
285 static int get_params_size(
286 AVCodecContext *avctx,
287 CMVideoFormatDescriptionRef vid_fmt,
290 size_t total_size = 0;
292 int is_count_bad = 0;
295 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
307 for (i = 0; i < ps_count || is_count_bad; i++) {
310 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
318 * When ps_count is invalid, status != 0 ends the loop normally
319 * unless we didn't get any parameter sets.
321 if (i > 0 && is_count_bad) status = 0;
326 total_size += ps_size + sizeof(start_code);
330 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set sizes: %d\n", status);
331 return AVERROR_EXTERNAL;
338 static int copy_param_sets(
339 AVCodecContext *avctx,
340 CMVideoFormatDescriptionRef vid_fmt,
345 int is_count_bad = 0;
350 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
363 for (i = 0; i < ps_count || is_count_bad; i++) {
368 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
375 if (i > 0 && is_count_bad) status = 0;
380 next_offset = offset + sizeof(start_code) + ps_size;
381 if (dst_size < next_offset) {
382 av_log(avctx, AV_LOG_ERROR, "Error: buffer too small for parameter sets.\n");
383 return AVERROR_BUFFER_TOO_SMALL;
386 memcpy(dst + offset, start_code, sizeof(start_code));
387 offset += sizeof(start_code);
389 memcpy(dst + offset, ps, ps_size);
390 offset = next_offset;
394 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set data: %d\n", status);
395 return AVERROR_EXTERNAL;
401 static int set_extradata(AVCodecContext *avctx, CMSampleBufferRef sample_buffer)
403 CMVideoFormatDescriptionRef vid_fmt;
407 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
409 av_log(avctx, AV_LOG_ERROR, "No video format.\n");
410 return AVERROR_EXTERNAL;
413 status = get_params_size(avctx, vid_fmt, &total_size);
415 av_log(avctx, AV_LOG_ERROR, "Could not get parameter sets.\n");
419 avctx->extradata = av_mallocz(total_size + AV_INPUT_BUFFER_PADDING_SIZE);
420 if (!avctx->extradata) {
421 return AVERROR(ENOMEM);
423 avctx->extradata_size = total_size;
425 status = copy_param_sets(avctx, vid_fmt, avctx->extradata, total_size);
428 av_log(avctx, AV_LOG_ERROR, "Could not copy param sets.\n");
435 static void vtenc_output_callback(
437 void *sourceFrameCtx,
439 VTEncodeInfoFlags flags,
440 CMSampleBufferRef sample_buffer)
442 AVCodecContext *avctx = ctx;
443 VTEncContext *vtctx = avctx->priv_data;
444 ExtraSEI *sei = sourceFrameCtx;
446 if (vtctx->async_error) {
447 if(sample_buffer) CFRelease(sample_buffer);
451 if (status || !sample_buffer) {
452 av_log(avctx, AV_LOG_ERROR, "Error encoding frame: %d\n", (int)status);
453 set_async_error(vtctx, AVERROR_EXTERNAL);
457 if (!avctx->extradata && (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
458 int set_status = set_extradata(avctx, sample_buffer);
460 set_async_error(vtctx, set_status);
465 vtenc_q_push(vtctx, sample_buffer, sei);
468 static int get_length_code_size(
469 AVCodecContext *avctx,
470 CMSampleBufferRef sample_buffer,
473 CMVideoFormatDescriptionRef vid_fmt;
477 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
479 av_log(avctx, AV_LOG_ERROR, "Error getting buffer format description.\n");
480 return AVERROR_EXTERNAL;
483 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
490 av_log(avctx, AV_LOG_ERROR, "Error getting length code size: %d\n", status);
491 return AVERROR_EXTERNAL;
499 * Returns true on success.
501 * If profile_level_val is NULL and this method returns true, don't specify the
502 * profile/level to the encoder.
504 static bool get_vt_profile_level(AVCodecContext *avctx,
505 CFStringRef *profile_level_val)
507 VTEncContext *vtctx = avctx->priv_data;
508 int64_t profile = vtctx->profile;
510 if (profile == H264_PROF_AUTO && vtctx->level) {
511 //Need to pick a profile if level is not auto-selected.
512 profile = vtctx->has_b_frames ? H264_PROF_MAIN : H264_PROF_BASELINE;
515 *profile_level_val = NULL;
521 case H264_PROF_BASELINE:
522 switch (vtctx->level) {
523 case 0: *profile_level_val = kVTProfileLevel_H264_Baseline_AutoLevel; break;
524 case 13: *profile_level_val = kVTProfileLevel_H264_Baseline_1_3; break;
525 case 30: *profile_level_val = kVTProfileLevel_H264_Baseline_3_0; break;
526 case 31: *profile_level_val = kVTProfileLevel_H264_Baseline_3_1; break;
527 case 32: *profile_level_val = kVTProfileLevel_H264_Baseline_3_2; break;
528 case 40: *profile_level_val = kVTProfileLevel_H264_Baseline_4_0; break;
529 case 41: *profile_level_val = kVTProfileLevel_H264_Baseline_4_1; break;
530 case 42: *profile_level_val = kVTProfileLevel_H264_Baseline_4_2; break;
531 case 50: *profile_level_val = kVTProfileLevel_H264_Baseline_5_0; break;
532 case 51: *profile_level_val = kVTProfileLevel_H264_Baseline_5_1; break;
533 case 52: *profile_level_val = kVTProfileLevel_H264_Baseline_5_2; break;
538 switch (vtctx->level) {
539 case 0: *profile_level_val = kVTProfileLevel_H264_Main_AutoLevel; break;
540 case 30: *profile_level_val = kVTProfileLevel_H264_Main_3_0; break;
541 case 31: *profile_level_val = kVTProfileLevel_H264_Main_3_1; break;
542 case 32: *profile_level_val = kVTProfileLevel_H264_Main_3_2; break;
543 case 40: *profile_level_val = kVTProfileLevel_H264_Main_4_0; break;
544 case 41: *profile_level_val = kVTProfileLevel_H264_Main_4_1; break;
545 case 42: *profile_level_val = kVTProfileLevel_H264_Main_4_2; break;
546 case 50: *profile_level_val = kVTProfileLevel_H264_Main_5_0; break;
547 case 51: *profile_level_val = kVTProfileLevel_H264_Main_5_1; break;
548 case 52: *profile_level_val = kVTProfileLevel_H264_Main_5_2; break;
553 switch (vtctx->level) {
554 case 0: *profile_level_val = kVTProfileLevel_H264_High_AutoLevel; break;
555 case 30: *profile_level_val = kVTProfileLevel_H264_High_3_0; break;
556 case 31: *profile_level_val = kVTProfileLevel_H264_High_3_1; break;
557 case 32: *profile_level_val = kVTProfileLevel_H264_High_3_2; break;
558 case 40: *profile_level_val = kVTProfileLevel_H264_High_4_0; break;
559 case 41: *profile_level_val = kVTProfileLevel_H264_High_4_1; break;
560 case 42: *profile_level_val = kVTProfileLevel_H264_High_4_2; break;
561 case 50: *profile_level_val = kVTProfileLevel_H264_High_5_0; break;
562 case 51: *profile_level_val = kVTProfileLevel_H264_High_5_1; break;
563 case 52: *profile_level_val = kVTProfileLevel_H264_High_5_2; break;
568 if (!*profile_level_val) {
569 av_log(avctx, AV_LOG_ERROR, "Invalid Profile/Level.\n");
576 static int get_cv_pixel_format(AVCodecContext* avctx,
577 enum AVPixelFormat fmt,
578 enum AVColorRange range,
579 int* av_pixel_format,
582 if (range_guessed) *range_guessed = range != AVCOL_RANGE_MPEG &&
583 range != AVCOL_RANGE_JPEG;
585 //MPEG range is used when no range is set
586 if (fmt == AV_PIX_FMT_NV12) {
587 *av_pixel_format = range == AVCOL_RANGE_JPEG ?
588 kCVPixelFormatType_420YpCbCr8BiPlanarFullRange :
589 kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
590 } else if (fmt == AV_PIX_FMT_YUV420P) {
591 *av_pixel_format = range == AVCOL_RANGE_JPEG ?
592 kCVPixelFormatType_420YpCbCr8PlanarFullRange :
593 kCVPixelFormatType_420YpCbCr8Planar;
595 return AVERROR(EINVAL);
601 static void add_color_attr(AVCodecContext *avctx, CFMutableDictionaryRef dict) {
602 VTEncContext *vtctx = avctx->priv_data;
604 if (vtctx->color_primaries) {
605 CFDictionarySetValue(dict,
606 kCVImageBufferColorPrimariesKey,
607 vtctx->color_primaries);
610 if (vtctx->transfer_function) {
611 CFDictionarySetValue(dict,
612 kCVImageBufferTransferFunctionKey,
613 vtctx->transfer_function);
616 if (vtctx->ycbcr_matrix) {
617 CFDictionarySetValue(dict,
618 kCVImageBufferYCbCrMatrixKey,
619 vtctx->ycbcr_matrix);
623 static int create_cv_pixel_buffer_info(AVCodecContext* avctx,
624 CFMutableDictionaryRef* dict)
626 CFNumberRef cv_color_format_num = NULL;
627 CFNumberRef width_num = NULL;
628 CFNumberRef height_num = NULL;
629 CFMutableDictionaryRef pixel_buffer_info = NULL;
631 int status = get_cv_pixel_format(avctx,
636 if (status) return status;
638 pixel_buffer_info = CFDictionaryCreateMutable(
641 &kCFCopyStringDictionaryKeyCallBacks,
642 &kCFTypeDictionaryValueCallBacks);
644 if (!pixel_buffer_info) goto pbinfo_nomem;
646 cv_color_format_num = CFNumberCreate(kCFAllocatorDefault,
649 if (!cv_color_format_num) goto pbinfo_nomem;
651 CFDictionarySetValue(pixel_buffer_info,
652 kCVPixelBufferPixelFormatTypeKey,
653 cv_color_format_num);
654 vt_release_num(&cv_color_format_num);
656 width_num = CFNumberCreate(kCFAllocatorDefault,
659 if (!width_num) return AVERROR(ENOMEM);
661 CFDictionarySetValue(pixel_buffer_info,
662 kCVPixelBufferWidthKey,
664 vt_release_num(&width_num);
666 height_num = CFNumberCreate(kCFAllocatorDefault,
669 if (!height_num) goto pbinfo_nomem;
671 CFDictionarySetValue(pixel_buffer_info,
672 kCVPixelBufferHeightKey,
674 vt_release_num(&height_num);
676 add_color_attr(avctx, pixel_buffer_info);
678 *dict = pixel_buffer_info;
682 vt_release_num(&cv_color_format_num);
683 vt_release_num(&width_num);
684 vt_release_num(&height_num);
685 if (pixel_buffer_info) CFRelease(pixel_buffer_info);
687 return AVERROR(ENOMEM);
690 static int get_cv_color_primaries(AVCodecContext *avctx,
691 CFStringRef *primaries)
693 enum AVColorPrimaries pri = avctx->color_primaries;
695 case AVCOL_PRI_UNSPECIFIED:
699 case AVCOL_PRI_BT709:
700 *primaries = kCVImageBufferColorPrimaries_ITU_R_709_2;
703 case AVCOL_PRI_BT2020:
704 *primaries = kCVImageBufferColorPrimaries_ITU_R_2020;
708 av_log(avctx, AV_LOG_ERROR, "Color primaries %s is not supported.\n", av_color_primaries_name(pri));
716 static int get_cv_transfer_function(AVCodecContext *avctx,
717 CFStringRef *transfer_fnc,
718 CFNumberRef *gamma_level)
720 enum AVColorTransferCharacteristic trc = avctx->color_trc;
725 case AVCOL_TRC_UNSPECIFIED:
726 *transfer_fnc = NULL;
729 case AVCOL_TRC_BT709:
730 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_709_2;
733 case AVCOL_TRC_SMPTE240M:
734 *transfer_fnc = kCVImageBufferTransferFunction_SMPTE_240M_1995;
737 case AVCOL_TRC_GAMMA22:
739 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
740 *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type, &gamma);
743 case AVCOL_TRC_GAMMA28:
745 *transfer_fnc = kCVImageBufferTransferFunction_UseGamma;
746 *gamma_level = CFNumberCreate(NULL, kCFNumberFloat32Type, &gamma);
749 case AVCOL_TRC_BT2020_10:
750 case AVCOL_TRC_BT2020_12:
751 *transfer_fnc = kCVImageBufferTransferFunction_ITU_R_2020;
755 av_log(avctx, AV_LOG_ERROR, "Transfer function %s is not supported.\n", av_color_transfer_name(trc));
762 static int get_cv_ycbcr_matrix(AVCodecContext *avctx, CFStringRef *matrix) {
763 switch(avctx->colorspace) {
764 case AVCOL_SPC_BT709:
765 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_709_2;
768 case AVCOL_SPC_UNSPECIFIED:
772 case AVCOL_SPC_BT470BG:
773 case AVCOL_SPC_SMPTE170M:
774 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_601_4;
777 case AVCOL_SPC_SMPTE240M:
778 *matrix = kCVImageBufferYCbCrMatrix_SMPTE_240M_1995;
781 case AVCOL_SPC_BT2020_NCL:
782 *matrix = kCVImageBufferYCbCrMatrix_ITU_R_2020;
786 av_log(avctx, AV_LOG_ERROR, "Color space %s is not supported.\n", av_color_space_name(avctx->colorspace));
793 static int vtenc_create_encoder(AVCodecContext *avctx,
794 CMVideoCodecType codec_type,
795 CFStringRef profile_level,
796 CFNumberRef gamma_level,
797 CFDictionaryRef enc_info,
798 CFDictionaryRef pixel_buffer_info,
799 VTCompressionSessionRef *session)
801 VTEncContext *vtctx = avctx->priv_data;
802 SInt32 bit_rate = avctx->bit_rate;
803 CFNumberRef bit_rate_num;
805 int status = VTCompressionSessionCreate(kCFAllocatorDefault,
812 vtenc_output_callback,
816 if (status || !vtctx->session) {
817 av_log(avctx, AV_LOG_ERROR, "Error: cannot create compression session: %d\n", status);
819 #if !TARGET_OS_IPHONE
820 if (!vtctx->allow_sw) {
821 av_log(avctx, AV_LOG_ERROR, "Try -allow_sw 1. The hardware encoder may be busy, or not supported.\n");
825 return AVERROR_EXTERNAL;
828 bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
831 if (!bit_rate_num) return AVERROR(ENOMEM);
833 status = VTSessionSetProperty(vtctx->session,
834 kVTCompressionPropertyKey_AverageBitRate,
836 CFRelease(bit_rate_num);
839 av_log(avctx, AV_LOG_ERROR, "Error setting bitrate property: %d\n", status);
840 return AVERROR_EXTERNAL;
844 status = VTSessionSetProperty(vtctx->session,
845 kVTCompressionPropertyKey_ProfileLevel,
848 av_log(avctx, AV_LOG_ERROR, "Error setting profile/level property: %d\n", status);
849 return AVERROR_EXTERNAL;
853 if (avctx->gop_size > 0) {
854 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
858 return AVERROR(ENOMEM);
861 status = VTSessionSetProperty(vtctx->session,
862 kVTCompressionPropertyKey_MaxKeyFrameInterval,
867 av_log(avctx, AV_LOG_ERROR, "Error setting 'max key-frame interval' property: %d\n", status);
868 return AVERROR_EXTERNAL;
872 if (vtctx->frames_before) {
873 status = VTSessionSetProperty(vtctx->session,
874 kVTCompressionPropertyKey_MoreFramesBeforeStart,
877 if (status == kVTPropertyNotSupportedErr) {
878 av_log(avctx, AV_LOG_WARNING, "frames_before property is not supported on this device. Ignoring.\n");
880 av_log(avctx, AV_LOG_ERROR, "Error setting frames_before property: %d\n", status);
884 if (vtctx->frames_after) {
885 status = VTSessionSetProperty(vtctx->session,
886 kVTCompressionPropertyKey_MoreFramesAfterEnd,
889 if (status == kVTPropertyNotSupportedErr) {
890 av_log(avctx, AV_LOG_WARNING, "frames_after property is not supported on this device. Ignoring.\n");
892 av_log(avctx, AV_LOG_ERROR, "Error setting frames_after property: %d\n", status);
896 if (avctx->sample_aspect_ratio.num != 0) {
899 CFMutableDictionaryRef par;
900 AVRational *avpar = &avctx->sample_aspect_ratio;
902 av_reduce(&avpar->num, &avpar->den,
903 avpar->num, avpar->den,
906 num = CFNumberCreate(kCFAllocatorDefault,
910 den = CFNumberCreate(kCFAllocatorDefault,
916 par = CFDictionaryCreateMutable(kCFAllocatorDefault,
918 &kCFCopyStringDictionaryKeyCallBacks,
919 &kCFTypeDictionaryValueCallBacks);
921 if (!par || !num || !den) {
922 if (par) CFRelease(par);
923 if (num) CFRelease(num);
924 if (den) CFRelease(den);
926 return AVERROR(ENOMEM);
929 CFDictionarySetValue(
931 kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing,
934 CFDictionarySetValue(
936 kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing,
939 status = VTSessionSetProperty(vtctx->session,
940 kVTCompressionPropertyKey_PixelAspectRatio,
950 "Error setting pixel aspect ratio to %d:%d: %d.\n",
951 avctx->sample_aspect_ratio.num,
952 avctx->sample_aspect_ratio.den,
955 return AVERROR_EXTERNAL;
960 if (vtctx->transfer_function) {
961 status = VTSessionSetProperty(vtctx->session,
962 kVTCompressionPropertyKey_TransferFunction,
963 vtctx->transfer_function);
966 av_log(avctx, AV_LOG_WARNING, "Could not set transfer function: %d\n", status);
971 if (vtctx->ycbcr_matrix) {
972 status = VTSessionSetProperty(vtctx->session,
973 kVTCompressionPropertyKey_YCbCrMatrix,
974 vtctx->ycbcr_matrix);
977 av_log(avctx, AV_LOG_WARNING, "Could not set ycbcr matrix: %d\n", status);
982 if (vtctx->color_primaries) {
983 status = VTSessionSetProperty(vtctx->session,
984 kVTCompressionPropertyKey_ColorPrimaries,
985 vtctx->color_primaries);
988 av_log(avctx, AV_LOG_WARNING, "Could not set color primaries: %d\n", status);
993 status = VTSessionSetProperty(vtctx->session,
994 kCVImageBufferGammaLevelKey,
998 av_log(avctx, AV_LOG_WARNING, "Could not set gamma level: %d\n", status);
1002 if (!vtctx->has_b_frames) {
1003 status = VTSessionSetProperty(vtctx->session,
1004 kVTCompressionPropertyKey_AllowFrameReordering,
1008 av_log(avctx, AV_LOG_ERROR, "Error setting 'allow frame reordering' property: %d\n", status);
1009 return AVERROR_EXTERNAL;
1013 if (vtctx->entropy != VT_ENTROPY_NOT_SET) {
1014 CFStringRef entropy = vtctx->entropy == VT_CABAC ?
1015 kVTH264EntropyMode_CABAC:
1016 kVTH264EntropyMode_CAVLC;
1018 status = VTSessionSetProperty(vtctx->session,
1019 kVTCompressionPropertyKey_H264EntropyMode,
1023 av_log(avctx, AV_LOG_ERROR, "Error setting entropy property: %d\n", status);
1024 return AVERROR_EXTERNAL;
1028 if (vtctx->realtime) {
1029 status = VTSessionSetProperty(vtctx->session,
1030 kVTCompressionPropertyKey_RealTime,
1034 av_log(avctx, AV_LOG_ERROR, "Error setting realtime property: %d\n", status);
1038 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
1040 av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", status);
1041 return AVERROR_EXTERNAL;
1047 static av_cold int vtenc_init(AVCodecContext *avctx)
1049 CFMutableDictionaryRef enc_info;
1050 CFMutableDictionaryRef pixel_buffer_info;
1051 CMVideoCodecType codec_type;
1052 VTEncContext *vtctx = avctx->priv_data;
1053 CFStringRef profile_level;
1054 CFBooleanRef has_b_frames_cfbool;
1055 CFNumberRef gamma_level = NULL;
1058 codec_type = get_cm_codec_type(avctx->codec_id);
1060 av_log(avctx, AV_LOG_ERROR, "Error: no mapping for AVCodecID %d\n", avctx->codec_id);
1061 return AVERROR(EINVAL);
1064 vtctx->has_b_frames = avctx->max_b_frames > 0;
1065 if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){
1066 av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");
1067 vtctx->has_b_frames = false;
1070 if (vtctx->entropy == VT_CABAC && vtctx->profile == H264_PROF_BASELINE) {
1071 av_log(avctx, AV_LOG_WARNING, "CABAC entropy requires 'main' or 'high' profile, but baseline was requested. Encode will not use CABAC entropy.\n");
1072 vtctx->entropy = VT_ENTROPY_NOT_SET;
1075 if (!get_vt_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);
1077 vtctx->session = NULL;
1079 enc_info = CFDictionaryCreateMutable(
1080 kCFAllocatorDefault,
1082 &kCFCopyStringDictionaryKeyCallBacks,
1083 &kCFTypeDictionaryValueCallBacks
1086 if (!enc_info) return AVERROR(ENOMEM);
1088 #if !TARGET_OS_IPHONE
1089 if (!vtctx->allow_sw) {
1090 CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, kCFBooleanTrue);
1092 CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, kCFBooleanTrue);
1096 if (avctx->pix_fmt != AV_PIX_FMT_VIDEOTOOLBOX) {
1097 status = create_cv_pixel_buffer_info(avctx, &pixel_buffer_info);
1101 pixel_buffer_info = NULL;
1104 pthread_mutex_init(&vtctx->lock, NULL);
1105 pthread_cond_init(&vtctx->cv_sample_sent, NULL);
1106 vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0;
1108 get_cv_transfer_function(avctx, &vtctx->transfer_function, &gamma_level);
1109 get_cv_ycbcr_matrix(avctx, &vtctx->ycbcr_matrix);
1110 get_cv_color_primaries(avctx, &vtctx->color_primaries);
1113 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1114 status = vtenc_populate_extradata(avctx,
1124 status = vtenc_create_encoder(avctx,
1135 status = VTSessionCopyProperty(vtctx->session,
1136 kVTCompressionPropertyKey_AllowFrameReordering,
1137 kCFAllocatorDefault,
1138 &has_b_frames_cfbool);
1141 //Some devices don't output B-frames for main profile, even if requested.
1142 vtctx->has_b_frames = CFBooleanGetValue(has_b_frames_cfbool);
1143 CFRelease(has_b_frames_cfbool);
1145 avctx->has_b_frames = vtctx->has_b_frames;
1149 CFRelease(gamma_level);
1151 if (pixel_buffer_info)
1152 CFRelease(pixel_buffer_info);
1154 CFRelease(enc_info);
1159 static void vtenc_get_frame_info(CMSampleBufferRef buffer, bool *is_key_frame)
1161 CFArrayRef attachments;
1162 CFDictionaryRef attachment;
1163 CFBooleanRef not_sync;
1166 attachments = CMSampleBufferGetSampleAttachmentsArray(buffer, false);
1167 len = !attachments ? 0 : CFArrayGetCount(attachments);
1170 *is_key_frame = true;
1174 attachment = CFArrayGetValueAtIndex(attachments, 0);
1176 if (CFDictionaryGetValueIfPresent(attachment,
1177 kCMSampleAttachmentKey_NotSync,
1178 (const void **)¬_sync))
1180 *is_key_frame = !CFBooleanGetValue(not_sync);
1182 *is_key_frame = true;
1186 static int is_post_sei_nal_type(int nal_type){
1187 return nal_type != H264_NAL_SEI &&
1188 nal_type != H264_NAL_SPS &&
1189 nal_type != H264_NAL_PPS &&
1190 nal_type != H264_NAL_AUD;
1194 * Finds the sei message start/size of type find_sei_type.
1195 * If more than one of that type exists, the last one is returned.
1197 static int find_sei_end(AVCodecContext *avctx,
1203 size_t sei_payload_size = 0;
1204 int sei_payload_type = 0;
1206 uint8_t *nal_start = nal_data;
1211 nal_type = *nal_data & 0x1F;
1212 if (nal_type != H264_NAL_SEI)
1218 if (nal_data[nal_size - 1] == 0x80)
1221 while (nal_size > 0 && *nal_data > 0) {
1223 sei_payload_type += *nal_data;
1226 } while (nal_size > 0 && *nal_data == 0xFF);
1229 av_log(avctx, AV_LOG_ERROR, "Unexpected end of SEI NAL Unit parsing type.\n");
1230 return AVERROR_INVALIDDATA;
1234 sei_payload_size += *nal_data;
1237 } while (nal_size > 0 && *nal_data == 0xFF);
1239 if (nal_size < sei_payload_size) {
1240 av_log(avctx, AV_LOG_ERROR, "Unexpected end of SEI NAL Unit parsing size.\n");
1241 return AVERROR_INVALIDDATA;
1244 nal_data += sei_payload_size;
1245 nal_size -= sei_payload_size;
1248 *sei_end = nal_data;
1250 return nal_data - nal_start + 1;
1254 * Copies the data inserting emulation prevention bytes as needed.
1255 * Existing data in the destination can be taken into account by providing
1256 * dst with a dst_offset > 0.
1258 * @return The number of bytes copied on success. On failure, the negative of
1259 * the number of bytes needed to copy src is returned.
1261 static int copy_emulation_prev(const uint8_t *src,
1270 uint8_t* dst_end = dst + dst_size;
1271 const uint8_t* src_end = src + src_size;
1272 int start_at = dst_offset > 2 ? dst_offset - 2 : 0;
1274 for (i = start_at; i < dst_offset && i < dst_size; i++) {
1283 for (; src < src_end; src++, dst++) {
1285 int insert_ep3_byte = *src <= 3;
1286 if (insert_ep3_byte) {
1304 wrote_bytes = dst - dst_start;
1307 return -wrote_bytes;
1312 static int write_sei(const ExtraSEI *sei,
1317 uint8_t *sei_start = dst;
1318 size_t remaining_sei_size = sei->size;
1319 size_t remaining_dst_size = dst_size;
1324 if (!remaining_dst_size)
1325 return AVERROR_BUFFER_TOO_SMALL;
1327 while (sei_type && remaining_dst_size != 0) {
1328 int sei_byte = sei_type > 255 ? 255 : sei_type;
1331 sei_type -= sei_byte;
1333 remaining_dst_size--;
1337 return AVERROR_BUFFER_TOO_SMALL;
1339 while (remaining_sei_size && remaining_dst_size != 0) {
1340 int size_byte = remaining_sei_size > 255 ? 255 : remaining_sei_size;
1343 remaining_sei_size -= size_byte;
1345 remaining_dst_size--;
1348 if (remaining_dst_size < sei->size)
1349 return AVERROR_BUFFER_TOO_SMALL;
1351 header_bytes = dst - sei_start;
1353 offset = header_bytes;
1354 bytes_written = copy_emulation_prev(sei->data,
1359 if (bytes_written < 0)
1360 return AVERROR_BUFFER_TOO_SMALL;
1362 bytes_written += header_bytes;
1363 return bytes_written;
1367 * Copies NAL units and replaces length codes with
1368 * H.264 Annex B start codes. On failure, the contents of
1369 * dst_data may have been modified.
1371 * @param length_code_size Byte length of each length code
1372 * @param sample_buffer NAL units prefixed with length codes.
1373 * @param sei Optional A53 closed captions SEI data.
1374 * @param dst_data Must be zeroed before calling this function.
1375 * Contains the copied NAL units prefixed with
1376 * start codes when the function returns
1378 * @param dst_size Length of dst_data
1379 * @return 0 on success
1380 * AVERROR_INVALIDDATA if length_code_size is invalid
1381 * AVERROR_BUFFER_TOO_SMALL if dst_data is too small
1382 * or if a length_code in src_data specifies data beyond
1383 * the end of its buffer.
1385 static int copy_replace_length_codes(
1386 AVCodecContext *avctx,
1387 size_t length_code_size,
1388 CMSampleBufferRef sample_buffer,
1393 size_t src_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1394 size_t remaining_src_size = src_size;
1395 size_t remaining_dst_size = dst_size;
1396 size_t src_offset = 0;
1399 uint8_t size_buf[4];
1401 CMBlockBufferRef block = CMSampleBufferGetDataBuffer(sample_buffer);
1403 if (length_code_size > 4) {
1404 return AVERROR_INVALIDDATA;
1407 while (remaining_src_size > 0) {
1408 size_t curr_src_len;
1409 size_t curr_dst_len;
1415 status = CMBlockBufferCopyDataBytes(block,
1420 av_log(avctx, AV_LOG_ERROR, "Cannot copy length: %d\n", status);
1421 return AVERROR_EXTERNAL;
1424 status = CMBlockBufferCopyDataBytes(block,
1425 src_offset + length_code_size,
1430 av_log(avctx, AV_LOG_ERROR, "Cannot copy type: %d\n", status);
1431 return AVERROR_EXTERNAL;
1436 for (i = 0; i < length_code_size; i++) {
1438 box_len |= size_buf[i];
1441 if (sei && !wrote_sei && is_post_sei_nal_type(nal_type)) {
1442 //No SEI NAL unit - insert.
1445 memcpy(dst_data, start_code, sizeof(start_code));
1446 dst_data += sizeof(start_code);
1447 remaining_dst_size -= sizeof(start_code);
1449 *dst_data = H264_NAL_SEI;
1451 remaining_dst_size--;
1453 wrote_bytes = write_sei(sei,
1454 SEI_TYPE_USER_DATA_REGISTERED,
1456 remaining_dst_size);
1458 if (wrote_bytes < 0)
1461 remaining_dst_size -= wrote_bytes;
1462 dst_data += wrote_bytes;
1464 if (remaining_dst_size <= 0)
1465 return AVERROR_BUFFER_TOO_SMALL;
1470 remaining_dst_size--;
1475 curr_src_len = box_len + length_code_size;
1476 curr_dst_len = box_len + sizeof(start_code);
1478 if (remaining_src_size < curr_src_len) {
1479 return AVERROR_BUFFER_TOO_SMALL;
1482 if (remaining_dst_size < curr_dst_len) {
1483 return AVERROR_BUFFER_TOO_SMALL;
1486 dst_box = dst_data + sizeof(start_code);
1488 memcpy(dst_data, start_code, sizeof(start_code));
1489 status = CMBlockBufferCopyDataBytes(block,
1490 src_offset + length_code_size,
1495 av_log(avctx, AV_LOG_ERROR, "Cannot copy data: %d\n", status);
1496 return AVERROR_EXTERNAL;
1499 if (sei && !wrote_sei && nal_type == H264_NAL_SEI) {
1500 //Found SEI NAL unit - append.
1505 old_sei_length = find_sei_end(avctx, dst_box, box_len, &new_sei);
1506 if (old_sei_length < 0)
1509 wrote_bytes = write_sei(sei,
1510 SEI_TYPE_USER_DATA_REGISTERED,
1512 remaining_dst_size - old_sei_length);
1513 if (wrote_bytes < 0)
1516 if (new_sei + wrote_bytes >= dst_data + remaining_dst_size)
1517 return AVERROR_BUFFER_TOO_SMALL;
1519 new_sei[wrote_bytes++] = 0x80;
1520 extra_bytes = wrote_bytes - (dst_box + box_len - new_sei);
1522 dst_data += extra_bytes;
1523 remaining_dst_size -= extra_bytes;
1528 src_offset += curr_src_len;
1529 dst_data += curr_dst_len;
1531 remaining_src_size -= curr_src_len;
1532 remaining_dst_size -= curr_dst_len;
1539 * Returns a sufficient number of bytes to contain the sei data.
1540 * It may be greater than the minimum required.
1542 static int get_sei_msg_bytes(const ExtraSEI* sei, int type){
1547 copied_size = -copy_emulation_prev(sei->data,
1553 if ((sei->size % 255) == 0) //may result in an extra byte
1556 return copied_size + sei->size / 255 + 1 + type / 255 + 1;
1559 static int vtenc_cm_to_avpacket(
1560 AVCodecContext *avctx,
1561 CMSampleBufferRef sample_buffer,
1565 VTEncContext *vtctx = avctx->priv_data;
1570 size_t length_code_size;
1571 size_t header_size = 0;
1573 size_t out_buf_size;
1574 size_t sei_nalu_size = 0;
1576 int64_t time_base_num;
1580 CMVideoFormatDescriptionRef vid_fmt;
1583 vtenc_get_frame_info(sample_buffer, &is_key_frame);
1584 status = get_length_code_size(avctx, sample_buffer, &length_code_size);
1585 if (status) return status;
1587 add_header = is_key_frame && !(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER);
1590 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
1592 av_log(avctx, AV_LOG_ERROR, "Cannot get format description.\n");
1593 return AVERROR_EXTERNAL;
1596 int status = get_params_size(avctx, vid_fmt, &header_size);
1597 if (status) return status;
1600 status = count_nalus(length_code_size, sample_buffer, &nalu_count);
1605 size_t msg_size = get_sei_msg_bytes(sei,
1606 SEI_TYPE_USER_DATA_REGISTERED);
1608 sei_nalu_size = sizeof(start_code) + 1 + msg_size + 1;
1611 in_buf_size = CMSampleBufferGetTotalSampleSize(sample_buffer);
1612 out_buf_size = header_size +
1615 nalu_count * ((int)sizeof(start_code) - (int)length_code_size);
1617 status = ff_alloc_packet2(avctx, pkt, out_buf_size, out_buf_size);
1622 status = copy_param_sets(avctx, vid_fmt, pkt->data, out_buf_size);
1623 if(status) return status;
1626 status = copy_replace_length_codes(
1631 pkt->data + header_size,
1632 pkt->size - header_size
1636 av_log(avctx, AV_LOG_ERROR, "Error copying packet data: %d\n", status);
1641 pkt->flags |= AV_PKT_FLAG_KEY;
1644 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
1645 dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
1647 if (CMTIME_IS_INVALID(dts)) {
1648 if (!vtctx->has_b_frames) {
1651 av_log(avctx, AV_LOG_ERROR, "DTS is invalid.\n");
1652 return AVERROR_EXTERNAL;
1656 dts_delta = vtctx->dts_delta >= 0 ? vtctx->dts_delta : 0;
1657 time_base_num = avctx->time_base.num;
1658 pkt->pts = pts.value / time_base_num;
1659 pkt->dts = dts.value / time_base_num - dts_delta;
1660 pkt->size = out_buf_size;
1666 * contiguous_buf_size is 0 if not contiguous, and the size of the buffer
1667 * containing all planes if so.
1669 static int get_cv_pixel_info(
1670 AVCodecContext *avctx,
1671 const AVFrame *frame,
1677 size_t *contiguous_buf_size)
1679 VTEncContext *vtctx = avctx->priv_data;
1680 int av_format = frame->format;
1681 int av_color_range = av_frame_get_color_range(frame);
1686 status = get_cv_pixel_format(avctx, av_format, av_color_range, color, &range_guessed);
1690 "Could not get pixel format for color format '%s' range '%s'.\n",
1691 av_get_pix_fmt_name(av_format),
1692 av_color_range > AVCOL_RANGE_UNSPECIFIED &&
1693 av_color_range < AVCOL_RANGE_NB ?
1694 av_color_range_name(av_color_range) :
1697 return AVERROR(EINVAL);
1700 if (range_guessed) {
1701 if (!vtctx->warned_color_range) {
1702 vtctx->warned_color_range = true;
1705 "Color range not set for %s. Using MPEG range.\n",
1706 av_get_pix_fmt_name(av_format));
1709 av_log(avctx, AV_LOG_WARNING, "");
1712 switch (av_format) {
1713 case AV_PIX_FMT_NV12:
1716 widths [0] = avctx->width;
1717 heights[0] = avctx->height;
1718 strides[0] = frame ? frame->linesize[0] : avctx->width;
1720 widths [1] = (avctx->width + 1) / 2;
1721 heights[1] = (avctx->height + 1) / 2;
1722 strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) & -2;
1725 case AV_PIX_FMT_YUV420P:
1728 widths [0] = avctx->width;
1729 heights[0] = avctx->height;
1730 strides[0] = frame ? frame->linesize[0] : avctx->width;
1732 widths [1] = (avctx->width + 1) / 2;
1733 heights[1] = (avctx->height + 1) / 2;
1734 strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) / 2;
1736 widths [2] = (avctx->width + 1) / 2;
1737 heights[2] = (avctx->height + 1) / 2;
1738 strides[2] = frame ? frame->linesize[2] : (avctx->width + 1) / 2;
1745 "Could not get frame format info for color %d range %d.\n",
1749 return AVERROR(EINVAL);
1752 *contiguous_buf_size = 0;
1753 for (i = 0; i < *plane_count; i++) {
1754 if (i < *plane_count - 1 &&
1755 frame->data[i] + strides[i] * heights[i] != frame->data[i + 1]) {
1756 *contiguous_buf_size = 0;
1760 *contiguous_buf_size += strides[i] * heights[i];
1766 #if !TARGET_OS_IPHONE
1767 //Not used on iOS - frame is always copied.
1768 static void free_avframe(
1773 const void *plane_addresses[])
1775 AVFrame *frame = release_ctx;
1776 av_frame_free(&frame);
1779 //Not used on OSX - frame is never copied.
1780 static int copy_avframe_to_pixel_buffer(AVCodecContext *avctx,
1781 const AVFrame *frame,
1782 CVPixelBufferRef cv_img,
1783 const size_t *plane_strides,
1784 const size_t *plane_rows)
1796 status = CVPixelBufferLockBaseAddress(cv_img, 0);
1801 "Error: Could not lock base address of CVPixelBuffer: %d.\n",
1806 if (CVPixelBufferIsPlanar(cv_img)) {
1807 plane_count = CVPixelBufferGetPlaneCount(cv_img);
1808 for (i = 0; frame->data[i]; i++) {
1809 if (i == plane_count) {
1810 CVPixelBufferUnlockBaseAddress(cv_img, 0);
1813 "Error: different number of planes in AVFrame and CVPixelBuffer.\n"
1816 return AVERROR_EXTERNAL;
1819 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(cv_img, i);
1820 src_addr = (uint8_t*)frame->data[i];
1821 dst_stride = CVPixelBufferGetBytesPerRowOfPlane(cv_img, i);
1822 src_stride = plane_strides[i];
1823 rows = plane_rows[i];
1825 if (dst_stride == src_stride) {
1826 memcpy(dst_addr, src_addr, src_stride * rows);
1828 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
1830 for (j = 0; j < rows; j++) {
1831 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
1836 if (frame->data[1]) {
1837 CVPixelBufferUnlockBaseAddress(cv_img, 0);
1840 "Error: different number of planes in AVFrame and non-planar CVPixelBuffer.\n"
1843 return AVERROR_EXTERNAL;
1846 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddress(cv_img);
1847 src_addr = (uint8_t*)frame->data[0];
1848 dst_stride = CVPixelBufferGetBytesPerRow(cv_img);
1849 src_stride = plane_strides[0];
1850 rows = plane_rows[0];
1852 if (dst_stride == src_stride) {
1853 memcpy(dst_addr, src_addr, src_stride * rows);
1855 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
1857 for (j = 0; j < rows; j++) {
1858 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
1863 status = CVPixelBufferUnlockBaseAddress(cv_img, 0);
1865 av_log(avctx, AV_LOG_ERROR, "Error: Could not unlock CVPixelBuffer base address: %d.\n", status);
1866 return AVERROR_EXTERNAL;
1871 #endif //!TARGET_OS_IPHONE
1873 static int create_cv_pixel_buffer(AVCodecContext *avctx,
1874 const AVFrame *frame,
1875 CVPixelBufferRef *cv_img)
1879 size_t widths [AV_NUM_DATA_POINTERS];
1880 size_t heights[AV_NUM_DATA_POINTERS];
1881 size_t strides[AV_NUM_DATA_POINTERS];
1883 size_t contiguous_buf_size;
1884 #if TARGET_OS_IPHONE
1885 CVPixelBufferPoolRef pix_buf_pool;
1886 VTEncContext* vtctx = avctx->priv_data;
1888 CFMutableDictionaryRef pix_buf_attachments = CFDictionaryCreateMutable(
1889 kCFAllocatorDefault,
1891 &kCFCopyStringDictionaryKeyCallBacks,
1892 &kCFTypeDictionaryValueCallBacks);
1894 if (!pix_buf_attachments) return AVERROR(ENOMEM);
1897 if (avctx->pix_fmt == AV_PIX_FMT_VIDEOTOOLBOX) {
1898 av_assert0(frame->format == AV_PIX_FMT_VIDEOTOOLBOX);
1900 *cv_img = (CVPixelBufferRef)frame->data[3];
1901 av_assert0(*cv_img);
1907 memset(widths, 0, sizeof(widths));
1908 memset(heights, 0, sizeof(heights));
1909 memset(strides, 0, sizeof(strides));
1911 status = get_cv_pixel_info(
1919 &contiguous_buf_size
1926 "Error: Cannot convert format %d color_range %d: %d\n",
1928 av_frame_get_color_range(frame),
1932 return AVERROR_EXTERNAL;
1935 #if TARGET_OS_IPHONE
1936 pix_buf_pool = VTCompressionSessionGetPixelBufferPool(vtctx->session);
1937 if (!pix_buf_pool) {
1938 av_log(avctx, AV_LOG_ERROR, "Could not get pixel buffer pool.\n");
1939 return AVERROR_EXTERNAL;
1942 status = CVPixelBufferPoolCreatePixelBuffer(NULL,
1948 av_log(avctx, AV_LOG_ERROR, "Could not create pixel buffer from pool: %d.\n", status);
1949 return AVERROR_EXTERNAL;
1952 status = copy_avframe_to_pixel_buffer(avctx, frame, *cv_img, strides, heights);
1959 AVFrame *enc_frame = av_frame_alloc();
1960 if (!enc_frame) return AVERROR(ENOMEM);
1962 status = av_frame_ref(enc_frame, frame);
1964 av_frame_free(&enc_frame);
1968 status = CVPixelBufferCreateWithPlanarBytes(
1969 kCFAllocatorDefault,
1974 contiguous_buf_size,
1976 (void **)enc_frame->data,
1986 add_color_attr(avctx, pix_buf_attachments);
1987 CVBufferSetAttachments(*cv_img, pix_buf_attachments, kCVAttachmentMode_ShouldPropagate);
1988 CFRelease(pix_buf_attachments);
1991 av_log(avctx, AV_LOG_ERROR, "Error: Could not create CVPixelBuffer: %d\n", status);
1992 return AVERROR_EXTERNAL;
1999 static int create_encoder_dict_h264(const AVFrame *frame,
2000 CFDictionaryRef* dict_out)
2002 CFDictionaryRef dict = NULL;
2003 if (frame->pict_type == AV_PICTURE_TYPE_I) {
2004 const void *keys[] = { kVTEncodeFrameOptionKey_ForceKeyFrame };
2005 const void *vals[] = { kCFBooleanTrue };
2007 dict = CFDictionaryCreate(NULL, keys, vals, 1, NULL, NULL);
2008 if(!dict) return AVERROR(ENOMEM);
2015 static int vtenc_send_frame(AVCodecContext *avctx,
2016 VTEncContext *vtctx,
2017 const AVFrame *frame)
2020 CFDictionaryRef frame_dict;
2021 CVPixelBufferRef cv_img = NULL;
2022 AVFrameSideData *side_data = NULL;
2023 ExtraSEI *sei = NULL;
2024 int status = create_cv_pixel_buffer(avctx, frame, &cv_img);
2026 if (status) return status;
2028 status = create_encoder_dict_h264(frame, &frame_dict);
2034 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
2035 if (vtctx->a53_cc && side_data && side_data->size) {
2036 sei = av_mallocz(sizeof(*sei));
2038 av_log(avctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
2040 int ret = ff_alloc_a53_sei(frame, 0, &sei->data, &sei->size);
2042 av_log(avctx, AV_LOG_ERROR, "Not enough memory for closed captions, skipping\n");
2049 time = CMTimeMake(frame->pts * avctx->time_base.num, avctx->time_base.den);
2050 status = VTCompressionSessionEncodeFrame(
2060 if (frame_dict) CFRelease(frame_dict);
2064 av_log(avctx, AV_LOG_ERROR, "Error: cannot encode frame: %d\n", status);
2065 return AVERROR_EXTERNAL;
2071 static av_cold int vtenc_frame(
2072 AVCodecContext *avctx,
2074 const AVFrame *frame,
2077 VTEncContext *vtctx = avctx->priv_data;
2080 CMSampleBufferRef buf = NULL;
2081 ExtraSEI *sei = NULL;
2084 status = vtenc_send_frame(avctx, vtctx, frame);
2087 status = AVERROR_EXTERNAL;
2091 if (vtctx->frame_ct_in == 0) {
2092 vtctx->first_pts = frame->pts;
2093 } else if(vtctx->frame_ct_in == 1 && vtctx->has_b_frames) {
2094 vtctx->dts_delta = frame->pts - vtctx->first_pts;
2097 vtctx->frame_ct_in++;
2098 } else if(!vtctx->flushing) {
2099 vtctx->flushing = true;
2101 status = VTCompressionSessionCompleteFrames(vtctx->session,
2105 av_log(avctx, AV_LOG_ERROR, "Error flushing frames: %d\n", status);
2106 status = AVERROR_EXTERNAL;
2112 get_frame = vtctx->dts_delta >= 0 || !frame;
2118 status = vtenc_q_pop(vtctx, !frame, &buf, &sei);
2119 if (status) goto end_nopkt;
2120 if (!buf) goto end_nopkt;
2122 status = vtenc_cm_to_avpacket(avctx, buf, pkt, sei);
2124 if (sei->data) av_free(sei->data);
2128 if (status) goto end_nopkt;
2134 av_packet_unref(pkt);
2138 static int vtenc_populate_extradata(AVCodecContext *avctx,
2139 CMVideoCodecType codec_type,
2140 CFStringRef profile_level,
2141 CFNumberRef gamma_level,
2142 CFDictionaryRef enc_info,
2143 CFDictionaryRef pixel_buffer_info)
2145 VTEncContext *vtctx = avctx->priv_data;
2146 AVFrame *frame = av_frame_alloc();
2147 int y_size = avctx->width * avctx->height;
2148 int chroma_size = (avctx->width / 2) * (avctx->height / 2);
2149 CMSampleBufferRef buf = NULL;
2153 return AVERROR(ENOMEM);
2155 frame->buf[0] = av_buffer_alloc(y_size + 2 * chroma_size);
2158 status = AVERROR(ENOMEM);
2162 status = vtenc_create_encoder(avctx,
2172 frame->data[0] = frame->buf[0]->data;
2173 memset(frame->data[0], 0, y_size);
2175 frame->data[1] = frame->buf[0]->data + y_size;
2176 memset(frame->data[1], 128, chroma_size);
2179 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
2180 frame->data[2] = frame->buf[0]->data + y_size + chroma_size;
2181 memset(frame->data[2], 128, chroma_size);
2184 frame->linesize[0] = avctx->width;
2186 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
2187 frame->linesize[1] =
2188 frame->linesize[2] = (avctx->width + 1) / 2;
2190 frame->linesize[1] = (avctx->width + 1) / 2;
2193 frame->format = avctx->pix_fmt;
2194 frame->width = avctx->width;
2195 frame->height = avctx->height;
2196 av_frame_set_colorspace(frame, avctx->colorspace);
2197 av_frame_set_color_range(frame, avctx->color_range);
2198 frame->color_trc = avctx->color_trc;
2199 frame->color_primaries = avctx->color_primaries;
2202 status = vtenc_send_frame(avctx, vtctx, frame);
2204 av_log(avctx, AV_LOG_ERROR, "Error sending frame: %d\n", status);
2208 //Populates extradata - output frames are flushed and param sets are available.
2209 status = VTCompressionSessionCompleteFrames(vtctx->session,
2215 status = vtenc_q_pop(vtctx, 0, &buf, NULL);
2217 av_log(avctx, AV_LOG_ERROR, "popping: %d\n", status);
2227 CFRelease(vtctx->session);
2229 vtctx->session = NULL;
2230 vtctx->frame_ct_out = 0;
2232 av_frame_unref(frame);
2233 av_frame_free(&frame);
2235 av_assert0(status != 0 || (avctx->extradata && avctx->extradata_size > 0));
2240 static av_cold int vtenc_close(AVCodecContext *avctx)
2242 VTEncContext *vtctx = avctx->priv_data;
2244 if(!vtctx->session) return 0;
2246 VTCompressionSessionCompleteFrames(vtctx->session,
2248 clear_frame_queue(vtctx);
2249 pthread_cond_destroy(&vtctx->cv_sample_sent);
2250 pthread_mutex_destroy(&vtctx->lock);
2251 CFRelease(vtctx->session);
2252 vtctx->session = NULL;
2254 if (vtctx->color_primaries) {
2255 CFRelease(vtctx->color_primaries);
2256 vtctx->color_primaries = NULL;
2259 if (vtctx->transfer_function) {
2260 CFRelease(vtctx->transfer_function);
2261 vtctx->transfer_function = NULL;
2264 if (vtctx->ycbcr_matrix) {
2265 CFRelease(vtctx->ycbcr_matrix);
2266 vtctx->ycbcr_matrix = NULL;
2272 static const enum AVPixelFormat pix_fmts[] = {
2273 AV_PIX_FMT_VIDEOTOOLBOX,
2279 #define OFFSET(x) offsetof(VTEncContext, x)
2280 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
2281 static const AVOption options[] = {
2282 { "profile", "Profile", OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = H264_PROF_AUTO }, H264_PROF_AUTO, H264_PROF_COUNT, VE, "profile" },
2283 { "baseline", "Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_BASELINE }, INT_MIN, INT_MAX, VE, "profile" },
2284 { "main", "Main Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
2285 { "high", "High Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_HIGH }, INT_MIN, INT_MAX, VE, "profile" },
2287 { "level", "Level", OFFSET(level), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, VE, "level" },
2288 { "1.3", "Level 1.3, only available with Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX, VE, "level" },
2289 { "3.0", "Level 3.0", 0, AV_OPT_TYPE_CONST, { .i64 = 30 }, INT_MIN, INT_MAX, VE, "level" },
2290 { "3.1", "Level 3.1", 0, AV_OPT_TYPE_CONST, { .i64 = 31 }, INT_MIN, INT_MAX, VE, "level" },
2291 { "3.2", "Level 3.2", 0, AV_OPT_TYPE_CONST, { .i64 = 32 }, INT_MIN, INT_MAX, VE, "level" },
2292 { "4.0", "Level 4.0", 0, AV_OPT_TYPE_CONST, { .i64 = 40 }, INT_MIN, INT_MAX, VE, "level" },
2293 { "4.1", "Level 4.1", 0, AV_OPT_TYPE_CONST, { .i64 = 41 }, INT_MIN, INT_MAX, VE, "level" },
2294 { "4.2", "Level 4.2", 0, AV_OPT_TYPE_CONST, { .i64 = 42 }, INT_MIN, INT_MAX, VE, "level" },
2295 { "5.0", "Level 5.0", 0, AV_OPT_TYPE_CONST, { .i64 = 50 }, INT_MIN, INT_MAX, VE, "level" },
2296 { "5.1", "Level 5.1", 0, AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX, VE, "level" },
2297 { "5.2", "Level 5.2", 0, AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX, VE, "level" },
2299 { "allow_sw", "Allow software encoding", OFFSET(allow_sw), AV_OPT_TYPE_BOOL,
2300 { .i64 = 0 }, 0, 1, VE },
2302 { "coder", "Entropy coding", OFFSET(entropy), AV_OPT_TYPE_INT, { .i64 = VT_ENTROPY_NOT_SET }, VT_ENTROPY_NOT_SET, VT_CABAC, VE, "coder" },
2303 { "cavlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC }, INT_MIN, INT_MAX, VE, "coder" },
2304 { "vlc", "CAVLC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CAVLC }, INT_MIN, INT_MAX, VE, "coder" },
2305 { "cabac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC }, INT_MIN, INT_MAX, VE, "coder" },
2306 { "ac", "CABAC entropy coding", 0, AV_OPT_TYPE_CONST, { .i64 = VT_CABAC }, INT_MIN, INT_MAX, VE, "coder" },
2308 { "realtime", "Hint that encoding should happen in real-time if not faster (e.g. capturing from camera).",
2309 OFFSET(realtime), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
2311 { "frames_before", "Other frames will come before the frames in this session. This helps smooth concatenation issues.",
2312 OFFSET(frames_before), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
2313 { "frames_after", "Other frames will come after the frames in this session. This helps smooth concatenation issues.",
2314 OFFSET(frames_after), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
2316 { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VE },
2321 static const AVClass h264_videotoolbox_class = {
2322 .class_name = "h264_videotoolbox",
2323 .item_name = av_default_item_name,
2325 .version = LIBAVUTIL_VERSION_INT,
2328 AVCodec ff_h264_videotoolbox_encoder = {
2329 .name = "h264_videotoolbox",
2330 .long_name = NULL_IF_CONFIG_SMALL("VideoToolbox H.264 Encoder"),
2331 .type = AVMEDIA_TYPE_VIDEO,
2332 .id = AV_CODEC_ID_H264,
2333 .priv_data_size = sizeof(VTEncContext),
2334 .pix_fmts = pix_fmts,
2336 .encode2 = vtenc_frame,
2337 .close = vtenc_close,
2338 .capabilities = AV_CODEC_CAP_DELAY,
2339 .priv_class = &h264_videotoolbox_class,
2340 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2341 FF_CODEC_CAP_INIT_CLEANUP,