qsvdec_h2645: switch to the new BSF API
[libav.git] / libavcodec / qsvdec_h2645.c
CommitLineData
4e08c821 1/*
41d47ea8 2 * Intel MediaSDK QSV based H.264 / HEVC decoder
4e08c821
AK
3 *
4 * copyright (c) 2013 Luca Barbato
5 * copyright (c) 2015 Anton Khirnov
6 *
7 * This file is part of Libav.
8 *
9 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24
25#include <stdint.h>
26#include <string.h>
27
28#include <mfx/mfxvideo.h>
29
30#include "libavutil/common.h"
31#include "libavutil/fifo.h"
32#include "libavutil/opt.h"
33
34#include "avcodec.h"
35#include "internal.h"
d0a63d8b 36#include "qsv_internal.h"
b04d009b 37#include "qsvdec.h"
4e08c821
AK
38#include "qsv.h"
39
41d47ea8
AK
40enum LoadPlugin {
41 LOAD_PLUGIN_NONE,
42 LOAD_PLUGIN_HEVC_SW,
43};
44
45typedef struct QSVH2645Context {
4e08c821
AK
46 AVClass *class;
47 QSVContext qsv;
48
41d47ea8
AK
49 int load_plugin;
50
4e08c821 51 // the filter for converting to Annex B
e3dfef8e 52 AVBSFContext *bsf;
4e08c821
AK
53
54 AVFifoBuffer *packet_fifo;
55
4e08c821 56 AVPacket pkt_filtered;
41d47ea8 57} QSVH2645Context;
4e08c821 58
41d47ea8 59static void qsv_clear_buffers(QSVH2645Context *s)
4e08c821
AK
60{
61 AVPacket pkt;
62 while (av_fifo_size(s->packet_fifo) >= sizeof(pkt)) {
63 av_fifo_generic_read(s->packet_fifo, &pkt, sizeof(pkt), NULL);
64 av_packet_unref(&pkt);
65 }
66
e3dfef8e
AK
67 av_bsf_free(&s->bsf);
68
69 av_packet_unref(&s->pkt_filtered);
4e08c821
AK
70}
71
72static av_cold int qsv_decode_close(AVCodecContext *avctx)
73{
41d47ea8 74 QSVH2645Context *s = avctx->priv_data;
4e08c821 75
9ba27c23 76 ff_qsv_decode_close(&s->qsv);
4e08c821
AK
77
78 qsv_clear_buffers(s);
79
80 av_fifo_free(s->packet_fifo);
81
4e08c821
AK
82 return 0;
83}
84
85static av_cold int qsv_decode_init(AVCodecContext *avctx)
86{
41d47ea8 87 QSVH2645Context *s = avctx->priv_data;
4e08c821
AK
88 int ret;
89
41d47ea8
AK
90 if (avctx->codec_id == AV_CODEC_ID_HEVC && s->load_plugin != LOAD_PLUGIN_NONE) {
91 static const char *uid_hevcenc_sw = "15dd936825ad475ea34e35f3f54217a6";
92
93 if (s->qsv.load_plugins[0]) {
94 av_log(avctx, AV_LOG_WARNING,
95 "load_plugins is not empty, but load_plugin is not set to 'none'."
96 "The load_plugin value will be ignored.\n");
97 } else {
98 av_freep(&s->qsv.load_plugins);
99 s->qsv.load_plugins = av_strdup(uid_hevcenc_sw);
100 if (!s->qsv.load_plugins)
101 return AVERROR(ENOMEM);
102 }
103 }
4e08c821
AK
104
105 s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
106 if (!s->packet_fifo) {
107 ret = AVERROR(ENOMEM);
108 goto fail;
109 }
110
4e08c821
AK
111 s->qsv.iopattern = MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
112
113 return 0;
114fail:
115 qsv_decode_close(avctx);
116 return ret;
117}
118
e3dfef8e
AK
119static int qsv_init_bsf(AVCodecContext *avctx, QSVH2645Context *s)
120{
121 const char *filter_name = avctx->codec_id == AV_CODEC_ID_HEVC ?
122 "hevc_mp4toannexb" : "h264_mp4toannexb";
123 const AVBitStreamFilter *filter;
124 int ret;
125
126 if (s->bsf)
127 return 0;
128
129 filter = av_bsf_get_by_name(filter_name);
130 if (!filter)
131 return AVERROR_BUG;
132
133 ret = av_bsf_alloc(filter, &s->bsf);
134 if (ret < 0)
135 return ret;
136
137 ret = avcodec_parameters_from_context(s->bsf->par_in, avctx);
138 if (ret < 0)
139 return ret;
140
141 s->bsf->time_base_in = avctx->time_base;
142
143 ret = av_bsf_init(s->bsf);
144 if (ret < 0)
145 return ret;
146
147 return ret;
148}
149
4e08c821
AK
150static int qsv_decode_frame(AVCodecContext *avctx, void *data,
151 int *got_frame, AVPacket *avpkt)
152{
41d47ea8 153 QSVH2645Context *s = avctx->priv_data;
4e08c821
AK
154 AVFrame *frame = data;
155 int ret;
156
e3dfef8e
AK
157 /* make sure the bitstream filter is initialized */
158 ret = qsv_init_bsf(avctx, s);
159 if (ret < 0)
160 return ret;
161
4e08c821
AK
162 /* buffer the input packet */
163 if (avpkt->size) {
164 AVPacket input_ref = { 0 };
165
166 if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
167 ret = av_fifo_realloc2(s->packet_fifo,
168 av_fifo_size(s->packet_fifo) + sizeof(input_ref));
169 if (ret < 0)
170 return ret;
171 }
172
173 ret = av_packet_ref(&input_ref, avpkt);
174 if (ret < 0)
175 return ret;
176 av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
177 }
178
179 /* process buffered data */
180 while (!*got_frame) {
181 /* prepare the input data -- convert to Annex B if needed */
182 if (s->pkt_filtered.size <= 0) {
e3dfef8e 183 AVPacket input_ref;
4e08c821
AK
184
185 /* no more data */
186 if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
96dca089 187 return avpkt->size ? avpkt->size : ff_qsv_process_data(avctx, &s->qsv, frame, got_frame, avpkt);
4e08c821 188
e3dfef8e 189 av_packet_unref(&s->pkt_filtered);
4e08c821 190
e3dfef8e
AK
191 av_fifo_generic_read(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
192 ret = av_bsf_send_packet(s->bsf, &input_ref);
4e08c821 193 if (ret < 0) {
e3dfef8e
AK
194 av_packet_unref(&input_ref);
195 return ret;
4e08c821 196 }
e3dfef8e
AK
197
198 ret = av_bsf_receive_packet(s->bsf, &s->pkt_filtered);
199 if (ret < 0)
200 av_packet_move_ref(&s->pkt_filtered, &input_ref);
201 else
202 av_packet_unref(&input_ref);
4e08c821
AK
203 }
204
96dca089 205 ret = ff_qsv_process_data(avctx, &s->qsv, frame, got_frame, &s->pkt_filtered);
4e08c821
AK
206 if (ret < 0)
207 return ret;
208
209 s->pkt_filtered.size -= ret;
210 s->pkt_filtered.data += ret;
211 }
212
213 return avpkt->size;
214}
215
216static void qsv_decode_flush(AVCodecContext *avctx)
217{
41d47ea8 218 QSVH2645Context *s = avctx->priv_data;
4e08c821
AK
219
220 qsv_clear_buffers(s);
96dca089 221 ff_qsv_decode_flush(avctx, &s->qsv);
4e08c821
AK
222}
223
41d47ea8
AK
224#define OFFSET(x) offsetof(QSVH2645Context, x)
225#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
226
227#if CONFIG_HEVC_QSV_DECODER
228AVHWAccel ff_hevc_qsv_hwaccel = {
229 .name = "hevc_qsv",
230 .type = AVMEDIA_TYPE_VIDEO,
231 .id = AV_CODEC_ID_HEVC,
232 .pix_fmt = AV_PIX_FMT_QSV,
233};
234
235static const AVOption hevc_options[] = {
236 { "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 },
237
238 { "load_plugin", "A user plugin to load in an internal session", OFFSET(load_plugin), AV_OPT_TYPE_INT, { .i64 = LOAD_PLUGIN_HEVC_SW }, LOAD_PLUGIN_NONE, LOAD_PLUGIN_HEVC_SW, VD, "load_plugin" },
239 { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_NONE }, 0, 0, VD, "load_plugin" },
240 { "hevc_sw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_SW }, 0, 0, VD, "load_plugin" },
241
242 { "load_plugins", "A :-separate list of hexadecimal plugin UIDs to load in an internal session",
243 OFFSET(qsv.load_plugins), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
244 { NULL },
245};
246
247static const AVClass hevc_class = {
248 .class_name = "hevc_qsv",
249 .item_name = av_default_item_name,
250 .option = hevc_options,
251 .version = LIBAVUTIL_VERSION_INT,
252};
253
254AVCodec ff_hevc_qsv_decoder = {
255 .name = "hevc_qsv",
256 .long_name = NULL_IF_CONFIG_SMALL("HEVC (Intel Quick Sync Video acceleration)"),
257 .priv_data_size = sizeof(QSVH2645Context),
258 .type = AVMEDIA_TYPE_VIDEO,
259 .id = AV_CODEC_ID_HEVC,
260 .init = qsv_decode_init,
261 .decode = qsv_decode_frame,
262 .flush = qsv_decode_flush,
263 .close = qsv_decode_close,
2d591595 264 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
41d47ea8 265 .priv_class = &hevc_class,
741b352b
AK
266 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
267 AV_PIX_FMT_QSV,
268 AV_PIX_FMT_NONE },
41d47ea8
AK
269};
270#endif
271
272#if CONFIG_H264_QSV_DECODER
4e08c821
AK
273AVHWAccel ff_h264_qsv_hwaccel = {
274 .name = "h264_qsv",
275 .type = AVMEDIA_TYPE_VIDEO,
276 .id = AV_CODEC_ID_H264,
277 .pix_fmt = AV_PIX_FMT_QSV,
278};
279
4e08c821
AK
280static const AVOption options[] = {
281 { "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 },
282 { NULL },
283};
284
285static const AVClass class = {
286 .class_name = "h264_qsv",
287 .item_name = av_default_item_name,
288 .option = options,
289 .version = LIBAVUTIL_VERSION_INT,
290};
291
292AVCodec ff_h264_qsv_decoder = {
293 .name = "h264_qsv",
294 .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration)"),
41d47ea8 295 .priv_data_size = sizeof(QSVH2645Context),
4e08c821
AK
296 .type = AVMEDIA_TYPE_VIDEO,
297 .id = AV_CODEC_ID_H264,
298 .init = qsv_decode_init,
299 .decode = qsv_decode_frame,
300 .flush = qsv_decode_flush,
301 .close = qsv_decode_close,
def97856 302 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
4e08c821 303 .priv_class = &class,
741b352b
AK
304 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
305 AV_PIX_FMT_QSV,
306 AV_PIX_FMT_NONE },
4e08c821 307};
41d47ea8 308#endif