2 * Intel MediaSDK QSV based VC-1 video decoder
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <mfx/mfxvideo.h>
26 #include "libavutil/common.h"
27 #include "libavutil/fifo.h"
28 #include "libavutil/opt.h"
32 #include "qsv_internal.h"
36 typedef struct QSVVC1Context {
40 AVFifoBuffer *packet_fifo;
45 static void qsv_clear_buffers(QSVVC1Context *s)
48 while (av_fifo_size(s->packet_fifo) >= sizeof(pkt)) {
49 av_fifo_generic_read(s->packet_fifo, &pkt, sizeof(pkt), NULL);
50 av_packet_unref(&pkt);
53 av_packet_unref(&s->input_ref);
56 static av_cold int qsv_decode_close(AVCodecContext *avctx)
58 QSVVC1Context *s = avctx->priv_data;
60 ff_qsv_decode_close(&s->qsv);
64 av_fifo_free(s->packet_fifo);
69 static av_cold int qsv_decode_init(AVCodecContext *avctx)
71 QSVVC1Context *s = avctx->priv_data;
74 s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
75 if (!s->packet_fifo) {
76 ret = AVERROR(ENOMEM);
82 qsv_decode_close(avctx);
86 static int qsv_decode_frame(AVCodecContext *avctx, void *data,
87 int *got_frame, AVPacket *avpkt)
89 QSVVC1Context *s = avctx->priv_data;
90 AVFrame *frame = data;
93 /* buffer the input packet */
95 AVPacket input_ref = { 0 };
97 if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
98 ret = av_fifo_realloc2(s->packet_fifo,
99 av_fifo_size(s->packet_fifo) + sizeof(input_ref));
104 ret = av_packet_ref(&input_ref, avpkt);
107 av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
110 /* process buffered data */
111 while (!*got_frame) {
112 if (s->input_ref.size <= 0) {
114 if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
115 return avpkt->size ? avpkt->size : ff_qsv_process_data(avctx, &s->qsv, frame, got_frame, avpkt);
117 av_packet_unref(&s->input_ref);
118 av_fifo_generic_read(s->packet_fifo, &s->input_ref, sizeof(s->input_ref), NULL);
121 ret = ff_qsv_process_data(avctx, &s->qsv, frame, got_frame, &s->input_ref);
125 s->input_ref.size -= ret;
126 s->input_ref.data += ret;
132 static void qsv_decode_flush(AVCodecContext *avctx)
134 QSVVC1Context *s = avctx->priv_data;
136 qsv_clear_buffers(s);
137 ff_qsv_decode_flush(avctx, &s->qsv);
140 AVHWAccel ff_vc1_qsv_hwaccel = {
142 .type = AVMEDIA_TYPE_VIDEO,
143 .id = AV_CODEC_ID_VC1,
144 .pix_fmt = AV_PIX_FMT_QSV,
147 #define OFFSET(x) offsetof(QSVVC1Context, x)
148 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
149 static const AVOption options[] = {
150 { "async_depth", "Internal parallelization depth, the higher the value the higher the latency.", OFFSET(qsv.async_depth), AV_OPT_TYPE_INT, { .i64 = ASYNC_DEPTH_DEFAULT }, 0, INT_MAX, VD },
154 static const AVClass class = {
155 .class_name = "vc1_qsv",
156 .item_name = av_default_item_name,
158 .version = LIBAVUTIL_VERSION_INT,
161 AVCodec ff_vc1_qsv_decoder = {
163 .long_name = NULL_IF_CONFIG_SMALL("VC-1 video (Intel Quick Sync Video acceleration)"),
164 .priv_data_size = sizeof(QSVVC1Context),
165 .type = AVMEDIA_TYPE_VIDEO,
166 .id = AV_CODEC_ID_VC1,
167 .init = qsv_decode_init,
168 .decode = qsv_decode_frame,
169 .flush = qsv_decode_flush,
170 .close = qsv_decode_close,
171 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_AVOID_PROBING,
172 .priv_class = &class,
173 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,