FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
qsvdec_h2645.c
Go to the documentation of this file.
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 FFmpeg.
8  *
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.
13  *
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.
18  *
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
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 "qsvdec.h"
37 
38 enum LoadPlugin {
41 };
42 
43 typedef struct QSVH2645Context {
44  AVClass *class;
46 
48 
49  // the filter for converting to Annex B
51 
53 
55 {
56  QSVH2645Context *s = avctx->priv_data;
57 
59 
61 
62  return 0;
63 }
64 
66 {
67  QSVH2645Context *s = avctx->priv_data;
68  int ret;
69 
70  if (avctx->codec_id == AV_CODEC_ID_HEVC && s->load_plugin != LOAD_PLUGIN_NONE) {
71  static const char *uid_hevcenc_sw = "15dd936825ad475ea34e35f3f54217a6";
72 
73  if (s->qsv.load_plugins[0]) {
74  av_log(avctx, AV_LOG_WARNING,
75  "load_plugins is not empty, but load_plugin is not set to 'none'."
76  "The load_plugin value will be ignored.\n");
77  } else {
79  s->qsv.load_plugins = av_strdup(uid_hevcenc_sw);
80  if (!s->qsv.load_plugins)
81  return AVERROR(ENOMEM);
82  }
83  }
84 
85  if (avctx->codec_id == AV_CODEC_ID_H264) {
86  s->bsf = av_bitstream_filter_init("h264_mp4toannexb");
87  //regarding ticks_per_frame description, should be 2 for h.264:
88  avctx->ticks_per_frame = 2;
89  } else
90  s->bsf = av_bitstream_filter_init("hevc_mp4toannexb");
91  if (!s->bsf) {
92  ret = AVERROR(ENOMEM);
93  goto fail;
94  }
95 
96  return 0;
97 fail:
98  qsv_decode_close(avctx);
99  return ret;
100 }
101 
102 static int qsv_decode_frame(AVCodecContext *avctx, void *data,
103  int *got_frame, AVPacket *avpkt)
104 {
105  QSVH2645Context *s = avctx->priv_data;
106  AVFrame *frame = data;
107  int ret;
108  uint8_t *p_filtered = NULL;
109  int n_filtered = NULL;
110  AVPacket pkt_filtered = { 0 };
111 
112  if (avpkt->size) {
113  if (avpkt->size > 3 && !avpkt->data[0] &&
114  !avpkt->data[1] && !avpkt->data[2] && 1==avpkt->data[3]) {
115  /* we already have annex-b prefix */
116  return ff_qsv_decode(avctx, &s->qsv, frame, got_frame, avpkt);
117 
118  } else {
119  /* no annex-b prefix. try to restore: */
120  ret = av_bitstream_filter_filter(s->bsf, avctx, "private_spspps_buf",
121  &p_filtered, &n_filtered,
122  avpkt->data, avpkt->size, 0);
123  if (ret>=0) {
124  pkt_filtered.pts = avpkt->pts;
125  pkt_filtered.data = p_filtered;
126  pkt_filtered.size = n_filtered;
127 
128  ret = ff_qsv_decode(avctx, &s->qsv, frame, got_frame, &pkt_filtered);
129 
130  if (p_filtered != avpkt->data)
131  av_free(p_filtered);
132  return ret > 0 ? avpkt->size : ret;
133  }
134  }
135  }
136 
137  return ff_qsv_decode(avctx, &s->qsv, frame, got_frame, avpkt);
138 }
139 
140 static void qsv_decode_flush(AVCodecContext *avctx)
141 {
142  QSVH2645Context *s = avctx->priv_data;
143  ff_qsv_decode_reset(avctx, &s->qsv);
144 }
145 
146 #define OFFSET(x) offsetof(QSVH2645Context, x)
147 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
148 
149 #if CONFIG_HEVC_QSV_DECODER
150 AVHWAccel ff_hevc_qsv_hwaccel = {
151  .name = "hevc_qsv",
152  .type = AVMEDIA_TYPE_VIDEO,
153  .id = AV_CODEC_ID_HEVC,
154  .pix_fmt = AV_PIX_FMT_QSV,
155 };
156 
157 static const AVOption hevc_options[] = {
158  { "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 },
159 
160  { "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" },
161  { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_NONE }, 0, 0, VD, "load_plugin" },
162  { "hevc_sw", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LOAD_PLUGIN_HEVC_SW }, 0, 0, VD, "load_plugin" },
163 
164  { "load_plugins", "A :-separate list of hexadecimal plugin UIDs to load in an internal session",
165  OFFSET(qsv.load_plugins), AV_OPT_TYPE_STRING, { .str = "" }, 0, 0, VD },
166  { NULL },
167 };
168 
169 static const AVClass hevc_class = {
170  .class_name = "hevc_qsv",
171  .item_name = av_default_item_name,
172  .option = hevc_options,
173  .version = LIBAVUTIL_VERSION_INT,
174 };
175 
176 AVCodec ff_hevc_qsv_decoder = {
177  .name = "hevc_qsv",
178  .long_name = NULL_IF_CONFIG_SMALL("HEVC (Intel Quick Sync Video acceleration)"),
179  .priv_data_size = sizeof(QSVH2645Context),
181  .id = AV_CODEC_ID_HEVC,
185  .close = qsv_decode_close,
186  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
187  .priv_class = &hevc_class,
188  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
190  AV_PIX_FMT_NONE },
191 };
192 #endif
193 
194 #if CONFIG_H264_QSV_DECODER
195 AVHWAccel ff_h264_qsv_hwaccel = {
196  .name = "h264_qsv",
197  .type = AVMEDIA_TYPE_VIDEO,
198  .id = AV_CODEC_ID_H264,
199  .pix_fmt = AV_PIX_FMT_QSV,
200 };
201 
202 static const AVOption options[] = {
203  { "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 },
204  { NULL },
205 };
206 
207 static const AVClass class = {
208  .class_name = "h264_qsv",
209  .item_name = av_default_item_name,
210  .option = options,
211  .version = LIBAVUTIL_VERSION_INT,
212 };
213 
214 AVCodec ff_h264_qsv_decoder = {
215  .name = "h264_qsv",
216  .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration)"),
217  .priv_data_size = sizeof(QSVH2645Context),
219  .id = AV_CODEC_ID_H264,
223  .close = qsv_decode_close,
224  .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
225  .priv_class = &class,
226  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_NV12,
228  AV_PIX_FMT_NONE },
229 };
230 #endif
#define NULL
Definition: coverity.c:32
AVBitStreamFilterContext * bsf
Definition: qsvdec_h2645.c:50
const char * s
Definition: avisynth_c.h:631
int ff_qsv_decode(AVCodecContext *avctx, QSVContext *q, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: qsvdec.c:493
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
attribute_deprecated int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
Filter bitstream.
int size
Definition: avcodec.h:1581
static av_cold int qsv_decode_close(AVCodecContext *avctx)
Definition: qsvdec_h2645.c:54
AVCodec.
Definition: avcodec.h:3542
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:981
void ff_qsv_decode_reset(AVCodecContext *avctx, QSVContext *q)
Definition: qsvdec.c:549
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
int ff_qsv_decode_close(QSVContext *q)
Definition: qsvdec.c:592
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1580
#define VD
Definition: qsvdec_h2645.c:147
const OptionDef options[]
Definition: ffserver.c:3969
#define av_log(a,...)
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
attribute_deprecated AVBitStreamFilterContext * av_bitstream_filter_init(const char *name)
Create and initialize a bitstream filter context given a bitstream filter name.
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:90
const char * name
Name of the codec implementation.
Definition: avcodec.h:3549
#define fail()
Definition: checkasm.h:81
#define ASYNC_DEPTH_DEFAULT
Definition: qsv_internal.h:48
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3668
static void qsv_decode_flush(AVCodecContext *avctx)
Definition: qsvdec_h2645.c:140
attribute_deprecated void av_bitstream_filter_close(AVBitStreamFilterContext *bsf)
Release bitstream filter context.
#define OFFSET(x)
Definition: qsvdec_h2645.c:146
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1795
QSVContext qsv
Definition: qsvdec_h2645.c:45
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1666
char * av_strdup(const char *s)
Duplicate the string s.
Definition: mem.c:267
main external API structure.
Definition: avcodec.h:1649
a very simple circular buffer FIFO implementation
GLint GLenum type
Definition: opengl_enc.c:105
Describe the class of an AVClass context structure.
Definition: log.h:67
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
Definition: pixfmt.h:235
static int qsv_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: qsvdec_h2645.c:102
char * load_plugins
Definition: qsvdec.h:73
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avpkt)
Definition: ccaption_dec.c:722
static av_cold int qsv_decode_init(AVCodecContext *avctx)
Definition: qsvdec_h2645.c:65
common internal api header.
common internal and external API header
void * priv_data
Definition: avcodec.h:1691
#define av_free(p)
#define av_freep(p)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1557
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:956
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1573
LoadPlugin
Definition: qsvdec_h2645.c:38