hwcontext_videotoolbox.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. };
  41. enum AVPixelFormat av_map_videotoolbox_format_to_pixfmt(uint32_t cv_fmt)
  42. {
  43. int i;
  44. for (i = 0; i < FF_ARRAY_ELEMS(cv_pix_fmts); i++) {
  45. if (cv_pix_fmts[i].cv_fmt == cv_fmt)
  46. return cv_pix_fmts[i].pix_fmt;
  47. }
  48. return AV_PIX_FMT_NONE;
  49. }
  50. uint32_t av_map_videotoolbox_format_from_pixfmt(enum AVPixelFormat pix_fmt)
  51. {
  52. int i;
  53. for (i = 0; i < FF_ARRAY_ELEMS(cv_pix_fmts); i++) {
  54. if (cv_pix_fmts[i].pix_fmt == pix_fmt)
  55. return cv_pix_fmts[i].cv_fmt;
  56. }
  57. return 0;
  58. }
  59. static int vt_get_buffer(AVHWFramesContext *ctx, AVFrame *frame)
  60. {
  61. frame->buf[0] = av_buffer_pool_get(ctx->pool);
  62. if (!frame->buf[0])
  63. return AVERROR(ENOMEM);
  64. frame->data[3] = frame->buf[0]->data;
  65. frame->format = AV_PIX_FMT_VIDEOTOOLBOX;
  66. frame->width = ctx->width;
  67. frame->height = ctx->height;
  68. return 0;
  69. }
  70. static int vt_transfer_get_formats(AVHWFramesContext *ctx,
  71. enum AVHWFrameTransferDirection dir,
  72. enum AVPixelFormat **formats)
  73. {
  74. enum AVPixelFormat *fmts = av_malloc_array(2, sizeof(*fmts));
  75. if (!fmts)
  76. return AVERROR(ENOMEM);
  77. fmts[0] = ctx->sw_format;
  78. fmts[1] = AV_PIX_FMT_NONE;
  79. *formats = fmts;
  80. return 0;
  81. }
  82. static void vt_unmap(AVHWFramesContext *ctx, HWMapDescriptor *hwmap)
  83. {
  84. CVPixelBufferRef pixbuf = (CVPixelBufferRef)hwmap->source->data[3];
  85. CVPixelBufferUnlockBaseAddress(pixbuf, (uintptr_t)hwmap->priv);
  86. }
  87. static int vt_map_frame(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src,
  88. int flags)
  89. {
  90. CVPixelBufferRef pixbuf = (CVPixelBufferRef)src->data[3];
  91. OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
  92. CVReturn err;
  93. uint32_t map_flags = 0;
  94. int ret;
  95. int i;
  96. enum AVPixelFormat format;
  97. format = av_map_videotoolbox_format_to_pixfmt(pixel_format);
  98. if (dst->format != format) {
  99. av_log(ctx, AV_LOG_ERROR, "Unsupported or mismatching pixel format: %s\n",
  100. av_fourcc2str(pixel_format));
  101. return AVERROR_UNKNOWN;
  102. }
  103. if (CVPixelBufferGetWidth(pixbuf) != ctx->width ||
  104. CVPixelBufferGetHeight(pixbuf) != ctx->height) {
  105. av_log(ctx, AV_LOG_ERROR, "Inconsistent frame dimensions.\n");
  106. return AVERROR_UNKNOWN;
  107. }
  108. if (flags == AV_HWFRAME_MAP_READ)
  109. map_flags = kCVPixelBufferLock_ReadOnly;
  110. err = CVPixelBufferLockBaseAddress(pixbuf, map_flags);
  111. if (err != kCVReturnSuccess) {
  112. av_log(ctx, AV_LOG_ERROR, "Error locking the pixel buffer.\n");
  113. return AVERROR_UNKNOWN;
  114. }
  115. if (CVPixelBufferIsPlanar(pixbuf)) {
  116. int planes = CVPixelBufferGetPlaneCount(pixbuf);
  117. for (i = 0; i < planes; i++) {
  118. dst->data[i] = CVPixelBufferGetBaseAddressOfPlane(pixbuf, i);
  119. dst->linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
  120. }
  121. } else {
  122. dst->data[0] = CVPixelBufferGetBaseAddress(pixbuf);
  123. dst->linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf);
  124. }
  125. ret = ff_hwframe_map_create(src->hw_frames_ctx, dst, src, vt_unmap,
  126. (void *)(uintptr_t)map_flags);
  127. if (ret < 0)
  128. goto unlock;
  129. return 0;
  130. unlock:
  131. CVPixelBufferUnlockBaseAddress(pixbuf, map_flags);
  132. return ret;
  133. }
  134. static int vt_transfer_data_from(AVHWFramesContext *hwfc,
  135. AVFrame *dst, const AVFrame *src)
  136. {
  137. AVFrame *map;
  138. int err;
  139. if (dst->width > hwfc->width || dst->height > hwfc->height)
  140. return AVERROR(EINVAL);
  141. map = av_frame_alloc();
  142. if (!map)
  143. return AVERROR(ENOMEM);
  144. map->format = dst->format;
  145. err = vt_map_frame(hwfc, map, src, AV_HWFRAME_MAP_READ);
  146. if (err)
  147. goto fail;
  148. map->width = dst->width;
  149. map->height = dst->height;
  150. err = av_frame_copy(dst, map);
  151. if (err)
  152. goto fail;
  153. err = 0;
  154. fail:
  155. av_frame_free(&map);
  156. return err;
  157. }
  158. static int vt_transfer_data_to(AVHWFramesContext *hwfc,
  159. AVFrame *dst, const AVFrame *src)
  160. {
  161. AVFrame *map;
  162. int err;
  163. if (src->width > hwfc->width || src->height > hwfc->height)
  164. return AVERROR(EINVAL);
  165. map = av_frame_alloc();
  166. if (!map)
  167. return AVERROR(ENOMEM);
  168. map->format = src->format;
  169. err = vt_map_frame(hwfc, map, dst, AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_OVERWRITE);
  170. if (err)
  171. goto fail;
  172. map->width = src->width;
  173. map->height = src->height;
  174. err = av_frame_copy(map, src);
  175. if (err)
  176. goto fail;
  177. err = 0;
  178. fail:
  179. av_frame_free(&map);
  180. return err;
  181. }
  182. static int vt_device_create(AVHWDeviceContext *ctx, const char *device,
  183. AVDictionary *opts, int flags)
  184. {
  185. if (device && device[0]) {
  186. av_log(ctx, AV_LOG_ERROR, "Device selection unsupported.\n");
  187. return AVERROR_UNKNOWN;
  188. }
  189. return 0;
  190. }
  191. const HWContextType ff_hwcontext_type_videotoolbox = {
  192. .type = AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
  193. .name = "videotoolbox",
  194. .device_create = vt_device_create,
  195. .frames_get_buffer = vt_get_buffer,
  196. .transfer_get_formats = vt_transfer_get_formats,
  197. .transfer_data_to = vt_transfer_data_to,
  198. .transfer_data_from = vt_transfer_data_from,
  199. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VIDEOTOOLBOX, AV_PIX_FMT_NONE },
  200. };