FFmpeg
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"
27 #include "libavutil/imgutils.h"
28 #include "ffmpeg.h"
29 
30 typedef struct VTContext {
32  int log_once;
33 } VTContext;
34 
36 
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) {
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 
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
68 #endif
69  default:
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;
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 
101  (const uint8_t **)data, linesize, vt->tmp_frame->format,
102  frame->width, frame->height);
103 
105  CVPixelBufferUnlockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
106  if (ret < 0)
107  return ret;
108 
111 
112  return 0;
113 }
114 
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 
126  av_freep(&ist->hwaccel_ctx);
127 }
128 
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) {
153  } else {
155  CFStringRef pixfmt_str = CFStringCreateWithCString(kCFAllocatorDefault,
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
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:
176  return ret;
177 }
av_videotoolbox_alloc_context
AVVideotoolboxContext * av_videotoolbox_alloc_context(void)
Allocate and initialize a Videotoolbox context.
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_frame_get_buffer
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:246
kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange
@ kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange
Definition: videotoolboxenc.c:50
av_videotoolbox_default_free
void av_videotoolbox_default_free(AVCodecContext *avctx)
This function must be called to free the Videotoolbox context initialized with av_videotoolbox_defaul...
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:310
AVFrame::width
int width
Definition: frame.h:380
data
const char data[16]
Definition: mxf.c:143
ffmpeg.h
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
kCVPixelFormatType_420YpCbCr10BiPlanarFullRange
@ kCVPixelFormatType_420YpCbCr10BiPlanarFullRange
Definition: videotoolboxenc.c:49
AVVideotoolboxContext
This struct holds all the information that needs to be passed between the caller and libavcodec for i...
Definition: videotoolbox.h:46
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:329
planes
static const struct @320 planes[]
InputStream
Definition: ffmpeg.h:302
VTContext::tmp_frame
AVFrame * tmp_frame
Definition: ffmpeg_videotoolbox.c:31
fail
#define fail()
Definition: checkasm.h:127
av_videotoolbox_default_init
int av_videotoolbox_default_init(AVCodecContext *avctx)
This is a convenience function that creates and sets up the Videotoolbox context using an internal im...
videotoolbox_retrieve_data
static int videotoolbox_retrieve_data(AVCodecContext *s, AVFrame *frame)
Definition: ffmpeg_videotoolbox.c:37
VTContext::log_once
int log_once
Definition: ffmpeg_videotoolbox.c:32
ist
fg inputs[0] ist
Definition: ffmpeg_filter.c:182
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
videotoolbox.h
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:537
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:395
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
VTContext
Definition: ffmpeg_videotoolbox.c:30
av_videotoolbox_default_init2
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...
HWACCEL_AUTO
@ HWACCEL_AUTO
Definition: ffmpeg.h:61
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
videotoolbox_uninit
static void videotoolbox_uninit(AVCodecContext *s)
Definition: ffmpeg_videotoolbox.c:115
videotoolbox_pixfmt
char * videotoolbox_pixfmt
Definition: ffmpeg_videotoolbox.c:35
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:461
videotoolbox_init
int videotoolbox_init(AVCodecContext *s)
Definition: ffmpeg_videotoolbox.c:129
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:437
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:263
avcodec.h
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:89
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_PIX_FMT_UYVY422
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:81
AVCodecContext
main external API structure.
Definition: avcodec.h:383
AVFrame::height
int height
Definition: frame.h:380
av_image_copy
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:422
AV_PIX_FMT_P010
#define AV_PIX_FMT_P010
Definition: pixfmt.h:440
av_log_once
void av_log_once(void *avcl, int initial_level, int subsequent_level, int *state, const char *fmt,...)
Definition: log.c:415
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVVideotoolboxContext::cv_pix_fmt_type
OSType cv_pix_fmt_type
CVPixelBuffer Format Type that Videotoolbox will use for decoded frames.
Definition: videotoolbox.h:64
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:353
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
av_fourcc2str
#define av_fourcc2str(fourcc)
Definition: avutil.h:348