2 * Intel MediaSDK QSV based H.264 decoder
4 * copyright (c) 2013 Luca Barbato
5 * copyright (c) 2015 Anton Khirnov
7 * This file is part of FFmpeg.
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.
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.
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
28 #include <mfx/mfxvideo.h>
30 #include "libavutil/common.h"
31 #include "libavutil/fifo.h"
32 #include "libavutil/opt.h"
38 typedef struct QSVH264Context {
42 // the filter for converting to Annex B
43 AVBitStreamFilterContext *bsf;
45 AVFifoBuffer *packet_fifo;
48 AVPacket pkt_filtered;
49 uint8_t *filtered_data;
52 static void qsv_clear_buffers(QSVH264Context *s)
55 while (av_fifo_size(s->packet_fifo) >= sizeof(pkt)) {
56 av_fifo_generic_read(s->packet_fifo, &pkt, sizeof(pkt), NULL);
57 av_packet_unref(&pkt);
60 if (s->filtered_data != s->input_ref.data)
61 av_freep(&s->filtered_data);
62 s->filtered_data = NULL;
63 av_packet_unref(&s->input_ref);
66 static av_cold int qsv_decode_close(AVCodecContext *avctx)
68 QSVH264Context *s = avctx->priv_data;
70 ff_qsv_decode_close(&s->qsv);
74 av_fifo_free(s->packet_fifo);
76 av_bitstream_filter_close(s->bsf);
81 static av_cold int qsv_decode_init(AVCodecContext *avctx)
83 QSVH264Context *s = avctx->priv_data;
86 s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
87 if (!s->packet_fifo) {
88 ret = AVERROR(ENOMEM);
92 s->bsf = av_bitstream_filter_init("h264_mp4toannexb");
94 ret = AVERROR(ENOMEM);
100 qsv_decode_close(avctx);
104 static int qsv_process_data(AVCodecContext *avctx, AVFrame *frame,
105 int *got_frame, AVPacket *pkt)
107 QSVH264Context *s = avctx->priv_data;
110 if (!s->qsv.session || AV_PIX_FMT_NONE==avctx->pix_fmt) {
111 ret = ff_qsv_decode_init(avctx, &s->qsv, pkt);
112 /* consume packet without a header */
113 if (AVERROR(EAGAIN)==ret)
119 return ff_qsv_decode(avctx, &s->qsv, frame, got_frame, pkt);
122 static int qsv_decode_frame(AVCodecContext *avctx, void *data,
123 int *got_frame, AVPacket *avpkt)
125 QSVH264Context *s = avctx->priv_data;
126 AVFrame *frame = data;
129 /* buffer the input packet */
131 AVPacket input_ref = { 0 };
133 if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
134 ret = av_fifo_realloc2(s->packet_fifo,
135 av_fifo_size(s->packet_fifo) + sizeof(input_ref));
140 ret = av_packet_ref(&input_ref, avpkt);
143 av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
146 /* process buffered data */
147 while (!*got_frame) {
148 /* prepare the input data -- convert to Annex B if needed */
149 if (s->pkt_filtered.size <= 0) {
153 if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
154 return avpkt->size ? avpkt->size : ff_qsv_decode(avctx, &s->qsv, frame, got_frame, avpkt);
156 if (s->filtered_data != s->input_ref.data)
157 av_freep(&s->filtered_data);
158 s->filtered_data = NULL;
159 av_packet_unref(&s->input_ref);
161 av_fifo_generic_read(s->packet_fifo, &s->input_ref, sizeof(s->input_ref), NULL);
162 ret = av_bitstream_filter_filter(s->bsf, avctx, NULL,
163 &s->filtered_data, &size,
164 s->input_ref.data, s->input_ref.size, 0);
166 s->filtered_data = s->input_ref.data;
167 size = s->input_ref.size;
169 s->pkt_filtered = s->input_ref;
170 s->pkt_filtered.data = s->filtered_data;
171 s->pkt_filtered.size = size;
174 ret = qsv_process_data(avctx, frame, got_frame, &s->pkt_filtered);
178 s->pkt_filtered.size -= ret;
179 s->pkt_filtered.data += ret;
185 static void qsv_decode_flush(AVCodecContext *avctx)
187 QSVH264Context *s = avctx->priv_data;
189 qsv_clear_buffers(s);
192 AVHWAccel ff_h264_qsv_hwaccel = {
194 .type = AVMEDIA_TYPE_VIDEO,
195 .id = AV_CODEC_ID_H264,
196 .pix_fmt = AV_PIX_FMT_QSV,
199 #define OFFSET(x) offsetof(QSVH264Context, x)
200 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
201 static const AVOption options[] = {
202 { "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 },
206 static const AVClass class = {
207 .class_name = "h264_qsv",
208 .item_name = av_default_item_name,
210 .version = LIBAVUTIL_VERSION_INT,
213 AVCodec ff_h264_qsv_decoder = {
215 .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration)"),
216 .priv_data_size = sizeof(QSVH264Context),
217 .type = AVMEDIA_TYPE_VIDEO,
218 .id = AV_CODEC_ID_H264,
219 .init = qsv_decode_init,
220 .decode = qsv_decode_frame,
221 .flush = qsv_decode_flush,
222 .close = qsv_decode_close,
223 .capabilities = CODEC_CAP_DELAY,
224 .priv_class = &class,