]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsvdec.c
hevc: change the stride of the MC buffer to be in bytes instead of elements
[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 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.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 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session)
53 {
54     if (!session) {
55         if (!q->internal_session) {
56             int ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
57                                                    q->load_plugins);
58             if (ret < 0)
59                 return ret;
60         }
61
62         q->session = q->internal_session;
63     } else {
64         q->session = session;
65     }
66
67     /* make sure the decoder is uninitialized */
68     MFXVideoDECODE_Close(q->session);
69
70     return 0;
71 }
72
73 static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxSession session)
74 {
75     mfxVideoParam param = { { 0 } };
76     int ret;
77
78     if (!q->async_fifo) {
79         q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
80                                       (sizeof(mfxSyncPoint) + sizeof(QSVFrame*)));
81         if (!q->async_fifo)
82             return AVERROR(ENOMEM);
83     }
84
85     ret = qsv_init_session(avctx, q, session);
86     if (ret < 0) {
87         av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
88         return ret;
89     }
90
91
92     ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
93     if (ret < 0)
94         return ret;
95
96     param.mfx.CodecId      = ret;
97     param.mfx.CodecProfile = avctx->profile;
98     param.mfx.CodecLevel   = avctx->level;
99
100     param.mfx.FrameInfo.BitDepthLuma   = 8;
101     param.mfx.FrameInfo.BitDepthChroma = 8;
102     param.mfx.FrameInfo.Shift          = 0;
103     param.mfx.FrameInfo.FourCC         = MFX_FOURCC_NV12;
104     param.mfx.FrameInfo.Width          = avctx->coded_width;
105     param.mfx.FrameInfo.Height         = avctx->coded_height;
106     param.mfx.FrameInfo.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
107
108     param.IOPattern   = q->iopattern;
109     param.AsyncDepth  = q->async_depth;
110     param.ExtParam    = q->ext_buffers;
111     param.NumExtParam = q->nb_ext_buffers;
112
113     ret = MFXVideoDECODE_Init(q->session, &param);
114     if (ret < 0) {
115         av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
116         return ff_qsv_error(ret);
117     }
118
119     return 0;
120 }
121
122 static int alloc_frame(AVCodecContext *avctx, QSVFrame *frame)
123 {
124     int ret;
125
126     ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
127     if (ret < 0)
128         return ret;
129
130     if (frame->frame->format == AV_PIX_FMT_QSV) {
131         frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
132     } else {
133         frame->surface_internal.Info.BitDepthLuma   = 8;
134         frame->surface_internal.Info.BitDepthChroma = 8;
135         frame->surface_internal.Info.FourCC         = MFX_FOURCC_NV12;
136         frame->surface_internal.Info.Width          = avctx->coded_width;
137         frame->surface_internal.Info.Height         = avctx->coded_height;
138         frame->surface_internal.Info.ChromaFormat   = MFX_CHROMAFORMAT_YUV420;
139
140         frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
141         frame->surface_internal.Data.Y        = frame->frame->data[0];
142         frame->surface_internal.Data.UV       = frame->frame->data[1];
143
144         frame->surface = &frame->surface_internal;
145     }
146
147     return 0;
148 }
149
150 static void qsv_clear_unused_frames(QSVContext *q)
151 {
152     QSVFrame *cur = q->work_frames;
153     while (cur) {
154         if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
155             cur->surface = NULL;
156             av_frame_unref(cur->frame);
157         }
158         cur = cur->next;
159     }
160 }
161
162 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
163 {
164     QSVFrame *frame, **last;
165     int ret;
166
167     qsv_clear_unused_frames(q);
168
169     frame = q->work_frames;
170     last  = &q->work_frames;
171     while (frame) {
172         if (!frame->surface) {
173             ret = alloc_frame(avctx, frame);
174             if (ret < 0)
175                 return ret;
176             *surf = frame->surface;
177             return 0;
178         }
179
180         last  = &frame->next;
181         frame = frame->next;
182     }
183
184     frame = av_mallocz(sizeof(*frame));
185     if (!frame)
186         return AVERROR(ENOMEM);
187     frame->frame = av_frame_alloc();
188     if (!frame->frame) {
189         av_freep(&frame);
190         return AVERROR(ENOMEM);
191     }
192     *last = frame;
193
194     ret = alloc_frame(avctx, frame);
195     if (ret < 0)
196         return ret;
197
198     *surf = frame->surface;
199
200     return 0;
201 }
202
203 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
204 {
205     QSVFrame *cur = q->work_frames;
206     while (cur) {
207         if (surf == cur->surface)
208             return cur;
209         cur = cur->next;
210     }
211     return NULL;
212 }
213
214 static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
215                       AVFrame *frame, int *got_frame,
216                       AVPacket *avpkt)
217 {
218     QSVFrame *out_frame;
219     mfxFrameSurface1 *insurf;
220     mfxFrameSurface1 *outsurf;
221     mfxSyncPoint sync;
222     mfxBitstream bs = { { { 0 } } };
223     int ret;
224
225     if (avpkt->size) {
226         bs.Data       = avpkt->data;
227         bs.DataLength = avpkt->size;
228         bs.MaxLength  = bs.DataLength;
229         bs.TimeStamp  = avpkt->pts;
230     }
231
232     do {
233         ret = get_surface(avctx, q, &insurf);
234         if (ret < 0)
235             return ret;
236
237         ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
238                                               insurf, &outsurf, &sync);
239         if (ret == MFX_WRN_DEVICE_BUSY)
240             av_usleep(1);
241
242     } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
243
244     if (ret != MFX_ERR_NONE &&
245         ret != MFX_ERR_MORE_DATA &&
246         ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
247         ret != MFX_ERR_MORE_SURFACE) {
248         av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
249         return ff_qsv_error(ret);
250     }
251
252     /* make sure we do not enter an infinite loop if the SDK
253      * did not consume any data and did not return anything */
254     if (!sync && !bs.DataOffset) {
255         av_log(avctx, AV_LOG_WARNING, "A decode call did not consume any data\n");
256         bs.DataOffset = avpkt->size;
257     }
258
259     if (sync) {
260         QSVFrame *out_frame = find_frame(q, outsurf);
261
262         if (!out_frame) {
263             av_log(avctx, AV_LOG_ERROR,
264                    "The returned surface does not correspond to any frame\n");
265             return AVERROR_BUG;
266         }
267
268         out_frame->queued = 1;
269         av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
270         av_fifo_generic_write(q->async_fifo, &sync,      sizeof(sync),      NULL);
271     }
272
273     if (!av_fifo_space(q->async_fifo) ||
274         (!avpkt->size && av_fifo_size(q->async_fifo))) {
275         AVFrame *src_frame;
276
277         av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
278         av_fifo_generic_read(q->async_fifo, &sync,      sizeof(sync),      NULL);
279         out_frame->queued = 0;
280
281         MFXVideoCORE_SyncOperation(q->session, sync, 60000);
282
283         src_frame = out_frame->frame;
284
285         ret = av_frame_ref(frame, src_frame);
286         if (ret < 0)
287             return ret;
288
289         outsurf = out_frame->surface;
290
291         frame->pkt_pts = frame->pts = outsurf->Data.TimeStamp;
292
293         frame->repeat_pict =
294             outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
295             outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
296             outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
297         frame->top_field_first =
298             outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
299         frame->interlaced_frame =
300             !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
301
302         *got_frame = 1;
303     }
304
305     return bs.DataOffset;
306 }
307
308 int ff_qsv_decode_close(QSVContext *q)
309 {
310     QSVFrame *cur = q->work_frames;
311
312     if (q->session)
313         MFXVideoDECODE_Close(q->session);
314
315     while (cur) {
316         q->work_frames = cur->next;
317         av_frame_free(&cur->frame);
318         av_freep(&cur);
319         cur = q->work_frames;
320     }
321
322     av_fifo_free(q->async_fifo);
323     q->async_fifo = NULL;
324
325     av_parser_close(q->parser);
326     avcodec_free_context(&q->avctx_internal);
327
328     if (q->internal_session)
329         MFXClose(q->internal_session);
330
331     return 0;
332 }
333
334 int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
335                         AVFrame *frame, int *got_frame, AVPacket *pkt)
336 {
337     uint8_t *dummy_data;
338     int dummy_size;
339     int ret;
340
341     if (!q->avctx_internal) {
342         q->avctx_internal = avcodec_alloc_context3(NULL);
343         if (!q->avctx_internal)
344             return AVERROR(ENOMEM);
345
346         if (avctx->extradata) {
347             q->avctx_internal->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
348             if (!q->avctx_internal->extradata)
349                 return AVERROR(ENOMEM);
350
351             memcpy(q->avctx_internal->extradata, avctx->extradata,
352                    avctx->extradata_size);
353             q->avctx_internal->extradata_size = avctx->extradata_size;
354         }
355
356         q->parser = av_parser_init(avctx->codec_id);
357         if (!q->parser)
358             return AVERROR(ENOMEM);
359
360         q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
361         q->orig_pix_fmt   = AV_PIX_FMT_NONE;
362     }
363
364     if (!pkt->size)
365         return qsv_decode(avctx, q, frame, got_frame, pkt);
366
367     /* we assume the packets are already split properly and want
368      * just the codec parameters here */
369     av_parser_parse2(q->parser, q->avctx_internal,
370                      &dummy_data, &dummy_size,
371                      pkt->data, pkt->size, pkt->pts, pkt->dts,
372                      pkt->pos);
373
374     /* TODO: flush delayed frames on reinit */
375     if (q->parser->format       != q->orig_pix_fmt    ||
376         q->parser->coded_width  != avctx->coded_width ||
377         q->parser->coded_height != avctx->coded_height) {
378         mfxSession session = NULL;
379
380         enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
381                                            AV_PIX_FMT_NONE,
382                                            AV_PIX_FMT_NONE };
383         enum AVPixelFormat qsv_format;
384
385         qsv_format = ff_qsv_map_pixfmt(q->parser->format);
386         if (qsv_format < 0) {
387             av_log(avctx, AV_LOG_ERROR,
388                    "Only 8-bit YUV420 streams are supported.\n");
389             ret = AVERROR(ENOSYS);
390             goto reinit_fail;
391         }
392
393         q->orig_pix_fmt     = q->parser->format;
394         avctx->pix_fmt      = pix_fmts[1] = qsv_format;
395         avctx->width        = q->parser->width;
396         avctx->height       = q->parser->height;
397         avctx->coded_width  = q->parser->coded_width;
398         avctx->coded_height = q->parser->coded_height;
399         avctx->level        = q->avctx_internal->level;
400         avctx->profile      = q->avctx_internal->profile;
401
402         ret = ff_get_format(avctx, pix_fmts);
403         if (ret < 0)
404             goto reinit_fail;
405
406         avctx->pix_fmt = ret;
407
408         if (avctx->hwaccel_context) {
409             AVQSVContext *user_ctx = avctx->hwaccel_context;
410             session           = user_ctx->session;
411             q->iopattern      = user_ctx->iopattern;
412             q->ext_buffers    = user_ctx->ext_buffers;
413             q->nb_ext_buffers = user_ctx->nb_ext_buffers;
414         }
415
416         ret = qsv_decode_init(avctx, q, session);
417         if (ret < 0)
418             goto reinit_fail;
419     }
420
421     return qsv_decode(avctx, q, frame, got_frame, pkt);
422
423 reinit_fail:
424     q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
425     return ret;
426 }
427
428 void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
429 {
430     q->orig_pix_fmt = AV_PIX_FMT_NONE;
431 }