]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsvdec_vc1.c
Merge commit '03adfe913062c6995136eb1ca51152b6d596c0f4'
[ffmpeg] / libavcodec / qsvdec_vc1.c
1 /*
2  * Intel MediaSDK QSV based VC-1 video decoder
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdint.h>
22 #include <string.h>
23
24 #include <mfx/mfxvideo.h>
25
26 #include "libavutil/common.h"
27 #include "libavutil/fifo.h"
28 #include "libavutil/opt.h"
29
30 #include "avcodec.h"
31 #include "internal.h"
32 #include "qsv_internal.h"
33 #include "qsvdec.h"
34 #include "qsv.h"
35
36 typedef struct QSVVC1Context {
37     AVClass *class;
38     QSVContext qsv;
39
40     AVFifoBuffer *packet_fifo;
41
42     AVPacket input_ref;
43 } QSVVC1Context;
44
45 static void qsv_clear_buffers(QSVVC1Context *s)
46 {
47     AVPacket pkt;
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);
51     }
52
53     av_packet_unref(&s->input_ref);
54 }
55
56 static av_cold int qsv_decode_close(AVCodecContext *avctx)
57 {
58     QSVVC1Context *s = avctx->priv_data;
59
60     ff_qsv_decode_close(&s->qsv);
61
62     qsv_clear_buffers(s);
63
64     av_fifo_free(s->packet_fifo);
65
66     return 0;
67 }
68
69 static av_cold int qsv_decode_init(AVCodecContext *avctx)
70 {
71     QSVVC1Context *s = avctx->priv_data;
72     int ret;
73
74     s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
75     if (!s->packet_fifo) {
76         ret = AVERROR(ENOMEM);
77         goto fail;
78     }
79
80     return 0;
81 fail:
82     qsv_decode_close(avctx);
83     return ret;
84 }
85
86 static int qsv_decode_frame(AVCodecContext *avctx, void *data,
87                             int *got_frame, AVPacket *avpkt)
88 {
89     QSVVC1Context *s = avctx->priv_data;
90     AVFrame *frame   = data;
91     int ret;
92
93     /* buffer the input packet */
94     if (avpkt->size) {
95         AVPacket input_ref = { 0 };
96
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));
100             if (ret < 0)
101                 return ret;
102         }
103
104         ret = av_packet_ref(&input_ref, avpkt);
105         if (ret < 0)
106             return ret;
107         av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
108     }
109
110     /* process buffered data */
111     while (!*got_frame) {
112         if (s->input_ref.size <= 0) {
113             /* no more data */
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);
116
117             av_packet_unref(&s->input_ref);
118             av_fifo_generic_read(s->packet_fifo, &s->input_ref, sizeof(s->input_ref), NULL);
119         }
120
121         ret = ff_qsv_process_data(avctx, &s->qsv, frame, got_frame, &s->input_ref);
122         if (ret < 0)
123             return ret;
124
125         s->input_ref.size -= ret;
126         s->input_ref.data += ret;
127     }
128
129     return avpkt->size;
130 }
131
132 static void qsv_decode_flush(AVCodecContext *avctx)
133 {
134     QSVVC1Context *s = avctx->priv_data;
135
136     qsv_clear_buffers(s);
137     ff_qsv_decode_flush(avctx, &s->qsv);
138 }
139
140 AVHWAccel ff_vc1_qsv_hwaccel = {
141     .name           = "vc1_qsv",
142     .type           = AVMEDIA_TYPE_VIDEO,
143     .id             = AV_CODEC_ID_VC1,
144     .pix_fmt        = AV_PIX_FMT_QSV,
145 };
146
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 },
151     { NULL },
152 };
153
154 static const AVClass class = {
155     .class_name = "vc1_qsv",
156     .item_name  = av_default_item_name,
157     .option     = options,
158     .version    = LIBAVUTIL_VERSION_INT,
159 };
160
161 AVCodec ff_vc1_qsv_decoder = {
162     .name           = "vc1_qsv",
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,
174                                                     AV_PIX_FMT_QSV,
175                                                     AV_PIX_FMT_NONE },
176 };