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.

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