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.

275 lines
9.3KB

  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. /* Decoder callback that adds the VDA frame to the queue in display order. */
  29. static void vda_decoder_callback(void *vda_hw_ctx,
  30. CFDictionaryRef user_info,
  31. OSStatus status,
  32. uint32_t infoFlags,
  33. CVImageBufferRef image_buffer)
  34. {
  35. struct vda_context *vda_ctx = vda_hw_ctx;
  36. if (!image_buffer)
  37. return;
  38. if (vda_ctx->cv_pix_fmt_type != CVPixelBufferGetPixelFormatType(image_buffer))
  39. return;
  40. if (vda_ctx->use_sync_decoding) {
  41. vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
  42. } else {
  43. vda_frame *new_frame;
  44. vda_frame *queue_walker;
  45. if (!(new_frame = av_mallocz(sizeof(*new_frame))))
  46. return;
  47. new_frame->next_frame = NULL;
  48. new_frame->cv_buffer = CVPixelBufferRetain(image_buffer);
  49. new_frame->pts = vda_pts_from_dictionary(user_info);
  50. pthread_mutex_lock(&vda_ctx->queue_mutex);
  51. queue_walker = vda_ctx->queue;
  52. if (!queue_walker || new_frame->pts < queue_walker->pts) {
  53. /* we have an empty queue, or this frame earlier than the current queue head */
  54. new_frame->next_frame = queue_walker;
  55. vda_ctx->queue = new_frame;
  56. } else {
  57. /* walk the queue and insert this frame where it belongs in display order */
  58. vda_frame *next_frame;
  59. while (1) {
  60. next_frame = queue_walker->next_frame;
  61. if (!next_frame || new_frame->pts < next_frame->pts) {
  62. new_frame->next_frame = next_frame;
  63. queue_walker->next_frame = new_frame;
  64. break;
  65. }
  66. queue_walker = next_frame;
  67. }
  68. }
  69. pthread_mutex_unlock(&vda_ctx->queue_mutex);
  70. }
  71. }
  72. static int vda_sync_decode(struct vda_context *vda_ctx)
  73. {
  74. OSStatus status;
  75. CFDataRef coded_frame;
  76. uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
  77. coded_frame = CFDataCreate(kCFAllocatorDefault,
  78. vda_ctx->priv_bitstream,
  79. vda_ctx->priv_bitstream_size);
  80. status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
  81. if (kVDADecoderNoErr == status)
  82. status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
  83. CFRelease(coded_frame);
  84. return status;
  85. }
  86. static int vda_h264_start_frame(AVCodecContext *avctx,
  87. av_unused const uint8_t *buffer,
  88. av_unused uint32_t size)
  89. {
  90. struct vda_context *vda_ctx = avctx->hwaccel_context;
  91. if (!vda_ctx->decoder)
  92. return -1;
  93. vda_ctx->priv_bitstream_size = 0;
  94. return 0;
  95. }
  96. static int vda_h264_decode_slice(AVCodecContext *avctx,
  97. const uint8_t *buffer,
  98. uint32_t size)
  99. {
  100. struct vda_context *vda_ctx = avctx->hwaccel_context;
  101. void *tmp;
  102. if (!vda_ctx->decoder)
  103. return -1;
  104. tmp = av_fast_realloc(vda_ctx->priv_bitstream,
  105. &vda_ctx->priv_allocated_size,
  106. vda_ctx->priv_bitstream_size + size + 4);
  107. if (!tmp)
  108. return AVERROR(ENOMEM);
  109. vda_ctx->priv_bitstream = tmp;
  110. AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size);
  111. memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size);
  112. vda_ctx->priv_bitstream_size += size + 4;
  113. return 0;
  114. }
  115. static int vda_h264_end_frame(AVCodecContext *avctx)
  116. {
  117. H264Context *h = avctx->priv_data;
  118. struct vda_context *vda_ctx = avctx->hwaccel_context;
  119. AVFrame *frame = &h->cur_pic_ptr->f;
  120. int status;
  121. if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
  122. return -1;
  123. if (vda_ctx->use_sync_decoding) {
  124. status = vda_sync_decode(vda_ctx);
  125. frame->data[3] = (void*)vda_ctx->cv_buffer;
  126. } else {
  127. status = vda_decoder_decode(vda_ctx, vda_ctx->priv_bitstream,
  128. vda_ctx->priv_bitstream_size,
  129. frame->reordered_opaque);
  130. }
  131. if (status)
  132. av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
  133. return status;
  134. }
  135. int ff_vda_create_decoder(struct vda_context *vda_ctx,
  136. uint8_t *extradata,
  137. int extradata_size)
  138. {
  139. OSStatus status = kVDADecoderNoErr;
  140. CFNumberRef height;
  141. CFNumberRef width;
  142. CFNumberRef format;
  143. CFDataRef avc_data;
  144. CFMutableDictionaryRef config_info;
  145. CFMutableDictionaryRef buffer_attributes;
  146. CFMutableDictionaryRef io_surface_properties;
  147. CFNumberRef cv_pix_fmt;
  148. /* Each VCL NAL in the bistream sent to the decoder
  149. * is preceded by a 4 bytes length header.
  150. * Change the avcC atom header if needed, to signal headers of 4 bytes. */
  151. if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
  152. uint8_t *rw_extradata;
  153. if (!(rw_extradata = av_malloc(extradata_size)))
  154. return AVERROR(ENOMEM);
  155. memcpy(rw_extradata, extradata, extradata_size);
  156. rw_extradata[4] |= 0x03;
  157. avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
  158. av_freep(&rw_extradata);
  159. } else {
  160. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  161. }
  162. config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
  163. 4,
  164. &kCFTypeDictionaryKeyCallBacks,
  165. &kCFTypeDictionaryValueCallBacks);
  166. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  167. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  168. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  169. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  170. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  171. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  172. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  173. buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
  174. 2,
  175. &kCFTypeDictionaryKeyCallBacks,
  176. &kCFTypeDictionaryValueCallBacks);
  177. io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
  178. 0,
  179. &kCFTypeDictionaryKeyCallBacks,
  180. &kCFTypeDictionaryValueCallBacks);
  181. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  182. kCFNumberSInt32Type,
  183. &vda_ctx->cv_pix_fmt_type);
  184. CFDictionarySetValue(buffer_attributes,
  185. kCVPixelBufferPixelFormatTypeKey,
  186. cv_pix_fmt);
  187. CFDictionarySetValue(buffer_attributes,
  188. kCVPixelBufferIOSurfacePropertiesKey,
  189. io_surface_properties);
  190. status = VDADecoderCreate(config_info,
  191. buffer_attributes,
  192. vda_decoder_callback,
  193. vda_ctx,
  194. &vda_ctx->decoder);
  195. CFRelease(height);
  196. CFRelease(width);
  197. CFRelease(format);
  198. CFRelease(avc_data);
  199. CFRelease(config_info);
  200. CFRelease(io_surface_properties);
  201. CFRelease(cv_pix_fmt);
  202. CFRelease(buffer_attributes);
  203. return status;
  204. }
  205. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  206. {
  207. OSStatus status = kVDADecoderNoErr;
  208. if (vda_ctx->decoder)
  209. status = VDADecoderDestroy(vda_ctx->decoder);
  210. av_freep(&vda_ctx->priv_bitstream);
  211. return status;
  212. }
  213. AVHWAccel ff_h264_vda_hwaccel = {
  214. .name = "h264_vda",
  215. .type = AVMEDIA_TYPE_VIDEO,
  216. .id = AV_CODEC_ID_H264,
  217. .pix_fmt = AV_PIX_FMT_VDA_VLD,
  218. .start_frame = vda_h264_start_frame,
  219. .decode_slice = vda_h264_decode_slice,
  220. .end_frame = vda_h264_end_frame,
  221. };