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
00153
00154
00155 if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
00156 uint8_t *rw_extradata;
00157
00158 if (!(rw_extradata = av_malloc(extradata_size)))
00159 return AVERROR(ENOMEM);
00160
00161 memcpy(rw_extradata, extradata, extradata_size);
00162
00163 rw_extradata[4] |= 0x03;
00164
00165 avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
00166
00167 av_freep(&rw_extradata);
00168 } else {
00169 avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
00170 }
00171
00172 config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
00173 4,
00174 &kCFTypeDictionaryKeyCallBacks,
00175 &kCFTypeDictionaryValueCallBacks);
00176
00177 height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
00178 width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
00179 format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
00180
00181 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
00182 CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
00183 CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
00184 CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
00185
00186 buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
00187 2,
00188 &kCFTypeDictionaryKeyCallBacks,
00189 &kCFTypeDictionaryValueCallBacks);
00190 io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
00191 0,
00192 &kCFTypeDictionaryKeyCallBacks,
00193 &kCFTypeDictionaryValueCallBacks);
00194 cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
00195 kCFNumberSInt32Type,
00196 &vda_ctx->cv_pix_fmt_type);
00197 CFDictionarySetValue(buffer_attributes,
00198 kCVPixelBufferPixelFormatTypeKey,
00199 cv_pix_fmt);
00200 CFDictionarySetValue(buffer_attributes,
00201 kCVPixelBufferIOSurfacePropertiesKey,
00202 io_surface_properties);
00203
00204 status = VDADecoderCreate(config_info,
00205 buffer_attributes,
00206 (VDADecoderOutputCallback *)vda_decoder_callback,
00207 vda_ctx,
00208 &vda_ctx->decoder);
00209
00210 CFRelease(height);
00211 CFRelease(width);
00212 CFRelease(format);
00213 CFRelease(avc_data);
00214 CFRelease(config_info);
00215 CFRelease(io_surface_properties);
00216 CFRelease(cv_pix_fmt);
00217 CFRelease(buffer_attributes);
00218
00219 if (kVDADecoderNoErr != status)
00220 return status;
00221
00222 return 0;
00223 }
00224
00225 int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
00226 {
00227 OSStatus status = kVDADecoderNoErr;
00228
00229 if (vda_ctx->decoder)
00230 status = VDADecoderDestroy(vda_ctx->decoder);
00231
00232 vda_clear_queue(vda_ctx);
00233
00234 pthread_mutex_destroy(&vda_ctx->queue_mutex);
00235
00236 if (vda_ctx->bitstream)
00237 av_freep(&vda_ctx->bitstream);
00238
00239 if (kVDADecoderNoErr != status)
00240 return status;
00241
00242 return 0;
00243 }
00244
00245 vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
00246 {
00247 vda_frame *top_frame;
00248
00249 if (!vda_ctx->queue)
00250 return NULL;
00251
00252 pthread_mutex_lock(&vda_ctx->queue_mutex);
00253 top_frame = vda_ctx->queue;
00254 vda_ctx->queue = top_frame->next_frame;
00255 pthread_mutex_unlock(&vda_ctx->queue_mutex);
00256
00257 return top_frame;
00258 }
00259
00260 void ff_vda_release_vda_frame(vda_frame *frame)
00261 {
00262 if (frame) {
00263 CVPixelBufferRelease(frame->cv_buffer);
00264 av_freep(&frame);
00265 }
00266 }
00267
00268 int ff_vda_decoder_decode(struct vda_context *vda_ctx,
00269 uint8_t *bitstream,
00270 int bitstream_size,
00271 int64_t frame_pts)
00272 {
00273 OSStatus status = kVDADecoderNoErr;
00274 CFDictionaryRef user_info;
00275 CFDataRef coded_frame;
00276
00277 coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
00278 user_info = vda_dictionary_with_pts(frame_pts);
00279
00280 status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
00281
00282 CFRelease(user_info);
00283 CFRelease(coded_frame);
00284
00285 if (kVDADecoderNoErr != status)
00286 return status;
00287
00288 return 0;
00289 }