vda_h264.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 "libavutil/avutil.h"
  26. #include "h264.h"
  27. #include "vda.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. new_frame = av_mallocz(sizeof(vda_frame));
  120. if (!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;
  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. vda_ctx->priv_bitstream = NULL;
  224. vda_ctx->priv_allocated_size = 0;
  225. #if FF_API_VDA_ASYNC
  226. pthread_mutex_init(&vda_ctx->queue_mutex, NULL);
  227. #endif
  228. /* Each VCL NAL in the bistream sent to the decoder
  229. * is preceded by a 4 bytes length header.
  230. * Change the avcC atom header if needed, to signal headers of 4 bytes. */
  231. if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
  232. uint8_t *rw_extradata;
  233. if (!(rw_extradata = av_malloc(extradata_size)))
  234. return AVERROR(ENOMEM);
  235. memcpy(rw_extradata, extradata, extradata_size);
  236. rw_extradata[4] |= 0x03;
  237. avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
  238. av_freep(&rw_extradata);
  239. } else {
  240. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  241. }
  242. config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
  243. 4,
  244. &kCFTypeDictionaryKeyCallBacks,
  245. &kCFTypeDictionaryValueCallBacks);
  246. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  247. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  248. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  249. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  250. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  251. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  252. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  253. buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
  254. 2,
  255. &kCFTypeDictionaryKeyCallBacks,
  256. &kCFTypeDictionaryValueCallBacks);
  257. io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
  258. 0,
  259. &kCFTypeDictionaryKeyCallBacks,
  260. &kCFTypeDictionaryValueCallBacks);
  261. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  262. kCFNumberSInt32Type,
  263. &vda_ctx->cv_pix_fmt_type);
  264. CFDictionarySetValue(buffer_attributes,
  265. kCVPixelBufferPixelFormatTypeKey,
  266. cv_pix_fmt);
  267. CFDictionarySetValue(buffer_attributes,
  268. kCVPixelBufferIOSurfacePropertiesKey,
  269. io_surface_properties);
  270. status = VDADecoderCreate(config_info,
  271. buffer_attributes,
  272. vda_decoder_callback,
  273. vda_ctx,
  274. &vda_ctx->decoder);
  275. CFRelease(height);
  276. CFRelease(width);
  277. CFRelease(format);
  278. CFRelease(avc_data);
  279. CFRelease(config_info);
  280. CFRelease(io_surface_properties);
  281. CFRelease(cv_pix_fmt);
  282. CFRelease(buffer_attributes);
  283. return status;
  284. }
  285. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  286. {
  287. OSStatus status = kVDADecoderNoErr;
  288. if (vda_ctx->decoder)
  289. status = VDADecoderDestroy(vda_ctx->decoder);
  290. #if FF_API_VDA_ASYNC
  291. vda_clear_queue(vda_ctx);
  292. pthread_mutex_destroy(&vda_ctx->queue_mutex);
  293. #endif
  294. av_freep(&vda_ctx->priv_bitstream);
  295. return status;
  296. }
  297. AVHWAccel ff_h264_vda_hwaccel = {
  298. .name = "h264_vda",
  299. .type = AVMEDIA_TYPE_VIDEO,
  300. .id = AV_CODEC_ID_H264,
  301. .pix_fmt = PIX_FMT_VDA_VLD,
  302. .start_frame = start_frame,
  303. .decode_slice = decode_slice,
  304. .end_frame = end_frame,
  305. };