FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffmpeg_vdpau.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 <stdint.h>
20 
21 #include "ffmpeg.h"
22 
23 #include "libavcodec/vdpau.h"
24 
25 #include "libavutil/buffer.h"
26 #include "libavutil/frame.h"
27 #include "libavutil/hwcontext.h"
29 #include "libavutil/pixfmt.h"
30 
31 typedef struct VDPAUContext {
34 } VDPAUContext;
35 
37 {
38  InputStream *ist = s->opaque;
40 
41  ist->hwaccel_uninit = NULL;
42  ist->hwaccel_get_buffer = NULL;
44 
46  av_frame_free(&ctx->tmp_frame);
47 
48  av_freep(&ist->hwaccel_ctx);
50 }
51 
53 {
54  InputStream *ist = s->opaque;
56 
57  return av_hwframe_get_buffer(ctx->hw_frames_ctx, frame, 0);
58 }
59 
61 {
62  InputStream *ist = s->opaque;
64  int ret;
65 
66  ret = av_hwframe_transfer_data(ctx->tmp_frame, frame, 0);
67  if (ret < 0)
68  return ret;
69 
70  ret = av_frame_copy_props(ctx->tmp_frame, frame);
71  if (ret < 0) {
73  return ret;
74  }
75 
76  av_frame_unref(frame);
77  av_frame_move_ref(frame, ctx->tmp_frame);
78 
79  return 0;
80 }
81 
83 {
84  InputStream *ist = s->opaque;
85  int loglevel = (ist->hwaccel_id == HWACCEL_AUTO) ? AV_LOG_VERBOSE : AV_LOG_ERROR;
87  int ret;
88 
89  AVBufferRef *device_ref = NULL;
90  AVHWDeviceContext *device_ctx;
91  AVVDPAUDeviceContext *device_hwctx;
92  AVHWFramesContext *frames_ctx;
93 
94  ctx = av_mallocz(sizeof(*ctx));
95  if (!ctx)
96  return AVERROR(ENOMEM);
97 
98  ist->hwaccel_ctx = ctx;
102 
103  ctx->tmp_frame = av_frame_alloc();
104  if (!ctx->tmp_frame)
105  goto fail;
106 
108  ist->hwaccel_device, NULL, 0);
109  if (ret < 0)
110  goto fail;
111  device_ctx = (AVHWDeviceContext*)device_ref->data;
112  device_hwctx = device_ctx->hwctx;
113 
114  ctx->hw_frames_ctx = av_hwframe_ctx_alloc(device_ref);
115  if (!ctx->hw_frames_ctx)
116  goto fail;
117  av_buffer_unref(&device_ref);
118 
119  frames_ctx = (AVHWFramesContext*)ctx->hw_frames_ctx->data;
120  frames_ctx->format = AV_PIX_FMT_VDPAU;
121  frames_ctx->sw_format = s->sw_pix_fmt;
122  frames_ctx->width = s->coded_width;
123  frames_ctx->height = s->coded_height;
124 
126  if (ret < 0)
127  goto fail;
128 
129  if (av_vdpau_bind_context(s, device_hwctx->device, device_hwctx->get_proc_address, 0))
130  goto fail;
131 
132  av_log(NULL, AV_LOG_VERBOSE, "Using VDPAU to decode input stream #%d:%d.\n",
133  ist->file_index, ist->st->index);
134 
135  return 0;
136 
137 fail:
138  av_log(NULL, loglevel, "VDPAU init failed for stream #%d:%d.\n",
139  ist->file_index, ist->st->index);
140  av_buffer_unref(&device_ref);
141  vdpau_uninit(s);
142  return AVERROR(EINVAL);
143 }
144 
146 {
147  InputStream *ist = s->opaque;
148 
149  if (!ist->hwaccel_ctx) {
150  int ret = vdpau_alloc(s);
151  if (ret < 0)
152  return ret;
153  }
154 
157 
158  return 0;
159 }
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:54
#define NULL
Definition: coverity.c:32
This struct is allocated as AVHWDeviceContext.hwctx.
const char * s
Definition: avisynth_c.h:768
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1934
VdpGetProcAddress * get_proc_address
int index
stream index in AVFormatContext
Definition: avformat.h:890
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:222
AVFrame * tmp_frame
Definition: ffmpeg_vdpau.c:33
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:202
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:524
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
int(* hwaccel_get_buffer)(AVCodecContext *s, AVFrame *frame, int flags)
Definition: ffmpeg.h:368
Public libavcodec VDPAU header.
AVBufferRef * hw_frames_ctx
Definition: ffmpeg_vdpau.c:32
An API-specific header for AV_HWDEVICE_TYPE_VDPAU.
int vdpau_init(AVCodecContext *s)
Definition: ffmpeg_vdpau.c:145
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:3064
static AVFrame * frame
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:85
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
Definition: hwcontext.c:502
static int flags
Definition: log.c:57
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int file_index
Definition: ffmpeg.h:292
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int vdpau_alloc(AVCodecContext *s)
Definition: ffmpeg_vdpau.c:82
void(* hwaccel_uninit)(AVCodecContext *s)
Definition: ffmpeg.h:367
#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:163
static int vdpau_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
Definition: ffmpeg_vdpau.c:52
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:268
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:404
#define fail()
Definition: checkasm.h:89
reference-counted frame API
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:378
AVFormatContext * ctx
Definition: movenc.c:48
int(* hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame)
Definition: ffmpeg.h:369
AVStream * st
Definition: ffmpeg.h:293
main external API structure.
Definition: avcodec.h:1732
uint8_t * data
The data buffer.
Definition: buffer.h:89
int coded_height
Definition: avcodec.h:1934
HW acceleration through VDPAU, Picture.data[3] contains a VdpVideoSurface.
Definition: pixfmt.h:209
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:117
refcounted data buffer API
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:498
int av_vdpau_bind_context(AVCodecContext *avctx, VdpDevice device, VdpGetProcAddress *get_proc, unsigned flags)
Associate a VDPAU device with a codec context for hardware acceleration.
Definition: vdpau.c:803
A reference to a data buffer.
Definition: buffer.h:81
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:182
static int vdpau_retrieve_data(AVCodecContext *s, AVFrame *frame)
Definition: ffmpeg_vdpau.c:60
pixel format definitions
void * hwaccel_ctx
Definition: ffmpeg.h:366
char * hwaccel_device
Definition: ffmpeg.h:361
#define av_freep(p)
static void vdpau_uninit(AVCodecContext *s)
Definition: ffmpeg_vdpau.c:36
enum HWAccelID hwaccel_id
Definition: ffmpeg.h:360
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:215
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:3436
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:1789
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:596