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.

282 lines
9.4KB

  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. /* 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;
  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. vda_ctx->priv_bitstream = NULL;
  149. vda_ctx->priv_allocated_size = 0;
  150. /* Each VCL NAL in the bitstream sent to the decoder
  151. * is preceded by a 4 bytes length header.
  152. * Change the avcC atom header if needed, to signal headers of 4 bytes. */
  153. if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
  154. uint8_t *rw_extradata;
  155. if (!(rw_extradata = av_malloc(extradata_size)))
  156. return AVERROR(ENOMEM);
  157. memcpy(rw_extradata, extradata, extradata_size);
  158. rw_extradata[4] |= 0x03;
  159. avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
  160. av_freep(&rw_extradata);
  161. } else {
  162. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  163. }
  164. config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
  165. 4,
  166. &kCFTypeDictionaryKeyCallBacks,
  167. &kCFTypeDictionaryValueCallBacks);
  168. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  169. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  170. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  171. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  172. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  173. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  174. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  175. buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
  176. 2,
  177. &kCFTypeDictionaryKeyCallBacks,
  178. &kCFTypeDictionaryValueCallBacks);
  179. io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
  180. 0,
  181. &kCFTypeDictionaryKeyCallBacks,
  182. &kCFTypeDictionaryValueCallBacks);
  183. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  184. kCFNumberSInt32Type,
  185. &vda_ctx->cv_pix_fmt_type);
  186. CFDictionarySetValue(buffer_attributes,
  187. kCVPixelBufferPixelFormatTypeKey,
  188. cv_pix_fmt);
  189. CFDictionarySetValue(buffer_attributes,
  190. kCVPixelBufferIOSurfacePropertiesKey,
  191. io_surface_properties);
  192. status = VDADecoderCreate(config_info,
  193. buffer_attributes,
  194. vda_decoder_callback,
  195. vda_ctx,
  196. &vda_ctx->decoder);
  197. CFRelease(height);
  198. CFRelease(width);
  199. CFRelease(format);
  200. CFRelease(avc_data);
  201. CFRelease(config_info);
  202. CFRelease(io_surface_properties);
  203. CFRelease(cv_pix_fmt);
  204. CFRelease(buffer_attributes);
  205. return status;
  206. }
  207. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  208. {
  209. OSStatus status = kVDADecoderNoErr;
  210. if (vda_ctx->decoder)
  211. status = VDADecoderDestroy(vda_ctx->decoder);
  212. av_freep(&vda_ctx->priv_bitstream);
  213. return status;
  214. }
  215. AVHWAccel ff_h264_vda_hwaccel = {
  216. .name = "h264_vda",
  217. .type = AVMEDIA_TYPE_VIDEO,
  218. .id = AV_CODEC_ID_H264,
  219. .pix_fmt = AV_PIX_FMT_VDA_VLD,
  220. .start_frame = vda_h264_start_frame,
  221. .decode_slice = vda_h264_decode_slice,
  222. .end_frame = vda_h264_end_frame,
  223. };