yading@10: /* yading@10: * VDA H264 HW acceleration. yading@10: * yading@10: * copyright (c) 2011 Sebastien Zwickert yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #include yading@10: #include yading@10: #include yading@10: yading@10: #include "vda.h" yading@10: #include "libavutil/avutil.h" yading@10: #include "h264.h" yading@10: yading@10: yading@10: /* Decoder callback that adds the vda frame to the queue in display order. */ yading@10: static void vda_decoder_callback (void *vda_hw_ctx, yading@10: CFDictionaryRef user_info, yading@10: OSStatus status, yading@10: uint32_t infoFlags, yading@10: CVImageBufferRef image_buffer) yading@10: { yading@10: struct vda_context *vda_ctx = vda_hw_ctx; yading@10: yading@10: if (!image_buffer) yading@10: return; yading@10: yading@10: if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer)) yading@10: return; yading@10: yading@10: vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer); yading@10: } yading@10: yading@10: static int vda_sync_decode(struct vda_context *vda_ctx) yading@10: { yading@10: OSStatus status; yading@10: CFDataRef coded_frame; yading@10: uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames yading@10: yading@10: coded_frame = CFDataCreate(kCFAllocatorDefault, yading@10: vda_ctx->priv_bitstream, yading@10: vda_ctx->priv_bitstream_size); yading@10: yading@10: status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL); yading@10: yading@10: if (kVDADecoderNoErr == status) yading@10: status = VDADecoderFlush(vda_ctx->decoder, flush_flags); yading@10: yading@10: CFRelease(coded_frame); yading@10: yading@10: return status; yading@10: } yading@10: yading@10: yading@10: static int vda_h264_start_frame(AVCodecContext *avctx, yading@10: av_unused const uint8_t *buffer, yading@10: av_unused uint32_t size) yading@10: { yading@10: struct vda_context *vda_ctx = avctx->hwaccel_context; yading@10: yading@10: if (!vda_ctx->decoder) yading@10: return -1; yading@10: yading@10: vda_ctx->priv_bitstream_size = 0; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int vda_h264_decode_slice(AVCodecContext *avctx, yading@10: const uint8_t *buffer, yading@10: uint32_t size) yading@10: { yading@10: struct vda_context *vda_ctx = avctx->hwaccel_context; yading@10: void *tmp; yading@10: yading@10: if (!vda_ctx->decoder) yading@10: return -1; yading@10: yading@10: tmp = av_fast_realloc(vda_ctx->priv_bitstream, yading@10: &vda_ctx->priv_allocated_size, yading@10: vda_ctx->priv_bitstream_size + size + 4); yading@10: if (!tmp) yading@10: return AVERROR(ENOMEM); yading@10: yading@10: vda_ctx->priv_bitstream = tmp; yading@10: yading@10: AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size); yading@10: memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size); yading@10: yading@10: vda_ctx->priv_bitstream_size += size + 4; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int vda_h264_end_frame(AVCodecContext *avctx) yading@10: { yading@10: H264Context *h = avctx->priv_data; yading@10: struct vda_context *vda_ctx = avctx->hwaccel_context; yading@10: AVFrame *frame = &h->cur_pic_ptr->f; yading@10: int status; yading@10: yading@10: if (!vda_ctx->decoder || !vda_ctx->priv_bitstream) yading@10: return -1; yading@10: yading@10: status = vda_sync_decode(vda_ctx); yading@10: frame->data[3] = (void*)vda_ctx->cv_buffer; yading@10: yading@10: if (status) yading@10: av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status); yading@10: yading@10: return status; yading@10: } yading@10: yading@10: int ff_vda_create_decoder(struct vda_context *vda_ctx, yading@10: uint8_t *extradata, yading@10: int extradata_size) yading@10: { yading@10: OSStatus status; yading@10: CFNumberRef height; yading@10: CFNumberRef width; yading@10: CFNumberRef format; yading@10: CFDataRef avc_data; yading@10: CFMutableDictionaryRef config_info; yading@10: CFMutableDictionaryRef buffer_attributes; yading@10: CFMutableDictionaryRef io_surface_properties; yading@10: CFNumberRef cv_pix_fmt; yading@10: yading@10: vda_ctx->priv_bitstream = NULL; yading@10: vda_ctx->priv_allocated_size = 0; yading@10: yading@10: /* Each VCL NAL in the bitstream sent to the decoder yading@10: * is preceded by a 4 bytes length header. yading@10: * Change the avcC atom header if needed, to signal headers of 4 bytes. */ yading@10: if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) { yading@10: uint8_t *rw_extradata; yading@10: yading@10: if (!(rw_extradata = av_malloc(extradata_size))) yading@10: return AVERROR(ENOMEM); yading@10: yading@10: memcpy(rw_extradata, extradata, extradata_size); yading@10: yading@10: rw_extradata[4] |= 0x03; yading@10: yading@10: avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size); yading@10: yading@10: av_freep(&rw_extradata); yading@10: } else { yading@10: avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size); yading@10: } yading@10: yading@10: config_info = CFDictionaryCreateMutable(kCFAllocatorDefault, yading@10: 4, yading@10: &kCFTypeDictionaryKeyCallBacks, yading@10: &kCFTypeDictionaryValueCallBacks); yading@10: yading@10: height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height); yading@10: width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width); yading@10: format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format); yading@10: yading@10: CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height); yading@10: CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width); yading@10: CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format); yading@10: CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data); yading@10: yading@10: buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault, yading@10: 2, yading@10: &kCFTypeDictionaryKeyCallBacks, yading@10: &kCFTypeDictionaryValueCallBacks); yading@10: io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault, yading@10: 0, yading@10: &kCFTypeDictionaryKeyCallBacks, yading@10: &kCFTypeDictionaryValueCallBacks); yading@10: cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault, yading@10: kCFNumberSInt32Type, yading@10: &vda_ctx->cv_pix_fmt_type); yading@10: CFDictionarySetValue(buffer_attributes, yading@10: kCVPixelBufferPixelFormatTypeKey, yading@10: cv_pix_fmt); yading@10: CFDictionarySetValue(buffer_attributes, yading@10: kCVPixelBufferIOSurfacePropertiesKey, yading@10: io_surface_properties); yading@10: yading@10: status = VDADecoderCreate(config_info, yading@10: buffer_attributes, yading@10: vda_decoder_callback, yading@10: vda_ctx, yading@10: &vda_ctx->decoder); yading@10: yading@10: CFRelease(height); yading@10: CFRelease(width); yading@10: CFRelease(format); yading@10: CFRelease(avc_data); yading@10: CFRelease(config_info); yading@10: CFRelease(io_surface_properties); yading@10: CFRelease(cv_pix_fmt); yading@10: CFRelease(buffer_attributes); yading@10: yading@10: return status; yading@10: } yading@10: yading@10: int ff_vda_destroy_decoder(struct vda_context *vda_ctx) yading@10: { yading@10: OSStatus status = kVDADecoderNoErr; yading@10: yading@10: if (vda_ctx->decoder) yading@10: status = VDADecoderDestroy(vda_ctx->decoder); yading@10: yading@10: av_freep(&vda_ctx->priv_bitstream); yading@10: yading@10: return status; yading@10: } yading@10: yading@10: AVHWAccel ff_h264_vda_hwaccel = { yading@10: .name = "h264_vda", yading@10: .type = AVMEDIA_TYPE_VIDEO, yading@10: .id = AV_CODEC_ID_H264, yading@10: .pix_fmt = AV_PIX_FMT_VDA_VLD, yading@10: .start_frame = vda_h264_start_frame, yading@10: .decode_slice = vda_h264_decode_slice, yading@10: .end_frame = vda_h264_end_frame, yading@10: };