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.

234 lines
7.8KB

  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. vda_ctx->cv_buffer = CVPixelBufferRetain(image_buffer);
  41. }
  42. static int vda_sync_decode(struct vda_context *vda_ctx)
  43. {
  44. OSStatus status;
  45. CFDataRef coded_frame;
  46. uint32_t flush_flags = 1 << 0; ///< kVDADecoderFlush_emitFrames
  47. coded_frame = CFDataCreate(kCFAllocatorDefault,
  48. vda_ctx->priv_bitstream,
  49. vda_ctx->priv_bitstream_size);
  50. status = VDADecoderDecode(vda_ctx->decoder, 0, coded_frame, NULL);
  51. if (kVDADecoderNoErr == status)
  52. status = VDADecoderFlush(vda_ctx->decoder, flush_flags);
  53. CFRelease(coded_frame);
  54. return status;
  55. }
  56. static int vda_h264_start_frame(AVCodecContext *avctx,
  57. av_unused const uint8_t *buffer,
  58. av_unused uint32_t size)
  59. {
  60. struct vda_context *vda_ctx = avctx->hwaccel_context;
  61. if (!vda_ctx->decoder)
  62. return -1;
  63. vda_ctx->priv_bitstream_size = 0;
  64. return 0;
  65. }
  66. static int vda_h264_decode_slice(AVCodecContext *avctx,
  67. const uint8_t *buffer,
  68. uint32_t size)
  69. {
  70. struct vda_context *vda_ctx = avctx->hwaccel_context;
  71. void *tmp;
  72. if (!vda_ctx->decoder)
  73. return -1;
  74. tmp = av_fast_realloc(vda_ctx->priv_bitstream,
  75. &vda_ctx->priv_allocated_size,
  76. vda_ctx->priv_bitstream_size + size + 4);
  77. if (!tmp)
  78. return AVERROR(ENOMEM);
  79. vda_ctx->priv_bitstream = tmp;
  80. AV_WB32(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size, size);
  81. memcpy(vda_ctx->priv_bitstream + vda_ctx->priv_bitstream_size + 4, buffer, size);
  82. vda_ctx->priv_bitstream_size += size + 4;
  83. return 0;
  84. }
  85. static int vda_h264_end_frame(AVCodecContext *avctx)
  86. {
  87. H264Context *h = avctx->priv_data;
  88. struct vda_context *vda_ctx = avctx->hwaccel_context;
  89. AVFrame *frame = &h->cur_pic_ptr->f;
  90. int status;
  91. if (!vda_ctx->decoder || !vda_ctx->priv_bitstream)
  92. return -1;
  93. status = vda_sync_decode(vda_ctx);
  94. frame->data[3] = (void*)vda_ctx->cv_buffer;
  95. if (status)
  96. av_log(avctx, AV_LOG_ERROR, "Failed to decode frame (%d)\n", status);
  97. return status;
  98. }
  99. int ff_vda_create_decoder(struct vda_context *vda_ctx,
  100. uint8_t *extradata,
  101. int extradata_size)
  102. {
  103. OSStatus status = kVDADecoderNoErr;
  104. CFNumberRef height;
  105. CFNumberRef width;
  106. CFNumberRef format;
  107. CFDataRef avc_data;
  108. CFMutableDictionaryRef config_info;
  109. CFMutableDictionaryRef buffer_attributes;
  110. CFMutableDictionaryRef io_surface_properties;
  111. CFNumberRef cv_pix_fmt;
  112. /* Each VCL NAL in the bistream sent to the decoder
  113. * is preceded by a 4 bytes length header.
  114. * Change the avcC atom header if needed, to signal headers of 4 bytes. */
  115. if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
  116. uint8_t *rw_extradata;
  117. if (!(rw_extradata = av_malloc(extradata_size)))
  118. return AVERROR(ENOMEM);
  119. memcpy(rw_extradata, extradata, extradata_size);
  120. rw_extradata[4] |= 0x03;
  121. avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
  122. av_freep(&rw_extradata);
  123. } else {
  124. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  125. }
  126. config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
  127. 4,
  128. &kCFTypeDictionaryKeyCallBacks,
  129. &kCFTypeDictionaryValueCallBacks);
  130. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  131. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  132. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  133. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  134. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  135. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  136. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  137. buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
  138. 2,
  139. &kCFTypeDictionaryKeyCallBacks,
  140. &kCFTypeDictionaryValueCallBacks);
  141. io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
  142. 0,
  143. &kCFTypeDictionaryKeyCallBacks,
  144. &kCFTypeDictionaryValueCallBacks);
  145. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  146. kCFNumberSInt32Type,
  147. &vda_ctx->cv_pix_fmt_type);
  148. CFDictionarySetValue(buffer_attributes,
  149. kCVPixelBufferPixelFormatTypeKey,
  150. cv_pix_fmt);
  151. CFDictionarySetValue(buffer_attributes,
  152. kCVPixelBufferIOSurfacePropertiesKey,
  153. io_surface_properties);
  154. status = VDADecoderCreate(config_info,
  155. buffer_attributes,
  156. vda_decoder_callback,
  157. vda_ctx,
  158. &vda_ctx->decoder);
  159. CFRelease(height);
  160. CFRelease(width);
  161. CFRelease(format);
  162. CFRelease(avc_data);
  163. CFRelease(config_info);
  164. CFRelease(io_surface_properties);
  165. CFRelease(cv_pix_fmt);
  166. CFRelease(buffer_attributes);
  167. return status;
  168. }
  169. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  170. {
  171. OSStatus status = kVDADecoderNoErr;
  172. if (vda_ctx->decoder)
  173. status = VDADecoderDestroy(vda_ctx->decoder);
  174. av_freep(&vda_ctx->priv_bitstream);
  175. return status;
  176. }
  177. AVHWAccel ff_h264_vda_hwaccel = {
  178. .name = "h264_vda",
  179. .type = AVMEDIA_TYPE_VIDEO,
  180. .id = AV_CODEC_ID_H264,
  181. .pix_fmt = AV_PIX_FMT_VDA_VLD,
  182. .start_frame = vda_h264_start_frame,
  183. .decode_slice = vda_h264_decode_slice,
  184. .end_frame = vda_h264_end_frame,
  185. };