]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsvdec.c
Merge commit 'ebe8b5d947c41449c684f17c6826fe6bc46c0360'
[ffmpeg] / libavcodec / qsvdec.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.h"
38 #include "qsv_internal.h"
39 #include "qsvdec.h"
40
41 int ff_qsv_map_pixfmt(enum AVPixelFormat format)
42 {
43     switch (format) {
44     case AV_PIX_FMT_YUV420P:
45     case AV_PIX_FMT_YUVJ420P:
46         return AV_PIX_FMT_NV12;
47     default:
48         return AVERROR(ENOSYS);
49     }
50 }
51
52 int ff_qsv_decode_init(AVCodecContext *avctx, QSVContext *q, AVPacket *avpkt)
53 {
54     mfxVideoParam param = { { 0 } };
55     mfxBitstream bs   = { { { 0 } } };
56     int ret;
57
58     q->iopattern  = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
59     if (!q->session) {
60         if (avctx->hwaccel_context) {
61             AVQSVContext *qsv = avctx->hwaccel_context;
62
63             q->session        = qsv->session;
64             q->iopattern      = qsv->iopattern;
65             q->ext_buffers    = qsv->ext_buffers;
66             q->nb_ext_buffers = qsv->nb_ext_buffers;
67         }
68         if (!q->session) {
69             ret = ff_qsv_init_internal_session(avctx, &q->internal_qs, NULL);
70             if (ret < 0)
71                 return ret;
72
73             q->session = q->internal_qs.session;
74         }
75     }
76
77     if (avpkt->size) {
78         bs.Data       = avpkt->data;
79         bs.DataLength = avpkt->size;
80         bs.MaxLength  = bs.DataLength;
81         bs.TimeStamp  = avpkt->pts;
82     } else
83         return AVERROR_INVALIDDATA;
84
85     ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
86     if (ret < 0) {
87         av_log(avctx, AV_LOG_ERROR, "Unsupported codec_id %08x\n", avctx->codec_id);
88         return ret;
89     }
90
91     param.mfx.CodecId = ret;
92
93     ret = MFXVideoDECODE_DecodeHeader(q->session, &bs, &param);
94     if (MFX_ERR_MORE_DATA==ret) {
95         return AVERROR(EAGAIN);
96     } else if (ret < 0) {
97         av_log(avctx, AV_LOG_ERROR, "Decode header error %d\n", ret);
98         return ff_qsv_error(ret);
99     }
100     param.IOPattern   = q->iopattern;
101     param.AsyncDepth  = q->async_depth;
102     param.ExtParam    = q->ext_buffers;
103     param.NumExtParam = q->nb_ext_buffers;
104     param.mfx.FrameInfo.BitDepthLuma   = 8;
105     param.mfx.FrameInfo.BitDepthChroma = 8;
106
107     ret = MFXVideoDECODE_Init(q->session, &param);
108     if (ret < 0) {
109         av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
110         return ff_qsv_error(ret);
111     }
112
113     avctx->pix_fmt      = AV_PIX_FMT_NV12;
114     avctx->profile      = param.mfx.CodecProfile;
115     avctx->level        = param.mfx.CodecLevel;
116     avctx->coded_width  = param.mfx.FrameInfo.Width;
117     avctx->coded_height = param.mfx.FrameInfo.Height;
118     avctx->width        = param.mfx.FrameInfo.CropW - param.mfx.FrameInfo.CropX;
119     avctx->height       = param.mfx.FrameInfo.CropH - param.mfx.FrameInfo.CropY;
120
121     q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
122                                   (sizeof(mfxSyncPoint) + sizeof(QSVFrame*)));
123     if (!q->async_fifo)
124         return AVERROR(ENOMEM);
125
126
127     return 0;
128 }
129
130 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
131 {
132     int ret;
133
134     ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
135     if (ret < 0)
136         return ret;
137
138     if (frame->frame->format == AV_PIX_FMT_QSV) {
139         frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
140     } else {
141         frame->surface_internal.Info.BitDepthLuma   = 8;
142         frame->surface_internal.Info.BitDepthChroma = 8;
143         frame->surface_internal.Info.FourCC         = MFX_FOURCC_NV12;
144         frame->surface_internal.Info.Width          = avctx->coded_width;
145         frame->surface_internal.Info.Height         = avctx->coded_height;
146         frame->surface_internal.Info.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
147
148         frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
149         frame->surface_internal.Data.Y        = frame->frame->data[0];
150         frame->surface_internal.Data.UV       = frame->frame->data[1];
151
152         frame->surface = &frame->surface_internal;
153     }
154
155     return 0;
156 }
157
158 static void qsv_clear_unused_frames(QSVContext *q)
159 {
160     QSVFrame *cur = q->work_frames;
161     while (cur) {
162         if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
163             cur->surface = NULL;
164             av_frame_unref(cur->frame);
165         }
166         cur = cur->next;
167     }
168 }
169
170 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
171 {
172     QSVFrame *frame, **last;
173     int ret;
174
175     qsv_clear_unused_frames(q);
176
177     frame = q->work_frames;
178     last  = &q->work_frames;
179     while (frame) {
180         if (!frame->surface) {
181             ret = alloc_frame(avctx, frame);
182             if (ret < 0)
183                 return ret;
184             *surf = frame->surface;
185             return 0;
186         }
187
188         last  = &frame->next;
189         frame = frame->next;
190     }
191
192     frame = av_mallocz(sizeof(*frame));
193     if (!frame)
194         return AVERROR(ENOMEM);
195     frame->frame = av_frame_alloc();
196     if (!frame->frame) {
197         av_freep(&frame);
198         return AVERROR(ENOMEM);
199     }
200     *last = frame;
201
202     ret = alloc_frame(avctx, frame);
203     if (ret < 0)
204         return ret;
205
206     *surf = frame->surface;
207
208     return 0;
209 }
210
211 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
212 {
213     QSVFrame *cur = q->work_frames;
214     while (cur) {
215         if (surf == cur->surface)
216             return cur;
217         cur = cur->next;
218     }
219     return NULL;
220 }
221
222 int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q,
223                   AVFrame *frame, int *got_frame,
224                   AVPacket *avpkt)
225 {
226     QSVFrame *out_frame;
227     mfxFrameSurface1 *insurf;
228     mfxFrameSurface1 *outsurf;
229     mfxSyncPoint sync;
230     mfxBitstream bs = { { { 0 } } };
231     int ret;
232
233     if (avpkt->size) {
234         bs.Data       = avpkt->data;
235         bs.DataLength = avpkt->size;
236         bs.MaxLength  = bs.DataLength;
237         bs.TimeStamp  = avpkt->pts;
238     }
239
240     do {
241         ret = get_surface(avctx, q, &insurf);
242         if (ret < 0)
243             return ret;
244
245         ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
246                                               insurf, &outsurf, &sync);
247         if (ret == MFX_WRN_DEVICE_BUSY)
248             av_usleep(1);
249
250     } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
251
252     if (ret != MFX_ERR_NONE &&
253         ret != MFX_ERR_MORE_DATA &&
254         ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
255         ret != MFX_ERR_MORE_SURFACE) {
256         av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
257         return ff_qsv_error(ret);
258     }
259
260     if (sync) {
261         QSVFrame *out_frame = find_frame(q, outsurf);
262
263         if (!out_frame) {
264             av_log(avctx, AV_LOG_ERROR,
265                    "The returned surface does not correspond to any frame\n");
266             return AVERROR_BUG;
267         }
268
269         out_frame->queued = 1;
270         av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
271         av_fifo_generic_write(q->async_fifo, &sync,      sizeof(sync),      NULL);
272     }
273
274     if (!av_fifo_space(q->async_fifo) ||
275         (!avpkt->size && av_fifo_size(q->async_fifo))) {
276         AVFrame *src_frame;
277
278         av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
279         av_fifo_generic_read(q->async_fifo, &sync,      sizeof(sync),      NULL);
280         out_frame->queued = 0;
281
282         MFXVideoCORE_SyncOperation(q->session, sync, 60000);
283
284         src_frame = out_frame->frame;
285
286         ret = av_frame_ref(frame, src_frame);
287         if (ret < 0)
288             return ret;
289
290         outsurf = out_frame->surface;
291
292         frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
293
294         frame->repeat_pict =
295             outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
296             outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
297             outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
298         frame->top_field_first =
299             outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
300         frame->interlaced_frame =
301             !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
302
303         *got_frame = 1;
304     }
305
306     return bs.DataOffset;
307 }
308
309 int ff_qsv_decode_close(QSVContext *q)
310 {
311     QSVFrame *cur = q->work_frames;
312
313     while (cur) {
314         q->work_frames = cur->next;
315         av_frame_free(&cur->frame);
316         av_freep(&cur);
317         cur = q->work_frames;
318     }
319
320     av_fifo_free(q->async_fifo);
321     q->async_fifo = NULL;
322
323     MFXVideoDECODE_Close(q->session);
324     q->session = NULL;
325
326     ff_qsv_close_internal_session(&q->internal_qs);
327
328     return 0;
329 }