]> git.sesse.net Git - ffmpeg/blob - libavcodec/qsvdec_h2645.c
134b5a614a28d95844f7a4e75d2fc5577933672d
[ffmpeg] / libavcodec / qsvdec_h2645.c
1 /*
2  * Intel MediaSDK QSV based H.264 / HEVC decoder
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"
36 #include "qsv_internal.h"
37 #include "qsvdec.h"
38 #include "qsv.h"
39
40 enum LoadPlugin {
41     LOAD_PLUGIN_NONE,
42     LOAD_PLUGIN_HEVC_SW,
43 };
44
45 typedef struct QSVH2645Context {
46     AVClass *class;
47     QSVContext qsv;
48
49     int load_plugin;
50
51     // the filter for converting to Annex B
52     AVBSFContext *bsf;
53
54     AVFifoBuffer *packet_fifo;
55
56     AVPacket pkt_filtered;
57 } QSVH2645Context;
58
59 static void qsv_clear_buffers(QSVH2645Context *s)
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
67     av_bsf_free(&s->bsf);
68
69     av_packet_unref(&s->pkt_filtered);
70 }
71
72 static av_cold int qsv_decode_close(AVCodecContext *avctx)
73 {
74     QSVH2645Context *s = avctx->priv_data;
75
76     ff_qsv_decode_close(&s->qsv);
77
78     qsv_clear_buffers(s);
79
80     av_fifo_free(s->packet_fifo);
81
82     return 0;
83 }
84
85 static av_cold int qsv_decode_init(AVCodecContext *avctx)
86 {
87     QSVH2645Context *s = avctx->priv_data;
88     int ret;
89
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     }
104
105     s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
106     if (!s->packet_fifo) {
107         ret = AVERROR(ENOMEM);
108         goto fail;
109     }
110
111     return 0;
112 fail:
113     qsv_decode_close(avctx);
114     return ret;
115 }
116
117 static int qsv_init_bsf(AVCodecContext *avctx, QSVH2645Context *s)
118 {
119     const char *filter_name = avctx->codec_id == AV_CODEC_ID_HEVC ?
120                               "hevc_mp4toannexb" : "h264_mp4toannexb";
121     const AVBitStreamFilter *filter;
122     int ret;
123
124     if (s->bsf)
125         return 0;
126
127     filter = av_bsf_get_by_name(filter_name);
128     if (!filter)
129         return AVERROR_BUG;
130
131     ret = av_bsf_alloc(filter, &s->bsf);
132     if (ret < 0)
133         return ret;
134
135     ret = avcodec_parameters_from_context(s->bsf->par_in, avctx);
136     if (ret < 0)
137         return ret;
138
139     s->bsf->time_base_in = avctx->time_base;
140
141     ret = av_bsf_init(s->bsf);
142     if (ret < 0)
143         return ret;
144
145     return ret;
146 }
147
148 static int qsv_decode_frame(AVCodecContext *avctx, void *data,
149                             int *got_frame, AVPacket *avpkt)
150 {
151     QSVH2645Context *s = avctx->priv_data;
152     AVFrame *frame    = data;
153     int ret;
154
155     /* make sure the bitstream filter is initialized */
156     ret = qsv_init_bsf(avctx, s);
157     if (ret < 0)
158         return ret;
159
160     /* buffer the input packet */
161     if (avpkt->size) {
162         AVPacket input_ref = { 0 };
163
164         if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
165             ret = av_fifo_realloc2(s->packet_fifo,
166                                    av_fifo_size(s->packet_fifo) + sizeof(input_ref));
167             if (ret < 0)
168                 return ret;
169         }
170
171         ret = av_packet_ref(&input_ref, avpkt);
172         if (ret < 0)
173             return ret;
174         av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
175     }
176
177     /* process buffered data */
178     while (!*got_frame) {
179         /* prepare the input data -- convert to Annex B if needed */
180         if (s->pkt_filtered.size <= 0) {
181             AVPacket input_ref;
182
183             /* no more data */
184             if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
185                 return avpkt->size ? avpkt->size : ff_qsv_process_data(avctx, &s->qsv, frame, got_frame, avpkt);
186
187             av_packet_unref(&s->pkt_filtered);
188
189             av_fifo_generic_read(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
190             ret = av_bsf_send_packet(s->bsf, &input_ref);
191             if (ret < 0) {
192                 av_packet_unref(&input_ref);
193                 return ret;
194             }
195
196             ret = av_bsf_receive_packet(s->bsf, &s->pkt_filtered);
197             if (ret < 0)
198                 av_packet_move_ref(&s->pkt_filtered, &input_ref);
199             else
200                 av_packet_unref(&input_ref);
201         }
202
203         ret = ff_qsv_process_data(avctx, &s->qsv, frame, got_frame, &s->pkt_filtered);
204         if (ret < 0)
205             return ret;
206
207         s->pkt_filtered.size -= ret;
208         s->pkt_filtered.data += ret;
209     }
210
211     return avpkt->size;
212 }
213
214 static void qsv_decode_flush(AVCodecContext *avctx)
215 {
216     QSVH2645Context *s = avctx->priv_data;
217
218     qsv_clear_buffers(s);
219     ff_qsv_decode_flush(avctx, &s->qsv);
220 }
221
222 #define OFFSET(x) offsetof(QSVH2645Context, x)
223 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
224
225 #if CONFIG_HEVC_QSV_DECODER
226 AVHWAccel ff_hevc_qsv_hwaccel = {
227     .name           = "hevc_qsv",
228     .type           = AVMEDIA_TYPE_VIDEO,
229     .id             = AV_CODEC_ID_HEVC,
230     .pix_fmt        = AV_PIX_FMT_QSV,
231 };
232
233 static const AVOption hevc_options[] = {
234     { "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 },
235
236     { "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" },
237     { "none",     NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_NONE },    0, 0, VD, "load_plugin" },
238     { "hevc_sw",  NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_SW }, 0, 0, VD, "load_plugin" },
239
240     { "load_plugins", "A :-separate list of hexadecimal plugin UIDs to load in an internal session",
241         OFFSET(qsv.load_plugins), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
242     { NULL },
243 };
244
245 static const AVClass hevc_class = {
246     .class_name = "hevc_qsv",
247     .item_name  = av_default_item_name,
248     .option     = hevc_options,
249     .version    = LIBAVUTIL_VERSION_INT,
250 };
251
252 AVCodec ff_hevc_qsv_decoder = {
253     .name           = "hevc_qsv",
254     .long_name      = NULL_IF_CONFIG_SMALL("HEVC (Intel Quick Sync Video acceleration)"),
255     .priv_data_size = sizeof(QSVH2645Context),
256     .type           = AVMEDIA_TYPE_VIDEO,
257     .id             = AV_CODEC_ID_HEVC,
258     .init           = qsv_decode_init,
259     .decode         = qsv_decode_frame,
260     .flush          = qsv_decode_flush,
261     .close          = qsv_decode_close,
262     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
263     .priv_class     = &hevc_class,
264     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
265                                                     AV_PIX_FMT_QSV,
266                                                     AV_PIX_FMT_NONE },
267 };
268 #endif
269
270 #if CONFIG_H264_QSV_DECODER
271 AVHWAccel ff_h264_qsv_hwaccel = {
272     .name           = "h264_qsv",
273     .type           = AVMEDIA_TYPE_VIDEO,
274     .id             = AV_CODEC_ID_H264,
275     .pix_fmt        = AV_PIX_FMT_QSV,
276 };
277
278 static const AVOption options[] = {
279     { "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 },
280     { NULL },
281 };
282
283 static const AVClass class = {
284     .class_name = "h264_qsv",
285     .item_name  = av_default_item_name,
286     .option     = options,
287     .version    = LIBAVUTIL_VERSION_INT,
288 };
289
290 AVCodec ff_h264_qsv_decoder = {
291     .name           = "h264_qsv",
292     .long_name      = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration)"),
293     .priv_data_size = sizeof(QSVH2645Context),
294     .type           = AVMEDIA_TYPE_VIDEO,
295     .id             = AV_CODEC_ID_H264,
296     .init           = qsv_decode_init,
297     .decode         = qsv_decode_frame,
298     .flush          = qsv_decode_flush,
299     .close          = qsv_decode_close,
300     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
301     .priv_class     = &class,
302     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
303                                                     AV_PIX_FMT_QSV,
304                                                     AV_PIX_FMT_NONE },
305 };
306 #endif