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.

290 lines
9.4KB

  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. /* Each VCL NAL in the bistream sent to the decoder
  125. * is preceeded by a 4 bytes length header.
  126. * Change the avcC atom header if needed, to signal headers of 4 bytes. */
  127. if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
  128. uint8_t *rw_extradata;
  129. if (!(rw_extradata = av_malloc(extradata_size)))
  130. return AVERROR(ENOMEM);
  131. memcpy(rw_extradata, extradata, extradata_size);
  132. rw_extradata[4] |= 0x03;
  133. avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
  134. av_freep(&rw_extradata);
  135. } else {
  136. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  137. }
  138. config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
  139. 4,
  140. &kCFTypeDictionaryKeyCallBacks,
  141. &kCFTypeDictionaryValueCallBacks);
  142. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  143. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  144. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  145. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  146. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  147. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  148. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  149. buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
  150. 2,
  151. &kCFTypeDictionaryKeyCallBacks,
  152. &kCFTypeDictionaryValueCallBacks);
  153. io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
  154. 0,
  155. &kCFTypeDictionaryKeyCallBacks,
  156. &kCFTypeDictionaryValueCallBacks);
  157. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  158. kCFNumberSInt32Type,
  159. &vda_ctx->cv_pix_fmt_type);
  160. CFDictionarySetValue(buffer_attributes,
  161. kCVPixelBufferPixelFormatTypeKey,
  162. cv_pix_fmt);
  163. CFDictionarySetValue(buffer_attributes,
  164. kCVPixelBufferIOSurfacePropertiesKey,
  165. io_surface_properties);
  166. status = VDADecoderCreate(config_info,
  167. buffer_attributes,
  168. (VDADecoderOutputCallback *)vda_decoder_callback,
  169. vda_ctx,
  170. &vda_ctx->decoder);
  171. CFRelease(height);
  172. CFRelease(width);
  173. CFRelease(format);
  174. CFRelease(avc_data);
  175. CFRelease(config_info);
  176. CFRelease(io_surface_properties);
  177. CFRelease(cv_pix_fmt);
  178. CFRelease(buffer_attributes);
  179. if (kVDADecoderNoErr != status)
  180. return status;
  181. return 0;
  182. }
  183. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  184. {
  185. OSStatus status = kVDADecoderNoErr;
  186. if (vda_ctx->decoder)
  187. status = VDADecoderDestroy(vda_ctx->decoder);
  188. vda_clear_queue(vda_ctx);
  189. pthread_mutex_destroy(&vda_ctx->queue_mutex);
  190. if (vda_ctx->bitstream)
  191. av_freep(&vda_ctx->bitstream);
  192. if (kVDADecoderNoErr != status)
  193. return status;
  194. return 0;
  195. }
  196. vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
  197. {
  198. vda_frame *top_frame;
  199. if (!vda_ctx->queue)
  200. return NULL;
  201. pthread_mutex_lock(&vda_ctx->queue_mutex);
  202. top_frame = vda_ctx->queue;
  203. vda_ctx->queue = top_frame->next_frame;
  204. pthread_mutex_unlock(&vda_ctx->queue_mutex);
  205. return top_frame;
  206. }
  207. void ff_vda_release_vda_frame(vda_frame *frame)
  208. {
  209. if (frame) {
  210. CVPixelBufferRelease(frame->cv_buffer);
  211. av_freep(&frame);
  212. }
  213. }
  214. int ff_vda_decoder_decode(struct vda_context *vda_ctx,
  215. uint8_t *bitstream,
  216. int bitstream_size,
  217. int64_t frame_pts)
  218. {
  219. OSStatus status = kVDADecoderNoErr;
  220. CFDictionaryRef user_info;
  221. CFDataRef coded_frame;
  222. coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
  223. user_info = vda_dictionary_with_pts(frame_pts);
  224. status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
  225. CFRelease(user_info);
  226. CFRelease(coded_frame);
  227. if (kVDADecoderNoErr != status)
  228. return status;
  229. return 0;
  230. }