00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <pthread.h>
00024 #include <CoreFoundation/CFDictionary.h>
00025 #include <CoreFoundation/CFNumber.h>
00026 #include <CoreFoundation/CFData.h>
00027 #include <CoreFoundation/CFString.h>
00028
00029 #include "avcodec.h"
00030 #include "vda_internal.h"
00031
00038
00039 static int vda_lock_operation(void **mtx, enum AVLockOp op)
00040 {
00041 switch (op) {
00042 case AV_LOCK_CREATE:
00043 *mtx = av_malloc(sizeof(pthread_mutex_t));
00044 if (!*mtx)
00045 return 1;
00046 return !!pthread_mutex_init(*mtx, NULL);
00047 case AV_LOCK_OBTAIN:
00048 return !!pthread_mutex_lock(*mtx);
00049 case AV_LOCK_RELEASE:
00050 return !!pthread_mutex_unlock(*mtx);
00051 case AV_LOCK_DESTROY:
00052 pthread_mutex_destroy(*mtx);
00053 av_freep(mtx);
00054 return 0;
00055 }
00056 return 1;
00057 }
00058
00059
00060 static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
00061 {
00062 CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
00063 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &i_pts);
00064 CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
00065 (const void **)&key,
00066 (const void **)&value,
00067 1,
00068 &kCFTypeDictionaryKeyCallBacks,
00069 &kCFTypeDictionaryValueCallBacks);
00070 CFRelease(value);
00071 return user_info;
00072 }
00073
00074
00075 static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
00076 {
00077 CFNumberRef pts;
00078 int64_t outValue = 0;
00079
00080 if (!user_info)
00081 return 0;
00082
00083 pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
00084
00085 if (pts)
00086 CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
00087
00088 return outValue;
00089 }
00090
00091
00092 static void vda_clear_queue(struct vda_context *vda_ctx)
00093 {
00094 vda_frame *top_frame;
00095
00096 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
00097
00098 while (vda_ctx->queue) {
00099 top_frame = vda_ctx->queue;
00100 vda_ctx->queue = top_frame->next_frame;
00101 ff_vda_release_vda_frame(top_frame);
00102 }
00103
00104 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
00105 }
00106
00107
00108
00109 static void vda_decoder_callback (void *vda_hw_ctx,
00110 CFDictionaryRef user_info,
00111 OSStatus status,
00112 uint32_t infoFlags,
00113 CVImageBufferRef image_buffer)
00114 {
00115 struct vda_context *vda_ctx = (struct vda_context*)vda_hw_ctx;
00116 vda_frame *new_frame;
00117 vda_frame *queue_walker;
00118
00119 if (!image_buffer)
00120 return;
00121
00122 if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
00123 return;
00124
00125 new_frame = av_mallocz(sizeof(vda_frame));
00126 if (!new_frame)
00127 return;
00128
00129 new_frame->next_frame = NULL;
00130 new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
00131 new_frame->pts = vda_pts_from_dictionary(user_info);
00132
00133 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
00134
00135 queue_walker = vda_ctx->queue;
00136
00137 if (!queue_walker || (new_frame->pts < queue_walker->pts)) {
00138
00139 new_frame->next_frame = queue_walker;
00140 vda_ctx->queue = new_frame;
00141 } else {
00142
00143 vda_frame *next_frame;
00144
00145 while (1) {
00146 next_frame = queue_walker->next_frame;
00147
00148 if (!next_frame || (new_frame->pts < next_frame->pts)) {
00149 new_frame->next_frame = next_frame;
00150 queue_walker->next_frame = new_frame;
00151 break;
00152 }
00153 queue_walker = next_frame;
00154 }
00155 }
00156
00157 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
00158 }
00159
00160 int ff_vda_create_decoder(struct vda_context *vda_ctx,
00161 uint8_t *extradata,
00162 int extradata_size)
00163 {
00164 OSStatus status = kVDADecoderNoErr;
00165 CFNumberRef height;
00166 CFNumberRef width;
00167 CFNumberRef format;
00168 CFDataRef avc_data;
00169 CFMutableDictionaryRef config_info;
00170 CFMutableDictionaryRef buffer_attributes;
00171 CFMutableDictionaryRef io_surface_properties;
00172 CFNumberRef cv_pix_fmt;
00173
00174 vda_ctx->bitstream = NULL;
00175 vda_ctx->ref_size = 0;
00176
00177 if (av_lockmgr_register(vda_lock_operation))
00178 return -1;
00179
00180 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_CREATE);
00181
00182 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
00183 4,
00184 &kCFTypeDictionaryKeyCallBacks,
00185 &kCFTypeDictionaryValueCallBacks);
00186
00187 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
00188 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
00189 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
00190 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
00191
00192 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
00193 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
00194 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
00195 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
00196
00197 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
00198 2,
00199 &kCFTypeDictionaryKeyCallBacks,
00200 &kCFTypeDictionaryValueCallBacks);
00201 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
00202 0,
00203 &kCFTypeDictionaryKeyCallBacks,
00204 &kCFTypeDictionaryValueCallBacks);
00205 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
00206 kCFNumberSInt32Type,
00207 &vda_ctx->cv_pix_fmt_type);
00208 CFDictionarySetValue(buffer_attributes,
00209 kCVPixelBufferPixelFormatTypeKey,
00210 cv_pix_fmt);
00211 CFDictionarySetValue(buffer_attributes,
00212 kCVPixelBufferIOSurfacePropertiesKey,
00213 io_surface_properties);
00214
00215 status = VDADecoderCreate(config_info,
00216 buffer_attributes,
00217 (VDADecoderOutputCallback *)vda_decoder_callback,
00218 vda_ctx,
00219 &vda_ctx->decoder);
00220
00221 CFRelease(height);
00222 CFRelease(width);
00223 CFRelease(format);
00224 CFRelease(avc_data);
00225 CFRelease(config_info);
00226 CFRelease(io_surface_properties);
00227 CFRelease(cv_pix_fmt);
00228 CFRelease(buffer_attributes);
00229
00230 if (kVDADecoderNoErr != status)
00231 return status;
00232
00233 return 0;
00234 }
00235
00236 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
00237 {
00238 OSStatus status = kVDADecoderNoErr;
00239
00240 if (vda_ctx->decoder)
00241 status = VDADecoderDestroy(vda_ctx->decoder);
00242
00243 vda_clear_queue(vda_ctx);
00244
00245 if (vda_ctx->queue_mutex)
00246 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_DESTROY);
00247
00248 if (vda_ctx->bitstream)
00249 av_freep(&vda_ctx->bitstream);
00250
00251 if (kVDADecoderNoErr != status)
00252 return status;
00253
00254 return 0;
00255 }
00256
00257 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
00258 {
00259 vda_frame *top_frame;
00260
00261 if (!vda_ctx->queue)
00262 return NULL;
00263
00264 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
00265 top_frame = vda_ctx->queue;
00266 vda_ctx->queue = top_frame->next_frame;
00267 vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
00268
00269 return top_frame;
00270 }
00271
00272 void ff_vda_release_vda_frame(vda_frame *frame)
00273 {
00274 if (frame) {
00275 CVPixelBufferRelease(frame->cv_buffer);
00276 av_freep(&frame);
00277 }
00278 }
00279
00280 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
00281 uint8_t *bitstream,
00282 int bitstream_size,
00283 int64_t frame_pts)
00284 {
00285 OSStatus status = kVDADecoderNoErr;
00286 CFDictionaryRef user_info;
00287 CFDataRef coded_frame;
00288
00289 coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
00290 user_info = vda_dictionary_with_pts(frame_pts);
00291
00292 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
00293
00294 CFRelease(user_info);
00295 CFRelease(coded_frame);
00296
00297 if (kVDADecoderNoErr != status)
00298 return status;
00299
00300 return 0;
00301 }
00302
00303