]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsv.c
qsv: Improve the log message of when initializing MFX_IMPL_HARDWARE{2, 3, 4}
[ffmpeg] / libavcodec / qsv.c
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 Libav.
8  *
9  * Libav 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  * Libav 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 Libav; 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
39 int ff_qsv_error(int mfx_err)
40 {
41     switch (mfx_err) {
42     case MFX_ERR_NONE:
43         return 0;
44     case MFX_ERR_MEMORY_ALLOC:
45     case MFX_ERR_NOT_ENOUGH_BUFFER:
46         return AVERROR(ENOMEM);
47     case MFX_ERR_INVALID_HANDLE:
48         return AVERROR(EINVAL);
49     case MFX_ERR_DEVICE_FAILED:
50     case MFX_ERR_DEVICE_LOST:
51     case MFX_ERR_LOCK_MEMORY:
52         return AVERROR(EIO);
53     case MFX_ERR_NULL_PTR:
54     case MFX_ERR_UNDEFINED_BEHAVIOR:
55     case MFX_ERR_NOT_INITIALIZED:
56         return AVERROR_BUG;
57     case MFX_ERR_UNSUPPORTED:
58     case MFX_ERR_NOT_FOUND:
59         return AVERROR(ENOSYS);
60     case MFX_ERR_MORE_DATA:
61     case MFX_ERR_MORE_SURFACE:
62     case MFX_ERR_MORE_BITSTREAM:
63         return AVERROR(EAGAIN);
64     case MFX_ERR_INCOMPATIBLE_VIDEO_PARAM:
65     case MFX_ERR_INVALID_VIDEO_PARAM:
66         return AVERROR(EINVAL);
67     case MFX_ERR_ABORTED:
68     case MFX_ERR_UNKNOWN:
69     default:
70         return AVERROR_UNKNOWN;
71     }
72 }
73
74 int ff_qsv_map_pixfmt(enum AVPixelFormat format)
75 {
76     switch (format) {
77     case AV_PIX_FMT_YUV420P:
78     case AV_PIX_FMT_YUVJ420P:
79         return AV_PIX_FMT_NV12;
80     default:
81         return AVERROR(ENOSYS);
82     }
83 }
84
85 static int codec_id_to_mfx(enum AVCodecID codec_id)
86 {
87     switch (codec_id) {
88     case AV_CODEC_ID_H264:
89         return MFX_CODEC_AVC;
90     case AV_CODEC_ID_MPEG1VIDEO:
91     case AV_CODEC_ID_MPEG2VIDEO:
92         return MFX_CODEC_MPEG2;
93     case AV_CODEC_ID_VC1:
94         return MFX_CODEC_VC1;
95     default:
96         break;
97     }
98
99     return AVERROR(ENOSYS);
100 }
101
102 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
103 {
104     if (!session) {
105         if (!q->internal_session) {
106             mfxIMPL impl   = MFX_IMPL_AUTO_ANY;
107             mfxVersion ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
108
109             const char *desc;
110             int ret;
111
112             ret = MFXInit(impl, &ver, &q->internal_session);
113             if (ret < 0) {
114                 av_log(avctx, AV_LOG_ERROR, "Error initializing an internal MFX session\n");
115                 return ff_qsv_error(ret);
116             }
117
118             MFXQueryIMPL(q->internal_session, &impl);
119
120             switch (MFX_IMPL_BASETYPE(impl)) {
121             case MFX_IMPL_SOFTWARE:
122                 desc = "software";
123                 break;
124             case MFX_IMPL_HARDWARE:
125             case MFX_IMPL_HARDWARE2:
126             case MFX_IMPL_HARDWARE3:
127             case MFX_IMPL_HARDWARE4:
128                 desc = "hardware accelerated";
129                 break;
130             default:
131                 desc = "unknown";
132             }
133
134             av_log(avctx, AV_LOG_VERBOSE,
135                    "Initialized an internal MFX session using %s implementation\n",
136                    desc);
137         }
138
139         q->session = q->internal_session;
140     } else {
141         q->session = session;
142     }
143
144     /* make sure the decoder is uninitialized */
145     MFXVideoDECODE_Close(q->session);
146
147     return 0;
148 }
149
150 int ff_qsv_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
151 {
152     mfxVideoParam param = { { 0 } };
153     int ret;
154
155     ret = qsv_init_session(avctx, q, session);
156     if (ret < 0) {
157         av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
158         return ret;
159     }
160
161
162     ret = codec_id_to_mfx(avctx->codec_id);
163     if (ret < 0)
164         return ret;
165
166     param.mfx.CodecId      = ret;
167     param.mfx.CodecProfile = avctx->profile;
168     param.mfx.CodecLevel   = avctx->level;
169
170     param.mfx.FrameInfo.BitDepthLuma   = 8;
171     param.mfx.FrameInfo.BitDepthChroma = 8;
172     param.mfx.FrameInfo.Shift          = 0;
173     param.mfx.FrameInfo.FourCC         = MFX_FOURCC_NV12;
174     param.mfx.FrameInfo.Width          = avctx->coded_width;
175     param.mfx.FrameInfo.Height         = avctx->coded_height;
176     param.mfx.FrameInfo.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
177
178     param.IOPattern   = q->iopattern;
179     param.AsyncDepth  = q->async_depth;
180     param.ExtParam    = q->ext_buffers;
181     param.NumExtParam = q->nb_ext_buffers;
182
183     ret = MFXVideoDECODE_Init(q->session, &param);
184     if (ret < 0) {
185         av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
186         return ff_qsv_error(ret);
187     }
188
189     return 0;
190 }
191
192 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
193 {
194     int ret;
195
196     ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
197     if (ret < 0)
198         return ret;
199
200     if (frame->frame->format == AV_PIX_FMT_QSV) {
201         frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
202     } else {
203         frame->surface_internal.Info.BitDepthLuma   = 8;
204         frame->surface_internal.Info.BitDepthChroma = 8;
205         frame->surface_internal.Info.FourCC         = MFX_FOURCC_NV12;
206         frame->surface_internal.Info.Width          = avctx->coded_width;
207         frame->surface_internal.Info.Height         = avctx->coded_height;
208         frame->surface_internal.Info.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
209
210         frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
211         frame->surface_internal.Data.Y        = frame->frame->data[0];
212         frame->surface_internal.Data.UV       = frame->frame->data[1];
213
214         frame->surface = &frame->surface_internal;
215     }
216
217     return 0;
218 }
219
220 static void qsv_clear_unused_frames(QSVContext *q)
221 {
222     QSVFrame *cur = q->work_frames;
223     while (cur) {
224         if (cur->surface && !cur->surface->Data.Locked) {
225             cur->surface = NULL;
226             av_frame_unref(cur->frame);
227         }
228         cur = cur->next;
229     }
230 }
231
232 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
233 {
234     QSVFrame *frame, **last;
235     int ret;
236
237     qsv_clear_unused_frames(q);
238
239     frame = q->work_frames;
240     last  = &q->work_frames;
241     while (frame) {
242         if (!frame->surface) {
243             ret = alloc_frame(avctx, frame);
244             if (ret < 0)
245                 return ret;
246             *surf = frame->surface;
247             return 0;
248         }
249
250         last  = &frame->next;
251         frame = frame->next;
252     }
253
254     frame = av_mallocz(sizeof(*frame));
255     if (!frame)
256         return AVERROR(ENOMEM);
257     frame->frame = av_frame_alloc();
258     if (!frame->frame) {
259         av_freep(&frame);
260         return AVERROR(ENOMEM);
261     }
262     *last = frame;
263
264     ret = alloc_frame(avctx, frame);
265     if (ret < 0)
266         return ret;
267
268     *surf = frame->surface;
269
270     return 0;
271 }
272
273 static AVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
274 {
275     QSVFrame *cur = q->work_frames;
276     while (cur) {
277         if (surf == cur->surface)
278             return cur->frame;
279         cur = cur->next;
280     }
281     return NULL;
282 }
283
284 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
285                   AVFrame *frame, int *got_frame,
286                   AVPacket *avpkt)
287 {
288     mfxFrameSurface1 *insurf;
289     mfxFrameSurface1 *outsurf;
290     mfxSyncPoint sync;
291     mfxBitstream bs = { { { 0 } } };
292     int ret;
293
294     if (avpkt->size) {
295         bs.Data       = avpkt->data;
296         bs.DataLength = avpkt->size;
297         bs.MaxLength  = bs.DataLength;
298         bs.TimeStamp  = avpkt->pts;
299     }
300
301     do {
302         ret = get_surface(avctx, q, &insurf);
303         if (ret < 0)
304             return ret;
305
306         ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
307                                               insurf, &outsurf, &sync);
308         if (ret == MFX_WRN_DEVICE_BUSY)
309             av_usleep(1);
310
311     } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
312
313     if (ret != MFX_ERR_NONE &&
314         ret != MFX_ERR_MORE_DATA &&
315         ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
316         ret != MFX_ERR_MORE_SURFACE) {
317         av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
318         return ff_qsv_error(ret);
319     }
320
321     if (sync) {
322         AVFrame *src_frame;
323
324         MFXVideoCORE_SyncOperation(q->session, sync, 60000);
325
326         src_frame = find_frame(q, outsurf);
327         if (!src_frame) {
328             av_log(avctx, AV_LOG_ERROR,
329                    "The returned surface does not correspond to any frame\n");
330             return AVERROR_BUG;
331         }
332
333         ret = av_frame_ref(frame, src_frame);
334         if (ret < 0)
335             return ret;
336
337         frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
338
339         frame->repeat_pict =
340             outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
341             outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
342             outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
343         frame->top_field_first =
344             outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
345         frame->interlaced_frame =
346             !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
347
348         *got_frame = 1;
349     }
350
351     return bs.DataOffset;
352 }
353
354 int ff_qsv_close(QSVContext *q)
355 {
356     QSVFrame *cur = q->work_frames;
357
358     while (cur) {
359         q->work_frames = cur->next;
360         av_frame_free(&cur->frame);
361         av_freep(&cur);
362         cur = q->work_frames;
363     }
364
365     if (q->internal_session)
366         MFXClose(q->internal_session);
367
368     return 0;
369 }