FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffmpeg_vda.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 "libavcodec/avcodec.h"
20 #include "libavcodec/vda.h"
21 #include "libavutil/imgutils.h"
22 
23 #include "ffmpeg.h"
24 
25 typedef struct VDAContext {
27 } VDAContext;
28 
30 {
31  InputStream *ist = s->opaque;
32  VDAContext *vda = ist->hwaccel_ctx;
33  CVPixelBufferRef pixbuf = (CVPixelBufferRef)frame->data[3];
34  OSType pixel_format = CVPixelBufferGetPixelFormatType(pixbuf);
35  CVReturn err;
36  uint8_t *data[4] = { 0 };
37  int linesize[4] = { 0 };
38  int planes, ret, i;
39 
41 
42  switch (pixel_format) {
43  case kCVPixelFormatType_420YpCbCr8Planar: vda->tmp_frame->format = AV_PIX_FMT_YUV420P; break;
44  case kCVPixelFormatType_422YpCbCr8: vda->tmp_frame->format = AV_PIX_FMT_UYVY422; break;
45  default:
47  "Unsupported pixel format: %u\n", pixel_format);
48  return AVERROR(ENOSYS);
49  }
50 
51  vda->tmp_frame->width = frame->width;
52  vda->tmp_frame->height = frame->height;
53  ret = av_frame_get_buffer(vda->tmp_frame, 32);
54  if (ret < 0)
55  return ret;
56 
57  err = CVPixelBufferLockBaseAddress(pixbuf, kCVPixelBufferLock_ReadOnly);
58  if (err != kCVReturnSuccess) {
59  av_log(NULL, AV_LOG_ERROR, "Error locking the pixel buffer.\n");
60  return AVERROR_UNKNOWN;
61  }
62 
63  if (CVPixelBufferIsPlanar(pixbuf)) {
64 
65  planes = CVPixelBufferGetPlaneCount(pixbuf);
66  for (i = 0; i < planes; i++) {
67  data[i] = CVPixelBufferGetBaseAddressOfPlane(pixbuf, i);
68  linesize[i] = CVPixelBufferGetBytesPerRowOfPlane(pixbuf, i);
69  }
70  } else {
71  data[0] = CVPixelBufferGetBaseAddress(pixbuf);
72  linesize[0] = CVPixelBufferGetBytesPerRow(pixbuf);
73  }
74 
76  (const uint8_t **)data, linesize, vda->tmp_frame->format,
77  frame->width, frame->height);
78 
79  ret = av_frame_copy_props(vda->tmp_frame, frame);
80  if (ret < 0)
81  return ret;
82 
83  av_frame_unref(frame);
84  av_frame_move_ref(frame, vda->tmp_frame);
85 
86  return 0;
87 }
88 
90 {
91  InputStream *ist = s->opaque;
92  VDAContext *vda = ist->hwaccel_ctx;
93 
94  ist->hwaccel_uninit = NULL;
96 
97  av_frame_free(&vda->tmp_frame);
98 
100  av_freep(&ist->hwaccel_ctx);
101 }
102 
104 {
105  InputStream *ist = s->opaque;
106  int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
107  VDAContext *vda;
108  int ret;
109 
110  vda = av_mallocz(sizeof(*vda));
111  if (!vda)
112  return AVERROR(ENOMEM);
113 
114  ist->hwaccel_ctx = vda;
115  ist->hwaccel_uninit = vda_uninit;
117 
118  vda->tmp_frame = av_frame_alloc();
119  if (!vda->tmp_frame) {
120  ret = AVERROR(ENOMEM);
121  goto fail;
122  }
123 
124  ret = av_vda_default_init(s);
125  if (ret < 0) {
126  av_log(NULL, loglevel, "Error creating VDA decoder.\n");
127  goto fail;
128  }
129 
130  return 0;
131 fail:
132  vda_uninit(s);
133  return ret;
134 }
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:83
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
misc image utilities
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:479
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 void vda_uninit(AVCodecContext *s)
Definition: ffmpeg_vda.c:89
int vda_init(AVCodecContext *s)
Definition: ffmpeg_vda.c:103
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
static AVFrame * frame
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
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:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void(* hwaccel_uninit)(AVCodecContext *s)
Definition: ffmpeg.h:321
#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:148
Libavcodec external API header.
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:288
AVFrame * tmp_frame
Definition: ffmpeg_vda.c:26
ret
Definition: avfilter.c:974
int(* hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame)
Definition: ffmpeg.h:323
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
Public libavcodec VDA header.
main external API structure.
Definition: avcodec.h:1241
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition: frame.c:265
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:462
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
static int vda_retrieve_data(AVCodecContext *s, AVFrame *frame)
Definition: ffmpeg_vda.c:29
void * hwaccel_ctx
Definition: ffmpeg.h:320
int height
Definition: frame.h:220
#define av_freep(p)
enum HWAccelID hwaccel_id
Definition: ffmpeg.h:315
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:250
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:1298
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:548