FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffmpeg_videotoolbox.c
Go to the documentation of this file.
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 #if CONFIG_VDA
27 # include "libavcodec/vda.h"
28 #endif
29 #if CONFIG_VIDEOTOOLBOX
30 # include "libavcodec/videotoolbox.h"
31 #endif
32 #include "libavutil/imgutils.h"
33 #include "ffmpeg.h"
34 
35 typedef struct VTContext {
37 } VTContext;
38 
40 
42 {
43  InputStream *ist = s->opaque;
44  VTContext *vt = ist->hwaccel_ctx;
45  CVPixelBufferRef pixbuf = (CVPixelBufferRef)frame->data[3];
46  OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
47  CVReturn err;
48  uint8_t *data[4] = { 0 };
49  int linesize[4] = { 0 };
50  int planes, ret, i;
51  char codec_str[32];
52 
54 
55  switch (pixel_format) {
56  case kCVPixelFormatType_420YpCbCr8Planar: vt->tmp_frame->format = AV_PIX_FMT_YUV420P; break;
57  case kCVPixelFormatType_422YpCbCr8: vt->tmp_frame->format = AV_PIX_FMT_UYVY422; break;
58  case kCVPixelFormatType_32BGRA: vt->tmp_frame->format = AV_PIX_FMT_BGRA; break;
59 #ifdef kCFCoreFoundationVersionNumber10_7
60  case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange: vt->tmp_frame->format = AV_PIX_FMT_NV12; break;
61 #endif
62  default:
63  av_get_codec_tag_string(codec_str, sizeof(codec_str), s->codec_tag);
65  "%s: Unsupported pixel format: %s\n", codec_str, videotoolbox_pixfmt);
66  return AVERROR(ENOSYS);
67  }
68 
69  vt->tmp_frame->width = frame->width;
70  vt->tmp_frame->height = frame->height;
71  ret = av_frame_get_buffer(vt->tmp_frame, 32);
72  if (ret < 0)
73  return ret;
74 
75  err = CVPixelBufferLockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
76  if (err != kCVReturnSuccess) {
77  av_log(NULL, AV_LOG_ERROR, "Error locking the pixel buffer.\n");
78  return AVERROR_UNKNOWN;
79  }
80 
81  if (CVPixelBufferIsPlanar(pixbuf)) {
82 
83  planes = CVPixelBufferGetPlaneCount(pixbuf);
84  for (i = 0; i < planes; i++) {
85  data[i] = CVPixelBufferGetBaseAddressOfPlane(pixbuf, i);
86  linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
87  }
88  } else {
89  data[0] = CVPixelBufferGetBaseAddress(pixbuf);
90  linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf);
91  }
92 
94  (const uint8_t **)data, linesize, vt->tmp_frame->format,
95  frame->width, frame->height);
96 
97  ret = av_frame_copy_props(vt->tmp_frame, frame);
98  CVPixelBufferUnlockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
99  if (ret < 0)
100  return ret;
101 
102  av_frame_unref(frame);
103  av_frame_move_ref(frame, vt->tmp_frame);
104 
105  return 0;
106 }
107 
109 {
110  InputStream *ist = s->opaque;
111  VTContext *vt = ist->hwaccel_ctx;
112 
113  ist->hwaccel_uninit = NULL;
115 
116  av_frame_free(&vt->tmp_frame);
117 
118  if (ist->hwaccel_id == HWACCEL_VIDEOTOOLBOX) {
119 #if CONFIG_VIDEOTOOLBOX
121 #endif
122  } else {
123 #if CONFIG_VDA
125 #endif
126  }
127  av_freep(&ist->hwaccel_ctx);
128 }
129 
131 {
132  InputStream *ist = s->opaque;
133  int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
134  int ret = 0;
135  VTContext *vt;
136 
137  vt = av_mallocz(sizeof(*vt));
138  if (!vt)
139  return AVERROR(ENOMEM);
140 
141  ist->hwaccel_ctx = vt;
144 
145  vt->tmp_frame = av_frame_alloc();
146  if (!vt->tmp_frame) {
147  ret = AVERROR(ENOMEM);
148  goto fail;
149  }
150 
151  if (ist->hwaccel_id == HWACCEL_VIDEOTOOLBOX) {
152 #if CONFIG_VIDEOTOOLBOX
153  if (!videotoolbox_pixfmt) {
155  } else {
157  CFStringRef pixfmt_str = CFStringCreateWithCString(kCFAllocatorDefault,
159  kCFStringEncodingUTF8);
160 #if HAVE_UTGETOSTYPEFROMSTRING
161  vtctx->cv_pix_fmt_type = UTGetOSTypeFromString(pixfmt_str);
162 #else
163  av_log(s, loglevel, "UTGetOSTypeFromString() is not available "
164  "on this platform, %s pixel format can not be honored from "
165  "the command line\n", videotoolbox_pixfmt);
166 #endif
167  ret = av_videotoolbox_default_init2(s, vtctx);
168  CFRelease(pixfmt_str);
169  }
170 #endif
171  } else {
172 #if CONFIG_VDA
173  if (!videotoolbox_pixfmt) {
174  ret = av_vda_default_init(s);
175  } else {
177  CFStringRef pixfmt_str = CFStringCreateWithCString(kCFAllocatorDefault,
179  kCFStringEncodingUTF8);
180 #if HAVE_UTGETOSTYPEFROMSTRING
181  vdactx->cv_pix_fmt_type = UTGetOSTypeFromString(pixfmt_str);
182 #else
183  av_log(s, loglevel, "UTGetOSTypeFromString() is not available "
184  "on this platform, %s pixel format can not be honored from "
185  "the command line\n", videotoolbox_pixfmt);
186 #endif
187  ret = av_vda_default_init2(s, vdactx);
188  CFRelease(pixfmt_str);
189  }
190 #endif
191  }
192  if (ret < 0) {
193  av_log(NULL, loglevel,
194  "Error creating %s decoder.\n", ist->hwaccel_id == HWACCEL_VIDEOTOOLBOX ? "Videotoolbox" : "VDA");
195  goto fail;
196  }
197 
198  return 0;
199 fail:
201  return ret;
202 }
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:82
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
int av_vda_default_init2(AVCodecContext *avctx, AVVDAContext *vdactx)
This is a convenience function that creates and sets up the VDA context using an internal implementat...
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
misc image utilities
static void videotoolbox_uninit(AVCodecContext *s)
AVFrame * tmp_frame
AVVideotoolboxContext * av_videotoolbox_alloc_context(void)
Allocate and initialize a Videotoolbox context.
char * videotoolbox_pixfmt
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:508
size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
Put a string representing the codec tag codec_tag in buf.
Definition: utils.c:3102
void av_vda_default_free(AVCodecContext *ctx)
This function must be called to free the VDA context initialized with av_vda_default_init().
Definition: vda.c:76
static int videotoolbox_retrieve_data(AVCodecContext *s, AVFrame *frame)
This struct holds all the information that needs to be passed between the caller and libavcodec for i...
Definition: vda.h:163
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:140
OSType cv_pix_fmt_type
CVPixelBuffer Format Type that VDA will use for decoded frames; set by the caller.
Definition: vda.h:179
static AVFrame * frame
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int av_videotoolbox_default_init(AVCodecContext *avctx)
This is a convenience function that creates and sets up the Videotoolbox context using an internal im...
int av_vda_default_init(AVCodecContext *avctx)
This is a convenience function that creates and sets up the VDA context using an internal implementat...
Definition: vda.c:71
#define av_log(a,...)
int width
width and height of the video frame
Definition: frame.h:236
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void(* hwaccel_uninit)(AVCodecContext *s)
Definition: ffmpeg.h:337
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:153
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:90
#define fail()
Definition: checkasm.h:81
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:302
int(* hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame)
Definition: ffmpeg.h:339
int av_videotoolbox_default_init2(AVCodecContext *avctx, AVVideotoolboxContext *vtctx)
This is a convenience function that creates and sets up the Videotoolbox context using an internal im...
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:248
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
Public libavcodec VDA header.
main external API structure.
Definition: avcodec.h:1649
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1681
int videotoolbox_init(AVCodecContext *s)
OSType cv_pix_fmt_type
CVPixelBuffer Format Type that Videotoolbox will use for decoded frames.
Definition: videotoolbox.h:63
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:270
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:484
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
void * hwaccel_ctx
Definition: ffmpeg.h:336
int height
Definition: frame.h:236
#define av_freep(p)
void av_videotoolbox_default_free(AVCodecContext *avctx)
This function must be called to free the Videotoolbox context initialized with av_videotoolbox_defaul...
AVVDAContext * av_vda_alloc_context(void)
Allocate and initialize a VDA context.
Definition: vda.c:66
enum HWAccelID hwaccel_id
Definition: ffmpeg.h:330
This struct holds all the information that needs to be passed between the caller and libavcodec for i...
Definition: videotoolbox.h:46
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:1706
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:580
Public libavcodec Videotoolbox header.