blob: 74866b57ffc767d92936dc4041cbee9c766ba927 [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,
45 AVBufferRef *hw_frames_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;
71 } else {
72 if (!q->internal_session) {
73 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
Mark Thompson1f26a232016-10-21 17:57:1279 q->session = q->internal_session;
Anton Khirnov4e08c822015-02-10 09:40:5980 }
81
Mark Thompson1f26a232016-10-21 17:57:1282 /* make sure the decoder is uninitialized */
83 MFXVideoDECODE_Close(q->session);
84
85 return 0;
Anton Khirnov4e08c822015-02-10 09:40:5986}
87
Mark Thompson1f26a232016-10-21 17:57:1288static 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;
Mark Thompson1f26a232016-10-21 17:57:1292 int iopattern = 0;
Diego Biurrun76167142016-11-15 15:19:3093 mfxVideoParam param = { 0 };
Anton Khirnov924e2ec2016-06-22 09:57:3694 int frame_width = avctx->coded_width;
95 int frame_height = avctx->coded_height;
Anton Khirnov4e08c822015-02-10 09:40:5996 int ret;
97
Anton Khirnov92736c72016-06-22 09:53:0098 desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
99 if (!desc)
100 return AVERROR_BUG;
101
Mark Thompson1f26a232016-10-21 17:57:12102 if (!q->async_fifo) {
103 q->async_fifo = av_fifo_alloc((1 + q->async_depth) *
104 (sizeof(mfxSyncPoint*) + sizeof(QSVFrame*)));
105 if (!q->async_fifo)
106 return AVERROR(ENOMEM);
Hendrik Leppkesb54d6452015-10-22 15:00:49107 }
Hendrik Leppkesb54d6452015-10-22 15:00:49108
Anton Khirnove3281782016-07-30 14:38:51109 if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) {
Mark Thompson1f26a232016-10-21 17:57:12110 AVQSVContext *user_ctx = avctx->hwaccel_context;
111 session = user_ctx->session;
112 iopattern = user_ctx->iopattern;
113 q->ext_buffers = user_ctx->ext_buffers;
114 q->nb_ext_buffers = user_ctx->nb_ext_buffers;
115 }
116
117 if (avctx->hw_frames_ctx) {
118 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
119 AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
120
121 if (!iopattern) {
122 if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
123 iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
124 else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
125 iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
126 }
Anton Khirnov924e2ec2016-06-22 09:57:36127
128 frame_width = frames_hwctx->surfaces[0].Info.Width;
129 frame_height = frames_hwctx->surfaces[0].Info.Height;
Mark Thompson1f26a232016-10-21 17:57:12130 }
131
132 if (!iopattern)
133 iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
134 q->iopattern = iopattern;
135
136 ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx);
Anton Khirnov4e08c822015-02-10 09:40:59137 if (ret < 0) {
138 av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
139 return ret;
Ivan Uskov6e127992015-07-14 11:07:04140 }
Anton Khirnov4e08c822015-02-10 09:40:59141
Anton Khirnovd0a63d82015-03-13 07:13:00142 ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
Mark Thompson1f26a232016-10-21 17:57:12143 if (ret < 0)
Anton Khirnov4e08c822015-02-10 09:40:59144 return ret;
Mark Thompson1f26a232016-10-21 17:57:12145
146 param.mfx.CodecId = ret;
147 param.mfx.CodecProfile = ff_qsv_profile_to_mfx(avctx->codec_id, avctx->profile);
148 param.mfx.CodecLevel = avctx->level == FF_LEVEL_UNKNOWN ? MFX_LEVEL_UNKNOWN : avctx->level;
149
Anton Khirnov92736c72016-06-22 09:53:00150 param.mfx.FrameInfo.BitDepthLuma = desc->comp[0].depth;
151 param.mfx.FrameInfo.BitDepthChroma = desc->comp[0].depth;
152 param.mfx.FrameInfo.Shift = desc->comp[0].depth > 8;
Anton Khirnov536bb172016-06-22 09:41:26153 param.mfx.FrameInfo.FourCC = q->fourcc;
Anton Khirnov924e2ec2016-06-22 09:57:36154 param.mfx.FrameInfo.Width = frame_width;
155 param.mfx.FrameInfo.Height = frame_height;
Mark Thompson1f26a232016-10-21 17:57:12156 param.mfx.FrameInfo.ChromaFormat = MFX_CHROMAFORMAT_YUV420;
157
158 switch (avctx->field_order) {
159 case AV_FIELD_PROGRESSIVE:
160 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_PROGRESSIVE;
161 break;
162 case AV_FIELD_TT:
163 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_TFF;
164 break;
165 case AV_FIELD_BB:
166 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_FIELD_BFF;
167 break;
168 default:
169 param.mfx.FrameInfo.PicStruct = MFX_PICSTRUCT_UNKNOWN;
170 break;
Ivan Uskov1acb19d2015-07-20 15:07:34171 }
Anton Khirnov4e08c822015-02-10 09:40:59172
Anton Khirnov4e08c822015-02-10 09:40:59173 param.IOPattern = q->iopattern;
174 param.AsyncDepth = q->async_depth;
175 param.ExtParam = q->ext_buffers;
176 param.NumExtParam = q->nb_ext_buffers;
177
178 ret = MFXVideoDECODE_Init(q->session, &param);
Anton Khirnov95414eb2016-06-25 19:38:10179 if (ret < 0)
180 return ff_qsv_print_error(avctx, ret,
181 "Error initializing the MFX video decoder");
Anton Khirnov4e08c822015-02-10 09:40:59182
Anton Khirnovce320cf2016-06-22 09:44:09183 q->frame_info = param.mfx.FrameInfo;
184
Anton Khirnov4e08c822015-02-10 09:40:59185 return 0;
186}
187
Anton Khirnovce320cf2016-06-22 09:44:09188static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
Anton Khirnov4e08c822015-02-10 09:40:59189{
190 int ret;
191
192 ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
193 if (ret < 0)
194 return ret;
195
196 if (frame->frame->format == AV_PIX_FMT_QSV) {
Anton Khirnov404e5142016-08-09 10:05:49197 frame->surface = *(mfxFrameSurface1*)frame->frame->data[3];
Anton Khirnov4e08c822015-02-10 09:40:59198 } else {
Anton Khirnov404e5142016-08-09 10:05:49199 frame->surface.Info = q->frame_info;
Anton Khirnov4e08c822015-02-10 09:40:59200
Anton Khirnov404e5142016-08-09 10:05:49201 frame->surface.Data.PitchLow = frame->frame->linesize[0];
202 frame->surface.Data.Y = frame->frame->data[0];
203 frame->surface.Data.UV = frame->frame->data[1];
Anton Khirnov4e08c822015-02-10 09:40:59204 }
205
Anton Khirnov00aeedd2016-08-10 06:29:23206 if (q->frames_ctx.mids) {
207 ret = ff_qsv_find_surface_idx(&q->frames_ctx, frame);
208 if (ret < 0)
209 return ret;
210
211 frame->surface.Data.MemId = &q->frames_ctx.mids[ret];
212 }
213
Anton Khirnov404e5142016-08-09 10:05:49214 frame->used = 1;
215
Anton Khirnov4e08c822015-02-10 09:40:59216 return 0;
217}
218
219static void qsv_clear_unused_frames(QSVContext *q)
220{
221 QSVFrame *cur = q->work_frames;
222 while (cur) {
Anton Khirnov404e5142016-08-09 10:05:49223 if (cur->used && !cur->surface.Data.Locked && !cur->queued) {
224 cur->used = 0;
Anton Khirnov4e08c822015-02-10 09:40:59225 av_frame_unref(cur->frame);
226 }
227 cur = cur->next;
228 }
229}
230
231static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
232{
233 QSVFrame *frame, **last;
234 int ret;
235
236 qsv_clear_unused_frames(q);
237
238 frame = q->work_frames;
239 last = &q->work_frames;
240 while (frame) {
Anton Khirnov404e5142016-08-09 10:05:49241 if (!frame->used) {
Anton Khirnovce320cf2016-06-22 09:44:09242 ret = alloc_frame(avctx, q, frame);
Anton Khirnov4e08c822015-02-10 09:40:59243 if (ret < 0)
244 return ret;
Anton Khirnov404e5142016-08-09 10:05:49245 *surf = &frame->surface;
Anton Khirnov4e08c822015-02-10 09:40:59246 return 0;
247 }
248
249 last = &frame->next;
250 frame = frame->next;
251 }
252
253 frame = av_mallocz(sizeof(*frame));
254 if (!frame)
255 return AVERROR(ENOMEM);
256 frame->frame = av_frame_alloc();
257 if (!frame->frame) {
258 av_freep(&frame);
259 return AVERROR(ENOMEM);
260 }
261 *last = frame;
262
Anton Khirnovce320cf2016-06-22 09:44:09263 ret = alloc_frame(avctx, q, frame);
Anton Khirnov4e08c822015-02-10 09:40:59264 if (ret < 0)
265 return ret;
266
Anton Khirnov404e5142016-08-09 10:05:49267 *surf = &frame->surface;
Anton Khirnov4e08c822015-02-10 09:40:59268
269 return 0;
270}
271
Anton Khirnovf5c4d382015-07-14 16:16:26272static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
Anton Khirnov4e08c822015-02-10 09:40:59273{
274 QSVFrame *cur = q->work_frames;
275 while (cur) {
Anton Khirnov404e5142016-08-09 10:05:49276 if (surf == &cur->surface)
Anton Khirnovf5c4d382015-07-14 16:16:26277 return cur;
Anton Khirnov4e08c822015-02-10 09:40:59278 cur = cur->next;
279 }
280 return NULL;
281}
282
Mark Thompson1f26a232016-10-21 17:57:12283static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
284 AVFrame *frame, int *got_frame,
285 AVPacket *avpkt)
Anton Khirnov4e08c822015-02-10 09:40:59286{
Anton Khirnovf5c4d382015-07-14 16:16:26287 QSVFrame *out_frame;
Anton Khirnov4e08c822015-02-10 09:40:59288 mfxFrameSurface1 *insurf;
289 mfxFrameSurface1 *outsurf;
Mark Thompson1f26a232016-10-21 17:57:12290 mfxSyncPoint *sync;
Anton Khirnov4e08c822015-02-10 09:40:59291 mfxBitstream bs = { { { 0 } } };
292 int ret;
293
Mark Thompson1f26a232016-10-21 17:57:12294 if (avpkt->size) {
295 bs.Data = avpkt->data;
296 bs.DataLength = avpkt->size;
Anton Khirnov4e08c822015-02-10 09:40:59297 bs.MaxLength = bs.DataLength;
298 bs.TimeStamp = avpkt->pts;
299 }
300
Mark Thompson1f26a232016-10-21 17:57:12301 sync = av_mallocz(sizeof(*sync));
302 if (!sync) {
303 av_freep(&sync);
304 return AVERROR(ENOMEM);
305 }
306
307 do {
Anton Khirnov4e08c822015-02-10 09:40:59308 ret = get_surface(avctx, q, &insurf);
Timothy Gub6f80b12016-12-05 18:20:26309 if (ret < 0) {
310 av_freep(&sync);
Anton Khirnov4e08c822015-02-10 09:40:59311 return ret;
Timothy Gub6f80b12016-12-05 18:20:26312 }
Mark Thompson1f26a232016-10-21 17:57:12313
314 ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
315 insurf, &outsurf, sync);
316 if (ret == MFX_WRN_DEVICE_BUSY)
Ivan Uskov9f543e02015-07-24 11:45:38317 av_usleep(500);
Anton Khirnov4e08c822015-02-10 09:40:59318
Mark Thompson1f26a232016-10-21 17:57:12319 } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
Anton Khirnov4e08c822015-02-10 09:40:59320
Mark Thompson1f26a232016-10-21 17:57:12321 if (ret != MFX_ERR_NONE &&
322 ret != MFX_ERR_MORE_DATA &&
323 ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
324 ret != MFX_ERR_MORE_SURFACE) {
Mark Thompson1f26a232016-10-21 17:57:12325 av_freep(&sync);
Anton Khirnov95414eb2016-06-25 19:38:10326 return ff_qsv_print_error(avctx, ret,
327 "Error during QSV decoding.");
Anton Khirnovf5c4d382015-07-14 16:16:26328 }
329
Anton Khirnovaa9d15d2015-07-09 18:08:13330 /* make sure we do not enter an infinite loop if the SDK
331 * did not consume any data and did not return anything */
Mark Thompson1f26a232016-10-21 17:57:12332 if (!*sync && !bs.DataOffset) {
Anton Khirnovaa9d15d2015-07-09 18:08:13333 bs.DataOffset = avpkt->size;
Mark Thompson0940b742016-10-30 16:58:23334 ++q->zero_consume_run;
335 if (q->zero_consume_run > 1)
336 ff_qsv_print_warning(avctx, ret, "A decode call did not consume any data");
337 } else {
338 q->zero_consume_run = 0;
Anton Khirnovaa9d15d2015-07-09 18:08:13339 }
340
Mark Thompson1f26a232016-10-21 17:57:12341 if (*sync) {
342 QSVFrame *out_frame = find_frame(q, outsurf);
343
344 if (!out_frame) {
345 av_log(avctx, AV_LOG_ERROR,
346 "The returned surface does not correspond to any frame\n");
347 av_freep(&sync);
348 return AVERROR_BUG;
349 }
350
351 out_frame->queued = 1;
352 av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
353 av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
354 } else {
355 av_freep(&sync);
Ivan Uskovc90dbc62015-07-24 10:26:14356 }
357
Mark Thompson1f26a232016-10-21 17:57:12358 if (!av_fifo_space(q->async_fifo) ||
359 (!avpkt->size && av_fifo_size(q->async_fifo))) {
Anton Khirnovf5c4d382015-07-14 16:16:26360 AVFrame *src_frame;
361
362 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
363 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
364 out_frame->queued = 0;
365
Anton Khirnovb68e3532017-01-07 20:06:16366 if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
367 do {
368 ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
369 } while (ret == MFX_WRN_IN_EXECUTION);
370 }
Anton Khirnovf5c4d382015-07-14 16:16:26371
Mark Thompson1f26a232016-10-21 17:57:12372 av_freep(&sync);
373
Anton Khirnovf5c4d382015-07-14 16:16:26374 src_frame = out_frame->frame;
375
Anton Khirnov4e08c822015-02-10 09:40:59376 ret = av_frame_ref(frame, src_frame);
377 if (ret < 0)
378 return ret;
379
Anton Khirnov404e5142016-08-09 10:05:49380 outsurf = &out_frame->surface;
Anton Khirnovf5c4d382015-07-14 16:16:26381
Anton Khirnov32c83592016-03-19 20:45:24382#if FF_API_PKT_PTS
383FF_DISABLE_DEPRECATION_WARNINGS
384 frame->pkt_pts = outsurf->Data.TimeStamp;
385FF_ENABLE_DEPRECATION_WARNINGS
386#endif
387 frame->pts = outsurf->Data.TimeStamp;
Anton Khirnov4e08c822015-02-10 09:40:59388
389 frame->repeat_pict =
390 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
391 outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
392 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
393 frame->top_field_first =
394 outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
395 frame->interlaced_frame =
396 !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
397
Anton Khirnov404e5142016-08-09 10:05:49398 /* update the surface properties */
399 if (avctx->pix_fmt == AV_PIX_FMT_QSV)
400 ((mfxFrameSurface1*)frame->data[3])->Info = outsurf->Info;
401
Anton Khirnov4e08c822015-02-10 09:40:59402 *got_frame = 1;
403 }
404
Mark Thompson1f26a232016-10-21 17:57:12405 return bs.DataOffset;
Ivan Uskov3f8e2e92015-08-06 16:10:24406}
Anton Khirnov4e08c822015-02-10 09:40:59407
Anton Khirnov9ba27c22015-03-13 07:21:38408int ff_qsv_decode_close(QSVContext *q)
Anton Khirnov4e08c822015-02-10 09:40:59409{
Mark Thompson1f26a232016-10-21 17:57:12410 QSVFrame *cur = q->work_frames;
Anton Khirnov4e08c822015-02-10 09:40:59411
Mark Thompson1f26a232016-10-21 17:57:12412 if (q->session)
413 MFXVideoDECODE_Close(q->session);
Ivan Uskovcc167f72015-08-04 10:40:06414
Mark Thompson1f26a232016-10-21 17:57:12415 while (q->async_fifo && av_fifo_size(q->async_fifo)) {
416 QSVFrame *out_frame;
417 mfxSyncPoint *sync;
418
419 av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
420 av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
421
422 av_freep(&sync);
423 }
424
425 while (cur) {
426 q->work_frames = cur->next;
427 av_frame_free(&cur->frame);
428 av_freep(&cur);
429 cur = q->work_frames;
430 }
Anton Khirnov4e08c822015-02-10 09:40:59431
Anton Khirnovf5c4d382015-07-14 16:16:26432 av_fifo_free(q->async_fifo);
433 q->async_fifo = NULL;
434
Mark Thompson1f26a232016-10-21 17:57:12435 av_parser_close(q->parser);
436 avcodec_free_context(&q->avctx_internal);
Ivan Uskovc90dbc62015-07-24 10:26:14437
Mark Thompson1f26a232016-10-21 17:57:12438 if (q->internal_session)
439 MFXClose(q->internal_session);
440
441 av_buffer_unref(&q->frames_ctx.hw_frames_ctx);
Anton Khirnov4ab61cd2016-08-10 07:38:21442 av_buffer_unref(&q->frames_ctx.mids_buf);
Ivan Uskovd50ab822015-07-23 09:14:41443
Anton Khirnov4e08c822015-02-10 09:40:59444 return 0;
445}
Mark Thompson1f26a232016-10-21 17:57:12446
447int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q,
448 AVFrame *frame, int *got_frame, AVPacket *pkt)
449{
450 uint8_t *dummy_data;
451 int dummy_size;
452 int ret;
453
454 if (!q->avctx_internal) {
455 q->avctx_internal = avcodec_alloc_context3(NULL);
456 if (!q->avctx_internal)
457 return AVERROR(ENOMEM);
458
459 q->parser = av_parser_init(avctx->codec_id);
460 if (!q->parser)
461 return AVERROR(ENOMEM);
462
463 q->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
464 q->orig_pix_fmt = AV_PIX_FMT_NONE;
465 }
466
467 if (!pkt->size)
468 return qsv_decode(avctx, q, frame, got_frame, pkt);
469
470 /* we assume the packets are already split properly and want
471 * just the codec parameters here */
472 av_parser_parse2(q->parser, q->avctx_internal,
473 &dummy_data, &dummy_size,
474 pkt->data, pkt->size, pkt->pts, pkt->dts,
475 pkt->pos);
476
477 /* TODO: flush delayed frames on reinit */
478 if (q->parser->format != q->orig_pix_fmt ||
479 q->parser->coded_width != avctx->coded_width ||
480 q->parser->coded_height != avctx->coded_height) {
481 enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_QSV,
482 AV_PIX_FMT_NONE,
483 AV_PIX_FMT_NONE };
484 enum AVPixelFormat qsv_format;
485
Anton Khirnov536bb172016-06-22 09:41:26486 qsv_format = ff_qsv_map_pixfmt(q->parser->format, &q->fourcc);
Mark Thompson1f26a232016-10-21 17:57:12487 if (qsv_format < 0) {
488 av_log(avctx, AV_LOG_ERROR,
Anton Khirnov92736c72016-06-22 09:53:00489 "Decoding pixel format '%s' is not supported\n",
490 av_get_pix_fmt_name(q->parser->format));
Mark Thompson1f26a232016-10-21 17:57:12491 ret = AVERROR(ENOSYS);
492 goto reinit_fail;
493 }
494
495 q->orig_pix_fmt = q->parser->format;
496 avctx->pix_fmt = pix_fmts[1] = qsv_format;
497 avctx->width = q->parser->width;
498 avctx->height = q->parser->height;
499 avctx->coded_width = q->parser->coded_width;
500 avctx->coded_height = q->parser->coded_height;
501 avctx->field_order = q->parser->field_order;
502 avctx->level = q->avctx_internal->level;
503 avctx->profile = q->avctx_internal->profile;
504
505 ret = ff_get_format(avctx, pix_fmts);
506 if (ret < 0)
507 goto reinit_fail;
508
509 avctx->pix_fmt = ret;
510
511 ret = qsv_decode_init(avctx, q);
512 if (ret < 0)
513 goto reinit_fail;
514 }
515
516 return qsv_decode(avctx, q, frame, got_frame, pkt);
517
518reinit_fail:
519 q->orig_pix_fmt = q->parser->format = avctx->pix_fmt = AV_PIX_FMT_NONE;
520 return ret;
521}
522
523void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
524{
525 q->orig_pix_fmt = AV_PIX_FMT_NONE;
526}