FFmpeg
qsvdec.c
Go to the documentation of this file.
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/hwcontext.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/log.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/pixfmt.h"
36 #include "libavutil/time.h"
37 #include "libavutil/imgutils.h"
38 
39 #include "avcodec.h"
40 #include "internal.h"
41 #include "decode.h"
42 #include "qsv.h"
43 #include "qsv_internal.h"
44 #include "qsvdec.h"
45 
47  &(const AVCodecHWConfigInternal) {
48  .public = {
52  .device_type = AV_HWDEVICE_TYPE_QSV,
53  },
54  .hwaccel = NULL,
55  },
56  NULL
57 };
58 
60 {
61  int ret = 0;
62 
63  ff_decode_frame_props(avctx, frame);
64 
65  frame->width = avctx->width;
66  frame->height = avctx->height;
67 
68  switch (avctx->pix_fmt) {
69  case AV_PIX_FMT_NV12:
70  frame->linesize[0] = FFALIGN(avctx->width, 128);
71  break;
72  case AV_PIX_FMT_P010:
73  frame->linesize[0] = 2 * FFALIGN(avctx->width, 128);
74  break;
75  default:
76  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
77  return AVERROR(ENOMEM);
78  }
79 
80  frame->linesize[1] = frame->linesize[0];
81  frame->buf[0] = av_buffer_pool_get(pool);
82  if (!frame->buf[0])
83  return AVERROR(ENOMEM);
84 
85  frame->data[0] = frame->buf[0]->data;
86  frame->data[1] = frame->data[0] +
87  frame->linesize[0] * FFALIGN(avctx->height, 64);
88 
89  ret = ff_attach_decode_data(frame);
90  if (ret < 0)
91  return ret;
92 
93  return 0;
94 }
95 
96 static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session,
97  AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref)
98 {
99  int ret;
100 
101  if (q->gpu_copy == MFX_GPUCOPY_ON &&
102  !(q->iopattern & MFX_IOPATTERN_OUT_SYSTEM_MEMORY))
103  av_log(avctx, AV_LOG_WARNING, "GPU-accelerated memory copy "
104  "only works in MFX_IOPATTERN_OUT_SYSTEM_MEMORY.\n");
105  if (session) {
106  q->session = session;
107  } else if (hw_frames_ref) {
108  if (q->internal_qs.session) {
109  MFXClose(q->internal_qs.session);
110  q->internal_qs.session = NULL;
111  }
113 
114  q->frames_ctx.hw_frames_ctx = av_buffer_ref(hw_frames_ref);
115  if (!q->frames_ctx.hw_frames_ctx)
116  return AVERROR(ENOMEM);
117 
119  &q->frames_ctx, q->load_plugins,
120  q->iopattern == MFX_IOPATTERN_OUT_OPAQUE_MEMORY,
121  q->gpu_copy);
122  if (ret < 0) {
124  return ret;
125  }
126 
127  q->session = q->internal_qs.session;
128  } else if (hw_device_ref) {
129  if (q->internal_qs.session) {
130  MFXClose(q->internal_qs.session);
131  q->internal_qs.session = NULL;
132  }
133 
135  hw_device_ref, q->load_plugins, q->gpu_copy);
136  if (ret < 0)
137  return ret;
138 
139  q->session = q->internal_qs.session;
140  } else {
141  if (!q->internal_qs.session) {
142  ret = ff_qsv_init_internal_session(avctx, &q->internal_qs,
143  q->load_plugins, q->gpu_copy);
144  if (ret < 0)
145  return ret;
146  }
147 
148  q->session = q->internal_qs.session;
149  }
150 
151  /* make sure the decoder is uninitialized */
152  MFXVideoDECODE_Close(q->session);
153 
154  return 0;
155 }
156 
157 static inline unsigned int qsv_fifo_item_size(void)
158 {
159  return sizeof(mfxSyncPoint*) + sizeof(QSVFrame*);
160 }
161 
162 static inline unsigned int qsv_fifo_size(const AVFifoBuffer* fifo)
163 {
164  return av_fifo_size(fifo) / qsv_fifo_item_size();
165 }
166 
167 static int check_dec_param(AVCodecContext *avctx, QSVContext *q, mfxVideoParam *param_in)
168 {
169  mfxVideoParam param_out = { .mfx.CodecId = param_in->mfx.CodecId };
170  mfxStatus ret;
171 
172 #define CHECK_MATCH(x) \
173  do { \
174  if (param_out.mfx.x != param_in->mfx.x) { \
175  av_log(avctx, AV_LOG_WARNING, "Required "#x" %d is unsupported\n", \
176  param_in->mfx.x); \
177  } \
178  } while (0)
179 
180  ret = MFXVideoDECODE_Query(q->session, param_in, &param_out);
181 
182  if (ret < 0) {
183  CHECK_MATCH(CodecId);
184  CHECK_MATCH(CodecProfile);
185  CHECK_MATCH(CodecLevel);
186  CHECK_MATCH(FrameInfo.Width);
187  CHECK_MATCH(FrameInfo.Height);
188 #undef CHECK_MATCH
189  return 0;
190  }
191  return 1;
192 }
193 
194 static int qsv_decode_preinit(AVCodecContext *avctx, QSVContext *q, enum AVPixelFormat pix_fmt, mfxVideoParam *param)
195 {
196  mfxSession session = NULL;
197  int iopattern = 0;
198  int ret;
199  enum AVPixelFormat pix_fmts[3] = {
200  AV_PIX_FMT_QSV, /* opaque format in case of video memory output */
201  pix_fmt, /* system memory format obtained from bitstream parser */
202  AV_PIX_FMT_NONE };
203 
204  ret = ff_get_format(avctx, pix_fmts);
205  if (ret < 0) {
206  q->orig_pix_fmt = avctx->pix_fmt = AV_PIX_FMT_NONE;
207  return ret;
208  }
209 
210  if (!q->async_fifo) {
212  if (!q->async_fifo)
213  return AVERROR(ENOMEM);
214  }
215 
216  if (avctx->pix_fmt == AV_PIX_FMT_QSV && avctx->hwaccel_context) {
217  AVQSVContext *user_ctx = avctx->hwaccel_context;
218  session = user_ctx->session;
219  iopattern = user_ctx->iopattern;
220  q->ext_buffers = user_ctx->ext_buffers;
221  q->nb_ext_buffers = user_ctx->nb_ext_buffers;
222  }
223 
224  if (avctx->hw_frames_ctx) {
225  AVHWFramesContext *frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
226  AVQSVFramesContext *frames_hwctx = frames_ctx->hwctx;
227 
228  if (!iopattern) {
229  if (frames_hwctx->frame_type & MFX_MEMTYPE_OPAQUE_FRAME)
230  iopattern = MFX_IOPATTERN_OUT_OPAQUE_MEMORY;
231  else if (frames_hwctx->frame_type & MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET)
232  iopattern = MFX_IOPATTERN_OUT_VIDEO_MEMORY;
233  }
234  }
235 
236  if (!iopattern)
237  iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
238  q->iopattern = iopattern;
239 
240  ff_qsv_print_iopattern(avctx, q->iopattern, "Decoder");
241 
242  ret = qsv_init_session(avctx, q, session, avctx->hw_frames_ctx, avctx->hw_device_ctx);
243  if (ret < 0) {
244  av_log(avctx, AV_LOG_ERROR, "Error initializing an MFX session\n");
245  return ret;
246  }
247 
248  param->IOPattern = q->iopattern;
249  param->AsyncDepth = q->async_depth;
250  param->ExtParam = q->ext_buffers;
251  param->NumExtParam = q->nb_ext_buffers;
252 
253  return 0;
254  }
255 
256 static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxVideoParam *param)
257 {
258  int ret;
259 
260  avctx->width = param->mfx.FrameInfo.CropW;
261  avctx->height = param->mfx.FrameInfo.CropH;
262  avctx->coded_width = param->mfx.FrameInfo.Width;
263  avctx->coded_height = param->mfx.FrameInfo.Height;
264  avctx->level = param->mfx.CodecLevel;
265  avctx->profile = param->mfx.CodecProfile;
266  avctx->field_order = ff_qsv_map_picstruct(param->mfx.FrameInfo.PicStruct);
267  avctx->pix_fmt = ff_qsv_map_fourcc(param->mfx.FrameInfo.FourCC);
268 
269  ret = MFXVideoDECODE_Init(q->session, param);
270  if (ret < 0)
271  return ff_qsv_print_error(avctx, ret,
272  "Error initializing the MFX video decoder");
273 
274  q->frame_info = param->mfx.FrameInfo;
275 
276  if (!avctx->hw_frames_ctx)
278  FFALIGN(avctx->width, 128), FFALIGN(avctx->height, 64), 1), av_buffer_allocz);
279  return 0;
280 }
281 
282 static int qsv_decode_header(AVCodecContext *avctx, QSVContext *q, AVPacket *avpkt, enum AVPixelFormat pix_fmt, mfxVideoParam *param)
283 {
284  int ret;
285 
286  mfxBitstream bs = { 0 };
287 
288  if (avpkt->size) {
289  bs.Data = avpkt->data;
290  bs.DataLength = avpkt->size;
291  bs.MaxLength = bs.DataLength;
292  bs.TimeStamp = avpkt->pts;
293  if (avctx->field_order == AV_FIELD_PROGRESSIVE)
294  bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME;
295  } else
296  return AVERROR_INVALIDDATA;
297 
298 
299  if(!q->session) {
300  ret = qsv_decode_preinit(avctx, q, pix_fmt, param);
301  if (ret < 0)
302  return ret;
303  }
304 
305  ret = ff_qsv_codec_id_to_mfx(avctx->codec_id);
306  if (ret < 0)
307  return ret;
308 
309  param->mfx.CodecId = ret;
310  ret = MFXVideoDECODE_DecodeHeader(q->session, &bs, param);
311  if (MFX_ERR_MORE_DATA == ret) {
312  return AVERROR(EAGAIN);
313  }
314  if (ret < 0)
315  return ff_qsv_print_error(avctx, ret,
316  "Error decoding stream header");
317 
318  return 0;
319 }
320 
322 {
323  int ret;
324 
325  if (q->pool)
326  ret = ff_qsv_get_continuous_buffer(avctx, frame->frame, q->pool);
327  else
328  ret = ff_get_buffer(avctx, frame->frame, AV_GET_BUFFER_FLAG_REF);
329 
330  if (ret < 0)
331  return ret;
332 
333  if (frame->frame->format == AV_PIX_FMT_QSV) {
334  frame->surface = *(mfxFrameSurface1*)frame->frame->data[3];
335  } else {
336  frame->surface.Info = q->frame_info;
337 
338  frame->surface.Data.PitchLow = frame->frame->linesize[0];
339  frame->surface.Data.Y = frame->frame->data[0];
340  frame->surface.Data.UV = frame->frame->data[1];
341  }
342 
343  if (q->frames_ctx.mids) {
344  ret = ff_qsv_find_surface_idx(&q->frames_ctx, frame);
345  if (ret < 0)
346  return ret;
347 
348  frame->surface.Data.MemId = &q->frames_ctx.mids[ret];
349  }
350  frame->surface.Data.ExtParam = &frame->ext_param;
351  frame->surface.Data.NumExtParam = 1;
352  frame->ext_param = (mfxExtBuffer*)&frame->dec_info;
353  frame->dec_info.Header.BufferId = MFX_EXTBUFF_DECODED_FRAME_INFO;
354  frame->dec_info.Header.BufferSz = sizeof(frame->dec_info);
355 
356  frame->used = 1;
357 
358  return 0;
359 }
360 
362 {
363  QSVFrame *cur = q->work_frames;
364  while (cur) {
365  if (cur->used && !cur->surface.Data.Locked && !cur->queued) {
366  cur->used = 0;
367  av_frame_unref(cur->frame);
368  }
369  cur = cur->next;
370  }
371 }
372 
373 static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
374 {
375  QSVFrame *frame, **last;
376  int ret;
377 
379 
380  frame = q->work_frames;
381  last = &q->work_frames;
382  while (frame) {
383  if (!frame->used) {
384  ret = alloc_frame(avctx, q, frame);
385  if (ret < 0)
386  return ret;
387  *surf = &frame->surface;
388  return 0;
389  }
390 
391  last = &frame->next;
392  frame = frame->next;
393  }
394 
395  frame = av_mallocz(sizeof(*frame));
396  if (!frame)
397  return AVERROR(ENOMEM);
398  frame->frame = av_frame_alloc();
399  if (!frame->frame) {
400  av_freep(&frame);
401  return AVERROR(ENOMEM);
402  }
403  *last = frame;
404 
405  ret = alloc_frame(avctx, q, frame);
406  if (ret < 0)
407  return ret;
408 
409  *surf = &frame->surface;
410 
411  return 0;
412 }
413 
414 static QSVFrame *find_frame(QSVContext *q, mfxFrameSurface1 *surf)
415 {
416  QSVFrame *cur = q->work_frames;
417  while (cur) {
418  if (surf == &cur->surface)
419  return cur;
420  cur = cur->next;
421  }
422  return NULL;
423 }
424 
425 static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
426  AVFrame *frame, int *got_frame,
427  AVPacket *avpkt)
428 {
429  QSVFrame *out_frame;
430  mfxFrameSurface1 *insurf;
431  mfxFrameSurface1 *outsurf;
432  mfxSyncPoint *sync;
433  mfxBitstream bs = { { { 0 } } };
434  int ret;
435 
436  if (avpkt->size) {
437  bs.Data = avpkt->data;
438  bs.DataLength = avpkt->size;
439  bs.MaxLength = bs.DataLength;
440  bs.TimeStamp = avpkt->pts;
441  if (avctx->field_order == AV_FIELD_PROGRESSIVE)
442  bs.DataFlag |= MFX_BITSTREAM_COMPLETE_FRAME;
443  }
444 
445  sync = av_mallocz(sizeof(*sync));
446  if (!sync) {
447  av_freep(&sync);
448  return AVERROR(ENOMEM);
449  }
450 
451  do {
452  ret = get_surface(avctx, q, &insurf);
453  if (ret < 0) {
454  av_freep(&sync);
455  return ret;
456  }
457 
458  ret = MFXVideoDECODE_DecodeFrameAsync(q->session, avpkt->size ? &bs : NULL,
459  insurf, &outsurf, sync);
460  if (ret == MFX_WRN_DEVICE_BUSY)
461  av_usleep(500);
462 
463  } while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_ERR_MORE_SURFACE);
464 
465  if (ret != MFX_ERR_NONE &&
466  ret != MFX_ERR_MORE_DATA &&
467  ret != MFX_WRN_VIDEO_PARAM_CHANGED &&
468  ret != MFX_ERR_MORE_SURFACE) {
469  av_freep(&sync);
470  return ff_qsv_print_error(avctx, ret,
471  "Error during QSV decoding.");
472  }
473 
474  /* make sure we do not enter an infinite loop if the SDK
475  * did not consume any data and did not return anything */
476  if (!*sync && !bs.DataOffset) {
477  bs.DataOffset = avpkt->size;
478  ++q->zero_consume_run;
479  if (q->zero_consume_run > 1)
480  ff_qsv_print_warning(avctx, ret, "A decode call did not consume any data");
481  } else if (!*sync && bs.DataOffset) {
482  ++q->buffered_count;
483  } else {
484  q->zero_consume_run = 0;
485  }
486 
487  if (*sync) {
488  QSVFrame *out_frame = find_frame(q, outsurf);
489 
490  if (!out_frame) {
491  av_log(avctx, AV_LOG_ERROR,
492  "The returned surface does not correspond to any frame\n");
493  av_freep(&sync);
494  return AVERROR_BUG;
495  }
496 
497  out_frame->queued = 1;
498  av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
499  av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
500  } else {
501  av_freep(&sync);
502  }
503 
504  if ((qsv_fifo_size(q->async_fifo) >= q->async_depth) ||
505  (!avpkt->size && av_fifo_size(q->async_fifo))) {
506  AVFrame *src_frame;
507 
508  av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
509  av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
510  out_frame->queued = 0;
511 
512  if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
513  do {
514  ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
515  } while (ret == MFX_WRN_IN_EXECUTION);
516  }
517 
518  av_freep(&sync);
519 
520  src_frame = out_frame->frame;
521 
522  ret = av_frame_ref(frame, src_frame);
523  if (ret < 0)
524  return ret;
525 
526  outsurf = &out_frame->surface;
527 
528 #if FF_API_PKT_PTS
530  frame->pkt_pts = outsurf->Data.TimeStamp;
532 #endif
533  frame->pts = outsurf->Data.TimeStamp;
534 
535  frame->repeat_pict =
536  outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_TRIPLING ? 4 :
537  outsurf->Info.PicStruct & MFX_PICSTRUCT_FRAME_DOUBLING ? 2 :
538  outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_REPEATED ? 1 : 0;
539  frame->top_field_first =
540  outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
541  frame->interlaced_frame =
542  !(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
543  frame->pict_type = ff_qsv_map_pictype(out_frame->dec_info.FrameType);
544  //Key frame is IDR frame is only suitable for H264. For HEVC, IRAPs are key frames.
545  if (avctx->codec_id == AV_CODEC_ID_H264)
546  frame->key_frame = !!(out_frame->dec_info.FrameType & MFX_FRAMETYPE_IDR);
547 
548  /* update the surface properties */
549  if (avctx->pix_fmt == AV_PIX_FMT_QSV)
550  ((mfxFrameSurface1*)frame->data[3])->Info = outsurf->Info;
551 
552  *got_frame = 1;
553  }
554 
555  return bs.DataOffset;
556 }
557 
559 {
560  QSVFrame *cur = q->work_frames;
561 
562  if (q->session)
563  MFXVideoDECODE_Close(q->session);
564 
565  while (q->async_fifo && av_fifo_size(q->async_fifo)) {
566  QSVFrame *out_frame;
567  mfxSyncPoint *sync;
568 
569  av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
570  av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
571 
572  av_freep(&sync);
573  }
574 
575  while (cur) {
576  q->work_frames = cur->next;
577  av_frame_free(&cur->frame);
578  av_freep(&cur);
579  cur = q->work_frames;
580  }
581 
583  q->async_fifo = NULL;
584 
586 
590 
591  return 0;
592 }
593 
595  AVFrame *frame, int *got_frame, AVPacket *pkt)
596 {
597  int ret;
598  mfxVideoParam param = { 0 };
600 
601  if (!pkt->size)
602  return qsv_decode(avctx, q, frame, got_frame, pkt);
603 
604  /* TODO: flush delayed frames on reinit */
605 
606  // sw_pix_fmt, coded_width/height should be set for ff_get_format(),
607  // assume sw_pix_fmt is NV12 and coded_width/height to be 1280x720,
608  // the assumption may be not corret but will be updated after header decoded if not true.
609  if (q->orig_pix_fmt != AV_PIX_FMT_NONE)
610  pix_fmt = q->orig_pix_fmt;
611  if (!avctx->coded_width)
612  avctx->coded_width = 1280;
613  if (!avctx->coded_height)
614  avctx->coded_height = 720;
615 
616  ret = qsv_decode_header(avctx, q, pkt, pix_fmt, &param);
617 
618  if (ret >= 0 && (q->orig_pix_fmt != ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC) ||
619  avctx->coded_width != param.mfx.FrameInfo.Width ||
620  avctx->coded_height != param.mfx.FrameInfo.Height)) {
621  AVPacket zero_pkt = {0};
622 
623  if (q->buffered_count) {
624  q->reinit_flag = 1;
625  /* decode zero-size pkt to flush the buffered pkt before reinit */
626  q->buffered_count--;
627  return qsv_decode(avctx, q, frame, got_frame, &zero_pkt);
628  }
629  q->reinit_flag = 0;
630 
631  q->orig_pix_fmt = avctx->pix_fmt = pix_fmt = ff_qsv_map_fourcc(param.mfx.FrameInfo.FourCC);
632 
633  avctx->coded_width = param.mfx.FrameInfo.Width;
634  avctx->coded_height = param.mfx.FrameInfo.Height;
635 
636  ret = qsv_decode_preinit(avctx, q, pix_fmt, &param);
637  if (ret < 0)
638  goto reinit_fail;
639  q->initialized = 0;
640  }
641 
642  if (!q->initialized) {
643  ret = qsv_decode_init(avctx, q, &param);
644  if (ret < 0)
645  goto reinit_fail;
646  q->initialized = 1;
647  }
648 
649  return qsv_decode(avctx, q, frame, got_frame, pkt);
650 
651 reinit_fail:
652  q->orig_pix_fmt = avctx->pix_fmt = AV_PIX_FMT_NONE;
653  return ret;
654 }
655 
657 {
659  q->initialized = 0;
660 }
AVCodecHWConfig public
This is the structure which will be returned to the user by avcodec_get_hw_config().
Definition: hwaccel.h:34
#define NULL
Definition: coverity.c:32
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: decode.c:1370
int iopattern
Definition: qsvdec.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static enum AVPixelFormat pix_fmt
static unsigned int qsv_fifo_size(const AVFifoBuffer *fifo)
Definition: qsvdec.c:162
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1756
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Memory handling functions.
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:486
This struct is allocated as AVHWFramesContext.hwctx.
Definition: hwcontext_qsv.h:42
int repeat_pict
When decoding, this signals how much the picture must be delayed.
Definition: frame.h:437
int size
Definition: avcodec.h:1481
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1778
mfxExtBuffer ** ext_buffers
Definition: qsvdec.h:73
int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession, AVBufferRef *device_ref, const char *load_plugins, int gpu_copy)
Definition: qsv.c:720
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
int ff_decode_frame_props(AVCodecContext *avctx, AVFrame *frame)
Set various frame properties from the codec context / packet data.
Definition: decode.c:1737
static AVPacket pkt
static int qsv_decode(AVCodecContext *avctx, QSVContext *q, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: qsvdec.c:425
int profile
profile
Definition: avcodec.h:2901
QSVSession internal_qs
Definition: qsvdec.h:45
static int ff_qsv_get_continuous_buffer(AVCodecContext *avctx, AVFrame *frame, AVBufferPool *pool)
Definition: qsvdec.c:59
static int qsv_decode_init(AVCodecContext *avctx, QSVContext *q, mfxVideoParam *param)
Definition: qsvdec.c:256
AVBufferRef * hw_frames_ctx
Definition: qsv_internal.h:92
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
Definition: fifo.c:122
int ff_qsv_print_error(void *log_ctx, mfxStatus err, const char *error_string)
Definition: qsv.c:224
#define AV_PIX_FMT_P010
Definition: pixfmt.h:436
mfxExtDecodedFrameInfo dec_info
Definition: qsv_internal.h:74
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
int ff_attach_decode_data(AVFrame *frame)
Definition: decode.c:1881
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2744
AVBufferRef * mids_buf
Definition: qsv_internal.h:99
The codec supports this format by some ad-hoc method.
Definition: avcodec.h:3463
int ff_qsv_decode_close(QSVContext *q)
Definition: qsvdec.c:558
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:443
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
enum AVPixelFormat pix_fmt
A hardware pixel format which the codec can use.
Definition: avcodec.h:3470
int queued
Definition: qsv_internal.h:77
uint8_t * data
Definition: avcodec.h:1480
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
Definition: fifo.c:55
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:442
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
The buffer pool.
static QSVFrame * find_frame(QSVContext *q, mfxFrameSurface1 *surf)
Definition: qsvdec.c:414
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
Definition: imgutils.c:431
int ff_qsv_init_session_frames(AVCodecContext *avctx, mfxSession *psession, QSVFramesContext *qsv_frames_ctx, const char *load_plugins, int opaque, int gpu_copy)
Definition: qsv.c:797
int width
Definition: frame.h:353
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
Definition: qsv.c:271
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
int reinit_flag
Definition: qsvdec.h:57
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
Definition: fifo.c:213
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:89
int ff_qsv_print_iopattern(void *log_ctx, int mfx_iopattern, const char *extra_string)
Definition: qsv.c:152
int iopattern
The IO pattern to use.
Definition: qsv.h:46
int ff_qsv_close_internal_session(QSVSession *qs)
Definition: qsv.c:844
enum AVPictureType ff_qsv_map_pictype(int mfx_pic_type)
Definition: qsv.c:300
int nb_ext_buffers
Definition: qsv.h:52
#define CHECK_MATCH(x)
void ff_qsv_decode_flush(AVCodecContext *avctx, QSVContext *q)
Definition: qsvdec.c:656
int buffered_count
Definition: qsvdec.h:56
int ff_qsv_print_warning(void *log_ctx, mfxStatus err, const char *warning_string)
Definition: qsv.c:234
mfxExtBuffer * ext_param
Definition: qsv_internal.h:75
int zero_consume_run
Definition: qsvdec.h:55
AVBufferPool * pool
Definition: qsvdec.h:62
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:378
static unsigned int qsv_fifo_item_size(void)
Definition: qsvdec.c:157
int width
picture width / height.
Definition: avcodec.h:1741
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
Definition: avcodec.h:3265
int ff_qsv_codec_id_to_mfx(enum AVCodecID codec_id)
Definition: qsv.c:43
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
int level
level
Definition: avcodec.h:3021
mfxFrameSurface1 surface
Definition: qsv_internal.h:72
if(ret)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:368
enum AVPixelFormat orig_pix_fmt
Definition: qsvdec.h:59
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1578
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
Definition: fifo.c:77
mfxExtBuffer ** ext_buffers
Extra buffers to pass to encoder or decoder initialization.
Definition: qsv.h:51
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:326
static int check_dec_param(AVCodecContext *avctx, QSVContext *q, mfxVideoParam *param_in)
Definition: qsvdec.c:167
static int alloc_frame(AVCodecContext *avctx, QSVContext *q, QSVFrame *frame)
Definition: qsvdec.c:321
main external API structure.
Definition: avcodec.h:1568
uint8_t * data
The data buffer.
Definition: buffer.h:89
struct QSVFrame * next
Definition: qsv_internal.h:80
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:161
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1968
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:83
static int get_surface(AVCodecContext *avctx, QSVContext *q, mfxFrameSurface1 **surf)
Definition: qsvdec.c:373
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
int coded_height
Definition: avcodec.h:1756
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:275
int gpu_copy
Definition: qsvdec.h:69
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:123
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
Definition: pixfmt.h:222
int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs, const char *load_plugins, int gpu_copy)
Definition: qsv.c:414
char * load_plugins
Definition: qsvdec.h:71
This struct is used for communicating QSV parameters between libavcodec and the caller.
Definition: qsv.h:36
static void qsv_clear_unused_frames(QSVContext *q)
Definition: qsvdec.c:361
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
The codec supports this format via the hw_frames_ctx interface.
Definition: avcodec.h:3447
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
attribute_deprecated int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
Definition: frame.h:396
A reference to a data buffer.
Definition: buffer.h:81
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
common internal api header.
common internal and external API header
AVBufferPool * av_buffer_pool_init(int size, AVBufferRef *(*alloc)(int size))
Allocate and initialize a buffer pool.
Definition: buffer.c:238
static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession session, AVBufferRef *hw_frames_ref, AVBufferRef *hw_device_ref)
Definition: qsvdec.c:96
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
mfxFrameInfo frame_info
Definition: qsvdec.h:61
pixel format definitions
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
Definition: fifo.c:43
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:447
QSVFramesContext frames_ctx
Definition: qsvdec.h:47
static int qsv_decode_preinit(AVCodecContext *avctx, QSVContext *q, enum AVPixelFormat pix_fmt, mfxVideoParam *param)
Definition: qsvdec.c:194
mfxSession session
Definition: qsvdec.h:41
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:373
AVFifoBuffer * async_fifo
Definition: qsvdec.h:54
int height
Definition: frame.h:353
int initialized
Definition: qsvdec.h:64
enum AVFieldOrder ff_qsv_map_picstruct(int mfx_pic_struct)
Definition: qsv.c:282
#define av_freep(p)
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:2225
An API-specific header for AV_HWDEVICE_TYPE_QSV.
AVFrame * frame
Definition: qsv_internal.h:71
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:334
const AVCodecHWConfigInternal * ff_qsv_hw_configs[]
Definition: qsvdec.c:46
int async_depth
Definition: qsvdec.h:67
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:3317
int ff_qsv_process_data(AVCodecContext *avctx, QSVContext *q, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: qsvdec.c:594
QSVFrame * work_frames
a linked list of frames currently being used by QSV
Definition: qsvdec.h:52
static int qsv_decode_header(AVCodecContext *avctx, QSVContext *q, AVPacket *avpkt, enum AVPixelFormat pix_fmt, mfxVideoParam *param)
Definition: qsvdec.c:282
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1457
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1179
mfxSession session
If non-NULL, the session to use for encoding or decoding.
Definition: qsv.h:41
mfxSession session
Definition: qsv_internal.h:84
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1473
enum AVPixelFormat ff_qsv_map_fourcc(uint32_t fourcc)
Definition: qsv.c:244
int nb_ext_buffers
Definition: qsvdec.h:74