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.

309 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 (NULL == 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 != NULL)
  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 (NULL == image_buffer)
  105. return;
  106. if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
  107. return;
  108. new_frame = (vda_frame *)av_mallocz(sizeof(vda_frame));
  109. new_frame->next_frame = NULL;
  110. new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
  111. new_frame->pts = vda_pts_from_dictionary(user_info);
  112. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
  113. queue_walker = vda_ctx->queue;
  114. if (!queue_walker || (new_frame->pts < queue_walker->pts))
  115. {
  116. /* we have an empty queue, or this frame earlier than the current queue head */
  117. new_frame->next_frame = queue_walker;
  118. vda_ctx->queue = new_frame;
  119. }
  120. else
  121. {
  122. /* walk the queue and insert this frame where it belongs in display order */
  123. vda_frame *next_frame;
  124. while (1)
  125. {
  126. next_frame = queue_walker->next_frame;
  127. if (!next_frame || (new_frame->pts < next_frame->pts))
  128. {
  129. new_frame->next_frame = next_frame;
  130. queue_walker->next_frame = new_frame;
  131. break;
  132. }
  133. queue_walker = next_frame;
  134. }
  135. }
  136. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
  137. }
  138. int ff_vda_create_decoder(struct vda_context *vda_ctx,
  139. uint8_t *extradata,
  140. int extradata_size)
  141. {
  142. OSStatus status = kVDADecoderNoErr;
  143. CFNumberRef height;
  144. CFNumberRef width;
  145. CFNumberRef format;
  146. CFDataRef avc_data;
  147. CFMutableDictionaryRef config_info;
  148. CFMutableDictionaryRef buffer_attributes;
  149. CFMutableDictionaryRef io_surface_properties;
  150. CFNumberRef cv_pix_fmt;
  151. vda_ctx->bitstream = NULL;
  152. vda_ctx->ref_size = 0;
  153. if (av_lockmgr_register(vda_lock_operation))
  154. return -1;
  155. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_CREATE);
  156. config_info = (CFDictionaryCreateMutable(kCFAllocatorDefault,
  157. 4,
  158. &kCFTypeDictionaryKeyCallBacks,
  159. &kCFTypeDictionaryValueCallBacks));
  160. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  161. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  162. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  163. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  164. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  165. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  166. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  167. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  168. buffer_attributes = (CFDictionaryCreateMutable(kCFAllocatorDefault,
  169. 2,
  170. &kCFTypeDictionaryKeyCallBacks,
  171. &kCFTypeDictionaryValueCallBacks));
  172. io_surface_properties = (CFDictionaryCreateMutable(kCFAllocatorDefault,
  173. 0,
  174. &kCFTypeDictionaryKeyCallBacks,
  175. &kCFTypeDictionaryValueCallBacks));
  176. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  177. kCFNumberSInt32Type,
  178. &vda_ctx->cv_pix_fmt_type);
  179. CFDictionarySetValue(buffer_attributes,
  180. kCVPixelBufferPixelFormatTypeKey,
  181. cv_pix_fmt);
  182. CFDictionarySetValue(buffer_attributes,
  183. kCVPixelBufferIOSurfacePropertiesKey,
  184. io_surface_properties);
  185. status = VDADecoderCreate( config_info,
  186. buffer_attributes,
  187. (VDADecoderOutputCallback *)vda_decoder_callback,
  188. (void *)vda_ctx,
  189. &vda_ctx->decoder );
  190. CFRelease(height);
  191. CFRelease(width);
  192. CFRelease(format);
  193. CFRelease(avc_data);
  194. CFRelease(config_info);
  195. CFRelease(io_surface_properties);
  196. CFRelease(cv_pix_fmt);
  197. CFRelease(buffer_attributes);
  198. if (kVDADecoderNoErr != status)
  199. return status;
  200. return 0;
  201. }
  202. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  203. {
  204. OSStatus status = kVDADecoderNoErr;
  205. if (vda_ctx->decoder)
  206. status = VDADecoderDestroy(vda_ctx->decoder);
  207. vda_clear_queue(vda_ctx);
  208. if (vda_ctx->queue_mutex != NULL)
  209. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_DESTROY);
  210. if (vda_ctx->bitstream)
  211. av_freep(&vda_ctx->bitstream);
  212. if (kVDADecoderNoErr != status)
  213. return status;
  214. return 0;
  215. }
  216. vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
  217. {
  218. vda_frame *top_frame;
  219. if (!vda_ctx->queue)
  220. return NULL;
  221. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
  222. top_frame = vda_ctx->queue;
  223. vda_ctx->queue = top_frame->next_frame;
  224. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
  225. return top_frame;
  226. }
  227. void ff_vda_release_vda_frame(vda_frame *frame)
  228. {
  229. if (frame != NULL)
  230. {
  231. CVPixelBufferRelease(frame->cv_buffer);
  232. av_freep(&frame);
  233. }
  234. }
  235. int ff_vda_decoder_decode(struct vda_context *vda_ctx,
  236. uint8_t *bitstream,
  237. int bitstream_size,
  238. int64_t frame_pts)
  239. {
  240. OSStatus status = kVDADecoderNoErr;
  241. CFDictionaryRef user_info;
  242. CFDataRef coded_frame;
  243. coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
  244. user_info = vda_dictionary_with_pts(frame_pts);
  245. status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
  246. CFRelease(user_info);
  247. CFRelease(coded_frame);
  248. if (kVDADecoderNoErr != status)
  249. return status;
  250. return 0;
  251. }
  252. /* @} */