hwcontext_videotoolbox.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <VideoToolbox/VideoToolbox.h>
  22. #include "buffer.h"
  23. #include "common.h"
  24. #include "hwcontext.h"
  25. #include "hwcontext_internal.h"
  26. #include "hwcontext_videotoolbox.h"
  27. #include "mem.h"
  28. #include "pixfmt.h"
  29. #include "pixdesc.h"
  30. static const struct {
  31. uint32_t cv_fmt;
  32. enum AVPixelFormat pix_fmt;
  33. } cv_pix_fmts[] = {
  34. { kCVPixelFormatType_420YpCbCr8Planar, AV_PIX_FMT_YUV420P },
  35. { kCVPixelFormatType_422YpCbCr8, AV_PIX_FMT_UYVY422 },
  36. { kCVPixelFormatType_32BGRA, AV_PIX_FMT_BGRA },
  37. #ifdef kCFCoreFoundationVersionNumber10_7
  38. { kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, AV_PIX_FMT_NV12 },
  39. #endif
  40. #if HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE
  41. { kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange, AV_PIX_FMT_P010 },
  42. #endif
  43. };
  44. enum AVPixelFormat av_map_videotoolbox_format_to_pixfmt(uint32_t cv_fmt)
  45. {
  46. int i;
  47. for (i = 0; i < FF_ARRAY_ELEMS(cv_pix_fmts); i++) {
  48. if (cv_pix_fmts[i].cv_fmt == cv_fmt)
  49. return cv_pix_fmts[i].pix_fmt;
  50. }
  51. return AV_PIX_FMT_NONE;
  52. }
  53. uint32_t av_map_videotoolbox_format_from_pixfmt(enum AVPixelFormat pix_fmt)
  54. {
  55. int i;
  56. for (i = 0; i < FF_ARRAY_ELEMS(cv_pix_fmts); i++) {
  57. if (cv_pix_fmts[i].pix_fmt == pix_fmt)
  58. return cv_pix_fmts[i].cv_fmt;
  59. }
  60. return 0;
  61. }
  62. static int vt_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
  63. {
  64. frame->buf[0] = av_buffer_pool_get(ctx->pool);
  65. if (!frame->buf[0])
  66. return AVERROR(ENOMEM);
  67. frame->data[3] = frame->buf[0]->data;
  68. frame->format = AV_PIX_FMT_VIDEOTOOLBOX;
  69. frame->width = ctx->width;
  70. frame->height = ctx->height;
  71. return 0;
  72. }
  73. static int vt_transfer_get_formats(AVHWFramesContext *ctx,
  74. enum AVHWFrameTransferDirection dir,
  75. enum AVPixelFormat **formats)
  76. {
  77. enum AVPixelFormat *fmts = av_malloc_array(2, sizeof(*fmts));
  78. if (!fmts)
  79. return AVERROR(ENOMEM);
  80. fmts[0] = ctx->sw_format;
  81. fmts[1] = AV_PIX_FMT_NONE;
  82. *formats = fmts;
  83. return 0;
  84. }
  85. static void vt_unmap(AVHWFramesContext *ctx, HWMapDescriptor *hwmap)
  86. {
  87. CVPixelBufferRef pixbuf = (CVPixelBufferRef)hwmap->source->data[3];
  88. CVPixelBufferUnlockBaseAddress(pixbuf, (uintptr_t)hwmap->priv);
  89. }
  90. static int vt_map_frame(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src,
  91. int flags)
  92. {
  93. CVPixelBufferRef pixbuf = (CVPixelBufferRef)src->data[3];
  94. OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
  95. CVReturn err;
  96. uint32_t map_flags = 0;
  97. int ret;
  98. int i;
  99. enum AVPixelFormat format;
  100. format = av_map_videotoolbox_format_to_pixfmt(pixel_format);
  101. if (dst->format != format) {
  102. av_log(ctx, AV_LOG_ERROR, "Unsupported or mismatching pixel format: %s\n",
  103. av_fourcc2str(pixel_format));
  104. return AVERROR_UNKNOWN;
  105. }
  106. if (CVPixelBufferGetWidth(pixbuf) != ctx->width ||
  107. CVPixelBufferGetHeight(pixbuf) != ctx->height) {
  108. av_log(ctx, AV_LOG_ERROR, "Inconsistent frame dimensions.\n");
  109. return AVERROR_UNKNOWN;
  110. }
  111. if (flags == AV_HWFRAME_MAP_READ)
  112. map_flags = kCVPixelBufferLock_ReadOnly;
  113. err = CVPixelBufferLockBaseAddress(pixbuf, map_flags);
  114. if (err != kCVReturnSuccess) {
  115. av_log(ctx, AV_LOG_ERROR, "Error locking the pixel buffer.\n");
  116. return AVERROR_UNKNOWN;
  117. }
  118. if (CVPixelBufferIsPlanar(pixbuf)) {
  119. int planes = CVPixelBufferGetPlaneCount(pixbuf);
  120. for (i = 0; i < planes; i++) {
  121. dst->data[i] = CVPixelBufferGetBaseAddressOfPlane(pixbuf, i);
  122. dst->linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
  123. }
  124. } else {
  125. dst->data[0] = CVPixelBufferGetBaseAddress(pixbuf);
  126. dst->linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf);
  127. }
  128. ret = ff_hwframe_map_create(src->hw_frames_ctx, dst, src, vt_unmap,
  129. (void *)(uintptr_t)map_flags);
  130. if (ret < 0)
  131. goto unlock;
  132. return 0;
  133. unlock:
  134. CVPixelBufferUnlockBaseAddress(pixbuf, map_flags);
  135. return ret;
  136. }
  137. static int vt_transfer_data_from(AVHWFramesContext *hwfc,
  138. AVFrame *dst, const AVFrame *src)
  139. {
  140. AVFrame *map;
  141. int err;
  142. if (dst->width > hwfc->width || dst->height > hwfc->height)
  143. return AVERROR(EINVAL);
  144. map = av_frame_alloc();
  145. if (!map)
  146. return AVERROR(ENOMEM);
  147. map->format = dst->format;
  148. err = vt_map_frame(hwfc, map, src, AV_HWFRAME_MAP_READ);
  149. if (err)
  150. goto fail;
  151. map->width = dst->width;
  152. map->height = dst->height;
  153. err = av_frame_copy(dst, map);
  154. if (err)
  155. goto fail;
  156. err = 0;
  157. fail:
  158. av_frame_free(&map);
  159. return err;
  160. }
  161. static int vt_transfer_data_to(AVHWFramesContext *hwfc,
  162. AVFrame *dst, const AVFrame *src)
  163. {
  164. AVFrame *map;
  165. int err;
  166. if (src->width > hwfc->width || src->height > hwfc->height)
  167. return AVERROR(EINVAL);
  168. map = av_frame_alloc();
  169. if (!map)
  170. return AVERROR(ENOMEM);
  171. map->format = src->format;
  172. err = vt_map_frame(hwfc, map, dst, AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_OVERWRITE);
  173. if (err)
  174. goto fail;
  175. map->width = src->width;
  176. map->height = src->height;
  177. err = av_frame_copy(map, src);
  178. if (err)
  179. goto fail;
  180. err = 0;
  181. fail:
  182. av_frame_free(&map);
  183. return err;
  184. }
  185. static int vt_device_create(AVHWDeviceContext *ctx, const char *device,
  186. AVDictionary *opts, int flags)
  187. {
  188. if (device && device[0]) {
  189. av_log(ctx, AV_LOG_ERROR, "Device selection unsupported.\n");
  190. return AVERROR_UNKNOWN;
  191. }
  192. return 0;
  193. }
  194. const HWContextType ff_hwcontext_type_videotoolbox = {
  195. .type = AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
  196. .name = "videotoolbox",
  197. .device_create = vt_device_create,
  198. .frames_get_buffer = vt_get_buffer,
  199. .transfer_get_formats = vt_transfer_get_formats,
  200. .transfer_data_to = vt_transfer_data_to,
  201. .transfer_data_from = vt_transfer_data_from,
  202. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VIDEOTOOLBOX, AV_PIX_FMT_NONE },
  203. };