FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
qsvdec.c
Go to the documentation of this file.
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 
29 #include "libavutil/common.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/log.h"
32 #include "libavutil/pixfmt.h"
33 #include "libavutil/time.h"
34 
35 #include "avcodec.h"
36 #include "internal.h"
37 #include "qsv_internal.h"
38 #include "qsvdec.h"
39 
41 {
42  switch (format) {
43  case AV_PIX_FMT_YUV420P:
45  return AV_PIX_FMT_NV12;
46  default:
47  return AVERROR(ENOSYS);
48  }
49 }
50 
51 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
52 {
53  if (!session) {
54  if (!q->internal_session) {
56  if (ret < 0)
57  return ret;
58  }
59 
60  q->session = q->internal_session;
61  } else {
62  q->session = session;
63  }
64 
65  /* make sure the decoder is uninitialized */
66  MFXVideoDECODE_Close(q->session);
67 
68  return 0;
69 }
70 
71 int ff_qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
72 {
73  mfxVideoParam param = { { 0 } };
74  int ret;
75 
76  ret = qsv_init_session(avctx, q, session);
77  if (ret < 0) {
78  av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
79  return ret;
80  }
81 
82 
83  ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
84  if (ret < 0)
85  return ret;
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;
95  param.mfx.FrameInfo.Width = avctx->coded_width;
96  param.mfx.FrameInfo.Height = avctx->coded_height;
97  param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
98 
99  param.IOPattern = q->iopattern;
100  param.AsyncDepth = q->async_depth;
101  param.ExtParam = q->ext_buffers;
102  param.NumExtParam = q->nb_ext_buffers;
103 
104  ret = MFXVideoDECODE_Init(q->session, &param);
105  if (ret < 0) {
106  av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
107  return ff_qsv_error(ret);
108  }
109 
110  return 0;
111 }
112 
114 {
115  int ret;
116 
117  ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
118  if (ret < 0)
119  return ret;
120 
121  if (frame->frame->format == AV_PIX_FMT_QSV) {
122  frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
123  } else {
124  frame->surface_internal.Info.BitDepthLuma = 8;
125  frame->surface_internal.Info.BitDepthChroma = 8;
126  frame->surface_internal.Info.FourCC = MFX_FOURCC_NV12;
127  frame->surface_internal.Info.Width = avctx->coded_width;
128  frame->surface_internal.Info.Height = avctx->coded_height;
129  frame->surface_internal.Info.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
130 
131  frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
132  frame->surface_internal.Data.Y = frame->frame->data[0];
133  frame->surface_internal.Data.UV = frame->frame->data[1];
134 
135  frame->surface = &frame->surface_internal;
136  }
137 
138  return 0;
139 }
140 
142 {
143  QSVFrame *cur = q->work_frames;
144  while (cur) {
145  if (cur->surface && !cur->surface->Data.Locked) {
146  cur->surface = NULL;
147  av_frame_unref(cur->frame);
148  }
149  cur = cur->next;
150  }
151 }
152 
153 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
154 {
155  QSVFrame *frame, **last;
156  int ret;
157 
159 
160  frame = q->work_frames;
161  last = &q->work_frames;
162  while (frame) {
163  if (!frame->surface) {
164  ret = alloc_frame(avctx, frame);
165  if (ret < 0)
166  return ret;
167  *surf = frame->surface;
168  return 0;
169  }
170 
171  last = &frame->next;
172  frame = frame->next;
173  }
174 
175  frame = av_mallocz(sizeof(*frame));
176  if (!frame)
177  return AVERROR(ENOMEM);
178  frame->frame = av_frame_alloc();
179  if (!frame->frame) {
180  av_freep(&frame);
181  return AVERROR(ENOMEM);
182  }
183  *last = frame;
184 
185  ret = alloc_frame(avctx, frame);
186  if (ret < 0)
187  return ret;
188 
189  *surf = frame->surface;
190 
191  return 0;
192 }
193 
194 static AVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
195 {
196  QSVFrame *cur = q->work_frames;
197  while (cur) {
198  if (surf == cur->surface)
199  return cur->frame;
200  cur = cur->next;
201  }
202  return NULL;
203 }
204 
206  AVFrame *frame, int *got_frame,
207  AVPacket *avpkt)
208 {
209  mfxFrameSurface1 *insurf;
210  mfxFrameSurface1 *outsurf;
211  mfxSyncPoint sync;
212  mfxBitstream bs = { { { 0 } } };
213  int ret;
214 
215  if (avpkt->size) {
216  bs.Data = avpkt->data;
217  bs.DataLength = avpkt->size;
218  bs.MaxLength = bs.DataLength;
219  bs.TimeStamp = avpkt->pts;
220  }
221 
222  do {
223  ret = get_surface(avctx, q, &insurf);
224  if (ret < 0)
225  return ret;
226 
227  ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
228  insurf, &outsurf, &sync);
229  if (ret == MFX_WRN_DEVICE_BUSY)
230  av_usleep(1);
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) {
238  av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
239  return ff_qsv_error(ret);
240  }
241 
242  if (sync) {
243  AVFrame *src_frame;
244 
245  MFXVideoCORE_SyncOperation(q->session, sync, 60000);
246 
247  src_frame = find_frame(q, outsurf);
248  if (!src_frame) {
249  av_log(avctx, AV_LOG_ERROR,
250  "The returned surface does not correspond to any frame\n");
251  return AVERROR_BUG;
252  }
253 
254  ret = av_frame_ref(frame, src_frame);
255  if (ret < 0)
256  return ret;
257 
258  frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
259 
260  frame->repeat_pict =
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;
264  frame->top_field_first =
265  outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
266  frame->interlaced_frame =
267  !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
268 
269  *got_frame = 1;
270  }
271 
272  return bs.DataOffset;
273 }
274 
276 {
277  QSVFrame *cur = q->work_frames;
278 
279  while (cur) {
280  q->work_frames = cur->next;
281  av_frame_free(&cur->frame);
282  av_freep(&cur);
283  cur = q->work_frames;
284  }
285 
286  if (q->internal_session)
287  MFXClose(q->internal_session);
288 
289  return 0;
290 }
#define NULL
Definition: coverity.c:32
int iopattern
Definition: qsvdec.h:52
int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: qsvdec.c:205
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1424
int ff_qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
Definition: qsvdec.c:71
memory handling functions
int repeat_pict
When decoding, this signals how much the picture must be delayed.
Definition: frame.h:362
int size
Definition: avcodec.h:1163
mfxExtBuffer ** ext_buffers
Definition: qsvdec.h:54
mfxFrameSurface1 * surface
Definition: qsv_internal.h:35
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:76
int profile
profile
Definition: avcodec.h:2835
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
int ff_qsv_decode_close(QSVContext *q)
Definition: qsvdec.c:275
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:363
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1162
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:367
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
struct QSVFrame * next
Definition: qsv_internal.h:39
#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
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:91
Libavcodec external API header.
AVFrame * frame
Definition: qsv_internal.h:34
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:75
ret
Definition: avfilter.c:974
int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
Definition: qsv.c:28
int level
level
Definition: avcodec.h:2925
mfxFrameSurface1 surface_internal
Definition: qsv_internal.h:37
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
enum AVCodecID codec_id
Definition: avcodec.h:1258
mfxSession internal_session
Definition: qsvdec.h:43
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1241
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1035
static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
Definition: qsvdec.c:153
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
int ff_qsv_init_internal_session(AVCodecContext *avctx, mfxSession *session)
Definition: qsv.c:80
int coded_height
Definition: avcodec.h:1424
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
Definition: pixfmt.h:249
static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
Definition: qsvdec.c:51
static void qsv_clear_unused_frames(QSVContext *q)
Definition: qsvdec.c:141
int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
Definition: frame.h:262
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
common internal api header.
common internal and external API header
int ff_qsv_map_pixfmt(enum AVPixelFormat format)
Definition: qsvdec.c:40
int ff_qsv_error(int mfx_err)
Convert a libmfx error code into a ffmpeg error code.
Definition: qsv.c:45
static AVFrame * find_frame(QSVContext *q, mfxFrameSurface1 *surf)
Definition: qsvdec.c:194
pixel format definitions
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:372
mfxSession session
Definition: qsvdec.h:39
static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
Definition: qsvdec.c:113
#define av_freep(p)
int async_depth
Definition: qsvdec.h:51
QSVFrame * work_frames
a linked list of frames currently being used by QSV
Definition: qsvdec.h:48
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1139
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:969
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
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1155
int nb_ext_buffers
Definition: qsvdec.h:55