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.

279 lines
9.3KB

  1. /*
  2. * VDA hardware acceleration
  3. *
  4. * copyright (c) 2011 Sebastien Zwickert
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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,
  34. kCFNumberSInt64Type, &i_pts);
  35. CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
  36. (const void **)&key,
  37. (const void **)&value,
  38. 1,
  39. &kCFTypeDictionaryKeyCallBacks,
  40. &kCFTypeDictionaryValueCallBacks);
  41. CFRelease(value);
  42. return user_info;
  43. }
  44. /* helper to retrieve the pts from the given dictionary */
  45. static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
  46. {
  47. CFNumberRef pts;
  48. int64_t outValue = 0;
  49. if (!user_info)
  50. return 0;
  51. pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
  52. if (pts)
  53. CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
  54. return outValue;
  55. }
  56. /* Remove and release all frames from the queue. */
  57. static void vda_clear_queue(struct vda_context *vda_ctx)
  58. {
  59. vda_frame *top_frame;
  60. pthread_mutex_lock(&vda_ctx->queue_mutex);
  61. while (vda_ctx->queue) {
  62. top_frame = vda_ctx->queue;
  63. vda_ctx->queue = top_frame->next_frame;
  64. ff_vda_release_vda_frame(top_frame);
  65. }
  66. pthread_mutex_unlock(&vda_ctx->queue_mutex);
  67. }
  68. /* Decoder callback that adds the VDA frame to the queue in display order. */
  69. static void vda_decoder_callback(void *vda_hw_ctx,
  70. CFDictionaryRef user_info,
  71. OSStatus status,
  72. uint32_t infoFlags,
  73. CVImageBufferRef image_buffer)
  74. {
  75. struct vda_context *vda_ctx = vda_hw_ctx;
  76. vda_frame *new_frame;
  77. vda_frame *queue_walker;
  78. if (!image_buffer)
  79. return;
  80. if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
  81. return;
  82. if (!(new_frame = av_mallocz(sizeof(vda_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. pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
  122. /* Each VCL NAL in the bistream sent to the decoder
  123. * is preceeded by a 4 bytes length header.
  124. * Change the avcC atom header if needed, to signal headers of 4 bytes. */
  125. if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
  126. uint8_t *rw_extradata;
  127. if (!(rw_extradata = av_malloc(extradata_size)))
  128. return AVERROR(ENOMEM);
  129. memcpy(rw_extradata, extradata, extradata_size);
  130. rw_extradata[4] |= 0x03;
  131. avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
  132. av_freep(&rw_extradata);
  133. } else {
  134. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  135. }
  136. config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
  137. 4,
  138. &kCFTypeDictionaryKeyCallBacks,
  139. &kCFTypeDictionaryValueCallBacks);
  140. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  141. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  142. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  143. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  144. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  145. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  146. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  147. buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
  148. 2,
  149. &kCFTypeDictionaryKeyCallBacks,
  150. &kCFTypeDictionaryValueCallBacks);
  151. io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
  152. 0,
  153. &kCFTypeDictionaryKeyCallBacks,
  154. &kCFTypeDictionaryValueCallBacks);
  155. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  156. kCFNumberSInt32Type,
  157. &vda_ctx->cv_pix_fmt_type);
  158. CFDictionarySetValue(buffer_attributes,
  159. kCVPixelBufferPixelFormatTypeKey,
  160. cv_pix_fmt);
  161. CFDictionarySetValue(buffer_attributes,
  162. kCVPixelBufferIOSurfacePropertiesKey,
  163. io_surface_properties);
  164. status = VDADecoderCreate(config_info,
  165. buffer_attributes,
  166. vda_decoder_callback,
  167. vda_ctx,
  168. &vda_ctx->decoder);
  169. CFRelease(height);
  170. CFRelease(width);
  171. CFRelease(format);
  172. CFRelease(avc_data);
  173. CFRelease(config_info);
  174. CFRelease(io_surface_properties);
  175. CFRelease(cv_pix_fmt);
  176. CFRelease(buffer_attributes);
  177. if (kVDADecoderNoErr != status)
  178. return status;
  179. return 0;
  180. }
  181. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  182. {
  183. OSStatus status = kVDADecoderNoErr;
  184. if (vda_ctx->decoder)
  185. status = VDADecoderDestroy(vda_ctx->decoder);
  186. vda_clear_queue(vda_ctx);
  187. pthread_mutex_destroy(&vda_ctx->queue_mutex);
  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. }