blob: 929b9af4266ede9d64fbf6e098b333e3f5efd8f7 [file] [log] [blame]
Anton Khirnov4e08c822015-02-10 09:40:591/*
2 * Intel MediaSDK QSV codec-independent code
3 *
4 * copyright (c) 2013 Luca Barbato
5 * copyright (c) 2015 Anton Khirnov <[email protected]>
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"
Anton Khirnova0524d92016-05-27 11:23:1930#include "libavutil/hwcontext.h"
31#include "libavutil/hwcontext_qsv.h"
Anton Khirnov4e08c822015-02-10 09:40:5932#include "libavutil/mem.h"
33#include "libavutil/log.h"
Anton Khirnov92736c72016-06-22 09:53:0034#include "libavutil/pixdesc.h"
Anton Khirnov4e08c822015-02-10 09:40:5935#include "libavutil/pixfmt.h"
36#include "libavutil/time.h"
37
38#include "avcodec.h"
39#include "internal.h"
Anton Khirnov96dca082015-07-21 16:57:4940#include "qsv.h"
Anton Khirnovd0a63d82015-03-13 07:13:0041#include "qsv_internal.h"
Anton Khirnovb04d0092015-03-13 06:55:5342#include "qsvdec.h"
Anton Khirnov4e08c822015-02-10 09:40:5943
Anton Khirnova0524d92016-05-27 11:23:1944static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
45 AVBufferRef *hw_frames_ref)
Anton Khirnov4e08c822015-02-10 09:40:5946{
Anton Khirnova0524d92016-05-27 11:23:1947 int ret;
48
49 if (session) {
50 q->session = session;
51 } else if (hw_frames_ref) {
52 if (q->internal_session) {
53 MFXClose(q->internal_session);
54 q->internal_session = NULL;
55 }
56 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
57
58 q->frames_ctx.hw_frames_ctx = av_buffer_ref(hw_frames_ref);
59 if (!q->frames_ctx.hw_frames_ctx)
60 return AVERROR(ENOMEM);
61
62 ret = ff_qsv_init_session_hwcontext(avctx, &q->internal_session,
63 &q->frames_ctx, q->load_plugins,
64 q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY);
65 if (ret < 0) {
66 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
67 return ret;
68 }
69
70 q->session = q->internal_session;
71 } else {
Anton Khirnov4e08c822015-02-10 09:40:5972 if (!q->internal_session) {
Anton Khirnova0524d92016-05-27 11:23:1973 ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
74 q->load_plugins);
Anton Khirnovd0a63d82015-03-13 07:13:0075 if (ret < 0)
76 return ret;
Anton Khirnov4e08c822015-02-10 09:40:5977 }
78
79 q->session = q->internal_session;
Anton Khirnov4e08c822015-02-10 09:40:5980 }
81
82 /* make sure the decoder is uninitialized */
83 MFXVideoDECODE_Close(q->session);
84
85 return 0;
86}
87
Anton Khirnov6f19bbc2016-05-21 16:26:4088static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q)
Anton Khirnov4e08c822015-02-10 09:40:5989{
Anton Khirnov92736c72016-06-22 09:53:0090 const AVPixFmtDescriptor *desc;
Anton Khirnov6f19bbc2016-05-21 16:26:4091 mfxSession session = NULL;
Anton Khirnova0524d92016-05-27 11:23:1992 int iopattern = 0;
Anton Khirnov4e08c822015-02-10 09:40:5993 mfxVideoParam param = { { 0 } };
94 int ret;
95
Anton Khirnov92736c72016-06-22 09:53:0096 desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
97 if (!desc)
98 return AVERROR_BUG;
99
Anton Khirnov22522d92015-07-21 17:41:59100 if (!q->async_fifo) {
101 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
Anton Khirnov3c536272016-02-22 09:48:34102 (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));
Anton Khirnov22522d92015-07-21 17:41:59103 if (!q->async_fifo)
104 return AVERROR(ENOMEM);
105 }
Anton Khirnovf5c4d382015-07-14 16:16:26106
Anton Khirnov6f19bbc2016-05-21 16:26:40107 if (avctx->hwaccel_context) {
108 AVQSVContext *user_ctx = avctx->hwaccel_context;
109 session = user_ctx->session;
Anton Khirnova0524d92016-05-27 11:23:19110 iopattern = user_ctx->iopattern;
Anton Khirnov6f19bbc2016-05-21 16:26:40111 q->ext_buffers = user_ctx->ext_buffers;
112 q->nb_ext_buffers = user_ctx->nb_ext_buffers;
113 }
114
Anton Khirnova0524d92016-05-27 11:23:19115 if (avctx->hw_frames_ctx) {
116 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
117 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
118
119 if (!iopattern) {
120 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
121 iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
122 else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
123 iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
124 }
125 }
126
127 if (!iopattern)
128 iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
129 q->iopattern = iopattern;
130
131 ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx);
Anton Khirnov4e08c822015-02-10 09:40:59132 if (ret < 0) {
133 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
134 return ret;
135 }
136
Anton Khirnovd0a63d82015-03-13 07:13:00137 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
Anton Khirnov4e08c822015-02-10 09:40:59138 if (ret < 0)
139 return ret;
140
141 param.mfx.CodecId = ret;
142 param.mfx.CodecProfile = avctx->profile;
143 param.mfx.CodecLevel = avctx->level;
144
Anton Khirnov92736c72016-06-22 09:53:00145 param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
146 param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
147 param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
Anton Khirnov536bb172016-06-22 09:41:26148 param.mfx.FrameInfo.FourCC = q->fourcc;
Anton Khirnov4e08c822015-02-10 09:40:59149 param.mfx.FrameInfo.Width = avctx->coded_width;
150 param.mfx.FrameInfo.Height = avctx->coded_height;
151 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
152
153 param.IOPattern = q->iopattern;
154 param.AsyncDepth = q->async_depth;
155 param.ExtParam = q->ext_buffers;
156 param.NumExtParam = q->nb_ext_buffers;
157
158 ret = MFXVideoDECODE_Init(q->session, &param);
159 if (ret < 0) {
160 av_log(avctx, AV_LOG_ERROR, "Error initializing the MFX video decoder\n");
161 return ff_qsv_error(ret);
162 }
163
Anton Khirnovce320cf2016-06-22 09:44:09164 q->frame_info = param.mfx.FrameInfo;
165
Anton Khirnov4e08c822015-02-10 09:40:59166 return 0;
167}
168
Anton Khirnovce320cf2016-06-22 09:44:09169static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
Anton Khirnov4e08c822015-02-10 09:40:59170{
171 int ret;
172
173 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
174 if (ret < 0)
175 return ret;
176
177 if (frame->frame->format == AV_PIX_FMT_QSV) {
178 frame->surface = (mfxFrameSurface1*)frame->frame->data[3];
179 } else {
Anton Khirnovce320cf2016-06-22 09:44:09180 frame->surface_internal.Info = q->frame_info;
Anton Khirnov4e08c822015-02-10 09:40:59181
182 frame->surface_internal.Data.PitchLow = frame->frame->linesize[0];
183 frame->surface_internal.Data.Y = frame->frame->data[0];
184 frame->surface_internal.Data.UV = frame->frame->data[1];
185
186 frame->surface = &frame->surface_internal;
187 }
188
189 return 0;
190}
191
192static void qsv_clear_unused_frames(QSVContext *q)
193{
194 QSVFrame *cur = q->work_frames;
195 while (cur) {
Anton Khirnovf5c4d382015-07-14 16:16:26196 if (cur->surface && !cur->surface->Data.Locked && !cur->queued) {
Anton Khirnov4e08c822015-02-10 09:40:59197 cur->surface = NULL;
198 av_frame_unref(cur->frame);
199 }
200 cur = cur->next;
201 }
202}
203
204static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
205{
206 QSVFrame *frame, **last;
207 int ret;
208
209 qsv_clear_unused_frames(q);
210
211 frame = q->work_frames;
212 last = &q->work_frames;
213 while (frame) {
214 if (!frame->surface) {
Anton Khirnovce320cf2016-06-22 09:44:09215 ret = alloc_frame(avctx, q, frame);
Anton Khirnov4e08c822015-02-10 09:40:59216 if (ret < 0)
217 return ret;
218 *surf = frame->surface;
219 return 0;
220 }
221
222 last = &frame->next;
223 frame = frame->next;
224 }
225
226 frame = av_mallocz(sizeof(*frame));
227 if (!frame)
228 return AVERROR(ENOMEM);
229 frame->frame = av_frame_alloc();
230 if (!frame->frame) {
231 av_freep(&frame);
232 return AVERROR(ENOMEM);
233 }
234 *last = frame;
235
Anton Khirnovce320cf2016-06-22 09:44:09236 ret = alloc_frame(avctx, q, frame);
Anton Khirnov4e08c822015-02-10 09:40:59237 if (ret < 0)
238 return ret;
239
240 *surf = frame->surface;
241
242 return 0;
243}
244
Anton Khirnovf5c4d382015-07-14 16:16:26245static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
Anton Khirnov4e08c822015-02-10 09:40:59246{
247 QSVFrame *cur = q->work_frames;
248 while (cur) {
249 if (surf == cur->surface)
Anton Khirnovf5c4d382015-07-14 16:16:26250 return cur;
Anton Khirnov4e08c822015-02-10 09:40:59251 cur = cur->next;
252 }
253 return NULL;
254}
255
Anton Khirnov96dca082015-07-21 16:57:49256static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
257 AVFrame *frame, int *got_frame,
258 AVPacket *avpkt)
Anton Khirnov4e08c822015-02-10 09:40:59259{
Anton Khirnovf5c4d382015-07-14 16:16:26260 QSVFrame *out_frame;
Anton Khirnov4e08c822015-02-10 09:40:59261 mfxFrameSurface1 *insurf;
262 mfxFrameSurface1 *outsurf;
Anton Khirnov3c536272016-02-22 09:48:34263 mfxSyncPoint *sync;
Anton Khirnov4e08c822015-02-10 09:40:59264 mfxBitstream bs = { { { 0 } } };
265 int ret;
266
267 if (avpkt->size) {
268 bs.Data = avpkt->data;
269 bs.DataLength = avpkt->size;
270 bs.MaxLength = bs.DataLength;
271 bs.TimeStamp = avpkt->pts;
272 }
273
Anton Khirnov3c536272016-02-22 09:48:34274 sync = av_mallocz(sizeof(*sync));
275 if (!sync) {
276 av_freep(&sync);
277 return AVERROR(ENOMEM);
278 }
279
Anton Khirnov4e08c822015-02-10 09:40:59280 do {
281 ret = get_surface(avctx, q, &insurf);
282 if (ret < 0)
283 return ret;
284
285 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
Anton Khirnov3c536272016-02-22 09:48:34286 insurf, &outsurf, sync);
Anton Khirnov4e08c822015-02-10 09:40:59287 if (ret == MFX_WRN_DEVICE_BUSY)
288 av_usleep(1);
289
290 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
291
292 if (ret != MFX_ERR_NONE &&
293 ret != MFX_ERR_MORE_DATA &&
294 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
295 ret != MFX_ERR_MORE_SURFACE) {
296 av_log(avctx, AV_LOG_ERROR, "Error during QSV decoding.\n");
Anton Khirnov3c536272016-02-22 09:48:34297 av_freep(&sync);
Anton Khirnov4e08c822015-02-10 09:40:59298 return ff_qsv_error(ret);
299 }
300
Anton Khirnovaa9d15d2015-07-09 18:08:13301 /* make sure we do not enter an infinite loop if the SDK
302 * did not consume any data and did not return anything */
Anton Khirnov3c536272016-02-22 09:48:34303 if (!*sync && !bs.DataOffset) {
Anton Khirnovaa9d15d2015-07-09 18:08:13304 av_log(avctx, AV_LOG_WARNING, "A decode call did not consume any data\n");
305 bs.DataOffset = avpkt->size;
306 }
307
Anton Khirnov3c536272016-02-22 09:48:34308 if (*sync) {
Anton Khirnovf5c4d382015-07-14 16:16:26309 QSVFrame *out_frame = find_frame(q, outsurf);
Anton Khirnov4e08c822015-02-10 09:40:59310
Anton Khirnovf5c4d382015-07-14 16:16:26311 if (!out_frame) {
Anton Khirnov4e08c822015-02-10 09:40:59312 av_log(avctx, AV_LOG_ERROR,
313 "The returned surface does not correspond to any frame\n");
Anton Khirnov3c536272016-02-22 09:48:34314 av_freep(&sync);
Anton Khirnov4e08c822015-02-10 09:40:59315 return AVERROR_BUG;
316 }
317
Anton Khirnovf5c4d382015-07-14 16:16:26318 out_frame->queued = 1;
319 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
320 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
Anton Khirnov3c536272016-02-22 09:48:34321 } else {
322 av_freep(&sync);
Anton Khirnovf5c4d382015-07-14 16:16:26323 }
324
325 if (!av_fifo_space(q->async_fifo) ||
326 (!avpkt->size && av_fifo_size(q->async_fifo))) {
327 AVFrame *src_frame;
328
329 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
330 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
331 out_frame->queued = 0;
332
Maxym Dmytrychenko3b6473b2015-12-18 13:24:36333 do {
Anton Khirnov3c536272016-02-22 09:48:34334 ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
Maxym Dmytrychenko3b6473b2015-12-18 13:24:36335 } while (ret == MFX_WRN_IN_EXECUTION);
Anton Khirnovf5c4d382015-07-14 16:16:26336
Anton Khirnov3c536272016-02-22 09:48:34337 av_freep(&sync);
338
Anton Khirnovf5c4d382015-07-14 16:16:26339 src_frame = out_frame->frame;
340
Anton Khirnov4e08c822015-02-10 09:40:59341 ret = av_frame_ref(frame, src_frame);
342 if (ret < 0)
343 return ret;
344
Anton Khirnovf5c4d382015-07-14 16:16:26345 outsurf = out_frame->surface;
346
Anton Khirnov32c83592016-03-19 20:45:24347#if FF_API_PKT_PTS
348FF_DISABLE_DEPRECATION_WARNINGS
349 frame->pkt_pts = outsurf->Data.TimeStamp;
350FF_ENABLE_DEPRECATION_WARNINGS
351#endif
352 frame->pts = outsurf->Data.TimeStamp;
Anton Khirnov4e08c822015-02-10 09:40:59353
354 frame->repeat_pict =
355 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
356 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
357 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
358 frame->top_field_first =
359 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
360 frame->interlaced_frame =
361 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
362
363 *got_frame = 1;
364 }
365
366 return bs.DataOffset;
367}
368
Anton Khirnov9ba27c22015-03-13 07:21:38369int ff_qsv_decode_close(QSVContext *q)
Anton Khirnov4e08c822015-02-10 09:40:59370{
371 QSVFrame *cur = q->work_frames;
372
Anton Khirnov2c32eac2015-08-11 12:52:31373 if (q->session)
374 MFXVideoDECODE_Close(q->session);
375
Anton Khirnov3c536272016-02-22 09:48:34376 while (q->async_fifo && av_fifo_size(q->async_fifo)) {
377 QSVFrame *out_frame;
378 mfxSyncPoint *sync;
379
380 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
381 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
382
383 av_freep(&sync);
384 }
385
Anton Khirnov4e08c822015-02-10 09:40:59386 while (cur) {
387 q->work_frames = cur->next;
388 av_frame_free(&cur->frame);
389 av_freep(&cur);
390 cur = q->work_frames;
391 }
392
Anton Khirnovf5c4d382015-07-14 16:16:26393 av_fifo_free(q->async_fifo);
394 q->async_fifo = NULL;
395
Anton Khirnov96dca082015-07-21 16:57:49396 av_parser_close(q->parser);
397 avcodec_free_context(&q->avctx_internal);
398
Anton Khirnov4e08c822015-02-10 09:40:59399 if (q->internal_session)
400 MFXClose(q->internal_session);
401
Anton Khirnova0524d92016-05-27 11:23:19402 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
403 av_freep(&q->frames_ctx.mids);
404 q->frames_ctx.nb_mids = 0;
405
Anton Khirnov4e08c822015-02-10 09:40:59406 return 0;
407}
Anton Khirnov96dca082015-07-21 16:57:49408
409int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
410 AVFrame *frame, int *got_frame, AVPacket *pkt)
411{
412 uint8_t *dummy_data;
413 int dummy_size;
414 int ret;
415
416 if (!q->avctx_internal) {
417 q->avctx_internal = avcodec_alloc_context3(NULL);
418 if (!q->avctx_internal)
419 return AVERROR(ENOMEM);
420
421 if (avctx->extradata) {
Vittorio Giovara059a9342015-06-29 21:48:34422 q->avctx_internal->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
Anton Khirnov96dca082015-07-21 16:57:49423 if (!q->avctx_internal->extradata)
424 return AVERROR(ENOMEM);
425
426 memcpy(q->avctx_internal->extradata, avctx->extradata,
427 avctx->extradata_size);
428 q->avctx_internal->extradata_size = avctx->extradata_size;
429 }
430
431 q->parser = av_parser_init(avctx->codec_id);
432 if (!q->parser)
433 return AVERROR(ENOMEM);
434
435 q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
436 q->orig_pix_fmt = AV_PIX_FMT_NONE;
437 }
438
439 if (!pkt->size)
440 return qsv_decode(avctx, q, frame, got_frame, pkt);
441
442 /* we assume the packets are already split properly and want
443 * just the codec parameters here */
444 av_parser_parse2(q->parser, q->avctx_internal,
445 &dummy_data, &dummy_size,
446 pkt->data, pkt->size, pkt->pts, pkt->dts,
447 pkt->pos);
448
449 /* TODO: flush delayed frames on reinit */
450 if (q->parser->format != q->orig_pix_fmt ||
451 q->parser->coded_width != avctx->coded_width ||
452 q->parser->coded_height != avctx->coded_height) {
Anton Khirnov96dca082015-07-21 16:57:49453 enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
454 AV_PIX_FMT_NONE,
455 AV_PIX_FMT_NONE };
456 enum AVPixelFormat qsv_format;
457
Anton Khirnov536bb172016-06-22 09:41:26458 qsv_format = ff_qsv_map_pixfmt(q->parser->format, &q->fourcc);
Anton Khirnov96dca082015-07-21 16:57:49459 if (qsv_format < 0) {
460 av_log(avctx, AV_LOG_ERROR,
Anton Khirnov92736c72016-06-22 09:53:00461 "Decoding pixel format '%s' is not supported\n",
462 av_get_pix_fmt_name(q->parser->format));
Anton Khirnov96dca082015-07-21 16:57:49463 ret = AVERROR(ENOSYS);
464 goto reinit_fail;
465 }
466
467 q->orig_pix_fmt = q->parser->format;
468 avctx->pix_fmt = pix_fmts[1] = qsv_format;
469 avctx->width = q->parser->width;
470 avctx->height = q->parser->height;
471 avctx->coded_width = q->parser->coded_width;
472 avctx->coded_height = q->parser->coded_height;
473 avctx->level = q->avctx_internal->level;
474 avctx->profile = q->avctx_internal->profile;
475
476 ret = ff_get_format(avctx, pix_fmts);
477 if (ret < 0)
478 goto reinit_fail;
479
480 avctx->pix_fmt = ret;
481
Anton Khirnov6f19bbc2016-05-21 16:26:40482 ret = qsv_decode_init(avctx, q);
Anton Khirnov96dca082015-07-21 16:57:49483 if (ret < 0)
484 goto reinit_fail;
485 }
486
487 return qsv_decode(avctx, q, frame, got_frame, pkt);
488
489reinit_fail:
490 q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
491 return ret;
492}
493
494void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
495{
496 q->orig_pix_fmt = AV_PIX_FMT_NONE;
497}