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.

383 lines
12KB

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