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"
35 typedef enum VT_H264Profile {
43 static const uint8_t start_code[] = { 0, 0, 0, 1 };
45 typedef struct BufNode {
46 CMSampleBufferRef cm_buffer;
51 typedef struct VTEncContext {
53 VTCompressionSessionRef session;
56 pthread_cond_t cv_sample_sent;
74 bool warned_color_range;
77 static void set_async_error(VTEncContext *vtctx, int err)
81 pthread_mutex_lock(&vtctx->lock);
83 vtctx->async_error = err;
86 vtctx->q_head = vtctx->q_tail = NULL;
89 BufNode *next = info->next;
90 CFRelease(info->cm_buffer);
95 pthread_mutex_unlock(&vtctx->lock);
98 static int vtenc_q_pop(VTEncContext *vtctx, bool wait, CMSampleBufferRef *buf)
102 pthread_mutex_lock(&vtctx->lock);
104 if (vtctx->async_error) {
105 pthread_mutex_unlock(&vtctx->lock);
106 return vtctx->async_error;
109 if (vtctx->flushing && vtctx->frame_ct_in == vtctx->frame_ct_out) {
112 pthread_mutex_unlock(&vtctx->lock);
116 while (!vtctx->q_head && !vtctx->async_error && wait) {
117 pthread_cond_wait(&vtctx->cv_sample_sent, &vtctx->lock);
120 if (!vtctx->q_head) {
121 pthread_mutex_unlock(&vtctx->lock);
126 info = vtctx->q_head;
127 vtctx->q_head = vtctx->q_head->next;
128 if (!vtctx->q_head) {
129 vtctx->q_tail = NULL;
132 pthread_mutex_unlock(&vtctx->lock);
134 *buf = info->cm_buffer;
137 vtctx->frame_ct_out++;
142 static void vtenc_q_push(VTEncContext *vtctx, CMSampleBufferRef buffer)
144 BufNode *info = av_malloc(sizeof(BufNode));
146 set_async_error(vtctx, AVERROR(ENOMEM));
151 info->cm_buffer = buffer;
154 pthread_mutex_lock(&vtctx->lock);
155 pthread_cond_signal(&vtctx->cv_sample_sent);
157 if (!vtctx->q_head) {
158 vtctx->q_head = info;
160 vtctx->q_tail->next = info;
163 vtctx->q_tail = info;
165 pthread_mutex_unlock(&vtctx->lock);
168 static CMVideoCodecType get_cm_codec_type(enum AVCodecID id)
171 case AV_CODEC_ID_H264: return kCMVideoCodecType_H264;
176 static void vtenc_free_block(void *opaque, uint8_t *data)
178 CMBlockBufferRef block = opaque;
183 * Get the parameter sets from a CMSampleBufferRef.
184 * @param dst If *dst isn't NULL, the parameters are copied into existing
185 * memory. *dst_size must be set accordingly when *dst != NULL.
186 * If *dst is NULL, it will be allocated.
187 * In all cases, *dst_size is set to the number of bytes used starting
190 static int get_params_size(
191 AVCodecContext *avctx,
192 CMVideoFormatDescriptionRef vid_fmt,
195 size_t total_size = 0;
199 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
206 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set count: %d\n", status);
207 return AVERROR_EXTERNAL;
210 for(i = 0; i < ps_count; i++){
213 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
220 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set size for index %zd: %d\n", i, status);
221 return AVERROR_EXTERNAL;
224 total_size += ps_size + sizeof(start_code);
231 static int copy_param_sets(
232 AVCodecContext *avctx,
233 CMVideoFormatDescriptionRef vid_fmt,
242 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
249 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set count for copying: %d\n", status);
250 return AVERROR_EXTERNAL;
253 for (i = 0; i < ps_count; i++) {
258 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
265 av_log(avctx, AV_LOG_ERROR, "Error getting parameter set data for index %zd: %d\n", i, status);
266 return AVERROR_EXTERNAL;
269 next_offset = offset + sizeof(start_code) + ps_size;
270 if (dst_size < next_offset) {
271 av_log(avctx, AV_LOG_ERROR, "Error: buffer too small for parameter sets.\n");
272 return AVERROR_BUFFER_TOO_SMALL;
275 memcpy(dst + offset, start_code, sizeof(start_code));
276 offset += sizeof(start_code);
278 memcpy(dst + offset, ps, ps_size);
279 offset = next_offset;
285 static int set_extradata(AVCodecContext *avctx, CMSampleBufferRef sample_buffer)
287 CMVideoFormatDescriptionRef vid_fmt;
291 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
293 av_log(avctx, AV_LOG_ERROR, "No video format.\n");
294 return AVERROR_EXTERNAL;
297 status = get_params_size(avctx, vid_fmt, &total_size);
299 av_log(avctx, AV_LOG_ERROR, "Could not get parameter sets.\n");
303 avctx->extradata = av_malloc(total_size);
304 if (!avctx->extradata) {
305 return AVERROR(ENOMEM);
307 avctx->extradata_size = total_size;
309 status = copy_param_sets(avctx, vid_fmt, avctx->extradata, total_size);
312 av_log(avctx, AV_LOG_ERROR, "Could not copy param sets.\n");
319 static void vtenc_output_callback(
321 void *sourceFrameCtx,
323 VTEncodeInfoFlags flags,
324 CMSampleBufferRef sample_buffer)
326 AVCodecContext *avctx = ctx;
327 VTEncContext *vtctx = avctx->priv_data;
329 if (vtctx->async_error) {
330 if(sample_buffer) CFRelease(sample_buffer);
334 if (status || !sample_buffer) {
335 av_log(avctx, AV_LOG_ERROR, "Error encoding frame: %d\n", (int)status);
336 set_async_error(vtctx, AVERROR_EXTERNAL);
340 if (!avctx->extradata && (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
341 int set_status = set_extradata(avctx, sample_buffer);
343 set_async_error(vtctx, set_status);
348 vtenc_q_push(vtctx, sample_buffer);
351 static int get_length_code_size(
352 AVCodecContext *avctx,
353 CMSampleBufferRef sample_buffer,
356 CMVideoFormatDescriptionRef vid_fmt;
360 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
362 av_log(avctx, AV_LOG_ERROR, "Error getting buffer format description.\n");
363 return AVERROR_EXTERNAL;
366 status = CMVideoFormatDescriptionGetH264ParameterSetAtIndex(vid_fmt,
373 av_log(avctx, AV_LOG_ERROR, "Error getting length code size: %d\n", status);
374 return AVERROR_EXTERNAL;
382 * Returns true on success.
384 * If profile_level_val is NULL and this method returns true, don't specify the
385 * profile/level to the encoder.
387 static bool get_vt_profile_level(AVCodecContext *avctx,
388 CFStringRef *profile_level_val)
390 VTEncContext *vtctx = avctx->priv_data;
391 int64_t profile = vtctx->profile;
393 if (profile == H264_PROF_AUTO && vtctx->level) {
394 //Need to pick a profile if level is not auto-selected.
395 profile = vtctx->has_b_frames ? H264_PROF_MAIN : H264_PROF_BASELINE;
398 *profile_level_val = NULL;
404 case H264_PROF_BASELINE:
405 switch (vtctx->level) {
406 case 0: *profile_level_val = kVTProfileLevel_H264_Baseline_AutoLevel; break;
407 case 13: *profile_level_val = kVTProfileLevel_H264_Baseline_1_3; break;
408 case 30: *profile_level_val = kVTProfileLevel_H264_Baseline_3_0; break;
409 case 31: *profile_level_val = kVTProfileLevel_H264_Baseline_3_1; break;
410 case 32: *profile_level_val = kVTProfileLevel_H264_Baseline_3_2; break;
411 case 40: *profile_level_val = kVTProfileLevel_H264_Baseline_4_0; break;
412 case 41: *profile_level_val = kVTProfileLevel_H264_Baseline_4_1; break;
413 case 42: *profile_level_val = kVTProfileLevel_H264_Baseline_4_2; break;
414 case 50: *profile_level_val = kVTProfileLevel_H264_Baseline_5_0; break;
415 case 51: *profile_level_val = kVTProfileLevel_H264_Baseline_5_1; break;
416 case 52: *profile_level_val = kVTProfileLevel_H264_Baseline_5_2; break;
421 switch (vtctx->level) {
422 case 0: *profile_level_val = kVTProfileLevel_H264_Main_AutoLevel; break;
423 case 30: *profile_level_val = kVTProfileLevel_H264_Main_3_0; break;
424 case 31: *profile_level_val = kVTProfileLevel_H264_Main_3_1; break;
425 case 32: *profile_level_val = kVTProfileLevel_H264_Main_3_2; break;
426 case 40: *profile_level_val = kVTProfileLevel_H264_Main_4_0; break;
427 case 41: *profile_level_val = kVTProfileLevel_H264_Main_4_1; break;
428 case 42: *profile_level_val = kVTProfileLevel_H264_Main_4_2; break;
429 case 50: *profile_level_val = kVTProfileLevel_H264_Main_5_0; break;
430 case 51: *profile_level_val = kVTProfileLevel_H264_Main_5_1; break;
431 case 52: *profile_level_val = kVTProfileLevel_H264_Main_5_2; break;
436 switch (vtctx->level) {
437 case 0: *profile_level_val = kVTProfileLevel_H264_High_AutoLevel; break;
438 case 30: *profile_level_val = kVTProfileLevel_H264_High_3_0; break;
439 case 31: *profile_level_val = kVTProfileLevel_H264_High_3_1; break;
440 case 32: *profile_level_val = kVTProfileLevel_H264_High_3_2; break;
441 case 40: *profile_level_val = kVTProfileLevel_H264_High_4_0; break;
442 case 41: *profile_level_val = kVTProfileLevel_H264_High_4_1; break;
443 case 42: *profile_level_val = kVTProfileLevel_H264_High_4_2; break;
444 case 50: *profile_level_val = kVTProfileLevel_H264_High_5_0; break;
445 case 51: *profile_level_val = kVTProfileLevel_H264_High_5_1; break;
446 case 52: *profile_level_val = kVTProfileLevel_H264_High_5_2; break;
451 if (!*profile_level_val) {
452 av_log(avctx, AV_LOG_ERROR, "Invalid Profile/Level.\n");
459 static av_cold int vtenc_init(AVCodecContext *avctx)
461 CFMutableDictionaryRef enc_info;
462 CMVideoCodecType codec_type;
463 VTEncContext *vtctx = avctx->priv_data;
464 CFStringRef profile_level;
465 SInt32 bit_rate = avctx->bit_rate;
466 CFNumberRef bit_rate_num;
469 codec_type = get_cm_codec_type(avctx->codec_id);
471 av_log(avctx, AV_LOG_ERROR, "Error: no mapping for AVCodecID %d\n", avctx->codec_id);
472 return AVERROR(EINVAL);
475 vtctx->has_b_frames = avctx->has_b_frames || avctx->max_b_frames > 0;
476 if(vtctx->has_b_frames && vtctx->profile == H264_PROF_BASELINE){
477 av_log(avctx, AV_LOG_WARNING, "Cannot use B-frames with baseline profile. Output will not contain B-frames.\n");
478 vtctx->has_b_frames = false;
481 if (!get_vt_profile_level(avctx, &profile_level)) return AVERROR(EINVAL);
483 vtctx->session = NULL;
485 enc_info = CFDictionaryCreateMutable(
488 &kCFCopyStringDictionaryKeyCallBacks,
489 &kCFTypeDictionaryValueCallBacks
492 if (!enc_info) return AVERROR(ENOMEM);
494 #if !TARGET_OS_IPHONE
495 CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder, kCFBooleanTrue);
496 CFDictionarySetValue(enc_info, kVTVideoEncoderSpecification_EnableHardwareAcceleratedVideoEncoder, kCFBooleanTrue);
499 status = VTCompressionSessionCreate(
507 vtenc_output_callback,
512 #if !TARGET_OS_IPHONE
513 if (status != 0 || !vtctx->session) {
514 CFDictionaryRemoveValue(enc_info, kVTVideoEncoderSpecification_RequireHardwareAcceleratedVideoEncoder);
516 status = VTCompressionSessionCreate(
524 vtenc_output_callback,
533 if (status || !vtctx->session) {
534 av_log(avctx, AV_LOG_ERROR, "Error: cannot create compression session: %d\n", status);
535 return AVERROR_EXTERNAL;
538 bit_rate_num = CFNumberCreate(kCFAllocatorDefault,
541 if (!bit_rate_num) return AVERROR(ENOMEM);
543 status = VTSessionSetProperty(vtctx->session,
544 kVTCompressionPropertyKey_AverageBitRate,
546 CFRelease(bit_rate_num);
549 av_log(avctx, AV_LOG_ERROR, "Error setting bitrate property: %d\n", status);
550 return AVERROR_EXTERNAL;
554 status = VTSessionSetProperty(vtctx->session,
555 kVTCompressionPropertyKey_ProfileLevel,
558 av_log(avctx, AV_LOG_ERROR, "Error setting profile/level property: %d\n", status);
559 return AVERROR_EXTERNAL;
563 if (avctx->gop_size > 0) {
564 CFNumberRef interval = CFNumberCreate(kCFAllocatorDefault,
567 status = VTSessionSetProperty(vtctx->session,
568 kVTCompressionPropertyKey_MaxKeyFrameInterval,
572 av_log(avctx, AV_LOG_ERROR, "Error setting 'max key-frame interval' property: %d\n", status);
573 return AVERROR_EXTERNAL;
577 if (!vtctx->has_b_frames) {
578 status = VTSessionSetProperty(vtctx->session,
579 kVTCompressionPropertyKey_AllowFrameReordering,
583 av_log(avctx, AV_LOG_ERROR, "Error setting 'allow frame reordering' property: %d\n", status);
584 return AVERROR_EXTERNAL;
588 status = VTCompressionSessionPrepareToEncodeFrames(vtctx->session);
590 av_log(avctx, AV_LOG_ERROR, "Error: cannot prepare encoder: %d\n", status);
591 return AVERROR_EXTERNAL;
594 pthread_mutex_init(&vtctx->lock, NULL);
595 pthread_cond_init(&vtctx->cv_sample_sent, NULL);
596 vtctx->dts_delta = vtctx->has_b_frames ? -1 : 0;
601 static void vtenc_get_frame_info(CMSampleBufferRef buffer, bool *is_key_frame)
603 CFArrayRef attachments;
604 CFDictionaryRef attachment;
605 CFBooleanRef not_sync;
608 attachments = CMSampleBufferGetSampleAttachmentsArray(buffer, false);
609 len = !attachments ? 0 : CFArrayGetCount(attachments);
612 *is_key_frame = true;
616 attachment = CFArrayGetValueAtIndex(attachments, 0);
618 if (CFDictionaryGetValueIfPresent(attachment,
619 kCMSampleAttachmentKey_NotSync,
620 (const void **)¬_sync))
622 *is_key_frame = !CFBooleanGetValue(not_sync);
624 *is_key_frame = true;
629 * Replaces length codes with H.264 Annex B start codes.
630 * length_code_size must equal sizeof(start_code).
631 * On failure, the contents of data may have been modified.
633 * @param length_code_size Byte length of each length code
634 * @param data Call with NAL units prefixed with length codes.
635 * On success, the length codes are replace with
637 * @param size Length of data, excluding any padding.
638 * @return 0 on success
639 * AVERROR_BUFFER_TOO_SMALL if length code size is smaller
640 * than a start code or if a length_code in data specifies
641 * data beyond the end of its buffer.
643 static int replace_length_codes(size_t length_code_size,
647 size_t remaining_size = size;
649 if (length_code_size != sizeof(start_code)) {
650 av_log(NULL, AV_LOG_ERROR, "Start code size and length code size not equal.\n");
651 return AVERROR_BUFFER_TOO_SMALL;
654 while (remaining_size > 0) {
658 for (i = 0; i < length_code_size; i++) {
663 if (remaining_size < box_len + sizeof(start_code)) {
664 av_log(NULL, AV_LOG_ERROR, "Length is out of range.\n");
665 AVERROR_BUFFER_TOO_SMALL;
668 memcpy(data, start_code, sizeof(start_code));
669 data += box_len + sizeof(start_code);
670 remaining_size -= box_len + sizeof(start_code);
677 * Copies NAL units and replaces length codes with
678 * H.264 Annex B start codes. On failure, the contents of
679 * dst_data may have been modified.
681 * @param length_code_size Byte length of each length code
682 * @param src_data NAL units prefixed with length codes.
683 * @param src_size Length of buffer, excluding any padding.
684 * @param dst_data Must be zeroed before calling this function.
685 * Contains the copied NAL units prefixed with
686 * start codes when the function returns
688 * @param dst_size Length of dst_data
689 * @return 0 on success
690 * AVERROR_INVALIDDATA if length_code_size is invalid
691 * AVERROR_BUFFER_TOO_SMALL if dst_data is too small
692 * or if a length_code in src_data specifies data beyond
693 * the end of its buffer.
695 static int copy_replace_length_codes(
696 size_t length_code_size,
697 const uint8_t *src_data,
702 size_t remaining_src_size = src_size;
703 size_t remaining_dst_size = dst_size;
705 if (length_code_size > 4) {
706 return AVERROR_INVALIDDATA;
709 while (remaining_src_size > 0) {
716 const uint8_t *src_box;
718 for (i = 0; i < length_code_size; i++) {
720 box_len |= src_data[i];
723 curr_src_len = box_len + length_code_size;
724 curr_dst_len = box_len + sizeof(start_code);
726 if (remaining_src_size < curr_src_len) {
727 return AVERROR_BUFFER_TOO_SMALL;
730 if (remaining_dst_size < curr_dst_len) {
731 return AVERROR_BUFFER_TOO_SMALL;
734 dst_box = dst_data + sizeof(start_code);
735 src_box = src_data + length_code_size;
737 memcpy(dst_data, start_code, sizeof(start_code));
738 memcpy(dst_box, src_box, box_len);
740 src_data += curr_src_len;
741 dst_data += curr_dst_len;
743 remaining_src_size -= curr_src_len;
744 remaining_dst_size -= curr_dst_len;
750 static int vtenc_cm_to_avpacket(
751 AVCodecContext *avctx,
752 CMSampleBufferRef sample_buffer,
755 VTEncContext *vtctx = avctx->priv_data;
761 size_t length_code_size;
762 size_t header_size = 0;
765 int64_t time_base_num;
769 CMBlockBufferRef block;
770 CMVideoFormatDescriptionRef vid_fmt;
773 vtenc_get_frame_info(sample_buffer, &is_key_frame);
774 status = get_length_code_size(avctx, sample_buffer, &length_code_size);
775 if (status) return status;
777 add_header = is_key_frame && !(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER);
780 vid_fmt = CMSampleBufferGetFormatDescription(sample_buffer);
782 av_log(avctx, AV_LOG_ERROR, "Cannot get format description.\n");
785 int status = get_params_size(avctx, vid_fmt, &header_size);
786 if (status) return status;
789 block = CMSampleBufferGetDataBuffer(sample_buffer);
791 av_log(avctx, AV_LOG_ERROR, "Could not get block buffer from sample buffer.\n");
792 return AVERROR_EXTERNAL;
796 status = CMBlockBufferGetDataPointer(block, 0, &in_buf_size, NULL, &buf_data);
798 av_log(avctx, AV_LOG_ERROR, "Error: cannot get data pointer: %d\n", status);
799 return AVERROR_EXTERNAL;
802 size_t out_buf_size = header_size + in_buf_size;
803 bool can_reuse_cmbuffer = !add_header &&
805 length_code_size == sizeof(start_code);
809 if (can_reuse_cmbuffer) {
810 AVBufferRef* buf_ref = av_buffer_create(
818 if (!buf_ref) return AVERROR(ENOMEM);
823 pkt->data = buf_data;
824 pkt->size = in_buf_size;
826 status = replace_length_codes(length_code_size, pkt->data, pkt->size);
828 av_log(avctx, AV_LOG_ERROR, "Error replacing length codes: %d\n", status);
833 status = av_new_packet(pkt, out_buf_size);
834 if(status) return status;
837 if (pkt->size < out_buf_size) {
838 av_log(avctx, AV_LOG_ERROR, "Error: packet's buffer is too small.\n");
839 return AVERROR_BUFFER_TOO_SMALL;
843 status = copy_param_sets(avctx, vid_fmt, pkt->data, out_buf_size);
844 if(status) return status;
847 status = copy_replace_length_codes(
851 pkt->data + header_size,
852 pkt->size - header_size
856 av_log(avctx, AV_LOG_ERROR, "Error copying packet data: %d", status);
862 pkt->flags |= AV_PKT_FLAG_KEY;
865 pts = CMSampleBufferGetPresentationTimeStamp(sample_buffer);
866 dts = CMSampleBufferGetDecodeTimeStamp (sample_buffer);
868 dts_delta = vtctx->dts_delta >= 0 ? vtctx->dts_delta : 0;
869 time_base_num = avctx->time_base.num;
870 pkt->pts = pts.value / time_base_num;
871 pkt->dts = dts.value / time_base_num - dts_delta;
877 * contiguous_buf_size is 0 if not contiguous, and the size of the buffer
878 * containing all planes if so.
880 static int get_cv_pixel_info(
881 AVCodecContext *avctx,
882 const AVFrame *frame,
888 size_t *contiguous_buf_size)
890 VTEncContext *vtctx = avctx->priv_data;
891 int av_format = frame->format;
892 int av_color_range = av_frame_get_color_range(frame);
896 case AV_PIX_FMT_NV12:
897 switch (av_color_range) {
898 case AVCOL_RANGE_MPEG:
899 *color = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
902 case AVCOL_RANGE_JPEG:
903 *color = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
907 if (!vtctx->warned_color_range) {
908 vtctx->warned_color_range = true;
909 av_log(avctx, AV_LOG_WARNING, "Color range not set for NV12. Using MPEG range.\n");
911 *color = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange;
916 widths [0] = avctx->width;
917 heights[0] = avctx->height;
918 strides[0] = frame ? frame->linesize[0] : avctx->width;
920 widths [1] = (avctx->width + 1) / 2;
921 heights[1] = (avctx->height + 1) / 2;
922 strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) & -2;
925 case AV_PIX_FMT_YUV420P:
926 switch (av_color_range) {
927 case AVCOL_RANGE_MPEG:
928 *color = kCVPixelFormatType_420YpCbCr8Planar;
931 case AVCOL_RANGE_JPEG:
932 *color = kCVPixelFormatType_420YpCbCr8PlanarFullRange;
936 if (!vtctx->warned_color_range) {
937 vtctx->warned_color_range = true;
938 av_log(avctx, AV_LOG_WARNING, "Color range not set for YUV 4:2:0. Using MPEG range.\n");
940 *color = kCVPixelFormatType_420YpCbCr8Planar;
945 widths [0] = avctx->width;
946 heights[0] = avctx->height;
947 strides[0] = frame ? frame->linesize[0] : avctx->width;
949 widths [1] = (avctx->width + 1) / 2;
950 heights[1] = (avctx->height + 1) / 2;
951 strides[1] = frame ? frame->linesize[1] : (avctx->width + 1) / 2;
953 widths [2] = (avctx->width + 1) / 2;
954 heights[2] = (avctx->height + 1) / 2;
955 strides[2] = frame ? frame->linesize[2] : (avctx->width + 1) / 2;
958 default: return AVERROR(EINVAL);
961 *contiguous_buf_size = 0;
962 for (i = 0; i < *plane_count; i++) {
963 if (i < *plane_count - 1 &&
964 frame->data[i] + strides[i] * heights[i] != frame->data[i + 1]) {
965 *contiguous_buf_size = 0;
969 *contiguous_buf_size += strides[i] * heights[i];
975 #if !TARGET_OS_IPHONE
976 //Not used on iOS - frame is always copied.
977 static void free_avframe(
982 const void *plane_addresses[])
984 AVFrame *frame = release_ctx;
985 av_frame_free(&frame);
988 //Not used on OSX - frame is never copied.
989 static int copy_avframe_to_pixel_buffer(AVCodecContext *avctx,
990 const AVFrame *frame,
991 CVPixelBufferRef cv_img,
992 const size_t *plane_strides,
993 const size_t *plane_rows)
1005 status = CVPixelBufferLockBaseAddress(cv_img, 0);
1010 "Error: Could not lock base address of CVPixelBuffer: %d.\n",
1015 if (CVPixelBufferIsPlanar(cv_img)) {
1016 plane_count = CVPixelBufferGetPlaneCount(cv_img);
1017 for (i = 0; frame->data[i]; i++) {
1018 if (i == plane_count) {
1019 CVPixelBufferUnlockBaseAddress(cv_img, 0);
1022 "Error: different number of planes in AVFrame and CVPixelBuffer.\n"
1025 return AVERROR_EXTERNAL;
1028 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddressOfPlane(cv_img, i);
1029 src_addr = (uint8_t*)frame->data[i];
1030 dst_stride = CVPixelBufferGetBytesPerRowOfPlane(cv_img, i);
1031 src_stride = plane_strides[i];
1032 rows = plane_rows[i];
1034 if (dst_stride == src_stride) {
1035 memcpy(dst_addr, src_addr, src_stride * rows);
1037 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
1039 for (j = 0; j < rows; j++) {
1040 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
1045 if (frame->data[1]) {
1046 CVPixelBufferUnlockBaseAddress(cv_img, 0);
1049 "Error: different number of planes in AVFrame and non-planar CVPixelBuffer.\n"
1052 return AVERROR_EXTERNAL;
1055 dst_addr = (uint8_t*)CVPixelBufferGetBaseAddress(cv_img);
1056 src_addr = (uint8_t*)frame->data[0];
1057 dst_stride = CVPixelBufferGetBytesPerRow(cv_img);
1058 src_stride = plane_strides[0];
1059 rows = plane_rows[0];
1061 if (dst_stride == src_stride) {
1062 memcpy(dst_addr, src_addr, src_stride * rows);
1064 copy_bytes = dst_stride < src_stride ? dst_stride : src_stride;
1066 for (j = 0; j < rows; j++) {
1067 memcpy(dst_addr + j * dst_stride, src_addr + j * src_stride, copy_bytes);
1072 status = CVPixelBufferUnlockBaseAddress(cv_img, 0);
1074 av_log(avctx, AV_LOG_ERROR, "Error: Could not unlock CVPixelBuffer base address: %d.\n", status);
1075 return AVERROR_EXTERNAL;
1080 #endif //!TARGET_OS_IPHONE
1082 static int create_cv_pixel_buffer(AVCodecContext *avctx,
1083 const AVFrame *frame,
1084 CVPixelBufferRef *cv_img)
1088 size_t widths [AV_NUM_DATA_POINTERS];
1089 size_t heights[AV_NUM_DATA_POINTERS];
1090 size_t strides[AV_NUM_DATA_POINTERS];
1092 size_t contiguous_buf_size;
1094 memset(widths, 0, sizeof(widths));
1095 memset(heights, 0, sizeof(heights));
1096 memset(strides, 0, sizeof(strides));
1098 status = get_cv_pixel_info(
1106 &contiguous_buf_size
1113 "Error: Cannot convert format %d color_range %d: %d\n",
1115 av_frame_get_color_range(frame),
1119 return AVERROR_EXTERNAL;
1122 #if TARGET_OS_IPHONE
1123 status = CVPixelBufferCreate(
1124 kCFAllocatorDefault,
1133 return AVERROR_EXTERNAL;
1136 status = copy_avframe_to_pixel_buffer(avctx, frame, *cv_img, strides, heights);
1143 AVFrame *enc_frame = av_frame_alloc();
1144 if (!enc_frame) return AVERROR(ENOMEM);
1146 status = av_frame_ref(enc_frame, frame);
1148 av_frame_free(&enc_frame);
1152 status = CVPixelBufferCreateWithPlanarBytes(
1153 kCFAllocatorDefault,
1158 contiguous_buf_size,
1160 (void **)enc_frame->data,
1171 av_log(avctx, AV_LOG_ERROR, "Error: Could not create CVPixelBuffer: %d\n", status);
1172 return AVERROR_EXTERNAL;
1179 static int vtenc_send_frame(AVCodecContext *avctx,
1180 VTEncContext *vtctx,
1181 const AVFrame *frame)
1184 CVPixelBufferRef cv_img = NULL;
1185 int status = create_cv_pixel_buffer(avctx, frame, &cv_img);
1187 if (status) return status;
1189 time = CMTimeMake(frame->pts * avctx->time_base.num, avctx->time_base.den);
1190 status = VTCompressionSessionEncodeFrame(
1203 av_log(avctx, AV_LOG_ERROR, "Error: cannot encode frame: %d\n", status);
1204 return AVERROR_EXTERNAL;
1210 static av_cold int vtenc_frame(
1211 AVCodecContext *avctx,
1213 const AVFrame *frame,
1216 VTEncContext *vtctx = avctx->priv_data;
1219 CMSampleBufferRef buf = NULL;
1222 status = vtenc_send_frame(avctx, vtctx, frame);
1225 status = AVERROR_EXTERNAL;
1229 if (vtctx->frame_ct_in == 0) {
1230 vtctx->first_pts = frame->pts;
1231 } else if(vtctx->frame_ct_in == 1 && vtctx->has_b_frames) {
1232 vtctx->dts_delta = frame->pts - vtctx->first_pts;
1235 vtctx->frame_ct_in++;
1236 } else if(!vtctx->flushing) {
1237 vtctx->flushing = true;
1239 status = VTCompressionSessionCompleteFrames(vtctx->session,
1243 av_log(avctx, AV_LOG_ERROR, "Error flushing frames: %d\n", status);
1244 status = AVERROR_EXTERNAL;
1250 get_frame = vtctx->dts_delta >= 0 || !frame;
1256 status = vtenc_q_pop(vtctx, !frame, &buf);
1257 if (status) goto end_nopkt;
1258 if (!buf) goto end_nopkt;
1260 status = vtenc_cm_to_avpacket(avctx, buf, pkt);
1262 if (status) goto end_nopkt;
1268 av_packet_unref(pkt);
1272 static av_cold int vtenc_close(AVCodecContext *avctx)
1274 VTEncContext *vtctx = avctx->priv_data;
1276 if(!vtctx->session) return 0;
1278 VTCompressionSessionInvalidate(vtctx->session);
1279 pthread_cond_destroy(&vtctx->cv_sample_sent);
1280 pthread_mutex_destroy(&vtctx->lock);
1281 CFRelease(vtctx->session);
1282 vtctx->session = NULL;
1287 static const enum AVPixelFormat pix_fmts[] = {
1289 #if !TARGET_OS_IPHONE
1295 #define OFFSET(x) offsetof(VTEncContext, x)
1296 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1297 static const AVOption options[] = {
1298 { "profile", "Profile", OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = H264_PROF_AUTO }, H264_PROF_AUTO, H264_PROF_COUNT, VE, "profile" },
1299 { "baseline", "Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_BASELINE }, INT_MIN, INT_MAX, VE, "profile" },
1300 { "main", "Main Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_MAIN }, INT_MIN, INT_MAX, VE, "profile" },
1301 { "high", "High Profile", 0, AV_OPT_TYPE_CONST, { .i64 = H264_PROF_HIGH }, INT_MIN, INT_MAX, VE, "profile" },
1303 { "level", "Level", OFFSET(level), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, VE, "level" },
1304 { "1.3", "Level 1.3, only available with Baseline Profile", 0, AV_OPT_TYPE_CONST, { .i64 = 13 }, INT_MIN, INT_MAX, VE, "level" },
1305 { "3.0", "Level 3.0", 0, AV_OPT_TYPE_CONST, { .i64 = 30 }, INT_MIN, INT_MAX, VE, "level" },
1306 { "3.1", "Level 3.1", 0, AV_OPT_TYPE_CONST, { .i64 = 31 }, INT_MIN, INT_MAX, VE, "level" },
1307 { "3.2", "Level 3.2", 0, AV_OPT_TYPE_CONST, { .i64 = 32 }, INT_MIN, INT_MAX, VE, "level" },
1308 { "4.0", "Level 4.0", 0, AV_OPT_TYPE_CONST, { .i64 = 40 }, INT_MIN, INT_MAX, VE, "level" },
1309 { "4.1", "Level 4.1", 0, AV_OPT_TYPE_CONST, { .i64 = 41 }, INT_MIN, INT_MAX, VE, "level" },
1310 { "4.2", "Level 4.2", 0, AV_OPT_TYPE_CONST, { .i64 = 42 }, INT_MIN, INT_MAX, VE, "level" },
1311 { "5.0", "Level 5.0", 0, AV_OPT_TYPE_CONST, { .i64 = 50 }, INT_MIN, INT_MAX, VE, "level" },
1312 { "5.1", "Level 5.1", 0, AV_OPT_TYPE_CONST, { .i64 = 51 }, INT_MIN, INT_MAX, VE, "level" },
1313 { "5.2", "Level 5.2", 0, AV_OPT_TYPE_CONST, { .i64 = 52 }, INT_MIN, INT_MAX, VE, "level" },
1318 static const AVClass h264_videotoolbox_class = {
1319 .class_name = "h264_videotoolbox",
1320 .item_name = av_default_item_name,
1322 .version = LIBAVUTIL_VERSION_INT,
1325 AVCodec ff_h264_videotoolbox_encoder = {
1326 .name = "h264_videotoolbox",
1327 .long_name = NULL_IF_CONFIG_SMALL("VideoToolbox H.264 Encoder"),
1328 .type = AVMEDIA_TYPE_VIDEO,
1329 .id = AV_CODEC_ID_H264,
1330 .priv_data_size = sizeof(VTEncContext),
1331 .pix_fmts = pix_fmts,
1333 .encode2 = vtenc_frame,
1334 .close = vtenc_close,
1335 .capabilities = AV_CODEC_CAP_DELAY,
1336 .priv_class = &h264_videotoolbox_class,
1337 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
1338 FF_CODEC_CAP_INIT_CLEANUP,