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 "libavutil/avutil.h"
00030 #include "vda_internal.h"
00031
00032
00033 static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
00034 {
00035 CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
00036 CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &i_pts);
00037 CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
00038 (const void **)&key,
00039 (const void **)&value,
00040 1,
00041 &kCFTypeDictionaryKeyCallBacks,
00042 &kCFTypeDictionaryValueCallBacks);
00043 CFRelease(value);
00044 return user_info;
00045 }
00046
00047
00048 static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
00049 {
00050 CFNumberRef pts;
00051 int64_t outValue = 0;
00052
00053 if (!user_info)
00054 return 0;
00055
00056 pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
00057
00058 if (pts)
00059 CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
00060
00061 return outValue;
00062 }
00063
00064
00065 static void vda_clear_queue(struct vda_context *vda_ctx)
00066 {
00067 vda_frame *top_frame;
00068
00069 pthread_mutex_lock(&vda_ctx->queue_mutex);
00070
00071 while (vda_ctx->queue) {
00072 top_frame = vda_ctx->queue;
00073 vda_ctx->queue = top_frame->next_frame;
00074 ff_vda_release_vda_frame(top_frame);
00075 }
00076
00077 pthread_mutex_unlock(&vda_ctx->queue_mutex);
00078 }
00079
00080
00081
00082 static void vda_decoder_callback (void *vda_hw_ctx,
00083 CFDictionaryRef user_info,
00084 OSStatus status,
00085 uint32_t infoFlags,
00086 CVImageBufferRef image_buffer)
00087 {
00088 struct vda_context *vda_ctx = (struct vda_context*)vda_hw_ctx;
00089 vda_frame *new_frame;
00090 vda_frame *queue_walker;
00091
00092 if (!image_buffer)
00093 return;
00094
00095 if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
00096 return;
00097
00098 new_frame = av_mallocz(sizeof(vda_frame));
00099 if (!new_frame)
00100 return;
00101
00102 new_frame->next_frame = NULL;
00103 new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
00104 new_frame->pts = vda_pts_from_dictionary(user_info);
00105
00106 pthread_mutex_lock(&vda_ctx->queue_mutex);
00107
00108 queue_walker = vda_ctx->queue;
00109
00110 if (!queue_walker || (new_frame->pts < queue_walker->pts)) {
00111
00112 new_frame->next_frame = queue_walker;
00113 vda_ctx->queue = new_frame;
00114 } else {
00115
00116 vda_frame *next_frame;
00117
00118 while (1) {
00119 next_frame = queue_walker->next_frame;
00120
00121 if (!next_frame || (new_frame->pts < next_frame->pts)) {
00122 new_frame->next_frame = next_frame;
00123 queue_walker->next_frame = new_frame;
00124 break;
00125 }
00126 queue_walker = next_frame;
00127 }
00128 }
00129
00130 pthread_mutex_unlock(&vda_ctx->queue_mutex);
00131 }
00132
00133 int ff_vda_create_decoder(struct vda_context *vda_ctx,
00134 uint8_t *extradata,
00135 int extradata_size)
00136 {
00137 OSStatus status = kVDADecoderNoErr;
00138 CFNumberRef height;
00139 CFNumberRef width;
00140 CFNumberRef format;
00141 CFDataRef avc_data;
00142 CFMutableDictionaryRef config_info;
00143 CFMutableDictionaryRef buffer_attributes;
00144 CFMutableDictionaryRef io_surface_properties;
00145 CFNumberRef cv_pix_fmt;
00146
00147 vda_ctx->bitstream = NULL;
00148 vda_ctx->ref_size = 0;
00149
00150 pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
00151
00152 if (extradata[4]==0xFE) {
00153
00154 extradata[4] = 0xFF;
00155 }
00156
00157 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
00158 4,
00159 &kCFTypeDictionaryKeyCallBacks,
00160 &kCFTypeDictionaryValueCallBacks);
00161
00162 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
00163 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
00164 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
00165 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
00166
00167 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
00168 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
00169 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
00170 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
00171
00172 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
00173 2,
00174 &kCFTypeDictionaryKeyCallBacks,
00175 &kCFTypeDictionaryValueCallBacks);
00176 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
00177 0,
00178 &kCFTypeDictionaryKeyCallBacks,
00179 &kCFTypeDictionaryValueCallBacks);
00180 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
00181 kCFNumberSInt32Type,
00182 &vda_ctx->cv_pix_fmt_type);
00183 CFDictionarySetValue(buffer_attributes,
00184 kCVPixelBufferPixelFormatTypeKey,
00185 cv_pix_fmt);
00186 CFDictionarySetValue(buffer_attributes,
00187 kCVPixelBufferIOSurfacePropertiesKey,
00188 io_surface_properties);
00189
00190 status = VDADecoderCreate(config_info,
00191 buffer_attributes,
00192 (VDADecoderOutputCallback *)vda_decoder_callback,
00193 vda_ctx,
00194 &vda_ctx->decoder);
00195
00196 CFRelease(height);
00197 CFRelease(width);
00198 CFRelease(format);
00199 CFRelease(avc_data);
00200 CFRelease(config_info);
00201 CFRelease(io_surface_properties);
00202 CFRelease(cv_pix_fmt);
00203 CFRelease(buffer_attributes);
00204
00205 if (kVDADecoderNoErr != status)
00206 return status;
00207
00208 return 0;
00209 }
00210
00211 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
00212 {
00213 OSStatus status = kVDADecoderNoErr;
00214
00215 if (vda_ctx->decoder)
00216 status = VDADecoderDestroy(vda_ctx->decoder);
00217
00218 vda_clear_queue(vda_ctx);
00219
00220 pthread_mutex_destroy(&vda_ctx->queue_mutex);
00221
00222 if (vda_ctx->bitstream)
00223 av_freep(&vda_ctx->bitstream);
00224
00225 if (kVDADecoderNoErr != status)
00226 return status;
00227
00228 return 0;
00229 }
00230
00231 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
00232 {
00233 vda_frame *top_frame;
00234
00235 if (!vda_ctx->queue)
00236 return NULL;
00237
00238 pthread_mutex_lock(&vda_ctx->queue_mutex);
00239 top_frame = vda_ctx->queue;
00240 vda_ctx->queue = top_frame->next_frame;
00241 pthread_mutex_unlock(&vda_ctx->queue_mutex);
00242
00243 return top_frame;
00244 }
00245
00246 void ff_vda_release_vda_frame(vda_frame *frame)
00247 {
00248 if (frame) {
00249 CVPixelBufferRelease(frame->cv_buffer);
00250 av_freep(&frame);
00251 }
00252 }
00253
00254 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
00255 uint8_t *bitstream,
00256 int bitstream_size,
00257 int64_t frame_pts)
00258 {
00259 OSStatus status = kVDADecoderNoErr;
00260 CFDictionaryRef user_info;
00261 CFDataRef coded_frame;
00262
00263 coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
00264 user_info = vda_dictionary_with_pts(frame_pts);
00265
00266 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
00267
00268 CFRelease(user_info);
00269 CFRelease(coded_frame);
00270
00271 if (kVDADecoderNoErr != status)
00272 return status;
00273
00274 return 0;
00275 }