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.

304 lines
9.6KB

  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. case AV_LOCK_CREATE:
  39. *mtx = av_malloc(sizeof(pthread_mutex_t));
  40. if (!*mtx)
  41. return 1;
  42. return !!pthread_mutex_init(*mtx, NULL);
  43. case AV_LOCK_OBTAIN:
  44. return !!pthread_mutex_lock(*mtx);
  45. case AV_LOCK_RELEASE:
  46. return !!pthread_mutex_unlock(*mtx);
  47. case AV_LOCK_DESTROY:
  48. pthread_mutex_destroy(*mtx);
  49. av_freep(mtx);
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. /* Helper to create a dictionary according to the given pts. */
  55. static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
  56. {
  57. CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
  58. CFNumberRef value = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &i_pts);
  59. CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
  60. (const void **)&key,
  61. (const void **)&value,
  62. 1,
  63. &kCFTypeDictionaryKeyCallBacks,
  64. &kCFTypeDictionaryValueCallBacks);
  65. CFRelease(value);
  66. return user_info;
  67. }
  68. /* Helper to retrieve the pts from the given dictionary. */
  69. static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
  70. {
  71. CFNumberRef pts;
  72. int64_t outValue = 0;
  73. if (!user_info)
  74. return 0;
  75. pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
  76. if (pts)
  77. CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
  78. return outValue;
  79. }
  80. /* Removes and releases all frames from the queue. */
  81. static void vda_clear_queue(struct vda_context *vda_ctx)
  82. {
  83. vda_frame *top_frame;
  84. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
  85. while (vda_ctx->queue) {
  86. top_frame = vda_ctx->queue;
  87. vda_ctx->queue = top_frame->next_frame;
  88. ff_vda_release_vda_frame(top_frame);
  89. }
  90. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
  91. }
  92. /* Decoder callback that adds the vda frame to the queue in display order. */
  93. static void vda_decoder_callback (void *vda_hw_ctx,
  94. CFDictionaryRef user_info,
  95. OSStatus status,
  96. uint32_t infoFlags,
  97. CVImageBufferRef image_buffer)
  98. {
  99. struct vda_context *vda_ctx = (struct vda_context*)vda_hw_ctx;
  100. vda_frame *new_frame;
  101. vda_frame *queue_walker;
  102. if (!image_buffer)
  103. return;
  104. if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
  105. return;
  106. new_frame = av_mallocz(sizeof(vda_frame));
  107. if (!new_frame)
  108. return;
  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. /* we have an empty queue, or this frame earlier than the current queue head */
  116. new_frame->next_frame = queue_walker;
  117. vda_ctx->queue = new_frame;
  118. } else {
  119. /* walk the queue and insert this frame where it belongs in display order */
  120. vda_frame *next_frame;
  121. while (1) {
  122. next_frame = queue_walker->next_frame;
  123. if (!next_frame || (new_frame->pts < next_frame->pts)) {
  124. new_frame->next_frame = next_frame;
  125. queue_walker->next_frame = new_frame;
  126. break;
  127. }
  128. queue_walker = next_frame;
  129. }
  130. }
  131. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
  132. }
  133. int ff_vda_create_decoder(struct vda_context *vda_ctx,
  134. uint8_t *extradata,
  135. int extradata_size)
  136. {
  137. OSStatus status = kVDADecoderNoErr;
  138. CFNumberRef height;
  139. CFNumberRef width;
  140. CFNumberRef format;
  141. CFDataRef avc_data;
  142. CFMutableDictionaryRef config_info;
  143. CFMutableDictionaryRef buffer_attributes;
  144. CFMutableDictionaryRef io_surface_properties;
  145. CFNumberRef cv_pix_fmt;
  146. vda_ctx->bitstream = NULL;
  147. vda_ctx->ref_size = 0;
  148. if (av_lockmgr_register(vda_lock_operation))
  149. return -1;
  150. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_CREATE);
  151. config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
  152. 4,
  153. &kCFTypeDictionaryKeyCallBacks,
  154. &kCFTypeDictionaryValueCallBacks);
  155. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  156. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  157. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  158. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  159. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  160. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  161. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  162. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  163. buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
  164. 2,
  165. &kCFTypeDictionaryKeyCallBacks,
  166. &kCFTypeDictionaryValueCallBacks);
  167. io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
  168. 0,
  169. &kCFTypeDictionaryKeyCallBacks,
  170. &kCFTypeDictionaryValueCallBacks);
  171. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  172. kCFNumberSInt32Type,
  173. &vda_ctx->cv_pix_fmt_type);
  174. CFDictionarySetValue(buffer_attributes,
  175. kCVPixelBufferPixelFormatTypeKey,
  176. cv_pix_fmt);
  177. CFDictionarySetValue(buffer_attributes,
  178. kCVPixelBufferIOSurfacePropertiesKey,
  179. io_surface_properties);
  180. status = VDADecoderCreate(config_info,
  181. buffer_attributes,
  182. (VDADecoderOutputCallback *)vda_decoder_callback,
  183. vda_ctx,
  184. &vda_ctx->decoder);
  185. CFRelease(height);
  186. CFRelease(width);
  187. CFRelease(format);
  188. CFRelease(avc_data);
  189. CFRelease(config_info);
  190. CFRelease(io_surface_properties);
  191. CFRelease(cv_pix_fmt);
  192. CFRelease(buffer_attributes);
  193. if (kVDADecoderNoErr != status)
  194. return status;
  195. return 0;
  196. }
  197. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  198. {
  199. OSStatus status = kVDADecoderNoErr;
  200. if (vda_ctx->decoder)
  201. status = VDADecoderDestroy(vda_ctx->decoder);
  202. vda_clear_queue(vda_ctx);
  203. if (vda_ctx->queue_mutex)
  204. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_DESTROY);
  205. if (vda_ctx->bitstream)
  206. av_freep(&vda_ctx->bitstream);
  207. if (kVDADecoderNoErr != status)
  208. return status;
  209. return 0;
  210. }
  211. vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
  212. {
  213. vda_frame *top_frame;
  214. if (!vda_ctx->queue)
  215. return NULL;
  216. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_OBTAIN);
  217. top_frame = vda_ctx->queue;
  218. vda_ctx->queue = top_frame->next_frame;
  219. vda_lock_operation(&vda_ctx->queue_mutex, AV_LOCK_RELEASE);
  220. return top_frame;
  221. }
  222. void ff_vda_release_vda_frame(vda_frame *frame)
  223. {
  224. if (frame) {
  225. CVPixelBufferRelease(frame->cv_buffer);
  226. av_freep(&frame);
  227. }
  228. }
  229. int ff_vda_decoder_decode(struct vda_context *vda_ctx,
  230. uint8_t *bitstream,
  231. int bitstream_size,
  232. int64_t frame_pts)
  233. {
  234. OSStatus status = kVDADecoderNoErr;
  235. CFDictionaryRef user_info;
  236. CFDataRef coded_frame;
  237. coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
  238. user_info = vda_dictionary_with_pts(frame_pts);
  239. status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
  240. CFRelease(user_info);
  241. CFRelease(coded_frame);
  242. if (kVDADecoderNoErr != status)
  243. return status;
  244. return 0;
  245. }
  246. /* @} */