You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
9.7KB

  1. /*
  2. * VDA HW acceleration.
  3. *
  4. * copyright (c) 2011 Sebastien Zwickert
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <pthread.h>
  23. #include <CoreFoundation/CFDictionary.h>
  24. #include <CoreFoundation/CFNumber.h>
  25. #include <CoreFoundation/CFData.h>
  26. #include <CoreFoundation/CFString.h>
  27. #include "avcodec.h"
  28. #include "vda_internal.h"
  29. /**
  30. * \addtogroup VDA_Decoding
  31. *
  32. * @{
  33. */
  34. /* Mutex manager callback. */
  35. static int vda_lock_operation(void **mtx, enum AVLockOp op)
  36. {
  37. switch(op)
  38. {
  39. case AV_LOCK_CREATE:
  40. *mtx = av_malloc(sizeof(pthread_mutex_t));
  41. if(!*mtx)
  42. return 1;
  43. return !!pthread_mutex_init(*mtx, NULL);
  44. case AV_LOCK_OBTAIN:
  45. return !!pthread_mutex_lock(*mtx);
  46. case AV_LOCK_RELEASE:
  47. return !!pthread_mutex_unlock(*mtx);
  48. case AV_LOCK_DESTROY:
  49. pthread_mutex_destroy(*mtx);
  50. av_freep(mtx);
  51. return 0;
  52. }
  53. return 1;
  54. }
  55. /* Helper to create a dictionary according to the given pts. */
  56. static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
  57. {
  58. CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
  59. CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &i_pts);
  60. CFDictionaryRef user_info = CFDictionaryCreate( kCFAllocatorDefault,
  61. (const void **)&key,
  62. (const void **)&value,
  63. 1,
  64. &kCFTypeDictionaryKeyCallBacks,
  65. &kCFTypeDictionaryValueCallBacks);
  66. CFRelease(value);
  67. return user_info;
  68. }
  69. /* Helper to retrieve the pts from the given dictionary. */
  70. static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
  71. {
  72. CFNumberRef pts;
  73. int64_t outValue = 0;
  74. if (!user_info)
  75. return 0;
  76. pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
  77. if (pts)
  78. CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
  79. return outValue;
  80. }
  81. /* Removes and releases all frames from the queue. */
  82. static void vda_clear_queue(struct vda_context *vda_ctx)
  83. {
  84. vda_frame *top_frame;
  85. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
  86. while (vda_ctx->queue)
  87. {
  88. top_frame = vda_ctx->queue;
  89. vda_ctx->queue = top_frame->next_frame;
  90. ff_vda_release_vda_frame(top_frame);
  91. }
  92. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
  93. }
  94. /* Decoder callback that adds the vda frame to the queue in display order. */
  95. static void vda_decoder_callback (void *vda_hw_ctx,
  96. CFDictionaryRef user_info,
  97. OSStatus status,
  98. uint32_t infoFlags,
  99. CVImageBufferRef image_buffer)
  100. {
  101. struct vda_context *vda_ctx = (struct vda_context*)vda_hw_ctx;
  102. vda_frame *new_frame;
  103. vda_frame *queue_walker;
  104. if (!image_buffer)
  105. return;
  106. if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
  107. return;
  108. new_frame = av_mallocz(sizeof(vda_frame));
  109. if (!new_frame)
  110. return;
  111. new_frame->next_frame = NULL;
  112. new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
  113. new_frame->pts = vda_pts_from_dictionary(user_info);
  114. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
  115. queue_walker = vda_ctx->queue;
  116. if (!queue_walker || (new_frame->pts < queue_walker->pts))
  117. {
  118. /* we have an empty queue, or this frame earlier than the current queue head */
  119. new_frame->next_frame = queue_walker;
  120. vda_ctx->queue = new_frame;
  121. }
  122. else
  123. {
  124. /* walk the queue and insert this frame where it belongs in display order */
  125. vda_frame *next_frame;
  126. while (1)
  127. {
  128. next_frame = queue_walker->next_frame;
  129. if (!next_frame || (new_frame->pts < next_frame->pts))
  130. {
  131. new_frame->next_frame = next_frame;
  132. queue_walker->next_frame = new_frame;
  133. break;
  134. }
  135. queue_walker = next_frame;
  136. }
  137. }
  138. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
  139. }
  140. int ff_vda_create_decoder(struct vda_context *vda_ctx,
  141. uint8_t *extradata,
  142. int extradata_size)
  143. {
  144. OSStatus status = kVDADecoderNoErr;
  145. CFNumberRef height;
  146. CFNumberRef width;
  147. CFNumberRef format;
  148. CFDataRef avc_data;
  149. CFMutableDictionaryRef config_info;
  150. CFMutableDictionaryRef buffer_attributes;
  151. CFMutableDictionaryRef io_surface_properties;
  152. CFNumberRef cv_pix_fmt;
  153. vda_ctx->bitstream = NULL;
  154. vda_ctx->ref_size = 0;
  155. if (av_lockmgr_register(vda_lock_operation))
  156. return -1;
  157. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_CREATE);
  158. config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
  159. 4,
  160. &kCFTypeDictionaryKeyCallBacks,
  161. &kCFTypeDictionaryValueCallBacks);
  162. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  163. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  164. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  165. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  166. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  167. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  168. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  169. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  170. buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
  171. 2,
  172. &kCFTypeDictionaryKeyCallBacks,
  173. &kCFTypeDictionaryValueCallBacks);
  174. io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
  175. 0,
  176. &kCFTypeDictionaryKeyCallBacks,
  177. &kCFTypeDictionaryValueCallBacks);
  178. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  179. kCFNumberSInt32Type,
  180. &vda_ctx->cv_pix_fmt_type);
  181. CFDictionarySetValue(buffer_attributes,
  182. kCVPixelBufferPixelFormatTypeKey,
  183. cv_pix_fmt);
  184. CFDictionarySetValue(buffer_attributes,
  185. kCVPixelBufferIOSurfacePropertiesKey,
  186. io_surface_properties);
  187. status = VDADecoderCreate( config_info,
  188. buffer_attributes,
  189. (VDADecoderOutputCallback *)vda_decoder_callback,
  190. vda_ctx,
  191. &vda_ctx->decoder );
  192. CFRelease(height);
  193. CFRelease(width);
  194. CFRelease(format);
  195. CFRelease(avc_data);
  196. CFRelease(config_info);
  197. CFRelease(io_surface_properties);
  198. CFRelease(cv_pix_fmt);
  199. CFRelease(buffer_attributes);
  200. if (kVDADecoderNoErr != status)
  201. return status;
  202. return 0;
  203. }
  204. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  205. {
  206. OSStatus status = kVDADecoderNoErr;
  207. if (vda_ctx->decoder)
  208. status = VDADecoderDestroy(vda_ctx->decoder);
  209. vda_clear_queue(vda_ctx);
  210. if (vda_ctx->queue_mutex)
  211. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_DESTROY);
  212. if (vda_ctx->bitstream)
  213. av_freep(&vda_ctx->bitstream);
  214. if (kVDADecoderNoErr != status)
  215. return status;
  216. return 0;
  217. }
  218. vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
  219. {
  220. vda_frame *top_frame;
  221. if (!vda_ctx->queue)
  222. return NULL;
  223. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
  224. top_frame = vda_ctx->queue;
  225. vda_ctx->queue = top_frame->next_frame;
  226. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
  227. return top_frame;
  228. }
  229. void ff_vda_release_vda_frame(vda_frame *frame)
  230. {
  231. if (frame)
  232. {
  233. CVPixelBufferRelease(frame->cv_buffer);
  234. av_freep(&frame);
  235. }
  236. }
  237. int ff_vda_decoder_decode(struct vda_context *vda_ctx,
  238. uint8_t *bitstream,
  239. int bitstream_size,
  240. int64_t frame_pts)
  241. {
  242. OSStatus status = kVDADecoderNoErr;
  243. CFDictionaryRef user_info;
  244. CFDataRef coded_frame;
  245. coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
  246. user_info = vda_dictionary_with_pts(frame_pts);
  247. status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
  248. CFRelease(user_info);
  249. CFRelease(coded_frame);
  250. if (kVDADecoderNoErr != status)
  251. return status;
  252. return 0;
  253. }
  254. /* @} */