FFmpeg
ffmpeg_qsv.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 <mfx/mfxvideo.h>
20 #include <stdlib.h>
21 
22 #include "libavutil/dict.h"
23 #include "libavutil/hwcontext.h"
25 #include "libavutil/opt.h"
26 #include "libavcodec/qsv.h"
27 
28 #include "ffmpeg.h"
29 
31 char *qsv_device = NULL;
32 
34 {
35  InputStream *ist = s->opaque;
36 
37  return av_hwframe_get_buffer(ist->hw_frames_ctx, frame, 0);
38 }
39 
41 {
42  InputStream *ist = s->opaque;
43  av_buffer_unref(&ist->hw_frames_ctx);
44 }
45 
47 {
48  int err;
49  AVDictionary *dict = NULL;
50 
51  if (qsv_device) {
52  err = av_dict_set(&dict, "child_device", qsv_device, 0);
53  if (err < 0)
54  return err;
55  }
56 
58  ist->hwaccel_device, dict, 0);
59  if (err < 0) {
60  av_log(NULL, AV_LOG_ERROR, "Error creating a QSV device\n");
61  goto err_out;
62  }
63 
64 err_out:
65  if (dict)
66  av_dict_free(&dict);
67 
68  return err;
69 }
70 
72 {
73  InputStream *ist = s->opaque;
74  AVHWFramesContext *frames_ctx;
75  AVQSVFramesContext *frames_hwctx;
76  int ret;
77 
78  if (!hw_device_ctx) {
80  if (ret < 0)
81  return ret;
82  }
83 
84  av_buffer_unref(&ist->hw_frames_ctx);
85  ist->hw_frames_ctx = av_hwframe_ctx_alloc(hw_device_ctx);
86  if (!ist->hw_frames_ctx)
87  return AVERROR(ENOMEM);
88 
89  frames_ctx = (AVHWFramesContext*)ist->hw_frames_ctx->data;
90  frames_hwctx = frames_ctx->hwctx;
91 
92  frames_ctx->width = FFALIGN(s->coded_width, 32);
93  frames_ctx->height = FFALIGN(s->coded_height, 32);
94  frames_ctx->format = AV_PIX_FMT_QSV;
95  frames_ctx->sw_format = s->sw_pix_fmt;
96  frames_ctx->initial_pool_size = 64 + s->extra_hw_frames;
97  frames_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
98 
99  ret = av_hwframe_ctx_init(ist->hw_frames_ctx);
100  if (ret < 0) {
101  av_log(NULL, AV_LOG_ERROR, "Error initializing a QSV frame pool\n");
102  return ret;
103  }
104 
105  ist->hwaccel_get_buffer = qsv_get_buffer;
106  ist->hwaccel_uninit = qsv_uninit;
107 
108  return 0;
109 }
AVQSVFramesContext::frame_type
int frame_type
A combination of MFX_MEMTYPE_* describing the frame pool.
Definition: hwcontext_qsv.h:49
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
opt.h
AVHWFramesContext::format
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition: hwcontext.h:209
av_hwframe_ctx_init
int av_hwframe_ctx_init(AVBufferRef *ref)
Finalize the context before use.
Definition: hwcontext.c:333
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
av_hwframe_ctx_alloc
AVBufferRef * av_hwframe_ctx_alloc(AVBufferRef *device_ref_in)
Allocate an AVHWFramesContext tied to a given device context.
Definition: hwcontext.c:247
ffmpeg.h
AVDictionary
Definition: dict.c:30
AVHWFramesContext::width
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:229
InputStream
Definition: ffmpeg.h:302
ist
fg inputs[0] ist
Definition: ffmpeg_filter.c:182
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
qsv_uninit
static void qsv_uninit(AVCodecContext *s)
Definition: ffmpeg_qsv.c:40
AVHWFramesContext::height
int height
Definition: hwcontext.h:229
s
#define s(width, name)
Definition: cbs_vp9.c:257
qsv_device_init
static int qsv_device_init(InputStream *ist)
Definition: ffmpeg_qsv.c:46
qsv_get_buffer
static int qsv_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
Definition: ffmpeg_qsv.c:33
NULL
#define NULL
Definition: coverity.c:32
AVHWFramesContext::sw_format
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:222
av_buffer_unref
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
qsv.h
AV_PIX_FMT_QSV
@ AV_PIX_FMT_QSV
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
Definition: pixfmt.h:212
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:203
hwcontext_qsv.h
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
ret
ret
Definition: filter_design.txt:187
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
AVHWFramesContext::hwctx
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:162
av_hwdevice_ctx_create
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:610
dict.h
AV_HWDEVICE_TYPE_QSV
@ AV_HWDEVICE_TYPE_QSV
Definition: hwcontext.h:33
AVCodecContext
main external API structure.
Definition: avcodec.h:379
AVQSVFramesContext
This struct is allocated as AVHWFramesContext.hwctx.
Definition: hwcontext_qsv.h:42
AVHWFramesContext::initial_pool_size
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:199
qsv_device
char * qsv_device
Definition: ffmpeg_qsv.c:31
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
hwcontext.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
qsv_init
int qsv_init(AVCodecContext *s)
Definition: ffmpeg_qsv.c:71
av_hwframe_get_buffer
int av_hwframe_get_buffer(AVBufferRef *hwframe_ref, AVFrame *frame, int flags)
Allocate a new frame attached to the given AVHWFramesContext.
Definition: hwcontext.c:502
hw_device_ctx
static AVBufferRef * hw_device_ctx
Definition: ffmpeg_qsv.c:30