annotate ffmpeg/libavcodec/vda_h264.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * VDA H264 HW acceleration.
yading@10 3 *
yading@10 4 * copyright (c) 2011 Sebastien Zwickert
yading@10 5 *
yading@10 6 * This file is part of FFmpeg.
yading@10 7 *
yading@10 8 * FFmpeg is free software; you can redistribute it and/or
yading@10 9 * modify it under the terms of the GNU Lesser General Public
yading@10 10 * License as published by the Free Software Foundation; either
yading@10 11 * version 2.1 of the License, or (at your option) any later version.
yading@10 12 *
yading@10 13 * FFmpeg is distributed in the hope that it will be useful,
yading@10 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 16 * Lesser General Public License for more details.
yading@10 17 *
yading@10 18 * You should have received a copy of the GNU Lesser General Public
yading@10 19 * License along with FFmpeg; if not, write to the Free Software
yading@10 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 21 */
yading@10 22
yading@10 23 #include <CoreFoundation/CFDictionary.h>
yading@10 24 #include <CoreFoundation/CFNumber.h>
yading@10 25 #include <CoreFoundation/CFData.h>
yading@10 26
yading@10 27 #include "vda.h"
yading@10 28 #include "libavutil/avutil.h"
yading@10 29 #include "h264.h"
yading@10 30
yading@10 31
yading@10 32 /* Decoder callback that adds the vda frame to the queue in display order. */
yading@10 33 static void vda_decoder_callback (void *vda_hw_ctx,
yading@10 34 CFDictionaryRef user_info,
yading@10 35 OSStatus status,
yading@10 36 uint32_t infoFlags,
yading@10 37 CVImageBufferRef image_buffer)
yading@10 38 {
yading@10 39 struct vda_context *vda_ctx = vda_hw_ctx;
yading@10 40
yading@10 41 if (!image_buffer)
yading@10 42 return;
yading@10 43
yading@10 44 if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
yading@10 45 return;
yading@10 46
yading@10 47 vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
yading@10 48 }
yading@10 49
yading@10 50 static int vda_sync_decode(struct vda_context *vda_ctx)
yading@10 51 {
yading@10 52 OSStatus status;
yading@10 53 CFDataRef coded_frame;
yading@10 54 uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
yading@10 55
yading@10 56 coded_frame = CFDataCreate(kCFAllocatorDefault,
yading@10 57 vda_ctx->priv_bitstream,
yading@10 58 vda_ctx->priv_bitstream_size);
yading@10 59
yading@10 60 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
yading@10 61
yading@10 62 if (kVDADecoderNoErr == status)
yading@10 63 status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
yading@10 64
yading@10 65 CFRelease(coded_frame);
yading@10 66
yading@10 67 return status;
yading@10 68 }
yading@10 69
yading@10 70
yading@10 71 static int vda_h264_start_frame(AVCodecContext *avctx,
yading@10 72 av_unused const uint8_t *buffer,
yading@10 73 av_unused uint32_t size)
yading@10 74 {
yading@10 75 struct vda_context *vda_ctx = avctx->hwaccel_context;
yading@10 76
yading@10 77 if (!vda_ctx->decoder)
yading@10 78 return -1;
yading@10 79
yading@10 80 vda_ctx->priv_bitstream_size = 0;
yading@10 81
yading@10 82 return 0;
yading@10 83 }
yading@10 84
yading@10 85 static int vda_h264_decode_slice(AVCodecContext *avctx,
yading@10 86 const uint8_t *buffer,
yading@10 87 uint32_t size)
yading@10 88 {
yading@10 89 struct vda_context *vda_ctx = avctx->hwaccel_context;
yading@10 90 void *tmp;
yading@10 91
yading@10 92 if (!vda_ctx->decoder)
yading@10 93 return -1;
yading@10 94
yading@10 95 tmp = av_fast_realloc(vda_ctx->priv_bitstream,
yading@10 96 &vda_ctx->priv_allocated_size,
yading@10 97 vda_ctx->priv_bitstream_size + size + 4);
yading@10 98 if (!tmp)
yading@10 99 return AVERROR(ENOMEM);
yading@10 100
yading@10 101 vda_ctx->priv_bitstream = tmp;
yading@10 102
yading@10 103 AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size);
yading@10 104 memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size);
yading@10 105
yading@10 106 vda_ctx->priv_bitstream_size += size + 4;
yading@10 107
yading@10 108 return 0;
yading@10 109 }
yading@10 110
yading@10 111 static int vda_h264_end_frame(AVCodecContext *avctx)
yading@10 112 {
yading@10 113 H264Context *h = avctx->priv_data;
yading@10 114 struct vda_context *vda_ctx = avctx->hwaccel_context;
yading@10 115 AVFrame *frame = &h->cur_pic_ptr->f;
yading@10 116 int status;
yading@10 117
yading@10 118 if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
yading@10 119 return -1;
yading@10 120
yading@10 121 status = vda_sync_decode(vda_ctx);
yading@10 122 frame->data[3] = (void*)vda_ctx->cv_buffer;
yading@10 123
yading@10 124 if (status)
yading@10 125 av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
yading@10 126
yading@10 127 return status;
yading@10 128 }
yading@10 129
yading@10 130 int ff_vda_create_decoder(struct vda_context *vda_ctx,
yading@10 131 uint8_t *extradata,
yading@10 132 int extradata_size)
yading@10 133 {
yading@10 134 OSStatus status;
yading@10 135 CFNumberRef height;
yading@10 136 CFNumberRef width;
yading@10 137 CFNumberRef format;
yading@10 138 CFDataRef avc_data;
yading@10 139 CFMutableDictionaryRef config_info;
yading@10 140 CFMutableDictionaryRef buffer_attributes;
yading@10 141 CFMutableDictionaryRef io_surface_properties;
yading@10 142 CFNumberRef cv_pix_fmt;
yading@10 143
yading@10 144 vda_ctx->priv_bitstream = NULL;
yading@10 145 vda_ctx->priv_allocated_size = 0;
yading@10 146
yading@10 147 /* Each VCL NAL in the bitstream sent to the decoder
yading@10 148 * is preceded by a 4 bytes length header.
yading@10 149 * Change the avcC atom header if needed, to signal headers of 4 bytes. */
yading@10 150 if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
yading@10 151 uint8_t *rw_extradata;
yading@10 152
yading@10 153 if (!(rw_extradata = av_malloc(extradata_size)))
yading@10 154 return AVERROR(ENOMEM);
yading@10 155
yading@10 156 memcpy(rw_extradata, extradata, extradata_size);
yading@10 157
yading@10 158 rw_extradata[4] |= 0x03;
yading@10 159
yading@10 160 avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
yading@10 161
yading@10 162 av_freep(&rw_extradata);
yading@10 163 } else {
yading@10 164 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
yading@10 165 }
yading@10 166
yading@10 167 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
yading@10 168 4,
yading@10 169 &kCFTypeDictionaryKeyCallBacks,
yading@10 170 &kCFTypeDictionaryValueCallBacks);
yading@10 171
yading@10 172 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
yading@10 173 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
yading@10 174 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
yading@10 175
yading@10 176 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
yading@10 177 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
yading@10 178 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
yading@10 179 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
yading@10 180
yading@10 181 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
yading@10 182 2,
yading@10 183 &kCFTypeDictionaryKeyCallBacks,
yading@10 184 &kCFTypeDictionaryValueCallBacks);
yading@10 185 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
yading@10 186 0,
yading@10 187 &kCFTypeDictionaryKeyCallBacks,
yading@10 188 &kCFTypeDictionaryValueCallBacks);
yading@10 189 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
yading@10 190 kCFNumberSInt32Type,
yading@10 191 &vda_ctx->cv_pix_fmt_type);
yading@10 192 CFDictionarySetValue(buffer_attributes,
yading@10 193 kCVPixelBufferPixelFormatTypeKey,
yading@10 194 cv_pix_fmt);
yading@10 195 CFDictionarySetValue(buffer_attributes,
yading@10 196 kCVPixelBufferIOSurfacePropertiesKey,
yading@10 197 io_surface_properties);
yading@10 198
yading@10 199 status = VDADecoderCreate(config_info,
yading@10 200 buffer_attributes,
yading@10 201 vda_decoder_callback,
yading@10 202 vda_ctx,
yading@10 203 &vda_ctx->decoder);
yading@10 204
yading@10 205 CFRelease(height);
yading@10 206 CFRelease(width);
yading@10 207 CFRelease(format);
yading@10 208 CFRelease(avc_data);
yading@10 209 CFRelease(config_info);
yading@10 210 CFRelease(io_surface_properties);
yading@10 211 CFRelease(cv_pix_fmt);
yading@10 212 CFRelease(buffer_attributes);
yading@10 213
yading@10 214 return status;
yading@10 215 }
yading@10 216
yading@10 217 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
yading@10 218 {
yading@10 219 OSStatus status = kVDADecoderNoErr;
yading@10 220
yading@10 221 if (vda_ctx->decoder)
yading@10 222 status = VDADecoderDestroy(vda_ctx->decoder);
yading@10 223
yading@10 224 av_freep(&vda_ctx->priv_bitstream);
yading@10 225
yading@10 226 return status;
yading@10 227 }
yading@10 228
yading@10 229 AVHWAccel ff_h264_vda_hwaccel = {
yading@10 230 .name = "h264_vda",
yading@10 231 .type = AVMEDIA_TYPE_VIDEO,
yading@10 232 .id = AV_CODEC_ID_H264,
yading@10 233 .pix_fmt = AV_PIX_FMT_VDA_VLD,
yading@10 234 .start_frame = vda_h264_start_frame,
yading@10 235 .decode_slice = vda_h264_decode_slice,
yading@10 236 .end_frame = vda_h264_end_frame,
yading@10 237 };