1 /*
2 * Intel MediaSDK QSV codec-independent code
3 *
4 * copyright (c) 2013 Luca Barbato
5 * copyright (c) 2015 Anton Khirnov <anton@khirnov.net>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <string.h>
25 #include <sys/types.h>
26
27 #include <mfx/mfxvideo.h>
28
34
39
41 {
42 switch (format) {
46 default:
48 }
49 }
50
52 {
53 if (!session) {
56 if (ret < 0)
58 }
59
61 } else {
63 }
64
65 /* make sure the decoder is uninitialized */
66 MFXVideoDECODE_Close(q->
session);
67
68 return 0;
69 }
70
72 {
73 mfxVideoParam param = { { 0 } };
75
77 if (ret < 0) {
80 }
81
82
84 if (ret < 0)
86
87 param.mfx.CodecId =
ret;
88 param.mfx.CodecProfile = avctx->
profile;
89 param.mfx.CodecLevel = avctx->
level;
90
91 param.mfx.FrameInfo.BitDepthLuma = 8;
92 param.mfx.FrameInfo.BitDepthChroma = 8;
93 param.mfx.FrameInfo.Shift = 0;
94 param.mfx.FrameInfo.FourCC = MFX_FOURCC_NV12;
97 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
98
103
104 ret = MFXVideoDECODE_Init(q->
session, ¶m);
105 if (ret < 0) {
108 }
109
110 return 0;
111 }
112
114 {
116
118 if (ret < 0)
120
123 } else {
130
134
136 }
137
138 return 0;
139 }
140
142 {
144 while (cur) {
148 }
150 }
151 }
152
154 {
157
159
162 while (frame) {
165 if (ret < 0)
168 return 0;
169 }
170
173 }
174
176 if (!frame)
182 }
184
186 if (ret < 0)
188
190
191 return 0;
192 }
193
195 {
197 while (cur) {
201 }
203 }
204
208 {
209 mfxFrameSurface1 *insurf;
210 mfxFrameSurface1 *outsurf;
211 mfxSyncPoint sync;
212 mfxBitstream bs = { { { 0 } } };
214
216 bs.Data = avpkt->
data;
217 bs.DataLength = avpkt->
size;
218 bs.MaxLength = bs.DataLength;
219 bs.TimeStamp = avpkt->
pts;
220 }
221
222 do {
224 if (ret < 0)
226
227 ret = MFXVideoDECODE_DecodeFrameAsync(q->
session, avpkt->
size ? &bs :
NULL,
228 insurf, &outsurf, &sync);
229 if (ret == MFX_WRN_DEVICE_BUSY)
231
232 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
233
234 if (ret != MFX_ERR_NONE &&
235 ret != MFX_ERR_MORE_DATA &&
236 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
237 ret != MFX_ERR_MORE_SURFACE) {
240 }
241
242 if (sync) {
244
245 MFXVideoCORE_SyncOperation(q->
session, sync, 60000);
246
248 if (!src_frame) {
250 "The returned surface does not correspond to any frame\n");
252 }
253
255 if (ret < 0)
257
258 frame->
pkt_pts = frame->
pts = outsurf->Data.TimeStamp;
259
261 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
262 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
263 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
265 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
267 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
268
269 *got_frame = 1;
270 }
271
272 return bs.DataOffset;
273 }
274
276 {
278
279 while (cur) {
284 }
285
288
289 return 0;
290 }
int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q, AVFrame *frame, int *got_frame, AVPacket *avpkt)
This structure describes decoded (raw) audio or video data.
int coded_width
Bitstream width / height, may be different from width/height e.g.
int ff_qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
memory handling functions
int repeat_pict
When decoding, this signals how much the picture must be delayed.
mfxExtBuffer ** ext_buffers
mfxFrameSurface1 * surface
int av_usleep(unsigned usec)
Sleep for a period of time.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int ff_qsv_decode_close(QSVContext *q)
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int interlaced_frame
The content of the picture is interlaced.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Libavcodec external API header.
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
mfxFrameSurface1 surface_internal
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
mfxSession internal_session
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session)
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
static void qsv_clear_unused_frames(QSVContext *q)
int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal api header.
common internal and external API header
int ff_qsv_map_pixfmt(enum AVPixelFormat format)
int ff_qsv_error(int mfx_err)
Convert a libmfx error code into a ffmpeg error code.
static AVFrame * find_frame(QSVContext *q, mfxFrameSurface1 *surf)
int top_field_first
If the content is interlaced, is top field displayed first.
static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
QSVFrame * work_frames
a linked list of frames currently being used by QSV
AVPixelFormat
Pixel format.
This structure stores compressed data.
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...