| @@ -1,177 +0,0 @@ |
| 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 | | − |
| 19 | | −#include "config.h" |
| 20 | | − |
| 21 | | −#if HAVE_UTGETOSTYPEFROMSTRING |
| 22 | | −#include <CoreServices/CoreServices.h> |
| 23 | | −#endif |
| 24 | | − |
| 25 | | −#include "libavcodec/avcodec.h" |
| 26 | | −#include "libavcodec/videotoolbox.h" |
| 27 | | −#include "libavutil/imgutils.h" |
| 28 | | −#include "ffmpeg.h" |
| 29 | | − |
| 30 | | −typedef struct VTContext { |
| 31 | | − AVFrame *tmp_frame; |
| 32 | | − int log_once; |
| 33 | | −} VTContext; |
| 34 | | − |
| 35 | | −char *videotoolbox_pixfmt; |
| 36 | | − |
| 37 | | −static int videotoolbox_retrieve_data(AVCodecContext *s, AVFrame *frame) |
| 38 | | −{ |
| 39 | | − InputStream *ist = s->opaque; |
| 40 | | − VTContext *vt = ist->hwaccel_ctx; |
| 41 | | − CVPixelBufferRef pixbuf = (CVPixelBufferRef)frame->data[3]; |
| 42 | | − OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf); |
| 43 | | − CVReturn err; |
| 44 | | − uint8_t *data[4] = { 0 }; |
| 45 | | − int linesize[4] = { 0 }; |
| 46 | | − int planes, ret, i; |
| 47 | | − |
| 48 | | − if (frame->format == ist->hwaccel_output_format) { |
| 49 | | − av_log_once(s, AV_LOG_INFO, AV_LOG_TRACE, &vt->log_once, |
| 50 | | − "There is no video filter for videotoolbox pix_fmt now, remove the " |
| 51 | | − "-hwaccel_output_format option if video filter doesn't work\n"); |
| 52 | | − return 0; |
| 53 | | − } |
| 54 | | − |
| 55 | | − av_frame_unref(vt->tmp_frame); |
| 56 | | − |
| 57 | | − switch (pixel_format) { |
| 58 | | − case kCVPixelFormatType_420YpCbCr8Planar: vt->tmp_frame->format = AV_PIX_FMT_YUV420P; break; |
| 59 | | − case kCVPixelFormatType_422YpCbCr8: vt->tmp_frame->format = AV_PIX_FMT_UYVY422; break; |
| 60 | | − case kCVPixelFormatType_32BGRA: vt->tmp_frame->format = AV_PIX_FMT_BGRA; break; |
| 61 | | −#ifdef kCFCoreFoundationVersionNumber10_7 |
| 62 | | − case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange: |
| 63 | | − case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange: vt->tmp_frame->format = AV_PIX_FMT_NV12; break; |
| 64 | | −#endif |
| 65 | | −#if HAVE_KCVPIXELFORMATTYPE_420YPCBCR10BIPLANARVIDEORANGE |
| 66 | | − case kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange: |
| 67 | | − case kCVPixelFormatType_420YpCbCr10BiPlanarFullRange: vt->tmp_frame->format = AV_PIX_FMT_P010; break; |
| 68 | | −#endif |
| 69 | | − default: |
| 70 | | − av_log(NULL, AV_LOG_ERROR, |
| 71 | | − "%s: Unsupported pixel format: %s\n", |
| 72 | | − av_fourcc2str(s->codec_tag), videotoolbox_pixfmt); |
| 73 | | − return AVERROR(ENOSYS); |
| 74 | | − } |
| 75 | | − |
| 76 | | − vt->tmp_frame->width = frame->width; |
| 77 | | − vt->tmp_frame->height = frame->height; |
| 78 | | − ret = av_frame_get_buffer(vt->tmp_frame, 0); |
| 79 | | − if (ret < 0) |
| 80 | | − return ret; |
| 81 | | − |
| 82 | | − err = CVPixelBufferLockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly); |
| 83 | | − if (err != kCVReturnSuccess) { |
| 84 | | − av_log(NULL, AV_LOG_ERROR, "Error locking the pixel buffer.\n"); |
| 85 | | − return AVERROR_UNKNOWN; |
| 86 | | − } |
| 87 | | − |
| 88 | | − if (CVPixelBufferIsPlanar(pixbuf)) { |
| 89 | | − |
| 90 | | − planes = CVPixelBufferGetPlaneCount(pixbuf); |
| 91 | | − for (i = 0; i < planes; i++) { |
| 92 | | − data[i] = CVPixelBufferGetBaseAddressOfPlane(pixbuf, i); |
| 93 | | − linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i); |
| 94 | | − } |
| 95 | | − } else { |
| 96 | | − data[0] = CVPixelBufferGetBaseAddress(pixbuf); |
| 97 | | − linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf); |
| 98 | | − } |
| 99 | | − |
| 100 | | − av_image_copy(vt->tmp_frame->data, vt->tmp_frame->linesize, |
| 101 | | − (const uint8_t **)data, linesize, vt->tmp_frame->format, |
| 102 | | − frame->width, frame->height); |
| 103 | | − |
| 104 | | − ret = av_frame_copy_props(vt->tmp_frame, frame); |
| 105 | | − CVPixelBufferUnlockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly); |
| 106 | | − if (ret < 0) |
| 107 | | − return ret; |
| 108 | | − |
| 109 | | − av_frame_unref(frame); |
| 110 | | − av_frame_move_ref(frame, vt->tmp_frame); |
| 111 | | − |
| 112 | | − return 0; |
| 113 | | −} |
| 114 | | − |
| 115 | | −static void videotoolbox_uninit(AVCodecContext *s) |
| 116 | | −{ |
| 117 | | − InputStream *ist = s->opaque; |
| 118 | | − VTContext *vt = ist->hwaccel_ctx; |
| 119 | | − |
| 120 | | − ist->hwaccel_uninit = NULL; |
| 121 | | − ist->hwaccel_retrieve_data = NULL; |
| 122 | | − |
| 123 | | − av_frame_free(&vt->tmp_frame); |
| 124 | | − |
| 125 | | − av_videotoolbox_default_free(s); |
| 126 | | − av_freep(&ist->hwaccel_ctx); |
| 127 | | −} |
| 128 | | − |
| 129 | | −int videotoolbox_init(AVCodecContext *s) |
| 130 | | −{ |
| 131 | | − InputStream *ist = s->opaque; |
| 132 | | − int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR; |
| 133 | | − int ret = 0; |
| 134 | | − VTContext *vt; |
| 135 | | − |
| 136 | | − vt = av_mallocz(sizeof(*vt)); |
| 137 | | − if (!vt) |
| 138 | | − return AVERROR(ENOMEM); |
| 139 | | − |
| 140 | | − ist->hwaccel_ctx = vt; |
| 141 | | − ist->hwaccel_uninit = videotoolbox_uninit; |
| 142 | | − ist->hwaccel_retrieve_data = videotoolbox_retrieve_data; |
| 143 | | − |
| 144 | | − vt->tmp_frame = av_frame_alloc(); |
| 145 | | − if (!vt->tmp_frame) { |
| 146 | | − ret = AVERROR(ENOMEM); |
| 147 | | − goto fail; |
| 148 | | − } |
| 149 | | − |
| 150 | | − // TODO: reindent |
| 151 | | − if (!videotoolbox_pixfmt) { |
| 152 | | − ret = av_videotoolbox_default_init(s); |
| 153 | | − } else { |
| 154 | | − AVVideotoolboxContext *vtctx = av_videotoolbox_alloc_context(); |
| 155 | | − CFStringRef pixfmt_str = CFStringCreateWithCString(kCFAllocatorDefault, |
| 156 | | − videotoolbox_pixfmt, |
| 157 | | − kCFStringEncodingUTF8); |
| 158 | | −#if HAVE_UTGETOSTYPEFROMSTRING |
| 159 | | − vtctx->cv_pix_fmt_type = UTGetOSTypeFromString(pixfmt_str); |
| 160 | | −#else |
| 161 | | − av_log(s, loglevel, "UTGetOSTypeFromString() is not available " |
| 162 | | − "on this platform, %s pixel format can not be honored from " |
| 163 | | − "the command line\n", videotoolbox_pixfmt); |
| 164 | | −#endif |
| 165 | | − ret = av_videotoolbox_default_init2(s, vtctx); |
| 166 | | − CFRelease(pixfmt_str); |
| 167 | | − } |
| 168 | | − if (ret < 0) { |
| 169 | | − av_log(NULL, loglevel, "Error creating Videotoolbox decoder.\n"); |
| 170 | | − goto fail; |
| 171 | | − } |
| 172 | | − |
| 173 | | − return 0; |
| 174 | | −fail: |
| 175 | | − videotoolbox_uninit(s); |
| 176 | | − return ret; |
| 177 | | −} |