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.

312 lines
10KB

  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 <CoreFoundation/CFDictionary.h>
  23. #include <CoreFoundation/CFNumber.h>
  24. #include <CoreFoundation/CFData.h>
  25. #include "libavutil/avutil.h"
  26. #include "vda_internal.h"
  27. #if FF_API_VDA_ASYNC
  28. #include <CoreFoundation/CFString.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. #endif
  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 = (struct vda_context*)vda_hw_ctx;
  76. if (!image_buffer)
  77. return;
  78. if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
  79. return;
  80. if (vda_ctx->use_sync_decoding) {
  81. vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
  82. }
  83. else {
  84. vda_frame *new_frame;
  85. vda_frame *queue_walker;
  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. }
  114. int ff_vda_create_decoder(struct vda_context *vda_ctx,
  115. uint8_t *extradata,
  116. int extradata_size)
  117. {
  118. OSStatus status;
  119. CFNumberRef height;
  120. CFNumberRef width;
  121. CFNumberRef format;
  122. CFDataRef avc_data;
  123. CFMutableDictionaryRef config_info;
  124. CFMutableDictionaryRef buffer_attributes;
  125. CFMutableDictionaryRef io_surface_properties;
  126. CFNumberRef cv_pix_fmt;
  127. vda_ctx->bitstream = NULL;
  128. vda_ctx->ref_size = 0;
  129. #if FF_API_VDA_ASYNC
  130. pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
  131. #endif
  132. /* Each VCL NAL in the bistream sent to the decoder
  133. * is preceded by a 4 bytes length header.
  134. * Change the avcC atom header if needed, to signal headers of 4 bytes. */
  135. if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
  136. uint8_t *rw_extradata;
  137. if (!(rw_extradata = av_malloc(extradata_size)))
  138. return AVERROR(ENOMEM);
  139. memcpy(rw_extradata, extradata, extradata_size);
  140. rw_extradata[4] |= 0x03;
  141. avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
  142. av_freep(&rw_extradata);
  143. } else {
  144. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  145. }
  146. config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
  147. 4,
  148. &kCFTypeDictionaryKeyCallBacks,
  149. &kCFTypeDictionaryValueCallBacks);
  150. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  151. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  152. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  153. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  154. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  155. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  156. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  157. buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
  158. 2,
  159. &kCFTypeDictionaryKeyCallBacks,
  160. &kCFTypeDictionaryValueCallBacks);
  161. io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
  162. 0,
  163. &kCFTypeDictionaryKeyCallBacks,
  164. &kCFTypeDictionaryValueCallBacks);
  165. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  166. kCFNumberSInt32Type,
  167. &vda_ctx->cv_pix_fmt_type);
  168. CFDictionarySetValue(buffer_attributes,
  169. kCVPixelBufferPixelFormatTypeKey,
  170. cv_pix_fmt);
  171. CFDictionarySetValue(buffer_attributes,
  172. kCVPixelBufferIOSurfacePropertiesKey,
  173. io_surface_properties);
  174. status = VDADecoderCreate(config_info,
  175. buffer_attributes,
  176. (VDADecoderOutputCallback *)vda_decoder_callback,
  177. vda_ctx,
  178. &vda_ctx->decoder);
  179. CFRelease(height);
  180. CFRelease(width);
  181. CFRelease(format);
  182. CFRelease(avc_data);
  183. CFRelease(config_info);
  184. CFRelease(io_surface_properties);
  185. CFRelease(cv_pix_fmt);
  186. CFRelease(buffer_attributes);
  187. return status;
  188. }
  189. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  190. {
  191. OSStatus status = kVDADecoderNoErr;
  192. if (vda_ctx->decoder)
  193. status = VDADecoderDestroy(vda_ctx->decoder);
  194. #if FF_API_VDA_ASYNC
  195. vda_clear_queue(vda_ctx);
  196. pthread_mutex_destroy(&vda_ctx->queue_mutex);
  197. #endif
  198. if (vda_ctx->bitstream)
  199. av_freep(&vda_ctx->bitstream);
  200. return status;
  201. }
  202. #if FF_API_VDA_ASYNC
  203. vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
  204. {
  205. vda_frame *top_frame;
  206. if (!vda_ctx->queue)
  207. return NULL;
  208. pthread_mutex_lock(&vda_ctx->queue_mutex);
  209. top_frame = vda_ctx->queue;
  210. vda_ctx->queue = top_frame->next_frame;
  211. pthread_mutex_unlock(&vda_ctx->queue_mutex);
  212. return top_frame;
  213. }
  214. void ff_vda_release_vda_frame(vda_frame *frame)
  215. {
  216. if (frame) {
  217. CVPixelBufferRelease(frame->cv_buffer);
  218. av_freep(&frame);
  219. }
  220. }
  221. int ff_vda_decoder_decode(struct vda_context *vda_ctx,
  222. uint8_t *bitstream,
  223. int bitstream_size,
  224. int64_t frame_pts)
  225. {
  226. OSStatus status;
  227. CFDictionaryRef user_info;
  228. CFDataRef coded_frame;
  229. coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
  230. user_info = vda_dictionary_with_pts(frame_pts);
  231. status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
  232. CFRelease(user_info);
  233. CFRelease(coded_frame);
  234. return status;
  235. }
  236. #endif
  237. int ff_vda_sync_decode(struct vda_context *vda_ctx)
  238. {
  239. OSStatus status;
  240. CFDataRef coded_frame;
  241. uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
  242. coded_frame = CFDataCreate(kCFAllocatorDefault,
  243. vda_ctx->bitstream,
  244. vda_ctx->bitstream_size);
  245. status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
  246. if (kVDADecoderNoErr == status)
  247. status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
  248. CFRelease(coded_frame);
  249. return status;
  250. }