blob: c00817f1d93e533c75c186b3477bc83a78822aa9 [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 *
Michael Niedermayer841e9f42015-02-19 19:52:297 * This file is part of FFmpeg.
Anton Khirnov4e08c822015-02-10 09:40:598 *
Michael Niedermayer841e9f42015-02-19 19:52:299 * FFmpeg is free software; you can redistribute it and/or
Anton Khirnov4e08c822015-02-10 09:40:5910 * 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 *
Michael Niedermayer841e9f42015-02-19 19:52:2914 * FFmpeg is distributed in the hope that it will be useful,
Anton Khirnov4e08c822015-02-10 09:40:5915 * 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
Michael Niedermayer841e9f42015-02-19 19:52:2920 * License along with FFmpeg; if not, write to the Free Software
Anton Khirnov4e08c822015-02-10 09:40:5921 * 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"
Mark Thompson1f26a232016-10-21 17:57:1230#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"
Ivan Uskov6e127992015-07-14 11:07:0440#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
Mark Thompson1f26a232016-10-21 17:57:1244static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
Mark Thompson8aa3c2d2017-03-04 23:57:3645 AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref)
Anton Khirnov4e08c822015-02-10 09:40:5946{
Mark Thompson1f26a232016-10-21 17:57:1247 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
Mark Thompson91c3b502017-03-04 23:57:3562 ret = ff_qsv_init_session_frames(avctx, &q->internal_session,
63 &q->frames_ctx, q->load_plugins,
64 q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY);
Mark Thompson1f26a232016-10-21 17:57:1265 if (ret < 0) {
66 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
67 return ret;
68 }
69
70 q->session = q->internal_session;
Mark Thompson8aa3c2d2017-03-04 23:57:3671 } else if (hw_device_ref) {
72 if (q->internal_session) {
73 MFXClose(q->internal_session);
74 q->internal_session = NULL;
75 }
76
77 ret = ff_qsv_init_session_device(avctx, &q->internal_session,
78 hw_device_ref, q->load_plugins);
79 if (ret < 0)
80 return ret;
81
82 q->session = q->internal_session;
Mark Thompson1f26a232016-10-21 17:57:1283 } else {
84 if (!q->internal_session) {
85 ret = ff_qsv_init_internal_session(avctx, &q->internal_session,
86 q->load_plugins);
Anton Khirnovd0a63d82015-03-13 07:13:0087 if (ret < 0)
88 return ret;
Anton Khirnov4e08c822015-02-10 09:40:5989 }
90
Mark Thompson1f26a232016-10-21 17:57:1291 q->session = q->internal_session;
Anton Khirnov4e08c822015-02-10 09:40:5992 }
93
Mark Thompson1f26a232016-10-21 17:57:1294 /* make sure the decoder is uninitialized */
95 MFXVideoDECODE_Close(q->session);
96
97 return 0;
Anton Khirnov4e08c822015-02-10 09:40:5998}
99
Mark Thompson1f26a232016-10-21 17:57:12100static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q)
Anton Khirnov4e08c822015-02-10 09:40:59101{
Anton Khirnov92736c72016-06-22 09:53:00102 const AVPixFmtDescriptor *desc;
Anton Khirnov6f19bbc2016-05-21 16:26:40103 mfxSession session = NULL;
Mark Thompson1f26a232016-10-21 17:57:12104 int iopattern = 0;
Diego Biurrun76167142016-11-15 15:19:30105 mfxVideoParam param = { 0 };
Anton Khirnov924e2ec2016-06-22 09:57:36106 int frame_width = avctx->coded_width;
107 int frame_height = avctx->coded_height;
Anton Khirnov4e08c822015-02-10 09:40:59108 int ret;
109
Anton Khirnov92736c72016-06-22 09:53:00110 desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
111 if (!desc)
112 return AVERROR_BUG;
113
Mark Thompson1f26a232016-10-21 17:57:12114 if (!q->async_fifo) {
115 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
116 (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));
117 if (!q->async_fifo)
118 return AVERROR(ENOMEM);
Hendrik Leppkesb54d6452015-10-22 15:00:49119 }
Hendrik Leppkesb54d6452015-10-22 15:00:49120
Anton Khirnove3281782016-07-30 14:38:51121 if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) {
Mark Thompson1f26a232016-10-21 17:57:12122 AVQSVContext *user_ctx = avctx->hwaccel_context;
123 session = user_ctx->session;
124 iopattern = user_ctx->iopattern;
125 q->ext_buffers = user_ctx->ext_buffers;
126 q->nb_ext_buffers = user_ctx->nb_ext_buffers;
127 }
128
129 if (avctx->hw_frames_ctx) {
130 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
131 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
132
133 if (!iopattern) {
134 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
135 iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
136 else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
137 iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
138 }
Anton Khirnov924e2ec2016-06-22 09:57:36139
140 frame_width = frames_hwctx->surfaces[0].Info.Width;
141 frame_height = frames_hwctx->surfaces[0].Info.Height;
Mark Thompson1f26a232016-10-21 17:57:12142 }
143
144 if (!iopattern)
145 iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
146 q->iopattern = iopattern;
147
Mark Thompson8aa3c2d2017-03-04 23:57:36148 ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx, avctx->hw_device_ctx);
Anton Khirnov4e08c822015-02-10 09:40:59149 if (ret < 0) {
150 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
151 return ret;
Ivan Uskov6e127992015-07-14 11:07:04152 }
Anton Khirnov4e08c822015-02-10 09:40:59153
Anton Khirnovd0a63d82015-03-13 07:13:00154 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
Mark Thompson1f26a232016-10-21 17:57:12155 if (ret < 0)
Anton Khirnov4e08c822015-02-10 09:40:59156 return ret;
Mark Thompson1f26a232016-10-21 17:57:12157
158 param.mfx.CodecId = ret;
159 param.mfx.CodecProfile = ff_qsv_profile_to_mfx(avctx->codec_id, avctx->profile);
160 param.mfx.CodecLevel = avctx->level == FF_LEVEL_UNKNOWN ? MFX_LEVEL_UNKNOWN : avctx->level;
161
Anton Khirnov92736c72016-06-22 09:53:00162 param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
163 param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
164 param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
Anton Khirnov536bb172016-06-22 09:41:26165 param.mfx.FrameInfo.FourCC = q->fourcc;
Anton Khirnov924e2ec2016-06-22 09:57:36166 param.mfx.FrameInfo.Width = frame_width;
167 param.mfx.FrameInfo.Height = frame_height;
Mark Thompson1f26a232016-10-21 17:57:12168 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
169
170 switch (avctx->field_order) {
171 case AV_FIELD_PROGRESSIVE:
172 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
173 break;
174 case AV_FIELD_TT:
175 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
176 break;
177 case AV_FIELD_BB:
178 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_BFF;
179 break;
180 default:
181 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_UNKNOWN;
182 break;
Ivan Uskov1acb19d2015-07-20 15:07:34183 }
Anton Khirnov4e08c822015-02-10 09:40:59184
Anton Khirnov4e08c822015-02-10 09:40:59185 param.IOPattern = q->iopattern;
186 param.AsyncDepth = q->async_depth;
187 param.ExtParam = q->ext_buffers;
188 param.NumExtParam = q->nb_ext_buffers;
189
190 ret = MFXVideoDECODE_Init(q->session, &param);
Anton Khirnov95414eb2016-06-25 19:38:10191 if (ret < 0)
192 return ff_qsv_print_error(avctx, ret,
193 "Error initializing the MFX video decoder");
Anton Khirnov4e08c822015-02-10 09:40:59194
Anton Khirnovce320cf2016-06-22 09:44:09195 q->frame_info = param.mfx.FrameInfo;
196
Anton Khirnov4e08c822015-02-10 09:40:59197 return 0;
198}
199
Anton Khirnovce320cf2016-06-22 09:44:09200static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
Anton Khirnov4e08c822015-02-10 09:40:59201{
202 int ret;
203
204 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
205 if (ret < 0)
206 return ret;
207
208 if (frame->frame->format == AV_PIX_FMT_QSV) {
Anton Khirnov404e5142016-08-09 10:05:49209 frame->surface = *(mfxFrameSurface1*)frame->frame->data[3];
Anton Khirnov4e08c822015-02-10 09:40:59210 } else {
Anton Khirnov404e5142016-08-09 10:05:49211 frame->surface.Info = q->frame_info;
Anton Khirnov4e08c822015-02-10 09:40:59212
Anton Khirnov404e5142016-08-09 10:05:49213 frame->surface.Data.PitchLow = frame->frame->linesize[0];
214 frame->surface.Data.Y = frame->frame->data[0];
215 frame->surface.Data.UV = frame->frame->data[1];
Anton Khirnov4e08c822015-02-10 09:40:59216 }
217
Anton Khirnov00aeedd2016-08-10 06:29:23218 if (q->frames_ctx.mids) {
219 ret = ff_qsv_find_surface_idx(&q->frames_ctx, frame);
220 if (ret < 0)
221 return ret;
222
223 frame->surface.Data.MemId = &q->frames_ctx.mids[ret];
224 }
225
Anton Khirnov404e5142016-08-09 10:05:49226 frame->used = 1;
227
Anton Khirnov4e08c822015-02-10 09:40:59228 return 0;
229}
230
231static void qsv_clear_unused_frames(QSVContext *q)
232{
233 QSVFrame *cur = q->work_frames;
234 while (cur) {
Anton Khirnov404e5142016-08-09 10:05:49235 if (cur->used && !cur->surface.Data.Locked && !cur->queued) {
236 cur->used = 0;
Anton Khirnov4e08c822015-02-10 09:40:59237 av_frame_unref(cur->frame);
238 }
239 cur = cur->next;
240 }
241}
242
243static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
244{
245 QSVFrame *frame, **last;
246 int ret;
247
248 qsv_clear_unused_frames(q);
249
250 frame = q->work_frames;
251 last = &q->work_frames;
252 while (frame) {
Anton Khirnov404e5142016-08-09 10:05:49253 if (!frame->used) {
Anton Khirnovce320cf2016-06-22 09:44:09254 ret = alloc_frame(avctx, q, frame);
Anton Khirnov4e08c822015-02-10 09:40:59255 if (ret < 0)
256 return ret;
Anton Khirnov404e5142016-08-09 10:05:49257 *surf = &frame->surface;
Anton Khirnov4e08c822015-02-10 09:40:59258 return 0;
259 }
260
261 last = &frame->next;
262 frame = frame->next;
263 }
264
265 frame = av_mallocz(sizeof(*frame));
266 if (!frame)
267 return AVERROR(ENOMEM);
268 frame->frame = av_frame_alloc();
269 if (!frame->frame) {
270 av_freep(&frame);
271 return AVERROR(ENOMEM);
272 }
273 *last = frame;
274
Anton Khirnovce320cf2016-06-22 09:44:09275 ret = alloc_frame(avctx, q, frame);
Anton Khirnov4e08c822015-02-10 09:40:59276 if (ret < 0)
277 return ret;
278
Anton Khirnov404e5142016-08-09 10:05:49279 *surf = &frame->surface;
Anton Khirnov4e08c822015-02-10 09:40:59280
281 return 0;
282}
283
Anton Khirnovf5c4d382015-07-14 16:16:26284static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
Anton Khirnov4e08c822015-02-10 09:40:59285{
286 QSVFrame *cur = q->work_frames;
287 while (cur) {
Anton Khirnov404e5142016-08-09 10:05:49288 if (surf == &cur->surface)
Anton Khirnovf5c4d382015-07-14 16:16:26289 return cur;
Anton Khirnov4e08c822015-02-10 09:40:59290 cur = cur->next;
291 }
292 return NULL;
293}
294
Mark Thompson1f26a232016-10-21 17:57:12295static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
296 AVFrame *frame, int *got_frame,
297 AVPacket *avpkt)
Anton Khirnov4e08c822015-02-10 09:40:59298{
Anton Khirnovf5c4d382015-07-14 16:16:26299 QSVFrame *out_frame;
Anton Khirnov4e08c822015-02-10 09:40:59300 mfxFrameSurface1 *insurf;
301 mfxFrameSurface1 *outsurf;
Mark Thompson1f26a232016-10-21 17:57:12302 mfxSyncPoint *sync;
Anton Khirnov4e08c822015-02-10 09:40:59303 mfxBitstream bs = { { { 0 } } };
304 int ret;
305
Mark Thompson1f26a232016-10-21 17:57:12306 if (avpkt->size) {
307 bs.Data = avpkt->data;
308 bs.DataLength = avpkt->size;
Anton Khirnov4e08c822015-02-10 09:40:59309 bs.MaxLength = bs.DataLength;
310 bs.TimeStamp = avpkt->pts;
311 }
312
Mark Thompson1f26a232016-10-21 17:57:12313 sync = av_mallocz(sizeof(*sync));
314 if (!sync) {
315 av_freep(&sync);
316 return AVERROR(ENOMEM);
317 }
318
319 do {
Anton Khirnov4e08c822015-02-10 09:40:59320 ret = get_surface(avctx, q, &insurf);
Timothy Gub6f80b12016-12-05 18:20:26321 if (ret < 0) {
322 av_freep(&sync);
Anton Khirnov4e08c822015-02-10 09:40:59323 return ret;
Timothy Gub6f80b12016-12-05 18:20:26324 }
Mark Thompson1f26a232016-10-21 17:57:12325
326 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
327 insurf, &outsurf, sync);
328 if (ret == MFX_WRN_DEVICE_BUSY)
Ivan Uskov9f543e02015-07-24 11:45:38329 av_usleep(500);
Anton Khirnov4e08c822015-02-10 09:40:59330
Mark Thompson1f26a232016-10-21 17:57:12331 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
Anton Khirnov4e08c822015-02-10 09:40:59332
Mark Thompson1f26a232016-10-21 17:57:12333 if (ret != MFX_ERR_NONE &&
334 ret != MFX_ERR_MORE_DATA &&
335 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
336 ret != MFX_ERR_MORE_SURFACE) {
Mark Thompson1f26a232016-10-21 17:57:12337 av_freep(&sync);
Anton Khirnov95414eb2016-06-25 19:38:10338 return ff_qsv_print_error(avctx, ret,
339 "Error during QSV decoding.");
Anton Khirnovf5c4d382015-07-14 16:16:26340 }
341
Anton Khirnovaa9d15d2015-07-09 18:08:13342 /* make sure we do not enter an infinite loop if the SDK
343 * did not consume any data and did not return anything */
Mark Thompson1f26a232016-10-21 17:57:12344 if (!*sync && !bs.DataOffset) {
Anton Khirnovaa9d15d2015-07-09 18:08:13345 bs.DataOffset = avpkt->size;
Mark Thompson0940b742016-10-30 16:58:23346 ++q->zero_consume_run;
347 if (q->zero_consume_run > 1)
348 ff_qsv_print_warning(avctx, ret, "A decode call did not consume any data");
349 } else {
350 q->zero_consume_run = 0;
Anton Khirnovaa9d15d2015-07-09 18:08:13351 }
352
Mark Thompson1f26a232016-10-21 17:57:12353 if (*sync) {
354 QSVFrame *out_frame = find_frame(q, outsurf);
355
356 if (!out_frame) {
357 av_log(avctx, AV_LOG_ERROR,
358 "The returned surface does not correspond to any frame\n");
359 av_freep(&sync);
360 return AVERROR_BUG;
361 }
362
363 out_frame->queued = 1;
364 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
365 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
366 } else {
367 av_freep(&sync);
Ivan Uskovc90dbc62015-07-24 10:26:14368 }
369
Mark Thompson1f26a232016-10-21 17:57:12370 if (!av_fifo_space(q->async_fifo) ||
371 (!avpkt->size && av_fifo_size(q->async_fifo))) {
Anton Khirnovf5c4d382015-07-14 16:16:26372 AVFrame *src_frame;
373
374 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
375 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
376 out_frame->queued = 0;
377
Anton Khirnovb68e3532017-01-07 20:06:16378 if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
379 do {
380 ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
381 } while (ret == MFX_WRN_IN_EXECUTION);
382 }
Anton Khirnovf5c4d382015-07-14 16:16:26383
Mark Thompson1f26a232016-10-21 17:57:12384 av_freep(&sync);
385
Anton Khirnovf5c4d382015-07-14 16:16:26386 src_frame = out_frame->frame;
387
Anton Khirnov4e08c822015-02-10 09:40:59388 ret = av_frame_ref(frame, src_frame);
389 if (ret < 0)
390 return ret;
391
Anton Khirnov404e5142016-08-09 10:05:49392 outsurf = &out_frame->surface;
Anton Khirnovf5c4d382015-07-14 16:16:26393
Anton Khirnov32c83592016-03-19 20:45:24394#if FF_API_PKT_PTS
395FF_DISABLE_DEPRECATION_WARNINGS
396 frame->pkt_pts = outsurf->Data.TimeStamp;
397FF_ENABLE_DEPRECATION_WARNINGS
398#endif
399 frame->pts = outsurf->Data.TimeStamp;
Anton Khirnov4e08c822015-02-10 09:40:59400
401 frame->repeat_pict =
402 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
403 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
404 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
405 frame->top_field_first =
406 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
407 frame->interlaced_frame =
408 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
409
Anton Khirnov404e5142016-08-09 10:05:49410 /* update the surface properties */
411 if (avctx->pix_fmt == AV_PIX_FMT_QSV)
412 ((mfxFrameSurface1*)frame->data[3])->Info = outsurf->Info;
413
Anton Khirnov4e08c822015-02-10 09:40:59414 *got_frame = 1;
415 }
416
Mark Thompson1f26a232016-10-21 17:57:12417 return bs.DataOffset;
Ivan Uskov3f8e2e92015-08-06 16:10:24418}
Anton Khirnov4e08c822015-02-10 09:40:59419
Anton Khirnov9ba27c22015-03-13 07:21:38420int ff_qsv_decode_close(QSVContext *q)
Anton Khirnov4e08c822015-02-10 09:40:59421{
Mark Thompson1f26a232016-10-21 17:57:12422 QSVFrame *cur = q->work_frames;
Anton Khirnov4e08c822015-02-10 09:40:59423
Mark Thompson1f26a232016-10-21 17:57:12424 if (q->session)
425 MFXVideoDECODE_Close(q->session);
Ivan Uskovcc167f72015-08-04 10:40:06426
Mark Thompson1f26a232016-10-21 17:57:12427 while (q->async_fifo && av_fifo_size(q->async_fifo)) {
428 QSVFrame *out_frame;
429 mfxSyncPoint *sync;
430
431 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
432 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
433
434 av_freep(&sync);
435 }
436
437 while (cur) {
438 q->work_frames = cur->next;
439 av_frame_free(&cur->frame);
440 av_freep(&cur);
441 cur = q->work_frames;
442 }
Anton Khirnov4e08c822015-02-10 09:40:59443
Anton Khirnovf5c4d382015-07-14 16:16:26444 av_fifo_free(q->async_fifo);
445 q->async_fifo = NULL;
446
Mark Thompson1f26a232016-10-21 17:57:12447 av_parser_close(q->parser);
448 avcodec_free_context(&q->avctx_internal);
Ivan Uskovc90dbc62015-07-24 10:26:14449
Mark Thompson1f26a232016-10-21 17:57:12450 if (q->internal_session)
451 MFXClose(q->internal_session);
452
453 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
Anton Khirnov4ab61cd2016-08-10 07:38:21454 av_buffer_unref(&q->frames_ctx.mids_buf);
Ivan Uskovd50ab822015-07-23 09:14:41455
Anton Khirnov4e08c822015-02-10 09:40:59456 return 0;
457}
Mark Thompson1f26a232016-10-21 17:57:12458
459int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
460 AVFrame *frame, int *got_frame, AVPacket *pkt)
461{
462 uint8_t *dummy_data;
463 int dummy_size;
464 int ret;
465
466 if (!q->avctx_internal) {
467 q->avctx_internal = avcodec_alloc_context3(NULL);
468 if (!q->avctx_internal)
469 return AVERROR(ENOMEM);
470
471 q->parser = av_parser_init(avctx->codec_id);
472 if (!q->parser)
473 return AVERROR(ENOMEM);
474
475 q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
476 q->orig_pix_fmt = AV_PIX_FMT_NONE;
477 }
478
479 if (!pkt->size)
480 return qsv_decode(avctx, q, frame, got_frame, pkt);
481
482 /* we assume the packets are already split properly and want
483 * just the codec parameters here */
484 av_parser_parse2(q->parser, q->avctx_internal,
485 &dummy_data, &dummy_size,
486 pkt->data, pkt->size, pkt->pts, pkt->dts,
487 pkt->pos);
488
489 /* TODO: flush delayed frames on reinit */
490 if (q->parser->format != q->orig_pix_fmt ||
491 q->parser->coded_width != avctx->coded_width ||
492 q->parser->coded_height != avctx->coded_height) {
493 enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
494 AV_PIX_FMT_NONE,
495 AV_PIX_FMT_NONE };
496 enum AVPixelFormat qsv_format;
497
Anton Khirnov536bb172016-06-22 09:41:26498 qsv_format = ff_qsv_map_pixfmt(q->parser->format, &q->fourcc);
Mark Thompson1f26a232016-10-21 17:57:12499 if (qsv_format < 0) {
500 av_log(avctx, AV_LOG_ERROR,
Anton Khirnov92736c72016-06-22 09:53:00501 "Decoding pixel format '%s' is not supported\n",
502 av_get_pix_fmt_name(q->parser->format));
Mark Thompson1f26a232016-10-21 17:57:12503 ret = AVERROR(ENOSYS);
504 goto reinit_fail;
505 }
506
507 q->orig_pix_fmt = q->parser->format;
508 avctx->pix_fmt = pix_fmts[1] = qsv_format;
509 avctx->width = q->parser->width;
510 avctx->height = q->parser->height;
511 avctx->coded_width = q->parser->coded_width;
512 avctx->coded_height = q->parser->coded_height;
513 avctx->field_order = q->parser->field_order;
514 avctx->level = q->avctx_internal->level;
515 avctx->profile = q->avctx_internal->profile;
516
517 ret = ff_get_format(avctx, pix_fmts);
518 if (ret < 0)
519 goto reinit_fail;
520
521 avctx->pix_fmt = ret;
522
523 ret = qsv_decode_init(avctx, q);
524 if (ret < 0)
525 goto reinit_fail;
526 }
527
528 return qsv_decode(avctx, q, frame, got_frame, pkt);
529
530reinit_fail:
531 q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
532 return ret;
533}
534
535void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
536{
537 q->orig_pix_fmt = AV_PIX_FMT_NONE;
538}