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.

238 lines
7.8KB

  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. 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;
  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. vda_ctx->priv_bitstream = NULL;
  113. vda_ctx->priv_allocated_size = 0;
  114. /* Each VCL NAL in the bitstream sent to the decoder
  115. * is preceded by a 4 bytes length header.
  116. * Change the avcC atom header if needed, to signal headers of 4 bytes. */
  117. if (extradata_size >= 4 && (extradata[4] & 0x03) != 0x03) {
  118. uint8_t *rw_extradata;
  119. if (!(rw_extradata = av_malloc(extradata_size)))
  120. return AVERROR(ENOMEM);
  121. memcpy(rw_extradata, extradata, extradata_size);
  122. rw_extradata[4] |= 0x03;
  123. avc_data = CFDataCreate(kCFAllocatorDefault, rw_extradata, extradata_size);
  124. av_freep(&rw_extradata);
  125. } else {
  126. avc_data = CFDataCreate(kCFAllocatorDefault, extradata, extradata_size);
  127. }
  128. config_info = CFDictionaryCreateMutable(kCFAllocatorDefault,
  129. 4,
  130. &kCFTypeDictionaryKeyCallBacks,
  131. &kCFTypeDictionaryValueCallBacks);
  132. height = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->height);
  133. width = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->width);
  134. format = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &vda_ctx->format);
  135. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Height, height);
  136. CFDictionarySetValue(config_info, kVDADecoderConfiguration_Width, width);
  137. CFDictionarySetValue(config_info, kVDADecoderConfiguration_SourceFormat, format);
  138. CFDictionarySetValue(config_info, kVDADecoderConfiguration_avcCData, avc_data);
  139. buffer_attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
  140. 2,
  141. &kCFTypeDictionaryKeyCallBacks,
  142. &kCFTypeDictionaryValueCallBacks);
  143. io_surface_properties = CFDictionaryCreateMutable(kCFAllocatorDefault,
  144. 0,
  145. &kCFTypeDictionaryKeyCallBacks,
  146. &kCFTypeDictionaryValueCallBacks);
  147. cv_pix_fmt = CFNumberCreate(kCFAllocatorDefault,
  148. kCFNumberSInt32Type,
  149. &vda_ctx->cv_pix_fmt_type);
  150. CFDictionarySetValue(buffer_attributes,
  151. kCVPixelBufferPixelFormatTypeKey,
  152. cv_pix_fmt);
  153. CFDictionarySetValue(buffer_attributes,
  154. kCVPixelBufferIOSurfacePropertiesKey,
  155. io_surface_properties);
  156. status = VDADecoderCreate(config_info,
  157. buffer_attributes,
  158. vda_decoder_callback,
  159. vda_ctx,
  160. &vda_ctx->decoder);
  161. CFRelease(height);
  162. CFRelease(width);
  163. CFRelease(format);
  164. CFRelease(avc_data);
  165. CFRelease(config_info);
  166. CFRelease(io_surface_properties);
  167. CFRelease(cv_pix_fmt);
  168. CFRelease(buffer_attributes);
  169. return status;
  170. }
  171. int ff_vda_destroy_decoder(struct vda_context *vda_ctx)
  172. {
  173. OSStatus status = kVDADecoderNoErr;
  174. if (vda_ctx->decoder)
  175. status = VDADecoderDestroy(vda_ctx->decoder);
  176. av_freep(&vda_ctx->priv_bitstream);
  177. return status;
  178. }
  179. AVHWAccel ff_h264_vda_hwaccel = {
  180. .name = "h264_vda",
  181. .type = AVMEDIA_TYPE_VIDEO,
  182. .id = AV_CODEC_ID_H264,
  183. .pix_fmt = AV_PIX_FMT_VDA_VLD,
  184. .start_frame = vda_h264_start_frame,
  185. .decode_slice = vda_h264_decode_slice,
  186. .end_frame = vda_h264_end_frame,
  187. };