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.

276 lines
8.9KB

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