]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsv.c
Merge commit 'ac0e54fda9305cc7d149007f5b512bb8619f7c78'
[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 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
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             if (impl & MFX_IMPL_SOFTWARE)
121                 desc = "software";
122             else if (impl & MFX_IMPL_HARDWARE)
123                 desc = "hardware accelerated";
124             else
125                 desc = "unknown";
126
127             av_log(avctx, AV_LOG_VERBOSE,
128                    "Initialized an internal MFX session using %s implementation\n",
129                    desc);
130         }
131
132         q->session = q->internal_session;
133     } else {
134         q->session = session;
135     }
136
137     /* make sure the decoder is uninitialized */
138     MFXVideoDECODE_Close(q->session);
139
140     return 0;
141 }
142
143 int ff_qsv_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
144 {
145     mfxVideoParam param = { { 0 } };
146     int ret;
147
148     ret = qsv_init_session(avctx, q, session);
149     if (ret < 0) {
150         av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
151         return ret;
152     }
153
154
155     ret = codec_id_to_mfx(avctx->codec_id);
156     if (ret < 0)
157         return ret;
158
159     param.mfx.CodecId      = ret;
160     param.mfx.CodecProfile = avctx->profile;
161     param.mfx.CodecLevel   = avctx->level;
162
163     param.mfx.FrameInfo.BitDepthLuma   = 8;
164     param.mfx.FrameInfo.BitDepthChroma = 8;
165     param.mfx.FrameInfo.Shift          = 0;
166     param.mfx.FrameInfo.FourCC         = MFX_FOURCC_NV12;
167     param.mfx.FrameInfo.Width          = avctx->coded_width;
168     param.mfx.FrameInfo.Height         = avctx->coded_height;
169     param.mfx.FrameInfo.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
170
171     param.IOPattern   = q->iopattern;
172     param.AsyncDepth  = q->async_depth;
173     param.ExtParam    = q->ext_buffers;
174     param.NumExtParam = q->nb_ext_buffers;
175
176     ret = MFXVideoDECODE_Init(q->session, &param);
177     if (ret < 0) {
178         av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
179         return ff_qsv_error(ret);
180     }
181
182     return 0;
183 }
184
185 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
186 {
187     int ret;
188
189     ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
190     if (ret < 0)
191         return ret;
192
193     if (frame->frame->format == AV_PIX_FMT_QSV) {
194         frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
195     } else {
196         frame->surface_internal.Info.BitDepthLuma   = 8;
197         frame->surface_internal.Info.BitDepthChroma = 8;
198         frame->surface_internal.Info.FourCC         = MFX_FOURCC_NV12;
199         frame->surface_internal.Info.Width          = avctx->coded_width;
200         frame->surface_internal.Info.Height         = avctx->coded_height;
201         frame->surface_internal.Info.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
202
203         frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
204         frame->surface_internal.Data.Y        = frame->frame->data[0];
205         frame->surface_internal.Data.UV       = frame->frame->data[1];
206
207         frame->surface = &frame->surface_internal;
208     }
209
210     return 0;
211 }
212
213 static void qsv_clear_unused_frames(QSVContext *q)
214 {
215     QSVFrame *cur = q->work_frames;
216     while (cur) {
217         if (cur->surface && !cur->surface->Data.Locked) {
218             cur->surface = NULL;
219             av_frame_unref(cur->frame);
220         }
221         cur = cur->next;
222     }
223 }
224
225 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
226 {
227     QSVFrame *frame, **last;
228     int ret;
229
230     qsv_clear_unused_frames(q);
231
232     frame = q->work_frames;
233     last  = &q->work_frames;
234     while (frame) {
235         if (!frame->surface) {
236             ret = alloc_frame(avctx, frame);
237             if (ret < 0)
238                 return ret;
239             *surf = frame->surface;
240             return 0;
241         }
242
243         last  = &frame->next;
244         frame = frame->next;
245     }
246
247     frame = av_mallocz(sizeof(*frame));
248     if (!frame)
249         return AVERROR(ENOMEM);
250     frame->frame = av_frame_alloc();
251     if (!frame->frame) {
252         av_freep(&frame);
253         return AVERROR(ENOMEM);
254     }
255     *last = frame;
256
257     ret = alloc_frame(avctx, frame);
258     if (ret < 0)
259         return ret;
260
261     *surf = frame->surface;
262
263     return 0;
264 }
265
266 static AVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
267 {
268     QSVFrame *cur = q->work_frames;
269     while (cur) {
270         if (surf == cur->surface)
271             return cur->frame;
272         cur = cur->next;
273     }
274     return NULL;
275 }
276
277 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
278                   AVFrame *frame, int *got_frame,
279                   AVPacket *avpkt)
280 {
281     mfxFrameSurface1 *insurf;
282     mfxFrameSurface1 *outsurf;
283     mfxSyncPoint sync;
284     mfxBitstream bs = { { { 0 } } };
285     int ret;
286
287     if (avpkt->size) {
288         bs.Data       = avpkt->data;
289         bs.DataLength = avpkt->size;
290         bs.MaxLength  = bs.DataLength;
291         bs.TimeStamp  = avpkt->pts;
292     }
293
294     do {
295         ret = get_surface(avctx, q, &insurf);
296         if (ret < 0)
297             return ret;
298
299         ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
300                                               insurf, &outsurf, &sync);
301         if (ret == MFX_WRN_DEVICE_BUSY)
302             av_usleep(1);
303
304     } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
305
306     if (ret != MFX_ERR_NONE &&
307         ret != MFX_ERR_MORE_DATA &&
308         ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
309         ret != MFX_ERR_MORE_SURFACE) {
310         av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
311         return ff_qsv_error(ret);
312     }
313
314     if (sync) {
315         AVFrame *src_frame;
316
317         MFXVideoCORE_SyncOperation(q->session, sync, 60000);
318
319         src_frame = find_frame(q, outsurf);
320         if (!src_frame) {
321             av_log(avctx, AV_LOG_ERROR,
322                    "The returned surface does not correspond to any frame\n");
323             return AVERROR_BUG;
324         }
325
326         ret = av_frame_ref(frame, src_frame);
327         if (ret < 0)
328             return ret;
329
330         frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
331
332         frame->repeat_pict =
333             outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
334             outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
335             outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
336         frame->top_field_first =
337             outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
338         frame->interlaced_frame =
339             !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
340
341         *got_frame = 1;
342     }
343
344     return bs.DataOffset;
345 }
346
347 int ff_qsv_close(QSVContext *q)
348 {
349     QSVFrame *cur = q->work_frames;
350
351     while (cur) {
352         q->work_frames = cur->next;
353         av_frame_free(&cur->frame);
354         av_freep(&cur);
355         cur = q->work_frames;
356     }
357
358     if (q->internal_session)
359         MFXClose(q->internal_session);
360
361     return 0;
362 }