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.

379 lines
12KB

  1. /*
  2. * VDA H.264 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 <CoreFoundation/CFNumber.h>
  23. #include <CoreFoundation/CFData.h>
  24. #include <CoreFoundation/CFString.h>
  25. #include "libavutil/avutil.h"
  26. #include "h264.h"
  27. #include "vda.h"
  28. #if FF_API_VDA_ASYNC
  29. #include <CoreFoundation/CFDictionary.h>
  30. /* helper to create a dictionary according to the given pts */
  31. static CFDictionaryRef vda_dictionary_with_pts(int64_t i_pts)
  32. {
  33. CFStringRef key = CFSTR("FF_VDA_DECODER_PTS_KEY");
  34. CFNumberRef value = CFNumberCreate(kCFAllocatorDefault,
  35. kCFNumberSInt64Type, &i_pts);
  36. CFDictionaryRef user_info = CFDictionaryCreate(kCFAllocatorDefault,
  37. (const void **)&key,
  38. (const void **)&value,
  39. 1,
  40. &kCFTypeDictionaryKeyCallBacks,
  41. &kCFTypeDictionaryValueCallBacks);
  42. CFRelease(value);
  43. return user_info;
  44. }
  45. /* helper to retrieve the pts from the given dictionary */
  46. static int64_t vda_pts_from_dictionary(CFDictionaryRef user_info)
  47. {
  48. CFNumberRef pts;
  49. int64_t outValue = 0;
  50. if (!user_info)
  51. return 0;
  52. pts = CFDictionaryGetValue(user_info, CFSTR("FF_VDA_DECODER_PTS_KEY"));
  53. if (pts)
  54. CFNumberGetValue(pts, kCFNumberSInt64Type, &outValue);
  55. return outValue;
  56. }
  57. /* Remove and release all frames from the queue. */
  58. static void vda_clear_queue(struct vda_context *vda_ctx)
  59. {
  60. vda_frame *top_frame;
  61. pthread_mutex_lock(&vda_ctx->queue_mutex);
  62. while (vda_ctx->queue) {
  63. top_frame = vda_ctx->queue;
  64. vda_ctx->queue = top_frame->next_frame;
  65. ff_vda_release_vda_frame(top_frame);
  66. }
  67. pthread_mutex_unlock(&vda_ctx->queue_mutex);
  68. }
  69. static int vda_decoder_decode(struct vda_context *vda_ctx,
  70. uint8_t *bitstream,
  71. int bitstream_size,
  72. int64_t frame_pts)
  73. {
  74. OSStatus status = kVDADecoderNoErr;
  75. CFDictionaryRef user_info;
  76. CFDataRef coded_frame;
  77. coded_frame = CFDataCreate(kCFAllocatorDefault, bitstream, bitstream_size);
  78. user_info = vda_dictionary_with_pts(frame_pts);
  79. status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, user_info);
  80. CFRelease(user_info);
  81. CFRelease(coded_frame);
  82. return status;
  83. }
  84. vda_frame *ff_vda_queue_pop(struct vda_context *vda_ctx)
  85. {
  86. vda_frame *top_frame;
  87. if (!vda_ctx->queue)
  88. return NULL;
  89. pthread_mutex_lock(&vda_ctx->queue_mutex);
  90. top_frame = vda_ctx->queue;
  91. vda_ctx->queue = top_frame->next_frame;
  92. pthread_mutex_unlock(&vda_ctx->queue_mutex);
  93. return top_frame;
  94. }
  95. void ff_vda_release_vda_frame(vda_frame *frame)
  96. {
  97. if (frame) {
  98. CVPixelBufferRelease(frame->cv_buffer);
  99. av_freep(&frame);
  100. }
  101. }
  102. #endif
  103. /* Decoder callback that adds the VDA frame to the queue in display order. */
  104. static void vda_decoder_callback(void *vda_hw_ctx,
  105. CFDictionaryRef user_info,
  106. OSStatus status,
  107. uint32_t infoFlags,
  108. CVImageBufferRef image_buffer)
  109. {
  110. struct vda_context *vda_ctx = vda_hw_ctx;
  111. if (!image_buffer)
  112. return;
  113. if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
  114. return;
  115. if (vda_ctx->use_sync_decoding) {
  116. vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
  117. } else {
  118. vda_frame *new_frame;
  119. vda_frame *queue_walker;
  120. if (!(new_frame = av_mallocz(sizeof(*new_frame))))
  121. return;
  122. new_frame->next_frame = NULL;
  123. new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
  124. new_frame->pts = vda_pts_from_dictionary(user_info);
  125. pthread_mutex_lock(&vda_ctx->queue_mutex);
  126. queue_walker = vda_ctx->queue;
  127. if (!queue_walker || new_frame->pts < queue_walker->pts) {
  128. /* we have an empty queue, or this frame earlier than the current queue head */
  129. new_frame->next_frame = queue_walker;
  130. vda_ctx->queue = new_frame;
  131. } else {
  132. /* walk the queue and insert this frame where it belongs in display order */
  133. vda_frame *next_frame;
  134. while (1) {
  135. next_frame = queue_walker->next_frame;
  136. if (!next_frame || new_frame->pts < next_frame->pts) {
  137. new_frame->next_frame = next_frame;
  138. queue_walker->next_frame = new_frame;
  139. break;
  140. }
  141. queue_walker = next_frame;
  142. }
  143. }
  144. pthread_mutex_unlock(&vda_ctx->queue_mutex);
  145. }
  146. }
  147. static int vda_sync_decode(struct vda_context *vda_ctx)
  148. {
  149. OSStatus status;
  150. CFDataRef coded_frame;
  151. uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
  152. coded_frame = CFDataCreate(kCFAllocatorDefault,
  153. vda_ctx->priv_bitstream,
  154. vda_ctx->priv_bitstream_size);
  155. status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
  156. if (kVDADecoderNoErr == status)
  157. status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
  158. CFRelease(coded_frame);
  159. return status;
  160. }
  161. static int start_frame(AVCodecContext *avctx,
  162. av_unused const uint8_t *buffer,
  163. av_unused uint32_t size)
  164. {
  165. struct vda_context *vda_ctx = avctx->hwaccel_context;
  166. if (!vda_ctx->decoder)
  167. return -1;
  168. vda_ctx->priv_bitstream_size = 0;
  169. return 0;
  170. }
  171. static int decode_slice(AVCodecContext *avctx,
  172. const uint8_t *buffer,
  173. uint32_t size)
  174. {
  175. struct vda_context *vda_ctx = avctx->hwaccel_context;
  176. void *tmp;
  177. if (!vda_ctx->decoder)
  178. return -1;
  179. tmp = av_fast_realloc(vda_ctx->priv_bitstream,
  180. &vda_ctx->priv_allocated_size,
  181. vda_ctx->priv_bitstream_size + size + 4);
  182. if (!tmp)
  183. return AVERROR(ENOMEM);
  184. vda_ctx->priv_bitstream = tmp;
  185. AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size);
  186. memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size);
  187. vda_ctx->priv_bitstream_size += size + 4;
  188. return 0;
  189. }
  190. static int end_frame(AVCodecContext *avctx)
  191. {
  192. H264Context *h = avctx->priv_data;
  193. struct vda_context *vda_ctx = avctx->hwaccel_context;
  194. AVFrame *frame = &h->s.current_picture_ptr->f;
  195. int status;
  196. if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
  197. return -1;
  198. if (vda_ctx->use_sync_decoding) {
  199. status = vda_sync_decode(vda_ctx);
  200. frame->data[3] = (void*)vda_ctx->cv_buffer;
  201. } else {
  202. status = vda_decoder_decode(vda_ctx, vda_ctx->priv_bitstream,
  203. vda_ctx->priv_bitstream_size,
  204. frame->reordered_opaque);
  205. }
  206. if (status)
  207. av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
  208. return status;
  209. }
  210. int ff_vda_create_decoder(struct vda_context *vda_ctx,
  211. uint8_t *extradata,
  212. int extradata_size)
  213. {
  214. OSStatus status = kVDADecoderNoErr;
  215. CFNumberRef height;
  216. CFNumberRef width;
  217. CFNumberRef format;
  218. CFDataRef avc_data;
  219. CFMutableDictionaryRef config_info;
  220. CFMutableDictionaryRef buffer_attributes;
  221. CFMutableDictionaryRef io_surface_properties;
  222. CFNumberRef cv_pix_fmt;
  223. #if FF_API_VDA_ASYNC
  224. pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
  225. #endif
  226. /* Each VCL NAL in the bistream sent to the decoder
  227. * is preceeded by a 4 bytes length header.
  228. * Change the avcC atom header if needed, to signal headers of 4 bytes. */
  229. if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
  230. uint8_t *rw_extradata;
  231. if (!(rw_extradata = av_malloc(extradata_size)))
  232. return AVERROR(ENOMEM);
  233. memcpy(rw_extradata, extradata, extradata_size);
  234. rw_extradata[4] |= 0x03;
  235. avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
  236. av_freep(&rw_extradata);
  237. } else {
  238. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  239. }
  240. config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
  241. 4,
  242. &kCFTypeDictionaryKeyCallBacks,
  243. &kCFTypeDictionaryValueCallBacks);
  244. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  245. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  246. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  247. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  248. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  249. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  250. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  251. buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
  252. 2,
  253. &kCFTypeDictionaryKeyCallBacks,
  254. &kCFTypeDictionaryValueCallBacks);
  255. io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
  256. 0,
  257. &kCFTypeDictionaryKeyCallBacks,
  258. &kCFTypeDictionaryValueCallBacks);
  259. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  260. kCFNumberSInt32Type,
  261. &vda_ctx->cv_pix_fmt_type);
  262. CFDictionarySetValue(buffer_attributes,
  263. kCVPixelBufferPixelFormatTypeKey,
  264. cv_pix_fmt);
  265. CFDictionarySetValue(buffer_attributes,
  266. kCVPixelBufferIOSurfacePropertiesKey,
  267. io_surface_properties);
  268. status = VDADecoderCreate(config_info,
  269. buffer_attributes,
  270. vda_decoder_callback,
  271. vda_ctx,
  272. &vda_ctx->decoder);
  273. CFRelease(height);
  274. CFRelease(width);
  275. CFRelease(format);
  276. CFRelease(avc_data);
  277. CFRelease(config_info);
  278. CFRelease(io_surface_properties);
  279. CFRelease(cv_pix_fmt);
  280. CFRelease(buffer_attributes);
  281. return status;
  282. }
  283. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  284. {
  285. OSStatus status = kVDADecoderNoErr;
  286. if (vda_ctx->decoder)
  287. status = VDADecoderDestroy(vda_ctx->decoder);
  288. #if FF_API_VDA_ASYNC
  289. vda_clear_queue(vda_ctx);
  290. pthread_mutex_destroy(&vda_ctx->queue_mutex);
  291. #endif
  292. av_freep(&vda_ctx->priv_bitstream);
  293. return status;
  294. }
  295. AVHWAccel ff_h264_vda_hwaccel = {
  296. .name = "h264_vda",
  297. .type = AVMEDIA_TYPE_VIDEO,
  298. .id = AV_CODEC_ID_H264,
  299. .pix_fmt = AV_PIX_FMT_VDA_VLD,
  300. .start_frame = start_frame,
  301. .decode_slice = decode_slice,
  302. .end_frame = end_frame,
  303. };